2 * Copyright 2006-2018 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"
27 #ifndef OPENSSL_NO_CMS
28 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri);
29 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri);
32 static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key)
34 const EC_GROUP *group;
36 if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
37 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
40 if (EC_GROUP_get_asn1_flag(group)
41 && (nid = EC_GROUP_get_curve_name(group)))
42 /* we have a 'named curve' => just set the OID */
44 *ppval = OBJ_nid2obj(nid);
45 *pptype = V_ASN1_OBJECT;
46 } else { /* explicit parameters */
48 ASN1_STRING *pstr = NULL;
49 pstr = ASN1_STRING_new();
52 pstr->length = i2d_ECParameters(ec_key, &pstr->data);
53 if (pstr->length <= 0) {
54 ASN1_STRING_free(pstr);
55 ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
59 *pptype = V_ASN1_SEQUENCE;
64 static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
66 EC_KEY *ec_key = pkey->pkey.ec;
69 unsigned char *penc = NULL, *p;
72 if (!eckey_param2type(&ptype, &pval, ec_key)) {
73 ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
76 penclen = i2o_ECPublicKey(ec_key, NULL);
79 penc = OPENSSL_malloc(penclen);
83 penclen = i2o_ECPublicKey(ec_key, &p);
86 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
87 ptype, pval, penc, penclen))
90 if (ptype == V_ASN1_OBJECT)
91 ASN1_OBJECT_free(pval);
93 ASN1_STRING_free(pval);
98 static EC_KEY *eckey_type2param(int ptype, const void *pval)
100 EC_KEY *eckey = NULL;
101 EC_GROUP *group = NULL;
103 if (ptype == V_ASN1_SEQUENCE) {
104 const ASN1_STRING *pstr = pval;
105 const unsigned char *pm = pstr->data;
106 int pmlen = pstr->length;
108 if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
109 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
112 } else if (ptype == V_ASN1_OBJECT) {
113 const ASN1_OBJECT *poid = pval;
116 * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
118 if ((eckey = EC_KEY_new()) == NULL) {
119 ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
122 group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
125 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
126 if (EC_KEY_set_group(eckey, group) == 0)
128 EC_GROUP_free(group);
130 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
138 EC_GROUP_free(group);
142 static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
144 const unsigned char *p = NULL;
147 EC_KEY *eckey = NULL;
150 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
152 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
154 eckey = eckey_type2param(ptype, pval);
157 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
161 /* We have parameters now set public key */
162 if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
163 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
167 EVP_PKEY_assign_EC_KEY(pkey, eckey);
175 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
178 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
179 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
180 *pb = EC_KEY_get0_public_key(b->pkey.ec);
181 if (group == NULL || pa == NULL || pb == NULL)
183 r = EC_POINT_cmp(group, pa, pb, NULL);
191 static int eckey_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
193 const unsigned char *p = NULL;
196 EC_KEY *eckey = NULL;
197 const X509_ALGOR *palg;
199 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
201 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
203 eckey = eckey_type2param(ptype, pval);
208 /* We have parameters now set private key */
209 if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
210 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
214 EVP_PKEY_assign_EC_KEY(pkey, eckey);
218 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
224 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
226 EC_KEY ec_key = *(pkey->pkey.ec);
227 unsigned char *ep, *p;
230 unsigned int old_flags;
232 if (!eckey_param2type(&ptype, &pval, &ec_key)) {
233 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
237 /* set the private key */
240 * do not include the parameters in the SEC1 private key see PKCS#11
243 old_flags = EC_KEY_get_enc_flags(&ec_key);
244 EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
246 eplen = i2d_ECPrivateKey(&ec_key, NULL);
248 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
251 ep = OPENSSL_malloc(eplen);
253 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
257 if (!i2d_ECPrivateKey(&ec_key, &p)) {
259 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
263 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
264 ptype, pval, ep, eplen)) {
272 static int int_ec_size(const EVP_PKEY *pkey)
274 return ECDSA_size(pkey->pkey.ec);
277 static int ec_bits(const EVP_PKEY *pkey)
279 return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
282 static int ec_security_bits(const EVP_PKEY *pkey)
284 int ecbits = ec_bits(pkey);
298 static int ec_missing_parameters(const EVP_PKEY *pkey)
300 if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
305 static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
307 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
311 if (to->pkey.ec == NULL) {
312 to->pkey.ec = EC_KEY_new();
313 if (to->pkey.ec == NULL)
316 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
318 EC_GROUP_free(group);
321 EC_GROUP_free(group);
325 static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
327 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
328 *group_b = EC_KEY_get0_group(b->pkey.ec);
329 if (group_a == NULL || group_b == NULL)
331 if (EC_GROUP_cmp(group_a, group_b, NULL))
337 static void int_ec_free(EVP_PKEY *pkey)
339 EC_KEY_free(pkey->pkey.ec);
343 EC_KEY_PRINT_PRIVATE,
348 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
351 unsigned char *priv = NULL, *pub = NULL;
352 size_t privlen = 0, publen = 0;
354 const EC_GROUP *group;
356 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
357 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_PASSED_NULL_PARAMETER);
361 if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
362 publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
367 if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
368 privlen = EC_KEY_priv2buf(x, &priv);
373 if (ktype == EC_KEY_PRINT_PRIVATE)
374 ecstr = "Private-Key";
375 else if (ktype == EC_KEY_PRINT_PUBLIC)
376 ecstr = "Public-Key";
378 ecstr = "ECDSA-Parameters";
380 if (!BIO_indent(bp, off, 128))
382 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
383 EC_GROUP_order_bits(group)) <= 0)
387 if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
389 if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
394 if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
396 if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
400 if (!ECPKParameters_print(bp, group, off))
405 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_EC_LIB);
406 OPENSSL_clear_free(priv, privlen);
411 static int eckey_param_decode(EVP_PKEY *pkey,
412 const unsigned char **pder, int derlen)
416 if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
417 ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
420 EVP_PKEY_assign_EC_KEY(pkey, eckey);
424 static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
426 return i2d_ECParameters(pkey->pkey.ec, pder);
429 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
432 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
435 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
438 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
441 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
444 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
447 static int old_ec_priv_decode(EVP_PKEY *pkey,
448 const unsigned char **pder, int derlen)
452 if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
453 ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
456 EVP_PKEY_assign_EC_KEY(pkey, ec);
460 static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
462 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
465 static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
468 case ASN1_PKEY_CTRL_PKCS7_SIGN:
471 X509_ALGOR *alg1, *alg2;
472 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
473 if (alg1 == NULL || alg1->algorithm == NULL)
475 hnid = OBJ_obj2nid(alg1->algorithm);
476 if (hnid == NID_undef)
478 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
480 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
483 #ifndef OPENSSL_NO_CMS
484 case ASN1_PKEY_CTRL_CMS_SIGN:
487 X509_ALGOR *alg1, *alg2;
488 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
489 if (alg1 == NULL || alg1->algorithm == NULL)
491 hnid = OBJ_obj2nid(alg1->algorithm);
492 if (hnid == NID_undef)
494 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
496 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
500 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
502 return ecdh_cms_decrypt(arg2);
504 return ecdh_cms_encrypt(arg2);
507 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
508 *(int *)arg2 = CMS_RECIPINFO_AGREE;
512 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
513 if (EVP_PKEY_id(pkey) == EVP_PKEY_SM2) {
514 /* For SM2, the only valid digest-alg is SM3 */
515 *(int *)arg2 = NID_sm3;
516 return 2; /* Make it mandatory */
518 *(int *)arg2 = NID_sha256;
521 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
522 return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL);
524 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
525 return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
526 POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
535 static int ec_pkey_check(const EVP_PKEY *pkey)
537 EC_KEY *eckey = pkey->pkey.ec;
539 /* stay consistent to what EVP_PKEY_check demands */
540 if (eckey->priv_key == NULL) {
541 ECerr(EC_F_EC_PKEY_CHECK, EC_R_MISSING_PRIVATE_KEY);
545 return EC_KEY_check_key(eckey);
548 static int ec_pkey_public_check(const EVP_PKEY *pkey)
550 EC_KEY *eckey = pkey->pkey.ec;
553 * Note: it unnecessary to check eckey->pub_key here since
554 * it will be checked in EC_KEY_check_key(). In fact, the
555 * EC_KEY_check_key() mainly checks the public key, and checks
556 * the private key optionally (only if there is one). So if
557 * someone passes a whole EC key (public + private), this
561 return EC_KEY_check_key(eckey);
564 static int ec_pkey_param_check(const EVP_PKEY *pkey)
566 EC_KEY *eckey = pkey->pkey.ec;
568 /* stay consistent to what EVP_PKEY_check demands */
569 if (eckey->group == NULL) {
570 ECerr(EC_F_EC_PKEY_PARAM_CHECK, EC_R_MISSING_PARAMETERS);
574 return EC_GROUP_check(eckey->group, NULL);
577 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
582 "OpenSSL EC algorithm",
599 ec_missing_parameters,
613 ec_pkey_public_check,
617 #if !defined(OPENSSL_NO_SM2)
618 const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = {
625 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
627 int private = EC_KEY_get0_private_key(x) != NULL;
629 return do_EC_KEY_print(bp, x, off,
630 private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
633 int ECParameters_print(BIO *bp, const EC_KEY *x)
635 return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
638 #ifndef OPENSSL_NO_CMS
640 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
641 X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
643 const ASN1_OBJECT *aoid;
647 EVP_PKEY *pkpeer = NULL;
648 EC_KEY *ecpeer = NULL;
649 const unsigned char *p;
651 X509_ALGOR_get0(&aoid, &atype, &aval, alg);
652 if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
654 /* If absent parameters get group from main key */
655 if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
658 pk = EVP_PKEY_CTX_get0_pkey(pctx);
661 grp = EC_KEY_get0_group(pk->pkey.ec);
662 ecpeer = EC_KEY_new();
665 if (!EC_KEY_set_group(ecpeer, grp))
668 ecpeer = eckey_type2param(atype, aval);
672 /* We have parameters now set public key */
673 plen = ASN1_STRING_length(pubkey);
674 p = ASN1_STRING_get0_data(pubkey);
675 if (p == NULL || plen == 0)
677 if (!o2i_ECPublicKey(&ecpeer, &p, plen))
679 pkpeer = EVP_PKEY_new();
682 EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
683 if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
687 EVP_PKEY_free(pkpeer);
691 /* Set KDF parameters based on KDF NID */
692 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
694 int kdf_nid, kdfmd_nid, cofactor;
695 const EVP_MD *kdf_md;
696 if (eckdf_nid == NID_undef)
699 /* Lookup KDF type, cofactor mode and digest */
700 if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
703 if (kdf_nid == NID_dh_std_kdf)
705 else if (kdf_nid == NID_dh_cofactor_kdf)
710 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
713 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
716 kdf_md = EVP_get_digestbynid(kdfmd_nid);
720 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
725 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
729 X509_ALGOR *alg, *kekalg = NULL;
730 ASN1_OCTET_STRING *ukm;
731 const unsigned char *p;
732 unsigned char *der = NULL;
734 const EVP_CIPHER *kekcipher;
735 EVP_CIPHER_CTX *kekctx;
737 if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
740 if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
741 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
745 if (alg->parameter->type != V_ASN1_SEQUENCE)
748 p = alg->parameter->value.sequence->data;
749 plen = alg->parameter->value.sequence->length;
750 kekalg = d2i_X509_ALGOR(NULL, &p, plen);
753 kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
756 kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
757 if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
759 if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
761 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
764 keylen = EVP_CIPHER_CTX_key_length(kekctx);
765 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
768 plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
773 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
779 X509_ALGOR_free(kekalg);
784 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
787 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
790 /* See if we need to set peer key */
791 if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
793 ASN1_BIT_STRING *pubkey;
794 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
799 if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
800 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
804 /* Set ECDH derivation parameters and initialise unwrap context */
805 if (!ecdh_cms_set_shared_info(pctx, ri)) {
806 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
812 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
818 X509_ALGOR *talg, *wrap_alg = NULL;
819 const ASN1_OBJECT *aoid;
820 ASN1_BIT_STRING *pubkey;
821 ASN1_STRING *wrap_str;
822 ASN1_OCTET_STRING *ukm;
823 unsigned char *penc = NULL;
826 int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
827 const EVP_MD *kdf_md;
828 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
831 /* Get ephemeral key */
832 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
833 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
836 X509_ALGOR_get0(&aoid, NULL, NULL, talg);
837 /* Is everything uninitialised? */
838 if (aoid == OBJ_nid2obj(NID_undef)) {
840 EC_KEY *eckey = pkey->pkey.ec;
844 penclen = i2o_ECPublicKey(eckey, NULL);
847 penc = OPENSSL_malloc(penclen);
851 penclen = i2o_ECPublicKey(eckey, &p);
854 ASN1_STRING_set0(pubkey, penc, penclen);
855 pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
856 pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
859 X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
863 /* See if custom parameters set */
864 kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
867 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
869 ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
872 else if (ecdh_nid == 0)
873 ecdh_nid = NID_dh_std_kdf;
874 else if (ecdh_nid == 1)
875 ecdh_nid = NID_dh_cofactor_kdf;
877 if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
878 kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
879 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
884 if (kdf_md == NULL) {
885 /* Fixme later for better MD */
887 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
891 if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
894 /* Lookup NID for KDF+cofactor+digest */
896 if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
899 ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
900 wrap_nid = EVP_CIPHER_CTX_type(ctx);
901 keylen = EVP_CIPHER_CTX_key_length(ctx);
903 /* Package wrap algorithm in an AlgorithmIdentifier */
905 wrap_alg = X509_ALGOR_new();
906 if (wrap_alg == NULL)
908 wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
909 wrap_alg->parameter = ASN1_TYPE_new();
910 if (wrap_alg->parameter == NULL)
912 if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
914 if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
915 ASN1_TYPE_free(wrap_alg->parameter);
916 wrap_alg->parameter = NULL;
919 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
922 penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
927 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
932 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
933 * of another AlgorithmIdentifier.
935 penclen = i2d_X509_ALGOR(wrap_alg, &penc);
936 if (!penc || !penclen)
938 wrap_str = ASN1_STRING_new();
939 if (wrap_str == NULL)
941 ASN1_STRING_set0(wrap_str, penc, penclen);
943 X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
949 X509_ALGOR_free(wrap_alg);