kex2-client.c 44 KB

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