sshrsa.c 21 KB

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