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