kexgexc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* $OpenBSD: kexgexc.c,v 1.35 2019/11/25 00:51:37 djm Exp $ */
  2. /*
  3. * Copyright (c) 2000 Niels Provos. All rights reserved.
  4. * Copyright (c) 2001 Markus Friedl. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "includes.h"
  27. #ifdef WITH_OPENSSL
  28. #include <sys/types.h>
  29. #include <openssl/dh.h>
  30. #include <stdarg.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <signal.h>
  34. #include "openbsd-compat/openssl-compat.h"
  35. #include "sshkey.h"
  36. #include "cipher.h"
  37. #include "digest.h"
  38. #include "kex.h"
  39. #include "log.h"
  40. #include "packet.h"
  41. #include "dh.h"
  42. #include "ssh2.h"
  43. #include "compat.h"
  44. #include "dispatch.h"
  45. #include "ssherr.h"
  46. #include "sshbuf.h"
  47. #include "misc.h"
  48. static int input_kex_dh_gex_group(int, u_int32_t, struct ssh *);
  49. static int input_kex_dh_gex_reply(int, u_int32_t, struct ssh *);
  50. int
  51. kexgex_client(struct ssh *ssh)
  52. {
  53. struct kex *kex = ssh->kex;
  54. int r;
  55. u_int nbits;
  56. nbits = dh_estimate(kex->dh_need * 8);
  57. kex->min = DH_GRP_MIN;
  58. kex->max = DH_GRP_MAX;
  59. kex->nbits = nbits;
  60. if (ssh->compat & SSH_BUG_DHGEX_LARGE)
  61. kex->nbits = MINIMUM(kex->nbits, 4096);
  62. /* New GEX request */
  63. if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 ||
  64. (r = sshpkt_put_u32(ssh, kex->min)) != 0 ||
  65. (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
  66. (r = sshpkt_put_u32(ssh, kex->max)) != 0 ||
  67. (r = sshpkt_send(ssh)) != 0)
  68. goto out;
  69. debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
  70. kex->min, kex->nbits, kex->max);
  71. #ifdef DEBUG_KEXDH
  72. fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
  73. kex->min, kex->nbits, kex->max);
  74. #endif
  75. debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
  76. ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP,
  77. &input_kex_dh_gex_group);
  78. r = 0;
  79. out:
  80. return r;
  81. }
  82. static int
  83. input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh)
  84. {
  85. struct kex *kex = ssh->kex;
  86. BIGNUM *p = NULL, *g = NULL;
  87. const BIGNUM *pub_key;
  88. int r, bits;
  89. debug("SSH2_MSG_KEX_DH_GEX_GROUP received");
  90. ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, &kex_protocol_error);
  91. if ((r = sshpkt_get_bignum2(ssh, &p)) != 0 ||
  92. (r = sshpkt_get_bignum2(ssh, &g)) != 0 ||
  93. (r = sshpkt_get_end(ssh)) != 0)
  94. goto out;
  95. if ((bits = BN_num_bits(p)) < 0 ||
  96. (u_int)bits < kex->min || (u_int)bits > kex->max) {
  97. r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
  98. goto out;
  99. }
  100. if ((kex->dh = dh_new_group(g, p)) == NULL) {
  101. r = SSH_ERR_ALLOC_FAIL;
  102. goto out;
  103. }
  104. p = g = NULL; /* belong to kex->dh now */
  105. /* generate and send 'e', client DH public key */
  106. if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
  107. goto out;
  108. DH_get0_key(kex->dh, &pub_key, NULL);
  109. if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
  110. (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
  111. (r = sshpkt_send(ssh)) != 0)
  112. goto out;
  113. debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
  114. #ifdef DEBUG_KEXDH
  115. DHparams_print_fp(stderr, kex->dh);
  116. fprintf(stderr, "pub= ");
  117. BN_print_fp(stderr, pub_key);
  118. fprintf(stderr, "\n");
  119. #endif
  120. debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
  121. ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
  122. r = 0;
  123. out:
  124. BN_clear_free(p);
  125. BN_clear_free(g);
  126. return r;
  127. }
  128. static int
  129. input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh)
  130. {
  131. struct kex *kex = ssh->kex;
  132. BIGNUM *dh_server_pub = NULL;
  133. const BIGNUM *pub_key, *dh_p, *dh_g;
  134. struct sshbuf *shared_secret = NULL;
  135. struct sshbuf *tmp = NULL, *server_host_key_blob = NULL;
  136. struct sshkey *server_host_key = NULL;
  137. u_char *signature = NULL;
  138. u_char hash[SSH_DIGEST_MAX_LENGTH];
  139. size_t slen, hashlen;
  140. int r;
  141. debug("SSH2_MSG_KEX_DH_GEX_REPLY received");
  142. ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &kex_protocol_error);
  143. /* key, cert */
  144. if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
  145. goto out;
  146. /* sshkey_fromb() consumes its buffer, so make a copy */
  147. if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) {
  148. r = SSH_ERR_ALLOC_FAIL;
  149. goto out;
  150. }
  151. if ((r = sshkey_fromb(tmp, &server_host_key)) != 0 ||
  152. (r = kex_verify_host_key(ssh, server_host_key)) != 0)
  153. goto out;
  154. /* DH parameter f, server public DH key, signed H */
  155. if ((r = sshpkt_get_bignum2(ssh, &dh_server_pub)) != 0 ||
  156. (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
  157. (r = sshpkt_get_end(ssh)) != 0)
  158. goto out;
  159. if ((shared_secret = sshbuf_new()) == NULL) {
  160. r = SSH_ERR_ALLOC_FAIL;
  161. goto out;
  162. }
  163. if ((r = kex_dh_compute_key(kex, dh_server_pub, shared_secret)) != 0)
  164. goto out;
  165. if (ssh->compat & SSH_OLD_DHGEX)
  166. kex->min = kex->max = -1;
  167. /* calc and verify H */
  168. DH_get0_key(kex->dh, &pub_key, NULL);
  169. DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
  170. hashlen = sizeof(hash);
  171. if ((r = kexgex_hash(
  172. kex->hash_alg,
  173. kex->client_version,
  174. kex->server_version,
  175. kex->my,
  176. kex->peer,
  177. server_host_key_blob,
  178. kex->min, kex->nbits, kex->max,
  179. dh_p, dh_g,
  180. pub_key,
  181. dh_server_pub,
  182. sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
  183. hash, &hashlen)) != 0)
  184. goto out;
  185. if ((r = sshkey_verify(server_host_key, signature, slen, hash,
  186. hashlen, kex->hostkey_alg, ssh->compat, NULL)) != 0)
  187. goto out;
  188. if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
  189. r = kex_send_newkeys(ssh);
  190. out:
  191. explicit_bzero(hash, sizeof(hash));
  192. DH_free(kex->dh);
  193. kex->dh = NULL;
  194. BN_clear_free(dh_server_pub);
  195. sshbuf_free(shared_secret);
  196. sshkey_free(server_host_key);
  197. sshbuf_free(tmp);
  198. sshbuf_free(server_host_key_blob);
  199. free(signature);
  200. return r;
  201. }
  202. #endif /* WITH_OPENSSL */