rsa.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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 **ret = snewn(SSH_N_FPTYPES, char *);
  314. unsigned i; // WINSCP
  315. for (i = 0; i < SSH_N_FPTYPES; i++)
  316. ret[i] = NULL;
  317. ret[SSH_FPTYPE_MD5] = rsa_ssh1_fingerprint(key);
  318. return ret;
  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. nbytes = (mp_get_nbits(out) + 7) / 8;
  738. put_uint32(bs, nbytes);
  739. { // WINSCP
  740. size_t i; // WINSCP
  741. for (i = 0; i < nbytes; i++)
  742. put_byte(bs, mp_get_byte(out, nbytes - 1 - i));
  743. } // WINSCP
  744. mp_free(out);
  745. }
  746. static char *rsa2_invalid(ssh_key *key, unsigned flags)
  747. {
  748. RSAKey *rsa = container_of(key, RSAKey, sshk);
  749. size_t bits = mp_get_nbits(rsa->modulus), nbytes = (bits + 7) / 8;
  750. const char *sign_alg_name;
  751. const ssh_hashalg *halg = rsa2_hash_alg_for_flags(flags, &sign_alg_name);
  752. if (nbytes < rsa_pkcs1_length_of_fixed_parts(halg)) {
  753. return dupprintf(
  754. "%"SIZEu"-bit RSA key is too short to generate %s signatures",
  755. bits, sign_alg_name);
  756. }
  757. return NULL;
  758. }
  759. static unsigned ssh_rsa_supported_flags(const ssh_keyalg *self)
  760. {
  761. return SSH_AGENT_RSA_SHA2_256 | SSH_AGENT_RSA_SHA2_512;
  762. }
  763. static const char *ssh_rsa_alternate_ssh_id(
  764. const ssh_keyalg *self, unsigned flags)
  765. {
  766. if (flags & SSH_AGENT_RSA_SHA2_512)
  767. return ssh_rsa_sha512.ssh_id;
  768. if (flags & SSH_AGENT_RSA_SHA2_256)
  769. return ssh_rsa_sha256.ssh_id;
  770. return self->ssh_id;
  771. }
  772. static char *rsa2_alg_desc(const ssh_keyalg *self) { return dupstr("RSA"); }
  773. static const struct ssh2_rsa_extra
  774. rsa_extra = { 0 },
  775. rsa_sha256_extra = { SSH_AGENT_RSA_SHA2_256 },
  776. rsa_sha512_extra = { SSH_AGENT_RSA_SHA2_512 };
  777. // WINSCP
  778. #define COMMON_KEYALG_FIELDS \
  779. /*.new_pub =*/ rsa2_new_pub, \
  780. /*.new_priv =*/ rsa2_new_priv, \
  781. /*.new_priv_openssh =*/ rsa2_new_priv_openssh, \
  782. /*.freekey =*/ rsa2_freekey, \
  783. /*.invalid =*/ rsa2_invalid, \
  784. /*.sign =*/ rsa2_sign, \
  785. /*.verify =*/ rsa2_verify, \
  786. /*.public_blob =*/ rsa2_public_blob, \
  787. /*.private_blob =*/ rsa2_private_blob, \
  788. /*.openssh_blob =*/ rsa2_openssh_blob, \
  789. /*.has_private =*/ rsa2_has_private, \
  790. /*.cache_str =*/ rsa2_cache_str, \
  791. /*.components =*/ rsa2_components, \
  792. /*.base_key =*/ nullkey_base_key, \
  793. NULL, NULL, NULL, NULL, \
  794. /*.pubkey_bits =*/ rsa2_pubkey_bits
  795. #define COMMON_KEYALG_FIELDS1a \
  796. /*.alg_desc =*/ rsa2_alg_desc, \
  797. /*.variable_size =*/ nullkey_variable_size_yes, \
  798. NULL
  799. #define COMMON_KEYALG_FIELDS2 \
  800. /*.cache_id =*/ "rsa2"
  801. #define COMMON_KEYALG_FIELDS3 \
  802. false, NULL
  803. const ssh_keyalg ssh_rsa = {
  804. // WINSCP
  805. COMMON_KEYALG_FIELDS,
  806. /*.supported_flags =*/ ssh_rsa_supported_flags,
  807. /*.alternate_ssh_id =*/ ssh_rsa_alternate_ssh_id,
  808. COMMON_KEYALG_FIELDS1a,
  809. /*.ssh_id =*/ "ssh-rsa",
  810. COMMON_KEYALG_FIELDS2,
  811. /*.extra =*/ &rsa_extra,
  812. COMMON_KEYALG_FIELDS3,
  813. };
  814. const ssh_keyalg ssh_rsa_sha256 = {
  815. // WINSCP
  816. COMMON_KEYALG_FIELDS,
  817. /*.supported_flags =*/ nullkey_supported_flags,
  818. /*.alternate_ssh_id =*/ nullkey_alternate_ssh_id,
  819. COMMON_KEYALG_FIELDS1a,
  820. /*.ssh_id =*/ "rsa-sha2-256",
  821. COMMON_KEYALG_FIELDS2,
  822. /*.extra =*/ &rsa_sha256_extra,
  823. COMMON_KEYALG_FIELDS3,
  824. };
  825. const ssh_keyalg ssh_rsa_sha512 = {
  826. // WINSCP
  827. COMMON_KEYALG_FIELDS,
  828. /*.supported_flags =*/ nullkey_supported_flags,
  829. /*.alternate_ssh_id =*/ nullkey_alternate_ssh_id,
  830. COMMON_KEYALG_FIELDS1a,
  831. /*.ssh_id =*/ "rsa-sha2-512",
  832. COMMON_KEYALG_FIELDS2,
  833. /*.extra =*/ &rsa_sha512_extra,
  834. COMMON_KEYALG_FIELDS3,
  835. };
  836. RSAKey *ssh_rsakex_newkey(ptrlen data)
  837. {
  838. ssh_key *sshk = rsa2_new_pub(&ssh_rsa, data);
  839. if (!sshk)
  840. return NULL;
  841. return container_of(sshk, RSAKey, sshk);
  842. }
  843. void ssh_rsakex_freekey(RSAKey *key)
  844. {
  845. rsa2_freekey(&key->sshk);
  846. }
  847. int ssh_rsakex_klen(RSAKey *rsa)
  848. {
  849. return mp_get_nbits(rsa->modulus);
  850. }
  851. static void oaep_mask(const ssh_hashalg *h, void *seed, int seedlen,
  852. void *vdata, int datalen)
  853. {
  854. unsigned char *data = (unsigned char *)vdata;
  855. unsigned count = 0;
  856. ssh_hash *s = ssh_hash_new(h);
  857. while (datalen > 0) {
  858. int i, max = (datalen > h->hlen ? h->hlen : datalen);
  859. unsigned char hash[MAX_HASH_LEN];
  860. ssh_hash_reset(s);
  861. assert(h->hlen <= MAX_HASH_LEN);
  862. put_data(s, seed, seedlen);
  863. put_uint32(s, count);
  864. ssh_hash_digest(s, hash);
  865. count++;
  866. for (i = 0; i < max; i++)
  867. data[i] ^= hash[i];
  868. data += max;
  869. datalen -= max;
  870. }
  871. ssh_hash_free(s);
  872. }
  873. strbuf *ssh_rsakex_encrypt(RSAKey *rsa, const ssh_hashalg *h, ptrlen in)
  874. {
  875. mp_int *b1, *b2;
  876. int k, i;
  877. char *p;
  878. const int HLEN = h->hlen;
  879. /*
  880. * Here we encrypt using RSAES-OAEP. Essentially this means:
  881. *
  882. * - we have a SHA-based `mask generation function' which
  883. * creates a pseudo-random stream of mask data
  884. * deterministically from an input chunk of data.
  885. *
  886. * - we have a random chunk of data called a seed.
  887. *
  888. * - we use the seed to generate a mask which we XOR with our
  889. * plaintext.
  890. *
  891. * - then we use _the masked plaintext_ to generate a mask
  892. * which we XOR with the seed.
  893. *
  894. * - then we concatenate the masked seed and the masked
  895. * plaintext, and RSA-encrypt that lot.
  896. *
  897. * The result is that the data input to the encryption function
  898. * is random-looking and (hopefully) contains no exploitable
  899. * structure such as PKCS1-v1_5 does.
  900. *
  901. * For a precise specification, see RFC 3447, section 7.1.1.
  902. * Some of the variable names below are derived from that, so
  903. * it'd probably help to read it anyway.
  904. */
  905. /* k denotes the length in octets of the RSA modulus. */
  906. k = (7 + mp_get_nbits(rsa->modulus)) / 8;
  907. /* The length of the input data must be at most k - 2hLen - 2. */
  908. assert(in.len > 0 && in.len <= k - 2*HLEN - 2);
  909. /* The length of the output data wants to be precisely k. */
  910. { // WINSCP
  911. strbuf *toret = strbuf_new_nm();
  912. int outlen = k;
  913. unsigned char *out = strbuf_append(toret, outlen);
  914. /*
  915. * Now perform EME-OAEP encoding. First set up all the unmasked
  916. * output data.
  917. */
  918. /* Leading byte zero. */
  919. out[0] = 0;
  920. /* At position 1, the seed: HLEN bytes of random data. */
  921. random_read(out + 1, HLEN);
  922. /* At position 1+HLEN, the data block DB, consisting of: */
  923. /* The hash of the label (we only support an empty label here) */
  924. hash_simple(h, PTRLEN_LITERAL(""), out + HLEN + 1);
  925. /* A bunch of zero octets */
  926. memset(out + 2*HLEN + 1, 0, outlen - (2*HLEN + 1));
  927. /* A single 1 octet, followed by the input message data. */
  928. out[outlen - in.len - 1] = 1;
  929. memcpy(out + outlen - in.len, in.ptr, in.len);
  930. /*
  931. * Now use the seed data to mask the block DB.
  932. */
  933. oaep_mask(h, out+1, HLEN, out+HLEN+1, outlen-HLEN-1);
  934. /*
  935. * And now use the masked DB to mask the seed itself.
  936. */
  937. oaep_mask(h, out+HLEN+1, outlen-HLEN-1, out+1, HLEN);
  938. /*
  939. * Now `out' contains precisely the data we want to
  940. * RSA-encrypt.
  941. */
  942. b1 = mp_from_bytes_be(make_ptrlen(out, outlen));
  943. b2 = mp_modpow(b1, rsa->exponent, rsa->modulus);
  944. p = (char *)out;
  945. for (i = outlen; i--;) {
  946. *p++ = mp_get_byte(b2, i);
  947. }
  948. mp_free(b1);
  949. mp_free(b2);
  950. /*
  951. * And we're done.
  952. */
  953. return toret;
  954. } // WINSCP
  955. }
  956. mp_int *ssh_rsakex_decrypt(
  957. RSAKey *rsa, const ssh_hashalg *h, ptrlen ciphertext)
  958. {
  959. mp_int *b1, *b2;
  960. int outlen, i;
  961. unsigned char *out;
  962. unsigned char labelhash[64];
  963. BinarySource src[1];
  964. const int HLEN = h->hlen;
  965. /*
  966. * Decryption side of the RSA key exchange operation.
  967. */
  968. /* The length of the encrypted data should be exactly the length
  969. * in octets of the RSA modulus.. */
  970. outlen = (7 + mp_get_nbits(rsa->modulus)) / 8;
  971. if (ciphertext.len != outlen)
  972. return NULL;
  973. /* Do the RSA decryption, and extract the result into a byte array. */
  974. b1 = mp_from_bytes_be(ciphertext);
  975. b2 = rsa_privkey_op(b1, rsa);
  976. out = snewn(outlen, unsigned char);
  977. for (i = 0; i < outlen; i++)
  978. out[i] = mp_get_byte(b2, outlen-1-i);
  979. mp_free(b1);
  980. mp_free(b2);
  981. /* Do the OAEP masking operations, in the reverse order from encryption */
  982. oaep_mask(h, out+HLEN+1, outlen-HLEN-1, out+1, HLEN);
  983. oaep_mask(h, out+1, HLEN, out+HLEN+1, outlen-HLEN-1);
  984. /* Check the leading byte is zero. */
  985. if (out[0] != 0) {
  986. sfree(out);
  987. return NULL;
  988. }
  989. /* Check the label hash at position 1+HLEN */
  990. assert(HLEN <= lenof(labelhash));
  991. hash_simple(h, PTRLEN_LITERAL(""), labelhash);
  992. if (memcmp(out + HLEN + 1, labelhash, HLEN)) {
  993. sfree(out);
  994. return NULL;
  995. }
  996. /* Expect zero bytes followed by a 1 byte */
  997. for (i = 1 + 2 * HLEN; i < outlen; i++) {
  998. if (out[i] == 1) {
  999. i++; /* skip over the 1 byte */
  1000. break;
  1001. } else if (out[i] != 0) {
  1002. sfree(out);
  1003. return NULL;
  1004. }
  1005. }
  1006. /* And what's left is the input message data, which should be
  1007. * encoded as an ordinary SSH-2 mpint. */
  1008. BinarySource_BARE_INIT(src, out + i, outlen - i);
  1009. b1 = get_mp_ssh2(src);
  1010. sfree(out);
  1011. if (get_err(src) || get_avail(src) != 0) {
  1012. mp_free(b1);
  1013. return NULL;
  1014. }
  1015. /* Success! */
  1016. return b1;
  1017. }
  1018. static const struct ssh_rsa_kex_extra ssh_rsa_kex_extra_sha1 = { 1024 };
  1019. static const struct ssh_rsa_kex_extra ssh_rsa_kex_extra_sha256 = { 2048 };
  1020. static const ssh_kex ssh_rsa_kex_sha1 = {
  1021. /*.name =*/ "rsa1024-sha1",
  1022. NULL,
  1023. /*.main_type =*/ KEXTYPE_RSA,
  1024. /*.hash =*/ &ssh_sha1,
  1025. NULL, // WINSCP
  1026. /*.extra =*/ &ssh_rsa_kex_extra_sha1,
  1027. };
  1028. static const ssh_kex ssh_rsa_kex_sha256 = {
  1029. /*.name =*/ "rsa2048-sha256",
  1030. NULL,
  1031. /*.main_type =*/ KEXTYPE_RSA,
  1032. /*.hash =*/ &ssh_sha256,
  1033. NULL, // WINSCP
  1034. /*.extra =*/ &ssh_rsa_kex_extra_sha256,
  1035. };
  1036. static const ssh_kex *const rsa_kex_list[] = {
  1037. &ssh_rsa_kex_sha256,
  1038. &ssh_rsa_kex_sha1
  1039. };
  1040. const ssh_kexes ssh_rsa_kex = { lenof(rsa_kex_list), rsa_kex_list };