1 /* $OpenBSD: kexgexs.c,v 1.33 2018/04/10 00:10:49 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.
37 #include <openssl/dh.h>
39 #include "openbsd-compat/openssl-compat.h"
53 #include "monitor_wrap.h"
59 static int input_kex_dh_gex_request(int, u_int32_t, struct ssh *);
60 static int input_kex_dh_gex_init(int, u_int32_t, struct ssh *);
63 kexgex_server(struct ssh *ssh)
65 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST,
66 &input_kex_dh_gex_request);
67 debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST");
72 input_kex_dh_gex_request(int type, u_int32_t seq, struct ssh *ssh)
74 struct kex *kex = ssh->kex;
76 u_int min = 0, max = 0, nbits = 0;
77 const BIGNUM *dh_p, *dh_g;
79 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
80 if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
81 (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
82 (r = sshpkt_get_u32(ssh, &max)) != 0 ||
83 (r = sshpkt_get_end(ssh)) != 0)
88 min = MAXIMUM(DH_GRP_MIN, min);
89 max = MINIMUM(DH_GRP_MAX, max);
90 nbits = MAXIMUM(DH_GRP_MIN, nbits);
91 nbits = MINIMUM(DH_GRP_MAX, nbits);
93 if (kex->max < kex->min || kex->nbits < kex->min ||
94 kex->max < kex->nbits || kex->max < DH_GRP_MIN) {
95 r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
99 /* Contact privileged parent */
100 kex->dh = PRIVSEP(choose_dh(min, nbits, max));
101 if (kex->dh == NULL) {
102 sshpkt_disconnect(ssh, "no matching DH grp found");
103 r = SSH_ERR_ALLOC_FAIL;
106 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
107 DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
108 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
109 (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 ||
110 (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 ||
111 (r = sshpkt_send(ssh)) != 0)
114 /* Compute our exchange value in parallel with the client */
115 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
118 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
119 ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
126 input_kex_dh_gex_init(int type, u_int32_t seq, struct ssh *ssh)
128 struct kex *kex = ssh->kex;
129 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
130 const BIGNUM *pub_key, *dh_p, *dh_g;
131 struct sshkey *server_host_public, *server_host_private;
132 u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
133 u_char hash[SSH_DIGEST_MAX_LENGTH];
134 size_t sbloblen, slen;
135 size_t klen = 0, hashlen;
138 if (kex->load_host_public_key == NULL ||
139 kex->load_host_private_key == NULL) {
140 r = SSH_ERR_INVALID_ARGUMENT;
143 server_host_public = kex->load_host_public_key(kex->hostkey_type,
144 kex->hostkey_nid, ssh);
145 server_host_private = kex->load_host_private_key(kex->hostkey_type,
146 kex->hostkey_nid, ssh);
147 if (server_host_public == NULL) {
148 r = SSH_ERR_NO_HOSTKEY_LOADED;
153 if ((dh_client_pub = BN_new()) == NULL) {
154 r = SSH_ERR_ALLOC_FAIL;
157 if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
158 (r = sshpkt_get_end(ssh)) != 0)
161 DH_get0_key(kex->dh, &pub_key, NULL);
162 DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
165 fprintf(stderr, "dh_client_pub= ");
166 BN_print_fp(stderr, dh_client_pub);
167 fprintf(stderr, "\n");
168 debug("bits %d", BN_num_bits(dh_client_pub));
169 DHparams_print_fp(stderr, kex->dh);
170 fprintf(stderr, "pub= ");
171 BN_print_fp(stderr, pub_key);
172 fprintf(stderr, "\n");
174 if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
175 sshpkt_disconnect(ssh, "bad client public DH value");
176 r = SSH_ERR_MESSAGE_INCOMPLETE;
180 klen = DH_size(kex->dh);
181 if ((kbuf = malloc(klen)) == NULL ||
182 (shared_secret = BN_new()) == NULL) {
183 r = SSH_ERR_ALLOC_FAIL;
186 if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
187 BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
188 r = SSH_ERR_LIBCRYPTO_ERROR;
192 dump_digest("shared secret", kbuf, kout);
194 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
198 hashlen = sizeof(hash);
199 if ((r = kexgex_hash(
201 kex->client_version_string,
202 kex->server_version_string,
203 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
204 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
205 server_host_key_blob, sbloblen,
206 kex->min, kex->nbits, kex->max,
211 hash, &hashlen)) != 0)
214 /* save session id := H */
215 if (kex->session_id == NULL) {
216 kex->session_id_len = hashlen;
217 kex->session_id = malloc(kex->session_id_len);
218 if (kex->session_id == NULL) {
219 r = SSH_ERR_ALLOC_FAIL;
222 memcpy(kex->session_id, hash, kex->session_id_len);
226 if ((r = kex->sign(server_host_private, server_host_public, &signature,
227 &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
230 /* destroy_sensitive_data(); */
232 /* send server hostkey, DH pubkey 'f' and signed H */
233 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
234 (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
235 (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || /* f */
236 (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
237 (r = sshpkt_send(ssh)) != 0)
240 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
241 r = kex_send_newkeys(ssh);
245 BN_clear_free(dh_client_pub);
247 explicit_bzero(kbuf, klen);
250 BN_clear_free(shared_secret);
251 free(server_host_key_blob);
255 #endif /* WITH_OPENSSL */