1294332Sdes/* $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
29294332Sdes#ifdef WITH_OPENSSL
30294332Sdes
31162852Sdes#include <sys/types.h>
32162852Sdes
33113908Sdes#include <openssl/evp.h>
34162852Sdes#include <signal.h>
3576259Sgreen
36294332Sdes#include "sshkey.h"
37162852Sdes#include "cipher.h"
3876259Sgreen#include "kex.h"
3976259Sgreen#include "ssh2.h"
40294332Sdes#include "ssherr.h"
41294332Sdes#include "sshbuf.h"
42261320Sdes#include "digest.h"
4376259Sgreen
44294332Sdesint
4576259Sgreenkexgex_hash(
46261320Sdes    int hash_alg,
47294332Sdes    const char *client_version_string,
48294332Sdes    const char *server_version_string,
49294332Sdes    const u_char *ckexinit, size_t ckexinitlen,
50294332Sdes    const u_char *skexinit, size_t skexinitlen,
51294332Sdes    const u_char *serverhostkeyblob, size_t sbloblen,
52294332Sdes    int min, int wantbits, int max,
53294332Sdes    const BIGNUM *prime,
54294332Sdes    const BIGNUM *gen,
55294332Sdes    const BIGNUM *client_dh_pub,
56294332Sdes    const BIGNUM *server_dh_pub,
57294332Sdes    const BIGNUM *shared_secret,
58294332Sdes    u_char *hash, size_t *hashlen)
5976259Sgreen{
60294332Sdes	struct sshbuf *b;
61294332Sdes	int r;
6276259Sgreen
63294332Sdes	if (*hashlen < ssh_digest_bytes(SSH_DIGEST_SHA1))
64294332Sdes		return SSH_ERR_INVALID_ARGUMENT;
65294332Sdes	if ((b = sshbuf_new()) == NULL)
66294332Sdes		return SSH_ERR_ALLOC_FAIL;
67294332Sdes	if ((r = sshbuf_put_cstring(b, client_version_string)) != 0 ||
68294332Sdes	    (r = sshbuf_put_cstring(b, server_version_string)) != 0 ||
69294332Sdes	    /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
70294332Sdes	    (r = sshbuf_put_u32(b, ckexinitlen+1)) != 0 ||
71294332Sdes	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
72294332Sdes	    (r = sshbuf_put(b, ckexinit, ckexinitlen)) != 0 ||
73294332Sdes	    (r = sshbuf_put_u32(b, skexinitlen+1)) != 0 ||
74294332Sdes	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
75294332Sdes	    (r = sshbuf_put(b, skexinit, skexinitlen)) != 0 ||
76294332Sdes	    (r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) != 0 ||
77294332Sdes	    (min != -1 && (r = sshbuf_put_u32(b, min)) != 0) ||
78294332Sdes	    (r = sshbuf_put_u32(b, wantbits)) != 0 ||
79294332Sdes	    (max != -1 && (r = sshbuf_put_u32(b, max)) != 0) ||
80294332Sdes	    (r = sshbuf_put_bignum2(b, prime)) != 0 ||
81294332Sdes	    (r = sshbuf_put_bignum2(b, gen)) != 0 ||
82294332Sdes	    (r = sshbuf_put_bignum2(b, client_dh_pub)) != 0 ||
83294332Sdes	    (r = sshbuf_put_bignum2(b, server_dh_pub)) != 0 ||
84294332Sdes	    (r = sshbuf_put_bignum2(b, shared_secret)) != 0) {
85294332Sdes		sshbuf_free(b);
86294332Sdes		return r;
8776259Sgreen	}
8876259Sgreen#ifdef DEBUG_KEXDH
89294332Sdes	sshbuf_dump(b, stderr);
9076259Sgreen#endif
91294332Sdes	if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
92294332Sdes		sshbuf_free(b);
93294332Sdes		return SSH_ERR_LIBCRYPTO_ERROR;
94294332Sdes	}
95294332Sdes	sshbuf_free(b);
96294332Sdes	*hashlen = ssh_digest_bytes(hash_alg);
97294332Sdes#ifdef DEBUG_KEXDH
98294332Sdes	dump_digest("hash", hash, *hashlen);
99261320Sdes#endif
100294332Sdes	return 0;
10176259Sgreen}
102294332Sdes#endif /* WITH_OPENSSL */
103