2011-02-17 11:47:40 +00:00
|
|
|
/* $OpenBSD: kexdhs.c,v 1.12 2010/11/10 01:33:07 djm Exp $ */
|
2003-04-23 16:53:02 +00: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-09-30 13:29:51 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
2003-04-23 16:53:02 +00:00
|
|
|
|
2011-02-17 11:47:40 +00:00
|
|
|
#include <openssl/dh.h>
|
|
|
|
|
2003-04-23 16:53:02 +00:00
|
|
|
#include "xmalloc.h"
|
2006-09-30 13:29:51 +00:00
|
|
|
#include "buffer.h"
|
2003-04-23 16:53:02 +00:00
|
|
|
#include "key.h"
|
2006-09-30 13:29:51 +00:00
|
|
|
#include "cipher.h"
|
2003-04-23 16:53:02 +00:00
|
|
|
#include "kex.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "dh.h"
|
|
|
|
#include "ssh2.h"
|
2006-09-30 13:29:51 +00:00
|
|
|
#ifdef GSSAPI
|
|
|
|
#include "ssh-gss.h"
|
|
|
|
#endif
|
2003-04-23 16:53:02 +00:00
|
|
|
#include "monitor_wrap.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
kexdh_server(Kex *kex)
|
|
|
|
{
|
|
|
|
BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
|
|
|
|
DH *dh;
|
2010-03-08 11:19:52 +00:00
|
|
|
Key *server_host_public, *server_host_private;
|
2003-04-23 16:53:02 +00:00
|
|
|
u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
|
2006-11-10 16:39:21 +00:00
|
|
|
u_int sbloblen, klen, hashlen, slen;
|
|
|
|
int kout;
|
2003-04-23 16:53:02 +00:00
|
|
|
|
|
|
|
/* generate server DH public key */
|
2004-10-28 16:03:53 +00: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-04-23 16:53:02 +00:00
|
|
|
dh_gen_key(dh, kex->we_need * 8);
|
|
|
|
|
|
|
|
debug("expecting SSH2_MSG_KEXDH_INIT");
|
|
|
|
packet_read_expect(SSH2_MSG_KEXDH_INIT);
|
|
|
|
|
2010-03-08 11:19:52 +00:00
|
|
|
if (kex->load_host_public_key == NULL ||
|
|
|
|
kex->load_host_private_key == NULL)
|
2003-04-23 16:53:02 +00:00
|
|
|
fatal("Cannot load hostkey");
|
2010-03-08 11:19:52 +00:00
|
|
|
server_host_public = kex->load_host_public_key(kex->hostkey_type);
|
|
|
|
if (server_host_public == NULL)
|
2003-04-23 16:53:02 +00:00
|
|
|
fatal("Unsupported hostkey type %d", kex->hostkey_type);
|
2010-03-08 11:19:52 +00:00
|
|
|
server_host_private = kex->load_host_private_key(kex->hostkey_type);
|
|
|
|
if (server_host_private == NULL)
|
|
|
|
fatal("Missing private key for hostkey type %d",
|
|
|
|
kex->hostkey_type);
|
2003-04-23 16:53:02 +00: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-10 16:39:21 +00:00
|
|
|
if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
|
|
|
|
fatal("DH_compute_key: failed");
|
2003-04-23 16:53:02 +00: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-10 16:39:21 +00:00
|
|
|
if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
|
|
|
|
fatal("kexdh_server: BN_bin2bn failed");
|
2003-04-23 16:53:02 +00:00
|
|
|
memset(kbuf, 0, klen);
|
|
|
|
xfree(kbuf);
|
|
|
|
|
2010-03-08 11:19:52 +00:00
|
|
|
key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
|
2003-04-23 16:53:02 +00:00
|
|
|
|
|
|
|
/* calc H */
|
2006-03-22 19:46:12 +00:00
|
|
|
kex_dh_hash(
|
2003-04-23 16:53:02 +00: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,
|
2006-03-22 19:46:12 +00:00
|
|
|
shared_secret,
|
|
|
|
&hash, &hashlen
|
2003-04-23 16:53:02 +00:00
|
|
|
);
|
|
|
|
BN_clear_free(dh_client_pub);
|
|
|
|
|
|
|
|
/* save session id := H */
|
|
|
|
if (kex->session_id == NULL) {
|
2006-03-22 19:46:12 +00:00
|
|
|
kex->session_id_len = hashlen;
|
2003-04-23 16:53:02 +00:00
|
|
|
kex->session_id = xmalloc(kex->session_id_len);
|
|
|
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sign H */
|
2010-03-08 11:19:52 +00:00
|
|
|
if (PRIVSEP(key_sign(server_host_private, &signature, &slen, hash,
|
2009-10-01 15:19:37 +00:00
|
|
|
hashlen)) < 0)
|
|
|
|
fatal("kexdh_server: key_sign failed");
|
2003-04-23 16:53:02 +00: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();
|
|
|
|
|
|
|
|
xfree(signature);
|
|
|
|
xfree(server_host_key_blob);
|
|
|
|
/* have keys, free DH */
|
|
|
|
DH_free(dh);
|
|
|
|
|
2006-03-22 19:46:12 +00:00
|
|
|
kex_derive_keys(kex, hash, hashlen, shared_secret);
|
2003-04-23 16:53:02 +00:00
|
|
|
BN_clear_free(shared_secret);
|
|
|
|
kex_finish(kex);
|
|
|
|
}
|