jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 1 | /* $OpenBSD: kexecdhc.c,v 1.13 2018/02/07 02:06:51 jsing 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 | |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #include <signal.h> |
| 36 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 37 | #include <openssl/ecdh.h> |
| 38 | |
| 39 | #include "sshkey.h" |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 40 | #include "cipher.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 41 | #include "digest.h" |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 42 | #include "kex.h" |
| 43 | #include "log.h" |
| 44 | #include "packet.h" |
| 45 | #include "dh.h" |
| 46 | #include "ssh2.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 47 | #include "dispatch.h" |
| 48 | #include "compat.h" |
| 49 | #include "ssherr.h" |
| 50 | #include "sshbuf.h" |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 51 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 52 | static int input_kex_ecdh_reply(int, u_int32_t, struct ssh *); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 53 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 54 | int |
| 55 | kexecdh_client(struct ssh *ssh) |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 56 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 57 | struct kex *kex = ssh->kex; |
| 58 | EC_KEY *client_key = NULL; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 59 | const EC_GROUP *group; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 60 | const EC_POINT *public_key; |
| 61 | int r; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 62 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 63 | 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 Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 71 | group = EC_KEY_get0_group(client_key); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 72 | public_key = EC_KEY_get0_public_key(client_key); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 73 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 74 | 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 Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 78 | debug("sending SSH2_MSG_KEX_ECDH_INIT"); |
| 79 | |
| 80 | #ifdef DEBUG_KEXECDH |
| 81 | fputs("client private key:\n", stderr); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 82 | sshkey_dump_ec_key(client_key); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 83 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 84 | kex->ec_client_key = client_key; |
| 85 | kex->ec_group = group; |
| 86 | client_key = NULL; /* owned by the kex */ |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 87 | |
| 88 | debug("expecting SSH2_MSG_KEX_ECDH_REPLY"); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 89 | ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_ecdh_reply); |
| 90 | r = 0; |
| 91 | out: |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 92 | EC_KEY_free(client_key); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 93 | return r; |
| 94 | } |
| 95 | |
| 96 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 97 | input_kex_ecdh_reply(int type, u_int32_t seq, struct ssh *ssh) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 98 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 99 | struct kex *kex = ssh->kex; |
| 100 | const EC_GROUP *group; |
| 101 | EC_POINT *server_public = NULL; |
| 102 | EC_KEY *client_key; |
| 103 | BIGNUM *shared_secret = NULL; |
| 104 | struct sshkey *server_host_key = NULL; |
| 105 | u_char *server_host_key_blob = NULL, *signature = NULL; |
| 106 | u_char *kbuf = NULL; |
| 107 | u_char hash[SSH_DIGEST_MAX_LENGTH]; |
| 108 | size_t slen, sbloblen; |
| 109 | size_t klen = 0, hashlen; |
| 110 | int r; |
| 111 | |
| 112 | if (kex->verify_host_key == NULL) { |
| 113 | r = SSH_ERR_INVALID_ARGUMENT; |
| 114 | goto out; |
| 115 | } |
| 116 | group = kex->ec_group; |
| 117 | client_key = kex->ec_client_key; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 118 | |
| 119 | /* hostkey */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 120 | if ((r = sshpkt_get_string(ssh, &server_host_key_blob, |
| 121 | &sbloblen)) != 0 || |
| 122 | (r = sshkey_from_blob(server_host_key_blob, sbloblen, |
| 123 | &server_host_key)) != 0) |
| 124 | goto out; |
djm@openbsd.org | 5104db7 | 2015-01-26 06:10:03 +0000 | [diff] [blame] | 125 | if (server_host_key->type != kex->hostkey_type || |
| 126 | (kex->hostkey_type == KEY_ECDSA && |
| 127 | server_host_key->ecdsa_nid != kex->hostkey_nid)) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 128 | r = SSH_ERR_KEY_TYPE_MISMATCH; |
| 129 | goto out; |
| 130 | } |
| 131 | if (kex->verify_host_key(server_host_key, ssh) == -1) { |
| 132 | r = SSH_ERR_SIGNATURE_INVALID; |
| 133 | goto out; |
| 134 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 135 | |
| 136 | /* Q_S, server public key */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 137 | /* signed H */ |
| 138 | if ((server_public = EC_POINT_new(group)) == NULL) { |
| 139 | r = SSH_ERR_ALLOC_FAIL; |
| 140 | goto out; |
| 141 | } |
| 142 | if ((r = sshpkt_get_ec(ssh, server_public, group)) != 0 || |
| 143 | (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 || |
| 144 | (r = sshpkt_get_end(ssh)) != 0) |
| 145 | goto out; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 146 | |
| 147 | #ifdef DEBUG_KEXECDH |
| 148 | fputs("server public key:\n", stderr); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 149 | sshkey_dump_ec_point(group, server_public); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 150 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 151 | if (sshkey_ec_validate_public(group, server_public) != 0) { |
| 152 | sshpkt_disconnect(ssh, "invalid server public key"); |
| 153 | r = SSH_ERR_MESSAGE_INCOMPLETE; |
| 154 | goto out; |
| 155 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 156 | |
| 157 | klen = (EC_GROUP_get_degree(group) + 7) / 8; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 158 | if ((kbuf = malloc(klen)) == NULL || |
| 159 | (shared_secret = BN_new()) == NULL) { |
| 160 | r = SSH_ERR_ALLOC_FAIL; |
| 161 | goto out; |
| 162 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 163 | if (ECDH_compute_key(kbuf, klen, server_public, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 164 | client_key, NULL) != (int)klen || |
| 165 | BN_bin2bn(kbuf, klen, shared_secret) == NULL) { |
| 166 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 167 | goto out; |
| 168 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 169 | |
| 170 | #ifdef DEBUG_KEXECDH |
| 171 | dump_digest("shared secret", kbuf, klen); |
| 172 | #endif |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 173 | /* calc and verify H */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 174 | hashlen = sizeof(hash); |
| 175 | if ((r = kex_ecdh_hash( |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 176 | kex->hash_alg, |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 177 | group, |
| 178 | kex->client_version_string, |
| 179 | kex->server_version_string, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 180 | sshbuf_ptr(kex->my), sshbuf_len(kex->my), |
| 181 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 182 | server_host_key_blob, sbloblen, |
| 183 | EC_KEY_get0_public_key(client_key), |
| 184 | server_public, |
| 185 | shared_secret, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 186 | hash, &hashlen)) != 0) |
| 187 | goto out; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 188 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 189 | if ((r = sshkey_verify(server_host_key, signature, slen, hash, |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 190 | hashlen, kex->hostkey_alg, ssh->compat)) != 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 191 | goto out; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 192 | |
| 193 | /* save session id */ |
| 194 | if (kex->session_id == NULL) { |
| 195 | kex->session_id_len = hashlen; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 196 | kex->session_id = malloc(kex->session_id_len); |
| 197 | if (kex->session_id == NULL) { |
| 198 | r = SSH_ERR_ALLOC_FAIL; |
| 199 | goto out; |
| 200 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 201 | memcpy(kex->session_id, hash, kex->session_id_len); |
| 202 | } |
| 203 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 204 | if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) |
| 205 | r = kex_send_newkeys(ssh); |
| 206 | out: |
| 207 | explicit_bzero(hash, sizeof(hash)); |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 208 | EC_KEY_free(kex->ec_client_key); |
| 209 | kex->ec_client_key = NULL; |
| 210 | EC_POINT_clear_free(server_public); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 211 | if (kbuf) { |
| 212 | explicit_bzero(kbuf, klen); |
| 213 | free(kbuf); |
| 214 | } |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 215 | BN_clear_free(shared_secret); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 216 | sshkey_free(server_host_key); |
| 217 | free(server_host_key_blob); |
| 218 | free(signature); |
| 219 | return r; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 220 | } |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 221 | #endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 222 | |