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