2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * @author Yuseok Jeon(yuseok.jeon@samsung.com)
20 * @brief new and free methods for the struct of CAPI
27 #include <ckm/ckm-type.h>
28 #include <ckmc/ckmc-type.h>
29 #include <ckmc/ckmc-error.h>
30 #include <ckmc-type-converter.h>
31 #include <protocols.h>
32 #include <openssl/x509v3.h>
33 #include <openssl/pkcs12.h>
34 #include <openssl/evp.h>
35 #include <openssl/pem.h>
38 const char * const ckmc_label_alias_separator = CKM::LABEL_ALIAS_SEPARATOR;
41 int _ckmc_load_cert_from_x509(X509 *xCert, ckmc_cert_s **cert);
44 int ckmc_key_new(unsigned char *raw_key, size_t key_size, ckmc_key_type_e key_type, char *password, ckmc_key_s **ppkey)
48 if(raw_key == NULL || key_size <= 0 || ppkey == NULL) {
49 return CKMC_ERROR_INVALID_PARAMETER;
52 pkey = static_cast<ckmc_key_s*>(malloc(sizeof(ckmc_key_s)));
54 return CKMC_ERROR_OUT_OF_MEMORY;
56 pkey->raw_key = reinterpret_cast<unsigned char*>(malloc(key_size));
57 if(pkey->raw_key == NULL) {
59 return CKMC_ERROR_OUT_OF_MEMORY;
61 memcpy(pkey->raw_key, raw_key, key_size);
63 pkey->key_size = key_size;
64 pkey->key_type = key_type;
66 if(password != NULL) {
67 pkey->password = reinterpret_cast<char*>(malloc(strlen(password) +1));
68 if(pkey->password == NULL) {
71 return CKMC_ERROR_OUT_OF_MEMORY;
73 memset(pkey->password, 0, strlen(password) +1);
74 strncpy(pkey->password, password, strlen(password));
76 pkey->password = NULL;
81 return CKMC_ERROR_NONE;
85 void ckmc_key_free(ckmc_key_s *key)
90 if(key->password != NULL)
92 if(key->raw_key != NULL) {
93 memset(key->raw_key, 0, key->key_size);
101 int ckmc_buffer_new(unsigned char *data, size_t size,ckmc_raw_buffer_s **ppbuffer)
103 ckmc_raw_buffer_s *pbuff;
105 if(data == NULL || size <= 0 || ppbuffer == NULL) {
106 return CKMC_ERROR_INVALID_PARAMETER;
109 pbuff = static_cast<ckmc_raw_buffer_s*>(malloc(sizeof(ckmc_raw_buffer_s)));
111 return CKMC_ERROR_OUT_OF_MEMORY;
113 pbuff->data = reinterpret_cast<unsigned char*>(malloc(size));
114 if(pbuff->data == NULL) {
116 return CKMC_ERROR_OUT_OF_MEMORY;
118 memcpy(pbuff->data, data, size);
123 return CKMC_ERROR_NONE;
127 void ckmc_buffer_free(ckmc_raw_buffer_s *buffer)
132 if(buffer->data != NULL) {
133 memset(buffer->data, 0, buffer->size);
140 int ckmc_cert_new(unsigned char *raw_cert, size_t cert_size, ckmc_data_format_e data_format, ckmc_cert_s **ppcert)
144 if(raw_cert == NULL || cert_size <= 0 || ppcert == NULL) {
145 return CKMC_ERROR_INVALID_PARAMETER;
148 pcert = static_cast<ckmc_cert_s*>(malloc(sizeof(ckmc_cert_s)));
150 return CKMC_ERROR_OUT_OF_MEMORY;
152 pcert->raw_cert = reinterpret_cast<unsigned char*>(malloc(cert_size));
153 if(pcert->raw_cert == NULL) {
155 return CKMC_ERROR_OUT_OF_MEMORY;
157 memcpy(pcert->raw_cert, raw_cert, cert_size);
159 pcert->cert_size = cert_size;
160 pcert->data_format = data_format;
163 return CKMC_ERROR_NONE;
167 int ckmc_load_cert_from_file(const char *file_path, ckmc_cert_s **cert)
169 OpenSSL_add_all_algorithms();
171 FILE *fp = fopen(file_path, "r");
173 return CKMC_ERROR_FILE_ACCESS_DENIED;
175 if(!(pcert = d2i_X509_fp(fp, NULL))) {
176 fseek(fp, 0, SEEK_SET);
177 pcert = PEM_read_X509(fp, NULL, NULL, NULL);
181 return CKMC_ERROR_INVALID_FORMAT;
184 int ret = _ckmc_load_cert_from_x509(pcert, cert);
185 if(ret != CKMC_ERROR_NONE) {
192 int ckmc_load_from_pkcs12_file(const char *file_path, const char *passphrase, ckmc_key_s **private_key, ckmc_cert_s **ckmcert, ckmc_cert_list_s **ca_cert_list)
194 class Pkcs12Converter {
204 ckmc_key_s *retPrivateKey;
205 ckmc_cert_s *retCkmCert;
206 ckmc_cert_list_s *retCaCertList;
214 ret = CKMC_ERROR_NONE;
215 retPrivateKey = NULL;
217 retCaCertList = NULL;
229 sk_X509_pop_free(ca, X509_free);
231 if(ret != CKMC_ERROR_NONE) {
232 if(retPrivateKey != NULL){
233 ckmc_key_free(retPrivateKey);
234 retPrivateKey = NULL;
236 if(retCkmCert != NULL) {
237 ckmc_cert_free(retCkmCert);
240 if(retCaCertList != NULL) {
241 ckmc_cert_list_all_free(retCaCertList);
242 retCaCertList = NULL;
247 int parsePkcs12(const char *filePath, const char *pass) {
249 if(!(fp_in = fopen(filePath, "rb"))) {
250 return CKMC_ERROR_FILE_ACCESS_DENIED;
253 if(!(p12 = d2i_PKCS12_fp(fp_in, NULL))) {
254 return CKMC_ERROR_INVALID_FORMAT;
257 /* parse PKCS#12 certificate */
258 if((ret = PKCS12_parse(p12, pass, &pkey, &x509Cert, &ca)) != 1) {
259 return CKMC_ERROR_INVALID_FORMAT;
261 return CKMC_ERROR_NONE;
265 if( (ret =_ckmc_load_cert_from_x509(x509Cert,&retCkmCert)) != CKMC_ERROR_NONE) {
268 return CKMC_ERROR_NONE;
272 BIO *bkey = BIO_new(BIO_s_mem());
274 i2d_PrivateKey_bio(bkey, pkey);
276 CKM::RawBuffer output(8196);
277 int size = BIO_read(bkey, output.data(), output.size());
280 return CKMC_ERROR_INVALID_FORMAT;
284 int type = EVP_PKEY_type(pkey->type);
285 ckmc_key_type_e key_type = CKMC_KEY_NONE;
288 key_type = CKMC_KEY_RSA_PRIVATE;
291 key_type = CKMC_KEY_DSA_PRIVATE;
294 key_type = CKMC_KEY_ECDSA_PRIVATE;
297 if(key_type == CKMC_KEY_NONE) {
298 return CKMC_ERROR_INVALID_FORMAT;
301 char *nullPassword = NULL;
303 return ckmc_key_new(output.data(), size, key_type, nullPassword, &retPrivateKey);
306 int toCaCkmCertList() {
308 X509* popedCert = NULL;
309 ckmc_cert_s *popedCkmCert = NULL;
310 ckmc_cert_list_s *tmpCertList = NULL;
311 while((popedCert = sk_X509_pop(ca)) != NULL) {
312 if( (tmpRet =_ckmc_load_cert_from_x509(popedCert, &popedCkmCert)) != CKMC_ERROR_NONE) {
313 return CKMC_ERROR_OUT_OF_MEMORY;
315 if(tmpCertList == NULL) { // first
316 tmpRet = ckmc_cert_list_new(popedCkmCert, &tmpCertList);
317 retCaCertList = tmpCertList;
319 tmpRet = ckmc_cert_list_add(tmpCertList, popedCkmCert, &tmpCertList);
321 if(tmpRet != CKMC_ERROR_NONE) {
322 ckmc_cert_list_all_free(retCaCertList);
323 retCaCertList = NULL;
327 return CKMC_ERROR_NONE;
332 OpenSSL_add_all_algorithms();
334 int ret = CKMC_ERROR_NONE;
336 Pkcs12Converter converter;
337 if((ret = converter.parsePkcs12(file_path, passphrase)) != CKMC_ERROR_NONE) {
340 if((ret = converter.toCkmCert()) != CKMC_ERROR_NONE) {
343 if((ret = converter.toCkmKey()) != CKMC_ERROR_NONE) {
346 if((ret = converter.toCaCkmCertList()) != CKMC_ERROR_NONE) {
350 *private_key = converter.retPrivateKey;
351 *ckmcert = converter.retCkmCert;
352 *ca_cert_list = converter.retCaCertList;
354 return CKMC_ERROR_NONE;
358 void ckmc_cert_free(ckmc_cert_s *cert)
363 if(cert->raw_cert != NULL) {
364 memset(cert->raw_cert, 0, cert->cert_size);
365 free(cert->raw_cert);
371 int ckmc_alias_list_new(char *alias, ckmc_alias_list_s **ppalias_list)
373 ckmc_alias_list_s *previous = NULL;
374 return ckmc_alias_list_add(previous, alias, ppalias_list);
378 int ckmc_alias_list_add(ckmc_alias_list_s *previous, char *alias, ckmc_alias_list_s **pplast)
380 ckmc_alias_list_s *plist;
382 if(alias == NULL || pplast == NULL) {
383 return CKMC_ERROR_INVALID_PARAMETER;
386 plist = static_cast<ckmc_alias_list_s*>(malloc(sizeof(ckmc_alias_list_s)));
388 return CKMC_ERROR_OUT_OF_MEMORY;
391 plist->alias = alias;
394 if(previous != NULL) {
395 previous->next = plist;
399 return CKMC_ERROR_NONE;
403 void ckmc_alias_list_free(ckmc_alias_list_s *first)
405 ckmc_alias_list_s *next = first;
407 ckmc_alias_list_s *current = next;
408 next = current->next;
414 void ckmc_alias_list_all_free(ckmc_alias_list_s *first)
416 ckmc_alias_list_s *next = first;
418 ckmc_alias_list_s *current = next;
419 next = current->next;
420 free(current->alias);
426 int ckmc_cert_list_new(ckmc_cert_s *cert, ckmc_cert_list_s **ppalias_list)
428 ckmc_cert_list_s *previous = NULL;
429 return ckmc_cert_list_add(previous, cert, ppalias_list);
433 int ckmc_cert_list_add(ckmc_cert_list_s *previous, ckmc_cert_s *cert, ckmc_cert_list_s **pplast)
435 ckmc_cert_list_s *plist;
437 if(cert == NULL || pplast == NULL) {
438 return CKMC_ERROR_INVALID_PARAMETER;
441 plist = static_cast<ckmc_cert_list_s*>(malloc(sizeof(ckmc_cert_list_s)));
443 return CKMC_ERROR_OUT_OF_MEMORY;
448 if(previous != NULL) {
449 previous->next = plist;
454 return CKMC_ERROR_NONE;
458 void ckmc_cert_list_free(ckmc_cert_list_s *first)
460 ckmc_cert_list_s *next = first;
462 ckmc_cert_list_s *current = next;
463 next = current->next;
469 void ckmc_cert_list_all_free(ckmc_cert_list_s *first)
471 ckmc_cert_list_s *next = first;
473 ckmc_cert_list_s *current = next;
474 next = current->next;
475 ckmc_cert_free(current->cert);
480 int _ckmc_load_cert_from_x509(X509 *xCert, ckmc_cert_s **cert)
483 return CKMC_ERROR_INVALID_FORMAT;
486 BIO *bcert = BIO_new(BIO_s_mem());
488 i2d_X509_bio(bcert, xCert);
490 CKM::RawBuffer output(8196);
491 int size = BIO_read(bcert, output.data(), output.size());
494 return CKMC_ERROR_INVALID_FORMAT;
498 return ckmc_cert_new(output.data(), output.size(), CKMC_FORM_DER, cert);