6f1ddd2067850f32c686d553c4d80b5109cd11fd
[platform/core/security/key-manager.git] / src / manager / client-capi / ckmc-type.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-type.cpp
18  * @author      Yuseok Jeon(yuseok.jeon@samsung.com)
19  * @version     1.0
20  * @brief       new and free methods for the struct of CAPI
21  */
22
23
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
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>
36 #include <fstream>
37 #include <crypto-init.h>
38
39 namespace {
40
41 const size_t DEFAULT_IV_LEN = 16;
42 const size_t DEFAULT_IV_LEN_BITS = 8*DEFAULT_IV_LEN;
43 const size_t DEFAULT_KEY_LEN_BITS = 4096;
44
45 int _ckmc_load_cert_from_x509(X509 *xCert, ckmc_cert_s **cert)
46 {
47     if(xCert == NULL) {
48         return CKMC_ERROR_INVALID_FORMAT;
49     }
50
51     BIO *bcert = BIO_new(BIO_s_mem());
52
53     i2d_X509_bio(bcert, xCert);
54
55     CKM::RawBuffer output(8196);
56     int size = BIO_read(bcert, output.data(), output.size());
57     BIO_free_all(bcert);
58     if (size <= 0) {
59         return CKMC_ERROR_INVALID_FORMAT;
60     }
61     output.resize(size);
62
63     return ckmc_cert_new(output.data(), output.size(), CKMC_FORM_DER, cert);
64 }
65
66 } // namespace anonymous
67
68
69 const char * const ckmc_label_name_separator    = CKM::LABEL_NAME_SEPARATOR;
70 const char * const ckmc_label_shared_owner      = CKM::OWNER_ID_SYSTEM;
71 const char * const ckmc_owner_id_separator      = CKM::LABEL_NAME_SEPARATOR;
72 const char * const ckmc_owner_id_system         = CKM::OWNER_ID_SYSTEM;
73
74 KEY_MANAGER_CAPI
75 int ckmc_key_new(unsigned char *raw_key, size_t key_size, ckmc_key_type_e key_type, char *password, ckmc_key_s **ppkey)
76 {
77     ckmc_key_s *pkey;
78
79     if(raw_key == NULL || key_size <= 0 || ppkey == NULL) {
80         return CKMC_ERROR_INVALID_PARAMETER;
81     }
82
83     pkey = static_cast<ckmc_key_s*>(malloc(sizeof(ckmc_key_s)));
84     if(pkey == NULL) {
85         return CKMC_ERROR_OUT_OF_MEMORY;
86     }
87     pkey->raw_key = reinterpret_cast<unsigned char*>(malloc(key_size));
88     if(pkey->raw_key == NULL) {
89         free(pkey);
90         return CKMC_ERROR_OUT_OF_MEMORY;
91     }
92     memcpy(pkey->raw_key, raw_key, key_size);
93
94     pkey->key_size = key_size;
95     pkey->key_type = key_type;
96
97     if(password != NULL) {
98         pkey->password = reinterpret_cast<char*>(malloc(strlen(password) +1));
99         if(pkey->password == NULL) {
100             free(pkey->raw_key);
101             free(pkey);
102             return CKMC_ERROR_OUT_OF_MEMORY;
103         }
104         memset(pkey->password, 0, strlen(password) +1);
105         strncpy(pkey->password, password, strlen(password));
106     }else {
107         pkey->password = NULL;
108     }
109
110     *ppkey = pkey;
111
112     return CKMC_ERROR_NONE;
113 }
114
115 KEY_MANAGER_CAPI
116 void ckmc_key_free(ckmc_key_s *key)
117 {
118     if(key == NULL)
119         return;
120
121     if(key->password != NULL)
122         free(key->password);
123     if(key->raw_key != NULL) {
124         memset(key->raw_key, 0, key->key_size);
125         free(key->raw_key);
126     }
127
128     free(key);
129 }
130
131 KEY_MANAGER_CAPI
132 int ckmc_buffer_new(unsigned char *data, size_t size,ckmc_raw_buffer_s **ppbuffer)
133 {
134     ckmc_raw_buffer_s *pbuff;
135
136     if(data == NULL || size <= 0 || ppbuffer == NULL) {
137         return CKMC_ERROR_INVALID_PARAMETER;
138     }
139
140     pbuff = static_cast<ckmc_raw_buffer_s*>(malloc(sizeof(ckmc_raw_buffer_s)));
141     if(pbuff == NULL)
142             return CKMC_ERROR_OUT_OF_MEMORY;
143
144     pbuff->data = reinterpret_cast<unsigned char*>(malloc(size));
145     if(pbuff->data == NULL) {
146         free(pbuff);
147         return CKMC_ERROR_OUT_OF_MEMORY;
148     }
149     memcpy(pbuff->data, data, size);
150
151     pbuff->size = size;
152     *ppbuffer = pbuff;
153
154     return CKMC_ERROR_NONE;
155 }
156
157 KEY_MANAGER_CAPI
158 void ckmc_buffer_free(ckmc_raw_buffer_s *buffer)
159 {
160     if(buffer == NULL)
161         return;
162
163     if(buffer->data != NULL) {
164         memset(buffer->data, 0, buffer->size);
165         free(buffer->data);
166     }
167     free(buffer);
168 }
169
170 KEY_MANAGER_CAPI
171 int ckmc_cert_new(unsigned char *raw_cert, size_t cert_size, ckmc_data_format_e data_format, ckmc_cert_s **ppcert)
172 {
173     ckmc_cert_s *pcert;
174
175     if(raw_cert == NULL || cert_size <= 0 || ppcert == NULL) {
176         return CKMC_ERROR_INVALID_PARAMETER;
177     }
178
179     pcert = static_cast<ckmc_cert_s*>(malloc(sizeof(ckmc_cert_s)));
180     if(pcert == NULL) {
181         return CKMC_ERROR_OUT_OF_MEMORY;
182     }
183     pcert->raw_cert = reinterpret_cast<unsigned char*>(malloc(cert_size));
184     if(pcert->raw_cert == NULL) {
185         free(pcert);
186         return CKMC_ERROR_OUT_OF_MEMORY;
187     }
188     memcpy(pcert->raw_cert, raw_cert, cert_size);
189
190     pcert->cert_size = cert_size;
191     pcert->data_format = data_format;
192
193     *ppcert = pcert;
194     return CKMC_ERROR_NONE;
195 }
196
197 KEY_MANAGER_CAPI
198 int ckmc_load_cert_from_file(const char *file_path, ckmc_cert_s **cert)
199 {
200     CKM::initOpenSslOnce();
201
202     FILE *fp = fopen(file_path, "r");
203     if(fp == NULL)
204         return CKMC_ERROR_FILE_ACCESS_DENIED;
205     X509 *pcert = NULL;
206     if(!(pcert = d2i_X509_fp(fp, NULL))) {
207         fseek(fp, 0, SEEK_SET);
208         pcert = PEM_read_X509(fp, NULL, NULL, NULL);
209     }
210     fclose(fp);
211     if(pcert == NULL) {
212         return CKMC_ERROR_INVALID_FORMAT;
213     }
214
215     int ret = _ckmc_load_cert_from_x509(pcert, cert);
216     if(ret != CKMC_ERROR_NONE) {
217         X509_free(pcert);
218     }
219     return ret;
220 }
221
222 KEY_MANAGER_CAPI
223 void ckmc_cert_free(ckmc_cert_s *cert)
224 {
225     if(cert == NULL)
226         return;
227
228     if(cert->raw_cert != NULL) {
229         memset(cert->raw_cert, 0, cert->cert_size);
230         free(cert->raw_cert);
231     }
232     free(cert);
233 }
234
235 KEY_MANAGER_CAPI
236 int ckmc_pkcs12_new(ckmc_key_s *private_key, ckmc_cert_s *cert,
237         ckmc_cert_list_s *ca_cert_list, ckmc_pkcs12_s **pkcs12_bundle)
238 {
239     ckmc_pkcs12_s *pkcs12;
240
241     if(!pkcs12_bundle ||
242        (private_key==NULL && cert==NULL && (ca_cert_list==NULL || ca_cert_list->cert==NULL))) {
243         return CKMC_ERROR_INVALID_PARAMETER;
244     }
245
246     pkcs12 = static_cast<ckmc_pkcs12_s*>(malloc(sizeof(ckmc_pkcs12_s)));
247     if(pkcs12 == NULL) {
248         return CKMC_ERROR_OUT_OF_MEMORY;
249     }
250     // ownership is transferred into pkcs12 - mentioned in the docs
251     pkcs12->priv_key = private_key;
252     pkcs12->cert = cert;
253     pkcs12->ca_chain = ca_cert_list;
254
255     *pkcs12_bundle = pkcs12;
256     return CKMC_ERROR_NONE;
257 }
258
259 KEY_MANAGER_CAPI
260 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)
261 {
262     class Pkcs12Converter {
263     private:
264         FILE* fp_in;
265         PKCS12* p12;
266         EVP_PKEY* pkey;
267         X509* x509Cert;
268         STACK_OF(X509)* ca;
269
270         int ret;
271     public:
272         ckmc_key_s *retPrivateKey;
273         ckmc_cert_s *retCkmCert;
274         ckmc_cert_list_s *retCaCertList;
275
276         Pkcs12Converter(){
277             fp_in = NULL;
278             p12 = NULL;
279             pkey = NULL;
280             x509Cert = NULL;
281             ca = NULL;
282             ret = CKMC_ERROR_NONE;
283             retPrivateKey = NULL;
284             retCkmCert = NULL;
285             retCaCertList = NULL;
286         };
287         ~Pkcs12Converter(){
288             if(fp_in != NULL)
289                 fclose(fp_in);
290             if(p12 != NULL)
291                 PKCS12_free(p12);
292             if(x509Cert != NULL)
293                 X509_free(x509Cert);
294             if(pkey != NULL)
295                 EVP_PKEY_free(pkey);
296             if(ca != NULL)
297                 sk_X509_pop_free(ca, X509_free);
298
299             if(ret != CKMC_ERROR_NONE) {
300                 if(retPrivateKey != NULL){
301                     ckmc_key_free(retPrivateKey);
302                     retPrivateKey = NULL;
303                 }
304                 if(retCkmCert != NULL) {
305                     ckmc_cert_free(retCkmCert);
306                     retCkmCert = NULL;
307                 }
308                 if(retCaCertList != NULL) {
309                     ckmc_cert_list_all_free(retCaCertList);
310                     retCaCertList = NULL;
311                 }
312             }
313         };
314
315         int parsePkcs12(const char *filePath, const char *pass) {
316             fp_in = NULL;
317             if(!(fp_in = fopen(filePath, "rb"))) {
318                 return CKMC_ERROR_FILE_ACCESS_DENIED;
319             }
320
321             if(!(p12 = d2i_PKCS12_fp(fp_in, NULL))) {
322                 return CKMC_ERROR_INVALID_FORMAT;
323             }
324
325             /* parse PKCS#12 certificate */
326             if((ret = PKCS12_parse(p12, pass, &pkey, &x509Cert, &ca)) != 1) {
327                 return CKMC_ERROR_INVALID_FORMAT;
328             }
329             return CKMC_ERROR_NONE;
330         }
331
332         int toCkmCert() {
333             if( (ret =_ckmc_load_cert_from_x509(x509Cert,&retCkmCert)) != CKMC_ERROR_NONE) {
334                 return ret;
335             }
336             return CKMC_ERROR_NONE;
337         }
338
339         int toCkmKey() {
340             BIO *bkey = BIO_new(BIO_s_mem());
341
342             i2d_PrivateKey_bio(bkey, pkey);
343
344             CKM::RawBuffer output(8196);
345             int size = BIO_read(bkey, output.data(), output.size());
346             BIO_free_all(bkey);
347             if (size <= 0) {
348                 return CKMC_ERROR_INVALID_FORMAT;
349             }
350             output.resize(size);
351
352             int type = EVP_PKEY_type(pkey->type);
353             ckmc_key_type_e key_type = CKMC_KEY_NONE;
354             switch(type) {
355             case EVP_PKEY_RSA :
356                 key_type = CKMC_KEY_RSA_PRIVATE;
357                 break;
358             case EVP_PKEY_DSA :
359                 key_type = CKMC_KEY_DSA_PRIVATE;
360                 break;
361             case EVP_PKEY_EC :
362                 key_type = CKMC_KEY_ECDSA_PRIVATE;
363                 break;
364             }
365             if(key_type == CKMC_KEY_NONE) {
366                 return CKMC_ERROR_INVALID_FORMAT;
367             }
368
369             char *nullPassword = NULL;
370
371             return ckmc_key_new(output.data(), size, key_type, nullPassword, &retPrivateKey);
372         }
373
374         int toCaCkmCertList() {
375             int tmpRet;
376             X509* popedCert = NULL;
377             ckmc_cert_s *popedCkmCert = NULL;
378             ckmc_cert_list_s *tmpCertList = NULL;
379             while((popedCert = sk_X509_pop(ca)) != NULL) {
380                 if( (tmpRet =_ckmc_load_cert_from_x509(popedCert, &popedCkmCert)) != CKMC_ERROR_NONE) {
381                     return CKMC_ERROR_OUT_OF_MEMORY;
382                 }
383                 if(tmpCertList == NULL) { // first
384                     tmpRet = ckmc_cert_list_new(popedCkmCert, &tmpCertList);
385                     retCaCertList = tmpCertList;
386                 }else {
387                     tmpRet = ckmc_cert_list_add(tmpCertList, popedCkmCert, &tmpCertList);
388                 }
389                 if(tmpRet != CKMC_ERROR_NONE) {
390                     ckmc_cert_list_all_free(retCaCertList);
391                     retCaCertList = NULL;
392                     return tmpRet;
393                 }
394             }
395             return CKMC_ERROR_NONE;
396         }
397
398     };
399
400     CKM::initOpenSslOnce();
401
402     int ret = CKMC_ERROR_NONE;
403
404     Pkcs12Converter converter;
405     if((ret = converter.parsePkcs12(file_path, passphrase)) != CKMC_ERROR_NONE) {
406         return ret;
407     }
408     if((ret = converter.toCkmCert()) != CKMC_ERROR_NONE) {
409         return ret;
410     }
411     if((ret = converter.toCkmKey()) != CKMC_ERROR_NONE) {
412         return ret;
413     }
414     if((ret = converter.toCaCkmCertList()) != CKMC_ERROR_NONE) {
415         return ret;
416     }
417
418     *private_key = converter.retPrivateKey;
419     *ckmcert = converter.retCkmCert;
420     *ca_cert_list = converter.retCaCertList;
421
422     return CKMC_ERROR_NONE;
423 }
424
425 KEY_MANAGER_CAPI
426 int ckmc_pkcs12_load(const char *file_path, const char *passphrase, ckmc_pkcs12_s **pkcs12_bundle)
427 {
428     int ec;
429     ckmc_key_s *private_key = 0;
430     ckmc_cert_s *cert = 0;
431     ckmc_cert_list_s *ca_cert_list = 0;
432
433     if(!file_path || !pkcs12_bundle)
434         return CKMC_ERROR_INVALID_PARAMETER;
435
436     ec = ckmc_load_from_pkcs12_file(file_path, passphrase, &private_key, &cert, &ca_cert_list);
437     if(ec != CKMC_ERROR_NONE)
438         return ec;
439
440     ec = ckmc_pkcs12_new(private_key, cert, ca_cert_list, pkcs12_bundle);
441     if(ec != CKMC_ERROR_NONE)
442     {
443         ckmc_key_free(private_key);
444         ckmc_cert_free(cert);
445         ckmc_cert_list_free(ca_cert_list);
446         return ec;
447     }
448
449     return CKMC_ERROR_NONE;
450 }
451
452 KEY_MANAGER_CAPI
453 void ckmc_pkcs12_free(ckmc_pkcs12_s *pkcs12)
454 {
455     if(pkcs12 == NULL)
456         return;
457
458     ckmc_key_free(pkcs12->priv_key);
459     ckmc_cert_free(pkcs12->cert);
460     ckmc_cert_list_free(pkcs12->ca_chain);
461     free(pkcs12);
462 }
463
464 KEY_MANAGER_CAPI
465 int ckmc_alias_list_new(char *alias, ckmc_alias_list_s **ppalias_list)
466 {
467     ckmc_alias_list_s *previous = NULL;
468     return ckmc_alias_list_add(previous, alias, ppalias_list);
469 }
470
471 KEY_MANAGER_CAPI
472 int ckmc_alias_list_add(ckmc_alias_list_s *previous, char *alias, ckmc_alias_list_s **pplast)
473 {
474     ckmc_alias_list_s *plist;
475
476     if(alias == NULL || pplast == NULL) {
477         return CKMC_ERROR_INVALID_PARAMETER;
478     }
479
480     plist = static_cast<ckmc_alias_list_s*>(malloc(sizeof(ckmc_alias_list_s)));
481     if(plist == NULL) {
482         return CKMC_ERROR_OUT_OF_MEMORY;
483     }
484
485     plist->alias = alias;
486     plist->next = NULL;
487
488     if(previous != NULL) {
489         previous->next = plist;
490     }
491     *pplast = plist;
492
493     return CKMC_ERROR_NONE;
494 }
495
496 KEY_MANAGER_CAPI
497 void ckmc_alias_list_free(ckmc_alias_list_s *first)
498 {
499     ckmc_alias_list_s *next = first;
500     while (next) {
501         ckmc_alias_list_s *current = next;
502         next = current->next;
503         free(current);
504     }
505 }
506
507 KEY_MANAGER_CAPI
508 void ckmc_alias_list_all_free(ckmc_alias_list_s *first)
509 {
510     ckmc_alias_list_s *next = first;
511     while (next) {
512         ckmc_alias_list_s *current = next;
513         next = current->next;
514         free(current->alias);
515         free(current);
516     }
517 }
518
519 KEY_MANAGER_CAPI
520 int ckmc_cert_list_new(ckmc_cert_s *cert, ckmc_cert_list_s **ppalias_list)
521 {
522     ckmc_cert_list_s *previous = NULL;
523     return ckmc_cert_list_add(previous, cert, ppalias_list);
524 }
525
526 KEY_MANAGER_CAPI
527 int ckmc_cert_list_add(ckmc_cert_list_s *previous, ckmc_cert_s *cert, ckmc_cert_list_s **pplast)
528 {
529     ckmc_cert_list_s *plist;
530
531     if(cert == NULL || pplast == NULL) {
532         return CKMC_ERROR_INVALID_PARAMETER;
533     }
534
535     plist = static_cast<ckmc_cert_list_s*>(malloc(sizeof(ckmc_cert_list_s)));
536     if(plist == NULL) {
537         return CKMC_ERROR_OUT_OF_MEMORY;
538     }
539     plist->cert = cert;
540     plist->next = NULL;
541
542     if(previous != NULL) {
543         previous->next = plist;
544     }
545
546     *pplast = plist;
547
548     return CKMC_ERROR_NONE;
549 }
550
551 KEY_MANAGER_CAPI
552 void ckmc_cert_list_free(ckmc_cert_list_s *first)
553 {
554     ckmc_cert_list_s *next = first;
555     while (next) {
556         ckmc_cert_list_s *current = next;
557         next = current->next;
558         free(current);
559     }
560 }
561
562 KEY_MANAGER_CAPI
563 void ckmc_cert_list_all_free(ckmc_cert_list_s *first)
564 {
565     ckmc_cert_list_s *next = first;
566     while (next) {
567         ckmc_cert_list_s *current = next;
568         next = current->next;
569         ckmc_cert_free(current->cert);
570         free(current);
571     }
572 }
573
574 KEY_MANAGER_CAPI
575 int ckmc_param_list_new(ckmc_param_list_s **ppparams)
576 {
577     if (!ppparams)
578         return CKMC_ERROR_INVALID_PARAMETER;
579
580     *ppparams = reinterpret_cast<ckmc_param_list_s*>(new(std::nothrow)(CKM::CryptoAlgorithm));
581     if (!*ppparams)
582         return CKMC_ERROR_OUT_OF_MEMORY;
583     return CKMC_ERROR_NONE;
584 }
585
586 KEY_MANAGER_CAPI
587 int ckmc_param_list_add_integer(ckmc_param_list_s *params,
588                                 ckmc_param_name_e name,
589                                 uint64_t value)
590 {
591     if (!params)
592         return CKMC_ERROR_INVALID_PARAMETER;
593
594     CKM::CryptoAlgorithm* algo = reinterpret_cast<CKM::CryptoAlgorithm*>(params);
595     bool ret = algo->setParam(static_cast<CKM::ParamName>(name), value);
596     return (ret ? CKMC_ERROR_NONE : CKMC_ERROR_INVALID_PARAMETER);
597 }
598
599 KEY_MANAGER_CAPI
600 int ckmc_param_list_add_buffer(ckmc_param_list_s *params,
601                                ckmc_param_name_e name,
602                                const ckmc_raw_buffer_s *buffer)
603 {
604     if (!params || !buffer || !buffer->data || buffer->size == 0)
605         return CKMC_ERROR_INVALID_PARAMETER;
606
607     CKM::CryptoAlgorithm* algo = reinterpret_cast<CKM::CryptoAlgorithm*>(params);
608     CKM::RawBuffer b(buffer->data, buffer->data + buffer->size);
609     bool ret =  algo->setParam(static_cast<CKM::ParamName>(name), b);
610     return (ret ? CKMC_ERROR_NONE : CKMC_ERROR_INVALID_PARAMETER);
611 }
612
613 KEY_MANAGER_CAPI
614 int ckmc_param_list_get_integer(const ckmc_param_list_s *params,
615                                 ckmc_param_name_e name,
616                                 uint64_t* value)
617 {
618     if (!params || !value)
619         return CKMC_ERROR_INVALID_PARAMETER;
620
621     const CKM::CryptoAlgorithm* algo = reinterpret_cast<const CKM::CryptoAlgorithm*>(params);
622     if (!algo->getParam(static_cast<CKM::ParamName>(name),*value))
623         return CKMC_ERROR_INVALID_PARAMETER;
624
625     return CKMC_ERROR_NONE;
626 }
627
628 KEY_MANAGER_CAPI
629 int ckmc_param_list_get_buffer(const ckmc_param_list_s *params,
630                                ckmc_param_name_e name,
631                                ckmc_raw_buffer_s **buffer)
632 {
633     if (!params || !buffer)
634         return CKMC_ERROR_INVALID_PARAMETER;
635
636     const CKM::CryptoAlgorithm* algo = reinterpret_cast<const CKM::CryptoAlgorithm*>(params);
637     CKM::RawBuffer value;
638     if (!algo->getParam(static_cast<CKM::ParamName>(name),value))
639         return CKMC_ERROR_INVALID_PARAMETER;
640
641     return ckmc_buffer_new(value.data(), value.size(), buffer);
642 }
643
644 KEY_MANAGER_CAPI
645 void ckmc_param_list_free(ckmc_param_list_s *params)
646 {
647     CKM::CryptoAlgorithm* algo = reinterpret_cast<CKM::CryptoAlgorithm*>(params);
648     delete algo;
649 }
650
651 KEY_MANAGER_CAPI
652 int ckmc_generate_params(ckmc_algo_type_e type, ckmc_param_list_s *params)
653 {
654     // return error if params are NULL
655     if(params == NULL)
656         return CKMC_ERROR_INVALID_PARAMETER;
657
658     int ret = CKMC_ERROR_NONE;
659     switch(type)
660     {
661     case CKMC_ALGO_AES_CTR:
662         ret = ckmc_param_list_add_integer(params, CKMC_PARAM_ED_CTR_LEN, DEFAULT_IV_LEN_BITS);
663         break;
664     case CKMC_ALGO_AES_CBC:
665     case CKMC_ALGO_AES_GCM:
666     case CKMC_ALGO_AES_CFB:
667     case CKMC_ALGO_RSA_OAEP:
668         // no iv by default
669         break;
670     case CKMC_ALGO_RSA_SV:
671     case CKMC_ALGO_DSA_SV:
672     case CKMC_ALGO_ECDSA_SV:
673         // no hash, no padding by default
674         break;
675     case CKMC_ALGO_RSA_GEN:
676     case CKMC_ALGO_DSA_GEN:
677         ret = ckmc_param_list_add_integer(params, CKMC_PARAM_GEN_KEY_LEN, DEFAULT_KEY_LEN_BITS);
678         break;
679     case CKMC_ALGO_ECDSA_GEN:
680         ret = ckmc_param_list_add_integer(params, CKMC_PARAM_GEN_EC, CKMC_EC_PRIME192V1);
681         break;
682     default:
683         return CKMC_ERROR_INVALID_PARAMETER;
684     }
685     if (ret == CKMC_ERROR_NONE)
686         return ckmc_param_list_add_integer(params, CKMC_PARAM_ALGO_TYPE, type);
687     return ret;
688 }