Fix Prevent defects in ckmc APIs
[platform/core/security/key-manager.git] / src / manager / client-capi / ckmc-manager.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  *
16  *
17  * @file        ckmc-control.h
18  * @author      Yuseok Jeon(yuseok.jeon@samsung.com)
19  * @version     1.0
20  * @brief       provides conversion methods to C from C++ for key-manager control functions.
21  */
22
23 #include <ckm/ckm-type.h>
24 #include <ckm/ckm-manager.h>
25 #include <ckmc/ckmc-type.h>
26 #include <ckmc/ckmc-manager.h>
27 #include <ckmc/ckmc-error.h>
28 #include <ckmc-type-converter.h>
29 #include <iostream>
30 #include <string.h>
31
32 CKM::Password _tostring(const char *str)
33 {
34     if(str == NULL)
35         return CKM::Password();
36     return CKM::Password(str);
37 }
38
39 CKM::CertificateShPtr _toCkmCertificate(const ckmc_cert_s *cert)
40 {
41     CKM::RawBuffer buffer(cert->raw_cert, cert->raw_cert + cert->cert_size);
42     CKM::DataFormat dataFormat = static_cast<CKM::DataFormat>(static_cast<int>(cert->data_format));
43     return CKM::Certificate::create(buffer, dataFormat);
44 }
45
46 ckmc_cert_list_s *_toNewCkmCertList(CKM::CertificateShPtrVector &certVector)
47 {
48     int ret;
49     ckmc_cert_list_s *start = NULL;
50     ckmc_cert_list_s *plist = NULL;
51     CKM::CertificateShPtrVector::iterator it;
52     for(it = certVector.begin(); it != certVector.end(); it++) {
53         CKM::RawBuffer rawBuffer = (*it)->getDER();
54         unsigned char *rawCert = static_cast<unsigned char *>(malloc(rawBuffer.size()));
55         memcpy(rawCert, rawBuffer.data(), rawBuffer.size());
56         ckmc_cert_s *pcert;
57         ret = ckmc_cert_new(rawCert, rawBuffer.size(), CKMC_FORM_DER, &pcert);
58         free(rawCert);
59         if(pcert == NULL) {
60             ckmc_cert_list_all_free(start);
61             return NULL;
62         }
63         if(plist == NULL) {
64             ret = ckmc_cert_list_new(pcert, &plist);
65             start = plist; // save the pointer of the first element
66         }else {
67             ret = ckmc_cert_list_add(plist, pcert, &plist);
68         }
69         if(ret != CKMC_ERROR_NONE) {
70             ckmc_cert_list_all_free(start);
71             return NULL;
72         }
73     }
74     return start;
75 }
76
77 KEY_MANAGER_CAPI
78 int ckmc_save_key(const char *alias, const ckmc_key_s key, const ckmc_policy_s policy)
79 {
80     CKM::ManagerShPtr mgr = CKM::Manager::create();
81
82     if(alias == NULL) {
83         return CKMC_ERROR_INVALID_PARAMETER;
84     }
85     CKM::Alias ckmAlias(alias);
86
87     if(key.raw_key == NULL || key.key_size <= 0) {
88             return CKMC_ERROR_INVALID_PARAMETER;
89     }
90     CKM::RawBuffer buffer(key.raw_key, key.raw_key + key.key_size);
91     CKM::KeyShPtr ckmKey = CKM::Key::create(buffer, _tostring(key.password));
92
93     if(ckmKey.get() == NULL) {
94         return CKMC_ERROR_INVALID_FORMAT;
95     }
96
97     CKM::Policy storePolicy(_tostring(policy.password), policy.extractable, policy.restricted);
98
99     int ret =  mgr->saveKey(ckmAlias, ckmKey, storePolicy);
100     return to_ckmc_error(ret);
101 }
102
103
104 KEY_MANAGER_CAPI
105 int ckmc_remove_key(const char *alias)
106 {
107     CKM::ManagerShPtr mgr = CKM::Manager::create();
108
109     if(alias == NULL) {
110         return CKMC_ERROR_INVALID_PARAMETER;
111     }
112     CKM::Alias ckmAlias(alias);
113
114     int ret =  mgr->removeKey(ckmAlias);
115     return to_ckmc_error(ret);
116 }
117
118 KEY_MANAGER_CAPI
119 int ckmc_get_key(const char *alias, const char *password, ckmc_key_s **key)
120 {
121     int ret;
122     CKM::KeyShPtr ckmKey;
123
124     if(alias == NULL || key == NULL) {
125         return CKMC_ERROR_INVALID_PARAMETER;
126     }
127     CKM::Alias ckmAlias(alias);
128
129     CKM::ManagerShPtr mgr = CKM::Manager::create();
130     if( (ret = mgr->getKey(ckmAlias, _tostring(password), ckmKey)) != CKM_API_SUCCESS) {
131         return to_ckmc_error(ret);
132     }
133
134     unsigned char *rawKey = reinterpret_cast<unsigned char*>(ckmKey->getDER().data());
135     ckmc_key_type_e keyType = static_cast<ckmc_key_type_e>(static_cast<int>(ckmKey->getType()));
136
137     ret = ckmc_key_new( rawKey, ckmKey->getDER().size(), keyType, NULL, key);
138
139     return to_ckmc_error(ret);
140 }
141
142 KEY_MANAGER_CAPI
143 int ckmc_get_key_alias_list(ckmc_alias_list_s** alias_list)
144 {
145     int ret;
146
147     if (alias_list == NULL) {
148         return CKMC_ERROR_INVALID_PARAMETER;
149     }
150
151     CKM::AliasVector aliasVector;
152     CKM::ManagerShPtr mgr = CKM::Manager::create();
153
154     if ((ret = mgr->getKeyAliasVector(aliasVector)) != CKM_API_SUCCESS) {
155         return to_ckmc_error(ret);
156     }
157
158     ckmc_alias_list_s *plist = NULL;
159
160     for (auto it = aliasVector.begin(); it != aliasVector.end(); it++) {
161         char *alias = strndup(it->c_str(), it->size());
162
163         if (plist == NULL) { // first
164             ret = ckmc_alias_list_new(alias, &plist);
165             *alias_list = plist; // save the pointer of the first element
166         } else {
167             ret = ckmc_alias_list_add(plist, alias, &plist);
168         }
169
170         if (ret != CKMC_ERROR_NONE) {
171             free(alias);
172             ckmc_alias_list_all_free(*alias_list);
173             return ret;
174         }
175     }
176
177     return CKMC_ERROR_NONE;
178 }
179
180 KEY_MANAGER_CAPI
181 int ckmc_save_cert(const char *alias, const ckmc_cert_s cert, const ckmc_policy_s policy)
182 {
183     if(alias == NULL) {
184         return CKMC_ERROR_INVALID_PARAMETER;
185     }
186     CKM::Alias ckmAlias(alias);
187
188     if(cert.raw_cert == NULL || cert.cert_size <= 0) {
189             return CKMC_ERROR_INVALID_PARAMETER;
190     }
191     CKM::CertificateShPtr ckmCert = _toCkmCertificate(&cert);
192     if(ckmCert.get() == NULL) {
193         return CKMC_ERROR_INVALID_FORMAT;
194     }
195
196     CKM::Policy storePolicy(_tostring(policy.password), policy.extractable, policy.restricted);
197
198     CKM::ManagerShPtr mgr = CKM::Manager::create();
199     int ret = mgr->saveCertificate(ckmAlias, ckmCert, storePolicy);
200
201     return to_ckmc_error(ret);
202 }
203
204 KEY_MANAGER_CAPI
205 int ckmc_remove_cert(const char *alias)
206 {
207     if(alias == NULL) {
208         return CKMC_ERROR_INVALID_PARAMETER;
209     }
210     CKM::Alias ckmAlias(alias);
211
212     CKM::ManagerShPtr mgr = CKM::Manager::create();
213     int ret = mgr->removeCertificate(ckmAlias);
214
215     return to_ckmc_error(ret);
216 }
217
218 KEY_MANAGER_CAPI
219 int ckmc_get_cert(const char *alias, const char *password, ckmc_cert_s **cert)
220 {
221     CKM::CertificateShPtr ckmCert;
222     int ret;
223
224     if(alias == NULL || cert == NULL) {
225         return CKMC_ERROR_INVALID_PARAMETER;
226     }
227     CKM::Alias ckmAlias(alias);
228
229     CKM::ManagerShPtr mgr = CKM::Manager::create();
230     if( (ret = mgr->getCertificate(ckmAlias, _tostring(password), ckmCert)) != CKM_API_SUCCESS) {
231         return to_ckmc_error(ret);
232     }
233
234     unsigned char *rawCert = reinterpret_cast<unsigned char*>(ckmCert->getDER().data());
235     ret = ckmc_cert_new( rawCert, ckmCert->getDER().size(), CKMC_FORM_DER, cert);
236
237     return ret;
238 }
239
240 KEY_MANAGER_CAPI
241 int ckmc_get_cert_alias_list(ckmc_alias_list_s** alias_list) {
242     int ret;
243
244     if (alias_list == NULL) {
245         return CKMC_ERROR_INVALID_PARAMETER;
246     }
247
248     *alias_list = NULL;
249
250     CKM::AliasVector aliasVector;
251     CKM::ManagerShPtr mgr = CKM::Manager::create();
252     if ((ret = mgr->getCertificateAliasVector(aliasVector)) != CKM_API_SUCCESS) {
253         return to_ckmc_error(ret);
254     }
255
256     ckmc_alias_list_s *plist = NULL;
257
258     for (auto it = aliasVector.begin(); it != aliasVector.end(); it++) {
259         char *alias = strndup(it->c_str(), it->size());
260
261         if (plist == NULL) { // first
262             ret  = ckmc_alias_list_new(alias, &plist);
263             *alias_list = plist; // save the pointer of the first element
264         } else {
265             ret = ckmc_alias_list_add(plist, alias, &plist);
266         }
267
268         if (ret != CKMC_ERROR_NONE) {
269             free(alias);
270             ckmc_alias_list_all_free(*alias_list);
271             return ret;
272         }
273     }
274
275     return CKMC_ERROR_NONE;
276 }
277
278 KEY_MANAGER_CAPI
279 int ckmc_save_data(const char *alias, ckmc_raw_buffer_s data, const ckmc_policy_s policy)
280 {
281     if(alias == NULL) {
282         return CKMC_ERROR_INVALID_PARAMETER;
283     }
284     CKM::Alias ckmAlias(alias);
285
286     if(data.data == NULL || data.size <= 0) {
287             return CKMC_ERROR_INVALID_PARAMETER;
288     }
289     CKM::RawBuffer buffer(data.data, data.data + data.size);
290
291     CKM::Policy storePolicy(_tostring(policy.password), policy.extractable, policy.restricted);
292
293     CKM::ManagerShPtr mgr = CKM::Manager::create();
294     int ret = mgr->saveData(ckmAlias, buffer, storePolicy);
295
296     return to_ckmc_error(ret);
297 }
298
299 KEY_MANAGER_CAPI
300 int ckmc_remove_data(const char *alias)
301 {
302     if(alias == NULL) {
303         return CKMC_ERROR_INVALID_PARAMETER;
304     }
305     CKM::Alias ckmAlias(alias);
306
307     CKM::ManagerShPtr mgr = CKM::Manager::create();
308     int ret = mgr->removeData(ckmAlias);
309     return to_ckmc_error(ret);
310 }
311
312 KEY_MANAGER_CAPI
313 int ckmc_get_data(const char *alias, const char *password, ckmc_raw_buffer_s **data)
314 {
315     CKM::RawBuffer ckmBuff;
316     int ret;
317
318     if(alias == NULL || data == NULL) {
319         return CKMC_ERROR_INVALID_PARAMETER;
320     }
321     CKM::Alias ckmAlias(alias);
322
323     CKM::ManagerShPtr mgr = CKM::Manager::create();
324     if( (ret = mgr->getData(ckmAlias, _tostring(password), ckmBuff)) != CKM_API_SUCCESS) {
325         return to_ckmc_error(ret);
326     }
327
328     unsigned char *rawBuff = reinterpret_cast<unsigned char*>(ckmBuff.data());
329     ret = ckmc_buffer_new(rawBuff, ckmBuff.size(), data);
330
331     return ret;
332 }
333
334 KEY_MANAGER_CAPI
335 int ckmc_get_data_alias_list(ckmc_alias_list_s** alias_list){
336     int ret;
337
338     if(alias_list == NULL) {
339         return CKMC_ERROR_INVALID_PARAMETER;
340     }
341
342     *alias_list = NULL;
343
344     CKM::AliasVector aliasVector;
345     CKM::ManagerShPtr mgr = CKM::Manager::create();
346     if( (ret = mgr->getDataAliasVector(aliasVector)) != CKM_API_SUCCESS) {
347         return to_ckmc_error(ret);
348     }
349
350     ckmc_alias_list_s *plist = NULL;
351
352     for(auto it = aliasVector.begin(); it != aliasVector.end(); it++) {
353         char *alias = strndup(it->c_str(), it->size());
354
355         if (plist == NULL) { // first
356             ret = ckmc_alias_list_new(alias, &plist);
357             *alias_list = plist; // save the pointer of the first element
358         } else {
359             ret = ckmc_alias_list_add(plist, alias, &plist);
360         }
361
362         if (ret != CKMC_ERROR_NONE) {
363             free(alias);
364             ckmc_alias_list_all_free(*alias_list);
365             return ret;
366         }
367     }
368
369     return CKMC_ERROR_NONE;
370 }
371
372 KEY_MANAGER_CAPI
373 int ckmc_create_key_pair_rsa(const size_t size,
374                             const char *private_key_alias,
375                             const char *public_key_alias,
376                             const ckmc_policy_s policy_private_key,
377                             const ckmc_policy_s policy_public_key)
378 {
379     int ret;
380     CKM::ManagerShPtr mgr = CKM::Manager::create();
381
382     if(private_key_alias == NULL || public_key_alias == NULL) {
383         return CKMC_ERROR_INVALID_PARAMETER;
384     }
385
386     CKM::Alias ckmPrivakeKeyAlias(private_key_alias);
387     CKM::Alias ckmPublicKeyAlias(public_key_alias);
388     CKM::Policy ckmPrivateKeyPolicy(_tostring(policy_private_key.password), policy_private_key.extractable, policy_private_key.restricted);
389     CKM::Policy ckmPublicKeyPolicy(_tostring(policy_public_key.password), policy_public_key.extractable, policy_public_key.restricted);
390
391     ret = mgr->createKeyPairRSA(size, ckmPrivakeKeyAlias, ckmPublicKeyAlias, ckmPrivateKeyPolicy, ckmPublicKeyPolicy);
392     return to_ckmc_error(ret);
393 }
394
395 KEY_MANAGER_CAPI
396 int ckmc_create_key_pair_ecdsa(const ckmc_ec_type_e type,
397                             const char *private_key_alias,
398                             const char *public_key_alias,
399                             const ckmc_policy_s policy_private_key,
400                             const ckmc_policy_s policy_public_key)
401 {
402     CKM::ManagerShPtr mgr = CKM::Manager::create();
403
404     if(private_key_alias == NULL || public_key_alias == NULL) {
405         return CKMC_ERROR_INVALID_PARAMETER;
406     }
407
408     CKM::ElipticCurve ckmType = static_cast<CKM::ElipticCurve>(static_cast<int>(type));
409     CKM::Alias ckmPrivakeKeyAlias(private_key_alias);
410     CKM::Alias ckmPublicKeyAlias(public_key_alias);
411     CKM::Policy ckmPrivateKeyPolicy(_tostring(policy_private_key.password), policy_private_key.extractable, policy_private_key.restricted);
412     CKM::Policy ckmPublicKeyPolicy(_tostring(policy_public_key.password), policy_public_key.extractable, policy_public_key.restricted);
413
414     int ret = mgr->createKeyPairECDSA(ckmType, ckmPrivakeKeyAlias, ckmPublicKeyAlias, ckmPrivateKeyPolicy, ckmPublicKeyPolicy);
415     return to_ckmc_error(ret);
416 }
417
418 KEY_MANAGER_CAPI
419 int ckmc_create_signature(const char *private_key_alias,
420                             const char *password,
421                             const ckmc_raw_buffer_s message,
422                             const ckmc_hash_algo_e hash,
423                             const ckmc_rsa_padding_algo_e padding,
424                             ckmc_raw_buffer_s **signature)
425 {
426     int ret;
427     CKM::ManagerShPtr mgr = CKM::Manager::create();
428     CKM::RawBuffer ckmSignature;
429
430     if(private_key_alias == NULL || signature == NULL) {
431         return CKMC_ERROR_INVALID_PARAMETER;
432     }
433
434     CKM::Alias ckmPrivakeKeyAlias(private_key_alias);
435     CKM::RawBuffer ckmMessage(message.data, message.data + message.size);
436     CKM::HashAlgorithm ckmHashAlgo = static_cast<CKM::HashAlgorithm>(static_cast<int>(hash));
437     CKM::RSAPaddingAlgorithm ckmPadding = static_cast<CKM::RSAPaddingAlgorithm>(static_cast<int>(padding));
438
439     if( (ret = mgr->createSignature(
440             ckmPrivakeKeyAlias,
441             _tostring(password),
442             ckmMessage,
443             ckmHashAlgo,
444             ckmPadding,
445             ckmSignature)) != CKM_API_SUCCESS) {
446         return to_ckmc_error(ret);
447     }
448
449     unsigned char *rawBuff = reinterpret_cast<unsigned char*>(ckmSignature.data());
450     ret = ckmc_buffer_new( rawBuff, ckmSignature.size(), signature);
451
452     return ret;
453 }
454
455 KEY_MANAGER_CAPI
456 int ckmc_verify_signature(const char *public_key_alias,
457                             const char *password,
458                             const ckmc_raw_buffer_s message,
459                             const ckmc_raw_buffer_s signature,
460                             const ckmc_hash_algo_e hash,
461                             const ckmc_rsa_padding_algo_e padding)
462 {
463     int ret;
464     CKM::ManagerShPtr mgr = CKM::Manager::create();
465
466     if(public_key_alias == NULL) {
467         return CKMC_ERROR_INVALID_PARAMETER;
468     }
469
470     CKM::Alias ckmPublicKeyAlias(public_key_alias);
471     CKM::RawBuffer ckmMessage(message.data, message.data + message.size);
472     CKM::RawBuffer ckmSignature(signature.data, signature.data + signature.size);
473     CKM::HashAlgorithm ckmHashAlgo = static_cast<CKM::HashAlgorithm>(static_cast<int>(hash));
474     CKM::RSAPaddingAlgorithm ckmPadding = static_cast<CKM::RSAPaddingAlgorithm>(static_cast<int>(padding));
475
476     if( (ret = mgr->verifySignature(
477             ckmPublicKeyAlias,
478             _tostring(password),
479             ckmMessage,
480             ckmSignature,
481             ckmHashAlgo,
482             ckmPadding)) != CKM_API_SUCCESS) {
483         return to_ckmc_error(ret);
484     }
485
486     return CKMC_ERROR_NONE;
487 }
488
489 KEY_MANAGER_CAPI
490 int ckmc_get_cert_chain(const ckmc_cert_s *cert, const ckmc_cert_list_s *untrustedcerts, ckmc_cert_list_s **cert_chain_list)
491 {
492     int ret;
493     CKM::ManagerShPtr mgr = CKM::Manager::create();
494     CKM::CertificateShPtrVector ckmCertChain;
495
496     if(cert == NULL || cert->raw_cert == NULL || cert->cert_size <= 0 || cert_chain_list == NULL) {
497         return CKMC_ERROR_INVALID_PARAMETER;
498     }
499
500     CKM::CertificateShPtr ckmCert = _toCkmCertificate(cert);
501
502     CKM::CertificateShPtrVector ckmUntrustedCerts;
503     if(untrustedcerts != NULL) {
504         ckmc_cert_list_s *current = NULL;
505         ckmc_cert_list_s *next = const_cast<ckmc_cert_list_s *>(untrustedcerts);
506         do {
507             current = next;
508             next = current->next;
509
510             if(current->cert == NULL){
511                 continue;
512             }
513
514             CKM::CertificateShPtr tmpCkmCert = _toCkmCertificate(current->cert);
515             ckmUntrustedCerts.push_back(tmpCkmCert);
516         }while(next != NULL);
517     }
518
519     ret = mgr->getCertificateChain(ckmCert, ckmUntrustedCerts, ckmCertChain);
520     if( ret != CKM_API_SUCCESS) {
521         return to_ckmc_error(ret);
522     }
523
524     *cert_chain_list = _toNewCkmCertList(ckmCertChain);
525
526     return CKMC_ERROR_NONE;
527 }
528
529 KEY_MANAGER_CAPI
530 int ckmc_get_cert_chain_with_alias(const ckmc_cert_s *cert, const ckmc_alias_list_s *untrustedcerts, ckmc_cert_list_s **cert_chain_list)
531 {
532     int ret;
533     CKM::ManagerShPtr mgr = CKM::Manager::create();
534     CKM::CertificateShPtrVector ckmCertChain;
535
536
537     if(cert == NULL || cert->raw_cert == NULL || cert->cert_size <= 0 || cert_chain_list == NULL) {
538         return CKMC_ERROR_INVALID_PARAMETER;
539     }
540
541     CKM::CertificateShPtr ckmCert = _toCkmCertificate(cert);
542     if(ckmCert.get() == NULL) {
543         return CKMC_ERROR_INVALID_FORMAT;
544     }
545
546     CKM::AliasVector ckmUntrustedAliases;
547     if(untrustedcerts != NULL) {
548         ckmc_alias_list_s *current = NULL;
549         ckmc_alias_list_s *next = const_cast<ckmc_alias_list_s *>(untrustedcerts);
550         do {
551             current = next;
552             next = current->next;
553
554             if(current->alias == NULL){
555                 return CKMC_ERROR_INVALID_PARAMETER;
556             }
557             CKM::Alias ckmAlias(current->alias);
558             ckmUntrustedAliases.push_back(ckmAlias);
559         }while(next != NULL);
560     }
561
562     if( (ret = mgr->getCertificateChain(ckmCert, ckmUntrustedAliases, ckmCertChain)) != CKM_API_SUCCESS) {
563         return to_ckmc_error(ret);
564     }
565
566     *cert_chain_list = _toNewCkmCertList(ckmCertChain);
567
568     return CKMC_ERROR_NONE;
569 }