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 ecparams_to_params(const EC_KEY
*eckey
, OSSL_PARAM_BLD
*tmpl
)
618 ecg
= EC_KEY_get0_group(eckey
);
622 curve_nid
= EC_GROUP_get_curve_name(ecg
);
624 if (curve_nid
== NID_undef
) {
625 /* explicit parameters */
628 * TODO(3.0): should we support explicit parameters curves?
633 const char *curve_name
= NULL
;
635 if ((curve_name
= OBJ_nid2sn(curve_nid
)) == NULL
)
638 if (!OSSL_PARAM_BLD_push_utf8_string(tmpl
, OSSL_PKEY_PARAM_GROUP_NAME
,
647 int ec_pkey_export_to(const EVP_PKEY
*from
, void *to_keydata
,
648 EVP_KEYMGMT
*to_keymgmt
, OPENSSL_CTX
*libctx
,
651 const EC_KEY
*eckey
= NULL
;
652 const EC_GROUP
*ecg
= NULL
;
653 unsigned char *pub_key_buf
= NULL
;
654 size_t pub_key_buflen
;
655 OSSL_PARAM_BLD
*tmpl
;
656 OSSL_PARAM
*params
= NULL
;
657 const BIGNUM
*priv_key
= NULL
;
658 const EC_POINT
*pub_point
= NULL
;
661 BN_CTX
*bnctx
= NULL
;
664 || (eckey
= from
->pkey
.ec
) == NULL
665 || (ecg
= EC_KEY_get0_group(eckey
)) == NULL
)
669 * If the EC_KEY method is foreign, then we can't be sure of anything,
670 * and can therefore not export or pretend to export.
672 if (EC_KEY_get_method(eckey
) != EC_KEY_OpenSSL())
675 tmpl
= OSSL_PARAM_BLD_new();
679 /* export the domain parameters */
680 if (!ecparams_to_params(eckey
, tmpl
))
682 selection
|= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
;
684 priv_key
= EC_KEY_get0_private_key(eckey
);
685 pub_point
= EC_KEY_get0_public_key(eckey
);
687 if (pub_point
!= NULL
) {
689 * EC_POINT_point2buf() can generate random numbers in some
690 * implementations so we need to ensure we use the correct libctx.
692 bnctx
= BN_CTX_new_ex(libctx
);
696 /* convert pub_point to a octet string according to the SECG standard */
697 if ((pub_key_buflen
= EC_POINT_point2buf(ecg
, pub_point
,
698 POINT_CONVERSION_COMPRESSED
,
699 &pub_key_buf
, bnctx
)) == 0
700 || !OSSL_PARAM_BLD_push_octet_string(tmpl
,
701 OSSL_PKEY_PARAM_PUB_KEY
,
705 selection
|= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
708 if (priv_key
!= NULL
) {
711 int ecdh_cofactor_mode
;
714 * Key import/export should never leak the bit length of the secret
717 * For this reason, on export we use padded BIGNUMs with fixed length.
719 * When importing we also should make sure that, even if short lived,
720 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
721 * soon as possible, so that any processing of this BIGNUM might opt for
722 * constant time implementations in the backend.
724 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
725 * to preallocate the BIGNUM internal buffer to a fixed public size big
726 * enough that operations performed during the processing never trigger
727 * a realloc which would leak the size of the scalar through memory
733 * The order of the large prime subgroup of the curve is our choice for
734 * a fixed public size, as that is generally the upper bound for
735 * generating a private key in EC cryptosystems and should fit all valid
738 * For padding on export we just use the bit length of the order
739 * converted to bytes (rounding up).
741 * For preallocating the BIGNUM storage we look at the number of "words"
742 * required for the internal representation of the order, and we
743 * preallocate 2 extra "words" in case any of the subsequent processing
744 * might temporarily overflow the order length.
746 ecbits
= EC_GROUP_order_bits(ecg
);
750 sz
= (ecbits
+ 7 ) / 8;
751 if (!OSSL_PARAM_BLD_push_BN_pad(tmpl
,
752 OSSL_PKEY_PARAM_PRIV_KEY
,
755 selection
|= OSSL_KEYMGMT_SELECT_PRIVATE_KEY
;
758 * The ECDH Cofactor Mode is defined only if the EC_KEY actually
759 * contains a private key, so we check for the flag and export it only
763 (EC_KEY_get_flags(eckey
) & EC_FLAG_COFACTOR_ECDH
) ? 1 : 0;
765 /* Export the ECDH_COFACTOR_MODE parameter */
766 if (!OSSL_PARAM_BLD_push_int(tmpl
,
767 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
,
770 selection
|= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
;
773 params
= OSSL_PARAM_BLD_to_param(tmpl
);
775 /* We export, the provider imports */
776 rv
= evp_keymgmt_import(to_keymgmt
, to_keydata
, selection
, params
);
779 OSSL_PARAM_BLD_free(tmpl
);
780 OSSL_PARAM_BLD_free_params(params
);
781 OPENSSL_free(pub_key_buf
);
786 static int ec_pkey_import_from(const OSSL_PARAM params
[], void *vpctx
)
788 EVP_PKEY_CTX
*pctx
= vpctx
;
789 EVP_PKEY
*pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
790 EC_KEY
*ec
= EC_KEY_new_with_libctx(pctx
->libctx
, pctx
->propquery
);
793 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
797 if (!ec_key_domparams_fromdata(ec
, params
)
798 || !ec_key_otherparams_fromdata(ec
, params
)
799 || !ec_key_fromdata(ec
, params
, 1)
800 || !EVP_PKEY_assign_EC_KEY(pkey
, ec
)) {
807 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth
= {
812 "OpenSSL EC algorithm",
829 ec_missing_parameters
,
843 ec_pkey_public_check
,
846 0, /* set_priv_key */
848 0, /* get_priv_key */
854 eckey_priv_decode_with_libctx
857 #if !defined(OPENSSL_NO_SM2)
858 const EVP_PKEY_ASN1_METHOD sm2_asn1_meth
= {
865 int EC_KEY_print(BIO
*bp
, const EC_KEY
*x
, int off
)
867 int private = EC_KEY_get0_private_key(x
) != NULL
;
869 return do_EC_KEY_print(bp
, x
, off
,
870 private ? EC_KEY_PRINT_PRIVATE
: EC_KEY_PRINT_PUBLIC
);
873 int ECParameters_print(BIO
*bp
, const EC_KEY
*x
)
875 return do_EC_KEY_print(bp
, x
, 4, EC_KEY_PRINT_PARAM
);
878 #ifndef OPENSSL_NO_CMS
880 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX
*pctx
,
881 X509_ALGOR
*alg
, ASN1_BIT_STRING
*pubkey
)
883 const ASN1_OBJECT
*aoid
;
887 EVP_PKEY
*pkpeer
= NULL
;
888 EC_KEY
*ecpeer
= NULL
;
889 const unsigned char *p
;
892 X509_ALGOR_get0(&aoid
, &atype
, &aval
, alg
);
893 if (OBJ_obj2nid(aoid
) != NID_X9_62_id_ecPublicKey
)
895 /* If absent parameters get group from main key */
896 if (atype
== V_ASN1_UNDEF
|| atype
== V_ASN1_NULL
) {
899 pk
= EVP_PKEY_CTX_get0_pkey(pctx
);
902 grp
= EC_KEY_get0_group(pk
->pkey
.ec
);
903 ecpeer
= EC_KEY_new();
906 if (!EC_KEY_set_group(ecpeer
, grp
))
909 ecpeer
= eckey_type2param(atype
, aval
, pctx
->libctx
, pctx
->propquery
);
913 /* We have parameters now set public key */
914 plen
= ASN1_STRING_length(pubkey
);
915 p
= ASN1_STRING_get0_data(pubkey
);
916 if (p
== NULL
|| plen
== 0)
918 if (!o2i_ECPublicKey(&ecpeer
, &p
, plen
))
920 pkpeer
= EVP_PKEY_new();
923 EVP_PKEY_set1_EC_KEY(pkpeer
, ecpeer
);
924 if (EVP_PKEY_derive_set_peer(pctx
, pkpeer
) > 0)
928 EVP_PKEY_free(pkpeer
);
932 /* Set KDF parameters based on KDF NID */
933 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX
*pctx
, int eckdf_nid
)
935 int kdf_nid
, kdfmd_nid
, cofactor
;
936 const EVP_MD
*kdf_md
;
937 if (eckdf_nid
== NID_undef
)
940 /* Lookup KDF type, cofactor mode and digest */
941 if (!OBJ_find_sigid_algs(eckdf_nid
, &kdfmd_nid
, &kdf_nid
))
944 if (kdf_nid
== NID_dh_std_kdf
)
946 else if (kdf_nid
== NID_dh_cofactor_kdf
)
951 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx
, cofactor
) <= 0)
954 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, EVP_PKEY_ECDH_KDF_X9_63
) <= 0)
957 kdf_md
= EVP_get_digestbynid(kdfmd_nid
);
961 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
966 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX
*pctx
, CMS_RecipientInfo
*ri
)
970 X509_ALGOR
*alg
, *kekalg
= NULL
;
971 ASN1_OCTET_STRING
*ukm
;
972 const unsigned char *p
;
973 unsigned char *der
= NULL
;
975 EVP_CIPHER
*kekcipher
= NULL
;
976 EVP_CIPHER_CTX
*kekctx
;
979 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &alg
, &ukm
))
982 if (!ecdh_cms_set_kdf_param(pctx
, OBJ_obj2nid(alg
->algorithm
))) {
983 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO
, EC_R_KDF_PARAMETER_ERROR
);
987 if (alg
->parameter
->type
!= V_ASN1_SEQUENCE
)
990 p
= alg
->parameter
->value
.sequence
->data
;
991 plen
= alg
->parameter
->value
.sequence
->length
;
992 kekalg
= d2i_X509_ALGOR(NULL
, &p
, plen
);
995 kekctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
998 name
= OBJ_nid2sn(OBJ_obj2nid(kekalg
->algorithm
));
999 kekcipher
= EVP_CIPHER_fetch(pctx
->libctx
, name
, NULL
);
1000 if (kekcipher
== NULL
|| EVP_CIPHER_mode(kekcipher
) != EVP_CIPH_WRAP_MODE
)
1002 if (!EVP_EncryptInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
))
1004 if (EVP_CIPHER_asn1_to_param(kekctx
, kekalg
->parameter
) <= 0)
1007 keylen
= EVP_CIPHER_CTX_key_length(kekctx
);
1008 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
1011 plen
= CMS_SharedInfo_encode(&der
, kekalg
, ukm
, keylen
);
1016 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, der
, plen
) <= 0)
1022 EVP_CIPHER_free(kekcipher
);
1023 X509_ALGOR_free(kekalg
);
1028 static int ecdh_cms_decrypt(CMS_RecipientInfo
*ri
)
1032 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
1035 /* See if we need to set peer key */
1036 if (!EVP_PKEY_CTX_get0_peerkey(pctx
)) {
1038 ASN1_BIT_STRING
*pubkey
;
1040 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &alg
, &pubkey
,
1043 if (!alg
|| !pubkey
)
1045 if (!ecdh_cms_set_peerkey(pctx
, alg
, pubkey
)) {
1046 ECerr(EC_F_ECDH_CMS_DECRYPT
, EC_R_PEER_KEY_ERROR
);
1050 /* Set ECDH derivation parameters and initialise unwrap context */
1051 if (!ecdh_cms_set_shared_info(pctx
, ri
)) {
1052 ECerr(EC_F_ECDH_CMS_DECRYPT
, EC_R_SHARED_INFO_ERROR
);
1058 static int ecdh_cms_encrypt(CMS_RecipientInfo
*ri
)
1062 EVP_CIPHER_CTX
*ctx
;
1064 X509_ALGOR
*talg
, *wrap_alg
= NULL
;
1065 const ASN1_OBJECT
*aoid
;
1066 ASN1_BIT_STRING
*pubkey
;
1067 ASN1_STRING
*wrap_str
;
1068 ASN1_OCTET_STRING
*ukm
;
1069 unsigned char *penc
= NULL
;
1072 int ecdh_nid
, kdf_type
, kdf_nid
, wrap_nid
;
1073 const EVP_MD
*kdf_md
;
1075 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
1078 /* Get ephemeral key */
1079 pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
1080 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &talg
, &pubkey
,
1083 X509_ALGOR_get0(&aoid
, NULL
, NULL
, talg
);
1084 /* Is everything uninitialised? */
1085 if (aoid
== OBJ_nid2obj(NID_undef
)) {
1087 EC_KEY
*eckey
= pkey
->pkey
.ec
;
1091 penclen
= i2o_ECPublicKey(eckey
, NULL
);
1094 penc
= OPENSSL_malloc(penclen
);
1098 penclen
= i2o_ECPublicKey(eckey
, &p
);
1101 ASN1_STRING_set0(pubkey
, penc
, penclen
);
1102 pubkey
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
1103 pubkey
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
1106 X509_ALGOR_set0(talg
, OBJ_nid2obj(NID_X9_62_id_ecPublicKey
),
1107 V_ASN1_UNDEF
, NULL
);
1110 /* See if custom parameters set */
1111 kdf_type
= EVP_PKEY_CTX_get_ecdh_kdf_type(pctx
);
1114 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx
, &kdf_md
))
1116 ecdh_nid
= EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx
);
1119 else if (ecdh_nid
== 0)
1120 ecdh_nid
= NID_dh_std_kdf
;
1121 else if (ecdh_nid
== 1)
1122 ecdh_nid
= NID_dh_cofactor_kdf
;
1124 if (kdf_type
== EVP_PKEY_ECDH_KDF_NONE
) {
1125 kdf_type
= EVP_PKEY_ECDH_KDF_X9_63
;
1126 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, kdf_type
) <= 0)
1131 if (kdf_md
== NULL
) {
1132 /* Fixme later for better MD */
1133 kdf_md
= EVP_sha1();
1134 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
1138 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &talg
, &ukm
))
1141 /* Lookup NID for KDF+cofactor+digest */
1143 if (!OBJ_find_sigid_by_algs(&kdf_nid
, EVP_MD_type(kdf_md
), ecdh_nid
))
1146 ctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
1147 wrap_nid
= EVP_CIPHER_CTX_type(ctx
);
1148 keylen
= EVP_CIPHER_CTX_key_length(ctx
);
1150 /* Package wrap algorithm in an AlgorithmIdentifier */
1152 wrap_alg
= X509_ALGOR_new();
1153 if (wrap_alg
== NULL
)
1155 wrap_alg
->algorithm
= OBJ_nid2obj(wrap_nid
);
1156 wrap_alg
->parameter
= ASN1_TYPE_new();
1157 if (wrap_alg
->parameter
== NULL
)
1159 if (EVP_CIPHER_param_to_asn1(ctx
, wrap_alg
->parameter
) <= 0)
1161 if (ASN1_TYPE_get(wrap_alg
->parameter
) == NID_undef
) {
1162 ASN1_TYPE_free(wrap_alg
->parameter
);
1163 wrap_alg
->parameter
= NULL
;
1166 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
1169 penclen
= CMS_SharedInfo_encode(&penc
, wrap_alg
, ukm
, keylen
);
1174 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, penc
, penclen
) <= 0)
1179 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
1180 * of another AlgorithmIdentifier.
1182 penclen
= i2d_X509_ALGOR(wrap_alg
, &penc
);
1183 if (!penc
|| !penclen
)
1185 wrap_str
= ASN1_STRING_new();
1186 if (wrap_str
== NULL
)
1188 ASN1_STRING_set0(wrap_str
, penc
, penclen
);
1190 X509_ALGOR_set0(talg
, OBJ_nid2obj(kdf_nid
), V_ASN1_SEQUENCE
, wrap_str
);
1196 X509_ALGOR_free(wrap_alg
);