Add support for the OpenSSH SSH2 agent protocol.
[u/mdw/putty] / 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 #include <windows.h> // FIXME
13 #include "putty.h" // FIXME
14
15 #include "ssh.h"
16
17
18 int makekey(unsigned char *data, struct RSAKey *result,
19 unsigned char **keystr, int order) {
20 unsigned char *p = data;
21 int i;
22
23 if (result) {
24 result->bits = 0;
25 for (i=0; i<4; i++)
26 result->bits = (result->bits << 8) + *p++;
27 } else
28 p += 4;
29
30 /*
31 * order=0 means exponent then modulus (the keys sent by the
32 * server). order=1 means modulus then exponent (the keys
33 * stored in a keyfile).
34 */
35
36 if (order == 0)
37 p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
38 if (result)
39 result->bytes = (((p[0] << 8) + p[1]) + 7) / 8;
40 if (keystr) *keystr = p+2;
41 p += ssh1_read_bignum(p, result ? &result->modulus : NULL);
42 if (order == 1)
43 p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
44
45 return p - data;
46 }
47
48 int makeprivate(unsigned char *data, struct RSAKey *result) {
49 return ssh1_read_bignum(data, &result->private_exponent);
50 }
51
52 void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
53 Bignum b1, b2;
54 int i;
55 unsigned char *p;
56
57 memmove(data+key->bytes-length, data, length);
58 data[0] = 0;
59 data[1] = 2;
60
61 for (i = 2; i < key->bytes-length-1; i++) {
62 do {
63 data[i] = random_byte();
64 } while (data[i] == 0);
65 }
66 data[key->bytes-length-1] = 0;
67
68 b1 = bignum_from_bytes(data, key->bytes);
69
70 b2 = modpow(b1, key->exponent, key->modulus);
71
72 p = data;
73 for (i=key->bytes; i-- ;) {
74 *p++ = bignum_byte(b2, i);
75 }
76
77 freebn(b1);
78 freebn(b2);
79 }
80
81 Bignum rsadecrypt(Bignum input, struct RSAKey *key) {
82 Bignum ret;
83 ret = modpow(input, key->private_exponent, key->modulus);
84 return ret;
85 }
86
87 int rsastr_len(struct RSAKey *key) {
88 Bignum md, ex;
89 int mdlen, exlen;
90
91 md = key->modulus;
92 ex = key->exponent;
93 mdlen = (ssh1_bignum_bitcount(md)+15) / 16;
94 exlen = (ssh1_bignum_bitcount(ex)+15) / 16;
95 return 4 * (mdlen+exlen) + 20;
96 }
97
98 void rsastr_fmt(char *str, struct RSAKey *key) {
99 Bignum md, ex;
100 int len = 0, i, nibbles;
101 static const char hex[] = "0123456789abcdef";
102
103 md = key->modulus;
104 ex = key->exponent;
105
106 len += sprintf(str+len, "0x");
107
108 nibbles = (3 + ssh1_bignum_bitcount(ex))/4; if (nibbles<1) nibbles=1;
109 for (i=nibbles; i-- ;)
110 str[len++] = hex[(bignum_byte(ex, i/2) >> (4*(i%2))) & 0xF];
111
112 len += sprintf(str+len, ",0x");
113
114 nibbles = (3 + ssh1_bignum_bitcount(md))/4; if (nibbles<1) nibbles=1;
115 for (i=nibbles; i-- ;)
116 str[len++] = hex[(bignum_byte(md, i/2) >> (4*(i%2))) & 0xF];
117
118 str[len] = '\0';
119 }
120
121 /*
122 * Generate a fingerprint string for the key. Compatible with the
123 * OpenSSH fingerprint code.
124 */
125 void rsa_fingerprint(char *str, int len, struct RSAKey *key) {
126 struct MD5Context md5c;
127 unsigned char digest[16];
128 char buffer[16*3+40];
129 int numlen, slen, i;
130
131 MD5Init(&md5c);
132 numlen = ssh1_bignum_length(key->modulus) - 2;
133 for (i = numlen; i-- ;) {
134 unsigned char c = bignum_byte(key->modulus, i);
135 MD5Update(&md5c, &c, 1);
136 }
137 numlen = ssh1_bignum_length(key->exponent) - 2;
138 for (i = numlen; i-- ;) {
139 unsigned char c = bignum_byte(key->exponent, i);
140 MD5Update(&md5c, &c, 1);
141 }
142 MD5Final(digest, &md5c);
143
144 sprintf(buffer, "%d ", ssh1_bignum_bitcount(key->modulus));
145 for (i = 0; i < 16; i++)
146 sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]);
147 strncpy(str, buffer, len); str[len-1] = '\0';
148 slen = strlen(str);
149 if (key->comment && slen < len-1) {
150 str[slen] = ' ';
151 strncpy(str+slen+1, key->comment, len-slen-1);
152 str[len-1] = '\0';
153 }
154 }
155
156 void freersakey(struct RSAKey *key) {
157 if (key->modulus) freebn(key->modulus);
158 if (key->exponent) freebn(key->exponent);
159 if (key->private_exponent) freebn(key->private_exponent);
160 if (key->comment) sfree(key->comment);
161 }
162
163 /* ----------------------------------------------------------------------
164 * Implementation of the ssh-rsa signing key type.
165 */
166
167 #define GET_32BIT(cp) \
168 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
169 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
170 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
171 ((unsigned long)(unsigned char)(cp)[3]))
172
173 #define PUT_32BIT(cp, value) { \
174 (cp)[0] = (unsigned char)((value) >> 24); \
175 (cp)[1] = (unsigned char)((value) >> 16); \
176 (cp)[2] = (unsigned char)((value) >> 8); \
177 (cp)[3] = (unsigned char)(value); }
178
179 static void getstring(char **data, int *datalen, char **p, int *length) {
180 *p = NULL;
181 if (*datalen < 4)
182 return;
183 *length = GET_32BIT(*data);
184 *datalen -= 4; *data += 4;
185 if (*datalen < *length)
186 return;
187 *p = *data;
188 *data += *length; *datalen -= *length;
189 }
190 static Bignum getmp(char **data, int *datalen) {
191 char *p;
192 int length;
193 Bignum b;
194
195 getstring(data, datalen, &p, &length);
196 if (!p)
197 return NULL;
198 b = bignum_from_bytes(p, length);
199 return b;
200 }
201
202 static void *rsa2_newkey(char *data, int len) {
203 char *p;
204 int slen;
205 struct RSAKey *rsa;
206
207 rsa = smalloc(sizeof(struct RSAKey));
208 if (!rsa) return NULL;
209 getstring(&data, &len, &p, &slen);
210
211 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
212 sfree(rsa);
213 return NULL;
214 }
215 rsa->exponent = getmp(&data, &len);
216 rsa->modulus = getmp(&data, &len);
217 rsa->private_exponent = NULL;
218 rsa->comment = NULL;
219
220 return rsa;
221 }
222
223 static void rsa2_freekey(void *key) {
224 struct RSAKey *rsa = (struct RSAKey *)key;
225 freersakey(rsa);
226 sfree(rsa);
227 }
228
229 static char *rsa2_fmtkey(void *key) {
230 struct RSAKey *rsa = (struct RSAKey *)key;
231 char *p;
232 int len;
233
234 len = rsastr_len(rsa);
235 p = smalloc(len);
236 rsastr_fmt(p, rsa);
237 return p;
238 }
239
240 static unsigned char *rsa2_public_blob(void *key, int *len) {
241 struct RSAKey *rsa = (struct RSAKey *)key;
242 int elen, mlen, bloblen;
243 int i;
244 unsigned char *blob, *p;
245
246 elen = (ssh1_bignum_bitcount(rsa->exponent)+8)/8;
247 mlen = (ssh1_bignum_bitcount(rsa->modulus)+8)/8;
248
249 /*
250 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
251 * (three length fields, 12+7=19).
252 */
253 bloblen = 19+elen+mlen;
254 blob = smalloc(bloblen);
255 p = blob;
256 PUT_32BIT(p, 7); p += 4;
257 memcpy(p, "ssh-rsa", 7); p += 7;
258 PUT_32BIT(p, elen); p += 4;
259 for (i = elen; i-- ;) *p++ = bignum_byte(rsa->exponent, i);
260 PUT_32BIT(p, mlen); p += 4;
261 for (i = mlen; i-- ;) *p++ = bignum_byte(rsa->modulus, i);
262 assert(p == blob + bloblen);
263 *len = bloblen;
264 return blob;
265 }
266
267 static unsigned char *rsa2_private_blob(void *key, int *len) {
268 struct RSAKey *rsa = (struct RSAKey *)key;
269 int dlen, plen, qlen, ulen, bloblen;
270 int i;
271 unsigned char *blob, *p;
272
273 dlen = (ssh1_bignum_bitcount(rsa->private_exponent)+8)/8;
274 plen = (ssh1_bignum_bitcount(rsa->p)+8)/8;
275 qlen = (ssh1_bignum_bitcount(rsa->q)+8)/8;
276 ulen = (ssh1_bignum_bitcount(rsa->iqmp)+8)/8;
277
278 /*
279 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
280 * sum of lengths.
281 */
282 bloblen = 16+dlen+plen+qlen+ulen;
283 blob = smalloc(bloblen);
284 p = blob;
285 PUT_32BIT(p, dlen); p += 4;
286 for (i = dlen; i-- ;) *p++ = bignum_byte(rsa->private_exponent, i);
287 PUT_32BIT(p, plen); p += 4;
288 for (i = plen; i-- ;) *p++ = bignum_byte(rsa->p, i);
289 PUT_32BIT(p, qlen); p += 4;
290 for (i = qlen; i-- ;) *p++ = bignum_byte(rsa->q, i);
291 PUT_32BIT(p, ulen); p += 4;
292 for (i = ulen; i-- ;) *p++ = bignum_byte(rsa->iqmp, i);
293 assert(p == blob + bloblen);
294 *len = bloblen;
295 return blob;
296 }
297
298 static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
299 unsigned char *priv_blob, int priv_len) {
300 struct RSAKey *rsa;
301 char *pb = (char *)priv_blob;
302
303 rsa = rsa2_newkey((char *)pub_blob, pub_len);
304 rsa->private_exponent = getmp(&pb, &priv_len);
305 rsa->p = getmp(&pb, &priv_len);
306 rsa->q = getmp(&pb, &priv_len);
307 rsa->iqmp = getmp(&pb, &priv_len);
308
309 return rsa;
310 }
311
312 static void *rsa2_openssh_createkey(unsigned char **blob, int *len) {
313 char **b = (char **)blob;
314 struct RSAKey *rsa;
315 char *p;
316 int slen;
317
318 rsa = smalloc(sizeof(struct RSAKey));
319 if (!rsa) return NULL;
320 rsa->comment = NULL;
321
322 rsa->modulus = getmp(b, len);
323 rsa->exponent = getmp(b, len);
324 rsa->private_exponent = getmp(b, len);
325 rsa->iqmp = getmp(b, len);
326 rsa->p = getmp(b, len);
327 rsa->q = getmp(b, len);
328
329 if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
330 !rsa->iqmp || !rsa->p || !rsa->q) {
331 sfree(rsa->modulus);
332 sfree(rsa->exponent);
333 sfree(rsa->private_exponent);
334 sfree(rsa->iqmp);
335 sfree(rsa->p);
336 sfree(rsa->q);
337 sfree(rsa);
338 return NULL;
339 }
340
341 return rsa;
342 }
343
344 static char *rsa2_fingerprint(void *key) {
345 struct RSAKey *rsa = (struct RSAKey *)key;
346 struct MD5Context md5c;
347 unsigned char digest[16], lenbuf[4];
348 char buffer[16*3+40];
349 char *ret;
350 int numlen, i;
351
352 MD5Init(&md5c);
353 MD5Update(&md5c, "\0\0\0\7ssh-rsa", 11);
354
355 #define ADD_BIGNUM(bignum) \
356 numlen = (ssh1_bignum_bitcount(bignum)+8)/8; \
357 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
358 for (i = numlen; i-- ;) { \
359 unsigned char c = bignum_byte(bignum, i); \
360 MD5Update(&md5c, &c, 1); \
361 }
362 ADD_BIGNUM(rsa->exponent);
363 ADD_BIGNUM(rsa->modulus);
364 #undef ADD_BIGNUM
365
366 MD5Final(digest, &md5c);
367
368 sprintf(buffer, "ssh-rsa %d ", ssh1_bignum_bitcount(rsa->modulus));
369 for (i = 0; i < 16; i++)
370 sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]);
371 ret = smalloc(strlen(buffer)+1);
372 if (ret)
373 strcpy(ret, buffer);
374 return ret;
375 }
376
377 /*
378 * This is the magic ASN.1/DER prefix that goes in the decoded
379 * signature, between the string of FFs and the actual SHA hash
380 * value. As closely as I can tell, the meaning of it is:
381 *
382 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
383 *
384 * 30 21 -- a constructed SEQUENCE of length 0x21
385 * 30 09 -- a constructed sub-SEQUENCE of length 9
386 * 06 05 -- an object identifier, length 5
387 * 2B 0E 03 02 1A --
388 * 05 00 -- NULL
389 * 04 14 -- a primitive OCTET STRING of length 0x14
390 * [0x14 bytes of hash data follows]
391 */
392 static unsigned char asn1_weird_stuff[] = {
393 0x00,0x30,0x21,0x30,0x09,0x06,0x05,0x2B,
394 0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14,
395 };
396
397 static int rsa2_verifysig(void *key, char *sig, int siglen,
398 char *data, int datalen) {
399 struct RSAKey *rsa = (struct RSAKey *)key;
400 Bignum in, out;
401 char *p;
402 int slen;
403 int bytes, i, j, ret;
404 unsigned char hash[20];
405
406 getstring(&sig, &siglen, &p, &slen);
407 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
408 return 0;
409 }
410 in = getmp(&sig, &siglen);
411 out = modpow(in, rsa->exponent, rsa->modulus);
412 freebn(in);
413
414 ret = 1;
415
416 bytes = ssh1_bignum_bitcount(rsa->modulus) / 8;
417 /* Top (partial) byte should be zero. */
418 if (bignum_byte(out, bytes-1) != 0)
419 ret = 0;
420 /* First whole byte should be 1. */
421 if (bignum_byte(out, bytes-2) != 1)
422 ret = 0;
423 /* Most of the rest should be FF. */
424 for (i = bytes-3; i >= 20 + sizeof(asn1_weird_stuff); i--) {
425 if (bignum_byte(out, i) != 0xFF)
426 ret = 0;
427 }
428 /* Then we expect to see the asn1_weird_stuff. */
429 for (i = 20 + sizeof(asn1_weird_stuff) - 1, j=0; i >= 20; i--,j++) {
430 if (bignum_byte(out, i) != asn1_weird_stuff[j])
431 ret = 0;
432 }
433 /* Finally, we expect to see the SHA-1 hash of the signed data. */
434 SHA_Simple(data, datalen, hash);
435 for (i = 19, j=0; i >= 0; i--,j++) {
436 if (bignum_byte(out, i) != hash[j])
437 ret = 0;
438 }
439
440 return ret;
441 }
442
443 unsigned char *rsa2_sign(void *key, char *data, int datalen, int *siglen) {
444 struct RSAKey *rsa = (struct RSAKey *)key;
445 unsigned char *bytes;
446 int nbytes;
447 unsigned char hash[20];
448 Bignum in, out;
449 int i, j;
450
451 SHA_Simple(data, datalen, hash);
452
453 nbytes = (ssh1_bignum_bitcount(rsa->modulus)-1) / 8;
454 bytes = smalloc(nbytes);
455
456 bytes[0] = 1;
457 for (i = 1; i < nbytes-20-sizeof(asn1_weird_stuff); i++)
458 bytes[i] = 0xFF;
459 for (i = nbytes-20-sizeof(asn1_weird_stuff), j=0; i < nbytes-20; i++,j++)
460 bytes[i] = asn1_weird_stuff[j];
461 for (i = nbytes-20, j=0; i < nbytes; i++,j++)
462 bytes[i] = hash[j];
463
464 in = bignum_from_bytes(bytes, nbytes);
465 sfree(bytes);
466
467 out = modpow(in, rsa->private_exponent, rsa->modulus);
468 freebn(in);
469
470 nbytes = (ssh1_bignum_bitcount(out)+7)/8;
471 bytes = smalloc(4+7+4+nbytes);
472 PUT_32BIT(bytes, 7);
473 memcpy(bytes+4, "ssh-rsa", 7);
474 PUT_32BIT(bytes+4+7, nbytes);
475 for (i = 0; i < nbytes; i++)
476 bytes[4+7+4+i] = bignum_byte(out, nbytes-1-i);
477 freebn(out);
478
479 *siglen = 4+7+4+nbytes;
480 return bytes;
481 }
482
483 const struct ssh_signkey ssh_rsa = {
484 rsa2_newkey,
485 rsa2_freekey,
486 rsa2_fmtkey,
487 rsa2_public_blob,
488 rsa2_private_blob,
489 rsa2_createkey,
490 rsa2_openssh_createkey,
491 rsa2_fingerprint,
492 rsa2_verifysig,
493 rsa2_sign,
494 "ssh-rsa",
495 "rsa2"
496 };