1*ba1276acSMatthew Dillon /* $OpenBSD: ssh-ecdsa.c,v 1.26 2023/03/08 04:43:12 guenther Exp $ */
29f304aafSPeter Avalos /*
39f304aafSPeter Avalos * Copyright (c) 2000 Markus Friedl. All rights reserved.
49f304aafSPeter Avalos * Copyright (c) 2010 Damien Miller. All rights reserved.
59f304aafSPeter Avalos *
69f304aafSPeter Avalos * Redistribution and use in source and binary forms, with or without
79f304aafSPeter Avalos * modification, are permitted provided that the following conditions
89f304aafSPeter Avalos * are met:
99f304aafSPeter Avalos * 1. Redistributions of source code must retain the above copyright
109f304aafSPeter Avalos * notice, this list of conditions and the following disclaimer.
119f304aafSPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
129f304aafSPeter Avalos * notice, this list of conditions and the following disclaimer in the
139f304aafSPeter Avalos * documentation and/or other materials provided with the distribution.
149f304aafSPeter Avalos *
159f304aafSPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
169f304aafSPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
179f304aafSPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
189f304aafSPeter Avalos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199f304aafSPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
209f304aafSPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
219f304aafSPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
229f304aafSPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
239f304aafSPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
249f304aafSPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
259f304aafSPeter Avalos */
269f304aafSPeter Avalos
279f304aafSPeter Avalos #include "includes.h"
289f304aafSPeter Avalos
29e9778795SPeter Avalos #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
309f304aafSPeter Avalos
319f304aafSPeter Avalos #include <sys/types.h>
329f304aafSPeter Avalos
339f304aafSPeter Avalos #include <openssl/bn.h>
349f304aafSPeter Avalos #include <openssl/ec.h>
359f304aafSPeter Avalos #include <openssl/ecdsa.h>
369f304aafSPeter Avalos #include <openssl/evp.h>
379f304aafSPeter Avalos
389f304aafSPeter Avalos #include <string.h>
399f304aafSPeter Avalos
4036e94dc5SPeter Avalos #include "sshbuf.h"
4136e94dc5SPeter Avalos #include "ssherr.h"
4236e94dc5SPeter Avalos #include "digest.h"
4336e94dc5SPeter Avalos #define SSHKEY_INTERNAL
4436e94dc5SPeter Avalos #include "sshkey.h"
459f304aafSPeter Avalos
46664f4763Szrj #include "openbsd-compat/openssl-compat.h"
47664f4763Szrj
48*ba1276acSMatthew Dillon static u_int
ssh_ecdsa_size(const struct sshkey * key)49*ba1276acSMatthew Dillon ssh_ecdsa_size(const struct sshkey *key)
509f304aafSPeter Avalos {
51*ba1276acSMatthew Dillon switch (key->ecdsa_nid) {
52*ba1276acSMatthew Dillon case NID_X9_62_prime256v1:
53*ba1276acSMatthew Dillon return 256;
54*ba1276acSMatthew Dillon case NID_secp384r1:
55*ba1276acSMatthew Dillon return 384;
56*ba1276acSMatthew Dillon #ifdef OPENSSL_HAS_NISTP521
57*ba1276acSMatthew Dillon case NID_secp521r1:
58*ba1276acSMatthew Dillon return 521;
59*ba1276acSMatthew Dillon #endif
60*ba1276acSMatthew Dillon default:
61*ba1276acSMatthew Dillon return 0;
62*ba1276acSMatthew Dillon }
63*ba1276acSMatthew Dillon }
64*ba1276acSMatthew Dillon
65*ba1276acSMatthew Dillon static void
ssh_ecdsa_cleanup(struct sshkey * k)66*ba1276acSMatthew Dillon ssh_ecdsa_cleanup(struct sshkey *k)
67*ba1276acSMatthew Dillon {
68*ba1276acSMatthew Dillon EC_KEY_free(k->ecdsa);
69*ba1276acSMatthew Dillon k->ecdsa = NULL;
70*ba1276acSMatthew Dillon }
71*ba1276acSMatthew Dillon
72*ba1276acSMatthew Dillon static int
ssh_ecdsa_equal(const struct sshkey * a,const struct sshkey * b)73*ba1276acSMatthew Dillon ssh_ecdsa_equal(const struct sshkey *a, const struct sshkey *b)
74*ba1276acSMatthew Dillon {
75*ba1276acSMatthew Dillon const EC_GROUP *grp_a, *grp_b;
76*ba1276acSMatthew Dillon const EC_POINT *pub_a, *pub_b;
77*ba1276acSMatthew Dillon
78*ba1276acSMatthew Dillon if (a->ecdsa == NULL || b->ecdsa == NULL)
79*ba1276acSMatthew Dillon return 0;
80*ba1276acSMatthew Dillon if ((grp_a = EC_KEY_get0_group(a->ecdsa)) == NULL ||
81*ba1276acSMatthew Dillon (grp_b = EC_KEY_get0_group(b->ecdsa)) == NULL)
82*ba1276acSMatthew Dillon return 0;
83*ba1276acSMatthew Dillon if ((pub_a = EC_KEY_get0_public_key(a->ecdsa)) == NULL ||
84*ba1276acSMatthew Dillon (pub_b = EC_KEY_get0_public_key(b->ecdsa)) == NULL)
85*ba1276acSMatthew Dillon return 0;
86*ba1276acSMatthew Dillon if (EC_GROUP_cmp(grp_a, grp_b, NULL) != 0)
87*ba1276acSMatthew Dillon return 0;
88*ba1276acSMatthew Dillon if (EC_POINT_cmp(grp_a, pub_a, pub_b, NULL) != 0)
89*ba1276acSMatthew Dillon return 0;
90*ba1276acSMatthew Dillon
91*ba1276acSMatthew Dillon return 1;
92*ba1276acSMatthew Dillon }
93*ba1276acSMatthew Dillon
94*ba1276acSMatthew Dillon static int
ssh_ecdsa_serialize_public(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)95*ba1276acSMatthew Dillon ssh_ecdsa_serialize_public(const struct sshkey *key, struct sshbuf *b,
96*ba1276acSMatthew Dillon enum sshkey_serialize_rep opts)
97*ba1276acSMatthew Dillon {
98*ba1276acSMatthew Dillon int r;
99*ba1276acSMatthew Dillon
100*ba1276acSMatthew Dillon if (key->ecdsa == NULL)
101*ba1276acSMatthew Dillon return SSH_ERR_INVALID_ARGUMENT;
102*ba1276acSMatthew Dillon if ((r = sshbuf_put_cstring(b,
103*ba1276acSMatthew Dillon sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 ||
104*ba1276acSMatthew Dillon (r = sshbuf_put_eckey(b, key->ecdsa)) != 0)
105*ba1276acSMatthew Dillon return r;
106*ba1276acSMatthew Dillon
107*ba1276acSMatthew Dillon return 0;
108*ba1276acSMatthew Dillon }
109*ba1276acSMatthew Dillon
110*ba1276acSMatthew Dillon static int
ssh_ecdsa_serialize_private(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)111*ba1276acSMatthew Dillon ssh_ecdsa_serialize_private(const struct sshkey *key, struct sshbuf *b,
112*ba1276acSMatthew Dillon enum sshkey_serialize_rep opts)
113*ba1276acSMatthew Dillon {
114*ba1276acSMatthew Dillon int r;
115*ba1276acSMatthew Dillon
116*ba1276acSMatthew Dillon if (!sshkey_is_cert(key)) {
117*ba1276acSMatthew Dillon if ((r = ssh_ecdsa_serialize_public(key, b, opts)) != 0)
118*ba1276acSMatthew Dillon return r;
119*ba1276acSMatthew Dillon }
120*ba1276acSMatthew Dillon if ((r = sshbuf_put_bignum2(b,
121*ba1276acSMatthew Dillon EC_KEY_get0_private_key(key->ecdsa))) != 0)
122*ba1276acSMatthew Dillon return r;
123*ba1276acSMatthew Dillon return 0;
124*ba1276acSMatthew Dillon }
125*ba1276acSMatthew Dillon
126*ba1276acSMatthew Dillon static int
ssh_ecdsa_generate(struct sshkey * k,int bits)127*ba1276acSMatthew Dillon ssh_ecdsa_generate(struct sshkey *k, int bits)
128*ba1276acSMatthew Dillon {
129*ba1276acSMatthew Dillon EC_KEY *private;
130*ba1276acSMatthew Dillon
131*ba1276acSMatthew Dillon if ((k->ecdsa_nid = sshkey_ecdsa_bits_to_nid(bits)) == -1)
132*ba1276acSMatthew Dillon return SSH_ERR_KEY_LENGTH;
133*ba1276acSMatthew Dillon if ((private = EC_KEY_new_by_curve_name(k->ecdsa_nid)) == NULL)
134*ba1276acSMatthew Dillon return SSH_ERR_ALLOC_FAIL;
135*ba1276acSMatthew Dillon if (EC_KEY_generate_key(private) != 1) {
136*ba1276acSMatthew Dillon EC_KEY_free(private);
137*ba1276acSMatthew Dillon return SSH_ERR_LIBCRYPTO_ERROR;
138*ba1276acSMatthew Dillon }
139*ba1276acSMatthew Dillon EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE);
140*ba1276acSMatthew Dillon k->ecdsa = private;
141*ba1276acSMatthew Dillon return 0;
142*ba1276acSMatthew Dillon }
143*ba1276acSMatthew Dillon
144*ba1276acSMatthew Dillon static int
ssh_ecdsa_copy_public(const struct sshkey * from,struct sshkey * to)145*ba1276acSMatthew Dillon ssh_ecdsa_copy_public(const struct sshkey *from, struct sshkey *to)
146*ba1276acSMatthew Dillon {
147*ba1276acSMatthew Dillon to->ecdsa_nid = from->ecdsa_nid;
148*ba1276acSMatthew Dillon if ((to->ecdsa = EC_KEY_new_by_curve_name(from->ecdsa_nid)) == NULL)
149*ba1276acSMatthew Dillon return SSH_ERR_ALLOC_FAIL;
150*ba1276acSMatthew Dillon if (EC_KEY_set_public_key(to->ecdsa,
151*ba1276acSMatthew Dillon EC_KEY_get0_public_key(from->ecdsa)) != 1)
152*ba1276acSMatthew Dillon return SSH_ERR_LIBCRYPTO_ERROR; /* caller will free k->ecdsa */
153*ba1276acSMatthew Dillon return 0;
154*ba1276acSMatthew Dillon }
155*ba1276acSMatthew Dillon
156*ba1276acSMatthew Dillon static int
ssh_ecdsa_deserialize_public(const char * ktype,struct sshbuf * b,struct sshkey * key)157*ba1276acSMatthew Dillon ssh_ecdsa_deserialize_public(const char *ktype, struct sshbuf *b,
158*ba1276acSMatthew Dillon struct sshkey *key)
159*ba1276acSMatthew Dillon {
160*ba1276acSMatthew Dillon int r;
161*ba1276acSMatthew Dillon char *curve = NULL;
162*ba1276acSMatthew Dillon
163*ba1276acSMatthew Dillon if ((key->ecdsa_nid = sshkey_ecdsa_nid_from_name(ktype)) == -1)
164*ba1276acSMatthew Dillon return SSH_ERR_INVALID_ARGUMENT;
165*ba1276acSMatthew Dillon if ((r = sshbuf_get_cstring(b, &curve, NULL)) != 0)
166*ba1276acSMatthew Dillon goto out;
167*ba1276acSMatthew Dillon if (key->ecdsa_nid != sshkey_curve_name_to_nid(curve)) {
168*ba1276acSMatthew Dillon r = SSH_ERR_EC_CURVE_MISMATCH;
169*ba1276acSMatthew Dillon goto out;
170*ba1276acSMatthew Dillon }
171*ba1276acSMatthew Dillon EC_KEY_free(key->ecdsa);
172*ba1276acSMatthew Dillon key->ecdsa = NULL;
173*ba1276acSMatthew Dillon if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid)) == NULL) {
174*ba1276acSMatthew Dillon r = SSH_ERR_LIBCRYPTO_ERROR;
175*ba1276acSMatthew Dillon goto out;
176*ba1276acSMatthew Dillon }
177*ba1276acSMatthew Dillon if ((r = sshbuf_get_eckey(b, key->ecdsa)) != 0)
178*ba1276acSMatthew Dillon goto out;
179*ba1276acSMatthew Dillon if (sshkey_ec_validate_public(EC_KEY_get0_group(key->ecdsa),
180*ba1276acSMatthew Dillon EC_KEY_get0_public_key(key->ecdsa)) != 0) {
181*ba1276acSMatthew Dillon r = SSH_ERR_KEY_INVALID_EC_VALUE;
182*ba1276acSMatthew Dillon goto out;
183*ba1276acSMatthew Dillon }
184*ba1276acSMatthew Dillon /* success */
185*ba1276acSMatthew Dillon r = 0;
186*ba1276acSMatthew Dillon #ifdef DEBUG_PK
187*ba1276acSMatthew Dillon sshkey_dump_ec_point(EC_KEY_get0_group(key->ecdsa),
188*ba1276acSMatthew Dillon EC_KEY_get0_public_key(key->ecdsa));
189*ba1276acSMatthew Dillon #endif
190*ba1276acSMatthew Dillon out:
191*ba1276acSMatthew Dillon free(curve);
192*ba1276acSMatthew Dillon if (r != 0) {
193*ba1276acSMatthew Dillon EC_KEY_free(key->ecdsa);
194*ba1276acSMatthew Dillon key->ecdsa = NULL;
195*ba1276acSMatthew Dillon }
196*ba1276acSMatthew Dillon return r;
197*ba1276acSMatthew Dillon }
198*ba1276acSMatthew Dillon
199*ba1276acSMatthew Dillon static int
ssh_ecdsa_deserialize_private(const char * ktype,struct sshbuf * b,struct sshkey * key)200*ba1276acSMatthew Dillon ssh_ecdsa_deserialize_private(const char *ktype, struct sshbuf *b,
201*ba1276acSMatthew Dillon struct sshkey *key)
202*ba1276acSMatthew Dillon {
203*ba1276acSMatthew Dillon int r;
204*ba1276acSMatthew Dillon BIGNUM *exponent = NULL;
205*ba1276acSMatthew Dillon
206*ba1276acSMatthew Dillon if (!sshkey_is_cert(key)) {
207*ba1276acSMatthew Dillon if ((r = ssh_ecdsa_deserialize_public(ktype, b, key)) != 0)
208*ba1276acSMatthew Dillon return r;
209*ba1276acSMatthew Dillon }
210*ba1276acSMatthew Dillon if ((r = sshbuf_get_bignum2(b, &exponent)) != 0)
211*ba1276acSMatthew Dillon goto out;
212*ba1276acSMatthew Dillon if (EC_KEY_set_private_key(key->ecdsa, exponent) != 1) {
213*ba1276acSMatthew Dillon r = SSH_ERR_LIBCRYPTO_ERROR;
214*ba1276acSMatthew Dillon goto out;
215*ba1276acSMatthew Dillon }
216*ba1276acSMatthew Dillon if ((r = sshkey_ec_validate_private(key->ecdsa)) != 0)
217*ba1276acSMatthew Dillon goto out;
218*ba1276acSMatthew Dillon /* success */
219*ba1276acSMatthew Dillon r = 0;
220*ba1276acSMatthew Dillon out:
221*ba1276acSMatthew Dillon BN_clear_free(exponent);
222*ba1276acSMatthew Dillon return r;
223*ba1276acSMatthew Dillon }
224*ba1276acSMatthew Dillon
225*ba1276acSMatthew Dillon 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)226*ba1276acSMatthew Dillon ssh_ecdsa_sign(struct sshkey *key,
227*ba1276acSMatthew Dillon u_char **sigp, size_t *lenp,
228*ba1276acSMatthew Dillon const u_char *data, size_t dlen,
229*ba1276acSMatthew Dillon const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
230*ba1276acSMatthew Dillon {
231*ba1276acSMatthew Dillon ECDSA_SIG *esig = NULL;
232664f4763Szrj const BIGNUM *sig_r, *sig_s;
23336e94dc5SPeter Avalos int hash_alg;
23436e94dc5SPeter Avalos u_char digest[SSH_DIGEST_MAX_LENGTH];
235*ba1276acSMatthew Dillon size_t len, hlen;
23636e94dc5SPeter Avalos struct sshbuf *b = NULL, *bb = NULL;
23736e94dc5SPeter Avalos int ret = SSH_ERR_INTERNAL_ERROR;
23836e94dc5SPeter Avalos
23936e94dc5SPeter Avalos if (lenp != NULL)
24036e94dc5SPeter Avalos *lenp = 0;
24136e94dc5SPeter Avalos if (sigp != NULL)
24236e94dc5SPeter Avalos *sigp = NULL;
2439f304aafSPeter Avalos
2449f304aafSPeter Avalos if (key == NULL || key->ecdsa == NULL ||
24536e94dc5SPeter Avalos sshkey_type_plain(key->type) != KEY_ECDSA)
24636e94dc5SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
2479f304aafSPeter Avalos
24836e94dc5SPeter Avalos if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
249*ba1276acSMatthew Dillon (hlen = ssh_digest_bytes(hash_alg)) == 0)
25036e94dc5SPeter Avalos return SSH_ERR_INTERNAL_ERROR;
251*ba1276acSMatthew Dillon if ((ret = ssh_digest_memory(hash_alg, data, dlen,
25236e94dc5SPeter Avalos digest, sizeof(digest))) != 0)
25336e94dc5SPeter Avalos goto out;
2549f304aafSPeter Avalos
255*ba1276acSMatthew Dillon if ((esig = ECDSA_do_sign(digest, hlen, key->ecdsa)) == NULL) {
25636e94dc5SPeter Avalos ret = SSH_ERR_LIBCRYPTO_ERROR;
25736e94dc5SPeter Avalos goto out;
2589f304aafSPeter Avalos }
2599f304aafSPeter Avalos
26036e94dc5SPeter Avalos if ((bb = sshbuf_new()) == NULL || (b = sshbuf_new()) == NULL) {
26136e94dc5SPeter Avalos ret = SSH_ERR_ALLOC_FAIL;
26236e94dc5SPeter Avalos goto out;
26336e94dc5SPeter Avalos }
264*ba1276acSMatthew Dillon ECDSA_SIG_get0(esig, &sig_r, &sig_s);
265664f4763Szrj if ((ret = sshbuf_put_bignum2(bb, sig_r)) != 0 ||
266664f4763Szrj (ret = sshbuf_put_bignum2(bb, sig_s)) != 0)
26736e94dc5SPeter Avalos goto out;
26836e94dc5SPeter Avalos if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
26936e94dc5SPeter Avalos (ret = sshbuf_put_stringb(b, bb)) != 0)
27036e94dc5SPeter Avalos goto out;
27136e94dc5SPeter Avalos len = sshbuf_len(b);
27236e94dc5SPeter Avalos if (sigp != NULL) {
27336e94dc5SPeter Avalos if ((*sigp = malloc(len)) == NULL) {
27436e94dc5SPeter Avalos ret = SSH_ERR_ALLOC_FAIL;
27536e94dc5SPeter Avalos goto out;
27636e94dc5SPeter Avalos }
27736e94dc5SPeter Avalos memcpy(*sigp, sshbuf_ptr(b), len);
27836e94dc5SPeter Avalos }
2799f304aafSPeter Avalos if (lenp != NULL)
2809f304aafSPeter Avalos *lenp = len;
28136e94dc5SPeter Avalos ret = 0;
28236e94dc5SPeter Avalos out:
28336e94dc5SPeter Avalos explicit_bzero(digest, sizeof(digest));
28436e94dc5SPeter Avalos sshbuf_free(b);
28536e94dc5SPeter Avalos sshbuf_free(bb);
286*ba1276acSMatthew Dillon ECDSA_SIG_free(esig);
28736e94dc5SPeter Avalos return ret;
2889f304aafSPeter Avalos }
2899f304aafSPeter Avalos
290*ba1276acSMatthew Dillon 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)29136e94dc5SPeter Avalos ssh_ecdsa_verify(const struct sshkey *key,
292*ba1276acSMatthew Dillon const u_char *sig, size_t siglen,
293*ba1276acSMatthew Dillon const u_char *data, size_t dlen, const char *alg, u_int compat,
294*ba1276acSMatthew Dillon struct sshkey_sig_details **detailsp)
2959f304aafSPeter Avalos {
296*ba1276acSMatthew Dillon ECDSA_SIG *esig = NULL;
297664f4763Szrj BIGNUM *sig_r = NULL, *sig_s = NULL;
29836e94dc5SPeter Avalos int hash_alg;
29936e94dc5SPeter Avalos u_char digest[SSH_DIGEST_MAX_LENGTH];
300*ba1276acSMatthew Dillon size_t hlen;
30136e94dc5SPeter Avalos int ret = SSH_ERR_INTERNAL_ERROR;
30236e94dc5SPeter Avalos struct sshbuf *b = NULL, *sigbuf = NULL;
30336e94dc5SPeter Avalos char *ktype = NULL;
3049f304aafSPeter Avalos
3059f304aafSPeter Avalos if (key == NULL || key->ecdsa == NULL ||
306e9778795SPeter Avalos sshkey_type_plain(key->type) != KEY_ECDSA ||
307*ba1276acSMatthew Dillon sig == NULL || siglen == 0)
30836e94dc5SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
30936e94dc5SPeter Avalos
31036e94dc5SPeter Avalos if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
311*ba1276acSMatthew Dillon (hlen = ssh_digest_bytes(hash_alg)) == 0)
31236e94dc5SPeter Avalos return SSH_ERR_INTERNAL_ERROR;
3139f304aafSPeter Avalos
3149f304aafSPeter Avalos /* fetch signature */
315*ba1276acSMatthew Dillon if ((b = sshbuf_from(sig, siglen)) == NULL)
31636e94dc5SPeter Avalos return SSH_ERR_ALLOC_FAIL;
31736e94dc5SPeter Avalos if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
31836e94dc5SPeter Avalos sshbuf_froms(b, &sigbuf) != 0) {
31936e94dc5SPeter Avalos ret = SSH_ERR_INVALID_FORMAT;
32036e94dc5SPeter Avalos goto out;
3219f304aafSPeter Avalos }
32236e94dc5SPeter Avalos if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
32336e94dc5SPeter Avalos ret = SSH_ERR_KEY_TYPE_MISMATCH;
32436e94dc5SPeter Avalos goto out;
32536e94dc5SPeter Avalos }
32636e94dc5SPeter Avalos if (sshbuf_len(b) != 0) {
32736e94dc5SPeter Avalos ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
32836e94dc5SPeter Avalos goto out;
3299f304aafSPeter Avalos }
3309f304aafSPeter Avalos
3319f304aafSPeter Avalos /* parse signature */
332664f4763Szrj if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
333664f4763Szrj sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
334664f4763Szrj ret = SSH_ERR_INVALID_FORMAT;
335664f4763Szrj goto out;
336664f4763Szrj }
337*ba1276acSMatthew Dillon if ((esig = ECDSA_SIG_new()) == NULL) {
33836e94dc5SPeter Avalos ret = SSH_ERR_ALLOC_FAIL;
33936e94dc5SPeter Avalos goto out;
34036e94dc5SPeter Avalos }
341*ba1276acSMatthew Dillon if (!ECDSA_SIG_set0(esig, sig_r, sig_s)) {
342664f4763Szrj ret = SSH_ERR_LIBCRYPTO_ERROR;
34336e94dc5SPeter Avalos goto out;
34436e94dc5SPeter Avalos }
345664f4763Szrj sig_r = sig_s = NULL; /* transferred */
346664f4763Szrj
34736e94dc5SPeter Avalos if (sshbuf_len(sigbuf) != 0) {
34836e94dc5SPeter Avalos ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
34936e94dc5SPeter Avalos goto out;
35036e94dc5SPeter Avalos }
351*ba1276acSMatthew Dillon if ((ret = ssh_digest_memory(hash_alg, data, dlen,
35236e94dc5SPeter Avalos digest, sizeof(digest))) != 0)
35336e94dc5SPeter Avalos goto out;
3549f304aafSPeter Avalos
355*ba1276acSMatthew Dillon switch (ECDSA_do_verify(digest, hlen, esig, key->ecdsa)) {
35636e94dc5SPeter Avalos case 1:
35736e94dc5SPeter Avalos ret = 0;
35836e94dc5SPeter Avalos break;
35936e94dc5SPeter Avalos case 0:
36036e94dc5SPeter Avalos ret = SSH_ERR_SIGNATURE_INVALID;
36136e94dc5SPeter Avalos goto out;
36236e94dc5SPeter Avalos default:
36336e94dc5SPeter Avalos ret = SSH_ERR_LIBCRYPTO_ERROR;
36436e94dc5SPeter Avalos goto out;
36536e94dc5SPeter Avalos }
3669f304aafSPeter Avalos
36736e94dc5SPeter Avalos out:
36836e94dc5SPeter Avalos explicit_bzero(digest, sizeof(digest));
36936e94dc5SPeter Avalos sshbuf_free(sigbuf);
37036e94dc5SPeter Avalos sshbuf_free(b);
371*ba1276acSMatthew Dillon ECDSA_SIG_free(esig);
372664f4763Szrj BN_clear_free(sig_r);
373664f4763Szrj BN_clear_free(sig_s);
37436e94dc5SPeter Avalos free(ktype);
3759f304aafSPeter Avalos return ret;
3769f304aafSPeter Avalos }
3779f304aafSPeter Avalos
378*ba1276acSMatthew Dillon /* NB. not static; used by ECDSA-SK */
379*ba1276acSMatthew Dillon const struct sshkey_impl_funcs sshkey_ecdsa_funcs = {
380*ba1276acSMatthew Dillon /* .size = */ ssh_ecdsa_size,
381*ba1276acSMatthew Dillon /* .alloc = */ NULL,
382*ba1276acSMatthew Dillon /* .cleanup = */ ssh_ecdsa_cleanup,
383*ba1276acSMatthew Dillon /* .equal = */ ssh_ecdsa_equal,
384*ba1276acSMatthew Dillon /* .ssh_serialize_public = */ ssh_ecdsa_serialize_public,
385*ba1276acSMatthew Dillon /* .ssh_deserialize_public = */ ssh_ecdsa_deserialize_public,
386*ba1276acSMatthew Dillon /* .ssh_serialize_private = */ ssh_ecdsa_serialize_private,
387*ba1276acSMatthew Dillon /* .ssh_deserialize_private = */ ssh_ecdsa_deserialize_private,
388*ba1276acSMatthew Dillon /* .generate = */ ssh_ecdsa_generate,
389*ba1276acSMatthew Dillon /* .copy_public = */ ssh_ecdsa_copy_public,
390*ba1276acSMatthew Dillon /* .sign = */ ssh_ecdsa_sign,
391*ba1276acSMatthew Dillon /* .verify = */ ssh_ecdsa_verify,
392*ba1276acSMatthew Dillon };
393*ba1276acSMatthew Dillon
394*ba1276acSMatthew Dillon const struct sshkey_impl sshkey_ecdsa_nistp256_impl = {
395*ba1276acSMatthew Dillon /* .name = */ "ecdsa-sha2-nistp256",
396*ba1276acSMatthew Dillon /* .shortname = */ "ECDSA",
397*ba1276acSMatthew Dillon /* .sigalg = */ NULL,
398*ba1276acSMatthew Dillon /* .type = */ KEY_ECDSA,
399*ba1276acSMatthew Dillon /* .nid = */ NID_X9_62_prime256v1,
400*ba1276acSMatthew Dillon /* .cert = */ 0,
401*ba1276acSMatthew Dillon /* .sigonly = */ 0,
402*ba1276acSMatthew Dillon /* .keybits = */ 0,
403*ba1276acSMatthew Dillon /* .funcs = */ &sshkey_ecdsa_funcs,
404*ba1276acSMatthew Dillon };
405*ba1276acSMatthew Dillon
406*ba1276acSMatthew Dillon const struct sshkey_impl sshkey_ecdsa_nistp256_cert_impl = {
407*ba1276acSMatthew Dillon /* .name = */ "ecdsa-sha2-nistp256-cert-v01@openssh.com",
408*ba1276acSMatthew Dillon /* .shortname = */ "ECDSA-CERT",
409*ba1276acSMatthew Dillon /* .sigalg = */ NULL,
410*ba1276acSMatthew Dillon /* .type = */ KEY_ECDSA_CERT,
411*ba1276acSMatthew Dillon /* .nid = */ NID_X9_62_prime256v1,
412*ba1276acSMatthew Dillon /* .cert = */ 1,
413*ba1276acSMatthew Dillon /* .sigonly = */ 0,
414*ba1276acSMatthew Dillon /* .keybits = */ 0,
415*ba1276acSMatthew Dillon /* .funcs = */ &sshkey_ecdsa_funcs,
416*ba1276acSMatthew Dillon };
417*ba1276acSMatthew Dillon
418*ba1276acSMatthew Dillon const struct sshkey_impl sshkey_ecdsa_nistp384_impl = {
419*ba1276acSMatthew Dillon /* .name = */ "ecdsa-sha2-nistp384",
420*ba1276acSMatthew Dillon /* .shortname = */ "ECDSA",
421*ba1276acSMatthew Dillon /* .sigalg = */ NULL,
422*ba1276acSMatthew Dillon /* .type = */ KEY_ECDSA,
423*ba1276acSMatthew Dillon /* .nid = */ NID_secp384r1,
424*ba1276acSMatthew Dillon /* .cert = */ 0,
425*ba1276acSMatthew Dillon /* .sigonly = */ 0,
426*ba1276acSMatthew Dillon /* .keybits = */ 0,
427*ba1276acSMatthew Dillon /* .funcs = */ &sshkey_ecdsa_funcs,
428*ba1276acSMatthew Dillon };
429*ba1276acSMatthew Dillon
430*ba1276acSMatthew Dillon const struct sshkey_impl sshkey_ecdsa_nistp384_cert_impl = {
431*ba1276acSMatthew Dillon /* .name = */ "ecdsa-sha2-nistp384-cert-v01@openssh.com",
432*ba1276acSMatthew Dillon /* .shortname = */ "ECDSA-CERT",
433*ba1276acSMatthew Dillon /* .sigalg = */ NULL,
434*ba1276acSMatthew Dillon /* .type = */ KEY_ECDSA_CERT,
435*ba1276acSMatthew Dillon /* .nid = */ NID_secp384r1,
436*ba1276acSMatthew Dillon /* .cert = */ 1,
437*ba1276acSMatthew Dillon /* .sigonly = */ 0,
438*ba1276acSMatthew Dillon /* .keybits = */ 0,
439*ba1276acSMatthew Dillon /* .funcs = */ &sshkey_ecdsa_funcs,
440*ba1276acSMatthew Dillon };
441*ba1276acSMatthew Dillon
442*ba1276acSMatthew Dillon #ifdef OPENSSL_HAS_NISTP521
443*ba1276acSMatthew Dillon const struct sshkey_impl sshkey_ecdsa_nistp521_impl = {
444*ba1276acSMatthew Dillon /* .name = */ "ecdsa-sha2-nistp521",
445*ba1276acSMatthew Dillon /* .shortname = */ "ECDSA",
446*ba1276acSMatthew Dillon /* .sigalg = */ NULL,
447*ba1276acSMatthew Dillon /* .type = */ KEY_ECDSA,
448*ba1276acSMatthew Dillon /* .nid = */ NID_secp521r1,
449*ba1276acSMatthew Dillon /* .cert = */ 0,
450*ba1276acSMatthew Dillon /* .sigonly = */ 0,
451*ba1276acSMatthew Dillon /* .keybits = */ 0,
452*ba1276acSMatthew Dillon /* .funcs = */ &sshkey_ecdsa_funcs,
453*ba1276acSMatthew Dillon };
454*ba1276acSMatthew Dillon
455*ba1276acSMatthew Dillon const struct sshkey_impl sshkey_ecdsa_nistp521_cert_impl = {
456*ba1276acSMatthew Dillon /* .name = */ "ecdsa-sha2-nistp521-cert-v01@openssh.com",
457*ba1276acSMatthew Dillon /* .shortname = */ "ECDSA-CERT",
458*ba1276acSMatthew Dillon /* .sigalg = */ NULL,
459*ba1276acSMatthew Dillon /* .type = */ KEY_ECDSA_CERT,
460*ba1276acSMatthew Dillon /* .nid = */ NID_secp521r1,
461*ba1276acSMatthew Dillon /* .cert = */ 1,
462*ba1276acSMatthew Dillon /* .sigonly = */ 0,
463*ba1276acSMatthew Dillon /* .keybits = */ 0,
464*ba1276acSMatthew Dillon /* .funcs = */ &sshkey_ecdsa_funcs,
465*ba1276acSMatthew Dillon };
466*ba1276acSMatthew Dillon #endif
467*ba1276acSMatthew Dillon
468e9778795SPeter Avalos #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
469