2 * Copyright 2009-2021 The OpenSSL Project Authors. All Rights Reserved.
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
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 <openssl/aes.h>
18 #include "internal/sizes.h"
19 #include "crypto/asn1.h"
20 #include "cms_local.h"
22 int CMS_RecipientInfo_set0_password(CMS_RecipientInfo
*ri
,
23 unsigned char *pass
, ossl_ssize_t passlen
)
25 CMS_PasswordRecipientInfo
*pwri
;
26 if (ri
->type
!= CMS_RECIPINFO_PASS
) {
27 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_PWRI
);
33 if (pass
&& passlen
< 0)
34 passlen
= strlen((char *)pass
);
35 pwri
->passlen
= passlen
;
39 CMS_RecipientInfo
*CMS_add0_recipient_password(CMS_ContentInfo
*cms
,
40 int iter
, int wrap_nid
,
44 const EVP_CIPHER
*kekciph
)
46 STACK_OF(CMS_RecipientInfo
) *ris
;
47 CMS_RecipientInfo
*ri
= NULL
;
48 CMS_EncryptedContentInfo
*ec
;
49 CMS_PasswordRecipientInfo
*pwri
;
50 EVP_CIPHER_CTX
*ctx
= NULL
;
51 X509_ALGOR
*encalg
= NULL
;
52 unsigned char iv
[EVP_MAX_IV_LENGTH
];
54 const CMS_CTX
*cms_ctx
= ossl_cms_get0_cmsctx(cms
);
56 ec
= ossl_cms_get0_env_enc_content(cms
);
59 ris
= CMS_get0_RecipientInfos(cms
);
64 wrap_nid
= NID_id_alg_PWRI_KEK
;
67 pbe_nid
= NID_id_pbkdf2
;
69 /* Get from enveloped data */
73 if (kekciph
== NULL
) {
74 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_CIPHER
);
77 if (wrap_nid
!= NID_id_alg_PWRI_KEK
) {
78 ERR_raise(ERR_LIB_CMS
, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM
);
82 /* Setup algorithm identifier for cipher */
83 encalg
= X509_ALGOR_new();
87 ctx
= EVP_CIPHER_CTX_new();
89 if (EVP_EncryptInit_ex(ctx
, kekciph
, NULL
, NULL
, NULL
) <= 0) {
90 ERR_raise(ERR_LIB_CMS
, ERR_R_EVP_LIB
);
94 ivlen
= EVP_CIPHER_CTX_get_iv_length(ctx
);
97 if (RAND_bytes_ex(ossl_cms_ctx_get0_libctx(cms_ctx
), iv
, ivlen
, 0) <= 0)
99 if (EVP_EncryptInit_ex(ctx
, NULL
, NULL
, NULL
, iv
) <= 0) {
100 ERR_raise(ERR_LIB_CMS
, ERR_R_EVP_LIB
);
103 encalg
->parameter
= ASN1_TYPE_new();
104 if (!encalg
->parameter
) {
105 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
108 if (EVP_CIPHER_param_to_asn1(ctx
, encalg
->parameter
) <= 0) {
109 ERR_raise(ERR_LIB_CMS
, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR
);
114 encalg
->algorithm
= OBJ_nid2obj(EVP_CIPHER_CTX_get_type(ctx
));
116 EVP_CIPHER_CTX_free(ctx
);
119 /* Initialize recipient info */
120 ri
= M_ASN1_new_of(CMS_RecipientInfo
);
124 ri
->d
.pwri
= M_ASN1_new_of(CMS_PasswordRecipientInfo
);
125 if (ri
->d
.pwri
== NULL
)
127 ri
->type
= CMS_RECIPINFO_PASS
;
130 pwri
->cms_ctx
= cms_ctx
;
131 /* Since this is overwritten, free up empty structure already there */
132 X509_ALGOR_free(pwri
->keyEncryptionAlgorithm
);
133 pwri
->keyEncryptionAlgorithm
= X509_ALGOR_new();
134 if (pwri
->keyEncryptionAlgorithm
== NULL
)
136 pwri
->keyEncryptionAlgorithm
->algorithm
= OBJ_nid2obj(wrap_nid
);
137 pwri
->keyEncryptionAlgorithm
->parameter
= ASN1_TYPE_new();
138 if (pwri
->keyEncryptionAlgorithm
->parameter
== NULL
)
141 if (!ASN1_item_pack(encalg
, ASN1_ITEM_rptr(X509_ALGOR
),
142 &pwri
->keyEncryptionAlgorithm
->parameter
->
145 pwri
->keyEncryptionAlgorithm
->parameter
->type
= V_ASN1_SEQUENCE
;
147 X509_ALGOR_free(encalg
);
150 /* Setup PBE algorithm */
152 pwri
->keyDerivationAlgorithm
= PKCS5_pbkdf2_set(iter
, NULL
, 0, -1, -1);
154 if (pwri
->keyDerivationAlgorithm
== NULL
)
157 CMS_RecipientInfo_set0_password(ri
, pass
, passlen
);
160 if (!sk_CMS_RecipientInfo_push(ris
, ri
))
166 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
168 EVP_CIPHER_CTX_free(ctx
);
170 M_ASN1_free_of(ri
, CMS_RecipientInfo
);
171 X509_ALGOR_free(encalg
);
177 * This is an implementation of the key wrapping mechanism in RFC3211, at
178 * some point this should go into EVP.
181 static int kek_unwrap_key(unsigned char *out
, size_t *outlen
,
182 const unsigned char *in
, size_t inlen
,
185 size_t blocklen
= EVP_CIPHER_CTX_get_block_size(ctx
);
188 if (inlen
< 2 * blocklen
) {
192 if (inlen
% blocklen
) {
196 if ((tmp
= OPENSSL_malloc(inlen
)) == NULL
) {
197 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
200 /* setup IV by decrypting last two blocks */
201 if (!EVP_DecryptUpdate(ctx
, tmp
+ inlen
- 2 * blocklen
, &outl
,
202 in
+ inlen
- 2 * blocklen
, blocklen
* 2)
204 * Do a decrypt of last decrypted block to set IV to correct value
205 * output it to start of buffer so we don't corrupt decrypted block
206 * this works because buffer is at least two block lengths long.
208 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
,
209 tmp
+ inlen
- blocklen
, blocklen
)
210 /* Can now decrypt first n - 1 blocks */
211 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
, in
, inlen
- blocklen
)
213 /* Reset IV to original value */
214 || !EVP_DecryptInit_ex(ctx
, NULL
, NULL
, NULL
, NULL
)
216 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
, tmp
, inlen
))
218 /* Check check bytes */
219 if (((tmp
[1] ^ tmp
[4]) & (tmp
[2] ^ tmp
[5]) & (tmp
[3] ^ tmp
[6])) != 0xff) {
220 /* Check byte failure */
223 if (inlen
< (size_t)(tmp
[0] - 4)) {
224 /* Invalid length value */
227 *outlen
= (size_t)tmp
[0];
228 memcpy(out
, tmp
+ 4, *outlen
);
231 OPENSSL_clear_free(tmp
, inlen
);
236 static int kek_wrap_key(unsigned char *out
, size_t *outlen
,
237 const unsigned char *in
, size_t inlen
,
238 EVP_CIPHER_CTX
*ctx
, const CMS_CTX
*cms_ctx
)
240 size_t blocklen
= EVP_CIPHER_CTX_get_block_size(ctx
);
244 * First decide length of output buffer: need header and round up to
245 * multiple of block length.
247 olen
= (inlen
+ 4 + blocklen
- 1) / blocklen
;
249 if (olen
< 2 * blocklen
) {
259 out
[0] = (unsigned char)inlen
;
260 out
[1] = in
[0] ^ 0xFF;
261 out
[2] = in
[1] ^ 0xFF;
262 out
[3] = in
[2] ^ 0xFF;
263 memcpy(out
+ 4, in
, inlen
);
264 /* Add random padding to end */
266 && RAND_bytes_ex(ossl_cms_ctx_get0_libctx(cms_ctx
), out
+ 4 + inlen
,
267 olen
- 4 - inlen
, 0) <= 0)
270 if (!EVP_EncryptUpdate(ctx
, out
, &dummy
, out
, olen
)
271 || !EVP_EncryptUpdate(ctx
, out
, &dummy
, out
, olen
))
280 /* Encrypt/Decrypt content key in PWRI recipient info */
282 int ossl_cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo
*cms
,
283 CMS_RecipientInfo
*ri
, int en_de
)
285 CMS_EncryptedContentInfo
*ec
;
286 CMS_PasswordRecipientInfo
*pwri
;
288 X509_ALGOR
*algtmp
, *kekalg
= NULL
;
289 EVP_CIPHER_CTX
*kekctx
= NULL
;
290 char name
[OSSL_MAX_NAME_SIZE
];
291 EVP_CIPHER
*kekcipher
;
292 unsigned char *key
= NULL
;
294 const CMS_CTX
*cms_ctx
= ossl_cms_get0_cmsctx(cms
);
296 ec
= ossl_cms_get0_env_enc_content(cms
);
300 if (pwri
->pass
== NULL
) {
301 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_PASSWORD
);
304 algtmp
= pwri
->keyEncryptionAlgorithm
;
306 if (!algtmp
|| OBJ_obj2nid(algtmp
->algorithm
) != NID_id_alg_PWRI_KEK
) {
307 ERR_raise(ERR_LIB_CMS
, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM
);
311 kekalg
= ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR
),
314 if (kekalg
== NULL
) {
315 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER
);
319 OBJ_obj2txt(name
, sizeof(name
), kekalg
->algorithm
, 0);
320 kekcipher
= EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(cms_ctx
), name
,
321 ossl_cms_ctx_get0_propq(cms_ctx
));
323 if (kekcipher
== NULL
) {
324 ERR_raise(ERR_LIB_CMS
, CMS_R_UNKNOWN_CIPHER
);
328 kekctx
= EVP_CIPHER_CTX_new();
329 if (kekctx
== NULL
) {
330 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
333 /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
334 if (!EVP_CipherInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
, en_de
))
336 EVP_CIPHER_CTX_set_padding(kekctx
, 0);
337 if (EVP_CIPHER_asn1_to_param(kekctx
, kekalg
->parameter
) <= 0) {
338 ERR_raise(ERR_LIB_CMS
, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR
);
342 algtmp
= pwri
->keyDerivationAlgorithm
;
344 /* Finish password based key derivation to setup key in "ctx" */
346 if (EVP_PBE_CipherInit(algtmp
->algorithm
,
347 (char *)pwri
->pass
, pwri
->passlen
,
348 algtmp
->parameter
, kekctx
, en_de
) < 0) {
349 ERR_raise(ERR_LIB_CMS
, ERR_R_EVP_LIB
);
353 /* Finally wrap/unwrap the key */
357 if (!kek_wrap_key(NULL
, &keylen
, ec
->key
, ec
->keylen
, kekctx
, cms_ctx
))
360 key
= OPENSSL_malloc(keylen
);
365 if (!kek_wrap_key(key
, &keylen
, ec
->key
, ec
->keylen
, kekctx
, cms_ctx
))
367 pwri
->encryptedKey
->data
= key
;
368 pwri
->encryptedKey
->length
= keylen
;
370 key
= OPENSSL_malloc(pwri
->encryptedKey
->length
);
373 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
376 if (!kek_unwrap_key(key
, &keylen
,
377 pwri
->encryptedKey
->data
,
378 pwri
->encryptedKey
->length
, kekctx
)) {
379 ERR_raise(ERR_LIB_CMS
, CMS_R_UNWRAP_FAILURE
);
383 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
392 EVP_CIPHER_free(kekcipher
);
393 EVP_CIPHER_CTX_free(kekctx
);
397 X509_ALGOR_free(kekalg
);