summaryrefslogtreecommitdiff
path: root/kexecdhs.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2010-08-31 22:41:14 +1000
committerDamien Miller <djm@mindrot.org>2010-08-31 22:41:14 +1000
commiteb8b60e320cdade9f4c07e2abacfb92c52e01348 (patch)
tree4e5bc25790566402e5b7ae00cefd2c57e867ef09 /kexecdhs.c
parentda108ece6843f1268aa36d7c8ed0030dc53acd15 (diff)
- djm@cvs.openbsd.org 2010/08/31 11:54:45
[PROTOCOL PROTOCOL.agent PROTOCOL.certkeys auth2-jpake.c authfd.c] [authfile.c buffer.h dns.c kex.c kex.h key.c key.h monitor.c] [monitor_wrap.c myproposal.h packet.c packet.h pathnames.h readconf.c] [ssh-add.1 ssh-add.c ssh-agent.1 ssh-agent.c ssh-keygen.1 ssh-keygen.c] [ssh-keyscan.1 ssh-keyscan.c ssh-keysign.8 ssh.1 ssh.c ssh2.h] [ssh_config.5 sshconnect.c sshconnect2.c sshd.8 sshd.c sshd_config.5] [uuencode.c uuencode.h bufec.c kexecdh.c kexecdhc.c kexecdhs.c ssh-ecdsa.c] Implement Elliptic Curve Cryptography modes for key exchange (ECDH) and host/user keys (ECDSA) as specified by RFC5656. ECDH and ECDSA offer better performance than plain DH and DSA at the same equivalent symmetric key length, as well as much shorter keys. Only the mandatory sections of RFC5656 are implemented, specifically the three REQUIRED curves nistp256, nistp384 and nistp521 and only ECDH and ECDSA. Point compression (optional in RFC5656 is NOT implemented). Certificate host and user keys using the new ECDSA key types are supported. Note that this code has not been tested for interoperability and may be subject to change. feedback and ok markus@
Diffstat (limited to 'kexecdhs.c')
-rw-r--r--kexecdhs.c161
1 files changed, 161 insertions, 0 deletions
diff --git a/kexecdhs.c b/kexecdhs.c
new file mode 100644
index 000000000..d73333893
--- /dev/null
+++ b/kexecdhs.c
@@ -0,0 +1,161 @@
1/* $OpenBSD: kexecdhs.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
27#include <sys/types.h>
28#include <string.h>
29#include <signal.h>
30
31#include <openssl/ecdh.h>
32
33#include "xmalloc.h"
34#include "buffer.h"
35#include "key.h"
36#include "cipher.h"
37#include "kex.h"
38#include "log.h"
39#include "packet.h"
40#include "dh.h"
41#include "ssh2.h"
42#ifdef GSSAPI
43#include "ssh-gss.h"
44#endif
45#include "monitor_wrap.h"
46
47void
48kexecdh_server(Kex *kex)
49{
50 EC_POINT *client_public;
51 EC_KEY *server_key;
52 const EC_GROUP *group;
53 BIGNUM *shared_secret;
54 Key *server_host_private, *server_host_public;
55 u_char *server_host_key_blob = NULL, *signature = NULL;
56 u_char *kbuf, *hash;
57 u_int klen, slen, sbloblen, hashlen;
58 int curve_nid;
59
60 curve_nid = kex_ecdh_name_to_nid(kex->name);
61 if ((server_key = EC_KEY_new_by_curve_name(curve_nid)) == NULL)
62 fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
63 if (EC_KEY_generate_key(server_key) != 1)
64 fatal("%s: EC_KEY_generate_key failed", __func__);
65 group = EC_KEY_get0_group(server_key);
66
67#ifdef DEBUG_KEXECDH
68 fputs("server private key:\n", stderr);
69 key_dump_ec_key(server_key);
70#endif
71
72 if (kex->load_host_public_key == NULL ||
73 kex->load_host_private_key == NULL)
74 fatal("Cannot load hostkey");
75 server_host_public = kex->load_host_public_key(kex->hostkey_type);
76 if (server_host_public == NULL)
77 fatal("Unsupported hostkey type %d", kex->hostkey_type);
78 server_host_private = kex->load_host_private_key(kex->hostkey_type);
79 if (server_host_private == NULL)
80 fatal("Missing private key for hostkey type %d",
81 kex->hostkey_type);
82
83 debug("expecting SSH2_MSG_KEX_ECDH_INIT");
84 packet_read_expect(SSH2_MSG_KEX_ECDH_INIT);
85 if ((client_public = EC_POINT_new(group)) == NULL)
86 fatal("%s: EC_POINT_new failed", __func__);
87 packet_get_ecpoint(group, client_public);
88 packet_check_eom();
89
90 if (key_ec_validate_public(group, client_public) != 0)
91 fatal("%s: invalid client public key", __func__);
92
93#ifdef DEBUG_KEXECDH
94 fputs("client public key:\n", stderr);
95 key_dump_ec_point(group, client_public);
96#endif
97
98 /* Calculate shared_secret */
99 klen = (EC_GROUP_get_degree(group) + 7) / 8;
100 kbuf = xmalloc(klen);
101 if (ECDH_compute_key(kbuf, klen, client_public,
102 server_key, NULL) != (int)klen)
103 fatal("%s: ECDH_compute_key failed", __func__);
104
105#ifdef DEBUG_KEXDH
106 dump_digest("shared secret", kbuf, klen);
107#endif
108 if ((shared_secret = BN_new()) == NULL)
109 fatal("%s: BN_new failed", __func__);
110 if (BN_bin2bn(kbuf, klen, shared_secret) == NULL)
111 fatal("%s: BN_bin2bn failed", __func__);
112 memset(kbuf, 0, klen);
113 xfree(kbuf);
114
115 /* calc H */
116 key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
117 kex_ecdh_hash(
118 kex->evp_md,
119 group,
120 kex->client_version_string,
121 kex->server_version_string,
122 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
123 buffer_ptr(&kex->my), buffer_len(&kex->my),
124 server_host_key_blob, sbloblen,
125 client_public,
126 EC_KEY_get0_public_key(server_key),
127 shared_secret,
128 &hash, &hashlen
129 );
130 EC_POINT_clear_free(client_public);
131
132 /* save session id := H */
133 if (kex->session_id == NULL) {
134 kex->session_id_len = hashlen;
135 kex->session_id = xmalloc(kex->session_id_len);
136 memcpy(kex->session_id, hash, kex->session_id_len);
137 }
138
139 /* sign H */
140 if (PRIVSEP(key_sign(server_host_private, &signature, &slen,
141 hash, hashlen)) < 0)
142 fatal("kexdh_server: key_sign failed");
143
144 /* destroy_sensitive_data(); */
145
146 /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
147 packet_start(SSH2_MSG_KEX_ECDH_REPLY);
148 packet_put_string(server_host_key_blob, sbloblen);
149 packet_put_ecpoint(group, EC_KEY_get0_public_key(server_key));
150 packet_put_string(signature, slen);
151 packet_send();
152
153 xfree(signature);
154 xfree(server_host_key_blob);
155 /* have keys, free server key */
156 EC_KEY_free(server_key);
157
158 kex_derive_keys(kex, hash, hashlen, shared_secret);
159 BN_clear_free(shared_secret);
160 kex_finish(kex);
161}