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 "cms_local.h"
19 #include "crypto/asn1.h"
21 int CMS_RecipientInfo_set0_password(CMS_RecipientInfo
*ri
,
22 unsigned char *pass
, ossl_ssize_t passlen
)
24 CMS_PasswordRecipientInfo
*pwri
;
25 if (ri
->type
!= CMS_RECIPINFO_PASS
) {
26 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_PWRI
);
32 if (pass
&& passlen
< 0)
33 passlen
= strlen((char *)pass
);
34 pwri
->passlen
= passlen
;
38 CMS_RecipientInfo
*CMS_add0_recipient_password(CMS_ContentInfo
*cms
,
39 int iter
, int wrap_nid
,
43 const EVP_CIPHER
*kekciph
)
45 STACK_OF(CMS_RecipientInfo
) *ris
;
46 CMS_RecipientInfo
*ri
= NULL
;
47 CMS_EncryptedContentInfo
*ec
;
48 CMS_PasswordRecipientInfo
*pwri
;
49 EVP_CIPHER_CTX
*ctx
= NULL
;
50 X509_ALGOR
*encalg
= NULL
;
51 unsigned char iv
[EVP_MAX_IV_LENGTH
];
53 const CMS_CTX
*cms_ctx
= ossl_cms_get0_cmsctx(cms
);
55 ec
= ossl_cms_get0_env_enc_content(cms
);
58 ris
= CMS_get0_RecipientInfos(cms
);
63 wrap_nid
= NID_id_alg_PWRI_KEK
;
66 pbe_nid
= NID_id_pbkdf2
;
68 /* Get from enveloped data */
72 if (kekciph
== NULL
) {
73 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_CIPHER
);
76 if (wrap_nid
!= NID_id_alg_PWRI_KEK
) {
77 ERR_raise(ERR_LIB_CMS
, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM
);
81 /* Setup algorithm identifier for cipher */
82 encalg
= X509_ALGOR_new();
86 ctx
= EVP_CIPHER_CTX_new();
88 if (EVP_EncryptInit_ex(ctx
, kekciph
, NULL
, NULL
, NULL
) <= 0) {
89 ERR_raise(ERR_LIB_CMS
, ERR_R_EVP_LIB
);
93 ivlen
= EVP_CIPHER_CTX_iv_length(ctx
);
96 if (RAND_bytes_ex(ossl_cms_ctx_get0_libctx(cms_ctx
), iv
, ivlen
) <= 0)
98 if (EVP_EncryptInit_ex(ctx
, NULL
, NULL
, NULL
, iv
) <= 0) {
99 ERR_raise(ERR_LIB_CMS
, ERR_R_EVP_LIB
);
102 encalg
->parameter
= ASN1_TYPE_new();
103 if (!encalg
->parameter
) {
104 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
107 if (EVP_CIPHER_param_to_asn1(ctx
, encalg
->parameter
) <= 0) {
108 ERR_raise(ERR_LIB_CMS
, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR
);
113 encalg
->algorithm
= OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx
));
115 EVP_CIPHER_CTX_free(ctx
);
118 /* Initialize recipient info */
119 ri
= M_ASN1_new_of(CMS_RecipientInfo
);
123 ri
->d
.pwri
= M_ASN1_new_of(CMS_PasswordRecipientInfo
);
124 if (ri
->d
.pwri
== NULL
)
126 ri
->type
= CMS_RECIPINFO_PASS
;
129 pwri
->cms_ctx
= cms_ctx
;
130 /* Since this is overwritten, free up empty structure already there */
131 X509_ALGOR_free(pwri
->keyEncryptionAlgorithm
);
132 pwri
->keyEncryptionAlgorithm
= X509_ALGOR_new();
133 if (pwri
->keyEncryptionAlgorithm
== NULL
)
135 pwri
->keyEncryptionAlgorithm
->algorithm
= OBJ_nid2obj(wrap_nid
);
136 pwri
->keyEncryptionAlgorithm
->parameter
= ASN1_TYPE_new();
137 if (pwri
->keyEncryptionAlgorithm
->parameter
== NULL
)
140 if (!ASN1_item_pack(encalg
, ASN1_ITEM_rptr(X509_ALGOR
),
141 &pwri
->keyEncryptionAlgorithm
->parameter
->
144 pwri
->keyEncryptionAlgorithm
->parameter
->type
= V_ASN1_SEQUENCE
;
146 X509_ALGOR_free(encalg
);
149 /* Setup PBE algorithm */
151 pwri
->keyDerivationAlgorithm
= PKCS5_pbkdf2_set(iter
, NULL
, 0, -1, -1);
153 if (pwri
->keyDerivationAlgorithm
== NULL
)
156 CMS_RecipientInfo_set0_password(ri
, pass
, passlen
);
159 if (!sk_CMS_RecipientInfo_push(ris
, ri
))
165 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
167 EVP_CIPHER_CTX_free(ctx
);
169 M_ASN1_free_of(ri
, CMS_RecipientInfo
);
170 X509_ALGOR_free(encalg
);
176 * This is an implementation of the key wrapping mechanism in RFC3211, at
177 * some point this should go into EVP.
180 static int kek_unwrap_key(unsigned char *out
, size_t *outlen
,
181 const unsigned char *in
, size_t inlen
,
184 size_t blocklen
= EVP_CIPHER_CTX_block_size(ctx
);
187 if (inlen
< 2 * blocklen
) {
191 if (inlen
% blocklen
) {
195 if ((tmp
= OPENSSL_malloc(inlen
)) == NULL
) {
196 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
199 /* setup IV by decrypting last two blocks */
200 if (!EVP_DecryptUpdate(ctx
, tmp
+ inlen
- 2 * blocklen
, &outl
,
201 in
+ inlen
- 2 * blocklen
, blocklen
* 2)
203 * Do a decrypt of last decrypted block to set IV to correct value
204 * output it to start of buffer so we don't corrupt decrypted block
205 * this works because buffer is at least two block lengths long.
207 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
,
208 tmp
+ inlen
- blocklen
, blocklen
)
209 /* Can now decrypt first n - 1 blocks */
210 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
, in
, inlen
- blocklen
)
212 /* Reset IV to original value */
213 || !EVP_DecryptInit_ex(ctx
, NULL
, NULL
, NULL
, NULL
)
215 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
, tmp
, inlen
))
217 /* Check check bytes */
218 if (((tmp
[1] ^ tmp
[4]) & (tmp
[2] ^ tmp
[5]) & (tmp
[3] ^ tmp
[6])) != 0xff) {
219 /* Check byte failure */
222 if (inlen
< (size_t)(tmp
[0] - 4)) {
223 /* Invalid length value */
226 *outlen
= (size_t)tmp
[0];
227 memcpy(out
, tmp
+ 4, *outlen
);
230 OPENSSL_clear_free(tmp
, inlen
);
235 static int kek_wrap_key(unsigned char *out
, size_t *outlen
,
236 const unsigned char *in
, size_t inlen
,
237 EVP_CIPHER_CTX
*ctx
, const CMS_CTX
*cms_ctx
)
239 size_t blocklen
= EVP_CIPHER_CTX_block_size(ctx
);
243 * First decide length of output buffer: need header and round up to
244 * multiple of block length.
246 olen
= (inlen
+ 4 + blocklen
- 1) / blocklen
;
248 if (olen
< 2 * blocklen
) {
258 out
[0] = (unsigned char)inlen
;
259 out
[1] = in
[0] ^ 0xFF;
260 out
[2] = in
[1] ^ 0xFF;
261 out
[3] = in
[2] ^ 0xFF;
262 memcpy(out
+ 4, in
, inlen
);
263 /* Add random padding to end */
265 && RAND_bytes_ex(ossl_cms_ctx_get0_libctx(cms_ctx
), out
+ 4 + inlen
,
266 olen
- 4 - inlen
) <= 0)
269 if (!EVP_EncryptUpdate(ctx
, out
, &dummy
, out
, olen
)
270 || !EVP_EncryptUpdate(ctx
, out
, &dummy
, out
, olen
))
279 /* Encrypt/Decrypt content key in PWRI recipient info */
281 int ossl_cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo
*cms
,
282 CMS_RecipientInfo
*ri
, int en_de
)
284 CMS_EncryptedContentInfo
*ec
;
285 CMS_PasswordRecipientInfo
*pwri
;
287 X509_ALGOR
*algtmp
, *kekalg
= NULL
;
288 EVP_CIPHER_CTX
*kekctx
= NULL
;
290 EVP_CIPHER
*kekcipher
;
291 unsigned char *key
= NULL
;
293 const CMS_CTX
*cms_ctx
= ossl_cms_get0_cmsctx(cms
);
295 ec
= ossl_cms_get0_env_enc_content(cms
);
299 if (pwri
->pass
== NULL
) {
300 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_PASSWORD
);
303 algtmp
= pwri
->keyEncryptionAlgorithm
;
305 if (!algtmp
|| OBJ_obj2nid(algtmp
->algorithm
) != NID_id_alg_PWRI_KEK
) {
306 ERR_raise(ERR_LIB_CMS
, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM
);
310 kekalg
= ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR
),
313 if (kekalg
== NULL
) {
314 ERR_raise(ERR_LIB_CMS
, CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER
);
318 name
= OBJ_nid2sn(OBJ_obj2nid(kekalg
->algorithm
));
319 kekcipher
= EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(cms_ctx
), name
,
320 ossl_cms_ctx_get0_propq(cms_ctx
));
322 if (kekcipher
== NULL
) {
323 ERR_raise(ERR_LIB_CMS
, CMS_R_UNKNOWN_CIPHER
);
327 kekctx
= EVP_CIPHER_CTX_new();
328 if (kekctx
== NULL
) {
329 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
332 /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
333 if (!EVP_CipherInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
, en_de
))
335 EVP_CIPHER_CTX_set_padding(kekctx
, 0);
336 if (EVP_CIPHER_asn1_to_param(kekctx
, kekalg
->parameter
) <= 0) {
337 ERR_raise(ERR_LIB_CMS
, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR
);
341 algtmp
= pwri
->keyDerivationAlgorithm
;
343 /* Finish password based key derivation to setup key in "ctx" */
345 if (EVP_PBE_CipherInit(algtmp
->algorithm
,
346 (char *)pwri
->pass
, pwri
->passlen
,
347 algtmp
->parameter
, kekctx
, en_de
) < 0) {
348 ERR_raise(ERR_LIB_CMS
, ERR_R_EVP_LIB
);
352 /* Finally wrap/unwrap the key */
356 if (!kek_wrap_key(NULL
, &keylen
, ec
->key
, ec
->keylen
, kekctx
, cms_ctx
))
359 key
= OPENSSL_malloc(keylen
);
364 if (!kek_wrap_key(key
, &keylen
, ec
->key
, ec
->keylen
, kekctx
, cms_ctx
))
366 pwri
->encryptedKey
->data
= key
;
367 pwri
->encryptedKey
->length
= keylen
;
369 key
= OPENSSL_malloc(pwri
->encryptedKey
->length
);
372 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
375 if (!kek_unwrap_key(key
, &keylen
,
376 pwri
->encryptedKey
->data
,
377 pwri
->encryptedKey
->length
, kekctx
)) {
378 ERR_raise(ERR_LIB_CMS
, CMS_R_UNWRAP_FAILURE
);
382 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
391 EVP_CIPHER_free(kekcipher
);
392 EVP_CIPHER_CTX_free(kekctx
);
396 X509_ALGOR_free(kekalg
);