Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: kexecdh.c,v 1.10 2019/01/21 10:40:11 djm Exp $ */ |
2 | | /* |
3 | | * Copyright (c) 2010 Damien Miller. All rights reserved. |
4 | | * Copyright (c) 2019 Markus Friedl. 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 | | |
29 | | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
30 | | |
31 | | #include <sys/types.h> |
32 | | |
33 | | #include <stdio.h> |
34 | | #include <string.h> |
35 | | #include <signal.h> |
36 | | |
37 | | #include <openssl/ecdh.h> |
38 | | |
39 | | #include "sshkey.h" |
40 | | #include "kex.h" |
41 | | #include "sshbuf.h" |
42 | | #include "digest.h" |
43 | | #include "ssherr.h" |
44 | | |
45 | | static int |
46 | | kex_ecdh_dec_key_group(struct kex *, const struct sshbuf *, EC_KEY *key, |
47 | | const EC_GROUP *, struct sshbuf **); |
48 | | |
49 | | int |
50 | | kex_ecdh_keypair(struct kex *kex) |
51 | 0 | { |
52 | 0 | EC_KEY *client_key = NULL; |
53 | 0 | const EC_GROUP *group; |
54 | 0 | const EC_POINT *public_key; |
55 | 0 | struct sshbuf *buf = NULL; |
56 | 0 | int r; |
57 | |
|
58 | 0 | if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) { |
59 | 0 | r = SSH_ERR_ALLOC_FAIL; |
60 | 0 | goto out; |
61 | 0 | } |
62 | 0 | if (EC_KEY_generate_key(client_key) != 1) { |
63 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
64 | 0 | goto out; |
65 | 0 | } |
66 | 0 | group = EC_KEY_get0_group(client_key); |
67 | 0 | public_key = EC_KEY_get0_public_key(client_key); |
68 | |
|
69 | 0 | if ((buf = sshbuf_new()) == NULL) { |
70 | 0 | r = SSH_ERR_ALLOC_FAIL; |
71 | 0 | goto out; |
72 | 0 | } |
73 | 0 | if ((r = sshbuf_put_ec(buf, public_key, group)) != 0 || |
74 | 0 | (r = sshbuf_get_u32(buf, NULL)) != 0) |
75 | 0 | goto out; |
76 | | #ifdef DEBUG_KEXECDH |
77 | | fputs("client private key:\n", stderr); |
78 | | sshkey_dump_ec_key(client_key); |
79 | | #endif |
80 | 0 | kex->ec_client_key = client_key; |
81 | 0 | kex->ec_group = group; |
82 | 0 | client_key = NULL; /* owned by the kex */ |
83 | 0 | kex->client_pub = buf; |
84 | 0 | buf = NULL; |
85 | 0 | out: |
86 | 0 | EC_KEY_free(client_key); |
87 | 0 | sshbuf_free(buf); |
88 | 0 | return r; |
89 | 0 | } |
90 | | |
91 | | int |
92 | | kex_ecdh_enc(struct kex *kex, const struct sshbuf *client_blob, |
93 | | struct sshbuf **server_blobp, struct sshbuf **shared_secretp) |
94 | 0 | { |
95 | 0 | const EC_GROUP *group; |
96 | 0 | const EC_POINT *pub_key; |
97 | 0 | EC_KEY *server_key = NULL; |
98 | 0 | struct sshbuf *server_blob = NULL; |
99 | 0 | int r; |
100 | |
|
101 | 0 | *server_blobp = NULL; |
102 | 0 | *shared_secretp = NULL; |
103 | |
|
104 | 0 | if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) { |
105 | 0 | r = SSH_ERR_ALLOC_FAIL; |
106 | 0 | goto out; |
107 | 0 | } |
108 | 0 | if (EC_KEY_generate_key(server_key) != 1) { |
109 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
110 | 0 | goto out; |
111 | 0 | } |
112 | 0 | group = EC_KEY_get0_group(server_key); |
113 | |
|
114 | | #ifdef DEBUG_KEXECDH |
115 | | fputs("server private key:\n", stderr); |
116 | | sshkey_dump_ec_key(server_key); |
117 | | #endif |
118 | 0 | pub_key = EC_KEY_get0_public_key(server_key); |
119 | 0 | if ((server_blob = sshbuf_new()) == NULL) { |
120 | 0 | r = SSH_ERR_ALLOC_FAIL; |
121 | 0 | goto out; |
122 | 0 | } |
123 | 0 | if ((r = sshbuf_put_ec(server_blob, pub_key, group)) != 0 || |
124 | 0 | (r = sshbuf_get_u32(server_blob, NULL)) != 0) |
125 | 0 | goto out; |
126 | 0 | if ((r = kex_ecdh_dec_key_group(kex, client_blob, server_key, group, |
127 | 0 | shared_secretp)) != 0) |
128 | 0 | goto out; |
129 | 0 | *server_blobp = server_blob; |
130 | 0 | server_blob = NULL; |
131 | 0 | out: |
132 | 0 | EC_KEY_free(server_key); |
133 | 0 | sshbuf_free(server_blob); |
134 | 0 | return r; |
135 | 0 | } |
136 | | |
137 | | static int |
138 | | kex_ecdh_dec_key_group(struct kex *kex, const struct sshbuf *ec_blob, |
139 | | EC_KEY *key, const EC_GROUP *group, struct sshbuf **shared_secretp) |
140 | 0 | { |
141 | 0 | struct sshbuf *buf = NULL; |
142 | 0 | BIGNUM *shared_secret = NULL; |
143 | 0 | EC_POINT *dh_pub = NULL; |
144 | 0 | u_char *kbuf = NULL; |
145 | 0 | size_t klen = 0; |
146 | 0 | int r; |
147 | |
|
148 | 0 | *shared_secretp = NULL; |
149 | |
|
150 | 0 | if ((buf = sshbuf_new()) == NULL) { |
151 | 0 | r = SSH_ERR_ALLOC_FAIL; |
152 | 0 | goto out; |
153 | 0 | } |
154 | 0 | if ((r = sshbuf_put_stringb(buf, ec_blob)) != 0) |
155 | 0 | goto out; |
156 | 0 | if ((dh_pub = EC_POINT_new(group)) == NULL) { |
157 | 0 | r = SSH_ERR_ALLOC_FAIL; |
158 | 0 | goto out; |
159 | 0 | } |
160 | 0 | if ((r = sshbuf_get_ec(buf, dh_pub, group)) != 0) { |
161 | 0 | goto out; |
162 | 0 | } |
163 | 0 | sshbuf_reset(buf); |
164 | |
|
165 | | #ifdef DEBUG_KEXECDH |
166 | | fputs("public key:\n", stderr); |
167 | | sshkey_dump_ec_point(group, dh_pub); |
168 | | #endif |
169 | 0 | if (sshkey_ec_validate_public(group, dh_pub) != 0) { |
170 | 0 | r = SSH_ERR_MESSAGE_INCOMPLETE; |
171 | 0 | goto out; |
172 | 0 | } |
173 | 0 | klen = (EC_GROUP_get_degree(group) + 7) / 8; |
174 | 0 | if ((kbuf = malloc(klen)) == NULL || |
175 | 0 | (shared_secret = BN_new()) == NULL) { |
176 | 0 | r = SSH_ERR_ALLOC_FAIL; |
177 | 0 | goto out; |
178 | 0 | } |
179 | 0 | if (ECDH_compute_key(kbuf, klen, dh_pub, key, NULL) != (int)klen || |
180 | 0 | BN_bin2bn(kbuf, klen, shared_secret) == NULL) { |
181 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
182 | 0 | goto out; |
183 | 0 | } |
184 | | #ifdef DEBUG_KEXECDH |
185 | | dump_digest("shared secret", kbuf, klen); |
186 | | #endif |
187 | 0 | if ((r = sshbuf_put_bignum2(buf, shared_secret)) != 0) |
188 | 0 | goto out; |
189 | 0 | *shared_secretp = buf; |
190 | 0 | buf = NULL; |
191 | 0 | out: |
192 | 0 | EC_POINT_clear_free(dh_pub); |
193 | 0 | BN_clear_free(shared_secret); |
194 | 0 | freezero(kbuf, klen); |
195 | 0 | sshbuf_free(buf); |
196 | 0 | return r; |
197 | 0 | } |
198 | | |
199 | | int |
200 | | kex_ecdh_dec(struct kex *kex, const struct sshbuf *server_blob, |
201 | | struct sshbuf **shared_secretp) |
202 | 0 | { |
203 | 0 | int r; |
204 | |
|
205 | 0 | r = kex_ecdh_dec_key_group(kex, server_blob, kex->ec_client_key, |
206 | 0 | kex->ec_group, shared_secretp); |
207 | 0 | EC_KEY_free(kex->ec_client_key); |
208 | 0 | kex->ec_client_key = NULL; |
209 | 0 | return r; |
210 | 0 | } |
211 | | |
212 | | #else |
213 | | |
214 | | #include "ssherr.h" |
215 | | |
216 | | struct kex; |
217 | | struct sshbuf; |
218 | | struct sshkey; |
219 | | |
220 | | int |
221 | | kex_ecdh_keypair(struct kex *kex) |
222 | | { |
223 | | return SSH_ERR_SIGN_ALG_UNSUPPORTED; |
224 | | } |
225 | | |
226 | | int |
227 | | kex_ecdh_enc(struct kex *kex, const struct sshbuf *client_blob, |
228 | | struct sshbuf **server_blobp, struct sshbuf **shared_secretp) |
229 | | { |
230 | | return SSH_ERR_SIGN_ALG_UNSUPPORTED; |
231 | | } |
232 | | |
233 | | int |
234 | | kex_ecdh_dec(struct kex *kex, const struct sshbuf *server_blob, |
235 | | struct sshbuf **shared_secretp) |
236 | | { |
237 | | return SSH_ERR_SIGN_ALG_UNSUPPORTED; |
238 | | } |
239 | | #endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */ |