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 #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, 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 EC_GROUP *group = NULL;
97 if (ptype == V_ASN1_SEQUENCE) {
98 const ASN1_STRING *pstr = pval;
99 const unsigned char *pm = pstr->data;
100 int pmlen = pstr->length;
102 if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
103 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
106 } else if (ptype == V_ASN1_OBJECT) {
107 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);
132 EC_GROUP_free(group);
136 static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
138 const unsigned char *p = NULL;
141 EC_KEY *eckey = NULL;
144 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
146 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
148 eckey = eckey_type2param(ptype, pval);
151 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
155 /* We have parameters now set public key */
156 if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
157 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
161 EVP_PKEY_assign_EC_KEY(pkey, eckey);
169 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
172 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
173 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
174 *pb = EC_KEY_get0_public_key(b->pkey.ec);
175 if (group == NULL || pa == NULL || pb == NULL)
177 r = EC_POINT_cmp(group, pa, pb, NULL);
185 static int eckey_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
187 const unsigned char *p = NULL;
190 EC_KEY *eckey = NULL;
191 const X509_ALGOR *palg;
193 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
195 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
197 eckey = eckey_type2param(ptype, pval);
202 /* We have parameters now set private key */
203 if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
204 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
208 EVP_PKEY_assign_EC_KEY(pkey, eckey);
212 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
218 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
220 EC_KEY ec_key = *(pkey->pkey.ec);
221 unsigned char *ep, *p;
224 unsigned int old_flags;
226 if (!eckey_param2type(&ptype, &pval, &ec_key)) {
227 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
231 /* set the private key */
234 * do not include the parameters in the SEC1 private key see PKCS#11
237 old_flags = EC_KEY_get_enc_flags(&ec_key);
238 EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
240 eplen = i2d_ECPrivateKey(&ec_key, NULL);
242 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
245 ep = OPENSSL_malloc(eplen);
247 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
251 if (!i2d_ECPrivateKey(&ec_key, &p)) {
253 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
257 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
258 ptype, pval, ep, eplen)) {
266 static int int_ec_size(const EVP_PKEY *pkey)
268 return ECDSA_size(pkey->pkey.ec);
271 static int ec_bits(const EVP_PKEY *pkey)
273 return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
276 static int ec_security_bits(const EVP_PKEY *pkey)
278 int ecbits = ec_bits(pkey);
292 static int ec_missing_parameters(const EVP_PKEY *pkey)
294 if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
299 static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
301 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
305 if (to->pkey.ec == NULL) {
306 to->pkey.ec = EC_KEY_new();
307 if (to->pkey.ec == NULL)
310 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
312 EC_GROUP_free(group);
315 EC_GROUP_free(group);
319 static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
321 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
322 *group_b = EC_KEY_get0_group(b->pkey.ec);
323 if (group_a == NULL || group_b == NULL)
325 if (EC_GROUP_cmp(group_a, group_b, NULL))
331 static void int_ec_free(EVP_PKEY *pkey)
333 EC_KEY_free(pkey->pkey.ec);
337 EC_KEY_PRINT_PRIVATE,
342 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
345 unsigned char *priv = NULL, *pub = NULL;
346 size_t privlen = 0, publen = 0;
348 const EC_GROUP *group;
350 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
351 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_PASSED_NULL_PARAMETER);
355 if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
356 publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
361 if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
362 privlen = EC_KEY_priv2buf(x, &priv);
367 if (ktype == EC_KEY_PRINT_PRIVATE)
368 ecstr = "Private-Key";
369 else if (ktype == EC_KEY_PRINT_PUBLIC)
370 ecstr = "Public-Key";
372 ecstr = "ECDSA-Parameters";
374 if (!BIO_indent(bp, off, 128))
376 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
377 EC_GROUP_order_bits(group)) <= 0)
381 if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
383 if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
388 if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
390 if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
394 if (!ECPKParameters_print(bp, group, off))
399 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_EC_LIB);
400 OPENSSL_clear_free(priv, privlen);
405 static int eckey_param_decode(EVP_PKEY *pkey,
406 const unsigned char **pder, int derlen)
410 if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
411 ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
414 EVP_PKEY_assign_EC_KEY(pkey, eckey);
418 static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
420 return i2d_ECParameters(pkey->pkey.ec, pder);
423 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
426 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
429 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
432 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
435 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
438 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
441 static int old_ec_priv_decode(EVP_PKEY *pkey,
442 const unsigned char **pder, int derlen)
446 if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
447 ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
450 EVP_PKEY_assign_EC_KEY(pkey, ec);
454 static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
456 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
459 static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
462 case ASN1_PKEY_CTRL_PKCS7_SIGN:
465 X509_ALGOR *alg1, *alg2;
466 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
467 if (alg1 == NULL || alg1->algorithm == NULL)
469 hnid = OBJ_obj2nid(alg1->algorithm);
470 if (hnid == NID_undef)
472 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
474 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
477 #ifndef OPENSSL_NO_CMS
478 case ASN1_PKEY_CTRL_CMS_SIGN:
481 X509_ALGOR *alg1, *alg2;
482 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
483 if (alg1 == NULL || alg1->algorithm == NULL)
485 hnid = OBJ_obj2nid(alg1->algorithm);
486 if (hnid == NID_undef)
488 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
490 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
494 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
496 return ecdh_cms_decrypt(arg2);
498 return ecdh_cms_encrypt(arg2);
501 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
502 *(int *)arg2 = CMS_RECIPINFO_AGREE;
506 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
507 if (EVP_PKEY_id(pkey) == EVP_PKEY_SM2) {
508 /* For SM2, the only valid digest-alg is SM3 */
509 *(int *)arg2 = NID_sm3;
511 *(int *)arg2 = NID_sha256;
515 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
516 return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL);
518 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
519 return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
520 POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
529 static int ec_pkey_check(const EVP_PKEY *pkey)
531 EC_KEY *eckey = pkey->pkey.ec;
533 /* stay consistent to what EVP_PKEY_check demands */
534 if (eckey->priv_key == NULL) {
535 ECerr(EC_F_EC_PKEY_CHECK, EC_R_MISSING_PRIVATE_KEY);
539 return EC_KEY_check_key(eckey);
542 static int ec_pkey_public_check(const EVP_PKEY *pkey)
544 EC_KEY *eckey = pkey->pkey.ec;
547 * Note: it unnecessary to check eckey->pub_key here since
548 * it will be checked in EC_KEY_check_key(). In fact, the
549 * EC_KEY_check_key() mainly checks the public key, and checks
550 * the private key optionally (only if there is one). So if
551 * someone passes a whole EC key (public + private), this
555 return EC_KEY_check_key(eckey);
558 static int ec_pkey_param_check(const EVP_PKEY *pkey)
560 EC_KEY *eckey = pkey->pkey.ec;
562 /* stay consistent to what EVP_PKEY_check demands */
563 if (eckey->group == NULL) {
564 ECerr(EC_F_EC_PKEY_PARAM_CHECK, EC_R_MISSING_PARAMETERS);
568 return EC_GROUP_check(eckey->group, NULL);
571 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
576 "OpenSSL EC algorithm",
593 ec_missing_parameters,
607 ec_pkey_public_check,
611 #if !defined(OPENSSL_NO_SM2)
612 const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = {
619 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
621 int private = EC_KEY_get0_private_key(x) != NULL;
623 return do_EC_KEY_print(bp, x, off,
624 private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
627 int ECParameters_print(BIO *bp, const EC_KEY *x)
629 return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
632 #ifndef OPENSSL_NO_CMS
634 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
635 X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
637 const ASN1_OBJECT *aoid;
641 EVP_PKEY *pkpeer = NULL;
642 EC_KEY *ecpeer = NULL;
643 const unsigned char *p;
645 X509_ALGOR_get0(&aoid, &atype, &aval, alg);
646 if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
648 /* If absent parameters get group from main key */
649 if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
652 pk = EVP_PKEY_CTX_get0_pkey(pctx);
655 grp = EC_KEY_get0_group(pk->pkey.ec);
656 ecpeer = EC_KEY_new();
659 if (!EC_KEY_set_group(ecpeer, grp))
662 ecpeer = eckey_type2param(atype, aval);
666 /* We have parameters now set public key */
667 plen = ASN1_STRING_length(pubkey);
668 p = ASN1_STRING_get0_data(pubkey);
671 if (!o2i_ECPublicKey(&ecpeer, &p, plen))
673 pkpeer = EVP_PKEY_new();
676 EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
677 if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
681 EVP_PKEY_free(pkpeer);
685 /* Set KDF parameters based on KDF NID */
686 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
688 int kdf_nid, kdfmd_nid, cofactor;
689 const EVP_MD *kdf_md;
690 if (eckdf_nid == NID_undef)
693 /* Lookup KDF type, cofactor mode and digest */
694 if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
697 if (kdf_nid == NID_dh_std_kdf)
699 else if (kdf_nid == NID_dh_cofactor_kdf)
704 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
707 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
710 kdf_md = EVP_get_digestbynid(kdfmd_nid);
714 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
719 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
723 X509_ALGOR *alg, *kekalg = NULL;
724 ASN1_OCTET_STRING *ukm;
725 const unsigned char *p;
726 unsigned char *der = NULL;
728 const EVP_CIPHER *kekcipher;
729 EVP_CIPHER_CTX *kekctx;
731 if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
734 if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
735 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
739 if (alg->parameter->type != V_ASN1_SEQUENCE)
742 p = alg->parameter->value.sequence->data;
743 plen = alg->parameter->value.sequence->length;
744 kekalg = d2i_X509_ALGOR(NULL, &p, plen);
747 kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
750 kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
751 if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
753 if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
755 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
758 keylen = EVP_CIPHER_CTX_key_length(kekctx);
759 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
762 plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
767 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
773 X509_ALGOR_free(kekalg);
778 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
781 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
784 /* See if we need to set peer key */
785 if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
787 ASN1_BIT_STRING *pubkey;
788 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
793 if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
794 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
798 /* Set ECDH derivation parameters and initialise unwrap context */
799 if (!ecdh_cms_set_shared_info(pctx, ri)) {
800 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
806 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
812 X509_ALGOR *talg, *wrap_alg = NULL;
813 const ASN1_OBJECT *aoid;
814 ASN1_BIT_STRING *pubkey;
815 ASN1_STRING *wrap_str;
816 ASN1_OCTET_STRING *ukm;
817 unsigned char *penc = NULL;
820 int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
821 const EVP_MD *kdf_md;
822 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
825 /* Get ephemeral key */
826 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
827 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
830 X509_ALGOR_get0(&aoid, NULL, NULL, talg);
831 /* Is everything uninitialised? */
832 if (aoid == OBJ_nid2obj(NID_undef)) {
834 EC_KEY *eckey = pkey->pkey.ec;
838 penclen = i2o_ECPublicKey(eckey, NULL);
841 penc = OPENSSL_malloc(penclen);
845 penclen = i2o_ECPublicKey(eckey, &p);
848 ASN1_STRING_set0(pubkey, penc, penclen);
849 pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
850 pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
853 X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
857 /* See if custom parameters set */
858 kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
861 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
863 ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
866 else if (ecdh_nid == 0)
867 ecdh_nid = NID_dh_std_kdf;
868 else if (ecdh_nid == 1)
869 ecdh_nid = NID_dh_cofactor_kdf;
871 if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
872 kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
873 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
878 if (kdf_md == NULL) {
879 /* Fixme later for better MD */
881 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
885 if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
888 /* Lookup NID for KDF+cofactor+digest */
890 if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
893 ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
894 wrap_nid = EVP_CIPHER_CTX_type(ctx);
895 keylen = EVP_CIPHER_CTX_key_length(ctx);
897 /* Package wrap algorithm in an AlgorithmIdentifier */
899 wrap_alg = X509_ALGOR_new();
900 if (wrap_alg == NULL)
902 wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
903 wrap_alg->parameter = ASN1_TYPE_new();
904 if (wrap_alg->parameter == NULL)
906 if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
908 if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
909 ASN1_TYPE_free(wrap_alg->parameter);
910 wrap_alg->parameter = NULL;
913 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
916 penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
921 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
926 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
927 * of another AlgorithmIdentifier.
929 penclen = i2d_X509_ALGOR(wrap_alg, &penc);
930 if (!penc || !penclen)
932 wrap_str = ASN1_STRING_new();
933 if (wrap_str == NULL)
935 ASN1_STRING_set0(wrap_str, penc, penclen);
937 X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
943 X509_ALGOR_free(wrap_alg);