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