Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Niels Provos. All rights reserved. |
| 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 | |
| 28 | #include "xmalloc.h" |
| 29 | #include "key.h" |
| 30 | #include "kex.h" |
| 31 | #include "log.h" |
| 32 | #include "packet.h" |
| 33 | #include "dh.h" |
| 34 | #include "ssh2.h" |
| 35 | #include "compat.h" |
| 36 | #include "monitor_wrap.h" |
| 37 | |
| 38 | void |
| 39 | kexgex_server(Kex *kex) |
| 40 | { |
| 41 | BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; |
| 42 | Key *server_host_key; |
| 43 | DH *dh; |
| 44 | u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 45 | u_int sbloblen, klen, kout, slen, hashlen; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 46 | int min = -1, max = -1, nbits = -1, type; |
| 47 | |
| 48 | if (kex->load_host_key == NULL) |
| 49 | fatal("Cannot load hostkey"); |
| 50 | server_host_key = kex->load_host_key(kex->hostkey_type); |
| 51 | if (server_host_key == NULL) |
| 52 | fatal("Unsupported hostkey type %d", kex->hostkey_type); |
| 53 | |
| 54 | type = packet_read(); |
| 55 | switch (type) { |
| 56 | case SSH2_MSG_KEX_DH_GEX_REQUEST: |
| 57 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST received"); |
| 58 | min = packet_get_int(); |
| 59 | nbits = packet_get_int(); |
| 60 | max = packet_get_int(); |
| 61 | min = MAX(DH_GRP_MIN, min); |
| 62 | max = MIN(DH_GRP_MAX, max); |
| 63 | break; |
| 64 | case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD: |
| 65 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received"); |
| 66 | nbits = packet_get_int(); |
| 67 | min = DH_GRP_MIN; |
| 68 | max = DH_GRP_MAX; |
| 69 | /* unused for old GEX */ |
| 70 | break; |
| 71 | default: |
| 72 | fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type); |
| 73 | } |
| 74 | packet_check_eom(); |
| 75 | |
| 76 | if (max < min || nbits < min || max < nbits) |
| 77 | fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d", |
| 78 | min, nbits, max); |
| 79 | |
| 80 | /* Contact privileged parent */ |
| 81 | dh = PRIVSEP(choose_dh(min, nbits, max)); |
| 82 | if (dh == NULL) |
| 83 | packet_disconnect("Protocol error: no matching DH grp found"); |
| 84 | |
| 85 | debug("SSH2_MSG_KEX_DH_GEX_GROUP sent"); |
| 86 | packet_start(SSH2_MSG_KEX_DH_GEX_GROUP); |
| 87 | packet_put_bignum2(dh->p); |
| 88 | packet_put_bignum2(dh->g); |
| 89 | packet_send(); |
| 90 | |
| 91 | /* flush */ |
| 92 | packet_write_wait(); |
| 93 | |
| 94 | /* Compute our exchange value in parallel with the client */ |
| 95 | dh_gen_key(dh, kex->we_need * 8); |
| 96 | |
| 97 | debug("expecting SSH2_MSG_KEX_DH_GEX_INIT"); |
| 98 | packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT); |
| 99 | |
| 100 | /* key, cert */ |
| 101 | if ((dh_client_pub = BN_new()) == NULL) |
| 102 | fatal("dh_client_pub == NULL"); |
| 103 | packet_get_bignum2(dh_client_pub); |
| 104 | packet_check_eom(); |
| 105 | |
| 106 | #ifdef DEBUG_KEXDH |
| 107 | fprintf(stderr, "dh_client_pub= "); |
| 108 | BN_print_fp(stderr, dh_client_pub); |
| 109 | fprintf(stderr, "\n"); |
| 110 | debug("bits %d", BN_num_bits(dh_client_pub)); |
| 111 | #endif |
| 112 | |
| 113 | #ifdef DEBUG_KEXDH |
| 114 | DHparams_print_fp(stderr, dh); |
| 115 | fprintf(stderr, "pub= "); |
| 116 | BN_print_fp(stderr, dh->pub_key); |
| 117 | fprintf(stderr, "\n"); |
| 118 | #endif |
| 119 | if (!dh_pub_is_valid(dh, dh_client_pub)) |
| 120 | packet_disconnect("bad client public DH value"); |
| 121 | |
| 122 | klen = DH_size(dh); |
| 123 | kbuf = xmalloc(klen); |
| 124 | kout = DH_compute_key(kbuf, dh_client_pub, dh); |
| 125 | #ifdef DEBUG_KEXDH |
| 126 | dump_digest("shared secret", kbuf, kout); |
| 127 | #endif |
| 128 | if ((shared_secret = BN_new()) == NULL) |
| 129 | fatal("kexgex_server: BN_new failed"); |
| 130 | BN_bin2bn(kbuf, kout, shared_secret); |
| 131 | memset(kbuf, 0, klen); |
| 132 | xfree(kbuf); |
| 133 | |
| 134 | key_to_blob(server_host_key, &server_host_key_blob, &sbloblen); |
| 135 | |
| 136 | if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) |
| 137 | min = max = -1; |
| 138 | |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 139 | /* calc H */ |
| 140 | kexgex_hash( |
| 141 | kex->evp_md, |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 142 | kex->client_version_string, |
| 143 | kex->server_version_string, |
| 144 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), |
| 145 | buffer_ptr(&kex->my), buffer_len(&kex->my), |
| 146 | server_host_key_blob, sbloblen, |
| 147 | min, nbits, max, |
| 148 | dh->p, dh->g, |
| 149 | dh_client_pub, |
| 150 | dh->pub_key, |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 151 | shared_secret, |
| 152 | &hash, &hashlen |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 153 | ); |
| 154 | BN_clear_free(dh_client_pub); |
| 155 | |
| 156 | /* save session id := H */ |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 157 | if (kex->session_id == NULL) { |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 158 | kex->session_id_len = hashlen; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 159 | kex->session_id = xmalloc(kex->session_id_len); |
| 160 | memcpy(kex->session_id, hash, kex->session_id_len); |
| 161 | } |
| 162 | |
| 163 | /* sign H */ |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 164 | PRIVSEP(key_sign(server_host_key, &signature, &slen, hash, hashlen)); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 165 | |
| 166 | /* destroy_sensitive_data(); */ |
| 167 | |
| 168 | /* send server hostkey, DH pubkey 'f' and singed H */ |
| 169 | debug("SSH2_MSG_KEX_DH_GEX_REPLY sent"); |
| 170 | packet_start(SSH2_MSG_KEX_DH_GEX_REPLY); |
| 171 | packet_put_string(server_host_key_blob, sbloblen); |
| 172 | packet_put_bignum2(dh->pub_key); /* f */ |
| 173 | packet_put_string(signature, slen); |
| 174 | packet_send(); |
| 175 | |
| 176 | xfree(signature); |
| 177 | xfree(server_host_key_blob); |
| 178 | /* have keys, free DH */ |
| 179 | DH_free(dh); |
| 180 | |
Damien Miller | 19bb3a5 | 2005-11-05 15:19:35 +1100 | [diff] [blame] | 181 | kex_derive_keys(kex, hash, hashlen, shared_secret); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 182 | BN_clear_free(shared_secret); |
| 183 | |
| 184 | kex_finish(kex); |
| 185 | } |