When calling EC_POINT_point2buf we must use a libctx
[openssl.git] / providers / implementations / keymgmt / ec_kmgmt.c
1 /*
2  * Copyright 2020 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  * ECDH/ECDSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <openssl/core_numbers.h>
17 #include <openssl/core_names.h>
18 #include <openssl/bn.h>
19 #include <openssl/err.h>
20 #include <openssl/objects.h>
21 #include "crypto/bn.h"
22 #include "crypto/ec.h"
23 #include "prov/implementations.h"
24 #include "prov/providercommon.h"
25 #include "prov/providercommonerr.h"
26 #include "prov/provider_ctx.h"
27 #include "internal/param_build_set.h"
28
29 static OSSL_OP_keymgmt_new_fn ec_newdata;
30 static OSSL_OP_keymgmt_gen_init_fn ec_gen_init;
31 static OSSL_OP_keymgmt_gen_set_template_fn ec_gen_set_template;
32 static OSSL_OP_keymgmt_gen_set_params_fn ec_gen_set_params;
33 static OSSL_OP_keymgmt_gen_settable_params_fn ec_gen_settable_params;
34 static OSSL_OP_keymgmt_gen_get_params_fn ec_gen_get_params;
35 static OSSL_OP_keymgmt_gen_gettable_params_fn ec_gen_gettable_params;
36 static OSSL_OP_keymgmt_gen_fn ec_gen;
37 static OSSL_OP_keymgmt_gen_cleanup_fn ec_gen_cleanup;
38 static OSSL_OP_keymgmt_free_fn ec_freedata;
39 static OSSL_OP_keymgmt_get_params_fn ec_get_params;
40 static OSSL_OP_keymgmt_gettable_params_fn ec_gettable_params;
41 static OSSL_OP_keymgmt_set_params_fn ec_set_params;
42 static OSSL_OP_keymgmt_settable_params_fn ec_settable_params;
43 static OSSL_OP_keymgmt_has_fn ec_has;
44 static OSSL_OP_keymgmt_match_fn ec_match;
45 static OSSL_OP_keymgmt_validate_fn ec_validate;
46 static OSSL_OP_keymgmt_import_fn ec_import;
47 static OSSL_OP_keymgmt_import_types_fn ec_import_types;
48 static OSSL_OP_keymgmt_export_fn ec_export;
49 static OSSL_OP_keymgmt_export_types_fn ec_export_types;
50 static OSSL_OP_keymgmt_query_operation_name_fn ec_query_operation_name;
51
52 #define EC_POSSIBLE_SELECTIONS                                                 \
53     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
54
55 static
56 const char *ec_query_operation_name(int operation_id)
57 {
58     switch (operation_id) {
59     case OSSL_OP_KEYEXCH:
60         return "ECDH";
61     case OSSL_OP_SIGNATURE:
62         return "ECDSA";
63     }
64     return NULL;
65 }
66
67 static ossl_inline
68 int domparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl,
69                         OSSL_PARAM params[])
70 {
71     const EC_GROUP *ecg;
72     int curve_nid;
73
74     if (ec == NULL)
75         return 0;
76
77     ecg = EC_KEY_get0_group(ec);
78     if (ecg == NULL)
79         return 0;
80
81     curve_nid = EC_GROUP_get_curve_name(ecg);
82
83     if (curve_nid == NID_undef) {
84         /* TODO(3.0): should we support explicit parameters curves? */
85         return 0;
86     } else {
87         /* named curve */
88         const char *curve_name = NULL;
89
90         if ((curve_name = ec_curve_nid2name(curve_nid)) == NULL)
91             return 0;
92         if (!ossl_param_build_set_utf8_string(tmpl, params,
93                                               OSSL_PKEY_PARAM_EC_NAME,
94                                               curve_name))
95
96             return 0;
97     }
98
99     return 1;
100 }
101
102 /*
103  * Callers of key_to_params MUST make sure that domparams_to_params is also
104  * called!
105  *
106  * This function only exports the bare keypair, domain parameters and other
107  * parameters are exported separately.
108  */
109 static ossl_inline
110 int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl,
111                   OSSL_PARAM params[], int include_private,
112                   unsigned char **pub_key)
113 {
114     const BIGNUM *priv_key = NULL;
115     const EC_POINT *pub_point = NULL;
116     const EC_GROUP *ecg = NULL;
117     size_t pub_key_len = 0;
118     int ret = 0;
119     BN_CTX *bnctx = NULL;
120
121     if (eckey == NULL
122         || (ecg = EC_KEY_get0_group(eckey)) == NULL)
123         return 0;
124
125     priv_key = EC_KEY_get0_private_key(eckey);
126     pub_point = EC_KEY_get0_public_key(eckey);
127
128     if (pub_point != NULL) {
129         /*
130          * EC_POINT_point2buf() can generate random numbers in some
131          * implementations so we need to ensure we use the correct libctx.
132          */
133         bnctx = BN_CTX_new_ex(ec_key_get_libctx(eckey));
134         if (bnctx == NULL)
135             goto err;
136
137         /* convert pub_point to a octet string according to the SECG standard */
138         if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
139                                               POINT_CONVERSION_COMPRESSED,
140                                               pub_key, bnctx)) == 0
141             || !ossl_param_build_set_octet_string(tmpl, params,
142                                                   OSSL_PKEY_PARAM_PUB_KEY,
143                                                   *pub_key, pub_key_len))
144             goto err;
145     }
146
147     if (priv_key != NULL && include_private) {
148         size_t sz;
149         int ecbits;
150
151         /*
152          * Key import/export should never leak the bit length of the secret
153          * scalar in the key.
154          *
155          * For this reason, on export we use padded BIGNUMs with fixed length.
156          *
157          * When importing we also should make sure that, even if short lived,
158          * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
159          * soon as possible, so that any processing of this BIGNUM might opt for
160          * constant time implementations in the backend.
161          *
162          * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
163          * to preallocate the BIGNUM internal buffer to a fixed public size big
164          * enough that operations performed during the processing never trigger
165          * a realloc which would leak the size of the scalar through memory
166          * accesses.
167          *
168          * Fixed Length
169          * ------------
170          *
171          * The order of the large prime subgroup of the curve is our choice for
172          * a fixed public size, as that is generally the upper bound for
173          * generating a private key in EC cryptosystems and should fit all valid
174          * secret scalars.
175          *
176          * For padding on export we just use the bit length of the order
177          * converted to bytes (rounding up).
178          *
179          * For preallocating the BIGNUM storage we look at the number of "words"
180          * required for the internal representation of the order, and we
181          * preallocate 2 extra "words" in case any of the subsequent processing
182          * might temporarily overflow the order length.
183          */
184         ecbits = EC_GROUP_order_bits(ecg);
185         if (ecbits <= 0)
186             goto err;
187         sz = (ecbits + 7 ) / 8;
188
189         if (!ossl_param_build_set_bn_pad(tmpl, params,
190                                          OSSL_PKEY_PARAM_PRIV_KEY,
191                                          priv_key, sz))
192             goto err;
193     }
194     ret = 1;
195  err:
196     BN_CTX_free(bnctx);
197     return ret;
198 }
199
200 static ossl_inline
201 int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl,
202                           OSSL_PARAM params[])
203 {
204     int ecdh_cofactor_mode = 0;
205
206     if (ec == NULL)
207         return 0;
208
209     ecdh_cofactor_mode =
210         (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
211     return ossl_param_build_set_int(tmpl, params,
212                                     OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
213                                     ecdh_cofactor_mode);
214 }
215
216 static
217 void *ec_newdata(void *provctx)
218 {
219     return EC_KEY_new_ex(PROV_LIBRARY_CONTEXT_OF(provctx));
220 }
221
222 static
223 void ec_freedata(void *keydata)
224 {
225     EC_KEY_free(keydata);
226 }
227
228 static
229 int ec_has(void *keydata, int selection)
230 {
231     EC_KEY *ec = keydata;
232     int ok = 0;
233
234     if (ec != NULL) {
235         if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
236             ok = 1;
237
238         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
239             ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
240         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
241             ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
242         if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
243             ok = ok && (EC_KEY_get0_group(ec) != NULL);
244         /*
245          * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
246          * available, so no extra check is needed other than the previous one
247          * against EC_POSSIBLE_SELECTIONS.
248          */
249     }
250     return ok;
251 }
252
253 static int ec_match(const void *keydata1, const void *keydata2, int selection)
254 {
255     const EC_KEY *ec1 = keydata1;
256     const EC_KEY *ec2 = keydata2;
257     const EC_GROUP *group_a = EC_KEY_get0_group(ec1);
258     const EC_GROUP *group_b = EC_KEY_get0_group(ec2);
259     int ok = 1;
260
261     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
262         ok = ok && group_a != NULL && group_b != NULL
263             && EC_GROUP_cmp(group_a, group_b, NULL) == 0;
264     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
265         const BIGNUM *pa = EC_KEY_get0_private_key(ec1);
266         const BIGNUM *pb = EC_KEY_get0_private_key(ec2);
267
268         ok = ok && BN_cmp(pa, pb) == 0;
269     }
270     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
271         const EC_POINT *pa = EC_KEY_get0_public_key(ec1);
272         const EC_POINT *pb = EC_KEY_get0_public_key(ec2);
273
274         ok = ok && EC_POINT_cmp(group_b, pa, pb, NULL);
275     }
276     return ok;
277 }
278
279 static
280 int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
281 {
282     EC_KEY *ec = keydata;
283     int ok = 1;
284
285     if (ec == NULL)
286         return 0;
287
288     /*
289      * In this implementation, we can export/import only keydata in the
290      * following combinations:
291      *   - domain parameters only
292      *   - public key with associated domain parameters (+optional other params)
293      *   - private key with associated public key and domain parameters
294      *         (+optional other params)
295      *
296      * This means:
297      *   - domain parameters must always be requested
298      *   - private key must be requested alongside public key
299      *   - other parameters must be requested only alongside a key
300      */
301     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
302         return 0;
303     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
304             && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
305         return 0;
306     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
307             && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
308         return 0;
309
310     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
311         ok = ok && ec_key_domparams_fromdata(ec, params);
312     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
313         int include_private =
314             selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
315
316         ok = ok && ec_key_fromdata(ec, params, include_private);
317     }
318     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
319         ok = ok && ec_key_otherparams_fromdata(ec, params);
320
321     return ok;
322 }
323
324 static
325 int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
326               void *cbarg)
327 {
328     EC_KEY *ec = keydata;
329     OSSL_PARAM_BLD *tmpl;
330     OSSL_PARAM *params = NULL;
331     unsigned char *pub_key = NULL;
332     int ok = 1;
333
334     if (ec == NULL)
335         return 0;
336
337     /*
338      * In this implementation, we can export/import only keydata in the
339      * following combinations:
340      *   - domain parameters only
341      *   - public key with associated domain parameters (+optional other params)
342      *   - private key with associated public key and domain parameters
343      *         (+optional other params)
344      *
345      * This means:
346      *   - domain parameters must always be requested
347      *   - private key must be requested alongside public key
348      *   - other parameters must be requested only alongside a key
349      */
350     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
351         return 0;
352     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
353             && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
354         return 0;
355     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
356             && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
357         return 0;
358
359     tmpl = OSSL_PARAM_BLD_new();
360     if (tmpl == NULL)
361         return 0;
362
363     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
364         ok = ok && domparams_to_params(ec, tmpl, NULL);
365
366     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
367         int include_private =
368             selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
369
370         ok = ok && key_to_params(ec, tmpl, NULL, include_private, &pub_key);
371     }
372     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
373         ok = ok && otherparams_to_params(ec, tmpl, NULL);
374
375     if (ok && (params = OSSL_PARAM_BLD_to_param(tmpl)) != NULL)
376         ok = param_cb(params, cbarg);
377
378     OSSL_PARAM_BLD_free_params(params);
379     OSSL_PARAM_BLD_free(tmpl);
380     OPENSSL_free(pub_key);
381     return ok;
382 }
383
384 /* IMEXPORT = IMPORT + EXPORT */
385
386 # define EC_IMEXPORTABLE_DOM_PARAMETERS                          \
387     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_NAME, NULL, 0)
388 # define EC_IMEXPORTABLE_PUBLIC_KEY                              \
389     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
390 # define EC_IMEXPORTABLE_PRIVATE_KEY                             \
391     OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
392 # define EC_IMEXPORTABLE_OTHER_PARAMETERS                        \
393     OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL)
394
395 /*
396  * Include all the possible combinations of OSSL_PARAM arrays for
397  * ec_imexport_types().
398  *
399  * They are in a separate file as it is ~100 lines of unreadable and
400  * uninteresting machine generated stuff.
401  *
402  * TODO(3.0): the generated list looks quite ugly, as to cover all possible
403  * combinations of the bits in `selection`, it also includes combinations that
404  * are not really useful: we might want to consider alternatives to this
405  * solution.
406  */
407 #include "ec_kmgmt_imexport.inc"
408
409 static ossl_inline
410 const OSSL_PARAM *ec_imexport_types(int selection)
411 {
412     int type_select = 0;
413
414     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
415         type_select += 1;
416     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
417         type_select += 2;
418     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
419         type_select += 4;
420     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
421         type_select += 8;
422     return ec_types[type_select];
423 }
424
425 static
426 const OSSL_PARAM *ec_import_types(int selection)
427 {
428     return ec_imexport_types(selection);
429 }
430
431 static
432 const OSSL_PARAM *ec_export_types(int selection)
433 {
434     return ec_imexport_types(selection);
435 }
436
437 static
438 int ec_get_params(void *key, OSSL_PARAM params[])
439 {
440     int ret;
441     EC_KEY *eck = key;
442     const EC_GROUP *ecg = NULL;
443     OSSL_PARAM *p;
444     unsigned char *pub_key = NULL;
445
446     ecg = EC_KEY_get0_group(eck);
447     if (ecg == NULL)
448         return 0;
449
450     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
451         && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
452         return 0;
453     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
454         && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
455         return 0;
456     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) {
457         int ecbits, sec_bits;
458
459         ecbits = EC_GROUP_order_bits(ecg);
460
461         /*
462          * The following estimates are based on the values published
463          * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
464          * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
465          *
466          * Note that the above reference explicitly categorizes algorithms in a
467          * discrete set of values {80, 112, 128, 192, 256}, and that it is
468          * relevant only for NIST approved Elliptic Curves, while OpenSSL
469          * applies the same logic also to other curves.
470          *
471          * Classifications produced by other standardazing bodies might differ,
472          * so the results provided for "bits of security" by this provider are
473          * to be considered merely indicative, and it is the users'
474          * responsibility to compare these values against the normative
475          * references that may be relevant for their intent and purposes.
476          */
477         if (ecbits >= 512)
478             sec_bits = 256;
479         else if (ecbits >= 384)
480             sec_bits = 192;
481         else if (ecbits >= 256)
482             sec_bits = 128;
483         else if (ecbits >= 224)
484             sec_bits = 112;
485         else if (ecbits >= 160)
486             sec_bits = 80;
487         else
488             sec_bits = ecbits / 2;
489
490         if (!OSSL_PARAM_set_int(p, sec_bits))
491             return 0;
492     }
493
494     p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
495     if (p != NULL) {
496         int ecdh_cofactor_mode = 0;
497
498         ecdh_cofactor_mode =
499             (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
500
501         if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
502             return 0;
503     }
504     ret = domparams_to_params(eck, NULL, params)
505           && key_to_params(eck, NULL, params, 1, &pub_key)
506           && otherparams_to_params(eck, NULL, params);
507     OPENSSL_free(pub_key);
508     return ret;
509 }
510
511 static const OSSL_PARAM ec_known_gettable_params[] = {
512     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
513     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
514     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
515     EC_IMEXPORTABLE_DOM_PARAMETERS,
516     EC_IMEXPORTABLE_PUBLIC_KEY,
517     EC_IMEXPORTABLE_PRIVATE_KEY,
518     EC_IMEXPORTABLE_OTHER_PARAMETERS,
519     OSSL_PARAM_END
520 };
521
522 static
523 const OSSL_PARAM *ec_gettable_params(void)
524 {
525     return ec_known_gettable_params;
526 }
527
528 static const OSSL_PARAM ec_known_settable_params[] = {
529     OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
530     OSSL_PARAM_END
531 };
532
533 static
534 const OSSL_PARAM *ec_settable_params(void)
535 {
536     return ec_known_settable_params;
537 }
538
539 static
540 int ec_set_params(void *key, const OSSL_PARAM params[])
541 {
542     EC_KEY *eck = key;
543     const OSSL_PARAM *p;
544
545     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
546     if (p != NULL && !ec_set_param_ecdh_cofactor_mode(eck, p))
547         return 0;
548
549     return 1;
550 }
551
552 static
553 int ec_validate(void *keydata, int selection)
554 {
555     EC_KEY *eck = keydata;
556     int ok = 0;
557     BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(eck));
558
559     if (ctx == NULL)
560         return 0;
561
562     if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
563         ok = 1;
564
565     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
566         ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
567
568     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
569         ok = ok && ec_key_public_check(eck, ctx);
570
571     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
572         ok = ok && ec_key_private_check(eck);
573
574     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
575         ok = ok && ec_key_pairwise_check(eck, ctx);
576
577     BN_CTX_free(ctx);
578     return ok;
579 }
580
581 struct ec_gen_ctx {
582     OPENSSL_CTX *libctx;
583
584     EC_GROUP *gen_group;
585     int selection;
586 };
587
588 static void *ec_gen_init(void *provctx, int selection)
589 {
590     OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
591     struct ec_gen_ctx *gctx = NULL;
592
593     if ((selection & (EC_POSSIBLE_SELECTIONS)) == 0)
594         return NULL;
595
596     if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
597         gctx->libctx = libctx;
598         gctx->gen_group = NULL;
599         gctx->selection = selection;
600     }
601     return gctx;
602 }
603
604 static int ec_gen_set_group(void *genctx, int nid)
605 {
606     struct ec_gen_ctx *gctx = genctx;
607     EC_GROUP *group;
608
609     group = EC_GROUP_new_by_curve_name_ex(gctx->libctx, nid);
610     if (group == NULL) {
611         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
612         return 0;
613     }
614     EC_GROUP_free(gctx->gen_group);
615     gctx->gen_group = group;
616     return 1;
617 }
618 static int ec_gen_set_template(void *genctx, void *templ)
619 {
620     struct ec_gen_ctx *gctx = genctx;
621     EC_KEY *ec = templ;
622     const EC_GROUP *ec_group;
623
624     if (gctx == NULL || ec == NULL)
625         return 0;
626     if ((ec_group = EC_KEY_get0_group(ec)) == NULL)
627         return 0;
628     return ec_gen_set_group(gctx, EC_GROUP_get_curve_name(ec_group));
629 }
630
631 static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[])
632 {
633     struct ec_gen_ctx *gctx = genctx;
634     const OSSL_PARAM *p;
635
636     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME))
637         != NULL) {
638         const char *curve_name = NULL;
639         int ret = 0;
640
641         switch (p->data_type) {
642         case OSSL_PARAM_UTF8_STRING:
643             /* The OSSL_PARAM functions have no support for this */
644             curve_name = p->data;
645             ret = (curve_name != NULL);
646             break;
647         case OSSL_PARAM_UTF8_PTR:
648             ret = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
649             break;
650         }
651
652         if (ret) {
653             int nid = ec_curve_name2nid(curve_name);
654
655             if (nid == NID_undef) {
656                 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
657                 ret = 0;
658             } else {
659                 ret = ec_gen_set_group(gctx, nid);
660             }
661         }
662         return ret;
663     }
664     return 1;
665 }
666
667 static const OSSL_PARAM *ec_gen_settable_params(void *provctx)
668 {
669     static OSSL_PARAM settable[] = {
670         { OSSL_PKEY_PARAM_EC_NAME, OSSL_PARAM_UTF8_STRING, NULL, 0, 0 },
671         OSSL_PARAM_END
672     };
673
674     return settable;
675 }
676
677 static int ec_gen_get_params(void *genctx, OSSL_PARAM params[])
678 {
679     struct ec_gen_ctx *gctx = genctx;
680     OSSL_PARAM *p;
681
682     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_NAME)) != NULL) {
683         int nid = EC_GROUP_get_curve_name(gctx->gen_group);
684         int ret = 0;
685         const char *curve_name = ec_curve_nid2name(nid);
686
687         switch (p->data_type) {
688         case OSSL_PARAM_UTF8_STRING:
689             ret = OSSL_PARAM_set_utf8_string(p, curve_name);
690             break;
691         case OSSL_PARAM_UTF8_PTR:
692             ret = OSSL_PARAM_set_utf8_ptr(p, curve_name);
693             break;
694         }
695         return ret;
696     }
697     return 1;
698 }
699
700 static const OSSL_PARAM *ec_gen_gettable_params(void *provctx)
701 {
702     static OSSL_PARAM gettable[] = {
703         { OSSL_PKEY_PARAM_EC_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
704         OSSL_PARAM_END
705     };
706
707     return gettable;
708 }
709
710 static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group)
711 {
712     if (group == NULL) {
713         ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
714         return 0;
715     }
716     return EC_KEY_set_group(ec, group) > 0;
717 }
718
719 /*
720  * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
721  */
722 static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
723 {
724     struct ec_gen_ctx *gctx = genctx;
725     EC_KEY *ec = NULL;
726     int ret = 1;                 /* Start optimistically */
727
728     if (gctx == NULL
729         || (ec = EC_KEY_new_ex(gctx->libctx)) == NULL)
730         return NULL;
731
732     /* We must always assign a group, no matter what */
733     ret = ec_gen_assign_group(ec, gctx->gen_group);
734     /* Whether you want it or not, you get a keypair, not just one half */
735     if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
736         ret = ret && EC_KEY_generate_key(ec);
737
738     if (ret)
739         return ec;
740
741     /* Something went wrong, throw the key away */
742     EC_KEY_free(ec);
743     return NULL;
744 }
745
746 static void ec_gen_cleanup(void *genctx)
747 {
748     struct ec_gen_ctx *gctx = genctx;
749
750     if (gctx == NULL)
751         return;
752
753     EC_GROUP_free(gctx->gen_group);
754     OPENSSL_free(gctx);
755 }
756
757 const OSSL_DISPATCH ec_keymgmt_functions[] = {
758     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
759     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init },
760     { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
761       (void (*)(void))ec_gen_set_template },
762     { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
763     { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
764       (void (*)(void))ec_gen_settable_params },
765     { OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS, (void (*)(void))ec_gen_get_params },
766     { OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS,
767       (void (*)(void))ec_gen_gettable_params },
768     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ec_gen },
769     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
770     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
771     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params },
772     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params },
773     { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
774     { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params },
775     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
776     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
777     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
778     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
779     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
780     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
781     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
782     { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
783       (void (*)(void))ec_query_operation_name },
784     { 0, NULL }
785 };