diff options
Diffstat (limited to 'kexecdhc.c')
-rw-r--r-- | kexecdhc.c | 221 |
1 files changed, 142 insertions, 79 deletions
diff --git a/kexecdhc.c b/kexecdhc.c index 2f7629cca..90220ce82 100644 --- a/kexecdhc.c +++ b/kexecdhc.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: kexecdhc.c,v 1.7 2014/02/02 03:44:31 djm Exp $ */ | 1 | /* $OpenBSD: kexecdhc.c,v 1.10 2015/01/26 06:10:03 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2001 Markus Friedl. All rights reserved. | 3 | * Copyright (c) 2001 Markus Friedl. All rights reserved. |
4 | * Copyright (c) 2010 Damien Miller. All rights reserved. | 4 | * Copyright (c) 2010 Damien Miller. All rights reserved. |
@@ -26,140 +26,203 @@ | |||
26 | 26 | ||
27 | #include "includes.h" | 27 | #include "includes.h" |
28 | 28 | ||
29 | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) | ||
30 | |||
29 | #include <sys/types.h> | 31 | #include <sys/types.h> |
30 | 32 | ||
31 | #include <stdio.h> | 33 | #include <stdio.h> |
32 | #include <string.h> | 34 | #include <string.h> |
33 | #include <signal.h> | 35 | #include <signal.h> |
34 | 36 | ||
35 | #include "xmalloc.h" | 37 | #include <openssl/ecdh.h> |
36 | #include "buffer.h" | 38 | |
37 | #include "key.h" | 39 | #include "sshkey.h" |
38 | #include "cipher.h" | 40 | #include "cipher.h" |
41 | #include "digest.h" | ||
39 | #include "kex.h" | 42 | #include "kex.h" |
40 | #include "log.h" | 43 | #include "log.h" |
41 | #include "packet.h" | 44 | #include "packet.h" |
42 | #include "dh.h" | 45 | #include "dh.h" |
43 | #include "ssh2.h" | 46 | #include "ssh2.h" |
47 | #include "dispatch.h" | ||
48 | #include "compat.h" | ||
49 | #include "ssherr.h" | ||
50 | #include "sshbuf.h" | ||
44 | 51 | ||
45 | #ifdef OPENSSL_HAS_ECC | 52 | static int input_kex_ecdh_reply(int, u_int32_t, void *); |
46 | |||
47 | #include <openssl/ecdh.h> | ||
48 | 53 | ||
49 | void | 54 | int |
50 | kexecdh_client(Kex *kex) | 55 | kexecdh_client(struct ssh *ssh) |
51 | { | 56 | { |
52 | EC_KEY *client_key; | 57 | struct kex *kex = ssh->kex; |
53 | EC_POINT *server_public; | 58 | EC_KEY *client_key = NULL; |
54 | const EC_GROUP *group; | 59 | const EC_GROUP *group; |
55 | BIGNUM *shared_secret; | 60 | const EC_POINT *public_key; |
56 | Key *server_host_key; | 61 | int r; |
57 | u_char *server_host_key_blob = NULL, *signature = NULL; | ||
58 | u_char *kbuf, *hash; | ||
59 | u_int klen, slen, sbloblen, hashlen; | ||
60 | 62 | ||
61 | if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) | 63 | if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) { |
62 | fatal("%s: EC_KEY_new_by_curve_name failed", __func__); | 64 | r = SSH_ERR_ALLOC_FAIL; |
63 | if (EC_KEY_generate_key(client_key) != 1) | 65 | goto out; |
64 | fatal("%s: EC_KEY_generate_key failed", __func__); | 66 | } |
67 | if (EC_KEY_generate_key(client_key) != 1) { | ||
68 | r = SSH_ERR_LIBCRYPTO_ERROR; | ||
69 | goto out; | ||
70 | } | ||
65 | group = EC_KEY_get0_group(client_key); | 71 | group = EC_KEY_get0_group(client_key); |
72 | public_key = EC_KEY_get0_public_key(client_key); | ||
66 | 73 | ||
67 | packet_start(SSH2_MSG_KEX_ECDH_INIT); | 74 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 || |
68 | packet_put_ecpoint(group, EC_KEY_get0_public_key(client_key)); | 75 | (r = sshpkt_put_ec(ssh, public_key, group)) != 0 || |
69 | packet_send(); | 76 | (r = sshpkt_send(ssh)) != 0) |
77 | goto out; | ||
70 | debug("sending SSH2_MSG_KEX_ECDH_INIT"); | 78 | debug("sending SSH2_MSG_KEX_ECDH_INIT"); |
71 | 79 | ||
72 | #ifdef DEBUG_KEXECDH | 80 | #ifdef DEBUG_KEXECDH |
73 | fputs("client private key:\n", stderr); | 81 | fputs("client private key:\n", stderr); |
74 | key_dump_ec_key(client_key); | 82 | sshkey_dump_ec_key(client_key); |
75 | #endif | 83 | #endif |
84 | kex->ec_client_key = client_key; | ||
85 | kex->ec_group = group; | ||
86 | client_key = NULL; /* owned by the kex */ | ||
76 | 87 | ||
77 | debug("expecting SSH2_MSG_KEX_ECDH_REPLY"); | 88 | debug("expecting SSH2_MSG_KEX_ECDH_REPLY"); |
78 | packet_read_expect(SSH2_MSG_KEX_ECDH_REPLY); | 89 | 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 | |||
97 | static int | ||
98 | input_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; | ||
79 | 120 | ||
80 | /* hostkey */ | 121 | /* hostkey */ |
81 | server_host_key_blob = packet_get_string(&sbloblen); | 122 | if ((r = sshpkt_get_string(ssh, &server_host_key_blob, |
82 | server_host_key = key_from_blob(server_host_key_blob, sbloblen); | 123 | &sbloblen)) != 0 || |
83 | if (server_host_key == NULL) | 124 | (r = sshkey_from_blob(server_host_key_blob, sbloblen, |
84 | fatal("cannot decode server_host_key_blob"); | 125 | &server_host_key)) != 0) |
85 | if (server_host_key->type != kex->hostkey_type) | 126 | goto out; |
86 | fatal("type mismatch for decoded server_host_key_blob"); | 127 | if (server_host_key->type != kex->hostkey_type || |
87 | if (kex->verify_host_key == NULL) | 128 | (kex->hostkey_type == KEY_ECDSA && |
88 | fatal("cannot verify server_host_key"); | 129 | server_host_key->ecdsa_nid != kex->hostkey_nid)) { |
89 | if (kex->verify_host_key(server_host_key) == -1) | 130 | r = SSH_ERR_KEY_TYPE_MISMATCH; |
90 | fatal("server_host_key verification failed"); | 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 | } | ||
91 | 137 | ||
92 | /* Q_S, server public key */ | 138 | /* Q_S, server public key */ |
93 | if ((server_public = EC_POINT_new(group)) == NULL) | 139 | /* signed H */ |
94 | fatal("%s: EC_POINT_new failed", __func__); | 140 | if ((server_public = EC_POINT_new(group)) == NULL) { |
95 | packet_get_ecpoint(group, server_public); | 141 | r = SSH_ERR_ALLOC_FAIL; |
96 | 142 | goto out; | |
97 | if (key_ec_validate_public(group, server_public) != 0) | 143 | } |
98 | fatal("%s: invalid server public key", __func__); | 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; | ||
99 | 148 | ||
100 | #ifdef DEBUG_KEXECDH | 149 | #ifdef DEBUG_KEXECDH |
101 | fputs("server public key:\n", stderr); | 150 | fputs("server public key:\n", stderr); |
102 | key_dump_ec_point(group, server_public); | 151 | sshkey_dump_ec_point(group, server_public); |
103 | #endif | 152 | #endif |
104 | 153 | if (sshkey_ec_validate_public(group, server_public) != 0) { | |
105 | /* signed H */ | 154 | sshpkt_disconnect(ssh, "invalid server public key"); |
106 | signature = packet_get_string(&slen); | 155 | r = SSH_ERR_MESSAGE_INCOMPLETE; |
107 | packet_check_eom(); | 156 | goto out; |
157 | } | ||
108 | 158 | ||
109 | klen = (EC_GROUP_get_degree(group) + 7) / 8; | 159 | klen = (EC_GROUP_get_degree(group) + 7) / 8; |
110 | kbuf = xmalloc(klen); | 160 | if ((kbuf = malloc(klen)) == NULL || |
161 | (shared_secret = BN_new()) == NULL) { | ||
162 | r = SSH_ERR_ALLOC_FAIL; | ||
163 | goto out; | ||
164 | } | ||
111 | if (ECDH_compute_key(kbuf, klen, server_public, | 165 | if (ECDH_compute_key(kbuf, klen, server_public, |
112 | client_key, NULL) != (int)klen) | 166 | client_key, NULL) != (int)klen || |
113 | fatal("%s: ECDH_compute_key failed", __func__); | 167 | BN_bin2bn(kbuf, klen, shared_secret) == NULL) { |
168 | r = SSH_ERR_LIBCRYPTO_ERROR; | ||
169 | goto out; | ||
170 | } | ||
114 | 171 | ||
115 | #ifdef DEBUG_KEXECDH | 172 | #ifdef DEBUG_KEXECDH |
116 | dump_digest("shared secret", kbuf, klen); | 173 | dump_digest("shared secret", kbuf, klen); |
117 | #endif | 174 | #endif |
118 | if ((shared_secret = BN_new()) == NULL) | ||
119 | fatal("%s: BN_new failed", __func__); | ||
120 | if (BN_bin2bn(kbuf, klen, shared_secret) == NULL) | ||
121 | fatal("%s: BN_bin2bn failed", __func__); | ||
122 | explicit_bzero(kbuf, klen); | ||
123 | free(kbuf); | ||
124 | |||
125 | /* calc and verify H */ | 175 | /* calc and verify H */ |
126 | kex_ecdh_hash( | 176 | hashlen = sizeof(hash); |
177 | if ((r = kex_ecdh_hash( | ||
127 | kex->hash_alg, | 178 | kex->hash_alg, |
128 | group, | 179 | group, |
129 | kex->client_version_string, | 180 | kex->client_version_string, |
130 | kex->server_version_string, | 181 | kex->server_version_string, |
131 | buffer_ptr(&kex->my), buffer_len(&kex->my), | 182 | sshbuf_ptr(kex->my), sshbuf_len(kex->my), |
132 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | 183 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), |
133 | server_host_key_blob, sbloblen, | 184 | server_host_key_blob, sbloblen, |
134 | EC_KEY_get0_public_key(client_key), | 185 | EC_KEY_get0_public_key(client_key), |
135 | server_public, | 186 | server_public, |
136 | shared_secret, | 187 | shared_secret, |
137 | &hash, &hashlen | 188 | hash, &hashlen)) != 0) |
138 | ); | 189 | goto out; |
139 | free(server_host_key_blob); | ||
140 | EC_POINT_clear_free(server_public); | ||
141 | EC_KEY_free(client_key); | ||
142 | 190 | ||
143 | if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1) | 191 | if ((r = sshkey_verify(server_host_key, signature, slen, hash, |
144 | fatal("key_verify failed for server_host_key"); | 192 | hashlen, ssh->compat)) != 0) |
145 | key_free(server_host_key); | 193 | goto out; |
146 | free(signature); | ||
147 | 194 | ||
148 | /* save session id */ | 195 | /* save session id */ |
149 | if (kex->session_id == NULL) { | 196 | if (kex->session_id == NULL) { |
150 | kex->session_id_len = hashlen; | 197 | kex->session_id_len = hashlen; |
151 | kex->session_id = xmalloc(kex->session_id_len); | 198 | kex->session_id = malloc(kex->session_id_len); |
199 | if (kex->session_id == NULL) { | ||
200 | r = SSH_ERR_ALLOC_FAIL; | ||
201 | goto out; | ||
202 | } | ||
152 | memcpy(kex->session_id, hash, kex->session_id_len); | 203 | memcpy(kex->session_id, hash, kex->session_id_len); |
153 | } | 204 | } |
154 | 205 | ||
155 | kex_derive_keys_bn(kex, hash, hashlen, shared_secret); | 206 | if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) |
156 | BN_clear_free(shared_secret); | 207 | r = kex_send_newkeys(ssh); |
157 | kex_finish(kex); | 208 | out: |
158 | } | 209 | explicit_bzero(hash, sizeof(hash)); |
159 | #else /* OPENSSL_HAS_ECC */ | 210 | if (kex->ec_client_key) { |
160 | void | 211 | EC_KEY_free(kex->ec_client_key); |
161 | kexecdh_client(Kex *kex) | 212 | kex->ec_client_key = NULL; |
162 | { | 213 | } |
163 | fatal("ECC support is not enabled"); | 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; | ||
164 | } | 226 | } |
165 | #endif /* OPENSSL_HAS_ECC */ | 227 | #endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */ |
228 | |||