2006-11-07 15:14:41 +03:00
|
|
|
/* $OpenBSD: kexdhc.c,v 1.11 2006/11/06 21:25:28 markus Exp $ */
|
2003-02-24 04:03:03 +03: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 06:39:39 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2006-09-01 09:38:36 +04:00
|
|
|
#include <stdarg.h>
|
2006-08-05 05:37:59 +04:00
|
|
|
#include <stdio.h>
|
2006-07-24 08:13:33 +04:00
|
|
|
#include <string.h>
|
2006-08-05 06:39:39 +04:00
|
|
|
#include <signal.h>
|
2006-07-24 08:13:33 +04:00
|
|
|
|
2003-02-24 04:03:03 +03:00
|
|
|
#include "xmalloc.h"
|
2006-08-05 06:39:39 +04:00
|
|
|
#include "buffer.h"
|
2003-02-24 04:03:03 +03:00
|
|
|
#include "key.h"
|
2006-08-05 06:39:39 +04:00
|
|
|
#include "cipher.h"
|
2003-02-24 04:03:03 +03:00
|
|
|
#include "kex.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "dh.h"
|
|
|
|
#include "ssh2.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
kexdh_client(Kex *kex)
|
|
|
|
{
|
|
|
|
BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
|
|
|
|
DH *dh;
|
|
|
|
Key *server_host_key;
|
|
|
|
u_char *server_host_key_blob = NULL, *signature = NULL;
|
|
|
|
u_char *kbuf, *hash;
|
2006-11-04 21:32:02 +03:00
|
|
|
u_int klen, slen, sbloblen, hashlen;
|
|
|
|
int kout;
|
2003-02-24 04:03:03 +03:00
|
|
|
|
|
|
|
/* generate and send 'e', client DH public key */
|
2004-06-15 04:30:09 +04: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 04:03:03 +03:00
|
|
|
dh_gen_key(dh, kex->we_need * 8);
|
|
|
|
packet_start(SSH2_MSG_KEXDH_INIT);
|
|
|
|
packet_put_bignum2(dh->pub_key);
|
|
|
|
packet_send();
|
|
|
|
|
|
|
|
debug("sending SSH2_MSG_KEXDH_INIT");
|
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
DHparams_print_fp(stderr, dh);
|
|
|
|
fprintf(stderr, "pub= ");
|
|
|
|
BN_print_fp(stderr, dh->pub_key);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
debug("expecting SSH2_MSG_KEXDH_REPLY");
|
|
|
|
packet_read_expect(SSH2_MSG_KEXDH_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 07:00:41 +04:00
|
|
|
/* DH parameter f, server public DH key */
|
2003-02-24 04:03:03 +03: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 21:32:02 +03:00
|
|
|
if ((kout = DH_compute_key(kbuf, dh_server_pub, dh)) < 0)
|
|
|
|
fatal("DH_compute_key: failed");
|
2003-02-24 04:03:03 +03:00
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
dump_digest("shared secret", kbuf, kout);
|
|
|
|
#endif
|
|
|
|
if ((shared_secret = BN_new()) == NULL)
|
|
|
|
fatal("kexdh_client: BN_new failed");
|
2006-11-07 15:14:41 +03:00
|
|
|
if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
|
|
|
|
fatal("kexdh_client: BN_bin2bn failed");
|
2003-02-24 04:03:03 +03:00
|
|
|
memset(kbuf, 0, klen);
|
|
|
|
xfree(kbuf);
|
|
|
|
|
|
|
|
/* calc and verify H */
|
2005-11-05 07:19:35 +03:00
|
|
|
kex_dh_hash(
|
2003-02-24 04:03:03 +03: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,
|
|
|
|
dh->pub_key,
|
|
|
|
dh_server_pub,
|
2005-11-05 07:19:35 +03:00
|
|
|
shared_secret,
|
|
|
|
&hash, &hashlen
|
2003-02-24 04:03:03 +03:00
|
|
|
);
|
|
|
|
xfree(server_host_key_blob);
|
|
|
|
BN_clear_free(dh_server_pub);
|
|
|
|
DH_free(dh);
|
|
|
|
|
2005-11-05 07:19:35 +03:00
|
|
|
if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
|
2003-02-24 04:03:03 +03:00
|
|
|
fatal("key_verify failed for server_host_key");
|
|
|
|
key_free(server_host_key);
|
|
|
|
xfree(signature);
|
|
|
|
|
|
|
|
/* save session id */
|
|
|
|
if (kex->session_id == NULL) {
|
2005-11-05 07:19:35 +03:00
|
|
|
kex->session_id_len = hashlen;
|
2003-02-24 04:03:03 +03:00
|
|
|
kex->session_id = xmalloc(kex->session_id_len);
|
|
|
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
|
|
|
}
|
|
|
|
|
2005-11-05 07:19:35 +03:00
|
|
|
kex_derive_keys(kex, hash, hashlen, shared_secret);
|
2003-02-24 04:03:03 +03:00
|
|
|
BN_clear_free(shared_secret);
|
|
|
|
kex_finish(kex);
|
|
|
|
}
|