kexdhs.c revision 221420
1221420Sdes/* $OpenBSD: kexdhs.c,v 1.12 2010/11/10 01:33:07 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 34221420Sdes#include <openssl/dh.h> 35221420Sdes 36113908Sdes#include "xmalloc.h" 37162852Sdes#include "buffer.h" 38113908Sdes#include "key.h" 39162852Sdes#include "cipher.h" 40113908Sdes#include "kex.h" 41113908Sdes#include "log.h" 42113908Sdes#include "packet.h" 43113908Sdes#include "dh.h" 44113908Sdes#include "ssh2.h" 45162852Sdes#ifdef GSSAPI 46162852Sdes#include "ssh-gss.h" 47162852Sdes#endif 48113908Sdes#include "monitor_wrap.h" 49113908Sdes 50113908Sdesvoid 51113908Sdeskexdh_server(Kex *kex) 52113908Sdes{ 53113908Sdes BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; 54113908Sdes DH *dh; 55204917Sdes Key *server_host_public, *server_host_private; 56113908Sdes u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; 57164146Sdes u_int sbloblen, klen, hashlen, slen; 58164146Sdes int kout; 59113908Sdes 60113908Sdes /* generate server DH public key */ 61137015Sdes switch (kex->kex_type) { 62137015Sdes case KEX_DH_GRP1_SHA1: 63137015Sdes dh = dh_new_group1(); 64137015Sdes break; 65137015Sdes case KEX_DH_GRP14_SHA1: 66137015Sdes dh = dh_new_group14(); 67137015Sdes break; 68137015Sdes default: 69137015Sdes fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type); 70137015Sdes } 71113908Sdes dh_gen_key(dh, kex->we_need * 8); 72113908Sdes 73113908Sdes debug("expecting SSH2_MSG_KEXDH_INIT"); 74113908Sdes packet_read_expect(SSH2_MSG_KEXDH_INIT); 75113908Sdes 76204917Sdes if (kex->load_host_public_key == NULL || 77204917Sdes kex->load_host_private_key == NULL) 78113908Sdes fatal("Cannot load hostkey"); 79204917Sdes server_host_public = kex->load_host_public_key(kex->hostkey_type); 80204917Sdes if (server_host_public == NULL) 81113908Sdes fatal("Unsupported hostkey type %d", kex->hostkey_type); 82204917Sdes server_host_private = kex->load_host_private_key(kex->hostkey_type); 83204917Sdes if (server_host_private == NULL) 84204917Sdes fatal("Missing private key for hostkey type %d", 85204917Sdes kex->hostkey_type); 86113908Sdes 87113908Sdes /* key, cert */ 88113908Sdes if ((dh_client_pub = BN_new()) == NULL) 89113908Sdes fatal("dh_client_pub == NULL"); 90113908Sdes packet_get_bignum2(dh_client_pub); 91113908Sdes packet_check_eom(); 92113908Sdes 93113908Sdes#ifdef DEBUG_KEXDH 94113908Sdes fprintf(stderr, "dh_client_pub= "); 95113908Sdes BN_print_fp(stderr, dh_client_pub); 96113908Sdes fprintf(stderr, "\n"); 97113908Sdes debug("bits %d", BN_num_bits(dh_client_pub)); 98113908Sdes#endif 99113908Sdes 100113908Sdes#ifdef DEBUG_KEXDH 101113908Sdes DHparams_print_fp(stderr, dh); 102113908Sdes fprintf(stderr, "pub= "); 103113908Sdes BN_print_fp(stderr, dh->pub_key); 104113908Sdes fprintf(stderr, "\n"); 105113908Sdes#endif 106113908Sdes if (!dh_pub_is_valid(dh, dh_client_pub)) 107113908Sdes packet_disconnect("bad client public DH value"); 108113908Sdes 109113908Sdes klen = DH_size(dh); 110113908Sdes kbuf = xmalloc(klen); 111164146Sdes if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0) 112164146Sdes fatal("DH_compute_key: failed"); 113113908Sdes#ifdef DEBUG_KEXDH 114113908Sdes dump_digest("shared secret", kbuf, kout); 115113908Sdes#endif 116113908Sdes if ((shared_secret = BN_new()) == NULL) 117113908Sdes fatal("kexdh_server: BN_new failed"); 118164146Sdes if (BN_bin2bn(kbuf, kout, shared_secret) == NULL) 119164146Sdes fatal("kexdh_server: BN_bin2bn failed"); 120113908Sdes memset(kbuf, 0, klen); 121113908Sdes xfree(kbuf); 122113908Sdes 123204917Sdes key_to_blob(server_host_public, &server_host_key_blob, &sbloblen); 124113908Sdes 125113908Sdes /* calc H */ 126157016Sdes kex_dh_hash( 127113908Sdes kex->client_version_string, 128113908Sdes kex->server_version_string, 129113908Sdes buffer_ptr(&kex->peer), buffer_len(&kex->peer), 130113908Sdes buffer_ptr(&kex->my), buffer_len(&kex->my), 131113908Sdes server_host_key_blob, sbloblen, 132113908Sdes dh_client_pub, 133113908Sdes dh->pub_key, 134157016Sdes shared_secret, 135157016Sdes &hash, &hashlen 136113908Sdes ); 137113908Sdes BN_clear_free(dh_client_pub); 138113908Sdes 139113908Sdes /* save session id := H */ 140113908Sdes if (kex->session_id == NULL) { 141157016Sdes kex->session_id_len = hashlen; 142113908Sdes kex->session_id = xmalloc(kex->session_id_len); 143113908Sdes memcpy(kex->session_id, hash, kex->session_id_len); 144113908Sdes } 145113908Sdes 146113908Sdes /* sign H */ 147204917Sdes if (PRIVSEP(key_sign(server_host_private, &signature, &slen, hash, 148197679Sdes hashlen)) < 0) 149197679Sdes fatal("kexdh_server: key_sign failed"); 150113908Sdes 151113908Sdes /* destroy_sensitive_data(); */ 152113908Sdes 153113908Sdes /* send server hostkey, DH pubkey 'f' and singed H */ 154113908Sdes packet_start(SSH2_MSG_KEXDH_REPLY); 155113908Sdes packet_put_string(server_host_key_blob, sbloblen); 156113908Sdes packet_put_bignum2(dh->pub_key); /* f */ 157113908Sdes packet_put_string(signature, slen); 158113908Sdes packet_send(); 159113908Sdes 160113908Sdes xfree(signature); 161113908Sdes xfree(server_host_key_blob); 162113908Sdes /* have keys, free DH */ 163113908Sdes DH_free(dh); 164113908Sdes 165157016Sdes kex_derive_keys(kex, hash, hashlen, shared_secret); 166113908Sdes BN_clear_free(shared_secret); 167113908Sdes kex_finish(kex); 168113908Sdes} 169