1*4d3fc8b0SEd Maste /* $OpenBSD: ssh-ecdsa.c,v 1.26 2023/03/08 04:43:12 guenther Exp $ */
24a421b63SDag-Erling Smørgrav /*
34a421b63SDag-Erling Smørgrav * Copyright (c) 2000 Markus Friedl. All rights reserved.
44a421b63SDag-Erling Smørgrav * Copyright (c) 2010 Damien Miller. All rights reserved.
54a421b63SDag-Erling Smørgrav *
64a421b63SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without
74a421b63SDag-Erling Smørgrav * modification, are permitted provided that the following conditions
84a421b63SDag-Erling Smørgrav * are met:
94a421b63SDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright
104a421b63SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer.
114a421b63SDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright
124a421b63SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the
134a421b63SDag-Erling Smørgrav * documentation and/or other materials provided with the distribution.
144a421b63SDag-Erling Smørgrav *
154a421b63SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
164a421b63SDag-Erling Smørgrav * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
174a421b63SDag-Erling Smørgrav * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
184a421b63SDag-Erling Smørgrav * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
194a421b63SDag-Erling Smørgrav * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
204a421b63SDag-Erling Smørgrav * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
214a421b63SDag-Erling Smørgrav * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
224a421b63SDag-Erling Smørgrav * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
234a421b63SDag-Erling Smørgrav * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
244a421b63SDag-Erling Smørgrav * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
254a421b63SDag-Erling Smørgrav */
264a421b63SDag-Erling Smørgrav
274a421b63SDag-Erling Smørgrav #include "includes.h"
284a421b63SDag-Erling Smørgrav
29bc5531deSDag-Erling Smørgrav #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
304a421b63SDag-Erling Smørgrav
314a421b63SDag-Erling Smørgrav #include <sys/types.h>
324a421b63SDag-Erling Smørgrav
334a421b63SDag-Erling Smørgrav #include <openssl/bn.h>
344a421b63SDag-Erling Smørgrav #include <openssl/ec.h>
354a421b63SDag-Erling Smørgrav #include <openssl/ecdsa.h>
364a421b63SDag-Erling Smørgrav #include <openssl/evp.h>
374a421b63SDag-Erling Smørgrav
384a421b63SDag-Erling Smørgrav #include <string.h>
394a421b63SDag-Erling Smørgrav
40a0ee8cc6SDag-Erling Smørgrav #include "sshbuf.h"
41a0ee8cc6SDag-Erling Smørgrav #include "ssherr.h"
42f7167e0eSDag-Erling Smørgrav #include "digest.h"
43a0ee8cc6SDag-Erling Smørgrav #define SSHKEY_INTERNAL
44a0ee8cc6SDag-Erling Smørgrav #include "sshkey.h"
454a421b63SDag-Erling Smørgrav
462a01feabSEd Maste #include "openbsd-compat/openssl-compat.h"
472a01feabSEd Maste
48f374ba41SEd Maste static u_int
ssh_ecdsa_size(const struct sshkey * key)49f374ba41SEd Maste ssh_ecdsa_size(const struct sshkey *key)
504a421b63SDag-Erling Smørgrav {
51f374ba41SEd Maste switch (key->ecdsa_nid) {
52f374ba41SEd Maste case NID_X9_62_prime256v1:
53f374ba41SEd Maste return 256;
54f374ba41SEd Maste case NID_secp384r1:
55f374ba41SEd Maste return 384;
56f374ba41SEd Maste #ifdef OPENSSL_HAS_NISTP521
57f374ba41SEd Maste case NID_secp521r1:
58f374ba41SEd Maste return 521;
59f374ba41SEd Maste #endif
60f374ba41SEd Maste default:
61f374ba41SEd Maste return 0;
62f374ba41SEd Maste }
63f374ba41SEd Maste }
64f374ba41SEd Maste
65f374ba41SEd Maste static void
ssh_ecdsa_cleanup(struct sshkey * k)66f374ba41SEd Maste ssh_ecdsa_cleanup(struct sshkey *k)
67f374ba41SEd Maste {
68f374ba41SEd Maste EC_KEY_free(k->ecdsa);
69f374ba41SEd Maste k->ecdsa = NULL;
70f374ba41SEd Maste }
71f374ba41SEd Maste
72f374ba41SEd Maste static int
ssh_ecdsa_equal(const struct sshkey * a,const struct sshkey * b)73f374ba41SEd Maste ssh_ecdsa_equal(const struct sshkey *a, const struct sshkey *b)
74f374ba41SEd Maste {
75f374ba41SEd Maste const EC_GROUP *grp_a, *grp_b;
76f374ba41SEd Maste const EC_POINT *pub_a, *pub_b;
77f374ba41SEd Maste
78f374ba41SEd Maste if (a->ecdsa == NULL || b->ecdsa == NULL)
79f374ba41SEd Maste return 0;
80f374ba41SEd Maste if ((grp_a = EC_KEY_get0_group(a->ecdsa)) == NULL ||
81f374ba41SEd Maste (grp_b = EC_KEY_get0_group(b->ecdsa)) == NULL)
82f374ba41SEd Maste return 0;
83f374ba41SEd Maste if ((pub_a = EC_KEY_get0_public_key(a->ecdsa)) == NULL ||
84f374ba41SEd Maste (pub_b = EC_KEY_get0_public_key(b->ecdsa)) == NULL)
85f374ba41SEd Maste return 0;
86f374ba41SEd Maste if (EC_GROUP_cmp(grp_a, grp_b, NULL) != 0)
87f374ba41SEd Maste return 0;
88f374ba41SEd Maste if (EC_POINT_cmp(grp_a, pub_a, pub_b, NULL) != 0)
89f374ba41SEd Maste return 0;
90f374ba41SEd Maste
91f374ba41SEd Maste return 1;
92f374ba41SEd Maste }
93f374ba41SEd Maste
94f374ba41SEd Maste static int
ssh_ecdsa_serialize_public(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)95f374ba41SEd Maste ssh_ecdsa_serialize_public(const struct sshkey *key, struct sshbuf *b,
96f374ba41SEd Maste enum sshkey_serialize_rep opts)
97f374ba41SEd Maste {
98f374ba41SEd Maste int r;
99f374ba41SEd Maste
100f374ba41SEd Maste if (key->ecdsa == NULL)
101f374ba41SEd Maste return SSH_ERR_INVALID_ARGUMENT;
102f374ba41SEd Maste if ((r = sshbuf_put_cstring(b,
103f374ba41SEd Maste sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 ||
104f374ba41SEd Maste (r = sshbuf_put_eckey(b, key->ecdsa)) != 0)
105f374ba41SEd Maste return r;
106f374ba41SEd Maste
107f374ba41SEd Maste return 0;
108f374ba41SEd Maste }
109f374ba41SEd Maste
110f374ba41SEd Maste static int
ssh_ecdsa_serialize_private(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)111f374ba41SEd Maste ssh_ecdsa_serialize_private(const struct sshkey *key, struct sshbuf *b,
112f374ba41SEd Maste enum sshkey_serialize_rep opts)
113f374ba41SEd Maste {
114f374ba41SEd Maste int r;
115f374ba41SEd Maste
116f374ba41SEd Maste if (!sshkey_is_cert(key)) {
117f374ba41SEd Maste if ((r = ssh_ecdsa_serialize_public(key, b, opts)) != 0)
118f374ba41SEd Maste return r;
119f374ba41SEd Maste }
120f374ba41SEd Maste if ((r = sshbuf_put_bignum2(b,
121f374ba41SEd Maste EC_KEY_get0_private_key(key->ecdsa))) != 0)
122f374ba41SEd Maste return r;
123f374ba41SEd Maste return 0;
124f374ba41SEd Maste }
125f374ba41SEd Maste
126f374ba41SEd Maste static int
ssh_ecdsa_generate(struct sshkey * k,int bits)127f374ba41SEd Maste ssh_ecdsa_generate(struct sshkey *k, int bits)
128f374ba41SEd Maste {
129f374ba41SEd Maste EC_KEY *private;
130f374ba41SEd Maste
131f374ba41SEd Maste if ((k->ecdsa_nid = sshkey_ecdsa_bits_to_nid(bits)) == -1)
132f374ba41SEd Maste return SSH_ERR_KEY_LENGTH;
133f374ba41SEd Maste if ((private = EC_KEY_new_by_curve_name(k->ecdsa_nid)) == NULL)
134f374ba41SEd Maste return SSH_ERR_ALLOC_FAIL;
135f374ba41SEd Maste if (EC_KEY_generate_key(private) != 1) {
136f374ba41SEd Maste EC_KEY_free(private);
137f374ba41SEd Maste return SSH_ERR_LIBCRYPTO_ERROR;
138f374ba41SEd Maste }
139f374ba41SEd Maste EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE);
140f374ba41SEd Maste k->ecdsa = private;
141f374ba41SEd Maste return 0;
142f374ba41SEd Maste }
143f374ba41SEd Maste
144f374ba41SEd Maste static int
ssh_ecdsa_copy_public(const struct sshkey * from,struct sshkey * to)145f374ba41SEd Maste ssh_ecdsa_copy_public(const struct sshkey *from, struct sshkey *to)
146f374ba41SEd Maste {
147f374ba41SEd Maste to->ecdsa_nid = from->ecdsa_nid;
148f374ba41SEd Maste if ((to->ecdsa = EC_KEY_new_by_curve_name(from->ecdsa_nid)) == NULL)
149f374ba41SEd Maste return SSH_ERR_ALLOC_FAIL;
150f374ba41SEd Maste if (EC_KEY_set_public_key(to->ecdsa,
151f374ba41SEd Maste EC_KEY_get0_public_key(from->ecdsa)) != 1)
152f374ba41SEd Maste return SSH_ERR_LIBCRYPTO_ERROR; /* caller will free k->ecdsa */
153f374ba41SEd Maste return 0;
154f374ba41SEd Maste }
155f374ba41SEd Maste
156f374ba41SEd Maste static int
ssh_ecdsa_deserialize_public(const char * ktype,struct sshbuf * b,struct sshkey * key)157f374ba41SEd Maste ssh_ecdsa_deserialize_public(const char *ktype, struct sshbuf *b,
158f374ba41SEd Maste struct sshkey *key)
159f374ba41SEd Maste {
160f374ba41SEd Maste int r;
161f374ba41SEd Maste char *curve = NULL;
162f374ba41SEd Maste
163f374ba41SEd Maste if ((key->ecdsa_nid = sshkey_ecdsa_nid_from_name(ktype)) == -1)
164f374ba41SEd Maste return SSH_ERR_INVALID_ARGUMENT;
165f374ba41SEd Maste if ((r = sshbuf_get_cstring(b, &curve, NULL)) != 0)
166f374ba41SEd Maste goto out;
167f374ba41SEd Maste if (key->ecdsa_nid != sshkey_curve_name_to_nid(curve)) {
168f374ba41SEd Maste r = SSH_ERR_EC_CURVE_MISMATCH;
169f374ba41SEd Maste goto out;
170f374ba41SEd Maste }
171f374ba41SEd Maste EC_KEY_free(key->ecdsa);
172f374ba41SEd Maste key->ecdsa = NULL;
173f374ba41SEd Maste if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid)) == NULL) {
174f374ba41SEd Maste r = SSH_ERR_LIBCRYPTO_ERROR;
175f374ba41SEd Maste goto out;
176f374ba41SEd Maste }
177f374ba41SEd Maste if ((r = sshbuf_get_eckey(b, key->ecdsa)) != 0)
178f374ba41SEd Maste goto out;
179f374ba41SEd Maste if (sshkey_ec_validate_public(EC_KEY_get0_group(key->ecdsa),
180f374ba41SEd Maste EC_KEY_get0_public_key(key->ecdsa)) != 0) {
181f374ba41SEd Maste r = SSH_ERR_KEY_INVALID_EC_VALUE;
182f374ba41SEd Maste goto out;
183f374ba41SEd Maste }
184f374ba41SEd Maste /* success */
185f374ba41SEd Maste r = 0;
186f374ba41SEd Maste #ifdef DEBUG_PK
187f374ba41SEd Maste sshkey_dump_ec_point(EC_KEY_get0_group(key->ecdsa),
188f374ba41SEd Maste EC_KEY_get0_public_key(key->ecdsa));
189f374ba41SEd Maste #endif
190f374ba41SEd Maste out:
191f374ba41SEd Maste free(curve);
192f374ba41SEd Maste if (r != 0) {
193f374ba41SEd Maste EC_KEY_free(key->ecdsa);
194f374ba41SEd Maste key->ecdsa = NULL;
195f374ba41SEd Maste }
196f374ba41SEd Maste return r;
197f374ba41SEd Maste }
198f374ba41SEd Maste
199f374ba41SEd Maste static int
ssh_ecdsa_deserialize_private(const char * ktype,struct sshbuf * b,struct sshkey * key)200f374ba41SEd Maste ssh_ecdsa_deserialize_private(const char *ktype, struct sshbuf *b,
201f374ba41SEd Maste struct sshkey *key)
202f374ba41SEd Maste {
203f374ba41SEd Maste int r;
204f374ba41SEd Maste BIGNUM *exponent = NULL;
205f374ba41SEd Maste
206f374ba41SEd Maste if (!sshkey_is_cert(key)) {
207f374ba41SEd Maste if ((r = ssh_ecdsa_deserialize_public(ktype, b, key)) != 0)
208f374ba41SEd Maste return r;
209f374ba41SEd Maste }
210f374ba41SEd Maste if ((r = sshbuf_get_bignum2(b, &exponent)) != 0)
211f374ba41SEd Maste goto out;
212f374ba41SEd Maste if (EC_KEY_set_private_key(key->ecdsa, exponent) != 1) {
213f374ba41SEd Maste r = SSH_ERR_LIBCRYPTO_ERROR;
214f374ba41SEd Maste goto out;
215f374ba41SEd Maste }
216f374ba41SEd Maste if ((r = sshkey_ec_validate_private(key->ecdsa)) != 0)
217f374ba41SEd Maste goto out;
218f374ba41SEd Maste /* success */
219f374ba41SEd Maste r = 0;
220f374ba41SEd Maste out:
221f374ba41SEd Maste BN_clear_free(exponent);
222f374ba41SEd Maste return r;
223f374ba41SEd Maste }
224f374ba41SEd Maste
225f374ba41SEd Maste static int
ssh_ecdsa_sign(struct sshkey * key,u_char ** sigp,size_t * lenp,const u_char * data,size_t dlen,const char * alg,const char * sk_provider,const char * sk_pin,u_int compat)226f374ba41SEd Maste ssh_ecdsa_sign(struct sshkey *key,
227f374ba41SEd Maste u_char **sigp, size_t *lenp,
228f374ba41SEd Maste const u_char *data, size_t dlen,
229f374ba41SEd Maste const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
230f374ba41SEd Maste {
231f374ba41SEd Maste ECDSA_SIG *esig = NULL;
2322a01feabSEd Maste const BIGNUM *sig_r, *sig_s;
233f7167e0eSDag-Erling Smørgrav int hash_alg;
234f7167e0eSDag-Erling Smørgrav u_char digest[SSH_DIGEST_MAX_LENGTH];
235f374ba41SEd Maste size_t len, hlen;
236a0ee8cc6SDag-Erling Smørgrav struct sshbuf *b = NULL, *bb = NULL;
237a0ee8cc6SDag-Erling Smørgrav int ret = SSH_ERR_INTERNAL_ERROR;
2384a421b63SDag-Erling Smørgrav
239a0ee8cc6SDag-Erling Smørgrav if (lenp != NULL)
240a0ee8cc6SDag-Erling Smørgrav *lenp = 0;
241a0ee8cc6SDag-Erling Smørgrav if (sigp != NULL)
242a0ee8cc6SDag-Erling Smørgrav *sigp = NULL;
243a0ee8cc6SDag-Erling Smørgrav
244a0ee8cc6SDag-Erling Smørgrav if (key == NULL || key->ecdsa == NULL ||
245a0ee8cc6SDag-Erling Smørgrav sshkey_type_plain(key->type) != KEY_ECDSA)
246a0ee8cc6SDag-Erling Smørgrav return SSH_ERR_INVALID_ARGUMENT;
247a0ee8cc6SDag-Erling Smørgrav
248a0ee8cc6SDag-Erling Smørgrav if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
249f374ba41SEd Maste (hlen = ssh_digest_bytes(hash_alg)) == 0)
250a0ee8cc6SDag-Erling Smørgrav return SSH_ERR_INTERNAL_ERROR;
251f374ba41SEd Maste if ((ret = ssh_digest_memory(hash_alg, data, dlen,
252a0ee8cc6SDag-Erling Smørgrav digest, sizeof(digest))) != 0)
253a0ee8cc6SDag-Erling Smørgrav goto out;
254a0ee8cc6SDag-Erling Smørgrav
255f374ba41SEd Maste if ((esig = ECDSA_do_sign(digest, hlen, key->ecdsa)) == NULL) {
256a0ee8cc6SDag-Erling Smørgrav ret = SSH_ERR_LIBCRYPTO_ERROR;
257a0ee8cc6SDag-Erling Smørgrav goto out;
2584a421b63SDag-Erling Smørgrav }
259f7167e0eSDag-Erling Smørgrav
260a0ee8cc6SDag-Erling Smørgrav if ((bb = sshbuf_new()) == NULL || (b = sshbuf_new()) == NULL) {
261a0ee8cc6SDag-Erling Smørgrav ret = SSH_ERR_ALLOC_FAIL;
262a0ee8cc6SDag-Erling Smørgrav goto out;
263f7167e0eSDag-Erling Smørgrav }
264f374ba41SEd Maste ECDSA_SIG_get0(esig, &sig_r, &sig_s);
2652a01feabSEd Maste if ((ret = sshbuf_put_bignum2(bb, sig_r)) != 0 ||
2662a01feabSEd Maste (ret = sshbuf_put_bignum2(bb, sig_s)) != 0)
267a0ee8cc6SDag-Erling Smørgrav goto out;
268a0ee8cc6SDag-Erling Smørgrav if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
269a0ee8cc6SDag-Erling Smørgrav (ret = sshbuf_put_stringb(b, bb)) != 0)
270a0ee8cc6SDag-Erling Smørgrav goto out;
271a0ee8cc6SDag-Erling Smørgrav len = sshbuf_len(b);
272a0ee8cc6SDag-Erling Smørgrav if (sigp != NULL) {
273a0ee8cc6SDag-Erling Smørgrav if ((*sigp = malloc(len)) == NULL) {
274a0ee8cc6SDag-Erling Smørgrav ret = SSH_ERR_ALLOC_FAIL;
275a0ee8cc6SDag-Erling Smørgrav goto out;
276f7167e0eSDag-Erling Smørgrav }
277a0ee8cc6SDag-Erling Smørgrav memcpy(*sigp, sshbuf_ptr(b), len);
2784a421b63SDag-Erling Smørgrav }
2794a421b63SDag-Erling Smørgrav if (lenp != NULL)
2804a421b63SDag-Erling Smørgrav *lenp = len;
281a0ee8cc6SDag-Erling Smørgrav ret = 0;
282a0ee8cc6SDag-Erling Smørgrav out:
283a0ee8cc6SDag-Erling Smørgrav explicit_bzero(digest, sizeof(digest));
284a0ee8cc6SDag-Erling Smørgrav sshbuf_free(b);
285a0ee8cc6SDag-Erling Smørgrav sshbuf_free(bb);
286f374ba41SEd Maste ECDSA_SIG_free(esig);
287a0ee8cc6SDag-Erling Smørgrav return ret;
2884a421b63SDag-Erling Smørgrav }
2894a421b63SDag-Erling Smørgrav
290f374ba41SEd Maste static int
ssh_ecdsa_verify(const struct sshkey * key,const u_char * sig,size_t siglen,const u_char * data,size_t dlen,const char * alg,u_int compat,struct sshkey_sig_details ** detailsp)291a0ee8cc6SDag-Erling Smørgrav ssh_ecdsa_verify(const struct sshkey *key,
292f374ba41SEd Maste const u_char *sig, size_t siglen,
293f374ba41SEd Maste const u_char *data, size_t dlen, const char *alg, u_int compat,
294f374ba41SEd Maste struct sshkey_sig_details **detailsp)
2954a421b63SDag-Erling Smørgrav {
296f374ba41SEd Maste ECDSA_SIG *esig = NULL;
2972a01feabSEd Maste BIGNUM *sig_r = NULL, *sig_s = NULL;
298f7167e0eSDag-Erling Smørgrav int hash_alg;
299a0ee8cc6SDag-Erling Smørgrav u_char digest[SSH_DIGEST_MAX_LENGTH];
300f374ba41SEd Maste size_t hlen;
301a0ee8cc6SDag-Erling Smørgrav int ret = SSH_ERR_INTERNAL_ERROR;
302a0ee8cc6SDag-Erling Smørgrav struct sshbuf *b = NULL, *sigbuf = NULL;
303a0ee8cc6SDag-Erling Smørgrav char *ktype = NULL;
3044a421b63SDag-Erling Smørgrav
305a0ee8cc6SDag-Erling Smørgrav if (key == NULL || key->ecdsa == NULL ||
306076ad2f8SDag-Erling Smørgrav sshkey_type_plain(key->type) != KEY_ECDSA ||
307f374ba41SEd Maste sig == NULL || siglen == 0)
308a0ee8cc6SDag-Erling Smørgrav return SSH_ERR_INVALID_ARGUMENT;
309a0ee8cc6SDag-Erling Smørgrav
310a0ee8cc6SDag-Erling Smørgrav if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
311f374ba41SEd Maste (hlen = ssh_digest_bytes(hash_alg)) == 0)
312a0ee8cc6SDag-Erling Smørgrav return SSH_ERR_INTERNAL_ERROR;
3134a421b63SDag-Erling Smørgrav
3144a421b63SDag-Erling Smørgrav /* fetch signature */
315f374ba41SEd Maste if ((b = sshbuf_from(sig, siglen)) == NULL)
316a0ee8cc6SDag-Erling Smørgrav return SSH_ERR_ALLOC_FAIL;
317a0ee8cc6SDag-Erling Smørgrav if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
318a0ee8cc6SDag-Erling Smørgrav sshbuf_froms(b, &sigbuf) != 0) {
319a0ee8cc6SDag-Erling Smørgrav ret = SSH_ERR_INVALID_FORMAT;
320a0ee8cc6SDag-Erling Smørgrav goto out;
3214a421b63SDag-Erling Smørgrav }
322a0ee8cc6SDag-Erling Smørgrav if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
323a0ee8cc6SDag-Erling Smørgrav ret = SSH_ERR_KEY_TYPE_MISMATCH;
324a0ee8cc6SDag-Erling Smørgrav goto out;
325a0ee8cc6SDag-Erling Smørgrav }
326a0ee8cc6SDag-Erling Smørgrav if (sshbuf_len(b) != 0) {
327a0ee8cc6SDag-Erling Smørgrav ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
328a0ee8cc6SDag-Erling Smørgrav goto out;
3294a421b63SDag-Erling Smørgrav }
3304a421b63SDag-Erling Smørgrav
3314a421b63SDag-Erling Smørgrav /* parse signature */
33219261079SEd Maste if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
33319261079SEd Maste sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
33419261079SEd Maste ret = SSH_ERR_INVALID_FORMAT;
335a0ee8cc6SDag-Erling Smørgrav goto out;
336f7167e0eSDag-Erling Smørgrav }
337f374ba41SEd Maste if ((esig = ECDSA_SIG_new()) == NULL) {
33819261079SEd Maste ret = SSH_ERR_ALLOC_FAIL;
339a0ee8cc6SDag-Erling Smørgrav goto out;
340a0ee8cc6SDag-Erling Smørgrav }
341f374ba41SEd Maste if (!ECDSA_SIG_set0(esig, sig_r, sig_s)) {
3422a01feabSEd Maste ret = SSH_ERR_LIBCRYPTO_ERROR;
3432a01feabSEd Maste goto out;
3442a01feabSEd Maste }
3452a01feabSEd Maste sig_r = sig_s = NULL; /* transferred */
3462a01feabSEd Maste
347a0ee8cc6SDag-Erling Smørgrav if (sshbuf_len(sigbuf) != 0) {
348a0ee8cc6SDag-Erling Smørgrav ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
349a0ee8cc6SDag-Erling Smørgrav goto out;
350a0ee8cc6SDag-Erling Smørgrav }
351f374ba41SEd Maste if ((ret = ssh_digest_memory(hash_alg, data, dlen,
352a0ee8cc6SDag-Erling Smørgrav digest, sizeof(digest))) != 0)
353a0ee8cc6SDag-Erling Smørgrav goto out;
354a0ee8cc6SDag-Erling Smørgrav
355f374ba41SEd Maste switch (ECDSA_do_verify(digest, hlen, esig, key->ecdsa)) {
356a0ee8cc6SDag-Erling Smørgrav case 1:
357a0ee8cc6SDag-Erling Smørgrav ret = 0;
358a0ee8cc6SDag-Erling Smørgrav break;
359a0ee8cc6SDag-Erling Smørgrav case 0:
360a0ee8cc6SDag-Erling Smørgrav ret = SSH_ERR_SIGNATURE_INVALID;
361a0ee8cc6SDag-Erling Smørgrav goto out;
362a0ee8cc6SDag-Erling Smørgrav default:
363a0ee8cc6SDag-Erling Smørgrav ret = SSH_ERR_LIBCRYPTO_ERROR;
364a0ee8cc6SDag-Erling Smørgrav goto out;
365f7167e0eSDag-Erling Smørgrav }
3664a421b63SDag-Erling Smørgrav
367a0ee8cc6SDag-Erling Smørgrav out:
368b83788ffSDag-Erling Smørgrav explicit_bzero(digest, sizeof(digest));
369a0ee8cc6SDag-Erling Smørgrav sshbuf_free(sigbuf);
370a0ee8cc6SDag-Erling Smørgrav sshbuf_free(b);
371f374ba41SEd Maste ECDSA_SIG_free(esig);
3722a01feabSEd Maste BN_clear_free(sig_r);
3732a01feabSEd Maste BN_clear_free(sig_s);
374a0ee8cc6SDag-Erling Smørgrav free(ktype);
3754a421b63SDag-Erling Smørgrav return ret;
3764a421b63SDag-Erling Smørgrav }
3774a421b63SDag-Erling Smørgrav
378f374ba41SEd Maste /* NB. not static; used by ECDSA-SK */
379f374ba41SEd Maste const struct sshkey_impl_funcs sshkey_ecdsa_funcs = {
380f374ba41SEd Maste /* .size = */ ssh_ecdsa_size,
381f374ba41SEd Maste /* .alloc = */ NULL,
382f374ba41SEd Maste /* .cleanup = */ ssh_ecdsa_cleanup,
383f374ba41SEd Maste /* .equal = */ ssh_ecdsa_equal,
384f374ba41SEd Maste /* .ssh_serialize_public = */ ssh_ecdsa_serialize_public,
385f374ba41SEd Maste /* .ssh_deserialize_public = */ ssh_ecdsa_deserialize_public,
386f374ba41SEd Maste /* .ssh_serialize_private = */ ssh_ecdsa_serialize_private,
387f374ba41SEd Maste /* .ssh_deserialize_private = */ ssh_ecdsa_deserialize_private,
388f374ba41SEd Maste /* .generate = */ ssh_ecdsa_generate,
389f374ba41SEd Maste /* .copy_public = */ ssh_ecdsa_copy_public,
390f374ba41SEd Maste /* .sign = */ ssh_ecdsa_sign,
391f374ba41SEd Maste /* .verify = */ ssh_ecdsa_verify,
392f374ba41SEd Maste };
393f374ba41SEd Maste
394f374ba41SEd Maste const struct sshkey_impl sshkey_ecdsa_nistp256_impl = {
395f374ba41SEd Maste /* .name = */ "ecdsa-sha2-nistp256",
396f374ba41SEd Maste /* .shortname = */ "ECDSA",
397f374ba41SEd Maste /* .sigalg = */ NULL,
398f374ba41SEd Maste /* .type = */ KEY_ECDSA,
399f374ba41SEd Maste /* .nid = */ NID_X9_62_prime256v1,
400f374ba41SEd Maste /* .cert = */ 0,
401f374ba41SEd Maste /* .sigonly = */ 0,
402f374ba41SEd Maste /* .keybits = */ 0,
403f374ba41SEd Maste /* .funcs = */ &sshkey_ecdsa_funcs,
404f374ba41SEd Maste };
405f374ba41SEd Maste
406f374ba41SEd Maste const struct sshkey_impl sshkey_ecdsa_nistp256_cert_impl = {
407f374ba41SEd Maste /* .name = */ "ecdsa-sha2-nistp256-cert-v01@openssh.com",
408f374ba41SEd Maste /* .shortname = */ "ECDSA-CERT",
409f374ba41SEd Maste /* .sigalg = */ NULL,
410f374ba41SEd Maste /* .type = */ KEY_ECDSA_CERT,
411f374ba41SEd Maste /* .nid = */ NID_X9_62_prime256v1,
412f374ba41SEd Maste /* .cert = */ 1,
413f374ba41SEd Maste /* .sigonly = */ 0,
414f374ba41SEd Maste /* .keybits = */ 0,
415f374ba41SEd Maste /* .funcs = */ &sshkey_ecdsa_funcs,
416f374ba41SEd Maste };
417f374ba41SEd Maste
418f374ba41SEd Maste const struct sshkey_impl sshkey_ecdsa_nistp384_impl = {
419f374ba41SEd Maste /* .name = */ "ecdsa-sha2-nistp384",
420f374ba41SEd Maste /* .shortname = */ "ECDSA",
421f374ba41SEd Maste /* .sigalg = */ NULL,
422f374ba41SEd Maste /* .type = */ KEY_ECDSA,
423f374ba41SEd Maste /* .nid = */ NID_secp384r1,
424f374ba41SEd Maste /* .cert = */ 0,
425f374ba41SEd Maste /* .sigonly = */ 0,
426f374ba41SEd Maste /* .keybits = */ 0,
427f374ba41SEd Maste /* .funcs = */ &sshkey_ecdsa_funcs,
428f374ba41SEd Maste };
429f374ba41SEd Maste
430f374ba41SEd Maste const struct sshkey_impl sshkey_ecdsa_nistp384_cert_impl = {
431f374ba41SEd Maste /* .name = */ "ecdsa-sha2-nistp384-cert-v01@openssh.com",
432f374ba41SEd Maste /* .shortname = */ "ECDSA-CERT",
433f374ba41SEd Maste /* .sigalg = */ NULL,
434f374ba41SEd Maste /* .type = */ KEY_ECDSA_CERT,
435f374ba41SEd Maste /* .nid = */ NID_secp384r1,
436f374ba41SEd Maste /* .cert = */ 1,
437f374ba41SEd Maste /* .sigonly = */ 0,
438f374ba41SEd Maste /* .keybits = */ 0,
439f374ba41SEd Maste /* .funcs = */ &sshkey_ecdsa_funcs,
440f374ba41SEd Maste };
441f374ba41SEd Maste
442f374ba41SEd Maste #ifdef OPENSSL_HAS_NISTP521
443f374ba41SEd Maste const struct sshkey_impl sshkey_ecdsa_nistp521_impl = {
444f374ba41SEd Maste /* .name = */ "ecdsa-sha2-nistp521",
445f374ba41SEd Maste /* .shortname = */ "ECDSA",
446f374ba41SEd Maste /* .sigalg = */ NULL,
447f374ba41SEd Maste /* .type = */ KEY_ECDSA,
448f374ba41SEd Maste /* .nid = */ NID_secp521r1,
449f374ba41SEd Maste /* .cert = */ 0,
450f374ba41SEd Maste /* .sigonly = */ 0,
451f374ba41SEd Maste /* .keybits = */ 0,
452f374ba41SEd Maste /* .funcs = */ &sshkey_ecdsa_funcs,
453f374ba41SEd Maste };
454f374ba41SEd Maste
455f374ba41SEd Maste const struct sshkey_impl sshkey_ecdsa_nistp521_cert_impl = {
456f374ba41SEd Maste /* .name = */ "ecdsa-sha2-nistp521-cert-v01@openssh.com",
457f374ba41SEd Maste /* .shortname = */ "ECDSA-CERT",
458f374ba41SEd Maste /* .sigalg = */ NULL,
459f374ba41SEd Maste /* .type = */ KEY_ECDSA_CERT,
460f374ba41SEd Maste /* .nid = */ NID_secp521r1,
461f374ba41SEd Maste /* .cert = */ 1,
462f374ba41SEd Maste /* .sigonly = */ 0,
463f374ba41SEd Maste /* .keybits = */ 0,
464f374ba41SEd Maste /* .funcs = */ &sshkey_ecdsa_funcs,
465f374ba41SEd Maste };
466f374ba41SEd Maste #endif
467f374ba41SEd Maste
468bc5531deSDag-Erling Smørgrav #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
469