1 /* $OpenBSD: kexgen.c,v 1.4 2019/11/25 00:51:37 djm Exp $ */
3 * Copyright (c) 2019 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/types.h>
44 static int input_kex_gen_init(int, u_int32_t, struct ssh *);
45 static int input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh);
50 const struct sshbuf *client_version,
51 const struct sshbuf *server_version,
52 const struct sshbuf *client_kexinit,
53 const struct sshbuf *server_kexinit,
54 const struct sshbuf *server_host_key_blob,
55 const struct sshbuf *client_pub,
56 const struct sshbuf *server_pub,
57 const struct sshbuf *shared_secret,
58 u_char *hash, size_t *hashlen)
63 if (*hashlen < ssh_digest_bytes(hash_alg))
64 return SSH_ERR_INVALID_ARGUMENT;
65 if ((b = sshbuf_new()) == NULL)
66 return SSH_ERR_ALLOC_FAIL;
67 if ((r = sshbuf_put_stringb(b, client_version)) != 0 ||
68 (r = sshbuf_put_stringb(b, server_version)) != 0 ||
69 /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
70 (r = sshbuf_put_u32(b, sshbuf_len(client_kexinit) + 1)) != 0 ||
71 (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
72 (r = sshbuf_putb(b, client_kexinit)) != 0 ||
73 (r = sshbuf_put_u32(b, sshbuf_len(server_kexinit) + 1)) != 0 ||
74 (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
75 (r = sshbuf_putb(b, server_kexinit)) != 0 ||
76 (r = sshbuf_put_stringb(b, server_host_key_blob)) != 0 ||
77 (r = sshbuf_put_stringb(b, client_pub)) != 0 ||
78 (r = sshbuf_put_stringb(b, server_pub)) != 0 ||
79 (r = sshbuf_putb(b, shared_secret)) != 0) {
84 sshbuf_dump(b, stderr);
86 if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
88 return SSH_ERR_LIBCRYPTO_ERROR;
91 *hashlen = ssh_digest_bytes(hash_alg);
93 dump_digest("hash", hash, *hashlen);
99 kex_gen_client(struct ssh *ssh)
101 struct kex *kex = ssh->kex;
104 switch (kex->kex_type) {
106 case KEX_DH_GRP1_SHA1:
107 case KEX_DH_GRP14_SHA1:
108 case KEX_DH_GRP14_SHA256:
109 case KEX_DH_GRP16_SHA512:
110 case KEX_DH_GRP18_SHA512:
111 r = kex_dh_keypair(kex);
114 r = kex_ecdh_keypair(kex);
117 case KEX_C25519_SHA256:
118 r = kex_c25519_keypair(kex);
120 case KEX_KEM_SNTRUP4591761X25519_SHA512:
121 r = kex_kem_sntrup4591761x25519_keypair(kex);
124 r = SSH_ERR_INVALID_ARGUMENT;
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)
133 debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
134 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_gen_reply);
139 input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh)
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;
152 if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
154 /* sshkey_fromb() consumes its buffer, so make a copy */
155 if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) {
156 r = SSH_ERR_ALLOC_FAIL;
159 if ((r = sshkey_fromb(tmp, &server_host_key)) != 0)
161 if ((r = kex_verify_host_key(ssh, server_host_key)) != 0)
164 /* Q_S, server public key */
166 if ((r = sshpkt_getb_froms(ssh, &server_blob)) != 0 ||
167 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
168 (r = sshpkt_get_end(ssh)) != 0)
171 /* compute shared secret */
172 switch (kex->kex_type) {
174 case KEX_DH_GRP1_SHA1:
175 case KEX_DH_GRP14_SHA1:
176 case KEX_DH_GRP14_SHA256:
177 case KEX_DH_GRP16_SHA512:
178 case KEX_DH_GRP18_SHA512:
179 r = kex_dh_dec(kex, server_blob, &shared_secret);
182 r = kex_ecdh_dec(kex, server_blob, &shared_secret);
185 case KEX_C25519_SHA256:
186 r = kex_c25519_dec(kex, server_blob, &shared_secret);
188 case KEX_KEM_SNTRUP4591761X25519_SHA512:
189 r = kex_kem_sntrup4591761x25519_dec(kex, server_blob,
193 r = SSH_ERR_INVALID_ARGUMENT;
199 /* calc and verify H */
200 hashlen = sizeof(hash);
201 if ((r = kex_gen_hash(
207 server_host_key_blob,
211 hash, &hashlen)) != 0)
214 if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
215 kex->hostkey_alg, ssh->compat, NULL)) != 0)
218 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
219 r = kex_send_newkeys(ssh);
221 explicit_bzero(hash, sizeof(hash));
222 explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
223 explicit_bzero(kex->sntrup4591761_client_key,
224 sizeof(kex->sntrup4591761_client_key));
225 sshbuf_free(server_host_key_blob);
228 sshkey_free(server_host_key);
229 sshbuf_free(server_blob);
230 sshbuf_free(shared_secret);
231 sshbuf_free(kex->client_pub);
232 kex->client_pub = NULL;
237 kex_gen_server(struct ssh *ssh)
239 debug("expecting SSH2_MSG_KEX_ECDH_INIT");
240 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_gen_init);
245 input_kex_gen_init(int type, u_int32_t seq, struct ssh *ssh)
247 struct kex *kex = ssh->kex;
248 struct sshkey *server_host_private, *server_host_public;
249 struct sshbuf *shared_secret = NULL;
250 struct sshbuf *server_pubkey = NULL;
251 struct sshbuf *client_pubkey = NULL;
252 struct sshbuf *server_host_key_blob = NULL;
253 u_char *signature = NULL, hash[SSH_DIGEST_MAX_LENGTH];
254 size_t slen, hashlen;
257 if ((r = kex_load_hostkey(ssh, &server_host_private,
258 &server_host_public)) != 0)
261 if ((r = sshpkt_getb_froms(ssh, &client_pubkey)) != 0 ||
262 (r = sshpkt_get_end(ssh)) != 0)
265 /* compute shared secret */
266 switch (kex->kex_type) {
268 case KEX_DH_GRP1_SHA1:
269 case KEX_DH_GRP14_SHA1:
270 case KEX_DH_GRP14_SHA256:
271 case KEX_DH_GRP16_SHA512:
272 case KEX_DH_GRP18_SHA512:
273 r = kex_dh_enc(kex, client_pubkey, &server_pubkey,
277 r = kex_ecdh_enc(kex, client_pubkey, &server_pubkey,
281 case KEX_C25519_SHA256:
282 r = kex_c25519_enc(kex, client_pubkey, &server_pubkey,
285 case KEX_KEM_SNTRUP4591761X25519_SHA512:
286 r = kex_kem_sntrup4591761x25519_enc(kex, client_pubkey,
287 &server_pubkey, &shared_secret);
290 r = SSH_ERR_INVALID_ARGUMENT;
297 if ((server_host_key_blob = sshbuf_new()) == NULL) {
298 r = SSH_ERR_ALLOC_FAIL;
301 if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0)
303 hashlen = sizeof(hash);
304 if ((r = kex_gen_hash(
310 server_host_key_blob,
314 hash, &hashlen)) != 0)
318 if ((r = kex->sign(ssh, server_host_private, server_host_public,
319 &signature, &slen, hash, hashlen, kex->hostkey_alg)) != 0)
322 /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
323 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
324 (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 ||
325 (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 ||
326 (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
327 (r = sshpkt_send(ssh)) != 0)
330 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
331 r = kex_send_newkeys(ssh);
333 explicit_bzero(hash, sizeof(hash));
334 sshbuf_free(server_host_key_blob);
336 sshbuf_free(shared_secret);
337 sshbuf_free(client_pubkey);
338 sshbuf_free(server_pubkey);