blob: 297a0e5a9d6958f601ac4f188347aed37a8d50dc [file] [log] [blame]
Damien Millereb8b60e2010-08-31 22:41:14 +10001/* $OpenBSD: kexecdhc.c,v 1.1 2010/08/31 11:54:45 djm 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
Damien Millerc79ff072010-08-31 22:50:48 +100027#include "includes.h"
28
Damien Millereb8b60e2010-08-31 22:41:14 +100029#include <sys/types.h>
30
31#include <stdio.h>
32#include <string.h>
33#include <signal.h>
34
Damien Millereb8b60e2010-08-31 22:41:14 +100035#include "xmalloc.h"
36#include "buffer.h"
37#include "key.h"
38#include "cipher.h"
39#include "kex.h"
40#include "log.h"
41#include "packet.h"
42#include "dh.h"
43#include "ssh2.h"
44
Damien Miller6af914a2010-09-10 11:39:26 +100045#ifdef OPENSSL_HAS_ECC
46
47#include <openssl/ecdh.h>
48
Damien Millereb8b60e2010-08-31 22:41:14 +100049void
50kexecdh_client(Kex *kex)
51{
52 EC_KEY *client_key;
53 EC_POINT *server_public;
54 const EC_GROUP *group;
55 BIGNUM *shared_secret;
56 Key *server_host_key;
57 u_char *server_host_key_blob = NULL, *signature = NULL;
58 u_char *kbuf, *hash;
59 u_int klen, slen, sbloblen, hashlen;
60 int curve_nid;
61
62 curve_nid = kex_ecdh_name_to_nid(kex->name);
63 if ((client_key = EC_KEY_new_by_curve_name(curve_nid)) == NULL)
64 fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
65 if (EC_KEY_generate_key(client_key) != 1)
66 fatal("%s: EC_KEY_generate_key failed", __func__);
67 group = EC_KEY_get0_group(client_key);
68
69 packet_start(SSH2_MSG_KEX_ECDH_INIT);
70 packet_put_ecpoint(group, EC_KEY_get0_public_key(client_key));
71 packet_send();
72 debug("sending SSH2_MSG_KEX_ECDH_INIT");
73
74#ifdef DEBUG_KEXECDH
75 fputs("client private key:\n", stderr);
76 key_dump_ec_key(client_key);
77#endif
78
79 debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
80 packet_read_expect(SSH2_MSG_KEX_ECDH_REPLY);
81
82 /* hostkey */
83 server_host_key_blob = packet_get_string(&sbloblen);
84 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
85 if (server_host_key == NULL)
86 fatal("cannot decode server_host_key_blob");
87 if (server_host_key->type != kex->hostkey_type)
88 fatal("type mismatch for decoded server_host_key_blob");
89 if (kex->verify_host_key == NULL)
90 fatal("cannot verify server_host_key");
91 if (kex->verify_host_key(server_host_key) == -1)
92 fatal("server_host_key verification failed");
93
94 /* Q_S, server public key */
95 if ((server_public = EC_POINT_new(group)) == NULL)
96 fatal("%s: EC_POINT_new failed", __func__);
97 packet_get_ecpoint(group, server_public);
98
99 if (key_ec_validate_public(group, server_public) != 0)
100 fatal("%s: invalid server public key", __func__);
101
102#ifdef DEBUG_KEXECDH
103 fputs("server public key:\n", stderr);
104 key_dump_ec_point(group, server_public);
105#endif
106
107 /* signed H */
108 signature = packet_get_string(&slen);
109 packet_check_eom();
110
111 klen = (EC_GROUP_get_degree(group) + 7) / 8;
112 kbuf = xmalloc(klen);
113 if (ECDH_compute_key(kbuf, klen, server_public,
114 client_key, NULL) != (int)klen)
115 fatal("%s: ECDH_compute_key failed", __func__);
116
117#ifdef DEBUG_KEXECDH
118 dump_digest("shared secret", kbuf, klen);
119#endif
120 if ((shared_secret = BN_new()) == NULL)
121 fatal("%s: BN_new failed", __func__);
122 if (BN_bin2bn(kbuf, klen, shared_secret) == NULL)
123 fatal("%s: BN_bin2bn failed", __func__);
124 memset(kbuf, 0, klen);
125 xfree(kbuf);
126
127 /* calc and verify H */
128 kex_ecdh_hash(
129 kex->evp_md,
130 group,
131 kex->client_version_string,
132 kex->server_version_string,
133 buffer_ptr(&kex->my), buffer_len(&kex->my),
134 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
135 server_host_key_blob, sbloblen,
136 EC_KEY_get0_public_key(client_key),
137 server_public,
138 shared_secret,
139 &hash, &hashlen
140 );
141 xfree(server_host_key_blob);
142 EC_POINT_clear_free(server_public);
143 EC_KEY_free(client_key);
144
145 if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
146 fatal("key_verify failed for server_host_key");
147 key_free(server_host_key);
148 xfree(signature);
149
150 /* save session id */
151 if (kex->session_id == NULL) {
152 kex->session_id_len = hashlen;
153 kex->session_id = xmalloc(kex->session_id_len);
154 memcpy(kex->session_id, hash, kex->session_id_len);
155 }
156
157 kex_derive_keys(kex, hash, hashlen, shared_secret);
158 BN_clear_free(shared_secret);
159 kex_finish(kex);
160}
Damien Miller6af914a2010-09-10 11:39:26 +1000161#else /* OPENSSL_HAS_ECC */
162void
163kexecdh_client(Kex *kex)
164{
165 fatal("ECC support is not enabled");
166}
167#endif /* OPENSSL_HAS_ECC */