xref: /openbsd-src/usr.bin/ssh/kexgen.c (revision 7c0ec4b8992567abb1e1536622dc789a9a39d9f1)
1 /* $OpenBSD: kexgen.c,v 1.9 2024/09/02 12:13:56 djm Exp $ */
2 /*
3  * Copyright (c) 2019 Markus Friedl.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/types.h>
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <signal.h>
31 
32 #include "sshkey.h"
33 #include "kex.h"
34 #include "log.h"
35 #include "packet.h"
36 #include "ssh2.h"
37 #include "sshbuf.h"
38 #include "digest.h"
39 #include "ssherr.h"
40 
41 static int input_kex_gen_init(int, u_int32_t, struct ssh *);
42 static int input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh);
43 
44 static int
45 kex_gen_hash(
46     int hash_alg,
47     const struct sshbuf *client_version,
48     const struct sshbuf *server_version,
49     const struct sshbuf *client_kexinit,
50     const struct sshbuf *server_kexinit,
51     const struct sshbuf *server_host_key_blob,
52     const struct sshbuf *client_pub,
53     const struct sshbuf *server_pub,
54     const struct sshbuf *shared_secret,
55     u_char *hash, size_t *hashlen)
56 {
57 	struct sshbuf *b;
58 	int r;
59 
60 	if (*hashlen < ssh_digest_bytes(hash_alg))
61 		return SSH_ERR_INVALID_ARGUMENT;
62 	if ((b = sshbuf_new()) == NULL)
63 		return SSH_ERR_ALLOC_FAIL;
64 	if ((r = sshbuf_put_stringb(b, client_version)) != 0 ||
65 	    (r = sshbuf_put_stringb(b, server_version)) != 0 ||
66 	    /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
67 	    (r = sshbuf_put_u32(b, sshbuf_len(client_kexinit) + 1)) != 0 ||
68 	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
69 	    (r = sshbuf_putb(b, client_kexinit)) != 0 ||
70 	    (r = sshbuf_put_u32(b, sshbuf_len(server_kexinit) + 1)) != 0 ||
71 	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
72 	    (r = sshbuf_putb(b, server_kexinit)) != 0 ||
73 	    (r = sshbuf_put_stringb(b, server_host_key_blob)) != 0 ||
74 	    (r = sshbuf_put_stringb(b, client_pub)) != 0 ||
75 	    (r = sshbuf_put_stringb(b, server_pub)) != 0 ||
76 	    (r = sshbuf_putb(b, shared_secret)) != 0) {
77 		sshbuf_free(b);
78 		return r;
79 	}
80 #ifdef DEBUG_KEX
81 	sshbuf_dump(b, stderr);
82 #endif
83 	if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
84 		sshbuf_free(b);
85 		return SSH_ERR_LIBCRYPTO_ERROR;
86 	}
87 	sshbuf_free(b);
88 	*hashlen = ssh_digest_bytes(hash_alg);
89 #ifdef DEBUG_KEX
90 	dump_digest("hash", hash, *hashlen);
91 #endif
92 	return 0;
93 }
94 
95 int
96 kex_gen_client(struct ssh *ssh)
97 {
98 	struct kex *kex = ssh->kex;
99 	int r;
100 
101 	switch (kex->kex_type) {
102 #ifdef WITH_OPENSSL
103 	case KEX_DH_GRP1_SHA1:
104 	case KEX_DH_GRP14_SHA1:
105 	case KEX_DH_GRP14_SHA256:
106 	case KEX_DH_GRP16_SHA512:
107 	case KEX_DH_GRP18_SHA512:
108 		r = kex_dh_keypair(kex);
109 		break;
110 	case KEX_ECDH_SHA2:
111 		r = kex_ecdh_keypair(kex);
112 		break;
113 #endif /* WITH_OPENSSL */
114 	case KEX_C25519_SHA256:
115 		r = kex_c25519_keypair(kex);
116 		break;
117 	case KEX_KEM_SNTRUP761X25519_SHA512:
118 		r = kex_kem_sntrup761x25519_keypair(kex);
119 		break;
120 #ifdef WITH_MLKEM
121 	case KEX_KEM_MLKEM768X25519_SHA256:
122 		r = kex_kem_mlkem768x25519_keypair(kex);
123 		break;
124 #endif
125 	default:
126 		r = SSH_ERR_INVALID_ARGUMENT;
127 		break;
128 	}
129 	if (r != 0)
130 		return r;
131 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
132 	    (r = sshpkt_put_stringb(ssh, kex->client_pub)) != 0 ||
133 	    (r = sshpkt_send(ssh)) != 0)
134 		return r;
135 	debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
136 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_gen_reply);
137 	return 0;
138 }
139 
140 static int
141 input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh)
142 {
143 	struct kex *kex = ssh->kex;
144 	struct sshkey *server_host_key = NULL;
145 	struct sshbuf *shared_secret = NULL;
146 	struct sshbuf *server_blob = NULL;
147 	struct sshbuf *tmp = NULL, *server_host_key_blob = NULL;
148 	u_char *signature = NULL;
149 	u_char hash[SSH_DIGEST_MAX_LENGTH];
150 	size_t slen, hashlen;
151 	int r;
152 
153 	debug("SSH2_MSG_KEX_ECDH_REPLY received");
154 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &kex_protocol_error);
155 
156 	/* hostkey */
157 	if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
158 		goto out;
159 	/* sshkey_fromb() consumes its buffer, so make a copy */
160 	if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) {
161 		r = SSH_ERR_ALLOC_FAIL;
162 		goto out;
163 	}
164 	if ((r = sshkey_fromb(tmp, &server_host_key)) != 0)
165 		goto out;
166 	if ((r = kex_verify_host_key(ssh, server_host_key)) != 0)
167 		goto out;
168 
169 	/* Q_S, server public key */
170 	/* signed H */
171 	if ((r = sshpkt_getb_froms(ssh, &server_blob)) != 0 ||
172 	    (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
173 	    (r = sshpkt_get_end(ssh)) != 0)
174 		goto out;
175 
176 	/* compute shared secret */
177 	switch (kex->kex_type) {
178 #ifdef WITH_OPENSSL
179 	case KEX_DH_GRP1_SHA1:
180 	case KEX_DH_GRP14_SHA1:
181 	case KEX_DH_GRP14_SHA256:
182 	case KEX_DH_GRP16_SHA512:
183 	case KEX_DH_GRP18_SHA512:
184 		r = kex_dh_dec(kex, server_blob, &shared_secret);
185 		break;
186 	case KEX_ECDH_SHA2:
187 		r = kex_ecdh_dec(kex, server_blob, &shared_secret);
188 		break;
189 #endif /* WITH_OPENSSL */
190 	case KEX_C25519_SHA256:
191 		r = kex_c25519_dec(kex, server_blob, &shared_secret);
192 		break;
193 	case KEX_KEM_SNTRUP761X25519_SHA512:
194 		r = kex_kem_sntrup761x25519_dec(kex, server_blob,
195 		    &shared_secret);
196 		break;
197 #ifdef WITH_MLKEM
198 	case KEX_KEM_MLKEM768X25519_SHA256:
199 		r = kex_kem_mlkem768x25519_dec(kex, server_blob,
200 		    &shared_secret);
201 		break;
202 #endif
203 	default:
204 		r = SSH_ERR_INVALID_ARGUMENT;
205 		break;
206 	}
207 	if (r !=0 )
208 		goto out;
209 
210 	/* calc and verify H */
211 	hashlen = sizeof(hash);
212 	if ((r = kex_gen_hash(
213 	    kex->hash_alg,
214 	    kex->client_version,
215 	    kex->server_version,
216 	    kex->my,
217 	    kex->peer,
218 	    server_host_key_blob,
219 	    kex->client_pub,
220 	    server_blob,
221 	    shared_secret,
222 	    hash, &hashlen)) != 0)
223 		goto out;
224 
225 	if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
226 	    kex->hostkey_alg, ssh->compat, NULL)) != 0)
227 		goto out;
228 
229 	if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
230 	    (r = kex_send_newkeys(ssh)) != 0)
231 		goto out;
232 
233 	/* save initial signature and hostkey */
234 	if ((kex->flags & KEX_INITIAL) != 0) {
235 		if (kex->initial_hostkey != NULL || kex->initial_sig != NULL) {
236 			r = SSH_ERR_INTERNAL_ERROR;
237 			goto out;
238 		}
239 		if ((kex->initial_sig = sshbuf_new()) == NULL) {
240 			r = SSH_ERR_ALLOC_FAIL;
241 			goto out;
242 		}
243 		if ((r = sshbuf_put(kex->initial_sig, signature, slen)) != 0)
244 			goto out;
245 		kex->initial_hostkey = server_host_key;
246 		server_host_key = NULL;
247 	}
248 	/* success */
249 out:
250 	explicit_bzero(hash, sizeof(hash));
251 	explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
252 	explicit_bzero(kex->sntrup761_client_key,
253 	    sizeof(kex->sntrup761_client_key));
254 	explicit_bzero(kex->mlkem768_client_key,
255 	    sizeof(kex->mlkem768_client_key));
256 	sshbuf_free(server_host_key_blob);
257 	free(signature);
258 	sshbuf_free(tmp);
259 	sshkey_free(server_host_key);
260 	sshbuf_free(server_blob);
261 	sshbuf_free(shared_secret);
262 	sshbuf_free(kex->client_pub);
263 	kex->client_pub = NULL;
264 	return r;
265 }
266 
267 int
268 kex_gen_server(struct ssh *ssh)
269 {
270 	debug("expecting SSH2_MSG_KEX_ECDH_INIT");
271 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_gen_init);
272 	return 0;
273 }
274 
275 static int
276 input_kex_gen_init(int type, u_int32_t seq, struct ssh *ssh)
277 {
278 	struct kex *kex = ssh->kex;
279 	struct sshkey *server_host_private, *server_host_public;
280 	struct sshbuf *shared_secret = NULL;
281 	struct sshbuf *server_pubkey = NULL;
282 	struct sshbuf *client_pubkey = NULL;
283 	struct sshbuf *server_host_key_blob = NULL;
284 	u_char *signature = NULL, hash[SSH_DIGEST_MAX_LENGTH];
285 	size_t slen, hashlen;
286 	int r;
287 
288 	debug("SSH2_MSG_KEX_ECDH_INIT received");
289 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &kex_protocol_error);
290 
291 	if ((r = kex_load_hostkey(ssh, &server_host_private,
292 	    &server_host_public)) != 0)
293 		goto out;
294 
295 	if ((r = sshpkt_getb_froms(ssh, &client_pubkey)) != 0 ||
296 	    (r = sshpkt_get_end(ssh)) != 0)
297 		goto out;
298 
299 	/* compute shared secret */
300 	switch (kex->kex_type) {
301 #ifdef WITH_OPENSSL
302 	case KEX_DH_GRP1_SHA1:
303 	case KEX_DH_GRP14_SHA1:
304 	case KEX_DH_GRP14_SHA256:
305 	case KEX_DH_GRP16_SHA512:
306 	case KEX_DH_GRP18_SHA512:
307 		r = kex_dh_enc(kex, client_pubkey, &server_pubkey,
308 		    &shared_secret);
309 		break;
310 	case KEX_ECDH_SHA2:
311 		r = kex_ecdh_enc(kex, client_pubkey, &server_pubkey,
312 		    &shared_secret);
313 		break;
314 #endif /* WITH_OPENSSL */
315 	case KEX_C25519_SHA256:
316 		r = kex_c25519_enc(kex, client_pubkey, &server_pubkey,
317 		    &shared_secret);
318 		break;
319 	case KEX_KEM_SNTRUP761X25519_SHA512:
320 		r = kex_kem_sntrup761x25519_enc(kex, client_pubkey,
321 		    &server_pubkey, &shared_secret);
322 		break;
323 #ifdef WITH_MLKEM
324 	case KEX_KEM_MLKEM768X25519_SHA256:
325 		r = kex_kem_mlkem768x25519_enc(kex, client_pubkey,
326 		    &server_pubkey, &shared_secret);
327 		break;
328 #endif
329 	default:
330 		r = SSH_ERR_INVALID_ARGUMENT;
331 		break;
332 	}
333 	if (r !=0 )
334 		goto out;
335 
336 	/* calc H */
337 	if ((server_host_key_blob = sshbuf_new()) == NULL) {
338 		r = SSH_ERR_ALLOC_FAIL;
339 		goto out;
340 	}
341 	if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0)
342 		goto out;
343 	hashlen = sizeof(hash);
344 	if ((r = kex_gen_hash(
345 	    kex->hash_alg,
346 	    kex->client_version,
347 	    kex->server_version,
348 	    kex->peer,
349 	    kex->my,
350 	    server_host_key_blob,
351 	    client_pubkey,
352 	    server_pubkey,
353 	    shared_secret,
354 	    hash, &hashlen)) != 0)
355 		goto out;
356 
357 	/* sign H */
358 	if ((r = kex->sign(ssh, server_host_private, server_host_public,
359 	    &signature, &slen, hash, hashlen, kex->hostkey_alg)) != 0)
360 		goto out;
361 
362 	/* send server hostkey, ECDH pubkey 'Q_S' and signed H */
363 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
364 	    (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 ||
365 	    (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 ||
366 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
367 	    (r = sshpkt_send(ssh)) != 0)
368 		goto out;
369 
370 	if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
371 	    (r = kex_send_newkeys(ssh)) != 0)
372 		goto out;
373 	/* retain copy of hostkey used at initial KEX */
374 	if (kex->initial_hostkey == NULL &&
375 	    (r = sshkey_from_private(server_host_public,
376 	    &kex->initial_hostkey)) != 0)
377 		goto out;
378 	/* success */
379 out:
380 	explicit_bzero(hash, sizeof(hash));
381 	sshbuf_free(server_host_key_blob);
382 	free(signature);
383 	sshbuf_free(shared_secret);
384 	sshbuf_free(client_pubkey);
385 	sshbuf_free(server_pubkey);
386 	return r;
387 }
388