2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
60 #include "internal/cryptlib.h"
61 #include <openssl/x509.h>
62 #include <openssl/ec.h>
63 #include <openssl/bn.h>
64 #include <openssl/cms.h>
65 #include <openssl/asn1t.h>
66 #include "internal/asn1_int.h"
67 #include "internal/evp_int.h"
69 #ifndef OPENSSL_NO_CMS
70 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri);
71 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri);
74 static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key)
76 const EC_GROUP *group;
78 if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
79 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
82 if (EC_GROUP_get_asn1_flag(group)
83 && (nid = EC_GROUP_get_curve_name(group)))
84 /* we have a 'named curve' => just set the OID */
86 *ppval = OBJ_nid2obj(nid);
87 *pptype = V_ASN1_OBJECT;
88 } else { /* explicit parameters */
90 ASN1_STRING *pstr = NULL;
91 pstr = ASN1_STRING_new();
94 pstr->length = i2d_ECParameters(ec_key, &pstr->data);
95 if (pstr->length <= 0) {
96 ASN1_STRING_free(pstr);
97 ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
101 *pptype = V_ASN1_SEQUENCE;
106 static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
108 EC_KEY *ec_key = pkey->pkey.ec;
111 unsigned char *penc = NULL, *p;
114 if (!eckey_param2type(&ptype, &pval, ec_key)) {
115 ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
118 penclen = i2o_ECPublicKey(ec_key, NULL);
121 penc = OPENSSL_malloc(penclen);
125 penclen = i2o_ECPublicKey(ec_key, &p);
128 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
129 ptype, pval, penc, penclen))
132 if (ptype == V_ASN1_OBJECT)
133 ASN1_OBJECT_free(pval);
135 ASN1_STRING_free(pval);
140 static EC_KEY *eckey_type2param(int ptype, void *pval)
142 EC_KEY *eckey = NULL;
143 if (ptype == V_ASN1_SEQUENCE) {
144 ASN1_STRING *pstr = pval;
145 const unsigned char *pm = NULL;
148 pmlen = pstr->length;
149 if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
150 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
153 } else if (ptype == V_ASN1_OBJECT) {
154 ASN1_OBJECT *poid = pval;
158 * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
160 if ((eckey = EC_KEY_new()) == NULL) {
161 ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
164 group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
167 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
168 if (EC_KEY_set_group(eckey, group) == 0)
170 EC_GROUP_free(group);
172 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
183 static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
185 const unsigned char *p = NULL;
188 EC_KEY *eckey = NULL;
191 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
193 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
195 eckey = eckey_type2param(ptype, pval);
198 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
202 /* We have parameters now set public key */
203 if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
204 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
208 EVP_PKEY_assign_EC_KEY(pkey, eckey);
216 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
219 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
220 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
221 *pb = EC_KEY_get0_public_key(b->pkey.ec);
222 r = EC_POINT_cmp(group, pa, pb, NULL);
230 static int eckey_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
232 const unsigned char *p = NULL;
235 EC_KEY *eckey = NULL;
238 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
240 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
242 eckey = eckey_type2param(ptype, pval);
247 /* We have parameters now set private key */
248 if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
249 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
253 EVP_PKEY_assign_EC_KEY(pkey, eckey);
257 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
263 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
266 unsigned char *ep, *p;
269 unsigned int tmp_flags, old_flags;
271 ec_key = pkey->pkey.ec;
273 if (!eckey_param2type(&ptype, &pval, ec_key)) {
274 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
278 /* set the private key */
281 * do not include the parameters in the SEC1 private key see PKCS#11
284 old_flags = EC_KEY_get_enc_flags(ec_key);
285 tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS;
286 EC_KEY_set_enc_flags(ec_key, tmp_flags);
287 eplen = i2d_ECPrivateKey(ec_key, NULL);
289 EC_KEY_set_enc_flags(ec_key, old_flags);
290 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
293 ep = OPENSSL_malloc(eplen);
295 EC_KEY_set_enc_flags(ec_key, old_flags);
296 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
300 if (!i2d_ECPrivateKey(ec_key, &p)) {
301 EC_KEY_set_enc_flags(ec_key, old_flags);
303 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
306 /* restore old encoding flags */
307 EC_KEY_set_enc_flags(ec_key, old_flags);
309 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
310 ptype, pval, ep, eplen))
316 static int int_ec_size(const EVP_PKEY *pkey)
318 return ECDSA_size(pkey->pkey.ec);
321 static int ec_bits(const EVP_PKEY *pkey)
323 return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
326 static int ec_security_bits(const EVP_PKEY *pkey)
328 int ecbits = ec_bits(pkey);
342 static int ec_missing_parameters(const EVP_PKEY *pkey)
344 if (EC_KEY_get0_group(pkey->pkey.ec) == NULL)
349 static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
351 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
354 if (to->pkey.ec == NULL) {
355 to->pkey.ec = EC_KEY_new();
356 if (to->pkey.ec == NULL)
359 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
361 EC_GROUP_free(group);
365 static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
367 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
368 *group_b = EC_KEY_get0_group(b->pkey.ec);
369 if (EC_GROUP_cmp(group_a, group_b, NULL))
375 static void int_ec_free(EVP_PKEY *pkey)
377 EC_KEY_free(pkey->pkey.ec);
381 EC_KEY_PRINT_PRIVATE,
386 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
389 unsigned char *priv = NULL, *pub = NULL;
390 size_t privlen = 0, publen = 0;
392 const EC_GROUP *group;
394 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
395 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_PASSED_NULL_PARAMETER);
399 if (ktype != EC_KEY_PRINT_PARAM) {
400 publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
405 if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
406 privlen = EC_KEY_priv2buf(x, &priv);
411 if (ktype == EC_KEY_PRINT_PRIVATE)
412 ecstr = "Private-Key";
413 else if (ktype == EC_KEY_PRINT_PUBLIC)
414 ecstr = "Public-Key";
416 ecstr = "ECDSA-Parameters";
418 if (!BIO_indent(bp, off, 128))
420 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
421 EC_GROUP_order_bits(group)) <= 0)
425 if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
427 if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
432 if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
434 if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
438 if (!ECPKParameters_print(bp, group, off))
443 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_EC_LIB);
444 OPENSSL_clear_free(priv, privlen);
449 static int eckey_param_decode(EVP_PKEY *pkey,
450 const unsigned char **pder, int derlen)
454 if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
455 ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
458 EVP_PKEY_assign_EC_KEY(pkey, eckey);
462 static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
464 return i2d_ECParameters(pkey->pkey.ec, pder);
467 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
470 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
473 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
476 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
479 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
482 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
485 static int old_ec_priv_decode(EVP_PKEY *pkey,
486 const unsigned char **pder, int derlen)
490 if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
491 ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
494 EVP_PKEY_assign_EC_KEY(pkey, ec);
498 static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
500 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
503 static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
506 case ASN1_PKEY_CTRL_PKCS7_SIGN:
509 X509_ALGOR *alg1, *alg2;
510 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
511 if (alg1 == NULL || alg1->algorithm == NULL)
513 hnid = OBJ_obj2nid(alg1->algorithm);
514 if (hnid == NID_undef)
516 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
518 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
521 #ifndef OPENSSL_NO_CMS
522 case ASN1_PKEY_CTRL_CMS_SIGN:
525 X509_ALGOR *alg1, *alg2;
526 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
527 if (alg1 == NULL || alg1->algorithm == NULL)
529 hnid = OBJ_obj2nid(alg1->algorithm);
530 if (hnid == NID_undef)
532 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
534 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
538 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
540 return ecdh_cms_decrypt(arg2);
542 return ecdh_cms_encrypt(arg2);
545 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
546 *(int *)arg2 = CMS_RECIPINFO_AGREE;
550 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
551 *(int *)arg2 = NID_sha256;
561 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
566 "OpenSSL EC algorithm",
583 ec_missing_parameters,
595 #ifndef OPENSSL_NO_CMS
597 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
598 X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
604 EVP_PKEY *pkpeer = NULL;
605 EC_KEY *ecpeer = NULL;
606 const unsigned char *p;
608 X509_ALGOR_get0(&aoid, &atype, &aval, alg);
609 if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
611 /* If absent parameters get group from main key */
612 if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
615 pk = EVP_PKEY_CTX_get0_pkey(pctx);
618 grp = EC_KEY_get0_group(pk->pkey.ec);
619 ecpeer = EC_KEY_new();
622 if (!EC_KEY_set_group(ecpeer, grp))
625 ecpeer = eckey_type2param(atype, aval);
629 /* We have parameters now set public key */
630 plen = ASN1_STRING_length(pubkey);
631 p = ASN1_STRING_data(pubkey);
634 if (!o2i_ECPublicKey(&ecpeer, &p, plen))
636 pkpeer = EVP_PKEY_new();
639 EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
640 if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
644 EVP_PKEY_free(pkpeer);
648 /* Set KDF parameters based on KDF NID */
649 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
651 int kdf_nid, kdfmd_nid, cofactor;
652 const EVP_MD *kdf_md;
653 if (eckdf_nid == NID_undef)
656 /* Lookup KDF type, cofactor mode and digest */
657 if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
660 if (kdf_nid == NID_dh_std_kdf)
662 else if (kdf_nid == NID_dh_cofactor_kdf)
667 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
670 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_62) <= 0)
673 kdf_md = EVP_get_digestbynid(kdfmd_nid);
677 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
682 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
686 X509_ALGOR *alg, *kekalg = NULL;
687 ASN1_OCTET_STRING *ukm;
688 const unsigned char *p;
689 unsigned char *der = NULL;
691 const EVP_CIPHER *kekcipher;
692 EVP_CIPHER_CTX *kekctx;
694 if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
697 if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
698 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
702 if (alg->parameter->type != V_ASN1_SEQUENCE)
705 p = alg->parameter->value.sequence->data;
706 plen = alg->parameter->value.sequence->length;
707 kekalg = d2i_X509_ALGOR(NULL, &p, plen);
710 kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
713 kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
714 if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
716 if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
718 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
721 keylen = EVP_CIPHER_CTX_key_length(kekctx);
722 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
725 plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
730 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
736 X509_ALGOR_free(kekalg);
741 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
744 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
747 /* See if we need to set peer key */
748 if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
750 ASN1_BIT_STRING *pubkey;
751 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
756 if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
757 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
761 /* Set ECDH derivation parameters and initialise unwrap context */
762 if (!ecdh_cms_set_shared_info(pctx, ri)) {
763 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
769 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
775 X509_ALGOR *talg, *wrap_alg = NULL;
777 ASN1_BIT_STRING *pubkey;
778 ASN1_STRING *wrap_str;
779 ASN1_OCTET_STRING *ukm;
780 unsigned char *penc = NULL;
783 int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
784 const EVP_MD *kdf_md;
785 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
788 /* Get ephemeral key */
789 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
790 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
793 X509_ALGOR_get0(&aoid, NULL, NULL, talg);
794 /* Is everything uninitialised? */
795 if (aoid == OBJ_nid2obj(NID_undef)) {
797 EC_KEY *eckey = pkey->pkey.ec;
801 penclen = i2o_ECPublicKey(eckey, NULL);
804 penc = OPENSSL_malloc(penclen);
808 penclen = i2o_ECPublicKey(eckey, &p);
811 ASN1_STRING_set0(pubkey, penc, penclen);
812 pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
813 pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
816 X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
820 /* See if custom parameters set */
821 kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
824 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
826 ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
829 else if (ecdh_nid == 0)
830 ecdh_nid = NID_dh_std_kdf;
831 else if (ecdh_nid == 1)
832 ecdh_nid = NID_dh_cofactor_kdf;
834 if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
835 kdf_type = EVP_PKEY_ECDH_KDF_X9_62;
836 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
841 if (kdf_md == NULL) {
842 /* Fixme later for better MD */
844 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
848 if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
851 /* Lookup NID for KDF+cofactor+digest */
853 if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
856 ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
857 wrap_nid = EVP_CIPHER_CTX_type(ctx);
858 keylen = EVP_CIPHER_CTX_key_length(ctx);
860 /* Package wrap algorithm in an AlgorithmIdentifier */
862 wrap_alg = X509_ALGOR_new();
863 if (wrap_alg == NULL)
865 wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
866 wrap_alg->parameter = ASN1_TYPE_new();
867 if (wrap_alg->parameter == NULL)
869 if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
871 if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
872 ASN1_TYPE_free(wrap_alg->parameter);
873 wrap_alg->parameter = NULL;
876 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
879 penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
884 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
889 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
890 * of another AlgorithmIdentifier.
892 penclen = i2d_X509_ALGOR(wrap_alg, &penc);
893 if (!penc || !penclen)
895 wrap_str = ASN1_STRING_new();
896 if (wrap_str == NULL)
898 ASN1_STRING_set0(wrap_str, penc, penclen);
900 X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
906 X509_ALGOR_free(wrap_alg);