djm@openbsd.org | 0e8eeec | 2016-05-02 10:26:04 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: kexdhs.c,v 1.24 2016/05/02 10:26:04 djm Exp $ */ |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2001 Markus Friedl. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "includes.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 27 | |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 28 | #ifdef WITH_OPENSSL |
| 29 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 30 | #include <sys/types.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 31 | |
| 32 | #include <stdarg.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 33 | #include <string.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 34 | #include <signal.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 35 | |
Damien Miller | 4499f4c | 2010-11-20 15:15:49 +1100 | [diff] [blame] | 36 | #include <openssl/dh.h> |
| 37 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 38 | #include "sshkey.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 39 | #include "cipher.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 40 | #include "digest.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 41 | #include "kex.h" |
| 42 | #include "log.h" |
| 43 | #include "packet.h" |
| 44 | #include "dh.h" |
| 45 | #include "ssh2.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 46 | |
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" |
| 51 | |
| 52 | static int input_kex_dh_init(int, u_int32_t, void *); |
| 53 | |
| 54 | int |
| 55 | kexdh_server(struct ssh *ssh) |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 56 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 57 | struct kex *kex = ssh->kex; |
| 58 | int r; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 59 | |
| 60 | /* generate server DH public key */ |
Damien Miller | f675fc4 | 2004-06-15 10:30:09 +1000 | [diff] [blame] | 61 | switch (kex->kex_type) { |
| 62 | case KEX_DH_GRP1_SHA1: |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 63 | kex->dh = dh_new_group1(); |
Damien Miller | f675fc4 | 2004-06-15 10:30:09 +1000 | [diff] [blame] | 64 | break; |
| 65 | case KEX_DH_GRP14_SHA1: |
djm@openbsd.org | 0e8eeec | 2016-05-02 10:26:04 +0000 | [diff] [blame^] | 66 | case KEX_DH_GRP14_SHA256: |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 67 | kex->dh = dh_new_group14(); |
Damien Miller | f675fc4 | 2004-06-15 10:30:09 +1000 | [diff] [blame] | 68 | break; |
djm@openbsd.org | 0e8eeec | 2016-05-02 10:26:04 +0000 | [diff] [blame^] | 69 | case KEX_DH_GRP16_SHA512: |
| 70 | kex->dh = dh_new_group16(); |
| 71 | break; |
| 72 | case KEX_DH_GRP18_SHA512: |
| 73 | kex->dh = dh_new_group18(); |
| 74 | break; |
Damien Miller | f675fc4 | 2004-06-15 10:30:09 +1000 | [diff] [blame] | 75 | default: |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 76 | r = SSH_ERR_INVALID_ARGUMENT; |
| 77 | goto out; |
Damien Miller | f675fc4 | 2004-06-15 10:30:09 +1000 | [diff] [blame] | 78 | } |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 79 | if (kex->dh == NULL) { |
| 80 | r = SSH_ERR_ALLOC_FAIL; |
| 81 | goto out; |
| 82 | } |
| 83 | if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0) |
| 84 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 85 | |
| 86 | debug("expecting SSH2_MSG_KEXDH_INIT"); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 87 | ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init); |
| 88 | r = 0; |
| 89 | out: |
| 90 | return r; |
| 91 | } |
| 92 | |
| 93 | int |
| 94 | input_kex_dh_init(int type, u_int32_t seq, void *ctxt) |
| 95 | { |
| 96 | struct ssh *ssh = ctxt; |
| 97 | struct kex *kex = ssh->kex; |
| 98 | BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; |
| 99 | struct sshkey *server_host_public, *server_host_private; |
| 100 | u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL; |
| 101 | u_char hash[SSH_DIGEST_MAX_LENGTH]; |
| 102 | size_t sbloblen, slen; |
| 103 | size_t klen = 0, hashlen; |
| 104 | int kout, r; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 105 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 106 | if (kex->load_host_public_key == NULL || |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 107 | kex->load_host_private_key == NULL) { |
| 108 | r = SSH_ERR_INVALID_ARGUMENT; |
| 109 | goto out; |
| 110 | } |
djm@openbsd.org | 5104db7 | 2015-01-26 06:10:03 +0000 | [diff] [blame] | 111 | server_host_public = kex->load_host_public_key(kex->hostkey_type, |
| 112 | kex->hostkey_nid, ssh); |
| 113 | server_host_private = kex->load_host_private_key(kex->hostkey_type, |
| 114 | kex->hostkey_nid, ssh); |
djm@openbsd.org | e2cc6be | 2015-01-20 07:55:33 +0000 | [diff] [blame] | 115 | if (server_host_public == NULL) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 116 | r = SSH_ERR_NO_HOSTKEY_LOADED; |
| 117 | goto out; |
| 118 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 119 | |
| 120 | /* key, cert */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 121 | if ((dh_client_pub = BN_new()) == NULL) { |
| 122 | r = SSH_ERR_ALLOC_FAIL; |
| 123 | goto out; |
| 124 | } |
| 125 | if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 || |
| 126 | (r = sshpkt_get_end(ssh)) != 0) |
| 127 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 128 | |
| 129 | #ifdef DEBUG_KEXDH |
| 130 | fprintf(stderr, "dh_client_pub= "); |
| 131 | BN_print_fp(stderr, dh_client_pub); |
| 132 | fprintf(stderr, "\n"); |
| 133 | debug("bits %d", BN_num_bits(dh_client_pub)); |
| 134 | #endif |
| 135 | |
| 136 | #ifdef DEBUG_KEXDH |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 137 | DHparams_print_fp(stderr, kex->dh); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 138 | fprintf(stderr, "pub= "); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 139 | BN_print_fp(stderr, kex->dh->pub_key); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 140 | fprintf(stderr, "\n"); |
| 141 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 142 | if (!dh_pub_is_valid(kex->dh, dh_client_pub)) { |
| 143 | sshpkt_disconnect(ssh, "bad client public DH value"); |
| 144 | r = SSH_ERR_MESSAGE_INCOMPLETE; |
| 145 | goto out; |
| 146 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 147 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 148 | klen = DH_size(kex->dh); |
| 149 | if ((kbuf = malloc(klen)) == NULL || |
| 150 | (shared_secret = BN_new()) == NULL) { |
| 151 | r = SSH_ERR_ALLOC_FAIL; |
| 152 | goto out; |
| 153 | } |
| 154 | if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 || |
| 155 | BN_bin2bn(kbuf, kout, shared_secret) == NULL) { |
| 156 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 157 | goto out; |
| 158 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 159 | #ifdef DEBUG_KEXDH |
| 160 | dump_digest("shared secret", kbuf, kout); |
| 161 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 162 | if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob, |
| 163 | &sbloblen)) != 0) |
| 164 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 165 | /* calc H */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 166 | hashlen = sizeof(hash); |
| 167 | if ((r = kex_dh_hash( |
djm@openbsd.org | 0e8eeec | 2016-05-02 10:26:04 +0000 | [diff] [blame^] | 168 | kex->hash_alg, |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 169 | kex->client_version_string, |
| 170 | kex->server_version_string, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 171 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), |
| 172 | sshbuf_ptr(kex->my), sshbuf_len(kex->my), |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 173 | server_host_key_blob, sbloblen, |
| 174 | dh_client_pub, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 175 | kex->dh->pub_key, |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 176 | shared_secret, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 177 | hash, &hashlen)) != 0) |
| 178 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 179 | |
| 180 | /* save session id := H */ |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 181 | if (kex->session_id == NULL) { |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 182 | kex->session_id_len = hashlen; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 183 | kex->session_id = malloc(kex->session_id_len); |
| 184 | if (kex->session_id == NULL) { |
| 185 | r = SSH_ERR_ALLOC_FAIL; |
| 186 | goto out; |
| 187 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 188 | memcpy(kex->session_id, hash, kex->session_id_len); |
| 189 | } |
| 190 | |
| 191 | /* sign H */ |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 192 | if ((r = kex->sign(server_host_private, server_host_public, &signature, |
| 193 | &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 194 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 195 | |
| 196 | /* destroy_sensitive_data(); */ |
| 197 | |
| 198 | /* send server hostkey, DH pubkey 'f' and singed H */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 199 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 || |
| 200 | (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 || |
| 201 | (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */ |
| 202 | (r = sshpkt_put_string(ssh, signature, slen)) != 0 || |
| 203 | (r = sshpkt_send(ssh)) != 0) |
| 204 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 205 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 206 | if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) |
| 207 | r = kex_send_newkeys(ssh); |
| 208 | out: |
| 209 | explicit_bzero(hash, sizeof(hash)); |
| 210 | DH_free(kex->dh); |
| 211 | kex->dh = NULL; |
| 212 | if (dh_client_pub) |
| 213 | BN_clear_free(dh_client_pub); |
| 214 | if (kbuf) { |
| 215 | explicit_bzero(kbuf, klen); |
| 216 | free(kbuf); |
| 217 | } |
| 218 | if (shared_secret) |
| 219 | BN_clear_free(shared_secret); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 220 | free(server_host_key_blob); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 221 | free(signature); |
| 222 | return r; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 223 | } |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 224 | #endif /* WITH_OPENSSL */ |