blob: 67669b3bf6dfec170d365b71c89dcbc98f8f6820 [file] [log] [blame]
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001/* $OpenBSD: kexecdhc.c,v 1.12 2017/12/18 02:25:15 djm Exp $ */
Damien Millereb8b60e2010-08-31 22:41:14 +10002/*
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2010 Damien Miller. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
Damien Millerc79ff072010-08-31 22:50:48 +100027#include "includes.h"
28
Damien Miller72ef7c12015-01-15 02:21:31 +110029#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
30
Damien Millereb8b60e2010-08-31 22:41:14 +100031#include <sys/types.h>
32
33#include <stdio.h>
34#include <string.h>
35#include <signal.h>
36
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000037#include <openssl/ecdh.h>
38
39#include "sshkey.h"
Damien Millereb8b60e2010-08-31 22:41:14 +100040#include "cipher.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000041#include "digest.h"
Damien Millereb8b60e2010-08-31 22:41:14 +100042#include "kex.h"
43#include "log.h"
44#include "packet.h"
45#include "dh.h"
46#include "ssh2.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000047#include "dispatch.h"
48#include "compat.h"
49#include "ssherr.h"
50#include "sshbuf.h"
Damien Millereb8b60e2010-08-31 22:41:14 +100051
markus@openbsd.org2ae666a2017-05-30 14:23:52 +000052static int input_kex_ecdh_reply(int, u_int32_t, struct ssh *);
Damien Miller6af914a2010-09-10 11:39:26 +100053
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000054int
55kexecdh_client(struct ssh *ssh)
Damien Millereb8b60e2010-08-31 22:41:14 +100056{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000057 struct kex *kex = ssh->kex;
58 EC_KEY *client_key = NULL;
Damien Millereb8b60e2010-08-31 22:41:14 +100059 const EC_GROUP *group;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000060 const EC_POINT *public_key;
61 int r;
Damien Millereb8b60e2010-08-31 22:41:14 +100062
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000063 if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
64 r = SSH_ERR_ALLOC_FAIL;
65 goto out;
66 }
67 if (EC_KEY_generate_key(client_key) != 1) {
68 r = SSH_ERR_LIBCRYPTO_ERROR;
69 goto out;
70 }
Damien Millereb8b60e2010-08-31 22:41:14 +100071 group = EC_KEY_get0_group(client_key);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000072 public_key = EC_KEY_get0_public_key(client_key);
Damien Millereb8b60e2010-08-31 22:41:14 +100073
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000074 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
75 (r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
76 (r = sshpkt_send(ssh)) != 0)
77 goto out;
Damien Millereb8b60e2010-08-31 22:41:14 +100078 debug("sending SSH2_MSG_KEX_ECDH_INIT");
79
80#ifdef DEBUG_KEXECDH
81 fputs("client private key:\n", stderr);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000082 sshkey_dump_ec_key(client_key);
Damien Millereb8b60e2010-08-31 22:41:14 +100083#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000084 kex->ec_client_key = client_key;
85 kex->ec_group = group;
86 client_key = NULL; /* owned by the kex */
Damien Millereb8b60e2010-08-31 22:41:14 +100087
88 debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000089 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_ecdh_reply);
90 r = 0;
91 out:
92 if (client_key)
93 EC_KEY_free(client_key);
94 return r;
95}
96
97static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +000098input_kex_ecdh_reply(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000099{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000100 struct kex *kex = ssh->kex;
101 const EC_GROUP *group;
102 EC_POINT *server_public = NULL;
103 EC_KEY *client_key;
104 BIGNUM *shared_secret = NULL;
105 struct sshkey *server_host_key = NULL;
106 u_char *server_host_key_blob = NULL, *signature = NULL;
107 u_char *kbuf = NULL;
108 u_char hash[SSH_DIGEST_MAX_LENGTH];
109 size_t slen, sbloblen;
110 size_t klen = 0, hashlen;
111 int r;
112
113 if (kex->verify_host_key == NULL) {
114 r = SSH_ERR_INVALID_ARGUMENT;
115 goto out;
116 }
117 group = kex->ec_group;
118 client_key = kex->ec_client_key;
Damien Millereb8b60e2010-08-31 22:41:14 +1000119
120 /* hostkey */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000121 if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
122 &sbloblen)) != 0 ||
123 (r = sshkey_from_blob(server_host_key_blob, sbloblen,
124 &server_host_key)) != 0)
125 goto out;
djm@openbsd.org5104db72015-01-26 06:10:03 +0000126 if (server_host_key->type != kex->hostkey_type ||
127 (kex->hostkey_type == KEY_ECDSA &&
128 server_host_key->ecdsa_nid != kex->hostkey_nid)) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000129 r = SSH_ERR_KEY_TYPE_MISMATCH;
130 goto out;
131 }
132 if (kex->verify_host_key(server_host_key, ssh) == -1) {
133 r = SSH_ERR_SIGNATURE_INVALID;
134 goto out;
135 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000136
137 /* Q_S, server public key */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000138 /* signed H */
139 if ((server_public = EC_POINT_new(group)) == NULL) {
140 r = SSH_ERR_ALLOC_FAIL;
141 goto out;
142 }
143 if ((r = sshpkt_get_ec(ssh, server_public, group)) != 0 ||
144 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
145 (r = sshpkt_get_end(ssh)) != 0)
146 goto out;
Damien Millereb8b60e2010-08-31 22:41:14 +1000147
148#ifdef DEBUG_KEXECDH
149 fputs("server public key:\n", stderr);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000150 sshkey_dump_ec_point(group, server_public);
Damien Millereb8b60e2010-08-31 22:41:14 +1000151#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000152 if (sshkey_ec_validate_public(group, server_public) != 0) {
153 sshpkt_disconnect(ssh, "invalid server public key");
154 r = SSH_ERR_MESSAGE_INCOMPLETE;
155 goto out;
156 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000157
158 klen = (EC_GROUP_get_degree(group) + 7) / 8;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000159 if ((kbuf = malloc(klen)) == NULL ||
160 (shared_secret = BN_new()) == NULL) {
161 r = SSH_ERR_ALLOC_FAIL;
162 goto out;
163 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000164 if (ECDH_compute_key(kbuf, klen, server_public,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000165 client_key, NULL) != (int)klen ||
166 BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
167 r = SSH_ERR_LIBCRYPTO_ERROR;
168 goto out;
169 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000170
171#ifdef DEBUG_KEXECDH
172 dump_digest("shared secret", kbuf, klen);
173#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000174 /* calc and verify H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000175 hashlen = sizeof(hash);
176 if ((r = kex_ecdh_hash(
Damien Millerb3051d02014-01-10 10:58:53 +1100177 kex->hash_alg,
Damien Millereb8b60e2010-08-31 22:41:14 +1000178 group,
179 kex->client_version_string,
180 kex->server_version_string,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000181 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
182 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
Damien Millereb8b60e2010-08-31 22:41:14 +1000183 server_host_key_blob, sbloblen,
184 EC_KEY_get0_public_key(client_key),
185 server_public,
186 shared_secret,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000187 hash, &hashlen)) != 0)
188 goto out;
Damien Millereb8b60e2010-08-31 22:41:14 +1000189
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000190 if ((r = sshkey_verify(server_host_key, signature, slen, hash,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000191 hashlen, kex->hostkey_alg, ssh->compat)) != 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000192 goto out;
Damien Millereb8b60e2010-08-31 22:41:14 +1000193
194 /* save session id */
195 if (kex->session_id == NULL) {
196 kex->session_id_len = hashlen;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000197 kex->session_id = malloc(kex->session_id_len);
198 if (kex->session_id == NULL) {
199 r = SSH_ERR_ALLOC_FAIL;
200 goto out;
201 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000202 memcpy(kex->session_id, hash, kex->session_id_len);
203 }
204
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000205 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
206 r = kex_send_newkeys(ssh);
207 out:
208 explicit_bzero(hash, sizeof(hash));
209 if (kex->ec_client_key) {
210 EC_KEY_free(kex->ec_client_key);
211 kex->ec_client_key = NULL;
212 }
213 if (server_public)
214 EC_POINT_clear_free(server_public);
215 if (kbuf) {
216 explicit_bzero(kbuf, klen);
217 free(kbuf);
218 }
219 if (shared_secret)
220 BN_clear_free(shared_secret);
221 sshkey_free(server_host_key);
222 free(server_host_key_blob);
223 free(signature);
224 return r;
Damien Millereb8b60e2010-08-31 22:41:14 +1000225}
Damien Miller72ef7c12015-01-15 02:21:31 +1100226#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000227