Copyright year updates
[openssl.git] / crypto / ec / ec_backend.c
1 /*
2  * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * Low level APIs related to EC_KEY are deprecated for public use,
12  * but still ok for internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <openssl/core_names.h>
17 #include <openssl/objects.h>
18 #include <openssl/params.h>
19 #include <openssl/err.h>
20 #ifndef FIPS_MODULE
21 # include <openssl/engine.h>
22 # include <openssl/x509.h>
23 #endif
24 #include "crypto/bn.h"
25 #include "crypto/ec.h"
26 #include "ec_local.h"
27 #include "internal/e_os.h"
28 #include "internal/nelem.h"
29 #include "internal/param_build_set.h"
30
31 /* Mapping between a flag and a name */
32 static const OSSL_ITEM encoding_nameid_map[] = {
33     { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT },
34     { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP },
35 };
36
37 static const OSSL_ITEM check_group_type_nameid_map[] = {
38     { 0, OSSL_PKEY_EC_GROUP_CHECK_DEFAULT },
39     { EC_FLAG_CHECK_NAMED_GROUP, OSSL_PKEY_EC_GROUP_CHECK_NAMED },
40     { EC_FLAG_CHECK_NAMED_GROUP_NIST, OSSL_PKEY_EC_GROUP_CHECK_NAMED_NIST },
41 };
42
43 static const OSSL_ITEM format_nameid_map[] = {
44     { (int)POINT_CONVERSION_UNCOMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_UNCOMPRESSED },
45     { (int)POINT_CONVERSION_COMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_COMPRESSED },
46     { (int)POINT_CONVERSION_HYBRID, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_HYBRID },
47 };
48
49 int ossl_ec_encoding_name2id(const char *name)
50 {
51     size_t i, sz;
52
53     /* Return the default value if there is no name */
54     if (name == NULL)
55         return OPENSSL_EC_NAMED_CURVE;
56
57     for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
58         if (OPENSSL_strcasecmp(name, encoding_nameid_map[i].ptr) == 0)
59             return encoding_nameid_map[i].id;
60     }
61     return -1;
62 }
63
64 static char *ec_param_encoding_id2name(int id)
65 {
66     size_t i, sz;
67
68     for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) {
69         if (id == (int)encoding_nameid_map[i].id)
70             return encoding_nameid_map[i].ptr;
71     }
72     return NULL;
73 }
74
75 char *ossl_ec_check_group_type_id2name(int id)
76 {
77     size_t i, sz;
78
79     for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
80         if (id == (int)check_group_type_nameid_map[i].id)
81             return check_group_type_nameid_map[i].ptr;
82     }
83     return NULL;
84 }
85
86 static int ec_check_group_type_name2id(const char *name)
87 {
88     size_t i, sz;
89
90     /* Return the default value if there is no name */
91     if (name == NULL)
92         return 0;
93
94     for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) {
95         if (OPENSSL_strcasecmp(name, check_group_type_nameid_map[i].ptr) == 0)
96             return check_group_type_nameid_map[i].id;
97     }
98     return -1;
99 }
100
101 int ossl_ec_set_check_group_type_from_name(EC_KEY *ec, const char *name)
102 {
103     int flags = ec_check_group_type_name2id(name);
104
105     if (flags == -1)
106         return 0;
107     EC_KEY_clear_flags(ec, EC_FLAG_CHECK_NAMED_GROUP_MASK);
108     EC_KEY_set_flags(ec, flags);
109     return 1;
110 }
111
112 static int ec_set_check_group_type_from_param(EC_KEY *ec, const OSSL_PARAM *p)
113 {
114     const char *name = NULL;
115     int status = 0;
116
117     switch (p->data_type) {
118     case OSSL_PARAM_UTF8_STRING:
119         name = p->data;
120         status = (name != NULL);
121         break;
122     case OSSL_PARAM_UTF8_PTR:
123         status = OSSL_PARAM_get_utf8_ptr(p, &name);
124         break;
125     }
126     if (status)
127         return ossl_ec_set_check_group_type_from_name(ec, name);
128     return 0;
129 }
130
131 int ossl_ec_pt_format_name2id(const char *name)
132 {
133     size_t i, sz;
134
135     /* Return the default value if there is no name */
136     if (name == NULL)
137         return (int)POINT_CONVERSION_UNCOMPRESSED;
138
139     for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
140         if (OPENSSL_strcasecmp(name, format_nameid_map[i].ptr) == 0)
141             return format_nameid_map[i].id;
142     }
143     return -1;
144 }
145
146 char *ossl_ec_pt_format_id2name(int id)
147 {
148     size_t i, sz;
149
150     for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) {
151         if (id == (int)format_nameid_map[i].id)
152             return format_nameid_map[i].ptr;
153     }
154     return NULL;
155 }
156
157 static int ec_group_explicit_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl,
158                                     OSSL_PARAM params[], BN_CTX *bnctx,
159                                     unsigned char **genbuf)
160 {
161     int ret = 0, fid;
162     const char *field_type;
163     const OSSL_PARAM *param = NULL;
164     const OSSL_PARAM *param_p = NULL;
165     const OSSL_PARAM *param_a = NULL;
166     const OSSL_PARAM *param_b = NULL;
167
168     fid = EC_GROUP_get_field_type(group);
169
170     if (fid == NID_X9_62_prime_field) {
171         field_type = SN_X9_62_prime_field;
172     } else if (fid == NID_X9_62_characteristic_two_field) {
173 #ifdef OPENSSL_NO_EC2M
174         ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
175         goto err;
176 #else
177         field_type = SN_X9_62_characteristic_two_field;
178 #endif
179     } else {
180         ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
181         return 0;
182     }
183
184     param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
185     param_a = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
186     param_b = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
187     if (tmpl != NULL || param_p != NULL || param_a != NULL || param_b != NULL)
188     {
189         BIGNUM *p = BN_CTX_get(bnctx);
190         BIGNUM *a = BN_CTX_get(bnctx);
191         BIGNUM *b = BN_CTX_get(bnctx);
192
193         if (b == NULL) {
194             ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
195             goto err;
196         }
197
198         if (!EC_GROUP_get_curve(group, p, a, b, bnctx)) {
199             ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
200             goto err;
201         }
202         if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_P, p)
203             || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_A, a)
204             || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_B, b)) {
205             ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
206             goto err;
207         }
208     }
209
210     param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
211     if (tmpl != NULL || param != NULL) {
212         const BIGNUM *order = EC_GROUP_get0_order(group);
213
214         if (order == NULL) {
215             ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
216             goto err;
217         }
218         if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_ORDER,
219                                     order)) {
220             ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
221             goto err;
222         }
223     }
224
225     param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
226     if (tmpl != NULL || param != NULL) {
227         if (!ossl_param_build_set_utf8_string(tmpl, params,
228                                               OSSL_PKEY_PARAM_EC_FIELD_TYPE,
229                                               field_type)) {
230             ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
231             goto err;
232         }
233     }
234
235     param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
236     if (tmpl != NULL || param != NULL) {
237         size_t genbuf_len;
238         const EC_POINT *genpt = EC_GROUP_get0_generator(group);
239         point_conversion_form_t genform = EC_GROUP_get_point_conversion_form(group);
240
241         if (genpt == NULL) {
242             ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
243             goto err;
244         }
245         genbuf_len = EC_POINT_point2buf(group, genpt, genform, genbuf, bnctx);
246         if (genbuf_len == 0) {
247             ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
248             goto err;
249         }
250         if (!ossl_param_build_set_octet_string(tmpl, params,
251                                                OSSL_PKEY_PARAM_EC_GENERATOR,
252                                                *genbuf, genbuf_len)) {
253             ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
254             goto err;
255         }
256     }
257
258     param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
259     if (tmpl != NULL || param != NULL) {
260         const BIGNUM *cofactor = EC_GROUP_get0_cofactor(group);
261
262         if (cofactor != NULL
263             && !ossl_param_build_set_bn(tmpl, params,
264                                         OSSL_PKEY_PARAM_EC_COFACTOR, cofactor)) {
265             ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
266             goto err;
267         }
268     }
269
270     param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
271     if (tmpl != NULL || param != NULL) {
272         unsigned char *seed = EC_GROUP_get0_seed(group);
273         size_t seed_len = EC_GROUP_get_seed_len(group);
274
275         if (seed != NULL
276             && seed_len > 0
277             && !ossl_param_build_set_octet_string(tmpl, params,
278                                                   OSSL_PKEY_PARAM_EC_SEED,
279                                                   seed, seed_len)) {
280             ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
281             goto err;
282         }
283     }
284     ret = 1;
285 err:
286     return ret;
287 }
288
289 int ossl_ec_group_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl,
290                          OSSL_PARAM params[], OSSL_LIB_CTX *libctx,
291                          const char *propq,
292                          BN_CTX *bnctx, unsigned char **genbuf)
293 {
294     int ret = 0, curve_nid, encoding_flag;
295     const char *encoding_name, *pt_form_name;
296     point_conversion_form_t genform;
297
298     if (group == NULL) {
299         ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER);
300         return 0;
301     }
302
303     genform = EC_GROUP_get_point_conversion_form(group);
304     pt_form_name = ossl_ec_pt_format_id2name(genform);
305     if (pt_form_name == NULL
306         || !ossl_param_build_set_utf8_string(
307                 tmpl, params,
308                 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, pt_form_name)) {
309         ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
310         return 0;
311     }
312     encoding_flag = EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE;
313     encoding_name = ec_param_encoding_id2name(encoding_flag);
314     if (encoding_name == NULL
315         || !ossl_param_build_set_utf8_string(tmpl, params,
316                                              OSSL_PKEY_PARAM_EC_ENCODING,
317                                              encoding_name)) {
318         ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
319         return 0;
320     }
321
322     if (!ossl_param_build_set_int(tmpl, params,
323                                   OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS,
324                                   group->decoded_from_explicit_params))
325         return 0;
326
327     curve_nid = EC_GROUP_get_curve_name(group);
328
329     /*
330      * Get the explicit parameters in these two cases:
331      * - We do not have a template, i.e. specific parameters are requested
332      * - The curve is not a named curve
333      */
334     if (tmpl == NULL || curve_nid == NID_undef)
335         if (!ec_group_explicit_todata(group, tmpl, params, bnctx, genbuf))
336             goto err;
337
338     if (curve_nid != NID_undef) {
339         /* Named curve */
340         const char *curve_name = OSSL_EC_curve_nid2name(curve_nid);
341
342         if (curve_name == NULL
343             || !ossl_param_build_set_utf8_string(tmpl, params,
344                                                  OSSL_PKEY_PARAM_GROUP_NAME,
345                                                  curve_name)) {
346             ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
347             goto err;
348         }
349     }
350     ret = 1;
351 err:
352     return ret;
353 }
354
355 /*
356  * The intention with the "backend" source file is to offer backend support
357  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
358  * implementations alike.
359  */
360 int ossl_ec_set_ecdh_cofactor_mode(EC_KEY *ec, int mode)
361 {
362     const EC_GROUP *ecg = EC_KEY_get0_group(ec);
363     const BIGNUM *cofactor;
364     /*
365      * mode can be only 0 for disable, or 1 for enable here.
366      *
367      * This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that
368      * also supports mode == -1 with the meaning of "reset to the default for
369      * the associated key".
370      */
371     if (mode < 0 || mode > 1)
372         return 0;
373
374     if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL )
375         return 0;
376
377     /* ECDH cofactor mode has no effect if cofactor is 1 */
378     if (BN_is_one(cofactor))
379         return 1;
380
381     if (mode == 1)
382         EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
383     else if (mode == 0)
384         EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
385
386     return 1;
387 }
388
389 /*
390  * Callers of ossl_ec_key_fromdata MUST make sure that ec_key_params_fromdata has
391  * been called before!
392  *
393  * This function only gets the bare keypair, domain parameters and other
394  * parameters are treated separately, and domain parameters are required to
395  * define a keypair.
396  */
397 int ossl_ec_key_fromdata(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
398 {
399     const OSSL_PARAM *param_priv_key = NULL, *param_pub_key = NULL;
400     BN_CTX *ctx = NULL;
401     BIGNUM *priv_key = NULL;
402     unsigned char *pub_key = NULL;
403     size_t pub_key_len;
404     const EC_GROUP *ecg = NULL;
405     EC_POINT *pub_point = NULL;
406     int ok = 0;
407
408     ecg = EC_KEY_get0_group(ec);
409     if (ecg == NULL)
410         return 0;
411
412     param_pub_key =
413         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
414     if (include_private)
415         param_priv_key =
416             OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
417
418     ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
419     if (ctx == NULL)
420         goto err;
421
422     if (param_pub_key != NULL)
423         if (!OSSL_PARAM_get_octet_string(param_pub_key,
424                                          (void **)&pub_key, 0, &pub_key_len)
425             || (pub_point = EC_POINT_new(ecg)) == NULL
426             || !EC_POINT_oct2point(ecg, pub_point, pub_key, pub_key_len, ctx))
427         goto err;
428
429     if (param_priv_key != NULL && include_private) {
430         int fixed_words;
431         const BIGNUM *order;
432
433         /*
434          * Key import/export should never leak the bit length of the secret
435          * scalar in the key.
436          *
437          * For this reason, on export we use padded BIGNUMs with fixed length.
438          *
439          * When importing we also should make sure that, even if short lived,
440          * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
441          * soon as possible, so that any processing of this BIGNUM might opt for
442          * constant time implementations in the backend.
443          *
444          * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
445          * to preallocate the BIGNUM internal buffer to a fixed public size big
446          * enough that operations performed during the processing never trigger
447          * a realloc which would leak the size of the scalar through memory
448          * accesses.
449          *
450          * Fixed Length
451          * ------------
452          *
453          * The order of the large prime subgroup of the curve is our choice for
454          * a fixed public size, as that is generally the upper bound for
455          * generating a private key in EC cryptosystems and should fit all valid
456          * secret scalars.
457          *
458          * For padding on export we just use the bit length of the order
459          * converted to bytes (rounding up).
460          *
461          * For preallocating the BIGNUM storage we look at the number of "words"
462          * required for the internal representation of the order, and we
463          * preallocate 2 extra "words" in case any of the subsequent processing
464          * might temporarily overflow the order length.
465          */
466         order = EC_GROUP_get0_order(ecg);
467         if (order == NULL || BN_is_zero(order))
468             goto err;
469
470         fixed_words = bn_get_top(order) + 2;
471
472         if ((priv_key = BN_secure_new()) == NULL)
473             goto err;
474         if (bn_wexpand(priv_key, fixed_words) == NULL)
475             goto err;
476         BN_set_flags(priv_key, BN_FLG_CONSTTIME);
477
478         if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
479             goto err;
480     }
481
482     if (priv_key != NULL
483         && !EC_KEY_set_private_key(ec, priv_key))
484         goto err;
485
486     if (pub_point != NULL
487         && !EC_KEY_set_public_key(ec, pub_point))
488         goto err;
489
490     ok = 1;
491
492  err:
493     BN_CTX_free(ctx);
494     BN_clear_free(priv_key);
495     OPENSSL_free(pub_key);
496     EC_POINT_free(pub_point);
497     return ok;
498 }
499
500 int ossl_ec_group_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
501 {
502     int ok = 0;
503     EC_GROUP *group = NULL;
504
505     if (ec == NULL)
506         return 0;
507
508      group = EC_GROUP_new_from_params(params, ossl_ec_key_get_libctx(ec),
509                                       ossl_ec_key_get0_propq(ec));
510
511     if (!EC_KEY_set_group(ec, group))
512         goto err;
513     ok = 1;
514 err:
515     EC_GROUP_free(group);
516     return ok;
517 }
518
519 static int ec_key_point_format_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
520 {
521     const OSSL_PARAM *p;
522     int format = -1;
523
524     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
525     if (p != NULL) {
526         if (!ossl_ec_pt_format_param2id(p, &format)) {
527             ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
528             return 0;
529         }
530         EC_KEY_set_conv_form(ec, format);
531     }
532     return 1;
533 }
534
535 static int ec_key_group_check_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
536 {
537     const OSSL_PARAM *p;
538
539     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE);
540     if (p != NULL)
541         return ec_set_check_group_type_from_param(ec, p);
542     return 1;
543 }
544
545 static int ec_set_include_public(EC_KEY *ec, int include)
546 {
547     int flags = EC_KEY_get_enc_flags(ec);
548
549     if (!include)
550         flags |= EC_PKEY_NO_PUBKEY;
551     else
552         flags &= ~EC_PKEY_NO_PUBKEY;
553     EC_KEY_set_enc_flags(ec, flags);
554     return 1;
555 }
556
557 int ossl_ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
558 {
559     const OSSL_PARAM *p;
560
561     if (ec == NULL)
562         return 0;
563
564     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
565     if (p != NULL) {
566         int mode;
567
568         if (!OSSL_PARAM_get_int(p, &mode)
569             || !ossl_ec_set_ecdh_cofactor_mode(ec, mode))
570             return 0;
571     }
572
573     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC);
574     if (p != NULL) {
575         int include = 1;
576
577         if (!OSSL_PARAM_get_int(p, &include)
578             || !ec_set_include_public(ec, include))
579             return 0;
580     }
581     if (!ec_key_point_format_fromdata(ec, params))
582         return 0;
583     if (!ec_key_group_check_fromdata(ec, params))
584         return 0;
585     return 1;
586 }
587
588 int ossl_ec_key_is_foreign(const EC_KEY *ec)
589 {
590 #ifndef FIPS_MODULE
591     if (ec->engine != NULL || EC_KEY_get_method(ec) != EC_KEY_OpenSSL())
592         return 1;
593 #endif
594     return 0;
595
596 }
597
598 EC_KEY *ossl_ec_key_dup(const EC_KEY *src, int selection)
599 {
600     EC_KEY *ret;
601
602     if (src == NULL) {
603         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
604         return NULL;
605     }
606
607     if ((ret = ossl_ec_key_new_method_int(src->libctx, src->propq,
608                                           src->engine)) == NULL)
609         return NULL;
610
611     /* copy the parameters */
612     if (src->group != NULL
613         && (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
614         ret->group = ossl_ec_group_new_ex(src->libctx, src->propq,
615                                           src->group->meth);
616         if (ret->group == NULL
617             || !EC_GROUP_copy(ret->group, src->group))
618             goto err;
619
620         if (src->meth != NULL) {
621 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
622             if (src->engine != NULL && ENGINE_init(src->engine) == 0)
623                 goto err;
624             ret->engine = src->engine;
625 #endif
626             ret->meth = src->meth;
627         }
628     }
629
630     /*  copy the public key */
631     if (src->pub_key != NULL
632         && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
633         if (ret->group == NULL)
634             /* no parameter-less keys allowed */
635             goto err;
636         ret->pub_key = EC_POINT_new(ret->group);
637         if (ret->pub_key == NULL
638             || !EC_POINT_copy(ret->pub_key, src->pub_key))
639                 goto err;
640     }
641
642     /* copy the private key */
643     if (src->priv_key != NULL
644         && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
645         if (ret->group == NULL)
646             /* no parameter-less keys allowed */
647             goto err;
648         ret->priv_key = BN_new();
649         if (ret->priv_key == NULL || !BN_copy(ret->priv_key, src->priv_key))
650             goto err;
651         if (ret->group->meth->keycopy
652             && ret->group->meth->keycopy(ret, src) == 0)
653             goto err;
654     }
655
656     /* copy the rest */
657     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
658         ret->enc_flag = src->enc_flag;
659         ret->conv_form = src->conv_form;
660     }
661
662     ret->version = src->version;
663     ret->flags = src->flags;
664
665 #ifndef FIPS_MODULE
666     if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
667                             &ret->ex_data, &src->ex_data))
668         goto err;
669 #endif
670
671     if (ret->meth != NULL && ret->meth->copy != NULL) {
672         if ((selection
673              & OSSL_KEYMGMT_SELECT_KEYPAIR) != OSSL_KEYMGMT_SELECT_KEYPAIR)
674             goto err;
675         if (ret->meth->copy(ret, src) == 0)
676             goto err;
677     }
678
679     return ret;
680  err:
681     EC_KEY_free(ret);
682     return NULL;
683 }
684
685 int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id)
686 {
687     const char *name = NULL;
688     int status = 0;
689
690     switch (p->data_type) {
691     case OSSL_PARAM_UTF8_STRING:
692         /* The OSSL_PARAM functions have no support for this */
693         name = p->data;
694         status = (name != NULL);
695         break;
696     case OSSL_PARAM_UTF8_PTR:
697         status = OSSL_PARAM_get_utf8_ptr(p, &name);
698         break;
699     }
700     if (status) {
701         int i = ossl_ec_encoding_name2id(name);
702
703         if (i >= 0) {
704             *id = i;
705             return 1;
706         }
707     }
708     return 0;
709 }
710
711 int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id)
712 {
713     const char *name = NULL;
714     int status = 0;
715
716     switch (p->data_type) {
717     case OSSL_PARAM_UTF8_STRING:
718         /* The OSSL_PARAM functions have no support for this */
719         name = p->data;
720         status = (name != NULL);
721         break;
722     case OSSL_PARAM_UTF8_PTR:
723         status = OSSL_PARAM_get_utf8_ptr(p, &name);
724         break;
725     }
726     if (status) {
727         int i = ossl_ec_pt_format_name2id(name);
728
729         if (i >= 0) {
730             *id = i;
731             return 1;
732         }
733     }
734     return 0;
735 }
736
737 #ifndef FIPS_MODULE
738 int ossl_x509_algor_is_sm2(const X509_ALGOR *palg)
739 {
740     int ptype = 0;
741     const void *pval = NULL;
742
743     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
744
745     if (ptype == V_ASN1_OBJECT)
746         return OBJ_obj2nid((ASN1_OBJECT *)pval) == NID_sm2;
747
748     if (ptype == V_ASN1_SEQUENCE) {
749         const ASN1_STRING *str = pval;
750         const unsigned char *der = str->data;
751         int derlen = str->length;
752         EC_GROUP *group;
753         int ret;
754
755         if ((group = d2i_ECPKParameters(NULL, &der, derlen)) == NULL)
756             ret = 0;
757         else
758             ret = (EC_GROUP_get_curve_name(group) == NID_sm2);
759
760         EC_GROUP_free(group);
761         return ret;
762     }
763
764     return 0;
765 }
766
767 EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg,
768                                      OSSL_LIB_CTX *libctx, const char *propq)
769 {
770     int ptype = 0;
771     const void *pval = NULL;
772     EC_KEY *eckey = NULL;
773     EC_GROUP *group = NULL;
774
775     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
776     if ((eckey = EC_KEY_new_ex(libctx, propq)) == NULL) {
777         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
778         goto ecerr;
779     }
780
781     if (ptype == V_ASN1_SEQUENCE) {
782         const ASN1_STRING *pstr = pval;
783         const unsigned char *pm = pstr->data;
784         int pmlen = pstr->length;
785
786
787         if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) {
788             ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
789             goto ecerr;
790         }
791     } else if (ptype == V_ASN1_OBJECT) {
792         const ASN1_OBJECT *poid = pval;
793
794         /*
795          * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
796          */
797
798         group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid));
799         if (group == NULL)
800             goto ecerr;
801         EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
802         if (EC_KEY_set_group(eckey, group) == 0)
803             goto ecerr;
804         EC_GROUP_free(group);
805     } else {
806         ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
807         goto ecerr;
808     }
809
810     return eckey;
811
812  ecerr:
813     EC_KEY_free(eckey);
814     EC_GROUP_free(group);
815     return NULL;
816 }
817
818 EC_KEY *ossl_ec_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
819                                OSSL_LIB_CTX *libctx, const char *propq)
820 {
821     const unsigned char *p = NULL;
822     int pklen;
823     EC_KEY *eckey = NULL;
824     const X509_ALGOR *palg;
825
826     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
827         return 0;
828     eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
829     if (eckey == NULL)
830         goto err;
831
832     /* We have parameters now set private key */
833     if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
834         ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
835         goto err;
836     }
837
838     return eckey;
839  err:
840     EC_KEY_free(eckey);
841     return NULL;
842 }
843 #endif