1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 /* ====================================================================
5 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
60 #include <openssl/x509.h>
61 #include <openssl/ec.h>
62 #include <openssl/bn.h>
63 #ifndef OPENSSL_NO_CMS
64 #include <openssl/cms.h>
66 #include <openssl/asn1t.h>
67 #include "asn1_locl.h"
69 static int ecdh_cms_decrypt(CMS_RecipientInfo
*ri
);
70 static int ecdh_cms_encrypt(CMS_RecipientInfo
*ri
);
72 static int eckey_param2type(int *pptype
, void **ppval
, EC_KEY
*ec_key
)
74 const EC_GROUP
*group
;
76 if (ec_key
== NULL
|| (group
= EC_KEY_get0_group(ec_key
)) == NULL
)
78 ECerr(EC_F_ECKEY_PARAM2TYPE
, EC_R_MISSING_PARAMETERS
);
81 if (EC_GROUP_get_asn1_flag(group
)
82 && (nid
= EC_GROUP_get_curve_name(group
)))
83 /* we have a 'named curve' => just set the OID */
85 *ppval
= OBJ_nid2obj(nid
);
86 *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)
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
))
117 ECerr(EC_F_ECKEY_PUB_ENCODE
, ERR_R_EC_LIB
);
120 penclen
= i2o_ECPublicKey(ec_key
, NULL
);
123 penc
= OPENSSL_malloc(penclen
);
127 penclen
= i2o_ECPublicKey(ec_key
, &p
);
130 if (X509_PUBKEY_set0_param(pk
, OBJ_nid2obj(EVP_PKEY_EC
),
131 ptype
, pval
, penc
, penclen
))
134 if (ptype
== V_ASN1_OBJECT
)
135 ASN1_OBJECT_free(pval
);
137 ASN1_STRING_free(pval
);
143 static EC_KEY
*eckey_type2param(int ptype
, void *pval
)
145 EC_KEY
*eckey
= NULL
;
146 if (ptype
== V_ASN1_SEQUENCE
)
148 ASN1_STRING
*pstr
= pval
;
149 const unsigned char *pm
= NULL
;
152 pmlen
= pstr
->length
;
153 if (!(eckey
= d2i_ECParameters(NULL
, &pm
, pmlen
)))
155 ECerr(EC_F_ECKEY_TYPE2PARAM
, EC_R_DECODE_ERROR
);
159 else if (ptype
== V_ASN1_OBJECT
)
161 ASN1_OBJECT
*poid
= pval
;
164 /* type == V_ASN1_OBJECT => the parameters are given
167 if ((eckey
= EC_KEY_new()) == NULL
)
169 ECerr(EC_F_ECKEY_TYPE2PARAM
, ERR_R_MALLOC_FAILURE
);
172 group
= EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid
));
175 EC_GROUP_set_asn1_flag(group
, OPENSSL_EC_NAMED_CURVE
);
176 if (EC_KEY_set_group(eckey
, group
) == 0)
178 EC_GROUP_free(group
);
182 ECerr(EC_F_ECKEY_TYPE2PARAM
, EC_R_DECODE_ERROR
);
194 static int eckey_pub_decode(EVP_PKEY
*pkey
, X509_PUBKEY
*pubkey
)
196 const unsigned char *p
= NULL
;
199 EC_KEY
*eckey
= NULL
;
202 if (!X509_PUBKEY_get0_param(NULL
, &p
, &pklen
, &palg
, pubkey
))
204 X509_ALGOR_get0(NULL
, &ptype
, &pval
, palg
);
206 eckey
= eckey_type2param(ptype
, pval
);
210 ECerr(EC_F_ECKEY_PUB_DECODE
, ERR_R_EC_LIB
);
214 /* We have parameters now set public key */
215 if (!o2i_ECPublicKey(&eckey
, &p
, pklen
))
217 ECerr(EC_F_ECKEY_PUB_DECODE
, EC_R_DECODE_ERROR
);
221 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
230 static int eckey_pub_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
233 const EC_GROUP
*group
= EC_KEY_get0_group(b
->pkey
.ec
);
234 const EC_POINT
*pa
= EC_KEY_get0_public_key(a
->pkey
.ec
),
235 *pb
= EC_KEY_get0_public_key(b
->pkey
.ec
);
236 r
= EC_POINT_cmp(group
, pa
, pb
, NULL
);
244 static int eckey_priv_decode(EVP_PKEY
*pkey
, PKCS8_PRIV_KEY_INFO
*p8
)
246 const unsigned char *p
= NULL
;
249 EC_KEY
*eckey
= NULL
;
252 if (!PKCS8_pkey_get0(NULL
, &p
, &pklen
, &palg
, p8
))
254 X509_ALGOR_get0(NULL
, &ptype
, &pval
, palg
);
256 eckey
= eckey_type2param(ptype
, pval
);
261 /* We have parameters now set private key */
262 if (!d2i_ECPrivateKey(&eckey
, &p
, pklen
))
264 ECerr(EC_F_ECKEY_PRIV_DECODE
, EC_R_DECODE_ERROR
);
268 /* calculate public key (if necessary) */
269 if (EC_KEY_get0_public_key(eckey
) == NULL
)
271 const BIGNUM
*priv_key
;
272 const EC_GROUP
*group
;
274 /* the public key was not included in the SEC1 private
275 * key => calculate the public key */
276 group
= EC_KEY_get0_group(eckey
);
277 pub_key
= EC_POINT_new(group
);
280 ECerr(EC_F_ECKEY_PRIV_DECODE
, ERR_R_EC_LIB
);
283 if (!EC_POINT_copy(pub_key
, EC_GROUP_get0_generator(group
)))
285 EC_POINT_free(pub_key
);
286 ECerr(EC_F_ECKEY_PRIV_DECODE
, ERR_R_EC_LIB
);
289 priv_key
= EC_KEY_get0_private_key(eckey
);
290 if (!EC_POINT_mul(group
, pub_key
, priv_key
, NULL
, NULL
, NULL
))
292 EC_POINT_free(pub_key
);
293 ECerr(EC_F_ECKEY_PRIV_DECODE
, ERR_R_EC_LIB
);
296 if (EC_KEY_set_public_key(eckey
, pub_key
) == 0)
298 EC_POINT_free(pub_key
);
299 ECerr(EC_F_ECKEY_PRIV_DECODE
, ERR_R_EC_LIB
);
302 EC_POINT_free(pub_key
);
305 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
309 ECerr(EC_F_ECKEY_PRIV_DECODE
, ERR_R_EC_LIB
);
316 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO
*p8
, const EVP_PKEY
*pkey
)
319 unsigned char *ep
, *p
;
322 unsigned int tmp_flags
, old_flags
;
324 ec_key
= pkey
->pkey
.ec
;
326 if (!eckey_param2type(&ptype
, &pval
, ec_key
))
328 ECerr(EC_F_ECKEY_PRIV_ENCODE
, EC_R_DECODE_ERROR
);
332 /* set the private key */
334 /* do not include the parameters in the SEC1 private key
335 * see PKCS#11 12.11 */
336 old_flags
= EC_KEY_get_enc_flags(ec_key
);
337 tmp_flags
= old_flags
| EC_PKEY_NO_PARAMETERS
;
338 EC_KEY_set_enc_flags(ec_key
, tmp_flags
);
339 eplen
= i2d_ECPrivateKey(ec_key
, NULL
);
342 EC_KEY_set_enc_flags(ec_key
, old_flags
);
343 ECerr(EC_F_ECKEY_PRIV_ENCODE
, ERR_R_EC_LIB
);
346 ep
= (unsigned char *) OPENSSL_malloc(eplen
);
349 EC_KEY_set_enc_flags(ec_key
, old_flags
);
350 ECerr(EC_F_ECKEY_PRIV_ENCODE
, ERR_R_MALLOC_FAILURE
);
354 if (!i2d_ECPrivateKey(ec_key
, &p
))
356 EC_KEY_set_enc_flags(ec_key
, old_flags
);
358 ECerr(EC_F_ECKEY_PRIV_ENCODE
, ERR_R_EC_LIB
);
360 /* restore old encoding flags */
361 EC_KEY_set_enc_flags(ec_key
, old_flags
);
363 if (!PKCS8_pkey_set0(p8
, OBJ_nid2obj(NID_X9_62_id_ecPublicKey
), 0,
364 ptype
, pval
, ep
, eplen
))
370 static int int_ec_size(const EVP_PKEY
*pkey
)
372 return ECDSA_size(pkey
->pkey
.ec
);
375 static int ec_bits(const EVP_PKEY
*pkey
)
377 BIGNUM
*order
= BN_new();
378 const EC_GROUP
*group
;
386 group
= EC_KEY_get0_group(pkey
->pkey
.ec
);
387 if (!EC_GROUP_get_order(group
, order
, NULL
))
393 ret
= BN_num_bits(order
);
398 static int ec_missing_parameters(const EVP_PKEY
*pkey
)
400 if (EC_KEY_get0_group(pkey
->pkey
.ec
) == NULL
)
405 static int ec_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
407 EC_GROUP
*group
= EC_GROUP_dup(EC_KEY_get0_group(from
->pkey
.ec
));
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
;
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
)
445 reason
= ERR_R_PASSED_NULL_PARAMETER
;
452 reason
= ERR_R_MALLOC_FAILURE
;
458 public_key
= EC_KEY_get0_public_key(x
);
459 if ((pub_key
= EC_POINT_point2bn(group
, public_key
,
460 EC_KEY_get_conv_form(x
), NULL
, ctx
)) == NULL
)
462 reason
= ERR_R_EC_LIB
;
466 buf_len
= (size_t)BN_num_bytes(pub_key
);
471 priv_key
= EC_KEY_get0_private_key(x
);
472 if (priv_key
&& (i
= (size_t)BN_num_bytes(priv_key
)) > buf_len
)
481 if ((buffer
= OPENSSL_malloc(buf_len
)) == NULL
)
483 reason
= ERR_R_MALLOC_FAILURE
;
488 ecstr
= "Private-Key";
490 ecstr
= "Public-Key";
492 ecstr
= "ECDSA-Parameters";
494 if (!BIO_indent(bp
, off
, 128))
496 if ((order
= BN_new()) == NULL
)
498 if (!EC_GROUP_get_order(group
, order
, NULL
))
500 if (BIO_printf(bp
, "%s: (%d bit)\n", ecstr
,
501 BN_num_bits(order
)) <= 0) goto err
;
503 if ((priv_key
!= NULL
) && !ASN1_bn_print(bp
, "priv:", priv_key
,
506 if ((pub_key
!= NULL
) && !ASN1_bn_print(bp
, "pub: ", pub_key
,
509 if (!ECPKParameters_print(bp
, group
, off
))
514 ECerr(EC_F_DO_EC_KEY_PRINT
, reason
);
522 OPENSSL_free(buffer
);
526 static int eckey_param_decode(EVP_PKEY
*pkey
,
527 const unsigned char **pder
, int derlen
)
530 if (!(eckey
= d2i_ECParameters(NULL
, pder
, derlen
)))
532 ECerr(EC_F_ECKEY_PARAM_DECODE
, ERR_R_EC_LIB
);
535 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
539 static int eckey_param_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
541 return i2d_ECParameters(pkey
->pkey
.ec
, pder
);
544 static int eckey_param_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
547 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, 0);
550 static int eckey_pub_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
553 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, 1);
557 static int eckey_priv_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
560 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, 2);
563 static int old_ec_priv_decode(EVP_PKEY
*pkey
,
564 const unsigned char **pder
, int derlen
)
567 if (!(ec
= d2i_ECPrivateKey (NULL
, pder
, derlen
)))
569 ECerr(EC_F_OLD_EC_PRIV_DECODE
, EC_R_DECODE_ERROR
);
572 EVP_PKEY_assign_EC_KEY(pkey
, ec
);
576 static int old_ec_priv_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
578 return i2d_ECPrivateKey(pkey
->pkey
.ec
, pder
);
581 static int ec_pkey_ctrl(EVP_PKEY
*pkey
, int op
, long arg1
, void *arg2
)
585 case ASN1_PKEY_CTRL_PKCS7_SIGN
:
589 X509_ALGOR
*alg1
, *alg2
;
590 PKCS7_SIGNER_INFO_get0_algs(arg2
, NULL
, &alg1
, &alg2
);
591 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
593 hnid
= OBJ_obj2nid(alg1
->algorithm
);
594 if (hnid
== NID_undef
)
596 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
598 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
601 #ifndef OPENSSL_NO_CMS
602 case ASN1_PKEY_CTRL_CMS_SIGN
:
606 X509_ALGOR
*alg1
, *alg2
;
607 CMS_SignerInfo_get0_algs(arg2
, NULL
, NULL
,
609 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
611 hnid
= OBJ_obj2nid(alg1
->algorithm
);
612 if (hnid
== NID_undef
)
614 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
616 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
620 case ASN1_PKEY_CTRL_CMS_ENVELOPE
:
622 return ecdh_cms_decrypt(arg2
);
624 return ecdh_cms_encrypt(arg2
);
627 case ASN1_PKEY_CTRL_CMS_RI_TYPE
:
628 *(int *)arg2
= CMS_RECIPINFO_AGREE
;
632 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
633 *(int *)arg2
= NID_sha1
;
643 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth
=
649 "OpenSSL EC algorithm",
665 ec_missing_parameters
,
677 #ifndef OPENSSL_NO_CMS
679 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX
*pctx
,
680 X509_ALGOR
*alg
, ASN1_BIT_STRING
*pubkey
)
686 EVP_PKEY
*pkpeer
= NULL
;
687 EC_KEY
*ecpeer
= NULL
;
688 const unsigned char *p
;
690 X509_ALGOR_get0(&aoid
, &atype
, &aval
, alg
);
691 if (OBJ_obj2nid(aoid
) != NID_X9_62_id_ecPublicKey
)
693 /* If absent parameters get group from main key */
694 if (atype
== V_ASN1_UNDEF
|| atype
== V_ASN1_NULL
)
698 pk
= EVP_PKEY_CTX_get0_pkey(pctx
);
701 grp
= EC_KEY_get0_group(pk
->pkey
.ec
);
702 ecpeer
= EC_KEY_new();
705 if (!EC_KEY_set_group(ecpeer
, grp
))
710 ecpeer
= eckey_type2param(atype
, aval
);
714 /* We have parameters now set public key */
715 plen
= ASN1_STRING_length(pubkey
);
716 p
= ASN1_STRING_data(pubkey
);
719 if (!o2i_ECPublicKey(&ecpeer
, &p
, plen
))
721 pkpeer
= EVP_PKEY_new();
724 EVP_PKEY_set1_EC_KEY(pkpeer
, ecpeer
);
725 if (EVP_PKEY_derive_set_peer(pctx
, pkpeer
) > 0)
731 EVP_PKEY_free(pkpeer
);
734 /* Set KDF parameters based on KDF NID */
735 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX
*pctx
, int eckdf_nid
)
737 int kdf_nid
, kdfmd_nid
, cofactor
;
738 const EVP_MD
*kdf_md
;
739 if (eckdf_nid
== NID_undef
)
742 /* Lookup KDF type, cofactor mode and digest */
743 if (!OBJ_find_sigid_algs(eckdf_nid
, &kdfmd_nid
, &kdf_nid
))
746 if (kdf_nid
== NID_dh_std_kdf
)
748 else if (kdf_nid
== NID_dh_cofactor_kdf
)
752 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx
, cofactor
) <= 0)
755 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, EVP_PKEY_ECDH_KDF_X9_62
) <= 0)
758 kdf_md
= EVP_get_digestbynid(kdfmd_nid
);
762 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
767 /* Utilities to encode the ECC_CMS_SharedInfo structure used during key
773 ASN1_OCTET_STRING
*entityUInfo
;
774 ASN1_OCTET_STRING
*suppPubInfo
;
775 } ECC_CMS_SharedInfo
;
777 ASN1_SEQUENCE(ECC_CMS_SharedInfo
) = {
778 ASN1_SIMPLE(ECC_CMS_SharedInfo
, keyInfo
, X509_ALGOR
),
779 ASN1_EXP_OPT(ECC_CMS_SharedInfo
, entityUInfo
, ASN1_OCTET_STRING
, 0),
780 ASN1_EXP_OPT(ECC_CMS_SharedInfo
, suppPubInfo
, ASN1_OCTET_STRING
, 2),
781 } ASN1_SEQUENCE_END(ECC_CMS_SharedInfo
)
783 static int ecdh_cms_set_ukm(EVP_PKEY_CTX
*pctx
,
785 ASN1_OCTET_STRING
*ukm
,
789 ECC_CMS_SharedInfo
*pecsi
;
793 unsigned char *der
= NULL
;
795 ASN1_OCTET_STRING oklen
;
797 ECC_CMS_SharedInfo ecsi
;
800 kl
[0] = (keylen
>> 24) & 0xff;
801 kl
[1] = (keylen
>> 16) & 0xff;
802 kl
[2] = (keylen
>> 8) & 0xff;
803 kl
[3] = keylen
& 0xff;
806 oklen
.type
= V_ASN1_OCTET_STRING
;
808 ecsi
.keyInfo
= kekalg
;
809 ecsi
.entityUInfo
= ukm
;
810 ecsi
.suppPubInfo
= &oklen
;
812 plen
= ASN1_item_i2d(intsi
.a
, &der
, ASN1_ITEM_rptr(ECC_CMS_SharedInfo
));
815 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, der
, plen
) <= 0)
824 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX
*pctx
, CMS_RecipientInfo
*ri
)
828 X509_ALGOR
*alg
, *kekalg
= NULL
;
829 ASN1_OCTET_STRING
*ukm
;
830 const unsigned char *p
;
832 const EVP_CIPHER
*kekcipher
;
833 EVP_CIPHER_CTX
*kekctx
;
835 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &alg
, &ukm
))
838 if (!ecdh_cms_set_kdf_param(pctx
, OBJ_obj2nid(alg
->algorithm
)))
840 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO
, EC_R_KDF_PARAMETER_ERROR
);
844 if (alg
->parameter
->type
!= V_ASN1_SEQUENCE
)
847 p
= alg
->parameter
->value
.sequence
->data
;
848 plen
= alg
->parameter
->value
.sequence
->length
;
849 kekalg
= d2i_X509_ALGOR(NULL
, &p
, plen
);
852 kekctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
855 kekcipher
= EVP_get_cipherbyobj(kekalg
->algorithm
);
856 if (!kekcipher
|| EVP_CIPHER_mode(kekcipher
) != EVP_CIPH_WRAP_MODE
)
858 if (!EVP_EncryptInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
))
861 keylen
= EVP_CIPHER_CTX_key_length(kekctx
);
862 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
865 if (!ecdh_cms_set_ukm(pctx
, kekalg
, ukm
, keylen
))
871 X509_ALGOR_free(kekalg
);
875 static int ecdh_cms_decrypt(CMS_RecipientInfo
*ri
)
878 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
881 /* See if we need to set peer key */
882 if (!EVP_PKEY_CTX_get0_peerkey(pctx
))
885 ASN1_BIT_STRING
*pubkey
;
886 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &alg
, &pubkey
,
891 if (!ecdh_cms_set_peerkey(pctx
, alg
, pubkey
))
893 ECerr(EC_F_ECDH_CMS_DECRYPT
, EC_R_PEER_KEY_ERROR
);
897 /* Set ECDH derivation parameters and initialise unwrap context */
898 if (!ecdh_cms_set_shared_info(pctx
, ri
))
900 ECerr(EC_F_ECDH_CMS_DECRYPT
, EC_R_SHARED_INFO_ERROR
);
906 static int ecdh_cms_encrypt(CMS_RecipientInfo
*ri
)
912 X509_ALGOR
*talg
, *wrap_alg
= NULL
;
914 ASN1_BIT_STRING
*pubkey
;
915 ASN1_STRING
*wrap_str
;
916 ASN1_OCTET_STRING
*ukm
;
917 unsigned char *penc
= NULL
;
920 int ecdh_nid
, kdf_type
, kdf_nid
, wrap_nid
;
921 const EVP_MD
*kdf_md
;
922 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
925 /* Get ephemeral key */
926 pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
927 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &talg
, &pubkey
,
930 X509_ALGOR_get0(&aoid
, NULL
, NULL
, talg
);
931 /* Is everything uninitialised? */
932 if (aoid
== OBJ_nid2obj(NID_undef
))
935 EC_KEY
*eckey
= pkey
->pkey
.ec
;
939 penclen
= i2o_ECPublicKey(eckey
, NULL
);
942 penc
= OPENSSL_malloc(penclen
);
946 penclen
= i2o_ECPublicKey(eckey
, &p
);
949 ASN1_STRING_set0(pubkey
, penc
, penclen
);
950 pubkey
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
|0x07);
951 pubkey
->flags
|=ASN1_STRING_FLAG_BITS_LEFT
;
954 X509_ALGOR_set0(talg
, OBJ_nid2obj(NID_X9_62_id_ecPublicKey
),
958 /* See if custom paraneters set */
959 kdf_type
= EVP_PKEY_CTX_get_ecdh_kdf_type(pctx
);
962 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx
, &kdf_md
))
964 ecdh_nid
= EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx
);
967 else if (ecdh_nid
== 0)
968 ecdh_nid
= NID_dh_std_kdf
;
969 else if (ecdh_nid
== 1)
970 ecdh_nid
= NID_dh_cofactor_kdf
;
972 if (kdf_type
== EVP_PKEY_ECDH_KDF_NONE
)
974 kdf_type
= EVP_PKEY_ECDH_KDF_X9_62
;
975 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, kdf_type
) <= 0)
983 /* Fixme later for better MD */
985 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
989 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &talg
, &ukm
))
992 /* Lookup NID for KDF+cofactor+digest */
994 if (!OBJ_find_sigid_by_algs(&kdf_nid
, EVP_MD_type(kdf_md
), ecdh_nid
))
997 ctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
998 wrap_nid
= EVP_CIPHER_CTX_type(ctx
);
999 keylen
= EVP_CIPHER_CTX_key_length(ctx
);
1001 /* Package wrap algorithm in an AlgorithmIdentifier */
1003 wrap_alg
= X509_ALGOR_new();
1006 X509_ALGOR_set0(wrap_alg
, OBJ_nid2obj(wrap_nid
), V_ASN1_UNDEF
, NULL
);
1008 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
1010 if (!ecdh_cms_set_ukm(pctx
, wrap_alg
, ukm
, keylen
))
1013 /* Now need to wrap encoding of wrap AlgorithmIdentifier into
1014 * parameter of another AlgorithmIdentifier.
1017 penclen
= i2d_X509_ALGOR(wrap_alg
, &penc
);
1018 if (!penc
|| !penclen
)
1020 wrap_str
= ASN1_STRING_new();
1023 ASN1_STRING_set0(wrap_str
, penc
, penclen
);
1025 X509_ALGOR_set0(talg
, OBJ_nid2obj(kdf_nid
), V_ASN1_SEQUENCE
, wrap_str
);
1033 X509_ALGOR_free(wrap_alg
);