ssh2kex-client.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /*
  2. * Client side of key exchange for the SSH-2 transport protocol (RFC 4253).
  3. */
  4. #include <assert.h>
  5. #include "putty.h"
  6. #include "ssh.h"
  7. #include "sshbpp.h"
  8. #include "sshppl.h"
  9. #include "sshcr.h"
  10. #include "storage.h"
  11. #include "ssh2transport.h"
  12. #include "mpint.h"
  13. void ssh2kex_coroutine(struct ssh2_transport_state *s, bool *aborted)
  14. {
  15. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  16. PktIn *pktin;
  17. PktOut *pktout;
  18. crBegin(s->crStateKex);
  19. if (s->kex_alg->main_type == KEXTYPE_DH) {
  20. /*
  21. * Work out the number of bits of key we will need from the
  22. * key exchange. We start with the maximum key length of
  23. * either cipher...
  24. */
  25. {
  26. int csbits, scbits;
  27. csbits = s->out.cipher ? s->out.cipher->real_keybits : 0;
  28. scbits = s->in.cipher ? s->in.cipher->real_keybits : 0;
  29. s->nbits = (csbits > scbits ? csbits : scbits);
  30. }
  31. /* The keys only have hlen-bit entropy, since they're based on
  32. * a hash. So cap the key size at hlen bits. */
  33. if (s->nbits > s->kex_alg->hash->hlen * 8)
  34. s->nbits = s->kex_alg->hash->hlen * 8;
  35. /*
  36. * If we're doing Diffie-Hellman group exchange, start by
  37. * requesting a group.
  38. */
  39. if (dh_is_gex(s->kex_alg)) {
  40. ppl_logevent("Doing Diffie-Hellman group exchange");
  41. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_DHGEX;
  42. /*
  43. * Work out how big a DH group we will need to allow that
  44. * much data.
  45. */
  46. s->pbits = 512 << ((s->nbits - 1) / 64);
  47. if (s->pbits < DH_MIN_SIZE)
  48. s->pbits = DH_MIN_SIZE;
  49. if (s->pbits > DH_MAX_SIZE)
  50. s->pbits = DH_MAX_SIZE;
  51. if ((s->ppl.remote_bugs & BUG_SSH2_OLDGEX)) {
  52. pktout = ssh_bpp_new_pktout(
  53. s->ppl.bpp, SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
  54. put_uint32(pktout, s->pbits);
  55. } else {
  56. pktout = ssh_bpp_new_pktout(
  57. s->ppl.bpp, SSH2_MSG_KEX_DH_GEX_REQUEST);
  58. put_uint32(pktout, DH_MIN_SIZE);
  59. put_uint32(pktout, s->pbits);
  60. put_uint32(pktout, DH_MAX_SIZE);
  61. }
  62. pq_push(s->ppl.out_pq, pktout);
  63. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  64. if (pktin->type != SSH2_MSG_KEX_DH_GEX_GROUP) {
  65. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  66. "expecting Diffie-Hellman group, type %d (%s)",
  67. pktin->type,
  68. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  69. s->ppl.bpp->pls->actx,
  70. pktin->type));
  71. *aborted = true;
  72. return;
  73. }
  74. s->p = get_mp_ssh2(pktin);
  75. s->g = get_mp_ssh2(pktin);
  76. if (get_err(pktin)) {
  77. ssh_proto_error(s->ppl.ssh,
  78. "Unable to parse Diffie-Hellman group packet");
  79. *aborted = true;
  80. return;
  81. }
  82. s->dh_ctx = dh_setup_gex(s->p, s->g);
  83. s->kex_init_value = SSH2_MSG_KEX_DH_GEX_INIT;
  84. s->kex_reply_value = SSH2_MSG_KEX_DH_GEX_REPLY;
  85. ppl_logevent("Doing Diffie-Hellman key exchange using %d-bit "
  86. "modulus and hash %s with a server-supplied group",
  87. dh_modulus_bit_size(s->dh_ctx),
  88. ssh_hash_alg(s->exhash)->text_name);
  89. } else {
  90. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_DHGROUP;
  91. s->dh_ctx = dh_setup_group(s->kex_alg);
  92. s->kex_init_value = SSH2_MSG_KEXDH_INIT;
  93. s->kex_reply_value = SSH2_MSG_KEXDH_REPLY;
  94. ppl_logevent("Doing Diffie-Hellman key exchange using %d-bit "
  95. "modulus and hash %s with standard group \"%s\"",
  96. dh_modulus_bit_size(s->dh_ctx),
  97. ssh_hash_alg(s->exhash)->text_name,
  98. s->kex_alg->groupname);
  99. }
  100. /*
  101. * Now generate and send e for Diffie-Hellman.
  102. */
  103. seat_set_busy_status(s->ppl.seat, BUSY_CPU);
  104. s->e = dh_create_e(s->dh_ctx, s->nbits * 2);
  105. pktout = ssh_bpp_new_pktout(s->ppl.bpp, s->kex_init_value);
  106. put_mp_ssh2(pktout, s->e);
  107. pq_push(s->ppl.out_pq, pktout);
  108. seat_set_busy_status(s->ppl.seat, BUSY_WAITING);
  109. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  110. if (pktin->type != s->kex_reply_value) {
  111. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  112. "expecting Diffie-Hellman reply, type %d (%s)",
  113. pktin->type,
  114. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  115. s->ppl.bpp->pls->actx,
  116. pktin->type));
  117. *aborted = true;
  118. return;
  119. }
  120. seat_set_busy_status(s->ppl.seat, BUSY_CPU);
  121. s->hostkeydata = get_string(pktin);
  122. s->hkey = ssh_key_new_pub(s->hostkey_alg, s->hostkeydata);
  123. s->f = get_mp_ssh2(pktin);
  124. s->sigdata = get_string(pktin);
  125. if (get_err(pktin)) {
  126. ssh_proto_error(s->ppl.ssh,
  127. "Unable to parse Diffie-Hellman reply packet");
  128. *aborted = true;
  129. return;
  130. }
  131. {
  132. const char *err = dh_validate_f(s->dh_ctx, s->f);
  133. if (err) {
  134. ssh_proto_error(s->ppl.ssh, "Diffie-Hellman reply failed "
  135. "validation: %s", err);
  136. *aborted = true;
  137. return;
  138. }
  139. }
  140. s->K = dh_find_K(s->dh_ctx, s->f);
  141. /* We assume everything from now on will be quick, and it might
  142. * involve user interaction. */
  143. seat_set_busy_status(s->ppl.seat, BUSY_NOT);
  144. put_stringpl(s->exhash, s->hostkeydata);
  145. if (dh_is_gex(s->kex_alg)) {
  146. if (!(s->ppl.remote_bugs & BUG_SSH2_OLDGEX))
  147. put_uint32(s->exhash, DH_MIN_SIZE);
  148. put_uint32(s->exhash, s->pbits);
  149. if (!(s->ppl.remote_bugs & BUG_SSH2_OLDGEX))
  150. put_uint32(s->exhash, DH_MAX_SIZE);
  151. put_mp_ssh2(s->exhash, s->p);
  152. put_mp_ssh2(s->exhash, s->g);
  153. }
  154. put_mp_ssh2(s->exhash, s->e);
  155. put_mp_ssh2(s->exhash, s->f);
  156. dh_cleanup(s->dh_ctx);
  157. s->dh_ctx = NULL;
  158. mp_free(s->f); s->f = NULL;
  159. if (dh_is_gex(s->kex_alg)) {
  160. mp_free(s->g); s->g = NULL;
  161. mp_free(s->p); s->p = NULL;
  162. }
  163. } else if (s->kex_alg->main_type == KEXTYPE_ECDH) {
  164. ppl_logevent("Doing ECDH key exchange with curve %s and hash %s",
  165. ssh_ecdhkex_curve_textname(s->kex_alg),
  166. ssh_hash_alg(s->exhash)->text_name);
  167. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_ECDHKEX;
  168. s->ecdh_key = ssh_ecdhkex_newkey(s->kex_alg);
  169. if (!s->ecdh_key) {
  170. ssh_sw_abort(s->ppl.ssh, "Unable to generate key for ECDH");
  171. *aborted = true;
  172. return;
  173. }
  174. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEX_ECDH_INIT);
  175. {
  176. strbuf *pubpoint = strbuf_new();
  177. ssh_ecdhkex_getpublic(s->ecdh_key, BinarySink_UPCAST(pubpoint));
  178. put_stringsb(pktout, pubpoint);
  179. }
  180. pq_push(s->ppl.out_pq, pktout);
  181. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  182. if (pktin->type != SSH2_MSG_KEX_ECDH_REPLY) {
  183. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  184. "expecting ECDH reply, type %d (%s)", pktin->type,
  185. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  186. s->ppl.bpp->pls->actx,
  187. pktin->type));
  188. *aborted = true;
  189. return;
  190. }
  191. s->hostkeydata = get_string(pktin);
  192. put_stringpl(s->exhash, s->hostkeydata);
  193. s->hkey = ssh_key_new_pub(s->hostkey_alg, s->hostkeydata);
  194. {
  195. strbuf *pubpoint = strbuf_new();
  196. ssh_ecdhkex_getpublic(s->ecdh_key, BinarySink_UPCAST(pubpoint));
  197. put_string(s->exhash, pubpoint->u, pubpoint->len);
  198. strbuf_free(pubpoint);
  199. }
  200. {
  201. ptrlen keydata = get_string(pktin);
  202. put_stringpl(s->exhash, keydata);
  203. s->K = ssh_ecdhkex_getkey(s->ecdh_key, keydata);
  204. if (!get_err(pktin) && !s->K) {
  205. ssh_proto_error(s->ppl.ssh, "Received invalid elliptic curve "
  206. "point in ECDH reply");
  207. *aborted = true;
  208. return;
  209. }
  210. }
  211. s->sigdata = get_string(pktin);
  212. if (get_err(pktin)) {
  213. ssh_proto_error(s->ppl.ssh, "Unable to parse ECDH reply packet");
  214. *aborted = true;
  215. return;
  216. }
  217. ssh_ecdhkex_freekey(s->ecdh_key);
  218. s->ecdh_key = NULL;
  219. #ifndef NO_GSSAPI
  220. } else if (s->kex_alg->main_type == KEXTYPE_GSS) {
  221. ptrlen data;
  222. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_GSSKEX;
  223. s->init_token_sent = false;
  224. s->complete_rcvd = false;
  225. s->hkey = NULL;
  226. s->fingerprint = NULL;
  227. s->keystr = NULL;
  228. /*
  229. * Work out the number of bits of key we will need from the
  230. * key exchange. We start with the maximum key length of
  231. * either cipher...
  232. *
  233. * This is rote from the KEXTYPE_DH section above.
  234. */
  235. {
  236. int csbits, scbits;
  237. csbits = s->out.cipher->real_keybits;
  238. scbits = s->in.cipher->real_keybits;
  239. s->nbits = (csbits > scbits ? csbits : scbits);
  240. }
  241. /* The keys only have hlen-bit entropy, since they're based on
  242. * a hash. So cap the key size at hlen bits. */
  243. if (s->nbits > s->kex_alg->hash->hlen * 8)
  244. s->nbits = s->kex_alg->hash->hlen * 8;
  245. if (dh_is_gex(s->kex_alg)) {
  246. /*
  247. * Work out how big a DH group we will need to allow that
  248. * much data.
  249. */
  250. s->pbits = 512 << ((s->nbits - 1) / 64);
  251. ppl_logevent("Doing GSSAPI (with Kerberos V5) Diffie-Hellman "
  252. "group exchange, with minimum %d bits", s->pbits);
  253. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEXGSS_GROUPREQ);
  254. put_uint32(pktout, s->pbits); /* min */
  255. put_uint32(pktout, s->pbits); /* preferred */
  256. put_uint32(pktout, s->pbits * 2); /* max */
  257. pq_push(s->ppl.out_pq, pktout);
  258. crMaybeWaitUntilV(
  259. (pktin = ssh2_transport_pop(s)) != NULL);
  260. if (pktin->type != SSH2_MSG_KEXGSS_GROUP) {
  261. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  262. "expecting Diffie-Hellman group, type %d (%s)",
  263. pktin->type,
  264. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  265. s->ppl.bpp->pls->actx,
  266. pktin->type));
  267. *aborted = true;
  268. return;
  269. }
  270. s->p = get_mp_ssh2(pktin);
  271. s->g = get_mp_ssh2(pktin);
  272. if (get_err(pktin)) {
  273. ssh_proto_error(s->ppl.ssh,
  274. "Unable to parse Diffie-Hellman group packet");
  275. *aborted = true;
  276. return;
  277. }
  278. s->dh_ctx = dh_setup_gex(s->p, s->g);
  279. } else {
  280. s->dh_ctx = dh_setup_group(s->kex_alg);
  281. ppl_logevent("Using GSSAPI (with Kerberos V5) Diffie-Hellman with"
  282. " standard group \"%s\"", s->kex_alg->groupname);
  283. }
  284. ppl_logevent("Doing GSSAPI (with Kerberos V5) Diffie-Hellman key "
  285. "exchange with hash %s", ssh_hash_alg(s->exhash)->text_name);
  286. /* Now generate e for Diffie-Hellman. */
  287. seat_set_busy_status(s->ppl.seat, BUSY_CPU);
  288. s->e = dh_create_e(s->dh_ctx, s->nbits * 2);
  289. if (s->shgss->lib->gsslogmsg)
  290. ppl_logevent("%s", s->shgss->lib->gsslogmsg);
  291. /* initial tokens are empty */
  292. SSH_GSS_CLEAR_BUF(&s->gss_rcvtok);
  293. SSH_GSS_CLEAR_BUF(&s->gss_sndtok);
  294. SSH_GSS_CLEAR_BUF(&s->mic);
  295. s->gss_stat = s->shgss->lib->acquire_cred(
  296. s->shgss->lib, &s->shgss->ctx, &s->gss_cred_expiry);
  297. if (s->gss_stat != SSH_GSS_OK) {
  298. ssh_sw_abort(s->ppl.ssh,
  299. "GSSAPI key exchange failed to initialise");
  300. *aborted = true;
  301. return;
  302. }
  303. /* now enter the loop */
  304. assert(s->shgss->srv_name);
  305. do {
  306. /*
  307. * When acquire_cred yields no useful expiration, go with the
  308. * service ticket expiration.
  309. */
  310. s->gss_stat = s->shgss->lib->init_sec_context(
  311. s->shgss->lib, &s->shgss->ctx, s->shgss->srv_name,
  312. s->gss_delegate, &s->gss_rcvtok, &s->gss_sndtok,
  313. (s->gss_cred_expiry == GSS_NO_EXPIRATION ?
  314. &s->gss_cred_expiry : NULL), NULL);
  315. SSH_GSS_CLEAR_BUF(&s->gss_rcvtok);
  316. if (s->gss_stat == SSH_GSS_S_COMPLETE && s->complete_rcvd)
  317. break; /* MIC is verified after the loop */
  318. if (s->gss_stat != SSH_GSS_S_COMPLETE &&
  319. s->gss_stat != SSH_GSS_S_CONTINUE_NEEDED) {
  320. if (s->shgss->lib->display_status(
  321. s->shgss->lib, s->shgss->ctx,
  322. &s->gss_buf) == SSH_GSS_OK) {
  323. char *err = s->gss_buf.value;
  324. ssh_sw_abort(s->ppl.ssh,
  325. "GSSAPI key exchange failed to initialise "
  326. "context: %s", err);
  327. sfree(err);
  328. *aborted = true;
  329. return;
  330. }
  331. }
  332. assert(s->gss_stat == SSH_GSS_S_COMPLETE ||
  333. s->gss_stat == SSH_GSS_S_CONTINUE_NEEDED);
  334. if (!s->init_token_sent) {
  335. s->init_token_sent = true;
  336. pktout = ssh_bpp_new_pktout(s->ppl.bpp,
  337. SSH2_MSG_KEXGSS_INIT);
  338. if (s->gss_sndtok.length == 0) {
  339. ssh_sw_abort(s->ppl.ssh, "GSSAPI key exchange failed: "
  340. "no initial context token");
  341. *aborted = true;
  342. return;
  343. }
  344. put_string(pktout,
  345. s->gss_sndtok.value, s->gss_sndtok.length);
  346. put_mp_ssh2(pktout, s->e);
  347. pq_push(s->ppl.out_pq, pktout);
  348. s->shgss->lib->free_tok(s->shgss->lib, &s->gss_sndtok);
  349. ppl_logevent("GSSAPI key exchange initialised");
  350. } else if (s->gss_sndtok.length != 0) {
  351. pktout = ssh_bpp_new_pktout(
  352. s->ppl.bpp, SSH2_MSG_KEXGSS_CONTINUE);
  353. put_string(pktout,
  354. s->gss_sndtok.value, s->gss_sndtok.length);
  355. pq_push(s->ppl.out_pq, pktout);
  356. s->shgss->lib->free_tok(s->shgss->lib, &s->gss_sndtok);
  357. }
  358. if (s->gss_stat == SSH_GSS_S_COMPLETE && s->complete_rcvd)
  359. break;
  360. wait_for_gss_token:
  361. crMaybeWaitUntilV(
  362. (pktin = ssh2_transport_pop(s)) != NULL);
  363. switch (pktin->type) {
  364. case SSH2_MSG_KEXGSS_CONTINUE:
  365. data = get_string(pktin);
  366. s->gss_rcvtok.value = (char *)data.ptr;
  367. s->gss_rcvtok.length = data.len;
  368. continue;
  369. case SSH2_MSG_KEXGSS_COMPLETE:
  370. s->complete_rcvd = true;
  371. s->f = get_mp_ssh2(pktin);
  372. data = get_string(pktin);
  373. s->mic.value = (char *)data.ptr;
  374. s->mic.length = data.len;
  375. /* Save expiration time of cred when delegating */
  376. if (s->gss_delegate && s->gss_cred_expiry != GSS_NO_EXPIRATION)
  377. s->gss_cred_expiry = s->gss_cred_expiry;
  378. /* If there's a final token we loop to consume it */
  379. if (get_bool(pktin)) {
  380. data = get_string(pktin);
  381. s->gss_rcvtok.value = (char *)data.ptr;
  382. s->gss_rcvtok.length = data.len;
  383. continue;
  384. }
  385. break;
  386. case SSH2_MSG_KEXGSS_HOSTKEY:
  387. s->hostkeydata = get_string(pktin);
  388. if (s->hostkey_alg) {
  389. s->hkey = ssh_key_new_pub(s->hostkey_alg,
  390. s->hostkeydata);
  391. put_stringpl(s->exhash, s->hostkeydata);
  392. }
  393. /*
  394. * Can't loop as we have no token to pass to
  395. * init_sec_context.
  396. */
  397. goto wait_for_gss_token;
  398. case SSH2_MSG_KEXGSS_ERROR:
  399. /*
  400. * We have no use for the server's major and minor
  401. * status. The minor status is really only
  402. * meaningful to the server, and with luck the major
  403. * status means something to us (but not really all
  404. * that much). The string is more meaningful, and
  405. * hopefully the server sends any error tokens, as
  406. * that will produce the most useful information for
  407. * us.
  408. */
  409. get_uint32(pktin); /* server's major status */
  410. get_uint32(pktin); /* server's minor status */
  411. data = get_string(pktin);
  412. ppl_logevent("GSSAPI key exchange failed; "
  413. "server's message: %.*s", PTRLEN_PRINTF(data));
  414. /* Language tag, but we have no use for it */
  415. get_string(pktin);
  416. /*
  417. * Wait for an error token, if there is one, or the
  418. * server's disconnect. The error token, if there
  419. * is one, must follow the SSH2_MSG_KEXGSS_ERROR
  420. * message, per the RFC.
  421. */
  422. goto wait_for_gss_token;
  423. default:
  424. ssh_proto_error(s->ppl.ssh, "Received unexpected packet "
  425. "during GSSAPI key exchange, type %d (%s)",
  426. pktin->type,
  427. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  428. s->ppl.bpp->pls->actx,
  429. pktin->type));
  430. *aborted = true;
  431. return;
  432. }
  433. } while (s->gss_rcvtok.length ||
  434. s->gss_stat == SSH_GSS_S_CONTINUE_NEEDED ||
  435. !s->complete_rcvd);
  436. {
  437. const char *err = dh_validate_f(s->dh_ctx, s->f);
  438. if (err) {
  439. ssh_proto_error(s->ppl.ssh, "GSSAPI reply failed "
  440. "validation: %s", err);
  441. *aborted = true;
  442. return;
  443. }
  444. }
  445. s->K = dh_find_K(s->dh_ctx, s->f);
  446. /* We assume everything from now on will be quick, and it might
  447. * involve user interaction. */
  448. seat_set_busy_status(s->ppl.seat, BUSY_NOT);
  449. if (!s->hkey)
  450. put_stringz(s->exhash, "");
  451. if (dh_is_gex(s->kex_alg)) {
  452. /* min, preferred, max */
  453. put_uint32(s->exhash, s->pbits);
  454. put_uint32(s->exhash, s->pbits);
  455. put_uint32(s->exhash, s->pbits * 2);
  456. put_mp_ssh2(s->exhash, s->p);
  457. put_mp_ssh2(s->exhash, s->g);
  458. }
  459. put_mp_ssh2(s->exhash, s->e);
  460. put_mp_ssh2(s->exhash, s->f);
  461. /*
  462. * MIC verification is done below, after we compute the hash
  463. * used as the MIC input.
  464. */
  465. dh_cleanup(s->dh_ctx);
  466. s->dh_ctx = NULL;
  467. mp_free(s->f); s->f = NULL;
  468. if (dh_is_gex(s->kex_alg)) {
  469. mp_free(s->g); s->g = NULL;
  470. mp_free(s->p); s->p = NULL;
  471. }
  472. #endif
  473. } else {
  474. ptrlen rsakeydata;
  475. assert(s->kex_alg->main_type == KEXTYPE_RSA);
  476. ppl_logevent("Doing RSA key exchange with hash %s",
  477. ssh_hash_alg(s->exhash)->text_name);
  478. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_RSAKEX;
  479. /*
  480. * RSA key exchange. First expect a KEXRSA_PUBKEY packet
  481. * from the server.
  482. */
  483. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  484. if (pktin->type != SSH2_MSG_KEXRSA_PUBKEY) {
  485. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  486. "expecting RSA public key, type %d (%s)",
  487. pktin->type,
  488. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  489. s->ppl.bpp->pls->actx,
  490. pktin->type));
  491. *aborted = true;
  492. return;
  493. }
  494. s->hostkeydata = get_string(pktin);
  495. put_stringpl(s->exhash, s->hostkeydata);
  496. s->hkey = ssh_key_new_pub(s->hostkey_alg, s->hostkeydata);
  497. rsakeydata = get_string(pktin);
  498. s->rsa_kex_key = ssh_rsakex_newkey(rsakeydata);
  499. if (!s->rsa_kex_key) {
  500. ssh_proto_error(s->ppl.ssh,
  501. "Unable to parse RSA public key packet");
  502. *aborted = true;
  503. return;
  504. }
  505. put_stringpl(s->exhash, rsakeydata);
  506. /*
  507. * Next, set up a shared secret K, of precisely KLEN -
  508. * 2*HLEN - 49 bits, where KLEN is the bit length of the
  509. * RSA key modulus and HLEN is the bit length of the hash
  510. * we're using.
  511. */
  512. {
  513. int klen = ssh_rsakex_klen(s->rsa_kex_key);
  514. const struct ssh_rsa_kex_extra *extra =
  515. (const struct ssh_rsa_kex_extra *)s->kex_alg->extra;
  516. if (klen < extra->minklen) {
  517. ssh_proto_error(s->ppl.ssh, "Server sent %d-bit RSA key, "
  518. "less than the minimum size %d for %s "
  519. "key exchange", klen, extra->minklen,
  520. s->kex_alg->name);
  521. *aborted = true;
  522. return;
  523. }
  524. { // WINSCP
  525. int nbits = klen - (2*s->kex_alg->hash->hlen*8 + 49);
  526. assert(nbits > 0);
  527. { // WINSCP
  528. strbuf *buf, *outstr;
  529. mp_int *tmp = mp_random_bits(nbits - 1);
  530. s->K = mp_power_2(nbits - 1);
  531. mp_add_into(s->K, s->K, tmp);
  532. mp_free(tmp);
  533. /*
  534. * Encode this as an mpint.
  535. */
  536. buf = strbuf_new_nm();
  537. put_mp_ssh2(buf, s->K);
  538. /*
  539. * Encrypt it with the given RSA key.
  540. */
  541. outstr = ssh_rsakex_encrypt(s->rsa_kex_key, s->kex_alg->hash,
  542. ptrlen_from_strbuf(buf));
  543. /*
  544. * And send it off in a return packet.
  545. */
  546. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEXRSA_SECRET);
  547. put_stringpl(pktout, ptrlen_from_strbuf(outstr));
  548. pq_push(s->ppl.out_pq, pktout);
  549. put_stringsb(s->exhash, outstr); /* frees outstr */
  550. strbuf_free(buf);
  551. } // WINSCP
  552. } // WINSCP
  553. }
  554. ssh_rsakex_freekey(s->rsa_kex_key);
  555. s->rsa_kex_key = NULL;
  556. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  557. if (pktin->type != SSH2_MSG_KEXRSA_DONE) {
  558. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  559. "expecting RSA kex signature, type %d (%s)",
  560. pktin->type,
  561. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  562. s->ppl.bpp->pls->actx,
  563. pktin->type));
  564. *aborted = true;
  565. return;
  566. }
  567. s->sigdata = get_string(pktin);
  568. if (get_err(pktin)) {
  569. ssh_proto_error(s->ppl.ssh, "Unable to parse RSA kex signature");
  570. *aborted = true;
  571. return;
  572. }
  573. }
  574. ssh2transport_finalise_exhash(s);
  575. #ifndef NO_GSSAPI
  576. if (s->kex_alg->main_type == KEXTYPE_GSS) {
  577. Ssh_gss_buf gss_buf;
  578. SSH_GSS_CLEAR_BUF(&s->gss_buf);
  579. gss_buf.value = s->exchange_hash;
  580. gss_buf.length = s->kex_alg->hash->hlen;
  581. s->gss_stat = s->shgss->lib->verify_mic(
  582. s->shgss->lib, s->shgss->ctx, &gss_buf, &s->mic);
  583. if (s->gss_stat != SSH_GSS_OK) {
  584. if (s->shgss->lib->display_status(
  585. s->shgss->lib, s->shgss->ctx, &s->gss_buf) == SSH_GSS_OK) {
  586. char *err = s->gss_buf.value;
  587. ssh_sw_abort(s->ppl.ssh, "GSSAPI key exchange MIC was "
  588. "not valid: %s", err);
  589. sfree(err);
  590. } else {
  591. ssh_sw_abort(s->ppl.ssh, "GSSAPI key exchange MIC was "
  592. "not valid");
  593. }
  594. *aborted = true;
  595. return;
  596. }
  597. s->gss_kex_used = true;
  598. /*-
  599. * If this the first KEX, save the GSS context for "gssapi-keyex"
  600. * authentication.
  601. *
  602. * http://tools.ietf.org/html/rfc4462#section-4
  603. *
  604. * This method may be used only if the initial key exchange was
  605. * performed using a GSS-API-based key exchange method defined in
  606. * accordance with Section 2. The GSS-API context used with this
  607. * method is always that established during an initial GSS-API-based
  608. * key exchange. Any context established during key exchange for the
  609. * purpose of rekeying MUST NOT be used with this method.
  610. */
  611. if (s->got_session_id) {
  612. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  613. }
  614. ppl_logevent("GSSAPI Key Exchange complete!");
  615. }
  616. #endif
  617. s->dh_ctx = NULL;
  618. /* In GSS keyex there's no hostkey signature to verify */
  619. if (s->kex_alg->main_type != KEXTYPE_GSS) {
  620. if (!s->hkey) {
  621. ssh_proto_error(s->ppl.ssh, "Server's host key is invalid");
  622. *aborted = true;
  623. return;
  624. }
  625. if (!ssh_key_verify(
  626. s->hkey, s->sigdata,
  627. make_ptrlen(s->exchange_hash, s->kex_alg->hash->hlen))) {
  628. #ifndef FUZZING
  629. ssh_proto_error(s->ppl.ssh, "Signature from server's host key "
  630. "is invalid");
  631. *aborted = true;
  632. return;
  633. #endif
  634. }
  635. }
  636. s->keystr = (s->hkey ? ssh_key_cache_str(s->hkey) : NULL);
  637. #ifndef NO_GSSAPI
  638. if (s->gss_kex_used) {
  639. /*
  640. * In a GSS-based session, check the host key (if any) against
  641. * the transient host key cache.
  642. */
  643. if (s->kex_alg->main_type == KEXTYPE_GSS) {
  644. /*
  645. * We've just done a GSS key exchange. If it gave us a
  646. * host key, store it.
  647. */
  648. if (s->hkey) {
  649. s->fingerprint = ssh2_fingerprint(s->hkey);
  650. ppl_logevent("GSS kex provided fallback host key:");
  651. ppl_logevent("%s", s->fingerprint);
  652. sfree(s->fingerprint);
  653. s->fingerprint = NULL;
  654. ssh_transient_hostkey_cache_add(s->thc, s->hkey);
  655. } else if (!ssh_transient_hostkey_cache_non_empty(s->thc)) {
  656. /*
  657. * But if it didn't, then we currently have no
  658. * fallback host key to use in subsequent non-GSS
  659. * rekeys. So we should immediately trigger a non-GSS
  660. * rekey of our own, to set one up, before the session
  661. * keys have been used for anything else.
  662. *
  663. * This is similar to the cross-certification done at
  664. * user request in the permanent host key cache, but
  665. * here we do it automatically, once, at session
  666. * startup, and only add the key to the transient
  667. * cache.
  668. */
  669. if (s->hostkey_alg) {
  670. s->need_gss_transient_hostkey = true;
  671. } else {
  672. /*
  673. * If we negotiated the "null" host key algorithm
  674. * in the key exchange, that's an indication that
  675. * no host key at all is available from the server
  676. * (both because we listed "null" last, and
  677. * because RFC 4462 section 5 says that a server
  678. * MUST NOT offer "null" as a host key algorithm
  679. * unless that is the only algorithm it provides
  680. * at all).
  681. *
  682. * In that case we actually _can't_ perform a
  683. * non-GSSAPI key exchange, so it's pointless to
  684. * attempt one proactively. This is also likely to
  685. * cause trouble later if a rekey is required at a
  686. * moment whne GSS credentials are not available,
  687. * but someone setting up a server in this
  688. * configuration presumably accepts that as a
  689. * consequence.
  690. */
  691. if (!s->warned_about_no_gss_transient_hostkey) {
  692. ppl_logevent("No fallback host key available");
  693. s->warned_about_no_gss_transient_hostkey = true;
  694. }
  695. }
  696. }
  697. } else {
  698. /*
  699. * We've just done a fallback key exchange, so make
  700. * sure the host key it used is in the cache of keys
  701. * we previously received in GSS kexes.
  702. *
  703. * An exception is if this was the non-GSS key exchange we
  704. * triggered on purpose to populate the transient cache.
  705. */
  706. assert(s->hkey); /* only KEXTYPE_GSS lets this be null */
  707. s->fingerprint = ssh2_fingerprint(s->hkey);
  708. if (s->need_gss_transient_hostkey) {
  709. ppl_logevent("Post-GSS rekey provided fallback host key:");
  710. ppl_logevent("%s", s->fingerprint);
  711. ssh_transient_hostkey_cache_add(s->thc, s->hkey);
  712. s->need_gss_transient_hostkey = false;
  713. } else if (!ssh_transient_hostkey_cache_verify(s->thc, s->hkey)) {
  714. ppl_logevent("Non-GSS rekey after initial GSS kex "
  715. "used host key:");
  716. ppl_logevent("%s", s->fingerprint);
  717. ssh_sw_abort(s->ppl.ssh, "Server's host key did not match any "
  718. "used in previous GSS kex");
  719. *aborted = true;
  720. return;
  721. }
  722. sfree(s->fingerprint);
  723. s->fingerprint = NULL;
  724. }
  725. } else
  726. #endif /* NO_GSSAPI */
  727. if (!s->got_session_id) {
  728. /*
  729. * Make a note of any other host key formats that are available.
  730. */
  731. {
  732. int i, j, nkeys = 0;
  733. char *list = NULL;
  734. for (i = 0; i < lenof(ssh2_hostkey_algs); i++) {
  735. if (ssh2_hostkey_algs[i].alg == s->hostkey_alg)
  736. continue;
  737. for (j = 0; j < s->n_uncert_hostkeys; j++)
  738. if (s->uncert_hostkeys[j] == i)
  739. break;
  740. if (j < s->n_uncert_hostkeys) {
  741. char *newlist;
  742. if (list)
  743. newlist = dupprintf(
  744. "%s/%s", list,
  745. ssh2_hostkey_algs[i].alg->ssh_id);
  746. else
  747. newlist = dupprintf(
  748. "%s", ssh2_hostkey_algs[i].alg->ssh_id);
  749. sfree(list);
  750. list = newlist;
  751. nkeys++;
  752. }
  753. }
  754. if (list) {
  755. ppl_logevent("Server also has %s host key%s, but we "
  756. "don't know %s", list,
  757. nkeys > 1 ? "s" : "",
  758. nkeys > 1 ? "any of them" : "it");
  759. sfree(list);
  760. }
  761. }
  762. /*
  763. * Authenticate remote host: verify host key. (We've already
  764. * checked the signature of the exchange hash.)
  765. */
  766. s->fingerprint = ssh2_fingerprint(s->hkey);
  767. ppl_logevent("Host key fingerprint is:");
  768. ppl_logevent("%s", s->fingerprint);
  769. /* First check against manually configured host keys. */
  770. s->dlgret = verify_ssh_manual_host_key(
  771. s->conf, s->fingerprint, s->hkey);
  772. if (s->dlgret == 0) { /* did not match */
  773. ssh_sw_abort(s->ppl.ssh, "Host key did not appear in manually "
  774. "configured list");
  775. *aborted = true;
  776. return;
  777. } else if (s->dlgret < 0) { /* none configured; use standard handling */
  778. s->dlgret = seat_verify_ssh_host_key(
  779. s->ppl.seat, s->savedhost, s->savedport,
  780. ssh_key_cache_id(s->hkey), s->keystr, s->fingerprint,
  781. ssh2_transport_dialog_callback, s);
  782. #ifdef FUZZING
  783. s->dlgret = 1;
  784. #endif
  785. crMaybeWaitUntilV(s->dlgret >= 0);
  786. if (s->dlgret == 0) {
  787. ssh_user_close(s->ppl.ssh,
  788. "User aborted at host key verification");
  789. *aborted = true;
  790. return;
  791. }
  792. }
  793. sfree(s->fingerprint);
  794. s->fingerprint = NULL;
  795. /*
  796. * Save this host key, to check against the one presented in
  797. * subsequent rekeys.
  798. */
  799. s->hostkey_str = s->keystr;
  800. s->keystr = NULL;
  801. } else if (s->cross_certifying) {
  802. assert(s->hkey);
  803. assert(ssh_key_alg(s->hkey) == s->cross_certifying);
  804. s->fingerprint = ssh2_fingerprint(s->hkey);
  805. ppl_logevent("Storing additional host key for this host:");
  806. ppl_logevent("%s", s->fingerprint);
  807. sfree(s->fingerprint);
  808. s->fingerprint = NULL;
  809. store_host_key(s->savedhost, s->savedport,
  810. ssh_key_cache_id(s->hkey), s->keystr);
  811. /*
  812. * Don't forget to store the new key as the one we'll be
  813. * re-checking in future normal rekeys.
  814. */
  815. s->hostkey_str = s->keystr;
  816. s->keystr = NULL;
  817. } else {
  818. /*
  819. * In a rekey, we never present an interactive host key
  820. * verification request to the user. Instead, we simply
  821. * enforce that the key we're seeing this time is identical to
  822. * the one we saw before.
  823. */
  824. assert(s->keystr); /* filled in by prior key exchange */
  825. if (strcmp(s->hostkey_str, s->keystr)) {
  826. #ifndef FUZZING
  827. ssh_sw_abort(s->ppl.ssh,
  828. "Host key was different in repeat key exchange");
  829. *aborted = true;
  830. return;
  831. #endif
  832. }
  833. }
  834. sfree(s->keystr);
  835. s->keystr = NULL;
  836. if (s->hkey) {
  837. ssh_key_free(s->hkey);
  838. s->hkey = NULL;
  839. }
  840. crFinishV;
  841. }