2013-07-20 03:21:52 +00:00
|
|
|
/* $OpenBSD: kexgexs.c,v 1.16 2013/07/19 07:37:48 markus Exp $ */
|
2003-02-24 01:03:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000 Niels Provos. All rights reserved.
|
|
|
|
* 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 01:02:17 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
2006-09-01 05:38:36 +00:00
|
|
|
#include <stdarg.h>
|
2006-08-05 01:37:59 +00:00
|
|
|
#include <stdio.h>
|
2006-07-24 04:13:33 +00:00
|
|
|
#include <string.h>
|
2006-08-05 02:39:39 +00:00
|
|
|
#include <signal.h>
|
2006-07-24 04:13:33 +00:00
|
|
|
|
2010-11-20 04:15:49 +00:00
|
|
|
#include <openssl/dh.h>
|
|
|
|
|
2003-02-24 01:03:03 +00:00
|
|
|
#include "xmalloc.h"
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "buffer.h"
|
2003-02-24 01:03:03 +00:00
|
|
|
#include "key.h"
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "cipher.h"
|
2003-02-24 01:03:03 +00:00
|
|
|
#include "kex.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "dh.h"
|
|
|
|
#include "ssh2.h"
|
|
|
|
#include "compat.h"
|
2006-08-05 02:39:39 +00:00
|
|
|
#ifdef GSSAPI
|
|
|
|
#include "ssh-gss.h"
|
|
|
|
#endif
|
2003-02-24 01:03:03 +00:00
|
|
|
#include "monitor_wrap.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
kexgex_server(Kex *kex)
|
|
|
|
{
|
|
|
|
BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
|
2010-02-26 20:55:05 +00:00
|
|
|
Key *server_host_public, *server_host_private;
|
2003-02-24 01:03:03 +00:00
|
|
|
DH *dh;
|
|
|
|
u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
|
2006-11-04 18:32:02 +00:00
|
|
|
u_int sbloblen, klen, slen, hashlen;
|
2009-01-28 05:23:06 +00:00
|
|
|
int omin = -1, min = -1, omax = -1, max = -1, onbits = -1, nbits = -1;
|
|
|
|
int type, kout;
|
2003-02-24 01:03:03 +00:00
|
|
|
|
2010-02-26 20:55:05 +00:00
|
|
|
if (kex->load_host_public_key == NULL ||
|
|
|
|
kex->load_host_private_key == NULL)
|
2003-02-24 01:03:03 +00:00
|
|
|
fatal("Cannot load hostkey");
|
2010-02-26 20:55:05 +00:00
|
|
|
server_host_public = kex->load_host_public_key(kex->hostkey_type);
|
|
|
|
if (server_host_public == NULL)
|
2003-02-24 01:03:03 +00:00
|
|
|
fatal("Unsupported hostkey type %d", kex->hostkey_type);
|
2010-02-26 20:55:05 +00:00
|
|
|
server_host_private = kex->load_host_private_key(kex->hostkey_type);
|
2003-02-24 01:03:03 +00:00
|
|
|
|
|
|
|
type = packet_read();
|
|
|
|
switch (type) {
|
|
|
|
case SSH2_MSG_KEX_DH_GEX_REQUEST:
|
|
|
|
debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
|
2009-01-28 05:23:06 +00:00
|
|
|
omin = min = packet_get_int();
|
|
|
|
onbits = nbits = packet_get_int();
|
|
|
|
omax = max = packet_get_int();
|
2003-02-24 01:03:03 +00:00
|
|
|
min = MAX(DH_GRP_MIN, min);
|
|
|
|
max = MIN(DH_GRP_MAX, max);
|
2009-01-28 05:23:06 +00:00
|
|
|
nbits = MAX(DH_GRP_MIN, nbits);
|
|
|
|
nbits = MIN(DH_GRP_MAX, nbits);
|
2003-02-24 01:03:03 +00:00
|
|
|
break;
|
|
|
|
case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
|
|
|
|
debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
|
2009-01-28 05:23:06 +00:00
|
|
|
onbits = nbits = packet_get_int();
|
2003-02-24 01:03:03 +00:00
|
|
|
/* unused for old GEX */
|
2009-01-28 05:23:06 +00:00
|
|
|
omin = min = DH_GRP_MIN;
|
|
|
|
omax = max = DH_GRP_MAX;
|
2003-02-24 01:03:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
|
|
|
|
}
|
|
|
|
packet_check_eom();
|
|
|
|
|
2009-01-28 05:23:06 +00:00
|
|
|
if (omax < omin || onbits < omin || omax < onbits)
|
2003-02-24 01:03:03 +00:00
|
|
|
fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
|
2009-01-28 05:23:06 +00:00
|
|
|
omin, onbits, omax);
|
2003-02-24 01:03:03 +00:00
|
|
|
|
|
|
|
/* Contact privileged parent */
|
|
|
|
dh = PRIVSEP(choose_dh(min, nbits, max));
|
|
|
|
if (dh == NULL)
|
|
|
|
packet_disconnect("Protocol error: no matching DH grp found");
|
|
|
|
|
|
|
|
debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
|
|
|
|
packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
|
|
|
|
packet_put_bignum2(dh->p);
|
|
|
|
packet_put_bignum2(dh->g);
|
|
|
|
packet_send();
|
|
|
|
|
|
|
|
/* flush */
|
|
|
|
packet_write_wait();
|
|
|
|
|
|
|
|
/* Compute our exchange value in parallel with the client */
|
|
|
|
dh_gen_key(dh, kex->we_need * 8);
|
|
|
|
|
|
|
|
debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
|
|
|
|
packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
|
|
|
|
|
|
|
|
/* 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-04 18:32:02 +00:00
|
|
|
if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
|
|
|
|
fatal("DH_compute_key: failed");
|
2003-02-24 01:03:03 +00:00
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
dump_digest("shared secret", kbuf, kout);
|
|
|
|
#endif
|
|
|
|
if ((shared_secret = BN_new()) == NULL)
|
|
|
|
fatal("kexgex_server: BN_new failed");
|
2006-11-07 12:14:41 +00:00
|
|
|
if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
|
|
|
|
fatal("kexgex_server: BN_bin2bn failed");
|
2003-02-24 01:03:03 +00:00
|
|
|
memset(kbuf, 0, klen);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(kbuf);
|
2003-02-24 01:03:03 +00:00
|
|
|
|
2010-02-26 20:55:05 +00:00
|
|
|
key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
|
2003-02-24 01:03:03 +00:00
|
|
|
|
|
|
|
if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
|
2009-01-28 05:23:06 +00:00
|
|
|
omin = min = omax = max = -1;
|
2003-02-24 01:03:03 +00:00
|
|
|
|
2005-11-05 04:19:35 +00:00
|
|
|
/* calc H */
|
|
|
|
kexgex_hash(
|
|
|
|
kex->evp_md,
|
2003-02-24 01:03:03 +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,
|
2009-01-28 05:23:06 +00:00
|
|
|
omin, onbits, omax,
|
2003-02-24 01:03:03 +00:00
|
|
|
dh->p, dh->g,
|
|
|
|
dh_client_pub,
|
|
|
|
dh->pub_key,
|
2005-11-05 04:19:35 +00:00
|
|
|
shared_secret,
|
|
|
|
&hash, &hashlen
|
2003-02-24 01:03:03 +00:00
|
|
|
);
|
|
|
|
BN_clear_free(dh_client_pub);
|
|
|
|
|
|
|
|
/* save session id := H */
|
|
|
|
if (kex->session_id == NULL) {
|
2005-11-05 04:19:35 +00:00
|
|
|
kex->session_id_len = hashlen;
|
2003-02-24 01:03:03 +00:00
|
|
|
kex->session_id = xmalloc(kex->session_id_len);
|
|
|
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sign H */
|
2013-07-20 03:21:52 +00:00
|
|
|
kex->sign(server_host_private, server_host_public, &signature, &slen,
|
|
|
|
hash, hashlen);
|
2003-02-24 01:03:03 +00:00
|
|
|
|
|
|
|
/* destroy_sensitive_data(); */
|
|
|
|
|
|
|
|
/* send server hostkey, DH pubkey 'f' and singed H */
|
|
|
|
debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
|
|
|
|
packet_start(SSH2_MSG_KEX_DH_GEX_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-01 21:31:17 +00:00
|
|
|
free(signature);
|
|
|
|
free(server_host_key_blob);
|
2003-02-24 01:03:03 +00:00
|
|
|
/* have keys, free DH */
|
|
|
|
DH_free(dh);
|
|
|
|
|
2005-11-05 04:19:35 +00:00
|
|
|
kex_derive_keys(kex, hash, hashlen, shared_secret);
|
2003-02-24 01:03:03 +00:00
|
|
|
BN_clear_free(shared_secret);
|
|
|
|
|
|
|
|
kex_finish(kex);
|
|
|
|
}
|