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 #ifndef OPENSSL_NO_CMS
65 # include <openssl/cms.h>
67 #include <openssl/asn1t.h>
68 #include "internal/asn1_int.h"
70 #ifndef OPENSSL_NO_CMS
71 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri);
72 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri);
75 static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key)
77 const EC_GROUP *group;
79 if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
80 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
83 if (EC_GROUP_get_asn1_flag(group)
84 && (nid = EC_GROUP_get_curve_name(group)))
85 /* we have a 'named curve' => just set the OID */
87 *ppval = OBJ_nid2obj(nid);
88 *pptype = V_ASN1_OBJECT;
89 } else { /* explicit parameters */
91 ASN1_STRING *pstr = NULL;
92 pstr = ASN1_STRING_new();
95 pstr->length = i2d_ECParameters(ec_key, &pstr->data);
96 if (pstr->length <= 0) {
97 ASN1_STRING_free(pstr);
98 ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
102 *pptype = V_ASN1_SEQUENCE;
107 static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
109 EC_KEY *ec_key = pkey->pkey.ec;
112 unsigned char *penc = NULL, *p;
115 if (!eckey_param2type(&ptype, &pval, ec_key)) {
116 ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
119 penclen = i2o_ECPublicKey(ec_key, NULL);
122 penc = OPENSSL_malloc(penclen);
126 penclen = i2o_ECPublicKey(ec_key, &p);
129 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
130 ptype, pval, penc, penclen))
133 if (ptype == V_ASN1_OBJECT)
134 ASN1_OBJECT_free(pval);
136 ASN1_STRING_free(pval);
141 static EC_KEY *eckey_type2param(int ptype, void *pval)
143 EC_KEY *eckey = NULL;
144 if (ptype == V_ASN1_SEQUENCE) {
145 ASN1_STRING *pstr = pval;
146 const unsigned char *pm = NULL;
149 pmlen = pstr->length;
150 if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
151 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
154 } else if (ptype == V_ASN1_OBJECT) {
155 ASN1_OBJECT *poid = pval;
159 * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
161 if ((eckey = EC_KEY_new()) == NULL) {
162 ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
165 group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
168 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
169 if (EC_KEY_set_group(eckey, group) == 0)
171 EC_GROUP_free(group);
173 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
184 static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
186 const unsigned char *p = NULL;
189 EC_KEY *eckey = NULL;
192 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
194 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
196 eckey = eckey_type2param(ptype, pval);
199 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
203 /* We have parameters now set public key */
204 if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
205 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
209 EVP_PKEY_assign_EC_KEY(pkey, eckey);
217 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
220 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
221 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
222 *pb = EC_KEY_get0_public_key(b->pkey.ec);
223 r = EC_POINT_cmp(group, pa, pb, NULL);
231 static int eckey_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
233 const unsigned char *p = NULL;
236 EC_KEY *eckey = NULL;
239 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
241 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
243 eckey = eckey_type2param(ptype, pval);
248 /* We have parameters now set private key */
249 if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
250 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
254 /* calculate public key (if necessary) */
255 if (EC_KEY_get0_public_key(eckey) == NULL) {
256 const BIGNUM *priv_key;
257 const EC_GROUP *group;
260 * the public key was not included in the SEC1 private key =>
261 * calculate the public key
263 group = EC_KEY_get0_group(eckey);
264 pub_key = EC_POINT_new(group);
265 if (pub_key == NULL) {
266 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
269 if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group))) {
270 EC_POINT_free(pub_key);
271 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
274 priv_key = EC_KEY_get0_private_key(eckey);
275 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL)) {
276 EC_POINT_free(pub_key);
277 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
280 if (EC_KEY_set_public_key(eckey, pub_key) == 0) {
281 EC_POINT_free(pub_key);
282 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
285 EC_POINT_free(pub_key);
288 EVP_PKEY_assign_EC_KEY(pkey, eckey);
292 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
298 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
301 unsigned char *ep, *p;
304 unsigned int tmp_flags, old_flags;
306 ec_key = pkey->pkey.ec;
308 if (!eckey_param2type(&ptype, &pval, ec_key)) {
309 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
313 /* set the private key */
316 * do not include the parameters in the SEC1 private key see PKCS#11
319 old_flags = EC_KEY_get_enc_flags(ec_key);
320 tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS;
321 EC_KEY_set_enc_flags(ec_key, tmp_flags);
322 eplen = i2d_ECPrivateKey(ec_key, NULL);
324 EC_KEY_set_enc_flags(ec_key, old_flags);
325 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
328 ep = OPENSSL_malloc(eplen);
330 EC_KEY_set_enc_flags(ec_key, old_flags);
331 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
335 if (!i2d_ECPrivateKey(ec_key, &p)) {
336 EC_KEY_set_enc_flags(ec_key, old_flags);
338 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
341 /* restore old encoding flags */
342 EC_KEY_set_enc_flags(ec_key, old_flags);
344 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
345 ptype, pval, ep, eplen))
351 static int int_ec_size(const EVP_PKEY *pkey)
353 return ECDSA_size(pkey->pkey.ec);
356 static int ec_bits(const EVP_PKEY *pkey)
358 BIGNUM *order = BN_new();
359 const EC_GROUP *group;
366 group = EC_KEY_get0_group(pkey->pkey.ec);
367 if (!EC_GROUP_get_order(group, order, NULL)) {
372 ret = BN_num_bits(order);
377 static int ec_security_bits(const EVP_PKEY *pkey)
379 int ecbits = ec_bits(pkey);
393 static int ec_missing_parameters(const EVP_PKEY *pkey)
395 if (EC_KEY_get0_group(pkey->pkey.ec) == NULL)
400 static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
402 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
405 if (to->pkey.ec == NULL) {
406 to->pkey.ec = EC_KEY_new();
407 if (to->pkey.ec == NULL)
410 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
412 EC_GROUP_free(group);
416 static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
418 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
419 *group_b = EC_KEY_get0_group(b->pkey.ec);
420 if (EC_GROUP_cmp(group_a, group_b, NULL))
426 static void int_ec_free(EVP_PKEY *pkey)
428 EC_KEY_free(pkey->pkey.ec);
431 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype)
433 unsigned char *buffer = NULL;
435 size_t buf_len = 0, i;
436 int ret = 0, reason = ERR_R_BIO_LIB;
437 BIGNUM *pub_key = NULL, *order = NULL;
439 const EC_GROUP *group;
440 const EC_POINT *public_key;
441 const BIGNUM *priv_key;
443 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
444 reason = ERR_R_PASSED_NULL_PARAMETER;
450 reason = ERR_R_MALLOC_FAILURE;
455 public_key = EC_KEY_get0_public_key(x);
456 if (public_key != NULL) {
457 if ((pub_key = EC_POINT_point2bn(group, public_key,
458 EC_KEY_get_conv_form(x), NULL,
460 reason = ERR_R_EC_LIB;
463 buf_len = (size_t)BN_num_bytes(pub_key);
468 priv_key = EC_KEY_get0_private_key(x);
469 if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len)
476 if ((buffer = OPENSSL_malloc(buf_len)) == NULL) {
477 reason = ERR_R_MALLOC_FAILURE;
482 ecstr = "Private-Key";
484 ecstr = "Public-Key";
486 ecstr = "ECDSA-Parameters";
488 if (!BIO_indent(bp, off, 128))
490 if ((order = BN_new()) == NULL)
492 if (!EC_GROUP_get_order(group, order, NULL))
494 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr, BN_num_bits(order)) <= 0)
497 if ((priv_key != NULL) && !ASN1_bn_print(bp, "priv:", priv_key,
500 if ((pub_key != NULL) && !ASN1_bn_print(bp, "pub: ", pub_key,
503 if (!ECPKParameters_print(bp, group, off))
508 ECerr(EC_F_DO_EC_KEY_PRINT, reason);
512 OPENSSL_free(buffer);
516 static int eckey_param_decode(EVP_PKEY *pkey,
517 const unsigned char **pder, int derlen)
521 if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
522 ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
525 EVP_PKEY_assign_EC_KEY(pkey, eckey);
529 static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
531 return i2d_ECParameters(pkey->pkey.ec, pder);
534 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
537 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
540 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
543 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
546 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
549 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
552 static int old_ec_priv_decode(EVP_PKEY *pkey,
553 const unsigned char **pder, int derlen)
557 if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
558 ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
561 EVP_PKEY_assign_EC_KEY(pkey, ec);
565 static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
567 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
570 static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
573 case ASN1_PKEY_CTRL_PKCS7_SIGN:
576 X509_ALGOR *alg1, *alg2;
577 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
578 if (alg1 == NULL || alg1->algorithm == NULL)
580 hnid = OBJ_obj2nid(alg1->algorithm);
581 if (hnid == NID_undef)
583 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
585 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
588 #ifndef OPENSSL_NO_CMS
589 case ASN1_PKEY_CTRL_CMS_SIGN:
592 X509_ALGOR *alg1, *alg2;
593 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
594 if (alg1 == NULL || alg1->algorithm == NULL)
596 hnid = OBJ_obj2nid(alg1->algorithm);
597 if (hnid == NID_undef)
599 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
601 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
605 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
607 return ecdh_cms_decrypt(arg2);
609 return ecdh_cms_encrypt(arg2);
612 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
613 *(int *)arg2 = CMS_RECIPINFO_AGREE;
617 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
618 *(int *)arg2 = NID_sha256;
628 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
633 "OpenSSL EC algorithm",
650 ec_missing_parameters,
662 #ifndef OPENSSL_NO_CMS
664 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
665 X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
671 EVP_PKEY *pkpeer = NULL;
672 EC_KEY *ecpeer = NULL;
673 const unsigned char *p;
675 X509_ALGOR_get0(&aoid, &atype, &aval, alg);
676 if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
678 /* If absent parameters get group from main key */
679 if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
682 pk = EVP_PKEY_CTX_get0_pkey(pctx);
685 grp = EC_KEY_get0_group(pk->pkey.ec);
686 ecpeer = EC_KEY_new();
689 if (!EC_KEY_set_group(ecpeer, grp))
692 ecpeer = eckey_type2param(atype, aval);
696 /* We have parameters now set public key */
697 plen = ASN1_STRING_length(pubkey);
698 p = ASN1_STRING_data(pubkey);
701 if (!o2i_ECPublicKey(&ecpeer, &p, plen))
703 pkpeer = EVP_PKEY_new();
706 EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
707 if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
711 EVP_PKEY_free(pkpeer);
715 /* Set KDF parameters based on KDF NID */
716 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
718 int kdf_nid, kdfmd_nid, cofactor;
719 const EVP_MD *kdf_md;
720 if (eckdf_nid == NID_undef)
723 /* Lookup KDF type, cofactor mode and digest */
724 if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
727 if (kdf_nid == NID_dh_std_kdf)
729 else if (kdf_nid == NID_dh_cofactor_kdf)
734 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
737 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_62) <= 0)
740 kdf_md = EVP_get_digestbynid(kdfmd_nid);
744 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
749 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
753 X509_ALGOR *alg, *kekalg = NULL;
754 ASN1_OCTET_STRING *ukm;
755 const unsigned char *p;
756 unsigned char *der = NULL;
758 const EVP_CIPHER *kekcipher;
759 EVP_CIPHER_CTX *kekctx;
761 if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
764 if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
765 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
769 if (alg->parameter->type != V_ASN1_SEQUENCE)
772 p = alg->parameter->value.sequence->data;
773 plen = alg->parameter->value.sequence->length;
774 kekalg = d2i_X509_ALGOR(NULL, &p, plen);
777 kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
780 kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
781 if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
783 if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
785 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
788 keylen = EVP_CIPHER_CTX_key_length(kekctx);
789 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
792 plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
797 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
803 X509_ALGOR_free(kekalg);
808 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
811 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
814 /* See if we need to set peer key */
815 if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
817 ASN1_BIT_STRING *pubkey;
818 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
823 if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
824 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
828 /* Set ECDH derivation parameters and initialise unwrap context */
829 if (!ecdh_cms_set_shared_info(pctx, ri)) {
830 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
836 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
842 X509_ALGOR *talg, *wrap_alg = NULL;
844 ASN1_BIT_STRING *pubkey;
845 ASN1_STRING *wrap_str;
846 ASN1_OCTET_STRING *ukm;
847 unsigned char *penc = NULL;
850 int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
851 const EVP_MD *kdf_md;
852 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
855 /* Get ephemeral key */
856 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
857 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
860 X509_ALGOR_get0(&aoid, NULL, NULL, talg);
861 /* Is everything uninitialised? */
862 if (aoid == OBJ_nid2obj(NID_undef)) {
864 EC_KEY *eckey = pkey->pkey.ec;
868 penclen = i2o_ECPublicKey(eckey, NULL);
871 penc = OPENSSL_malloc(penclen);
875 penclen = i2o_ECPublicKey(eckey, &p);
878 ASN1_STRING_set0(pubkey, penc, penclen);
879 pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
880 pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
883 X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
887 /* See if custom paraneters set */
888 kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
891 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
893 ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
896 else if (ecdh_nid == 0)
897 ecdh_nid = NID_dh_std_kdf;
898 else if (ecdh_nid == 1)
899 ecdh_nid = NID_dh_cofactor_kdf;
901 if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
902 kdf_type = EVP_PKEY_ECDH_KDF_X9_62;
903 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
908 if (kdf_md == NULL) {
909 /* Fixme later for better MD */
911 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
915 if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
918 /* Lookup NID for KDF+cofactor+digest */
920 if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
923 ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
924 wrap_nid = EVP_CIPHER_CTX_type(ctx);
925 keylen = EVP_CIPHER_CTX_key_length(ctx);
927 /* Package wrap algorithm in an AlgorithmIdentifier */
929 wrap_alg = X509_ALGOR_new();
930 if (wrap_alg == NULL)
932 wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
933 wrap_alg->parameter = ASN1_TYPE_new();
934 if (wrap_alg->parameter == NULL)
936 if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
938 if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
939 ASN1_TYPE_free(wrap_alg->parameter);
940 wrap_alg->parameter = NULL;
943 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
946 penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
951 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
956 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
957 * of another AlgorithmIdentifier.
959 penclen = i2d_X509_ALGOR(wrap_alg, &penc);
960 if (!penc || !penclen)
962 wrap_str = ASN1_STRING_new();
963 if (wrap_str == NULL)
965 ASN1_STRING_set0(wrap_str, penc, penclen);
967 X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
973 X509_ALGOR_free(wrap_alg);