2014-02-04 08:20:14 +08:00
|
|
|
/* $OpenBSD: kexdhs.c,v 1.18 2014/02/02 03:44:31 djm Exp $ */
|
2003-02-24 09:03:03 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
2006-08-05 10:39:39 +08:00
|
|
|
#include <sys/types.h>
|
2006-09-01 13:38:36 +08:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
2006-07-24 12:13:33 +08:00
|
|
|
#include <string.h>
|
2006-08-05 10:39:39 +08:00
|
|
|
#include <signal.h>
|
2006-07-24 12:13:33 +08:00
|
|
|
|
2010-11-20 12:15:49 +08:00
|
|
|
#include <openssl/dh.h>
|
|
|
|
|
2003-02-24 09:03:03 +08:00
|
|
|
#include "xmalloc.h"
|
2006-08-05 10:39:39 +08:00
|
|
|
#include "buffer.h"
|
2003-02-24 09:03:03 +08:00
|
|
|
#include "key.h"
|
2006-08-05 10:39:39 +08:00
|
|
|
#include "cipher.h"
|
2003-02-24 09:03:03 +08:00
|
|
|
#include "kex.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "dh.h"
|
|
|
|
#include "ssh2.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
kexdh_server(Kex *kex)
|
|
|
|
{
|
|
|
|
BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
|
|
|
|
DH *dh;
|
2010-02-27 04:55:05 +08:00
|
|
|
Key *server_host_public, *server_host_private;
|
2003-02-24 09:03:03 +08:00
|
|
|
u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
|
2006-11-05 02:32:02 +08:00
|
|
|
u_int sbloblen, klen, hashlen, slen;
|
|
|
|
int kout;
|
2003-02-24 09:03:03 +08:00
|
|
|
|
|
|
|
/* generate server DH public key */
|
2004-06-15 08:30:09 +08:00
|
|
|
switch (kex->kex_type) {
|
|
|
|
case KEX_DH_GRP1_SHA1:
|
|
|
|
dh = dh_new_group1();
|
|
|
|
break;
|
|
|
|
case KEX_DH_GRP14_SHA1:
|
|
|
|
dh = dh_new_group14();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
|
|
|
|
}
|
2003-02-24 09:03:03 +08:00
|
|
|
dh_gen_key(dh, kex->we_need * 8);
|
|
|
|
|
|
|
|
debug("expecting SSH2_MSG_KEXDH_INIT");
|
|
|
|
packet_read_expect(SSH2_MSG_KEXDH_INIT);
|
|
|
|
|
2010-02-27 04:55:05 +08:00
|
|
|
if (kex->load_host_public_key == NULL ||
|
|
|
|
kex->load_host_private_key == NULL)
|
2003-02-24 09:03:03 +08:00
|
|
|
fatal("Cannot load hostkey");
|
2010-02-27 04:55:05 +08:00
|
|
|
server_host_public = kex->load_host_public_key(kex->hostkey_type);
|
|
|
|
if (server_host_public == NULL)
|
2003-02-24 09:03:03 +08:00
|
|
|
fatal("Unsupported hostkey type %d", kex->hostkey_type);
|
2010-02-27 04:55:05 +08:00
|
|
|
server_host_private = kex->load_host_private_key(kex->hostkey_type);
|
2003-02-24 09:03:03 +08:00
|
|
|
|
|
|
|
/* key, cert */
|
|
|
|
if ((dh_client_pub = BN_new()) == NULL)
|
|
|
|
fatal("dh_client_pub == NULL");
|
|
|
|
packet_get_bignum2(dh_client_pub);
|
|
|
|
packet_check_eom();
|
|
|
|
|
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
fprintf(stderr, "dh_client_pub= ");
|
|
|
|
BN_print_fp(stderr, dh_client_pub);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
debug("bits %d", BN_num_bits(dh_client_pub));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
DHparams_print_fp(stderr, dh);
|
|
|
|
fprintf(stderr, "pub= ");
|
|
|
|
BN_print_fp(stderr, dh->pub_key);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
#endif
|
|
|
|
if (!dh_pub_is_valid(dh, dh_client_pub))
|
|
|
|
packet_disconnect("bad client public DH value");
|
|
|
|
|
|
|
|
klen = DH_size(dh);
|
|
|
|
kbuf = xmalloc(klen);
|
2006-11-05 02:32:02 +08:00
|
|
|
if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
|
|
|
|
fatal("DH_compute_key: failed");
|
2003-02-24 09:03:03 +08:00
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
dump_digest("shared secret", kbuf, kout);
|
|
|
|
#endif
|
|
|
|
if ((shared_secret = BN_new()) == NULL)
|
|
|
|
fatal("kexdh_server: BN_new failed");
|
2006-11-07 20:14:41 +08:00
|
|
|
if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
|
|
|
|
fatal("kexdh_server: BN_bin2bn failed");
|
2014-02-04 08:20:14 +08:00
|
|
|
explicit_bzero(kbuf, klen);
|
2013-06-02 05:31:17 +08:00
|
|
|
free(kbuf);
|
2003-02-24 09:03:03 +08:00
|
|
|
|
2010-02-27 04:55:05 +08:00
|
|
|
key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
|
2003-02-24 09:03:03 +08:00
|
|
|
|
|
|
|
/* calc H */
|
2005-11-05 12:19:35 +08:00
|
|
|
kex_dh_hash(
|
2003-02-24 09:03:03 +08:00
|
|
|
kex->client_version_string,
|
|
|
|
kex->server_version_string,
|
|
|
|
buffer_ptr(&kex->peer), buffer_len(&kex->peer),
|
|
|
|
buffer_ptr(&kex->my), buffer_len(&kex->my),
|
|
|
|
server_host_key_blob, sbloblen,
|
|
|
|
dh_client_pub,
|
|
|
|
dh->pub_key,
|
2005-11-05 12:19:35 +08:00
|
|
|
shared_secret,
|
|
|
|
&hash, &hashlen
|
2003-02-24 09:03:03 +08:00
|
|
|
);
|
|
|
|
BN_clear_free(dh_client_pub);
|
|
|
|
|
|
|
|
/* save session id := H */
|
|
|
|
if (kex->session_id == NULL) {
|
2005-11-05 12:19:35 +08:00
|
|
|
kex->session_id_len = hashlen;
|
2003-02-24 09:03:03 +08:00
|
|
|
kex->session_id = xmalloc(kex->session_id_len);
|
|
|
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sign H */
|
2013-07-20 11:21:52 +08:00
|
|
|
kex->sign(server_host_private, server_host_public, &signature, &slen,
|
|
|
|
hash, hashlen);
|
2003-02-24 09:03:03 +08:00
|
|
|
|
|
|
|
/* destroy_sensitive_data(); */
|
|
|
|
|
|
|
|
/* send server hostkey, DH pubkey 'f' and singed H */
|
|
|
|
packet_start(SSH2_MSG_KEXDH_REPLY);
|
|
|
|
packet_put_string(server_host_key_blob, sbloblen);
|
|
|
|
packet_put_bignum2(dh->pub_key); /* f */
|
|
|
|
packet_put_string(signature, slen);
|
|
|
|
packet_send();
|
|
|
|
|
2013-06-02 05:31:17 +08:00
|
|
|
free(signature);
|
|
|
|
free(server_host_key_blob);
|
2003-02-24 09:03:03 +08:00
|
|
|
/* have keys, free DH */
|
|
|
|
DH_free(dh);
|
|
|
|
|
2014-01-12 16:21:22 +08:00
|
|
|
kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
|
2003-02-24 09:03:03 +08:00
|
|
|
BN_clear_free(shared_secret);
|
|
|
|
kex_finish(kex);
|
|
|
|
}
|