Fixed possible buffer overflow in PKSC#1 message decoding.
[crypto.git] / lib / silccrypt / silcpkcs1.c
1 /*
2
3   silcpkcs1.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2003 - 2008 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
20 #include "silccrypto.h"
21 #include "rsa.h"
22 #include "silcpkcs1_i.h"
23
24 /************************** PKCS #1 message format ***************************/
25
26 /* Minimum padding in block */
27 #define SILC_PKCS1_MIN_PADDING 8
28
29 /* Encodes PKCS#1 data block from the `data' according to the block type
30    indicated by `bt'.  When encoding signatures the `bt' must be
31    SILC_PKCS1_BT_PRV1 and when encoding encryption blocks the `bt' must
32    be SILC_PKCS1_BT_PUB.  The encoded data is copied into the `dest_data'
33    buffer which is size of `dest_data_size'.  If the `dest_data' is not
34    able to hold the encoded block this returns FALSE.  The `rng' must be
35    set when `bt' is SILC_PKCS1_BT_PUB.  This function returns TRUE on
36    success. */
37
38 SilcBool silc_pkcs1_encode(SilcPkcs1BlockType bt,
39                            const unsigned char *data,
40                            SilcUInt32 data_len,
41                            unsigned char *dest_data,
42                            SilcUInt32 dest_data_size,
43                            SilcRng rng)
44 {
45   SilcInt32 padlen;
46   int i;
47
48   SILC_LOG_DEBUG(("PKCS#1 encoding, bt %d", bt));
49
50   if (!data || !dest_data ||
51       dest_data_size < SILC_PKCS1_MIN_PADDING + 3 ||
52       dest_data_size < data_len) {
53     SILC_LOG_DEBUG(("Data to be encoded is too long"));
54     return FALSE;
55   }
56
57   /* Start of block */
58   dest_data[0] = 0x00;
59   dest_data[1] = (unsigned char)bt;
60
61   padlen = (SilcInt32)dest_data_size - (SilcInt32)data_len - 3;
62   if (padlen < SILC_PKCS1_MIN_PADDING) {
63     SILC_LOG_DEBUG(("Data to be encoded is too long"));
64     return FALSE;
65   }
66
67   /* Encode according to block type */
68   switch (bt) {
69   case SILC_PKCS1_BT_PRV0:
70   case SILC_PKCS1_BT_PRV1:
71     /* Signature */
72     memset(dest_data + 2, bt == SILC_PKCS1_BT_PRV1 ? 0xff : 0x00, padlen);
73     break;
74
75   case SILC_PKCS1_BT_PUB:
76     /* Encryption */
77     if (!rng) {
78       SILC_LOG_ERROR(("Cannot encrypt: random number generator not provided"));
79       return FALSE;
80     }
81
82     /* It is guaranteed this routine does not return zero byte. */
83     for (i = 2; i < padlen; i++)
84       dest_data[i] = silc_rng_get_byte_fast(rng);
85
86     break;
87   }
88
89   /* Copy the data */
90   dest_data[padlen + 2] = 0x00;
91   memcpy(dest_data + padlen + 3, data, data_len);
92
93   return TRUE;
94 }
95
96 /* Decodes the PKCS#1 encoded block according to the block type `bt'.
97    When verifying signatures the `bt' must be SILC_PKCS1_BT_PRV1 and
98    when decrypting it must be SILC_PKCS1_BT_PUB.  This copies the
99    decoded data into `dest_data' which is size of `dest_data_size'.  If
100    the deocded block does not fit to `dest_data' this returns FALSE.
101    Returns TRUE on success. */
102
103 SilcBool silc_pkcs1_decode(SilcPkcs1BlockType bt,
104                            const unsigned char *data,
105                            SilcUInt32 data_len,
106                            unsigned char *dest_data,
107                            SilcUInt32 dest_data_size,
108                            SilcUInt32 *dest_len)
109 {
110   SilcUInt32 i = 0;
111
112   SILC_LOG_DEBUG(("PKCS#1 decoding, bt %d", bt));
113
114   /* Sanity checks */
115   if (!data || !dest_data || dest_data_size < 3 ||
116       data[0] != 0x00 || data[1] != (unsigned char)bt) {
117     SILC_LOG_DEBUG(("Malformed block"));
118     return FALSE;
119   }
120
121   /* Decode according to block type */
122   switch (bt) {
123   case SILC_PKCS1_BT_PRV0:
124     /* Do nothing */
125     break;
126
127   case SILC_PKCS1_BT_PRV1:
128     /* Verification */
129     for (i = 2; i < data_len; i++)
130       if (data[i] != 0xff)
131         break;
132     break;
133
134   case SILC_PKCS1_BT_PUB:
135     /* Decryption */
136     for (i = 2; i < data_len; i++)
137       if (data[i] == 0x00)
138         break;
139     break;
140   }
141
142   /* Sanity checks */
143   if (i >= data_len) {
144     SILC_LOG_DEBUG(("Malformed block, too short message"));
145     return FALSE;
146   }
147   if (i < SILC_PKCS1_MIN_PADDING) {
148     SILC_LOG_DEBUG(("Malformed block, too short padding"));
149     return FALSE;
150   }
151   if (data[i++] != 0x00) {
152     SILC_LOG_DEBUG(("Malformed block"));
153     return FALSE;
154   }
155   if (i >= data_len) {
156     SILC_LOG_DEBUG(("Malformed block, too short message"));
157     return FALSE;
158   }
159   if (dest_data_size < data_len - i) {
160     SILC_LOG_DEBUG(("Destination buffer too small"));
161     return FALSE;
162   }
163
164   /* Copy the data */
165   memcpy(dest_data, data + i, data_len - i);
166
167   /* Return data length */
168   if (dest_len)
169     *dest_len = data_len - i;
170
171   return TRUE;
172 }
173
174
175 /***************************** PKCS #1 PKCS API ******************************/
176
177 /* Generates RSA key pair. */
178
179 SILC_PKCS_ALG_GENERATE_KEY(silc_pkcs1_generate_key)
180 {
181   SilcUInt32 prime_bits = keylen / 2;
182   SilcMPInt p, q;
183   SilcBool found = FALSE;
184
185   if (keylen < 768 || keylen > 16384)
186     return FALSE;
187
188   silc_mp_init(&p);
189   silc_mp_init(&q);
190
191   /* Find p and q */
192   while (!found) {
193     silc_math_gen_prime(&p, prime_bits, FALSE, rng);
194     silc_math_gen_prime(&q, prime_bits, FALSE, rng);
195     if ((silc_mp_cmp(&p, &q)) != 0)
196       found = TRUE;
197   }
198
199   /* If p is smaller than q, switch them */
200   if ((silc_mp_cmp(&p, &q)) > 0) {
201     SilcMPInt hlp;
202     silc_mp_init(&hlp);
203
204     silc_mp_set(&hlp, &p);
205     silc_mp_set(&p, &q);
206     silc_mp_set(&q, &hlp);
207
208     silc_mp_uninit(&hlp);
209   }
210
211   /* Generate the actual keys */
212   if (!silc_rsa_generate_keys(keylen, &p, &q, ret_public_key, ret_private_key))
213     return FALSE;
214
215   silc_mp_uninit(&p);
216   silc_mp_uninit(&q);
217
218   return TRUE;
219 }
220
221 /* Import PKCS #1 compliant public key */
222
223 SILC_PKCS_ALG_IMPORT_PUBLIC_KEY(silc_pkcs1_import_public_key)
224 {
225   SilcAsn1 asn1 = NULL;
226   SilcBufferStruct alg_key;
227   RsaPublicKey *pubkey;
228
229   if (!ret_public_key)
230     return 0;
231
232   asn1 = silc_asn1_alloc(NULL);
233   if (!asn1)
234     return 0;
235
236   /* Allocate RSA public key */
237   *ret_public_key = pubkey = silc_calloc(1, sizeof(*pubkey));
238   if (!pubkey)
239     goto err;
240
241   /* Parse the PKCS #1 public key */
242   silc_buffer_set(&alg_key, key, key_len);
243   if (!silc_asn1_decode(asn1, &alg_key,
244                         SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
245                         SILC_ASN1_SEQUENCE,
246                           SILC_ASN1_INT(&pubkey->n),
247                           SILC_ASN1_INT(&pubkey->e),
248                         SILC_ASN1_END, SILC_ASN1_END))
249     goto err;
250
251   /* Set key length */
252   pubkey->bits = ((silc_mp_sizeinbase(&pubkey->n, 2) + 7) / 8) * 8;
253
254   silc_asn1_free(asn1);
255
256   return key_len;
257
258  err:
259   silc_free(pubkey);
260   silc_asn1_free(asn1);
261   return 0;
262 }
263
264 /* Export PKCS #1 compliant public key */
265
266 SILC_PKCS_ALG_EXPORT_PUBLIC_KEY(silc_pkcs1_export_public_key)
267 {
268   RsaPublicKey *key = public_key;
269   SilcAsn1 asn1 = NULL;
270   SilcBufferStruct alg_key;
271   unsigned char *ret;
272
273   asn1 = silc_asn1_alloc(stack);
274   if (!asn1)
275     goto err;
276
277   /* Encode to PKCS #1 public key */
278   memset(&alg_key, 0, sizeof(alg_key));
279   if (!silc_asn1_encode(asn1, &alg_key,
280                         SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
281                         SILC_ASN1_SEQUENCE,
282                           SILC_ASN1_INT(&key->n),
283                           SILC_ASN1_INT(&key->e),
284                         SILC_ASN1_END, SILC_ASN1_END))
285     goto err;
286
287   ret = silc_buffer_steal(&alg_key, ret_len);
288   silc_asn1_free(asn1);
289
290   return ret;
291
292  err:
293   if (asn1)
294     silc_asn1_free(asn1);
295   return NULL;
296 }
297
298 /* Returns key length */
299
300 SILC_PKCS_ALG_PUBLIC_KEY_BITLEN(silc_pkcs1_public_key_bitlen)
301 {
302   RsaPublicKey *key = public_key;
303   return key->bits;
304 }
305
306 /* Copy public key */
307
308 SILC_PKCS_ALG_PUBLIC_KEY_COPY(silc_pkcs1_public_key_copy)
309 {
310   RsaPublicKey *key = public_key, *new_key;
311
312   new_key = silc_calloc(1, sizeof(*new_key));
313   if (!new_key)
314     return NULL;
315
316   silc_mp_init(&new_key->n);
317   silc_mp_init(&new_key->e);
318   silc_mp_set(&new_key->n, &key->n);
319   silc_mp_set(&new_key->e, &key->e);
320   new_key->bits = key->bits;
321
322   return new_key;
323 }
324
325 /* Compare public keys */
326
327 SILC_PKCS_ALG_PUBLIC_KEY_COMPARE(silc_pkcs1_public_key_compare)
328 {
329   RsaPublicKey *k1 = key1, *k2 = key2;
330
331   if (k1->bits != k2->bits)
332     return FALSE;
333   if (silc_mp_cmp(&k1->e, &k2->e) != 0)
334     return FALSE;
335   if (silc_mp_cmp(&k1->n, &k2->n) != 0)
336     return FALSE;
337
338   return TRUE;
339 }
340
341 /* Frees public key */
342
343 SILC_PKCS_ALG_PUBLIC_KEY_FREE(silc_pkcs1_public_key_free)
344 {
345   RsaPublicKey *key = public_key;
346
347   silc_mp_uninit(&key->n);
348   silc_mp_uninit(&key->e);
349   silc_hash_free(key->hash);
350   silc_free(key);
351 }
352
353 /* Import PKCS #1 compliant private key */
354
355 SILC_PKCS_ALG_IMPORT_PRIVATE_KEY(silc_pkcs1_import_private_key)
356 {
357   SilcAsn1 asn1;
358   SilcBufferStruct alg_key;
359   RsaPrivateKey *privkey;
360   SilcUInt32 ver;
361
362   if (!ret_private_key)
363     return 0;
364
365   asn1 = silc_asn1_alloc(NULL);
366   if (!asn1)
367     return 0;
368
369   /* Allocate RSA private key */
370   *ret_private_key = privkey = silc_calloc(1, sizeof(*privkey));
371   if (!privkey)
372     goto err;
373
374   /* Parse the PKCS #1 private key */
375   silc_buffer_set(&alg_key, key, key_len);
376   if (!silc_asn1_decode(asn1, &alg_key,
377                         SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
378                         SILC_ASN1_SEQUENCE,
379                           SILC_ASN1_SHORT_INT(&ver),
380                           SILC_ASN1_INT(&privkey->n),
381                           SILC_ASN1_INT(&privkey->e),
382                           SILC_ASN1_INT(&privkey->d),
383                           SILC_ASN1_INT(&privkey->p),
384                           SILC_ASN1_INT(&privkey->q),
385                           SILC_ASN1_INT(&privkey->dP),
386                           SILC_ASN1_INT(&privkey->dQ),
387                           SILC_ASN1_INT(&privkey->qP),
388                         SILC_ASN1_END, SILC_ASN1_END))
389     goto err;
390
391   if (ver != 0)
392     goto err;
393
394   /* Set key length */
395   privkey->bits = ((silc_mp_sizeinbase(&privkey->n, 2) + 7) / 8) * 8;
396
397   silc_asn1_free(asn1);
398
399   return key_len;
400
401  err:
402   silc_free(privkey);
403   silc_asn1_free(asn1);
404   return 0;
405 }
406
407 /* Export PKCS #1 compliant private key */
408
409 SILC_PKCS_ALG_EXPORT_PRIVATE_KEY(silc_pkcs1_export_private_key)
410 {
411   RsaPrivateKey *key = private_key;
412   SilcAsn1 asn1;
413   SilcBufferStruct alg_key;
414   unsigned char *ret;
415
416   asn1 = silc_asn1_alloc(stack);
417   if (!asn1)
418     return FALSE;
419
420   /* Encode to PKCS #1 private key */
421   memset(&alg_key, 0, sizeof(alg_key));
422   if (!silc_asn1_encode(asn1, &alg_key,
423                         SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
424                         SILC_ASN1_SEQUENCE,
425                           SILC_ASN1_SHORT_INT(0),
426                           SILC_ASN1_INT(&key->n),
427                           SILC_ASN1_INT(&key->e),
428                           SILC_ASN1_INT(&key->d),
429                           SILC_ASN1_INT(&key->p),
430                           SILC_ASN1_INT(&key->q),
431                           SILC_ASN1_INT(&key->dP),
432                           SILC_ASN1_INT(&key->dQ),
433                           SILC_ASN1_INT(&key->qP),
434                         SILC_ASN1_END, SILC_ASN1_END))
435     goto err;
436
437   ret = silc_buffer_steal(&alg_key, ret_len);
438   silc_asn1_free(asn1);
439
440   return ret;
441
442  err:
443   silc_asn1_free(asn1);
444   return NULL;
445 }
446
447 /* Returns key length */
448
449 SILC_PKCS_ALG_PRIVATE_KEY_BITLEN(silc_pkcs1_private_key_bitlen)
450 {
451   RsaPrivateKey *key = private_key;
452   return key->bits;
453 }
454
455 /* Frees private key */
456
457 SILC_PKCS_ALG_PRIVATE_KEY_FREE(silc_pkcs1_private_key_free)
458 {
459   RsaPrivateKey *key = private_key;
460
461   silc_mp_uninit(&key->n);
462   silc_mp_uninit(&key->e);
463   silc_mp_uninit(&key->d);
464   silc_mp_uninit(&key->dP);
465   silc_mp_uninit(&key->dQ);
466   silc_mp_uninit(&key->qP);
467   silc_mp_uninit(&key->p);
468   silc_mp_uninit(&key->q);
469   silc_hash_free(key->hash);
470   silc_free(key);
471 }
472
473 /* PKCS #1 RSA routines */
474
475 SILC_PKCS_ALG_ENCRYPT(silc_pkcs1_encrypt)
476 {
477   RsaPublicKey *key = public_key;
478   SilcMPInt mp_tmp;
479   SilcMPInt mp_dst;
480   unsigned char padded[2048 + 1];
481   SilcUInt32 len = (key->bits + 7) / 8;
482   SilcStack stack;
483
484   if (sizeof(padded) < len) {
485     encrypt_cb(FALSE, NULL, 0, context);
486     return NULL;
487   }
488
489   /* Pad data */
490   if (!silc_pkcs1_encode(SILC_PKCS1_BT_PUB, src, src_len,
491                          padded, len, rng)) {
492     encrypt_cb(FALSE, NULL, 0, context);
493     return NULL;
494   }
495
496   stack = silc_stack_alloc(2048, silc_crypto_stack());
497
498   silc_mp_sinit(stack, &mp_tmp);
499   silc_mp_sinit(stack, &mp_dst);
500
501   /* Data to MP */
502   silc_mp_bin2mp(padded, len, &mp_tmp);
503
504   /* Encrypt */
505   silc_rsa_public_operation(key, &mp_tmp, &mp_dst);
506
507   /* MP to data */
508   silc_mp_mp2bin_noalloc(&mp_dst, padded, len);
509
510   /* Deliver result */
511   encrypt_cb(TRUE, padded, len, context);
512
513   memset(padded, 0, sizeof(padded));
514   silc_mp_uninit(&mp_tmp);
515   silc_mp_uninit(&mp_dst);
516   silc_stack_free(stack);
517
518   return NULL;
519 }
520
521 SILC_PKCS_ALG_DECRYPT(silc_pkcs1_decrypt)
522 {
523   RsaPrivateKey *key = private_key;
524   SilcMPInt mp_tmp;
525   SilcMPInt mp_dst;
526   unsigned char *padded, unpadded[2048 + 1];
527   SilcUInt32 padded_len, dst_len;
528   SilcStack stack;
529
530   if (sizeof(unpadded) < (key->bits + 7) / 8) {
531     decrypt_cb(FALSE, NULL, 0, context);
532     return NULL;
533   }
534
535   stack = silc_stack_alloc(2048, silc_crypto_stack());
536
537   silc_mp_sinit(stack, &mp_tmp);
538   silc_mp_sinit(stack, &mp_dst);
539
540   /* Data to MP */
541   silc_mp_bin2mp(src, src_len, &mp_tmp);
542
543   /* Decrypt */
544   silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
545
546   /* MP to data */
547   padded = silc_mp_mp2bin(&mp_dst, (key->bits + 7) / 8, &padded_len);
548
549   /* Unpad data */
550   if (!silc_pkcs1_decode(SILC_PKCS1_BT_PUB, padded, padded_len,
551                          unpadded, sizeof(unpadded), &dst_len)) {
552     memset(padded, 0, padded_len);
553     silc_free(padded);
554     silc_mp_uninit(&mp_tmp);
555     silc_mp_uninit(&mp_dst);
556     decrypt_cb(FALSE, NULL, 0, context);
557     return NULL;
558   }
559
560   /* Deliver result */
561   decrypt_cb(TRUE, unpadded, dst_len, context);
562
563   memset(padded, 0, padded_len);
564   memset(unpadded, 0, sizeof(unpadded));
565   silc_free(padded);
566   silc_mp_uninit(&mp_tmp);
567   silc_mp_uninit(&mp_dst);
568   silc_stack_free(stack);
569
570   return NULL;
571 }
572
573 /* PKCS #1 sign with appendix, hash OID included in the signature */
574
575 SILC_PKCS_ALG_SIGN(silc_pkcs1_sign)
576 {
577   RsaPrivateKey *key = private_key;
578   unsigned char padded[2048 + 1], hashr[SILC_HASH_MAXLEN];
579   SilcMPInt mp_tmp;
580   SilcMPInt mp_dst;
581   SilcBufferStruct di;
582   SilcUInt32 len = (key->bits + 7) / 8;
583   const char *oid;
584   SilcStack stack;
585   SilcAsn1 asn1;
586
587   SILC_LOG_DEBUG(("Sign"));
588
589   if (sizeof(padded) < len) {
590     sign_cb(FALSE, NULL, 0, context);
591     return NULL;
592   }
593
594   oid = silc_hash_get_oid(hash);
595   if (!oid) {
596     sign_cb(FALSE, NULL, 0, context);
597     return NULL;
598   }
599
600   stack = silc_stack_alloc(2048, silc_crypto_stack());
601
602   asn1 = silc_asn1_alloc(stack);
603   if (!asn1) {
604     silc_stack_free(stack);
605     sign_cb(FALSE, NULL, 0, context);
606     return NULL;
607   }
608
609   /* Compute hash */
610   if (compute_hash) {
611     if (!hash)
612       hash = key->hash;
613     silc_hash_make(hash, src, src_len, hashr);
614     src = hashr;
615     src_len = silc_hash_len(hash);
616   }
617
618   /* Encode digest info */
619   memset(&di, 0, sizeof(di));
620   if (!silc_asn1_encode(asn1, &di,
621                         SILC_ASN1_SEQUENCE,
622                           SILC_ASN1_SEQUENCE,
623                             SILC_ASN1_OID(oid),
624                             SILC_ASN1_NULL(TRUE),
625                           SILC_ASN1_END,
626                           SILC_ASN1_OCTET_STRING(src, src_len),
627                         SILC_ASN1_END, SILC_ASN1_END)) {
628     silc_asn1_free(asn1);
629     silc_stack_free(stack);
630     sign_cb(FALSE, NULL, 0, context);
631     return NULL;
632   }
633   SILC_LOG_HEXDUMP(("DigestInfo"), silc_buffer_data(&di),
634                    silc_buffer_len(&di));
635
636   /* Pad data */
637   if (!silc_pkcs1_encode(SILC_PKCS1_BT_PRV1, silc_buffer_data(&di),
638                          silc_buffer_len(&di), padded, len, NULL)) {
639     silc_asn1_free(asn1);
640     silc_stack_free(stack);
641     sign_cb(FALSE, NULL, 0, context);
642     return NULL;
643   }
644
645   silc_mp_sinit(stack, &mp_tmp);
646   silc_mp_sinit(stack, &mp_dst);
647
648   /* Data to MP */
649   silc_mp_bin2mp(padded, len, &mp_tmp);
650
651   /* Sign */
652   silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
653
654   /* MP to data */
655   silc_mp_mp2bin_noalloc(&mp_dst, padded, len);
656
657   /* Deliver result */
658   sign_cb(TRUE, padded, len, context);
659
660   memset(padded, 0, sizeof(padded));
661   if (compute_hash)
662     memset(hashr, 0, sizeof(hashr));
663   silc_mp_uninit(&mp_tmp);
664   silc_mp_uninit(&mp_dst);
665   silc_asn1_free(asn1);
666   silc_stack_free(stack);
667
668   return NULL;
669 }
670
671 /* PKCS #1 verification with appendix. */
672
673 SILC_PKCS_ALG_VERIFY(silc_pkcs1_verify)
674 {
675   RsaPublicKey *key = public_key;
676   SilcBool ret = FALSE;
677   SilcMPInt mp_tmp2;
678   SilcMPInt mp_dst;
679   unsigned char *verify, unpadded[2048 + 1], hashr[SILC_HASH_MAXLEN];
680   SilcUInt32 verify_len, len = (key->bits + 7) / 8;
681   SilcBufferStruct di, ldi;
682   SilcBool has_null = TRUE;
683   SilcHash ihash = NULL;
684   SilcStack stack;
685   SilcAsn1 asn1;
686   char *oid;
687
688   SILC_LOG_DEBUG(("Verify signature"));
689
690   stack = silc_stack_alloc(2048, silc_crypto_stack());
691
692   asn1 = silc_asn1_alloc(stack);
693   if (!asn1) {
694     verify_cb(FALSE, context);
695     return NULL;
696   }
697
698   silc_mp_sinit(stack, &mp_tmp2);
699   silc_mp_sinit(stack, &mp_dst);
700
701   /* Format the signature into MP int */
702   silc_mp_bin2mp(signature, signature_len, &mp_tmp2);
703
704   /* Verify */
705   silc_rsa_public_operation(key, &mp_tmp2, &mp_dst);
706
707   /* MP to data */
708   verify = silc_mp_mp2bin(&mp_dst, len, &verify_len);
709
710   /* Unpad data */
711   if (!silc_pkcs1_decode(SILC_PKCS1_BT_PRV1, verify, verify_len,
712                          unpadded, sizeof(unpadded), &len))
713     goto err;
714   silc_buffer_set(&di, unpadded, len);
715
716   /* If hash isn't given, allocate the one given in digest info */
717   if (compute_hash) {
718     if (!hash) {
719       has_null = FALSE;
720
721       /* Decode digest info */
722       if (!silc_asn1_decode(asn1, &di,
723                             SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
724                             SILC_ASN1_SEQUENCE,
725                               SILC_ASN1_SEQUENCE,
726                                 SILC_ASN1_OID(&oid),
727                                 SILC_ASN1_NULL_T(SILC_ASN1_OPTIONAL,
728                                                  SILC_ASN1_TAG_NULL, &has_null),
729                               SILC_ASN1_END,
730                             SILC_ASN1_END, SILC_ASN1_END))
731         goto err;
732
733       if (!silc_hash_alloc_by_oid(oid, &ihash)) {
734         SILC_LOG_DEBUG(("Unknown OID %s", oid));
735         goto err;
736       }
737       hash = ihash;
738     }
739
740     /* Hash the data */
741     silc_hash_make(hash, data, data_len, hashr);
742     data = hashr;
743     data_len = silc_hash_len(hash);
744     oid = (char *)silc_hash_get_oid(hash);
745   }
746
747   /* Encode digest info for comparison */
748   memset(&ldi, 0, sizeof(ldi));
749   if (!silc_asn1_encode(asn1, &ldi,
750                         SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
751                         SILC_ASN1_SEQUENCE,
752                           SILC_ASN1_SEQUENCE,
753                             SILC_ASN1_OID(oid),
754                             SILC_ASN1_NULL(has_null),
755                           SILC_ASN1_END,
756                           SILC_ASN1_OCTET_STRING(data, data_len),
757                         SILC_ASN1_END, SILC_ASN1_END))
758     goto err;
759
760   SILC_LOG_HEXDUMP(("DigestInfo remote"), silc_buffer_data(&di),
761                    silc_buffer_len(&di));
762   SILC_LOG_HEXDUMP(("DigestInfo local"), silc_buffer_data(&ldi),
763                    silc_buffer_len(&ldi));
764
765   /* Compare */
766   if (silc_buffer_len(&di) == silc_buffer_len(&ldi) &&
767       !memcmp(silc_buffer_data(&di), silc_buffer_data(&ldi),
768               silc_buffer_len(&ldi)))
769     ret = TRUE;
770
771   /* Deliver result */
772   verify_cb(ret, context);
773
774   memset(verify, 0, verify_len);
775   memset(unpadded, 0, sizeof(unpadded));
776   silc_free(verify);
777   silc_mp_uninit(&mp_tmp2);
778   silc_mp_uninit(&mp_dst);
779   if (compute_hash)
780     memset(hashr, 0, sizeof(hashr));
781   if (ihash)
782     silc_hash_free(ihash);
783   silc_asn1_free(asn1);
784   silc_stack_free(stack);
785
786   return NULL;
787
788  err:
789   memset(verify, 0, verify_len);
790   silc_free(verify);
791   silc_mp_uninit(&mp_tmp2);
792   silc_mp_uninit(&mp_dst);
793   if (ihash)
794     silc_hash_free(ihash);
795   silc_asn1_free(asn1);
796   silc_stack_free(stack);
797
798   verify_cb(FALSE, context);
799   return NULL;
800 }
801
802 /* PKCS #1 sign without hash oid */
803
804 SILC_PKCS_ALG_SIGN(silc_pkcs1_sign_no_oid)
805 {
806   RsaPrivateKey *key = private_key;
807   SilcMPInt mp_tmp;
808   SilcMPInt mp_dst;
809   unsigned char padded[2048 + 1], hashr[SILC_HASH_MAXLEN];
810   SilcUInt32 len = (key->bits + 7) / 8;
811   SilcStack stack;
812
813   SILC_LOG_DEBUG(("Sign"));
814
815   if (sizeof(padded) < len) {
816     sign_cb(FALSE, NULL, 0, context);
817     return NULL;
818   }
819
820   /* Compute hash if requested */
821   if (compute_hash) {
822     if (!hash)
823       hash = key->hash;
824     silc_hash_make(hash, src, src_len, hashr);
825     src = hashr;
826     src_len = silc_hash_len(hash);
827   }
828
829   /* Pad data */
830   if (!silc_pkcs1_encode(SILC_PKCS1_BT_PRV1, src, src_len,
831                          padded, len, NULL)) {
832     sign_cb(FALSE, NULL, 0, context);
833     return NULL;
834   }
835
836   stack = silc_stack_alloc(2048, silc_crypto_stack());
837
838   silc_mp_sinit(stack, &mp_tmp);
839   silc_mp_sinit(stack, &mp_dst);
840
841   /* Data to MP */
842   silc_mp_bin2mp(padded, len, &mp_tmp);
843
844   /* Sign */
845   silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
846
847   /* MP to data */
848   silc_mp_mp2bin_noalloc(&mp_dst, padded, len);
849
850   /* Deliver result */
851   sign_cb(TRUE, padded, len, context);
852
853   memset(padded, 0, sizeof(padded));
854   if (compute_hash)
855     memset(hashr, 0, sizeof(hashr));
856   silc_mp_uninit(&mp_tmp);
857   silc_mp_uninit(&mp_dst);
858   silc_stack_free(stack);
859
860   return NULL;
861 }
862
863 /* PKCS #1 verify without hash oid */
864
865 SILC_PKCS_ALG_VERIFY(silc_pkcs1_verify_no_oid)
866 {
867   RsaPublicKey *key = public_key;
868   SilcBool ret = FALSE;
869   SilcMPInt mp_tmp2;
870   SilcMPInt mp_dst;
871   unsigned char *verify, unpadded[2048 + 1], hashr[SILC_HASH_MAXLEN];
872   SilcUInt32 verify_len, len = (key->bits + 7) / 8;
873   SilcStack stack;
874
875   SILC_LOG_DEBUG(("Verify signature"));
876
877   stack = silc_stack_alloc(2048, silc_crypto_stack());
878
879   silc_mp_sinit(stack, &mp_tmp2);
880   silc_mp_sinit(stack, &mp_dst);
881
882   /* Format the signature into MP int */
883   silc_mp_bin2mp(signature, signature_len, &mp_tmp2);
884
885   /* Verify */
886   silc_rsa_public_operation(key, &mp_tmp2, &mp_dst);
887
888   /* MP to data */
889   verify = silc_mp_mp2bin(&mp_dst, len, &verify_len);
890
891   /* Unpad data */
892   if (!silc_pkcs1_decode(SILC_PKCS1_BT_PRV1, verify, verify_len,
893                          unpadded, sizeof(unpadded), &len)) {
894     memset(verify, 0, verify_len);
895     silc_free(verify);
896     silc_mp_uninit(&mp_tmp2);
897     silc_mp_uninit(&mp_dst);
898     silc_stack_free(stack);
899     verify_cb(FALSE, context);
900     return NULL;
901   }
902
903   /* Hash data if requested */
904   if (compute_hash) {
905     if (!hash)
906       hash = key->hash;
907     silc_hash_make(hash, data, data_len, hashr);
908     data = hashr;
909     data_len = silc_hash_len(hash);
910   }
911
912   /* Compare */
913   if (len == data_len && !memcmp(data, unpadded, len))
914     ret = TRUE;
915
916   /* Deliver result */
917   verify_cb(ret, context);
918
919   memset(verify, 0, verify_len);
920   memset(unpadded, 0, sizeof(unpadded));
921   if (compute_hash)
922     memset(hashr, 0, sizeof(hashr));
923   silc_free(verify);
924   silc_mp_uninit(&mp_tmp2);
925   silc_mp_uninit(&mp_dst);
926   silc_stack_free(stack);
927
928   return NULL;
929 }