ssh2kex-server.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Server 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 "sshserver.h"
  11. #include "sshkeygen.h"
  12. #include "storage.h"
  13. #include "ssh2transport.h"
  14. #include "mpint.h"
  15. void ssh2_transport_provide_hostkeys(PacketProtocolLayer *ppl,
  16. ssh_key *const *hostkeys, int nhostkeys)
  17. {
  18. struct ssh2_transport_state *s =
  19. container_of(ppl, struct ssh2_transport_state, ppl);
  20. s->hostkeys = hostkeys;
  21. s->nhostkeys = nhostkeys;
  22. }
  23. static strbuf *finalise_and_sign_exhash(struct ssh2_transport_state *s)
  24. {
  25. strbuf *sb;
  26. ssh2transport_finalise_exhash(s);
  27. sb = strbuf_new();
  28. ssh_key_sign(
  29. s->hkey, make_ptrlen(s->exchange_hash, s->kex_alg->hash->hlen),
  30. s->hkflags, BinarySink_UPCAST(sb));
  31. return sb;
  32. }
  33. void ssh2kex_coroutine(struct ssh2_transport_state *s, bool *aborted)
  34. {
  35. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  36. PktIn *pktin;
  37. PktOut *pktout;
  38. crBegin(s->crStateKex);
  39. {
  40. int i;
  41. for (i = 0; i < s->nhostkeys; i++)
  42. if (ssh_key_alg(s->hostkeys[i]) == s->hostkey_alg) {
  43. s->hkey = s->hostkeys[i];
  44. break;
  45. }
  46. assert(s->hkey);
  47. }
  48. strbuf_clear(s->hostkeyblob);
  49. ssh_key_public_blob(s->hkey, BinarySink_UPCAST(s->hostkeyblob));
  50. s->hostkeydata = ptrlen_from_strbuf(s->hostkeyblob);
  51. put_stringpl(s->exhash, s->hostkeydata);
  52. if (s->kex_alg->main_type == KEXTYPE_DH) {
  53. /*
  54. * If we're doing Diffie-Hellman group exchange, start by
  55. * waiting for the group request.
  56. */
  57. if (dh_is_gex(s->kex_alg)) {
  58. ppl_logevent("Doing Diffie-Hellman group exchange");
  59. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_DHGEX;
  60. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  61. if (pktin->type != SSH2_MSG_KEX_DH_GEX_REQUEST &&
  62. pktin->type != SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) {
  63. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  64. "expecting Diffie-Hellman group exchange "
  65. "request, type %d (%s)", pktin->type,
  66. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  67. s->ppl.bpp->pls->actx,
  68. pktin->type));
  69. *aborted = true;
  70. return;
  71. }
  72. if (pktin->type != SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) {
  73. s->dh_got_size_bounds = true;
  74. s->dh_min_size = get_uint32(pktin);
  75. s->pbits = get_uint32(pktin);
  76. s->dh_max_size = get_uint32(pktin);
  77. } else {
  78. s->dh_got_size_bounds = false;
  79. s->pbits = get_uint32(pktin);
  80. }
  81. /*
  82. * This is a hopeless strategy for making a secure DH
  83. * group! It's good enough for testing a client against,
  84. * but not for serious use.
  85. */
  86. PrimeGenerationContext *pgc = primegen_new_context(
  87. &primegen_probabilistic);
  88. ProgressReceiver null_progress;
  89. null_progress.vt = &null_progress_vt;
  90. s->p = primegen_generate(pgc, pcs_new(s->pbits), &null_progress);
  91. primegen_free_context(pgc);
  92. s->g = mp_from_integer(2);
  93. s->dh_ctx = dh_setup_gex(s->p, s->g);
  94. s->kex_init_value = SSH2_MSG_KEX_DH_GEX_INIT;
  95. s->kex_reply_value = SSH2_MSG_KEX_DH_GEX_REPLY;
  96. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEX_DH_GEX_GROUP);
  97. put_mp_ssh2(pktout, s->p);
  98. put_mp_ssh2(pktout, s->g);
  99. pq_push(s->ppl.out_pq, pktout);
  100. } else {
  101. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_DHGROUP;
  102. s->dh_ctx = dh_setup_group(s->kex_alg);
  103. s->kex_init_value = SSH2_MSG_KEXDH_INIT;
  104. s->kex_reply_value = SSH2_MSG_KEXDH_REPLY;
  105. ppl_logevent("Using Diffie-Hellman with standard group \"%s\"",
  106. s->kex_alg->groupname);
  107. }
  108. ppl_logevent("Doing Diffie-Hellman key exchange with hash %s",
  109. ssh_hash_alg(s->exhash)->text_name);
  110. /*
  111. * Generate e for Diffie-Hellman.
  112. */
  113. s->e = dh_create_e(s->dh_ctx, s->nbits * 2);
  114. /*
  115. * Wait to receive f.
  116. */
  117. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  118. if (pktin->type != s->kex_init_value) {
  119. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  120. "expecting Diffie-Hellman initial packet, "
  121. "type %d (%s)", pktin->type,
  122. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  123. s->ppl.bpp->pls->actx,
  124. pktin->type));
  125. *aborted = true;
  126. return;
  127. }
  128. s->f = get_mp_ssh2(pktin);
  129. if (get_err(pktin)) {
  130. ssh_proto_error(s->ppl.ssh,
  131. "Unable to parse Diffie-Hellman initial packet");
  132. *aborted = true;
  133. return;
  134. }
  135. {
  136. const char *err = dh_validate_f(s->dh_ctx, s->f);
  137. if (err) {
  138. ssh_proto_error(s->ppl.ssh, "Diffie-Hellman initial packet "
  139. "failed validation: %s", err);
  140. *aborted = true;
  141. return;
  142. }
  143. }
  144. s->K = dh_find_K(s->dh_ctx, s->f);
  145. if (dh_is_gex(s->kex_alg)) {
  146. if (s->dh_got_size_bounds)
  147. put_uint32(s->exhash, s->dh_min_size);
  148. put_uint32(s->exhash, s->pbits);
  149. if (s->dh_got_size_bounds)
  150. put_uint32(s->exhash, s->dh_max_size);
  151. put_mp_ssh2(s->exhash, s->p);
  152. put_mp_ssh2(s->exhash, s->g);
  153. }
  154. put_mp_ssh2(s->exhash, s->f);
  155. put_mp_ssh2(s->exhash, s->e);
  156. pktout = ssh_bpp_new_pktout(s->ppl.bpp, s->kex_reply_value);
  157. put_stringpl(pktout, s->hostkeydata);
  158. put_mp_ssh2(pktout, s->e);
  159. put_stringsb(pktout, finalise_and_sign_exhash(s));
  160. pq_push(s->ppl.out_pq, pktout);
  161. dh_cleanup(s->dh_ctx);
  162. s->dh_ctx = NULL;
  163. mp_free(s->f); s->f = NULL;
  164. if (dh_is_gex(s->kex_alg)) {
  165. mp_free(s->g); s->g = NULL;
  166. mp_free(s->p); s->p = NULL;
  167. }
  168. } else if (s->kex_alg->main_type == KEXTYPE_ECDH) {
  169. ppl_logevent("Doing ECDH key exchange with curve %s and hash %s",
  170. ssh_ecdhkex_curve_textname(s->kex_alg),
  171. ssh_hash_alg(s->exhash)->text_name);
  172. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_ECDHKEX;
  173. s->ecdh_key = ssh_ecdhkex_newkey(s->kex_alg);
  174. if (!s->ecdh_key) {
  175. ssh_sw_abort(s->ppl.ssh, "Unable to generate key for ECDH");
  176. *aborted = true;
  177. return;
  178. }
  179. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  180. if (pktin->type != SSH2_MSG_KEX_ECDH_INIT) {
  181. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  182. "expecting ECDH initial packet, type %d (%s)",
  183. pktin->type,
  184. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  185. s->ppl.bpp->pls->actx,
  186. pktin->type));
  187. *aborted = true;
  188. return;
  189. }
  190. {
  191. ptrlen keydata = get_string(pktin);
  192. put_stringpl(s->exhash, keydata);
  193. s->K = ssh_ecdhkex_getkey(s->ecdh_key, keydata);
  194. if (!get_err(pktin) && !s->K) {
  195. ssh_proto_error(s->ppl.ssh, "Received invalid elliptic curve "
  196. "point in ECDH initial packet");
  197. *aborted = true;
  198. return;
  199. }
  200. }
  201. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEX_ECDH_REPLY);
  202. put_stringpl(pktout, s->hostkeydata);
  203. {
  204. strbuf *pubpoint = strbuf_new();
  205. ssh_ecdhkex_getpublic(s->ecdh_key, BinarySink_UPCAST(pubpoint));
  206. put_string(s->exhash, pubpoint->u, pubpoint->len);
  207. put_stringsb(pktout, pubpoint);
  208. }
  209. put_stringsb(pktout, finalise_and_sign_exhash(s));
  210. pq_push(s->ppl.out_pq, pktout);
  211. ssh_ecdhkex_freekey(s->ecdh_key);
  212. s->ecdh_key = NULL;
  213. } else if (s->kex_alg->main_type == KEXTYPE_GSS) {
  214. ssh_sw_abort(s->ppl.ssh, "GSS key exchange not supported in server");
  215. } else {
  216. assert(s->kex_alg->main_type == KEXTYPE_RSA);
  217. ppl_logevent("Doing RSA key exchange with hash %s",
  218. ssh_hash_alg(s->exhash)->text_name);
  219. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_RSAKEX;
  220. const struct ssh_rsa_kex_extra *extra =
  221. (const struct ssh_rsa_kex_extra *)s->kex_alg->extra;
  222. if (s->ssc && s->ssc->rsa_kex_key) {
  223. int klen = ssh_rsakex_klen(s->ssc->rsa_kex_key);
  224. if (klen >= extra->minklen) {
  225. ppl_logevent("Using configured %d-bit RSA key", klen);
  226. s->rsa_kex_key = s->ssc->rsa_kex_key;
  227. } else {
  228. ppl_logevent("Configured %d-bit RSA key is too short (min %d)",
  229. klen, extra->minklen);
  230. }
  231. }
  232. if (!s->rsa_kex_key) {
  233. ppl_logevent("Generating a %d-bit RSA key", extra->minklen);
  234. s->rsa_kex_key = snew(RSAKey);
  235. PrimeGenerationContext *pgc = primegen_new_context(
  236. &primegen_probabilistic);
  237. ProgressReceiver null_progress;
  238. null_progress.vt = &null_progress_vt;
  239. rsa_generate(s->rsa_kex_key, extra->minklen, false,
  240. pgc, &null_progress);
  241. primegen_free_context(pgc);
  242. s->rsa_kex_key->comment = NULL;
  243. s->rsa_kex_key_needs_freeing = true;
  244. }
  245. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEXRSA_PUBKEY);
  246. put_stringpl(pktout, s->hostkeydata);
  247. {
  248. strbuf *pubblob = strbuf_new();
  249. ssh_key_public_blob(&s->rsa_kex_key->sshk,
  250. BinarySink_UPCAST(pubblob));
  251. put_string(s->exhash, pubblob->u, pubblob->len);
  252. put_stringsb(pktout, pubblob);
  253. }
  254. pq_push(s->ppl.out_pq, pktout);
  255. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  256. if (pktin->type != SSH2_MSG_KEXRSA_SECRET) {
  257. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  258. "expecting RSA kex secret, type %d (%s)",
  259. pktin->type,
  260. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  261. s->ppl.bpp->pls->actx,
  262. pktin->type));
  263. *aborted = true;
  264. return;
  265. }
  266. {
  267. ptrlen encrypted_secret = get_string(pktin);
  268. put_stringpl(s->exhash, encrypted_secret);
  269. s->K = ssh_rsakex_decrypt(
  270. s->rsa_kex_key, s->kex_alg->hash, encrypted_secret);
  271. }
  272. if (!s->K) {
  273. ssh_proto_error(s->ppl.ssh, "Unable to decrypt RSA kex secret");
  274. *aborted = true;
  275. return;
  276. }
  277. if (s->rsa_kex_key_needs_freeing) {
  278. ssh_rsakex_freekey(s->rsa_kex_key);
  279. sfree(s->rsa_kex_key);
  280. }
  281. s->rsa_kex_key = NULL;
  282. s->rsa_kex_key_needs_freeing = false;
  283. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEXRSA_DONE);
  284. put_stringsb(pktout, finalise_and_sign_exhash(s));
  285. pq_push(s->ppl.out_pq, pktout);
  286. }
  287. crFinishV;
  288. }