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 <openssl/x509v3.h>
32 #include <openssl/pkcs12.h>
33 #include <openssl/evp.h>
34 #include <openssl/pem.h>
36 int _ckmc_load_cert_from_x509(X509 *xCert, ckmc_cert_s **cert);
39 int ckmc_key_new(unsigned char *raw_key, size_t key_size, ckmc_key_type_e key_type, char *password, ckmc_key_s **ppkey)
43 if(raw_key == NULL || key_size <= 0 || ppkey == NULL) {
44 return CKMC_ERROR_INVALID_PARAMETER;
47 pkey = static_cast<ckmc_key_s*>(malloc(sizeof(ckmc_key_s)));
49 return CKMC_ERROR_OUT_OF_MEMORY;
51 pkey->raw_key = reinterpret_cast<unsigned char*>(malloc(key_size));
52 if(pkey->raw_key == NULL) {
54 return CKMC_ERROR_OUT_OF_MEMORY;
56 memcpy(pkey->raw_key, raw_key, key_size);
58 pkey->key_size = key_size;
59 pkey->key_type = key_type;
61 if(password != NULL) {
62 pkey->password = reinterpret_cast<char*>(malloc(strlen(password) +1));
63 if(pkey->password == NULL) {
66 return CKMC_ERROR_OUT_OF_MEMORY;
68 memset(pkey->password, 0, strlen(password) +1);
69 strncpy(pkey->password, password, strlen(password));
71 pkey->password = NULL;
76 return CKMC_ERROR_NONE;
80 void ckmc_key_free(ckmc_key_s *key)
85 if(key->password != NULL)
87 if(key->raw_key != NULL) {
88 memset(key->raw_key, 0, key->key_size);
96 int ckmc_buffer_new(unsigned char *data, size_t size,ckmc_raw_buffer_s **ppbuffer)
98 ckmc_raw_buffer_s *pbuff;
100 if(data == NULL || size <= 0 || ppbuffer == NULL) {
101 return CKMC_ERROR_INVALID_PARAMETER;
104 pbuff = static_cast<ckmc_raw_buffer_s*>(malloc(sizeof(ckmc_raw_buffer_s)));
106 return CKMC_ERROR_OUT_OF_MEMORY;
108 pbuff->data = reinterpret_cast<unsigned char*>(malloc(size));
109 if(pbuff->data == NULL) {
111 return CKMC_ERROR_OUT_OF_MEMORY;
113 memcpy(pbuff->data, data, size);
118 return CKMC_ERROR_NONE;
122 void ckmc_buffer_free(ckmc_raw_buffer_s *buffer)
127 if(buffer->data != NULL) {
128 memset(buffer->data, 0, buffer->size);
135 int ckmc_cert_new(unsigned char *raw_cert, size_t cert_size, ckmc_data_format_e data_format, ckmc_cert_s **ppcert)
139 if(raw_cert == NULL || cert_size <= 0 || ppcert == NULL) {
140 return CKMC_ERROR_INVALID_PARAMETER;
143 pcert = static_cast<ckmc_cert_s*>(malloc(sizeof(ckmc_cert_s)));
145 return CKMC_ERROR_OUT_OF_MEMORY;
147 pcert->raw_cert = reinterpret_cast<unsigned char*>(malloc(cert_size));
148 if(pcert->raw_cert == NULL) {
150 return CKMC_ERROR_OUT_OF_MEMORY;
152 memcpy(pcert->raw_cert, raw_cert, cert_size);
154 pcert->cert_size = cert_size;
155 pcert->data_format = data_format;
158 return CKMC_ERROR_NONE;
162 int ckmc_load_cert_from_file(const char *file_path, ckmc_cert_s **cert)
164 OpenSSL_add_all_algorithms();
166 FILE *fp = fopen(file_path, "r");
168 return CKMC_ERROR_FILE_ACCESS_DENIED;
170 if(!(pcert = d2i_X509_fp(fp, NULL))) {
171 fseek(fp, 0, SEEK_SET);
172 pcert = PEM_read_X509(fp, NULL, NULL, NULL);
176 return CKMC_ERROR_INVALID_FORMAT;
179 int ret = _ckmc_load_cert_from_x509(pcert, cert);
180 if(ret != CKMC_ERROR_NONE) {
187 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)
189 class Pkcs12Converter {
199 ckmc_key_s *retPrivateKey;
200 ckmc_cert_s *retCkmCert;
201 ckmc_cert_list_s *retCaCertList;
209 ret = CKMC_ERROR_NONE;
210 retPrivateKey = NULL;
212 retCaCertList = NULL;
224 sk_X509_pop_free(ca, X509_free);
226 if(ret != CKMC_ERROR_NONE) {
227 if(retPrivateKey != NULL){
228 ckmc_key_free(retPrivateKey);
229 retPrivateKey = NULL;
231 if(retCkmCert != NULL) {
232 ckmc_cert_free(retCkmCert);
235 if(retCaCertList != NULL) {
236 ckmc_cert_list_all_free(retCaCertList);
237 retCaCertList = NULL;
242 int parsePkcs12(const char *filePath, const char *pass) {
244 if(!(fp_in = fopen(filePath, "rb"))) {
245 return CKMC_ERROR_FILE_ACCESS_DENIED;
248 if(!(p12 = d2i_PKCS12_fp(fp_in, NULL))) {
249 return CKMC_ERROR_INVALID_FORMAT;
252 /* parse PKCS#12 certificate */
253 if((ret = PKCS12_parse(p12, pass, &pkey, &x509Cert, &ca)) != 1) {
254 return CKMC_ERROR_INVALID_FORMAT;
256 return CKMC_ERROR_NONE;
260 if( (ret =_ckmc_load_cert_from_x509(x509Cert,&retCkmCert)) != CKMC_ERROR_NONE) {
263 return CKMC_ERROR_NONE;
267 BIO *bkey = BIO_new(BIO_s_mem());
269 i2d_PrivateKey_bio(bkey, pkey);
271 CKM::RawBuffer output(8196);
272 int size = BIO_read(bkey, output.data(), output.size());
275 return CKMC_ERROR_INVALID_FORMAT;
279 int type = EVP_PKEY_type(pkey->type);
280 ckmc_key_type_e key_type = CKMC_KEY_NONE;
283 key_type = CKMC_KEY_RSA_PRIVATE;
286 key_type = CKMC_KEY_DSA_PRIVATE;
289 key_type = CKMC_KEY_ECDSA_PRIVATE;
292 if(key_type == CKMC_KEY_NONE) {
293 return CKMC_ERROR_INVALID_FORMAT;
296 char *nullPassword = NULL;
298 return ckmc_key_new(output.data(), size, key_type, nullPassword, &retPrivateKey);
301 int toCaCkmCertList() {
303 X509* popedCert = NULL;
304 ckmc_cert_s *popedCkmCert = NULL;
305 ckmc_cert_list_s *tmpCertList = NULL;
306 while((popedCert = sk_X509_pop(ca)) != NULL) {
307 if( (tmpRet =_ckmc_load_cert_from_x509(popedCert, &popedCkmCert)) != CKMC_ERROR_NONE) {
308 return CKMC_ERROR_OUT_OF_MEMORY;
310 if(tmpCertList == NULL) { // first
311 tmpRet = ckmc_cert_list_new(popedCkmCert, &tmpCertList);
312 retCaCertList = tmpCertList;
314 tmpRet = ckmc_cert_list_add(tmpCertList, popedCkmCert, &tmpCertList);
316 if(tmpRet != CKMC_ERROR_NONE) {
317 ckmc_cert_list_all_free(retCaCertList);
318 retCaCertList = NULL;
322 return CKMC_ERROR_NONE;
327 OpenSSL_add_all_algorithms();
329 int ret = CKMC_ERROR_NONE;
331 Pkcs12Converter converter;
332 if((ret = converter.parsePkcs12(file_path, passphrase)) != CKMC_ERROR_NONE) {
335 if((ret = converter.toCkmCert()) != CKMC_ERROR_NONE) {
338 if((ret = converter.toCkmKey()) != CKMC_ERROR_NONE) {
341 if((ret = converter.toCaCkmCertList()) != CKMC_ERROR_NONE) {
345 *private_key = converter.retPrivateKey;
346 *ckmcert = converter.retCkmCert;
347 *ca_cert_list = converter.retCaCertList;
349 return CKMC_ERROR_NONE;
353 void ckmc_cert_free(ckmc_cert_s *cert)
358 if(cert->raw_cert != NULL) {
359 memset(cert->raw_cert, 0, cert->cert_size);
360 free(cert->raw_cert);
366 int ckmc_alias_list_new(char *alias, ckmc_alias_list_s **ppalias_list)
368 ckmc_alias_list_s *previous = NULL;
369 return ckmc_alias_list_add(previous, alias, ppalias_list);
373 int ckmc_alias_list_add(ckmc_alias_list_s *previous, char *alias, ckmc_alias_list_s **pplast)
375 ckmc_alias_list_s *plist;
377 if(alias == NULL || pplast == NULL) {
378 return CKMC_ERROR_INVALID_PARAMETER;
381 plist = static_cast<ckmc_alias_list_s*>(malloc(sizeof(ckmc_alias_list_s)));
383 return CKMC_ERROR_OUT_OF_MEMORY;
386 plist->alias = alias;
389 if(previous != NULL) {
390 previous->next = plist;
394 return CKMC_ERROR_NONE;
398 void ckmc_alias_list_free(ckmc_alias_list_s *first)
400 ckmc_alias_list_s *next = first;
402 ckmc_alias_list_s *current = next;
403 next = current->next;
409 void ckmc_alias_list_all_free(ckmc_alias_list_s *first)
411 ckmc_alias_list_s *next = first;
413 ckmc_alias_list_s *current = next;
414 next = current->next;
415 free(current->alias);
421 int ckmc_cert_list_new(ckmc_cert_s *cert, ckmc_cert_list_s **ppalias_list)
423 ckmc_cert_list_s *previous = NULL;
424 return ckmc_cert_list_add(previous, cert, ppalias_list);
428 int ckmc_cert_list_add(ckmc_cert_list_s *previous, ckmc_cert_s *cert, ckmc_cert_list_s **pplast)
430 ckmc_cert_list_s *plist;
432 if(cert == NULL || pplast == NULL) {
433 return CKMC_ERROR_INVALID_PARAMETER;
436 plist = static_cast<ckmc_cert_list_s*>(malloc(sizeof(ckmc_cert_list_s)));
438 return CKMC_ERROR_OUT_OF_MEMORY;
443 if(previous != NULL) {
444 previous->next = plist;
449 return CKMC_ERROR_NONE;
453 void ckmc_cert_list_free(ckmc_cert_list_s *first)
455 ckmc_cert_list_s *next = first;
457 ckmc_cert_list_s *current = next;
458 next = current->next;
464 void ckmc_cert_list_all_free(ckmc_cert_list_s *first)
466 ckmc_cert_list_s *next = first;
468 ckmc_cert_list_s *current = next;
469 next = current->next;
470 ckmc_cert_free(current->cert);
475 int _ckmc_load_cert_from_x509(X509 *xCert, ckmc_cert_s **cert)
478 return CKMC_ERROR_INVALID_FORMAT;
481 BIO *bcert = BIO_new(BIO_s_mem());
483 i2d_X509_bio(bcert, xCert);
485 CKM::RawBuffer output(8196);
486 int size = BIO_read(bcert, output.data(), output.size());
489 return CKMC_ERROR_INVALID_FORMAT;
493 return ckmc_cert_new(output.data(), output.size(), CKMC_FORM_DER, cert);