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 * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/x509.h>
19 #include <openssl/ec.h>
20 #include <openssl/bn.h>
21 #include <openssl/cms.h>
22 #include <openssl/asn1t.h>
23 #include "crypto/asn1.h"
24 #include "crypto/evp.h"
25 #include <openssl/core_names.h>
26 #include "internal/param_build.h"
29 #ifndef OPENSSL_NO_CMS
30 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri);
31 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri);
34 static int eckey_param2type(int *pptype, void **ppval, const EC_KEY *ec_key)
36 const EC_GROUP *group;
38 if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
39 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
42 if (EC_GROUP_get_asn1_flag(group)
43 && (nid = EC_GROUP_get_curve_name(group)))
44 /* we have a 'named curve' => just set the OID */
46 *ppval = OBJ_nid2obj(nid);
47 *pptype = V_ASN1_OBJECT;
48 } else { /* explicit parameters */
50 ASN1_STRING *pstr = NULL;
51 pstr = ASN1_STRING_new();
54 pstr->length = i2d_ECParameters(ec_key, &pstr->data);
55 if (pstr->length <= 0) {
56 ASN1_STRING_free(pstr);
57 ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
61 *pptype = V_ASN1_SEQUENCE;
66 static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
68 const EC_KEY *ec_key = pkey->pkey.ec;
71 unsigned char *penc = NULL, *p;
74 if (!eckey_param2type(&ptype, &pval, ec_key)) {
75 ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
78 penclen = i2o_ECPublicKey(ec_key, NULL);
81 penc = OPENSSL_malloc(penclen);
85 penclen = i2o_ECPublicKey(ec_key, &p);
88 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
89 ptype, pval, penc, penclen))
92 if (ptype == V_ASN1_OBJECT)
93 ASN1_OBJECT_free(pval);
95 ASN1_STRING_free(pval);
100 static EC_KEY *eckey_type2param(int ptype, const void *pval)
102 EC_KEY *eckey = NULL;
103 EC_GROUP *group = NULL;
105 if (ptype == V_ASN1_SEQUENCE) {
106 const ASN1_STRING *pstr = pval;
107 const unsigned char *pm = pstr->data;
108 int pmlen = pstr->length;
110 if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
111 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
114 } else if (ptype == V_ASN1_OBJECT) {
115 const ASN1_OBJECT *poid = pval;
118 * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
120 if ((eckey = EC_KEY_new()) == NULL) {
121 ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
124 group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
127 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
128 if (EC_KEY_set_group(eckey, group) == 0)
130 EC_GROUP_free(group);
132 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
140 EC_GROUP_free(group);
144 static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
146 const unsigned char *p = NULL;
149 EC_KEY *eckey = NULL;
152 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
154 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
156 eckey = eckey_type2param(ptype, pval);
159 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
163 /* We have parameters now set public key */
164 if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
165 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
169 EVP_PKEY_assign_EC_KEY(pkey, eckey);
177 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
180 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
181 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
182 *pb = EC_KEY_get0_public_key(b->pkey.ec);
183 if (group == NULL || pa == NULL || pb == NULL)
185 r = EC_POINT_cmp(group, pa, pb, NULL);
193 static int eckey_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
195 const unsigned char *p = NULL;
198 EC_KEY *eckey = NULL;
199 const X509_ALGOR *palg;
201 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
203 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
205 eckey = eckey_type2param(ptype, pval);
210 /* We have parameters now set private key */
211 if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
212 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
216 EVP_PKEY_assign_EC_KEY(pkey, eckey);
220 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
226 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
228 EC_KEY ec_key = *(pkey->pkey.ec);
229 unsigned char *ep, *p;
232 unsigned int old_flags;
234 if (!eckey_param2type(&ptype, &pval, &ec_key)) {
235 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
239 /* set the private key */
242 * do not include the parameters in the SEC1 private key see PKCS#11
245 old_flags = EC_KEY_get_enc_flags(&ec_key);
246 EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
248 eplen = i2d_ECPrivateKey(&ec_key, NULL);
250 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
253 ep = OPENSSL_malloc(eplen);
255 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
259 if (!i2d_ECPrivateKey(&ec_key, &p)) {
261 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
265 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
266 ptype, pval, ep, eplen)) {
274 static int int_ec_size(const EVP_PKEY *pkey)
276 return ECDSA_size(pkey->pkey.ec);
279 static int ec_bits(const EVP_PKEY *pkey)
281 return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
284 static int ec_security_bits(const EVP_PKEY *pkey)
286 int ecbits = ec_bits(pkey);
300 static int ec_missing_parameters(const EVP_PKEY *pkey)
302 if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
307 static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
309 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
313 if (to->pkey.ec == NULL) {
314 to->pkey.ec = EC_KEY_new();
315 if (to->pkey.ec == NULL)
318 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
320 EC_GROUP_free(group);
323 EC_GROUP_free(group);
327 static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
329 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
330 *group_b = EC_KEY_get0_group(b->pkey.ec);
331 if (group_a == NULL || group_b == NULL)
333 if (EC_GROUP_cmp(group_a, group_b, NULL))
339 static void int_ec_free(EVP_PKEY *pkey)
341 EC_KEY_free(pkey->pkey.ec);
345 EC_KEY_PRINT_PRIVATE,
350 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
353 unsigned char *priv = NULL, *pub = NULL;
354 size_t privlen = 0, publen = 0;
356 const EC_GROUP *group;
358 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
359 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_PASSED_NULL_PARAMETER);
363 if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
364 publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
369 if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
370 privlen = EC_KEY_priv2buf(x, &priv);
375 if (ktype == EC_KEY_PRINT_PRIVATE)
376 ecstr = "Private-Key";
377 else if (ktype == EC_KEY_PRINT_PUBLIC)
378 ecstr = "Public-Key";
380 ecstr = "ECDSA-Parameters";
382 if (!BIO_indent(bp, off, 128))
384 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
385 EC_GROUP_order_bits(group)) <= 0)
389 if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
391 if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
396 if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
398 if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
402 if (!ECPKParameters_print(bp, group, off))
407 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_EC_LIB);
408 OPENSSL_clear_free(priv, privlen);
413 static int eckey_param_decode(EVP_PKEY *pkey,
414 const unsigned char **pder, int derlen)
418 if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
419 ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
422 EVP_PKEY_assign_EC_KEY(pkey, eckey);
426 static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
428 return i2d_ECParameters(pkey->pkey.ec, pder);
431 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
434 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
437 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
440 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
443 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
446 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
449 static int old_ec_priv_decode(EVP_PKEY *pkey,
450 const unsigned char **pder, int derlen)
454 if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
455 ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
458 EVP_PKEY_assign_EC_KEY(pkey, ec);
462 static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
464 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
467 static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
470 case ASN1_PKEY_CTRL_PKCS7_SIGN:
473 X509_ALGOR *alg1, *alg2;
474 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
475 if (alg1 == NULL || alg1->algorithm == NULL)
477 hnid = OBJ_obj2nid(alg1->algorithm);
478 if (hnid == NID_undef)
480 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
482 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
485 #ifndef OPENSSL_NO_CMS
486 case ASN1_PKEY_CTRL_CMS_SIGN:
489 X509_ALGOR *alg1, *alg2;
490 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
491 if (alg1 == NULL || alg1->algorithm == NULL)
493 hnid = OBJ_obj2nid(alg1->algorithm);
494 if (hnid == NID_undef)
496 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
498 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
502 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
504 return ecdh_cms_decrypt(arg2);
506 return ecdh_cms_encrypt(arg2);
509 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
510 *(int *)arg2 = CMS_RECIPINFO_AGREE;
514 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
515 if (EVP_PKEY_id(pkey) == EVP_PKEY_SM2) {
516 /* For SM2, the only valid digest-alg is SM3 */
517 *(int *)arg2 = NID_sm3;
518 return 2; /* Make it mandatory */
520 *(int *)arg2 = NID_sha256;
523 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
524 return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL);
526 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
527 return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
528 POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
537 static int ec_pkey_check(const EVP_PKEY *pkey)
539 EC_KEY *eckey = pkey->pkey.ec;
541 /* stay consistent to what EVP_PKEY_check demands */
542 if (eckey->priv_key == NULL) {
543 ECerr(EC_F_EC_PKEY_CHECK, EC_R_MISSING_PRIVATE_KEY);
547 return EC_KEY_check_key(eckey);
550 static int ec_pkey_public_check(const EVP_PKEY *pkey)
552 EC_KEY *eckey = pkey->pkey.ec;
555 * Note: it unnecessary to check eckey->pub_key here since
556 * it will be checked in EC_KEY_check_key(). In fact, the
557 * EC_KEY_check_key() mainly checks the public key, and checks
558 * the private key optionally (only if there is one). So if
559 * someone passes a whole EC key (public + private), this
563 return EC_KEY_check_key(eckey);
566 static int ec_pkey_param_check(const EVP_PKEY *pkey)
568 EC_KEY *eckey = pkey->pkey.ec;
570 /* stay consistent to what EVP_PKEY_check demands */
571 if (eckey->group == NULL) {
572 ECerr(EC_F_EC_PKEY_PARAM_CHECK, EC_R_MISSING_PARAMETERS);
576 return EC_GROUP_check(eckey->group, NULL);
580 size_t ec_pkey_dirty_cnt(const EVP_PKEY *pkey)
582 return pkey->pkey.ec->dirty_cnt;
586 int ecparams_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl)
594 ecg = EC_KEY_get0_group(eckey);
598 curve_nid = EC_GROUP_get_curve_name(ecg);
600 if (curve_nid == NID_undef) {
601 /* explicit parameters */
604 * TODO(3.0): should we support explicit parameters curves?
609 const char *curve_name = NULL;
611 if ((curve_name = OBJ_nid2sn(curve_nid)) == NULL)
614 if (!ossl_param_bld_push_utf8_string(tmpl, OSSL_PKEY_PARAM_EC_NAME, curve_name, 0))
622 int ec_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
623 EVP_KEYMGMT *to_keymgmt)
625 const EC_KEY *eckey = NULL;
626 const EC_GROUP *ecg = NULL;
627 unsigned char *pub_key_buf = NULL;
628 size_t pub_key_buflen;
630 OSSL_PARAM *params = NULL;
631 const BIGNUM *priv_key = NULL;
632 const EC_POINT *pub_point = NULL;
636 || (eckey = from->pkey.ec) == NULL
637 || (ecg = EC_KEY_get0_group(eckey)) == NULL)
640 ossl_param_bld_init(&tmpl);
642 /* export the domain parameters */
643 if (!ecparams_to_params(eckey, &tmpl))
646 priv_key = EC_KEY_get0_private_key(eckey);
647 pub_point = EC_KEY_get0_public_key(eckey);
649 /* public_key must be present, priv_key is optional */
650 if (pub_point == NULL)
653 /* convert pub_point to a octet string according to the SECG standard */
654 if ((pub_key_buflen = EC_POINT_point2buf(ecg, pub_point,
655 POINT_CONVERSION_COMPRESSED,
656 &pub_key_buf, NULL)) == 0)
659 if (!ossl_param_bld_push_octet_string(&tmpl,
660 OSSL_PKEY_PARAM_PUB_KEY,
665 if (priv_key != NULL) {
667 * The ECDH Cofactor Mode is defined only if the EC_KEY actually
668 * contains a private key, so we check for the flag and export it only
671 int ecdh_cofactor_mode =
672 (EC_KEY_get_flags(eckey) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
674 /* Export the actual private key */
675 if (!ossl_param_bld_push_BN(&tmpl,
676 OSSL_PKEY_PARAM_PRIV_KEY,
680 /* Export the ECDH_COFACTOR_MODE parameter */
681 if (!ossl_param_bld_push_int(&tmpl,
682 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
687 params = ossl_param_bld_to_param(&tmpl);
689 /* We export, the provider imports */
690 rv = evp_keymgmt_import(to_keymgmt, to_keydata, OSSL_KEYMGMT_SELECT_ALL,
694 ossl_param_bld_free(params);
695 OPENSSL_free(pub_key_buf);
699 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
704 "OpenSSL EC algorithm",
721 ec_missing_parameters,
735 ec_pkey_public_check,
738 0, /* set_priv_key */
740 0, /* get_priv_key */
747 #if !defined(OPENSSL_NO_SM2)
748 const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = {
755 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
757 int private = EC_KEY_get0_private_key(x) != NULL;
759 return do_EC_KEY_print(bp, x, off,
760 private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
763 int ECParameters_print(BIO *bp, const EC_KEY *x)
765 return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
768 #ifndef OPENSSL_NO_CMS
770 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
771 X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
773 const ASN1_OBJECT *aoid;
777 EVP_PKEY *pkpeer = NULL;
778 EC_KEY *ecpeer = NULL;
779 const unsigned char *p;
781 X509_ALGOR_get0(&aoid, &atype, &aval, alg);
782 if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
784 /* If absent parameters get group from main key */
785 if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
788 pk = EVP_PKEY_CTX_get0_pkey(pctx);
791 grp = EC_KEY_get0_group(pk->pkey.ec);
792 ecpeer = EC_KEY_new();
795 if (!EC_KEY_set_group(ecpeer, grp))
798 ecpeer = eckey_type2param(atype, aval);
802 /* We have parameters now set public key */
803 plen = ASN1_STRING_length(pubkey);
804 p = ASN1_STRING_get0_data(pubkey);
805 if (p == NULL || plen == 0)
807 if (!o2i_ECPublicKey(&ecpeer, &p, plen))
809 pkpeer = EVP_PKEY_new();
812 EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
813 if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
817 EVP_PKEY_free(pkpeer);
821 /* Set KDF parameters based on KDF NID */
822 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
824 int kdf_nid, kdfmd_nid, cofactor;
825 const EVP_MD *kdf_md;
826 if (eckdf_nid == NID_undef)
829 /* Lookup KDF type, cofactor mode and digest */
830 if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
833 if (kdf_nid == NID_dh_std_kdf)
835 else if (kdf_nid == NID_dh_cofactor_kdf)
840 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
843 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
846 kdf_md = EVP_get_digestbynid(kdfmd_nid);
850 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
855 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
859 X509_ALGOR *alg, *kekalg = NULL;
860 ASN1_OCTET_STRING *ukm;
861 const unsigned char *p;
862 unsigned char *der = NULL;
864 const EVP_CIPHER *kekcipher;
865 EVP_CIPHER_CTX *kekctx;
867 if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
870 if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
871 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
875 if (alg->parameter->type != V_ASN1_SEQUENCE)
878 p = alg->parameter->value.sequence->data;
879 plen = alg->parameter->value.sequence->length;
880 kekalg = d2i_X509_ALGOR(NULL, &p, plen);
883 kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
886 kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
887 if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
889 if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
891 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
894 keylen = EVP_CIPHER_CTX_key_length(kekctx);
895 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
898 plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
903 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
909 X509_ALGOR_free(kekalg);
914 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
917 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
920 /* See if we need to set peer key */
921 if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
923 ASN1_BIT_STRING *pubkey;
924 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
929 if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
930 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
934 /* Set ECDH derivation parameters and initialise unwrap context */
935 if (!ecdh_cms_set_shared_info(pctx, ri)) {
936 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
942 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
948 X509_ALGOR *talg, *wrap_alg = NULL;
949 const ASN1_OBJECT *aoid;
950 ASN1_BIT_STRING *pubkey;
951 ASN1_STRING *wrap_str;
952 ASN1_OCTET_STRING *ukm;
953 unsigned char *penc = NULL;
956 int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
957 const EVP_MD *kdf_md;
958 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
961 /* Get ephemeral key */
962 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
963 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
966 X509_ALGOR_get0(&aoid, NULL, NULL, talg);
967 /* Is everything uninitialised? */
968 if (aoid == OBJ_nid2obj(NID_undef)) {
970 EC_KEY *eckey = pkey->pkey.ec;
974 penclen = i2o_ECPublicKey(eckey, NULL);
977 penc = OPENSSL_malloc(penclen);
981 penclen = i2o_ECPublicKey(eckey, &p);
984 ASN1_STRING_set0(pubkey, penc, penclen);
985 pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
986 pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
989 X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
993 /* See if custom parameters set */
994 kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
997 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
999 ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
1002 else if (ecdh_nid == 0)
1003 ecdh_nid = NID_dh_std_kdf;
1004 else if (ecdh_nid == 1)
1005 ecdh_nid = NID_dh_cofactor_kdf;
1007 if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
1008 kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
1009 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
1014 if (kdf_md == NULL) {
1015 /* Fixme later for better MD */
1016 kdf_md = EVP_sha1();
1017 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
1021 if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
1024 /* Lookup NID for KDF+cofactor+digest */
1026 if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
1029 ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
1030 wrap_nid = EVP_CIPHER_CTX_type(ctx);
1031 keylen = EVP_CIPHER_CTX_key_length(ctx);
1033 /* Package wrap algorithm in an AlgorithmIdentifier */
1035 wrap_alg = X509_ALGOR_new();
1036 if (wrap_alg == NULL)
1038 wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
1039 wrap_alg->parameter = ASN1_TYPE_new();
1040 if (wrap_alg->parameter == NULL)
1042 if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
1044 if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
1045 ASN1_TYPE_free(wrap_alg->parameter);
1046 wrap_alg->parameter = NULL;
1049 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
1052 penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
1057 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
1062 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
1063 * of another AlgorithmIdentifier.
1065 penclen = i2d_X509_ALGOR(wrap_alg, &penc);
1066 if (!penc || !penclen)
1068 wrap_str = ASN1_STRING_new();
1069 if (wrap_str == NULL)
1071 ASN1_STRING_set0(wrap_str, penc, penclen);
1073 X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
1079 X509_ALGOR_free(wrap_alg);