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