1295367Sdes/* $OpenBSD: kexdh.c,v 1.25 2015/01/19 20:16:15 markus Exp $ */ 276259Sgreen/* 376259Sgreen * Copyright (c) 2001 Markus Friedl. All rights reserved. 476259Sgreen * 576259Sgreen * Redistribution and use in source and binary forms, with or without 676259Sgreen * modification, are permitted provided that the following conditions 776259Sgreen * are met: 876259Sgreen * 1. Redistributions of source code must retain the above copyright 976259Sgreen * notice, this list of conditions and the following disclaimer. 1076259Sgreen * 2. Redistributions in binary form must reproduce the above copyright 1176259Sgreen * notice, this list of conditions and the following disclaimer in the 1276259Sgreen * documentation and/or other materials provided with the distribution. 1376259Sgreen * 1476259Sgreen * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1576259Sgreen * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1676259Sgreen * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1776259Sgreen * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1876259Sgreen * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1976259Sgreen * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2076259Sgreen * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2176259Sgreen * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2276259Sgreen * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2376259Sgreen * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2476259Sgreen */ 2576259Sgreen 2676259Sgreen#include "includes.h" 2776259Sgreen 28295367Sdes#ifdef WITH_OPENSSL 29295367Sdes 30162852Sdes#include <sys/types.h> 31162852Sdes 32162852Sdes#include <signal.h> 33162852Sdes 34113908Sdes#include <openssl/evp.h> 3576259Sgreen 36113908Sdes#include "ssh2.h" 37295367Sdes#include "sshkey.h" 38162852Sdes#include "cipher.h" 3976259Sgreen#include "kex.h" 40295367Sdes#include "ssherr.h" 41295367Sdes#include "sshbuf.h" 42262566Sdes#include "digest.h" 4376259Sgreen 44295367Sdesint 4576259Sgreenkex_dh_hash( 46295367Sdes const char *client_version_string, 47295367Sdes const char *server_version_string, 48295367Sdes const u_char *ckexinit, size_t ckexinitlen, 49295367Sdes const u_char *skexinit, size_t skexinitlen, 50295367Sdes const u_char *serverhostkeyblob, size_t sbloblen, 51295367Sdes const BIGNUM *client_dh_pub, 52295367Sdes const BIGNUM *server_dh_pub, 53295367Sdes const BIGNUM *shared_secret, 54295367Sdes u_char *hash, size_t *hashlen) 5576259Sgreen{ 56295367Sdes struct sshbuf *b; 57295367Sdes int r; 5876259Sgreen 59295367Sdes if (*hashlen < ssh_digest_bytes(SSH_DIGEST_SHA1)) 60295367Sdes return SSH_ERR_INVALID_ARGUMENT; 61295367Sdes if ((b = sshbuf_new()) == NULL) 62295367Sdes return SSH_ERR_ALLOC_FAIL; 63295367Sdes if ((r = sshbuf_put_cstring(b, client_version_string)) != 0 || 64295367Sdes (r = sshbuf_put_cstring(b, server_version_string)) != 0 || 65295367Sdes /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */ 66295367Sdes (r = sshbuf_put_u32(b, ckexinitlen+1)) != 0 || 67295367Sdes (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 || 68295367Sdes (r = sshbuf_put(b, ckexinit, ckexinitlen)) != 0 || 69295367Sdes (r = sshbuf_put_u32(b, skexinitlen+1)) != 0 || 70295367Sdes (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 || 71295367Sdes (r = sshbuf_put(b, skexinit, skexinitlen)) != 0 || 72295367Sdes (r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) != 0 || 73295367Sdes (r = sshbuf_put_bignum2(b, client_dh_pub)) != 0 || 74295367Sdes (r = sshbuf_put_bignum2(b, server_dh_pub)) != 0 || 75295367Sdes (r = sshbuf_put_bignum2(b, shared_secret)) != 0) { 76295367Sdes sshbuf_free(b); 77295367Sdes return r; 78295367Sdes } 7976259Sgreen#ifdef DEBUG_KEX 80295367Sdes sshbuf_dump(b, stderr); 8176259Sgreen#endif 82295367Sdes if (ssh_digest_buffer(SSH_DIGEST_SHA1, b, hash, *hashlen) != 0) { 83295367Sdes sshbuf_free(b); 84295367Sdes return SSH_ERR_LIBCRYPTO_ERROR; 85295367Sdes } 86295367Sdes sshbuf_free(b); 87295367Sdes *hashlen = ssh_digest_bytes(SSH_DIGEST_SHA1); 8876259Sgreen#ifdef DEBUG_KEX 89295367Sdes dump_digest("hash", hash, *hashlen); 9076259Sgreen#endif 91295367Sdes return 0; 9276259Sgreen} 93295367Sdes#endif /* WITH_OPENSSL */ 94