2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 #include "internal/cryptlib.h"
12 #include <openssl/x509.h>
13 #include <openssl/ec.h>
14 #include <openssl/bn.h>
15 #include <openssl/cms.h>
16 #include <openssl/asn1t.h>
17 #include "internal/asn1_int.h"
18 #include "internal/evp_int.h"
21 #ifndef OPENSSL_NO_CMS
22 static int ecdh_cms_decrypt(CMS_RecipientInfo
*ri
);
23 static int ecdh_cms_encrypt(CMS_RecipientInfo
*ri
);
26 static int eckey_param2type(int *pptype
, void **ppval
, EC_KEY
*ec_key
)
28 const EC_GROUP
*group
;
30 if (ec_key
== NULL
|| (group
= EC_KEY_get0_group(ec_key
)) == NULL
) {
31 ECerr(EC_F_ECKEY_PARAM2TYPE
, EC_R_MISSING_PARAMETERS
);
34 if (EC_GROUP_get_asn1_flag(group
)
35 && (nid
= EC_GROUP_get_curve_name(group
)))
36 /* we have a 'named curve' => just set the OID */
38 *ppval
= OBJ_nid2obj(nid
);
39 *pptype
= V_ASN1_OBJECT
;
40 } else { /* explicit parameters */
42 ASN1_STRING
*pstr
= NULL
;
43 pstr
= ASN1_STRING_new();
46 pstr
->length
= i2d_ECParameters(ec_key
, &pstr
->data
);
47 if (pstr
->length
<= 0) {
48 ASN1_STRING_free(pstr
);
49 ECerr(EC_F_ECKEY_PARAM2TYPE
, ERR_R_EC_LIB
);
53 *pptype
= V_ASN1_SEQUENCE
;
58 static int eckey_pub_encode(X509_PUBKEY
*pk
, const EVP_PKEY
*pkey
)
60 EC_KEY
*ec_key
= pkey
->pkey
.ec
;
63 unsigned char *penc
= NULL
, *p
;
66 if (!eckey_param2type(&ptype
, &pval
, ec_key
)) {
67 ECerr(EC_F_ECKEY_PUB_ENCODE
, ERR_R_EC_LIB
);
70 penclen
= i2o_ECPublicKey(ec_key
, NULL
);
73 penc
= OPENSSL_malloc(penclen
);
77 penclen
= i2o_ECPublicKey(ec_key
, &p
);
80 if (X509_PUBKEY_set0_param(pk
, OBJ_nid2obj(EVP_PKEY_EC
),
81 ptype
, pval
, penc
, penclen
))
84 if (ptype
== V_ASN1_OBJECT
)
85 ASN1_OBJECT_free(pval
);
87 ASN1_STRING_free(pval
);
92 static EC_KEY
*eckey_type2param(int ptype
, const void *pval
)
95 if (ptype
== V_ASN1_SEQUENCE
) {
96 const ASN1_STRING
*pstr
= pval
;
97 const unsigned char *pm
= NULL
;
100 pmlen
= pstr
->length
;
101 if ((eckey
= d2i_ECParameters(NULL
, &pm
, pmlen
)) == NULL
) {
102 ECerr(EC_F_ECKEY_TYPE2PARAM
, EC_R_DECODE_ERROR
);
105 } else if (ptype
== V_ASN1_OBJECT
) {
106 const ASN1_OBJECT
*poid
= pval
;
110 * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
112 if ((eckey
= EC_KEY_new()) == NULL
) {
113 ECerr(EC_F_ECKEY_TYPE2PARAM
, ERR_R_MALLOC_FAILURE
);
116 group
= EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid
));
119 EC_GROUP_set_asn1_flag(group
, OPENSSL_EC_NAMED_CURVE
);
120 if (EC_KEY_set_group(eckey
, group
) == 0)
122 EC_GROUP_free(group
);
124 ECerr(EC_F_ECKEY_TYPE2PARAM
, EC_R_DECODE_ERROR
);
135 static int eckey_pub_decode(EVP_PKEY
*pkey
, X509_PUBKEY
*pubkey
)
137 const unsigned char *p
= NULL
;
140 EC_KEY
*eckey
= NULL
;
143 if (!X509_PUBKEY_get0_param(NULL
, &p
, &pklen
, &palg
, pubkey
))
145 X509_ALGOR_get0(NULL
, &ptype
, &pval
, palg
);
147 eckey
= eckey_type2param(ptype
, pval
);
150 ECerr(EC_F_ECKEY_PUB_DECODE
, ERR_R_EC_LIB
);
154 /* We have parameters now set public key */
155 if (!o2i_ECPublicKey(&eckey
, &p
, pklen
)) {
156 ECerr(EC_F_ECKEY_PUB_DECODE
, EC_R_DECODE_ERROR
);
160 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
168 static int eckey_pub_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
171 const EC_GROUP
*group
= EC_KEY_get0_group(b
->pkey
.ec
);
172 const EC_POINT
*pa
= EC_KEY_get0_public_key(a
->pkey
.ec
),
173 *pb
= EC_KEY_get0_public_key(b
->pkey
.ec
);
174 if (group
== NULL
|| pa
== NULL
|| pb
== NULL
)
176 r
= EC_POINT_cmp(group
, pa
, pb
, NULL
);
184 static int eckey_priv_decode(EVP_PKEY
*pkey
, const PKCS8_PRIV_KEY_INFO
*p8
)
186 const unsigned char *p
= NULL
;
189 EC_KEY
*eckey
= NULL
;
190 const X509_ALGOR
*palg
;
192 if (!PKCS8_pkey_get0(NULL
, &p
, &pklen
, &palg
, p8
))
194 X509_ALGOR_get0(NULL
, &ptype
, &pval
, palg
);
196 eckey
= eckey_type2param(ptype
, pval
);
201 /* We have parameters now set private key */
202 if (!d2i_ECPrivateKey(&eckey
, &p
, pklen
)) {
203 ECerr(EC_F_ECKEY_PRIV_DECODE
, EC_R_DECODE_ERROR
);
207 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
211 ECerr(EC_F_ECKEY_PRIV_DECODE
, ERR_R_EC_LIB
);
217 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO
*p8
, const EVP_PKEY
*pkey
)
219 EC_KEY ec_key
= *(pkey
->pkey
.ec
);
220 unsigned char *ep
, *p
;
223 unsigned int old_flags
;
225 if (!eckey_param2type(&ptype
, &pval
, &ec_key
)) {
226 ECerr(EC_F_ECKEY_PRIV_ENCODE
, EC_R_DECODE_ERROR
);
230 /* set the private key */
233 * do not include the parameters in the SEC1 private key see PKCS#11
236 old_flags
= EC_KEY_get_enc_flags(&ec_key
);
237 EC_KEY_set_enc_flags(&ec_key
, old_flags
| EC_PKEY_NO_PARAMETERS
);
239 eplen
= i2d_ECPrivateKey(&ec_key
, NULL
);
241 ECerr(EC_F_ECKEY_PRIV_ENCODE
, ERR_R_EC_LIB
);
244 ep
= OPENSSL_malloc(eplen
);
246 ECerr(EC_F_ECKEY_PRIV_ENCODE
, ERR_R_MALLOC_FAILURE
);
250 if (!i2d_ECPrivateKey(&ec_key
, &p
)) {
252 ECerr(EC_F_ECKEY_PRIV_ENCODE
, ERR_R_EC_LIB
);
256 if (!PKCS8_pkey_set0(p8
, OBJ_nid2obj(NID_X9_62_id_ecPublicKey
), 0,
257 ptype
, pval
, ep
, eplen
)) {
265 static int int_ec_size(const EVP_PKEY
*pkey
)
267 return ECDSA_size(pkey
->pkey
.ec
);
270 static int ec_bits(const EVP_PKEY
*pkey
)
272 return EC_GROUP_order_bits(EC_KEY_get0_group(pkey
->pkey
.ec
));
275 static int ec_security_bits(const EVP_PKEY
*pkey
)
277 int ecbits
= ec_bits(pkey
);
291 static int ec_missing_parameters(const EVP_PKEY
*pkey
)
293 if (pkey
->pkey
.ec
== NULL
|| EC_KEY_get0_group(pkey
->pkey
.ec
) == NULL
)
298 static int ec_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
300 EC_GROUP
*group
= EC_GROUP_dup(EC_KEY_get0_group(from
->pkey
.ec
));
304 if (to
->pkey
.ec
== NULL
) {
305 to
->pkey
.ec
= EC_KEY_new();
306 if (to
->pkey
.ec
== NULL
)
309 if (EC_KEY_set_group(to
->pkey
.ec
, group
) == 0)
311 EC_GROUP_free(group
);
314 EC_GROUP_free(group
);
318 static int ec_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
320 const EC_GROUP
*group_a
= EC_KEY_get0_group(a
->pkey
.ec
),
321 *group_b
= EC_KEY_get0_group(b
->pkey
.ec
);
322 if (group_a
== NULL
|| group_b
== NULL
)
324 if (EC_GROUP_cmp(group_a
, group_b
, NULL
))
330 static void int_ec_free(EVP_PKEY
*pkey
)
332 EC_KEY_free(pkey
->pkey
.ec
);
336 EC_KEY_PRINT_PRIVATE
,
341 static int do_EC_KEY_print(BIO
*bp
, const EC_KEY
*x
, int off
, ec_print_t ktype
)
344 unsigned char *priv
= NULL
, *pub
= NULL
;
345 size_t privlen
= 0, publen
= 0;
347 const EC_GROUP
*group
;
349 if (x
== NULL
|| (group
= EC_KEY_get0_group(x
)) == NULL
) {
350 ECerr(EC_F_DO_EC_KEY_PRINT
, ERR_R_PASSED_NULL_PARAMETER
);
354 if (ktype
!= EC_KEY_PRINT_PARAM
&& EC_KEY_get0_public_key(x
) != NULL
) {
355 publen
= EC_KEY_key2buf(x
, EC_KEY_get_conv_form(x
), &pub
, NULL
);
360 if (ktype
== EC_KEY_PRINT_PRIVATE
&& EC_KEY_get0_private_key(x
) != NULL
) {
361 privlen
= EC_KEY_priv2buf(x
, &priv
);
366 if (ktype
== EC_KEY_PRINT_PRIVATE
)
367 ecstr
= "Private-Key";
368 else if (ktype
== EC_KEY_PRINT_PUBLIC
)
369 ecstr
= "Public-Key";
371 ecstr
= "ECDSA-Parameters";
373 if (!BIO_indent(bp
, off
, 128))
375 if (BIO_printf(bp
, "%s: (%d bit)\n", ecstr
,
376 EC_GROUP_order_bits(group
)) <= 0)
380 if (BIO_printf(bp
, "%*spriv:\n", off
, "") <= 0)
382 if (ASN1_buf_print(bp
, priv
, privlen
, off
+ 4) == 0)
387 if (BIO_printf(bp
, "%*spub:\n", off
, "") <= 0)
389 if (ASN1_buf_print(bp
, pub
, publen
, off
+ 4) == 0)
393 if (!ECPKParameters_print(bp
, group
, off
))
398 ECerr(EC_F_DO_EC_KEY_PRINT
, ERR_R_EC_LIB
);
399 OPENSSL_clear_free(priv
, privlen
);
404 static int eckey_param_decode(EVP_PKEY
*pkey
,
405 const unsigned char **pder
, int derlen
)
409 if ((eckey
= d2i_ECParameters(NULL
, pder
, derlen
)) == NULL
) {
410 ECerr(EC_F_ECKEY_PARAM_DECODE
, ERR_R_EC_LIB
);
413 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
417 static int eckey_param_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
419 return i2d_ECParameters(pkey
->pkey
.ec
, pder
);
422 static int eckey_param_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
425 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, EC_KEY_PRINT_PARAM
);
428 static int eckey_pub_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
431 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, EC_KEY_PRINT_PUBLIC
);
434 static int eckey_priv_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
437 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, EC_KEY_PRINT_PRIVATE
);
440 static int old_ec_priv_decode(EVP_PKEY
*pkey
,
441 const unsigned char **pder
, int derlen
)
445 if ((ec
= d2i_ECPrivateKey(NULL
, pder
, derlen
)) == NULL
) {
446 ECerr(EC_F_OLD_EC_PRIV_DECODE
, EC_R_DECODE_ERROR
);
449 EVP_PKEY_assign_EC_KEY(pkey
, ec
);
453 static int old_ec_priv_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
455 return i2d_ECPrivateKey(pkey
->pkey
.ec
, pder
);
458 static int ec_pkey_ctrl(EVP_PKEY
*pkey
, int op
, long arg1
, void *arg2
)
461 case ASN1_PKEY_CTRL_PKCS7_SIGN
:
464 X509_ALGOR
*alg1
, *alg2
;
465 PKCS7_SIGNER_INFO_get0_algs(arg2
, NULL
, &alg1
, &alg2
);
466 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
468 hnid
= OBJ_obj2nid(alg1
->algorithm
);
469 if (hnid
== NID_undef
)
471 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
473 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
476 #ifndef OPENSSL_NO_CMS
477 case ASN1_PKEY_CTRL_CMS_SIGN
:
480 X509_ALGOR
*alg1
, *alg2
;
481 CMS_SignerInfo_get0_algs(arg2
, NULL
, NULL
, &alg1
, &alg2
);
482 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
484 hnid
= OBJ_obj2nid(alg1
->algorithm
);
485 if (hnid
== NID_undef
)
487 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
489 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
493 case ASN1_PKEY_CTRL_CMS_ENVELOPE
:
495 return ecdh_cms_decrypt(arg2
);
497 return ecdh_cms_encrypt(arg2
);
500 case ASN1_PKEY_CTRL_CMS_RI_TYPE
:
501 *(int *)arg2
= CMS_RECIPINFO_AGREE
;
505 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
506 *(int *)arg2
= NID_sha256
;
509 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT
:
510 return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey
), arg2
, arg1
, NULL
);
512 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT
:
513 return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey
),
514 POINT_CONVERSION_UNCOMPRESSED
, arg2
, NULL
);
523 static int ec_pkey_check(const EVP_PKEY
*pkey
)
525 EC_KEY
*eckey
= pkey
->pkey
.ec
;
527 /* stay consistent to what EVP_PKEY_check demands */
528 if (eckey
->priv_key
== NULL
) {
529 ECerr(EC_F_EC_KEY_CHECK_KEY
, EC_R_MISSING_PRIVATE_KEY
);
533 return EC_KEY_check_key(eckey
);
536 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth
= {
541 "OpenSSL EC algorithm",
558 ec_missing_parameters
,
574 int EC_KEY_print(BIO
*bp
, const EC_KEY
*x
, int off
)
576 int private = EC_KEY_get0_private_key(x
) != NULL
;
578 return do_EC_KEY_print(bp
, x
, off
,
579 private ? EC_KEY_PRINT_PRIVATE
: EC_KEY_PRINT_PUBLIC
);
582 int ECParameters_print(BIO
*bp
, const EC_KEY
*x
)
584 return do_EC_KEY_print(bp
, x
, 4, EC_KEY_PRINT_PARAM
);
587 #ifndef OPENSSL_NO_CMS
589 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX
*pctx
,
590 X509_ALGOR
*alg
, ASN1_BIT_STRING
*pubkey
)
592 const ASN1_OBJECT
*aoid
;
596 EVP_PKEY
*pkpeer
= NULL
;
597 EC_KEY
*ecpeer
= NULL
;
598 const unsigned char *p
;
600 X509_ALGOR_get0(&aoid
, &atype
, &aval
, alg
);
601 if (OBJ_obj2nid(aoid
) != NID_X9_62_id_ecPublicKey
)
603 /* If absent parameters get group from main key */
604 if (atype
== V_ASN1_UNDEF
|| atype
== V_ASN1_NULL
) {
607 pk
= EVP_PKEY_CTX_get0_pkey(pctx
);
610 grp
= EC_KEY_get0_group(pk
->pkey
.ec
);
611 ecpeer
= EC_KEY_new();
614 if (!EC_KEY_set_group(ecpeer
, grp
))
617 ecpeer
= eckey_type2param(atype
, aval
);
621 /* We have parameters now set public key */
622 plen
= ASN1_STRING_length(pubkey
);
623 p
= ASN1_STRING_get0_data(pubkey
);
626 if (!o2i_ECPublicKey(&ecpeer
, &p
, plen
))
628 pkpeer
= EVP_PKEY_new();
631 EVP_PKEY_set1_EC_KEY(pkpeer
, ecpeer
);
632 if (EVP_PKEY_derive_set_peer(pctx
, pkpeer
) > 0)
636 EVP_PKEY_free(pkpeer
);
640 /* Set KDF parameters based on KDF NID */
641 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX
*pctx
, int eckdf_nid
)
643 int kdf_nid
, kdfmd_nid
, cofactor
;
644 const EVP_MD
*kdf_md
;
645 if (eckdf_nid
== NID_undef
)
648 /* Lookup KDF type, cofactor mode and digest */
649 if (!OBJ_find_sigid_algs(eckdf_nid
, &kdfmd_nid
, &kdf_nid
))
652 if (kdf_nid
== NID_dh_std_kdf
)
654 else if (kdf_nid
== NID_dh_cofactor_kdf
)
659 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx
, cofactor
) <= 0)
662 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, EVP_PKEY_ECDH_KDF_X9_62
) <= 0)
665 kdf_md
= EVP_get_digestbynid(kdfmd_nid
);
669 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
674 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX
*pctx
, CMS_RecipientInfo
*ri
)
678 X509_ALGOR
*alg
, *kekalg
= NULL
;
679 ASN1_OCTET_STRING
*ukm
;
680 const unsigned char *p
;
681 unsigned char *der
= NULL
;
683 const EVP_CIPHER
*kekcipher
;
684 EVP_CIPHER_CTX
*kekctx
;
686 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &alg
, &ukm
))
689 if (!ecdh_cms_set_kdf_param(pctx
, OBJ_obj2nid(alg
->algorithm
))) {
690 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO
, EC_R_KDF_PARAMETER_ERROR
);
694 if (alg
->parameter
->type
!= V_ASN1_SEQUENCE
)
697 p
= alg
->parameter
->value
.sequence
->data
;
698 plen
= alg
->parameter
->value
.sequence
->length
;
699 kekalg
= d2i_X509_ALGOR(NULL
, &p
, plen
);
702 kekctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
705 kekcipher
= EVP_get_cipherbyobj(kekalg
->algorithm
);
706 if (!kekcipher
|| EVP_CIPHER_mode(kekcipher
) != EVP_CIPH_WRAP_MODE
)
708 if (!EVP_EncryptInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
))
710 if (EVP_CIPHER_asn1_to_param(kekctx
, kekalg
->parameter
) <= 0)
713 keylen
= EVP_CIPHER_CTX_key_length(kekctx
);
714 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
717 plen
= CMS_SharedInfo_encode(&der
, kekalg
, ukm
, keylen
);
722 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, der
, plen
) <= 0)
728 X509_ALGOR_free(kekalg
);
733 static int ecdh_cms_decrypt(CMS_RecipientInfo
*ri
)
736 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
739 /* See if we need to set peer key */
740 if (!EVP_PKEY_CTX_get0_peerkey(pctx
)) {
742 ASN1_BIT_STRING
*pubkey
;
743 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &alg
, &pubkey
,
748 if (!ecdh_cms_set_peerkey(pctx
, alg
, pubkey
)) {
749 ECerr(EC_F_ECDH_CMS_DECRYPT
, EC_R_PEER_KEY_ERROR
);
753 /* Set ECDH derivation parameters and initialise unwrap context */
754 if (!ecdh_cms_set_shared_info(pctx
, ri
)) {
755 ECerr(EC_F_ECDH_CMS_DECRYPT
, EC_R_SHARED_INFO_ERROR
);
761 static int ecdh_cms_encrypt(CMS_RecipientInfo
*ri
)
767 X509_ALGOR
*talg
, *wrap_alg
= NULL
;
768 const ASN1_OBJECT
*aoid
;
769 ASN1_BIT_STRING
*pubkey
;
770 ASN1_STRING
*wrap_str
;
771 ASN1_OCTET_STRING
*ukm
;
772 unsigned char *penc
= NULL
;
775 int ecdh_nid
, kdf_type
, kdf_nid
, wrap_nid
;
776 const EVP_MD
*kdf_md
;
777 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
780 /* Get ephemeral key */
781 pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
782 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &talg
, &pubkey
,
785 X509_ALGOR_get0(&aoid
, NULL
, NULL
, talg
);
786 /* Is everything uninitialised? */
787 if (aoid
== OBJ_nid2obj(NID_undef
)) {
789 EC_KEY
*eckey
= pkey
->pkey
.ec
;
793 penclen
= i2o_ECPublicKey(eckey
, NULL
);
796 penc
= OPENSSL_malloc(penclen
);
800 penclen
= i2o_ECPublicKey(eckey
, &p
);
803 ASN1_STRING_set0(pubkey
, penc
, penclen
);
804 pubkey
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
805 pubkey
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
808 X509_ALGOR_set0(talg
, OBJ_nid2obj(NID_X9_62_id_ecPublicKey
),
812 /* See if custom parameters set */
813 kdf_type
= EVP_PKEY_CTX_get_ecdh_kdf_type(pctx
);
816 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx
, &kdf_md
))
818 ecdh_nid
= EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx
);
821 else if (ecdh_nid
== 0)
822 ecdh_nid
= NID_dh_std_kdf
;
823 else if (ecdh_nid
== 1)
824 ecdh_nid
= NID_dh_cofactor_kdf
;
826 if (kdf_type
== EVP_PKEY_ECDH_KDF_NONE
) {
827 kdf_type
= EVP_PKEY_ECDH_KDF_X9_62
;
828 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, kdf_type
) <= 0)
833 if (kdf_md
== NULL
) {
834 /* Fixme later for better MD */
836 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
840 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &talg
, &ukm
))
843 /* Lookup NID for KDF+cofactor+digest */
845 if (!OBJ_find_sigid_by_algs(&kdf_nid
, EVP_MD_type(kdf_md
), ecdh_nid
))
848 ctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
849 wrap_nid
= EVP_CIPHER_CTX_type(ctx
);
850 keylen
= EVP_CIPHER_CTX_key_length(ctx
);
852 /* Package wrap algorithm in an AlgorithmIdentifier */
854 wrap_alg
= X509_ALGOR_new();
855 if (wrap_alg
== NULL
)
857 wrap_alg
->algorithm
= OBJ_nid2obj(wrap_nid
);
858 wrap_alg
->parameter
= ASN1_TYPE_new();
859 if (wrap_alg
->parameter
== NULL
)
861 if (EVP_CIPHER_param_to_asn1(ctx
, wrap_alg
->parameter
) <= 0)
863 if (ASN1_TYPE_get(wrap_alg
->parameter
) == NID_undef
) {
864 ASN1_TYPE_free(wrap_alg
->parameter
);
865 wrap_alg
->parameter
= NULL
;
868 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
871 penclen
= CMS_SharedInfo_encode(&penc
, wrap_alg
, ukm
, keylen
);
876 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, penc
, penclen
) <= 0)
881 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
882 * of another AlgorithmIdentifier.
884 penclen
= i2d_X509_ALGOR(wrap_alg
, &penc
);
885 if (!penc
|| !penclen
)
887 wrap_str
= ASN1_STRING_new();
888 if (wrap_str
== NULL
)
890 ASN1_STRING_set0(wrap_str
, penc
, penclen
);
892 X509_ALGOR_set0(talg
, OBJ_nid2obj(kdf_nid
), V_ASN1_SEQUENCE
, wrap_str
);
898 X509_ALGOR_free(wrap_alg
);