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