1255767Sdes/* $OpenBSD: kexdhc.c,v 1.13 2013/05/17 00:13:13 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 30221420Sdes#include <openssl/dh.h> 31221420Sdes 32162852Sdes#include <stdarg.h> 33162852Sdes#include <stdio.h> 34162852Sdes#include <string.h> 35162852Sdes#include <signal.h> 36162852Sdes 37113908Sdes#include "xmalloc.h" 38162852Sdes#include "buffer.h" 39113908Sdes#include "key.h" 40162852Sdes#include "cipher.h" 41113908Sdes#include "kex.h" 42113908Sdes#include "log.h" 43113908Sdes#include "packet.h" 44113908Sdes#include "dh.h" 45113908Sdes#include "ssh2.h" 46113908Sdes 47113908Sdesvoid 48113908Sdeskexdh_client(Kex *kex) 49113908Sdes{ 50113908Sdes BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; 51113908Sdes DH *dh; 52113908Sdes Key *server_host_key; 53113908Sdes u_char *server_host_key_blob = NULL, *signature = NULL; 54113908Sdes u_char *kbuf, *hash; 55164146Sdes u_int klen, slen, sbloblen, hashlen; 56164146Sdes int kout; 57113908Sdes 58113908Sdes /* generate and send 'e', client 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 packet_start(SSH2_MSG_KEXDH_INIT); 71113908Sdes packet_put_bignum2(dh->pub_key); 72113908Sdes packet_send(); 73113908Sdes 74113908Sdes debug("sending SSH2_MSG_KEXDH_INIT"); 75113908Sdes#ifdef DEBUG_KEXDH 76113908Sdes DHparams_print_fp(stderr, dh); 77113908Sdes fprintf(stderr, "pub= "); 78113908Sdes BN_print_fp(stderr, dh->pub_key); 79113908Sdes fprintf(stderr, "\n"); 80113908Sdes#endif 81113908Sdes 82113908Sdes debug("expecting SSH2_MSG_KEXDH_REPLY"); 83113908Sdes packet_read_expect(SSH2_MSG_KEXDH_REPLY); 84113908Sdes 85113908Sdes /* key, cert */ 86113908Sdes server_host_key_blob = packet_get_string(&sbloblen); 87113908Sdes server_host_key = key_from_blob(server_host_key_blob, sbloblen); 88113908Sdes if (server_host_key == NULL) 89113908Sdes fatal("cannot decode server_host_key_blob"); 90113908Sdes if (server_host_key->type != kex->hostkey_type) 91113908Sdes fatal("type mismatch for decoded server_host_key_blob"); 92113908Sdes if (kex->verify_host_key == NULL) 93113908Sdes fatal("cannot verify server_host_key"); 94113908Sdes if (kex->verify_host_key(server_host_key) == -1) 95113908Sdes fatal("server_host_key verification failed"); 96113908Sdes 97162852Sdes /* DH parameter f, server public DH key */ 98113908Sdes if ((dh_server_pub = BN_new()) == NULL) 99113908Sdes fatal("dh_server_pub == NULL"); 100113908Sdes packet_get_bignum2(dh_server_pub); 101113908Sdes 102113908Sdes#ifdef DEBUG_KEXDH 103113908Sdes fprintf(stderr, "dh_server_pub= "); 104113908Sdes BN_print_fp(stderr, dh_server_pub); 105113908Sdes fprintf(stderr, "\n"); 106113908Sdes debug("bits %d", BN_num_bits(dh_server_pub)); 107113908Sdes#endif 108113908Sdes 109113908Sdes /* signed H */ 110113908Sdes signature = packet_get_string(&slen); 111113908Sdes packet_check_eom(); 112113908Sdes 113113908Sdes if (!dh_pub_is_valid(dh, dh_server_pub)) 114113908Sdes packet_disconnect("bad server public DH value"); 115113908Sdes 116113908Sdes klen = DH_size(dh); 117113908Sdes kbuf = xmalloc(klen); 118164146Sdes if ((kout = DH_compute_key(kbuf, dh_server_pub, dh)) < 0) 119164146Sdes fatal("DH_compute_key: failed"); 120113908Sdes#ifdef DEBUG_KEXDH 121113908Sdes dump_digest("shared secret", kbuf, kout); 122113908Sdes#endif 123113908Sdes if ((shared_secret = BN_new()) == NULL) 124113908Sdes fatal("kexdh_client: BN_new failed"); 125164146Sdes if (BN_bin2bn(kbuf, kout, shared_secret) == NULL) 126164146Sdes fatal("kexdh_client: BN_bin2bn failed"); 127113908Sdes memset(kbuf, 0, klen); 128255767Sdes free(kbuf); 129113908Sdes 130113908Sdes /* calc and verify H */ 131157016Sdes kex_dh_hash( 132113908Sdes kex->client_version_string, 133113908Sdes kex->server_version_string, 134113908Sdes buffer_ptr(&kex->my), buffer_len(&kex->my), 135113908Sdes buffer_ptr(&kex->peer), buffer_len(&kex->peer), 136113908Sdes server_host_key_blob, sbloblen, 137113908Sdes dh->pub_key, 138113908Sdes dh_server_pub, 139157016Sdes shared_secret, 140157016Sdes &hash, &hashlen 141113908Sdes ); 142255767Sdes free(server_host_key_blob); 143113908Sdes BN_clear_free(dh_server_pub); 144113908Sdes DH_free(dh); 145113908Sdes 146157016Sdes if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1) 147113908Sdes fatal("key_verify failed for server_host_key"); 148113908Sdes key_free(server_host_key); 149255767Sdes free(signature); 150113908Sdes 151113908Sdes /* save session id */ 152113908Sdes if (kex->session_id == NULL) { 153157016Sdes kex->session_id_len = hashlen; 154113908Sdes kex->session_id = xmalloc(kex->session_id_len); 155113908Sdes memcpy(kex->session_id, hash, kex->session_id_len); 156113908Sdes } 157113908Sdes 158157016Sdes kex_derive_keys(kex, hash, hashlen, shared_secret); 159113908Sdes BN_clear_free(shared_secret); 160113908Sdes kex_finish(kex); 161113908Sdes} 162