blob: dc24a3af609bbb2f5c437c55644fa2fdbfeb6581 [file] [log] [blame]
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001/* $OpenBSD: kexecdhs.c,v 1.16 2017/05/30 14:23:52 markus 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#include <string.h>
33#include <signal.h>
34
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000035#include <openssl/ecdh.h>
36
37#include "sshkey.h"
Damien Millereb8b60e2010-08-31 22:41:14 +100038#include "cipher.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000039#include "digest.h"
Damien Millereb8b60e2010-08-31 22:41:14 +100040#include "kex.h"
41#include "log.h"
42#include "packet.h"
Damien Millereb8b60e2010-08-31 22:41:14 +100043#include "ssh2.h"
Damien Millereb8b60e2010-08-31 22:41:14 +100044
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000045#include "dispatch.h"
46#include "compat.h"
47#include "ssherr.h"
48#include "sshbuf.h"
Damien Miller6af914a2010-09-10 11:39:26 +100049
markus@openbsd.org2ae666a2017-05-30 14:23:52 +000050static int input_kex_ecdh_init(int, u_int32_t, struct ssh *);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000051
52int
53kexecdh_server(struct ssh *ssh)
Damien Millereb8b60e2010-08-31 22:41:14 +100054{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000055 debug("expecting SSH2_MSG_KEX_ECDH_INIT");
56 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_ecdh_init);
57 return 0;
58}
Damien Millereb8b60e2010-08-31 22:41:14 +100059
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000060static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +000061input_kex_ecdh_init(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000062{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000063 struct kex *kex = ssh->kex;
64 EC_POINT *client_public;
65 EC_KEY *server_key = NULL;
66 const EC_GROUP *group;
67 const EC_POINT *public_key;
68 BIGNUM *shared_secret = NULL;
69 struct sshkey *server_host_private, *server_host_public;
70 u_char *server_host_key_blob = NULL, *signature = NULL;
71 u_char *kbuf = NULL;
72 u_char hash[SSH_DIGEST_MAX_LENGTH];
73 size_t slen, sbloblen;
74 size_t klen = 0, hashlen;
75 int r;
76
77 if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
78 r = SSH_ERR_ALLOC_FAIL;
79 goto out;
80 }
81 if (EC_KEY_generate_key(server_key) != 1) {
82 r = SSH_ERR_LIBCRYPTO_ERROR;
83 goto out;
84 }
Damien Millereb8b60e2010-08-31 22:41:14 +100085 group = EC_KEY_get0_group(server_key);
86
87#ifdef DEBUG_KEXECDH
88 fputs("server private key:\n", stderr);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000089 sshkey_dump_ec_key(server_key);
Damien Millereb8b60e2010-08-31 22:41:14 +100090#endif
91
92 if (kex->load_host_public_key == NULL ||
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000093 kex->load_host_private_key == NULL) {
94 r = SSH_ERR_INVALID_ARGUMENT;
95 goto out;
96 }
djm@openbsd.org5104db72015-01-26 06:10:03 +000097 server_host_public = kex->load_host_public_key(kex->hostkey_type,
98 kex->hostkey_nid, ssh);
99 server_host_private = kex->load_host_private_key(kex->hostkey_type,
100 kex->hostkey_nid, ssh);
djm@openbsd.orge2cc6be2015-01-20 07:55:33 +0000101 if (server_host_public == NULL) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000102 r = SSH_ERR_NO_HOSTKEY_LOADED;
103 goto out;
104 }
105 if ((client_public = EC_POINT_new(group)) == NULL) {
106 r = SSH_ERR_ALLOC_FAIL;
107 goto out;
108 }
109 if ((r = sshpkt_get_ec(ssh, client_public, group)) != 0 ||
110 (r = sshpkt_get_end(ssh)) != 0)
111 goto out;
Damien Millereb8b60e2010-08-31 22:41:14 +1000112
113#ifdef DEBUG_KEXECDH
114 fputs("client public key:\n", stderr);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000115 sshkey_dump_ec_point(group, client_public);
Damien Millereb8b60e2010-08-31 22:41:14 +1000116#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000117 if (sshkey_ec_validate_public(group, client_public) != 0) {
118 sshpkt_disconnect(ssh, "invalid client public key");
119 r = SSH_ERR_MESSAGE_INCOMPLETE;
120 goto out;
121 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000122
123 /* Calculate shared_secret */
124 klen = (EC_GROUP_get_degree(group) + 7) / 8;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000125 if ((kbuf = malloc(klen)) == NULL ||
126 (shared_secret = BN_new()) == NULL) {
127 r = SSH_ERR_ALLOC_FAIL;
128 goto out;
129 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000130 if (ECDH_compute_key(kbuf, klen, client_public,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000131 server_key, NULL) != (int)klen ||
132 BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
133 r = SSH_ERR_LIBCRYPTO_ERROR;
134 goto out;
135 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000136
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000137#ifdef DEBUG_KEXECDH
Damien Millereb8b60e2010-08-31 22:41:14 +1000138 dump_digest("shared secret", kbuf, klen);
139#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000140 /* calc H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000141 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
142 &sbloblen)) != 0)
143 goto out;
144 hashlen = sizeof(hash);
145 if ((r = kex_ecdh_hash(
Damien Millerb3051d02014-01-10 10:58:53 +1100146 kex->hash_alg,
Damien Millereb8b60e2010-08-31 22:41:14 +1000147 group,
148 kex->client_version_string,
149 kex->server_version_string,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000150 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
151 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
Damien Millereb8b60e2010-08-31 22:41:14 +1000152 server_host_key_blob, sbloblen,
153 client_public,
154 EC_KEY_get0_public_key(server_key),
155 shared_secret,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000156 hash, &hashlen)) != 0)
157 goto out;
Damien Millereb8b60e2010-08-31 22:41:14 +1000158
159 /* save session id := H */
160 if (kex->session_id == NULL) {
161 kex->session_id_len = hashlen;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000162 kex->session_id = malloc(kex->session_id_len);
163 if (kex->session_id == NULL) {
164 r = SSH_ERR_ALLOC_FAIL;
165 goto out;
166 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000167 memcpy(kex->session_id, hash, kex->session_id_len);
168 }
169
170 /* sign H */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000171 if ((r = kex->sign(server_host_private, server_host_public, &signature,
172 &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000173 goto out;
Damien Millereb8b60e2010-08-31 22:41:14 +1000174
175 /* destroy_sensitive_data(); */
176
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000177 public_key = EC_KEY_get0_public_key(server_key);
Damien Millereb8b60e2010-08-31 22:41:14 +1000178 /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000179 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
180 (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
181 (r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
182 (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
183 (r = sshpkt_send(ssh)) != 0)
184 goto out;
Damien Millereb8b60e2010-08-31 22:41:14 +1000185
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000186 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
187 r = kex_send_newkeys(ssh);
188 out:
189 explicit_bzero(hash, sizeof(hash));
190 if (kex->ec_client_key) {
191 EC_KEY_free(kex->ec_client_key);
192 kex->ec_client_key = NULL;
193 }
194 if (server_key)
195 EC_KEY_free(server_key);
196 if (kbuf) {
197 explicit_bzero(kbuf, klen);
198 free(kbuf);
199 }
200 if (shared_secret)
201 BN_clear_free(shared_secret);
Darren Tuckera627d422013-06-02 07:31:17 +1000202 free(server_host_key_blob);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000203 free(signature);
204 return r;
Damien Millereb8b60e2010-08-31 22:41:14 +1000205}
Damien Miller72ef7c12015-01-15 02:21:31 +1100206#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000207