xref: /openbsd-src/usr.bin/ssh/kexgen.c (revision 5e3c7963eb248119b7dfd4b0defad58a7d9cd306)
1 /* $OpenBSD: kexgen.c,v 1.2 2019/01/23 00:30:41 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 	case KEX_DH_GRP1_SHA1:
103 	case KEX_DH_GRP14_SHA1:
104 	case KEX_DH_GRP14_SHA256:
105 	case KEX_DH_GRP16_SHA512:
106 	case KEX_DH_GRP18_SHA512:
107 		r = kex_dh_keypair(kex);
108 		break;
109 	case KEX_ECDH_SHA2:
110 		r = kex_ecdh_keypair(kex);
111 		break;
112 	case KEX_C25519_SHA256:
113 		r = kex_c25519_keypair(kex);
114 		break;
115 	case KEX_KEM_SNTRUP4591761X25519_SHA512:
116 		r = kex_kem_sntrup4591761x25519_keypair(kex);
117 		break;
118 	default:
119 		r = SSH_ERR_INVALID_ARGUMENT;
120 		break;
121 	}
122 	if (r != 0)
123 		return r;
124 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
125 	    (r = sshpkt_put_stringb(ssh, kex->client_pub)) != 0 ||
126 	    (r = sshpkt_send(ssh)) != 0)
127 		return r;
128 	debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
129 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_gen_reply);
130 	return 0;
131 }
132 
133 static int
134 input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh)
135 {
136 	struct kex *kex = ssh->kex;
137 	struct sshkey *server_host_key = NULL;
138 	struct sshbuf *shared_secret = NULL;
139 	struct sshbuf *server_blob = NULL;
140 	struct sshbuf *tmp = NULL, *server_host_key_blob = NULL;
141 	u_char *signature = NULL;
142 	u_char hash[SSH_DIGEST_MAX_LENGTH];
143 	size_t slen, hashlen;
144 	int r;
145 
146 	/* hostkey */
147 	if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
148 		goto out;
149 	/* sshkey_fromb() consumes its buffer, so make a copy */
150 	if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) {
151 		r = SSH_ERR_ALLOC_FAIL;
152 		goto out;
153 	}
154 	if ((r = sshkey_fromb(tmp, &server_host_key)) != 0)
155 		goto out;
156 	if ((r = kex_verify_host_key(ssh, server_host_key)) != 0)
157 		goto out;
158 
159 	/* Q_S, server public key */
160 	/* signed H */
161 	if ((r = sshpkt_getb_froms(ssh, &server_blob)) != 0 ||
162 	    (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
163 	    (r = sshpkt_get_end(ssh)) != 0)
164 		goto out;
165 
166 	/* compute shared secret */
167 	switch (kex->kex_type) {
168 	case KEX_DH_GRP1_SHA1:
169 	case KEX_DH_GRP14_SHA1:
170 	case KEX_DH_GRP14_SHA256:
171 	case KEX_DH_GRP16_SHA512:
172 	case KEX_DH_GRP18_SHA512:
173 		r = kex_dh_dec(kex, server_blob, &shared_secret);
174 		break;
175 	case KEX_ECDH_SHA2:
176 		r = kex_ecdh_dec(kex, server_blob, &shared_secret);
177 		break;
178 	case KEX_C25519_SHA256:
179 		r = kex_c25519_dec(kex, server_blob, &shared_secret);
180 		break;
181 	case KEX_KEM_SNTRUP4591761X25519_SHA512:
182 		r = kex_kem_sntrup4591761x25519_dec(kex, server_blob,
183 		    &shared_secret);
184 		break;
185 	default:
186 		r = SSH_ERR_INVALID_ARGUMENT;
187 		break;
188 	}
189 	if (r !=0 )
190 		goto out;
191 
192 	/* calc and verify H */
193 	hashlen = sizeof(hash);
194 	if ((r = kex_gen_hash(
195 	    kex->hash_alg,
196 	    kex->client_version,
197 	    kex->server_version,
198 	    kex->my,
199 	    kex->peer,
200 	    server_host_key_blob,
201 	    kex->client_pub,
202 	    server_blob,
203 	    shared_secret,
204 	    hash, &hashlen)) != 0)
205 		goto out;
206 
207 	if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
208 	    kex->hostkey_alg, ssh->compat)) != 0)
209 		goto out;
210 
211 	if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
212 		r = kex_send_newkeys(ssh);
213 out:
214 	explicit_bzero(hash, sizeof(hash));
215 	explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
216 	explicit_bzero(kex->sntrup4591761_client_key,
217 	    sizeof(kex->sntrup4591761_client_key));
218 	sshbuf_free(server_host_key_blob);
219 	free(signature);
220 	sshbuf_free(tmp);
221 	sshkey_free(server_host_key);
222 	sshbuf_free(server_blob);
223 	sshbuf_free(shared_secret);
224 	sshbuf_free(kex->client_pub);
225 	kex->client_pub = NULL;
226 	return r;
227 }
228 
229 int
230 kex_gen_server(struct ssh *ssh)
231 {
232 	debug("expecting SSH2_MSG_KEX_ECDH_INIT");
233 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_gen_init);
234 	return 0;
235 }
236 
237 static int
238 input_kex_gen_init(int type, u_int32_t seq, struct ssh *ssh)
239 {
240 	struct kex *kex = ssh->kex;
241 	struct sshkey *server_host_private, *server_host_public;
242 	struct sshbuf *shared_secret = NULL;
243 	struct sshbuf *server_pubkey = NULL;
244 	struct sshbuf *client_pubkey = NULL;
245 	struct sshbuf *server_host_key_blob = NULL;
246 	u_char *signature = NULL, hash[SSH_DIGEST_MAX_LENGTH];
247 	size_t slen, hashlen;
248 	int r;
249 
250 	if ((r = kex_load_hostkey(ssh, &server_host_private,
251 	    &server_host_public)) != 0)
252 		goto out;
253 
254 	if ((r = sshpkt_getb_froms(ssh, &client_pubkey)) != 0 ||
255 	    (r = sshpkt_get_end(ssh)) != 0)
256 		goto out;
257 
258 	/* compute shared secret */
259 	switch (kex->kex_type) {
260 	case KEX_DH_GRP1_SHA1:
261 	case KEX_DH_GRP14_SHA1:
262 	case KEX_DH_GRP14_SHA256:
263 	case KEX_DH_GRP16_SHA512:
264 	case KEX_DH_GRP18_SHA512:
265 		r = kex_dh_enc(kex, client_pubkey, &server_pubkey,
266 		    &shared_secret);
267 		break;
268 	case KEX_ECDH_SHA2:
269 		r = kex_ecdh_enc(kex, client_pubkey, &server_pubkey,
270 		    &shared_secret);
271 		break;
272 	case KEX_C25519_SHA256:
273 		r = kex_c25519_enc(kex, client_pubkey, &server_pubkey,
274 		    &shared_secret);
275 		break;
276 	case KEX_KEM_SNTRUP4591761X25519_SHA512:
277 		r = kex_kem_sntrup4591761x25519_enc(kex, client_pubkey,
278 		    &server_pubkey, &shared_secret);
279 		break;
280 	default:
281 		r = SSH_ERR_INVALID_ARGUMENT;
282 		break;
283 	}
284 	if (r !=0 )
285 		goto out;
286 
287 	/* calc H */
288 	if ((server_host_key_blob = sshbuf_new()) == NULL) {
289 		r = SSH_ERR_ALLOC_FAIL;
290 		goto out;
291 	}
292 	if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0)
293 		goto out;
294 	hashlen = sizeof(hash);
295 	if ((r = kex_gen_hash(
296 	    kex->hash_alg,
297 	    kex->client_version,
298 	    kex->server_version,
299 	    kex->peer,
300 	    kex->my,
301 	    server_host_key_blob,
302 	    client_pubkey,
303 	    server_pubkey,
304 	    shared_secret,
305 	    hash, &hashlen)) != 0)
306 		goto out;
307 
308 	/* sign H */
309 	if ((r = kex->sign(ssh, server_host_private, server_host_public,
310 	     &signature, &slen, hash, hashlen, kex->hostkey_alg)) != 0)
311 		goto out;
312 
313 	/* send server hostkey, ECDH pubkey 'Q_S' and signed H */
314 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
315 	    (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 ||
316 	    (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 ||
317 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
318 	    (r = sshpkt_send(ssh)) != 0)
319 		goto out;
320 
321 	if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
322 		r = kex_send_newkeys(ssh);
323 out:
324 	explicit_bzero(hash, sizeof(hash));
325 	sshbuf_free(server_host_key_blob);
326 	free(signature);
327 	sshbuf_free(shared_secret);
328 	sshbuf_free(client_pubkey);
329 	sshbuf_free(server_pubkey);
330 	return r;
331 }
332