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