2 * Copyright 2008-2021 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
10 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/pem.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/err.h>
15 #include <openssl/cms.h>
16 #include <openssl/rand.h>
17 #include "crypto/evp.h"
18 #include "cms_local.h"
20 /* CMS EncryptedData Utilities */
22 /* Return BIO based on EncryptedContentInfo and key */
24 BIO
*ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo
*ec
,
25 const CMS_CTX
*cms_ctx
)
29 EVP_CIPHER
*fetched_ciph
= NULL
;
30 const EVP_CIPHER
*cipher
= NULL
;
31 X509_ALGOR
*calg
= ec
->contentEncryptionAlgorithm
;
32 evp_cipher_aead_asn1_params aparams
;
33 unsigned char iv
[EVP_MAX_IV_LENGTH
], *piv
= NULL
;
34 unsigned char *tkey
= NULL
;
39 int enc
, keep_key
= 0;
40 OSSL_LIB_CTX
*libctx
= ossl_cms_ctx_get0_libctx(cms_ctx
);
41 const char *propq
= ossl_cms_ctx_get0_propq(cms_ctx
);
43 enc
= ec
->cipher
? 1 : 0;
45 b
= BIO_new(BIO_f_cipher());
47 ERR_raise(ERR_LIB_CMS
, ERR_R_BIO_LIB
);
51 BIO_get_cipher_ctx(b
, &ctx
);
57 * If not keeping key set cipher to NULL so subsequent calls decrypt.
62 cipher
= EVP_get_cipherbyobj(calg
->algorithm
);
65 fetched_ciph
= EVP_CIPHER_fetch(libctx
, EVP_CIPHER_get0_name(cipher
),
67 if (fetched_ciph
!= NULL
)
68 cipher
= fetched_ciph
;
71 (void)ERR_clear_last_mark();
72 ERR_raise(ERR_LIB_CMS
, CMS_R_UNKNOWN_CIPHER
);
75 (void)ERR_pop_to_mark();
77 if (EVP_CipherInit_ex(ctx
, cipher
, NULL
, NULL
, NULL
, enc
) <= 0) {
78 ERR_raise(ERR_LIB_CMS
, CMS_R_CIPHER_INITIALISATION_ERROR
);
83 calg
->algorithm
= OBJ_nid2obj(EVP_CIPHER_CTX_get_type(ctx
));
84 if (calg
->algorithm
== NULL
) {
85 ERR_raise(ERR_LIB_CMS
, CMS_R_UNSUPPORTED_CONTENT_ENCRYPTION_ALGORITHM
);
88 /* Generate a random IV if we need one */
89 ivlen
= EVP_CIPHER_CTX_get_iv_length(ctx
);
91 ERR_raise(ERR_LIB_CMS
, ERR_R_EVP_LIB
);
96 if (RAND_bytes_ex(libctx
, iv
, ivlen
, 0) <= 0)
101 if (evp_cipher_asn1_to_param_ex(ctx
, calg
->parameter
, &aparams
) <= 0) {
102 ERR_raise(ERR_LIB_CMS
, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR
);
105 if ((EVP_CIPHER_get_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
)) {
108 && EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
109 ec
->taglen
, ec
->tag
) <= 0) {
110 ERR_raise(ERR_LIB_CMS
, CMS_R_CIPHER_AEAD_SET_TAG_ERROR
);
115 len
= EVP_CIPHER_CTX_get_key_length(ctx
);
118 tkeylen
= (size_t)len
;
120 /* Generate random session key */
121 if (!enc
|| !ec
->key
) {
122 tkey
= OPENSSL_malloc(tkeylen
);
125 if (EVP_CIPHER_CTX_rand_key(ctx
, tkey
) <= 0)
131 ec
->keylen
= tkeylen
;
140 if (ec
->keylen
!= tkeylen
) {
141 /* If necessary set key length */
142 if (EVP_CIPHER_CTX_set_key_length(ctx
, ec
->keylen
) <= 0) {
144 * Only reveal failure if debugging so we don't leak information
145 * which may be useful in MMA.
147 if (enc
|| ec
->debug
) {
148 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_LENGTH
);
152 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
154 ec
->keylen
= tkeylen
;
161 if (EVP_CipherInit_ex(ctx
, NULL
, NULL
, ec
->key
, piv
, enc
) <= 0) {
162 ERR_raise(ERR_LIB_CMS
, CMS_R_CIPHER_INITIALISATION_ERROR
);
166 calg
->parameter
= ASN1_TYPE_new();
167 if (calg
->parameter
== NULL
) {
168 ERR_raise(ERR_LIB_CMS
, ERR_R_ASN1_LIB
);
171 if ((EVP_CIPHER_get_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
)) {
172 memcpy(aparams
.iv
, piv
, ivlen
);
173 aparams
.iv_len
= ivlen
;
174 aparams
.tag_len
= EVP_CIPHER_CTX_get_tag_length(ctx
);
175 if (aparams
.tag_len
<= 0)
179 if (evp_cipher_param_to_asn1_ex(ctx
, calg
->parameter
, &aparams
) <= 0) {
180 ERR_raise(ERR_LIB_CMS
, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR
);
183 /* If parameter type not set omit parameter */
184 if (calg
->parameter
->type
== V_ASN1_UNDEF
) {
185 ASN1_TYPE_free(calg
->parameter
);
186 calg
->parameter
= NULL
;
192 EVP_CIPHER_free(fetched_ciph
);
193 if (!keep_key
|| !ok
) {
194 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
197 OPENSSL_clear_free(tkey
, tkeylen
);
204 int ossl_cms_EncryptedContent_init(CMS_EncryptedContentInfo
*ec
,
205 const EVP_CIPHER
*cipher
,
206 const unsigned char *key
, size_t keylen
,
207 const CMS_CTX
*cms_ctx
)
211 if ((ec
->key
= OPENSSL_malloc(keylen
)) == NULL
)
213 memcpy(ec
->key
, key
, keylen
);
217 ec
->contentType
= OBJ_nid2obj(NID_pkcs7_data
);
221 int CMS_EncryptedData_set1_key(CMS_ContentInfo
*cms
, const EVP_CIPHER
*ciph
,
222 const unsigned char *key
, size_t keylen
)
224 CMS_EncryptedContentInfo
*ec
;
226 if (!key
|| !keylen
) {
227 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_KEY
);
231 cms
->d
.encryptedData
= M_ASN1_new_of(CMS_EncryptedData
);
232 if (!cms
->d
.encryptedData
) {
233 ERR_raise(ERR_LIB_CMS
, ERR_R_ASN1_LIB
);
236 cms
->contentType
= OBJ_nid2obj(NID_pkcs7_encrypted
);
237 cms
->d
.encryptedData
->version
= 0;
238 } else if (OBJ_obj2nid(cms
->contentType
) != NID_pkcs7_encrypted
) {
239 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_ENCRYPTED_DATA
);
242 ec
= cms
->d
.encryptedData
->encryptedContentInfo
;
243 return ossl_cms_EncryptedContent_init(ec
, ciph
, key
, keylen
,
244 ossl_cms_get0_cmsctx(cms
));
247 BIO
*ossl_cms_EncryptedData_init_bio(const CMS_ContentInfo
*cms
)
249 CMS_EncryptedData
*enc
= cms
->d
.encryptedData
;
250 if (enc
->encryptedContentInfo
->cipher
&& enc
->unprotectedAttrs
)
252 return ossl_cms_EncryptedContent_init_bio(enc
->encryptedContentInfo
,
253 ossl_cms_get0_cmsctx(cms
));