blob: 90220ce8205d8ead69d3f2efdcdd941d3c08e3cf [file] [log] [blame]
Adam Langleyd0592972015-03-30 14:49:51 -07001/* $OpenBSD: kexecdhc.c,v 1.10 2015/01/26 06:10:03 djm Exp $ */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08002/*
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 "includes.h"
28
Adam Langleyd0592972015-03-30 14:49:51 -070029#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
30
Greg Hartmanbd77cf72015-02-25 13:21:06 -080031#include <sys/types.h>
32
33#include <stdio.h>
34#include <string.h>
35#include <signal.h>
36
Adam Langleyd0592972015-03-30 14:49:51 -070037#include <openssl/ecdh.h>
38
39#include "sshkey.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080040#include "cipher.h"
Adam Langleyd0592972015-03-30 14:49:51 -070041#include "digest.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080042#include "kex.h"
43#include "log.h"
44#include "packet.h"
45#include "dh.h"
46#include "ssh2.h"
Adam Langleyd0592972015-03-30 14:49:51 -070047#include "dispatch.h"
48#include "compat.h"
49#include "ssherr.h"
50#include "sshbuf.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080051
Adam Langleyd0592972015-03-30 14:49:51 -070052static int input_kex_ecdh_reply(int, u_int32_t, void *);
Greg Hartmanbd77cf72015-02-25 13:21:06 -080053
Adam Langleyd0592972015-03-30 14:49:51 -070054int
55kexecdh_client(struct ssh *ssh)
Greg Hartmanbd77cf72015-02-25 13:21:06 -080056{
Adam Langleyd0592972015-03-30 14:49:51 -070057 struct kex *kex = ssh->kex;
58 EC_KEY *client_key = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080059 const EC_GROUP *group;
Adam Langleyd0592972015-03-30 14:49:51 -070060 const EC_POINT *public_key;
61 int r;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080062
Adam Langleyd0592972015-03-30 14:49:51 -070063 if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
64 r = SSH_ERR_ALLOC_FAIL;
65 goto out;
66 }
67 if (EC_KEY_generate_key(client_key) != 1) {
68 r = SSH_ERR_LIBCRYPTO_ERROR;
69 goto out;
70 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -080071 group = EC_KEY_get0_group(client_key);
Adam Langleyd0592972015-03-30 14:49:51 -070072 public_key = EC_KEY_get0_public_key(client_key);
Greg Hartmanbd77cf72015-02-25 13:21:06 -080073
Adam Langleyd0592972015-03-30 14:49:51 -070074 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
75 (r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
76 (r = sshpkt_send(ssh)) != 0)
77 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080078 debug("sending SSH2_MSG_KEX_ECDH_INIT");
79
80#ifdef DEBUG_KEXECDH
81 fputs("client private key:\n", stderr);
Adam Langleyd0592972015-03-30 14:49:51 -070082 sshkey_dump_ec_key(client_key);
Greg Hartmanbd77cf72015-02-25 13:21:06 -080083#endif
Adam Langleyd0592972015-03-30 14:49:51 -070084 kex->ec_client_key = client_key;
85 kex->ec_group = group;
86 client_key = NULL; /* owned by the kex */
Greg Hartmanbd77cf72015-02-25 13:21:06 -080087
88 debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
Adam Langleyd0592972015-03-30 14:49:51 -070089 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_ecdh_reply);
90 r = 0;
91 out:
92 if (client_key)
93 EC_KEY_free(client_key);
94 return r;
95}
96
97static int
98input_kex_ecdh_reply(int type, u_int32_t seq, void *ctxt)
99{
100 struct ssh *ssh = ctxt;
101 struct kex *kex = ssh->kex;
102 const EC_GROUP *group;
103 EC_POINT *server_public = NULL;
104 EC_KEY *client_key;
105 BIGNUM *shared_secret = NULL;
106 struct sshkey *server_host_key = NULL;
107 u_char *server_host_key_blob = NULL, *signature = NULL;
108 u_char *kbuf = NULL;
109 u_char hash[SSH_DIGEST_MAX_LENGTH];
110 size_t slen, sbloblen;
111 size_t klen = 0, hashlen;
112 int r;
113
114 if (kex->verify_host_key == NULL) {
115 r = SSH_ERR_INVALID_ARGUMENT;
116 goto out;
117 }
118 group = kex->ec_group;
119 client_key = kex->ec_client_key;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800120
121 /* hostkey */
Adam Langleyd0592972015-03-30 14:49:51 -0700122 if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
123 &sbloblen)) != 0 ||
124 (r = sshkey_from_blob(server_host_key_blob, sbloblen,
125 &server_host_key)) != 0)
126 goto out;
127 if (server_host_key->type != kex->hostkey_type ||
128 (kex->hostkey_type == KEY_ECDSA &&
129 server_host_key->ecdsa_nid != kex->hostkey_nid)) {
130 r = SSH_ERR_KEY_TYPE_MISMATCH;
131 goto out;
132 }
133 if (kex->verify_host_key(server_host_key, ssh) == -1) {
134 r = SSH_ERR_SIGNATURE_INVALID;
135 goto out;
136 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800137
138 /* Q_S, server public key */
Adam Langleyd0592972015-03-30 14:49:51 -0700139 /* signed H */
140 if ((server_public = EC_POINT_new(group)) == NULL) {
141 r = SSH_ERR_ALLOC_FAIL;
142 goto out;
143 }
144 if ((r = sshpkt_get_ec(ssh, server_public, group)) != 0 ||
145 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
146 (r = sshpkt_get_end(ssh)) != 0)
147 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800148
149#ifdef DEBUG_KEXECDH
150 fputs("server public key:\n", stderr);
Adam Langleyd0592972015-03-30 14:49:51 -0700151 sshkey_dump_ec_point(group, server_public);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800152#endif
Adam Langleyd0592972015-03-30 14:49:51 -0700153 if (sshkey_ec_validate_public(group, server_public) != 0) {
154 sshpkt_disconnect(ssh, "invalid server public key");
155 r = SSH_ERR_MESSAGE_INCOMPLETE;
156 goto out;
157 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800158
159 klen = (EC_GROUP_get_degree(group) + 7) / 8;
Adam Langleyd0592972015-03-30 14:49:51 -0700160 if ((kbuf = malloc(klen)) == NULL ||
161 (shared_secret = BN_new()) == NULL) {
162 r = SSH_ERR_ALLOC_FAIL;
163 goto out;
164 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800165 if (ECDH_compute_key(kbuf, klen, server_public,
Adam Langleyd0592972015-03-30 14:49:51 -0700166 client_key, NULL) != (int)klen ||
167 BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
168 r = SSH_ERR_LIBCRYPTO_ERROR;
169 goto out;
170 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800171
172#ifdef DEBUG_KEXECDH
173 dump_digest("shared secret", kbuf, klen);
174#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800175 /* calc and verify H */
Adam Langleyd0592972015-03-30 14:49:51 -0700176 hashlen = sizeof(hash);
177 if ((r = kex_ecdh_hash(
178 kex->hash_alg,
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800179 group,
180 kex->client_version_string,
181 kex->server_version_string,
Adam Langleyd0592972015-03-30 14:49:51 -0700182 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
183 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800184 server_host_key_blob, sbloblen,
185 EC_KEY_get0_public_key(client_key),
186 server_public,
187 shared_secret,
Adam Langleyd0592972015-03-30 14:49:51 -0700188 hash, &hashlen)) != 0)
189 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800190
Adam Langleyd0592972015-03-30 14:49:51 -0700191 if ((r = sshkey_verify(server_host_key, signature, slen, hash,
192 hashlen, ssh->compat)) != 0)
193 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800194
195 /* save session id */
196 if (kex->session_id == NULL) {
197 kex->session_id_len = hashlen;
Adam Langleyd0592972015-03-30 14:49:51 -0700198 kex->session_id = malloc(kex->session_id_len);
199 if (kex->session_id == NULL) {
200 r = SSH_ERR_ALLOC_FAIL;
201 goto out;
202 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800203 memcpy(kex->session_id, hash, kex->session_id_len);
204 }
205
Adam Langleyd0592972015-03-30 14:49:51 -0700206 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
207 r = kex_send_newkeys(ssh);
208 out:
209 explicit_bzero(hash, sizeof(hash));
210 if (kex->ec_client_key) {
211 EC_KEY_free(kex->ec_client_key);
212 kex->ec_client_key = NULL;
213 }
214 if (server_public)
215 EC_POINT_clear_free(server_public);
216 if (kbuf) {
217 explicit_bzero(kbuf, klen);
218 free(kbuf);
219 }
220 if (shared_secret)
221 BN_clear_free(shared_secret);
222 sshkey_free(server_host_key);
223 free(server_host_key_blob);
224 free(signature);
225 return r;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800226}
Adam Langleyd0592972015-03-30 14:49:51 -0700227#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
228