xref: /freebsd/crypto/openssh/kexgexc.c (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1*1323ec57SEd Maste /* $OpenBSD: kexgexc.c,v 1.38 2021/12/19 22:08:06 djm Exp $ */
2d0c8c0bcSDag-Erling Smørgrav /*
3d0c8c0bcSDag-Erling Smørgrav  * Copyright (c) 2000 Niels Provos.  All rights reserved.
4d0c8c0bcSDag-Erling Smørgrav  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
5d0c8c0bcSDag-Erling Smørgrav  *
6d0c8c0bcSDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
7d0c8c0bcSDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
8d0c8c0bcSDag-Erling Smørgrav  * are met:
9d0c8c0bcSDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
10d0c8c0bcSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer.
11d0c8c0bcSDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
12d0c8c0bcSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
13d0c8c0bcSDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
14d0c8c0bcSDag-Erling Smørgrav  *
15d0c8c0bcSDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16d0c8c0bcSDag-Erling Smørgrav  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17d0c8c0bcSDag-Erling Smørgrav  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18d0c8c0bcSDag-Erling Smørgrav  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19d0c8c0bcSDag-Erling Smørgrav  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20d0c8c0bcSDag-Erling Smørgrav  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21d0c8c0bcSDag-Erling Smørgrav  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22d0c8c0bcSDag-Erling Smørgrav  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23d0c8c0bcSDag-Erling Smørgrav  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24d0c8c0bcSDag-Erling Smørgrav  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25d0c8c0bcSDag-Erling Smørgrav  */
26d0c8c0bcSDag-Erling Smørgrav 
27d0c8c0bcSDag-Erling Smørgrav #include "includes.h"
28761efaa7SDag-Erling Smørgrav 
29bc5531deSDag-Erling Smørgrav #ifdef WITH_OPENSSL
30bc5531deSDag-Erling Smørgrav 
31761efaa7SDag-Erling Smørgrav #include <sys/types.h>
32761efaa7SDag-Erling Smørgrav 
334a421b63SDag-Erling Smørgrav #include <openssl/dh.h>
344a421b63SDag-Erling Smørgrav 
35761efaa7SDag-Erling Smørgrav #include <stdarg.h>
36761efaa7SDag-Erling Smørgrav #include <stdio.h>
37761efaa7SDag-Erling Smørgrav #include <string.h>
38761efaa7SDag-Erling Smørgrav #include <signal.h>
39d0c8c0bcSDag-Erling Smørgrav 
402a01feabSEd Maste #include "openbsd-compat/openssl-compat.h"
412a01feabSEd Maste 
42bc5531deSDag-Erling Smørgrav #include "sshkey.h"
43761efaa7SDag-Erling Smørgrav #include "cipher.h"
44bc5531deSDag-Erling Smørgrav #include "digest.h"
45d0c8c0bcSDag-Erling Smørgrav #include "kex.h"
46d0c8c0bcSDag-Erling Smørgrav #include "log.h"
47d0c8c0bcSDag-Erling Smørgrav #include "packet.h"
48d0c8c0bcSDag-Erling Smørgrav #include "dh.h"
49d0c8c0bcSDag-Erling Smørgrav #include "ssh2.h"
50d0c8c0bcSDag-Erling Smørgrav #include "compat.h"
51bc5531deSDag-Erling Smørgrav #include "dispatch.h"
52bc5531deSDag-Erling Smørgrav #include "ssherr.h"
53bc5531deSDag-Erling Smørgrav #include "sshbuf.h"
54ca86bcf2SDag-Erling Smørgrav #include "misc.h"
55d0c8c0bcSDag-Erling Smørgrav 
564f52dfbbSDag-Erling Smørgrav static int input_kex_dh_gex_group(int, u_int32_t, struct ssh *);
574f52dfbbSDag-Erling Smørgrav static int input_kex_dh_gex_reply(int, u_int32_t, struct ssh *);
58bc5531deSDag-Erling Smørgrav 
59bc5531deSDag-Erling Smørgrav int
kexgex_client(struct ssh * ssh)60bc5531deSDag-Erling Smørgrav kexgex_client(struct ssh *ssh)
61d0c8c0bcSDag-Erling Smørgrav {
62bc5531deSDag-Erling Smørgrav 	struct kex *kex = ssh->kex;
63bc5531deSDag-Erling Smørgrav 	int r;
64bc5531deSDag-Erling Smørgrav 	u_int nbits;
65d0c8c0bcSDag-Erling Smørgrav 
66f7167e0eSDag-Erling Smørgrav 	nbits = dh_estimate(kex->dh_need * 8);
67d0c8c0bcSDag-Erling Smørgrav 
68bc5531deSDag-Erling Smørgrav 	kex->min = DH_GRP_MIN;
69bc5531deSDag-Erling Smørgrav 	kex->max = DH_GRP_MAX;
70bc5531deSDag-Erling Smørgrav 	kex->nbits = nbits;
7119261079SEd Maste 	if (ssh->compat & SSH_BUG_DHGEX_LARGE)
72ca86bcf2SDag-Erling Smørgrav 		kex->nbits = MINIMUM(kex->nbits, 4096);
73d0c8c0bcSDag-Erling Smørgrav 	/* New GEX request */
74bc5531deSDag-Erling Smørgrav 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 ||
75bc5531deSDag-Erling Smørgrav 	    (r = sshpkt_put_u32(ssh, kex->min)) != 0 ||
76bc5531deSDag-Erling Smørgrav 	    (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
77bc5531deSDag-Erling Smørgrav 	    (r = sshpkt_put_u32(ssh, kex->max)) != 0 ||
78bc5531deSDag-Erling Smørgrav 	    (r = sshpkt_send(ssh)) != 0)
79bc5531deSDag-Erling Smørgrav 		goto out;
80efcad6b7SDag-Erling Smørgrav 	debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
81bc5531deSDag-Erling Smørgrav 	    kex->min, kex->nbits, kex->max);
82d0c8c0bcSDag-Erling Smørgrav #ifdef DEBUG_KEXDH
83d0c8c0bcSDag-Erling Smørgrav 	fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
84bc5531deSDag-Erling Smørgrav 	    kex->min, kex->nbits, kex->max);
85d0c8c0bcSDag-Erling Smørgrav #endif
8619261079SEd Maste 	debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
87bc5531deSDag-Erling Smørgrav 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP,
88bc5531deSDag-Erling Smørgrav 	    &input_kex_dh_gex_group);
89bc5531deSDag-Erling Smørgrav 	r = 0;
90bc5531deSDag-Erling Smørgrav  out:
91bc5531deSDag-Erling Smørgrav 	return r;
92bc5531deSDag-Erling Smørgrav }
93d0c8c0bcSDag-Erling Smørgrav 
94bc5531deSDag-Erling Smørgrav static int
input_kex_dh_gex_group(int type,u_int32_t seq,struct ssh * ssh)954f52dfbbSDag-Erling Smørgrav input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh)
96bc5531deSDag-Erling Smørgrav {
97bc5531deSDag-Erling Smørgrav 	struct kex *kex = ssh->kex;
98bc5531deSDag-Erling Smørgrav 	BIGNUM *p = NULL, *g = NULL;
992a01feabSEd Maste 	const BIGNUM *pub_key;
100bc5531deSDag-Erling Smørgrav 	int r, bits;
101d0c8c0bcSDag-Erling Smørgrav 
10219261079SEd Maste 	debug("SSH2_MSG_KEX_DH_GEX_GROUP received");
10319261079SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, &kex_protocol_error);
104d0c8c0bcSDag-Erling Smørgrav 
10519261079SEd Maste 	if ((r = sshpkt_get_bignum2(ssh, &p)) != 0 ||
10619261079SEd Maste 	    (r = sshpkt_get_bignum2(ssh, &g)) != 0 ||
107bc5531deSDag-Erling Smørgrav 	    (r = sshpkt_get_end(ssh)) != 0)
108bc5531deSDag-Erling Smørgrav 		goto out;
109bc5531deSDag-Erling Smørgrav 	if ((bits = BN_num_bits(p)) < 0 ||
110bc5531deSDag-Erling Smørgrav 	    (u_int)bits < kex->min || (u_int)bits > kex->max) {
111bc5531deSDag-Erling Smørgrav 		r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
112bc5531deSDag-Erling Smørgrav 		goto out;
113bc5531deSDag-Erling Smørgrav 	}
114bc5531deSDag-Erling Smørgrav 	if ((kex->dh = dh_new_group(g, p)) == NULL) {
115bc5531deSDag-Erling Smørgrav 		r = SSH_ERR_ALLOC_FAIL;
116bc5531deSDag-Erling Smørgrav 		goto out;
117bc5531deSDag-Erling Smørgrav 	}
118bc5531deSDag-Erling Smørgrav 	p = g = NULL; /* belong to kex->dh now */
119d0c8c0bcSDag-Erling Smørgrav 
120bc5531deSDag-Erling Smørgrav 	/* generate and send 'e', client DH public key */
1212a01feabSEd Maste 	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
1222a01feabSEd Maste 		goto out;
1232a01feabSEd Maste 	DH_get0_key(kex->dh, &pub_key, NULL);
1242a01feabSEd Maste 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
1252a01feabSEd Maste 	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
126bc5531deSDag-Erling Smørgrav 	    (r = sshpkt_send(ssh)) != 0)
127bc5531deSDag-Erling Smørgrav 		goto out;
128bc5531deSDag-Erling Smørgrav 	debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
129d0c8c0bcSDag-Erling Smørgrav #ifdef DEBUG_KEXDH
130bc5531deSDag-Erling Smørgrav 	DHparams_print_fp(stderr, kex->dh);
131d0c8c0bcSDag-Erling Smørgrav 	fprintf(stderr, "pub= ");
1322a01feabSEd Maste 	BN_print_fp(stderr, pub_key);
133d0c8c0bcSDag-Erling Smørgrav 	fprintf(stderr, "\n");
134d0c8c0bcSDag-Erling Smørgrav #endif
13519261079SEd Maste 	debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
136bc5531deSDag-Erling Smørgrav 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
137bc5531deSDag-Erling Smørgrav 	r = 0;
138bc5531deSDag-Erling Smørgrav out:
139bc5531deSDag-Erling Smørgrav 	BN_clear_free(p);
140bc5531deSDag-Erling Smørgrav 	BN_clear_free(g);
141bc5531deSDag-Erling Smørgrav 	return r;
142bc5531deSDag-Erling Smørgrav }
143d0c8c0bcSDag-Erling Smørgrav 
144bc5531deSDag-Erling Smørgrav static int
input_kex_dh_gex_reply(int type,u_int32_t seq,struct ssh * ssh)1454f52dfbbSDag-Erling Smørgrav input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh)
146bc5531deSDag-Erling Smørgrav {
147bc5531deSDag-Erling Smørgrav 	struct kex *kex = ssh->kex;
14819261079SEd Maste 	BIGNUM *dh_server_pub = NULL;
1492a01feabSEd Maste 	const BIGNUM *pub_key, *dh_p, *dh_g;
15019261079SEd Maste 	struct sshbuf *shared_secret = NULL;
15119261079SEd Maste 	struct sshbuf *tmp = NULL, *server_host_key_blob = NULL;
152bc5531deSDag-Erling Smørgrav 	struct sshkey *server_host_key = NULL;
15319261079SEd Maste 	u_char *signature = NULL;
154bc5531deSDag-Erling Smørgrav 	u_char hash[SSH_DIGEST_MAX_LENGTH];
15519261079SEd Maste 	size_t slen, hashlen;
15619261079SEd Maste 	int r;
157d0c8c0bcSDag-Erling Smørgrav 
15819261079SEd Maste 	debug("SSH2_MSG_KEX_DH_GEX_REPLY received");
15919261079SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &kex_protocol_error);
16019261079SEd Maste 
161d0c8c0bcSDag-Erling Smørgrav 	/* key, cert */
16219261079SEd Maste 	if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
163bc5531deSDag-Erling Smørgrav 		goto out;
16419261079SEd Maste 	/* sshkey_fromb() consumes its buffer, so make a copy */
16519261079SEd Maste 	if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) {
166bc5531deSDag-Erling Smørgrav 		r = SSH_ERR_ALLOC_FAIL;
167bc5531deSDag-Erling Smørgrav 		goto out;
168bc5531deSDag-Erling Smørgrav 	}
16919261079SEd Maste 	if ((r = sshkey_fromb(tmp, &server_host_key)) != 0 ||
17019261079SEd Maste 	    (r = kex_verify_host_key(ssh, server_host_key)) != 0)
17119261079SEd Maste 		goto out;
17219261079SEd Maste 	/* DH parameter f, server public DH key, signed H */
17319261079SEd Maste 	if ((r = sshpkt_get_bignum2(ssh, &dh_server_pub)) != 0 ||
174bc5531deSDag-Erling Smørgrav 	    (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
175bc5531deSDag-Erling Smørgrav 	    (r = sshpkt_get_end(ssh)) != 0)
176bc5531deSDag-Erling Smørgrav 		goto out;
17719261079SEd Maste 	if ((shared_secret = sshbuf_new()) == NULL) {
178bc5531deSDag-Erling Smørgrav 		r = SSH_ERR_ALLOC_FAIL;
179bc5531deSDag-Erling Smørgrav 		goto out;
180bc5531deSDag-Erling Smørgrav 	}
18119261079SEd Maste 	if ((r = kex_dh_compute_key(kex, dh_server_pub, shared_secret)) != 0)
182bc5531deSDag-Erling Smørgrav 		goto out;
183bc5531deSDag-Erling Smørgrav 	if (ssh->compat & SSH_OLD_DHGEX)
184bc5531deSDag-Erling Smørgrav 		kex->min = kex->max = -1;
185d0c8c0bcSDag-Erling Smørgrav 
186d0c8c0bcSDag-Erling Smørgrav 	/* calc and verify H */
1872a01feabSEd Maste 	DH_get0_key(kex->dh, &pub_key, NULL);
1882a01feabSEd Maste 	DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
189bc5531deSDag-Erling Smørgrav 	hashlen = sizeof(hash);
190bc5531deSDag-Erling Smørgrav 	if ((r = kexgex_hash(
191f7167e0eSDag-Erling Smørgrav 	    kex->hash_alg,
19219261079SEd Maste 	    kex->client_version,
19319261079SEd Maste 	    kex->server_version,
19419261079SEd Maste 	    kex->my,
19519261079SEd Maste 	    kex->peer,
19619261079SEd Maste 	    server_host_key_blob,
197bc5531deSDag-Erling Smørgrav 	    kex->min, kex->nbits, kex->max,
1982a01feabSEd Maste 	    dh_p, dh_g,
1992a01feabSEd Maste 	    pub_key,
200d0c8c0bcSDag-Erling Smørgrav 	    dh_server_pub,
20119261079SEd Maste 	    sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
202bc5531deSDag-Erling Smørgrav 	    hash, &hashlen)) != 0)
203bc5531deSDag-Erling Smørgrav 		goto out;
204021d409fSDag-Erling Smørgrav 
205bc5531deSDag-Erling Smørgrav 	if ((r = sshkey_verify(server_host_key, signature, slen, hash,
20619261079SEd Maste 	    hashlen, kex->hostkey_alg, ssh->compat, NULL)) != 0)
207bc5531deSDag-Erling Smørgrav 		goto out;
208d0c8c0bcSDag-Erling Smørgrav 
209*1323ec57SEd Maste 	if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
210*1323ec57SEd Maste 	    (r = kex_send_newkeys(ssh)) != 0)
211*1323ec57SEd Maste 		goto out;
212*1323ec57SEd Maste 
213*1323ec57SEd Maste 	/* save initial signature and hostkey */
214*1323ec57SEd Maste 	if ((kex->flags & KEX_INITIAL) != 0) {
215*1323ec57SEd Maste 		if (kex->initial_hostkey != NULL || kex->initial_sig != NULL) {
216*1323ec57SEd Maste 			r = SSH_ERR_INTERNAL_ERROR;
217*1323ec57SEd Maste 			goto out;
218*1323ec57SEd Maste 		}
219*1323ec57SEd Maste 		if ((kex->initial_sig = sshbuf_new()) == NULL) {
220*1323ec57SEd Maste 			r = SSH_ERR_ALLOC_FAIL;
221*1323ec57SEd Maste 			goto out;
222*1323ec57SEd Maste 		}
223*1323ec57SEd Maste 		if ((r = sshbuf_put(kex->initial_sig, signature, slen)) != 0)
224*1323ec57SEd Maste 			goto out;
225*1323ec57SEd Maste 		kex->initial_hostkey = server_host_key;
226*1323ec57SEd Maste 		server_host_key = NULL;
227*1323ec57SEd Maste 	}
228*1323ec57SEd Maste 	/* success */
229bc5531deSDag-Erling Smørgrav  out:
230bc5531deSDag-Erling Smørgrav 	explicit_bzero(hash, sizeof(hash));
231bc5531deSDag-Erling Smørgrav 	DH_free(kex->dh);
232bc5531deSDag-Erling Smørgrav 	kex->dh = NULL;
233bc5531deSDag-Erling Smørgrav 	BN_clear_free(dh_server_pub);
23419261079SEd Maste 	sshbuf_free(shared_secret);
235bc5531deSDag-Erling Smørgrav 	sshkey_free(server_host_key);
23619261079SEd Maste 	sshbuf_free(tmp);
23719261079SEd Maste 	sshbuf_free(server_host_key_blob);
238bc5531deSDag-Erling Smørgrav 	free(signature);
239bc5531deSDag-Erling Smørgrav 	return r;
240bc5531deSDag-Erling Smørgrav }
241bc5531deSDag-Erling Smørgrav #endif /* WITH_OPENSSL */
242