xref: /openbsd/usr.bin/ssh/kexgen.c (revision 4e5f03cb)
1 /* $OpenBSD: kexgen.c,v 1.10 2024/09/09 02:39:57 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
kex_gen_hash(int hash_alg,const struct sshbuf * client_version,const struct sshbuf * server_version,const struct sshbuf * client_kexinit,const struct sshbuf * server_kexinit,const struct sshbuf * server_host_key_blob,const struct sshbuf * client_pub,const struct sshbuf * server_pub,const struct sshbuf * shared_secret,u_char * hash,size_t * hashlen)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
kex_gen_client(struct ssh * ssh)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 	case KEX_KEM_MLKEM768X25519_SHA256:
121 		r = kex_kem_mlkem768x25519_keypair(kex);
122 		break;
123 	default:
124 		r = SSH_ERR_INVALID_ARGUMENT;
125 		break;
126 	}
127 	if (r != 0)
128 		return r;
129 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
130 	    (r = sshpkt_put_stringb(ssh, kex->client_pub)) != 0 ||
131 	    (r = sshpkt_send(ssh)) != 0)
132 		return r;
133 	debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
134 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_gen_reply);
135 	return 0;
136 }
137 
138 static int
input_kex_gen_reply(int type,u_int32_t seq,struct ssh * ssh)139 input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh)
140 {
141 	struct kex *kex = ssh->kex;
142 	struct sshkey *server_host_key = NULL;
143 	struct sshbuf *shared_secret = NULL;
144 	struct sshbuf *server_blob = NULL;
145 	struct sshbuf *tmp = NULL, *server_host_key_blob = NULL;
146 	u_char *signature = NULL;
147 	u_char hash[SSH_DIGEST_MAX_LENGTH];
148 	size_t slen, hashlen;
149 	int r;
150 
151 	debug("SSH2_MSG_KEX_ECDH_REPLY received");
152 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &kex_protocol_error);
153 
154 	/* hostkey */
155 	if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
156 		goto out;
157 	/* sshkey_fromb() consumes its buffer, so make a copy */
158 	if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) {
159 		r = SSH_ERR_ALLOC_FAIL;
160 		goto out;
161 	}
162 	if ((r = sshkey_fromb(tmp, &server_host_key)) != 0)
163 		goto out;
164 	if ((r = kex_verify_host_key(ssh, server_host_key)) != 0)
165 		goto out;
166 
167 	/* Q_S, server public key */
168 	/* signed H */
169 	if ((r = sshpkt_getb_froms(ssh, &server_blob)) != 0 ||
170 	    (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
171 	    (r = sshpkt_get_end(ssh)) != 0)
172 		goto out;
173 
174 	/* compute shared secret */
175 	switch (kex->kex_type) {
176 #ifdef WITH_OPENSSL
177 	case KEX_DH_GRP1_SHA1:
178 	case KEX_DH_GRP14_SHA1:
179 	case KEX_DH_GRP14_SHA256:
180 	case KEX_DH_GRP16_SHA512:
181 	case KEX_DH_GRP18_SHA512:
182 		r = kex_dh_dec(kex, server_blob, &shared_secret);
183 		break;
184 	case KEX_ECDH_SHA2:
185 		r = kex_ecdh_dec(kex, server_blob, &shared_secret);
186 		break;
187 #endif /* WITH_OPENSSL */
188 	case KEX_C25519_SHA256:
189 		r = kex_c25519_dec(kex, server_blob, &shared_secret);
190 		break;
191 	case KEX_KEM_SNTRUP761X25519_SHA512:
192 		r = kex_kem_sntrup761x25519_dec(kex, server_blob,
193 		    &shared_secret);
194 		break;
195 	case KEX_KEM_MLKEM768X25519_SHA256:
196 		r = kex_kem_mlkem768x25519_dec(kex, server_blob,
197 		    &shared_secret);
198 		break;
199 	default:
200 		r = SSH_ERR_INVALID_ARGUMENT;
201 		break;
202 	}
203 	if (r !=0 )
204 		goto out;
205 
206 	/* calc and verify H */
207 	hashlen = sizeof(hash);
208 	if ((r = kex_gen_hash(
209 	    kex->hash_alg,
210 	    kex->client_version,
211 	    kex->server_version,
212 	    kex->my,
213 	    kex->peer,
214 	    server_host_key_blob,
215 	    kex->client_pub,
216 	    server_blob,
217 	    shared_secret,
218 	    hash, &hashlen)) != 0)
219 		goto out;
220 
221 	if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
222 	    kex->hostkey_alg, ssh->compat, NULL)) != 0)
223 		goto out;
224 
225 	if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
226 	    (r = kex_send_newkeys(ssh)) != 0)
227 		goto out;
228 
229 	/* save initial signature and hostkey */
230 	if ((kex->flags & KEX_INITIAL) != 0) {
231 		if (kex->initial_hostkey != NULL || kex->initial_sig != NULL) {
232 			r = SSH_ERR_INTERNAL_ERROR;
233 			goto out;
234 		}
235 		if ((kex->initial_sig = sshbuf_new()) == NULL) {
236 			r = SSH_ERR_ALLOC_FAIL;
237 			goto out;
238 		}
239 		if ((r = sshbuf_put(kex->initial_sig, signature, slen)) != 0)
240 			goto out;
241 		kex->initial_hostkey = server_host_key;
242 		server_host_key = NULL;
243 	}
244 	/* success */
245 out:
246 	explicit_bzero(hash, sizeof(hash));
247 	explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
248 	explicit_bzero(kex->sntrup761_client_key,
249 	    sizeof(kex->sntrup761_client_key));
250 	explicit_bzero(kex->mlkem768_client_key,
251 	    sizeof(kex->mlkem768_client_key));
252 	sshbuf_free(server_host_key_blob);
253 	free(signature);
254 	sshbuf_free(tmp);
255 	sshkey_free(server_host_key);
256 	sshbuf_free(server_blob);
257 	sshbuf_free(shared_secret);
258 	sshbuf_free(kex->client_pub);
259 	kex->client_pub = NULL;
260 	return r;
261 }
262 
263 int
kex_gen_server(struct ssh * ssh)264 kex_gen_server(struct ssh *ssh)
265 {
266 	debug("expecting SSH2_MSG_KEX_ECDH_INIT");
267 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_gen_init);
268 	return 0;
269 }
270 
271 static int
input_kex_gen_init(int type,u_int32_t seq,struct ssh * ssh)272 input_kex_gen_init(int type, u_int32_t seq, struct ssh *ssh)
273 {
274 	struct kex *kex = ssh->kex;
275 	struct sshkey *server_host_private, *server_host_public;
276 	struct sshbuf *shared_secret = NULL;
277 	struct sshbuf *server_pubkey = NULL;
278 	struct sshbuf *client_pubkey = NULL;
279 	struct sshbuf *server_host_key_blob = NULL;
280 	u_char *signature = NULL, hash[SSH_DIGEST_MAX_LENGTH];
281 	size_t slen, hashlen;
282 	int r;
283 
284 	debug("SSH2_MSG_KEX_ECDH_INIT received");
285 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &kex_protocol_error);
286 
287 	if ((r = kex_load_hostkey(ssh, &server_host_private,
288 	    &server_host_public)) != 0)
289 		goto out;
290 
291 	if ((r = sshpkt_getb_froms(ssh, &client_pubkey)) != 0 ||
292 	    (r = sshpkt_get_end(ssh)) != 0)
293 		goto out;
294 
295 	/* compute shared secret */
296 	switch (kex->kex_type) {
297 #ifdef WITH_OPENSSL
298 	case KEX_DH_GRP1_SHA1:
299 	case KEX_DH_GRP14_SHA1:
300 	case KEX_DH_GRP14_SHA256:
301 	case KEX_DH_GRP16_SHA512:
302 	case KEX_DH_GRP18_SHA512:
303 		r = kex_dh_enc(kex, client_pubkey, &server_pubkey,
304 		    &shared_secret);
305 		break;
306 	case KEX_ECDH_SHA2:
307 		r = kex_ecdh_enc(kex, client_pubkey, &server_pubkey,
308 		    &shared_secret);
309 		break;
310 #endif /* WITH_OPENSSL */
311 	case KEX_C25519_SHA256:
312 		r = kex_c25519_enc(kex, client_pubkey, &server_pubkey,
313 		    &shared_secret);
314 		break;
315 	case KEX_KEM_SNTRUP761X25519_SHA512:
316 		r = kex_kem_sntrup761x25519_enc(kex, client_pubkey,
317 		    &server_pubkey, &shared_secret);
318 		break;
319 	case KEX_KEM_MLKEM768X25519_SHA256:
320 		r = kex_kem_mlkem768x25519_enc(kex, client_pubkey,
321 		    &server_pubkey, &shared_secret);
322 		break;
323 	default:
324 		r = SSH_ERR_INVALID_ARGUMENT;
325 		break;
326 	}
327 	if (r !=0 )
328 		goto out;
329 
330 	/* calc H */
331 	if ((server_host_key_blob = sshbuf_new()) == NULL) {
332 		r = SSH_ERR_ALLOC_FAIL;
333 		goto out;
334 	}
335 	if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0)
336 		goto out;
337 	hashlen = sizeof(hash);
338 	if ((r = kex_gen_hash(
339 	    kex->hash_alg,
340 	    kex->client_version,
341 	    kex->server_version,
342 	    kex->peer,
343 	    kex->my,
344 	    server_host_key_blob,
345 	    client_pubkey,
346 	    server_pubkey,
347 	    shared_secret,
348 	    hash, &hashlen)) != 0)
349 		goto out;
350 
351 	/* sign H */
352 	if ((r = kex->sign(ssh, server_host_private, server_host_public,
353 	    &signature, &slen, hash, hashlen, kex->hostkey_alg)) != 0)
354 		goto out;
355 
356 	/* send server hostkey, ECDH pubkey 'Q_S' and signed H */
357 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
358 	    (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 ||
359 	    (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 ||
360 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
361 	    (r = sshpkt_send(ssh)) != 0)
362 		goto out;
363 
364 	if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
365 	    (r = kex_send_newkeys(ssh)) != 0)
366 		goto out;
367 	/* retain copy of hostkey used at initial KEX */
368 	if (kex->initial_hostkey == NULL &&
369 	    (r = sshkey_from_private(server_host_public,
370 	    &kex->initial_hostkey)) != 0)
371 		goto out;
372 	/* success */
373 out:
374 	explicit_bzero(hash, sizeof(hash));
375 	sshbuf_free(server_host_key_blob);
376 	free(signature);
377 	sshbuf_free(shared_secret);
378 	sshbuf_free(client_pubkey);
379 	sshbuf_free(server_pubkey);
380 	return r;
381 }
382