6bd2eaacf020c32d897d29781ae7e1667072c325
[silc.git] / lib / silccrypt / silcpkcs.c
1 /*
2
3   silcpkcs.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19 /* $Id$ */
20
21 #include "silc.h"
22 #include "silcpk_i.h"
23 #include "silcpkcs1_i.h"
24
25 #ifndef SILC_SYMBIAN
26 /* Dynamically registered list of PKCS. */
27 SilcDList silc_pkcs_list = NULL;
28 SilcDList silc_pkcs_alg_list = NULL;
29 #define SILC_PKCS_LIST silc_pkcs_list
30 #define SILC_PKCS_ALG_LIST silc_pkcs_alg_list
31 #else
32 #define SILC_PKCS_LIST TRUE
33 #define SILC_PKCS_ALG_LIST TRUE
34 #endif /* SILC_SYMBIAN */
35
36 /* Static list of PKCS for silc_pkcs_register_default(). */
37 const SilcPKCSObject silc_default_pkcs[] =
38 {
39   /* SILC PKCS */
40   {
41     SILC_PKCS_SILC,
42     silc_pkcs_silc_get_algorithm,
43     silc_pkcs_silc_import_public_key_file,
44     silc_pkcs_silc_import_public_key,
45     silc_pkcs_silc_export_public_key_file,
46     silc_pkcs_silc_export_public_key,
47     silc_pkcs_silc_public_key_bitlen,
48     silc_pkcs_silc_public_key_copy,
49     silc_pkcs_silc_public_key_compare,
50     silc_pkcs_silc_public_key_free,
51     silc_pkcs_silc_import_private_key_file,
52     silc_pkcs_silc_import_private_key,
53     silc_pkcs_silc_export_private_key_file,
54     silc_pkcs_silc_export_private_key,
55     silc_pkcs_silc_private_key_bitlen,
56     silc_pkcs_silc_private_key_free,
57     silc_pkcs_silc_encrypt,
58     silc_pkcs_silc_decrypt,
59     silc_pkcs_silc_sign,
60     silc_pkcs_silc_verify,
61   },
62
63   {
64     0, NULL, NULL, NULL, NULL, NULL,
65        NULL, NULL, NULL, NULL, NULL
66   }
67 };
68
69 /* Builtin PKCS algorithms */
70 const SilcPKCSAlgorithm silc_default_pkcs_alg[] =
71 {
72   /* PKCS #1, Version 1.5 without hash OIDs */
73   {
74     "rsa",
75     "pkcs1-no-oid",
76     "sha1,md5",
77     silc_pkcs1_generate_key,
78     silc_pkcs1_import_public_key,
79     silc_pkcs1_export_public_key,
80     silc_pkcs1_public_key_bitlen,
81     silc_pkcs1_public_key_copy,
82     silc_pkcs1_public_key_compare,
83     silc_pkcs1_public_key_free,
84     silc_pkcs1_import_private_key,
85     silc_pkcs1_export_private_key,
86     silc_pkcs1_private_key_bitlen,
87     silc_pkcs1_private_key_free,
88     silc_pkcs1_encrypt,
89     silc_pkcs1_decrypt,
90     silc_pkcs1_sign_no_oid,
91     silc_pkcs1_verify_no_oid
92   },
93
94   /* PKCS #1, Version 1.5 */
95   {
96     "rsa",
97     "pkcs1",
98     "sha1,md5",
99     silc_pkcs1_generate_key,
100     silc_pkcs1_import_public_key,
101     silc_pkcs1_export_public_key,
102     silc_pkcs1_public_key_bitlen,
103     silc_pkcs1_public_key_copy,
104     silc_pkcs1_public_key_compare,
105     silc_pkcs1_public_key_free,
106     silc_pkcs1_import_private_key,
107     silc_pkcs1_export_private_key,
108     silc_pkcs1_private_key_bitlen,
109     silc_pkcs1_private_key_free,
110     silc_pkcs1_encrypt,
111     silc_pkcs1_decrypt,
112     silc_pkcs1_sign,
113     silc_pkcs1_verify
114   },
115
116   {
117     NULL, NULL, NULL, NULL,
118     NULL, NULL, NULL, NULL,
119     NULL, NULL, NULL, NULL,
120     NULL, NULL
121   }
122 };
123
124 /* Register a new PKCS into SILC. */
125
126 SilcBool silc_pkcs_register(const SilcPKCSObject *pkcs)
127 {
128 #ifndef SILC_SYMBIAN
129   SilcPKCSObject *newpkcs;
130
131   SILC_LOG_DEBUG(("Registering new PKCS"));
132
133   /* Check if exists already */
134   if (silc_pkcs_list) {
135     SilcPKCSObject *entry;
136     silc_dlist_start(silc_pkcs_list);
137     while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
138       if (entry->type == pkcs->type)
139         return FALSE;
140     }
141   }
142
143   newpkcs = silc_calloc(1, sizeof(*newpkcs));
144   if (!newpkcs)
145     return FALSE;
146   *newpkcs = *pkcs;
147
148   /* Add to list */
149   if (silc_pkcs_list == NULL)
150     silc_pkcs_list = silc_dlist_init();
151   silc_dlist_add(silc_pkcs_list, newpkcs);
152
153 #endif /* SILC_SYMBIAN */
154   return TRUE;
155 }
156
157 /* Unregister a PKCS from the SILC. */
158
159 SilcBool silc_pkcs_unregister(SilcPKCSObject *pkcs)
160 {
161 #ifndef SILC_SYMBIAN
162   SilcPKCSObject *entry;
163
164   SILC_LOG_DEBUG(("Unregistering PKCS"));
165
166   if (!silc_pkcs_list)
167     return FALSE;
168
169   silc_dlist_start(silc_pkcs_list);
170   while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
171     if (pkcs == SILC_ALL_PKCS || entry == pkcs) {
172       silc_dlist_del(silc_pkcs_list, entry);
173       silc_free(entry);
174
175       if (silc_dlist_count(silc_pkcs_list) == 0) {
176         silc_dlist_uninit(silc_pkcs_list);
177         silc_pkcs_list = NULL;
178       }
179
180       return TRUE;
181     }
182   }
183
184 #endif /* SILC_SYMBIAN */
185   return FALSE;
186 }
187
188 /* Register algorithm */
189
190 SilcBool silc_pkcs_algorithm_register(const SilcPKCSAlgorithm *pkcs)
191 {
192 #ifndef SILC_SYMBIAN
193   SilcPKCSAlgorithm *newalg;
194
195   SILC_LOG_DEBUG(("Registering new PKCS algorithm %s",
196                   pkcs->name));
197
198   /* Check if exists already */
199   if (silc_pkcs_alg_list) {
200     SilcPKCSAlgorithm *entry;
201     silc_dlist_start(silc_pkcs_alg_list);
202     while ((entry = silc_dlist_get(silc_pkcs_alg_list)) != SILC_LIST_END) {
203       if (!strcmp(entry->name, pkcs->name) &&
204           entry->scheme && pkcs->scheme &&
205           !strcmp(entry->scheme, pkcs->scheme))
206         return FALSE;
207     }
208   }
209
210   newalg = silc_calloc(1, sizeof(*newalg));
211   if (!newalg)
212     return FALSE;
213
214   *newalg = *pkcs;
215   newalg->name = strdup(pkcs->name);
216   if (!newalg->name)
217     return FALSE;
218   if (pkcs->scheme) {
219     newalg->scheme = strdup(pkcs->scheme);
220     if (!newalg->scheme)
221       return FALSE;
222   }
223   newalg->hash = strdup(pkcs->hash);
224   if (!newalg->hash)
225     return FALSE;
226
227   /* Add to list */
228   if (silc_pkcs_alg_list == NULL)
229     silc_pkcs_alg_list = silc_dlist_init();
230   silc_dlist_add(silc_pkcs_alg_list, newalg);
231
232 #endif /* SILC_SYMBIAN */
233   return TRUE;
234 }
235
236 /* Unregister algorithm */
237
238 SilcBool silc_pkcs_algorithm_unregister(SilcPKCSAlgorithm *pkcs)
239 {
240 #ifndef SILC_SYMBIAN
241   SilcPKCSAlgorithm*entry;
242
243   SILC_LOG_DEBUG(("Unregistering PKCS algorithm"));
244
245   if (!silc_pkcs_alg_list)
246     return FALSE;
247
248   silc_dlist_start(silc_pkcs_alg_list);
249   while ((entry = silc_dlist_get(silc_pkcs_alg_list)) != SILC_LIST_END) {
250     if (pkcs == SILC_ALL_PKCS_ALG || entry == pkcs) {
251       silc_dlist_del(silc_pkcs_alg_list, entry);
252       silc_free(entry->name);
253       silc_free(entry->scheme);
254       silc_free(entry->hash);
255       silc_free(entry);
256
257       if (silc_dlist_count(silc_pkcs_alg_list) == 0) {
258         silc_dlist_uninit(silc_pkcs_alg_list);
259         silc_pkcs_alg_list = NULL;
260       }
261
262       return TRUE;
263     }
264   }
265
266 #endif /* SILC_SYMBIAN */
267   return FALSE;
268 }
269
270 /* Function that registers all the default PKCS and PKCS algorithms. */
271
272 SilcBool silc_pkcs_register_default(void)
273 {
274 #ifndef SILC_SYMBIAN
275   int i;
276
277   for (i = 0; silc_default_pkcs[i].type; i++)
278     silc_pkcs_register(&(silc_default_pkcs[i]));
279
280   for (i = 0; silc_default_pkcs_alg[i].name; i++)
281     silc_pkcs_algorithm_register(&(silc_default_pkcs_alg[i]));
282
283 #endif /* SILC_SYMBIAN */
284   return TRUE;
285 }
286
287 SilcBool silc_pkcs_unregister_all(void)
288 {
289 #ifndef SILC_SYMBIAN
290   SilcPKCSObject *entry;
291   SilcPKCSAlgorithm *alg;
292
293   if (silc_pkcs_list) {
294     silc_dlist_start(silc_pkcs_list);
295     while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
296       silc_pkcs_unregister(entry);
297       if (!silc_pkcs_list)
298         break;
299     }
300   }
301
302   if (silc_pkcs_alg_list) {
303     silc_dlist_start(silc_pkcs_alg_list);
304     while ((alg = silc_dlist_get(silc_pkcs_alg_list)) != SILC_LIST_END) {
305       silc_pkcs_algorithm_unregister(alg);
306       if (!silc_pkcs_alg_list)
307         break;
308     }
309   }
310
311 #endif /* SILC_SYMBIAN */
312   return TRUE;
313 }
314
315 /* Returns comma separated list of supported PKCS algorithms */
316
317 char *silc_pkcs_get_supported(void)
318 {
319   SilcPKCSAlgorithm *entry;
320   char *list = NULL;
321   int len = 0;
322
323 #ifndef SILC_SYMBIAN
324   if (silc_pkcs_alg_list) {
325     silc_dlist_start(silc_pkcs_alg_list);
326     while ((entry = silc_dlist_get(silc_pkcs_alg_list)) != SILC_LIST_END) {
327       len += strlen(entry->name);
328       list = silc_realloc(list, len + 1);
329       if (!list)
330         return NULL;
331
332       memcpy(list + (len - strlen(entry->name)),
333              entry->name, strlen(entry->name));
334       memcpy(list + len, ",", 1);
335       len++;
336     }
337   }
338 #else
339   {
340     int i;
341     for (i = 0; silc_default_pkcs_alg[i].name; i++) {
342       entry = (SilcPKCSAlgorithm *)&(silc_default_pkcs_alg[i]);
343       len += strlen(entry->name);
344       list = silc_realloc(list, len + 1);
345       if (!list)
346         return NULL;
347
348       memcpy(list + (len - strlen(entry->name)),
349              entry->name, strlen(entry->name));
350       memcpy(list + len, ",", 1);
351       len++;
352     }
353   }
354 #endif /* SILC_SYMBIAN */
355
356   list[len - 1] = 0;
357
358   return list;
359 }
360
361 /* Finds PKCS object */
362
363 const SilcPKCSObject *silc_pkcs_find_pkcs(SilcPKCSType type)
364 {
365   SilcPKCSObject *entry;
366
367 #ifndef SILC_SYMBIAN
368   if (silc_pkcs_list) {
369     silc_dlist_start(silc_pkcs_list);
370     while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
371       if (entry->type == type)
372         return (const SilcPKCSObject *)entry;
373     }
374   }
375 #else
376   {
377     int i;
378     for (i = 0; silc_default_pkcs[i].type; i++) {
379       entry = (SilcPKCSObject *)&(silc_default_pkcs[i]);
380       if (entry->type == type)
381         return (const SilcPKCSObject *)entry;
382     }
383   }
384 #endif /* SILC_SYMBIAN */
385
386   return NULL;
387 }
388
389 /* Finds PKCS algorithms object */
390
391 const SilcPKCSAlgorithm *silc_pkcs_find_algorithm(const char *algorithm,
392                                                   const char *scheme)
393 {
394   SilcPKCSAlgorithm *entry;
395
396 #ifndef SILC_SYMBIAN
397   if (silc_pkcs_alg_list) {
398     silc_dlist_start(silc_pkcs_alg_list);
399     while ((entry = silc_dlist_get(silc_pkcs_alg_list)) != SILC_LIST_END) {
400       if (!strcmp(entry->name, algorithm) &&
401           (!scheme || !entry->scheme || !strcmp(entry->scheme, scheme)))
402         return (const SilcPKCSAlgorithm *)entry;
403     }
404   }
405 #else
406   {
407     int i;
408     for (i = 0; silc_default_pkcs_alg[i].name; i++) {
409       entry = (SilcPKCSAlgorithm *)&(silc_default_pkcs_alg[i]);
410       if (!strcmp(entry->name, algorithm) &&
411           (!scheme || !entry->scheme || !strcmp(entry->scheme, scheme)))
412         return (const SilcPKCSAlgorithm *)entry;
413     }
414   }
415 #endif /* SILC_SYMBIAN */
416
417   return NULL;
418 }
419
420 /* Returns PKCS context */
421
422 const SilcPKCSObject *silc_pkcs_get_pkcs(void *key)
423 {
424   SilcPublicKey public_key = key;
425   return public_key->pkcs;
426 }
427
428 /* Returns PKCS algorithm context */
429
430 const SilcPKCSAlgorithm *silc_pkcs_get_algorithm(void *key)
431 {
432   SilcPublicKey public_key = key;
433   return public_key->pkcs->get_algorithm(public_key->public_key);
434 }
435
436 /* Return algorithm name */
437
438 const char *silc_pkcs_get_name(void *key)
439 {
440   const SilcPKCSAlgorithm *pkcs = silc_pkcs_get_algorithm(key);
441   return pkcs->name;
442 }
443
444 /* Returns PKCS type */
445
446 SilcPKCSType silc_pkcs_get_type(void *key)
447 {
448   SilcPublicKey public_key = key;
449   return public_key->pkcs->type;
450 }
451
452 /* Allocates new public key from the key data */
453
454 SilcBool silc_pkcs_public_key_alloc(SilcPKCSType type,
455                                     unsigned char *key,
456                                     SilcUInt32 key_len,
457                                     SilcPublicKey *ret_public_key)
458 {
459   const SilcPKCSObject *pkcs;
460   SilcPublicKey public_key;
461
462   if (!ret_public_key)
463     return FALSE;
464
465   /* Allocate public key context */
466   public_key = silc_calloc(1, sizeof(*public_key));
467   if (!public_key)
468     return FALSE;
469
470   public_key->pkcs = pkcs = silc_pkcs_find_pkcs(type);
471   if (!public_key->pkcs) {
472     silc_free(public_key);
473     return FALSE;
474   }
475
476   /* Import the PKCS public key */
477   if (!pkcs->import_public_key(key, key_len, &public_key->public_key)) {
478     silc_free(public_key);
479     return FALSE;
480   }
481
482   *ret_public_key = public_key;
483
484   return TRUE;
485 }
486
487 /* Frees the public key */
488
489 void silc_pkcs_public_key_free(SilcPublicKey public_key)
490 {
491   public_key->pkcs->public_key_free(public_key->public_key);
492 }
493
494 /* Exports public key */
495
496 unsigned char *silc_pkcs_public_key_encode(SilcPublicKey public_key,
497                                            SilcUInt32 *ret_len)
498 {
499   return public_key->pkcs->export_public_key(public_key->public_key,
500                                              ret_len);
501 }
502
503 /* Return key length */
504
505 SilcUInt32 silc_pkcs_public_key_get_len(SilcPublicKey public_key)
506 {
507   return public_key->pkcs->public_key_bitlen(public_key->public_key);
508 }
509
510 /* Returns internal PKCS public key context */
511
512 void *silc_pkcs_get_context(SilcPKCSType type, SilcPublicKey public_key)
513 {
514   if (public_key->pkcs->type != type)
515     return FALSE;
516   return public_key->public_key;
517 }
518
519
520 /* Allocates new private key from key data */
521
522 SilcBool silc_pkcs_private_key_alloc(SilcPKCSType type,
523                                      unsigned char *key,
524                                      SilcUInt32 key_len,
525                                      SilcPrivateKey *ret_private_key)
526 {
527   const SilcPKCSObject *pkcs;
528   SilcPrivateKey private_key;
529
530   if (!ret_private_key)
531     return FALSE;
532
533   /* Allocate private key context */
534   private_key = silc_calloc(1, sizeof(*private_key));
535   if (!private_key)
536     return FALSE;
537
538   private_key->pkcs = pkcs = silc_pkcs_find_pkcs(type);
539   if (!private_key->pkcs) {
540     silc_free(private_key);
541     return FALSE;
542   }
543
544   /* Import the PKCS private key */
545   if (!pkcs->import_private_key(key, key_len, &private_key->private_key)) {
546     silc_free(private_key);
547     return FALSE;
548   }
549
550   *ret_private_key = private_key;
551
552   return TRUE;
553 }
554
555 /* Return key length */
556
557 SilcUInt32 silc_pkcs_private_key_get_len(SilcPrivateKey private_key)
558 {
559   return private_key->pkcs->private_key_bitlen(private_key->private_key);
560 }
561
562 /* Frees the private key */
563
564 void silc_pkcs_private_key_free(SilcPrivateKey private_key)
565 {
566   private_key->pkcs->private_key_free(private_key->private_key);
567 }
568
569 /* Encrypts */
570
571 SilcBool silc_pkcs_encrypt(SilcPublicKey public_key,
572                            unsigned char *src, SilcUInt32 src_len,
573                            unsigned char *dst, SilcUInt32 dst_size,
574                            SilcUInt32 *dst_len, SilcRng rng)
575 {
576   return public_key->pkcs->encrypt(public_key->public_key, src, src_len,
577                                    dst, dst_size, dst_len, rng);
578 }
579
580 /* Decrypts */
581
582 SilcBool silc_pkcs_decrypt(SilcPrivateKey private_key,
583                            unsigned char *src, SilcUInt32 src_len,
584                            unsigned char *dst, SilcUInt32 dst_size,
585                            SilcUInt32 *dst_len)
586 {
587   return private_key->pkcs->decrypt(private_key->private_key, src, src_len,
588                                     dst, dst_size, dst_len);
589 }
590
591 /* Generates signature */
592
593 SilcBool silc_pkcs_sign(SilcPrivateKey private_key,
594                         unsigned char *src, SilcUInt32 src_len,
595                         unsigned char *dst, SilcUInt32 dst_size,
596                         SilcUInt32 *dst_len, SilcBool compute_hash,
597                         SilcHash hash)
598 {
599   return private_key->pkcs->sign(private_key->private_key, src, src_len,
600                                  dst, dst_size, dst_len, compute_hash, hash);
601 }
602
603 /* Verifies signature */
604
605 SilcBool silc_pkcs_verify(SilcPublicKey public_key,
606                           unsigned char *signature,
607                           SilcUInt32 signature_len,
608                           unsigned char *data,
609                           SilcUInt32 data_len, SilcHash hash)
610 {
611   return public_key->pkcs->verify(public_key->public_key, signature,
612                                   signature_len, data, data_len, hash);
613 }
614
615 /* Compares two public keys and returns TRUE if they are same key, and
616    FALSE if they are not same. */
617
618 SilcBool silc_pkcs_public_key_compare(SilcPublicKey key1, SilcPublicKey key2)
619 {
620   if (key1->pkcs->type != key2->pkcs->type)
621     return FALSE;
622
623   return key1->pkcs->public_key_compare(key1->public_key, key2->public_key);
624 }
625
626 /* Copies the public key indicated by `public_key' and returns new allocated
627    public key which is indentical to the `public_key'. */
628
629 SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key)
630 {
631   SilcPublicKey key = silc_calloc(1, sizeof(*key));
632   if (!key)
633     return NULL;
634
635   key->pkcs = public_key->pkcs;
636   key->public_key = public_key->pkcs->public_key_copy(public_key->public_key);
637   if (!key->public_key) {
638     silc_free(key);
639     return NULL;
640   }
641
642   return key;
643 }
644
645 /* Loads any kind of public key */
646
647 SilcBool silc_pkcs_load_public_key(const char *filename,
648                                    SilcPublicKey *ret_public_key)
649 {
650   unsigned char *data;
651   SilcUInt32 data_len;
652   SilcPublicKey public_key;
653   SilcPKCSType type;
654
655   SILC_LOG_DEBUG(("Loading public key file '%s'", filename));
656
657   if (!ret_public_key)
658     return FALSE;
659
660   data = silc_file_readfile(filename, &data_len);
661   if (!data)
662     return FALSE;
663
664   /* Allocate public key context */
665   *ret_public_key = public_key = silc_calloc(1, sizeof(*public_key));
666   if (!public_key) {
667     silc_free(data);
668     return FALSE;
669   }
670
671   /* Try loading all types until one succeeds. */
672   for (type = SILC_PKCS_SILC; type <= SILC_PKCS_SPKI; type++) {
673     public_key->pkcs = silc_pkcs_find_pkcs(type);
674     if (!public_key->pkcs)
675       continue;
676
677     if (public_key->pkcs->import_public_key_file(data, data_len,
678                                                  SILC_PKCS_FILE_BASE64,
679                                                  &public_key->public_key)) {
680       silc_free(data);
681       return TRUE;
682     }
683
684     if (public_key->pkcs->import_public_key_file(data, data_len,
685                                                  SILC_PKCS_FILE_BIN,
686                                                  &public_key->public_key)) {
687       silc_free(data);
688       return TRUE;
689     }
690   }
691
692   silc_free(data);
693   silc_free(public_key);
694   return FALSE;
695 }
696
697 /* Saves public key into a file */
698
699 SilcBool silc_pkcs_save_public_key(const char *filename,
700                                    SilcPublicKey public_key,
701                                    SilcPKCSFileEncoding encoding)
702 {
703   unsigned char *data;
704   SilcUInt32 data_len;
705
706   /* Export the public key file */
707   data = public_key->pkcs->export_public_key_file(public_key->public_key,
708                                                   encoding, &data_len);
709   if (!data)
710     return FALSE;
711
712   /* Write to file */
713   if (silc_file_writefile(filename, data, data_len)) {
714     silc_free(data);
715     return FALSE;
716   }
717
718   silc_free(data);
719   return TRUE;
720 }
721
722 /* Loads any kind of private key */
723
724 SilcBool silc_pkcs_load_private_key(const char *filename,
725                                     const unsigned char *passphrase,
726                                     SilcUInt32 passphrase_len,
727                                     SilcPrivateKey *ret_private_key)
728 {
729   unsigned char *data;
730   SilcUInt32 data_len;
731   SilcPrivateKey private_key;
732   SilcPKCSType type;
733
734   SILC_LOG_DEBUG(("Loading private key file '%s'", filename));
735
736   if (!ret_private_key)
737     return FALSE;
738
739   data = silc_file_readfile(filename, &data_len);
740   if (!data)
741     return FALSE;
742
743   /* Allocate private key context */
744   *ret_private_key = private_key = silc_calloc(1, sizeof(*private_key));
745   if (!private_key) {
746     silc_free(data);
747     return FALSE;
748   }
749
750   /* Try loading all types until one succeeds. */
751   for (type = SILC_PKCS_SILC; type <= SILC_PKCS_SPKI; type++) {
752     private_key->pkcs = silc_pkcs_find_pkcs(type);
753     if (!private_key->pkcs)
754       continue;
755
756     if (private_key->pkcs->import_private_key_file(
757                                               data, data_len,
758                                               passphrase,
759                                               passphrase_len,
760                                               SILC_PKCS_FILE_BIN,
761                                               &private_key->private_key)) {
762       silc_free(data);
763       return TRUE;
764     }
765
766     if (private_key->pkcs->import_private_key_file(
767                                               data, data_len,
768                                               passphrase,
769                                               passphrase_len,
770                                               SILC_PKCS_FILE_BASE64,
771                                               &private_key->private_key)) {
772       silc_free(data);
773       return TRUE;
774     }
775   }
776
777   silc_free(data);
778   silc_free(private_key);
779   return FALSE;
780 }
781
782 /* Saves private key into a file */
783
784 SilcBool silc_pkcs_save_private_key(const char *filename,
785                                     SilcPrivateKey private_key,
786                                     const unsigned char *passphrase,
787                                     SilcUInt32 passphrase_len,
788                                     SilcPKCSFileEncoding encoding,
789                                     SilcRng rng)
790 {
791   unsigned char *data;
792   SilcUInt32 data_len;
793
794   /* Export the private key file */
795   data = private_key->pkcs->export_private_key_file(private_key->private_key,
796                                                     passphrase,
797                                                     passphrase_len,
798                                                     encoding, rng, &data_len);
799   if (!data)
800     return FALSE;
801
802   /* Write to file */
803   if (silc_file_writefile(filename, data, data_len)) {
804     silc_free(data);
805     return FALSE;
806   }
807
808   silc_free(data);
809   return TRUE;
810 }