/src/openssl/crypto/cms/cms_pwri.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* crypto/cms/cms_pwri.c */ |
2 | | /* |
3 | | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
4 | | * project. |
5 | | */ |
6 | | /* ==================================================================== |
7 | | * Copyright (c) 2009 The OpenSSL Project. All rights reserved. |
8 | | * |
9 | | * Redistribution and use in source and binary forms, with or without |
10 | | * modification, are permitted provided that the following conditions |
11 | | * are met: |
12 | | * |
13 | | * 1. Redistributions of source code must retain the above copyright |
14 | | * notice, this list of conditions and the following disclaimer. |
15 | | * |
16 | | * 2. Redistributions in binary form must reproduce the above copyright |
17 | | * notice, this list of conditions and the following disclaimer in |
18 | | * the documentation and/or other materials provided with the |
19 | | * distribution. |
20 | | * |
21 | | * 3. All advertising materials mentioning features or use of this |
22 | | * software must display the following acknowledgment: |
23 | | * "This product includes software developed by the OpenSSL Project |
24 | | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
25 | | * |
26 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
27 | | * endorse or promote products derived from this software without |
28 | | * prior written permission. For written permission, please contact |
29 | | * licensing@OpenSSL.org. |
30 | | * |
31 | | * 5. Products derived from this software may not be called "OpenSSL" |
32 | | * nor may "OpenSSL" appear in their names without prior written |
33 | | * permission of the OpenSSL Project. |
34 | | * |
35 | | * 6. Redistributions of any form whatsoever must retain the following |
36 | | * acknowledgment: |
37 | | * "This product includes software developed by the OpenSSL Project |
38 | | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
39 | | * |
40 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
41 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
43 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
44 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
45 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
46 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
47 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
49 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
50 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
51 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
52 | | * ==================================================================== |
53 | | */ |
54 | | |
55 | | #include "cryptlib.h" |
56 | | #include <openssl/asn1t.h> |
57 | | #include <openssl/pem.h> |
58 | | #include <openssl/x509v3.h> |
59 | | #include <openssl/err.h> |
60 | | #include <openssl/cms.h> |
61 | | #include <openssl/rand.h> |
62 | | #include <openssl/aes.h> |
63 | | #include "cms_lcl.h" |
64 | | #include "asn1_locl.h" |
65 | | |
66 | | int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri, |
67 | | unsigned char *pass, ossl_ssize_t passlen) |
68 | 0 | { |
69 | 0 | CMS_PasswordRecipientInfo *pwri; |
70 | 0 | if (ri->type != CMS_RECIPINFO_PASS) { |
71 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD, CMS_R_NOT_PWRI); |
72 | 0 | return 0; |
73 | 0 | } |
74 | | |
75 | 0 | pwri = ri->d.pwri; |
76 | 0 | pwri->pass = pass; |
77 | 0 | if (pass && passlen < 0) |
78 | 0 | passlen = strlen((char *)pass); |
79 | 0 | pwri->passlen = passlen; |
80 | 0 | return 1; |
81 | 0 | } |
82 | | |
83 | | CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms, |
84 | | int iter, int wrap_nid, |
85 | | int pbe_nid, |
86 | | unsigned char *pass, |
87 | | ossl_ssize_t passlen, |
88 | | const EVP_CIPHER *kekciph) |
89 | 0 | { |
90 | 0 | CMS_RecipientInfo *ri = NULL; |
91 | 0 | CMS_EnvelopedData *env; |
92 | 0 | CMS_PasswordRecipientInfo *pwri; |
93 | 0 | EVP_CIPHER_CTX ctx; |
94 | 0 | X509_ALGOR *encalg = NULL; |
95 | 0 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
96 | 0 | int ivlen; |
97 | |
|
98 | 0 | env = cms_get0_enveloped(cms); |
99 | 0 | if (!env) |
100 | 0 | return NULL; |
101 | | |
102 | 0 | if (wrap_nid <= 0) |
103 | 0 | wrap_nid = NID_id_alg_PWRI_KEK; |
104 | |
|
105 | 0 | if (pbe_nid <= 0) |
106 | 0 | pbe_nid = NID_id_pbkdf2; |
107 | | |
108 | | /* Get from enveloped data */ |
109 | 0 | if (kekciph == NULL) |
110 | 0 | kekciph = env->encryptedContentInfo->cipher; |
111 | |
|
112 | 0 | if (kekciph == NULL) { |
113 | 0 | CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, CMS_R_NO_CIPHER); |
114 | 0 | return NULL; |
115 | 0 | } |
116 | 0 | if (wrap_nid != NID_id_alg_PWRI_KEK) { |
117 | 0 | CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, |
118 | 0 | CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM); |
119 | 0 | return NULL; |
120 | 0 | } |
121 | | |
122 | | /* Setup algorithm identifier for cipher */ |
123 | 0 | encalg = X509_ALGOR_new(); |
124 | 0 | if (encalg == NULL) { |
125 | 0 | goto merr; |
126 | 0 | } |
127 | 0 | EVP_CIPHER_CTX_init(&ctx); |
128 | |
|
129 | 0 | if (EVP_EncryptInit_ex(&ctx, kekciph, NULL, NULL, NULL) <= 0) { |
130 | 0 | CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB); |
131 | 0 | goto err; |
132 | 0 | } |
133 | | |
134 | 0 | ivlen = EVP_CIPHER_CTX_iv_length(&ctx); |
135 | |
|
136 | 0 | if (ivlen > 0) { |
137 | 0 | if (RAND_bytes(iv, ivlen) <= 0) |
138 | 0 | goto err; |
139 | 0 | if (EVP_EncryptInit_ex(&ctx, NULL, NULL, NULL, iv) <= 0) { |
140 | 0 | CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB); |
141 | 0 | goto err; |
142 | 0 | } |
143 | 0 | encalg->parameter = ASN1_TYPE_new(); |
144 | 0 | if (!encalg->parameter) { |
145 | 0 | CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE); |
146 | 0 | goto err; |
147 | 0 | } |
148 | 0 | if (EVP_CIPHER_param_to_asn1(&ctx, encalg->parameter) <= 0) { |
149 | 0 | CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, |
150 | 0 | CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR); |
151 | 0 | goto err; |
152 | 0 | } |
153 | 0 | } |
154 | | |
155 | 0 | encalg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(&ctx)); |
156 | |
|
157 | 0 | EVP_CIPHER_CTX_cleanup(&ctx); |
158 | | |
159 | | /* Initialize recipient info */ |
160 | 0 | ri = M_ASN1_new_of(CMS_RecipientInfo); |
161 | 0 | if (!ri) |
162 | 0 | goto merr; |
163 | | |
164 | 0 | ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo); |
165 | 0 | if (!ri->d.pwri) |
166 | 0 | goto merr; |
167 | 0 | ri->type = CMS_RECIPINFO_PASS; |
168 | |
|
169 | 0 | pwri = ri->d.pwri; |
170 | | /* Since this is overwritten, free up empty structure already there */ |
171 | 0 | X509_ALGOR_free(pwri->keyEncryptionAlgorithm); |
172 | 0 | pwri->keyEncryptionAlgorithm = X509_ALGOR_new(); |
173 | 0 | if (!pwri->keyEncryptionAlgorithm) |
174 | 0 | goto merr; |
175 | 0 | pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid); |
176 | 0 | pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new(); |
177 | 0 | if (!pwri->keyEncryptionAlgorithm->parameter) |
178 | 0 | goto merr; |
179 | | |
180 | 0 | if (!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR), |
181 | 0 | &pwri->keyEncryptionAlgorithm->parameter-> |
182 | 0 | value.sequence)) |
183 | 0 | goto merr; |
184 | 0 | pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE; |
185 | |
|
186 | 0 | X509_ALGOR_free(encalg); |
187 | 0 | encalg = NULL; |
188 | | |
189 | | /* Setup PBE algorithm */ |
190 | |
|
191 | 0 | pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set(iter, NULL, 0, -1, -1); |
192 | |
|
193 | 0 | if (!pwri->keyDerivationAlgorithm) |
194 | 0 | goto err; |
195 | | |
196 | 0 | CMS_RecipientInfo_set0_password(ri, pass, passlen); |
197 | 0 | pwri->version = 0; |
198 | |
|
199 | 0 | if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri)) |
200 | 0 | goto merr; |
201 | | |
202 | 0 | return ri; |
203 | | |
204 | 0 | merr: |
205 | 0 | CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE); |
206 | 0 | err: |
207 | 0 | EVP_CIPHER_CTX_cleanup(&ctx); |
208 | 0 | if (ri) |
209 | 0 | M_ASN1_free_of(ri, CMS_RecipientInfo); |
210 | 0 | if (encalg) |
211 | 0 | X509_ALGOR_free(encalg); |
212 | 0 | return NULL; |
213 | |
|
214 | 0 | } |
215 | | |
216 | | /* |
217 | | * This is an implementation of the key wrapping mechanism in RFC3211, at |
218 | | * some point this should go into EVP. |
219 | | */ |
220 | | |
221 | | static int kek_unwrap_key(unsigned char *out, size_t *outlen, |
222 | | const unsigned char *in, size_t inlen, |
223 | | EVP_CIPHER_CTX *ctx) |
224 | 0 | { |
225 | 0 | size_t blocklen = EVP_CIPHER_CTX_block_size(ctx); |
226 | 0 | unsigned char *tmp; |
227 | 0 | int outl, rv = 0; |
228 | 0 | if (inlen < 2 * blocklen) { |
229 | | /* too small */ |
230 | 0 | return 0; |
231 | 0 | } |
232 | 0 | if (inlen % blocklen) { |
233 | | /* Invalid size */ |
234 | 0 | return 0; |
235 | 0 | } |
236 | 0 | tmp = OPENSSL_malloc(inlen); |
237 | 0 | if (!tmp) |
238 | 0 | return 0; |
239 | | /* setup IV by decrypting last two blocks */ |
240 | 0 | EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl, |
241 | 0 | in + inlen - 2 * blocklen, blocklen * 2); |
242 | | /* |
243 | | * Do a decrypt of last decrypted block to set IV to correct value output |
244 | | * it to start of buffer so we don't corrupt decrypted block this works |
245 | | * because buffer is at least two block lengths long. |
246 | | */ |
247 | 0 | EVP_DecryptUpdate(ctx, tmp, &outl, tmp + inlen - blocklen, blocklen); |
248 | | /* Can now decrypt first n - 1 blocks */ |
249 | 0 | EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen); |
250 | | |
251 | | /* Reset IV to original value */ |
252 | 0 | EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL); |
253 | | /* Decrypt again */ |
254 | 0 | EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen); |
255 | | /* Check check bytes */ |
256 | 0 | if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) { |
257 | | /* Check byte failure */ |
258 | 0 | goto err; |
259 | 0 | } |
260 | 0 | if (inlen < (size_t)(tmp[0] - 4)) { |
261 | | /* Invalid length value */ |
262 | 0 | goto err; |
263 | 0 | } |
264 | 0 | *outlen = (size_t)tmp[0]; |
265 | 0 | memcpy(out, tmp + 4, *outlen); |
266 | 0 | rv = 1; |
267 | 0 | err: |
268 | 0 | OPENSSL_cleanse(tmp, inlen); |
269 | 0 | OPENSSL_free(tmp); |
270 | 0 | return rv; |
271 | |
|
272 | 0 | } |
273 | | |
274 | | static int kek_wrap_key(unsigned char *out, size_t *outlen, |
275 | | const unsigned char *in, size_t inlen, |
276 | | EVP_CIPHER_CTX *ctx) |
277 | 0 | { |
278 | 0 | size_t blocklen = EVP_CIPHER_CTX_block_size(ctx); |
279 | 0 | size_t olen; |
280 | 0 | int dummy; |
281 | | /* |
282 | | * First decide length of output buffer: need header and round up to |
283 | | * multiple of block length. |
284 | | */ |
285 | 0 | olen = (inlen + 4 + blocklen - 1) / blocklen; |
286 | 0 | olen *= blocklen; |
287 | 0 | if (olen < 2 * blocklen) { |
288 | | /* Key too small */ |
289 | 0 | return 0; |
290 | 0 | } |
291 | 0 | if (inlen > 0xFF) { |
292 | | /* Key too large */ |
293 | 0 | return 0; |
294 | 0 | } |
295 | 0 | if (out) { |
296 | | /* Set header */ |
297 | 0 | out[0] = (unsigned char)inlen; |
298 | 0 | out[1] = in[0] ^ 0xFF; |
299 | 0 | out[2] = in[1] ^ 0xFF; |
300 | 0 | out[3] = in[2] ^ 0xFF; |
301 | 0 | memcpy(out + 4, in, inlen); |
302 | | /* Add random padding to end */ |
303 | 0 | if (olen > inlen + 4 |
304 | 0 | && RAND_bytes(out + 4 + inlen, olen - 4 - inlen) <= 0) |
305 | 0 | return 0; |
306 | | /* Encrypt twice */ |
307 | 0 | EVP_EncryptUpdate(ctx, out, &dummy, out, olen); |
308 | 0 | EVP_EncryptUpdate(ctx, out, &dummy, out, olen); |
309 | 0 | } |
310 | | |
311 | 0 | *outlen = olen; |
312 | |
|
313 | 0 | return 1; |
314 | 0 | } |
315 | | |
316 | | /* Encrypt/Decrypt content key in PWRI recipient info */ |
317 | | |
318 | | int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri, |
319 | | int en_de) |
320 | 0 | { |
321 | 0 | CMS_EncryptedContentInfo *ec; |
322 | 0 | CMS_PasswordRecipientInfo *pwri; |
323 | 0 | const unsigned char *p = NULL; |
324 | 0 | int plen; |
325 | 0 | int r = 0; |
326 | 0 | X509_ALGOR *algtmp, *kekalg = NULL; |
327 | 0 | EVP_CIPHER_CTX kekctx; |
328 | 0 | const EVP_CIPHER *kekcipher; |
329 | 0 | unsigned char *key = NULL; |
330 | 0 | size_t keylen; |
331 | |
|
332 | 0 | ec = cms->d.envelopedData->encryptedContentInfo; |
333 | |
|
334 | 0 | pwri = ri->d.pwri; |
335 | 0 | EVP_CIPHER_CTX_init(&kekctx); |
336 | |
|
337 | 0 | if (!pwri->pass) { |
338 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD); |
339 | 0 | return 0; |
340 | 0 | } |
341 | 0 | algtmp = pwri->keyEncryptionAlgorithm; |
342 | |
|
343 | 0 | if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) { |
344 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, |
345 | 0 | CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM); |
346 | 0 | return 0; |
347 | 0 | } |
348 | | |
349 | 0 | if (algtmp->parameter->type == V_ASN1_SEQUENCE) { |
350 | 0 | p = algtmp->parameter->value.sequence->data; |
351 | 0 | plen = algtmp->parameter->value.sequence->length; |
352 | 0 | kekalg = d2i_X509_ALGOR(NULL, &p, plen); |
353 | 0 | } |
354 | 0 | if (kekalg == NULL) { |
355 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, |
356 | 0 | CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER); |
357 | 0 | return 0; |
358 | 0 | } |
359 | | |
360 | 0 | kekcipher = EVP_get_cipherbyobj(kekalg->algorithm); |
361 | |
|
362 | 0 | if (!kekcipher) { |
363 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNKNOWN_CIPHER); |
364 | 0 | goto err; |
365 | 0 | } |
366 | | |
367 | | /* Fixup cipher based on AlgorithmIdentifier to set IV etc */ |
368 | 0 | if (!EVP_CipherInit_ex(&kekctx, kekcipher, NULL, NULL, NULL, en_de)) |
369 | 0 | goto err; |
370 | 0 | EVP_CIPHER_CTX_set_padding(&kekctx, 0); |
371 | 0 | if (EVP_CIPHER_asn1_to_param(&kekctx, kekalg->parameter) < 0) { |
372 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, |
373 | 0 | CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR); |
374 | 0 | goto err; |
375 | 0 | } |
376 | | |
377 | 0 | algtmp = pwri->keyDerivationAlgorithm; |
378 | | |
379 | | /* Finish password based key derivation to setup key in "ctx" */ |
380 | |
|
381 | 0 | if (EVP_PBE_CipherInit(algtmp->algorithm, |
382 | 0 | (char *)pwri->pass, pwri->passlen, |
383 | 0 | algtmp->parameter, &kekctx, en_de) < 0) { |
384 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB); |
385 | 0 | goto err; |
386 | 0 | } |
387 | | |
388 | | /* Finally wrap/unwrap the key */ |
389 | | |
390 | 0 | if (en_de) { |
391 | |
|
392 | 0 | if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, &kekctx)) |
393 | 0 | goto err; |
394 | | |
395 | 0 | key = OPENSSL_malloc(keylen); |
396 | |
|
397 | 0 | if (!key) |
398 | 0 | goto err; |
399 | | |
400 | 0 | if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, &kekctx)) |
401 | 0 | goto err; |
402 | 0 | pwri->encryptedKey->data = key; |
403 | 0 | pwri->encryptedKey->length = keylen; |
404 | 0 | } else { |
405 | 0 | key = OPENSSL_malloc(pwri->encryptedKey->length); |
406 | |
|
407 | 0 | if (!key) { |
408 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE); |
409 | 0 | goto err; |
410 | 0 | } |
411 | 0 | if (!kek_unwrap_key(key, &keylen, |
412 | 0 | pwri->encryptedKey->data, |
413 | 0 | pwri->encryptedKey->length, &kekctx)) { |
414 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNWRAP_FAILURE); |
415 | 0 | goto err; |
416 | 0 | } |
417 | | |
418 | 0 | ec->key = key; |
419 | 0 | ec->keylen = keylen; |
420 | |
|
421 | 0 | } |
422 | | |
423 | 0 | r = 1; |
424 | |
|
425 | 0 | err: |
426 | |
|
427 | 0 | EVP_CIPHER_CTX_cleanup(&kekctx); |
428 | |
|
429 | 0 | if (!r && key) |
430 | 0 | OPENSSL_free(key); |
431 | 0 | X509_ALGOR_free(kekalg); |
432 | |
|
433 | 0 | return r; |
434 | |
|
435 | 0 | } |