| 1 | /* |
| 2 | * RSA implementation for PuTTY. |
| 3 | */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <assert.h> |
| 9 | |
| 10 | #include "ssh.h" |
| 11 | #include "misc.h" |
| 12 | |
| 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); } |
| 24 | |
| 25 | int makekey(unsigned char *data, struct RSAKey *result, |
| 26 | unsigned char **keystr, int order) |
| 27 | { |
| 28 | unsigned char *p = data; |
| 29 | int i; |
| 30 | |
| 31 | if (result) { |
| 32 | result->bits = 0; |
| 33 | for (i = 0; i < 4; i++) |
| 34 | result->bits = (result->bits << 8) + *p++; |
| 35 | } else |
| 36 | p += 4; |
| 37 | |
| 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 | */ |
| 43 | |
| 44 | if (order == 0) |
| 45 | p += ssh1_read_bignum(p, result ? &result->exponent : NULL); |
| 46 | if (result) |
| 47 | result->bytes = (((p[0] << 8) + p[1]) + 7) / 8; |
| 48 | if (keystr) |
| 49 | *keystr = p + 2; |
| 50 | p += ssh1_read_bignum(p, result ? &result->modulus : NULL); |
| 51 | if (order == 1) |
| 52 | p += ssh1_read_bignum(p, result ? &result->exponent : NULL); |
| 53 | |
| 54 | return p - data; |
| 55 | } |
| 56 | |
| 57 | int makeprivate(unsigned char *data, struct RSAKey *result) |
| 58 | { |
| 59 | return ssh1_read_bignum(data, &result->private_exponent); |
| 60 | } |
| 61 | |
| 62 | void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) |
| 63 | { |
| 64 | Bignum b1, b2; |
| 65 | int i; |
| 66 | unsigned char *p; |
| 67 | |
| 68 | memmove(data + key->bytes - length, data, length); |
| 69 | data[0] = 0; |
| 70 | data[1] = 2; |
| 71 | |
| 72 | for (i = 2; i < key->bytes - length - 1; i++) { |
| 73 | do { |
| 74 | data[i] = random_byte(); |
| 75 | } while (data[i] == 0); |
| 76 | } |
| 77 | data[key->bytes - length - 1] = 0; |
| 78 | |
| 79 | b1 = bignum_from_bytes(data, key->bytes); |
| 80 | |
| 81 | b2 = modpow(b1, key->exponent, key->modulus); |
| 82 | |
| 83 | p = data; |
| 84 | for (i = key->bytes; i--;) { |
| 85 | *p++ = bignum_byte(b2, i); |
| 86 | } |
| 87 | |
| 88 | freebn(b1); |
| 89 | freebn(b2); |
| 90 | } |
| 91 | |
| 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 | |
| 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) |
| 112 | { |
| 113 | Bignum random, random_encrypted, random_inverse; |
| 114 | Bignum input_blinded, ret_blinded; |
| 115 | Bignum ret; |
| 116 | |
| 117 | SHA512_State ss; |
| 118 | unsigned char digest512[64]; |
| 119 | int digestused = lenof(digest512); |
| 120 | int hashseq = 0; |
| 121 | |
| 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.) |
| 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. |
| 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--) { |
| 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 | } |
| 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 |
| 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. |
| 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 |
| 208 | * _y^d_, and use the _public_ exponent to compute (y^d)^e = y |
| 209 | * from it, which is much faster to do. |
| 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 | |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | Bignum rsadecrypt(Bignum input, struct RSAKey *key) |
| 227 | { |
| 228 | return rsa_privkey_op(input, key); |
| 229 | } |
| 230 | |
| 231 | int rsastr_len(struct RSAKey *key) |
| 232 | { |
| 233 | Bignum md, ex; |
| 234 | int mdlen, exlen; |
| 235 | |
| 236 | md = key->modulus; |
| 237 | ex = key->exponent; |
| 238 | mdlen = (bignum_bitcount(md) + 15) / 16; |
| 239 | exlen = (bignum_bitcount(ex) + 15) / 16; |
| 240 | return 4 * (mdlen + exlen) + 20; |
| 241 | } |
| 242 | |
| 243 | void rsastr_fmt(char *str, struct RSAKey *key) |
| 244 | { |
| 245 | Bignum md, ex; |
| 246 | int len = 0, i, nibbles; |
| 247 | static const char hex[] = "0123456789abcdef"; |
| 248 | |
| 249 | md = key->modulus; |
| 250 | ex = key->exponent; |
| 251 | |
| 252 | len += sprintf(str + len, "0x"); |
| 253 | |
| 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]; |
| 259 | |
| 260 | len += sprintf(str + len, ",0x"); |
| 261 | |
| 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]; |
| 267 | |
| 268 | str[len] = '\0'; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Generate a fingerprint string for the key. Compatible with the |
| 273 | * OpenSSH fingerprint code. |
| 274 | */ |
| 275 | void rsa_fingerprint(char *str, int len, struct RSAKey *key) |
| 276 | { |
| 277 | struct MD5Context md5c; |
| 278 | unsigned char digest[16]; |
| 279 | char buffer[16 * 3 + 40]; |
| 280 | int numlen, slen, i; |
| 281 | |
| 282 | MD5Init(&md5c); |
| 283 | numlen = ssh1_bignum_length(key->modulus) - 2; |
| 284 | for (i = numlen; i--;) { |
| 285 | unsigned char c = bignum_byte(key->modulus, i); |
| 286 | MD5Update(&md5c, &c, 1); |
| 287 | } |
| 288 | numlen = ssh1_bignum_length(key->exponent) - 2; |
| 289 | for (i = numlen; i--;) { |
| 290 | unsigned char c = bignum_byte(key->exponent, i); |
| 291 | MD5Update(&md5c, &c, 1); |
| 292 | } |
| 293 | MD5Final(digest, &md5c); |
| 294 | |
| 295 | sprintf(buffer, "%d ", bignum_bitcount(key->modulus)); |
| 296 | for (i = 0; i < 16; i++) |
| 297 | sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "", |
| 298 | digest[i]); |
| 299 | strncpy(str, buffer, len); |
| 300 | str[len - 1] = '\0'; |
| 301 | slen = strlen(str); |
| 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'; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Verify that the public data in an RSA key matches the private |
| 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. |
| 313 | */ |
| 314 | int rsa_verify(struct RSAKey *key) |
| 315 | { |
| 316 | Bignum n, ed, pm1, qm1; |
| 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 | |
| 326 | /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */ |
| 327 | pm1 = copybn(key->p); |
| 328 | decbn(pm1); |
| 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 | |
| 335 | qm1 = copybn(key->q); |
| 336 | decbn(qm1); |
| 337 | ed = modmul(key->exponent, key->private_exponent, qm1); |
| 338 | cmp = bignum_cmp(ed, One); |
| 339 | sfree(ed); |
| 340 | if (cmp != 0) |
| 341 | return 0; |
| 342 | |
| 343 | /* |
| 344 | * Ensure p > q. |
| 345 | */ |
| 346 | if (bignum_cmp(key->p, key->q) <= 0) |
| 347 | return 0; |
| 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) |
| 356 | return 0; |
| 357 | |
| 358 | return 1; |
| 359 | } |
| 360 | |
| 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); |
| 369 | ret = snewn(length, unsigned char); |
| 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; |
| 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 | |
| 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); |
| 402 | } |
| 403 | |
| 404 | /* ---------------------------------------------------------------------- |
| 405 | * Implementation of the ssh-rsa signing key type. |
| 406 | */ |
| 407 | |
| 408 | static void getstring(char **data, int *datalen, char **p, int *length) |
| 409 | { |
| 410 | *p = NULL; |
| 411 | if (*datalen < 4) |
| 412 | return; |
| 413 | *length = GET_32BIT(*data); |
| 414 | *datalen -= 4; |
| 415 | *data += 4; |
| 416 | if (*datalen < *length) |
| 417 | return; |
| 418 | *p = *data; |
| 419 | *data += *length; |
| 420 | *datalen -= *length; |
| 421 | } |
| 422 | static Bignum getmp(char **data, int *datalen) |
| 423 | { |
| 424 | char *p; |
| 425 | int length; |
| 426 | Bignum b; |
| 427 | |
| 428 | getstring(data, datalen, &p, &length); |
| 429 | if (!p) |
| 430 | return NULL; |
| 431 | b = bignum_from_bytes((unsigned char *)p, length); |
| 432 | return b; |
| 433 | } |
| 434 | |
| 435 | static void *rsa2_newkey(char *data, int len) |
| 436 | { |
| 437 | char *p; |
| 438 | int slen; |
| 439 | struct RSAKey *rsa; |
| 440 | |
| 441 | rsa = snew(struct RSAKey); |
| 442 | if (!rsa) |
| 443 | return NULL; |
| 444 | getstring(&data, &len, &p, &slen); |
| 445 | |
| 446 | if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) { |
| 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 | |
| 458 | static void rsa2_freekey(void *key) |
| 459 | { |
| 460 | struct RSAKey *rsa = (struct RSAKey *) key; |
| 461 | freersakey(rsa); |
| 462 | sfree(rsa); |
| 463 | } |
| 464 | |
| 465 | static char *rsa2_fmtkey(void *key) |
| 466 | { |
| 467 | struct RSAKey *rsa = (struct RSAKey *) key; |
| 468 | char *p; |
| 469 | int len; |
| 470 | |
| 471 | len = rsastr_len(rsa); |
| 472 | p = snewn(len, char); |
| 473 | rsastr_fmt(p, rsa); |
| 474 | return p; |
| 475 | } |
| 476 | |
| 477 | static unsigned char *rsa2_public_blob(void *key, int *len) |
| 478 | { |
| 479 | struct RSAKey *rsa = (struct RSAKey *) key; |
| 480 | int elen, mlen, bloblen; |
| 481 | int i; |
| 482 | unsigned char *blob, *p; |
| 483 | |
| 484 | elen = (bignum_bitcount(rsa->exponent) + 8) / 8; |
| 485 | mlen = (bignum_bitcount(rsa->modulus) + 8) / 8; |
| 486 | |
| 487 | /* |
| 488 | * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen. |
| 489 | * (three length fields, 12+7=19). |
| 490 | */ |
| 491 | bloblen = 19 + elen + mlen; |
| 492 | blob = snewn(bloblen, unsigned char); |
| 493 | p = blob; |
| 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); |
| 506 | assert(p == blob + bloblen); |
| 507 | *len = bloblen; |
| 508 | return blob; |
| 509 | } |
| 510 | |
| 511 | static unsigned char *rsa2_private_blob(void *key, int *len) |
| 512 | { |
| 513 | struct RSAKey *rsa = (struct RSAKey *) key; |
| 514 | int dlen, plen, qlen, ulen, bloblen; |
| 515 | int i; |
| 516 | unsigned char *blob, *p; |
| 517 | |
| 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; |
| 522 | |
| 523 | /* |
| 524 | * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 + |
| 525 | * sum of lengths. |
| 526 | */ |
| 527 | bloblen = 16 + dlen + plen + qlen + ulen; |
| 528 | blob = snewn(bloblen, unsigned char); |
| 529 | p = blob; |
| 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); |
| 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, |
| 552 | unsigned char *priv_blob, int priv_len) |
| 553 | { |
| 554 | struct RSAKey *rsa; |
| 555 | char *pb = (char *) priv_blob; |
| 556 | |
| 557 | rsa = rsa2_newkey((char *) pub_blob, pub_len); |
| 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 | |
| 563 | if (!rsa_verify(rsa)) { |
| 564 | rsa2_freekey(rsa); |
| 565 | return NULL; |
| 566 | } |
| 567 | |
| 568 | return rsa; |
| 569 | } |
| 570 | |
| 571 | static void *rsa2_openssh_createkey(unsigned char **blob, int *len) |
| 572 | { |
| 573 | char **b = (char **) blob; |
| 574 | struct RSAKey *rsa; |
| 575 | |
| 576 | rsa = snew(struct RSAKey); |
| 577 | if (!rsa) |
| 578 | return NULL; |
| 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 | |
| 603 | static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len) |
| 604 | { |
| 605 | struct RSAKey *rsa = (struct RSAKey *) key; |
| 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) + |
| 613 | ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q); |
| 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 | |
| 632 | static char *rsa2_fingerprint(void *key) |
| 633 | { |
| 634 | struct RSAKey *rsa = (struct RSAKey *) key; |
| 635 | struct MD5Context md5c; |
| 636 | unsigned char digest[16], lenbuf[4]; |
| 637 | char buffer[16 * 3 + 40]; |
| 638 | char *ret; |
| 639 | int numlen, i; |
| 640 | |
| 641 | MD5Init(&md5c); |
| 642 | MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-rsa", 11); |
| 643 | |
| 644 | #define ADD_BIGNUM(bignum) \ |
| 645 | numlen = (bignum_bitcount(bignum)+8)/8; \ |
| 646 | PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \ |
| 647 | for (i = numlen; i-- ;) { \ |
| 648 | unsigned char c = bignum_byte(bignum, i); \ |
| 649 | MD5Update(&md5c, &c, 1); \ |
| 650 | } |
| 651 | ADD_BIGNUM(rsa->exponent); |
| 652 | ADD_BIGNUM(rsa->modulus); |
| 653 | #undef ADD_BIGNUM |
| 654 | |
| 655 | MD5Final(digest, &md5c); |
| 656 | |
| 657 | sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus)); |
| 658 | for (i = 0; i < 16; i++) |
| 659 | sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "", |
| 660 | digest[i]); |
| 661 | ret = snewn(strlen(buffer) + 1, char); |
| 662 | if (ret) |
| 663 | strcpy(ret, buffer); |
| 664 | return ret; |
| 665 | } |
| 666 | |
| 667 | /* |
| 668 | * This is the magic ASN.1/DER prefix that goes in the decoded |
| 669 | * signature, between the string of FFs and the actual SHA hash |
| 670 | * value. The meaning of it is: |
| 671 | * |
| 672 | * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself |
| 673 | * |
| 674 | * 30 21 -- a constructed SEQUENCE of length 0x21 |
| 675 | * 30 09 -- a constructed sub-SEQUENCE of length 9 |
| 676 | * 06 05 -- an object identifier, length 5 |
| 677 | * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 } |
| 678 | * (the 1,3 comes from 0x2B = 43 = 40*1+3) |
| 679 | * 05 00 -- NULL |
| 680 | * 04 14 -- a primitive OCTET STRING of length 0x14 |
| 681 | * [0x14 bytes of hash data follows] |
| 682 | * |
| 683 | * The object id in the middle there is listed as `id-sha1' in |
| 684 | * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the |
| 685 | * ASN module for PKCS #1) and its expanded form is as follows: |
| 686 | * |
| 687 | * id-sha1 OBJECT IDENTIFIER ::= { |
| 688 | * iso(1) identified-organization(3) oiw(14) secsig(3) |
| 689 | * algorithms(2) 26 } |
| 690 | */ |
| 691 | static const unsigned char asn1_weird_stuff[] = { |
| 692 | 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, |
| 693 | 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14, |
| 694 | }; |
| 695 | |
| 696 | #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) ) |
| 697 | |
| 698 | static int rsa2_verifysig(void *key, char *sig, int siglen, |
| 699 | char *data, int datalen) |
| 700 | { |
| 701 | struct RSAKey *rsa = (struct RSAKey *) key; |
| 702 | Bignum in, out; |
| 703 | char *p; |
| 704 | int slen; |
| 705 | int bytes, i, j, ret; |
| 706 | unsigned char hash[20]; |
| 707 | |
| 708 | getstring(&sig, &siglen, &p, &slen); |
| 709 | if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) { |
| 710 | return 0; |
| 711 | } |
| 712 | in = getmp(&sig, &siglen); |
| 713 | out = modpow(in, rsa->exponent, rsa->modulus); |
| 714 | freebn(in); |
| 715 | |
| 716 | ret = 1; |
| 717 | |
| 718 | bytes = bignum_bitcount(rsa->modulus) / 8; |
| 719 | /* Top (partial) byte should be zero. */ |
| 720 | if (bignum_byte(out, bytes - 1) != 0) |
| 721 | ret = 0; |
| 722 | /* First whole byte should be 1. */ |
| 723 | if (bignum_byte(out, bytes - 2) != 1) |
| 724 | ret = 0; |
| 725 | /* Most of the rest should be FF. */ |
| 726 | for (i = bytes - 3; i >= 20 + ASN1_LEN; i--) { |
| 727 | if (bignum_byte(out, i) != 0xFF) |
| 728 | ret = 0; |
| 729 | } |
| 730 | /* Then we expect to see the asn1_weird_stuff. */ |
| 731 | for (i = 20 + ASN1_LEN - 1, j = 0; i >= 20; i--, j++) { |
| 732 | if (bignum_byte(out, i) != asn1_weird_stuff[j]) |
| 733 | ret = 0; |
| 734 | } |
| 735 | /* Finally, we expect to see the SHA-1 hash of the signed data. */ |
| 736 | SHA_Simple(data, datalen, hash); |
| 737 | for (i = 19, j = 0; i >= 0; i--, j++) { |
| 738 | if (bignum_byte(out, i) != hash[j]) |
| 739 | ret = 0; |
| 740 | } |
| 741 | |
| 742 | return ret; |
| 743 | } |
| 744 | |
| 745 | static unsigned char *rsa2_sign(void *key, char *data, int datalen, |
| 746 | int *siglen) |
| 747 | { |
| 748 | struct RSAKey *rsa = (struct RSAKey *) key; |
| 749 | unsigned char *bytes; |
| 750 | int nbytes; |
| 751 | unsigned char hash[20]; |
| 752 | Bignum in, out; |
| 753 | int i, j; |
| 754 | |
| 755 | SHA_Simple(data, datalen, hash); |
| 756 | |
| 757 | nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8; |
| 758 | bytes = snewn(nbytes, unsigned char); |
| 759 | |
| 760 | bytes[0] = 1; |
| 761 | for (i = 1; i < nbytes - 20 - ASN1_LEN; i++) |
| 762 | bytes[i] = 0xFF; |
| 763 | for (i = nbytes - 20 - ASN1_LEN, j = 0; i < nbytes - 20; i++, j++) |
| 764 | bytes[i] = asn1_weird_stuff[j]; |
| 765 | for (i = nbytes - 20, j = 0; i < nbytes; i++, j++) |
| 766 | bytes[i] = hash[j]; |
| 767 | |
| 768 | in = bignum_from_bytes(bytes, nbytes); |
| 769 | sfree(bytes); |
| 770 | |
| 771 | out = rsa_privkey_op(in, rsa); |
| 772 | freebn(in); |
| 773 | |
| 774 | nbytes = (bignum_bitcount(out) + 7) / 8; |
| 775 | bytes = snewn(4 + 7 + 4 + nbytes, unsigned char); |
| 776 | PUT_32BIT(bytes, 7); |
| 777 | memcpy(bytes + 4, "ssh-rsa", 7); |
| 778 | PUT_32BIT(bytes + 4 + 7, nbytes); |
| 779 | for (i = 0; i < nbytes; i++) |
| 780 | bytes[4 + 7 + 4 + i] = bignum_byte(out, nbytes - 1 - i); |
| 781 | freebn(out); |
| 782 | |
| 783 | *siglen = 4 + 7 + 4 + nbytes; |
| 784 | return bytes; |
| 785 | } |
| 786 | |
| 787 | const struct ssh_signkey ssh_rsa = { |
| 788 | rsa2_newkey, |
| 789 | rsa2_freekey, |
| 790 | rsa2_fmtkey, |
| 791 | rsa2_public_blob, |
| 792 | rsa2_private_blob, |
| 793 | rsa2_createkey, |
| 794 | rsa2_openssh_createkey, |
| 795 | rsa2_openssh_fmtkey, |
| 796 | rsa2_fingerprint, |
| 797 | rsa2_verifysig, |
| 798 | rsa2_sign, |
| 799 | "ssh-rsa", |
| 800 | "rsa2" |
| 801 | }; |