1/* $OpenBSD: kexgexs.c,v 1.47 2024/05/17 00:30:23 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
27
28#include <stdio.h>
29#include <string.h>
30#include <signal.h>
31
32#include <openssl/dh.h>
33
34#include "sshkey.h"
35#include "cipher.h"
36#include "digest.h"
37#include "kex.h"
38#include "log.h"
39#include "packet.h"
40#include "dh.h"
41#include "ssh2.h"
42#ifdef GSSAPI
43#include "ssh-gss.h"
44#endif
45#include "monitor_wrap.h"
46#include "dispatch.h"
47#include "ssherr.h"
48#include "sshbuf.h"
49#include "misc.h"
50
51static int input_kex_dh_gex_request(int, u_int32_t, struct ssh *);
52static int input_kex_dh_gex_init(int, u_int32_t, struct ssh *);
53
54int
55kexgex_server(struct ssh *ssh)
56{
57	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST,
58	    &input_kex_dh_gex_request);
59	debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST");
60	return 0;
61}
62
63static int
64input_kex_dh_gex_request(int type, u_int32_t seq, struct ssh *ssh)
65{
66	struct kex *kex = ssh->kex;
67	int r;
68	u_int min = 0, max = 0, nbits = 0;
69	const BIGNUM *dh_p, *dh_g;
70
71	debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
72	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST, &kex_protocol_error);
73
74	if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
75	    (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
76	    (r = sshpkt_get_u32(ssh, &max)) != 0 ||
77	    (r = sshpkt_get_end(ssh)) != 0)
78		goto out;
79	kex->nbits = nbits;
80	kex->min = min;
81	kex->max = max;
82	min = MAXIMUM(DH_GRP_MIN, min);
83	max = MINIMUM(DH_GRP_MAX, max);
84	nbits = MAXIMUM(DH_GRP_MIN, nbits);
85	nbits = MINIMUM(DH_GRP_MAX, nbits);
86
87	if (kex->max < kex->min || kex->nbits < kex->min ||
88	    kex->max < kex->nbits || kex->max < DH_GRP_MIN) {
89		r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
90		goto out;
91	}
92
93	/* Contact privileged parent */
94	kex->dh = mm_choose_dh(min, nbits, max);
95	if (kex->dh == NULL) {
96		(void)sshpkt_disconnect(ssh, "no matching DH grp found");
97		r = SSH_ERR_ALLOC_FAIL;
98		goto out;
99	}
100	debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
101	DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
102	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
103	    (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 ||
104	    (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 ||
105	    (r = sshpkt_send(ssh)) != 0)
106		goto out;
107
108	/* Compute our exchange value in parallel with the client */
109	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
110		goto out;
111
112	debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
113	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
114	r = 0;
115 out:
116	return r;
117}
118
119static int
120input_kex_dh_gex_init(int type, u_int32_t seq, struct ssh *ssh)
121{
122	struct kex *kex = ssh->kex;
123	BIGNUM *dh_client_pub = NULL;
124	const BIGNUM *pub_key, *dh_p, *dh_g;
125	struct sshbuf *shared_secret = NULL;
126	struct sshbuf *server_host_key_blob = NULL;
127	struct sshkey *server_host_public, *server_host_private;
128	u_char *signature = NULL;
129	u_char hash[SSH_DIGEST_MAX_LENGTH];
130	size_t slen, hashlen;
131	int r;
132
133	debug("SSH2_MSG_KEX_DH_GEX_INIT received");
134	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &kex_protocol_error);
135
136	if ((r = kex_load_hostkey(ssh, &server_host_private,
137	    &server_host_public)) != 0)
138		goto out;
139
140	/* key, cert */
141	if ((r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 ||
142	    (r = sshpkt_get_end(ssh)) != 0)
143		goto out;
144	if ((shared_secret = sshbuf_new()) == NULL) {
145		r = SSH_ERR_ALLOC_FAIL;
146		goto out;
147	}
148	if ((r = kex_dh_compute_key(kex, dh_client_pub, shared_secret)) != 0)
149		goto out;
150	if ((server_host_key_blob = sshbuf_new()) == NULL) {
151		r = SSH_ERR_ALLOC_FAIL;
152		goto out;
153	}
154	if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0)
155		goto out;
156
157	/* calc H */
158	DH_get0_key(kex->dh, &pub_key, NULL);
159	DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
160	hashlen = sizeof(hash);
161	if ((r = kexgex_hash(
162	    kex->hash_alg,
163	    kex->client_version,
164	    kex->server_version,
165	    kex->peer,
166	    kex->my,
167	    server_host_key_blob,
168	    kex->min, kex->nbits, kex->max,
169	    dh_p, dh_g,
170	    dh_client_pub,
171	    pub_key,
172	    sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
173	    hash, &hashlen)) != 0)
174		goto out;
175
176	/* sign H */
177	if ((r = kex->sign(ssh, server_host_private, server_host_public,
178	    &signature, &slen, hash, hashlen, kex->hostkey_alg)) < 0)
179		goto out;
180
181	/* send server hostkey, DH pubkey 'f' and signed H */
182	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
183	    (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 ||
184	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||     /* f */
185	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
186	    (r = sshpkt_send(ssh)) != 0)
187		goto out;
188
189	if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
190	    (r = kex_send_newkeys(ssh)) != 0)
191		goto out;
192
193	/* retain copy of hostkey used at initial KEX */
194	if (kex->initial_hostkey == NULL &&
195	    (r = sshkey_from_private(server_host_public,
196	    &kex->initial_hostkey)) != 0)
197		goto out;
198	/* success */
199 out:
200	explicit_bzero(hash, sizeof(hash));
201	DH_free(kex->dh);
202	kex->dh = NULL;
203	BN_clear_free(dh_client_pub);
204	sshbuf_free(shared_secret);
205	sshbuf_free(server_host_key_blob);
206	free(signature);
207	return r;
208}
209