kex2-server.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 "bpp.h"
  8. #include "ppl.h"
  9. #include "sshcr.h"
  10. #include "server.h"
  11. #include "sshkeygen.h"
  12. #include "storage.h"
  13. #include "transport2.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);
  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. mp_int *K = dh_find_K(s->dh_ctx, s->f);
  145. put_mp_ssh2(s->kex_shared_secret, K);
  146. mp_free(K);
  147. if (dh_is_gex(s->kex_alg)) {
  148. if (s->dh_got_size_bounds)
  149. put_uint32(s->exhash, s->dh_min_size);
  150. put_uint32(s->exhash, s->pbits);
  151. if (s->dh_got_size_bounds)
  152. put_uint32(s->exhash, s->dh_max_size);
  153. put_mp_ssh2(s->exhash, s->p);
  154. put_mp_ssh2(s->exhash, s->g);
  155. }
  156. put_mp_ssh2(s->exhash, s->f);
  157. put_mp_ssh2(s->exhash, s->e);
  158. pktout = ssh_bpp_new_pktout(s->ppl.bpp, s->kex_reply_value);
  159. put_stringpl(pktout, s->hostkeydata);
  160. put_mp_ssh2(pktout, s->e);
  161. put_stringsb(pktout, finalise_and_sign_exhash(s));
  162. pq_push(s->ppl.out_pq, pktout);
  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->ecdh_key = ecdh_key_new(s->kex_alg, true);
  176. if (!s->ecdh_key) {
  177. ssh_sw_abort(s->ppl.ssh, "Unable to generate key for ECDH");
  178. *aborted = true;
  179. return;
  180. }
  181. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  182. if (pktin->type != SSH2_MSG_KEX_ECDH_INIT) {
  183. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  184. "expecting ECDH initial packet, type %d (%s)",
  185. pktin->type,
  186. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  187. s->ppl.bpp->pls->actx,
  188. pktin->type));
  189. *aborted = true;
  190. return;
  191. }
  192. {
  193. ptrlen keydata = get_string(pktin);
  194. put_stringpl(s->exhash, keydata);
  195. bool ok = ecdh_key_getkey(s->ecdh_key, keydata,
  196. BinarySink_UPCAST(s->kex_shared_secret));
  197. if (!get_err(pktin) && !ok) {
  198. ssh_proto_error(s->ppl.ssh, "Received invalid elliptic curve "
  199. "point in ECDH initial packet");
  200. *aborted = true;
  201. return;
  202. }
  203. }
  204. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEX_ECDH_REPLY);
  205. put_stringpl(pktout, s->hostkeydata);
  206. {
  207. strbuf *pubpoint = strbuf_new();
  208. ecdh_key_getpublic(s->ecdh_key, BinarySink_UPCAST(pubpoint));
  209. put_string(s->exhash, pubpoint->u, pubpoint->len);
  210. put_stringsb(pktout, pubpoint);
  211. }
  212. put_stringsb(pktout, finalise_and_sign_exhash(s));
  213. pq_push(s->ppl.out_pq, pktout);
  214. ecdh_key_free(s->ecdh_key);
  215. s->ecdh_key = NULL;
  216. } else if (s->kex_alg->main_type == KEXTYPE_GSS) {
  217. ssh_sw_abort(s->ppl.ssh, "GSS key exchange not supported in server");
  218. } else {
  219. assert(s->kex_alg->main_type == KEXTYPE_RSA);
  220. ppl_logevent("Doing RSA key exchange with hash %s",
  221. ssh_hash_alg(s->exhash)->text_name);
  222. s->ppl.bpp->pls->kctx = SSH2_PKTCTX_RSAKEX;
  223. const struct ssh_rsa_kex_extra *extra =
  224. (const struct ssh_rsa_kex_extra *)s->kex_alg->extra;
  225. if (s->ssc && s->ssc->rsa_kex_key) {
  226. int klen = ssh_rsakex_klen(s->ssc->rsa_kex_key);
  227. if (klen >= extra->minklen) {
  228. ppl_logevent("Using configured %d-bit RSA key", klen);
  229. s->rsa_kex_key = s->ssc->rsa_kex_key;
  230. } else {
  231. ppl_logevent("Configured %d-bit RSA key is too short (min %d)",
  232. klen, extra->minklen);
  233. }
  234. }
  235. if (!s->rsa_kex_key) {
  236. ppl_logevent("Generating a %d-bit RSA key", extra->minklen);
  237. s->rsa_kex_key = snew(RSAKey);
  238. PrimeGenerationContext *pgc = primegen_new_context(
  239. &primegen_probabilistic);
  240. ProgressReceiver null_progress;
  241. null_progress.vt = &null_progress_vt;
  242. rsa_generate(s->rsa_kex_key, extra->minklen, false,
  243. pgc, &null_progress);
  244. primegen_free_context(pgc);
  245. s->rsa_kex_key->comment = NULL;
  246. s->rsa_kex_key_needs_freeing = true;
  247. }
  248. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEXRSA_PUBKEY);
  249. put_stringpl(pktout, s->hostkeydata);
  250. {
  251. strbuf *pubblob = strbuf_new();
  252. ssh_key_public_blob(&s->rsa_kex_key->sshk,
  253. BinarySink_UPCAST(pubblob));
  254. put_string(s->exhash, pubblob->u, pubblob->len);
  255. put_stringsb(pktout, pubblob);
  256. }
  257. pq_push(s->ppl.out_pq, pktout);
  258. crMaybeWaitUntilV((pktin = ssh2_transport_pop(s)) != NULL);
  259. if (pktin->type != SSH2_MSG_KEXRSA_SECRET) {
  260. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  261. "expecting RSA kex secret, type %d (%s)",
  262. pktin->type,
  263. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  264. s->ppl.bpp->pls->actx,
  265. pktin->type));
  266. *aborted = true;
  267. return;
  268. }
  269. mp_int *K;
  270. {
  271. ptrlen encrypted_secret = get_string(pktin);
  272. put_stringpl(s->exhash, encrypted_secret);
  273. K = ssh_rsakex_decrypt(
  274. s->rsa_kex_key, s->kex_alg->hash, encrypted_secret);
  275. }
  276. if (!K) {
  277. ssh_proto_error(s->ppl.ssh, "Unable to decrypt RSA kex secret");
  278. *aborted = true;
  279. return;
  280. }
  281. put_mp_ssh2(s->kex_shared_secret, K);
  282. mp_free(K);
  283. if (s->rsa_kex_key_needs_freeing) {
  284. ssh_rsakex_freekey(s->rsa_kex_key);
  285. sfree(s->rsa_kex_key);
  286. }
  287. s->rsa_kex_key = NULL;
  288. s->rsa_kex_key_needs_freeing = false;
  289. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_KEXRSA_DONE);
  290. put_stringsb(pktout, finalise_and_sign_exhash(s));
  291. pq_push(s->ppl.out_pq, pktout);
  292. }
  293. crFinishV;
  294. }