2 * Copyright 2009-2020 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 DEFINE_STACK_OF(CMS_RecipientInfo
)
23 int CMS_RecipientInfo_set0_password(CMS_RecipientInfo
*ri
,
24 unsigned char *pass
, ossl_ssize_t passlen
)
26 CMS_PasswordRecipientInfo
*pwri
;
27 if (ri
->type
!= CMS_RECIPINFO_PASS
) {
28 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD
, CMS_R_NOT_PWRI
);
34 if (pass
&& passlen
< 0)
35 passlen
= strlen((char *)pass
);
36 pwri
->passlen
= passlen
;
40 CMS_RecipientInfo
*CMS_add0_recipient_password(CMS_ContentInfo
*cms
,
41 int iter
, int wrap_nid
,
45 const EVP_CIPHER
*kekciph
)
47 STACK_OF(CMS_RecipientInfo
) *ris
;
48 CMS_RecipientInfo
*ri
= NULL
;
49 CMS_EncryptedContentInfo
*ec
;
50 CMS_PasswordRecipientInfo
*pwri
;
51 EVP_CIPHER_CTX
*ctx
= NULL
;
52 X509_ALGOR
*encalg
= NULL
;
53 unsigned char iv
[EVP_MAX_IV_LENGTH
];
55 const CMS_CTX
*cms_ctx
= cms_get0_cmsctx(cms
);
57 ec
= cms_get0_env_enc_content(cms
);
60 ris
= CMS_get0_RecipientInfos(cms
);
65 wrap_nid
= NID_id_alg_PWRI_KEK
;
68 pbe_nid
= NID_id_pbkdf2
;
70 /* Get from enveloped data */
74 if (kekciph
== NULL
) {
75 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, CMS_R_NO_CIPHER
);
78 if (wrap_nid
!= NID_id_alg_PWRI_KEK
) {
79 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
,
80 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM
);
84 /* Setup algorithm identifier for cipher */
85 encalg
= X509_ALGOR_new();
89 ctx
= EVP_CIPHER_CTX_new();
91 if (EVP_EncryptInit_ex(ctx
, kekciph
, NULL
, NULL
, NULL
) <= 0) {
92 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, ERR_R_EVP_LIB
);
96 ivlen
= EVP_CIPHER_CTX_iv_length(ctx
);
99 if (RAND_bytes_ex(cms_ctx
->libctx
, iv
, ivlen
) <= 0)
101 if (EVP_EncryptInit_ex(ctx
, NULL
, NULL
, NULL
, iv
) <= 0) {
102 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, ERR_R_EVP_LIB
);
105 encalg
->parameter
= ASN1_TYPE_new();
106 if (!encalg
->parameter
) {
107 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, ERR_R_MALLOC_FAILURE
);
110 if (EVP_CIPHER_param_to_asn1(ctx
, encalg
->parameter
) <= 0) {
111 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
,
112 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR
);
117 encalg
->algorithm
= OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx
));
119 EVP_CIPHER_CTX_free(ctx
);
122 /* Initialize recipient info */
123 ri
= M_ASN1_new_of(CMS_RecipientInfo
);
127 ri
->d
.pwri
= M_ASN1_new_of(CMS_PasswordRecipientInfo
);
128 if (ri
->d
.pwri
== NULL
)
130 ri
->type
= CMS_RECIPINFO_PASS
;
133 pwri
->cms_ctx
= cms_ctx
;
134 /* Since this is overwritten, free up empty structure already there */
135 X509_ALGOR_free(pwri
->keyEncryptionAlgorithm
);
136 pwri
->keyEncryptionAlgorithm
= X509_ALGOR_new();
137 if (pwri
->keyEncryptionAlgorithm
== NULL
)
139 pwri
->keyEncryptionAlgorithm
->algorithm
= OBJ_nid2obj(wrap_nid
);
140 pwri
->keyEncryptionAlgorithm
->parameter
= ASN1_TYPE_new();
141 if (pwri
->keyEncryptionAlgorithm
->parameter
== NULL
)
144 if (!ASN1_item_pack(encalg
, ASN1_ITEM_rptr(X509_ALGOR
),
145 &pwri
->keyEncryptionAlgorithm
->parameter
->
148 pwri
->keyEncryptionAlgorithm
->parameter
->type
= V_ASN1_SEQUENCE
;
150 X509_ALGOR_free(encalg
);
153 /* Setup PBE algorithm */
155 pwri
->keyDerivationAlgorithm
= PKCS5_pbkdf2_set(iter
, NULL
, 0, -1, -1);
157 if (pwri
->keyDerivationAlgorithm
== NULL
)
160 CMS_RecipientInfo_set0_password(ri
, pass
, passlen
);
163 if (!sk_CMS_RecipientInfo_push(ris
, ri
))
169 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, ERR_R_MALLOC_FAILURE
);
171 EVP_CIPHER_CTX_free(ctx
);
173 M_ASN1_free_of(ri
, CMS_RecipientInfo
);
174 X509_ALGOR_free(encalg
);
180 * This is an implementation of the key wrapping mechanism in RFC3211, at
181 * some point this should go into EVP.
184 static int kek_unwrap_key(unsigned char *out
, size_t *outlen
,
185 const unsigned char *in
, size_t inlen
,
188 size_t blocklen
= EVP_CIPHER_CTX_block_size(ctx
);
191 if (inlen
< 2 * blocklen
) {
195 if (inlen
% blocklen
) {
199 if ((tmp
= OPENSSL_malloc(inlen
)) == NULL
) {
200 CMSerr(CMS_F_KEK_UNWRAP_KEY
, ERR_R_MALLOC_FAILURE
);
203 /* setup IV by decrypting last two blocks */
204 if (!EVP_DecryptUpdate(ctx
, tmp
+ inlen
- 2 * blocklen
, &outl
,
205 in
+ inlen
- 2 * blocklen
, blocklen
* 2)
207 * Do a decrypt of last decrypted block to set IV to correct value
208 * output it to start of buffer so we don't corrupt decrypted block
209 * this works because buffer is at least two block lengths long.
211 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
,
212 tmp
+ inlen
- blocklen
, blocklen
)
213 /* Can now decrypt first n - 1 blocks */
214 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
, in
, inlen
- blocklen
)
216 /* Reset IV to original value */
217 || !EVP_DecryptInit_ex(ctx
, NULL
, NULL
, NULL
, NULL
)
219 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
, tmp
, inlen
))
221 /* Check check bytes */
222 if (((tmp
[1] ^ tmp
[4]) & (tmp
[2] ^ tmp
[5]) & (tmp
[3] ^ tmp
[6])) != 0xff) {
223 /* Check byte failure */
226 if (inlen
< (size_t)(tmp
[0] - 4)) {
227 /* Invalid length value */
230 *outlen
= (size_t)tmp
[0];
231 memcpy(out
, tmp
+ 4, *outlen
);
234 OPENSSL_clear_free(tmp
, inlen
);
239 static int kek_wrap_key(unsigned char *out
, size_t *outlen
,
240 const unsigned char *in
, size_t inlen
,
241 EVP_CIPHER_CTX
*ctx
, const CMS_CTX
*cms_ctx
)
243 size_t blocklen
= EVP_CIPHER_CTX_block_size(ctx
);
247 * First decide length of output buffer: need header and round up to
248 * multiple of block length.
250 olen
= (inlen
+ 4 + blocklen
- 1) / blocklen
;
252 if (olen
< 2 * blocklen
) {
262 out
[0] = (unsigned char)inlen
;
263 out
[1] = in
[0] ^ 0xFF;
264 out
[2] = in
[1] ^ 0xFF;
265 out
[3] = in
[2] ^ 0xFF;
266 memcpy(out
+ 4, in
, inlen
);
267 /* Add random padding to end */
269 && RAND_bytes_ex(cms_ctx
->libctx
, out
+ 4 + inlen
,
270 olen
- 4 - inlen
) <= 0)
273 if (!EVP_EncryptUpdate(ctx
, out
, &dummy
, out
, olen
)
274 || !EVP_EncryptUpdate(ctx
, out
, &dummy
, out
, olen
))
283 /* Encrypt/Decrypt content key in PWRI recipient info */
285 int cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo
*cms
,
286 CMS_RecipientInfo
*ri
, int en_de
)
288 CMS_EncryptedContentInfo
*ec
;
289 CMS_PasswordRecipientInfo
*pwri
;
291 X509_ALGOR
*algtmp
, *kekalg
= NULL
;
292 EVP_CIPHER_CTX
*kekctx
= NULL
;
294 EVP_CIPHER
*kekcipher
;
295 unsigned char *key
= NULL
;
297 const CMS_CTX
*cms_ctx
= cms_get0_cmsctx(cms
);
299 ec
= cms_get0_env_enc_content(cms
);
303 if (pwri
->pass
== NULL
) {
304 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, CMS_R_NO_PASSWORD
);
307 algtmp
= pwri
->keyEncryptionAlgorithm
;
309 if (!algtmp
|| OBJ_obj2nid(algtmp
->algorithm
) != NID_id_alg_PWRI_KEK
) {
310 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
,
311 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM
);
315 kekalg
= ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR
),
318 if (kekalg
== NULL
) {
319 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
,
320 CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER
);
324 name
= OBJ_nid2sn(OBJ_obj2nid(kekalg
->algorithm
));
325 kekcipher
= EVP_CIPHER_fetch(cms_ctx
->libctx
, name
, cms_ctx
->propq
);
327 if (kekcipher
== NULL
) {
328 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, CMS_R_UNKNOWN_CIPHER
);
332 kekctx
= EVP_CIPHER_CTX_new();
333 if (kekctx
== NULL
) {
334 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, ERR_R_MALLOC_FAILURE
);
337 /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
338 if (!EVP_CipherInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
, en_de
))
340 EVP_CIPHER_CTX_set_padding(kekctx
, 0);
341 if (EVP_CIPHER_asn1_to_param(kekctx
, kekalg
->parameter
) <= 0) {
342 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
,
343 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR
);
347 algtmp
= pwri
->keyDerivationAlgorithm
;
349 /* Finish password based key derivation to setup key in "ctx" */
351 if (EVP_PBE_CipherInit(algtmp
->algorithm
,
352 (char *)pwri
->pass
, pwri
->passlen
,
353 algtmp
->parameter
, kekctx
, en_de
) < 0) {
354 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, ERR_R_EVP_LIB
);
358 /* Finally wrap/unwrap the key */
362 if (!kek_wrap_key(NULL
, &keylen
, ec
->key
, ec
->keylen
, kekctx
, cms_ctx
))
365 key
= OPENSSL_malloc(keylen
);
370 if (!kek_wrap_key(key
, &keylen
, ec
->key
, ec
->keylen
, kekctx
, cms_ctx
))
372 pwri
->encryptedKey
->data
= key
;
373 pwri
->encryptedKey
->length
= keylen
;
375 key
= OPENSSL_malloc(pwri
->encryptedKey
->length
);
378 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, ERR_R_MALLOC_FAILURE
);
381 if (!kek_unwrap_key(key
, &keylen
,
382 pwri
->encryptedKey
->data
,
383 pwri
->encryptedKey
->length
, kekctx
)) {
384 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, CMS_R_UNWRAP_FAILURE
);
388 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
397 EVP_CIPHER_free(kekcipher
);
398 EVP_CIPHER_CTX_free(kekctx
);
402 X509_ALGOR_free(kekalg
);