10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Copyright (c) 2000 Niels Provos. All rights reserved. 30Sstevel@tonic-gate * Copyright (c) 2001 Markus Friedl. All rights reserved. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 60Sstevel@tonic-gate * modification, are permitted provided that the following conditions 70Sstevel@tonic-gate * are met: 80Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 90Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 100Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 110Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 120Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 150Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 160Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 170Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 180Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 190Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 200Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 210Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 220Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 230Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include "includes.h" 270Sstevel@tonic-gate RCSID("$OpenBSD: kexgex.c,v 1.22 2002/03/24 17:27:03 stevesk Exp $"); 280Sstevel@tonic-gate 290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include <openssl/bn.h> 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include "xmalloc.h" 340Sstevel@tonic-gate #include "buffer.h" 350Sstevel@tonic-gate #include "bufaux.h" 360Sstevel@tonic-gate #include "key.h" 370Sstevel@tonic-gate #include "kex.h" 380Sstevel@tonic-gate #include "log.h" 390Sstevel@tonic-gate #include "packet.h" 400Sstevel@tonic-gate #include "dh.h" 410Sstevel@tonic-gate #include "ssh2.h" 420Sstevel@tonic-gate #include "compat.h" 430Sstevel@tonic-gate 440Sstevel@tonic-gate void 450Sstevel@tonic-gate kexgex_server(Kex *kex) 460Sstevel@tonic-gate { 470Sstevel@tonic-gate BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; 480Sstevel@tonic-gate Key *server_host_key; 490Sstevel@tonic-gate DH *dh; 500Sstevel@tonic-gate u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; 510Sstevel@tonic-gate u_int sbloblen, klen, kout, slen; 520Sstevel@tonic-gate int min = -1, max = -1, nbits = -1, type; 530Sstevel@tonic-gate 540Sstevel@tonic-gate if (kex->load_host_key == NULL) 550Sstevel@tonic-gate fatal("Cannot load hostkey"); 560Sstevel@tonic-gate server_host_key = kex->load_host_key(kex->hostkey_type); 570Sstevel@tonic-gate if (server_host_key == NULL) 580Sstevel@tonic-gate fatal("Unsupported hostkey type %d", kex->hostkey_type); 590Sstevel@tonic-gate 600Sstevel@tonic-gate type = packet_read(); 610Sstevel@tonic-gate switch (type) { 620Sstevel@tonic-gate case SSH2_MSG_KEX_DH_GEX_REQUEST: 630Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_REQUEST received"); 640Sstevel@tonic-gate min = packet_get_int(); 650Sstevel@tonic-gate nbits = packet_get_int(); 660Sstevel@tonic-gate max = packet_get_int(); 670Sstevel@tonic-gate min = MAX(DH_GRP_MIN, min); 680Sstevel@tonic-gate max = MIN(DH_GRP_MAX, max); 690Sstevel@tonic-gate break; 700Sstevel@tonic-gate case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD: 710Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received"); 720Sstevel@tonic-gate nbits = packet_get_int(); 730Sstevel@tonic-gate min = DH_GRP_MIN; 740Sstevel@tonic-gate max = DH_GRP_MAX; 750Sstevel@tonic-gate /* unused for old GEX */ 760Sstevel@tonic-gate break; 770Sstevel@tonic-gate default: 780Sstevel@tonic-gate fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type); 790Sstevel@tonic-gate } 800Sstevel@tonic-gate packet_check_eom(); 810Sstevel@tonic-gate 820Sstevel@tonic-gate if (max < min || nbits < min || max < nbits) 830Sstevel@tonic-gate fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d", 840Sstevel@tonic-gate min, nbits, max); 850Sstevel@tonic-gate 860Sstevel@tonic-gate /* Contact privileged parent */ 87*5562Sjp161948 dh = choose_dh(min, nbits, max); 880Sstevel@tonic-gate if (dh == NULL) 890Sstevel@tonic-gate packet_disconnect("Protocol error: no matching DH grp found"); 900Sstevel@tonic-gate 910Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_GROUP sent"); 920Sstevel@tonic-gate packet_start(SSH2_MSG_KEX_DH_GEX_GROUP); 930Sstevel@tonic-gate packet_put_bignum2(dh->p); 940Sstevel@tonic-gate packet_put_bignum2(dh->g); 950Sstevel@tonic-gate packet_send(); 960Sstevel@tonic-gate 970Sstevel@tonic-gate /* flush */ 980Sstevel@tonic-gate packet_write_wait(); 990Sstevel@tonic-gate 1000Sstevel@tonic-gate /* Compute our exchange value in parallel with the client */ 1010Sstevel@tonic-gate dh_gen_key(dh, kex->we_need * 8); 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate debug("expecting SSH2_MSG_KEX_DH_GEX_INIT"); 1040Sstevel@tonic-gate packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT); 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* key, cert */ 1070Sstevel@tonic-gate if ((dh_client_pub = BN_new()) == NULL) 1080Sstevel@tonic-gate fatal("dh_client_pub == NULL"); 1090Sstevel@tonic-gate packet_get_bignum2(dh_client_pub); 1100Sstevel@tonic-gate packet_check_eom(); 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate #ifdef DEBUG_KEXDH 1130Sstevel@tonic-gate fprintf(stderr, "dh_client_pub= "); 1140Sstevel@tonic-gate BN_print_fp(stderr, dh_client_pub); 1150Sstevel@tonic-gate fprintf(stderr, "\n"); 1160Sstevel@tonic-gate debug("bits %d", BN_num_bits(dh_client_pub)); 1170Sstevel@tonic-gate #endif 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate #ifdef DEBUG_KEXDH 1200Sstevel@tonic-gate DHparams_print_fp(stderr, dh); 1210Sstevel@tonic-gate fprintf(stderr, "pub= "); 1220Sstevel@tonic-gate BN_print_fp(stderr, dh->pub_key); 1230Sstevel@tonic-gate fprintf(stderr, "\n"); 1240Sstevel@tonic-gate #endif 1250Sstevel@tonic-gate if (!dh_pub_is_valid(dh, dh_client_pub)) 1260Sstevel@tonic-gate packet_disconnect("bad client public DH value"); 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate klen = DH_size(dh); 1290Sstevel@tonic-gate kbuf = xmalloc(klen); 1300Sstevel@tonic-gate kout = DH_compute_key(kbuf, dh_client_pub, dh); 1310Sstevel@tonic-gate #ifdef DEBUG_KEXDH 1320Sstevel@tonic-gate dump_digest("shared secret", kbuf, kout); 1330Sstevel@tonic-gate #endif 1340Sstevel@tonic-gate if ((shared_secret = BN_new()) == NULL) 1350Sstevel@tonic-gate fatal("kexgex_server: BN_new failed"); 1360Sstevel@tonic-gate BN_bin2bn(kbuf, kout, shared_secret); 1370Sstevel@tonic-gate memset(kbuf, 0, klen); 1380Sstevel@tonic-gate xfree(kbuf); 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate key_to_blob(server_host_key, &server_host_key_blob, &sbloblen); 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) 1430Sstevel@tonic-gate min = max = -1; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate /* calc H */ /* XXX depends on 'kex' */ 1460Sstevel@tonic-gate hash = kexgex_hash( 1470Sstevel@tonic-gate kex->client_version_string, 1480Sstevel@tonic-gate kex->server_version_string, 1490Sstevel@tonic-gate buffer_ptr(&kex->peer), buffer_len(&kex->peer), 1500Sstevel@tonic-gate buffer_ptr(&kex->my), buffer_len(&kex->my), 1510Sstevel@tonic-gate server_host_key_blob, sbloblen, 1520Sstevel@tonic-gate min, nbits, max, 1530Sstevel@tonic-gate dh->p, dh->g, 1540Sstevel@tonic-gate dh_client_pub, 1550Sstevel@tonic-gate dh->pub_key, 1560Sstevel@tonic-gate shared_secret 1570Sstevel@tonic-gate ); 1580Sstevel@tonic-gate BN_clear_free(dh_client_pub); 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate /* save session id := H */ 1610Sstevel@tonic-gate /* XXX hashlen depends on KEX */ 1620Sstevel@tonic-gate if (kex->session_id == NULL) { 1630Sstevel@tonic-gate kex->session_id_len = 20; 1640Sstevel@tonic-gate kex->session_id = xmalloc(kex->session_id_len); 1650Sstevel@tonic-gate memcpy(kex->session_id, hash, kex->session_id_len); 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate /* sign H */ 1690Sstevel@tonic-gate /* XXX hashlen depends on KEX */ 170*5562Sjp161948 key_sign(server_host_key, &signature, &slen, hash, 20); 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate /* destroy_sensitive_data(); */ 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate /* send server hostkey, DH pubkey 'f' and singed H */ 1750Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_REPLY sent"); 1760Sstevel@tonic-gate packet_start(SSH2_MSG_KEX_DH_GEX_REPLY); 1770Sstevel@tonic-gate packet_put_string(server_host_key_blob, sbloblen); 1780Sstevel@tonic-gate packet_put_bignum2(dh->pub_key); /* f */ 1790Sstevel@tonic-gate packet_put_string(signature, slen); 1800Sstevel@tonic-gate packet_send(); 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate xfree(signature); 1830Sstevel@tonic-gate xfree(server_host_key_blob); 1840Sstevel@tonic-gate /* have keys, free DH */ 1850Sstevel@tonic-gate DH_free(dh); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate kex_derive_keys(kex, hash, shared_secret); 1880Sstevel@tonic-gate BN_clear_free(shared_secret); 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate kex_finish(kex); 1910Sstevel@tonic-gate } 192