kex2-client.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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 "bpp.h"
  8. #include "ppl.h"
  9. #include "sshcr.h"
  10. #include "storage.h"
  11. #include "transport2.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);
  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. mp_int *K = dh_find_K(s->dh_ctx, s->f);
  146. put_mp_ssh2(s->kex_shared_secret, K);
  147. mp_free(K);
  148. /* We assume everything from now on will be quick, and it might
  149. * involve user interaction. */
  150. seat_set_busy_status(s->ppl.seat, BUSY_NOT);
  151. put_stringpl(s->exhash, s->hostkeydata);
  152. if (dh_is_gex(s->kex_alg)) {
  153. if (!(s->ppl.remote_bugs & BUG_SSH2_OLDGEX))
  154. put_uint32(s->exhash, DH_MIN_SIZE);
  155. put_uint32(s->exhash, s->pbits);
  156. if (!(s->ppl.remote_bugs & BUG_SSH2_OLDGEX))
  157. put_uint32(s->exhash, DH_MAX_SIZE);
  158. put_mp_ssh2(s->exhash, s->p);
  159. put_mp_ssh2(s->exhash, s->g);
  160. }
  161. put_mp_ssh2(s->exhash, s->e);
  162. put_mp_ssh2(s->exhash, s->f);
  163. dh_cleanup(s->dh_ctx);
  164. s->dh_ctx = NULL;
  165. mp_free(s->f); s->f = NULL;
  166. if (dh_is_gex(s->kex_alg)) {
  167. mp_free(s->g); s->g = NULL;
  168. mp_free(s->p); s->p = NULL;
  169. }
  170. } else if (s->kex_alg->main_type == KEXTYPE_ECDH) {
  171. char *desc = ecdh_keyalg_description(s->kex_alg);
  172. ppl_logevent("Doing %s, using hash %s", desc,
  173. ssh_hash_alg(s->exhash)->text_name);
  174. sfree(desc);
  175. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_ECDHKEX;
  176. s->ecdh_key = ecdh_key_new(s->kex_alg, false);
  177. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEX_ECDH_INIT);
  178. {
  179. strbuf *pubpoint = strbuf_new();
  180. ecdh_key_getpublic(s->ecdh_key, BinarySink_UPCAST(pubpoint));
  181. put_stringsb(pktout, pubpoint);
  182. }
  183. pq_push(s->ppl.out_pq, pktout);
  184. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  185. if (pktin->type != SSH2_MSG_KEX_ECDH_REPLY) {
  186. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  187. "expecting ECDH reply, type %d (%s)", pktin->type,
  188. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  189. s->ppl.bpp->pls->actx,
  190. pktin->type));
  191. *aborted = true;
  192. return;
  193. }
  194. s->hostkeydata = get_string(pktin);
  195. put_stringpl(s->exhash, s->hostkeydata);
  196. s->hkey = ssh_key_new_pub(s->hostkey_alg, s->hostkeydata);
  197. {
  198. strbuf *pubpoint = strbuf_new();
  199. ecdh_key_getpublic(s->ecdh_key, BinarySink_UPCAST(pubpoint));
  200. put_string(s->exhash, pubpoint->u, pubpoint->len);
  201. strbuf_free(pubpoint);
  202. }
  203. {
  204. ptrlen keydata = get_string(pktin);
  205. put_stringpl(s->exhash, keydata);
  206. bool ok = ecdh_key_getkey(s->ecdh_key, keydata,
  207. BinarySink_UPCAST(s->kex_shared_secret));
  208. if (!get_err(pktin) && !ok) {
  209. ssh_proto_error(s->ppl.ssh, "Received invalid elliptic curve "
  210. "point in ECDH reply");
  211. *aborted = true;
  212. return;
  213. }
  214. }
  215. s->sigdata = get_string(pktin);
  216. if (get_err(pktin)) {
  217. ssh_proto_error(s->ppl.ssh, "Unable to parse ECDH reply packet");
  218. *aborted = true;
  219. return;
  220. }
  221. ecdh_key_free(s->ecdh_key);
  222. s->ecdh_key = NULL;
  223. #ifndef NO_GSSAPI
  224. } else if (kex_is_gss(s->kex_alg)) {
  225. ptrlen data;
  226. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_GSSKEX;
  227. s->init_token_sent = false;
  228. s->complete_rcvd = false;
  229. s->hkey = NULL;
  230. s->keystr = NULL;
  231. /*
  232. * Work out the number of bits of key we will need from the
  233. * key exchange. We start with the maximum key length of
  234. * either cipher...
  235. *
  236. * This is rote from the KEXTYPE_DH section above.
  237. */
  238. {
  239. int csbits, scbits;
  240. csbits = s->out.cipher->real_keybits;
  241. scbits = s->in.cipher->real_keybits;
  242. s->nbits = (csbits > scbits ? csbits : scbits);
  243. }
  244. /* The keys only have hlen-bit entropy, since they're based on
  245. * a hash. So cap the key size at hlen bits. */
  246. if (s->nbits > s->kex_alg->hash->hlen * 8)
  247. s->nbits = s->kex_alg->hash->hlen * 8;
  248. assert(!s->ecdh_key);
  249. assert(!s->dh_ctx);
  250. if (s->kex_alg->main_type == KEXTYPE_GSS_ECDH) {
  251. s->ecdh_key = ecdh_key_new(s->kex_alg, false);
  252. char *desc = ecdh_keyalg_description(s->kex_alg);
  253. ppl_logevent("Doing GSSAPI (with Kerberos V5) %s with hash %s",
  254. desc, ssh_hash_alg(s->exhash)->text_name);
  255. sfree(desc);
  256. } else if (dh_is_gex(s->kex_alg)) {
  257. /*
  258. * Work out how big a DH group we will need to allow that
  259. * much data.
  260. */
  261. s->pbits = 512 << ((s->nbits - 1) / 64);
  262. ppl_logevent("Doing GSSAPI (with Kerberos V5) Diffie-Hellman "
  263. "group exchange, with minimum %d bits, and hash %s",
  264. s->pbits, ssh_hash_alg(s->exhash)->text_name);
  265. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEXGSS_GROUPREQ);
  266. put_uint32(pktout, s->pbits); /* min */
  267. put_uint32(pktout, s->pbits); /* preferred */
  268. put_uint32(pktout, s->pbits * 2); /* max */
  269. pq_push(s->ppl.out_pq, pktout);
  270. crMaybeWaitUntilV(
  271. (pktin = ssh2_transport_pop(s)) != NULL);
  272. if (pktin->type != SSH2_MSG_KEXGSS_GROUP) {
  273. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  274. "expecting Diffie-Hellman group, type %d (%s)",
  275. pktin->type,
  276. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  277. s->ppl.bpp->pls->actx,
  278. pktin->type));
  279. *aborted = true;
  280. return;
  281. }
  282. s->p = get_mp_ssh2(pktin);
  283. s->g = get_mp_ssh2(pktin);
  284. if (get_err(pktin)) {
  285. ssh_proto_error(s->ppl.ssh,
  286. "Unable to parse Diffie-Hellman group packet");
  287. *aborted = true;
  288. return;
  289. }
  290. s->dh_ctx = dh_setup_gex(s->p, s->g);
  291. } else {
  292. s->dh_ctx = dh_setup_group(s->kex_alg);
  293. ppl_logevent("Using GSSAPI (with Kerberos V5) Diffie-Hellman with"
  294. " standard group \"%s\" and hash %s",
  295. s->kex_alg->groupname,
  296. ssh_hash_alg(s->exhash)->text_name);
  297. }
  298. /* Now generate e for Diffie-Hellman. */
  299. seat_set_busy_status(s->ppl.seat, BUSY_CPU);
  300. if (s->ecdh_key) {
  301. s->ebuf = strbuf_new_nm();
  302. ecdh_key_getpublic(s->ecdh_key, BinarySink_UPCAST(s->ebuf));
  303. } else {
  304. s->e = dh_create_e(s->dh_ctx);
  305. }
  306. if (s->shgss->lib->gsslogmsg)
  307. ppl_logevent("%s", s->shgss->lib->gsslogmsg);
  308. /* initial tokens are empty */
  309. SSH_GSS_CLEAR_BUF(&s->gss_rcvtok);
  310. SSH_GSS_CLEAR_BUF(&s->gss_sndtok);
  311. SSH_GSS_CLEAR_BUF(&s->mic);
  312. s->gss_stat = s->shgss->lib->acquire_cred(
  313. s->shgss->lib, &s->shgss->ctx, &s->gss_cred_expiry);
  314. if (s->gss_stat != SSH_GSS_OK) {
  315. ssh_sw_abort(s->ppl.ssh,
  316. "GSSAPI key exchange failed to initialise");
  317. *aborted = true;
  318. return;
  319. }
  320. /* now enter the loop */
  321. assert(s->shgss->srv_name);
  322. do {
  323. /*
  324. * When acquire_cred yields no useful expiration, go with the
  325. * service ticket expiration.
  326. */
  327. s->gss_stat = s->shgss->lib->init_sec_context(
  328. s->shgss->lib, &s->shgss->ctx, s->shgss->srv_name,
  329. s->gss_delegate, &s->gss_rcvtok, &s->gss_sndtok,
  330. (s->gss_cred_expiry == GSS_NO_EXPIRATION ?
  331. &s->gss_cred_expiry : NULL), NULL);
  332. SSH_GSS_CLEAR_BUF(&s->gss_rcvtok);
  333. if (s->gss_stat == SSH_GSS_S_COMPLETE && s->complete_rcvd)
  334. break; /* MIC is verified after the loop */
  335. if (s->gss_stat != SSH_GSS_S_COMPLETE &&
  336. s->gss_stat != SSH_GSS_S_CONTINUE_NEEDED) {
  337. if (s->shgss->lib->display_status(
  338. s->shgss->lib, s->shgss->ctx,
  339. &s->gss_buf) == SSH_GSS_OK) {
  340. char *err = s->gss_buf.value;
  341. ssh_sw_abort(s->ppl.ssh,
  342. "GSSAPI key exchange failed to initialise "
  343. "context: %s", err);
  344. sfree(err);
  345. *aborted = true;
  346. return;
  347. }
  348. }
  349. assert(s->gss_stat == SSH_GSS_S_COMPLETE ||
  350. s->gss_stat == SSH_GSS_S_CONTINUE_NEEDED);
  351. if (!s->init_token_sent) {
  352. s->init_token_sent = true;
  353. pktout = ssh_bpp_new_pktout(s->ppl.bpp,
  354. SSH2_MSG_KEXGSS_INIT);
  355. if (s->gss_sndtok.length == 0) {
  356. ssh_sw_abort(s->ppl.ssh, "GSSAPI key exchange failed: "
  357. "no initial context token");
  358. *aborted = true;
  359. return;
  360. }
  361. put_string(pktout,
  362. s->gss_sndtok.value, s->gss_sndtok.length);
  363. if (s->ecdh_key) {
  364. put_stringpl(pktout, ptrlen_from_strbuf(s->ebuf));
  365. } else {
  366. put_mp_ssh2(pktout, s->e);
  367. }
  368. pq_push(s->ppl.out_pq, pktout);
  369. s->shgss->lib->free_tok(s->shgss->lib, &s->gss_sndtok);
  370. ppl_logevent("GSSAPI key exchange initialised");
  371. } else if (s->gss_sndtok.length != 0) {
  372. pktout = ssh_bpp_new_pktout(
  373. s->ppl.bpp, SSH2_MSG_KEXGSS_CONTINUE);
  374. put_string(pktout,
  375. s->gss_sndtok.value, s->gss_sndtok.length);
  376. pq_push(s->ppl.out_pq, pktout);
  377. s->shgss->lib->free_tok(s->shgss->lib, &s->gss_sndtok);
  378. }
  379. if (s->gss_stat == SSH_GSS_S_COMPLETE && s->complete_rcvd)
  380. break;
  381. wait_for_gss_token:
  382. crMaybeWaitUntilV(
  383. (pktin = ssh2_transport_pop(s)) != NULL);
  384. switch (pktin->type) {
  385. case SSH2_MSG_KEXGSS_CONTINUE:
  386. data = get_string(pktin);
  387. s->gss_rcvtok.value = (char *)data.ptr;
  388. s->gss_rcvtok.length = data.len;
  389. continue;
  390. case SSH2_MSG_KEXGSS_COMPLETE:
  391. s->complete_rcvd = true;
  392. if (s->ecdh_key) {
  393. s->fbuf = strbuf_dup_nm(get_string(pktin));
  394. } else {
  395. s->f = get_mp_ssh2(pktin);
  396. }
  397. data = get_string(pktin);
  398. s->mic.value = (char *)data.ptr;
  399. s->mic.length = data.len;
  400. /* If there's a final token we loop to consume it */
  401. if (get_bool(pktin)) {
  402. data = get_string(pktin);
  403. s->gss_rcvtok.value = (char *)data.ptr;
  404. s->gss_rcvtok.length = data.len;
  405. continue;
  406. }
  407. break;
  408. case SSH2_MSG_KEXGSS_HOSTKEY:
  409. s->hostkeydata = get_string(pktin);
  410. if (s->hostkey_alg) {
  411. s->hkey = ssh_key_new_pub(s->hostkey_alg,
  412. s->hostkeydata);
  413. put_stringpl(s->exhash, s->hostkeydata);
  414. }
  415. /*
  416. * Can't loop as we have no token to pass to
  417. * init_sec_context.
  418. */
  419. goto wait_for_gss_token;
  420. case SSH2_MSG_KEXGSS_ERROR:
  421. /*
  422. * We have no use for the server's major and minor
  423. * status. The minor status is really only
  424. * meaningful to the server, and with luck the major
  425. * status means something to us (but not really all
  426. * that much). The string is more meaningful, and
  427. * hopefully the server sends any error tokens, as
  428. * that will produce the most useful information for
  429. * us.
  430. */
  431. get_uint32(pktin); /* server's major status */
  432. get_uint32(pktin); /* server's minor status */
  433. data = get_string(pktin);
  434. ppl_logevent("GSSAPI key exchange failed; "
  435. "server's message: %.*s", PTRLEN_PRINTF(data));
  436. /* Language tag, but we have no use for it */
  437. get_string(pktin);
  438. /*
  439. * Wait for an error token, if there is one, or the
  440. * server's disconnect. The error token, if there
  441. * is one, must follow the SSH2_MSG_KEXGSS_ERROR
  442. * message, per the RFC.
  443. */
  444. goto wait_for_gss_token;
  445. default:
  446. ssh_proto_error(s->ppl.ssh, "Received unexpected packet "
  447. "during GSSAPI key exchange, type %d (%s)",
  448. pktin->type,
  449. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  450. s->ppl.bpp->pls->actx,
  451. pktin->type));
  452. *aborted = true;
  453. return;
  454. }
  455. } while (s->gss_rcvtok.length ||
  456. s->gss_stat == SSH_GSS_S_CONTINUE_NEEDED ||
  457. !s->complete_rcvd);
  458. if (s->ecdh_key) {
  459. bool ok = ecdh_key_getkey(s->ecdh_key, ptrlen_from_strbuf(s->fbuf),
  460. BinarySink_UPCAST(s->kex_shared_secret));
  461. if (!ok) {
  462. ssh_proto_error(s->ppl.ssh, "Received invalid elliptic curve "
  463. "point in GSSAPI ECDH reply");
  464. *aborted = true;
  465. return;
  466. }
  467. } else {
  468. const char *err = dh_validate_f(s->dh_ctx, s->f);
  469. if (err) {
  470. ssh_proto_error(s->ppl.ssh, "GSSAPI reply failed "
  471. "validation: %s", err);
  472. *aborted = true;
  473. return;
  474. }
  475. mp_int *K = dh_find_K(s->dh_ctx, s->f);
  476. put_mp_ssh2(s->kex_shared_secret, K);
  477. mp_free(K);
  478. }
  479. /* We assume everything from now on will be quick, and it might
  480. * involve user interaction. */
  481. seat_set_busy_status(s->ppl.seat, BUSY_NOT);
  482. if (!s->hkey)
  483. put_stringz(s->exhash, "");
  484. if (s->ecdh_key) {
  485. put_stringpl(s->exhash, ptrlen_from_strbuf(s->ebuf));
  486. put_stringpl(s->exhash, ptrlen_from_strbuf(s->fbuf));
  487. } else {
  488. if (dh_is_gex(s->kex_alg)) {
  489. /* min, preferred, max */
  490. put_uint32(s->exhash, s->pbits);
  491. put_uint32(s->exhash, s->pbits);
  492. put_uint32(s->exhash, s->pbits * 2);
  493. put_mp_ssh2(s->exhash, s->p);
  494. put_mp_ssh2(s->exhash, s->g);
  495. }
  496. put_mp_ssh2(s->exhash, s->e);
  497. put_mp_ssh2(s->exhash, s->f);
  498. }
  499. /*
  500. * MIC verification is done below, after we compute the hash
  501. * used as the MIC input.
  502. */
  503. if (s->ecdh_key) {
  504. ecdh_key_free(s->ecdh_key);
  505. s->ecdh_key = NULL;
  506. strbuf_free(s->ebuf); s->ebuf = NULL;
  507. strbuf_free(s->fbuf); s->fbuf = NULL;
  508. } else {
  509. dh_cleanup(s->dh_ctx);
  510. s->dh_ctx = NULL;
  511. mp_free(s->f); s->f = NULL;
  512. if (dh_is_gex(s->kex_alg)) {
  513. mp_free(s->g); s->g = NULL;
  514. mp_free(s->p); s->p = NULL;
  515. }
  516. }
  517. #endif
  518. } else {
  519. ptrlen rsakeydata;
  520. assert(s->kex_alg->main_type == KEXTYPE_RSA);
  521. ppl_logevent("Doing RSA key exchange with hash %s",
  522. ssh_hash_alg(s->exhash)->text_name);
  523. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_RSAKEX;
  524. /*
  525. * RSA key exchange. First expect a KEXRSA_PUBKEY packet
  526. * from the server.
  527. */
  528. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  529. if (pktin->type != SSH2_MSG_KEXRSA_PUBKEY) {
  530. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  531. "expecting RSA public key, type %d (%s)",
  532. pktin->type,
  533. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  534. s->ppl.bpp->pls->actx,
  535. pktin->type));
  536. *aborted = true;
  537. return;
  538. }
  539. s->hostkeydata = get_string(pktin);
  540. put_stringpl(s->exhash, s->hostkeydata);
  541. s->hkey = ssh_key_new_pub(s->hostkey_alg, s->hostkeydata);
  542. rsakeydata = get_string(pktin);
  543. s->rsa_kex_key = ssh_rsakex_newkey(rsakeydata);
  544. if (!s->rsa_kex_key) {
  545. ssh_proto_error(s->ppl.ssh,
  546. "Unable to parse RSA public key packet");
  547. *aborted = true;
  548. return;
  549. }
  550. s->rsa_kex_key_needs_freeing = true;
  551. put_stringpl(s->exhash, rsakeydata);
  552. /*
  553. * Next, set up a shared secret K, of precisely KLEN -
  554. * 2*HLEN - 49 bits, where KLEN is the bit length of the
  555. * RSA key modulus and HLEN is the bit length of the hash
  556. * we're using.
  557. */
  558. {
  559. int klen = ssh_rsakex_klen(s->rsa_kex_key);
  560. const struct ssh_rsa_kex_extra *extra =
  561. (const struct ssh_rsa_kex_extra *)s->kex_alg->extra;
  562. if (klen < extra->minklen) {
  563. ssh_proto_error(s->ppl.ssh, "Server sent %d-bit RSA key, "
  564. "less than the minimum size %d for %s "
  565. "key exchange", klen, extra->minklen,
  566. s->kex_alg->name);
  567. *aborted = true;
  568. return;
  569. }
  570. int nbits = klen - (2*s->kex_alg->hash->hlen*8 + 49);
  571. assert(nbits > 0);
  572. strbuf *buf, *outstr;
  573. mp_int *tmp = mp_random_bits(nbits - 1);
  574. mp_int *K = mp_power_2(nbits - 1);
  575. mp_add_into(K, K, tmp);
  576. mp_free(tmp);
  577. /*
  578. * Encode this as an mpint.
  579. */
  580. buf = strbuf_new_nm();
  581. put_mp_ssh2(buf, K);
  582. /*
  583. * Store a copy as the output shared secret from the kex.
  584. */
  585. put_mp_ssh2(s->kex_shared_secret, K);
  586. mp_free(K);
  587. /*
  588. * Encrypt it with the given RSA key.
  589. */
  590. outstr = ssh_rsakex_encrypt(s->rsa_kex_key, s->kex_alg->hash,
  591. ptrlen_from_strbuf(buf));
  592. /*
  593. * And send it off in a return packet.
  594. */
  595. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEXRSA_SECRET);
  596. put_stringpl(pktout, ptrlen_from_strbuf(outstr));
  597. pq_push(s->ppl.out_pq, pktout);
  598. put_stringsb(s->exhash, outstr); /* frees outstr */
  599. strbuf_free(buf);
  600. }
  601. ssh_rsakex_freekey(s->rsa_kex_key);
  602. s->rsa_kex_key = NULL;
  603. s->rsa_kex_key_needs_freeing = false;
  604. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  605. if (pktin->type != SSH2_MSG_KEXRSA_DONE) {
  606. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  607. "expecting RSA kex signature, type %d (%s)",
  608. pktin->type,
  609. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  610. s->ppl.bpp->pls->actx,
  611. pktin->type));
  612. *aborted = true;
  613. return;
  614. }
  615. s->sigdata = get_string(pktin);
  616. if (get_err(pktin)) {
  617. ssh_proto_error(s->ppl.ssh, "Unable to parse RSA kex signature");
  618. *aborted = true;
  619. return;
  620. }
  621. }
  622. ssh2transport_finalise_exhash(s);
  623. #ifndef NO_GSSAPI
  624. if (kex_is_gss(s->kex_alg)) {
  625. Ssh_gss_buf gss_buf;
  626. SSH_GSS_CLEAR_BUF(&s->gss_buf);
  627. gss_buf.value = s->exchange_hash;
  628. gss_buf.length = s->kex_alg->hash->hlen;
  629. s->gss_stat = s->shgss->lib->verify_mic(
  630. s->shgss->lib, s->shgss->ctx, &gss_buf, &s->mic);
  631. if (s->gss_stat != SSH_GSS_OK) {
  632. if (s->shgss->lib->display_status(
  633. s->shgss->lib, s->shgss->ctx, &s->gss_buf) == SSH_GSS_OK) {
  634. char *err = s->gss_buf.value;
  635. ssh_sw_abort(s->ppl.ssh, "GSSAPI key exchange MIC was "
  636. "not valid: %s", err);
  637. sfree(err);
  638. } else {
  639. ssh_sw_abort(s->ppl.ssh, "GSSAPI key exchange MIC was "
  640. "not valid");
  641. }
  642. *aborted = true;
  643. return;
  644. }
  645. s->gss_kex_used = true;
  646. /*-
  647. * If this the first KEX, save the GSS context for "gssapi-keyex"
  648. * authentication.
  649. *
  650. * https://www.rfc-editor.org/rfc/rfc4462#section-4
  651. *
  652. * This method may be used only if the initial key exchange was
  653. * performed using a GSS-API-based key exchange method defined in
  654. * accordance with Section 2. The GSS-API context used with this
  655. * method is always that established during an initial GSS-API-based
  656. * key exchange. Any context established during key exchange for the
  657. * purpose of rekeying MUST NOT be used with this method.
  658. */
  659. if (s->got_session_id) {
  660. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  661. }
  662. ppl_logevent("GSSAPI Key Exchange complete!");
  663. }
  664. #endif
  665. s->dh_ctx = NULL;
  666. /* In GSS keyex there's no hostkey signature to verify */
  667. if (!kex_is_gss(s->kex_alg)) {
  668. if (!s->hkey) {
  669. ssh_proto_error(s->ppl.ssh, "Server's host key is invalid");
  670. *aborted = true;
  671. return;
  672. }
  673. if (!ssh_key_verify(
  674. s->hkey, s->sigdata,
  675. make_ptrlen(s->exchange_hash, s->kex_alg->hash->hlen))) {
  676. #ifndef FUZZING
  677. ssh_proto_error(s->ppl.ssh, "Signature from server's host key "
  678. "is invalid");
  679. *aborted = true;
  680. return;
  681. #endif
  682. }
  683. }
  684. s->keystr = s->hkey ? ssh_key_cache_str(s->hkey) : NULL;
  685. #ifndef NO_GSSAPI
  686. if (s->gss_kex_used) {
  687. /*
  688. * In a GSS-based session, check the host key (if any) against
  689. * the transient host key cache.
  690. */
  691. if (kex_is_gss(s->kex_alg)) {
  692. /*
  693. * We've just done a GSS key exchange. If it gave us a
  694. * host key, store it.
  695. */
  696. if (s->hkey) {
  697. char *fingerprint = ssh2_double_fingerprint(
  698. s->hkey, SSH_FPTYPE_DEFAULT);
  699. ppl_logevent("GSS kex provided fallback host key:");
  700. ppl_logevent("%s", fingerprint);
  701. sfree(fingerprint);
  702. ssh_transient_hostkey_cache_add(s->thc, s->hkey);
  703. } else if (!ssh_transient_hostkey_cache_non_empty(s->thc)) {
  704. /*
  705. * But if it didn't, then we currently have no
  706. * fallback host key to use in subsequent non-GSS
  707. * rekeys. So we should immediately trigger a non-GSS
  708. * rekey of our own, to set one up, before the session
  709. * keys have been used for anything else.
  710. *
  711. * This is similar to the cross-certification done at
  712. * user request in the permanent host key cache, but
  713. * here we do it automatically, once, at session
  714. * startup, and only add the key to the transient
  715. * cache.
  716. */
  717. if (s->hostkey_alg) {
  718. s->need_gss_transient_hostkey = true;
  719. } else {
  720. /*
  721. * If we negotiated the "null" host key algorithm
  722. * in the key exchange, that's an indication that
  723. * no host key at all is available from the server
  724. * (both because we listed "null" last, and
  725. * because RFC 4462 section 5 says that a server
  726. * MUST NOT offer "null" as a host key algorithm
  727. * unless that is the only algorithm it provides
  728. * at all).
  729. *
  730. * In that case we actually _can't_ perform a
  731. * non-GSSAPI key exchange, so it's pointless to
  732. * attempt one proactively. This is also likely to
  733. * cause trouble later if a rekey is required at a
  734. * moment whne GSS credentials are not available,
  735. * but someone setting up a server in this
  736. * configuration presumably accepts that as a
  737. * consequence.
  738. */
  739. if (!s->warned_about_no_gss_transient_hostkey) {
  740. ppl_logevent("No fallback host key available");
  741. s->warned_about_no_gss_transient_hostkey = true;
  742. }
  743. }
  744. }
  745. } else {
  746. /*
  747. * We've just done a fallback key exchange, so make
  748. * sure the host key it used is in the cache of keys
  749. * we previously received in GSS kexes.
  750. *
  751. * An exception is if this was the non-GSS key exchange we
  752. * triggered on purpose to populate the transient cache.
  753. */
  754. assert(s->hkey); /* only KEXTYPE_GSS* lets this be null */
  755. char *fingerprint = ssh2_double_fingerprint(
  756. s->hkey, SSH_FPTYPE_DEFAULT);
  757. if (s->need_gss_transient_hostkey) {
  758. ppl_logevent("Post-GSS rekey provided fallback host key:");
  759. ppl_logevent("%s", fingerprint);
  760. ssh_transient_hostkey_cache_add(s->thc, s->hkey);
  761. s->need_gss_transient_hostkey = false;
  762. } else if (!ssh_transient_hostkey_cache_verify(s->thc, s->hkey)) {
  763. ppl_logevent("Non-GSS rekey after initial GSS kex "
  764. "used host key:");
  765. ppl_logevent("%s", fingerprint);
  766. sfree(fingerprint);
  767. ssh_sw_abort(s->ppl.ssh, "Server's host key did not match any "
  768. "used in previous GSS kex");
  769. *aborted = true;
  770. return;
  771. }
  772. sfree(fingerprint);
  773. }
  774. } else
  775. #endif /* NO_GSSAPI */
  776. if (!s->got_session_id) {
  777. /*
  778. * Make a note of any other host key formats that are available.
  779. */
  780. {
  781. int i, j, nkeys = 0;
  782. char *list = NULL;
  783. for (i = 0; i < lenof(ssh2_hostkey_algs); i++) {
  784. if (ssh2_hostkey_algs[i].alg == s->hostkey_alg)
  785. continue;
  786. for (j = 0; j < s->n_uncert_hostkeys; j++)
  787. if (s->uncert_hostkeys[j] == i)
  788. break;
  789. if (j < s->n_uncert_hostkeys) {
  790. char *newlist;
  791. if (list)
  792. newlist = dupprintf(
  793. "%s/%s", list,
  794. ssh2_hostkey_algs[i].alg->ssh_id);
  795. else
  796. newlist = dupprintf(
  797. "%s", ssh2_hostkey_algs[i].alg->ssh_id);
  798. sfree(list);
  799. list = newlist;
  800. nkeys++;
  801. }
  802. }
  803. if (list) {
  804. ppl_logevent("Server also has %s host key%s, but we "
  805. "don't know %s", list,
  806. nkeys > 1 ? "s" : "",
  807. nkeys > 1 ? "any of them" : "it");
  808. sfree(list);
  809. }
  810. }
  811. ssh2_userkey uk = { .key = s->hkey, .comment = NULL };
  812. char **fingerprints = ssh2_all_fingerprints(s->hkey);
  813. FingerprintType fptype_default =
  814. ssh2_pick_default_fingerprint(fingerprints);
  815. ppl_logevent("Host key fingerprint is:");
  816. ppl_logevent("%s", fingerprints[fptype_default]);
  817. /*
  818. * Authenticate remote host: verify host key, either by
  819. * certification or by the local host key cache.
  820. *
  821. * (We've already checked the signature of the exchange
  822. * hash.)
  823. */
  824. if (ssh_key_alg(s->hkey)->is_certificate) {
  825. char *base_fp = ssh2_fingerprint(
  826. s->hkey, ssh_fptype_to_cert(fptype_default));
  827. ppl_logevent("Host key is a certificate. "
  828. "Hash including certificate:");
  829. ppl_logevent("%s", base_fp);
  830. sfree(base_fp);
  831. strbuf *id_string = strbuf_new();
  832. StripCtrlChars *id_string_scc = stripctrl_new(
  833. BinarySink_UPCAST(id_string), false, L'\0');
  834. ssh_key_cert_id_string(
  835. s->hkey, BinarySink_UPCAST(id_string_scc));
  836. stripctrl_free(id_string_scc);
  837. ppl_logevent("Certificate ID string is \"%s\"", id_string->s);
  838. strbuf_free(id_string);
  839. strbuf *ca_pub = strbuf_new();
  840. ssh_key_ca_public_blob(s->hkey, BinarySink_UPCAST(ca_pub));
  841. host_ca hca_search = { .ca_public_key = ca_pub };
  842. host_ca *hca_found = find234(s->host_cas, &hca_search, NULL);
  843. char *ca_fp = ssh2_fingerprint_blob(ptrlen_from_strbuf(ca_pub),
  844. fptype_default);
  845. ppl_logevent("Fingerprint of certification authority:");
  846. ppl_logevent("%s", ca_fp);
  847. sfree(ca_fp);
  848. strbuf_free(ca_pub);
  849. strbuf *error = strbuf_new();
  850. bool cert_ok = false;
  851. if (!hca_found) {
  852. put_fmt(error, "Certification authority is not trusted");
  853. } else {
  854. ppl_logevent("Certification authority matches '%s'",
  855. hca_found->name);
  856. cert_ok = ssh_key_check_cert(
  857. s->hkey,
  858. true, /* host certificate */
  859. ptrlen_from_asciz(s->savedhost),
  860. time(NULL),
  861. &hca_found->opts,
  862. BinarySink_UPCAST(error));
  863. }
  864. if (cert_ok) {
  865. strbuf_free(error);
  866. ssh2_free_all_fingerprints(fingerprints);
  867. ppl_logevent("Accepted certificate");
  868. goto host_key_ok;
  869. } else {
  870. ppl_logevent("Rejected host key certificate: %s",
  871. error->s);
  872. strbuf_free(error);
  873. /* now fall through into normal host key checking */
  874. }
  875. }
  876. {
  877. char *keydisp = ssh2_pubkey_openssh_str(&uk);
  878. int ca_count = ssh_key_alg(s->hkey)->is_certificate ?
  879. count234(s->host_cas) : 0;
  880. s->spr = verify_ssh_host_key(
  881. ppl_get_iseat(&s->ppl), s->conf, s->savedhost, s->savedport,
  882. s->hkey, ssh_key_cache_id(s->hkey), s->keystr, keydisp,
  883. fingerprints, ca_count, ssh2_transport_dialog_callback, s);
  884. ssh2_free_all_fingerprints(fingerprints);
  885. sfree(keydisp);
  886. #ifdef FUZZING
  887. s->spr = SPR_OK;
  888. #endif
  889. crMaybeWaitUntilV(s->spr.kind != SPRK_INCOMPLETE);
  890. if (spr_is_abort(s->spr)) {
  891. *aborted = true;
  892. ssh_spr_close(s->ppl.ssh, s->spr, "host key verification");
  893. return;
  894. }
  895. if (ssh_key_alg(s->hkey)->is_certificate) {
  896. /*
  897. * Explain what's going on in the Event Log: if we
  898. * got here by way of a certified key whose
  899. * certificate we didn't like, then we should
  900. * explain why we chose to continue with the
  901. * connection anyway!
  902. */
  903. ppl_logevent("Accepting certified host key anyway based "
  904. "on cache");
  905. }
  906. }
  907. host_key_ok:
  908. /*
  909. * Save this host key, to check against the one presented in
  910. * subsequent rekeys.
  911. */
  912. strbuf_clear(s->hostkeyblob);
  913. ssh_key_public_blob(s->hkey, BinarySink_UPCAST(s->hostkeyblob));
  914. } else if (s->cross_certifying) {
  915. assert(s->hkey);
  916. assert(ssh_key_alg(s->hkey) == s->cross_certifying);
  917. char *fingerprint = ssh2_double_fingerprint(
  918. s->hkey, SSH_FPTYPE_DEFAULT);
  919. ppl_logevent("Storing additional host key for this host:");
  920. ppl_logevent("%s", fingerprint);
  921. sfree(fingerprint);
  922. store_host_key(s->ppl.seat, s->savedhost, s->savedport,
  923. ssh_key_cache_id(s->hkey), s->keystr);
  924. /*
  925. * Don't forget to store the new key as the one we'll be
  926. * re-checking in future normal rekeys.
  927. */
  928. strbuf_clear(s->hostkeyblob);
  929. ssh_key_public_blob(s->hkey, BinarySink_UPCAST(s->hostkeyblob));
  930. } else {
  931. /*
  932. * In a rekey, we never present an interactive host key
  933. * verification request to the user. Instead, we simply
  934. * enforce that the key we're seeing this time is identical to
  935. * the one we saw before.
  936. */
  937. strbuf *thisblob = strbuf_new();
  938. ssh_key_public_blob(s->hkey, BinarySink_UPCAST(thisblob));
  939. bool match = ptrlen_eq_ptrlen(ptrlen_from_strbuf(thisblob),
  940. ptrlen_from_strbuf(s->hostkeyblob));
  941. strbuf_free(thisblob);
  942. if (!match) {
  943. #ifndef FUZZING
  944. ssh_sw_abort(s->ppl.ssh,
  945. "Host key was different in repeat key exchange");
  946. *aborted = true;
  947. return;
  948. #endif
  949. }
  950. }
  951. sfree(s->keystr);
  952. s->keystr = NULL;
  953. if (s->hkey) {
  954. ssh_key_free(s->hkey);
  955. s->hkey = NULL;
  956. }
  957. crFinishV;
  958. }