ssh2kex-client.c 35 KB

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