Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1 | /* $OpenBSD: kexgexc.c,v 1.17 2014/02/02 03:44:31 djm Exp $ */ |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000 Niels Provos. All rights reserved. |
| 4 | * Copyright (c) 2001 Markus Friedl. 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 | |
| 27 | #include "includes.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 28 | |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 29 | #ifdef WITH_OPENSSL |
| 30 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | |
Damien Miller | 4499f4c | 2010-11-20 15:15:49 +1100 | [diff] [blame] | 33 | #include <openssl/dh.h> |
| 34 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 35 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 36 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 37 | #include <string.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 38 | #include <signal.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 39 | |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 40 | #include "xmalloc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 41 | #include "buffer.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 42 | #include "key.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 43 | #include "cipher.h" |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 44 | #include "kex.h" |
| 45 | #include "log.h" |
| 46 | #include "packet.h" |
| 47 | #include "dh.h" |
| 48 | #include "ssh2.h" |
| 49 | #include "compat.h" |
| 50 | |
| 51 | void |
| 52 | kexgex_client(Kex *kex) |
| 53 | { |
| 54 | BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; |
| 55 | BIGNUM *p = NULL, *g = NULL; |
| 56 | Key *server_host_key; |
| 57 | u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; |
Damien Miller | 570c2ab | 2006-11-05 05:32:02 +1100 | [diff] [blame] | 58 | u_int klen, slen, sbloblen, hashlen; |
| 59 | int kout; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 60 | int min, max, nbits; |
| 61 | DH *dh; |
| 62 | |
Damien Miller | 76eea4a | 2014-01-26 09:37:25 +1100 | [diff] [blame] | 63 | nbits = dh_estimate(kex->dh_need * 8); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 64 | |
| 65 | if (datafellows & SSH_OLD_DHGEX) { |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 66 | /* Old GEX request */ |
| 67 | packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD); |
| 68 | packet_put_int(nbits); |
| 69 | min = DH_GRP_MIN; |
| 70 | max = DH_GRP_MAX; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 71 | |
Darren Tucker | 564f19e | 2003-12-09 19:18:07 +1100 | [diff] [blame] | 72 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits); |
| 73 | } else { |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 74 | /* New GEX request */ |
| 75 | min = DH_GRP_MIN; |
| 76 | max = DH_GRP_MAX; |
| 77 | packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST); |
| 78 | packet_put_int(min); |
| 79 | packet_put_int(nbits); |
| 80 | packet_put_int(max); |
Darren Tucker | 564f19e | 2003-12-09 19:18:07 +1100 | [diff] [blame] | 81 | |
| 82 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent", |
| 83 | min, nbits, max); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 84 | } |
| 85 | #ifdef DEBUG_KEXDH |
| 86 | fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n", |
| 87 | min, nbits, max); |
| 88 | #endif |
| 89 | packet_send(); |
| 90 | |
| 91 | debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP"); |
| 92 | packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP); |
| 93 | |
| 94 | if ((p = BN_new()) == NULL) |
| 95 | fatal("BN_new"); |
| 96 | packet_get_bignum2(p); |
| 97 | if ((g = BN_new()) == NULL) |
| 98 | fatal("BN_new"); |
| 99 | packet_get_bignum2(g); |
| 100 | packet_check_eom(); |
| 101 | |
| 102 | if (BN_num_bits(p) < min || BN_num_bits(p) > max) |
| 103 | fatal("DH_GEX group out of range: %d !< %d !< %d", |
| 104 | min, BN_num_bits(p), max); |
| 105 | |
| 106 | dh = dh_new_group(g, p); |
| 107 | dh_gen_key(dh, kex->we_need * 8); |
| 108 | |
| 109 | #ifdef DEBUG_KEXDH |
| 110 | DHparams_print_fp(stderr, dh); |
| 111 | fprintf(stderr, "pub= "); |
| 112 | BN_print_fp(stderr, dh->pub_key); |
| 113 | fprintf(stderr, "\n"); |
| 114 | #endif |
| 115 | |
| 116 | debug("SSH2_MSG_KEX_DH_GEX_INIT sent"); |
| 117 | /* generate and send 'e', client DH public key */ |
| 118 | packet_start(SSH2_MSG_KEX_DH_GEX_INIT); |
| 119 | packet_put_bignum2(dh->pub_key); |
| 120 | packet_send(); |
| 121 | |
| 122 | debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY"); |
| 123 | packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY); |
| 124 | |
| 125 | /* key, cert */ |
| 126 | server_host_key_blob = packet_get_string(&sbloblen); |
| 127 | server_host_key = key_from_blob(server_host_key_blob, sbloblen); |
| 128 | if (server_host_key == NULL) |
| 129 | fatal("cannot decode server_host_key_blob"); |
| 130 | if (server_host_key->type != kex->hostkey_type) |
| 131 | fatal("type mismatch for decoded server_host_key_blob"); |
| 132 | if (kex->verify_host_key == NULL) |
| 133 | fatal("cannot verify server_host_key"); |
| 134 | if (kex->verify_host_key(server_host_key) == -1) |
| 135 | fatal("server_host_key verification failed"); |
| 136 | |
Damien Miller | ad6b14d | 2006-06-13 13:00:41 +1000 | [diff] [blame] | 137 | /* DH parameter f, server public DH key */ |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 138 | if ((dh_server_pub = BN_new()) == NULL) |
| 139 | fatal("dh_server_pub == NULL"); |
| 140 | packet_get_bignum2(dh_server_pub); |
| 141 | |
| 142 | #ifdef DEBUG_KEXDH |
| 143 | fprintf(stderr, "dh_server_pub= "); |
| 144 | BN_print_fp(stderr, dh_server_pub); |
| 145 | fprintf(stderr, "\n"); |
| 146 | debug("bits %d", BN_num_bits(dh_server_pub)); |
| 147 | #endif |
| 148 | |
| 149 | /* signed H */ |
| 150 | signature = packet_get_string(&slen); |
| 151 | packet_check_eom(); |
| 152 | |
| 153 | if (!dh_pub_is_valid(dh, dh_server_pub)) |
| 154 | packet_disconnect("bad server public DH value"); |
| 155 | |
| 156 | klen = DH_size(dh); |
| 157 | kbuf = xmalloc(klen); |
Damien Miller | 570c2ab | 2006-11-05 05:32:02 +1100 | [diff] [blame] | 158 | if ((kout = DH_compute_key(kbuf, dh_server_pub, dh)) < 0) |
| 159 | fatal("DH_compute_key: failed"); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 160 | #ifdef DEBUG_KEXDH |
| 161 | dump_digest("shared secret", kbuf, kout); |
| 162 | #endif |
| 163 | if ((shared_secret = BN_new()) == NULL) |
| 164 | fatal("kexgex_client: BN_new failed"); |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 165 | if (BN_bin2bn(kbuf, kout, shared_secret) == NULL) |
| 166 | fatal("kexgex_client: BN_bin2bn failed"); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 167 | explicit_bzero(kbuf, klen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 168 | free(kbuf); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 169 | |
| 170 | if (datafellows & SSH_OLD_DHGEX) |
| 171 | min = max = -1; |
| 172 | |
| 173 | /* calc and verify H */ |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 174 | kexgex_hash( |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 175 | kex->hash_alg, |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 176 | kex->client_version_string, |
| 177 | kex->server_version_string, |
| 178 | buffer_ptr(&kex->my), buffer_len(&kex->my), |
| 179 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), |
| 180 | server_host_key_blob, sbloblen, |
| 181 | min, nbits, max, |
| 182 | dh->p, dh->g, |
| 183 | dh->pub_key, |
| 184 | dh_server_pub, |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 185 | shared_secret, |
| 186 | &hash, &hashlen |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 187 | ); |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 188 | |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 189 | /* have keys, free DH */ |
| 190 | DH_free(dh); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 191 | free(server_host_key_blob); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 192 | BN_clear_free(dh_server_pub); |
| 193 | |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 194 | if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1) |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 195 | fatal("key_verify failed for server_host_key"); |
| 196 | key_free(server_host_key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 197 | free(signature); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 198 | |
| 199 | /* save session id */ |
| 200 | if (kex->session_id == NULL) { |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 201 | kex->session_id_len = hashlen; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 202 | kex->session_id = xmalloc(kex->session_id_len); |
| 203 | memcpy(kex->session_id, hash, kex->session_id_len); |
| 204 | } |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 205 | kex_derive_keys_bn(kex, hash, hashlen, shared_secret); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 206 | BN_clear_free(shared_secret); |
| 207 | |
| 208 | kex_finish(kex); |
| 209 | } |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 210 | #endif /* WITH_OPENSSL */ |