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