ssh2kex-client.c 37 KB

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