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