Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/cms/cms_enc.c

raw
1 /*
2 * Copyright 2008-2026 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
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 "crypto/asn1.h"
19 #include "cms_local.h"
20
21 /* CMS EncryptedData Utilities */
22
23 /* Return BIO based on EncryptedContentInfo and key */
24
25 BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
26 const CMS_CTX *cms_ctx, int auth)
27 {
28 BIO *b;
29 EVP_CIPHER_CTX *ctx;
30 EVP_CIPHER *fetched_ciph = NULL;
31 const EVP_CIPHER *cipher = NULL;
32 X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
33 evp_cipher_aead_asn1_params aparams;
34 unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
35 unsigned char *tkey = NULL;
36 int len;
37 int ivlen = 0;
38 size_t tkeylen = 0;
39 int ok = 0;
40 int enc, keep_key = 0;
41 OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(cms_ctx);
42 const char *propq = ossl_cms_ctx_get0_propq(cms_ctx);
43
44 enc = ec->cipher ? 1 : 0;
45
46 b = BIO_new(BIO_f_cipher());
47 if (b == NULL) {
48 ERR_raise(ERR_LIB_CMS, ERR_R_BIO_LIB);
49 return NULL;
50 }
51
52 BIO_get_cipher_ctx(b, &ctx);
53
54 (void)ERR_set_mark();
55 if (enc) {
56 cipher = ec->cipher;
57 /*
58 * If not keeping key set cipher to NULL so subsequent calls decrypt.
59 */
60 if (ec->key != NULL)
61 ec->cipher = NULL;
62 } else {
63 cipher = EVP_get_cipherbyobj(calg->algorithm);
64 }
65 if (cipher != NULL) {
66 fetched_ciph = EVP_CIPHER_fetch(libctx, EVP_CIPHER_get0_name(cipher),
67 propq);
68 if (fetched_ciph != NULL)
69 cipher = fetched_ciph;
70 }
71 if (cipher == NULL) {
72 (void)ERR_clear_last_mark();
73 ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
74 goto err;
75 }
76 (void)ERR_pop_to_mark();
77
78 if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc) <= 0) {
79 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR);
80 goto err;
81 }
82
83 if (enc) {
84 calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_get_type(ctx));
85 if (calg->algorithm == NULL || calg->algorithm->nid == NID_undef) {
86 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_ENCRYPTION_ALGORITHM);
87 goto err;
88 }
89 /* Generate a random IV if we need one */
90 ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
91 if (ivlen < 0) {
92 ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
93 goto err;
94 }
95
96 if (ivlen > 0) {
97 if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
98 goto err;
99 piv = iv;
100 }
101 } else {
102 if (evp_cipher_asn1_to_param_ex(ctx, calg->parameter, &aparams) <= 0) {
103 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
104 goto err;
105 }
106 if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
107 if (!auth) {
108 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_AEAD_IN_ENVELOPED_DATA);
109 goto err;
110 }
111 piv = aparams.iv;
112
113 if (ec->taglen < 4 || ec->taglen > 16
114 || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (int)ec->taglen, ec->tag) <= 0) {
115 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_AEAD_SET_TAG_ERROR);
116 goto err;
117 }
118 } else if (auth) {
119 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_ENCRYPTION_ALGORITHM);
120 goto err;
121 }
122 }
123 len = EVP_CIPHER_CTX_get_key_length(ctx);
124 if (len <= 0)
125 goto err;
126 tkeylen = (size_t)len;
127
128 /* Generate random session key */
129 if (!enc || !ec->key) {
130 tkey = OPENSSL_malloc(tkeylen);
131 if (tkey == NULL)
132 goto err;
133 if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
134 goto err;
135 }
136
137 if (!ec->key) {
138 ec->key = tkey;
139 ec->keylen = tkeylen;
140 tkey = NULL;
141 if (enc)
142 keep_key = 1;
143 else
144 ERR_clear_error();
145 }
146
147 if (ec->keylen != tkeylen) {
148 /* If necessary set key length */
149 if (EVP_CIPHER_CTX_set_key_length(ctx, (int)ec->keylen) <= 0) {
150 /*
151 * Only reveal failure if debugging so we don't leak information
152 * which may be useful in MMA.
153 */
154 if (enc || ec->debug) {
155 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
156 goto err;
157 } else {
158 /* Use random key */
159 OPENSSL_clear_free(ec->key, ec->keylen);
160 ec->key = tkey;
161 ec->keylen = tkeylen;
162 tkey = NULL;
163 ERR_clear_error();
164 }
165 }
166 }
167
168 if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
169 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_INITIALISATION_ERROR);
170 goto err;
171 }
172 if (enc) {
173 calg->parameter = ASN1_TYPE_new();
174 if (calg->parameter == NULL) {
175 ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
176 goto err;
177 }
178 if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
179 memcpy(aparams.iv, piv, ivlen);
180 aparams.iv_len = ivlen;
181 aparams.tag_len = EVP_CIPHER_CTX_get_tag_length(ctx);
182 if (aparams.tag_len <= 0)
183 goto err;
184 }
185
186 if (evp_cipher_param_to_asn1_ex(ctx, calg->parameter, &aparams) <= 0) {
187 ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
188 goto err;
189 }
190 /* If parameter type not set omit parameter */
191 if (calg->parameter->type == V_ASN1_UNDEF) {
192 ASN1_TYPE_free(calg->parameter);
193 calg->parameter = NULL;
194 }
195 }
196 ok = 1;
197
198 err:
199 EVP_CIPHER_free(fetched_ciph);
200 if (!keep_key || !ok) {
201 OPENSSL_clear_free(ec->key, ec->keylen);
202 ec->key = NULL;
203 }
204 OPENSSL_clear_free(tkey, tkeylen);
205 if (ok)
206 return b;
207 BIO_free(b);
208 return NULL;
209 }
210
211 int ossl_cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
212 const EVP_CIPHER *cipher,
213 const unsigned char *key, size_t keylen,
214 const CMS_CTX *cms_ctx)
215 {
216 ec->cipher = cipher;
217 if (key) {
218 if ((ec->key = OPENSSL_malloc(keylen)) == NULL)
219 return 0;
220 memcpy(ec->key, key, keylen);
221 }
222 ec->keylen = keylen;
223 if (cipher != NULL)
224 ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
225 return 1;
226 }
227
228 int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
229 const unsigned char *key, size_t keylen)
230 {
231 CMS_EncryptedContentInfo *ec;
232
233 if (!key || !keylen) {
234 ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
235 return 0;
236 }
237 if (ciph) {
238 if ((EVP_CIPHER_get_flags(ciph) & EVP_CIPH_FLAG_AEAD_CIPHER) != 0) {
239 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_ENCRYPTION_ALGORITHM);
240 return 0;
241 }
242 if (cms->d.encryptedData != NULL) {
243 M_ASN1_free_of(cms->d.encryptedData, CMS_EncryptedData);
244 cms->d.encryptedData = NULL;
245 }
246 cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
247 if (!cms->d.encryptedData) {
248 ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
249 return 0;
250 }
251 cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
252 cms->d.encryptedData->version = 0;
253 } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) {
254 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_ENCRYPTED_DATA);
255 return 0;
256 }
257 ec = cms->d.encryptedData->encryptedContentInfo;
258 return ossl_cms_EncryptedContent_init(ec, ciph, key, keylen,
259 ossl_cms_get0_cmsctx(cms));
260 }
261
262 BIO *ossl_cms_EncryptedData_init_bio(const CMS_ContentInfo *cms)
263 {
264 CMS_EncryptedData *enc = cms->d.encryptedData;
265 if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
266 enc->version = 2;
267 return ossl_cms_EncryptedContent_init_bio(enc->encryptedContentInfo,
268 ossl_cms_get0_cmsctx(cms), 0);
269 }