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 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 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD
, 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
= cms_get0_cmsctx(cms
);
55 ec
= 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 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, CMS_R_NO_CIPHER
);
76 if (wrap_nid
!= NID_id_alg_PWRI_KEK
) {
77 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
,
78 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 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, ERR_R_EVP_LIB
);
94 ivlen
= EVP_CIPHER_CTX_iv_length(ctx
);
97 if (RAND_bytes_ex(cms_ctx
->libctx
, iv
, ivlen
) <= 0)
99 if (EVP_EncryptInit_ex(ctx
, NULL
, NULL
, NULL
, iv
) <= 0) {
100 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, ERR_R_EVP_LIB
);
103 encalg
->parameter
= ASN1_TYPE_new();
104 if (!encalg
->parameter
) {
105 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, ERR_R_MALLOC_FAILURE
);
108 if (EVP_CIPHER_param_to_asn1(ctx
, encalg
->parameter
) <= 0) {
109 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
,
110 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR
);
115 encalg
->algorithm
= OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx
));
117 EVP_CIPHER_CTX_free(ctx
);
120 /* Initialize recipient info */
121 ri
= M_ASN1_new_of(CMS_RecipientInfo
);
125 ri
->d
.pwri
= M_ASN1_new_of(CMS_PasswordRecipientInfo
);
126 if (ri
->d
.pwri
== NULL
)
128 ri
->type
= CMS_RECIPINFO_PASS
;
131 pwri
->cms_ctx
= cms_ctx
;
132 /* Since this is overwritten, free up empty structure already there */
133 X509_ALGOR_free(pwri
->keyEncryptionAlgorithm
);
134 pwri
->keyEncryptionAlgorithm
= X509_ALGOR_new();
135 if (pwri
->keyEncryptionAlgorithm
== NULL
)
137 pwri
->keyEncryptionAlgorithm
->algorithm
= OBJ_nid2obj(wrap_nid
);
138 pwri
->keyEncryptionAlgorithm
->parameter
= ASN1_TYPE_new();
139 if (pwri
->keyEncryptionAlgorithm
->parameter
== NULL
)
142 if (!ASN1_item_pack(encalg
, ASN1_ITEM_rptr(X509_ALGOR
),
143 &pwri
->keyEncryptionAlgorithm
->parameter
->
146 pwri
->keyEncryptionAlgorithm
->parameter
->type
= V_ASN1_SEQUENCE
;
148 X509_ALGOR_free(encalg
);
151 /* Setup PBE algorithm */
153 pwri
->keyDerivationAlgorithm
= PKCS5_pbkdf2_set(iter
, NULL
, 0, -1, -1);
155 if (pwri
->keyDerivationAlgorithm
== NULL
)
158 CMS_RecipientInfo_set0_password(ri
, pass
, passlen
);
161 if (!sk_CMS_RecipientInfo_push(ris
, ri
))
167 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, ERR_R_MALLOC_FAILURE
);
169 EVP_CIPHER_CTX_free(ctx
);
171 M_ASN1_free_of(ri
, CMS_RecipientInfo
);
172 X509_ALGOR_free(encalg
);
178 * This is an implementation of the key wrapping mechanism in RFC3211, at
179 * some point this should go into EVP.
182 static int kek_unwrap_key(unsigned char *out
, size_t *outlen
,
183 const unsigned char *in
, size_t inlen
,
186 size_t blocklen
= EVP_CIPHER_CTX_block_size(ctx
);
189 if (inlen
< 2 * blocklen
) {
193 if (inlen
% blocklen
) {
197 if ((tmp
= OPENSSL_malloc(inlen
)) == NULL
) {
198 CMSerr(CMS_F_KEK_UNWRAP_KEY
, ERR_R_MALLOC_FAILURE
);
201 /* setup IV by decrypting last two blocks */
202 if (!EVP_DecryptUpdate(ctx
, tmp
+ inlen
- 2 * blocklen
, &outl
,
203 in
+ inlen
- 2 * blocklen
, blocklen
* 2)
205 * Do a decrypt of last decrypted block to set IV to correct value
206 * output it to start of buffer so we don't corrupt decrypted block
207 * this works because buffer is at least two block lengths long.
209 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
,
210 tmp
+ inlen
- blocklen
, blocklen
)
211 /* Can now decrypt first n - 1 blocks */
212 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
, in
, inlen
- blocklen
)
214 /* Reset IV to original value */
215 || !EVP_DecryptInit_ex(ctx
, NULL
, NULL
, NULL
, NULL
)
217 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
, tmp
, inlen
))
219 /* Check check bytes */
220 if (((tmp
[1] ^ tmp
[4]) & (tmp
[2] ^ tmp
[5]) & (tmp
[3] ^ tmp
[6])) != 0xff) {
221 /* Check byte failure */
224 if (inlen
< (size_t)(tmp
[0] - 4)) {
225 /* Invalid length value */
228 *outlen
= (size_t)tmp
[0];
229 memcpy(out
, tmp
+ 4, *outlen
);
232 OPENSSL_clear_free(tmp
, inlen
);
237 static int kek_wrap_key(unsigned char *out
, size_t *outlen
,
238 const unsigned char *in
, size_t inlen
,
239 EVP_CIPHER_CTX
*ctx
, const CMS_CTX
*cms_ctx
)
241 size_t blocklen
= EVP_CIPHER_CTX_block_size(ctx
);
245 * First decide length of output buffer: need header and round up to
246 * multiple of block length.
248 olen
= (inlen
+ 4 + blocklen
- 1) / blocklen
;
250 if (olen
< 2 * blocklen
) {
260 out
[0] = (unsigned char)inlen
;
261 out
[1] = in
[0] ^ 0xFF;
262 out
[2] = in
[1] ^ 0xFF;
263 out
[3] = in
[2] ^ 0xFF;
264 memcpy(out
+ 4, in
, inlen
);
265 /* Add random padding to end */
267 && RAND_bytes_ex(cms_ctx
->libctx
, out
+ 4 + inlen
,
268 olen
- 4 - inlen
) <= 0)
271 if (!EVP_EncryptUpdate(ctx
, out
, &dummy
, out
, olen
)
272 || !EVP_EncryptUpdate(ctx
, out
, &dummy
, out
, olen
))
281 /* Encrypt/Decrypt content key in PWRI recipient info */
283 int cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo
*cms
,
284 CMS_RecipientInfo
*ri
, int en_de
)
286 CMS_EncryptedContentInfo
*ec
;
287 CMS_PasswordRecipientInfo
*pwri
;
289 X509_ALGOR
*algtmp
, *kekalg
= NULL
;
290 EVP_CIPHER_CTX
*kekctx
= NULL
;
292 EVP_CIPHER
*kekcipher
;
293 unsigned char *key
= NULL
;
295 const CMS_CTX
*cms_ctx
= cms_get0_cmsctx(cms
);
297 ec
= cms_get0_env_enc_content(cms
);
301 if (pwri
->pass
== NULL
) {
302 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, CMS_R_NO_PASSWORD
);
305 algtmp
= pwri
->keyEncryptionAlgorithm
;
307 if (!algtmp
|| OBJ_obj2nid(algtmp
->algorithm
) != NID_id_alg_PWRI_KEK
) {
308 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
,
309 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM
);
313 kekalg
= ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR
),
316 if (kekalg
== NULL
) {
317 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
,
318 CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER
);
322 name
= OBJ_nid2sn(OBJ_obj2nid(kekalg
->algorithm
));
323 kekcipher
= EVP_CIPHER_fetch(cms_ctx
->libctx
, name
, cms_ctx
->propq
);
325 if (kekcipher
== NULL
) {
326 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, CMS_R_UNKNOWN_CIPHER
);
330 kekctx
= EVP_CIPHER_CTX_new();
331 if (kekctx
== NULL
) {
332 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, ERR_R_MALLOC_FAILURE
);
335 /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
336 if (!EVP_CipherInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
, en_de
))
338 EVP_CIPHER_CTX_set_padding(kekctx
, 0);
339 if (EVP_CIPHER_asn1_to_param(kekctx
, kekalg
->parameter
) <= 0) {
340 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
,
341 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR
);
345 algtmp
= pwri
->keyDerivationAlgorithm
;
347 /* Finish password based key derivation to setup key in "ctx" */
349 if (EVP_PBE_CipherInit(algtmp
->algorithm
,
350 (char *)pwri
->pass
, pwri
->passlen
,
351 algtmp
->parameter
, kekctx
, en_de
) < 0) {
352 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, ERR_R_EVP_LIB
);
356 /* Finally wrap/unwrap the key */
360 if (!kek_wrap_key(NULL
, &keylen
, ec
->key
, ec
->keylen
, kekctx
, cms_ctx
))
363 key
= OPENSSL_malloc(keylen
);
368 if (!kek_wrap_key(key
, &keylen
, ec
->key
, ec
->keylen
, kekctx
, cms_ctx
))
370 pwri
->encryptedKey
->data
= key
;
371 pwri
->encryptedKey
->length
= keylen
;
373 key
= OPENSSL_malloc(pwri
->encryptedKey
->length
);
376 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, ERR_R_MALLOC_FAILURE
);
379 if (!kek_unwrap_key(key
, &keylen
,
380 pwri
->encryptedKey
->data
,
381 pwri
->encryptedKey
->length
, kekctx
)) {
382 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, CMS_R_UNWRAP_FAILURE
);
386 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
395 EVP_CIPHER_free(kekcipher
);
396 EVP_CIPHER_CTX_free(kekctx
);
400 X509_ALGOR_free(kekalg
);