]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ec_ameth.c
Fix external symbols related to ec & sm2 keys
[thirdparty/openssl.git] / crypto / ec / ec_ameth.c
1 /*
2 * Copyright 2006-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 and ECDSA low level APIs are deprecated for public use, but still ok
12 * for internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/x509.h>
19 #include <openssl/ec.h>
20 #include <openssl/bn.h>
21 #include <openssl/asn1t.h>
22 #include "crypto/asn1.h"
23 #include "crypto/evp.h"
24 #include "crypto/x509.h"
25 #include <openssl/core_names.h>
26 #include "openssl/param_build.h"
27 #include "ec_local.h"
28
29 static int eckey_param2type(int *pptype, void **ppval, const EC_KEY *ec_key)
30 {
31 const EC_GROUP *group;
32 int nid;
33
34 if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
35 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
36 return 0;
37 }
38 if (EC_GROUP_get_asn1_flag(group)
39 && (nid = EC_GROUP_get_curve_name(group)))
40 /* we have a 'named curve' => just set the OID */
41 {
42 ASN1_OBJECT *asn1obj = OBJ_nid2obj(nid);
43
44 if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
45 ASN1_OBJECT_free(asn1obj);
46 ERR_raise(ERR_LIB_EC, EC_R_MISSING_OID);
47 return 0;
48 }
49 *ppval = asn1obj;
50 *pptype = V_ASN1_OBJECT;
51 } else { /* explicit parameters */
52
53 ASN1_STRING *pstr = NULL;
54 pstr = ASN1_STRING_new();
55 if (pstr == NULL)
56 return 0;
57 pstr->length = i2d_ECParameters(ec_key, &pstr->data);
58 if (pstr->length <= 0) {
59 ASN1_STRING_free(pstr);
60 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
61 return 0;
62 }
63 *ppval = pstr;
64 *pptype = V_ASN1_SEQUENCE;
65 }
66 return 1;
67 }
68
69 static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
70 {
71 const EC_KEY *ec_key = pkey->pkey.ec;
72 void *pval = NULL;
73 int ptype;
74 unsigned char *penc = NULL, *p;
75 int penclen;
76
77 if (!eckey_param2type(&ptype, &pval, ec_key)) {
78 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
79 return 0;
80 }
81 penclen = i2o_ECPublicKey(ec_key, NULL);
82 if (penclen <= 0)
83 goto err;
84 penc = OPENSSL_malloc(penclen);
85 if (penc == NULL)
86 goto err;
87 p = penc;
88 penclen = i2o_ECPublicKey(ec_key, &p);
89 if (penclen <= 0)
90 goto err;
91 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
92 ptype, pval, penc, penclen))
93 return 1;
94 err:
95 if (ptype == V_ASN1_OBJECT)
96 ASN1_OBJECT_free(pval);
97 else
98 ASN1_STRING_free(pval);
99 OPENSSL_free(penc);
100 return 0;
101 }
102
103 static EC_KEY *eckey_type2param(int ptype, const void *pval,
104 OSSL_LIB_CTX *libctx, const char *propq)
105 {
106 EC_KEY *eckey = NULL;
107 EC_GROUP *group = NULL;
108
109 if ((eckey = EC_KEY_new_ex(libctx, propq)) == NULL) {
110 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
111 goto ecerr;
112 }
113
114 if (ptype == V_ASN1_SEQUENCE) {
115 const ASN1_STRING *pstr = pval;
116 const unsigned char *pm = pstr->data;
117 int pmlen = pstr->length;
118
119
120 if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) {
121 ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
122 goto ecerr;
123 }
124 } else if (ptype == V_ASN1_OBJECT) {
125 const ASN1_OBJECT *poid = pval;
126
127 /*
128 * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
129 */
130
131 group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid));
132 if (group == NULL)
133 goto ecerr;
134 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
135 if (EC_KEY_set_group(eckey, group) == 0)
136 goto ecerr;
137 EC_GROUP_free(group);
138 } else {
139 ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
140 goto ecerr;
141 }
142
143 return eckey;
144
145 ecerr:
146 EC_KEY_free(eckey);
147 EC_GROUP_free(group);
148 return NULL;
149 }
150
151 static int eckey_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
152 {
153 const unsigned char *p = NULL;
154 const void *pval;
155 int ptype, pklen;
156 EC_KEY *eckey = NULL;
157 X509_ALGOR *palg;
158 OSSL_LIB_CTX *libctx = NULL;
159 const char *propq = NULL;
160
161 if (!X509_PUBKEY_get0_libctx(&libctx, &propq, pubkey)
162 || !X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
163 return 0;
164 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
165
166 eckey = eckey_type2param(ptype, pval, libctx, propq);
167
168 if (!eckey)
169 return 0;
170
171 /* We have parameters now set public key */
172 if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
173 ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
174 goto ecerr;
175 }
176
177 EVP_PKEY_assign_EC_KEY(pkey, eckey);
178 return 1;
179
180 ecerr:
181 EC_KEY_free(eckey);
182 return 0;
183 }
184
185 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
186 {
187 int r;
188 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
189 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
190 *pb = EC_KEY_get0_public_key(b->pkey.ec);
191
192 if (group == NULL || pa == NULL || pb == NULL)
193 return -2;
194 r = EC_POINT_cmp(group, pa, pb, NULL);
195 if (r == 0)
196 return 1;
197 if (r == 1)
198 return 0;
199 return -2;
200 }
201
202 static int eckey_priv_decode_ex(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8,
203 OSSL_LIB_CTX *libctx, const char *propq)
204 {
205 const unsigned char *p = NULL;
206 const void *pval;
207 int ptype, pklen;
208 EC_KEY *eckey = NULL;
209 const X509_ALGOR *palg;
210
211 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
212 return 0;
213 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
214
215 eckey = eckey_type2param(ptype, pval, libctx, propq);
216 if (eckey == NULL)
217 goto err;
218
219 /* We have parameters now set private key */
220 if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
221 ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
222 goto err;
223 }
224
225 EVP_PKEY_assign_EC_KEY(pkey, eckey);
226 return 1;
227
228 err:
229 EC_KEY_free(eckey);
230 return 0;
231 }
232
233 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
234 {
235 EC_KEY ec_key = *(pkey->pkey.ec);
236 unsigned char *ep, *p;
237 int eplen, ptype;
238 void *pval;
239 unsigned int old_flags;
240
241 if (!eckey_param2type(&ptype, &pval, &ec_key)) {
242 ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
243 return 0;
244 }
245
246 /* set the private key */
247
248 /*
249 * do not include the parameters in the SEC1 private key see PKCS#11
250 * 12.11
251 */
252 old_flags = EC_KEY_get_enc_flags(&ec_key);
253 EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
254
255 eplen = i2d_ECPrivateKey(&ec_key, NULL);
256 if (!eplen) {
257 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
258 return 0;
259 }
260 ep = OPENSSL_malloc(eplen);
261 if (ep == NULL) {
262 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
263 return 0;
264 }
265 p = ep;
266 if (!i2d_ECPrivateKey(&ec_key, &p)) {
267 OPENSSL_free(ep);
268 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
269 return 0;
270 }
271
272 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
273 ptype, pval, ep, eplen)) {
274 OPENSSL_free(ep);
275 return 0;
276 }
277
278 return 1;
279 }
280
281 static int int_ec_size(const EVP_PKEY *pkey)
282 {
283 return ECDSA_size(pkey->pkey.ec);
284 }
285
286 static int ec_bits(const EVP_PKEY *pkey)
287 {
288 return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
289 }
290
291 static int ec_security_bits(const EVP_PKEY *pkey)
292 {
293 int ecbits = ec_bits(pkey);
294
295 if (ecbits >= 512)
296 return 256;
297 if (ecbits >= 384)
298 return 192;
299 if (ecbits >= 256)
300 return 128;
301 if (ecbits >= 224)
302 return 112;
303 if (ecbits >= 160)
304 return 80;
305 return ecbits / 2;
306 }
307
308 static int ec_missing_parameters(const EVP_PKEY *pkey)
309 {
310 if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
311 return 1;
312 return 0;
313 }
314
315 static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
316 {
317 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
318
319 if (group == NULL)
320 return 0;
321 if (to->pkey.ec == NULL) {
322 to->pkey.ec = EC_KEY_new();
323 if (to->pkey.ec == NULL)
324 goto err;
325 }
326 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
327 goto err;
328 EC_GROUP_free(group);
329 return 1;
330 err:
331 EC_GROUP_free(group);
332 return 0;
333 }
334
335 static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
336 {
337 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
338 *group_b = EC_KEY_get0_group(b->pkey.ec);
339
340 if (group_a == NULL || group_b == NULL)
341 return -2;
342 if (EC_GROUP_cmp(group_a, group_b, NULL))
343 return 0;
344 else
345 return 1;
346 }
347
348 static void int_ec_free(EVP_PKEY *pkey)
349 {
350 EC_KEY_free(pkey->pkey.ec);
351 }
352
353 typedef enum {
354 EC_KEY_PRINT_PRIVATE,
355 EC_KEY_PRINT_PUBLIC,
356 EC_KEY_PRINT_PARAM
357 } ec_print_t;
358
359 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
360 {
361 const char *ecstr;
362 unsigned char *priv = NULL, *pub = NULL;
363 size_t privlen = 0, publen = 0;
364 int ret = 0;
365 const EC_GROUP *group;
366
367 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
368 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
369 return 0;
370 }
371
372 if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
373 publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
374 if (publen == 0)
375 goto err;
376 }
377
378 if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
379 privlen = EC_KEY_priv2buf(x, &priv);
380 if (privlen == 0)
381 goto err;
382 }
383
384 if (ktype == EC_KEY_PRINT_PRIVATE)
385 ecstr = "Private-Key";
386 else if (ktype == EC_KEY_PRINT_PUBLIC)
387 ecstr = "Public-Key";
388 else
389 ecstr = "ECDSA-Parameters";
390
391 if (!BIO_indent(bp, off, 128))
392 goto err;
393 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
394 EC_GROUP_order_bits(group)) <= 0)
395 goto err;
396
397 if (privlen != 0) {
398 if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
399 goto err;
400 if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
401 goto err;
402 }
403
404 if (publen != 0) {
405 if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
406 goto err;
407 if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
408 goto err;
409 }
410
411 if (!ECPKParameters_print(bp, group, off))
412 goto err;
413 ret = 1;
414 err:
415 if (!ret)
416 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
417 OPENSSL_clear_free(priv, privlen);
418 OPENSSL_free(pub);
419 return ret;
420 }
421
422 static int eckey_param_decode(EVP_PKEY *pkey,
423 const unsigned char **pder, int derlen)
424 {
425 EC_KEY *eckey;
426
427 if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL)
428 return 0;
429 EVP_PKEY_assign_EC_KEY(pkey, eckey);
430 return 1;
431 }
432
433 static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
434 {
435 return i2d_ECParameters(pkey->pkey.ec, pder);
436 }
437
438 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
439 ASN1_PCTX *ctx)
440 {
441 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
442 }
443
444 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
445 ASN1_PCTX *ctx)
446 {
447 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
448 }
449
450 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
451 ASN1_PCTX *ctx)
452 {
453 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
454 }
455
456 static int old_ec_priv_decode(EVP_PKEY *pkey,
457 const unsigned char **pder, int derlen)
458 {
459 EC_KEY *ec;
460
461 if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL)
462 return 0;
463 EVP_PKEY_assign_EC_KEY(pkey, ec);
464 return 1;
465 }
466
467 static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
468 {
469 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
470 }
471
472 static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
473 {
474 switch (op) {
475 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
476 if (EVP_PKEY_id(pkey) == EVP_PKEY_SM2) {
477 /* For SM2, the only valid digest-alg is SM3 */
478 *(int *)arg2 = NID_sm3;
479 return 2; /* Make it mandatory */
480 }
481 *(int *)arg2 = NID_sha256;
482 return 1;
483
484 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
485 return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL);
486
487 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
488 return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
489 POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
490
491 default:
492 return -2;
493 }
494 }
495
496 static int ec_pkey_check(const EVP_PKEY *pkey)
497 {
498 EC_KEY *eckey = pkey->pkey.ec;
499
500 /* stay consistent to what EVP_PKEY_check demands */
501 if (eckey->priv_key == NULL) {
502 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
503 return 0;
504 }
505
506 return EC_KEY_check_key(eckey);
507 }
508
509 static int ec_pkey_public_check(const EVP_PKEY *pkey)
510 {
511 EC_KEY *eckey = pkey->pkey.ec;
512
513 /*
514 * Note: it unnecessary to check eckey->pub_key here since
515 * it will be checked in EC_KEY_check_key(). In fact, the
516 * EC_KEY_check_key() mainly checks the public key, and checks
517 * the private key optionally (only if there is one). So if
518 * someone passes a whole EC key (public + private), this
519 * will also work...
520 */
521
522 return EC_KEY_check_key(eckey);
523 }
524
525 static int ec_pkey_param_check(const EVP_PKEY *pkey)
526 {
527 EC_KEY *eckey = pkey->pkey.ec;
528
529 /* stay consistent to what EVP_PKEY_check demands */
530 if (eckey->group == NULL) {
531 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
532 return 0;
533 }
534
535 return EC_GROUP_check(eckey->group, NULL);
536 }
537
538 static
539 size_t ec_pkey_dirty_cnt(const EVP_PKEY *pkey)
540 {
541 return pkey->pkey.ec->dirty_cnt;
542 }
543
544 static
545 int ec_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
546 EVP_KEYMGMT *to_keymgmt, OSSL_LIB_CTX *libctx,
547 const char *propq)
548 {
549 const EC_KEY *eckey = NULL;
550 const EC_GROUP *ecg = NULL;
551 unsigned char *pub_key_buf = NULL, *gen_buf = NULL;
552 size_t pub_key_buflen;
553 OSSL_PARAM_BLD *tmpl;
554 OSSL_PARAM *params = NULL;
555 const BIGNUM *priv_key = NULL;
556 const EC_POINT *pub_point = NULL;
557 int selection = 0;
558 int rv = 0;
559 BN_CTX *bnctx = NULL;
560
561 if (from == NULL
562 || (eckey = from->pkey.ec) == NULL
563 || (ecg = EC_KEY_get0_group(eckey)) == NULL)
564 return 0;
565
566 /*
567 * If the EC_KEY method is foreign, then we can't be sure of anything,
568 * and can therefore not export or pretend to export.
569 */
570 if (EC_KEY_get_method(eckey) != EC_KEY_OpenSSL())
571 return 0;
572
573 tmpl = OSSL_PARAM_BLD_new();
574 if (tmpl == NULL)
575 return 0;
576
577 /*
578 * EC_POINT_point2buf() can generate random numbers in some
579 * implementations so we need to ensure we use the correct libctx.
580 */
581 bnctx = BN_CTX_new_ex(libctx);
582 if (bnctx == NULL)
583 goto err;
584 BN_CTX_start(bnctx);
585
586 /* export the domain parameters */
587 if (!ossl_ec_group_todata(ecg, tmpl, NULL, libctx, propq, bnctx, &gen_buf))
588 goto err;
589 selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
590
591 priv_key = EC_KEY_get0_private_key(eckey);
592 pub_point = EC_KEY_get0_public_key(eckey);
593
594 if (pub_point != NULL) {
595 /* convert pub_point to a octet string according to the SECG standard */
596 if ((pub_key_buflen = EC_POINT_point2buf(ecg, pub_point,
597 POINT_CONVERSION_COMPRESSED,
598 &pub_key_buf, bnctx)) == 0
599 || !OSSL_PARAM_BLD_push_octet_string(tmpl,
600 OSSL_PKEY_PARAM_PUB_KEY,
601 pub_key_buf,
602 pub_key_buflen))
603 goto err;
604 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
605 }
606
607 if (priv_key != NULL) {
608 size_t sz;
609 int ecbits;
610 int ecdh_cofactor_mode;
611
612 /*
613 * Key import/export should never leak the bit length of the secret
614 * scalar in the key.
615 *
616 * For this reason, on export we use padded BIGNUMs with fixed length.
617 *
618 * When importing we also should make sure that, even if short lived,
619 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
620 * soon as possible, so that any processing of this BIGNUM might opt for
621 * constant time implementations in the backend.
622 *
623 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
624 * to preallocate the BIGNUM internal buffer to a fixed public size big
625 * enough that operations performed during the processing never trigger
626 * a realloc which would leak the size of the scalar through memory
627 * accesses.
628 *
629 * Fixed Length
630 * ------------
631 *
632 * The order of the large prime subgroup of the curve is our choice for
633 * a fixed public size, as that is generally the upper bound for
634 * generating a private key in EC cryptosystems and should fit all valid
635 * secret scalars.
636 *
637 * For padding on export we just use the bit length of the order
638 * converted to bytes (rounding up).
639 *
640 * For preallocating the BIGNUM storage we look at the number of "words"
641 * required for the internal representation of the order, and we
642 * preallocate 2 extra "words" in case any of the subsequent processing
643 * might temporarily overflow the order length.
644 */
645 ecbits = EC_GROUP_order_bits(ecg);
646 if (ecbits <= 0)
647 goto err;
648
649 sz = (ecbits + 7 ) / 8;
650 if (!OSSL_PARAM_BLD_push_BN_pad(tmpl,
651 OSSL_PKEY_PARAM_PRIV_KEY,
652 priv_key, sz))
653 goto err;
654 selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
655
656 /*
657 * The ECDH Cofactor Mode is defined only if the EC_KEY actually
658 * contains a private key, so we check for the flag and export it only
659 * in this case.
660 */
661 ecdh_cofactor_mode =
662 (EC_KEY_get_flags(eckey) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
663
664 /* Export the ECDH_COFACTOR_MODE parameter */
665 if (!OSSL_PARAM_BLD_push_int(tmpl,
666 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
667 ecdh_cofactor_mode))
668 goto err;
669 selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
670 }
671
672 params = OSSL_PARAM_BLD_to_param(tmpl);
673
674 /* We export, the provider imports */
675 rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
676
677 err:
678 OSSL_PARAM_BLD_free(tmpl);
679 OSSL_PARAM_BLD_free_params(params);
680 OPENSSL_free(pub_key_buf);
681 OPENSSL_free(gen_buf);
682 BN_CTX_end(bnctx);
683 BN_CTX_free(bnctx);
684 return rv;
685 }
686
687 static int ec_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
688 {
689 EVP_PKEY_CTX *pctx = vpctx;
690 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
691 EC_KEY *ec = EC_KEY_new_ex(pctx->libctx, pctx->propquery);
692
693 if (ec == NULL) {
694 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
695 return 0;
696 }
697
698 if (!ossl_ec_group_fromdata(ec, params)
699 || !ossl_ec_key_otherparams_fromdata(ec, params)
700 || !ossl_ec_key_fromdata(ec, params, 1)
701 || !EVP_PKEY_assign_EC_KEY(pkey, ec)) {
702 EC_KEY_free(ec);
703 return 0;
704 }
705 return 1;
706 }
707
708 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
709 EVP_PKEY_EC,
710 EVP_PKEY_EC,
711 0,
712 "EC",
713 "OpenSSL EC algorithm",
714
715 eckey_pub_decode,
716 eckey_pub_encode,
717 eckey_pub_cmp,
718 eckey_pub_print,
719
720 NULL,
721 eckey_priv_encode,
722 eckey_priv_print,
723
724 int_ec_size,
725 ec_bits,
726 ec_security_bits,
727
728 eckey_param_decode,
729 eckey_param_encode,
730 ec_missing_parameters,
731 ec_copy_parameters,
732 ec_cmp_parameters,
733 eckey_param_print,
734 0,
735
736 int_ec_free,
737 ec_pkey_ctrl,
738 old_ec_priv_decode,
739 old_ec_priv_encode,
740
741 0, 0, 0,
742
743 ec_pkey_check,
744 ec_pkey_public_check,
745 ec_pkey_param_check,
746
747 0, /* set_priv_key */
748 0, /* set_pub_key */
749 0, /* get_priv_key */
750 0, /* get_pub_key */
751
752 ec_pkey_dirty_cnt,
753 ec_pkey_export_to,
754 ec_pkey_import_from,
755 eckey_priv_decode_ex
756 };
757
758 #if !defined(OPENSSL_NO_SM2)
759 const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = {
760 EVP_PKEY_SM2,
761 EVP_PKEY_EC,
762 ASN1_PKEY_ALIAS
763 };
764 #endif
765
766 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
767 {
768 int private = EC_KEY_get0_private_key(x) != NULL;
769
770 return do_EC_KEY_print(bp, x, off,
771 private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
772 }
773
774 int ECParameters_print(BIO *bp, const EC_KEY *x)
775 {
776 return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
777 }