markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 1 | /* $OpenBSD: kexecdhs.c,v 1.15 2015/12/04 16:41:28 markus Exp $ */ |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 2 | /* |
| 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 Miller | c79ff07 | 2010-08-31 22:50:48 +1000 | [diff] [blame] | 27 | #include "includes.h" |
| 28 | |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 29 | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
| 30 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <string.h> |
| 33 | #include <signal.h> |
| 34 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 35 | #include <openssl/ecdh.h> |
| 36 | |
| 37 | #include "sshkey.h" |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 38 | #include "cipher.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 39 | #include "digest.h" |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 40 | #include "kex.h" |
| 41 | #include "log.h" |
| 42 | #include "packet.h" |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 43 | #include "ssh2.h" |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 44 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 45 | #include "dispatch.h" |
| 46 | #include "compat.h" |
| 47 | #include "ssherr.h" |
| 48 | #include "sshbuf.h" |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 49 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 50 | static int input_kex_ecdh_init(int, u_int32_t, void *); |
| 51 | |
| 52 | int |
| 53 | kexecdh_server(struct ssh *ssh) |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 54 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 55 | 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 Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 59 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 60 | static int |
| 61 | input_kex_ecdh_init(int type, u_int32_t seq, void *ctxt) |
| 62 | { |
| 63 | struct ssh *ssh = ctxt; |
| 64 | struct kex *kex = ssh->kex; |
| 65 | EC_POINT *client_public; |
| 66 | EC_KEY *server_key = NULL; |
| 67 | const EC_GROUP *group; |
| 68 | const EC_POINT *public_key; |
| 69 | BIGNUM *shared_secret = NULL; |
| 70 | struct sshkey *server_host_private, *server_host_public; |
| 71 | u_char *server_host_key_blob = NULL, *signature = NULL; |
| 72 | u_char *kbuf = NULL; |
| 73 | u_char hash[SSH_DIGEST_MAX_LENGTH]; |
| 74 | size_t slen, sbloblen; |
| 75 | size_t klen = 0, hashlen; |
| 76 | int r; |
| 77 | |
| 78 | if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) { |
| 79 | r = SSH_ERR_ALLOC_FAIL; |
| 80 | goto out; |
| 81 | } |
| 82 | if (EC_KEY_generate_key(server_key) != 1) { |
| 83 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 84 | goto out; |
| 85 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 86 | group = EC_KEY_get0_group(server_key); |
| 87 | |
| 88 | #ifdef DEBUG_KEXECDH |
| 89 | fputs("server private key:\n", stderr); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 90 | sshkey_dump_ec_key(server_key); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 91 | #endif |
| 92 | |
| 93 | if (kex->load_host_public_key == NULL || |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 94 | kex->load_host_private_key == NULL) { |
| 95 | r = SSH_ERR_INVALID_ARGUMENT; |
| 96 | goto out; |
| 97 | } |
djm@openbsd.org | 5104db7 | 2015-01-26 06:10:03 +0000 | [diff] [blame] | 98 | server_host_public = kex->load_host_public_key(kex->hostkey_type, |
| 99 | kex->hostkey_nid, ssh); |
| 100 | server_host_private = kex->load_host_private_key(kex->hostkey_type, |
| 101 | kex->hostkey_nid, ssh); |
djm@openbsd.org | e2cc6be | 2015-01-20 07:55:33 +0000 | [diff] [blame] | 102 | if (server_host_public == NULL) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 103 | r = SSH_ERR_NO_HOSTKEY_LOADED; |
| 104 | goto out; |
| 105 | } |
| 106 | if ((client_public = EC_POINT_new(group)) == NULL) { |
| 107 | r = SSH_ERR_ALLOC_FAIL; |
| 108 | goto out; |
| 109 | } |
| 110 | if ((r = sshpkt_get_ec(ssh, client_public, group)) != 0 || |
| 111 | (r = sshpkt_get_end(ssh)) != 0) |
| 112 | goto out; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 113 | |
| 114 | #ifdef DEBUG_KEXECDH |
| 115 | fputs("client public key:\n", stderr); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 116 | sshkey_dump_ec_point(group, client_public); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 117 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 118 | if (sshkey_ec_validate_public(group, client_public) != 0) { |
| 119 | sshpkt_disconnect(ssh, "invalid client public key"); |
| 120 | r = SSH_ERR_MESSAGE_INCOMPLETE; |
| 121 | goto out; |
| 122 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 123 | |
| 124 | /* Calculate shared_secret */ |
| 125 | klen = (EC_GROUP_get_degree(group) + 7) / 8; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 126 | if ((kbuf = malloc(klen)) == NULL || |
| 127 | (shared_secret = BN_new()) == NULL) { |
| 128 | r = SSH_ERR_ALLOC_FAIL; |
| 129 | goto out; |
| 130 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 131 | if (ECDH_compute_key(kbuf, klen, client_public, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 132 | server_key, NULL) != (int)klen || |
| 133 | BN_bin2bn(kbuf, klen, shared_secret) == NULL) { |
| 134 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 135 | goto out; |
| 136 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 137 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 138 | #ifdef DEBUG_KEXECDH |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 139 | dump_digest("shared secret", kbuf, klen); |
| 140 | #endif |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 141 | /* calc H */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 142 | if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob, |
| 143 | &sbloblen)) != 0) |
| 144 | goto out; |
| 145 | hashlen = sizeof(hash); |
| 146 | if ((r = kex_ecdh_hash( |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 147 | kex->hash_alg, |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 148 | group, |
| 149 | kex->client_version_string, |
| 150 | kex->server_version_string, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 151 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), |
| 152 | sshbuf_ptr(kex->my), sshbuf_len(kex->my), |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 153 | server_host_key_blob, sbloblen, |
| 154 | client_public, |
| 155 | EC_KEY_get0_public_key(server_key), |
| 156 | shared_secret, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 157 | hash, &hashlen)) != 0) |
| 158 | goto out; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 159 | |
| 160 | /* save session id := H */ |
| 161 | if (kex->session_id == NULL) { |
| 162 | kex->session_id_len = hashlen; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 163 | kex->session_id = malloc(kex->session_id_len); |
| 164 | if (kex->session_id == NULL) { |
| 165 | r = SSH_ERR_ALLOC_FAIL; |
| 166 | goto out; |
| 167 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 168 | memcpy(kex->session_id, hash, kex->session_id_len); |
| 169 | } |
| 170 | |
| 171 | /* sign H */ |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 172 | if ((r = kex->sign(server_host_private, server_host_public, &signature, |
| 173 | &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 174 | goto out; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 175 | |
| 176 | /* destroy_sensitive_data(); */ |
| 177 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 178 | public_key = EC_KEY_get0_public_key(server_key); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 179 | /* send server hostkey, ECDH pubkey 'Q_S' and signed H */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 180 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 || |
| 181 | (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 || |
| 182 | (r = sshpkt_put_ec(ssh, public_key, group)) != 0 || |
| 183 | (r = sshpkt_put_string(ssh, signature, slen)) != 0 || |
| 184 | (r = sshpkt_send(ssh)) != 0) |
| 185 | goto out; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 186 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 187 | if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) |
| 188 | r = kex_send_newkeys(ssh); |
| 189 | out: |
| 190 | explicit_bzero(hash, sizeof(hash)); |
| 191 | if (kex->ec_client_key) { |
| 192 | EC_KEY_free(kex->ec_client_key); |
| 193 | kex->ec_client_key = NULL; |
| 194 | } |
| 195 | if (server_key) |
| 196 | EC_KEY_free(server_key); |
| 197 | if (kbuf) { |
| 198 | explicit_bzero(kbuf, klen); |
| 199 | free(kbuf); |
| 200 | } |
| 201 | if (shared_secret) |
| 202 | BN_clear_free(shared_secret); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 203 | free(server_host_key_blob); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 204 | free(signature); |
| 205 | return r; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 206 | } |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 207 | #endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 208 | |