Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame^] | 1 | /* $OpenBSD: kexecdhs.c,v 1.10 2014/02/02 03:44:31 djm 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 | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <string.h> |
| 31 | #include <signal.h> |
| 32 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 33 | #include "xmalloc.h" |
| 34 | #include "buffer.h" |
| 35 | #include "key.h" |
| 36 | #include "cipher.h" |
| 37 | #include "kex.h" |
| 38 | #include "log.h" |
| 39 | #include "packet.h" |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 40 | #include "ssh2.h" |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 41 | |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 42 | #ifdef OPENSSL_HAS_ECC |
| 43 | |
| 44 | #include <openssl/ecdh.h> |
| 45 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 46 | void |
| 47 | kexecdh_server(Kex *kex) |
| 48 | { |
| 49 | EC_POINT *client_public; |
| 50 | EC_KEY *server_key; |
| 51 | const EC_GROUP *group; |
| 52 | BIGNUM *shared_secret; |
| 53 | Key *server_host_private, *server_host_public; |
| 54 | u_char *server_host_key_blob = NULL, *signature = NULL; |
| 55 | u_char *kbuf, *hash; |
| 56 | u_int klen, slen, sbloblen, hashlen; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 57 | |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 58 | if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 59 | fatal("%s: EC_KEY_new_by_curve_name failed", __func__); |
| 60 | if (EC_KEY_generate_key(server_key) != 1) |
| 61 | fatal("%s: EC_KEY_generate_key failed", __func__); |
| 62 | group = EC_KEY_get0_group(server_key); |
| 63 | |
| 64 | #ifdef DEBUG_KEXECDH |
| 65 | fputs("server private key:\n", stderr); |
| 66 | key_dump_ec_key(server_key); |
| 67 | #endif |
| 68 | |
| 69 | if (kex->load_host_public_key == NULL || |
| 70 | kex->load_host_private_key == NULL) |
| 71 | fatal("Cannot load hostkey"); |
| 72 | server_host_public = kex->load_host_public_key(kex->hostkey_type); |
| 73 | if (server_host_public == NULL) |
| 74 | fatal("Unsupported hostkey type %d", kex->hostkey_type); |
| 75 | server_host_private = kex->load_host_private_key(kex->hostkey_type); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 76 | |
| 77 | debug("expecting SSH2_MSG_KEX_ECDH_INIT"); |
| 78 | packet_read_expect(SSH2_MSG_KEX_ECDH_INIT); |
| 79 | if ((client_public = EC_POINT_new(group)) == NULL) |
| 80 | fatal("%s: EC_POINT_new failed", __func__); |
| 81 | packet_get_ecpoint(group, client_public); |
| 82 | packet_check_eom(); |
| 83 | |
| 84 | if (key_ec_validate_public(group, client_public) != 0) |
| 85 | fatal("%s: invalid client public key", __func__); |
| 86 | |
| 87 | #ifdef DEBUG_KEXECDH |
| 88 | fputs("client public key:\n", stderr); |
| 89 | key_dump_ec_point(group, client_public); |
| 90 | #endif |
| 91 | |
| 92 | /* Calculate shared_secret */ |
| 93 | klen = (EC_GROUP_get_degree(group) + 7) / 8; |
| 94 | kbuf = xmalloc(klen); |
| 95 | if (ECDH_compute_key(kbuf, klen, client_public, |
| 96 | server_key, NULL) != (int)klen) |
| 97 | fatal("%s: ECDH_compute_key failed", __func__); |
| 98 | |
| 99 | #ifdef DEBUG_KEXDH |
| 100 | dump_digest("shared secret", kbuf, klen); |
| 101 | #endif |
| 102 | if ((shared_secret = BN_new()) == NULL) |
| 103 | fatal("%s: BN_new failed", __func__); |
| 104 | if (BN_bin2bn(kbuf, klen, shared_secret) == NULL) |
| 105 | fatal("%s: BN_bin2bn failed", __func__); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame^] | 106 | explicit_bzero(kbuf, klen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 107 | free(kbuf); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 108 | |
| 109 | /* calc H */ |
| 110 | key_to_blob(server_host_public, &server_host_key_blob, &sbloblen); |
| 111 | kex_ecdh_hash( |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 112 | kex->hash_alg, |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 113 | group, |
| 114 | kex->client_version_string, |
| 115 | kex->server_version_string, |
| 116 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), |
| 117 | buffer_ptr(&kex->my), buffer_len(&kex->my), |
| 118 | server_host_key_blob, sbloblen, |
| 119 | client_public, |
| 120 | EC_KEY_get0_public_key(server_key), |
| 121 | shared_secret, |
| 122 | &hash, &hashlen |
| 123 | ); |
| 124 | EC_POINT_clear_free(client_public); |
| 125 | |
| 126 | /* save session id := H */ |
| 127 | if (kex->session_id == NULL) { |
| 128 | kex->session_id_len = hashlen; |
| 129 | kex->session_id = xmalloc(kex->session_id_len); |
| 130 | memcpy(kex->session_id, hash, kex->session_id_len); |
| 131 | } |
| 132 | |
| 133 | /* sign H */ |
Damien Miller | 85b45e0 | 2013-07-20 13:21:52 +1000 | [diff] [blame] | 134 | kex->sign(server_host_private, server_host_public, &signature, &slen, |
| 135 | hash, hashlen); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 136 | |
| 137 | /* destroy_sensitive_data(); */ |
| 138 | |
| 139 | /* send server hostkey, ECDH pubkey 'Q_S' and signed H */ |
| 140 | packet_start(SSH2_MSG_KEX_ECDH_REPLY); |
| 141 | packet_put_string(server_host_key_blob, sbloblen); |
| 142 | packet_put_ecpoint(group, EC_KEY_get0_public_key(server_key)); |
| 143 | packet_put_string(signature, slen); |
| 144 | packet_send(); |
| 145 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 146 | free(signature); |
| 147 | free(server_host_key_blob); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 148 | /* have keys, free server key */ |
| 149 | EC_KEY_free(server_key); |
| 150 | |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 151 | kex_derive_keys_bn(kex, hash, hashlen, shared_secret); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 152 | BN_clear_free(shared_secret); |
| 153 | kex_finish(kex); |
| 154 | } |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 155 | #else /* OPENSSL_HAS_ECC */ |
| 156 | void |
| 157 | kexecdh_server(Kex *kex) |
| 158 | { |
| 159 | fatal("ECC support is not enabled"); |
| 160 | } |
| 161 | #endif /* OPENSSL_HAS_ECC */ |