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