xref: /netbsd-src/crypto/external/bsd/openssh/dist/kexgexs.c (revision 1c7715dda22cf2bd169e2f84953c050393e8fe9c)
1 /*	$NetBSD: kexgexs.c,v 1.24 2024/07/08 22:33:43 christos Exp $	*/
2 /* $OpenBSD: kexgexs.c,v 1.47 2024/05/17 00:30:23 djm Exp $ */
3 
4 /*
5  * Copyright (c) 2000 Niels Provos.  All rights reserved.
6  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "includes.h"
30 __RCSID("$NetBSD: kexgexs.c,v 1.24 2024/07/08 22:33:43 christos Exp $");
31 
32 #include <sys/param.h>	/* MIN MAX */
33 #include <stdio.h>
34 #include <string.h>
35 #include <signal.h>
36 
37 #include <openssl/dh.h>
38 
39 #include "sshkey.h"
40 #include "cipher.h"
41 #include "digest.h"
42 #include "kex.h"
43 #include "log.h"
44 #include "packet.h"
45 #include "dh.h"
46 #include "ssh2.h"
47 #ifdef GSSAPI
48 #include "ssh-gss.h"
49 #endif
50 #include "monitor_wrap.h"
51 #include "dispatch.h"
52 #include "ssherr.h"
53 #include "sshbuf.h"
54 #include "misc.h"
55 
56 static int input_kex_dh_gex_request(int, u_int32_t, struct ssh *);
57 static int input_kex_dh_gex_init(int, u_int32_t, struct ssh *);
58 
59 int
kexgex_server(struct ssh * ssh)60 kexgex_server(struct ssh *ssh)
61 {
62 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST,
63 	    &input_kex_dh_gex_request);
64 	debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST");
65 	return 0;
66 }
67 
68 static int
input_kex_dh_gex_request(int type,u_int32_t seq,struct ssh * ssh)69 input_kex_dh_gex_request(int type, u_int32_t seq, struct ssh *ssh)
70 {
71 	struct kex *kex = ssh->kex;
72 	int r;
73 	u_int min = 0, max = 0, nbits = 0;
74 	const BIGNUM *dh_p, *dh_g;
75 
76 	debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
77 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST, &kex_protocol_error);
78 
79 	if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
80 	    (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
81 	    (r = sshpkt_get_u32(ssh, &max)) != 0 ||
82 	    (r = sshpkt_get_end(ssh)) != 0)
83 		goto out;
84 	kex->nbits = nbits;
85 	kex->min = min;
86 	kex->max = max;
87 	min = MAXIMUM(DH_GRP_MIN, min);
88 	max = MINIMUM(DH_GRP_MAX, max);
89 	nbits = MAXIMUM(DH_GRP_MIN, nbits);
90 	nbits = MINIMUM(DH_GRP_MAX, nbits);
91 
92 	if (kex->max < kex->min || kex->nbits < kex->min ||
93 	    kex->max < kex->nbits || kex->max < DH_GRP_MIN) {
94 		r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
95 		goto out;
96 	}
97 
98 	/* Contact privileged parent */
99 	kex->dh = mm_choose_dh(min, nbits, max);
100 	if (kex->dh == NULL) {
101 		(void)sshpkt_disconnect(ssh, "no matching DH grp found");
102 		r = SSH_ERR_ALLOC_FAIL;
103 		goto out;
104 	}
105 	debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
106 	DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
107 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
108 	    (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 ||
109 	    (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 ||
110 	    (r = sshpkt_send(ssh)) != 0)
111 		goto out;
112 
113 	/* Compute our exchange value in parallel with the client */
114 	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
115 		goto out;
116 
117 	debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
118 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
119 	r = 0;
120  out:
121 	return r;
122 }
123 
124 static int
input_kex_dh_gex_init(int type,u_int32_t seq,struct ssh * ssh)125 input_kex_dh_gex_init(int type, u_int32_t seq, struct ssh *ssh)
126 {
127 	struct kex *kex = ssh->kex;
128 	BIGNUM *dh_client_pub = NULL;
129 	const BIGNUM *pub_key, *dh_p, *dh_g;
130 	struct sshbuf *shared_secret = NULL;
131 	struct sshbuf *server_host_key_blob = NULL;
132 	struct sshkey *server_host_public, *server_host_private;
133 	u_char *signature = NULL;
134 	u_char hash[SSH_DIGEST_MAX_LENGTH];
135 	size_t slen, hashlen;
136 	int r;
137 
138 	debug("SSH2_MSG_KEX_DH_GEX_INIT received");
139 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &kex_protocol_error);
140 
141 	if ((r = kex_load_hostkey(ssh, &server_host_private,
142 	    &server_host_public)) != 0)
143 		goto out;
144 
145 	/* key, cert */
146 	if ((r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 ||
147 	    (r = sshpkt_get_end(ssh)) != 0)
148 		goto out;
149 	if ((shared_secret = sshbuf_new()) == NULL) {
150 		r = SSH_ERR_ALLOC_FAIL;
151 		goto out;
152 	}
153 	if ((r = kex_dh_compute_key(kex, dh_client_pub, shared_secret)) != 0)
154 		goto out;
155 	if ((server_host_key_blob = sshbuf_new()) == NULL) {
156 		r = SSH_ERR_ALLOC_FAIL;
157 		goto out;
158 	}
159 	if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0)
160 		goto out;
161 
162 	/* calc H */
163 	DH_get0_key(kex->dh, &pub_key, NULL);
164 	DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
165 	hashlen = sizeof(hash);
166 	if ((r = kexgex_hash(
167 	    kex->hash_alg,
168 	    kex->client_version,
169 	    kex->server_version,
170 	    kex->peer,
171 	    kex->my,
172 	    server_host_key_blob,
173 	    kex->min, kex->nbits, kex->max,
174 	    dh_p, dh_g,
175 	    dh_client_pub,
176 	    pub_key,
177 	    sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
178 	    hash, &hashlen)) != 0)
179 		goto out;
180 
181 	/* sign H */
182 	if ((r = kex->sign(ssh, server_host_private, server_host_public,
183 	    &signature, &slen, hash, hashlen, kex->hostkey_alg)) < 0)
184 		goto out;
185 
186 	/* send server hostkey, DH pubkey 'f' and signed H */
187 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
188 	    (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 ||
189 	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||     /* f */
190 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
191 	    (r = sshpkt_send(ssh)) != 0)
192 		goto out;
193 
194 	if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
195 	    (r = kex_send_newkeys(ssh)) != 0)
196 		goto out;
197 
198 	/* retain copy of hostkey used at initial KEX */
199 	if (kex->initial_hostkey == NULL &&
200 	    (r = sshkey_from_private(server_host_public,
201 	    &kex->initial_hostkey)) != 0)
202 		goto out;
203 	/* success */
204  out:
205 	explicit_bzero(hash, sizeof(hash));
206 	DH_free(kex->dh);
207 	kex->dh = NULL;
208 	BN_clear_free(dh_client_pub);
209 	sshbuf_free(shared_secret);
210 	sshbuf_free(server_host_key_blob);
211 	free(signature);
212 	return r;
213 }
214