1 /* $OpenBSD: kexgexc.c,v 1.34 2019/01/23 00:30:41 djm Exp $ */
3 * Copyright (c) 2000 Niels Provos. All rights reserved.
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/types.h>
33 #include <openssl/dh.h>
40 #include "openbsd-compat/openssl-compat.h"
56 static int input_kex_dh_gex_group(int, u_int32_t, struct ssh *);
57 static int input_kex_dh_gex_reply(int, u_int32_t, struct ssh *);
60 kexgex_client(struct ssh *ssh)
62 struct kex *kex = ssh->kex;
66 nbits = dh_estimate(kex->dh_need * 8);
68 kex->min = DH_GRP_MIN;
69 kex->max = DH_GRP_MAX;
71 if (datafellows & SSH_BUG_DHGEX_LARGE)
72 kex->nbits = MINIMUM(kex->nbits, 4096);
74 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 ||
75 (r = sshpkt_put_u32(ssh, kex->min)) != 0 ||
76 (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
77 (r = sshpkt_put_u32(ssh, kex->max)) != 0 ||
78 (r = sshpkt_send(ssh)) != 0)
80 debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
81 kex->min, kex->nbits, kex->max);
83 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
84 kex->min, kex->nbits, kex->max);
86 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP,
87 &input_kex_dh_gex_group);
94 input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh)
96 struct kex *kex = ssh->kex;
97 BIGNUM *p = NULL, *g = NULL;
98 const BIGNUM *pub_key;
101 debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
103 if ((r = sshpkt_get_bignum2(ssh, &p)) != 0 ||
104 (r = sshpkt_get_bignum2(ssh, &g)) != 0 ||
105 (r = sshpkt_get_end(ssh)) != 0)
107 if ((bits = BN_num_bits(p)) < 0 ||
108 (u_int)bits < kex->min || (u_int)bits > kex->max) {
109 r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
112 if ((kex->dh = dh_new_group(g, p)) == NULL) {
113 r = SSH_ERR_ALLOC_FAIL;
116 p = g = NULL; /* belong to kex->dh now */
118 /* generate and send 'e', client DH public key */
119 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
121 DH_get0_key(kex->dh, &pub_key, NULL);
122 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
123 (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
124 (r = sshpkt_send(ssh)) != 0)
126 debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
128 DHparams_print_fp(stderr, kex->dh);
129 fprintf(stderr, "pub= ");
130 BN_print_fp(stderr, pub_key);
131 fprintf(stderr, "\n");
133 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
134 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
143 input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh)
145 struct kex *kex = ssh->kex;
146 BIGNUM *dh_server_pub = NULL;
147 const BIGNUM *pub_key, *dh_p, *dh_g;
148 struct sshbuf *shared_secret = NULL;
149 struct sshbuf *tmp = NULL, *server_host_key_blob = NULL;
150 struct sshkey *server_host_key = NULL;
151 u_char *signature = NULL;
152 u_char hash[SSH_DIGEST_MAX_LENGTH];
153 size_t slen, hashlen;
156 debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
158 if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
160 /* sshkey_fromb() consumes its buffer, so make a copy */
161 if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) {
162 r = SSH_ERR_ALLOC_FAIL;
165 if ((r = sshkey_fromb(tmp, &server_host_key)) != 0 ||
166 (r = kex_verify_host_key(ssh, server_host_key)) != 0)
168 /* DH parameter f, server public DH key, signed H */
169 if ((r = sshpkt_get_bignum2(ssh, &dh_server_pub)) != 0 ||
170 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
171 (r = sshpkt_get_end(ssh)) != 0)
173 if ((shared_secret = sshbuf_new()) == NULL) {
174 r = SSH_ERR_ALLOC_FAIL;
177 if ((r = kex_dh_compute_key(kex, dh_server_pub, shared_secret)) != 0)
179 if (ssh->compat & SSH_OLD_DHGEX)
180 kex->min = kex->max = -1;
182 /* calc and verify H */
183 DH_get0_key(kex->dh, &pub_key, NULL);
184 DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
185 hashlen = sizeof(hash);
186 if ((r = kexgex_hash(
192 server_host_key_blob,
193 kex->min, kex->nbits, kex->max,
197 sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
198 hash, &hashlen)) != 0)
201 if ((r = sshkey_verify(server_host_key, signature, slen, hash,
202 hashlen, kex->hostkey_alg, ssh->compat)) != 0)
205 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
206 r = kex_send_newkeys(ssh);
208 explicit_bzero(hash, sizeof(hash));
211 BN_clear_free(dh_server_pub);
212 sshbuf_free(shared_secret);
213 sshkey_free(server_host_key);
215 sshbuf_free(server_host_key_blob);
219 #endif /* WITH_OPENSSL */