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