1295367Sdes/* $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
29295367Sdes#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
30295367Sdes
31218767Sdes#include <sys/types.h>
32218767Sdes
33218767Sdes#include <stdio.h>
34218767Sdes#include <string.h>
35218767Sdes#include <signal.h>
36218767Sdes
37295367Sdes#include <openssl/ecdh.h>
38295367Sdes
39295367Sdes#include "sshkey.h"
40218767Sdes#include "cipher.h"
41295367Sdes#include "digest.h"
42218767Sdes#include "kex.h"
43218767Sdes#include "log.h"
44218767Sdes#include "packet.h"
45218767Sdes#include "dh.h"
46218767Sdes#include "ssh2.h"
47295367Sdes#include "dispatch.h"
48295367Sdes#include "compat.h"
49295367Sdes#include "ssherr.h"
50295367Sdes#include "sshbuf.h"
51218767Sdes
52295367Sdesstatic int input_kex_ecdh_reply(int, u_int32_t, void *);
53218767Sdes
54295367Sdesint
55295367Sdeskexecdh_client(struct ssh *ssh)
56218767Sdes{
57295367Sdes	struct kex *kex = ssh->kex;
58295367Sdes	EC_KEY *client_key = NULL;
59218767Sdes	const EC_GROUP *group;
60295367Sdes	const EC_POINT *public_key;
61295367Sdes	int r;
62218767Sdes
63295367Sdes	if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
64295367Sdes		r = SSH_ERR_ALLOC_FAIL;
65295367Sdes		goto out;
66295367Sdes	}
67295367Sdes	if (EC_KEY_generate_key(client_key) != 1) {
68295367Sdes		r = SSH_ERR_LIBCRYPTO_ERROR;
69295367Sdes		goto out;
70295367Sdes	}
71218767Sdes	group = EC_KEY_get0_group(client_key);
72295367Sdes	public_key = EC_KEY_get0_public_key(client_key);
73218767Sdes
74295367Sdes	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
75295367Sdes	    (r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
76295367Sdes	    (r = sshpkt_send(ssh)) != 0)
77295367Sdes		goto out;
78218767Sdes	debug("sending SSH2_MSG_KEX_ECDH_INIT");
79218767Sdes
80218767Sdes#ifdef DEBUG_KEXECDH
81218767Sdes	fputs("client private key:\n", stderr);
82295367Sdes	sshkey_dump_ec_key(client_key);
83218767Sdes#endif
84295367Sdes	kex->ec_client_key = client_key;
85295367Sdes	kex->ec_group = group;
86295367Sdes	client_key = NULL;	/* owned by the kex */
87218767Sdes
88218767Sdes	debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
89295367Sdes	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_ecdh_reply);
90295367Sdes	r = 0;
91295367Sdes out:
92295367Sdes	if (client_key)
93295367Sdes		EC_KEY_free(client_key);
94295367Sdes	return r;
95295367Sdes}
96218767Sdes
97295367Sdesstatic int
98295367Sdesinput_kex_ecdh_reply(int type, u_int32_t seq, void *ctxt)
99295367Sdes{
100295367Sdes	struct ssh *ssh = ctxt;
101295367Sdes	struct kex *kex = ssh->kex;
102295367Sdes	const EC_GROUP *group;
103295367Sdes	EC_POINT *server_public = NULL;
104295367Sdes	EC_KEY *client_key;
105295367Sdes	BIGNUM *shared_secret = NULL;
106295367Sdes	struct sshkey *server_host_key = NULL;
107295367Sdes	u_char *server_host_key_blob = NULL, *signature = NULL;
108295367Sdes	u_char *kbuf = NULL;
109295367Sdes	u_char hash[SSH_DIGEST_MAX_LENGTH];
110295367Sdes	size_t slen, sbloblen;
111295367Sdes	size_t klen = 0, hashlen;
112295367Sdes	int r;
113295367Sdes
114295367Sdes	if (kex->verify_host_key == NULL) {
115295367Sdes		r = SSH_ERR_INVALID_ARGUMENT;
116295367Sdes		goto out;
117295367Sdes	}
118295367Sdes	group = kex->ec_group;
119295367Sdes	client_key = kex->ec_client_key;
120295367Sdes
121218767Sdes	/* hostkey */
122295367Sdes	if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
123295367Sdes	    &sbloblen)) != 0 ||
124295367Sdes	    (r = sshkey_from_blob(server_host_key_blob, sbloblen,
125295367Sdes	    &server_host_key)) != 0)
126295367Sdes		goto out;
127295367Sdes	if (server_host_key->type != kex->hostkey_type ||
128295367Sdes	    (kex->hostkey_type == KEY_ECDSA &&
129295367Sdes	    server_host_key->ecdsa_nid != kex->hostkey_nid)) {
130295367Sdes		r = SSH_ERR_KEY_TYPE_MISMATCH;
131295367Sdes		goto out;
132295367Sdes	}
133295367Sdes	if (kex->verify_host_key(server_host_key, ssh) == -1) {
134295367Sdes		r = SSH_ERR_SIGNATURE_INVALID;
135295367Sdes		goto out;
136295367Sdes	}
137218767Sdes
138218767Sdes	/* Q_S, server public key */
139295367Sdes	/* signed H */
140295367Sdes	if ((server_public = EC_POINT_new(group)) == NULL) {
141295367Sdes		r = SSH_ERR_ALLOC_FAIL;
142295367Sdes		goto out;
143295367Sdes	}
144295367Sdes	if ((r = sshpkt_get_ec(ssh, server_public, group)) != 0 ||
145295367Sdes	    (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
146295367Sdes	    (r = sshpkt_get_end(ssh)) != 0)
147295367Sdes		goto out;
148218767Sdes
149218767Sdes#ifdef DEBUG_KEXECDH
150218767Sdes	fputs("server public key:\n", stderr);
151295367Sdes	sshkey_dump_ec_point(group, server_public);
152218767Sdes#endif
153295367Sdes	if (sshkey_ec_validate_public(group, server_public) != 0) {
154295367Sdes		sshpkt_disconnect(ssh, "invalid server public key");
155295367Sdes		r = SSH_ERR_MESSAGE_INCOMPLETE;
156295367Sdes		goto out;
157295367Sdes	}
158218767Sdes
159218767Sdes	klen = (EC_GROUP_get_degree(group) + 7) / 8;
160295367Sdes	if ((kbuf = malloc(klen)) == NULL ||
161295367Sdes	    (shared_secret = BN_new()) == NULL) {
162295367Sdes		r = SSH_ERR_ALLOC_FAIL;
163295367Sdes		goto out;
164295367Sdes	}
165218767Sdes	if (ECDH_compute_key(kbuf, klen, server_public,
166295367Sdes	    client_key, NULL) != (int)klen ||
167295367Sdes	    BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
168295367Sdes		r = SSH_ERR_LIBCRYPTO_ERROR;
169295367Sdes		goto out;
170295367Sdes	}
171218767Sdes
172218767Sdes#ifdef DEBUG_KEXECDH
173218767Sdes	dump_digest("shared secret", kbuf, klen);
174218767Sdes#endif
175218767Sdes	/* calc and verify H */
176295367Sdes	hashlen = sizeof(hash);
177295367Sdes	if ((r = kex_ecdh_hash(
178262566Sdes	    kex->hash_alg,
179218767Sdes	    group,
180218767Sdes	    kex->client_version_string,
181218767Sdes	    kex->server_version_string,
182295367Sdes	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
183295367Sdes	    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,
188295367Sdes	    hash, &hashlen)) != 0)
189295367Sdes		goto out;
190218767Sdes
191295367Sdes	if ((r = sshkey_verify(server_host_key, signature, slen, hash,
192295367Sdes	    hashlen, ssh->compat)) != 0)
193295367Sdes		goto out;
194218767Sdes
195218767Sdes	/* save session id */
196218767Sdes	if (kex->session_id == NULL) {
197218767Sdes		kex->session_id_len = hashlen;
198295367Sdes		kex->session_id = malloc(kex->session_id_len);
199295367Sdes		if (kex->session_id == NULL) {
200295367Sdes			r = SSH_ERR_ALLOC_FAIL;
201295367Sdes			goto out;
202295367Sdes		}
203218767Sdes		memcpy(kex->session_id, hash, kex->session_id_len);
204218767Sdes	}
205218767Sdes
206295367Sdes	if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
207295367Sdes		r = kex_send_newkeys(ssh);
208295367Sdes out:
209295367Sdes	explicit_bzero(hash, sizeof(hash));
210295367Sdes	if (kex->ec_client_key) {
211295367Sdes		EC_KEY_free(kex->ec_client_key);
212295367Sdes		kex->ec_client_key = NULL;
213295367Sdes	}
214295367Sdes	if (server_public)
215295367Sdes		EC_POINT_clear_free(server_public);
216295367Sdes	if (kbuf) {
217295367Sdes		explicit_bzero(kbuf, klen);
218295367Sdes		free(kbuf);
219295367Sdes	}
220295367Sdes	if (shared_secret)
221295367Sdes		BN_clear_free(shared_secret);
222295367Sdes	sshkey_free(server_host_key);
223295367Sdes	free(server_host_key_blob);
224295367Sdes	free(signature);
225295367Sdes	return r;
226218767Sdes}
227295367Sdes#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
228295367Sdes
229