1 /* $OpenBSD: kexgen.c,v 1.2 2019/01/23 00:30:41 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>
43 static int input_kex_gen_init(int, u_int32_t, struct ssh *);
44 static int input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh);
49 const struct sshbuf *client_version,
50 const struct sshbuf *server_version,
51 const struct sshbuf *client_kexinit,
52 const struct sshbuf *server_kexinit,
53 const struct sshbuf *server_host_key_blob,
54 const struct sshbuf *client_pub,
55 const struct sshbuf *server_pub,
56 const struct sshbuf *shared_secret,
57 u_char *hash, size_t *hashlen)
62 if (*hashlen < ssh_digest_bytes(hash_alg))
63 return SSH_ERR_INVALID_ARGUMENT;
64 if ((b = sshbuf_new()) == NULL)
65 return SSH_ERR_ALLOC_FAIL;
66 if ((r = sshbuf_put_stringb(b, client_version)) != 0 ||
67 (r = sshbuf_put_stringb(b, server_version)) != 0 ||
68 /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
69 (r = sshbuf_put_u32(b, sshbuf_len(client_kexinit) + 1)) != 0 ||
70 (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
71 (r = sshbuf_putb(b, client_kexinit)) != 0 ||
72 (r = sshbuf_put_u32(b, sshbuf_len(server_kexinit) + 1)) != 0 ||
73 (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
74 (r = sshbuf_putb(b, server_kexinit)) != 0 ||
75 (r = sshbuf_put_stringb(b, server_host_key_blob)) != 0 ||
76 (r = sshbuf_put_stringb(b, client_pub)) != 0 ||
77 (r = sshbuf_put_stringb(b, server_pub)) != 0 ||
78 (r = sshbuf_putb(b, shared_secret)) != 0) {
83 sshbuf_dump(b, stderr);
85 if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
87 return SSH_ERR_LIBCRYPTO_ERROR;
90 *hashlen = ssh_digest_bytes(hash_alg);
92 dump_digest("hash", hash, *hashlen);
98 kex_gen_client(struct ssh *ssh)
100 struct kex *kex = ssh->kex;
103 switch (kex->kex_type) {
105 case KEX_DH_GRP1_SHA1:
106 case KEX_DH_GRP14_SHA1:
107 case KEX_DH_GRP14_SHA256:
108 case KEX_DH_GRP16_SHA512:
109 case KEX_DH_GRP18_SHA512:
110 r = kex_dh_keypair(kex);
113 r = kex_ecdh_keypair(kex);
116 case KEX_C25519_SHA256:
117 r = kex_c25519_keypair(kex);
119 case KEX_KEM_SNTRUP4591761X25519_SHA512:
120 r = kex_kem_sntrup4591761x25519_keypair(kex);
123 r = SSH_ERR_INVALID_ARGUMENT;
128 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
129 (r = sshpkt_put_stringb(ssh, kex->client_pub)) != 0 ||
130 (r = sshpkt_send(ssh)) != 0)
132 debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
133 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_gen_reply);
138 input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh)
140 struct kex *kex = ssh->kex;
141 struct sshkey *server_host_key = NULL;
142 struct sshbuf *shared_secret = NULL;
143 struct sshbuf *server_blob = NULL;
144 struct sshbuf *tmp = NULL, *server_host_key_blob = NULL;
145 u_char *signature = NULL;
146 u_char hash[SSH_DIGEST_MAX_LENGTH];
147 size_t slen, hashlen;
151 if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
153 /* sshkey_fromb() consumes its buffer, so make a copy */
154 if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) {
155 r = SSH_ERR_ALLOC_FAIL;
158 if ((r = sshkey_fromb(tmp, &server_host_key)) != 0)
160 if ((r = kex_verify_host_key(ssh, server_host_key)) != 0)
163 /* Q_S, server public key */
165 if ((r = sshpkt_getb_froms(ssh, &server_blob)) != 0 ||
166 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
167 (r = sshpkt_get_end(ssh)) != 0)
170 /* compute shared secret */
171 switch (kex->kex_type) {
173 case KEX_DH_GRP1_SHA1:
174 case KEX_DH_GRP14_SHA1:
175 case KEX_DH_GRP14_SHA256:
176 case KEX_DH_GRP16_SHA512:
177 case KEX_DH_GRP18_SHA512:
178 r = kex_dh_dec(kex, server_blob, &shared_secret);
181 r = kex_ecdh_dec(kex, server_blob, &shared_secret);
184 case KEX_C25519_SHA256:
185 r = kex_c25519_dec(kex, server_blob, &shared_secret);
187 case KEX_KEM_SNTRUP4591761X25519_SHA512:
188 r = kex_kem_sntrup4591761x25519_dec(kex, server_blob,
192 r = SSH_ERR_INVALID_ARGUMENT;
198 /* calc and verify H */
199 hashlen = sizeof(hash);
200 if ((r = kex_gen_hash(
206 server_host_key_blob,
210 hash, &hashlen)) != 0)
213 if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
214 kex->hostkey_alg, ssh->compat)) != 0)
217 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
218 r = kex_send_newkeys(ssh);
220 explicit_bzero(hash, sizeof(hash));
221 explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
222 explicit_bzero(kex->sntrup4591761_client_key,
223 sizeof(kex->sntrup4591761_client_key));
224 sshbuf_free(server_host_key_blob);
227 sshkey_free(server_host_key);
228 sshbuf_free(server_blob);
229 sshbuf_free(shared_secret);
230 sshbuf_free(kex->client_pub);
231 kex->client_pub = NULL;
236 kex_gen_server(struct ssh *ssh)
238 debug("expecting SSH2_MSG_KEX_ECDH_INIT");
239 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_gen_init);
244 input_kex_gen_init(int type, u_int32_t seq, struct ssh *ssh)
246 struct kex *kex = ssh->kex;
247 struct sshkey *server_host_private, *server_host_public;
248 struct sshbuf *shared_secret = NULL;
249 struct sshbuf *server_pubkey = NULL;
250 struct sshbuf *client_pubkey = NULL;
251 struct sshbuf *server_host_key_blob = NULL;
252 u_char *signature = NULL, hash[SSH_DIGEST_MAX_LENGTH];
253 size_t slen, hashlen;
256 if ((r = kex_load_hostkey(ssh, &server_host_private,
257 &server_host_public)) != 0)
260 if ((r = sshpkt_getb_froms(ssh, &client_pubkey)) != 0 ||
261 (r = sshpkt_get_end(ssh)) != 0)
264 /* compute shared secret */
265 switch (kex->kex_type) {
267 case KEX_DH_GRP1_SHA1:
268 case KEX_DH_GRP14_SHA1:
269 case KEX_DH_GRP14_SHA256:
270 case KEX_DH_GRP16_SHA512:
271 case KEX_DH_GRP18_SHA512:
272 r = kex_dh_enc(kex, client_pubkey, &server_pubkey,
276 r = kex_ecdh_enc(kex, client_pubkey, &server_pubkey,
280 case KEX_C25519_SHA256:
281 r = kex_c25519_enc(kex, client_pubkey, &server_pubkey,
284 case KEX_KEM_SNTRUP4591761X25519_SHA512:
285 r = kex_kem_sntrup4591761x25519_enc(kex, client_pubkey,
286 &server_pubkey, &shared_secret);
289 r = SSH_ERR_INVALID_ARGUMENT;
296 if ((server_host_key_blob = sshbuf_new()) == NULL) {
297 r = SSH_ERR_ALLOC_FAIL;
300 if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0)
302 hashlen = sizeof(hash);
303 if ((r = kex_gen_hash(
309 server_host_key_blob,
313 hash, &hashlen)) != 0)
317 if ((r = kex->sign(ssh, server_host_private, server_host_public,
318 &signature, &slen, hash, hashlen, kex->hostkey_alg)) != 0)
321 /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
322 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
323 (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 ||
324 (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 ||
325 (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
326 (r = sshpkt_send(ssh)) != 0)
329 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
330 r = kex_send_newkeys(ssh);
332 explicit_bzero(hash, sizeof(hash));
333 sshbuf_free(server_host_key_blob);
335 sshbuf_free(shared_secret);
336 sshbuf_free(client_pubkey);
337 sshbuf_free(server_pubkey);