1264377Sdes/* $OpenBSD: kexgexs.c,v 1.19 2014/02/02 03:44:31 djm Exp $ */ 2113908Sdes/* 3113908Sdes * Copyright (c) 2000 Niels Provos. All rights reserved. 4113908Sdes * Copyright (c) 2001 Markus Friedl. All rights reserved. 5113908Sdes * 6113908Sdes * Redistribution and use in source and binary forms, with or without 7113908Sdes * modification, are permitted provided that the following conditions 8113908Sdes * are met: 9113908Sdes * 1. Redistributions of source code must retain the above copyright 10113908Sdes * notice, this list of conditions and the following disclaimer. 11113908Sdes * 2. Redistributions in binary form must reproduce the above copyright 12113908Sdes * notice, this list of conditions and the following disclaimer in the 13113908Sdes * documentation and/or other materials provided with the distribution. 14113908Sdes * 15113908Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16113908Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17113908Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18113908Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19113908Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20113908Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21113908Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22113908Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23113908Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24113908Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25113908Sdes */ 26113908Sdes 27113908Sdes#include "includes.h" 28113908Sdes 29162852Sdes#include <sys/param.h> 30162852Sdes 31162852Sdes#include <stdarg.h> 32162852Sdes#include <stdio.h> 33162852Sdes#include <string.h> 34162852Sdes#include <signal.h> 35162852Sdes 36221420Sdes#include <openssl/dh.h> 37221420Sdes 38113908Sdes#include "xmalloc.h" 39162852Sdes#include "buffer.h" 40113908Sdes#include "key.h" 41162852Sdes#include "cipher.h" 42113908Sdes#include "kex.h" 43113908Sdes#include "log.h" 44113908Sdes#include "packet.h" 45113908Sdes#include "dh.h" 46113908Sdes#include "ssh2.h" 47113908Sdes#include "compat.h" 48162852Sdes#ifdef GSSAPI 49162852Sdes#include "ssh-gss.h" 50162852Sdes#endif 51113908Sdes#include "monitor_wrap.h" 52113908Sdes 53113908Sdesvoid 54113908Sdeskexgex_server(Kex *kex) 55113908Sdes{ 56113908Sdes BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; 57204917Sdes Key *server_host_public, *server_host_private; 58113908Sdes DH *dh; 59113908Sdes u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; 60164146Sdes u_int sbloblen, klen, slen, hashlen; 61192595Sdes int omin = -1, min = -1, omax = -1, max = -1, onbits = -1, nbits = -1; 62192595Sdes int type, kout; 63113908Sdes 64204917Sdes if (kex->load_host_public_key == NULL || 65204917Sdes kex->load_host_private_key == NULL) 66113908Sdes fatal("Cannot load hostkey"); 67204917Sdes server_host_public = kex->load_host_public_key(kex->hostkey_type); 68204917Sdes if (server_host_public == NULL) 69113908Sdes fatal("Unsupported hostkey type %d", kex->hostkey_type); 70204917Sdes server_host_private = kex->load_host_private_key(kex->hostkey_type); 71113908Sdes 72113908Sdes type = packet_read(); 73113908Sdes switch (type) { 74113908Sdes case SSH2_MSG_KEX_DH_GEX_REQUEST: 75113908Sdes debug("SSH2_MSG_KEX_DH_GEX_REQUEST received"); 76192595Sdes omin = min = packet_get_int(); 77192595Sdes onbits = nbits = packet_get_int(); 78192595Sdes omax = max = packet_get_int(); 79113908Sdes min = MAX(DH_GRP_MIN, min); 80113908Sdes max = MIN(DH_GRP_MAX, max); 81192595Sdes nbits = MAX(DH_GRP_MIN, nbits); 82192595Sdes nbits = MIN(DH_GRP_MAX, nbits); 83113908Sdes break; 84113908Sdes case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD: 85113908Sdes debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received"); 86192595Sdes onbits = nbits = packet_get_int(); 87113908Sdes /* unused for old GEX */ 88192595Sdes omin = min = DH_GRP_MIN; 89192595Sdes omax = max = DH_GRP_MAX; 90113908Sdes break; 91113908Sdes default: 92113908Sdes fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type); 93113908Sdes } 94113908Sdes packet_check_eom(); 95113908Sdes 96192595Sdes if (omax < omin || onbits < omin || omax < onbits) 97113908Sdes fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d", 98192595Sdes omin, onbits, omax); 99113908Sdes 100113908Sdes /* Contact privileged parent */ 101113908Sdes dh = PRIVSEP(choose_dh(min, nbits, max)); 102113908Sdes if (dh == NULL) 103113908Sdes packet_disconnect("Protocol error: no matching DH grp found"); 104113908Sdes 105113908Sdes debug("SSH2_MSG_KEX_DH_GEX_GROUP sent"); 106113908Sdes packet_start(SSH2_MSG_KEX_DH_GEX_GROUP); 107113908Sdes packet_put_bignum2(dh->p); 108113908Sdes packet_put_bignum2(dh->g); 109113908Sdes packet_send(); 110113908Sdes 111113908Sdes /* flush */ 112113908Sdes packet_write_wait(); 113113908Sdes 114113908Sdes /* Compute our exchange value in parallel with the client */ 115113908Sdes dh_gen_key(dh, kex->we_need * 8); 116113908Sdes 117113908Sdes debug("expecting SSH2_MSG_KEX_DH_GEX_INIT"); 118113908Sdes packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT); 119113908Sdes 120113908Sdes /* key, cert */ 121113908Sdes if ((dh_client_pub = BN_new()) == NULL) 122113908Sdes fatal("dh_client_pub == NULL"); 123113908Sdes packet_get_bignum2(dh_client_pub); 124113908Sdes packet_check_eom(); 125113908Sdes 126113908Sdes#ifdef DEBUG_KEXDH 127113908Sdes fprintf(stderr, "dh_client_pub= "); 128113908Sdes BN_print_fp(stderr, dh_client_pub); 129113908Sdes fprintf(stderr, "\n"); 130113908Sdes debug("bits %d", BN_num_bits(dh_client_pub)); 131113908Sdes#endif 132113908Sdes 133113908Sdes#ifdef DEBUG_KEXDH 134113908Sdes DHparams_print_fp(stderr, dh); 135113908Sdes fprintf(stderr, "pub= "); 136113908Sdes BN_print_fp(stderr, dh->pub_key); 137113908Sdes fprintf(stderr, "\n"); 138113908Sdes#endif 139113908Sdes if (!dh_pub_is_valid(dh, dh_client_pub)) 140113908Sdes packet_disconnect("bad client public DH value"); 141113908Sdes 142113908Sdes klen = DH_size(dh); 143113908Sdes kbuf = xmalloc(klen); 144164146Sdes if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0) 145164146Sdes fatal("DH_compute_key: failed"); 146113908Sdes#ifdef DEBUG_KEXDH 147113908Sdes dump_digest("shared secret", kbuf, kout); 148113908Sdes#endif 149113908Sdes if ((shared_secret = BN_new()) == NULL) 150113908Sdes fatal("kexgex_server: BN_new failed"); 151164146Sdes if (BN_bin2bn(kbuf, kout, shared_secret) == NULL) 152164146Sdes fatal("kexgex_server: BN_bin2bn failed"); 153264377Sdes explicit_bzero(kbuf, klen); 154255767Sdes free(kbuf); 155113908Sdes 156204917Sdes key_to_blob(server_host_public, &server_host_key_blob, &sbloblen); 157113908Sdes 158113908Sdes if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) 159192595Sdes omin = min = omax = max = -1; 160113908Sdes 161157016Sdes /* calc H */ 162157016Sdes kexgex_hash( 163262566Sdes kex->hash_alg, 164113908Sdes kex->client_version_string, 165113908Sdes kex->server_version_string, 166113908Sdes buffer_ptr(&kex->peer), buffer_len(&kex->peer), 167113908Sdes buffer_ptr(&kex->my), buffer_len(&kex->my), 168113908Sdes server_host_key_blob, sbloblen, 169192595Sdes omin, onbits, omax, 170113908Sdes dh->p, dh->g, 171113908Sdes dh_client_pub, 172113908Sdes dh->pub_key, 173157016Sdes shared_secret, 174157016Sdes &hash, &hashlen 175113908Sdes ); 176113908Sdes BN_clear_free(dh_client_pub); 177113908Sdes 178113908Sdes /* save session id := H */ 179113908Sdes if (kex->session_id == NULL) { 180157016Sdes kex->session_id_len = hashlen; 181113908Sdes kex->session_id = xmalloc(kex->session_id_len); 182113908Sdes memcpy(kex->session_id, hash, kex->session_id_len); 183113908Sdes } 184113908Sdes 185113908Sdes /* sign H */ 186255767Sdes kex->sign(server_host_private, server_host_public, &signature, &slen, 187255767Sdes hash, hashlen); 188113908Sdes 189113908Sdes /* destroy_sensitive_data(); */ 190113908Sdes 191113908Sdes /* send server hostkey, DH pubkey 'f' and singed H */ 192113908Sdes debug("SSH2_MSG_KEX_DH_GEX_REPLY sent"); 193113908Sdes packet_start(SSH2_MSG_KEX_DH_GEX_REPLY); 194113908Sdes packet_put_string(server_host_key_blob, sbloblen); 195113908Sdes packet_put_bignum2(dh->pub_key); /* f */ 196113908Sdes packet_put_string(signature, slen); 197113908Sdes packet_send(); 198113908Sdes 199255767Sdes free(signature); 200255767Sdes free(server_host_key_blob); 201113908Sdes /* have keys, free DH */ 202113908Sdes DH_free(dh); 203113908Sdes 204262566Sdes kex_derive_keys_bn(kex, hash, hashlen, shared_secret); 205113908Sdes BN_clear_free(shared_secret); 206113908Sdes 207113908Sdes kex_finish(kex); 208113908Sdes} 209