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