1296633Sdes/* $OpenBSD: kexecdhs.c,v 1.15 2015/12/04 16:41:28 markus 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
29294332Sdes#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
30294332Sdes
31218767Sdes#include <sys/types.h>
32218767Sdes#include <string.h>
33218767Sdes#include <signal.h>
34218767Sdes
35294332Sdes#include <openssl/ecdh.h>
36294332Sdes
37294332Sdes#include "sshkey.h"
38218767Sdes#include "cipher.h"
39294332Sdes#include "digest.h"
40218767Sdes#include "kex.h"
41218767Sdes#include "log.h"
42218767Sdes#include "packet.h"
43218767Sdes#include "ssh2.h"
44218767Sdes
45294332Sdes#include "dispatch.h"
46294332Sdes#include "compat.h"
47294332Sdes#include "ssherr.h"
48294332Sdes#include "sshbuf.h"
49218767Sdes
50294332Sdesstatic int input_kex_ecdh_init(int, u_int32_t, void *);
51218767Sdes
52294332Sdesint
53294332Sdeskexecdh_server(struct ssh *ssh)
54218767Sdes{
55294332Sdes	debug("expecting SSH2_MSG_KEX_ECDH_INIT");
56294332Sdes	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_ecdh_init);
57294332Sdes	return 0;
58294332Sdes}
59294332Sdes
60294332Sdesstatic int
61294332Sdesinput_kex_ecdh_init(int type, u_int32_t seq, void *ctxt)
62294332Sdes{
63294332Sdes	struct ssh *ssh = ctxt;
64294332Sdes	struct kex *kex = ssh->kex;
65218767Sdes	EC_POINT *client_public;
66294332Sdes	EC_KEY *server_key = NULL;
67218767Sdes	const EC_GROUP *group;
68294332Sdes	const EC_POINT *public_key;
69294332Sdes	BIGNUM *shared_secret = NULL;
70294332Sdes	struct sshkey *server_host_private, *server_host_public;
71218767Sdes	u_char *server_host_key_blob = NULL, *signature = NULL;
72294332Sdes	u_char *kbuf = NULL;
73294332Sdes	u_char hash[SSH_DIGEST_MAX_LENGTH];
74294332Sdes	size_t slen, sbloblen;
75294332Sdes	size_t klen = 0, hashlen;
76294332Sdes	int r;
77218767Sdes
78294332Sdes	if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
79294332Sdes		r = SSH_ERR_ALLOC_FAIL;
80294332Sdes		goto out;
81294332Sdes	}
82294332Sdes	if (EC_KEY_generate_key(server_key) != 1) {
83294332Sdes		r = SSH_ERR_LIBCRYPTO_ERROR;
84294332Sdes		goto out;
85294332Sdes	}
86218767Sdes	group = EC_KEY_get0_group(server_key);
87218767Sdes
88218767Sdes#ifdef DEBUG_KEXECDH
89218767Sdes	fputs("server private key:\n", stderr);
90294332Sdes	sshkey_dump_ec_key(server_key);
91218767Sdes#endif
92218767Sdes
93218767Sdes	if (kex->load_host_public_key == NULL ||
94294332Sdes	    kex->load_host_private_key == NULL) {
95294332Sdes		r = SSH_ERR_INVALID_ARGUMENT;
96294332Sdes		goto out;
97294332Sdes	}
98294332Sdes	server_host_public = kex->load_host_public_key(kex->hostkey_type,
99294332Sdes	    kex->hostkey_nid, ssh);
100294332Sdes	server_host_private = kex->load_host_private_key(kex->hostkey_type,
101294332Sdes	    kex->hostkey_nid, ssh);
102294332Sdes	if (server_host_public == NULL) {
103294332Sdes		r = SSH_ERR_NO_HOSTKEY_LOADED;
104294332Sdes		goto out;
105294332Sdes	}
106294332Sdes	if ((client_public = EC_POINT_new(group)) == NULL) {
107294332Sdes		r = SSH_ERR_ALLOC_FAIL;
108294332Sdes		goto out;
109294332Sdes	}
110294332Sdes	if ((r = sshpkt_get_ec(ssh, client_public, group)) != 0 ||
111294332Sdes	    (r = sshpkt_get_end(ssh)) != 0)
112294332Sdes		goto out;
113218767Sdes
114218767Sdes#ifdef DEBUG_KEXECDH
115218767Sdes	fputs("client public key:\n", stderr);
116294332Sdes	sshkey_dump_ec_point(group, client_public);
117218767Sdes#endif
118294332Sdes	if (sshkey_ec_validate_public(group, client_public) != 0) {
119294332Sdes		sshpkt_disconnect(ssh, "invalid client public key");
120294332Sdes		r = SSH_ERR_MESSAGE_INCOMPLETE;
121294332Sdes		goto out;
122294332Sdes	}
123218767Sdes
124218767Sdes	/* Calculate shared_secret */
125218767Sdes	klen = (EC_GROUP_get_degree(group) + 7) / 8;
126294332Sdes	if ((kbuf = malloc(klen)) == NULL ||
127294332Sdes	    (shared_secret = BN_new()) == NULL) {
128294332Sdes		r = SSH_ERR_ALLOC_FAIL;
129294332Sdes		goto out;
130294332Sdes	}
131218767Sdes	if (ECDH_compute_key(kbuf, klen, client_public,
132294332Sdes	    server_key, NULL) != (int)klen ||
133294332Sdes	    BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
134294332Sdes		r = SSH_ERR_LIBCRYPTO_ERROR;
135294332Sdes		goto out;
136294332Sdes	}
137218767Sdes
138294332Sdes#ifdef DEBUG_KEXECDH
139218767Sdes	dump_digest("shared secret", kbuf, klen);
140218767Sdes#endif
141218767Sdes	/* calc H */
142294332Sdes	if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
143294332Sdes	    &sbloblen)) != 0)
144294332Sdes		goto out;
145294332Sdes	hashlen = sizeof(hash);
146294332Sdes	if ((r = kex_ecdh_hash(
147261320Sdes	    kex->hash_alg,
148218767Sdes	    group,
149218767Sdes	    kex->client_version_string,
150218767Sdes	    kex->server_version_string,
151294332Sdes	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
152294332Sdes	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
153218767Sdes	    server_host_key_blob, sbloblen,
154218767Sdes	    client_public,
155218767Sdes	    EC_KEY_get0_public_key(server_key),
156218767Sdes	    shared_secret,
157294332Sdes	    hash, &hashlen)) != 0)
158294332Sdes		goto out;
159218767Sdes
160218767Sdes	/* save session id := H */
161218767Sdes	if (kex->session_id == NULL) {
162218767Sdes		kex->session_id_len = hashlen;
163294332Sdes		kex->session_id = malloc(kex->session_id_len);
164294332Sdes		if (kex->session_id == NULL) {
165294332Sdes			r = SSH_ERR_ALLOC_FAIL;
166294332Sdes			goto out;
167294332Sdes		}
168218767Sdes		memcpy(kex->session_id, hash, kex->session_id_len);
169218767Sdes	}
170218767Sdes
171218767Sdes	/* sign H */
172296633Sdes	if ((r = kex->sign(server_host_private, server_host_public, &signature,
173296633Sdes	     &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
174294332Sdes		goto out;
175218767Sdes
176218767Sdes	/* destroy_sensitive_data(); */
177218767Sdes
178294332Sdes	public_key = EC_KEY_get0_public_key(server_key);
179218767Sdes	/* send server hostkey, ECDH pubkey 'Q_S' and signed H */
180294332Sdes	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
181294332Sdes	    (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
182294332Sdes	    (r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
183294332Sdes	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
184294332Sdes	    (r = sshpkt_send(ssh)) != 0)
185294332Sdes		goto out;
186218767Sdes
187294332Sdes	if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
188294332Sdes		r = kex_send_newkeys(ssh);
189294332Sdes out:
190294332Sdes	explicit_bzero(hash, sizeof(hash));
191294332Sdes	if (kex->ec_client_key) {
192294332Sdes		EC_KEY_free(kex->ec_client_key);
193294332Sdes		kex->ec_client_key = NULL;
194294332Sdes	}
195294332Sdes	if (server_key)
196294332Sdes		EC_KEY_free(server_key);
197294332Sdes	if (kbuf) {
198294332Sdes		explicit_bzero(kbuf, klen);
199294332Sdes		free(kbuf);
200294332Sdes	}
201294332Sdes	if (shared_secret)
202294332Sdes		BN_clear_free(shared_secret);
203294332Sdes	free(server_host_key_blob);
204255767Sdes	free(signature);
205294332Sdes	return r;
206294332Sdes}
207294332Sdes#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
208218767Sdes
209