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 (EC_KEY_set_group(to
->pkey
.ec
, group
) == 0)
407 EC_GROUP_free(group
);
411 static int ec_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
413 const EC_GROUP
*group_a
= EC_KEY_get0_group(a
->pkey
.ec
),
414 *group_b
= EC_KEY_get0_group(b
->pkey
.ec
);
415 if (EC_GROUP_cmp(group_a
, group_b
, NULL
))
421 static void int_ec_free(EVP_PKEY
*pkey
)
423 EC_KEY_free(pkey
->pkey
.ec
);
426 static int do_EC_KEY_print(BIO
*bp
, const EC_KEY
*x
, int off
, int ktype
)
428 unsigned char *buffer
= NULL
;
430 size_t buf_len
= 0, i
;
431 int ret
= 0, reason
= ERR_R_BIO_LIB
;
432 BIGNUM
*pub_key
= NULL
, *order
= NULL
;
434 const EC_GROUP
*group
;
435 const EC_POINT
*public_key
;
436 const BIGNUM
*priv_key
;
438 if (x
== NULL
|| (group
= EC_KEY_get0_group(x
)) == NULL
) {
439 reason
= ERR_R_PASSED_NULL_PARAMETER
;
445 reason
= ERR_R_MALLOC_FAILURE
;
450 public_key
= EC_KEY_get0_public_key(x
);
451 if (public_key
!= NULL
) {
452 if ((pub_key
= EC_POINT_point2bn(group
, public_key
,
453 EC_KEY_get_conv_form(x
), NULL
,
455 reason
= ERR_R_EC_LIB
;
458 buf_len
= (size_t)BN_num_bytes(pub_key
);
463 priv_key
= EC_KEY_get0_private_key(x
);
464 if (priv_key
&& (i
= (size_t)BN_num_bytes(priv_key
)) > buf_len
)
471 if ((buffer
= OPENSSL_malloc(buf_len
)) == NULL
) {
472 reason
= ERR_R_MALLOC_FAILURE
;
477 ecstr
= "Private-Key";
479 ecstr
= "Public-Key";
481 ecstr
= "ECDSA-Parameters";
483 if (!BIO_indent(bp
, off
, 128))
485 if ((order
= BN_new()) == NULL
)
487 if (!EC_GROUP_get_order(group
, order
, NULL
))
489 if (BIO_printf(bp
, "%s: (%d bit)\n", ecstr
, BN_num_bits(order
)) <= 0)
492 if ((priv_key
!= NULL
) && !ASN1_bn_print(bp
, "priv:", priv_key
,
495 if ((pub_key
!= NULL
) && !ASN1_bn_print(bp
, "pub: ", pub_key
,
498 if (!ECPKParameters_print(bp
, group
, off
))
503 ECerr(EC_F_DO_EC_KEY_PRINT
, reason
);
507 OPENSSL_free(buffer
);
511 static int eckey_param_decode(EVP_PKEY
*pkey
,
512 const unsigned char **pder
, int derlen
)
516 if ((eckey
= d2i_ECParameters(NULL
, pder
, derlen
)) == NULL
) {
517 ECerr(EC_F_ECKEY_PARAM_DECODE
, ERR_R_EC_LIB
);
520 EVP_PKEY_assign_EC_KEY(pkey
, eckey
);
524 static int eckey_param_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
526 return i2d_ECParameters(pkey
->pkey
.ec
, pder
);
529 static int eckey_param_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
532 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, 0);
535 static int eckey_pub_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
538 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, 1);
541 static int eckey_priv_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
544 return do_EC_KEY_print(bp
, pkey
->pkey
.ec
, indent
, 2);
547 static int old_ec_priv_decode(EVP_PKEY
*pkey
,
548 const unsigned char **pder
, int derlen
)
552 if ((ec
= d2i_ECPrivateKey(NULL
, pder
, derlen
)) == NULL
) {
553 ECerr(EC_F_OLD_EC_PRIV_DECODE
, EC_R_DECODE_ERROR
);
556 EVP_PKEY_assign_EC_KEY(pkey
, ec
);
560 static int old_ec_priv_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
562 return i2d_ECPrivateKey(pkey
->pkey
.ec
, pder
);
565 static int ec_pkey_ctrl(EVP_PKEY
*pkey
, int op
, long arg1
, void *arg2
)
568 case ASN1_PKEY_CTRL_PKCS7_SIGN
:
571 X509_ALGOR
*alg1
, *alg2
;
572 PKCS7_SIGNER_INFO_get0_algs(arg2
, NULL
, &alg1
, &alg2
);
573 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
575 hnid
= OBJ_obj2nid(alg1
->algorithm
);
576 if (hnid
== NID_undef
)
578 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
580 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
583 #ifndef OPENSSL_NO_CMS
584 case ASN1_PKEY_CTRL_CMS_SIGN
:
587 X509_ALGOR
*alg1
, *alg2
;
588 CMS_SignerInfo_get0_algs(arg2
, NULL
, NULL
, &alg1
, &alg2
);
589 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
591 hnid
= OBJ_obj2nid(alg1
->algorithm
);
592 if (hnid
== NID_undef
)
594 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
596 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
600 case ASN1_PKEY_CTRL_CMS_ENVELOPE
:
602 return ecdh_cms_decrypt(arg2
);
604 return ecdh_cms_encrypt(arg2
);
607 case ASN1_PKEY_CTRL_CMS_RI_TYPE
:
608 *(int *)arg2
= CMS_RECIPINFO_AGREE
;
612 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
613 *(int *)arg2
= NID_sha256
;
623 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth
= {
628 "OpenSSL EC algorithm",
645 ec_missing_parameters
,
657 #ifndef OPENSSL_NO_CMS
659 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX
*pctx
,
660 X509_ALGOR
*alg
, ASN1_BIT_STRING
*pubkey
)
666 EVP_PKEY
*pkpeer
= NULL
;
667 EC_KEY
*ecpeer
= NULL
;
668 const unsigned char *p
;
670 X509_ALGOR_get0(&aoid
, &atype
, &aval
, alg
);
671 if (OBJ_obj2nid(aoid
) != NID_X9_62_id_ecPublicKey
)
673 /* If absent parameters get group from main key */
674 if (atype
== V_ASN1_UNDEF
|| atype
== V_ASN1_NULL
) {
677 pk
= EVP_PKEY_CTX_get0_pkey(pctx
);
680 grp
= EC_KEY_get0_group(pk
->pkey
.ec
);
681 ecpeer
= EC_KEY_new();
684 if (!EC_KEY_set_group(ecpeer
, grp
))
687 ecpeer
= eckey_type2param(atype
, aval
);
691 /* We have parameters now set public key */
692 plen
= ASN1_STRING_length(pubkey
);
693 p
= ASN1_STRING_data(pubkey
);
696 if (!o2i_ECPublicKey(&ecpeer
, &p
, plen
))
698 pkpeer
= EVP_PKEY_new();
701 EVP_PKEY_set1_EC_KEY(pkpeer
, ecpeer
);
702 if (EVP_PKEY_derive_set_peer(pctx
, pkpeer
) > 0)
706 EVP_PKEY_free(pkpeer
);
710 /* Set KDF parameters based on KDF NID */
711 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX
*pctx
, int eckdf_nid
)
713 int kdf_nid
, kdfmd_nid
, cofactor
;
714 const EVP_MD
*kdf_md
;
715 if (eckdf_nid
== NID_undef
)
718 /* Lookup KDF type, cofactor mode and digest */
719 if (!OBJ_find_sigid_algs(eckdf_nid
, &kdfmd_nid
, &kdf_nid
))
722 if (kdf_nid
== NID_dh_std_kdf
)
724 else if (kdf_nid
== NID_dh_cofactor_kdf
)
729 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx
, cofactor
) <= 0)
732 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, EVP_PKEY_ECDH_KDF_X9_62
) <= 0)
735 kdf_md
= EVP_get_digestbynid(kdfmd_nid
);
739 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
744 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX
*pctx
, CMS_RecipientInfo
*ri
)
748 X509_ALGOR
*alg
, *kekalg
= NULL
;
749 ASN1_OCTET_STRING
*ukm
;
750 const unsigned char *p
;
751 unsigned char *der
= NULL
;
753 const EVP_CIPHER
*kekcipher
;
754 EVP_CIPHER_CTX
*kekctx
;
756 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &alg
, &ukm
))
759 if (!ecdh_cms_set_kdf_param(pctx
, OBJ_obj2nid(alg
->algorithm
))) {
760 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO
, EC_R_KDF_PARAMETER_ERROR
);
764 if (alg
->parameter
->type
!= V_ASN1_SEQUENCE
)
767 p
= alg
->parameter
->value
.sequence
->data
;
768 plen
= alg
->parameter
->value
.sequence
->length
;
769 kekalg
= d2i_X509_ALGOR(NULL
, &p
, plen
);
772 kekctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
775 kekcipher
= EVP_get_cipherbyobj(kekalg
->algorithm
);
776 if (!kekcipher
|| EVP_CIPHER_mode(kekcipher
) != EVP_CIPH_WRAP_MODE
)
778 if (!EVP_EncryptInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
))
780 if (EVP_CIPHER_asn1_to_param(kekctx
, kekalg
->parameter
) <= 0)
783 keylen
= EVP_CIPHER_CTX_key_length(kekctx
);
784 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
787 plen
= CMS_SharedInfo_encode(&der
, kekalg
, ukm
, keylen
);
792 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, der
, plen
) <= 0)
798 X509_ALGOR_free(kekalg
);
803 static int ecdh_cms_decrypt(CMS_RecipientInfo
*ri
)
806 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
809 /* See if we need to set peer key */
810 if (!EVP_PKEY_CTX_get0_peerkey(pctx
)) {
812 ASN1_BIT_STRING
*pubkey
;
813 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &alg
, &pubkey
,
818 if (!ecdh_cms_set_peerkey(pctx
, alg
, pubkey
)) {
819 ECerr(EC_F_ECDH_CMS_DECRYPT
, EC_R_PEER_KEY_ERROR
);
823 /* Set ECDH derivation parameters and initialise unwrap context */
824 if (!ecdh_cms_set_shared_info(pctx
, ri
)) {
825 ECerr(EC_F_ECDH_CMS_DECRYPT
, EC_R_SHARED_INFO_ERROR
);
831 static int ecdh_cms_encrypt(CMS_RecipientInfo
*ri
)
837 X509_ALGOR
*talg
, *wrap_alg
= NULL
;
839 ASN1_BIT_STRING
*pubkey
;
840 ASN1_STRING
*wrap_str
;
841 ASN1_OCTET_STRING
*ukm
;
842 unsigned char *penc
= NULL
;
845 int ecdh_nid
, kdf_type
, kdf_nid
, wrap_nid
;
846 const EVP_MD
*kdf_md
;
847 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
850 /* Get ephemeral key */
851 pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
852 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &talg
, &pubkey
,
855 X509_ALGOR_get0(&aoid
, NULL
, NULL
, talg
);
856 /* Is everything uninitialised? */
857 if (aoid
== OBJ_nid2obj(NID_undef
)) {
859 EC_KEY
*eckey
= pkey
->pkey
.ec
;
863 penclen
= i2o_ECPublicKey(eckey
, NULL
);
866 penc
= OPENSSL_malloc(penclen
);
870 penclen
= i2o_ECPublicKey(eckey
, &p
);
873 ASN1_STRING_set0(pubkey
, penc
, penclen
);
874 pubkey
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
875 pubkey
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
878 X509_ALGOR_set0(talg
, OBJ_nid2obj(NID_X9_62_id_ecPublicKey
),
882 /* See if custom paraneters set */
883 kdf_type
= EVP_PKEY_CTX_get_ecdh_kdf_type(pctx
);
886 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx
, &kdf_md
))
888 ecdh_nid
= EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx
);
891 else if (ecdh_nid
== 0)
892 ecdh_nid
= NID_dh_std_kdf
;
893 else if (ecdh_nid
== 1)
894 ecdh_nid
= NID_dh_cofactor_kdf
;
896 if (kdf_type
== EVP_PKEY_ECDH_KDF_NONE
) {
897 kdf_type
= EVP_PKEY_ECDH_KDF_X9_62
;
898 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, kdf_type
) <= 0)
903 if (kdf_md
== NULL
) {
904 /* Fixme later for better MD */
906 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
910 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &talg
, &ukm
))
913 /* Lookup NID for KDF+cofactor+digest */
915 if (!OBJ_find_sigid_by_algs(&kdf_nid
, EVP_MD_type(kdf_md
), ecdh_nid
))
918 ctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
919 wrap_nid
= EVP_CIPHER_CTX_type(ctx
);
920 keylen
= EVP_CIPHER_CTX_key_length(ctx
);
922 /* Package wrap algorithm in an AlgorithmIdentifier */
924 wrap_alg
= X509_ALGOR_new();
925 if (wrap_alg
== NULL
)
927 wrap_alg
->algorithm
= OBJ_nid2obj(wrap_nid
);
928 wrap_alg
->parameter
= ASN1_TYPE_new();
929 if (wrap_alg
->parameter
== NULL
)
931 if (EVP_CIPHER_param_to_asn1(ctx
, wrap_alg
->parameter
) <= 0)
933 if (ASN1_TYPE_get(wrap_alg
->parameter
) == NID_undef
) {
934 ASN1_TYPE_free(wrap_alg
->parameter
);
935 wrap_alg
->parameter
= NULL
;
938 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
941 penclen
= CMS_SharedInfo_encode(&penc
, wrap_alg
, ukm
, keylen
);
946 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, penc
, penclen
) <= 0)
951 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
952 * of another AlgorithmIdentifier.
954 penclen
= i2d_X509_ALGOR(wrap_alg
, &penc
);
955 if (!penc
|| !penclen
)
957 wrap_str
= ASN1_STRING_new();
958 if (wrap_str
== NULL
)
960 ASN1_STRING_set0(wrap_str
, penc
, penclen
);
962 X509_ALGOR_set0(talg
, OBJ_nid2obj(kdf_nid
), V_ASN1_SEQUENCE
, wrap_str
);
968 X509_ALGOR_free(wrap_alg
);