]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - sshrsa.c
Pageant is now able to avoid asking for the passphrase when asked to
[PuTTY.git] / sshrsa.c
1 /*
2  * RSA implementation just sufficient for ssh client-side
3  * initialisation step
4  *
5  * Rewritten for more speed by Joris van Rantwijk, Jun 1999.
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <assert.h>
12
13 #include "ssh.h"
14
15 #define GET_32BIT(cp) \
16     (((unsigned long)(unsigned char)(cp)[0] << 24) | \
17     ((unsigned long)(unsigned char)(cp)[1] << 16) | \
18     ((unsigned long)(unsigned char)(cp)[2] << 8) | \
19     ((unsigned long)(unsigned char)(cp)[3]))
20
21 #define PUT_32BIT(cp, value) { \
22     (cp)[0] = (unsigned char)((value) >> 24); \
23     (cp)[1] = (unsigned char)((value) >> 16); \
24     (cp)[2] = (unsigned char)((value) >> 8); \
25     (cp)[3] = (unsigned char)(value); }
26
27 int makekey(unsigned char *data, struct RSAKey *result,
28             unsigned char **keystr, int order)
29 {
30     unsigned char *p = data;
31     int i;
32
33     if (result) {
34         result->bits = 0;
35         for (i = 0; i < 4; i++)
36             result->bits = (result->bits << 8) + *p++;
37     } else
38         p += 4;
39
40     /*
41      * order=0 means exponent then modulus (the keys sent by the
42      * server). order=1 means modulus then exponent (the keys
43      * stored in a keyfile).
44      */
45
46     if (order == 0)
47         p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
48     if (result)
49         result->bytes = (((p[0] << 8) + p[1]) + 7) / 8;
50     if (keystr)
51         *keystr = p + 2;
52     p += ssh1_read_bignum(p, result ? &result->modulus : NULL);
53     if (order == 1)
54         p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
55
56     return p - data;
57 }
58
59 int makeprivate(unsigned char *data, struct RSAKey *result)
60 {
61     return ssh1_read_bignum(data, &result->private_exponent);
62 }
63
64 void rsaencrypt(unsigned char *data, int length, struct RSAKey *key)
65 {
66     Bignum b1, b2;
67     int i;
68     unsigned char *p;
69
70     memmove(data + key->bytes - length, data, length);
71     data[0] = 0;
72     data[1] = 2;
73
74     for (i = 2; i < key->bytes - length - 1; i++) {
75         do {
76             data[i] = random_byte();
77         } while (data[i] == 0);
78     }
79     data[key->bytes - length - 1] = 0;
80
81     b1 = bignum_from_bytes(data, key->bytes);
82
83     b2 = modpow(b1, key->exponent, key->modulus);
84
85     p = data;
86     for (i = key->bytes; i--;) {
87         *p++ = bignum_byte(b2, i);
88     }
89
90     freebn(b1);
91     freebn(b2);
92 }
93
94 Bignum rsadecrypt(Bignum input, struct RSAKey *key)
95 {
96     Bignum ret;
97     ret = modpow(input, key->private_exponent, key->modulus);
98     return ret;
99 }
100
101 int rsastr_len(struct RSAKey *key)
102 {
103     Bignum md, ex;
104     int mdlen, exlen;
105
106     md = key->modulus;
107     ex = key->exponent;
108     mdlen = (bignum_bitcount(md) + 15) / 16;
109     exlen = (bignum_bitcount(ex) + 15) / 16;
110     return 4 * (mdlen + exlen) + 20;
111 }
112
113 void rsastr_fmt(char *str, struct RSAKey *key)
114 {
115     Bignum md, ex;
116     int len = 0, i, nibbles;
117     static const char hex[] = "0123456789abcdef";
118
119     md = key->modulus;
120     ex = key->exponent;
121
122     len += sprintf(str + len, "0x");
123
124     nibbles = (3 + bignum_bitcount(ex)) / 4;
125     if (nibbles < 1)
126         nibbles = 1;
127     for (i = nibbles; i--;)
128         str[len++] = hex[(bignum_byte(ex, i / 2) >> (4 * (i % 2))) & 0xF];
129
130     len += sprintf(str + len, ",0x");
131
132     nibbles = (3 + bignum_bitcount(md)) / 4;
133     if (nibbles < 1)
134         nibbles = 1;
135     for (i = nibbles; i--;)
136         str[len++] = hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF];
137
138     str[len] = '\0';
139 }
140
141 /*
142  * Generate a fingerprint string for the key. Compatible with the
143  * OpenSSH fingerprint code.
144  */
145 void rsa_fingerprint(char *str, int len, struct RSAKey *key)
146 {
147     struct MD5Context md5c;
148     unsigned char digest[16];
149     char buffer[16 * 3 + 40];
150     int numlen, slen, i;
151
152     MD5Init(&md5c);
153     numlen = ssh1_bignum_length(key->modulus) - 2;
154     for (i = numlen; i--;) {
155         unsigned char c = bignum_byte(key->modulus, i);
156         MD5Update(&md5c, &c, 1);
157     }
158     numlen = ssh1_bignum_length(key->exponent) - 2;
159     for (i = numlen; i--;) {
160         unsigned char c = bignum_byte(key->exponent, i);
161         MD5Update(&md5c, &c, 1);
162     }
163     MD5Final(digest, &md5c);
164
165     sprintf(buffer, "%d ", bignum_bitcount(key->modulus));
166     for (i = 0; i < 16; i++)
167         sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
168                 digest[i]);
169     strncpy(str, buffer, len);
170     str[len - 1] = '\0';
171     slen = strlen(str);
172     if (key->comment && slen < len - 1) {
173         str[slen] = ' ';
174         strncpy(str + slen + 1, key->comment, len - slen - 1);
175         str[len - 1] = '\0';
176     }
177 }
178
179 /*
180  * Verify that the public data in an RSA key matches the private
181  * data. We also check the private data itself: we ensure that p >
182  * q and that iqmp really is the inverse of q mod p.
183  */
184 int rsa_verify(struct RSAKey *key)
185 {
186     Bignum n, ed, pm1, qm1;
187     int cmp;
188
189     /* n must equal pq. */
190     n = bigmul(key->p, key->q);
191     cmp = bignum_cmp(n, key->modulus);
192     freebn(n);
193     if (cmp != 0)
194         return 0;
195
196     /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
197     pm1 = copybn(key->p);
198     decbn(pm1);
199     ed = modmul(key->exponent, key->private_exponent, pm1);
200     cmp = bignum_cmp(ed, One);
201     sfree(ed);
202     if (cmp != 0)
203         return 0;
204
205     qm1 = copybn(key->q);
206     decbn(qm1);
207     ed = modmul(key->exponent, key->private_exponent, qm1);
208     cmp = bignum_cmp(ed, One);
209     sfree(ed);
210     if (cmp != 0)
211         return 0;
212
213     /*
214      * Ensure p > q.
215      */
216     if (bignum_cmp(key->p, key->q) <= 0)
217         return 0;
218
219     /*
220      * Ensure iqmp * q is congruent to 1, modulo p.
221      */
222     n = modmul(key->iqmp, key->q, key->p);
223     cmp = bignum_cmp(n, One);
224     sfree(n);
225     if (cmp != 0)
226         return 0;
227
228     return 1;
229 }
230
231 /* Public key blob as used by Pageant: exponent before modulus. */
232 unsigned char *rsa_public_blob(struct RSAKey *key, int *len)
233 {
234     int length, pos;
235     unsigned char *ret;
236
237     length = (ssh1_bignum_length(key->modulus) +
238               ssh1_bignum_length(key->exponent) + 4);
239     ret = smalloc(length);
240
241     PUT_32BIT(ret, bignum_bitcount(key->modulus));
242     pos = 4;
243     pos += ssh1_write_bignum(ret + pos, key->exponent);
244     pos += ssh1_write_bignum(ret + pos, key->modulus);
245
246     *len = length;
247     return ret;
248 }
249
250 /* Given a public blob, determine its length. */
251 int rsa_public_blob_len(void *data)
252 {
253     unsigned char *p = (unsigned char *)data;
254     int ret;
255
256     p += 4;                            /* length word */
257     p += ssh1_read_bignum(p, NULL);    /* exponent */
258     p += ssh1_read_bignum(p, NULL);    /* modulus */
259
260     return p - (unsigned char *)data;
261 }
262
263 void freersakey(struct RSAKey *key)
264 {
265     if (key->modulus)
266         freebn(key->modulus);
267     if (key->exponent)
268         freebn(key->exponent);
269     if (key->private_exponent)
270         freebn(key->private_exponent);
271     if (key->comment)
272         sfree(key->comment);
273 }
274
275 /* ----------------------------------------------------------------------
276  * Implementation of the ssh-rsa signing key type. 
277  */
278
279 static void getstring(char **data, int *datalen, char **p, int *length)
280 {
281     *p = NULL;
282     if (*datalen < 4)
283         return;
284     *length = GET_32BIT(*data);
285     *datalen -= 4;
286     *data += 4;
287     if (*datalen < *length)
288         return;
289     *p = *data;
290     *data += *length;
291     *datalen -= *length;
292 }
293 static Bignum getmp(char **data, int *datalen)
294 {
295     char *p;
296     int length;
297     Bignum b;
298
299     getstring(data, datalen, &p, &length);
300     if (!p)
301         return NULL;
302     b = bignum_from_bytes(p, length);
303     return b;
304 }
305
306 static void *rsa2_newkey(char *data, int len)
307 {
308     char *p;
309     int slen;
310     struct RSAKey *rsa;
311
312     rsa = smalloc(sizeof(struct RSAKey));
313     if (!rsa)
314         return NULL;
315     getstring(&data, &len, &p, &slen);
316
317     if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
318         sfree(rsa);
319         return NULL;
320     }
321     rsa->exponent = getmp(&data, &len);
322     rsa->modulus = getmp(&data, &len);
323     rsa->private_exponent = NULL;
324     rsa->comment = NULL;
325
326     return rsa;
327 }
328
329 static void rsa2_freekey(void *key)
330 {
331     struct RSAKey *rsa = (struct RSAKey *) key;
332     freersakey(rsa);
333     sfree(rsa);
334 }
335
336 static char *rsa2_fmtkey(void *key)
337 {
338     struct RSAKey *rsa = (struct RSAKey *) key;
339     char *p;
340     int len;
341
342     len = rsastr_len(rsa);
343     p = smalloc(len);
344     rsastr_fmt(p, rsa);
345     return p;
346 }
347
348 static unsigned char *rsa2_public_blob(void *key, int *len)
349 {
350     struct RSAKey *rsa = (struct RSAKey *) key;
351     int elen, mlen, bloblen;
352     int i;
353     unsigned char *blob, *p;
354
355     elen = (bignum_bitcount(rsa->exponent) + 8) / 8;
356     mlen = (bignum_bitcount(rsa->modulus) + 8) / 8;
357
358     /*
359      * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
360      * (three length fields, 12+7=19).
361      */
362     bloblen = 19 + elen + mlen;
363     blob = smalloc(bloblen);
364     p = blob;
365     PUT_32BIT(p, 7);
366     p += 4;
367     memcpy(p, "ssh-rsa", 7);
368     p += 7;
369     PUT_32BIT(p, elen);
370     p += 4;
371     for (i = elen; i--;)
372         *p++ = bignum_byte(rsa->exponent, i);
373     PUT_32BIT(p, mlen);
374     p += 4;
375     for (i = mlen; i--;)
376         *p++ = bignum_byte(rsa->modulus, i);
377     assert(p == blob + bloblen);
378     *len = bloblen;
379     return blob;
380 }
381
382 static unsigned char *rsa2_private_blob(void *key, int *len)
383 {
384     struct RSAKey *rsa = (struct RSAKey *) key;
385     int dlen, plen, qlen, ulen, bloblen;
386     int i;
387     unsigned char *blob, *p;
388
389     dlen = (bignum_bitcount(rsa->private_exponent) + 8) / 8;
390     plen = (bignum_bitcount(rsa->p) + 8) / 8;
391     qlen = (bignum_bitcount(rsa->q) + 8) / 8;
392     ulen = (bignum_bitcount(rsa->iqmp) + 8) / 8;
393
394     /*
395      * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
396      * sum of lengths.
397      */
398     bloblen = 16 + dlen + plen + qlen + ulen;
399     blob = smalloc(bloblen);
400     p = blob;
401     PUT_32BIT(p, dlen);
402     p += 4;
403     for (i = dlen; i--;)
404         *p++ = bignum_byte(rsa->private_exponent, i);
405     PUT_32BIT(p, plen);
406     p += 4;
407     for (i = plen; i--;)
408         *p++ = bignum_byte(rsa->p, i);
409     PUT_32BIT(p, qlen);
410     p += 4;
411     for (i = qlen; i--;)
412         *p++ = bignum_byte(rsa->q, i);
413     PUT_32BIT(p, ulen);
414     p += 4;
415     for (i = ulen; i--;)
416         *p++ = bignum_byte(rsa->iqmp, i);
417     assert(p == blob + bloblen);
418     *len = bloblen;
419     return blob;
420 }
421
422 static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
423                             unsigned char *priv_blob, int priv_len)
424 {
425     struct RSAKey *rsa;
426     char *pb = (char *) priv_blob;
427
428     rsa = rsa2_newkey((char *) pub_blob, pub_len);
429     rsa->private_exponent = getmp(&pb, &priv_len);
430     rsa->p = getmp(&pb, &priv_len);
431     rsa->q = getmp(&pb, &priv_len);
432     rsa->iqmp = getmp(&pb, &priv_len);
433
434     if (!rsa_verify(rsa)) {
435         rsa2_freekey(rsa);
436         return NULL;
437     }
438
439     return rsa;
440 }
441
442 static void *rsa2_openssh_createkey(unsigned char **blob, int *len)
443 {
444     char **b = (char **) blob;
445     struct RSAKey *rsa;
446
447     rsa = smalloc(sizeof(struct RSAKey));
448     if (!rsa)
449         return NULL;
450     rsa->comment = NULL;
451
452     rsa->modulus = getmp(b, len);
453     rsa->exponent = getmp(b, len);
454     rsa->private_exponent = getmp(b, len);
455     rsa->iqmp = getmp(b, len);
456     rsa->p = getmp(b, len);
457     rsa->q = getmp(b, len);
458
459     if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
460         !rsa->iqmp || !rsa->p || !rsa->q) {
461         sfree(rsa->modulus);
462         sfree(rsa->exponent);
463         sfree(rsa->private_exponent);
464         sfree(rsa->iqmp);
465         sfree(rsa->p);
466         sfree(rsa->q);
467         sfree(rsa);
468         return NULL;
469     }
470
471     return rsa;
472 }
473
474 static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len)
475 {
476     struct RSAKey *rsa = (struct RSAKey *) key;
477     int bloblen, i;
478
479     bloblen =
480         ssh2_bignum_length(rsa->modulus) +
481         ssh2_bignum_length(rsa->exponent) +
482         ssh2_bignum_length(rsa->private_exponent) +
483         ssh2_bignum_length(rsa->iqmp) +
484         ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q);
485
486     if (bloblen > len)
487         return bloblen;
488
489     bloblen = 0;
490 #define ENC(x) \
491     PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
492     for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
493     ENC(rsa->modulus);
494     ENC(rsa->exponent);
495     ENC(rsa->private_exponent);
496     ENC(rsa->iqmp);
497     ENC(rsa->p);
498     ENC(rsa->q);
499
500     return bloblen;
501 }
502
503 static char *rsa2_fingerprint(void *key)
504 {
505     struct RSAKey *rsa = (struct RSAKey *) key;
506     struct MD5Context md5c;
507     unsigned char digest[16], lenbuf[4];
508     char buffer[16 * 3 + 40];
509     char *ret;
510     int numlen, i;
511
512     MD5Init(&md5c);
513     MD5Update(&md5c, "\0\0\0\7ssh-rsa", 11);
514
515 #define ADD_BIGNUM(bignum) \
516     numlen = (bignum_bitcount(bignum)+8)/8; \
517     PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
518     for (i = numlen; i-- ;) { \
519         unsigned char c = bignum_byte(bignum, i); \
520         MD5Update(&md5c, &c, 1); \
521     }
522     ADD_BIGNUM(rsa->exponent);
523     ADD_BIGNUM(rsa->modulus);
524 #undef ADD_BIGNUM
525
526     MD5Final(digest, &md5c);
527
528     sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus));
529     for (i = 0; i < 16; i++)
530         sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
531                 digest[i]);
532     ret = smalloc(strlen(buffer) + 1);
533     if (ret)
534         strcpy(ret, buffer);
535     return ret;
536 }
537
538 /*
539  * This is the magic ASN.1/DER prefix that goes in the decoded
540  * signature, between the string of FFs and the actual SHA hash
541  * value. The meaning of it is:
542  * 
543  * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
544  * 
545  * 30 21 -- a constructed SEQUENCE of length 0x21
546  *    30 09 -- a constructed sub-SEQUENCE of length 9
547  *       06 05 -- an object identifier, length 5
548  *          2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
549  *                            (the 1,3 comes from 0x2B = 43 = 40*1+3)
550  *       05 00 -- NULL
551  *    04 14 -- a primitive OCTET STRING of length 0x14
552  *       [0x14 bytes of hash data follows]
553  * 
554  * The object id in the middle there is listed as `id-sha1' in
555  * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the
556  * ASN module for PKCS #1) and its expanded form is as follows:
557  * 
558  * id-sha1                OBJECT IDENTIFIER ::= {
559  *    iso(1) identified-organization(3) oiw(14) secsig(3)
560  *    algorithms(2) 26 }
561  */
562 static unsigned char asn1_weird_stuff[] = {
563     0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B,
564     0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
565 };
566
567 #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
568
569 static int rsa2_verifysig(void *key, char *sig, int siglen,
570                           char *data, int datalen)
571 {
572     struct RSAKey *rsa = (struct RSAKey *) key;
573     Bignum in, out;
574     char *p;
575     int slen;
576     int bytes, i, j, ret;
577     unsigned char hash[20];
578
579     getstring(&sig, &siglen, &p, &slen);
580     if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
581         return 0;
582     }
583     in = getmp(&sig, &siglen);
584     out = modpow(in, rsa->exponent, rsa->modulus);
585     freebn(in);
586
587     ret = 1;
588
589     bytes = bignum_bitcount(rsa->modulus) / 8;
590     /* Top (partial) byte should be zero. */
591     if (bignum_byte(out, bytes - 1) != 0)
592         ret = 0;
593     /* First whole byte should be 1. */
594     if (bignum_byte(out, bytes - 2) != 1)
595         ret = 0;
596     /* Most of the rest should be FF. */
597     for (i = bytes - 3; i >= 20 + ASN1_LEN; i--) {
598         if (bignum_byte(out, i) != 0xFF)
599             ret = 0;
600     }
601     /* Then we expect to see the asn1_weird_stuff. */
602     for (i = 20 + ASN1_LEN - 1, j = 0; i >= 20; i--, j++) {
603         if (bignum_byte(out, i) != asn1_weird_stuff[j])
604             ret = 0;
605     }
606     /* Finally, we expect to see the SHA-1 hash of the signed data. */
607     SHA_Simple(data, datalen, hash);
608     for (i = 19, j = 0; i >= 0; i--, j++) {
609         if (bignum_byte(out, i) != hash[j])
610             ret = 0;
611     }
612
613     return ret;
614 }
615
616 unsigned char *rsa2_sign(void *key, char *data, int datalen, int *siglen)
617 {
618     struct RSAKey *rsa = (struct RSAKey *) key;
619     unsigned char *bytes;
620     int nbytes;
621     unsigned char hash[20];
622     Bignum in, out;
623     int i, j;
624
625     SHA_Simple(data, datalen, hash);
626
627     nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8;
628     bytes = smalloc(nbytes);
629
630     bytes[0] = 1;
631     for (i = 1; i < nbytes - 20 - ASN1_LEN; i++)
632         bytes[i] = 0xFF;
633     for (i = nbytes - 20 - ASN1_LEN, j = 0; i < nbytes - 20; i++, j++)
634         bytes[i] = asn1_weird_stuff[j];
635     for (i = nbytes - 20, j = 0; i < nbytes; i++, j++)
636         bytes[i] = hash[j];
637
638     in = bignum_from_bytes(bytes, nbytes);
639     sfree(bytes);
640
641     out = modpow(in, rsa->private_exponent, rsa->modulus);
642     freebn(in);
643
644     nbytes = (bignum_bitcount(out) + 7) / 8;
645     bytes = smalloc(4 + 7 + 4 + nbytes);
646     PUT_32BIT(bytes, 7);
647     memcpy(bytes + 4, "ssh-rsa", 7);
648     PUT_32BIT(bytes + 4 + 7, nbytes);
649     for (i = 0; i < nbytes; i++)
650         bytes[4 + 7 + 4 + i] = bignum_byte(out, nbytes - 1 - i);
651     freebn(out);
652
653     *siglen = 4 + 7 + 4 + nbytes;
654     return bytes;
655 }
656
657 const struct ssh_signkey ssh_rsa = {
658     rsa2_newkey,
659     rsa2_freekey,
660     rsa2_fmtkey,
661     rsa2_public_blob,
662     rsa2_private_blob,
663     rsa2_createkey,
664     rsa2_openssh_createkey,
665     rsa2_openssh_fmtkey,
666     rsa2_fingerprint,
667     rsa2_verifysig,
668     rsa2_sign,
669     "ssh-rsa",
670     "rsa2"
671 };