]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/keymgmt/ec_kmgmt.c
Add Explicit EC parameter support to providers.
[thirdparty/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 "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 "crypto/bn.h"
24 #include "crypto/ec.h"
25 #include "prov/implementations.h"
26 #include "prov/providercommon.h"
27 #include "prov/providercommonerr.h"
28 #include "prov/provider_ctx.h"
29 #include "internal/param_build_set.h"
30
31 static OSSL_FUNC_keymgmt_new_fn ec_newdata;
32 static OSSL_FUNC_keymgmt_gen_init_fn ec_gen_init;
33 static OSSL_FUNC_keymgmt_gen_set_template_fn ec_gen_set_template;
34 static OSSL_FUNC_keymgmt_gen_set_params_fn ec_gen_set_params;
35 static OSSL_FUNC_keymgmt_gen_settable_params_fn ec_gen_settable_params;
36 static OSSL_FUNC_keymgmt_gen_fn ec_gen;
37 static OSSL_FUNC_keymgmt_gen_cleanup_fn ec_gen_cleanup;
38 static OSSL_FUNC_keymgmt_load_fn ec_load;
39 static OSSL_FUNC_keymgmt_free_fn ec_freedata;
40 static OSSL_FUNC_keymgmt_get_params_fn ec_get_params;
41 static OSSL_FUNC_keymgmt_gettable_params_fn ec_gettable_params;
42 static OSSL_FUNC_keymgmt_set_params_fn ec_set_params;
43 static OSSL_FUNC_keymgmt_settable_params_fn ec_settable_params;
44 static OSSL_FUNC_keymgmt_has_fn ec_has;
45 static OSSL_FUNC_keymgmt_match_fn ec_match;
46 static OSSL_FUNC_keymgmt_validate_fn ec_validate;
47 static OSSL_FUNC_keymgmt_import_fn ec_import;
48 static OSSL_FUNC_keymgmt_import_types_fn ec_import_types;
49 static OSSL_FUNC_keymgmt_export_fn ec_export;
50 static OSSL_FUNC_keymgmt_export_types_fn ec_export_types;
51 static OSSL_FUNC_keymgmt_query_operation_name_fn ec_query_operation_name;
52
53 #define EC_DEFAULT_MD "SHA256"
54 #define EC_POSSIBLE_SELECTIONS \
55 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
56
57 static
58 const char *ec_query_operation_name(int operation_id)
59 {
60 switch (operation_id) {
61 case OSSL_OP_KEYEXCH:
62 return "ECDH";
63 case OSSL_OP_SIGNATURE:
64 return "ECDSA";
65 }
66 return NULL;
67 }
68
69 /*
70 * Callers of key_to_params MUST make sure that domparams_to_params is also
71 * called!
72 *
73 * This function only exports the bare keypair, domain parameters and other
74 * parameters are exported separately.
75 */
76 static ossl_inline
77 int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl,
78 OSSL_PARAM params[], int include_private,
79 unsigned char **pub_key)
80 {
81 BIGNUM *x = NULL, *y = NULL;
82 const BIGNUM *priv_key = NULL;
83 const EC_POINT *pub_point = NULL;
84 const EC_GROUP *ecg = NULL;
85 size_t pub_key_len = 0;
86 int ret = 0;
87 BN_CTX *bnctx = NULL;
88
89 if (eckey == NULL
90 || (ecg = EC_KEY_get0_group(eckey)) == NULL)
91 return 0;
92
93 priv_key = EC_KEY_get0_private_key(eckey);
94 pub_point = EC_KEY_get0_public_key(eckey);
95
96 if (pub_point != NULL) {
97 OSSL_PARAM *p = NULL, *px = NULL, *py = NULL;
98 /*
99 * EC_POINT_point2buf() can generate random numbers in some
100 * implementations so we need to ensure we use the correct libctx.
101 */
102 bnctx = BN_CTX_new_ex(ec_key_get_libctx(eckey));
103 if (bnctx == NULL)
104 goto err;
105
106
107 /* If we are doing a get then check first before decoding the point */
108 if (tmpl == NULL) {
109 p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY);
110 px = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_X);
111 py = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_Y);
112 }
113
114 if (p != NULL || tmpl != NULL) {
115 /* convert pub_point to a octet string according to the SECG standard */
116 if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
117 POINT_CONVERSION_COMPRESSED,
118 pub_key, bnctx)) == 0
119 || !ossl_param_build_set_octet_string(tmpl, p,
120 OSSL_PKEY_PARAM_PUB_KEY,
121 *pub_key, pub_key_len))
122 goto err;
123 }
124 if (px != NULL || py != NULL) {
125 if (px != NULL)
126 x = BN_CTX_get(bnctx);
127 if (py != NULL)
128 y = BN_CTX_get(bnctx);
129
130 if (!EC_POINT_get_affine_coordinates(ecg, pub_point, x, y, bnctx))
131 goto err;
132 if (px != NULL
133 && !ossl_param_build_set_bn(tmpl, px,
134 OSSL_PKEY_PARAM_EC_PUB_X, x))
135 goto err;
136 if (py != NULL
137 && !ossl_param_build_set_bn(tmpl, py,
138 OSSL_PKEY_PARAM_EC_PUB_Y, y))
139 goto err;
140 }
141 }
142
143 if (priv_key != NULL && include_private) {
144 size_t sz;
145 int ecbits;
146
147 /*
148 * Key import/export should never leak the bit length of the secret
149 * scalar in the key.
150 *
151 * For this reason, on export we use padded BIGNUMs with fixed length.
152 *
153 * When importing we also should make sure that, even if short lived,
154 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
155 * soon as possible, so that any processing of this BIGNUM might opt for
156 * constant time implementations in the backend.
157 *
158 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
159 * to preallocate the BIGNUM internal buffer to a fixed public size big
160 * enough that operations performed during the processing never trigger
161 * a realloc which would leak the size of the scalar through memory
162 * accesses.
163 *
164 * Fixed Length
165 * ------------
166 *
167 * The order of the large prime subgroup of the curve is our choice for
168 * a fixed public size, as that is generally the upper bound for
169 * generating a private key in EC cryptosystems and should fit all valid
170 * secret scalars.
171 *
172 * For padding on export we just use the bit length of the order
173 * converted to bytes (rounding up).
174 *
175 * For preallocating the BIGNUM storage we look at the number of "words"
176 * required for the internal representation of the order, and we
177 * preallocate 2 extra "words" in case any of the subsequent processing
178 * might temporarily overflow the order length.
179 */
180 ecbits = EC_GROUP_order_bits(ecg);
181 if (ecbits <= 0)
182 goto err;
183 sz = (ecbits + 7 ) / 8;
184
185 if (!ossl_param_build_set_bn_pad(tmpl, params,
186 OSSL_PKEY_PARAM_PRIV_KEY,
187 priv_key, sz))
188 goto err;
189 }
190 ret = 1;
191 err:
192 BN_CTX_free(bnctx);
193 return ret;
194 }
195
196 static ossl_inline
197 int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl,
198 OSSL_PARAM params[])
199 {
200 int ecdh_cofactor_mode = 0;
201
202 if (ec == NULL)
203 return 0;
204
205 ecdh_cofactor_mode =
206 (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
207 return ossl_param_build_set_int(tmpl, params,
208 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
209 ecdh_cofactor_mode);
210 }
211
212 static
213 void *ec_newdata(void *provctx)
214 {
215 return EC_KEY_new_with_libctx(PROV_LIBRARY_CONTEXT_OF(provctx), NULL);
216 }
217
218 static
219 void ec_freedata(void *keydata)
220 {
221 EC_KEY_free(keydata);
222 }
223
224 static
225 int ec_has(void *keydata, int selection)
226 {
227 EC_KEY *ec = keydata;
228 int ok = 0;
229
230 if (ec != NULL) {
231 if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
232 ok = 1;
233
234 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
235 ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
236 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
237 ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
238 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
239 ok = ok && (EC_KEY_get0_group(ec) != NULL);
240 /*
241 * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
242 * available, so no extra check is needed other than the previous one
243 * against EC_POSSIBLE_SELECTIONS.
244 */
245 }
246 return ok;
247 }
248
249 static int ec_match(const void *keydata1, const void *keydata2, int selection)
250 {
251 const EC_KEY *ec1 = keydata1;
252 const EC_KEY *ec2 = keydata2;
253 const EC_GROUP *group_a = EC_KEY_get0_group(ec1);
254 const EC_GROUP *group_b = EC_KEY_get0_group(ec2);
255 BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(ec1));
256 int ok = 1;
257
258 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
259 ok = ok && group_a != NULL && group_b != NULL
260 && EC_GROUP_cmp(group_a, group_b, ctx) == 0;
261 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
262 const BIGNUM *pa = EC_KEY_get0_private_key(ec1);
263 const BIGNUM *pb = EC_KEY_get0_private_key(ec2);
264
265 ok = ok && BN_cmp(pa, pb) == 0;
266 }
267 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
268 const EC_POINT *pa = EC_KEY_get0_public_key(ec1);
269 const EC_POINT *pb = EC_KEY_get0_public_key(ec2);
270
271 ok = ok && EC_POINT_cmp(group_b, pa, pb, ctx) == 0;
272 }
273 BN_CTX_free(ctx);
274 return ok;
275 }
276
277 static
278 int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
279 {
280 EC_KEY *ec = keydata;
281 int ok = 1;
282
283 if (ec == NULL)
284 return 0;
285
286 /*
287 * In this implementation, we can export/import only keydata in the
288 * following combinations:
289 * - domain parameters (+optional other params)
290 * - public key with associated domain parameters (+optional other params)
291 * - private key with associated public key and domain parameters
292 * (+optional other params)
293 *
294 * This means:
295 * - domain parameters must always be requested
296 * - private key must be requested alongside public key
297 * - other parameters are always optional
298 */
299 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
300 return 0;
301 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
302 && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
303 return 0;
304
305 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
306 ok = ok && ec_group_fromdata(ec, params);
307 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
308 int include_private =
309 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
310
311 ok = ok && ec_key_fromdata(ec, params, include_private);
312 }
313 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
314 ok = ok && ec_key_otherparams_fromdata(ec, params);
315
316 return ok;
317 }
318
319 static
320 int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
321 void *cbarg)
322 {
323 EC_KEY *ec = keydata;
324 OSSL_PARAM_BLD *tmpl;
325 OSSL_PARAM *params = NULL;
326 unsigned char *pub_key = NULL, *genbuf = NULL;
327 BN_CTX *bnctx = NULL;
328 int ok = 1;
329
330 if (ec == NULL)
331 return 0;
332
333 /*
334 * In this implementation, we can export/import only keydata in the
335 * following combinations:
336 * - domain parameters (+optional other params)
337 * - public key with associated domain parameters (+optional other params)
338 * - private key with associated public key and domain parameters
339 * (+optional other params)
340 *
341 * This means:
342 * - domain parameters must always be requested
343 * - private key must be requested alongside public key
344 * - other parameters are always optional
345 */
346 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
347 return 0;
348 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
349 && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
350 return 0;
351 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0
352 && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
353 return 0;
354
355 tmpl = OSSL_PARAM_BLD_new();
356 if (tmpl == NULL)
357 return 0;
358
359 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
360 bnctx = BN_CTX_new_ex(ec_key_get_libctx(ec));
361 BN_CTX_start(bnctx);
362 ok = ok && (bnctx != NULL);
363 ok = ok && ec_group_todata(EC_KEY_get0_group(ec), tmpl, NULL,
364 ec_key_get_libctx(ec), ec_key_get0_propq(ec),
365 bnctx, &genbuf);
366 }
367
368 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
369 int include_private =
370 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
371
372 ok = ok && key_to_params(ec, tmpl, NULL, include_private, &pub_key);
373 }
374 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
375 ok = ok && otherparams_to_params(ec, tmpl, NULL);
376
377 if (ok && (params = OSSL_PARAM_BLD_to_param(tmpl)) != NULL)
378 ok = param_cb(params, cbarg);
379
380 OSSL_PARAM_BLD_free_params(params);
381 OSSL_PARAM_BLD_free(tmpl);
382 OPENSSL_free(pub_key);
383 OPENSSL_free(genbuf);
384 BN_CTX_end(bnctx);
385 BN_CTX_free(bnctx);
386 return ok;
387 }
388
389 /* IMEXPORT = IMPORT + EXPORT */
390
391 # define EC_IMEXPORTABLE_DOM_PARAMETERS \
392 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), \
393 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), \
394 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0), \
395 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0), \
396 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0), \
397 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0), \
398 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0), \
399 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0), \
400 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0), \
401 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0)
402
403 # define EC_IMEXPORTABLE_PUBLIC_KEY \
404 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
405 # define EC_IMEXPORTABLE_PRIVATE_KEY \
406 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
407 # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
408 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL)
409
410 /*
411 * Include all the possible combinations of OSSL_PARAM arrays for
412 * ec_imexport_types().
413 *
414 * They are in a separate file as it is ~100 lines of unreadable and
415 * uninteresting machine generated stuff.
416 *
417 * TODO(3.0): the generated list looks quite ugly, as to cover all possible
418 * combinations of the bits in `selection`, it also includes combinations that
419 * are not really useful: we might want to consider alternatives to this
420 * solution.
421 */
422 #include "ec_kmgmt_imexport.inc"
423
424 static ossl_inline
425 const OSSL_PARAM *ec_imexport_types(int selection)
426 {
427 int type_select = 0;
428
429 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
430 type_select += 1;
431 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
432 type_select += 2;
433 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
434 type_select += 4;
435 if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
436 type_select += 8;
437 return ec_types[type_select];
438 }
439
440 static
441 const OSSL_PARAM *ec_import_types(int selection)
442 {
443 return ec_imexport_types(selection);
444 }
445
446 static
447 const OSSL_PARAM *ec_export_types(int selection)
448 {
449 return ec_imexport_types(selection);
450 }
451
452 static int ec_get_ecm_params(const EC_GROUP *group, OSSL_PARAM params[])
453 {
454 #ifdef OPENSSL_NO_EC2M
455 return 1;
456 #else
457 int ret = 0, m;
458 unsigned int k1 = 0, k2 = 0, k3 = 0;
459 int basis_nid;
460 const char *basis_name = NULL;
461 int fid = EC_GROUP_get_field_type(group);
462
463 if (fid != NID_X9_62_characteristic_two_field)
464 return 1;
465
466 basis_nid = EC_GROUP_get_basis_type(group);
467 if (basis_nid == NID_X9_62_tpBasis)
468 basis_name = SN_X9_62_tpBasis;
469 else if (basis_nid == NID_X9_62_ppBasis)
470 basis_name = SN_X9_62_ppBasis;
471 else
472 goto err;
473
474 m = EC_GROUP_get_degree(group);
475 if (!ossl_param_build_set_int(NULL, params, OSSL_PKEY_PARAM_EC_CHAR2_M, m)
476 || !ossl_param_build_set_utf8_string(NULL, params,
477 OSSL_PKEY_PARAM_EC_CHAR2_TYPE,
478 basis_name))
479 goto err;
480
481 if (basis_nid == NID_X9_62_tpBasis) {
482 if (!EC_GROUP_get_trinomial_basis(group, &k1)
483 || !ossl_param_build_set_int(NULL, params,
484 OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS,
485 (int)k1))
486 goto err;
487 } else {
488 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)
489 || !ossl_param_build_set_int(NULL, params,
490 OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, (int)k1)
491 || !ossl_param_build_set_int(NULL, params,
492 OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, (int)k2)
493 || !ossl_param_build_set_int(NULL, params,
494 OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, (int)k3))
495 goto err;
496 }
497 ret = 1;
498 err:
499 return ret;
500 #endif /* OPENSSL_NO_EC2M */
501 }
502
503 static
504 int ec_get_params(void *key, OSSL_PARAM params[])
505 {
506 int ret = 0;
507 EC_KEY *eck = key;
508 const EC_GROUP *ecg = NULL;
509 OSSL_PARAM *p;
510 unsigned char *pub_key = NULL, *genbuf = NULL;
511 OPENSSL_CTX *libctx;
512 const char *propq;
513 BN_CTX *bnctx = NULL;
514
515 ecg = EC_KEY_get0_group(eck);
516 if (ecg == NULL)
517 return 0;
518
519 libctx = ec_key_get_libctx(eck);
520 propq = ec_key_get0_propq(eck);
521
522 bnctx = BN_CTX_new_ex(libctx);
523 if (bnctx == NULL)
524 return 0;
525 BN_CTX_start(bnctx);
526
527 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
528 && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
529 return 0;
530 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
531 && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
532 return 0;
533 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) {
534 int ecbits, sec_bits;
535
536 ecbits = EC_GROUP_order_bits(ecg);
537
538 /*
539 * The following estimates are based on the values published
540 * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
541 * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
542 *
543 * Note that the above reference explicitly categorizes algorithms in a
544 * discrete set of values {80, 112, 128, 192, 256}, and that it is
545 * relevant only for NIST approved Elliptic Curves, while OpenSSL
546 * applies the same logic also to other curves.
547 *
548 * Classifications produced by other standardazing bodies might differ,
549 * so the results provided for "bits of security" by this provider are
550 * to be considered merely indicative, and it is the users'
551 * responsibility to compare these values against the normative
552 * references that may be relevant for their intent and purposes.
553 */
554 if (ecbits >= 512)
555 sec_bits = 256;
556 else if (ecbits >= 384)
557 sec_bits = 192;
558 else if (ecbits >= 256)
559 sec_bits = 128;
560 else if (ecbits >= 224)
561 sec_bits = 112;
562 else if (ecbits >= 160)
563 sec_bits = 80;
564 else
565 sec_bits = ecbits / 2;
566
567 if (!OSSL_PARAM_set_int(p, sec_bits))
568 return 0;
569 }
570
571 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
572 && !OSSL_PARAM_set_utf8_string(p, EC_DEFAULT_MD))
573 return 0;
574
575 p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
576 if (p != NULL) {
577 int ecdh_cofactor_mode = 0;
578
579 ecdh_cofactor_mode =
580 (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
581
582 if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
583 return 0;
584 }
585 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT)) != NULL) {
586 p->return_size = EC_POINT_point2oct(EC_KEY_get0_group(key),
587 EC_KEY_get0_public_key(key),
588 POINT_CONVERSION_UNCOMPRESSED,
589 p->data, p->return_size, bnctx);
590 if (p->return_size == 0)
591 goto err;
592 }
593
594 ret = ec_get_ecm_params(ecg, params)
595 && ec_group_todata(ecg, NULL, params, libctx, propq, bnctx, &genbuf)
596 && key_to_params(eck, NULL, params, 1, &pub_key)
597 && otherparams_to_params(eck, NULL, params);
598 err:
599 OPENSSL_free(genbuf);
600 OPENSSL_free(pub_key);
601 BN_CTX_end(bnctx);
602 BN_CTX_free(bnctx);
603 return ret;
604 }
605
606 #ifndef OPENSSL_NO_EC2M
607 # define EC2M_GETTABLE_DOM_PARAMS \
608 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL), \
609 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0), \
610 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL), \
611 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL), \
612 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL), \
613 OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL),
614 #else
615 # define EC2M_GETTABLE_DOM_PARAMS
616 #endif
617
618 static const OSSL_PARAM ec_known_gettable_params[] = {
619 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
620 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
621 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
622 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0),
623 EC_IMEXPORTABLE_DOM_PARAMETERS,
624 EC2M_GETTABLE_DOM_PARAMS
625 EC_IMEXPORTABLE_PUBLIC_KEY,
626 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
627 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
628 EC_IMEXPORTABLE_PRIVATE_KEY,
629 EC_IMEXPORTABLE_OTHER_PARAMETERS,
630 OSSL_PARAM_END
631 };
632
633 static
634 const OSSL_PARAM *ec_gettable_params(void *provctx)
635 {
636 return ec_known_gettable_params;
637 }
638
639 static const OSSL_PARAM ec_known_settable_params[] = {
640 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
641 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0),
642 OSSL_PARAM_END
643 };
644
645 static
646 const OSSL_PARAM *ec_settable_params(void *provctx)
647 {
648 return ec_known_settable_params;
649 }
650
651 static
652 int ec_set_params(void *key, const OSSL_PARAM params[])
653 {
654 EC_KEY *eck = key;
655 const OSSL_PARAM *p;
656
657 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT);
658 if (p != NULL) {
659 BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(key));
660 int ret = 1;
661
662 if (ctx == NULL
663 || p->data_type != OSSL_PARAM_OCTET_STRING
664 || !EC_KEY_oct2key(key, p->data, p->data_size, ctx))
665 ret = 0;
666 BN_CTX_free(ctx);
667 if (!ret)
668 return 0;
669 }
670
671 return ec_key_otherparams_fromdata(eck, params);
672 }
673
674 static
675 int ec_validate(void *keydata, int selection)
676 {
677 EC_KEY *eck = keydata;
678 int ok = 0;
679 BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(eck));
680
681 if (ctx == NULL)
682 return 0;
683
684 if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
685 ok = 1;
686
687 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
688 ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
689
690 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
691 ok = ok && ec_key_public_check(eck, ctx);
692
693 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
694 ok = ok && ec_key_private_check(eck);
695
696 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
697 ok = ok && ec_key_pairwise_check(eck, ctx);
698
699 BN_CTX_free(ctx);
700 return ok;
701 }
702
703 struct ec_gen_ctx {
704 OPENSSL_CTX *libctx;
705 char *group_name;
706 char *encoding;
707 char *field_type;
708 BIGNUM *p, *a, *b, *order, *cofactor;
709 unsigned char *gen, *seed;
710 size_t gen_len, seed_len;
711 int selection;
712 int ecdh_mode;
713 EC_GROUP *gen_group;
714 };
715
716 static void *ec_gen_init(void *provctx, int selection)
717 {
718 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
719 struct ec_gen_ctx *gctx = NULL;
720
721 if ((selection & (EC_POSSIBLE_SELECTIONS)) == 0)
722 return NULL;
723
724 if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
725 gctx->libctx = libctx;
726 gctx->selection = selection;
727 gctx->ecdh_mode = 0;
728 }
729 return gctx;
730 }
731
732 static int ec_gen_set_group(void *genctx, const EC_GROUP *src)
733 {
734 struct ec_gen_ctx *gctx = genctx;
735 EC_GROUP *group;
736
737 group = EC_GROUP_dup(src);
738 if (group == NULL) {
739 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
740 return 0;
741 }
742 EC_GROUP_free(gctx->gen_group);
743 gctx->gen_group = group;
744 return 1;
745 }
746
747 static int ec_gen_set_template(void *genctx, void *templ)
748 {
749 struct ec_gen_ctx *gctx = genctx;
750 EC_KEY *ec = templ;
751 const EC_GROUP *ec_group;
752
753 if (gctx == NULL || ec == NULL)
754 return 0;
755 if ((ec_group = EC_KEY_get0_group(ec)) == NULL)
756 return 0;
757 return ec_gen_set_group(gctx, ec_group);
758 }
759
760 #define COPY_INT_PARAM(params, key, val) \
761 p = OSSL_PARAM_locate_const(params, key); \
762 if (p != NULL && !OSSL_PARAM_get_int(p, &val)) \
763 goto err;
764
765 #define COPY_UTF8_PARAM(params, key, val) \
766 p = OSSL_PARAM_locate_const(params, key); \
767 if (p != NULL) { \
768 if (p->data_type != OSSL_PARAM_UTF8_STRING) \
769 goto err; \
770 OPENSSL_free(val); \
771 val = OPENSSL_strdup(p->data); \
772 if (val == NULL) \
773 goto err; \
774 }
775
776 #define COPY_OCTET_PARAM(params, key, val, len) \
777 p = OSSL_PARAM_locate_const(params, key); \
778 if (p != NULL) { \
779 if (p->data_type != OSSL_PARAM_OCTET_STRING) \
780 goto err; \
781 OPENSSL_free(val); \
782 len = p->data_size; \
783 val = OPENSSL_memdup(p->data, p->data_size); \
784 if (val == NULL) \
785 goto err; \
786 }
787
788 #define COPY_BN_PARAM(params, key, bn) \
789 p = OSSL_PARAM_locate_const(params, key); \
790 if (p != NULL) { \
791 if (bn == NULL) \
792 bn = BN_new(); \
793 if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn)) \
794 goto err; \
795 }
796
797 static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[])
798 {
799 int ret = 0;
800 struct ec_gen_ctx *gctx = genctx;
801 const OSSL_PARAM *p;
802 EC_GROUP *group = NULL;
803
804 COPY_INT_PARAM(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, gctx->ecdh_mode);
805
806 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_GROUP_NAME, gctx->group_name);
807 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE, gctx->field_type);
808 COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_ENCODING, gctx->encoding);
809
810 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_P, gctx->p);
811 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_A, gctx->a);
812 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_B, gctx->b);
813 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_ORDER, gctx->order);
814 COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_COFACTOR, gctx->cofactor);
815
816 COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_SEED, gctx->seed, gctx->seed_len);
817 COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_GENERATOR, gctx->gen,
818 gctx->gen_len);
819
820 ret = 1;
821 err:
822 EC_GROUP_free(group);
823 return ret;
824 }
825
826 static int ec_gen_set_group_from_params(struct ec_gen_ctx *gctx)
827 {
828 int ret = 0;
829 OSSL_PARAM_BLD *bld;
830 OSSL_PARAM *params = NULL;
831 EC_GROUP *group = NULL;
832
833 bld = OSSL_PARAM_BLD_new();
834 if (bld == NULL)
835 return 0;
836
837 if (gctx->encoding != NULL
838 && !OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_ENCODING,
839 gctx->encoding, 0))
840 goto err;
841
842 if (gctx->group_name != NULL) {
843 if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
844 gctx->group_name, 0))
845 goto err;
846 /* Ignore any other parameters if there is a group name */
847 goto build;
848 } else if (gctx->field_type != NULL) {
849 if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
850 gctx->field_type, 0))
851 goto err;
852 } else {
853 goto err;
854 }
855 if (gctx->p == NULL
856 || gctx->a == NULL
857 || gctx->b == NULL
858 || gctx->order == NULL
859 || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, gctx->p)
860 || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, gctx->a)
861 || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, gctx->b)
862 || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER, gctx->order))
863 goto err;
864
865 if (gctx->cofactor != NULL
866 && !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR,
867 gctx->cofactor))
868 goto err;
869
870 if (gctx->seed != NULL
871 && !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_SEED,
872 gctx->seed, gctx->seed_len))
873 goto err;
874
875 if (gctx->gen == NULL
876 || !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_GENERATOR,
877 gctx->gen, gctx->gen_len))
878 goto err;
879 build:
880 params = OSSL_PARAM_BLD_to_param(bld);
881 if (params == NULL)
882 goto err;
883 group = EC_GROUP_new_from_params(params, gctx->libctx, NULL);
884 if (group == NULL)
885 goto err;
886
887 EC_GROUP_free(gctx->gen_group);
888 gctx->gen_group = group;
889
890 ret = 1;
891 err:
892 OSSL_PARAM_BLD_free_params(params);
893 OSSL_PARAM_BLD_free(bld);
894 return ret;
895 }
896
897 static const OSSL_PARAM *ec_gen_settable_params(void *provctx)
898 {
899 static OSSL_PARAM settable[] = {
900 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
901 OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
902 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
903 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0),
904 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0),
905 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0),
906 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0),
907 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0),
908 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0),
909 OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0),
910 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
911 OSSL_PARAM_END
912 };
913
914 return settable;
915 }
916
917 static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group)
918 {
919 if (group == NULL) {
920 ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
921 return 0;
922 }
923 return EC_KEY_set_group(ec, group) > 0;
924 }
925
926 /*
927 * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
928 */
929 static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
930 {
931 struct ec_gen_ctx *gctx = genctx;
932 EC_KEY *ec = NULL;
933 int ret = 0;
934
935 if (gctx == NULL
936 || (ec = EC_KEY_new_with_libctx(gctx->libctx, NULL)) == NULL)
937 return NULL;
938
939 if (gctx->gen_group == NULL) {
940 if (!ec_gen_set_group_from_params(gctx))
941 goto err;
942 } else {
943 if (gctx->encoding) {
944 int flags = ec_encoding_name2id(gctx->encoding);
945 if (flags < 0)
946 goto err;
947 EC_GROUP_set_asn1_flag(gctx->gen_group, flags);
948 }
949 }
950
951 /* We must always assign a group, no matter what */
952 ret = ec_gen_assign_group(ec, gctx->gen_group);
953
954 /* Whether you want it or not, you get a keypair, not just one half */
955 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
956 ret = ret && EC_KEY_generate_key(ec);
957
958 if (gctx->ecdh_mode != -1)
959 ret = ret && ec_set_ecdh_cofactor_mode(ec, gctx->ecdh_mode);
960
961 if (ret)
962 return ec;
963 err:
964 /* Something went wrong, throw the key away */
965 EC_KEY_free(ec);
966 return NULL;
967 }
968
969 static void ec_gen_cleanup(void *genctx)
970 {
971 struct ec_gen_ctx *gctx = genctx;
972
973 if (gctx == NULL)
974 return;
975
976 EC_GROUP_free(gctx->gen_group);
977 BN_free(gctx->p);
978 BN_free(gctx->a);
979 BN_free(gctx->b);
980 BN_free(gctx->order);
981 BN_free(gctx->cofactor);
982 OPENSSL_free(gctx->group_name);
983 OPENSSL_free(gctx->field_type);;
984 OPENSSL_free(gctx->encoding);
985 OPENSSL_free(gctx->seed);
986 OPENSSL_free(gctx->gen);
987 OPENSSL_free(gctx);
988 }
989
990 void *ec_load(const void *reference, size_t reference_sz)
991 {
992 EC_KEY *ec = NULL;
993
994 if (reference_sz == sizeof(ec)) {
995 /* The contents of the reference is the address to our object */
996 ec = *(EC_KEY **)reference;
997 /* We grabbed, so we detach it */
998 *(EC_KEY **)reference = NULL;
999 return ec;
1000 }
1001 return NULL;
1002 }
1003
1004 const OSSL_DISPATCH ec_keymgmt_functions[] = {
1005 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
1006 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init },
1007 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
1008 (void (*)(void))ec_gen_set_template },
1009 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
1010 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
1011 (void (*)(void))ec_gen_settable_params },
1012 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ec_gen },
1013 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
1014 { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))ec_load },
1015 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
1016 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params },
1017 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params },
1018 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
1019 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params },
1020 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
1021 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
1022 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
1023 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
1024 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
1025 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
1026 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
1027 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
1028 (void (*)(void))ec_query_operation_name },
1029 { 0, NULL }
1030 };