rsa.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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 "mpint.h"
  10. #include "misc.h"
  11. void BinarySource_get_rsa_ssh1_pub(
  12. BinarySource *src, RSAKey *rsa, RsaSsh1Order order)
  13. {
  14. unsigned bits;
  15. mp_int *e, *m;
  16. bits = get_uint32(src);
  17. if (order == RSA_SSH1_EXPONENT_FIRST) {
  18. e = get_mp_ssh1(src);
  19. m = get_mp_ssh1(src);
  20. } else {
  21. m = get_mp_ssh1(src);
  22. e = get_mp_ssh1(src);
  23. }
  24. if (rsa) {
  25. rsa->bits = bits;
  26. rsa->exponent = e;
  27. rsa->modulus = m;
  28. rsa->bytes = (mp_get_nbits(m) + 7) / 8;
  29. } else {
  30. mp_free(e);
  31. mp_free(m);
  32. }
  33. }
  34. void BinarySource_get_rsa_ssh1_priv(
  35. BinarySource *src, RSAKey *rsa)
  36. {
  37. rsa->private_exponent = get_mp_ssh1(src);
  38. }
  39. key_components *rsa_components(RSAKey *rsa)
  40. {
  41. key_components *kc = key_components_new();
  42. key_components_add_text(kc, "key_type", "RSA");
  43. key_components_add_mp(kc, "public_modulus", rsa->modulus);
  44. key_components_add_mp(kc, "public_exponent", rsa->exponent);
  45. if (rsa->private_exponent) {
  46. key_components_add_mp(kc, "private_exponent", rsa->private_exponent);
  47. key_components_add_mp(kc, "private_p", rsa->p);
  48. key_components_add_mp(kc, "private_q", rsa->q);
  49. key_components_add_mp(kc, "private_inverse_q_mod_p", rsa->iqmp);
  50. }
  51. return kc;
  52. }
  53. RSAKey *BinarySource_get_rsa_ssh1_priv_agent(BinarySource *src)
  54. {
  55. RSAKey *rsa = snew(RSAKey);
  56. memset(rsa, 0, sizeof(RSAKey));
  57. get_rsa_ssh1_pub(src, rsa, RSA_SSH1_MODULUS_FIRST);
  58. get_rsa_ssh1_priv(src, rsa);
  59. /* SSH-1 names p and q the other way round, i.e. we have the
  60. * inverse of p mod q and not of q mod p. We swap the names,
  61. * because our internal RSA wants iqmp. */
  62. rsa->iqmp = get_mp_ssh1(src);
  63. rsa->q = get_mp_ssh1(src);
  64. rsa->p = get_mp_ssh1(src);
  65. return rsa;
  66. }
  67. void duprsakey(RSAKey *dst, const RSAKey *src)
  68. {
  69. dst->bits = src->bits;
  70. dst->bytes = src->bytes;
  71. dst->modulus = mp_copy(src->modulus);
  72. dst->exponent = mp_copy(src->exponent);
  73. dst->private_exponent = src->private_exponent ?
  74. mp_copy(src->private_exponent) : NULL;
  75. dst->p = mp_copy(src->p);
  76. dst->q = mp_copy(src->q);
  77. dst->iqmp = mp_copy(src->iqmp);
  78. dst->comment = src->comment ? dupstr(src->comment) : NULL;
  79. dst->sshk.vt = src->sshk.vt;
  80. }
  81. bool rsa_ssh1_encrypt(unsigned char *data, int length, RSAKey *key)
  82. {
  83. mp_int *b1, *b2;
  84. int i;
  85. unsigned char *p;
  86. if (key->bytes < length + 4)
  87. return false; /* RSA key too short! */
  88. memmove(data + key->bytes - length, data, length);
  89. data[0] = 0;
  90. data[1] = 2;
  91. { // WINSCP
  92. size_t npad = key->bytes - length - 3;
  93. /*
  94. * Generate a sequence of nonzero padding bytes. We do this in a
  95. * reasonably uniform way and without having to loop round
  96. * retrying the random number generation, by first generating an
  97. * integer in [0,2^n) for an appropriately large n; then we
  98. * repeatedly multiply by 255 to give an integer in [0,255*2^n),
  99. * extract the top 8 bits to give an integer in [0,255), and mask
  100. * those bits off before multiplying up again for the next digit.
  101. * This gives us a sequence of numbers in [0,255), and of course
  102. * adding 1 to each of them gives numbers in [1,256) as we wanted.
  103. *
  104. * (You could imagine this being a sort of fixed-point operation:
  105. * given a uniformly random binary _fraction_, multiplying it by k
  106. * and subtracting off the integer part will yield you a sequence
  107. * of integers each in [0,k). I'm just doing that scaled up by a
  108. * power of 2 to avoid the fractions.)
  109. */
  110. size_t random_bits = (npad + 16) * 8;
  111. mp_int *randval = mp_new(random_bits + 8);
  112. mp_int *tmp = mp_random_bits(random_bits);
  113. mp_copy_into(randval, tmp);
  114. mp_free(tmp);
  115. for (i = 2; i < key->bytes - length - 1; i++) {
  116. mp_mul_integer_into(randval, randval, 255);
  117. { // WINSCP
  118. uint8_t byte = mp_get_byte(randval, random_bits / 8);
  119. assert(byte != 255);
  120. data[i] = byte + 1;
  121. mp_reduce_mod_2to(randval, random_bits);
  122. } // WINSCP
  123. }
  124. mp_free(randval);
  125. data[key->bytes - length - 1] = 0;
  126. b1 = mp_from_bytes_be(make_ptrlen(data, key->bytes));
  127. b2 = mp_modpow(b1, key->exponent, key->modulus);
  128. p = data;
  129. for (i = key->bytes; i--;) {
  130. *p++ = mp_get_byte(b2, i);
  131. }
  132. mp_free(b1);
  133. mp_free(b2);
  134. return true;
  135. } // WINSCP
  136. }
  137. /*
  138. * Compute (base ^ exp) % mod, provided mod == p * q, with p,q
  139. * distinct primes, and iqmp is the multiplicative inverse of q mod p.
  140. * Uses Chinese Remainder Theorem to speed computation up over the
  141. * obvious implementation of a single big modpow.
  142. */
  143. static mp_int *crt_modpow(mp_int *base, mp_int *exp, mp_int *mod,
  144. mp_int *p, mp_int *q, mp_int *iqmp)
  145. {
  146. mp_int *pm1, *qm1, *pexp, *qexp, *presult, *qresult;
  147. mp_int *diff, *multiplier, *ret0, *ret;
  148. /*
  149. * Reduce the exponent mod phi(p) and phi(q), to save time when
  150. * exponentiating mod p and mod q respectively. Of course, since p
  151. * and q are prime, phi(p) == p-1 and similarly for q.
  152. */
  153. pm1 = mp_copy(p);
  154. mp_sub_integer_into(pm1, pm1, 1);
  155. qm1 = mp_copy(q);
  156. mp_sub_integer_into(qm1, qm1, 1);
  157. pexp = mp_mod(exp, pm1);
  158. qexp = mp_mod(exp, qm1);
  159. /*
  160. * Do the two modpows.
  161. */
  162. { // WINSCP
  163. mp_int *base_mod_p = mp_mod(base, p);
  164. presult = mp_modpow(base_mod_p, pexp, p);
  165. mp_free(base_mod_p);
  166. } // WINSCP
  167. { // WINSCP
  168. mp_int *base_mod_q = mp_mod(base, q);
  169. qresult = mp_modpow(base_mod_q, qexp, q);
  170. mp_free(base_mod_q);
  171. } // WINSCP
  172. /*
  173. * Recombine the results. We want a value which is congruent to
  174. * qresult mod q, and to presult mod p.
  175. *
  176. * We know that iqmp * q is congruent to 1 * mod p (by definition
  177. * of iqmp) and to 0 mod q (obviously). So we start with qresult
  178. * (which is congruent to qresult mod both primes), and add on
  179. * (presult-qresult) * (iqmp * q) which adjusts it to be congruent
  180. * to presult mod p without affecting its value mod q.
  181. *
  182. * (If presult-qresult < 0, we add p to it to keep it positive.)
  183. */
  184. { // WINSCP
  185. unsigned presult_too_small = mp_cmp_hs(qresult, presult);
  186. mp_cond_add_into(presult, presult, p, presult_too_small);
  187. } // WINSCP
  188. diff = mp_sub(presult, qresult);
  189. multiplier = mp_mul(iqmp, q);
  190. ret0 = mp_mul(multiplier, diff);
  191. mp_add_into(ret0, ret0, qresult);
  192. /*
  193. * Finally, reduce the result mod n.
  194. */
  195. ret = mp_mod(ret0, mod);
  196. /*
  197. * Free all the intermediate results before returning.
  198. */
  199. mp_free(pm1);
  200. mp_free(qm1);
  201. mp_free(pexp);
  202. mp_free(qexp);
  203. mp_free(presult);
  204. mp_free(qresult);
  205. mp_free(diff);
  206. mp_free(multiplier);
  207. mp_free(ret0);
  208. return ret;
  209. }
  210. /*
  211. * Wrapper on crt_modpow that looks up all the right values from an
  212. * RSAKey.
  213. */
  214. static mp_int *rsa_privkey_op(mp_int *input, RSAKey *key)
  215. {
  216. return crt_modpow(input, key->private_exponent,
  217. key->modulus, key->p, key->q, key->iqmp);
  218. }
  219. mp_int *rsa_ssh1_decrypt(mp_int *input, RSAKey *key)
  220. {
  221. return rsa_privkey_op(input, key);
  222. }
  223. bool rsa_ssh1_decrypt_pkcs1(mp_int *input, RSAKey *key,
  224. strbuf *outbuf)
  225. {
  226. strbuf *data = strbuf_new_nm();
  227. bool success = false;
  228. BinarySource src[1];
  229. {
  230. mp_int *b = rsa_ssh1_decrypt(input, key);
  231. size_t i; // WINSCP
  232. for (i = (mp_get_nbits(key->modulus) + 7) / 8; i-- > 0 ;) {
  233. put_byte(data, mp_get_byte(b, i));
  234. }
  235. mp_free(b);
  236. }
  237. BinarySource_BARE_INIT(src, data->u, data->len);
  238. /* Check PKCS#1 formatting prefix */
  239. if (get_byte(src) != 0) goto out;
  240. if (get_byte(src) != 2) goto out;
  241. while (1) {
  242. unsigned char byte = get_byte(src);
  243. if (get_err(src)) goto out;
  244. if (byte == 0)
  245. break;
  246. }
  247. /* Everything else is the payload */
  248. success = true;
  249. put_data(outbuf, get_ptr(src), get_avail(src));
  250. out:
  251. strbuf_free(data);
  252. return success;
  253. }
  254. static void append_hex_to_strbuf(strbuf *sb, mp_int *x)
  255. {
  256. if (sb->len > 0)
  257. put_byte(sb, ',');
  258. put_data(sb, "0x", 2);
  259. { // WINSCP
  260. char *hex = mp_get_hex(x);
  261. size_t hexlen = strlen(hex);
  262. put_data(sb, hex, hexlen);
  263. smemclr(hex, hexlen);
  264. sfree(hex);
  265. } // WINSCP
  266. }
  267. char *rsastr_fmt(RSAKey *key)
  268. {
  269. strbuf *sb = strbuf_new();
  270. append_hex_to_strbuf(sb, key->exponent);
  271. append_hex_to_strbuf(sb, key->modulus);
  272. return strbuf_to_str(sb);
  273. }
  274. /*
  275. * Generate a fingerprint string for the key. Compatible with the
  276. * OpenSSH fingerprint code.
  277. */
  278. char *rsa_ssh1_fingerprint(RSAKey *key)
  279. {
  280. unsigned char digest[16];
  281. strbuf *out;
  282. int i;
  283. /*
  284. * The hash preimage for SSH-1 key fingerprinting consists of the
  285. * modulus and exponent _without_ any preceding length field -
  286. * just the minimum number of bytes to represent each integer,
  287. * stored big-endian, concatenated with no marker at the division
  288. * between them.
  289. */
  290. ssh_hash *hash = ssh_hash_new(&ssh_md5);
  291. { // WINSCP
  292. size_t i; // WINSCP
  293. for (i = (mp_get_nbits(key->modulus) + 7) / 8; i-- > 0 ;)
  294. put_byte(hash, mp_get_byte(key->modulus, i));
  295. for (i = (mp_get_nbits(key->exponent) + 7) / 8; i-- > 0 ;)
  296. put_byte(hash, mp_get_byte(key->exponent, i));
  297. } // WINSCP
  298. ssh_hash_final(hash, digest);
  299. out = strbuf_new();
  300. put_fmt(out, "%"SIZEu" ", mp_get_nbits(key->modulus));
  301. for (i = 0; i < 16; i++)
  302. put_fmt(out, "%s%02x", i ? ":" : "", digest[i]);
  303. if (key->comment)
  304. put_fmt(out, " %s", key->comment);
  305. return strbuf_to_str(out);
  306. }
  307. /*
  308. * Wrap the output of rsa_ssh1_fingerprint up into the same kind of
  309. * structure that comes from ssh2_all_fingerprints.
  310. */
  311. char **rsa_ssh1_fake_all_fingerprints(RSAKey *key)
  312. {
  313. char **fingerprints = snewn(SSH_N_FPTYPES, char *);
  314. unsigned i; // WINSCP
  315. for (i = 0; i < SSH_N_FPTYPES; i++)
  316. fingerprints[i] = NULL;
  317. fingerprints[SSH_FPTYPE_MD5] = rsa_ssh1_fingerprint(key);
  318. return fingerprints;
  319. }
  320. /*
  321. * Verify that the public data in an RSA key matches the private
  322. * data. We also check the private data itself: we ensure that p >
  323. * q and that iqmp really is the inverse of q mod p.
  324. */
  325. bool rsa_verify(RSAKey *key)
  326. {
  327. mp_int *n, *ed, *pm1, *qm1;
  328. unsigned ok = 1;
  329. /* Preliminary checks: p,q can't be 0 or 1. (Of course no other
  330. * very small value is any good either, but these are the values
  331. * we _must_ check for to avoid assertion failures further down
  332. * this function.) */
  333. if (!(mp_hs_integer(key->p, 2) & mp_hs_integer(key->q, 2)))
  334. return false;
  335. /* n must equal pq. */
  336. n = mp_mul(key->p, key->q);
  337. ok &= mp_cmp_eq(n, key->modulus);
  338. mp_free(n);
  339. /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
  340. pm1 = mp_copy(key->p);
  341. mp_sub_integer_into(pm1, pm1, 1);
  342. ed = mp_modmul(key->exponent, key->private_exponent, pm1);
  343. mp_free(pm1);
  344. ok &= mp_eq_integer(ed, 1);
  345. mp_free(ed);
  346. qm1 = mp_copy(key->q);
  347. mp_sub_integer_into(qm1, qm1, 1);
  348. ed = mp_modmul(key->exponent, key->private_exponent, qm1);
  349. mp_free(qm1);
  350. ok &= mp_eq_integer(ed, 1);
  351. mp_free(ed);
  352. /*
  353. * Ensure p > q.
  354. *
  355. * I have seen key blobs in the wild which were generated with
  356. * p < q, so instead of rejecting the key in this case we
  357. * should instead flip them round into the canonical order of
  358. * p > q. This also involves regenerating iqmp.
  359. */
  360. { // WINSCP
  361. mp_int *p_new = mp_max(key->p, key->q);
  362. mp_int *q_new = mp_min(key->p, key->q);
  363. mp_free(key->p);
  364. mp_free(key->q);
  365. mp_free(key->iqmp);
  366. key->p = p_new;
  367. key->q = q_new;
  368. key->iqmp = mp_invert(key->q, key->p);
  369. return ok;
  370. } // WINSCP
  371. }
  372. void rsa_ssh1_public_blob(BinarySink *bs, RSAKey *key,
  373. RsaSsh1Order order)
  374. {
  375. put_uint32(bs, mp_get_nbits(key->modulus));
  376. if (order == RSA_SSH1_EXPONENT_FIRST) {
  377. put_mp_ssh1(bs, key->exponent);
  378. put_mp_ssh1(bs, key->modulus);
  379. } else {
  380. put_mp_ssh1(bs, key->modulus);
  381. put_mp_ssh1(bs, key->exponent);
  382. }
  383. }
  384. void rsa_ssh1_private_blob_agent(BinarySink *bs, RSAKey *key)
  385. {
  386. rsa_ssh1_public_blob(bs, key, RSA_SSH1_MODULUS_FIRST);
  387. put_mp_ssh1(bs, key->private_exponent);
  388. put_mp_ssh1(bs, key->iqmp);
  389. put_mp_ssh1(bs, key->q);
  390. put_mp_ssh1(bs, key->p);
  391. }
  392. /* Given an SSH-1 public key blob, determine its length. */
  393. int rsa_ssh1_public_blob_len(ptrlen data)
  394. {
  395. BinarySource src[1];
  396. BinarySource_BARE_INIT_PL(src, data);
  397. /* Expect a length word, then exponent and modulus. (It doesn't
  398. * even matter which order.) */
  399. get_uint32(src);
  400. mp_free(get_mp_ssh1(src));
  401. mp_free(get_mp_ssh1(src));
  402. if (get_err(src))
  403. return -1;
  404. /* Return the number of bytes consumed. */
  405. return src->pos;
  406. }
  407. void freersapriv(RSAKey *key)
  408. {
  409. if (key->private_exponent) {
  410. mp_free(key->private_exponent);
  411. key->private_exponent = NULL;
  412. }
  413. if (key->p) {
  414. mp_free(key->p);
  415. key->p = NULL;
  416. }
  417. if (key->q) {
  418. mp_free(key->q);
  419. key->q = NULL;
  420. }
  421. if (key->iqmp) {
  422. mp_free(key->iqmp);
  423. key->iqmp = NULL;
  424. }
  425. }
  426. void freersakey(RSAKey *key)
  427. {
  428. freersapriv(key);
  429. if (key->modulus) {
  430. mp_free(key->modulus);
  431. key->modulus = NULL;
  432. }
  433. if (key->exponent) {
  434. mp_free(key->exponent);
  435. key->exponent = NULL;
  436. }
  437. if (key->comment) {
  438. sfree(key->comment);
  439. key->comment = NULL;
  440. }
  441. }
  442. /* ----------------------------------------------------------------------
  443. * Implementation of the ssh-rsa signing key type family.
  444. */
  445. struct ssh2_rsa_extra {
  446. unsigned signflags;
  447. };
  448. static void rsa2_freekey(ssh_key *key); /* forward reference */
  449. static ssh_key *rsa2_new_pub(const ssh_keyalg *self, ptrlen data)
  450. {
  451. BinarySource src[1];
  452. RSAKey *rsa;
  453. BinarySource_BARE_INIT_PL(src, data);
  454. if (!ptrlen_eq_string(get_string(src), "ssh-rsa"))
  455. return NULL;
  456. rsa = snew(RSAKey);
  457. rsa->sshk.vt = self;
  458. rsa->exponent = get_mp_ssh2(src);
  459. rsa->modulus = get_mp_ssh2(src);
  460. rsa->private_exponent = NULL;
  461. rsa->p = rsa->q = rsa->iqmp = NULL;
  462. rsa->comment = NULL;
  463. if (get_err(src)) {
  464. rsa2_freekey(&rsa->sshk);
  465. return NULL;
  466. }
  467. return &rsa->sshk;
  468. }
  469. static void rsa2_freekey(ssh_key *key)
  470. {
  471. RSAKey *rsa = container_of(key, RSAKey, sshk);
  472. freersakey(rsa);
  473. sfree(rsa);
  474. }
  475. static char *rsa2_cache_str(ssh_key *key)
  476. {
  477. RSAKey *rsa = container_of(key, RSAKey, sshk);
  478. return rsastr_fmt(rsa);
  479. }
  480. static key_components *rsa2_components(ssh_key *key)
  481. {
  482. RSAKey *rsa = container_of(key, RSAKey, sshk);
  483. return rsa_components(rsa);
  484. }
  485. static bool rsa2_has_private(ssh_key *key)
  486. {
  487. RSAKey *rsa = container_of(key, RSAKey, sshk);
  488. return rsa->private_exponent != NULL;
  489. }
  490. static void rsa2_public_blob(ssh_key *key, BinarySink *bs)
  491. {
  492. RSAKey *rsa = container_of(key, RSAKey, sshk);
  493. put_stringz(bs, "ssh-rsa");
  494. put_mp_ssh2(bs, rsa->exponent);
  495. put_mp_ssh2(bs, rsa->modulus);
  496. }
  497. static void rsa2_private_blob(ssh_key *key, BinarySink *bs)
  498. {
  499. RSAKey *rsa = container_of(key, RSAKey, sshk);
  500. put_mp_ssh2(bs, rsa->private_exponent);
  501. put_mp_ssh2(bs, rsa->p);
  502. put_mp_ssh2(bs, rsa->q);
  503. put_mp_ssh2(bs, rsa->iqmp);
  504. }
  505. static ssh_key *rsa2_new_priv(const ssh_keyalg *self,
  506. ptrlen pub, ptrlen priv)
  507. {
  508. BinarySource src[1];
  509. ssh_key *sshk;
  510. RSAKey *rsa;
  511. sshk = rsa2_new_pub(self, pub);
  512. if (!sshk)
  513. return NULL;
  514. rsa = container_of(sshk, RSAKey, sshk);
  515. BinarySource_BARE_INIT_PL(src, priv);
  516. rsa->private_exponent = get_mp_ssh2(src);
  517. rsa->p = get_mp_ssh2(src);
  518. rsa->q = get_mp_ssh2(src);
  519. rsa->iqmp = get_mp_ssh2(src);
  520. if (get_err(src) || !rsa_verify(rsa)) {
  521. rsa2_freekey(&rsa->sshk);
  522. return NULL;
  523. }
  524. return &rsa->sshk;
  525. }
  526. static ssh_key *rsa2_new_priv_openssh(const ssh_keyalg *self,
  527. BinarySource *src)
  528. {
  529. RSAKey *rsa;
  530. rsa = snew(RSAKey);
  531. rsa->sshk.vt = &ssh_rsa;
  532. rsa->comment = NULL;
  533. rsa->modulus = get_mp_ssh2(src);
  534. rsa->exponent = get_mp_ssh2(src);
  535. rsa->private_exponent = get_mp_ssh2(src);
  536. rsa->iqmp = get_mp_ssh2(src);
  537. rsa->p = get_mp_ssh2(src);
  538. rsa->q = get_mp_ssh2(src);
  539. if (get_err(src) || !rsa_verify(rsa)) {
  540. rsa2_freekey(&rsa->sshk);
  541. return NULL;
  542. }
  543. return &rsa->sshk;
  544. }
  545. static void rsa2_openssh_blob(ssh_key *key, BinarySink *bs)
  546. {
  547. RSAKey *rsa = container_of(key, RSAKey, sshk);
  548. put_mp_ssh2(bs, rsa->modulus);
  549. put_mp_ssh2(bs, rsa->exponent);
  550. put_mp_ssh2(bs, rsa->private_exponent);
  551. put_mp_ssh2(bs, rsa->iqmp);
  552. put_mp_ssh2(bs, rsa->p);
  553. put_mp_ssh2(bs, rsa->q);
  554. }
  555. static int rsa2_pubkey_bits(const ssh_keyalg *self, ptrlen pub)
  556. {
  557. ssh_key *sshk;
  558. RSAKey *rsa;
  559. int ret;
  560. sshk = rsa2_new_pub(self, pub);
  561. if (!sshk)
  562. return -1;
  563. rsa = container_of(sshk, RSAKey, sshk);
  564. ret = mp_get_nbits(rsa->modulus);
  565. rsa2_freekey(&rsa->sshk);
  566. return ret;
  567. }
  568. static inline const ssh_hashalg *rsa2_hash_alg_for_flags(
  569. unsigned flags, const char **protocol_id_out)
  570. {
  571. const ssh_hashalg *halg;
  572. const char *protocol_id;
  573. if (flags & SSH_AGENT_RSA_SHA2_256) {
  574. halg = &ssh_sha256;
  575. protocol_id = "rsa-sha2-256";
  576. } else if (flags & SSH_AGENT_RSA_SHA2_512) {
  577. halg = &ssh_sha512;
  578. protocol_id = "rsa-sha2-512";
  579. } else {
  580. halg = &ssh_sha1;
  581. protocol_id = "ssh-rsa";
  582. }
  583. if (protocol_id_out)
  584. *protocol_id_out = protocol_id;
  585. return halg;
  586. }
  587. static inline ptrlen rsa_pkcs1_prefix_for_hash(const ssh_hashalg *halg)
  588. {
  589. if (halg == &ssh_sha1) {
  590. /*
  591. * This is the magic ASN.1/DER prefix that goes in the decoded
  592. * signature, between the string of FFs and the actual SHA-1
  593. * hash value. The meaning of it is:
  594. *
  595. * 00 -- this marks the end of the FFs; not part of the ASN.1
  596. * bit itself
  597. *
  598. * 30 21 -- a constructed SEQUENCE of length 0x21
  599. * 30 09 -- a constructed sub-SEQUENCE of length 9
  600. * 06 05 -- an object identifier, length 5
  601. * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
  602. * (the 1,3 comes from 0x2B = 43 = 40*1+3)
  603. * 05 00 -- NULL
  604. * 04 14 -- a primitive OCTET STRING of length 0x14
  605. * [0x14 bytes of hash data follows]
  606. *
  607. * The object id in the middle there is listed as `id-sha1' in
  608. * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn
  609. * (the ASN module for PKCS #1) and its expanded form is as
  610. * follows:
  611. *
  612. * id-sha1 OBJECT IDENTIFIER ::= {
  613. * iso(1) identified-organization(3) oiw(14) secsig(3)
  614. * algorithms(2) 26 }
  615. */
  616. static const unsigned char sha1_asn1_prefix[] = {
  617. 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B,
  618. 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
  619. };
  620. return PTRLEN_FROM_CONST_BYTES(sha1_asn1_prefix);
  621. }
  622. if (halg == &ssh_sha256) {
  623. /*
  624. * A similar piece of ASN.1 used for signatures using SHA-256,
  625. * in the same format but differing only in various length
  626. * fields and OID.
  627. */
  628. static const unsigned char sha256_asn1_prefix[] = {
  629. 0x00, 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60,
  630. 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
  631. 0x05, 0x00, 0x04, 0x20,
  632. };
  633. return PTRLEN_FROM_CONST_BYTES(sha256_asn1_prefix);
  634. }
  635. if (halg == &ssh_sha512) {
  636. /*
  637. * And one more for SHA-512.
  638. */
  639. static const unsigned char sha512_asn1_prefix[] = {
  640. 0x00, 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60,
  641. 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
  642. 0x05, 0x00, 0x04, 0x40,
  643. };
  644. return PTRLEN_FROM_CONST_BYTES(sha512_asn1_prefix);
  645. }
  646. unreachable("bad hash algorithm for RSA PKCS#1");
  647. }
  648. static inline size_t rsa_pkcs1_length_of_fixed_parts(const ssh_hashalg *halg)
  649. {
  650. ptrlen asn1_prefix = rsa_pkcs1_prefix_for_hash(halg);
  651. return halg->hlen + asn1_prefix.len + 2;
  652. }
  653. static unsigned char *rsa_pkcs1_signature_string(
  654. size_t nbytes, const ssh_hashalg *halg, ptrlen data)
  655. {
  656. size_t fixed_parts = rsa_pkcs1_length_of_fixed_parts(halg);
  657. pinitassert(nbytes >= fixed_parts);
  658. size_t padding = nbytes - fixed_parts;
  659. ptrlen asn1_prefix = rsa_pkcs1_prefix_for_hash(halg);
  660. unsigned char *bytes = snewn(nbytes, unsigned char);
  661. bytes[0] = 0;
  662. bytes[1] = 1;
  663. memset(bytes + 2, 0xFF, padding);
  664. memcpy(bytes + 2 + padding, asn1_prefix.ptr, asn1_prefix.len);
  665. { // WINSCP
  666. ssh_hash *h = ssh_hash_new(halg);
  667. put_datapl(h, data);
  668. ssh_hash_final(h, bytes + 2 + padding + asn1_prefix.len);
  669. } // WINSCP
  670. return bytes;
  671. }
  672. static bool rsa2_verify(ssh_key *key, ptrlen sig, ptrlen data)
  673. {
  674. RSAKey *rsa = container_of(key, RSAKey, sshk);
  675. BinarySource src[1];
  676. ptrlen type, in_pl;
  677. mp_int *in, *out;
  678. const struct ssh2_rsa_extra *extra =
  679. (const struct ssh2_rsa_extra *)key->vt->extra;
  680. const ssh_hashalg *halg = rsa2_hash_alg_for_flags(extra->signflags, NULL);
  681. /* Start by making sure the key is even long enough to encode a
  682. * signature. If not, everything fails to verify. */
  683. size_t nbytes = (mp_get_nbits(rsa->modulus) + 7) / 8;
  684. if (nbytes < rsa_pkcs1_length_of_fixed_parts(halg))
  685. return false;
  686. BinarySource_BARE_INIT_PL(src, sig);
  687. type = get_string(src);
  688. /*
  689. * RFC 4253 section 6.6: the signature integer in an ssh-rsa
  690. * signature is 'without lengths or padding'. That is, we _don't_
  691. * expect the usual leading zero byte if the topmost bit of the
  692. * first byte is set. (However, because of the possibility of
  693. * BUG_SSH2_RSA_PADDING at the other end, we tolerate it if it's
  694. * there.) So we can't use get_mp_ssh2, which enforces that
  695. * leading-byte scheme; instead we use get_string and
  696. * mp_from_bytes_be, which will tolerate anything.
  697. */
  698. in_pl = get_string(src);
  699. if (get_err(src) || !ptrlen_eq_string(type, key->vt->ssh_id))
  700. return false;
  701. in = mp_from_bytes_be(in_pl);
  702. out = mp_modpow(in, rsa->exponent, rsa->modulus);
  703. mp_free(in);
  704. { // WINSCP
  705. unsigned diff = 0;
  706. unsigned char *bytes = rsa_pkcs1_signature_string(nbytes, halg, data);
  707. size_t i; // WINSCP
  708. for (i = 0; i < nbytes; i++)
  709. diff |= bytes[nbytes-1 - i] ^ mp_get_byte(out, i);
  710. smemclr(bytes, nbytes);
  711. sfree(bytes);
  712. mp_free(out);
  713. return diff == 0;
  714. } // WINSCP
  715. }
  716. static void rsa2_sign(ssh_key *key, ptrlen data,
  717. unsigned flags, BinarySink *bs)
  718. {
  719. RSAKey *rsa = container_of(key, RSAKey, sshk);
  720. unsigned char *bytes;
  721. size_t nbytes;
  722. mp_int *in, *out;
  723. const ssh_hashalg *halg;
  724. const char *sign_alg_name;
  725. const struct ssh2_rsa_extra *extra =
  726. (const struct ssh2_rsa_extra *)key->vt->extra;
  727. flags |= extra->signflags;
  728. halg = rsa2_hash_alg_for_flags(flags, &sign_alg_name);
  729. nbytes = (mp_get_nbits(rsa->modulus) + 7) / 8;
  730. bytes = rsa_pkcs1_signature_string(nbytes, halg, data);
  731. in = mp_from_bytes_be(make_ptrlen(bytes, nbytes));
  732. smemclr(bytes, nbytes);
  733. sfree(bytes);
  734. out = rsa_privkey_op(in, rsa);
  735. mp_free(in);
  736. put_stringz(bs, sign_alg_name);
  737. if (flags == 0) {
  738. /*
  739. * Original "ssh-rsa", per RFC 4253 section 6.6, stores the
  740. * signature integer in a string without padding - not even
  741. * the leading zero byte that an ordinary SSH-2 mpint would
  742. * require to avoid looking like two's complement.
  743. *
  744. * "The value for 'rsa_signature_blob' is encoded as a string
  745. * containing s (which is an integer, without lengths or
  746. * padding, unsigned, and in network byte order)."
  747. */
  748. nbytes = (mp_get_nbits(out) + 7) / 8;
  749. } else {
  750. /*
  751. * The SHA-256 and SHA-512 signature systems, per RFC 8332
  752. * section 3, should be padded to the length of the key
  753. * modulus.
  754. *
  755. * "The value for 'rsa_signature_blob' is encoded as a string
  756. * that contains an octet string S (which is the output of
  757. * RSASSA-PKCS1-v1_5) and that has the same length (in octets)
  758. * as the RSA modulus."
  759. *
  760. * Awkwardly, RFC 8332 doesn't say whether that means the
  761. * 'raw' length of the RSA modulus (that is, ceil(n/8) for an
  762. * n-bit key) or the length it would occupy as an SSH-2 mpint.
  763. * My interpretation is the former.
  764. */
  765. nbytes = (mp_get_nbits(rsa->modulus) + 7) / 8;
  766. }
  767. put_uint32(bs, nbytes);
  768. { // WINSCP
  769. size_t i; // WINSCP
  770. for (i = 0; i < nbytes; i++)
  771. put_byte(bs, mp_get_byte(out, nbytes - 1 - i));
  772. } // WINSCP
  773. mp_free(out);
  774. }
  775. static char *rsa2_invalid(ssh_key *key, unsigned flags)
  776. {
  777. RSAKey *rsa = container_of(key, RSAKey, sshk);
  778. size_t bits = mp_get_nbits(rsa->modulus), nbytes = (bits + 7) / 8;
  779. const char *sign_alg_name;
  780. const ssh_hashalg *halg = rsa2_hash_alg_for_flags(flags, &sign_alg_name);
  781. if (nbytes < rsa_pkcs1_length_of_fixed_parts(halg)) {
  782. return dupprintf(
  783. "%"SIZEu"-bit RSA key is too short to generate %s signatures",
  784. bits, sign_alg_name);
  785. }
  786. return NULL;
  787. }
  788. static unsigned ssh_rsa_supported_flags(const ssh_keyalg *self)
  789. {
  790. return SSH_AGENT_RSA_SHA2_256 | SSH_AGENT_RSA_SHA2_512;
  791. }
  792. static const char *ssh_rsa_alternate_ssh_id(
  793. const ssh_keyalg *self, unsigned flags)
  794. {
  795. if (flags & SSH_AGENT_RSA_SHA2_512)
  796. return ssh_rsa_sha512.ssh_id;
  797. if (flags & SSH_AGENT_RSA_SHA2_256)
  798. return ssh_rsa_sha256.ssh_id;
  799. return self->ssh_id;
  800. }
  801. static char *rsa2_alg_desc(const ssh_keyalg *self) { return dupstr("RSA"); }
  802. static const struct ssh2_rsa_extra
  803. rsa_extra = { 0 },
  804. rsa_sha256_extra = { SSH_AGENT_RSA_SHA2_256 },
  805. rsa_sha512_extra = { SSH_AGENT_RSA_SHA2_512 };
  806. // WINSCP
  807. #define COMMON_KEYALG_FIELDS \
  808. /*.new_pub =*/ rsa2_new_pub, \
  809. /*.new_priv =*/ rsa2_new_priv, \
  810. /*.new_priv_openssh =*/ rsa2_new_priv_openssh, \
  811. /*.freekey =*/ rsa2_freekey, \
  812. /*.invalid =*/ rsa2_invalid, \
  813. /*.sign =*/ rsa2_sign, \
  814. /*.verify =*/ rsa2_verify, \
  815. /*.public_blob =*/ rsa2_public_blob, \
  816. /*.private_blob =*/ rsa2_private_blob, \
  817. /*.openssh_blob =*/ rsa2_openssh_blob, \
  818. /*.has_private =*/ rsa2_has_private, \
  819. /*.cache_str =*/ rsa2_cache_str, \
  820. /*.components =*/ rsa2_components, \
  821. /*.base_key =*/ nullkey_base_key, \
  822. NULL, NULL, NULL, NULL, \
  823. /*.pubkey_bits =*/ rsa2_pubkey_bits
  824. #define COMMON_KEYALG_FIELDS1a \
  825. /*.alg_desc =*/ rsa2_alg_desc, \
  826. /*.variable_size =*/ nullkey_variable_size_yes, \
  827. NULL
  828. #define COMMON_KEYALG_FIELDS2 \
  829. /*.cache_id =*/ "rsa2"
  830. #define COMMON_KEYALG_FIELDS3 \
  831. false, NULL
  832. const ssh_keyalg ssh_rsa = {
  833. // WINSCP
  834. COMMON_KEYALG_FIELDS,
  835. /*.supported_flags =*/ ssh_rsa_supported_flags,
  836. /*.alternate_ssh_id =*/ ssh_rsa_alternate_ssh_id,
  837. COMMON_KEYALG_FIELDS1a,
  838. /*.ssh_id =*/ "ssh-rsa",
  839. COMMON_KEYALG_FIELDS2,
  840. /*.extra =*/ &rsa_extra,
  841. COMMON_KEYALG_FIELDS3,
  842. };
  843. const ssh_keyalg ssh_rsa_sha256 = {
  844. // WINSCP
  845. COMMON_KEYALG_FIELDS,
  846. /*.supported_flags =*/ nullkey_supported_flags,
  847. /*.alternate_ssh_id =*/ nullkey_alternate_ssh_id,
  848. COMMON_KEYALG_FIELDS1a,
  849. /*.ssh_id =*/ "rsa-sha2-256",
  850. COMMON_KEYALG_FIELDS2,
  851. /*.extra =*/ &rsa_sha256_extra,
  852. COMMON_KEYALG_FIELDS3,
  853. };
  854. const ssh_keyalg ssh_rsa_sha512 = {
  855. // WINSCP
  856. COMMON_KEYALG_FIELDS,
  857. /*.supported_flags =*/ nullkey_supported_flags,
  858. /*.alternate_ssh_id =*/ nullkey_alternate_ssh_id,
  859. COMMON_KEYALG_FIELDS1a,
  860. /*.ssh_id =*/ "rsa-sha2-512",
  861. COMMON_KEYALG_FIELDS2,
  862. /*.extra =*/ &rsa_sha512_extra,
  863. COMMON_KEYALG_FIELDS3,
  864. };
  865. RSAKey *ssh_rsakex_newkey(ptrlen data)
  866. {
  867. ssh_key *sshk = rsa2_new_pub(&ssh_rsa, data);
  868. if (!sshk)
  869. return NULL;
  870. return container_of(sshk, RSAKey, sshk);
  871. }
  872. void ssh_rsakex_freekey(RSAKey *key)
  873. {
  874. rsa2_freekey(&key->sshk);
  875. }
  876. int ssh_rsakex_klen(RSAKey *rsa)
  877. {
  878. return mp_get_nbits(rsa->modulus);
  879. }
  880. static void oaep_mask(const ssh_hashalg *h, void *seed, int seedlen,
  881. void *vdata, int datalen)
  882. {
  883. unsigned char *data = (unsigned char *)vdata;
  884. unsigned count = 0;
  885. ssh_hash *s = ssh_hash_new(h);
  886. while (datalen > 0) {
  887. int i, max = (datalen > h->hlen ? h->hlen : datalen);
  888. unsigned char hash[MAX_HASH_LEN];
  889. ssh_hash_reset(s);
  890. assert(h->hlen <= MAX_HASH_LEN);
  891. put_data(s, seed, seedlen);
  892. put_uint32(s, count);
  893. ssh_hash_digest(s, hash);
  894. count++;
  895. for (i = 0; i < max; i++)
  896. data[i] ^= hash[i];
  897. data += max;
  898. datalen -= max;
  899. }
  900. ssh_hash_free(s);
  901. }
  902. strbuf *ssh_rsakex_encrypt(RSAKey *rsa, const ssh_hashalg *h, ptrlen in)
  903. {
  904. mp_int *b1, *b2;
  905. int k, i;
  906. char *p;
  907. const int HLEN = h->hlen;
  908. /*
  909. * Here we encrypt using RSAES-OAEP. Essentially this means:
  910. *
  911. * - we have a SHA-based `mask generation function' which
  912. * creates a pseudo-random stream of mask data
  913. * deterministically from an input chunk of data.
  914. *
  915. * - we have a random chunk of data called a seed.
  916. *
  917. * - we use the seed to generate a mask which we XOR with our
  918. * plaintext.
  919. *
  920. * - then we use _the masked plaintext_ to generate a mask
  921. * which we XOR with the seed.
  922. *
  923. * - then we concatenate the masked seed and the masked
  924. * plaintext, and RSA-encrypt that lot.
  925. *
  926. * The result is that the data input to the encryption function
  927. * is random-looking and (hopefully) contains no exploitable
  928. * structure such as PKCS1-v1_5 does.
  929. *
  930. * For a precise specification, see RFC 3447, section 7.1.1.
  931. * Some of the variable names below are derived from that, so
  932. * it'd probably help to read it anyway.
  933. */
  934. /* k denotes the length in octets of the RSA modulus. */
  935. k = (7 + mp_get_nbits(rsa->modulus)) / 8;
  936. /* The length of the input data must be at most k - 2hLen - 2. */
  937. assert(in.len > 0 && in.len <= k - 2*HLEN - 2);
  938. /* The length of the output data wants to be precisely k. */
  939. { // WINSCP
  940. strbuf *toret = strbuf_new_nm();
  941. int outlen = k;
  942. unsigned char *out = strbuf_append(toret, outlen);
  943. /*
  944. * Now perform EME-OAEP encoding. First set up all the unmasked
  945. * output data.
  946. */
  947. /* Leading byte zero. */
  948. out[0] = 0;
  949. /* At position 1, the seed: HLEN bytes of random data. */
  950. random_read(out + 1, HLEN);
  951. /* At position 1+HLEN, the data block DB, consisting of: */
  952. /* The hash of the label (we only support an empty label here) */
  953. hash_simple(h, PTRLEN_LITERAL(""), out + HLEN + 1);
  954. /* A bunch of zero octets */
  955. memset(out + 2*HLEN + 1, 0, outlen - (2*HLEN + 1));
  956. /* A single 1 octet, followed by the input message data. */
  957. out[outlen - in.len - 1] = 1;
  958. memcpy(out + outlen - in.len, in.ptr, in.len);
  959. /*
  960. * Now use the seed data to mask the block DB.
  961. */
  962. oaep_mask(h, out+1, HLEN, out+HLEN+1, outlen-HLEN-1);
  963. /*
  964. * And now use the masked DB to mask the seed itself.
  965. */
  966. oaep_mask(h, out+HLEN+1, outlen-HLEN-1, out+1, HLEN);
  967. /*
  968. * Now `out' contains precisely the data we want to
  969. * RSA-encrypt.
  970. */
  971. b1 = mp_from_bytes_be(make_ptrlen(out, outlen));
  972. b2 = mp_modpow(b1, rsa->exponent, rsa->modulus);
  973. p = (char *)out;
  974. for (i = outlen; i--;) {
  975. *p++ = mp_get_byte(b2, i);
  976. }
  977. mp_free(b1);
  978. mp_free(b2);
  979. /*
  980. * And we're done.
  981. */
  982. return toret;
  983. } // WINSCP
  984. }
  985. mp_int *ssh_rsakex_decrypt(
  986. RSAKey *rsa, const ssh_hashalg *h, ptrlen ciphertext)
  987. {
  988. mp_int *b1, *b2;
  989. int outlen, i;
  990. unsigned char *out;
  991. unsigned char labelhash[64];
  992. BinarySource src[1];
  993. const int HLEN = h->hlen;
  994. /*
  995. * Decryption side of the RSA key exchange operation.
  996. */
  997. /* The length of the encrypted data should be exactly the length
  998. * in octets of the RSA modulus.. */
  999. outlen = (7 + mp_get_nbits(rsa->modulus)) / 8;
  1000. if (ciphertext.len != outlen)
  1001. return NULL;
  1002. /* Do the RSA decryption, and extract the result into a byte array. */
  1003. b1 = mp_from_bytes_be(ciphertext);
  1004. b2 = rsa_privkey_op(b1, rsa);
  1005. out = snewn(outlen, unsigned char);
  1006. for (i = 0; i < outlen; i++)
  1007. out[i] = mp_get_byte(b2, outlen-1-i);
  1008. mp_free(b1);
  1009. mp_free(b2);
  1010. /* Do the OAEP masking operations, in the reverse order from encryption */
  1011. oaep_mask(h, out+HLEN+1, outlen-HLEN-1, out+1, HLEN);
  1012. oaep_mask(h, out+1, HLEN, out+HLEN+1, outlen-HLEN-1);
  1013. /* Check the leading byte is zero. */
  1014. if (out[0] != 0) {
  1015. sfree(out);
  1016. return NULL;
  1017. }
  1018. /* Check the label hash at position 1+HLEN */
  1019. assert(HLEN <= lenof(labelhash));
  1020. hash_simple(h, PTRLEN_LITERAL(""), labelhash);
  1021. if (memcmp(out + HLEN + 1, labelhash, HLEN)) {
  1022. sfree(out);
  1023. return NULL;
  1024. }
  1025. /* Expect zero bytes followed by a 1 byte */
  1026. for (i = 1 + 2 * HLEN; i < outlen; i++) {
  1027. if (out[i] == 1) {
  1028. i++; /* skip over the 1 byte */
  1029. break;
  1030. } else if (out[i] != 0) {
  1031. sfree(out);
  1032. return NULL;
  1033. }
  1034. }
  1035. /* And what's left is the input message data, which should be
  1036. * encoded as an ordinary SSH-2 mpint. */
  1037. BinarySource_BARE_INIT(src, out + i, outlen - i);
  1038. b1 = get_mp_ssh2(src);
  1039. sfree(out);
  1040. if (get_err(src) || get_avail(src) != 0) {
  1041. mp_free(b1);
  1042. return NULL;
  1043. }
  1044. /* Success! */
  1045. return b1;
  1046. }
  1047. static const struct ssh_rsa_kex_extra ssh_rsa_kex_extra_sha1 = { 1024 };
  1048. static const struct ssh_rsa_kex_extra ssh_rsa_kex_extra_sha256 = { 2048 };
  1049. static const ssh_kex ssh_rsa_kex_sha1 = {
  1050. /*.name =*/ "rsa1024-sha1",
  1051. NULL,
  1052. /*.main_type =*/ KEXTYPE_RSA,
  1053. /*.hash =*/ &ssh_sha1,
  1054. NULL, // WINSCP
  1055. /*.extra =*/ &ssh_rsa_kex_extra_sha1,
  1056. };
  1057. static const ssh_kex ssh_rsa_kex_sha256 = {
  1058. /*.name =*/ "rsa2048-sha256",
  1059. NULL,
  1060. /*.main_type =*/ KEXTYPE_RSA,
  1061. /*.hash =*/ &ssh_sha256,
  1062. NULL, // WINSCP
  1063. /*.extra =*/ &ssh_rsa_kex_extra_sha256,
  1064. };
  1065. static const ssh_kex *const rsa_kex_list[] = {
  1066. &ssh_rsa_kex_sha256,
  1067. &ssh_rsa_kex_sha1
  1068. };
  1069. const ssh_kexes ssh_rsa_kex = { lenof(rsa_kex_list), rsa_kex_list };