2 * Copyright 2006-2020 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 "crypto/asn1.h"
18 #include "crypto/evp.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, const 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 ASN1_OBJECT *asn1obj = OBJ_nid2obj(nid);
40 if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
41 ASN1_OBJECT_free(asn1obj);
42 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_OID);
46 *pptype = V_ASN1_OBJECT;
47 } else { /* explicit parameters */
49 ASN1_STRING *pstr = NULL;
50 pstr = ASN1_STRING_new();
55 * The cast in the following line is intentional as the
56 * `i2d_ECParameters` signature can't be constified (see discussion at
57 * https://github.com/openssl/openssl/pull/9347 where related and
58 * required constification backports were rejected).
60 * This cast should be safe anyway, because we can expect
61 * `i2d_ECParameters()` to treat the first argument as if it was const.
63 pstr->length = i2d_ECParameters((EC_KEY *)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)
111 EC_KEY *eckey = NULL;
112 EC_GROUP *group = NULL;
114 if (ptype == V_ASN1_SEQUENCE) {
115 const ASN1_STRING *pstr = pval;
116 const unsigned char *pm = pstr->data;
117 int pmlen = pstr->length;
119 if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
120 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
123 } else if (ptype == V_ASN1_OBJECT) {
124 const ASN1_OBJECT *poid = pval;
127 * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
129 if ((eckey = EC_KEY_new()) == NULL) {
130 ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
133 group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
136 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
137 if (EC_KEY_set_group(eckey, group) == 0)
139 EC_GROUP_free(group);
141 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
149 EC_GROUP_free(group);
153 static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
155 const unsigned char *p = NULL;
158 EC_KEY *eckey = NULL;
161 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
163 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
165 eckey = eckey_type2param(ptype, pval);
168 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
172 /* We have parameters now set public key */
173 if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
174 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
178 EVP_PKEY_assign_EC_KEY(pkey, eckey);
186 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
189 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
190 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
191 *pb = EC_KEY_get0_public_key(b->pkey.ec);
192 if (group == NULL || pa == NULL || pb == NULL)
194 r = EC_POINT_cmp(group, pa, pb, NULL);
202 static int eckey_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
204 const unsigned char *p = NULL;
207 EC_KEY *eckey = NULL;
208 const X509_ALGOR *palg;
210 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
212 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
214 eckey = eckey_type2param(ptype, pval);
219 /* We have parameters now set private key */
220 if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
221 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
225 EVP_PKEY_assign_EC_KEY(pkey, eckey);
229 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
235 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
237 EC_KEY ec_key = *(pkey->pkey.ec);
238 unsigned char *ep, *p;
241 unsigned int old_flags;
243 if (!eckey_param2type(&ptype, &pval, &ec_key)) {
244 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
248 /* set the private key */
251 * do not include the parameters in the SEC1 private key see PKCS#11
254 old_flags = EC_KEY_get_enc_flags(&ec_key);
255 EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
257 eplen = i2d_ECPrivateKey(&ec_key, NULL);
259 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
262 ep = OPENSSL_malloc(eplen);
264 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
268 if (!i2d_ECPrivateKey(&ec_key, &p)) {
270 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
274 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
275 ptype, pval, ep, eplen)) {
283 static int int_ec_size(const EVP_PKEY *pkey)
285 return ECDSA_size(pkey->pkey.ec);
288 static int ec_bits(const EVP_PKEY *pkey)
290 return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
293 static int ec_security_bits(const EVP_PKEY *pkey)
295 int ecbits = ec_bits(pkey);
309 static int ec_missing_parameters(const EVP_PKEY *pkey)
311 if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
316 static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
318 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
322 if (to->pkey.ec == NULL) {
323 to->pkey.ec = EC_KEY_new();
324 if (to->pkey.ec == NULL)
327 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
329 EC_GROUP_free(group);
332 EC_GROUP_free(group);
336 static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
338 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
339 *group_b = EC_KEY_get0_group(b->pkey.ec);
340 if (group_a == NULL || group_b == NULL)
342 if (EC_GROUP_cmp(group_a, group_b, NULL))
348 static void int_ec_free(EVP_PKEY *pkey)
350 EC_KEY_free(pkey->pkey.ec);
354 EC_KEY_PRINT_PRIVATE,
359 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
362 unsigned char *priv = NULL, *pub = NULL;
363 size_t privlen = 0, publen = 0;
365 const EC_GROUP *group;
367 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
368 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_PASSED_NULL_PARAMETER);
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);
378 if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
379 privlen = EC_KEY_priv2buf(x, &priv);
384 if (ktype == EC_KEY_PRINT_PRIVATE)
385 ecstr = "Private-Key";
386 else if (ktype == EC_KEY_PRINT_PUBLIC)
387 ecstr = "Public-Key";
389 ecstr = "ECDSA-Parameters";
391 if (!BIO_indent(bp, off, 128))
393 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
394 EC_GROUP_order_bits(group)) <= 0)
398 if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
400 if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
405 if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
407 if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
411 if (!ECPKParameters_print(bp, group, off))
416 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_EC_LIB);
417 OPENSSL_clear_free(priv, privlen);
422 static int eckey_param_decode(EVP_PKEY *pkey,
423 const unsigned char **pder, int derlen)
427 if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
428 ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
431 EVP_PKEY_assign_EC_KEY(pkey, eckey);
435 static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
437 return i2d_ECParameters(pkey->pkey.ec, pder);
440 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
443 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
446 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
449 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
452 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
455 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
458 static int old_ec_priv_decode(EVP_PKEY *pkey,
459 const unsigned char **pder, int derlen)
463 if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
464 ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
467 EVP_PKEY_assign_EC_KEY(pkey, ec);
471 static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
473 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
476 static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
479 case ASN1_PKEY_CTRL_PKCS7_SIGN:
482 X509_ALGOR *alg1, *alg2;
483 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
484 if (alg1 == NULL || alg1->algorithm == NULL)
486 hnid = OBJ_obj2nid(alg1->algorithm);
487 if (hnid == NID_undef)
489 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
491 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
494 #ifndef OPENSSL_NO_CMS
495 case ASN1_PKEY_CTRL_CMS_SIGN:
498 X509_ALGOR *alg1, *alg2;
499 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
500 if (alg1 == NULL || alg1->algorithm == NULL)
502 hnid = OBJ_obj2nid(alg1->algorithm);
503 if (hnid == NID_undef)
505 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
507 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
511 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
513 return ecdh_cms_decrypt(arg2);
515 return ecdh_cms_encrypt(arg2);
518 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
519 *(int *)arg2 = CMS_RECIPINFO_AGREE;
523 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
524 if (EVP_PKEY_id(pkey) == EVP_PKEY_SM2) {
525 /* For SM2, the only valid digest-alg is SM3 */
526 *(int *)arg2 = NID_sm3;
528 *(int *)arg2 = NID_sha256;
532 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
533 return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL);
535 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
536 return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
537 POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
546 static int ec_pkey_check(const EVP_PKEY *pkey)
548 EC_KEY *eckey = pkey->pkey.ec;
550 /* stay consistent to what EVP_PKEY_check demands */
551 if (eckey->priv_key == NULL) {
552 ECerr(EC_F_EC_PKEY_CHECK, EC_R_MISSING_PRIVATE_KEY);
556 return EC_KEY_check_key(eckey);
559 static int ec_pkey_public_check(const EVP_PKEY *pkey)
561 EC_KEY *eckey = pkey->pkey.ec;
564 * Note: it unnecessary to check eckey->pub_key here since
565 * it will be checked in EC_KEY_check_key(). In fact, the
566 * EC_KEY_check_key() mainly checks the public key, and checks
567 * the private key optionally (only if there is one). So if
568 * someone passes a whole EC key (public + private), this
572 return EC_KEY_check_key(eckey);
575 static int ec_pkey_param_check(const EVP_PKEY *pkey)
577 EC_KEY *eckey = pkey->pkey.ec;
579 /* stay consistent to what EVP_PKEY_check demands */
580 if (eckey->group == NULL) {
581 ECerr(EC_F_EC_PKEY_PARAM_CHECK, EC_R_MISSING_PARAMETERS);
585 return EC_GROUP_check(eckey->group, NULL);
588 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
593 "OpenSSL EC algorithm",
610 ec_missing_parameters,
624 ec_pkey_public_check,
628 #if !defined(OPENSSL_NO_SM2)
629 const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = {
636 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
638 int private = EC_KEY_get0_private_key(x) != NULL;
640 return do_EC_KEY_print(bp, x, off,
641 private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
644 int ECParameters_print(BIO *bp, const EC_KEY *x)
646 return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
649 #ifndef OPENSSL_NO_CMS
651 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
652 X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
654 const ASN1_OBJECT *aoid;
658 EVP_PKEY *pkpeer = NULL;
659 EC_KEY *ecpeer = NULL;
660 const unsigned char *p;
662 X509_ALGOR_get0(&aoid, &atype, &aval, alg);
663 if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
665 /* If absent parameters get group from main key */
666 if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
669 pk = EVP_PKEY_CTX_get0_pkey(pctx);
672 grp = EC_KEY_get0_group(pk->pkey.ec);
673 ecpeer = EC_KEY_new();
676 if (!EC_KEY_set_group(ecpeer, grp))
679 ecpeer = eckey_type2param(atype, aval);
683 /* We have parameters now set public key */
684 plen = ASN1_STRING_length(pubkey);
685 p = ASN1_STRING_get0_data(pubkey);
688 if (!o2i_ECPublicKey(&ecpeer, &p, plen))
690 pkpeer = EVP_PKEY_new();
693 EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
694 if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
698 EVP_PKEY_free(pkpeer);
702 /* Set KDF parameters based on KDF NID */
703 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
705 int kdf_nid, kdfmd_nid, cofactor;
706 const EVP_MD *kdf_md;
707 if (eckdf_nid == NID_undef)
710 /* Lookup KDF type, cofactor mode and digest */
711 if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
714 if (kdf_nid == NID_dh_std_kdf)
716 else if (kdf_nid == NID_dh_cofactor_kdf)
721 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
724 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
727 kdf_md = EVP_get_digestbynid(kdfmd_nid);
731 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
736 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
740 X509_ALGOR *alg, *kekalg = NULL;
741 ASN1_OCTET_STRING *ukm;
742 const unsigned char *p;
743 unsigned char *der = NULL;
745 const EVP_CIPHER *kekcipher;
746 EVP_CIPHER_CTX *kekctx;
748 if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
751 if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
752 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
756 if (alg->parameter->type != V_ASN1_SEQUENCE)
759 p = alg->parameter->value.sequence->data;
760 plen = alg->parameter->value.sequence->length;
761 kekalg = d2i_X509_ALGOR(NULL, &p, plen);
764 kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
767 kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
768 if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
770 if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
772 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
775 keylen = EVP_CIPHER_CTX_key_length(kekctx);
776 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
779 plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
784 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
790 X509_ALGOR_free(kekalg);
795 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
798 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
801 /* See if we need to set peer key */
802 if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
804 ASN1_BIT_STRING *pubkey;
805 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
810 if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
811 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
815 /* Set ECDH derivation parameters and initialise unwrap context */
816 if (!ecdh_cms_set_shared_info(pctx, ri)) {
817 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
823 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
829 X509_ALGOR *talg, *wrap_alg = NULL;
830 const ASN1_OBJECT *aoid;
831 ASN1_BIT_STRING *pubkey;
832 ASN1_STRING *wrap_str;
833 ASN1_OCTET_STRING *ukm;
834 unsigned char *penc = NULL;
837 int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
838 const EVP_MD *kdf_md;
839 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
842 /* Get ephemeral key */
843 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
844 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
847 X509_ALGOR_get0(&aoid, NULL, NULL, talg);
848 /* Is everything uninitialised? */
849 if (aoid == OBJ_nid2obj(NID_undef)) {
851 EC_KEY *eckey = pkey->pkey.ec;
855 penclen = i2o_ECPublicKey(eckey, NULL);
858 penc = OPENSSL_malloc(penclen);
862 penclen = i2o_ECPublicKey(eckey, &p);
865 ASN1_STRING_set0(pubkey, penc, penclen);
866 pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
867 pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
870 X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
874 /* See if custom parameters set */
875 kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
878 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
880 ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
883 else if (ecdh_nid == 0)
884 ecdh_nid = NID_dh_std_kdf;
885 else if (ecdh_nid == 1)
886 ecdh_nid = NID_dh_cofactor_kdf;
888 if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
889 kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
890 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
895 if (kdf_md == NULL) {
896 /* Fixme later for better MD */
898 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
902 if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
905 /* Lookup NID for KDF+cofactor+digest */
907 if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
910 ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
911 wrap_nid = EVP_CIPHER_CTX_type(ctx);
912 keylen = EVP_CIPHER_CTX_key_length(ctx);
914 /* Package wrap algorithm in an AlgorithmIdentifier */
916 wrap_alg = X509_ALGOR_new();
917 if (wrap_alg == NULL)
919 wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
920 wrap_alg->parameter = ASN1_TYPE_new();
921 if (wrap_alg->parameter == NULL)
923 if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
925 if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
926 ASN1_TYPE_free(wrap_alg->parameter);
927 wrap_alg->parameter = NULL;
930 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
933 penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
938 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
943 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
944 * of another AlgorithmIdentifier.
946 penclen = i2d_X509_ALGOR(wrap_alg, &penc);
947 if (!penc || !penclen)
949 wrap_str = ASN1_STRING_new();
950 if (wrap_str == NULL)
952 ASN1_STRING_set0(wrap_str, penc, penclen);
954 X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
960 X509_ALGOR_free(wrap_alg);