GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/ssh/lib/../kexecdhc.c Lines: 0 90 0.0 %
Date: 2017-11-13 Branches: 0 62 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: kexecdhc.c,v 1.11 2017/05/30 14:23:52 markus Exp $ */
2
/*
3
 * Copyright (c) 2001 Markus Friedl.  All rights reserved.
4
 * Copyright (c) 2010 Damien Miller.  All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 */
26
27
#include <sys/types.h>
28
29
#include <stdio.h>
30
#include <string.h>
31
#include <signal.h>
32
33
#include <openssl/ecdh.h>
34
35
#include "sshkey.h"
36
#include "cipher.h"
37
#include "digest.h"
38
#include "kex.h"
39
#include "log.h"
40
#include "packet.h"
41
#include "dh.h"
42
#include "ssh2.h"
43
#include "dispatch.h"
44
#include "compat.h"
45
#include "ssherr.h"
46
#include "sshbuf.h"
47
48
static int input_kex_ecdh_reply(int, u_int32_t, struct ssh *);
49
50
int
51
kexecdh_client(struct ssh *ssh)
52
{
53
	struct kex *kex = ssh->kex;
54
	EC_KEY *client_key = NULL;
55
	const EC_GROUP *group;
56
	const EC_POINT *public_key;
57
	int r;
58
59
	if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
60
		r = SSH_ERR_ALLOC_FAIL;
61
		goto out;
62
	}
63
	if (EC_KEY_generate_key(client_key) != 1) {
64
		r = SSH_ERR_LIBCRYPTO_ERROR;
65
		goto out;
66
	}
67
	group = EC_KEY_get0_group(client_key);
68
	public_key = EC_KEY_get0_public_key(client_key);
69
70
	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
71
	    (r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
72
	    (r = sshpkt_send(ssh)) != 0)
73
		goto out;
74
	debug("sending SSH2_MSG_KEX_ECDH_INIT");
75
76
#ifdef DEBUG_KEXECDH
77
	fputs("client private key:\n", stderr);
78
	sshkey_dump_ec_key(client_key);
79
#endif
80
	kex->ec_client_key = client_key;
81
	kex->ec_group = group;
82
	client_key = NULL;	/* owned by the kex */
83
84
	debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
85
	ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_ecdh_reply);
86
	r = 0;
87
 out:
88
	if (client_key)
89
		EC_KEY_free(client_key);
90
	return r;
91
}
92
93
static int
94
input_kex_ecdh_reply(int type, u_int32_t seq, struct ssh *ssh)
95
{
96
	struct kex *kex = ssh->kex;
97
	const EC_GROUP *group;
98
	EC_POINT *server_public = NULL;
99
	EC_KEY *client_key;
100
	BIGNUM *shared_secret = NULL;
101
	struct sshkey *server_host_key = NULL;
102
	u_char *server_host_key_blob = NULL, *signature = NULL;
103
	u_char *kbuf = NULL;
104
	u_char hash[SSH_DIGEST_MAX_LENGTH];
105
	size_t slen, sbloblen;
106
	size_t klen = 0, hashlen;
107
	int r;
108
109
	if (kex->verify_host_key == NULL) {
110
		r = SSH_ERR_INVALID_ARGUMENT;
111
		goto out;
112
	}
113
	group = kex->ec_group;
114
	client_key = kex->ec_client_key;
115
116
	/* hostkey */
117
	if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
118
	    &sbloblen)) != 0 ||
119
	    (r = sshkey_from_blob(server_host_key_blob, sbloblen,
120
	    &server_host_key)) != 0)
121
		goto out;
122
	if (server_host_key->type != kex->hostkey_type ||
123
	    (kex->hostkey_type == KEY_ECDSA &&
124
	    server_host_key->ecdsa_nid != kex->hostkey_nid)) {
125
		r = SSH_ERR_KEY_TYPE_MISMATCH;
126
		goto out;
127
	}
128
	if (kex->verify_host_key(server_host_key, ssh) == -1) {
129
		r = SSH_ERR_SIGNATURE_INVALID;
130
		goto out;
131
	}
132
133
	/* Q_S, server public key */
134
	/* signed H */
135
	if ((server_public = EC_POINT_new(group)) == NULL) {
136
		r = SSH_ERR_ALLOC_FAIL;
137
		goto out;
138
	}
139
	if ((r = sshpkt_get_ec(ssh, server_public, group)) != 0 ||
140
	    (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
141
	    (r = sshpkt_get_end(ssh)) != 0)
142
		goto out;
143
144
#ifdef DEBUG_KEXECDH
145
	fputs("server public key:\n", stderr);
146
	sshkey_dump_ec_point(group, server_public);
147
#endif
148
	if (sshkey_ec_validate_public(group, server_public) != 0) {
149
		sshpkt_disconnect(ssh, "invalid server public key");
150
		r = SSH_ERR_MESSAGE_INCOMPLETE;
151
		goto out;
152
	}
153
154
	klen = (EC_GROUP_get_degree(group) + 7) / 8;
155
	if ((kbuf = malloc(klen)) == NULL ||
156
	    (shared_secret = BN_new()) == NULL) {
157
		r = SSH_ERR_ALLOC_FAIL;
158
		goto out;
159
	}
160
	if (ECDH_compute_key(kbuf, klen, server_public,
161
	    client_key, NULL) != (int)klen ||
162
	    BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
163
		r = SSH_ERR_LIBCRYPTO_ERROR;
164
		goto out;
165
	}
166
167
#ifdef DEBUG_KEXECDH
168
	dump_digest("shared secret", kbuf, klen);
169
#endif
170
	/* calc and verify H */
171
	hashlen = sizeof(hash);
172
	if ((r = kex_ecdh_hash(
173
	    kex->hash_alg,
174
	    group,
175
	    kex->client_version_string,
176
	    kex->server_version_string,
177
	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
178
	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
179
	    server_host_key_blob, sbloblen,
180
	    EC_KEY_get0_public_key(client_key),
181
	    server_public,
182
	    shared_secret,
183
	    hash, &hashlen)) != 0)
184
		goto out;
185
186
	if ((r = sshkey_verify(server_host_key, signature, slen, hash,
187
	    hashlen, ssh->compat)) != 0)
188
		goto out;
189
190
	/* save session id */
191
	if (kex->session_id == NULL) {
192
		kex->session_id_len = hashlen;
193
		kex->session_id = malloc(kex->session_id_len);
194
		if (kex->session_id == NULL) {
195
			r = SSH_ERR_ALLOC_FAIL;
196
			goto out;
197
		}
198
		memcpy(kex->session_id, hash, kex->session_id_len);
199
	}
200
201
	if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
202
		r = kex_send_newkeys(ssh);
203
 out:
204
	explicit_bzero(hash, sizeof(hash));
205
	if (kex->ec_client_key) {
206
		EC_KEY_free(kex->ec_client_key);
207
		kex->ec_client_key = NULL;
208
	}
209
	if (server_public)
210
		EC_POINT_clear_free(server_public);
211
	if (kbuf) {
212
		explicit_bzero(kbuf, klen);
213
		free(kbuf);
214
	}
215
	if (shared_secret)
216
		BN_clear_free(shared_secret);
217
	sshkey_free(server_host_key);
218
	free(server_host_key_blob);
219
	free(signature);
220
	return r;
221
}