1294332Sdes/* $OpenBSD: kexecdhc.c,v 1.10 2015/01/26 06:10:03 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
29294332Sdes#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
30294332Sdes
31218767Sdes#include <sys/types.h>
32218767Sdes
33218767Sdes#include <stdio.h>
34218767Sdes#include <string.h>
35218767Sdes#include <signal.h>
36218767Sdes
37294332Sdes#include <openssl/ecdh.h>
38294332Sdes
39294332Sdes#include "sshkey.h"
40218767Sdes#include "cipher.h"
41294332Sdes#include "digest.h"
42218767Sdes#include "kex.h"
43218767Sdes#include "log.h"
44218767Sdes#include "packet.h"
45218767Sdes#include "dh.h"
46218767Sdes#include "ssh2.h"
47294332Sdes#include "dispatch.h"
48294332Sdes#include "compat.h"
49294332Sdes#include "ssherr.h"
50294332Sdes#include "sshbuf.h"
51218767Sdes
52294332Sdesstatic int input_kex_ecdh_reply(int, u_int32_t, void *);
53218767Sdes
54294332Sdesint
55294332Sdeskexecdh_client(struct ssh *ssh)
56218767Sdes{
57294332Sdes	struct kex *kex = ssh->kex;
58294332Sdes	EC_KEY *client_key = NULL;
59218767Sdes	const EC_GROUP *group;
60294332Sdes	const EC_POINT *public_key;
61294332Sdes	int r;
62218767Sdes
63294332Sdes	if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
64294332Sdes		r = SSH_ERR_ALLOC_FAIL;
65294332Sdes		goto out;
66294332Sdes	}
67294332Sdes	if (EC_KEY_generate_key(client_key) != 1) {
68294332Sdes		r = SSH_ERR_LIBCRYPTO_ERROR;
69294332Sdes		goto out;
70294332Sdes	}
71218767Sdes	group = EC_KEY_get0_group(client_key);
72294332Sdes	public_key = EC_KEY_get0_public_key(client_key);
73218767Sdes
74294332Sdes	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
75294332Sdes	    (r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
76294332Sdes	    (r = sshpkt_send(ssh)) != 0)
77294332Sdes		goto out;
78218767Sdes	debug("sending SSH2_MSG_KEX_ECDH_INIT");
79218767Sdes
80218767Sdes#ifdef DEBUG_KEXECDH
81218767Sdes	fputs("client private key:\n", stderr);
82294332Sdes	sshkey_dump_ec_key(client_key);
83218767Sdes#endif
84294332Sdes	kex->ec_client_key = client_key;
85294332Sdes	kex->ec_group = group;
86294332Sdes	client_key = NULL;	/* owned by the kex */
87218767Sdes
88218767Sdes	debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
89294332Sdes	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_ecdh_reply);
90294332Sdes	r = 0;
91294332Sdes out:
92294332Sdes	if (client_key)
93294332Sdes		EC_KEY_free(client_key);
94294332Sdes	return r;
95294332Sdes}
96218767Sdes
97294332Sdesstatic int
98294332Sdesinput_kex_ecdh_reply(int type, u_int32_t seq, void *ctxt)
99294332Sdes{
100294332Sdes	struct ssh *ssh = ctxt;
101294332Sdes	struct kex *kex = ssh->kex;
102294332Sdes	const EC_GROUP *group;
103294332Sdes	EC_POINT *server_public = NULL;
104294332Sdes	EC_KEY *client_key;
105294332Sdes	BIGNUM *shared_secret = NULL;
106294332Sdes	struct sshkey *server_host_key = NULL;
107294332Sdes	u_char *server_host_key_blob = NULL, *signature = NULL;
108294332Sdes	u_char *kbuf = NULL;
109294332Sdes	u_char hash[SSH_DIGEST_MAX_LENGTH];
110294332Sdes	size_t slen, sbloblen;
111294332Sdes	size_t klen = 0, hashlen;
112294332Sdes	int r;
113294332Sdes
114294332Sdes	if (kex->verify_host_key == NULL) {
115294332Sdes		r = SSH_ERR_INVALID_ARGUMENT;
116294332Sdes		goto out;
117294332Sdes	}
118294332Sdes	group = kex->ec_group;
119294332Sdes	client_key = kex->ec_client_key;
120294332Sdes
121218767Sdes	/* hostkey */
122294332Sdes	if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
123294332Sdes	    &sbloblen)) != 0 ||
124294332Sdes	    (r = sshkey_from_blob(server_host_key_blob, sbloblen,
125294332Sdes	    &server_host_key)) != 0)
126294332Sdes		goto out;
127294332Sdes	if (server_host_key->type != kex->hostkey_type ||
128294332Sdes	    (kex->hostkey_type == KEY_ECDSA &&
129294332Sdes	    server_host_key->ecdsa_nid != kex->hostkey_nid)) {
130294332Sdes		r = SSH_ERR_KEY_TYPE_MISMATCH;
131294332Sdes		goto out;
132294332Sdes	}
133294332Sdes	if (kex->verify_host_key(server_host_key, ssh) == -1) {
134294332Sdes		r = SSH_ERR_SIGNATURE_INVALID;
135294332Sdes		goto out;
136294332Sdes	}
137218767Sdes
138218767Sdes	/* Q_S, server public key */
139294332Sdes	/* signed H */
140294332Sdes	if ((server_public = EC_POINT_new(group)) == NULL) {
141294332Sdes		r = SSH_ERR_ALLOC_FAIL;
142294332Sdes		goto out;
143294332Sdes	}
144294332Sdes	if ((r = sshpkt_get_ec(ssh, server_public, group)) != 0 ||
145294332Sdes	    (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
146294332Sdes	    (r = sshpkt_get_end(ssh)) != 0)
147294332Sdes		goto out;
148218767Sdes
149218767Sdes#ifdef DEBUG_KEXECDH
150218767Sdes	fputs("server public key:\n", stderr);
151294332Sdes	sshkey_dump_ec_point(group, server_public);
152218767Sdes#endif
153294332Sdes	if (sshkey_ec_validate_public(group, server_public) != 0) {
154294332Sdes		sshpkt_disconnect(ssh, "invalid server public key");
155294332Sdes		r = SSH_ERR_MESSAGE_INCOMPLETE;
156294332Sdes		goto out;
157294332Sdes	}
158218767Sdes
159218767Sdes	klen = (EC_GROUP_get_degree(group) + 7) / 8;
160294332Sdes	if ((kbuf = malloc(klen)) == NULL ||
161294332Sdes	    (shared_secret = BN_new()) == NULL) {
162294332Sdes		r = SSH_ERR_ALLOC_FAIL;
163294332Sdes		goto out;
164294332Sdes	}
165218767Sdes	if (ECDH_compute_key(kbuf, klen, server_public,
166294332Sdes	    client_key, NULL) != (int)klen ||
167294332Sdes	    BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
168294332Sdes		r = SSH_ERR_LIBCRYPTO_ERROR;
169294332Sdes		goto out;
170294332Sdes	}
171218767Sdes
172218767Sdes#ifdef DEBUG_KEXECDH
173218767Sdes	dump_digest("shared secret", kbuf, klen);
174218767Sdes#endif
175218767Sdes	/* calc and verify H */
176294332Sdes	hashlen = sizeof(hash);
177294332Sdes	if ((r = kex_ecdh_hash(
178261320Sdes	    kex->hash_alg,
179218767Sdes	    group,
180218767Sdes	    kex->client_version_string,
181218767Sdes	    kex->server_version_string,
182294332Sdes	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
183294332Sdes	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
184218767Sdes	    server_host_key_blob, sbloblen,
185218767Sdes	    EC_KEY_get0_public_key(client_key),
186218767Sdes	    server_public,
187218767Sdes	    shared_secret,
188294332Sdes	    hash, &hashlen)) != 0)
189294332Sdes		goto out;
190218767Sdes
191294332Sdes	if ((r = sshkey_verify(server_host_key, signature, slen, hash,
192294332Sdes	    hashlen, ssh->compat)) != 0)
193294332Sdes		goto out;
194218767Sdes
195218767Sdes	/* save session id */
196218767Sdes	if (kex->session_id == NULL) {
197218767Sdes		kex->session_id_len = hashlen;
198294332Sdes		kex->session_id = malloc(kex->session_id_len);
199294332Sdes		if (kex->session_id == NULL) {
200294332Sdes			r = SSH_ERR_ALLOC_FAIL;
201294332Sdes			goto out;
202294332Sdes		}
203218767Sdes		memcpy(kex->session_id, hash, kex->session_id_len);
204218767Sdes	}
205218767Sdes
206294332Sdes	if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
207294332Sdes		r = kex_send_newkeys(ssh);
208294332Sdes out:
209294332Sdes	explicit_bzero(hash, sizeof(hash));
210294332Sdes	if (kex->ec_client_key) {
211294332Sdes		EC_KEY_free(kex->ec_client_key);
212294332Sdes		kex->ec_client_key = NULL;
213294332Sdes	}
214294332Sdes	if (server_public)
215294332Sdes		EC_POINT_clear_free(server_public);
216294332Sdes	if (kbuf) {
217294332Sdes		explicit_bzero(kbuf, klen);
218294332Sdes		free(kbuf);
219294332Sdes	}
220294332Sdes	if (shared_secret)
221294332Sdes		BN_clear_free(shared_secret);
222294332Sdes	sshkey_free(server_host_key);
223294332Sdes	free(server_host_key_blob);
224294332Sdes	free(signature);
225294332Sdes	return r;
226218767Sdes}
227294332Sdes#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
228294332Sdes
229