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