diff options
author | Damien Miller <djm@mindrot.org> | 2010-08-31 22:41:14 +1000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2010-08-31 22:41:14 +1000 |
commit | eb8b60e320cdade9f4c07e2abacfb92c52e01348 (patch) | |
tree | 4e5bc25790566402e5b7ae00cefd2c57e867ef09 /kexecdhc.c | |
parent | da108ece6843f1268aa36d7c8ed0030dc53acd15 (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 'kexecdhc.c')
-rw-r--r-- | kexecdhc.c | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/kexecdhc.c b/kexecdhc.c new file mode 100644 index 000000000..f6d9977c5 --- /dev/null +++ b/kexecdhc.c | |||
@@ -0,0 +1,156 @@ | |||
1 | /* $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 | |||
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 "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 | |||
45 | void | ||
46 | kexecdh_client(Kex *kex) | ||
47 | { | ||
48 | EC_KEY *client_key; | ||
49 | EC_POINT *server_public; | ||
50 | const EC_GROUP *group; | ||
51 | BIGNUM *shared_secret; | ||
52 | Key *server_host_key; | ||
53 | u_char *server_host_key_blob = NULL, *signature = NULL; | ||
54 | u_char *kbuf, *hash; | ||
55 | u_int klen, slen, sbloblen, hashlen; | ||
56 | int curve_nid; | ||
57 | |||
58 | curve_nid = kex_ecdh_name_to_nid(kex->name); | ||
59 | if ((client_key = EC_KEY_new_by_curve_name(curve_nid)) == NULL) | ||
60 | fatal("%s: EC_KEY_new_by_curve_name failed", __func__); | ||
61 | if (EC_KEY_generate_key(client_key) != 1) | ||
62 | fatal("%s: EC_KEY_generate_key failed", __func__); | ||
63 | group = EC_KEY_get0_group(client_key); | ||
64 | |||
65 | packet_start(SSH2_MSG_KEX_ECDH_INIT); | ||
66 | packet_put_ecpoint(group, EC_KEY_get0_public_key(client_key)); | ||
67 | packet_send(); | ||
68 | debug("sending SSH2_MSG_KEX_ECDH_INIT"); | ||
69 | |||
70 | #ifdef DEBUG_KEXECDH | ||
71 | fputs("client private key:\n", stderr); | ||
72 | key_dump_ec_key(client_key); | ||
73 | #endif | ||
74 | |||
75 | debug("expecting SSH2_MSG_KEX_ECDH_REPLY"); | ||
76 | packet_read_expect(SSH2_MSG_KEX_ECDH_REPLY); | ||
77 | |||
78 | /* hostkey */ | ||
79 | server_host_key_blob = packet_get_string(&sbloblen); | ||
80 | server_host_key = key_from_blob(server_host_key_blob, sbloblen); | ||
81 | if (server_host_key == NULL) | ||
82 | fatal("cannot decode server_host_key_blob"); | ||
83 | if (server_host_key->type != kex->hostkey_type) | ||
84 | fatal("type mismatch for decoded server_host_key_blob"); | ||
85 | if (kex->verify_host_key == NULL) | ||
86 | fatal("cannot verify server_host_key"); | ||
87 | if (kex->verify_host_key(server_host_key) == -1) | ||
88 | fatal("server_host_key verification failed"); | ||
89 | |||
90 | /* Q_S, server public key */ | ||
91 | if ((server_public = EC_POINT_new(group)) == NULL) | ||
92 | fatal("%s: EC_POINT_new failed", __func__); | ||
93 | packet_get_ecpoint(group, server_public); | ||
94 | |||
95 | if (key_ec_validate_public(group, server_public) != 0) | ||
96 | fatal("%s: invalid server public key", __func__); | ||
97 | |||
98 | #ifdef DEBUG_KEXECDH | ||
99 | fputs("server public key:\n", stderr); | ||
100 | key_dump_ec_point(group, server_public); | ||
101 | #endif | ||
102 | |||
103 | /* signed H */ | ||
104 | signature = packet_get_string(&slen); | ||
105 | packet_check_eom(); | ||
106 | |||
107 | klen = (EC_GROUP_get_degree(group) + 7) / 8; | ||
108 | kbuf = xmalloc(klen); | ||
109 | if (ECDH_compute_key(kbuf, klen, server_public, | ||
110 | client_key, NULL) != (int)klen) | ||
111 | fatal("%s: ECDH_compute_key failed", __func__); | ||
112 | |||
113 | #ifdef DEBUG_KEXECDH | ||
114 | dump_digest("shared secret", kbuf, klen); | ||
115 | #endif | ||
116 | if ((shared_secret = BN_new()) == NULL) | ||
117 | fatal("%s: BN_new failed", __func__); | ||
118 | if (BN_bin2bn(kbuf, klen, shared_secret) == NULL) | ||
119 | fatal("%s: BN_bin2bn failed", __func__); | ||
120 | memset(kbuf, 0, klen); | ||
121 | xfree(kbuf); | ||
122 | |||
123 | /* calc and verify H */ | ||
124 | kex_ecdh_hash( | ||
125 | kex->evp_md, | ||
126 | group, | ||
127 | kex->client_version_string, | ||
128 | kex->server_version_string, | ||
129 | buffer_ptr(&kex->my), buffer_len(&kex->my), | ||
130 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | ||
131 | server_host_key_blob, sbloblen, | ||
132 | EC_KEY_get0_public_key(client_key), | ||
133 | server_public, | ||
134 | shared_secret, | ||
135 | &hash, &hashlen | ||
136 | ); | ||
137 | xfree(server_host_key_blob); | ||
138 | EC_POINT_clear_free(server_public); | ||
139 | EC_KEY_free(client_key); | ||
140 | |||
141 | if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1) | ||
142 | fatal("key_verify failed for server_host_key"); | ||
143 | key_free(server_host_key); | ||
144 | xfree(signature); | ||
145 | |||
146 | /* save session id */ | ||
147 | if (kex->session_id == NULL) { | ||
148 | kex->session_id_len = hashlen; | ||
149 | kex->session_id = xmalloc(kex->session_id_len); | ||
150 | memcpy(kex->session_id, hash, kex->session_id_len); | ||
151 | } | ||
152 | |||
153 | kex_derive_keys(kex, hash, hashlen, shared_secret); | ||
154 | BN_clear_free(shared_secret); | ||
155 | kex_finish(kex); | ||
156 | } | ||