djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 1 | /* $OpenBSD: kexdhc.c,v 1.21 2017/12/18 02:25:15 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> |
| 31 | |
Damien Miller | 4499f4c | 2010-11-20 15:15:49 +1100 | [diff] [blame] | 32 | #include <openssl/dh.h> |
| 33 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 34 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 35 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 36 | #include <string.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 37 | #include <signal.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 38 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 39 | #include "sshkey.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +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 | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [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 | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 51 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 52 | static int input_kex_dh(int, u_int32_t, struct ssh *); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 53 | |
| 54 | int |
| 55 | kexdh_client(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 and send 'e', client 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 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 83 | debug("sending SSH2_MSG_KEXDH_INIT"); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 84 | if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 || |
| 85 | (r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 || |
| 86 | (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || |
| 87 | (r = sshpkt_send(ssh)) != 0) |
| 88 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 89 | #ifdef DEBUG_KEXDH |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 90 | DHparams_print_fp(stderr, kex->dh); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 91 | fprintf(stderr, "pub= "); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 92 | BN_print_fp(stderr, kex->dh->pub_key); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 93 | fprintf(stderr, "\n"); |
| 94 | #endif |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 95 | debug("expecting SSH2_MSG_KEXDH_REPLY"); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 96 | ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh); |
| 97 | r = 0; |
| 98 | out: |
| 99 | return r; |
| 100 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 101 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 102 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 103 | input_kex_dh(int type, u_int32_t seq, struct ssh *ssh) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 104 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 105 | struct kex *kex = ssh->kex; |
| 106 | BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; |
| 107 | struct sshkey *server_host_key = NULL; |
| 108 | u_char *kbuf = NULL, *server_host_key_blob = NULL, *signature = NULL; |
| 109 | u_char hash[SSH_DIGEST_MAX_LENGTH]; |
| 110 | size_t klen = 0, slen, sbloblen, hashlen; |
| 111 | int kout, r; |
| 112 | |
| 113 | if (kex->verify_host_key == NULL) { |
| 114 | r = SSH_ERR_INVALID_ARGUMENT; |
| 115 | goto out; |
| 116 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 117 | /* key, cert */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 118 | if ((r = sshpkt_get_string(ssh, &server_host_key_blob, |
| 119 | &sbloblen)) != 0 || |
| 120 | (r = sshkey_from_blob(server_host_key_blob, sbloblen, |
| 121 | &server_host_key)) != 0) |
| 122 | goto out; |
djm@openbsd.org | 5104db7 | 2015-01-26 06:10:03 +0000 | [diff] [blame] | 123 | if (server_host_key->type != kex->hostkey_type || |
| 124 | (kex->hostkey_type == KEY_ECDSA && |
| 125 | server_host_key->ecdsa_nid != kex->hostkey_nid)) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 126 | r = SSH_ERR_KEY_TYPE_MISMATCH; |
| 127 | goto out; |
| 128 | } |
| 129 | if (kex->verify_host_key(server_host_key, ssh) == -1) { |
| 130 | r = SSH_ERR_SIGNATURE_INVALID; |
| 131 | goto out; |
| 132 | } |
Damien Miller | ad6b14d | 2006-06-13 13:00:41 +1000 | [diff] [blame] | 133 | /* DH parameter f, server public DH key */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 134 | if ((dh_server_pub = BN_new()) == NULL) { |
| 135 | r = SSH_ERR_ALLOC_FAIL; |
| 136 | goto out; |
| 137 | } |
| 138 | /* signed H */ |
| 139 | if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 || |
| 140 | (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 || |
| 141 | (r = sshpkt_get_end(ssh)) != 0) |
| 142 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 143 | #ifdef DEBUG_KEXDH |
| 144 | fprintf(stderr, "dh_server_pub= "); |
| 145 | BN_print_fp(stderr, dh_server_pub); |
| 146 | fprintf(stderr, "\n"); |
| 147 | debug("bits %d", BN_num_bits(dh_server_pub)); |
| 148 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 149 | if (!dh_pub_is_valid(kex->dh, dh_server_pub)) { |
| 150 | sshpkt_disconnect(ssh, "bad server public DH value"); |
| 151 | r = SSH_ERR_MESSAGE_INCOMPLETE; |
| 152 | goto out; |
| 153 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 154 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 155 | klen = DH_size(kex->dh); |
| 156 | if ((kbuf = malloc(klen)) == NULL || |
| 157 | (shared_secret = BN_new()) == NULL) { |
| 158 | r = SSH_ERR_ALLOC_FAIL; |
| 159 | goto out; |
| 160 | } |
| 161 | if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 || |
| 162 | BN_bin2bn(kbuf, kout, shared_secret) == NULL) { |
| 163 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 164 | goto out; |
| 165 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 166 | #ifdef DEBUG_KEXDH |
| 167 | dump_digest("shared secret", kbuf, kout); |
| 168 | #endif |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 169 | |
| 170 | /* calc and verify H */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 171 | hashlen = sizeof(hash); |
| 172 | if ((r = kex_dh_hash( |
djm@openbsd.org | 0e8eeec | 2016-05-02 10:26:04 +0000 | [diff] [blame] | 173 | kex->hash_alg, |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 174 | kex->client_version_string, |
| 175 | kex->server_version_string, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 176 | sshbuf_ptr(kex->my), sshbuf_len(kex->my), |
| 177 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 178 | server_host_key_blob, sbloblen, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 179 | kex->dh->pub_key, |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 180 | dh_server_pub, |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 181 | shared_secret, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 182 | hash, &hashlen)) != 0) |
| 183 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 184 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 185 | if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen, |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 186 | kex->hostkey_alg, ssh->compat)) != 0) |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 187 | goto out; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 188 | |
| 189 | /* save session id */ |
| 190 | if (kex->session_id == NULL) { |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 191 | kex->session_id_len = hashlen; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 192 | kex->session_id = malloc(kex->session_id_len); |
| 193 | if (kex->session_id == NULL) { |
| 194 | r = SSH_ERR_ALLOC_FAIL; |
| 195 | goto out; |
| 196 | } |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 197 | memcpy(kex->session_id, hash, kex->session_id_len); |
| 198 | } |
| 199 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 200 | if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) |
| 201 | r = kex_send_newkeys(ssh); |
| 202 | out: |
| 203 | explicit_bzero(hash, sizeof(hash)); |
| 204 | DH_free(kex->dh); |
| 205 | kex->dh = NULL; |
| 206 | if (dh_server_pub) |
| 207 | BN_clear_free(dh_server_pub); |
| 208 | if (kbuf) { |
| 209 | explicit_bzero(kbuf, klen); |
| 210 | free(kbuf); |
| 211 | } |
| 212 | if (shared_secret) |
| 213 | BN_clear_free(shared_secret); |
| 214 | sshkey_free(server_host_key); |
| 215 | free(server_host_key_blob); |
| 216 | free(signature); |
| 217 | return r; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 218 | } |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 219 | #endif /* WITH_OPENSSL */ |