2013-06-01 23:31:17 +02:00
|
|
|
/* $OpenBSD: kexgexc.c,v 1.13 2013/05/17 00:13:13 djm Exp $ */
|
2003-02-24 02:03:03 +01: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 04:39:39 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2010-11-20 05:15:49 +01:00
|
|
|
#include <openssl/dh.h>
|
|
|
|
|
2006-09-01 07:38:36 +02:00
|
|
|
#include <stdarg.h>
|
2006-08-05 03:37:59 +02:00
|
|
|
#include <stdio.h>
|
2006-07-24 06:13:33 +02:00
|
|
|
#include <string.h>
|
2006-08-05 04:39:39 +02:00
|
|
|
#include <signal.h>
|
2006-07-24 06:13:33 +02:00
|
|
|
|
2003-02-24 02:03:03 +01:00
|
|
|
#include "xmalloc.h"
|
2006-08-05 04:39:39 +02:00
|
|
|
#include "buffer.h"
|
2003-02-24 02:03:03 +01:00
|
|
|
#include "key.h"
|
2006-08-05 04:39:39 +02:00
|
|
|
#include "cipher.h"
|
2003-02-24 02:03:03 +01:00
|
|
|
#include "kex.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "dh.h"
|
|
|
|
#include "ssh2.h"
|
|
|
|
#include "compat.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
kexgex_client(Kex *kex)
|
|
|
|
{
|
|
|
|
BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
|
|
|
|
BIGNUM *p = NULL, *g = NULL;
|
|
|
|
Key *server_host_key;
|
|
|
|
u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
|
2006-11-04 19:32:02 +01:00
|
|
|
u_int klen, slen, sbloblen, hashlen;
|
|
|
|
int kout;
|
2003-02-24 02:03:03 +01:00
|
|
|
int min, max, nbits;
|
|
|
|
DH *dh;
|
|
|
|
|
|
|
|
nbits = dh_estimate(kex->we_need * 8);
|
|
|
|
|
|
|
|
if (datafellows & SSH_OLD_DHGEX) {
|
|
|
|
/* Old GEX request */
|
|
|
|
packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
|
|
|
|
packet_put_int(nbits);
|
|
|
|
min = DH_GRP_MIN;
|
|
|
|
max = DH_GRP_MAX;
|
|
|
|
|
2003-12-09 09:18:07 +01:00
|
|
|
debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits);
|
|
|
|
} else {
|
2003-02-24 02:03:03 +01:00
|
|
|
/* New GEX request */
|
|
|
|
min = DH_GRP_MIN;
|
|
|
|
max = DH_GRP_MAX;
|
|
|
|
packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
|
|
|
|
packet_put_int(min);
|
|
|
|
packet_put_int(nbits);
|
|
|
|
packet_put_int(max);
|
2003-12-09 09:18:07 +01:00
|
|
|
|
|
|
|
debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
|
|
|
|
min, nbits, max);
|
2003-02-24 02:03:03 +01:00
|
|
|
}
|
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
|
|
|
|
min, nbits, max);
|
|
|
|
#endif
|
|
|
|
packet_send();
|
|
|
|
|
|
|
|
debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
|
|
|
|
packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
|
|
|
|
|
|
|
|
if ((p = BN_new()) == NULL)
|
|
|
|
fatal("BN_new");
|
|
|
|
packet_get_bignum2(p);
|
|
|
|
if ((g = BN_new()) == NULL)
|
|
|
|
fatal("BN_new");
|
|
|
|
packet_get_bignum2(g);
|
|
|
|
packet_check_eom();
|
|
|
|
|
|
|
|
if (BN_num_bits(p) < min || BN_num_bits(p) > max)
|
|
|
|
fatal("DH_GEX group out of range: %d !< %d !< %d",
|
|
|
|
min, BN_num_bits(p), max);
|
|
|
|
|
|
|
|
dh = dh_new_group(g, p);
|
|
|
|
dh_gen_key(dh, kex->we_need * 8);
|
|
|
|
|
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
DHparams_print_fp(stderr, dh);
|
|
|
|
fprintf(stderr, "pub= ");
|
|
|
|
BN_print_fp(stderr, dh->pub_key);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
|
|
|
|
/* generate and send 'e', client DH public key */
|
|
|
|
packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
|
|
|
|
packet_put_bignum2(dh->pub_key);
|
|
|
|
packet_send();
|
|
|
|
|
|
|
|
debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
|
|
|
|
packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
|
|
|
|
|
|
|
|
/* key, cert */
|
|
|
|
server_host_key_blob = packet_get_string(&sbloblen);
|
|
|
|
server_host_key = key_from_blob(server_host_key_blob, sbloblen);
|
|
|
|
if (server_host_key == NULL)
|
|
|
|
fatal("cannot decode server_host_key_blob");
|
|
|
|
if (server_host_key->type != kex->hostkey_type)
|
|
|
|
fatal("type mismatch for decoded server_host_key_blob");
|
|
|
|
if (kex->verify_host_key == NULL)
|
|
|
|
fatal("cannot verify server_host_key");
|
|
|
|
if (kex->verify_host_key(server_host_key) == -1)
|
|
|
|
fatal("server_host_key verification failed");
|
|
|
|
|
2006-06-13 05:00:41 +02:00
|
|
|
/* DH parameter f, server public DH key */
|
2003-02-24 02:03:03 +01:00
|
|
|
if ((dh_server_pub = BN_new()) == NULL)
|
|
|
|
fatal("dh_server_pub == NULL");
|
|
|
|
packet_get_bignum2(dh_server_pub);
|
|
|
|
|
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
fprintf(stderr, "dh_server_pub= ");
|
|
|
|
BN_print_fp(stderr, dh_server_pub);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
debug("bits %d", BN_num_bits(dh_server_pub));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* signed H */
|
|
|
|
signature = packet_get_string(&slen);
|
|
|
|
packet_check_eom();
|
|
|
|
|
|
|
|
if (!dh_pub_is_valid(dh, dh_server_pub))
|
|
|
|
packet_disconnect("bad server public DH value");
|
|
|
|
|
|
|
|
klen = DH_size(dh);
|
|
|
|
kbuf = xmalloc(klen);
|
2006-11-04 19:32:02 +01:00
|
|
|
if ((kout = DH_compute_key(kbuf, dh_server_pub, dh)) < 0)
|
|
|
|
fatal("DH_compute_key: failed");
|
2003-02-24 02:03:03 +01:00
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
dump_digest("shared secret", kbuf, kout);
|
|
|
|
#endif
|
|
|
|
if ((shared_secret = BN_new()) == NULL)
|
|
|
|
fatal("kexgex_client: BN_new failed");
|
2006-11-07 13:14:41 +01:00
|
|
|
if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
|
|
|
|
fatal("kexgex_client: BN_bin2bn failed");
|
2003-02-24 02:03:03 +01:00
|
|
|
memset(kbuf, 0, klen);
|
2013-06-01 23:31:17 +02:00
|
|
|
free(kbuf);
|
2003-02-24 02:03:03 +01:00
|
|
|
|
|
|
|
if (datafellows & SSH_OLD_DHGEX)
|
|
|
|
min = max = -1;
|
|
|
|
|
|
|
|
/* calc and verify H */
|
2005-11-05 05:19:35 +01:00
|
|
|
kexgex_hash(
|
|
|
|
kex->evp_md,
|
2003-02-24 02:03:03 +01:00
|
|
|
kex->client_version_string,
|
|
|
|
kex->server_version_string,
|
|
|
|
buffer_ptr(&kex->my), buffer_len(&kex->my),
|
|
|
|
buffer_ptr(&kex->peer), buffer_len(&kex->peer),
|
|
|
|
server_host_key_blob, sbloblen,
|
|
|
|
min, nbits, max,
|
|
|
|
dh->p, dh->g,
|
|
|
|
dh->pub_key,
|
|
|
|
dh_server_pub,
|
2005-11-05 05:19:35 +01:00
|
|
|
shared_secret,
|
|
|
|
&hash, &hashlen
|
2003-02-24 02:03:03 +01:00
|
|
|
);
|
2005-11-05 05:19:35 +01:00
|
|
|
|
2003-02-24 02:03:03 +01:00
|
|
|
/* have keys, free DH */
|
|
|
|
DH_free(dh);
|
2013-06-01 23:31:17 +02:00
|
|
|
free(server_host_key_blob);
|
2003-02-24 02:03:03 +01:00
|
|
|
BN_clear_free(dh_server_pub);
|
|
|
|
|
2005-11-05 05:19:35 +01:00
|
|
|
if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
|
2003-02-24 02:03:03 +01:00
|
|
|
fatal("key_verify failed for server_host_key");
|
|
|
|
key_free(server_host_key);
|
2013-06-01 23:31:17 +02:00
|
|
|
free(signature);
|
2003-02-24 02:03:03 +01:00
|
|
|
|
|
|
|
/* save session id */
|
|
|
|
if (kex->session_id == NULL) {
|
2005-11-05 05:19:35 +01:00
|
|
|
kex->session_id_len = hashlen;
|
2003-02-24 02:03:03 +01:00
|
|
|
kex->session_id = xmalloc(kex->session_id_len);
|
|
|
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
|
|
|
}
|
2005-11-05 05:19:35 +01:00
|
|
|
kex_derive_keys(kex, hash, hashlen, shared_secret);
|
2003-02-24 02:03:03 +01:00
|
|
|
BN_clear_free(shared_secret);
|
|
|
|
|
|
|
|
kex_finish(kex);
|
|
|
|
}
|