2 * Copyright 2006-2023 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
12 #include <openssl/cms.h>
13 #include <openssl/err.h>
14 #include <openssl/decoder.h>
15 #include "internal/sizes.h"
16 #include "crypto/asn1.h"
17 #include "crypto/evp.h"
18 #include "cms_local.h"
20 static EVP_PKEY
*pkey_type2param(int ptype
, const void *pval
,
21 OSSL_LIB_CTX
*libctx
, const char *propq
)
23 EVP_PKEY
*pkey
= NULL
;
24 EVP_PKEY_CTX
*pctx
= NULL
;
25 OSSL_DECODER_CTX
*ctx
= NULL
;
27 if (ptype
== V_ASN1_SEQUENCE
) {
28 const ASN1_STRING
*pstr
= pval
;
29 const unsigned char *pm
= pstr
->data
;
30 size_t pmlen
= (size_t)pstr
->length
;
31 int selection
= OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
;
33 ctx
= OSSL_DECODER_CTX_new_for_pkey(&pkey
, "DER", NULL
, "EC",
34 selection
, libctx
, propq
);
38 if (!OSSL_DECODER_from_data(ctx
, &pm
, &pmlen
)) {
39 ERR_raise(ERR_LIB_CMS
, CMS_R_DECODE_ERROR
);
42 OSSL_DECODER_CTX_free(ctx
);
44 } else if (ptype
== V_ASN1_OBJECT
) {
45 const ASN1_OBJECT
*poid
= pval
;
46 char groupname
[OSSL_MAX_NAME_SIZE
];
48 /* type == V_ASN1_OBJECT => the parameters are given by an asn1 OID */
49 pctx
= EVP_PKEY_CTX_new_from_name(libctx
, "EC", propq
);
50 if (pctx
== NULL
|| EVP_PKEY_paramgen_init(pctx
) <= 0)
52 if (OBJ_obj2txt(groupname
, sizeof(groupname
), poid
, 0) <= 0
53 || EVP_PKEY_CTX_set_group_name(pctx
, groupname
) <= 0) {
54 ERR_raise(ERR_LIB_CMS
, CMS_R_DECODE_ERROR
);
57 if (EVP_PKEY_paramgen(pctx
, &pkey
) <= 0)
59 EVP_PKEY_CTX_free(pctx
);
63 ERR_raise(ERR_LIB_CMS
, CMS_R_DECODE_ERROR
);
68 EVP_PKEY_CTX_free(pctx
);
69 OSSL_DECODER_CTX_free(ctx
);
73 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX
*pctx
,
74 X509_ALGOR
*alg
, ASN1_BIT_STRING
*pubkey
)
76 const ASN1_OBJECT
*aoid
;
80 EVP_PKEY
*pkpeer
= NULL
;
81 const unsigned char *p
;
84 X509_ALGOR_get0(&aoid
, &atype
, &aval
, alg
);
85 if (OBJ_obj2nid(aoid
) != NID_X9_62_id_ecPublicKey
)
88 /* If absent parameters get group from main key */
89 if (atype
== V_ASN1_UNDEF
|| atype
== V_ASN1_NULL
) {
92 pk
= EVP_PKEY_CTX_get0_pkey(pctx
);
96 pkpeer
= EVP_PKEY_new();
99 if (!EVP_PKEY_copy_parameters(pkpeer
, pk
))
102 pkpeer
= pkey_type2param(atype
, aval
,
103 EVP_PKEY_CTX_get0_libctx(pctx
),
104 EVP_PKEY_CTX_get0_propq(pctx
));
108 /* We have parameters now set public key */
109 plen
= ASN1_STRING_length(pubkey
);
110 p
= ASN1_STRING_get0_data(pubkey
);
111 if (p
== NULL
|| plen
== 0)
114 if (!EVP_PKEY_set1_encoded_public_key(pkpeer
, p
, plen
))
117 if (EVP_PKEY_derive_set_peer(pctx
, pkpeer
) > 0)
120 EVP_PKEY_free(pkpeer
);
124 /* Set KDF parameters based on KDF NID */
125 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX
*pctx
, int eckdf_nid
)
127 int kdf_nid
, kdfmd_nid
, cofactor
;
128 const EVP_MD
*kdf_md
;
130 if (eckdf_nid
== NID_undef
)
133 /* Lookup KDF type, cofactor mode and digest */
134 if (!OBJ_find_sigid_algs(eckdf_nid
, &kdfmd_nid
, &kdf_nid
))
137 if (kdf_nid
== NID_dh_std_kdf
)
139 else if (kdf_nid
== NID_dh_cofactor_kdf
)
144 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx
, cofactor
) <= 0)
147 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, EVP_PKEY_ECDH_KDF_X9_63
) <= 0)
150 kdf_md
= EVP_get_digestbynid(kdfmd_nid
);
154 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
159 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX
*pctx
, CMS_RecipientInfo
*ri
)
162 X509_ALGOR
*alg
, *kekalg
= NULL
;
163 ASN1_OCTET_STRING
*ukm
;
164 const unsigned char *p
;
165 unsigned char *der
= NULL
;
167 EVP_CIPHER
*kekcipher
= NULL
;
168 EVP_CIPHER_CTX
*kekctx
;
169 char name
[OSSL_MAX_NAME_SIZE
];
171 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &alg
, &ukm
))
174 if (!ecdh_cms_set_kdf_param(pctx
, OBJ_obj2nid(alg
->algorithm
))) {
175 ERR_raise(ERR_LIB_CMS
, CMS_R_KDF_PARAMETER_ERROR
);
179 if (alg
->parameter
->type
!= V_ASN1_SEQUENCE
)
182 p
= alg
->parameter
->value
.sequence
->data
;
183 plen
= alg
->parameter
->value
.sequence
->length
;
184 kekalg
= d2i_X509_ALGOR(NULL
, &p
, plen
);
187 kekctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
190 OBJ_obj2txt(name
, sizeof(name
), kekalg
->algorithm
, 0);
191 kekcipher
= EVP_CIPHER_fetch(pctx
->libctx
, name
, pctx
->propquery
);
192 if (kekcipher
== NULL
|| EVP_CIPHER_get_mode(kekcipher
) != EVP_CIPH_WRAP_MODE
)
194 if (!EVP_EncryptInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
))
196 if (EVP_CIPHER_asn1_to_param(kekctx
, kekalg
->parameter
) <= 0)
199 keylen
= EVP_CIPHER_CTX_get_key_length(kekctx
);
200 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
203 plen
= CMS_SharedInfo_encode(&der
, kekalg
, ukm
, keylen
);
208 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, der
, plen
) <= 0)
214 EVP_CIPHER_free(kekcipher
);
215 X509_ALGOR_free(kekalg
);
220 static int ecdh_cms_decrypt(CMS_RecipientInfo
*ri
)
224 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
227 /* See if we need to set peer key */
228 if (!EVP_PKEY_CTX_get0_peerkey(pctx
)) {
230 ASN1_BIT_STRING
*pubkey
;
232 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &alg
, &pubkey
,
235 if (alg
== NULL
|| pubkey
== NULL
)
237 if (!ecdh_cms_set_peerkey(pctx
, alg
, pubkey
)) {
238 ERR_raise(ERR_LIB_CMS
, CMS_R_PEER_KEY_ERROR
);
242 /* Set ECDH derivation parameters and initialise unwrap context */
243 if (!ecdh_cms_set_shared_info(pctx
, ri
)) {
244 ERR_raise(ERR_LIB_CMS
, CMS_R_SHARED_INFO_ERROR
);
250 static int ecdh_cms_encrypt(CMS_RecipientInfo
*ri
)
256 X509_ALGOR
*talg
, *wrap_alg
= NULL
;
257 const ASN1_OBJECT
*aoid
;
258 ASN1_BIT_STRING
*pubkey
;
259 ASN1_STRING
*wrap_str
;
260 ASN1_OCTET_STRING
*ukm
;
261 unsigned char *penc
= NULL
;
264 int ecdh_nid
, kdf_type
, kdf_nid
, wrap_nid
;
265 const EVP_MD
*kdf_md
;
267 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
270 /* Get ephemeral key */
271 pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
272 if (!CMS_RecipientInfo_kari_get0_orig_id(ri
, &talg
, &pubkey
,
275 X509_ALGOR_get0(&aoid
, NULL
, NULL
, talg
);
276 /* Is everything uninitialised? */
277 if (aoid
== OBJ_nid2obj(NID_undef
)) {
281 enckeylen
= EVP_PKEY_get1_encoded_public_key(pkey
, &penc
);
282 if (enckeylen
> INT_MAX
|| enckeylen
== 0)
284 ASN1_STRING_set0(pubkey
, penc
, (int)enckeylen
);
285 ossl_asn1_string_set_bits_left(pubkey
, 0);
288 (void)X509_ALGOR_set0(talg
, OBJ_nid2obj(NID_X9_62_id_ecPublicKey
),
289 V_ASN1_UNDEF
, NULL
); /* cannot fail */
292 /* See if custom parameters set */
293 kdf_type
= EVP_PKEY_CTX_get_ecdh_kdf_type(pctx
);
296 if (EVP_PKEY_CTX_get_ecdh_kdf_md(pctx
, &kdf_md
) <= 0)
298 ecdh_nid
= EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx
);
301 else if (ecdh_nid
== 0)
302 ecdh_nid
= NID_dh_std_kdf
;
303 else if (ecdh_nid
== 1)
304 ecdh_nid
= NID_dh_cofactor_kdf
;
306 if (kdf_type
== EVP_PKEY_ECDH_KDF_NONE
) {
307 kdf_type
= EVP_PKEY_ECDH_KDF_X9_63
;
308 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx
, kdf_type
) <= 0)
313 if (kdf_md
== NULL
) {
314 /* Fixme later for better MD */
316 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx
, kdf_md
) <= 0)
320 if (!CMS_RecipientInfo_kari_get0_alg(ri
, &talg
, &ukm
))
323 /* Lookup NID for KDF+cofactor+digest */
325 if (!OBJ_find_sigid_by_algs(&kdf_nid
, EVP_MD_get_type(kdf_md
), ecdh_nid
))
328 ctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
329 wrap_nid
= EVP_CIPHER_CTX_get_type(ctx
);
330 keylen
= EVP_CIPHER_CTX_get_key_length(ctx
);
332 /* Package wrap algorithm in an AlgorithmIdentifier */
334 wrap_alg
= X509_ALGOR_new();
335 if (wrap_alg
== NULL
)
337 wrap_alg
->algorithm
= OBJ_nid2obj(wrap_nid
);
338 wrap_alg
->parameter
= ASN1_TYPE_new();
339 if (wrap_alg
->parameter
== NULL
)
341 if (EVP_CIPHER_param_to_asn1(ctx
, wrap_alg
->parameter
) <= 0)
343 if (ASN1_TYPE_get(wrap_alg
->parameter
) == NID_undef
) {
344 ASN1_TYPE_free(wrap_alg
->parameter
);
345 wrap_alg
->parameter
= NULL
;
348 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx
, keylen
) <= 0)
351 penclen
= CMS_SharedInfo_encode(&penc
, wrap_alg
, ukm
, keylen
);
356 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx
, penc
, penclen
) <= 0)
361 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
362 * of another AlgorithmIdentifier.
364 penclen
= i2d_X509_ALGOR(wrap_alg
, &penc
);
367 wrap_str
= ASN1_STRING_new();
368 if (wrap_str
== NULL
)
370 ASN1_STRING_set0(wrap_str
, penc
, penclen
);
372 rv
= X509_ALGOR_set0(talg
, OBJ_nid2obj(kdf_nid
), V_ASN1_SEQUENCE
, wrap_str
);
374 ASN1_STRING_free(wrap_str
);
378 X509_ALGOR_free(wrap_alg
);
382 int ossl_cms_ecdh_envelope(CMS_RecipientInfo
*ri
, int decrypt
)
384 assert(decrypt
== 0 || decrypt
== 1);
387 return ecdh_cms_decrypt(ri
);
390 return ecdh_cms_encrypt(ri
);
392 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE
);