374330e2 |
1 | /* |
8671a580 |
2 | * RSA implementation for PuTTY. |
374330e2 |
3 | */ |
4 | |
374330e2 |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <string.h> |
65a22376 |
8 | #include <assert.h> |
374330e2 |
9 | |
e5574168 |
10 | #include "ssh.h" |
8365990c |
11 | #include "misc.h" |
374330e2 |
12 | |
3f2d010c |
13 | #define GET_32BIT(cp) \ |
14 | (((unsigned long)(unsigned char)(cp)[0] << 24) | \ |
15 | ((unsigned long)(unsigned char)(cp)[1] << 16) | \ |
16 | ((unsigned long)(unsigned char)(cp)[2] << 8) | \ |
17 | ((unsigned long)(unsigned char)(cp)[3])) |
18 | |
19 | #define PUT_32BIT(cp, value) { \ |
20 | (cp)[0] = (unsigned char)((value) >> 24); \ |
21 | (cp)[1] = (unsigned char)((value) >> 16); \ |
22 | (cp)[2] = (unsigned char)((value) >> 8); \ |
23 | (cp)[3] = (unsigned char)(value); } |
1c2a93c4 |
24 | |
374330e2 |
25 | int makekey(unsigned char *data, struct RSAKey *result, |
32874aea |
26 | unsigned char **keystr, int order) |
27 | { |
374330e2 |
28 | unsigned char *p = data; |
7cca0d81 |
29 | int i; |
374330e2 |
30 | |
a52f067e |
31 | if (result) { |
32874aea |
32 | result->bits = 0; |
33 | for (i = 0; i < 4; i++) |
34 | result->bits = (result->bits << 8) + *p++; |
a52f067e |
35 | } else |
32874aea |
36 | p += 4; |
374330e2 |
37 | |
7cca0d81 |
38 | /* |
39 | * order=0 means exponent then modulus (the keys sent by the |
40 | * server). order=1 means modulus then exponent (the keys |
41 | * stored in a keyfile). |
42 | */ |
374330e2 |
43 | |
7cca0d81 |
44 | if (order == 0) |
32874aea |
45 | p += ssh1_read_bignum(p, result ? &result->exponent : NULL); |
a52f067e |
46 | if (result) |
32874aea |
47 | result->bytes = (((p[0] << 8) + p[1]) + 7) / 8; |
48 | if (keystr) |
49 | *keystr = p + 2; |
a52f067e |
50 | p += ssh1_read_bignum(p, result ? &result->modulus : NULL); |
7cca0d81 |
51 | if (order == 1) |
32874aea |
52 | p += ssh1_read_bignum(p, result ? &result->exponent : NULL); |
374330e2 |
53 | |
54 | return p - data; |
55 | } |
56 | |
32874aea |
57 | int makeprivate(unsigned char *data, struct RSAKey *result) |
58 | { |
7cca0d81 |
59 | return ssh1_read_bignum(data, &result->private_exponent); |
60 | } |
61 | |
32874aea |
62 | void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) |
63 | { |
374330e2 |
64 | Bignum b1, b2; |
3709bfe9 |
65 | int i; |
374330e2 |
66 | unsigned char *p; |
67 | |
32874aea |
68 | memmove(data + key->bytes - length, data, length); |
374330e2 |
69 | data[0] = 0; |
70 | data[1] = 2; |
71 | |
32874aea |
72 | for (i = 2; i < key->bytes - length - 1; i++) { |
374330e2 |
73 | do { |
74 | data[i] = random_byte(); |
75 | } while (data[i] == 0); |
76 | } |
32874aea |
77 | data[key->bytes - length - 1] = 0; |
374330e2 |
78 | |
3709bfe9 |
79 | b1 = bignum_from_bytes(data, key->bytes); |
374330e2 |
80 | |
59600f67 |
81 | b2 = modpow(b1, key->exponent, key->modulus); |
374330e2 |
82 | |
374330e2 |
83 | p = data; |
32874aea |
84 | for (i = key->bytes; i--;) { |
85 | *p++ = bignum_byte(b2, i); |
374330e2 |
86 | } |
87 | |
88 | freebn(b1); |
89 | freebn(b2); |
90 | } |
91 | |
b492c4d7 |
92 | static void sha512_mpint(SHA512_State * s, Bignum b) |
93 | { |
94 | unsigned char lenbuf[4]; |
95 | int len; |
96 | len = (bignum_bitcount(b) + 8) / 8; |
97 | PUT_32BIT(lenbuf, len); |
98 | SHA512_Bytes(s, lenbuf, 4); |
99 | while (len-- > 0) { |
100 | lenbuf[0] = bignum_byte(b, len); |
101 | SHA512_Bytes(s, lenbuf, 1); |
102 | } |
103 | memset(lenbuf, 0, sizeof(lenbuf)); |
104 | } |
105 | |
8671a580 |
106 | /* |
107 | * This function is a wrapper on modpow(). It has the same effect |
108 | * as modpow(), but employs RSA blinding to protect against timing |
109 | * attacks. |
110 | */ |
111 | static Bignum rsa_privkey_op(Bignum input, struct RSAKey *key) |
32874aea |
112 | { |
8671a580 |
113 | Bignum random, random_encrypted, random_inverse; |
114 | Bignum input_blinded, ret_blinded; |
7cca0d81 |
115 | Bignum ret; |
8671a580 |
116 | |
b492c4d7 |
117 | SHA512_State ss; |
118 | unsigned char digest512[64]; |
119 | int digestused = lenof(digest512); |
120 | int hashseq = 0; |
121 | |
8671a580 |
122 | /* |
123 | * Start by inventing a random number chosen uniformly from the |
124 | * range 2..modulus-1. (We do this by preparing a random number |
125 | * of the right length and retrying if it's greater than the |
126 | * modulus, to prevent any potential Bleichenbacher-like |
127 | * attacks making use of the uneven distribution within the |
128 | * range that would arise from just reducing our number mod n. |
129 | * There are timing implications to the potential retries, of |
130 | * course, but all they tell you is the modulus, which you |
131 | * already knew.) |
b492c4d7 |
132 | * |
133 | * To preserve determinism and avoid Pageant needing to share |
134 | * the random number pool, we actually generate this `random' |
135 | * number by hashing stuff with the private key. |
8671a580 |
136 | */ |
137 | while (1) { |
138 | int bits, byte, bitsleft, v; |
139 | random = copybn(key->modulus); |
140 | /* |
141 | * Find the topmost set bit. (This function will return its |
142 | * index plus one.) Then we'll set all bits from that one |
143 | * downwards randomly. |
144 | */ |
145 | bits = bignum_bitcount(random); |
146 | byte = 0; |
147 | bitsleft = 0; |
148 | while (bits--) { |
b492c4d7 |
149 | if (bitsleft <= 0) { |
150 | bitsleft = 8; |
151 | /* |
152 | * Conceptually the following few lines are equivalent to |
153 | * byte = random_byte(); |
154 | */ |
155 | if (digestused >= lenof(digest512)) { |
156 | unsigned char seqbuf[4]; |
157 | PUT_32BIT(seqbuf, hashseq); |
158 | SHA512_Init(&ss); |
159 | SHA512_Bytes(&ss, "RSA deterministic blinding", 26); |
160 | SHA512_Bytes(&ss, seqbuf, sizeof(seqbuf)); |
161 | sha512_mpint(&ss, key->private_exponent); |
162 | SHA512_Final(&ss, digest512); |
163 | hashseq++; |
164 | |
165 | /* |
166 | * Now hash that digest plus the signature |
167 | * input. |
168 | */ |
169 | SHA512_Init(&ss); |
170 | SHA512_Bytes(&ss, digest512, sizeof(digest512)); |
171 | sha512_mpint(&ss, input); |
172 | SHA512_Final(&ss, digest512); |
173 | |
174 | digestused = 0; |
175 | } |
176 | byte = digest512[digestused++]; |
177 | } |
8671a580 |
178 | v = byte & 1; |
179 | byte >>= 1; |
180 | bitsleft--; |
181 | bignum_set_bit(random, bits, v); |
182 | } |
183 | |
184 | /* |
185 | * Now check that this number is strictly greater than |
186 | * zero, and strictly less than modulus. |
187 | */ |
188 | if (bignum_cmp(random, Zero) <= 0 || |
189 | bignum_cmp(random, key->modulus) >= 0) { |
190 | freebn(random); |
191 | continue; |
192 | } else { |
193 | break; |
194 | } |
195 | } |
196 | |
197 | /* |
198 | * RSA blinding relies on the fact that (xy)^d mod n is equal |
199 | * to (x^d mod n) * (y^d mod n) mod n. We invent a random pair |
033a3ded |
200 | * y and y^d; then we multiply x by y, raise to the power d mod |
201 | * n as usual, and divide by y^d to recover x^d. Thus an |
202 | * attacker can't correlate the timing of the modpow with the |
203 | * input, because they don't know anything about the number |
204 | * that was input to the actual modpow. |
8671a580 |
205 | * |
206 | * The clever bit is that we don't have to do a huge modpow to |
207 | * get y and y^d; we will use the number we just invented as |
033a3ded |
208 | * _y^d_, and use the _public_ exponent to compute (y^d)^e = y |
209 | * from it, which is much faster to do. |
8671a580 |
210 | */ |
211 | random_encrypted = modpow(random, key->exponent, key->modulus); |
212 | random_inverse = modinv(random, key->modulus); |
213 | input_blinded = modmul(input, random_encrypted, key->modulus); |
214 | ret_blinded = modpow(input_blinded, key->private_exponent, key->modulus); |
215 | ret = modmul(ret_blinded, random_inverse, key->modulus); |
216 | |
217 | freebn(ret_blinded); |
218 | freebn(input_blinded); |
219 | freebn(random_inverse); |
220 | freebn(random_encrypted); |
221 | freebn(random); |
222 | |
7cca0d81 |
223 | return ret; |
224 | } |
225 | |
8671a580 |
226 | Bignum rsadecrypt(Bignum input, struct RSAKey *key) |
227 | { |
228 | return rsa_privkey_op(input, key); |
229 | } |
230 | |
32874aea |
231 | int rsastr_len(struct RSAKey *key) |
232 | { |
374330e2 |
233 | Bignum md, ex; |
3709bfe9 |
234 | int mdlen, exlen; |
374330e2 |
235 | |
236 | md = key->modulus; |
237 | ex = key->exponent; |
32874aea |
238 | mdlen = (bignum_bitcount(md) + 15) / 16; |
239 | exlen = (bignum_bitcount(ex) + 15) / 16; |
240 | return 4 * (mdlen + exlen) + 20; |
374330e2 |
241 | } |
242 | |
32874aea |
243 | void rsastr_fmt(char *str, struct RSAKey *key) |
244 | { |
374330e2 |
245 | Bignum md, ex; |
d5859615 |
246 | int len = 0, i, nibbles; |
247 | static const char hex[] = "0123456789abcdef"; |
374330e2 |
248 | |
249 | md = key->modulus; |
250 | ex = key->exponent; |
251 | |
32874aea |
252 | len += sprintf(str + len, "0x"); |
d5859615 |
253 | |
32874aea |
254 | nibbles = (3 + bignum_bitcount(ex)) / 4; |
255 | if (nibbles < 1) |
256 | nibbles = 1; |
257 | for (i = nibbles; i--;) |
258 | str[len++] = hex[(bignum_byte(ex, i / 2) >> (4 * (i % 2))) & 0xF]; |
d5859615 |
259 | |
32874aea |
260 | len += sprintf(str + len, ",0x"); |
d5859615 |
261 | |
32874aea |
262 | nibbles = (3 + bignum_bitcount(md)) / 4; |
263 | if (nibbles < 1) |
264 | nibbles = 1; |
265 | for (i = nibbles; i--;) |
266 | str[len++] = hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF]; |
d5859615 |
267 | |
374330e2 |
268 | str[len] = '\0'; |
269 | } |
270 | |
1c2a93c4 |
271 | /* |
272 | * Generate a fingerprint string for the key. Compatible with the |
273 | * OpenSSH fingerprint code. |
274 | */ |
32874aea |
275 | void rsa_fingerprint(char *str, int len, struct RSAKey *key) |
276 | { |
1c2a93c4 |
277 | struct MD5Context md5c; |
278 | unsigned char digest[16]; |
32874aea |
279 | char buffer[16 * 3 + 40]; |
1c2a93c4 |
280 | int numlen, slen, i; |
281 | |
282 | MD5Init(&md5c); |
283 | numlen = ssh1_bignum_length(key->modulus) - 2; |
32874aea |
284 | for (i = numlen; i--;) { |
285 | unsigned char c = bignum_byte(key->modulus, i); |
286 | MD5Update(&md5c, &c, 1); |
1c2a93c4 |
287 | } |
288 | numlen = ssh1_bignum_length(key->exponent) - 2; |
32874aea |
289 | for (i = numlen; i--;) { |
290 | unsigned char c = bignum_byte(key->exponent, i); |
291 | MD5Update(&md5c, &c, 1); |
1c2a93c4 |
292 | } |
293 | MD5Final(digest, &md5c); |
294 | |
ddecd643 |
295 | sprintf(buffer, "%d ", bignum_bitcount(key->modulus)); |
1c2a93c4 |
296 | for (i = 0; i < 16; i++) |
32874aea |
297 | sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "", |
298 | digest[i]); |
299 | strncpy(str, buffer, len); |
300 | str[len - 1] = '\0'; |
1c2a93c4 |
301 | slen = strlen(str); |
32874aea |
302 | if (key->comment && slen < len - 1) { |
303 | str[slen] = ' '; |
304 | strncpy(str + slen + 1, key->comment, len - slen - 1); |
305 | str[len - 1] = '\0'; |
1c2a93c4 |
306 | } |
307 | } |
308 | |
98f022f5 |
309 | /* |
310 | * Verify that the public data in an RSA key matches the private |
60fe6ff7 |
311 | * data. We also check the private data itself: we ensure that p > |
312 | * q and that iqmp really is the inverse of q mod p. |
98f022f5 |
313 | */ |
32874aea |
314 | int rsa_verify(struct RSAKey *key) |
315 | { |
60fe6ff7 |
316 | Bignum n, ed, pm1, qm1; |
98f022f5 |
317 | int cmp; |
318 | |
319 | /* n must equal pq. */ |
320 | n = bigmul(key->p, key->q); |
321 | cmp = bignum_cmp(n, key->modulus); |
322 | freebn(n); |
323 | if (cmp != 0) |
324 | return 0; |
325 | |
60fe6ff7 |
326 | /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */ |
98f022f5 |
327 | pm1 = copybn(key->p); |
328 | decbn(pm1); |
60fe6ff7 |
329 | ed = modmul(key->exponent, key->private_exponent, pm1); |
330 | cmp = bignum_cmp(ed, One); |
331 | sfree(ed); |
332 | if (cmp != 0) |
333 | return 0; |
334 | |
98f022f5 |
335 | qm1 = copybn(key->q); |
336 | decbn(qm1); |
60fe6ff7 |
337 | ed = modmul(key->exponent, key->private_exponent, qm1); |
98f022f5 |
338 | cmp = bignum_cmp(ed, One); |
339 | sfree(ed); |
340 | if (cmp != 0) |
341 | return 0; |
014970c8 |
342 | |
60fe6ff7 |
343 | /* |
344 | * Ensure p > q. |
345 | */ |
346 | if (bignum_cmp(key->p, key->q) <= 0) |
32874aea |
347 | return 0; |
60fe6ff7 |
348 | |
349 | /* |
350 | * Ensure iqmp * q is congruent to 1, modulo p. |
351 | */ |
352 | n = modmul(key->iqmp, key->q, key->p); |
353 | cmp = bignum_cmp(n, One); |
354 | sfree(n); |
355 | if (cmp != 0) |
32874aea |
356 | return 0; |
60fe6ff7 |
357 | |
014970c8 |
358 | return 1; |
98f022f5 |
359 | } |
360 | |
3f2d010c |
361 | /* Public key blob as used by Pageant: exponent before modulus. */ |
362 | unsigned char *rsa_public_blob(struct RSAKey *key, int *len) |
363 | { |
364 | int length, pos; |
365 | unsigned char *ret; |
366 | |
367 | length = (ssh1_bignum_length(key->modulus) + |
368 | ssh1_bignum_length(key->exponent) + 4); |
3d88e64d |
369 | ret = snewn(length, unsigned char); |
3f2d010c |
370 | |
371 | PUT_32BIT(ret, bignum_bitcount(key->modulus)); |
372 | pos = 4; |
373 | pos += ssh1_write_bignum(ret + pos, key->exponent); |
374 | pos += ssh1_write_bignum(ret + pos, key->modulus); |
375 | |
376 | *len = length; |
377 | return ret; |
378 | } |
379 | |
380 | /* Given a public blob, determine its length. */ |
381 | int rsa_public_blob_len(void *data) |
382 | { |
383 | unsigned char *p = (unsigned char *)data; |
3f2d010c |
384 | |
385 | p += 4; /* length word */ |
386 | p += ssh1_read_bignum(p, NULL); /* exponent */ |
387 | p += ssh1_read_bignum(p, NULL); /* modulus */ |
388 | |
389 | return p - (unsigned char *)data; |
390 | } |
391 | |
32874aea |
392 | void freersakey(struct RSAKey *key) |
393 | { |
394 | if (key->modulus) |
395 | freebn(key->modulus); |
396 | if (key->exponent) |
397 | freebn(key->exponent); |
398 | if (key->private_exponent) |
399 | freebn(key->private_exponent); |
400 | if (key->comment) |
401 | sfree(key->comment); |
5c58ad2d |
402 | } |
85cc02bb |
403 | |
404 | /* ---------------------------------------------------------------------- |
405 | * Implementation of the ssh-rsa signing key type. |
406 | */ |
407 | |
32874aea |
408 | static void getstring(char **data, int *datalen, char **p, int *length) |
409 | { |
85cc02bb |
410 | *p = NULL; |
411 | if (*datalen < 4) |
32874aea |
412 | return; |
85cc02bb |
413 | *length = GET_32BIT(*data); |
32874aea |
414 | *datalen -= 4; |
415 | *data += 4; |
85cc02bb |
416 | if (*datalen < *length) |
32874aea |
417 | return; |
85cc02bb |
418 | *p = *data; |
32874aea |
419 | *data += *length; |
420 | *datalen -= *length; |
85cc02bb |
421 | } |
32874aea |
422 | static Bignum getmp(char **data, int *datalen) |
423 | { |
85cc02bb |
424 | char *p; |
425 | int length; |
426 | Bignum b; |
427 | |
428 | getstring(data, datalen, &p, &length); |
429 | if (!p) |
32874aea |
430 | return NULL; |
9bf430c9 |
431 | b = bignum_from_bytes((unsigned char *)p, length); |
85cc02bb |
432 | return b; |
433 | } |
434 | |
32874aea |
435 | static void *rsa2_newkey(char *data, int len) |
436 | { |
85cc02bb |
437 | char *p; |
438 | int slen; |
439 | struct RSAKey *rsa; |
440 | |
3d88e64d |
441 | rsa = snew(struct RSAKey); |
32874aea |
442 | if (!rsa) |
443 | return NULL; |
85cc02bb |
444 | getstring(&data, &len, &p, &slen); |
445 | |
45cebe79 |
446 | if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) { |
85cc02bb |
447 | sfree(rsa); |
448 | return NULL; |
449 | } |
450 | rsa->exponent = getmp(&data, &len); |
451 | rsa->modulus = getmp(&data, &len); |
452 | rsa->private_exponent = NULL; |
453 | rsa->comment = NULL; |
454 | |
455 | return rsa; |
456 | } |
457 | |
32874aea |
458 | static void rsa2_freekey(void *key) |
459 | { |
460 | struct RSAKey *rsa = (struct RSAKey *) key; |
85cc02bb |
461 | freersakey(rsa); |
462 | sfree(rsa); |
463 | } |
464 | |
32874aea |
465 | static char *rsa2_fmtkey(void *key) |
466 | { |
467 | struct RSAKey *rsa = (struct RSAKey *) key; |
85cc02bb |
468 | char *p; |
469 | int len; |
32874aea |
470 | |
85cc02bb |
471 | len = rsastr_len(rsa); |
3d88e64d |
472 | p = snewn(len, char); |
32874aea |
473 | rsastr_fmt(p, rsa); |
85cc02bb |
474 | return p; |
475 | } |
476 | |
32874aea |
477 | static unsigned char *rsa2_public_blob(void *key, int *len) |
478 | { |
479 | struct RSAKey *rsa = (struct RSAKey *) key; |
65a22376 |
480 | int elen, mlen, bloblen; |
481 | int i; |
482 | unsigned char *blob, *p; |
483 | |
32874aea |
484 | elen = (bignum_bitcount(rsa->exponent) + 8) / 8; |
485 | mlen = (bignum_bitcount(rsa->modulus) + 8) / 8; |
65a22376 |
486 | |
487 | /* |
488 | * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen. |
489 | * (three length fields, 12+7=19). |
490 | */ |
32874aea |
491 | bloblen = 19 + elen + mlen; |
3d88e64d |
492 | blob = snewn(bloblen, unsigned char); |
65a22376 |
493 | p = blob; |
32874aea |
494 | PUT_32BIT(p, 7); |
495 | p += 4; |
496 | memcpy(p, "ssh-rsa", 7); |
497 | p += 7; |
498 | PUT_32BIT(p, elen); |
499 | p += 4; |
500 | for (i = elen; i--;) |
501 | *p++ = bignum_byte(rsa->exponent, i); |
502 | PUT_32BIT(p, mlen); |
503 | p += 4; |
504 | for (i = mlen; i--;) |
505 | *p++ = bignum_byte(rsa->modulus, i); |
65a22376 |
506 | assert(p == blob + bloblen); |
507 | *len = bloblen; |
508 | return blob; |
509 | } |
510 | |
32874aea |
511 | static unsigned char *rsa2_private_blob(void *key, int *len) |
512 | { |
513 | struct RSAKey *rsa = (struct RSAKey *) key; |
65a22376 |
514 | int dlen, plen, qlen, ulen, bloblen; |
515 | int i; |
516 | unsigned char *blob, *p; |
517 | |
32874aea |
518 | dlen = (bignum_bitcount(rsa->private_exponent) + 8) / 8; |
519 | plen = (bignum_bitcount(rsa->p) + 8) / 8; |
520 | qlen = (bignum_bitcount(rsa->q) + 8) / 8; |
521 | ulen = (bignum_bitcount(rsa->iqmp) + 8) / 8; |
65a22376 |
522 | |
523 | /* |
524 | * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 + |
525 | * sum of lengths. |
526 | */ |
32874aea |
527 | bloblen = 16 + dlen + plen + qlen + ulen; |
3d88e64d |
528 | blob = snewn(bloblen, unsigned char); |
65a22376 |
529 | p = blob; |
32874aea |
530 | PUT_32BIT(p, dlen); |
531 | p += 4; |
532 | for (i = dlen; i--;) |
533 | *p++ = bignum_byte(rsa->private_exponent, i); |
534 | PUT_32BIT(p, plen); |
535 | p += 4; |
536 | for (i = plen; i--;) |
537 | *p++ = bignum_byte(rsa->p, i); |
538 | PUT_32BIT(p, qlen); |
539 | p += 4; |
540 | for (i = qlen; i--;) |
541 | *p++ = bignum_byte(rsa->q, i); |
542 | PUT_32BIT(p, ulen); |
543 | p += 4; |
544 | for (i = ulen; i--;) |
545 | *p++ = bignum_byte(rsa->iqmp, i); |
65a22376 |
546 | assert(p == blob + bloblen); |
547 | *len = bloblen; |
548 | return blob; |
549 | } |
550 | |
551 | static void *rsa2_createkey(unsigned char *pub_blob, int pub_len, |
32874aea |
552 | unsigned char *priv_blob, int priv_len) |
553 | { |
65a22376 |
554 | struct RSAKey *rsa; |
32874aea |
555 | char *pb = (char *) priv_blob; |
556 | |
557 | rsa = rsa2_newkey((char *) pub_blob, pub_len); |
65a22376 |
558 | rsa->private_exponent = getmp(&pb, &priv_len); |
559 | rsa->p = getmp(&pb, &priv_len); |
560 | rsa->q = getmp(&pb, &priv_len); |
561 | rsa->iqmp = getmp(&pb, &priv_len); |
562 | |
98f022f5 |
563 | if (!rsa_verify(rsa)) { |
564 | rsa2_freekey(rsa); |
565 | return NULL; |
566 | } |
567 | |
65a22376 |
568 | return rsa; |
569 | } |
570 | |
32874aea |
571 | static void *rsa2_openssh_createkey(unsigned char **blob, int *len) |
572 | { |
573 | char **b = (char **) blob; |
45cebe79 |
574 | struct RSAKey *rsa; |
45cebe79 |
575 | |
3d88e64d |
576 | rsa = snew(struct RSAKey); |
32874aea |
577 | if (!rsa) |
578 | return NULL; |
45cebe79 |
579 | rsa->comment = NULL; |
580 | |
581 | rsa->modulus = getmp(b, len); |
582 | rsa->exponent = getmp(b, len); |
583 | rsa->private_exponent = getmp(b, len); |
584 | rsa->iqmp = getmp(b, len); |
585 | rsa->p = getmp(b, len); |
586 | rsa->q = getmp(b, len); |
587 | |
588 | if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent || |
589 | !rsa->iqmp || !rsa->p || !rsa->q) { |
590 | sfree(rsa->modulus); |
591 | sfree(rsa->exponent); |
592 | sfree(rsa->private_exponent); |
593 | sfree(rsa->iqmp); |
594 | sfree(rsa->p); |
595 | sfree(rsa->q); |
596 | sfree(rsa); |
597 | return NULL; |
598 | } |
599 | |
600 | return rsa; |
601 | } |
602 | |
32874aea |
603 | static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len) |
604 | { |
605 | struct RSAKey *rsa = (struct RSAKey *) key; |
ddecd643 |
606 | int bloblen, i; |
607 | |
608 | bloblen = |
609 | ssh2_bignum_length(rsa->modulus) + |
610 | ssh2_bignum_length(rsa->exponent) + |
611 | ssh2_bignum_length(rsa->private_exponent) + |
612 | ssh2_bignum_length(rsa->iqmp) + |
32874aea |
613 | ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q); |
ddecd643 |
614 | |
615 | if (bloblen > len) |
616 | return bloblen; |
617 | |
618 | bloblen = 0; |
619 | #define ENC(x) \ |
620 | PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \ |
621 | for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i); |
622 | ENC(rsa->modulus); |
623 | ENC(rsa->exponent); |
624 | ENC(rsa->private_exponent); |
625 | ENC(rsa->iqmp); |
626 | ENC(rsa->p); |
627 | ENC(rsa->q); |
628 | |
629 | return bloblen; |
630 | } |
631 | |
47a6b94c |
632 | static int rsa2_pubkey_bits(void *blob, int len) |
633 | { |
634 | struct RSAKey *rsa; |
635 | int ret; |
636 | |
637 | rsa = rsa2_newkey((char *) blob, len); |
638 | ret = bignum_bitcount(rsa->modulus); |
639 | rsa2_freekey(rsa); |
640 | |
641 | return ret; |
642 | } |
643 | |
32874aea |
644 | static char *rsa2_fingerprint(void *key) |
645 | { |
646 | struct RSAKey *rsa = (struct RSAKey *) key; |
85cc02bb |
647 | struct MD5Context md5c; |
648 | unsigned char digest[16], lenbuf[4]; |
32874aea |
649 | char buffer[16 * 3 + 40]; |
85cc02bb |
650 | char *ret; |
651 | int numlen, i; |
652 | |
653 | MD5Init(&md5c); |
9bf430c9 |
654 | MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-rsa", 11); |
85cc02bb |
655 | |
656 | #define ADD_BIGNUM(bignum) \ |
ddecd643 |
657 | numlen = (bignum_bitcount(bignum)+8)/8; \ |
85cc02bb |
658 | PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \ |
659 | for (i = numlen; i-- ;) { \ |
660 | unsigned char c = bignum_byte(bignum, i); \ |
661 | MD5Update(&md5c, &c, 1); \ |
662 | } |
663 | ADD_BIGNUM(rsa->exponent); |
664 | ADD_BIGNUM(rsa->modulus); |
665 | #undef ADD_BIGNUM |
666 | |
667 | MD5Final(digest, &md5c); |
668 | |
ddecd643 |
669 | sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus)); |
85cc02bb |
670 | for (i = 0; i < 16; i++) |
32874aea |
671 | sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "", |
672 | digest[i]); |
3d88e64d |
673 | ret = snewn(strlen(buffer) + 1, char); |
85cc02bb |
674 | if (ret) |
32874aea |
675 | strcpy(ret, buffer); |
85cc02bb |
676 | return ret; |
677 | } |
678 | |
679 | /* |
680 | * This is the magic ASN.1/DER prefix that goes in the decoded |
681 | * signature, between the string of FFs and the actual SHA hash |
96a73db9 |
682 | * value. The meaning of it is: |
85cc02bb |
683 | * |
684 | * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself |
685 | * |
686 | * 30 21 -- a constructed SEQUENCE of length 0x21 |
687 | * 30 09 -- a constructed sub-SEQUENCE of length 9 |
688 | * 06 05 -- an object identifier, length 5 |
96a73db9 |
689 | * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 } |
690 | * (the 1,3 comes from 0x2B = 43 = 40*1+3) |
85cc02bb |
691 | * 05 00 -- NULL |
692 | * 04 14 -- a primitive OCTET STRING of length 0x14 |
693 | * [0x14 bytes of hash data follows] |
96a73db9 |
694 | * |
695 | * The object id in the middle there is listed as `id-sha1' in |
696 | * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the |
697 | * ASN module for PKCS #1) and its expanded form is as follows: |
698 | * |
699 | * id-sha1 OBJECT IDENTIFIER ::= { |
700 | * iso(1) identified-organization(3) oiw(14) secsig(3) |
701 | * algorithms(2) 26 } |
85cc02bb |
702 | */ |
b5864f2c |
703 | static const unsigned char asn1_weird_stuff[] = { |
32874aea |
704 | 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, |
705 | 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14, |
85cc02bb |
706 | }; |
707 | |
d8770b12 |
708 | #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) ) |
709 | |
85cc02bb |
710 | static int rsa2_verifysig(void *key, char *sig, int siglen, |
32874aea |
711 | char *data, int datalen) |
712 | { |
713 | struct RSAKey *rsa = (struct RSAKey *) key; |
85cc02bb |
714 | Bignum in, out; |
715 | char *p; |
716 | int slen; |
717 | int bytes, i, j, ret; |
718 | unsigned char hash[20]; |
719 | |
720 | getstring(&sig, &siglen, &p, &slen); |
721 | if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) { |
32874aea |
722 | return 0; |
85cc02bb |
723 | } |
724 | in = getmp(&sig, &siglen); |
725 | out = modpow(in, rsa->exponent, rsa->modulus); |
726 | freebn(in); |
727 | |
728 | ret = 1; |
729 | |
ddecd643 |
730 | bytes = bignum_bitcount(rsa->modulus) / 8; |
85cc02bb |
731 | /* Top (partial) byte should be zero. */ |
32874aea |
732 | if (bignum_byte(out, bytes - 1) != 0) |
733 | ret = 0; |
85cc02bb |
734 | /* First whole byte should be 1. */ |
32874aea |
735 | if (bignum_byte(out, bytes - 2) != 1) |
736 | ret = 0; |
85cc02bb |
737 | /* Most of the rest should be FF. */ |
32874aea |
738 | for (i = bytes - 3; i >= 20 + ASN1_LEN; i--) { |
739 | if (bignum_byte(out, i) != 0xFF) |
740 | ret = 0; |
85cc02bb |
741 | } |
742 | /* Then we expect to see the asn1_weird_stuff. */ |
32874aea |
743 | for (i = 20 + ASN1_LEN - 1, j = 0; i >= 20; i--, j++) { |
744 | if (bignum_byte(out, i) != asn1_weird_stuff[j]) |
745 | ret = 0; |
85cc02bb |
746 | } |
747 | /* Finally, we expect to see the SHA-1 hash of the signed data. */ |
748 | SHA_Simple(data, datalen, hash); |
32874aea |
749 | for (i = 19, j = 0; i >= 0; i--, j++) { |
750 | if (bignum_byte(out, i) != hash[j]) |
751 | ret = 0; |
85cc02bb |
752 | } |
679539d7 |
753 | freebn(out); |
85cc02bb |
754 | |
755 | return ret; |
756 | } |
757 | |
164feb13 |
758 | static unsigned char *rsa2_sign(void *key, char *data, int datalen, |
759 | int *siglen) |
32874aea |
760 | { |
761 | struct RSAKey *rsa = (struct RSAKey *) key; |
65a22376 |
762 | unsigned char *bytes; |
763 | int nbytes; |
764 | unsigned char hash[20]; |
765 | Bignum in, out; |
766 | int i, j; |
767 | |
768 | SHA_Simple(data, datalen, hash); |
769 | |
32874aea |
770 | nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8; |
3d88e64d |
771 | bytes = snewn(nbytes, unsigned char); |
65a22376 |
772 | |
773 | bytes[0] = 1; |
32874aea |
774 | for (i = 1; i < nbytes - 20 - ASN1_LEN; i++) |
65a22376 |
775 | bytes[i] = 0xFF; |
32874aea |
776 | for (i = nbytes - 20 - ASN1_LEN, j = 0; i < nbytes - 20; i++, j++) |
65a22376 |
777 | bytes[i] = asn1_weird_stuff[j]; |
32874aea |
778 | for (i = nbytes - 20, j = 0; i < nbytes; i++, j++) |
65a22376 |
779 | bytes[i] = hash[j]; |
780 | |
781 | in = bignum_from_bytes(bytes, nbytes); |
782 | sfree(bytes); |
783 | |
8671a580 |
784 | out = rsa_privkey_op(in, rsa); |
65a22376 |
785 | freebn(in); |
786 | |
32874aea |
787 | nbytes = (bignum_bitcount(out) + 7) / 8; |
3d88e64d |
788 | bytes = snewn(4 + 7 + 4 + nbytes, unsigned char); |
65a22376 |
789 | PUT_32BIT(bytes, 7); |
32874aea |
790 | memcpy(bytes + 4, "ssh-rsa", 7); |
791 | PUT_32BIT(bytes + 4 + 7, nbytes); |
65a22376 |
792 | for (i = 0; i < nbytes; i++) |
32874aea |
793 | bytes[4 + 7 + 4 + i] = bignum_byte(out, nbytes - 1 - i); |
65a22376 |
794 | freebn(out); |
795 | |
32874aea |
796 | *siglen = 4 + 7 + 4 + nbytes; |
65a22376 |
797 | return bytes; |
85cc02bb |
798 | } |
799 | |
65a22376 |
800 | const struct ssh_signkey ssh_rsa = { |
85cc02bb |
801 | rsa2_newkey, |
802 | rsa2_freekey, |
803 | rsa2_fmtkey, |
65a22376 |
804 | rsa2_public_blob, |
805 | rsa2_private_blob, |
806 | rsa2_createkey, |
45cebe79 |
807 | rsa2_openssh_createkey, |
ddecd643 |
808 | rsa2_openssh_fmtkey, |
47a6b94c |
809 | rsa2_pubkey_bits, |
85cc02bb |
810 | rsa2_fingerprint, |
811 | rsa2_verifysig, |
812 | rsa2_sign, |
813 | "ssh-rsa", |
814 | "rsa2" |
815 | }; |