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