1 /*
2  * Copyright 2020-2021 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 "e_os.h" /* strcasecmp */
17 #include <string.h>
18 #include <openssl/core_dispatch.h>
19 #include <openssl/core_names.h>
20 #include <openssl/bn.h>
21 #include <openssl/err.h>
22 #include <openssl/objects.h>
23 #include <openssl/proverr.h>
24 #include "crypto/bn.h"
25 #include "crypto/ec.h"
26 #include "prov/implementations.h"
27 #include "prov/providercommon.h"
28 #include "prov/provider_ctx.h"
29 #include "internal/param_build_set.h"
30 
31 #ifndef FIPS_MODULE
32 # ifndef OPENSSL_NO_SM2
33 #  include "crypto/sm2.h"
34 # endif
35 #endif
36 
37 static OSSL_FUNC_keymgmt_new_fn ec_newdata;
38 static OSSL_FUNC_keymgmt_gen_init_fn ec_gen_init;
39 static OSSL_FUNC_keymgmt_gen_set_template_fn ec_gen_set_template;
40 static OSSL_FUNC_keymgmt_gen_set_params_fn ec_gen_set_params;
41 static OSSL_FUNC_keymgmt_gen_settable_params_fn ec_gen_settable_params;
42 static OSSL_FUNC_keymgmt_gen_fn ec_gen;
43 static OSSL_FUNC_keymgmt_gen_cleanup_fn ec_gen_cleanup;
44 static OSSL_FUNC_keymgmt_load_fn ec_load;
45 static OSSL_FUNC_keymgmt_free_fn ec_freedata;
46 static OSSL_FUNC_keymgmt_get_params_fn ec_get_params;
47 static OSSL_FUNC_keymgmt_gettable_params_fn ec_gettable_params;
48 static OSSL_FUNC_keymgmt_set_params_fn ec_set_params;
49 static OSSL_FUNC_keymgmt_settable_params_fn ec_settable_params;
50 static OSSL_FUNC_keymgmt_has_fn ec_has;
51 static OSSL_FUNC_keymgmt_match_fn ec_match;
52 static OSSL_FUNC_keymgmt_validate_fn ec_validate;
53 static OSSL_FUNC_keymgmt_import_fn ec_import;
54 static OSSL_FUNC_keymgmt_import_types_fn ec_import_types;
55 static OSSL_FUNC_keymgmt_export_fn ec_export;
56 static OSSL_FUNC_keymgmt_export_types_fn ec_export_types;
57 static OSSL_FUNC_keymgmt_query_operation_name_fn ec_query_operation_name;
58 static OSSL_FUNC_keymgmt_dup_fn ec_dup;
59 #ifndef FIPS_MODULE
60 # ifndef OPENSSL_NO_SM2
61 static OSSL_FUNC_keymgmt_new_fn sm2_newdata;
62 static OSSL_FUNC_keymgmt_gen_init_fn sm2_gen_init;
63 static OSSL_FUNC_keymgmt_gen_fn sm2_gen;
64 static OSSL_FUNC_keymgmt_get_params_fn sm2_get_params;
65 static OSSL_FUNC_keymgmt_gettable_params_fn sm2_gettable_params;
66 static OSSL_FUNC_keymgmt_settable_params_fn sm2_settable_params;
67 static OSSL_FUNC_keymgmt_import_fn sm2_import;
68 static OSSL_FUNC_keymgmt_query_operation_name_fn sm2_query_operation_name;
69 static OSSL_FUNC_keymgmt_validate_fn sm2_validate;
70 # endif
71 #endif
72 
73 #define EC_DEFAULT_MD "SHA256"
74 #define EC_POSSIBLE_SELECTIONS                                                 \
75     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
76 #define SM2_DEFAULT_MD "SM3"
77 
78 static
ec_query_operation_name(int operation_id)79 const char *ec_query_operation_name(int operation_id)
80 {
81     switch (operation_id) {
82     case OSSL_OP_KEYEXCH:
83         return "ECDH";
84     case OSSL_OP_SIGNATURE:
85         return "ECDSA";
86     }
87     return NULL;
88 }
89 
90 #ifndef FIPS_MODULE
91 # ifndef OPENSSL_NO_SM2
92 static
sm2_query_operation_name(int operation_id)93 const char *sm2_query_operation_name(int operation_id)
94 {
95     switch (operation_id) {
96     case OSSL_OP_SIGNATURE:
97         return "SM2";
98     }
99     return NULL;
100 }
101 # endif
102 #endif
103 
104 /*
105  * Callers of key_to_params MUST make sure that domparams_to_params is also
106  * called!
107  *
108  * This function only exports the bare keypair, domain parameters and other
109  * parameters are exported separately.
110  */
111 static ossl_inline
key_to_params(const EC_KEY * eckey,OSSL_PARAM_BLD * tmpl,OSSL_PARAM params[],int include_private,unsigned char ** pub_key)112 int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl,
113                   OSSL_PARAM params[], int include_private,
114                   unsigned char **pub_key)
115 {
116     BIGNUM *x = NULL, *y = NULL;
117     const BIGNUM *priv_key = NULL;
118     const EC_POINT *pub_point = NULL;
119     const EC_GROUP *ecg = NULL;
120     size_t pub_key_len = 0;
121     int ret = 0;
122     BN_CTX *bnctx = NULL;
123 
124     if (eckey == NULL
125         || (ecg = EC_KEY_get0_group(eckey)) == NULL)
126         return 0;
127 
128     priv_key = EC_KEY_get0_private_key(eckey);
129     pub_point = EC_KEY_get0_public_key(eckey);
130 
131     if (pub_point != NULL) {
132         OSSL_PARAM *p = NULL, *px = NULL, *py = NULL;
133         /*
134          * EC_POINT_point2buf() can generate random numbers in some
135          * implementations so we need to ensure we use the correct libctx.
136          */
137         bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey));
138         if (bnctx == NULL)
139             goto err;
140 
141 
142         /* If we are doing a get then check first before decoding the point */
143         if (tmpl == NULL) {
144             p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY);
145             px = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_X);
146             py = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_Y);
147         }
148 
149         if (p != NULL || tmpl != NULL) {
150             /* convert pub_point to a octet string according to the SECG standard */
151             if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
152                                                   POINT_CONVERSION_COMPRESSED,
153                                                   pub_key, bnctx)) == 0
154                 || !ossl_param_build_set_octet_string(tmpl, p,
155                                                       OSSL_PKEY_PARAM_PUB_KEY,
156                                                       *pub_key, pub_key_len))
157                 goto err;
158         }
159         if (px != NULL || py != NULL) {
160             if (px != NULL)
161                 x = BN_CTX_get(bnctx);
162             if (py != NULL)
163                 y = BN_CTX_get(bnctx);
164 
165             if (!EC_POINT_get_affine_coordinates(ecg, pub_point, x, y, bnctx))
166                 goto err;
167             if (px != NULL
168                 && !ossl_param_build_set_bn(tmpl, px,
169                                             OSSL_PKEY_PARAM_EC_PUB_X, x))
170                 goto err;
171             if (py != NULL
172                 && !ossl_param_build_set_bn(tmpl, py,
173                                             OSSL_PKEY_PARAM_EC_PUB_Y, y))
174                 goto err;
175         }
176     }
177 
178     if (priv_key != NULL && include_private) {
179         size_t sz;
180         int ecbits;
181 
182         /*
183          * Key import/export should never leak the bit length of the secret
184          * scalar in the key.
185          *
186          * For this reason, on export we use padded BIGNUMs with fixed length.
187          *
188          * When importing we also should make sure that, even if short lived,
189          * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
190          * soon as possible, so that any processing of this BIGNUM might opt for
191          * constant time implementations in the backend.
192          *
193          * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
194          * to preallocate the BIGNUM internal buffer to a fixed public size big
195          * enough that operations performed during the processing never trigger
196          * a realloc which would leak the size of the scalar through memory
197          * accesses.
198          *
199          * Fixed Length
200          * ------------
201          *
202          * The order of the large prime subgroup of the curve is our choice for
203          * a fixed public size, as that is generally the upper bound for
204          * generating a private key in EC cryptosystems and should fit all valid
205          * secret scalars.
206          *
207          * For padding on export we just use the bit length of the order
208          * converted to bytes (rounding up).
209          *
210          * For preallocating the BIGNUM storage we look at the number of "words"
211          * required for the internal representation of the order, and we
212          * preallocate 2 extra "words" in case any of the subsequent processing
213          * might temporarily overflow the order length.
214          */
215         ecbits = EC_GROUP_order_bits(ecg);
216         if (ecbits <= 0)
217             goto err;
218         sz = (ecbits + 7) / 8;
219 
220         if (!ossl_param_build_set_bn_pad(tmpl, params,
221                                          OSSL_PKEY_PARAM_PRIV_KEY,
222                                          priv_key, sz))
223             goto err;
224     }
225     ret = 1;
226  err:
227     BN_CTX_free(bnctx);
228     return ret;
229 }
230 
231 static ossl_inline
otherparams_to_params(const EC_KEY * ec,OSSL_PARAM_BLD * tmpl,OSSL_PARAM params[])232 int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl,
233                           OSSL_PARAM params[])
234 {
235     int ecdh_cofactor_mode = 0, group_check = 0;
236     const char *name = NULL;
237     point_conversion_form_t format;
238 
239     if (ec == NULL)
240         return 0;
241 
242     format = EC_KEY_get_conv_form(ec);
243     name = ossl_ec_pt_format_id2name((int)format);
244     if (name != NULL
245         && !ossl_param_build_set_utf8_string(tmpl, params,
246                                              OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
247                                              name))
248         return 0;
249 
250     group_check = EC_KEY_get_flags(ec) & EC_FLAG_CHECK_NAMED_GROUP_MASK;
251     name = ossl_ec_check_group_type_id2name(group_check);
252     if (name != NULL
253         && !ossl_param_build_set_utf8_string(tmpl, params,
254                                              OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE,
255                                              name))
256         return 0;
257 
258     if ((EC_KEY_get_enc_flags(ec) & EC_PKEY_NO_PUBKEY) != 0
259             && !ossl_param_build_set_int(tmpl, params,
260                                          OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 0))
261         return 0;
262 
263     ecdh_cofactor_mode =
264         (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
265     return ossl_param_build_set_int(tmpl, params,
266                                     OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
267                                     ecdh_cofactor_mode);
268 }
269 
270 static
ec_newdata(void * provctx)271 void *ec_newdata(void *provctx)
272 {
273     if (!ossl_prov_is_running())
274         return NULL;
275     return EC_KEY_new_ex(PROV_LIBCTX_OF(provctx), NULL);
276 }
277 
278 #ifndef FIPS_MODULE
279 # ifndef OPENSSL_NO_SM2
280 static
sm2_newdata(void * provctx)281 void *sm2_newdata(void *provctx)
282 {
283     if (!ossl_prov_is_running())
284         return NULL;
285     return EC_KEY_new_by_curve_name_ex(PROV_LIBCTX_OF(provctx), NULL, NID_sm2);
286 }
287 # endif
288 #endif
289 
290 static
ec_freedata(void * keydata)291 void ec_freedata(void *keydata)
292 {
293     EC_KEY_free(keydata);
294 }
295 
296 static
ec_has(const void * keydata,int selection)297 int ec_has(const void *keydata, int selection)
298 {
299     const EC_KEY *ec = keydata;
300     int ok = 1;
301 
302     if (!ossl_prov_is_running() || ec == NULL)
303         return 0;
304     if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
305         return 1; /* the selection is not missing */
306 
307     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
308         ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
309     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
310         ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
311     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
312         ok = ok && (EC_KEY_get0_group(ec) != NULL);
313     /*
314      * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
315      * available, so no extra check is needed other than the previous one
316      * against EC_POSSIBLE_SELECTIONS.
317      */
318     return ok;
319 }
320 
ec_match(const void * keydata1,const void * keydata2,int selection)321 static int ec_match(const void *keydata1, const void *keydata2, int selection)
322 {
323     const EC_KEY *ec1 = keydata1;
324     const EC_KEY *ec2 = keydata2;
325     const EC_GROUP *group_a = EC_KEY_get0_group(ec1);
326     const EC_GROUP *group_b = EC_KEY_get0_group(ec2);
327     BN_CTX *ctx = NULL;
328     int ok = 1;
329 
330     if (!ossl_prov_is_running())
331         return 0;
332 
333     ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec1));
334     if (ctx == NULL)
335         return 0;
336 
337     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
338         ok = ok && group_a != NULL && group_b != NULL
339             && EC_GROUP_cmp(group_a, group_b, ctx) == 0;
340     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
341         const BIGNUM *pa = EC_KEY_get0_private_key(ec1);
342         const BIGNUM *pb = EC_KEY_get0_private_key(ec2);
343 
344         ok = ok && BN_cmp(pa, pb) == 0;
345     }
346     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
347         const EC_POINT *pa = EC_KEY_get0_public_key(ec1);
348         const EC_POINT *pb = EC_KEY_get0_public_key(ec2);
349 
350         ok = ok && EC_POINT_cmp(group_b, pa, pb, ctx) == 0;
351     }
352     BN_CTX_free(ctx);
353     return ok;
354 }
355 
common_check_sm2(const EC_KEY * ec,int sm2_wanted)356 static int common_check_sm2(const EC_KEY *ec, int sm2_wanted)
357 {
358     const EC_GROUP *ecg = NULL;
359 
360     /*
361      * sm2_wanted: import the keys or domparams only on SM2 Curve
362      * !sm2_wanted: import the keys or domparams only not on SM2 Curve
363      */
364     if ((ecg = EC_KEY_get0_group(ec)) == NULL
365         || (sm2_wanted ^ (EC_GROUP_get_curve_name(ecg) == NID_sm2)))
366         return 0;
367     return 1;
368 }
369 
370 static
common_import(void * keydata,int selection,const OSSL_PARAM params[],int sm2_wanted)371 int common_import(void *keydata, int selection, const OSSL_PARAM params[],
372                   int sm2_wanted)
373 {
374     EC_KEY *ec = keydata;
375     int ok = 1;
376 
377     if (!ossl_prov_is_running() || ec == NULL)
378         return 0;
379 
380     /*
381      * In this implementation, we can export/import only keydata in the
382      * following combinations:
383      *   - domain parameters (+optional other params)
384      *   - public key with associated domain parameters (+optional other params)
385      *   - private key with associated domain parameters and optional public key
386      *         (+optional other params)
387      *
388      * This means:
389      *   - domain parameters must always be requested
390      *   - private key must be requested alongside public key
391      *   - other parameters are always optional
392      */
393     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
394         return 0;
395 
396     ok = ok && ossl_ec_group_fromdata(ec, params);
397 
398     if (!common_check_sm2(ec, sm2_wanted))
399         return 0;
400 
401     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
402         int include_private =
403             selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
404 
405         ok = ok && ossl_ec_key_fromdata(ec, params, include_private);
406     }
407     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
408         ok = ok && ossl_ec_key_otherparams_fromdata(ec, params);
409 
410     return ok;
411 }
412 
413 static
ec_import(void * keydata,int selection,const OSSL_PARAM params[])414 int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
415 {
416     return common_import(keydata, selection, params, 0);
417 }
418 
419 #ifndef FIPS_MODULE
420 # ifndef OPENSSL_NO_SM2
421 static
sm2_import(void * keydata,int selection,const OSSL_PARAM params[])422 int sm2_import(void *keydata, int selection, const OSSL_PARAM params[])
423 {
424     return common_import(keydata, selection, params, 1);
425 }
426 # endif
427 #endif
428 
429 static
ec_export(void * keydata,int selection,OSSL_CALLBACK * param_cb,void * cbarg)430 int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
431               void *cbarg)
432 {
433     EC_KEY *ec = keydata;
434     OSSL_PARAM_BLD *tmpl = NULL;
435     OSSL_PARAM *params = NULL;
436     unsigned char *pub_key = NULL, *genbuf = NULL;
437     BN_CTX *bnctx = NULL;
438     int ok = 1;
439 
440     if (!ossl_prov_is_running() || ec == NULL)
441         return 0;
442 
443     /*
444      * In this implementation, we can export/import only keydata in the
445      * following combinations:
446      *   - domain parameters (+optional other params)
447      *   - public key with associated domain parameters (+optional other params)
448      *   - private key with associated public key and domain parameters
449      *         (+optional other params)
450      *
451      * This means:
452      *   - domain parameters must always be requested
453      *   - private key must be requested alongside public key
454      *   - other parameters are always optional
455      */
456     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
457         return 0;
458     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
459             && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
460         return 0;
461     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
462             && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
463         return 0;
464 
465     tmpl = OSSL_PARAM_BLD_new();
466     if (tmpl == NULL)
467         return 0;
468 
469     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
470         bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
471         if (bnctx == NULL) {
472             ok = 0;
473             goto end;
474         }
475         BN_CTX_start(bnctx);
476         ok = ok && ossl_ec_group_todata(EC_KEY_get0_group(ec), tmpl, NULL,
477                                         ossl_ec_key_get_libctx(ec),
478                                         ossl_ec_key_get0_propq(ec),
479                                         bnctx, &genbuf);
480     }
481 
482     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
483         int include_private =
484             selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
485 
486         ok = ok && key_to_params(ec, tmpl, NULL, include_private, &pub_key);
487     }
488     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
489         ok = ok && otherparams_to_params(ec, tmpl, NULL);
490 
491     if (ok && (params = OSSL_PARAM_BLD_to_param(tmpl)) != NULL)
492         ok = param_cb(params, cbarg);
493 end:
494     OSSL_PARAM_free(params);
495     OSSL_PARAM_BLD_free(tmpl);
496     OPENSSL_free(pub_key);
497     OPENSSL_free(genbuf);
498     BN_CTX_end(bnctx);
499     BN_CTX_free(bnctx);
500     return ok;
501 }
502 
503 /* IMEXPORT = IMPORT + EXPORT */
504 
505 # define EC_IMEXPORTABLE_DOM_PARAMETERS                                        \
506     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),               \
507     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),              \
508     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),\
509     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0),            \
510     OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0),                              \
511     OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0),                              \
512     OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0),                              \
513     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0),            \
514     OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0),                          \
515     OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0),                       \
516     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0)
517 
518 # define EC_IMEXPORTABLE_PUBLIC_KEY                                            \
519     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
520 # define EC_IMEXPORTABLE_PRIVATE_KEY                                           \
521     OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
522 # define EC_IMEXPORTABLE_OTHER_PARAMETERS                                      \
523     OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),                   \
524     OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL)
525 
526 /*
527  * Include all the possible combinations of OSSL_PARAM arrays for
528  * ec_imexport_types().
529  *
530  * They are in a separate file as it is ~100 lines of unreadable and
531  * uninteresting machine generated stuff.
532  */
533 #include "ec_kmgmt_imexport.inc"
534 
535 static ossl_inline
ec_imexport_types(int selection)536 const OSSL_PARAM *ec_imexport_types(int selection)
537 {
538     int type_select = 0;
539 
540     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
541         type_select += 1;
542     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
543         type_select += 2;
544     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
545         type_select += 4;
546     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
547         type_select += 8;
548     return ec_types[type_select];
549 }
550 
551 static
ec_import_types(int selection)552 const OSSL_PARAM *ec_import_types(int selection)
553 {
554     return ec_imexport_types(selection);
555 }
556 
557 static
ec_export_types(int selection)558 const OSSL_PARAM *ec_export_types(int selection)
559 {
560     return ec_imexport_types(selection);
561 }
562 
ec_get_ecm_params(const EC_GROUP * group,OSSL_PARAM params[])563 static int ec_get_ecm_params(const EC_GROUP *group, OSSL_PARAM params[])
564 {
565 #ifdef OPENSSL_NO_EC2M
566     return 1;
567 #else
568     int ret = 0, m;
569     unsigned int k1 = 0, k2 = 0, k3 = 0;
570     int basis_nid;
571     const char *basis_name = NULL;
572     int fid = EC_GROUP_get_field_type(group);
573 
574     if (fid != NID_X9_62_characteristic_two_field)
575         return 1;
576 
577     basis_nid = EC_GROUP_get_basis_type(group);
578     if (basis_nid == NID_X9_62_tpBasis)
579         basis_name = SN_X9_62_tpBasis;
580     else if (basis_nid == NID_X9_62_ppBasis)
581         basis_name = SN_X9_62_ppBasis;
582     else
583         goto err;
584 
585     m = EC_GROUP_get_degree(group);
586     if (!ossl_param_build_set_int(NULL, params, OSSL_PKEY_PARAM_EC_CHAR2_M, m)
587         || !ossl_param_build_set_utf8_string(NULL, params,
588                                              OSSL_PKEY_PARAM_EC_CHAR2_TYPE,
589                                              basis_name))
590         goto err;
591 
592     if (basis_nid == NID_X9_62_tpBasis) {
593         if (!EC_GROUP_get_trinomial_basis(group, &k1)
594             || !ossl_param_build_set_int(NULL, params,
595                                          OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS,
596                                          (int)k1))
597             goto err;
598     } else {
599         if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)
600             || !ossl_param_build_set_int(NULL, params,
601                                          OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, (int)k1)
602             || !ossl_param_build_set_int(NULL, params,
603                                          OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, (int)k2)
604             || !ossl_param_build_set_int(NULL, params,
605                                          OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, (int)k3))
606             goto err;
607     }
608     ret = 1;
609 err:
610     return ret;
611 #endif /* OPENSSL_NO_EC2M */
612 }
613 
614 static
common_get_params(void * key,OSSL_PARAM params[],int sm2)615 int common_get_params(void *key, OSSL_PARAM params[], int sm2)
616 {
617     int ret = 0;
618     EC_KEY *eck = key;
619     const EC_GROUP *ecg = NULL;
620     OSSL_PARAM *p;
621     unsigned char *pub_key = NULL, *genbuf = NULL;
622     OSSL_LIB_CTX *libctx;
623     const char *propq;
624     BN_CTX *bnctx = NULL;
625 
626     ecg = EC_KEY_get0_group(eck);
627     if (ecg == NULL)
628         return 0;
629 
630     libctx = ossl_ec_key_get_libctx(eck);
631     propq = ossl_ec_key_get0_propq(eck);
632 
633     bnctx = BN_CTX_new_ex(libctx);
634     if (bnctx == NULL)
635         return 0;
636     BN_CTX_start(bnctx);
637 
638     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
639         && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
640         goto err;
641     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
642         && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
643         goto err;
644     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) {
645         int ecbits, sec_bits;
646 
647         ecbits = EC_GROUP_order_bits(ecg);
648 
649         /*
650          * The following estimates are based on the values published
651          * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
652          * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
653          *
654          * Note that the above reference explicitly categorizes algorithms in a
655          * discrete set of values {80, 112, 128, 192, 256}, and that it is
656          * relevant only for NIST approved Elliptic Curves, while OpenSSL
657          * applies the same logic also to other curves.
658          *
659          * Classifications produced by other standardazing bodies might differ,
660          * so the results provided for "bits of security" by this provider are
661          * to be considered merely indicative, and it is the users'
662          * responsibility to compare these values against the normative
663          * references that may be relevant for their intent and purposes.
664          */
665         if (ecbits >= 512)
666             sec_bits = 256;
667         else if (ecbits >= 384)
668             sec_bits = 192;
669         else if (ecbits >= 256)
670             sec_bits = 128;
671         else if (ecbits >= 224)
672             sec_bits = 112;
673         else if (ecbits >= 160)
674             sec_bits = 80;
675         else
676             sec_bits = ecbits / 2;
677 
678         if (!OSSL_PARAM_set_int(p, sec_bits))
679             goto err;
680     }
681 
682     if ((p = OSSL_PARAM_locate(params,
683                                OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS))
684             != NULL) {
685         int explicitparams = EC_KEY_decoded_from_explicit_params(eck);
686 
687         if (explicitparams < 0
688              || !OSSL_PARAM_set_int(p, explicitparams))
689             goto err;
690     }
691 
692     if (!sm2) {
693         if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
694                 && !OSSL_PARAM_set_utf8_string(p, EC_DEFAULT_MD))
695             goto err;
696     } else {
697         if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
698                 && !OSSL_PARAM_set_utf8_string(p, SM2_DEFAULT_MD))
699             goto err;
700     }
701 
702     /* SM2 doesn't support this PARAM */
703     if (!sm2) {
704         p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
705         if (p != NULL) {
706             int ecdh_cofactor_mode = 0;
707 
708             ecdh_cofactor_mode =
709                 (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
710 
711             if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
712                 goto err;
713         }
714     }
715     if ((p = OSSL_PARAM_locate(params,
716                                OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) {
717         p->return_size = EC_POINT_point2oct(EC_KEY_get0_group(key),
718                                             EC_KEY_get0_public_key(key),
719                                             POINT_CONVERSION_UNCOMPRESSED,
720                                             p->data, p->return_size, bnctx);
721         if (p->return_size == 0)
722             goto err;
723     }
724 
725     ret = ec_get_ecm_params(ecg, params)
726           && ossl_ec_group_todata(ecg, NULL, params, libctx, propq, bnctx,
727                                   &genbuf)
728           && key_to_params(eck, NULL, params, 1, &pub_key)
729           && otherparams_to_params(eck, NULL, params);
730 err:
731     OPENSSL_free(genbuf);
732     OPENSSL_free(pub_key);
733     BN_CTX_end(bnctx);
734     BN_CTX_free(bnctx);
735     return ret;
736 }
737 
738 static
ec_get_params(void * key,OSSL_PARAM params[])739 int ec_get_params(void *key, OSSL_PARAM params[])
740 {
741     return common_get_params(key, params, 0);
742 }
743 
744 #ifndef OPENSSL_NO_EC2M
745 # define EC2M_GETTABLE_DOM_PARAMS                                              \
746         OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL),                      \
747         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0),        \
748         OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL),               \
749         OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL),                  \
750         OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL),                  \
751         OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL),
752 #else
753 # define EC2M_GETTABLE_DOM_PARAMS
754 #endif
755 
756 static const OSSL_PARAM ec_known_gettable_params[] = {
757     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
758     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
759     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
760     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
761     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
762     OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL),
763     EC_IMEXPORTABLE_DOM_PARAMETERS,
764     EC2M_GETTABLE_DOM_PARAMS
765     EC_IMEXPORTABLE_PUBLIC_KEY,
766     OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
767     OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
768     EC_IMEXPORTABLE_PRIVATE_KEY,
769     EC_IMEXPORTABLE_OTHER_PARAMETERS,
770     OSSL_PARAM_END
771 };
772 
773 static
ec_gettable_params(void * provctx)774 const OSSL_PARAM *ec_gettable_params(void *provctx)
775 {
776     return ec_known_gettable_params;
777 }
778 
779 static const OSSL_PARAM ec_known_settable_params[] = {
780     OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
781     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
782     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
783     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),
784     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
785     OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL),
786     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, NULL, 0),
787     OSSL_PARAM_END
788 };
789 
790 static
ec_settable_params(void * provctx)791 const OSSL_PARAM *ec_settable_params(void *provctx)
792 {
793     return ec_known_settable_params;
794 }
795 
796 static
ec_set_params(void * key,const OSSL_PARAM params[])797 int ec_set_params(void *key, const OSSL_PARAM params[])
798 {
799     EC_KEY *eck = key;
800     const OSSL_PARAM *p;
801 
802     if (key == NULL)
803         return 0;
804     if (params == NULL)
805         return 1;
806 
807 
808     if (!ossl_ec_group_set_params((EC_GROUP *)EC_KEY_get0_group(key), params))
809         return 0;
810 
811     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
812     if (p != NULL) {
813         BN_CTX *ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key));
814         int ret = 1;
815 
816         if (ctx == NULL
817                 || p->data_type != OSSL_PARAM_OCTET_STRING
818                 || !EC_KEY_oct2key(key, p->data, p->data_size, ctx))
819             ret = 0;
820         BN_CTX_free(ctx);
821         if (!ret)
822             return 0;
823     }
824 
825     return ossl_ec_key_otherparams_fromdata(eck, params);
826 }
827 
828 #ifndef FIPS_MODULE
829 # ifndef OPENSSL_NO_SM2
830 static
sm2_get_params(void * key,OSSL_PARAM params[])831 int sm2_get_params(void *key, OSSL_PARAM params[])
832 {
833     return common_get_params(key, params, 1);
834 }
835 
836 static const OSSL_PARAM sm2_known_gettable_params[] = {
837     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
838     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
839     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
840     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
841     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
842     OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL),
843     EC_IMEXPORTABLE_DOM_PARAMETERS,
844     EC_IMEXPORTABLE_PUBLIC_KEY,
845     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
846     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
847     EC_IMEXPORTABLE_PRIVATE_KEY,
848     OSSL_PARAM_END
849 };
850 
851 static
sm2_gettable_params(ossl_unused void * provctx)852 const OSSL_PARAM *sm2_gettable_params(ossl_unused void *provctx)
853 {
854     return sm2_known_gettable_params;
855 }
856 
857 static const OSSL_PARAM sm2_known_settable_params[] = {
858     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
859     OSSL_PARAM_END
860 };
861 
862 static
sm2_settable_params(ossl_unused void * provctx)863 const OSSL_PARAM *sm2_settable_params(ossl_unused void *provctx)
864 {
865     return sm2_known_settable_params;
866 }
867 
868 static
sm2_validate(const void * keydata,int selection,int checktype)869 int sm2_validate(const void *keydata, int selection, int checktype)
870 {
871     const EC_KEY *eck = keydata;
872     int ok = 1;
873     BN_CTX *ctx = NULL;
874 
875     if (!ossl_prov_is_running())
876         return 0;
877 
878     if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
879         return 1; /* nothing to validate */
880 
881     ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck));
882     if  (ctx == NULL)
883         return 0;
884 
885     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
886         ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
887 
888     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
889         if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
890             ok = ok && ossl_ec_key_public_check_quick(eck, ctx);
891         else
892             ok = ok && ossl_ec_key_public_check(eck, ctx);
893     }
894 
895     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
896         ok = ok && ossl_sm2_key_private_check(eck);
897 
898     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
899         ok = ok && ossl_ec_key_pairwise_check(eck, ctx);
900 
901     BN_CTX_free(ctx);
902     return ok;
903 }
904 # endif
905 #endif
906 
907 static
ec_validate(const void * keydata,int selection,int checktype)908 int ec_validate(const void *keydata, int selection, int checktype)
909 {
910     const EC_KEY *eck = keydata;
911     int ok = 1;
912     BN_CTX *ctx = NULL;
913 
914     if (!ossl_prov_is_running())
915         return 0;
916 
917     if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
918         return 1; /* nothing to validate */
919 
920     ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck));
921     if  (ctx == NULL)
922         return 0;
923 
924     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
925         int flags = EC_KEY_get_flags(eck);
926 
927         if ((flags & EC_FLAG_CHECK_NAMED_GROUP) != 0)
928             ok = ok && EC_GROUP_check_named_curve(EC_KEY_get0_group(eck),
929                            (flags & EC_FLAG_CHECK_NAMED_GROUP_NIST) != 0, ctx);
930         else
931             ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
932     }
933 
934     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
935         if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
936             ok = ok && ossl_ec_key_public_check_quick(eck, ctx);
937         else
938             ok = ok && ossl_ec_key_public_check(eck, ctx);
939     }
940 
941     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
942         ok = ok && ossl_ec_key_private_check(eck);
943 
944     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
945         ok = ok && ossl_ec_key_pairwise_check(eck, ctx);
946 
947     BN_CTX_free(ctx);
948     return ok;
949 }
950 
951 struct ec_gen_ctx {
952     OSSL_LIB_CTX *libctx;
953     char *group_name;
954     char *encoding;
955     char *pt_format;
956     char *group_check;
957     char *field_type;
958     BIGNUM *p, *a, *b, *order, *cofactor;
959     unsigned char *gen, *seed;
960     size_t gen_len, seed_len;
961     int selection;
962     int ecdh_mode;
963     EC_GROUP *gen_group;
964 };
965 
ec_gen_init(void * provctx,int selection,const OSSL_PARAM params[])966 static void *ec_gen_init(void *provctx, int selection,
967                          const OSSL_PARAM params[])
968 {
969     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
970     struct ec_gen_ctx *gctx = NULL;
971 
972     if (!ossl_prov_is_running() || (selection & (EC_POSSIBLE_SELECTIONS)) == 0)
973         return NULL;
974 
975     if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
976         gctx->libctx = libctx;
977         gctx->selection = selection;
978         gctx->ecdh_mode = 0;
979     }
980     if (!ec_gen_set_params(gctx, params)) {
981         OPENSSL_free(gctx);
982         gctx = NULL;
983     }
984     return gctx;
985 }
986 
987 #ifndef FIPS_MODULE
988 # ifndef OPENSSL_NO_SM2
sm2_gen_init(void * provctx,int selection,const OSSL_PARAM params[])989 static void *sm2_gen_init(void *provctx, int selection,
990                          const OSSL_PARAM params[])
991 {
992     struct ec_gen_ctx *gctx = ec_gen_init(provctx, selection, params);
993 
994     if (gctx != NULL) {
995         if (gctx->group_name != NULL)
996             return gctx;
997         if ((gctx->group_name = OPENSSL_strdup("sm2")) != NULL)
998             return gctx;
999         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
1000         ec_gen_cleanup(gctx);
1001     }
1002     return NULL;
1003 }
1004 # endif
1005 #endif
1006 
ec_gen_set_group(void * genctx,const EC_GROUP * src)1007 static int ec_gen_set_group(void *genctx, const EC_GROUP *src)
1008 {
1009     struct ec_gen_ctx *gctx = genctx;
1010     EC_GROUP *group;
1011 
1012     group = EC_GROUP_dup(src);
1013     if (group == NULL) {
1014         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
1015         return 0;
1016     }
1017     EC_GROUP_free(gctx->gen_group);
1018     gctx->gen_group = group;
1019     return 1;
1020 }
1021 
ec_gen_set_template(void * genctx,void * templ)1022 static int ec_gen_set_template(void *genctx, void *templ)
1023 {
1024     struct ec_gen_ctx *gctx = genctx;
1025     EC_KEY *ec = templ;
1026     const EC_GROUP *ec_group;
1027 
1028     if (!ossl_prov_is_running() || gctx == NULL || ec == NULL)
1029         return 0;
1030     if ((ec_group = EC_KEY_get0_group(ec)) == NULL)
1031         return 0;
1032     return ec_gen_set_group(gctx, ec_group);
1033 }
1034 
1035 #define COPY_INT_PARAM(params, key, val)                                       \
1036 p = OSSL_PARAM_locate_const(params, key);                                      \
1037 if (p != NULL && !OSSL_PARAM_get_int(p, &val))                                 \
1038     goto err;
1039 
1040 #define COPY_UTF8_PARAM(params, key, val)                                      \
1041 p = OSSL_PARAM_locate_const(params, key);                                      \
1042 if (p != NULL) {                                                               \
1043     if (p->data_type != OSSL_PARAM_UTF8_STRING)                                \
1044         goto err;                                                              \
1045     OPENSSL_free(val);                                                         \
1046     val = OPENSSL_strdup(p->data);                                             \
1047     if (val == NULL)                                                           \
1048         goto err;                                                              \
1049 }
1050 
1051 #define COPY_OCTET_PARAM(params, key, val, len)                                \
1052 p = OSSL_PARAM_locate_const(params, key);                                      \
1053 if (p != NULL) {                                                               \
1054     if (p->data_type != OSSL_PARAM_OCTET_STRING)                               \
1055         goto err;                                                              \
1056     OPENSSL_free(val);                                                         \
1057     len = p->data_size;                                                        \
1058     val = OPENSSL_memdup(p->data, p->data_size);                               \
1059     if (val == NULL)                                                           \
1060         goto err;                                                              \
1061 }
1062 
1063 #define COPY_BN_PARAM(params, key, bn)                                         \
1064 p = OSSL_PARAM_locate_const(params, key);                                      \
1065 if (p != NULL) {                                                               \
1066     if (bn == NULL)                                                            \
1067         bn = BN_new();                                                         \
1068     if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn))                              \
1069         goto err;                                                              \
1070 }
1071 
ec_gen_set_params(void * genctx,const OSSL_PARAM params[])1072 static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[])
1073 {
1074     int ret = 0;
1075     struct ec_gen_ctx *gctx = genctx;
1076     const OSSL_PARAM *p;
1077     EC_GROUP *group = NULL;
1078 
1079     COPY_INT_PARAM(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, gctx->ecdh_mode);
1080 
1081     COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_GROUP_NAME, gctx->group_name);
1082     COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE, gctx->field_type);
1083     COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_ENCODING, gctx->encoding);
1084     COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, gctx->pt_format);
1085     COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, gctx->group_check);
1086 
1087     COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_P, gctx->p);
1088     COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_A, gctx->a);
1089     COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_B, gctx->b);
1090     COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_ORDER, gctx->order);
1091     COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_COFACTOR, gctx->cofactor);
1092 
1093     COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_SEED, gctx->seed, gctx->seed_len);
1094     COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_GENERATOR, gctx->gen,
1095                      gctx->gen_len);
1096 
1097     ret = 1;
1098 err:
1099     EC_GROUP_free(group);
1100     return ret;
1101 }
1102 
ec_gen_set_group_from_params(struct ec_gen_ctx * gctx)1103 static int ec_gen_set_group_from_params(struct ec_gen_ctx *gctx)
1104 {
1105     int ret = 0;
1106     OSSL_PARAM_BLD *bld;
1107     OSSL_PARAM *params = NULL;
1108     EC_GROUP *group = NULL;
1109 
1110     bld = OSSL_PARAM_BLD_new();
1111     if (bld == NULL)
1112         return 0;
1113 
1114     if (gctx->encoding != NULL
1115         && !OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_ENCODING,
1116                                             gctx->encoding, 0))
1117         goto err;
1118 
1119     if (gctx->pt_format != NULL
1120         && !OSSL_PARAM_BLD_push_utf8_string(bld,
1121                                             OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
1122                                             gctx->pt_format, 0))
1123         goto err;
1124 
1125     if (gctx->group_name != NULL) {
1126         if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
1127                                              gctx->group_name, 0))
1128             goto err;
1129         /* Ignore any other parameters if there is a group name */
1130         goto build;
1131     } else if (gctx->field_type != NULL) {
1132         if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
1133                                              gctx->field_type, 0))
1134             goto err;
1135     } else {
1136         goto err;
1137     }
1138     if (gctx->p == NULL
1139         || gctx->a == NULL
1140         || gctx->b == NULL
1141         || gctx->order == NULL
1142         || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, gctx->p)
1143         || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, gctx->a)
1144         || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, gctx->b)
1145         || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER, gctx->order))
1146         goto err;
1147 
1148     if (gctx->cofactor != NULL
1149         && !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR,
1150                                    gctx->cofactor))
1151         goto err;
1152 
1153     if (gctx->seed != NULL
1154         && !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_SEED,
1155                                              gctx->seed, gctx->seed_len))
1156         goto err;
1157 
1158     if (gctx->gen == NULL
1159         || !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_GENERATOR,
1160                                              gctx->gen, gctx->gen_len))
1161         goto err;
1162 build:
1163     params = OSSL_PARAM_BLD_to_param(bld);
1164     if (params == NULL)
1165         goto err;
1166     group = EC_GROUP_new_from_params(params, gctx->libctx, NULL);
1167     if (group == NULL)
1168         goto err;
1169 
1170     EC_GROUP_free(gctx->gen_group);
1171     gctx->gen_group = group;
1172 
1173     ret = 1;
1174 err:
1175     OSSL_PARAM_free(params);
1176     OSSL_PARAM_BLD_free(bld);
1177     return ret;
1178 }
1179 
ec_gen_settable_params(ossl_unused void * genctx,ossl_unused void * provctx)1180 static const OSSL_PARAM *ec_gen_settable_params(ossl_unused void *genctx,
1181                                                 ossl_unused void *provctx)
1182 {
1183     static OSSL_PARAM settable[] = {
1184         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
1185         OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
1186         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
1187         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),
1188         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0),
1189         OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0),
1190         OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0),
1191         OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0),
1192         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0),
1193         OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0),
1194         OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0),
1195         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
1196         OSSL_PARAM_END
1197     };
1198 
1199     return settable;
1200 }
1201 
ec_gen_assign_group(EC_KEY * ec,EC_GROUP * group)1202 static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group)
1203 {
1204     if (group == NULL) {
1205         ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
1206         return 0;
1207     }
1208     return EC_KEY_set_group(ec, group) > 0;
1209 }
1210 
1211 /*
1212  * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1213  */
ec_gen(void * genctx,OSSL_CALLBACK * osslcb,void * cbarg)1214 static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
1215 {
1216     struct ec_gen_ctx *gctx = genctx;
1217     EC_KEY *ec = NULL;
1218     int ret = 0;
1219 
1220     if (!ossl_prov_is_running()
1221         || gctx == NULL
1222         || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL)
1223         return NULL;
1224 
1225     if (gctx->gen_group == NULL) {
1226         if (!ec_gen_set_group_from_params(gctx))
1227             goto err;
1228     } else {
1229         if (gctx->encoding != NULL) {
1230             int flags = ossl_ec_encoding_name2id(gctx->encoding);
1231 
1232             if (flags < 0)
1233                 goto err;
1234             EC_GROUP_set_asn1_flag(gctx->gen_group, flags);
1235         }
1236         if (gctx->pt_format != NULL) {
1237             int format = ossl_ec_pt_format_name2id(gctx->pt_format);
1238 
1239             if (format < 0)
1240                 goto err;
1241             EC_GROUP_set_point_conversion_form(gctx->gen_group, format);
1242         }
1243     }
1244 
1245     /* We must always assign a group, no matter what */
1246     ret = ec_gen_assign_group(ec, gctx->gen_group);
1247 
1248     /* Whether you want it or not, you get a keypair, not just one half */
1249     if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
1250         ret = ret && EC_KEY_generate_key(ec);
1251 
1252     if (gctx->ecdh_mode != -1)
1253         ret = ret && ossl_ec_set_ecdh_cofactor_mode(ec, gctx->ecdh_mode);
1254 
1255     if (gctx->group_check != NULL)
1256         ret = ret && ossl_ec_set_check_group_type_from_name(ec, gctx->group_check);
1257     if (ret)
1258         return ec;
1259 err:
1260     /* Something went wrong, throw the key away */
1261     EC_KEY_free(ec);
1262     return NULL;
1263 }
1264 
1265 #ifndef FIPS_MODULE
1266 # ifndef OPENSSL_NO_SM2
1267 /*
1268  * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
1269  */
sm2_gen(void * genctx,OSSL_CALLBACK * osslcb,void * cbarg)1270 static void *sm2_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
1271 {
1272     struct ec_gen_ctx *gctx = genctx;
1273     EC_KEY *ec = NULL;
1274     int ret = 1;
1275 
1276     if (gctx == NULL
1277         || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL)
1278         return NULL;
1279 
1280     if (gctx->gen_group == NULL) {
1281         if (!ec_gen_set_group_from_params(gctx))
1282             goto err;
1283     } else {
1284         if (gctx->encoding) {
1285             int flags = ossl_ec_encoding_name2id(gctx->encoding);
1286 
1287             if (flags < 0)
1288                 goto err;
1289             EC_GROUP_set_asn1_flag(gctx->gen_group, flags);
1290         }
1291         if (gctx->pt_format != NULL) {
1292             int format = ossl_ec_pt_format_name2id(gctx->pt_format);
1293 
1294             if (format < 0)
1295                 goto err;
1296             EC_GROUP_set_point_conversion_form(gctx->gen_group, format);
1297         }
1298     }
1299 
1300     /* We must always assign a group, no matter what */
1301     ret = ec_gen_assign_group(ec, gctx->gen_group);
1302 
1303     /* Whether you want it or not, you get a keypair, not just one half */
1304     if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
1305         ret = ret && EC_KEY_generate_key(ec);
1306 
1307     if (ret)
1308         return ec;
1309 err:
1310     /* Something went wrong, throw the key away */
1311     EC_KEY_free(ec);
1312     return NULL;
1313 }
1314 # endif
1315 #endif
1316 
ec_gen_cleanup(void * genctx)1317 static void ec_gen_cleanup(void *genctx)
1318 {
1319     struct ec_gen_ctx *gctx = genctx;
1320 
1321     if (gctx == NULL)
1322         return;
1323 
1324     EC_GROUP_free(gctx->gen_group);
1325     BN_free(gctx->p);
1326     BN_free(gctx->a);
1327     BN_free(gctx->b);
1328     BN_free(gctx->order);
1329     BN_free(gctx->cofactor);
1330     OPENSSL_free(gctx->group_name);
1331     OPENSSL_free(gctx->field_type);
1332     OPENSSL_free(gctx->pt_format);
1333     OPENSSL_free(gctx->encoding);
1334     OPENSSL_free(gctx->seed);
1335     OPENSSL_free(gctx->gen);
1336     OPENSSL_free(gctx);
1337 }
1338 
common_load(const void * reference,size_t reference_sz,int sm2_wanted)1339 static void *common_load(const void *reference, size_t reference_sz,
1340                          int sm2_wanted)
1341 {
1342     EC_KEY *ec = NULL;
1343 
1344     if (ossl_prov_is_running() && reference_sz == sizeof(ec)) {
1345         /* The contents of the reference is the address to our object */
1346         ec = *(EC_KEY **)reference;
1347 
1348         if (!common_check_sm2(ec, sm2_wanted))
1349             return NULL;
1350 
1351         /* We grabbed, so we detach it */
1352         *(EC_KEY **)reference = NULL;
1353         return ec;
1354     }
1355     return NULL;
1356 }
1357 
ec_load(const void * reference,size_t reference_sz)1358 static void *ec_load(const void *reference, size_t reference_sz)
1359 {
1360     return common_load(reference, reference_sz, 0);
1361 }
1362 
1363 #ifndef FIPS_MODULE
1364 # ifndef OPENSSL_NO_SM2
sm2_load(const void * reference,size_t reference_sz)1365 static void *sm2_load(const void *reference, size_t reference_sz)
1366 {
1367     return common_load(reference, reference_sz, 1);
1368 }
1369 # endif
1370 #endif
1371 
ec_dup(const void * keydata_from,int selection)1372 static void *ec_dup(const void *keydata_from, int selection)
1373 {
1374     if (ossl_prov_is_running())
1375         return ossl_ec_key_dup(keydata_from, selection);
1376     return NULL;
1377 }
1378 
1379 const OSSL_DISPATCH ossl_ec_keymgmt_functions[] = {
1380     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
1381     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init },
1382     { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
1383       (void (*)(void))ec_gen_set_template },
1384     { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
1385     { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
1386       (void (*)(void))ec_gen_settable_params },
1387     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ec_gen },
1388     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
1389     { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))ec_load },
1390     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
1391     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params },
1392     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params },
1393     { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
1394     { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params },
1395     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
1396     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
1397     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
1398     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
1399     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
1400     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
1401     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
1402     { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
1403       (void (*)(void))ec_query_operation_name },
1404     { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ec_dup },
1405     { 0, NULL }
1406 };
1407 
1408 #ifndef FIPS_MODULE
1409 # ifndef OPENSSL_NO_SM2
1410 const OSSL_DISPATCH ossl_sm2_keymgmt_functions[] = {
1411     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))sm2_newdata },
1412     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))sm2_gen_init },
1413     { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
1414       (void (*)(void))ec_gen_set_template },
1415     { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
1416     { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
1417       (void (*)(void))ec_gen_settable_params },
1418     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))sm2_gen },
1419     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
1420     { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))sm2_load },
1421     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
1422     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))sm2_get_params },
1423     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))sm2_gettable_params },
1424     { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
1425     { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))sm2_settable_params },
1426     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
1427     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
1428     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))sm2_validate },
1429     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))sm2_import },
1430     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
1431     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
1432     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
1433     { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
1434       (void (*)(void))sm2_query_operation_name },
1435     { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ec_dup },
1436     { 0, NULL }
1437 };
1438 # endif
1439 #endif
1440