2 * Copyright 2009-2018 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 CMS_RecipientInfo
*ri
= NULL
;
46 CMS_EnvelopedData
*env
;
47 CMS_PasswordRecipientInfo
*pwri
;
48 EVP_CIPHER_CTX
*ctx
= NULL
;
49 X509_ALGOR
*encalg
= NULL
;
50 unsigned char iv
[EVP_MAX_IV_LENGTH
];
53 env
= cms_get0_enveloped(cms
);
58 wrap_nid
= NID_id_alg_PWRI_KEK
;
61 pbe_nid
= NID_id_pbkdf2
;
63 /* Get from enveloped data */
65 kekciph
= env
->encryptedContentInfo
->cipher
;
67 if (kekciph
== NULL
) {
68 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, CMS_R_NO_CIPHER
);
71 if (wrap_nid
!= NID_id_alg_PWRI_KEK
) {
72 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
,
73 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM
);
77 /* Setup algorithm identifier for cipher */
78 encalg
= X509_ALGOR_new();
82 ctx
= EVP_CIPHER_CTX_new();
84 if (EVP_EncryptInit_ex(ctx
, kekciph
, NULL
, NULL
, NULL
) <= 0) {
85 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, ERR_R_EVP_LIB
);
89 ivlen
= EVP_CIPHER_CTX_iv_length(ctx
);
92 if (RAND_bytes(iv
, ivlen
) <= 0)
94 if (EVP_EncryptInit_ex(ctx
, NULL
, NULL
, NULL
, iv
) <= 0) {
95 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, ERR_R_EVP_LIB
);
98 encalg
->parameter
= ASN1_TYPE_new();
99 if (!encalg
->parameter
) {
100 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, ERR_R_MALLOC_FAILURE
);
103 if (EVP_CIPHER_param_to_asn1(ctx
, encalg
->parameter
) <= 0) {
104 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
,
105 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR
);
110 encalg
->algorithm
= OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx
));
112 EVP_CIPHER_CTX_free(ctx
);
115 /* Initialize recipient info */
116 ri
= M_ASN1_new_of(CMS_RecipientInfo
);
120 ri
->d
.pwri
= M_ASN1_new_of(CMS_PasswordRecipientInfo
);
121 if (ri
->d
.pwri
== NULL
)
123 ri
->type
= CMS_RECIPINFO_PASS
;
126 /* Since this is overwritten, free up empty structure already there */
127 X509_ALGOR_free(pwri
->keyEncryptionAlgorithm
);
128 pwri
->keyEncryptionAlgorithm
= X509_ALGOR_new();
129 if (pwri
->keyEncryptionAlgorithm
== NULL
)
131 pwri
->keyEncryptionAlgorithm
->algorithm
= OBJ_nid2obj(wrap_nid
);
132 pwri
->keyEncryptionAlgorithm
->parameter
= ASN1_TYPE_new();
133 if (pwri
->keyEncryptionAlgorithm
->parameter
== NULL
)
136 if (!ASN1_item_pack(encalg
, ASN1_ITEM_rptr(X509_ALGOR
),
137 &pwri
->keyEncryptionAlgorithm
->parameter
->
140 pwri
->keyEncryptionAlgorithm
->parameter
->type
= V_ASN1_SEQUENCE
;
142 X509_ALGOR_free(encalg
);
145 /* Setup PBE algorithm */
147 pwri
->keyDerivationAlgorithm
= PKCS5_pbkdf2_set(iter
, NULL
, 0, -1, -1);
149 if (!pwri
->keyDerivationAlgorithm
)
152 CMS_RecipientInfo_set0_password(ri
, pass
, passlen
);
155 if (!sk_CMS_RecipientInfo_push(env
->recipientInfos
, ri
))
161 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD
, ERR_R_MALLOC_FAILURE
);
163 EVP_CIPHER_CTX_free(ctx
);
165 M_ASN1_free_of(ri
, CMS_RecipientInfo
);
166 X509_ALGOR_free(encalg
);
172 * This is an implementation of the key wrapping mechanism in RFC3211, at
173 * some point this should go into EVP.
176 static int kek_unwrap_key(unsigned char *out
, size_t *outlen
,
177 const unsigned char *in
, size_t inlen
,
180 size_t blocklen
= EVP_CIPHER_CTX_block_size(ctx
);
183 if (inlen
< 2 * blocklen
) {
187 if (inlen
% blocklen
) {
191 if ((tmp
= OPENSSL_malloc(inlen
)) == NULL
) {
192 CMSerr(CMS_F_KEK_UNWRAP_KEY
, ERR_R_MALLOC_FAILURE
);
195 /* setup IV by decrypting last two blocks */
196 if (!EVP_DecryptUpdate(ctx
, tmp
+ inlen
- 2 * blocklen
, &outl
,
197 in
+ inlen
- 2 * blocklen
, blocklen
* 2)
199 * Do a decrypt of last decrypted block to set IV to correct value
200 * output it to start of buffer so we don't corrupt decrypted block
201 * this works because buffer is at least two block lengths long.
203 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
,
204 tmp
+ inlen
- blocklen
, blocklen
)
205 /* Can now decrypt first n - 1 blocks */
206 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
, in
, inlen
- blocklen
)
208 /* Reset IV to original value */
209 || !EVP_DecryptInit_ex(ctx
, NULL
, NULL
, NULL
, NULL
)
211 || !EVP_DecryptUpdate(ctx
, tmp
, &outl
, tmp
, inlen
))
213 /* Check check bytes */
214 if (((tmp
[1] ^ tmp
[4]) & (tmp
[2] ^ tmp
[5]) & (tmp
[3] ^ tmp
[6])) != 0xff) {
215 /* Check byte failure */
218 if (inlen
< (size_t)(tmp
[0] - 4)) {
219 /* Invalid length value */
222 *outlen
= (size_t)tmp
[0];
223 memcpy(out
, tmp
+ 4, *outlen
);
226 OPENSSL_clear_free(tmp
, inlen
);
231 static int kek_wrap_key(unsigned char *out
, size_t *outlen
,
232 const unsigned char *in
, size_t inlen
,
235 size_t blocklen
= EVP_CIPHER_CTX_block_size(ctx
);
239 * First decide length of output buffer: need header and round up to
240 * multiple of block length.
242 olen
= (inlen
+ 4 + blocklen
- 1) / blocklen
;
244 if (olen
< 2 * blocklen
) {
254 out
[0] = (unsigned char)inlen
;
255 out
[1] = in
[0] ^ 0xFF;
256 out
[2] = in
[1] ^ 0xFF;
257 out
[3] = in
[2] ^ 0xFF;
258 memcpy(out
+ 4, in
, inlen
);
259 /* Add random padding to end */
261 && RAND_bytes(out
+ 4 + inlen
, olen
- 4 - inlen
) <= 0)
264 if (!EVP_EncryptUpdate(ctx
, out
, &dummy
, out
, olen
)
265 || !EVP_EncryptUpdate(ctx
, out
, &dummy
, out
, olen
))
274 /* Encrypt/Decrypt content key in PWRI recipient info */
276 int cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo
*cms
, CMS_RecipientInfo
*ri
,
279 CMS_EncryptedContentInfo
*ec
;
280 CMS_PasswordRecipientInfo
*pwri
;
282 X509_ALGOR
*algtmp
, *kekalg
= NULL
;
283 EVP_CIPHER_CTX
*kekctx
= NULL
;
284 const EVP_CIPHER
*kekcipher
;
285 unsigned char *key
= NULL
;
288 ec
= cms
->d
.envelopedData
->encryptedContentInfo
;
293 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, CMS_R_NO_PASSWORD
);
296 algtmp
= pwri
->keyEncryptionAlgorithm
;
298 if (!algtmp
|| OBJ_obj2nid(algtmp
->algorithm
) != NID_id_alg_PWRI_KEK
) {
299 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
,
300 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM
);
304 kekalg
= ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR
),
307 if (kekalg
== NULL
) {
308 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
,
309 CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER
);
313 kekcipher
= EVP_get_cipherbyobj(kekalg
->algorithm
);
316 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, CMS_R_UNKNOWN_CIPHER
);
320 kekctx
= EVP_CIPHER_CTX_new();
321 if (kekctx
== NULL
) {
322 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, ERR_R_MALLOC_FAILURE
);
325 /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
326 if (!EVP_CipherInit_ex(kekctx
, kekcipher
, NULL
, NULL
, NULL
, en_de
))
328 EVP_CIPHER_CTX_set_padding(kekctx
, 0);
329 if (EVP_CIPHER_asn1_to_param(kekctx
, kekalg
->parameter
) <= 0) {
330 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
,
331 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR
);
335 algtmp
= pwri
->keyDerivationAlgorithm
;
337 /* Finish password based key derivation to setup key in "ctx" */
339 if (EVP_PBE_CipherInit(algtmp
->algorithm
,
340 (char *)pwri
->pass
, pwri
->passlen
,
341 algtmp
->parameter
, kekctx
, en_de
) < 0) {
342 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, ERR_R_EVP_LIB
);
346 /* Finally wrap/unwrap the key */
350 if (!kek_wrap_key(NULL
, &keylen
, ec
->key
, ec
->keylen
, kekctx
))
353 key
= OPENSSL_malloc(keylen
);
358 if (!kek_wrap_key(key
, &keylen
, ec
->key
, ec
->keylen
, kekctx
))
360 pwri
->encryptedKey
->data
= key
;
361 pwri
->encryptedKey
->length
= keylen
;
363 key
= OPENSSL_malloc(pwri
->encryptedKey
->length
);
366 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, ERR_R_MALLOC_FAILURE
);
369 if (!kek_unwrap_key(key
, &keylen
,
370 pwri
->encryptedKey
->data
,
371 pwri
->encryptedKey
->length
, kekctx
)) {
372 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT
, CMS_R_UNWRAP_FAILURE
);
376 OPENSSL_clear_free(ec
->key
, ec
->keylen
);
386 EVP_CIPHER_CTX_free(kekctx
);
390 X509_ALGOR_free(kekalg
);