2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/x509.h>
19 #include <openssl/ec.h>
20 #include <openssl/bn.h>
21 #include <openssl/cms.h>
22 #include <openssl/asn1t.h>
23 #include "crypto/asn1.h"
24 #include "crypto/evp.h"
25 #include "crypto/x509.h"
26 #include <openssl/core_names.h>
27 #include "openssl/param_build.h"
30 #ifndef OPENSSL_NO_CMS
31 static int ecdh_cms_decrypt(CMS_RecipientInfo
*ri
);
32 static int ecdh_cms_encrypt(CMS_RecipientInfo
*ri
);
35 static int eckey_param2type(int *pptype
, void **ppval
, const EC_KEY
*ec_key
)
37 const EC_GROUP
*group
;
40 if (ec_key
== NULL
|| (group
= EC_KEY_get0_group(ec_key
)) == NULL
) {
41 ECerr(EC_F_ECKEY_PARAM2TYPE
, EC_R_MISSING_PARAMETERS
);
44 if (EC_GROUP_get_asn1_flag(group
)
45 && (nid
= EC_GROUP_get_curve_name(group
)))
46 /* we have a 'named curve' => just set the OID */
48 ASN1_OBJECT
*asn1obj
= OBJ_nid2obj(nid
);
50 if (asn1obj
== NULL
|| OBJ_length(asn1obj
) == 0) {
51 ASN1_OBJECT_free(asn1obj
);
52 ECerr(EC_F_ECKEY_PARAM2TYPE
, EC_R_MISSING_OID
);
56 *pptype
= V_ASN1_OBJECT
;
57 } else { /* explicit parameters */
59 ASN1_STRING
*pstr
= NULL
;
60 pstr
= ASN1_STRING_new();
63 pstr
->length
= i2d_ECParameters(ec_key
, &pstr
->data
);
64 if (pstr
->length
<= 0) {
65 ASN1_STRING_free(pstr
);
66 ECerr(EC_F_ECKEY_PARAM2TYPE
, ERR_R_EC_LIB
);
70 *pptype
= V_ASN1_SEQUENCE
;
75 static int eckey_pub_encode(X509_PUBKEY
*pk
, const EVP_PKEY
*pkey
)
77 const EC_KEY
*ec_key
= pkey
->pkey
.ec
;
80 unsigned char *penc
= NULL
, *p
;
83 if (!eckey_param2type(&ptype
, &pval
, ec_key
)) {
84 ECerr(EC_F_ECKEY_PUB_ENCODE
, ERR_R_EC_LIB
);
87 penclen
= i2o_ECPublicKey(ec_key
, NULL
);
90 penc
= OPENSSL_malloc(penclen
);
94 penclen
= i2o_ECPublicKey(ec_key
, &p
);
97 if (X509_PUBKEY_set0_param(pk
, OBJ_nid2obj(EVP_PKEY_EC
),
98 ptype
, pval
, penc
, penclen
))
101 if (ptype
== V_ASN1_OBJECT
)
102 ASN1_OBJECT_free(pval
);
104 ASN1_STRING_free(pval
);
109 static EC_KEY
*eckey_type2param(int ptype
, const void *pval
,
110 OPENSSL_CTX
*libctx
, const char *propq
)
112 EC_KEY
*eckey
= NULL
;
113 EC_GROUP
*group
= NULL
;
115 if ((eckey
= EC_KEY_new_with_libctx(libctx
, propq
)) == NULL
) {
116 ECerr(EC_F_ECKEY_TYPE2PARAM
, ERR_R_MALLOC_FAILURE
);
120 if (ptype
== V_ASN1_SEQUENCE
) {
121 const ASN1_STRING
*pstr
= pval
;
122 const unsigned char *pm
= pstr
->data
;
123 int pmlen
= pstr
->length
;
126 if (d2i_ECParameters(&eckey
, &pm
, pmlen
) == NULL
) {
127 ECerr(EC_F_ECKEY_TYPE2PARAM
, EC_R_DECODE_ERROR
);
130 } else if (ptype
== V_ASN1_OBJECT
) {
131 const ASN1_OBJECT
*poid
= pval
;
134 * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
137 group
= EC_GROUP_new_by_curve_name_with_libctx(libctx
, propq
,
141 EC_GROUP_set_asn1_flag(group
, OPENSSL_EC_NAMED_CURVE
);
142 if (EC_KEY_set_group(eckey
, group
) == 0)
144 EC_GROUP_free(group
);
146 ECerr(EC_F_ECKEY_TYPE2PARAM
, EC_R_DECODE_ERROR
);
154 EC_GROUP_free(group
);
158 static int eckey_pub_decode(EVP_PKEY
*pkey
, const X509_PUBKEY
*pubkey
)
160 const unsigned char *p
= NULL
;
163 EC_KEY
*eckey
= NULL
;
165 OPENSSL_CTX
*libctx
= NULL
;
166 const char *propq
= NULL
;
168 if (!X509_PUBKEY_get0_libctx(&libctx
, &propq
, pubkey
)
169 || !X509_PUBKEY_get0_param(NULL
, &p
, &pklen
, &palg
, pubkey
))
171 X509_ALGOR_get0(NULL
, &ptype
, &pval
, palg
);
173 eckey
= eckey_type2param(ptype
, pval
, libctx
, propq
);
176 ECerr(EC_F_ECKEY_PUB_DECODE
, ERR_R_EC_LIB
);
180 /* We have parameters now set public key */
181 if (!o2i_ECPublicKey(&eckey
, &p
, pklen
)) {
182 ECerr(EC_F_ECKEY_PUB_DECODE
, EC_R_DECODE_ERROR
);
186 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
194 static int eckey_pub_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
197 const EC_GROUP
*group
= EC_KEY_get0_group(b
->pkey
.ec
);
198 const EC_POINT
*pa
= EC_KEY_get0_public_key(a
->pkey
.ec
),
199 *pb
= EC_KEY_get0_public_key(b
->pkey
.ec
);
201 if (group
== NULL
|| pa
== NULL
|| pb
== NULL
)
203 r
= EC_POINT_cmp(group
, pa
, pb
, NULL
);
211 static int eckey_priv_decode_with_libctx(EVP_PKEY
*pkey
,
212 const PKCS8_PRIV_KEY_INFO
*p8
,
216 const unsigned char *p
= NULL
;
219 EC_KEY
*eckey
= NULL
;
220 const X509_ALGOR
*palg
;
222 if (!PKCS8_pkey_get0(NULL
, &p
, &pklen
, &palg
, p8
))
224 X509_ALGOR_get0(NULL
, &ptype
, &pval
, palg
);
226 eckey
= eckey_type2param(ptype
, pval
, libctx
, propq
);
231 /* We have parameters now set private key */
232 if (!d2i_ECPrivateKey(&eckey
, &p
, pklen
)) {
233 ECerr(0, EC_R_DECODE_ERROR
);
237 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
241 ECerr(0, ERR_R_EC_LIB
);
247 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO
*p8
, const EVP_PKEY
*pkey
)
249 EC_KEY ec_key
= *(pkey
->pkey
.ec
);
250 unsigned char *ep
, *p
;
253 unsigned int old_flags
;
255 if (!eckey_param2type(&ptype
, &pval
, &ec_key
)) {
256 ECerr(EC_F_ECKEY_PRIV_ENCODE
, EC_R_DECODE_ERROR
);
260 /* set the private key */
263 * do not include the parameters in the SEC1 private key see PKCS#11
266 old_flags
= EC_KEY_get_enc_flags(&ec_key
);
267 EC_KEY_set_enc_flags(&ec_key
, old_flags
| EC_PKEY_NO_PARAMETERS
);
269 eplen
= i2d_ECPrivateKey(&ec_key
, NULL
);
271 ECerr(EC_F_ECKEY_PRIV_ENCODE
, ERR_R_EC_LIB
);
274 ep
= OPENSSL_malloc(eplen
);
276 ECerr(EC_F_ECKEY_PRIV_ENCODE
, ERR_R_MALLOC_FAILURE
);
280 if (!i2d_ECPrivateKey(&ec_key
, &p
)) {
282 ECerr(EC_F_ECKEY_PRIV_ENCODE
, ERR_R_EC_LIB
);
286 if (!PKCS8_pkey_set0(p8
, OBJ_nid2obj(NID_X9_62_id_ecPublicKey
), 0,
287 ptype
, pval
, ep
, eplen
)) {
295 static int int_ec_size(const EVP_PKEY
*pkey
)
297 return ECDSA_size(pkey
->pkey
.ec
);
300 static int ec_bits(const EVP_PKEY
*pkey
)
302 return EC_GROUP_order_bits(EC_KEY_get0_group(pkey
->pkey
.ec
));
305 static int ec_security_bits(const EVP_PKEY
*pkey
)
307 int ecbits
= ec_bits(pkey
);
322 static int ec_missing_parameters(const EVP_PKEY
*pkey
)
324 if (pkey
->pkey
.ec
== NULL
|| EC_KEY_get0_group(pkey
->pkey
.ec
) == NULL
)
329 static int ec_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
331 EC_GROUP
*group
= EC_GROUP_dup(EC_KEY_get0_group(from
->pkey
.ec
));
335 if (to
->pkey
.ec
== NULL
) {
336 to
->pkey
.ec
= EC_KEY_new();
337 if (to
->pkey
.ec
== NULL
)
340 if (EC_KEY_set_group(to
->pkey
.ec
, group
) == 0)
342 EC_GROUP_free(group
);
345 EC_GROUP_free(group
);
349 static int ec_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
351 const EC_GROUP
*group_a
= EC_KEY_get0_group(a
->pkey
.ec
),
352 *group_b
= EC_KEY_get0_group(b
->pkey
.ec
);
354 if (group_a
== NULL
|| group_b
== NULL
)
356 if (EC_GROUP_cmp(group_a
, group_b
, NULL
))
362 static void int_ec_free(EVP_PKEY
*pkey
)
364 EC_KEY_free(pkey
->pkey
.ec
);
368 EC_KEY_PRINT_PRIVATE
,
373 static int do_EC_KEY_print(BIO
*bp
, const EC_KEY
*x
, int off
, ec_print_t ktype
)
376 unsigned char *priv
= NULL
, *pub
= NULL
;
377 size_t privlen
= 0, publen
= 0;
379 const EC_GROUP
*group
;
381 if (x
== NULL
|| (group
= EC_KEY_get0_group(x
)) == NULL
) {
382 ECerr(EC_F_DO_EC_KEY_PRINT
, ERR_R_PASSED_NULL_PARAMETER
);
386 if (ktype
!= EC_KEY_PRINT_PARAM
&& EC_KEY_get0_public_key(x
) != NULL
) {
387 publen
= EC_KEY_key2buf(x
, EC_KEY_get_conv_form(x
), &pub
, NULL
);
392 if (ktype
== EC_KEY_PRINT_PRIVATE
&& EC_KEY_get0_private_key(x
) != NULL
) {
393 privlen
= EC_KEY_priv2buf(x
, &priv
);
398 if (ktype
== EC_KEY_PRINT_PRIVATE
)
399 ecstr
= "Private-Key";
400 else if (ktype
== EC_KEY_PRINT_PUBLIC
)
401 ecstr
= "Public-Key";
403 ecstr
= "ECDSA-Parameters";
405 if (!BIO_indent(bp
, off
, 128))
407 if (BIO_printf(bp
, "%s: (%d bit)\n", ecstr
,
408 EC_GROUP_order_bits(group
)) <= 0)
412 if (BIO_printf(bp
, "%*spriv:\n", off
, "") <= 0)
414 if (ASN1_buf_print(bp
, priv
, privlen
, off
+ 4) == 0)
419 if (BIO_printf(bp
, "%*spub:\n", off
, "") <= 0)
421 if (ASN1_buf_print(bp
, pub
, publen
, off
+ 4) == 0)
425 if (!ECPKParameters_print(bp
, group
, off
))
430 ECerr(EC_F_DO_EC_KEY_PRINT
, ERR_R_EC_LIB
);
431 OPENSSL_clear_free(priv
, privlen
);
436 static int eckey_param_decode(EVP_PKEY
*pkey
,
437 const unsigned char **pder
, int derlen
)
441 if ((eckey
= d2i_ECParameters(NULL
, pder
, derlen
)) == NULL
) {
442 ECerr(EC_F_ECKEY_PARAM_DECODE
, ERR_R_EC_LIB
);
445 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
449 static int eckey_param_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
451 return i2d_ECParameters(pkey
->pkey
.ec
, pder
);
454 static int eckey_param_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
457 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, EC_KEY_PRINT_PARAM
);
460 static int eckey_pub_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
463 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, EC_KEY_PRINT_PUBLIC
);
466 static int eckey_priv_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
469 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, EC_KEY_PRINT_PRIVATE
);
472 static int old_ec_priv_decode(EVP_PKEY
*pkey
,
473 const unsigned char **pder
, int derlen
)
477 if ((ec
= d2i_ECPrivateKey(NULL
, pder
, derlen
)) == NULL
) {
478 ECerr(EC_F_OLD_EC_PRIV_DECODE
, EC_R_DECODE_ERROR
);
481 EVP_PKEY_assign_EC_KEY(pkey
, ec
);
485 static int old_ec_priv_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
487 return i2d_ECPrivateKey(pkey
->pkey
.ec
, pder
);
490 static int ec_pkey_ctrl(EVP_PKEY
*pkey
, int op
, long arg1
, void *arg2
)
493 case ASN1_PKEY_CTRL_PKCS7_SIGN
:
496 X509_ALGOR
*alg1
, *alg2
;
498 PKCS7_SIGNER_INFO_get0_algs(arg2
, NULL
, &alg1
, &alg2
);
499 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
501 hnid
= OBJ_obj2nid(alg1
->algorithm
);
502 if (hnid
== NID_undef
)
504 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
506 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
509 #ifndef OPENSSL_NO_CMS
510 case ASN1_PKEY_CTRL_CMS_SIGN
:
513 X509_ALGOR
*alg1
, *alg2
;
514 CMS_SignerInfo_get0_algs(arg2
, NULL
, NULL
, &alg1
, &alg2
);
515 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
517 hnid
= OBJ_obj2nid(alg1
->algorithm
);
518 if (hnid
== NID_undef
)
520 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
522 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
526 case ASN1_PKEY_CTRL_CMS_ENVELOPE
:
528 return ecdh_cms_decrypt(arg2
);
530 return ecdh_cms_encrypt(arg2
);
533 case ASN1_PKEY_CTRL_CMS_RI_TYPE
:
534 *(int *)arg2
= CMS_RECIPINFO_AGREE
;
538 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
539 if (EVP_PKEY_id(pkey
) == EVP_PKEY_SM2
) {
540 /* For SM2, the only valid digest-alg is SM3 */
541 *(int *)arg2
= NID_sm3
;
542 return 2; /* Make it mandatory */
544 *(int *)arg2
= NID_sha256
;
547 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT
:
548 return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey
), arg2
, arg1
, NULL
);
550 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT
:
551 return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey
),
552 POINT_CONVERSION_UNCOMPRESSED
, arg2
, NULL
);
561 static int ec_pkey_check(const EVP_PKEY
*pkey
)
563 EC_KEY
*eckey
= pkey
->pkey
.ec
;
565 /* stay consistent to what EVP_PKEY_check demands */
566 if (eckey
->priv_key
== NULL
) {
567 ECerr(EC_F_EC_PKEY_CHECK
, EC_R_MISSING_PRIVATE_KEY
);
571 return EC_KEY_check_key(eckey
);
574 static int ec_pkey_public_check(const EVP_PKEY
*pkey
)
576 EC_KEY
*eckey
= pkey
->pkey
.ec
;
579 * Note: it unnecessary to check eckey->pub_key here since
580 * it will be checked in EC_KEY_check_key(). In fact, the
581 * EC_KEY_check_key() mainly checks the public key, and checks
582 * the private key optionally (only if there is one). So if
583 * someone passes a whole EC key (public + private), this
587 return EC_KEY_check_key(eckey
);
590 static int ec_pkey_param_check(const EVP_PKEY
*pkey
)
592 EC_KEY
*eckey
= pkey
->pkey
.ec
;
594 /* stay consistent to what EVP_PKEY_check demands */
595 if (eckey
->group
== NULL
) {
596 ECerr(EC_F_EC_PKEY_PARAM_CHECK
, EC_R_MISSING_PARAMETERS
);
600 return EC_GROUP_check(eckey
->group
, NULL
);
604 size_t ec_pkey_dirty_cnt(const EVP_PKEY
*pkey
)
606 return pkey
->pkey
.ec
->dirty_cnt
;
610 int ec_pkey_export_to(const EVP_PKEY
*from
, void *to_keydata
,
611 EVP_KEYMGMT
*to_keymgmt
, OPENSSL_CTX
*libctx
,
614 const EC_KEY
*eckey
= NULL
;
615 const EC_GROUP
*ecg
= NULL
;
616 unsigned char *pub_key_buf
= NULL
, *gen_buf
= NULL
;
617 size_t pub_key_buflen
;
618 OSSL_PARAM_BLD
*tmpl
;
619 OSSL_PARAM
*params
= NULL
;
620 const BIGNUM
*priv_key
= NULL
;
621 const EC_POINT
*pub_point
= NULL
;
624 BN_CTX
*bnctx
= NULL
;
627 || (eckey
= from
->pkey
.ec
) == NULL
628 || (ecg
= EC_KEY_get0_group(eckey
)) == NULL
)
632 * If the EC_KEY method is foreign, then we can't be sure of anything,
633 * and can therefore not export or pretend to export.
635 if (EC_KEY_get_method(eckey
) != EC_KEY_OpenSSL())
638 tmpl
= OSSL_PARAM_BLD_new();
643 * EC_POINT_point2buf() can generate random numbers in some
644 * implementations so we need to ensure we use the correct libctx.
646 bnctx
= BN_CTX_new_ex(libctx
);
651 /* export the domain parameters */
652 if (!ec_group_todata(ecg
, tmpl
, NULL
, libctx
, propq
, bnctx
, &gen_buf
))
654 selection
|= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
;
656 priv_key
= EC_KEY_get0_private_key(eckey
);
657 pub_point
= EC_KEY_get0_public_key(eckey
);
659 if (pub_point
!= NULL
) {
660 /* convert pub_point to a octet string according to the SECG standard */
661 if ((pub_key_buflen
= EC_POINT_point2buf(ecg
, pub_point
,
662 POINT_CONVERSION_COMPRESSED
,
663 &pub_key_buf
, bnctx
)) == 0
664 || !OSSL_PARAM_BLD_push_octet_string(tmpl
,
665 OSSL_PKEY_PARAM_PUB_KEY
,
669 selection
|= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
672 if (priv_key
!= NULL
) {
675 int ecdh_cofactor_mode
;
678 * Key import/export should never leak the bit length of the secret
681 * For this reason, on export we use padded BIGNUMs with fixed length.
683 * When importing we also should make sure that, even if short lived,
684 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
685 * soon as possible, so that any processing of this BIGNUM might opt for
686 * constant time implementations in the backend.
688 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
689 * to preallocate the BIGNUM internal buffer to a fixed public size big
690 * enough that operations performed during the processing never trigger
691 * a realloc which would leak the size of the scalar through memory
697 * The order of the large prime subgroup of the curve is our choice for
698 * a fixed public size, as that is generally the upper bound for
699 * generating a private key in EC cryptosystems and should fit all valid
702 * For padding on export we just use the bit length of the order
703 * converted to bytes (rounding up).
705 * For preallocating the BIGNUM storage we look at the number of "words"
706 * required for the internal representation of the order, and we
707 * preallocate 2 extra "words" in case any of the subsequent processing
708 * might temporarily overflow the order length.
710 ecbits
= EC_GROUP_order_bits(ecg
);
714 sz
= (ecbits
+ 7 ) / 8;
715 if (!OSSL_PARAM_BLD_push_BN_pad(tmpl
,
716 OSSL_PKEY_PARAM_PRIV_KEY
,
719 selection
|= OSSL_KEYMGMT_SELECT_PRIVATE_KEY
;
722 * The ECDH Cofactor Mode is defined only if the EC_KEY actually
723 * contains a private key, so we check for the flag and export it only
727 (EC_KEY_get_flags(eckey
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
729 /* Export the ECDH_COFACTOR_MODE parameter */
730 if (!OSSL_PARAM_BLD_push_int(tmpl
,
731 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
,
734 selection
|= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
;
737 params
= OSSL_PARAM_BLD_to_param(tmpl
);
739 /* We export, the provider imports */
740 rv
= evp_keymgmt_import(to_keymgmt
, to_keydata
, selection
, params
);
743 OSSL_PARAM_BLD_free(tmpl
);
744 OSSL_PARAM_BLD_free_params(params
);
745 OPENSSL_free(pub_key_buf
);
746 OPENSSL_free(gen_buf
);
752 static int ec_pkey_import_from(const OSSL_PARAM params
[], void *vpctx
)
754 EVP_PKEY_CTX
*pctx
= vpctx
;
755 EVP_PKEY
*pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
756 EC_KEY
*ec
= EC_KEY_new_with_libctx(pctx
->libctx
, pctx
->propquery
);
759 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
763 if (!ec_group_fromdata(ec
, params
)
764 || !ec_key_otherparams_fromdata(ec
, params
)
765 || !ec_key_fromdata(ec
, params
, 1)
766 || !EVP_PKEY_assign_EC_KEY(pkey
, ec
)) {
773 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth
= {
778 "OpenSSL EC algorithm",
795 ec_missing_parameters
,
809 ec_pkey_public_check
,
812 0, /* set_priv_key */
814 0, /* get_priv_key */
820 eckey_priv_decode_with_libctx
823 #if !defined(OPENSSL_NO_SM2)
824 const EVP_PKEY_ASN1_METHOD sm2_asn1_meth
= {
831 int EC_KEY_print(BIO
*bp
, const EC_KEY
*x
, int off
)
833 int private = EC_KEY_get0_private_key(x
) != NULL
;
835 return do_EC_KEY_print(bp
, x
, off
,
836 private ? EC_KEY_PRINT_PRIVATE
: EC_KEY_PRINT_PUBLIC
);
839 int ECParameters_print(BIO
*bp
, const EC_KEY
*x
)
841 return do_EC_KEY_print(bp
, x
, 4, EC_KEY_PRINT_PARAM
);
844 #ifndef OPENSSL_NO_CMS
846 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX
*pctx
,
847 X509_ALGOR
*alg
, ASN1_BIT_STRING
*pubkey
)
849 const ASN1_OBJECT
*aoid
;
853 EVP_PKEY
*pkpeer
= NULL
;
854 EC_KEY
*ecpeer
= NULL
;
855 const unsigned char *p
;
858 X509_ALGOR_get0(&aoid
, &atype
, &aval
, alg
);
859 if (OBJ_obj2nid(aoid
) != NID_X9_62_id_ecPublicKey
)
861 /* If absent parameters get group from main key */
862 if (atype
== V_ASN1_UNDEF
|| atype
== V_ASN1_NULL
) {
865 pk
= EVP_PKEY_CTX_get0_pkey(pctx
);
868 grp
= EC_KEY_get0_group(pk
->pkey
.ec
);
869 ecpeer
= EC_KEY_new();
872 if (!EC_KEY_set_group(ecpeer
, grp
))
875 ecpeer
= eckey_type2param(atype
, aval
, pctx
->libctx
, pctx
->propquery
);
879 /* We have parameters now set public key */
880 plen
= ASN1_STRING_length(pubkey
);
881 p
= ASN1_STRING_get0_data(pubkey
);
882 if (p
== NULL
|| plen
== 0)
884 if (!o2i_ECPublicKey(&ecpeer
, &p
, plen
))
886 pkpeer
= EVP_PKEY_new();
889 EVP_PKEY_set1_EC_KEY(pkpeer
, ecpeer
);
890 if (EVP_PKEY_derive_set_peer(pctx
, pkpeer
) > 0)
894 EVP_PKEY_free(pkpeer
);
898 /* Set KDF parameters based on KDF NID */
899 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX
*pctx
, int eckdf_nid
)
901 int kdf_nid
, kdfmd_nid
, cofactor
;
902 const EVP_MD
*kdf_md
;
903 if (eckdf_nid
== NID_undef
)
906 /* Lookup KDF type, cofactor mode and digest */
907 if (!OBJ_find_sigid_algs(eckdf_nid
, &kdfmd_nid
, &kdf_nid
))
910 if (kdf_nid
== NID_dh_std_kdf
)
912 else if (kdf_nid
== NID_dh_cofactor_kdf
)
917 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx
, cofactor
) <= 0)
920 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, EVP_PKEY_ECDH_KDF_X9_63
) <= 0)
923 kdf_md
= EVP_get_digestbynid(kdfmd_nid
);
927 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
932 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX
*pctx
, CMS_RecipientInfo
*ri
)
936 X509_ALGOR
*alg
, *kekalg
= NULL
;
937 ASN1_OCTET_STRING
*ukm
;
938 const unsigned char *p
;
939 unsigned char *der
= NULL
;
941 EVP_CIPHER
*kekcipher
= NULL
;
942 EVP_CIPHER_CTX
*kekctx
;
945 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &alg
, &ukm
))
948 if (!ecdh_cms_set_kdf_param(pctx
, OBJ_obj2nid(alg
->algorithm
))) {
949 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO
, EC_R_KDF_PARAMETER_ERROR
);
953 if (alg
->parameter
->type
!= V_ASN1_SEQUENCE
)
956 p
= alg
->parameter
->value
.sequence
->data
;
957 plen
= alg
->parameter
->value
.sequence
->length
;
958 kekalg
= d2i_X509_ALGOR(NULL
, &p
, plen
);
961 kekctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
964 name
= OBJ_nid2sn(OBJ_obj2nid(kekalg
->algorithm
));
965 kekcipher
= EVP_CIPHER_fetch(pctx
->libctx
, name
, pctx
->propquery
);
966 if (kekcipher
== NULL
|| EVP_CIPHER_mode(kekcipher
) != EVP_CIPH_WRAP_MODE
)
968 if (!EVP_EncryptInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
))
970 if (EVP_CIPHER_asn1_to_param(kekctx
, kekalg
->parameter
) <= 0)
973 keylen
= EVP_CIPHER_CTX_key_length(kekctx
);
974 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
977 plen
= CMS_SharedInfo_encode(&der
, kekalg
, ukm
, keylen
);
982 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, der
, plen
) <= 0)
988 EVP_CIPHER_free(kekcipher
);
989 X509_ALGOR_free(kekalg
);
994 static int ecdh_cms_decrypt(CMS_RecipientInfo
*ri
)
998 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
1001 /* See if we need to set peer key */
1002 if (!EVP_PKEY_CTX_get0_peerkey(pctx
)) {
1004 ASN1_BIT_STRING
*pubkey
;
1006 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &alg
, &pubkey
,
1009 if (!alg
|| !pubkey
)
1011 if (!ecdh_cms_set_peerkey(pctx
, alg
, pubkey
)) {
1012 ECerr(EC_F_ECDH_CMS_DECRYPT
, EC_R_PEER_KEY_ERROR
);
1016 /* Set ECDH derivation parameters and initialise unwrap context */
1017 if (!ecdh_cms_set_shared_info(pctx
, ri
)) {
1018 ECerr(EC_F_ECDH_CMS_DECRYPT
, EC_R_SHARED_INFO_ERROR
);
1024 static int ecdh_cms_encrypt(CMS_RecipientInfo
*ri
)
1028 EVP_CIPHER_CTX
*ctx
;
1030 X509_ALGOR
*talg
, *wrap_alg
= NULL
;
1031 const ASN1_OBJECT
*aoid
;
1032 ASN1_BIT_STRING
*pubkey
;
1033 ASN1_STRING
*wrap_str
;
1034 ASN1_OCTET_STRING
*ukm
;
1035 unsigned char *penc
= NULL
;
1038 int ecdh_nid
, kdf_type
, kdf_nid
, wrap_nid
;
1039 const EVP_MD
*kdf_md
;
1041 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
1044 /* Get ephemeral key */
1045 pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
1046 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &talg
, &pubkey
,
1049 X509_ALGOR_get0(&aoid
, NULL
, NULL
, talg
);
1050 /* Is everything uninitialised? */
1051 if (aoid
== OBJ_nid2obj(NID_undef
)) {
1053 EC_KEY
*eckey
= pkey
->pkey
.ec
;
1057 penclen
= i2o_ECPublicKey(eckey
, NULL
);
1060 penc
= OPENSSL_malloc(penclen
);
1064 penclen
= i2o_ECPublicKey(eckey
, &p
);
1067 ASN1_STRING_set0(pubkey
, penc
, penclen
);
1068 pubkey
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
1069 pubkey
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
1072 X509_ALGOR_set0(talg
, OBJ_nid2obj(NID_X9_62_id_ecPublicKey
),
1073 V_ASN1_UNDEF
, NULL
);
1076 /* See if custom parameters set */
1077 kdf_type
= EVP_PKEY_CTX_get_ecdh_kdf_type(pctx
);
1080 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx
, &kdf_md
))
1082 ecdh_nid
= EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx
);
1085 else if (ecdh_nid
== 0)
1086 ecdh_nid
= NID_dh_std_kdf
;
1087 else if (ecdh_nid
== 1)
1088 ecdh_nid
= NID_dh_cofactor_kdf
;
1090 if (kdf_type
== EVP_PKEY_ECDH_KDF_NONE
) {
1091 kdf_type
= EVP_PKEY_ECDH_KDF_X9_63
;
1092 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, kdf_type
) <= 0)
1097 if (kdf_md
== NULL
) {
1098 /* Fixme later for better MD */
1099 kdf_md
= EVP_sha1();
1100 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
1104 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &talg
, &ukm
))
1107 /* Lookup NID for KDF+cofactor+digest */
1109 if (!OBJ_find_sigid_by_algs(&kdf_nid
, EVP_MD_type(kdf_md
), ecdh_nid
))
1112 ctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
1113 wrap_nid
= EVP_CIPHER_CTX_type(ctx
);
1114 keylen
= EVP_CIPHER_CTX_key_length(ctx
);
1116 /* Package wrap algorithm in an AlgorithmIdentifier */
1118 wrap_alg
= X509_ALGOR_new();
1119 if (wrap_alg
== NULL
)
1121 wrap_alg
->algorithm
= OBJ_nid2obj(wrap_nid
);
1122 wrap_alg
->parameter
= ASN1_TYPE_new();
1123 if (wrap_alg
->parameter
== NULL
)
1125 if (EVP_CIPHER_param_to_asn1(ctx
, wrap_alg
->parameter
) <= 0)
1127 if (ASN1_TYPE_get(wrap_alg
->parameter
) == NID_undef
) {
1128 ASN1_TYPE_free(wrap_alg
->parameter
);
1129 wrap_alg
->parameter
= NULL
;
1132 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
1135 penclen
= CMS_SharedInfo_encode(&penc
, wrap_alg
, ukm
, keylen
);
1140 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, penc
, penclen
) <= 0)
1145 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
1146 * of another AlgorithmIdentifier.
1148 penclen
= i2d_X509_ALGOR(wrap_alg
, &penc
);
1149 if (!penc
|| !penclen
)
1151 wrap_str
= ASN1_STRING_new();
1152 if (wrap_str
== NULL
)
1154 ASN1_STRING_set0(wrap_str
, penc
, penclen
);
1156 X509_ALGOR_set0(talg
, OBJ_nid2obj(kdf_nid
), V_ASN1_SEQUENCE
, wrap_str
);
1162 X509_ALGOR_free(wrap_alg
);