Line | Count | Source |
1 | | /* $OpenBSD: ssh-ecdsa.c,v 1.30 2026/10/30 03:22:47 djm Exp $ */ |
2 | | /* |
3 | | * Copyright (c) 2000 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 "includes.h" |
28 | | |
29 | | #if defined(WITH_OPENSSL) |
30 | | #include "openbsd-compat/openssl-compat.h" |
31 | | |
32 | | #include <sys/types.h> |
33 | | |
34 | | #include <openssl/bn.h> |
35 | | #include <openssl/ec.h> |
36 | | #include <openssl/ecdsa.h> |
37 | | #include <openssl/evp.h> |
38 | | |
39 | | #include <string.h> |
40 | | |
41 | | #include "sshbuf.h" |
42 | | #include "ssherr.h" |
43 | | #define SSHKEY_INTERNAL |
44 | | #include "sshkey.h" |
45 | | |
46 | | int |
47 | | sshkey_ecdsa_fixup_group(EVP_PKEY *k) |
48 | 0 | { |
49 | 0 | int nids[] = { |
50 | 0 | NID_X9_62_prime256v1, |
51 | 0 | NID_secp384r1, |
52 | 0 | NID_secp521r1, |
53 | 0 | -1 |
54 | 0 | }; |
55 | 0 | int nid = -1; |
56 | 0 | u_int i; |
57 | 0 | const EC_GROUP *g; |
58 | 0 | EC_KEY *ec = NULL; |
59 | 0 | EC_GROUP *eg = NULL; |
60 | |
|
61 | 0 | if ((ec = EVP_PKEY_get1_EC_KEY(k)) == NULL || |
62 | 0 | (g = EC_KEY_get0_group(ec)) == NULL) |
63 | 0 | goto out; |
64 | | /* |
65 | | * The group may be stored in a ASN.1 encoded private key in one of two |
66 | | * ways: as a "named group", which is reconstituted by ASN.1 object ID |
67 | | * or explicit group parameters encoded into the key blob. Only the |
68 | | * "named group" case sets the group NID for us, but we can figure |
69 | | * it out for the other case by comparing against all the groups that |
70 | | * are supported. |
71 | | */ |
72 | 0 | if ((nid = EC_GROUP_get_curve_name(g)) > 0) |
73 | 0 | goto out; |
74 | 0 | nid = -1; |
75 | 0 | for (i = 0; nids[i] != -1; i++) { |
76 | 0 | if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL) |
77 | 0 | goto out; |
78 | 0 | if (EC_GROUP_cmp(g, eg, NULL) == 0) |
79 | 0 | break; |
80 | 0 | EC_GROUP_free(eg); |
81 | 0 | eg = NULL; |
82 | 0 | } |
83 | 0 | if (nids[i] == -1) |
84 | 0 | goto out; |
85 | | |
86 | | /* Use the group with the NID attached */ |
87 | 0 | EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE); |
88 | 0 | if (EC_KEY_set_group(ec, eg) != 1 || |
89 | 0 | EVP_PKEY_set1_EC_KEY(k, ec) != 1) |
90 | 0 | goto out; |
91 | | /* success */ |
92 | 0 | nid = nids[i]; |
93 | 0 | out: |
94 | 0 | EC_KEY_free(ec); |
95 | 0 | EC_GROUP_free(eg); |
96 | 0 | return nid; |
97 | 0 | } |
98 | | |
99 | | static u_int |
100 | | ssh_ecdsa_size(const struct sshkey *key) |
101 | 0 | { |
102 | 0 | switch (key->ecdsa_nid) { |
103 | 0 | case NID_X9_62_prime256v1: |
104 | 0 | return 256; |
105 | 0 | case NID_secp384r1: |
106 | 0 | return 384; |
107 | 0 | case NID_secp521r1: |
108 | 0 | return 521; |
109 | 0 | default: |
110 | 0 | return 0; |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | | static void |
115 | | ssh_ecdsa_cleanup(struct sshkey *k) |
116 | 0 | { |
117 | 0 | EVP_PKEY_free(k->pkey); |
118 | 0 | k->pkey = NULL; |
119 | 0 | } |
120 | | |
121 | | static int |
122 | | ssh_ecdsa_equal(const struct sshkey *a, const struct sshkey *b) |
123 | 0 | { |
124 | 0 | if (a->pkey == NULL || b->pkey == NULL) |
125 | 0 | return 0; |
126 | 0 | return EVP_PKEY_cmp(a->pkey, b->pkey) == 1; |
127 | 0 | } |
128 | | |
129 | | static int |
130 | | ssh_ecdsa_serialize_public(const struct sshkey *key, struct sshbuf *b, |
131 | | enum sshkey_serialize_rep opts) |
132 | 0 | { |
133 | 0 | int r; |
134 | |
|
135 | 0 | if (key->pkey == NULL) |
136 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
137 | 0 | if ((r = sshbuf_put_cstring(b, |
138 | 0 | sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 || |
139 | 0 | (r = sshbuf_put_ec_pkey(b, key->pkey)) != 0) |
140 | 0 | return r; |
141 | | |
142 | 0 | return 0; |
143 | 0 | } |
144 | | |
145 | | static int |
146 | | ssh_ecdsa_serialize_private(const struct sshkey *key, struct sshbuf *b, |
147 | | enum sshkey_serialize_rep opts) |
148 | 0 | { |
149 | 0 | int r; |
150 | 0 | const BIGNUM *priv; |
151 | |
|
152 | 0 | if ((priv = EC_KEY_get0_private_key( |
153 | 0 | EVP_PKEY_get0_EC_KEY(key->pkey))) == NULL) |
154 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
155 | 0 | if (!sshkey_is_cert(key)) { |
156 | 0 | if ((r = ssh_ecdsa_serialize_public(key, b, opts)) != 0) |
157 | 0 | return r; |
158 | 0 | } |
159 | 0 | if ((r = sshbuf_put_bignum2(b, priv)) != 0) |
160 | 0 | return r; |
161 | 0 | return 0; |
162 | 0 | } |
163 | | |
164 | | static int |
165 | | ssh_ecdsa_generate(struct sshkey *k, int bits) |
166 | 0 | { |
167 | 0 | EVP_PKEY *res = NULL; |
168 | 0 | EVP_PKEY_CTX *ctx = NULL; |
169 | 0 | int ret = SSH_ERR_INTERNAL_ERROR; |
170 | |
|
171 | 0 | if ((k->ecdsa_nid = sshkey_ecdsa_bits_to_nid(bits)) == -1) |
172 | 0 | return SSH_ERR_KEY_LENGTH; |
173 | | |
174 | 0 | if ((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL) |
175 | 0 | return SSH_ERR_ALLOC_FAIL; |
176 | | |
177 | 0 | if (EVP_PKEY_keygen_init(ctx) <= 0 || |
178 | 0 | EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, k->ecdsa_nid) <= 0 || |
179 | 0 | EVP_PKEY_keygen(ctx, &res) <= 0) { |
180 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
181 | 0 | goto out; |
182 | 0 | } |
183 | | /* success */ |
184 | 0 | k->pkey = res; |
185 | 0 | res = NULL; |
186 | 0 | ret = 0; |
187 | 0 | out: |
188 | 0 | EVP_PKEY_free(res); |
189 | 0 | EVP_PKEY_CTX_free(ctx); |
190 | 0 | return ret; |
191 | 0 | } |
192 | | |
193 | | static int |
194 | | ssh_ecdsa_copy_public(const struct sshkey *from, struct sshkey *to) |
195 | 0 | { |
196 | 0 | const EC_KEY *ec_from; |
197 | 0 | EC_KEY *ec_to = NULL; |
198 | 0 | int ret = SSH_ERR_INTERNAL_ERROR; |
199 | |
|
200 | 0 | ec_from = EVP_PKEY_get0_EC_KEY(from->pkey); |
201 | 0 | if (ec_from == NULL) |
202 | 0 | return SSH_ERR_LIBCRYPTO_ERROR; |
203 | | |
204 | 0 | to->ecdsa_nid = from->ecdsa_nid; |
205 | 0 | if ((ec_to = EC_KEY_new_by_curve_name(from->ecdsa_nid)) == NULL) |
206 | 0 | return SSH_ERR_ALLOC_FAIL; |
207 | 0 | if (EC_KEY_set_public_key(ec_to, |
208 | 0 | EC_KEY_get0_public_key(ec_from)) != 1) { |
209 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
210 | 0 | goto out; |
211 | 0 | } |
212 | 0 | EVP_PKEY_free(to->pkey); |
213 | 0 | if ((to->pkey = EVP_PKEY_new()) == NULL) { |
214 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
215 | 0 | goto out; |
216 | 0 | } |
217 | 0 | if (EVP_PKEY_set1_EC_KEY(to->pkey, ec_to) != 1) { |
218 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
219 | 0 | goto out; |
220 | 0 | } |
221 | 0 | ret = 0; |
222 | 0 | out: |
223 | 0 | EC_KEY_free(ec_to); |
224 | 0 | return ret; |
225 | 0 | } |
226 | | |
227 | | static int |
228 | | ssh_ecdsa_deserialize_public(const char *ktype, struct sshbuf *b, |
229 | | struct sshkey *key) |
230 | 0 | { |
231 | 0 | int r; |
232 | 0 | char *curve = NULL; |
233 | 0 | EVP_PKEY *pkey = NULL; |
234 | 0 | EC_KEY *ec = NULL; |
235 | |
|
236 | 0 | if ((key->ecdsa_nid = sshkey_ecdsa_nid_from_name(ktype)) == -1) |
237 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
238 | 0 | if ((r = sshbuf_get_cstring(b, &curve, NULL)) != 0) |
239 | 0 | goto out; |
240 | 0 | if (key->ecdsa_nid != sshkey_curve_name_to_nid(curve)) { |
241 | 0 | r = SSH_ERR_EC_CURVE_MISMATCH; |
242 | 0 | goto out; |
243 | 0 | } |
244 | 0 | if ((ec = EC_KEY_new_by_curve_name(key->ecdsa_nid)) == NULL) { |
245 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
246 | 0 | goto out; |
247 | 0 | } |
248 | 0 | if ((r = sshbuf_get_eckey(b, ec)) != 0) |
249 | 0 | goto out; |
250 | 0 | if (sshkey_ec_validate_public(EC_KEY_get0_group(ec), |
251 | 0 | EC_KEY_get0_public_key(ec)) != 0) { |
252 | 0 | r = SSH_ERR_KEY_INVALID_EC_VALUE; |
253 | 0 | goto out; |
254 | 0 | } |
255 | 0 | if ((pkey = EVP_PKEY_new()) == NULL) { |
256 | 0 | r = SSH_ERR_ALLOC_FAIL; |
257 | 0 | goto out; |
258 | 0 | } |
259 | 0 | if (EVP_PKEY_set1_EC_KEY(pkey, ec) != 1) { |
260 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
261 | 0 | goto out; |
262 | 0 | } |
263 | 0 | EVP_PKEY_free(key->pkey); |
264 | 0 | key->pkey = pkey; |
265 | 0 | pkey = NULL; |
266 | | /* success */ |
267 | 0 | r = 0; |
268 | | #ifdef DEBUG_PK |
269 | | sshkey_dump_ec_point( |
270 | | EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(key->pkey)), |
271 | | EC_KEY_get0_public_key(EVP_PKEY_get0_EC_KEY(key->pkey))); |
272 | | #endif |
273 | 0 | out: |
274 | 0 | EC_KEY_free(ec); |
275 | 0 | EVP_PKEY_free(pkey); |
276 | 0 | free(curve); |
277 | 0 | return r; |
278 | 0 | } |
279 | | |
280 | | static int |
281 | | ssh_ecdsa_deserialize_private(const char *ktype, struct sshbuf *b, |
282 | | struct sshkey *key) |
283 | 0 | { |
284 | 0 | int r; |
285 | 0 | BIGNUM *exponent = NULL; |
286 | 0 | EC_KEY *ec = NULL; |
287 | |
|
288 | 0 | if (!sshkey_is_cert(key)) { |
289 | 0 | if ((r = ssh_ecdsa_deserialize_public(ktype, b, key)) != 0) |
290 | 0 | return r; |
291 | 0 | } |
292 | 0 | if ((r = sshbuf_get_bignum2(b, &exponent)) != 0) |
293 | 0 | goto out; |
294 | 0 | if ((ec = EVP_PKEY_get1_EC_KEY(key->pkey)) == NULL) { |
295 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
296 | 0 | goto out; |
297 | 0 | } |
298 | 0 | if (EC_KEY_set_private_key(ec, exponent) != 1) { |
299 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
300 | 0 | goto out; |
301 | 0 | } |
302 | 0 | if ((r = sshkey_ec_validate_private(ec)) != 0) |
303 | 0 | goto out; |
304 | 0 | if (EVP_PKEY_set1_EC_KEY(key->pkey, ec) != 1) { |
305 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
306 | 0 | goto out; |
307 | 0 | } |
308 | | /* success */ |
309 | 0 | r = 0; |
310 | 0 | out: |
311 | 0 | BN_clear_free(exponent); |
312 | 0 | EC_KEY_free(ec); |
313 | 0 | return r; |
314 | 0 | } |
315 | | |
316 | | static int |
317 | | ssh_ecdsa_sign(struct sshkey *key, |
318 | | u_char **sigp, size_t *lenp, |
319 | | const u_char *data, size_t dlen, |
320 | | const char *alg, const char *sk_provider, const char *sk_pin, u_int compat) |
321 | 0 | { |
322 | 0 | ECDSA_SIG *esig = NULL; |
323 | 0 | unsigned char *sigb = NULL; |
324 | 0 | const unsigned char *psig; |
325 | 0 | const BIGNUM *sig_r, *sig_s; |
326 | 0 | int hash_alg; |
327 | 0 | size_t slen = 0; |
328 | 0 | int ret = SSH_ERR_INTERNAL_ERROR; |
329 | |
|
330 | 0 | if (lenp != NULL) |
331 | 0 | *lenp = 0; |
332 | 0 | if (sigp != NULL) |
333 | 0 | *sigp = NULL; |
334 | |
|
335 | 0 | if (key == NULL || key->pkey == NULL || |
336 | 0 | sshkey_type_plain(key->type) != KEY_ECDSA) |
337 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
338 | | |
339 | 0 | if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1) |
340 | 0 | return SSH_ERR_INTERNAL_ERROR; |
341 | | |
342 | 0 | if ((ret = sshkey_pkey_digest_sign(key->pkey, hash_alg, &sigb, &slen, |
343 | 0 | data, dlen)) != 0) |
344 | 0 | goto out; |
345 | | |
346 | 0 | psig = sigb; |
347 | 0 | if ((esig = d2i_ECDSA_SIG(NULL, &psig, slen)) == NULL) { |
348 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
349 | 0 | goto out; |
350 | 0 | } |
351 | 0 | ECDSA_SIG_get0(esig, &sig_r, &sig_s); |
352 | |
|
353 | 0 | if ((ret = ssh_ecdsa_encode_store_sig(key, sig_r, sig_s, |
354 | 0 | sigp, lenp)) != 0) |
355 | 0 | goto out; |
356 | | /* success */ |
357 | 0 | ret = 0; |
358 | 0 | out: |
359 | 0 | freezero(sigb, slen); |
360 | 0 | ECDSA_SIG_free(esig); |
361 | 0 | return ret; |
362 | 0 | } |
363 | | |
364 | | int |
365 | | ssh_ecdsa_encode_store_sig(const struct sshkey *key, |
366 | | const BIGNUM *sig_r, const BIGNUM *sig_s, |
367 | | u_char **sigp, size_t *lenp) |
368 | 0 | { |
369 | 0 | struct sshbuf *b = NULL, *bb = NULL; |
370 | 0 | int ret; |
371 | 0 | size_t len; |
372 | |
|
373 | 0 | if (lenp != NULL) |
374 | 0 | *lenp = 0; |
375 | 0 | if (sigp != NULL) |
376 | 0 | *sigp = NULL; |
377 | |
|
378 | 0 | if ((bb = sshbuf_new()) == NULL || (b = sshbuf_new()) == NULL) { |
379 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
380 | 0 | goto out; |
381 | 0 | } |
382 | 0 | if ((ret = sshbuf_put_bignum2(bb, sig_r)) != 0 || |
383 | 0 | (ret = sshbuf_put_bignum2(bb, sig_s)) != 0) |
384 | 0 | goto out; |
385 | 0 | if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 || |
386 | 0 | (ret = sshbuf_put_stringb(b, bb)) != 0) |
387 | 0 | goto out; |
388 | 0 | len = sshbuf_len(b); |
389 | 0 | if (sigp != NULL) { |
390 | 0 | if ((*sigp = malloc(len)) == NULL) { |
391 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
392 | 0 | goto out; |
393 | 0 | } |
394 | 0 | memcpy(*sigp, sshbuf_ptr(b), len); |
395 | 0 | } |
396 | 0 | if (lenp != NULL) |
397 | 0 | *lenp = len; |
398 | 0 | ret = 0; |
399 | 0 | out: |
400 | 0 | sshbuf_free(b); |
401 | 0 | sshbuf_free(bb); |
402 | 0 | return ret; |
403 | 0 | } |
404 | | |
405 | | static int |
406 | | ssh_ecdsa_verify(const struct sshkey *key, |
407 | | const u_char *sig, size_t siglen, |
408 | | const u_char *data, size_t dlen, const char *alg, u_int compat, |
409 | | struct sshkey_sig_details **detailsp) |
410 | 0 | { |
411 | 0 | ECDSA_SIG *esig = NULL; |
412 | 0 | BIGNUM *sig_r = NULL, *sig_s = NULL; |
413 | 0 | int hash_alg, len = 0; |
414 | 0 | int ret = SSH_ERR_INTERNAL_ERROR; |
415 | 0 | struct sshbuf *b = NULL, *sigbuf = NULL; |
416 | 0 | char *ktype = NULL; |
417 | 0 | unsigned char *sigb = NULL, *cp; |
418 | |
|
419 | 0 | if (key == NULL || key->pkey == NULL || |
420 | 0 | sshkey_type_plain(key->type) != KEY_ECDSA || |
421 | 0 | sig == NULL || siglen == 0) |
422 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
423 | | |
424 | 0 | if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1) |
425 | 0 | return SSH_ERR_INTERNAL_ERROR; |
426 | | |
427 | | /* fetch signature */ |
428 | 0 | if ((b = sshbuf_from(sig, siglen)) == NULL) |
429 | 0 | return SSH_ERR_ALLOC_FAIL; |
430 | 0 | if (sshbuf_get_cstring(b, &ktype, NULL) != 0 || |
431 | 0 | sshbuf_froms(b, &sigbuf) != 0) { |
432 | 0 | ret = SSH_ERR_INVALID_FORMAT; |
433 | 0 | goto out; |
434 | 0 | } |
435 | 0 | if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) { |
436 | 0 | ret = SSH_ERR_KEY_TYPE_MISMATCH; |
437 | 0 | goto out; |
438 | 0 | } |
439 | 0 | if (sshbuf_len(b) != 0) { |
440 | 0 | ret = SSH_ERR_UNEXPECTED_TRAILING_DATA; |
441 | 0 | goto out; |
442 | 0 | } |
443 | | |
444 | | /* parse signature */ |
445 | 0 | if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 || |
446 | 0 | sshbuf_get_bignum2(sigbuf, &sig_s) != 0) { |
447 | 0 | ret = SSH_ERR_INVALID_FORMAT; |
448 | 0 | goto out; |
449 | 0 | } |
450 | 0 | if (sshbuf_len(sigbuf) != 0) { |
451 | 0 | ret = SSH_ERR_UNEXPECTED_TRAILING_DATA; |
452 | 0 | goto out; |
453 | 0 | } |
454 | | |
455 | 0 | if ((esig = ECDSA_SIG_new()) == NULL) { |
456 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
457 | 0 | goto out; |
458 | 0 | } |
459 | 0 | if (!ECDSA_SIG_set0(esig, sig_r, sig_s)) { |
460 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
461 | 0 | goto out; |
462 | 0 | } |
463 | 0 | sig_r = sig_s = NULL; /* transferred */ |
464 | |
|
465 | 0 | if ((len = i2d_ECDSA_SIG(esig, NULL)) <= 0) { |
466 | 0 | len = 0; |
467 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
468 | 0 | goto out; |
469 | 0 | } |
470 | 0 | if ((sigb = calloc(1, len)) == NULL) { |
471 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
472 | 0 | goto out; |
473 | 0 | } |
474 | 0 | cp = sigb; /* ASN1_item_i2d increments the pointer past the object */ |
475 | 0 | if (i2d_ECDSA_SIG(esig, &cp) != len) { |
476 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
477 | 0 | goto out; |
478 | 0 | } |
479 | 0 | if ((ret = sshkey_pkey_digest_verify(key->pkey, hash_alg, |
480 | 0 | data, dlen, sigb, len)) != 0) |
481 | 0 | goto out; |
482 | | /* success */ |
483 | 0 | out: |
484 | 0 | freezero(sigb, len); |
485 | 0 | sshbuf_free(sigbuf); |
486 | 0 | sshbuf_free(b); |
487 | 0 | ECDSA_SIG_free(esig); |
488 | 0 | BN_clear_free(sig_r); |
489 | 0 | BN_clear_free(sig_s); |
490 | 0 | free(ktype); |
491 | 0 | return ret; |
492 | 0 | } |
493 | | |
494 | | /* NB. not static; used by ECDSA-SK */ |
495 | | const struct sshkey_impl_funcs sshkey_ecdsa_funcs = { |
496 | | /* .size = */ ssh_ecdsa_size, |
497 | | /* .alloc = */ NULL, |
498 | | /* .cleanup = */ ssh_ecdsa_cleanup, |
499 | | /* .equal = */ ssh_ecdsa_equal, |
500 | | /* .ssh_serialize_public = */ ssh_ecdsa_serialize_public, |
501 | | /* .ssh_deserialize_public = */ ssh_ecdsa_deserialize_public, |
502 | | /* .ssh_serialize_private = */ ssh_ecdsa_serialize_private, |
503 | | /* .ssh_deserialize_private = */ ssh_ecdsa_deserialize_private, |
504 | | /* .generate = */ ssh_ecdsa_generate, |
505 | | /* .copy_public = */ ssh_ecdsa_copy_public, |
506 | | /* .sign = */ ssh_ecdsa_sign, |
507 | | /* .verify = */ ssh_ecdsa_verify, |
508 | | }; |
509 | | |
510 | | const struct sshkey_impl sshkey_ecdsa_nistp256_impl = { |
511 | | /* .name = */ "ecdsa-sha2-nistp256", |
512 | | /* .shortname = */ "ECDSA", |
513 | | /* .sigalg = */ NULL, |
514 | | /* .type = */ KEY_ECDSA, |
515 | | /* .nid = */ NID_X9_62_prime256v1, |
516 | | /* .cert = */ 0, |
517 | | /* .sigonly = */ 0, |
518 | | /* .keybits = */ 0, |
519 | | /* .funcs = */ &sshkey_ecdsa_funcs, |
520 | | }; |
521 | | |
522 | | const struct sshkey_impl sshkey_ecdsa_nistp256_cert_impl = { |
523 | | /* .name = */ "ecdsa-sha2-nistp256-cert-v01@openssh.com", |
524 | | /* .shortname = */ "ECDSA-CERT", |
525 | | /* .sigalg = */ NULL, |
526 | | /* .type = */ KEY_ECDSA_CERT, |
527 | | /* .nid = */ NID_X9_62_prime256v1, |
528 | | /* .cert = */ 1, |
529 | | /* .sigonly = */ 0, |
530 | | /* .keybits = */ 0, |
531 | | /* .funcs = */ &sshkey_ecdsa_funcs, |
532 | | }; |
533 | | |
534 | | const struct sshkey_impl sshkey_ecdsa_nistp384_impl = { |
535 | | /* .name = */ "ecdsa-sha2-nistp384", |
536 | | /* .shortname = */ "ECDSA", |
537 | | /* .sigalg = */ NULL, |
538 | | /* .type = */ KEY_ECDSA, |
539 | | /* .nid = */ NID_secp384r1, |
540 | | /* .cert = */ 0, |
541 | | /* .sigonly = */ 0, |
542 | | /* .keybits = */ 0, |
543 | | /* .funcs = */ &sshkey_ecdsa_funcs, |
544 | | }; |
545 | | |
546 | | const struct sshkey_impl sshkey_ecdsa_nistp384_cert_impl = { |
547 | | /* .name = */ "ecdsa-sha2-nistp384-cert-v01@openssh.com", |
548 | | /* .shortname = */ "ECDSA-CERT", |
549 | | /* .sigalg = */ NULL, |
550 | | /* .type = */ KEY_ECDSA_CERT, |
551 | | /* .nid = */ NID_secp384r1, |
552 | | /* .cert = */ 1, |
553 | | /* .sigonly = */ 0, |
554 | | /* .keybits = */ 0, |
555 | | /* .funcs = */ &sshkey_ecdsa_funcs, |
556 | | }; |
557 | | |
558 | | const struct sshkey_impl sshkey_ecdsa_nistp521_impl = { |
559 | | /* .name = */ "ecdsa-sha2-nistp521", |
560 | | /* .shortname = */ "ECDSA", |
561 | | /* .sigalg = */ NULL, |
562 | | /* .type = */ KEY_ECDSA, |
563 | | /* .nid = */ NID_secp521r1, |
564 | | /* .cert = */ 0, |
565 | | /* .sigonly = */ 0, |
566 | | /* .keybits = */ 0, |
567 | | /* .funcs = */ &sshkey_ecdsa_funcs, |
568 | | }; |
569 | | |
570 | | const struct sshkey_impl sshkey_ecdsa_nistp521_cert_impl = { |
571 | | /* .name = */ "ecdsa-sha2-nistp521-cert-v01@openssh.com", |
572 | | /* .shortname = */ "ECDSA-CERT", |
573 | | /* .sigalg = */ NULL, |
574 | | /* .type = */ KEY_ECDSA_CERT, |
575 | | /* .nid = */ NID_secp521r1, |
576 | | /* .cert = */ 1, |
577 | | /* .sigonly = */ 0, |
578 | | /* .keybits = */ 0, |
579 | | /* .funcs = */ &sshkey_ecdsa_funcs, |
580 | | }; |
581 | | |
582 | | #endif /* WITH_OPENSSL */ |