ssh2kex-client.c 35 KB

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