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