1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright (c) 2000 Niels Provos. All rights reserved. 3*0Sstevel@tonic-gate * Copyright (c) 2001 Markus Friedl. All rights reserved. 4*0Sstevel@tonic-gate * 5*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 6*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 7*0Sstevel@tonic-gate * are met: 8*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 9*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 10*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 11*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 12*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15*0Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17*0Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18*0Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20*0Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21*0Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22*0Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23*0Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24*0Sstevel@tonic-gate */ 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate #include "includes.h" 27*0Sstevel@tonic-gate RCSID("$OpenBSD: kexgex.c,v 1.22 2002/03/24 17:27:03 stevesk Exp $"); 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #include <openssl/bn.h> 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include "xmalloc.h" 34*0Sstevel@tonic-gate #include "buffer.h" 35*0Sstevel@tonic-gate #include "bufaux.h" 36*0Sstevel@tonic-gate #include "key.h" 37*0Sstevel@tonic-gate #include "kex.h" 38*0Sstevel@tonic-gate #include "log.h" 39*0Sstevel@tonic-gate #include "packet.h" 40*0Sstevel@tonic-gate #include "dh.h" 41*0Sstevel@tonic-gate #include "ssh2.h" 42*0Sstevel@tonic-gate #include "compat.h" 43*0Sstevel@tonic-gate #include "monitor_wrap.h" 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate void 46*0Sstevel@tonic-gate kexgex_server(Kex *kex) 47*0Sstevel@tonic-gate { 48*0Sstevel@tonic-gate BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; 49*0Sstevel@tonic-gate Key *server_host_key; 50*0Sstevel@tonic-gate DH *dh; 51*0Sstevel@tonic-gate u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; 52*0Sstevel@tonic-gate u_int sbloblen, klen, kout, slen; 53*0Sstevel@tonic-gate int min = -1, max = -1, nbits = -1, type; 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate if (kex->load_host_key == NULL) 56*0Sstevel@tonic-gate fatal("Cannot load hostkey"); 57*0Sstevel@tonic-gate server_host_key = kex->load_host_key(kex->hostkey_type); 58*0Sstevel@tonic-gate if (server_host_key == NULL) 59*0Sstevel@tonic-gate fatal("Unsupported hostkey type %d", kex->hostkey_type); 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate type = packet_read(); 62*0Sstevel@tonic-gate switch (type) { 63*0Sstevel@tonic-gate case SSH2_MSG_KEX_DH_GEX_REQUEST: 64*0Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_REQUEST received"); 65*0Sstevel@tonic-gate min = packet_get_int(); 66*0Sstevel@tonic-gate nbits = packet_get_int(); 67*0Sstevel@tonic-gate max = packet_get_int(); 68*0Sstevel@tonic-gate min = MAX(DH_GRP_MIN, min); 69*0Sstevel@tonic-gate max = MIN(DH_GRP_MAX, max); 70*0Sstevel@tonic-gate break; 71*0Sstevel@tonic-gate case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD: 72*0Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received"); 73*0Sstevel@tonic-gate nbits = packet_get_int(); 74*0Sstevel@tonic-gate min = DH_GRP_MIN; 75*0Sstevel@tonic-gate max = DH_GRP_MAX; 76*0Sstevel@tonic-gate /* unused for old GEX */ 77*0Sstevel@tonic-gate break; 78*0Sstevel@tonic-gate default: 79*0Sstevel@tonic-gate fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type); 80*0Sstevel@tonic-gate } 81*0Sstevel@tonic-gate packet_check_eom(); 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate if (max < min || nbits < min || max < nbits) 84*0Sstevel@tonic-gate fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d", 85*0Sstevel@tonic-gate min, nbits, max); 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate /* Contact privileged parent */ 88*0Sstevel@tonic-gate dh = PRIVSEP(choose_dh(min, nbits, max)); 89*0Sstevel@tonic-gate if (dh == NULL) 90*0Sstevel@tonic-gate packet_disconnect("Protocol error: no matching DH grp found"); 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_GROUP sent"); 93*0Sstevel@tonic-gate packet_start(SSH2_MSG_KEX_DH_GEX_GROUP); 94*0Sstevel@tonic-gate packet_put_bignum2(dh->p); 95*0Sstevel@tonic-gate packet_put_bignum2(dh->g); 96*0Sstevel@tonic-gate packet_send(); 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate /* flush */ 99*0Sstevel@tonic-gate packet_write_wait(); 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate /* Compute our exchange value in parallel with the client */ 102*0Sstevel@tonic-gate dh_gen_key(dh, kex->we_need * 8); 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate debug("expecting SSH2_MSG_KEX_DH_GEX_INIT"); 105*0Sstevel@tonic-gate packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT); 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate /* key, cert */ 108*0Sstevel@tonic-gate if ((dh_client_pub = BN_new()) == NULL) 109*0Sstevel@tonic-gate fatal("dh_client_pub == NULL"); 110*0Sstevel@tonic-gate packet_get_bignum2(dh_client_pub); 111*0Sstevel@tonic-gate packet_check_eom(); 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate #ifdef DEBUG_KEXDH 114*0Sstevel@tonic-gate fprintf(stderr, "dh_client_pub= "); 115*0Sstevel@tonic-gate BN_print_fp(stderr, dh_client_pub); 116*0Sstevel@tonic-gate fprintf(stderr, "\n"); 117*0Sstevel@tonic-gate debug("bits %d", BN_num_bits(dh_client_pub)); 118*0Sstevel@tonic-gate #endif 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate #ifdef DEBUG_KEXDH 121*0Sstevel@tonic-gate DHparams_print_fp(stderr, dh); 122*0Sstevel@tonic-gate fprintf(stderr, "pub= "); 123*0Sstevel@tonic-gate BN_print_fp(stderr, dh->pub_key); 124*0Sstevel@tonic-gate fprintf(stderr, "\n"); 125*0Sstevel@tonic-gate #endif 126*0Sstevel@tonic-gate if (!dh_pub_is_valid(dh, dh_client_pub)) 127*0Sstevel@tonic-gate packet_disconnect("bad client public DH value"); 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate klen = DH_size(dh); 130*0Sstevel@tonic-gate kbuf = xmalloc(klen); 131*0Sstevel@tonic-gate kout = DH_compute_key(kbuf, dh_client_pub, dh); 132*0Sstevel@tonic-gate #ifdef DEBUG_KEXDH 133*0Sstevel@tonic-gate dump_digest("shared secret", kbuf, kout); 134*0Sstevel@tonic-gate #endif 135*0Sstevel@tonic-gate if ((shared_secret = BN_new()) == NULL) 136*0Sstevel@tonic-gate fatal("kexgex_server: BN_new failed"); 137*0Sstevel@tonic-gate BN_bin2bn(kbuf, kout, shared_secret); 138*0Sstevel@tonic-gate memset(kbuf, 0, klen); 139*0Sstevel@tonic-gate xfree(kbuf); 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate key_to_blob(server_host_key, &server_host_key_blob, &sbloblen); 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) 144*0Sstevel@tonic-gate min = max = -1; 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate /* calc H */ /* XXX depends on 'kex' */ 147*0Sstevel@tonic-gate hash = kexgex_hash( 148*0Sstevel@tonic-gate kex->client_version_string, 149*0Sstevel@tonic-gate kex->server_version_string, 150*0Sstevel@tonic-gate buffer_ptr(&kex->peer), buffer_len(&kex->peer), 151*0Sstevel@tonic-gate buffer_ptr(&kex->my), buffer_len(&kex->my), 152*0Sstevel@tonic-gate server_host_key_blob, sbloblen, 153*0Sstevel@tonic-gate min, nbits, max, 154*0Sstevel@tonic-gate dh->p, dh->g, 155*0Sstevel@tonic-gate dh_client_pub, 156*0Sstevel@tonic-gate dh->pub_key, 157*0Sstevel@tonic-gate shared_secret 158*0Sstevel@tonic-gate ); 159*0Sstevel@tonic-gate BN_clear_free(dh_client_pub); 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate /* save session id := H */ 162*0Sstevel@tonic-gate /* XXX hashlen depends on KEX */ 163*0Sstevel@tonic-gate if (kex->session_id == NULL) { 164*0Sstevel@tonic-gate kex->session_id_len = 20; 165*0Sstevel@tonic-gate kex->session_id = xmalloc(kex->session_id_len); 166*0Sstevel@tonic-gate memcpy(kex->session_id, hash, kex->session_id_len); 167*0Sstevel@tonic-gate } 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate /* sign H */ 170*0Sstevel@tonic-gate /* XXX hashlen depends on KEX */ 171*0Sstevel@tonic-gate PRIVSEP(key_sign(server_host_key, &signature, &slen, hash, 20)); 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate /* destroy_sensitive_data(); */ 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate /* send server hostkey, DH pubkey 'f' and singed H */ 176*0Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_REPLY sent"); 177*0Sstevel@tonic-gate packet_start(SSH2_MSG_KEX_DH_GEX_REPLY); 178*0Sstevel@tonic-gate packet_put_string(server_host_key_blob, sbloblen); 179*0Sstevel@tonic-gate packet_put_bignum2(dh->pub_key); /* f */ 180*0Sstevel@tonic-gate packet_put_string(signature, slen); 181*0Sstevel@tonic-gate packet_send(); 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate xfree(signature); 184*0Sstevel@tonic-gate xfree(server_host_key_blob); 185*0Sstevel@tonic-gate /* have keys, free DH */ 186*0Sstevel@tonic-gate DH_free(dh); 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate kex_derive_keys(kex, hash, shared_secret); 189*0Sstevel@tonic-gate BN_clear_free(shared_secret); 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate kex_finish(kex); 192*0Sstevel@tonic-gate } 193