kexecdhs.c revision 262566
1262566Sdes/* $OpenBSD: kexecdhs.c,v 1.9 2014/01/12 08:13:13 djm Exp $ */
2218767Sdes/*
3218767Sdes * Copyright (c) 2001 Markus Friedl.  All rights reserved.
4218767Sdes * Copyright (c) 2010 Damien Miller.  All rights reserved.
5218767Sdes *
6218767Sdes * Redistribution and use in source and binary forms, with or without
7218767Sdes * modification, are permitted provided that the following conditions
8218767Sdes * are met:
9218767Sdes * 1. Redistributions of source code must retain the above copyright
10218767Sdes *    notice, this list of conditions and the following disclaimer.
11218767Sdes * 2. Redistributions in binary form must reproduce the above copyright
12218767Sdes *    notice, this list of conditions and the following disclaimer in the
13218767Sdes *    documentation and/or other materials provided with the distribution.
14218767Sdes *
15218767Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16218767Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17218767Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18218767Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19218767Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20218767Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21218767Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22218767Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23218767Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24218767Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25218767Sdes */
26218767Sdes
27218767Sdes#include "includes.h"
28218767Sdes
29218767Sdes#include <sys/types.h>
30218767Sdes#include <string.h>
31218767Sdes#include <signal.h>
32218767Sdes
33218767Sdes#include "xmalloc.h"
34218767Sdes#include "buffer.h"
35218767Sdes#include "key.h"
36218767Sdes#include "cipher.h"
37218767Sdes#include "kex.h"
38218767Sdes#include "log.h"
39218767Sdes#include "packet.h"
40218767Sdes#include "ssh2.h"
41218767Sdes
42218767Sdes#ifdef OPENSSL_HAS_ECC
43218767Sdes
44218767Sdes#include <openssl/ecdh.h>
45218767Sdes
46218767Sdesvoid
47218767Sdeskexecdh_server(Kex *kex)
48218767Sdes{
49218767Sdes	EC_POINT *client_public;
50218767Sdes	EC_KEY *server_key;
51218767Sdes	const EC_GROUP *group;
52218767Sdes	BIGNUM *shared_secret;
53218767Sdes	Key *server_host_private, *server_host_public;
54218767Sdes	u_char *server_host_key_blob = NULL, *signature = NULL;
55218767Sdes	u_char *kbuf, *hash;
56218767Sdes	u_int klen, slen, sbloblen, hashlen;
57218767Sdes
58255767Sdes	if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL)
59218767Sdes		fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
60218767Sdes	if (EC_KEY_generate_key(server_key) != 1)
61218767Sdes		fatal("%s: EC_KEY_generate_key failed", __func__);
62218767Sdes	group = EC_KEY_get0_group(server_key);
63218767Sdes
64218767Sdes#ifdef DEBUG_KEXECDH
65218767Sdes	fputs("server private key:\n", stderr);
66218767Sdes	key_dump_ec_key(server_key);
67218767Sdes#endif
68218767Sdes
69218767Sdes	if (kex->load_host_public_key == NULL ||
70218767Sdes	    kex->load_host_private_key == NULL)
71218767Sdes		fatal("Cannot load hostkey");
72218767Sdes	server_host_public = kex->load_host_public_key(kex->hostkey_type);
73218767Sdes	if (server_host_public == NULL)
74218767Sdes		fatal("Unsupported hostkey type %d", kex->hostkey_type);
75218767Sdes	server_host_private = kex->load_host_private_key(kex->hostkey_type);
76218767Sdes
77218767Sdes	debug("expecting SSH2_MSG_KEX_ECDH_INIT");
78218767Sdes	packet_read_expect(SSH2_MSG_KEX_ECDH_INIT);
79218767Sdes	if ((client_public = EC_POINT_new(group)) == NULL)
80218767Sdes		fatal("%s: EC_POINT_new failed", __func__);
81218767Sdes	packet_get_ecpoint(group, client_public);
82218767Sdes	packet_check_eom();
83218767Sdes
84218767Sdes	if (key_ec_validate_public(group, client_public) != 0)
85218767Sdes		fatal("%s: invalid client public key", __func__);
86218767Sdes
87218767Sdes#ifdef DEBUG_KEXECDH
88218767Sdes	fputs("client public key:\n", stderr);
89218767Sdes	key_dump_ec_point(group, client_public);
90218767Sdes#endif
91218767Sdes
92218767Sdes	/* Calculate shared_secret */
93218767Sdes	klen = (EC_GROUP_get_degree(group) + 7) / 8;
94218767Sdes	kbuf = xmalloc(klen);
95218767Sdes	if (ECDH_compute_key(kbuf, klen, client_public,
96218767Sdes	    server_key, NULL) != (int)klen)
97218767Sdes		fatal("%s: ECDH_compute_key failed", __func__);
98218767Sdes
99218767Sdes#ifdef DEBUG_KEXDH
100218767Sdes	dump_digest("shared secret", kbuf, klen);
101218767Sdes#endif
102218767Sdes	if ((shared_secret = BN_new()) == NULL)
103218767Sdes		fatal("%s: BN_new failed", __func__);
104218767Sdes	if (BN_bin2bn(kbuf, klen, shared_secret) == NULL)
105218767Sdes		fatal("%s: BN_bin2bn failed", __func__);
106218767Sdes	memset(kbuf, 0, klen);
107255767Sdes	free(kbuf);
108218767Sdes
109218767Sdes	/* calc H */
110218767Sdes	key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
111218767Sdes	kex_ecdh_hash(
112262566Sdes	    kex->hash_alg,
113218767Sdes	    group,
114218767Sdes	    kex->client_version_string,
115218767Sdes	    kex->server_version_string,
116218767Sdes	    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
117218767Sdes	    buffer_ptr(&kex->my), buffer_len(&kex->my),
118218767Sdes	    server_host_key_blob, sbloblen,
119218767Sdes	    client_public,
120218767Sdes	    EC_KEY_get0_public_key(server_key),
121218767Sdes	    shared_secret,
122218767Sdes	    &hash, &hashlen
123218767Sdes	);
124218767Sdes	EC_POINT_clear_free(client_public);
125218767Sdes
126218767Sdes	/* save session id := H */
127218767Sdes	if (kex->session_id == NULL) {
128218767Sdes		kex->session_id_len = hashlen;
129218767Sdes		kex->session_id = xmalloc(kex->session_id_len);
130218767Sdes		memcpy(kex->session_id, hash, kex->session_id_len);
131218767Sdes	}
132218767Sdes
133218767Sdes	/* sign H */
134255767Sdes	kex->sign(server_host_private, server_host_public, &signature, &slen,
135255767Sdes	    hash, hashlen);
136218767Sdes
137218767Sdes	/* destroy_sensitive_data(); */
138218767Sdes
139218767Sdes	/* send server hostkey, ECDH pubkey 'Q_S' and signed H */
140218767Sdes	packet_start(SSH2_MSG_KEX_ECDH_REPLY);
141218767Sdes	packet_put_string(server_host_key_blob, sbloblen);
142218767Sdes	packet_put_ecpoint(group, EC_KEY_get0_public_key(server_key));
143218767Sdes	packet_put_string(signature, slen);
144218767Sdes	packet_send();
145218767Sdes
146255767Sdes	free(signature);
147255767Sdes	free(server_host_key_blob);
148218767Sdes	/* have keys, free server key */
149218767Sdes	EC_KEY_free(server_key);
150218767Sdes
151262566Sdes	kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
152218767Sdes	BN_clear_free(shared_secret);
153218767Sdes	kex_finish(kex);
154218767Sdes}
155218767Sdes#else /* OPENSSL_HAS_ECC */
156218767Sdesvoid
157218767Sdeskexecdh_server(Kex *kex)
158218767Sdes{
159218767Sdes	fatal("ECC support is not enabled");
160218767Sdes}
161218767Sdes#endif /* OPENSSL_HAS_ECC */
162