kexdhs.c revision 204917
1204917Sdes/* $OpenBSD: kexdhs.c,v 1.11 2010/02/26 20:29:54 djm Exp $ */ 2113908Sdes/* 3113908Sdes * Copyright (c) 2001 Markus Friedl. All rights reserved. 4113908Sdes * 5113908Sdes * Redistribution and use in source and binary forms, with or without 6113908Sdes * modification, are permitted provided that the following conditions 7113908Sdes * are met: 8113908Sdes * 1. Redistributions of source code must retain the above copyright 9113908Sdes * notice, this list of conditions and the following disclaimer. 10113908Sdes * 2. Redistributions in binary form must reproduce the above copyright 11113908Sdes * notice, this list of conditions and the following disclaimer in the 12113908Sdes * documentation and/or other materials provided with the distribution. 13113908Sdes * 14113908Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15113908Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16113908Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17113908Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18113908Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19113908Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20113908Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21113908Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22113908Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23113908Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24113908Sdes */ 25113908Sdes 26113908Sdes#include "includes.h" 27113908Sdes 28162852Sdes#include <sys/types.h> 29162852Sdes 30162852Sdes#include <stdarg.h> 31162852Sdes#include <string.h> 32162852Sdes#include <signal.h> 33162852Sdes 34113908Sdes#include "xmalloc.h" 35162852Sdes#include "buffer.h" 36113908Sdes#include "key.h" 37162852Sdes#include "cipher.h" 38113908Sdes#include "kex.h" 39113908Sdes#include "log.h" 40113908Sdes#include "packet.h" 41113908Sdes#include "dh.h" 42113908Sdes#include "ssh2.h" 43162852Sdes#ifdef GSSAPI 44162852Sdes#include "ssh-gss.h" 45162852Sdes#endif 46113908Sdes#include "monitor_wrap.h" 47113908Sdes 48113908Sdesvoid 49113908Sdeskexdh_server(Kex *kex) 50113908Sdes{ 51113908Sdes BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; 52113908Sdes DH *dh; 53204917Sdes Key *server_host_public, *server_host_private; 54113908Sdes u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; 55164146Sdes u_int sbloblen, klen, hashlen, slen; 56164146Sdes int kout; 57113908Sdes 58113908Sdes /* generate server DH public key */ 59137015Sdes switch (kex->kex_type) { 60137015Sdes case KEX_DH_GRP1_SHA1: 61137015Sdes dh = dh_new_group1(); 62137015Sdes break; 63137015Sdes case KEX_DH_GRP14_SHA1: 64137015Sdes dh = dh_new_group14(); 65137015Sdes break; 66137015Sdes default: 67137015Sdes fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type); 68137015Sdes } 69113908Sdes dh_gen_key(dh, kex->we_need * 8); 70113908Sdes 71113908Sdes debug("expecting SSH2_MSG_KEXDH_INIT"); 72113908Sdes packet_read_expect(SSH2_MSG_KEXDH_INIT); 73113908Sdes 74204917Sdes if (kex->load_host_public_key == NULL || 75204917Sdes kex->load_host_private_key == NULL) 76113908Sdes fatal("Cannot load hostkey"); 77204917Sdes server_host_public = kex->load_host_public_key(kex->hostkey_type); 78204917Sdes if (server_host_public == NULL) 79113908Sdes fatal("Unsupported hostkey type %d", kex->hostkey_type); 80204917Sdes server_host_private = kex->load_host_private_key(kex->hostkey_type); 81204917Sdes if (server_host_private == NULL) 82204917Sdes fatal("Missing private key for hostkey type %d", 83204917Sdes kex->hostkey_type); 84113908Sdes 85113908Sdes /* key, cert */ 86113908Sdes if ((dh_client_pub = BN_new()) == NULL) 87113908Sdes fatal("dh_client_pub == NULL"); 88113908Sdes packet_get_bignum2(dh_client_pub); 89113908Sdes packet_check_eom(); 90113908Sdes 91113908Sdes#ifdef DEBUG_KEXDH 92113908Sdes fprintf(stderr, "dh_client_pub= "); 93113908Sdes BN_print_fp(stderr, dh_client_pub); 94113908Sdes fprintf(stderr, "\n"); 95113908Sdes debug("bits %d", BN_num_bits(dh_client_pub)); 96113908Sdes#endif 97113908Sdes 98113908Sdes#ifdef DEBUG_KEXDH 99113908Sdes DHparams_print_fp(stderr, dh); 100113908Sdes fprintf(stderr, "pub= "); 101113908Sdes BN_print_fp(stderr, dh->pub_key); 102113908Sdes fprintf(stderr, "\n"); 103113908Sdes#endif 104113908Sdes if (!dh_pub_is_valid(dh, dh_client_pub)) 105113908Sdes packet_disconnect("bad client public DH value"); 106113908Sdes 107113908Sdes klen = DH_size(dh); 108113908Sdes kbuf = xmalloc(klen); 109164146Sdes if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0) 110164146Sdes fatal("DH_compute_key: failed"); 111113908Sdes#ifdef DEBUG_KEXDH 112113908Sdes dump_digest("shared secret", kbuf, kout); 113113908Sdes#endif 114113908Sdes if ((shared_secret = BN_new()) == NULL) 115113908Sdes fatal("kexdh_server: BN_new failed"); 116164146Sdes if (BN_bin2bn(kbuf, kout, shared_secret) == NULL) 117164146Sdes fatal("kexdh_server: BN_bin2bn failed"); 118113908Sdes memset(kbuf, 0, klen); 119113908Sdes xfree(kbuf); 120113908Sdes 121204917Sdes key_to_blob(server_host_public, &server_host_key_blob, &sbloblen); 122113908Sdes 123113908Sdes /* calc H */ 124157016Sdes kex_dh_hash( 125113908Sdes kex->client_version_string, 126113908Sdes kex->server_version_string, 127113908Sdes buffer_ptr(&kex->peer), buffer_len(&kex->peer), 128113908Sdes buffer_ptr(&kex->my), buffer_len(&kex->my), 129113908Sdes server_host_key_blob, sbloblen, 130113908Sdes dh_client_pub, 131113908Sdes dh->pub_key, 132157016Sdes shared_secret, 133157016Sdes &hash, &hashlen 134113908Sdes ); 135113908Sdes BN_clear_free(dh_client_pub); 136113908Sdes 137113908Sdes /* save session id := H */ 138113908Sdes if (kex->session_id == NULL) { 139157016Sdes kex->session_id_len = hashlen; 140113908Sdes kex->session_id = xmalloc(kex->session_id_len); 141113908Sdes memcpy(kex->session_id, hash, kex->session_id_len); 142113908Sdes } 143113908Sdes 144113908Sdes /* sign H */ 145204917Sdes if (PRIVSEP(key_sign(server_host_private, &signature, &slen, hash, 146197679Sdes hashlen)) < 0) 147197679Sdes fatal("kexdh_server: key_sign failed"); 148113908Sdes 149113908Sdes /* destroy_sensitive_data(); */ 150113908Sdes 151113908Sdes /* send server hostkey, DH pubkey 'f' and singed H */ 152113908Sdes packet_start(SSH2_MSG_KEXDH_REPLY); 153113908Sdes packet_put_string(server_host_key_blob, sbloblen); 154113908Sdes packet_put_bignum2(dh->pub_key); /* f */ 155113908Sdes packet_put_string(signature, slen); 156113908Sdes packet_send(); 157113908Sdes 158113908Sdes xfree(signature); 159113908Sdes xfree(server_host_key_blob); 160113908Sdes /* have keys, free DH */ 161113908Sdes DH_free(dh); 162113908Sdes 163157016Sdes kex_derive_keys(kex, hash, hashlen, shared_secret); 164113908Sdes BN_clear_free(shared_secret); 165113908Sdes kex_finish(kex); 166113908Sdes} 167