5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
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.
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.
21 #include "silcincludes.h"
26 /* The main SILC PKCS structure. */
27 struct SilcPKCSStruct {
28 void *context; /* Algorithm internal context */
29 SilcPKCSObject *pkcs; /* Algorithm implementation */
30 SilcUInt32 key_len; /* Key length in bits */
34 /* Dynamically registered list of PKCS. */
35 SilcDList silc_pkcs_list = NULL;
36 #define SILC_PKCS_LIST silc_pkcs_list
38 #define SILC_PKCS_LIST TRUE
39 #endif /* SILC_EPOC */
41 /* Static list of PKCS for silc_pkcs_register_default(). */
42 const SilcPKCSObject silc_default_pkcs[] =
44 /* RSA with PKCS #1 (Uses directly routines from Raw RSA operations) */
46 silc_rsa_init, silc_rsa_clear_keys, silc_rsa_get_public_key,
47 silc_rsa_get_private_key, silc_rsa_set_public_key,
48 silc_rsa_set_private_key, silc_rsa_context_len,
49 silc_pkcs1_encrypt, silc_pkcs1_decrypt,
50 silc_pkcs1_sign, silc_pkcs1_verify },
52 /* Raw RSA operations */
54 silc_rsa_init, silc_rsa_clear_keys, silc_rsa_get_public_key,
55 silc_rsa_get_private_key, silc_rsa_set_public_key,
56 silc_rsa_set_private_key, silc_rsa_context_len,
57 silc_rsa_encrypt, silc_rsa_decrypt,
58 silc_rsa_sign, silc_rsa_verify },
60 { NULL, NULL, NULL, NULL, NULL,
61 NULL, NULL, NULL, NULL, NULL, NULL }
64 /* Register a new PKCS into SILC. This is used at the initialization of
67 bool silc_pkcs_register(const SilcPKCSObject *pkcs)
72 SILC_LOG_DEBUG(("Registering new PKCS `%s'", pkcs->name));
74 /* Check if exists already */
76 SilcPKCSObject *entry;
77 silc_dlist_start(silc_pkcs_list);
78 while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
79 if (!strcmp(entry->name, pkcs->name))
84 new = silc_calloc(1, sizeof(*new));
85 new->name = strdup(pkcs->name);
86 new->init = pkcs->init;
87 new->clear_keys = pkcs->clear_keys;
88 new->get_public_key = pkcs->get_public_key;
89 new->get_private_key = pkcs->get_private_key;
90 new->set_public_key = pkcs->set_public_key;
91 new->set_private_key = pkcs->set_private_key;
92 new->context_len = pkcs->context_len;
93 new->encrypt = pkcs->encrypt;
94 new->decrypt = pkcs->decrypt;
95 new->sign = pkcs->sign;
96 new->verify = pkcs->verify;
99 if (silc_pkcs_list == NULL)
100 silc_pkcs_list = silc_dlist_init();
101 silc_dlist_add(silc_pkcs_list, new);
103 #endif /* SILC_EPOC */
107 /* Unregister a PKCS from the SILC. */
109 bool silc_pkcs_unregister(SilcPKCSObject *pkcs)
112 SilcPKCSObject *entry;
114 SILC_LOG_DEBUG(("Unregistering PKCS"));
119 silc_dlist_start(silc_pkcs_list);
120 while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
121 if (pkcs == SILC_ALL_PKCS || entry == pkcs) {
122 silc_dlist_del(silc_pkcs_list, entry);
123 silc_free(entry->name);
126 if (silc_dlist_count(silc_pkcs_list) == 0) {
127 silc_dlist_uninit(silc_pkcs_list);
128 silc_pkcs_list = NULL;
135 #endif /* SILC_EPOC */
139 /* Function that registers all the default PKCS (all builtin PKCS).
140 The application may use this to register the default PKCS if specific
141 PKCS in any specific order is not wanted. */
143 bool silc_pkcs_register_default(void)
148 for (i = 0; silc_default_pkcs[i].name; i++)
149 silc_pkcs_register(&(silc_default_pkcs[i]));
151 #endif /* SILC_EPOC */
155 bool silc_pkcs_unregister_all(void)
158 SilcPKCSObject *entry;
163 silc_dlist_start(silc_pkcs_list);
164 while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
165 silc_pkcs_unregister(entry);
169 #endif /* SILC_EPOC */
173 /* Allocates a new SilcPKCS object. The new allocated object is returned
174 to the 'new_pkcs' argument. */
176 bool silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs)
178 SilcPKCSObject *entry = NULL;
180 SILC_LOG_DEBUG(("Allocating new PKCS object"));
183 if (silc_pkcs_list) {
184 silc_dlist_start(silc_pkcs_list);
185 while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
186 if (!strcmp(entry->name, name))
192 /* On EPOC which don't have globals we check our constant hash list. */
194 for (i = 0; silc_default_pkcs[i].name; i++) {
195 if (!strcmp(silc_default_pkcs[i].name, name)) {
196 entry = (SilcPKCSObject *)&(silc_default_pkcs[i]);
201 #endif /* SILC_EPOC */
204 *new_pkcs = silc_calloc(1, sizeof(**new_pkcs));
205 (*new_pkcs)->pkcs = entry;
206 (*new_pkcs)->context = silc_calloc(1, entry->context_len());
213 /* Free's the PKCS object */
215 void silc_pkcs_free(SilcPKCS pkcs)
218 pkcs->pkcs->clear_keys(pkcs->context);
219 silc_free(pkcs->context);
224 /* Return TRUE if PKCS algorithm `name' is supported. */
226 bool silc_pkcs_is_supported(const unsigned char *name)
229 SilcPKCSObject *entry;
231 if (silc_pkcs_list) {
232 silc_dlist_start(silc_pkcs_list);
233 while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
234 if (!strcmp(entry->name, name))
241 for (i = 0; silc_default_pkcs[i].name; i++)
242 if (!strcmp(silc_default_pkcs[i].name, name))
245 #endif /* SILC_EPOC */
249 /* Returns comma separated list of supported PKCS algorithms */
251 char *silc_pkcs_get_supported(void)
253 SilcPKCSObject *entry;
258 if (silc_pkcs_list) {
259 silc_dlist_start(silc_pkcs_list);
260 while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
261 len += strlen(entry->name);
262 list = silc_realloc(list, len + 1);
264 memcpy(list + (len - strlen(entry->name)),
265 entry->name, strlen(entry->name));
266 memcpy(list + len, ",", 1);
273 for (i = 0; silc_default_pkcs[i].name; i++) {
274 entry = (SilcPKCSObject *)&(silc_default_pkcs[i]);
275 len += strlen(entry->name);
276 list = silc_realloc(list, len + 1);
278 memcpy(list + (len - strlen(entry->name)),
279 entry->name, strlen(entry->name));
280 memcpy(list + len, ",", 1);
284 #endif /* SILC_EPOC */
291 /* Generate new key pair into the `pkcs' context. */
293 bool silc_pkcs_generate_key(SilcPKCS pkcs, SilcUInt32 bits_key_len,
296 return pkcs->pkcs->init(pkcs->context, bits_key_len, rng);
299 /* Returns the length of the key */
301 SilcUInt32 silc_pkcs_get_key_len(SilcPKCS pkcs)
303 return pkcs->key_len;
306 const char *silc_pkcs_get_name(SilcPKCS pkcs)
308 return pkcs->pkcs->name;
311 /* Returns SILC style public key */
313 unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, SilcUInt32 *len)
315 return pkcs->pkcs->get_public_key(pkcs->context, len);
318 /* Returns SILC style private key */
320 unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs, SilcUInt32 *len)
322 return pkcs->pkcs->get_private_key(pkcs->context, len);
325 /* Sets public key from SilcPublicKey. */
327 SilcUInt32 silc_pkcs_public_key_set(SilcPKCS pkcs, SilcPublicKey public_key)
329 pkcs->key_len = pkcs->pkcs->set_public_key(pkcs->context, public_key->pk,
331 return pkcs->key_len;
334 /* Sets public key from data. */
336 SilcUInt32 silc_pkcs_public_key_data_set(SilcPKCS pkcs, unsigned char *pk,
339 pkcs->key_len = pkcs->pkcs->set_public_key(pkcs->context, pk, pk_len);
340 return pkcs->key_len;
343 /* Sets private key from SilcPrivateKey. */
345 SilcUInt32 silc_pkcs_private_key_set(SilcPKCS pkcs, SilcPrivateKey private_key)
348 key_len = pkcs->pkcs->set_private_key(pkcs->context, private_key->prv,
349 private_key->prv_len);
351 pkcs->key_len = key_len;
352 return pkcs->key_len;
355 /* Sets private key from data. */
357 SilcUInt32 silc_pkcs_private_key_data_set(SilcPKCS pkcs, unsigned char *prv,
361 key_len = pkcs->pkcs->set_private_key(pkcs->context, prv, prv_len);
363 pkcs->key_len = key_len;
364 return pkcs->key_len;
369 bool silc_pkcs_encrypt(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
370 unsigned char *dst, SilcUInt32 *dst_len)
372 return pkcs->pkcs->encrypt(pkcs->context, src, src_len, dst, dst_len);
377 bool silc_pkcs_decrypt(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
378 unsigned char *dst, SilcUInt32 *dst_len)
380 return pkcs->pkcs->decrypt(pkcs->context, src, src_len, dst, dst_len);
383 /* Generates signature */
385 bool silc_pkcs_sign(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
386 unsigned char *dst, SilcUInt32 *dst_len)
388 return pkcs->pkcs->sign(pkcs->context, src, src_len, dst, dst_len);
391 /* Verifies signature */
393 bool silc_pkcs_verify(SilcPKCS pkcs, unsigned char *signature,
394 SilcUInt32 signature_len, unsigned char *data,
397 return pkcs->pkcs->verify(pkcs->context, signature, signature_len,
401 /* Generates signature with hash. The hash is signed. */
403 bool silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
404 unsigned char *src, SilcUInt32 src_len,
405 unsigned char *dst, SilcUInt32 *dst_len)
407 unsigned char hashr[32];
411 silc_hash_make(hash, src, src_len, hashr);
412 hash_len = silc_hash_len(hash);
414 SILC_LOG_HEXDUMP(("Hash"), hashr, hash_len);
416 ret = pkcs->pkcs->sign(pkcs->context, hashr, hash_len, dst, dst_len);
417 memset(hashr, 0, sizeof(hashr));
422 /* Verifies signature with hash. The `data' is hashed and verified against
425 bool silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash,
426 unsigned char *signature,
427 SilcUInt32 signature_len,
431 unsigned char hashr[32];
435 silc_hash_make(hash, data, data_len, hashr);
436 hash_len = silc_hash_len(hash);
438 SILC_LOG_HEXDUMP(("Hash"), hashr, hash_len);
440 ret = pkcs->pkcs->verify(pkcs->context, signature, signature_len,
442 memset(hashr, 0, sizeof(hashr));
447 /* Encodes and returns SILC public key identifier. If some of the
448 arguments is NULL those are not encoded into the identifier string.
449 Protocol says that at least username and host must be provided. */
451 char *silc_pkcs_encode_identifier(char *username, char *host, char *realname,
452 char *email, char *org, char *country)
456 SilcUInt32 len, tlen = 0;
458 if (!username || !host)
461 len = (username ? strlen(username) : 0) +
462 (host ? strlen(host) : 0) +
463 (realname ? strlen(realname) : 0) +
464 (email ? strlen(email) : 0) +
465 (org ? strlen(org) : 0) +
466 (country ? strlen(country) : 0);
471 len += 3 + 5 + 5 + 4 + 4 + 4;
472 buf = silc_buffer_alloc(len);
473 silc_buffer_pull_tail(buf, len);
476 silc_buffer_format(buf,
477 SILC_STR_UI32_STRING("UN="),
478 SILC_STR_UI32_STRING(username),
480 silc_buffer_pull(buf, 3 + strlen(username));
481 tlen = 3 + strlen(username);
485 silc_buffer_format(buf,
486 SILC_STR_UI32_STRING(", "),
487 SILC_STR_UI32_STRING("HN="),
488 SILC_STR_UI32_STRING(host),
490 silc_buffer_pull(buf, 5 + strlen(host));
491 tlen += 5 + strlen(host);
495 silc_buffer_format(buf,
496 SILC_STR_UI32_STRING(", "),
497 SILC_STR_UI32_STRING("RN="),
498 SILC_STR_UI32_STRING(realname),
500 silc_buffer_pull(buf, 5 + strlen(realname));
501 tlen += 5 + strlen(realname);
505 silc_buffer_format(buf,
506 SILC_STR_UI32_STRING(", "),
507 SILC_STR_UI32_STRING("E="),
508 SILC_STR_UI32_STRING(email),
510 silc_buffer_pull(buf, 4 + strlen(email));
511 tlen += 4 + strlen(email);
515 silc_buffer_format(buf,
516 SILC_STR_UI32_STRING(", "),
517 SILC_STR_UI32_STRING("O="),
518 SILC_STR_UI32_STRING(org),
520 silc_buffer_pull(buf, 4 + strlen(org));
521 tlen += 4 + strlen(org);
525 silc_buffer_format(buf,
526 SILC_STR_UI32_STRING(", "),
527 SILC_STR_UI32_STRING("C="),
528 SILC_STR_UI32_STRING(country),
530 silc_buffer_pull(buf, 4 + strlen(country));
531 tlen += 4 + strlen(country);
534 silc_buffer_push(buf, buf->data - buf->head);
535 identifier = silc_calloc(tlen + 1, sizeof(*identifier));
536 memcpy(identifier, buf->data, tlen);
537 silc_buffer_free(buf);
542 /* Decodes the provided `identifier' and returns allocated context for
545 SilcPublicKeyIdentifier silc_pkcs_decode_identifier(char *identifier)
547 SilcPublicKeyIdentifier ident;
551 ident = silc_calloc(1, sizeof(*ident));
555 len = strcspn(cp, ",");
556 if (len - 1 >= 0 && cp[len - 1] == '\\') {
559 len = strcspn(cp, ",") + len;
560 if (len - 1 >= 0 && cp[len - 1] != '\\')
565 item = silc_calloc(len + 1, sizeof(char));
566 memcpy(item, cp, len);
568 if (strstr(item, "UN="))
569 ident->username = strdup(item + strcspn(cp, "=") + 1);
570 else if (strstr(item, "HN="))
571 ident->host = strdup(item + strcspn(cp, "=") + 1);
572 else if (strstr(item, "RN="))
573 ident->realname = strdup(item + strcspn(cp, "=") + 1);
574 else if (strstr(item, "E="))
575 ident->email = strdup(item + strcspn(cp, "=") + 1);
576 else if (strstr(item, "O="))
577 ident->org = strdup(item + strcspn(cp, "=") + 1);
578 else if (strstr(item, "C="))
579 ident->country = strdup(item + strcspn(cp, "=") + 1);
594 /* Free's decoded public key identifier context. Call this to free the
595 context returned by the silc_pkcs_decode_identifier. */
597 void silc_pkcs_free_identifier(SilcPublicKeyIdentifier identifier)
599 silc_free(identifier->username);
600 silc_free(identifier->host);
601 silc_free(identifier->realname);
602 silc_free(identifier->email);
603 silc_free(identifier->org);
604 silc_free(identifier->country);
605 silc_free(identifier);
608 /* Allocates SILC style public key formed from sent arguments. All data
611 SilcPublicKey silc_pkcs_public_key_alloc(const char *name,
612 const char *identifier,
613 const unsigned char *pk,
616 SilcPublicKey public_key;
619 public_key = silc_calloc(1, sizeof(*public_key));
620 public_key->name = strdup(name);
621 public_key->pk_len = pk_len;
622 public_key->pk = silc_calloc(pk_len, sizeof(*public_key->pk));
623 public_key->pk_type = SILC_SKE_PK_TYPE_SILC;
624 memcpy(public_key->pk, pk, pk_len);
626 if (!silc_utf8_valid(identifier, strlen(identifier))) {
627 int len = silc_utf8_encoded_len(identifier, strlen(identifier), 0);
628 tmp = silc_calloc(len + 1, sizeof(*tmp));
629 silc_utf8_encode(identifier, strlen(identifier), 0, tmp, len);
633 public_key->identifier = strdup(identifier);
634 public_key->len = 4 + 2 + strlen(name) + 2 + strlen(identifier) + pk_len;
640 /* Free's public key */
642 void silc_pkcs_public_key_free(SilcPublicKey public_key)
645 silc_free(public_key->name);
646 silc_free(public_key->identifier);
647 silc_free(public_key->pk);
648 silc_free(public_key);
652 /* Allocates SILC private key formed from sent arguments. All data is
655 SilcPrivateKey silc_pkcs_private_key_alloc(const char *name,
656 const unsigned char *prv,
659 SilcPrivateKey private_key;
661 private_key = silc_calloc(1, sizeof(*private_key));
662 private_key->name = strdup(name);
663 private_key->prv_len = prv_len;
664 private_key->prv = silc_calloc(prv_len, sizeof(*private_key->prv));
665 memcpy(private_key->prv, prv, prv_len);
670 /* Free's private key */
672 void silc_pkcs_private_key_free(SilcPrivateKey private_key)
675 silc_free(private_key->name);
676 silc_free(private_key->prv);
677 silc_free(private_key);
681 /* Encodes SILC style public key from SilcPublicKey. Returns the encoded
685 silc_pkcs_public_key_encode(SilcPublicKey public_key, SilcUInt32 *len)
690 buf = silc_buffer_alloc(public_key->len);
691 silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
693 silc_buffer_format(buf,
694 SILC_STR_UI_INT(public_key->len),
695 SILC_STR_UI_SHORT(strlen(public_key->name)),
696 SILC_STR_UI32_STRING(public_key->name),
697 SILC_STR_UI_SHORT(strlen(public_key->identifier)),
698 SILC_STR_UI32_STRING(public_key->identifier),
699 SILC_STR_UI_XNSTRING(public_key->pk,
703 *len = public_key->len;
705 ret = silc_calloc(buf->len, sizeof(*ret));
706 memcpy(ret, buf->data, buf->len);
707 silc_buffer_free(buf);
712 /* Encodes SILC style public key. Returns the encoded data. */
715 silc_pkcs_public_key_data_encode(unsigned char *pk, SilcUInt32 pk_len,
716 char *pkcs, char *identifier,
723 totlen = 4 + 2 + strlen(pkcs) + 2 + strlen(identifier) + pk_len;
724 buf = silc_buffer_alloc(totlen);
725 silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
727 silc_buffer_format(buf,
728 SILC_STR_UI_INT(totlen),
729 SILC_STR_UI_SHORT(strlen(pkcs)),
730 SILC_STR_UI32_STRING(pkcs),
731 SILC_STR_UI_SHORT(strlen(identifier)),
732 SILC_STR_UI32_STRING(identifier),
733 SILC_STR_UI_XNSTRING(pk, pk_len),
738 ret = silc_calloc(buf->len, sizeof(*ret));
739 memcpy(ret, buf->data, buf->len);
740 silc_buffer_free(buf);
745 /* Decodes SILC style public key. Returns TRUE if the decoding was
746 successful. Allocates new public key as well. */
748 bool silc_pkcs_public_key_decode(unsigned char *data, SilcUInt32 data_len,
749 SilcPublicKey *public_key)
753 SilcUInt16 pkcs_len, identifier_len;
754 SilcUInt32 totlen, key_len;
755 unsigned char *pkcs_name = NULL, *ident = NULL, *key_data = NULL;
758 buf = silc_buffer_alloc(data_len);
759 silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
760 silc_buffer_put(buf, data, data_len);
763 ret = silc_buffer_unformat(buf,
764 SILC_STR_UI_INT(&totlen),
767 silc_buffer_free(buf);
771 if (totlen != data_len) {
772 silc_buffer_free(buf);
776 /* Get algorithm name and identifier */
777 silc_buffer_pull(buf, 4);
779 silc_buffer_unformat(buf,
780 SILC_STR_UI16_NSTRING_ALLOC(&pkcs_name, &pkcs_len),
781 SILC_STR_UI16_NSTRING_ALLOC(&ident, &identifier_len),
786 if (pkcs_len < 1 || identifier_len < 3 ||
787 pkcs_len + identifier_len > totlen)
790 /* See if we support this algorithm (check only if PKCS are registered) */
791 if (SILC_PKCS_LIST && !silc_pkcs_is_supported(pkcs_name)) {
792 SILC_LOG_DEBUG(("Unknown PKCS %s", pkcs_name));
796 /* Protocol says that at least UN and HN must be provided as identifier,
798 if (!strstr(ident, "UN=") && !strstr(ident, "HN=")) {
799 SILC_LOG_DEBUG(("The public does not have the required UN= and HN= "
804 /* Get key data. We assume that rest of the buffer is key data. */
805 silc_buffer_pull(buf, 2 + pkcs_len + 2 + identifier_len);
807 ret = silc_buffer_unformat(buf,
808 SILC_STR_UI_XNSTRING_ALLOC(&key_data, key_len),
813 /* Try to set the key. If this fails the key must be malformed. This
814 code assumes that the PKCS routine checks the format of the key.
815 (check only if PKCS are registered) */
816 if (SILC_PKCS_LIST) {
817 silc_pkcs_alloc(pkcs_name, &alg);
818 if (!alg->pkcs->set_public_key(alg->context, key_data, key_len))
824 *public_key = silc_calloc(1, sizeof(**public_key));
825 (*public_key)->len = totlen;
826 (*public_key)->name = pkcs_name;
827 (*public_key)->identifier = ident;
828 (*public_key)->pk = key_data;
829 (*public_key)->pk_len = key_len;
830 (*public_key)->pk_type = SILC_SKE_PK_TYPE_SILC;
833 silc_buffer_free(buf);
838 silc_free(pkcs_name);
843 silc_buffer_free(buf);
847 /* Encodes Public Key Payload for transmitting public keys and certificates. */
849 SilcBuffer silc_pkcs_public_key_payload_encode(SilcPublicKey public_key)
858 pk = silc_pkcs_public_key_encode(public_key, &pk_len);
862 buffer = silc_buffer_alloc_size(4 + pk_len);
868 silc_buffer_format(buffer,
869 SILC_STR_UI_SHORT(pk_len),
870 SILC_STR_UI_SHORT(public_key->pk_type),
871 SILC_STR_UI_XNSTRING(pk, pk_len),
878 /* Decode Public Key Payload and decodes the public key inside it to
881 bool silc_pkcs_public_key_payload_decode(unsigned char *data,
883 SilcPublicKey *public_key)
885 SilcBufferStruct buf;
886 SilcUInt16 pk_len, pk_type;
894 /* XXX 1.1 version support. Check whether the data is actually raw
895 public key and attempt to decode. Remove this later! */
896 if (silc_pkcs_public_key_decode(data, data_len, public_key)) {
897 (*public_key)->pk_type = SILC_SKE_PK_TYPE_SILC;
902 silc_buffer_set(&buf, data, data_len);
903 ret = silc_buffer_unformat(&buf,
904 SILC_STR_UI_SHORT(&pk_len),
905 SILC_STR_UI_SHORT(&pk_type),
907 if (ret < 0 || pk_len > data_len - 4)
910 /* For now we support only SILC public keys */
911 if (pk_type != SILC_SKE_PK_TYPE_SILC)
914 silc_buffer_pull(&buf, 4);
915 ret = silc_buffer_unformat(&buf,
916 SILC_STR_UI_XNSTRING(&pk, pk_len),
918 silc_buffer_push(&buf, 4);
922 if (!silc_pkcs_public_key_decode(pk, pk_len, public_key))
924 (*public_key)->pk_type = SILC_SKE_PK_TYPE_SILC;
929 /* Compares two public keys and returns TRUE if they are same key, and
930 FALSE if they are not same. */
932 bool silc_pkcs_public_key_compare(SilcPublicKey key1, SilcPublicKey key2)
937 if (key1->len == key2->len &&
938 key1->name && key2->name && key1->identifier && key2->identifier &&
939 !strcmp(key1->name, key2->name) &&
940 !strcmp(key1->identifier, key2->identifier) &&
941 !memcmp(key1->pk, key2->pk, key1->pk_len) &&
942 key1->pk_len == key2->pk_len)
948 /* Copies the public key indicated by `public_key' and returns new allocated
949 public key which is indentical to the `public_key'. */
951 SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key)
953 SilcPublicKey key = silc_calloc(1, sizeof(*key));
957 key->len = public_key->len;
958 key->name = silc_memdup(public_key->name, strlen(public_key->name));
959 key->identifier = silc_memdup(public_key->identifier,
960 strlen(public_key->identifier));
961 key->pk = silc_memdup(public_key->pk, public_key->pk_len);
962 key->pk_len = public_key->pk_len;
963 key->pk_type = public_key->pk_type;
968 /* Encodes SILC private key from SilcPrivateKey. Returns the encoded data. */
971 silc_pkcs_private_key_encode(SilcPrivateKey private_key, SilcUInt32 *len)
977 totlen = 2 + strlen(private_key->name) + private_key->prv_len;
978 buf = silc_buffer_alloc(totlen);
979 silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
981 silc_buffer_format(buf,
982 SILC_STR_UI_SHORT(strlen(private_key->name)),
983 SILC_STR_UI32_STRING(private_key->name),
984 SILC_STR_UI_XNSTRING(private_key->prv,
985 private_key->prv_len),
990 ret = silc_calloc(buf->len, sizeof(*ret));
991 memcpy(ret, buf->data, buf->len);
992 silc_buffer_clear(buf);
993 silc_buffer_free(buf);
998 /* Encodes SILC private key. Returns the encoded data. */
1001 silc_pkcs_private_key_data_encode(unsigned char *prv, SilcUInt32 prv_len,
1002 char *pkcs, SilcUInt32 *len)
1008 totlen = 2 + strlen(pkcs) + prv_len;
1009 buf = silc_buffer_alloc(totlen);
1010 silc_buffer_pull_tail(buf, totlen);
1012 silc_buffer_format(buf,
1013 SILC_STR_UI_SHORT(strlen(pkcs)),
1014 SILC_STR_UI32_STRING(pkcs),
1015 SILC_STR_UI_XNSTRING(prv, prv_len),
1020 ret = silc_calloc(buf->len, sizeof(*ret));
1021 memcpy(ret, buf->data, buf->len);
1022 silc_buffer_clear(buf);
1023 silc_buffer_free(buf);
1028 /* Decodes SILC style private key. Returns TRUE if the decoding was
1029 successful. Allocates new private key as well. */
1031 bool silc_pkcs_private_key_decode(unsigned char *data, SilcUInt32 data_len,
1032 SilcPrivateKey *private_key)
1036 SilcUInt16 pkcs_len;
1038 unsigned char *pkcs_name = NULL, *key_data = NULL;
1041 buf = silc_buffer_alloc(data_len);
1042 silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
1043 silc_buffer_put(buf, data, data_len);
1045 /* Get algorithm name and identifier */
1047 silc_buffer_unformat(buf,
1048 SILC_STR_UI16_NSTRING_ALLOC(&pkcs_name, &pkcs_len),
1051 SILC_LOG_DEBUG(("Cannot decode private key buffer"));
1055 if (pkcs_len < 1 || pkcs_len > buf->truelen) {
1056 SILC_LOG_DEBUG(("Malformed private key buffer"));
1060 /* See if we support this algorithm (check only if PKCS are registered). */
1061 if (SILC_PKCS_LIST && !silc_pkcs_is_supported(pkcs_name)) {
1062 SILC_LOG_DEBUG(("Unknown PKCS `%s'", pkcs_name));
1066 /* Get key data. We assume that rest of the buffer is key data. */
1067 silc_buffer_pull(buf, 2 + pkcs_len);
1069 ret = silc_buffer_unformat(buf,
1070 SILC_STR_UI_XNSTRING_ALLOC(&key_data, key_len),
1075 /* Try to set the key. If this fails the key must be malformed. This
1076 code assumes that the PKCS routine checks the format of the key.
1077 (check only if PKCS are registered) */
1078 if (SILC_PKCS_LIST) {
1079 silc_pkcs_alloc(pkcs_name, &alg);
1080 if (!alg->pkcs->set_private_key(alg->context, key_data, key_len)) {
1081 SILC_LOG_DEBUG(("Could not set private key data"));
1084 silc_pkcs_free(alg);
1088 *private_key = silc_calloc(1, sizeof(**private_key));
1089 (*private_key)->name = pkcs_name;
1090 (*private_key)->prv = key_data;
1091 (*private_key)->prv_len = key_len;
1094 silc_buffer_clear(buf);
1095 silc_buffer_free(buf);
1100 silc_free(pkcs_name);
1102 silc_free(key_data);
1103 silc_buffer_clear(buf);
1104 silc_buffer_free(buf);
1108 /* Internal routine to save public key */
1110 static bool silc_pkcs_save_public_key_internal(char *filename,
1111 unsigned char *data,
1112 SilcUInt32 data_len,
1113 SilcUInt32 encoding)
1119 case SILC_PKCS_FILE_BIN:
1121 case SILC_PKCS_FILE_PEM:
1122 data = silc_pem_encode_file(data, data_len);
1123 data_len = strlen(data);
1127 len = data_len + (strlen(SILC_PKCS_PUBLIC_KEYFILE_BEGIN) +
1128 strlen(SILC_PKCS_PUBLIC_KEYFILE_END));
1129 buf = silc_buffer_alloc(len);
1130 silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
1132 silc_buffer_format(buf,
1133 SILC_STR_UI32_STRING(SILC_PKCS_PUBLIC_KEYFILE_BEGIN),
1134 SILC_STR_UI_XNSTRING(data, data_len),
1135 SILC_STR_UI32_STRING(SILC_PKCS_PUBLIC_KEYFILE_END),
1138 /* Save into file */
1139 if (silc_file_writefile(filename, buf->data, buf->len)) {
1140 silc_buffer_free(buf);
1144 silc_buffer_free(buf);
1148 /* Saves public key into file */
1150 bool silc_pkcs_save_public_key(char *filename, SilcPublicKey public_key,
1151 SilcUInt32 encoding)
1153 unsigned char *data;
1154 SilcUInt32 data_len;
1157 data = silc_pkcs_public_key_encode(public_key, &data_len);
1158 ret = silc_pkcs_save_public_key_internal(filename, data, data_len,
1164 /* Saves public key into file */
1166 bool silc_pkcs_save_public_key_data(char *filename, unsigned char *data,
1167 SilcUInt32 data_len,
1168 SilcUInt32 encoding)
1170 return silc_pkcs_save_public_key_internal(filename, data, data_len,
1174 #define SILC_PKCS_PRIVATE_KEY_MAGIC 0x738df531
1176 /* Internal routine to save private key. */
1178 static bool silc_pkcs_save_private_key_internal(char *filename,
1179 unsigned char *data,
1180 SilcUInt32 data_len,
1183 SilcUInt32 encoding)
1188 SilcBuffer buf, enc;
1189 SilcUInt32 len, blocklen;
1190 unsigned char tmp[32], keymat[32];
1193 memset(tmp, 0, sizeof(tmp));
1194 memset(keymat, 0, sizeof(keymat));
1196 /* Allocate the AES cipher */
1197 if (!silc_cipher_alloc("aes-256-cbc", &aes)) {
1198 SILC_LOG_ERROR(("Could not allocate AES cipher, probably not registered"));
1201 blocklen = silc_cipher_get_block_len(aes);
1202 if (blocklen * 2 > sizeof(tmp))
1205 /* Allocate MD5 hash */
1206 if (!silc_hash_alloc("md5", &md5)) {
1207 SILC_LOG_ERROR(("Could not allocate MD5 hash, probably not registered"));
1208 silc_cipher_free(aes);
1213 if (!silc_hmac_alloc("hmac-sha1-96", NULL, &sha1hmac)) {
1214 SILC_LOG_ERROR(("Could not allocate SHA1 HMAC, probably not registered"));
1215 silc_hash_free(md5);
1216 silc_cipher_free(aes);
1220 /* Derive the encryption key from the provided key material. The key
1221 is 256 bits length, and derived by taking hash of the data, then
1222 re-hashing the data and the previous digest, and using the first and
1223 second digest as the key. */
1224 silc_hash_init(md5);
1225 silc_hash_update(md5, key, key_len);
1226 silc_hash_final(md5, keymat);
1227 silc_hash_init(md5);
1228 silc_hash_update(md5, key, key_len);
1229 silc_hash_update(md5, keymat, 16);
1230 silc_hash_final(md5, keymat + 16);
1232 /* Set the key to the cipher */
1233 silc_cipher_set_key(aes, keymat, sizeof(keymat) * 8);
1235 /* Encode the buffer to be encrypted. Add padding to it too, at least
1236 block size of the cipher. */
1238 /* Allocate buffer for encryption */
1239 len = silc_hmac_len(sha1hmac);
1240 enc = silc_buffer_alloc_size(data_len + 4 + 4 +
1241 (blocklen + (data_len % blocklen)) + len);
1243 silc_hmac_free(sha1hmac);
1244 silc_hash_free(md5);
1245 silc_cipher_free(aes);
1249 /* Generate padding */
1250 for (i = 0; i < blocklen + (data_len % blocklen); i++)
1251 tmp[i] = silc_rng_global_get_byte_fast();
1253 /* Put magic number */
1254 SILC_PUT32_MSB(SILC_PKCS_PRIVATE_KEY_MAGIC, enc->data);
1255 silc_buffer_pull(enc, 4);
1257 /* Encode the buffer */
1258 silc_buffer_format(enc,
1259 SILC_STR_UI_INT(data_len),
1260 SILC_STR_UI_XNSTRING(data, data_len),
1261 SILC_STR_UI_XNSTRING(tmp, blocklen + (data_len %
1266 silc_cipher_encrypt(aes, enc->data, enc->data, enc->len - len,
1267 silc_cipher_get_iv(aes));
1269 silc_buffer_push(enc, 4);
1271 /* Compute HMAC over the encrypted data and append the MAC to data.
1272 The key is the first digest of the original key material. */
1273 data_len = enc->len - len;
1274 silc_hmac_init_with_key(sha1hmac, keymat, 16);
1275 silc_hmac_update(sha1hmac, enc->data, data_len);
1276 silc_buffer_pull(enc, data_len);
1277 silc_hmac_final(sha1hmac, enc->data, NULL);
1278 silc_buffer_push(enc, data_len);
1281 memset(keymat, 0, sizeof(keymat));
1282 memset(tmp, 0, sizeof(tmp));
1283 silc_hmac_free(sha1hmac);
1284 silc_hash_free(md5);
1285 silc_cipher_free(aes);
1288 data_len = enc->len;
1291 case SILC_PKCS_FILE_BIN:
1293 case SILC_PKCS_FILE_PEM:
1294 data = silc_pem_encode_file(data, data_len);
1295 data_len = strlen(data);
1299 /* Encode the data and save to file */
1300 len = data_len + (strlen(SILC_PKCS_PRIVATE_KEYFILE_BEGIN) +
1301 strlen(SILC_PKCS_PRIVATE_KEYFILE_END));
1302 buf = silc_buffer_alloc_size(len);
1303 silc_buffer_format(buf,
1304 SILC_STR_UI32_STRING(SILC_PKCS_PRIVATE_KEYFILE_BEGIN),
1305 SILC_STR_UI_XNSTRING(data, data_len),
1306 SILC_STR_UI32_STRING(SILC_PKCS_PRIVATE_KEYFILE_END),
1309 /* Save into a file */
1310 if (silc_file_writefile_mode(filename, buf->data, buf->len, 0600)) {
1311 silc_buffer_clear(buf);
1312 silc_buffer_free(buf);
1313 silc_buffer_clear(enc);
1314 silc_buffer_free(enc);
1318 silc_buffer_clear(buf);
1319 silc_buffer_free(buf);
1320 silc_buffer_clear(enc);
1321 silc_buffer_free(enc);
1325 /* Saves private key into file. */
1327 bool silc_pkcs_save_private_key(char *filename, SilcPrivateKey private_key,
1328 unsigned char *passphrase,
1329 SilcUInt32 passphrase_len,
1330 SilcUInt32 encoding)
1332 unsigned char *data;
1333 SilcUInt32 data_len;
1336 data = silc_pkcs_private_key_encode(private_key, &data_len);
1337 ret = silc_pkcs_save_private_key_internal(filename, data, data_len,
1338 passphrase, passphrase_len,
1340 memset(data, 0, data_len);
1345 /* Loads public key from file and allocates new public key. Returns TRUE
1346 if loading was successful. */
1348 bool silc_pkcs_load_public_key(char *filename, SilcPublicKey *public_key,
1349 SilcUInt32 encoding)
1351 unsigned char *cp, *old, *data, byte;
1352 SilcUInt32 i, data_len, len;
1354 old = data = silc_file_readfile(filename, &data_len);
1358 /* Check start of file and remove header from the data. */
1359 len = strlen(SILC_PKCS_PUBLIC_KEYFILE_BEGIN);
1361 for (i = 0; i < len; i++) {
1364 if (byte != SILC_PKCS_PUBLIC_KEYFILE_BEGIN[i]) {
1365 memset(old, 0, data_len);
1372 /* Decode public key */
1374 len = data_len - (strlen(SILC_PKCS_PUBLIC_KEYFILE_BEGIN) +
1375 strlen(SILC_PKCS_PUBLIC_KEYFILE_END));
1378 case SILC_PKCS_FILE_BIN:
1380 case SILC_PKCS_FILE_PEM:
1381 data = silc_pem_decode(data, len, &len);
1382 memset(old, 0, data_len);
1389 if (!data || !silc_pkcs_public_key_decode(data, len, public_key)) {
1390 memset(old, 0, data_len);
1396 memset(old, 0, data_len);
1401 /* Load private key from file and allocates new private key. Returns TRUE
1402 if loading was successful. */
1404 bool silc_pkcs_load_private_key(char *filename, SilcPrivateKey *private_key,
1405 unsigned char *passphrase,
1406 SilcUInt32 passphrase_len,
1407 SilcUInt32 encoding)
1412 SilcUInt32 blocklen;
1413 unsigned char tmp[32], keymat[32];
1414 unsigned char *cp, *old, *data, byte;
1415 SilcUInt32 i, data_len, len, magic, mac_len;
1417 old = data = silc_file_readfile(filename, &data_len);
1421 /* Check start of file and remove header from the data. */
1422 len = strlen(SILC_PKCS_PRIVATE_KEYFILE_BEGIN);
1424 for (i = 0; i < len; i++) {
1427 if (byte != SILC_PKCS_PRIVATE_KEYFILE_BEGIN[i]) {
1428 memset(old, 0, data_len);
1435 /* Decode private key */
1436 len = data_len - (strlen(SILC_PKCS_PRIVATE_KEYFILE_BEGIN) +
1437 strlen(SILC_PKCS_PRIVATE_KEYFILE_END));
1440 case SILC_PKCS_FILE_BIN:
1442 case SILC_PKCS_FILE_PEM:
1443 data = silc_pem_decode(data, len, &len);
1445 memset(old, 0, data_len);
1452 memset(tmp, 0, sizeof(tmp));
1453 memset(keymat, 0, sizeof(keymat));
1455 /* Private key files without the specific magic number are assumed
1456 to be the old-style private keys that are not encrypted. */
1457 SILC_GET32_MSB(magic, data);
1458 if (magic != SILC_PKCS_PRIVATE_KEY_MAGIC) {
1459 SILC_LOG_DEBUG(("Private key does not have correct magic!"));
1461 /* Now decode the actual private key */
1462 if (!silc_pkcs_private_key_decode(data, len, private_key)) {
1463 memset(old, 0, data_len);
1468 memset(old, 0, data_len);
1473 /* Allocate the AES cipher */
1474 if (!silc_cipher_alloc("aes-256-cbc", &aes)) {
1475 SILC_LOG_ERROR(("Could not allocate AES cipher, probably not registered"));
1476 memset(old, 0, data_len);
1480 blocklen = silc_cipher_get_block_len(aes);
1481 if (blocklen * 2 > sizeof(tmp)) {
1482 memset(old, 0, data_len);
1487 /* Allocate MD5 hash */
1488 if (!silc_hash_alloc("md5", &md5)) {
1489 SILC_LOG_ERROR(("Could not allocate MD5 hash, probably not registered"));
1490 silc_cipher_free(aes);
1491 memset(old, 0, data_len);
1497 if (!silc_hmac_alloc("hmac-sha1-96", NULL, &sha1hmac)) {
1498 SILC_LOG_ERROR(("Could not allocate SHA1 HMAC, probably not registered"));
1499 silc_hash_free(md5);
1500 silc_cipher_free(aes);
1501 memset(old, 0, data_len);
1506 /* Derive the decryption key from the provided key material. The key
1507 is 256 bits length, and derived by taking hash of the data, then
1508 re-hashing the data and the previous digest, and using the first and
1509 second digest as the key. */
1510 silc_hash_init(md5);
1511 silc_hash_update(md5, passphrase, passphrase_len);
1512 silc_hash_final(md5, keymat);
1513 silc_hash_init(md5);
1514 silc_hash_update(md5, passphrase, passphrase_len);
1515 silc_hash_update(md5, keymat, 16);
1516 silc_hash_final(md5, keymat + 16);
1518 /* Set the key to the cipher */
1519 silc_cipher_set_key(aes, keymat, sizeof(keymat) * 8);
1521 /* First, verify the MAC of the private key data */
1522 mac_len = silc_hmac_len(sha1hmac);
1523 silc_hmac_init_with_key(sha1hmac, keymat, 16);
1524 silc_hmac_update(sha1hmac, data, len - mac_len);
1525 silc_hmac_final(sha1hmac, tmp, NULL);
1526 if (memcmp(tmp, data + (len - mac_len), mac_len)) {
1527 SILC_LOG_DEBUG(("Integrity check for private key failed"));
1528 memset(keymat, 0, sizeof(keymat));
1529 memset(tmp, 0, sizeof(tmp));
1530 silc_hmac_free(sha1hmac);
1531 silc_hash_free(md5);
1532 silc_cipher_free(aes);
1533 memset(old, 0, data_len);
1540 /* Decrypt the private key buffer */
1541 silc_cipher_decrypt(aes, data, data, len - mac_len, silc_cipher_get_iv(aes));
1542 SILC_GET32_MSB(i, data);
1544 SILC_LOG_DEBUG(("Bad private key length in buffer!"));
1545 memset(keymat, 0, sizeof(keymat));
1546 memset(tmp, 0, sizeof(tmp));
1547 silc_hmac_free(sha1hmac);
1548 silc_hash_free(md5);
1549 silc_cipher_free(aes);
1550 memset(old, 0, data_len);
1558 memset(keymat, 0, sizeof(keymat));
1559 memset(tmp, 0, sizeof(tmp));
1560 silc_hmac_free(sha1hmac);
1561 silc_hash_free(md5);
1562 silc_cipher_free(aes);
1564 /* Now decode the actual private key */
1565 if (!silc_pkcs_private_key_decode(data, len, private_key)) {
1566 memset(old, 0, data_len);
1571 memset(old, 0, data_len);