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 CMS_RecipientInfo *ri = NULL;
48 CMS_EnvelopedData *env;
49 CMS_PasswordRecipientInfo *pwri;
50 EVP_CIPHER_CTX *ctx = NULL;
51 X509_ALGOR *encalg = NULL;
52 unsigned char iv[EVP_MAX_IV_LENGTH];
55 env = cms_get0_enveloped(cms);
60 wrap_nid = NID_id_alg_PWRI_KEK;
63 pbe_nid = NID_id_pbkdf2;
65 /* Get from enveloped data */
67 kekciph = env->encryptedContentInfo->cipher;
69 if (kekciph == NULL) {
70 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, CMS_R_NO_CIPHER);
73 if (wrap_nid != NID_id_alg_PWRI_KEK) {
74 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
75 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
79 /* Setup algorithm identifier for cipher */
80 encalg = X509_ALGOR_new();
84 ctx = EVP_CIPHER_CTX_new();
86 if (EVP_EncryptInit_ex(ctx, kekciph, NULL, NULL, NULL) <= 0) {
87 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB);
91 ivlen = EVP_CIPHER_CTX_iv_length(ctx);
94 if (RAND_bytes(iv, ivlen) <= 0)
96 if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0) {
97 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB);
100 encalg->parameter = ASN1_TYPE_new();
101 if (!encalg->parameter) {
102 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE);
105 if (EVP_CIPHER_param_to_asn1(ctx, encalg->parameter) <= 0) {
106 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
107 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
112 encalg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
114 EVP_CIPHER_CTX_free(ctx);
117 /* Initialize recipient info */
118 ri = M_ASN1_new_of(CMS_RecipientInfo);
122 ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo);
123 if (ri->d.pwri == NULL)
125 ri->type = CMS_RECIPINFO_PASS;
128 /* Since this is overwritten, free up empty structure already there */
129 X509_ALGOR_free(pwri->keyEncryptionAlgorithm);
130 pwri->keyEncryptionAlgorithm = X509_ALGOR_new();
131 if (pwri->keyEncryptionAlgorithm == NULL)
133 pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid);
134 pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new();
135 if (pwri->keyEncryptionAlgorithm->parameter == NULL)
138 if (!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR),
139 &pwri->keyEncryptionAlgorithm->parameter->
142 pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE;
144 X509_ALGOR_free(encalg);
147 /* Setup PBE algorithm */
149 pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set(iter, NULL, 0, -1, -1);
151 if (pwri->keyDerivationAlgorithm == NULL)
154 CMS_RecipientInfo_set0_password(ri, pass, passlen);
157 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
163 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE);
165 EVP_CIPHER_CTX_free(ctx);
167 M_ASN1_free_of(ri, CMS_RecipientInfo);
168 X509_ALGOR_free(encalg);
174 * This is an implementation of the key wrapping mechanism in RFC3211, at
175 * some point this should go into EVP.
178 static int kek_unwrap_key(unsigned char *out, size_t *outlen,
179 const unsigned char *in, size_t inlen,
182 size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
185 if (inlen < 2 * blocklen) {
189 if (inlen % blocklen) {
193 if ((tmp = OPENSSL_malloc(inlen)) == NULL) {
194 CMSerr(CMS_F_KEK_UNWRAP_KEY, ERR_R_MALLOC_FAILURE);
197 /* setup IV by decrypting last two blocks */
198 if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
199 in + inlen - 2 * blocklen, blocklen * 2)
201 * Do a decrypt of last decrypted block to set IV to correct value
202 * output it to start of buffer so we don't corrupt decrypted block
203 * this works because buffer is at least two block lengths long.
205 || !EVP_DecryptUpdate(ctx, tmp, &outl,
206 tmp + inlen - blocklen, blocklen)
207 /* Can now decrypt first n - 1 blocks */
208 || !EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen)
210 /* Reset IV to original value */
211 || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)
213 || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))
215 /* Check check bytes */
216 if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {
217 /* Check byte failure */
220 if (inlen < (size_t)(tmp[0] - 4)) {
221 /* Invalid length value */
224 *outlen = (size_t)tmp[0];
225 memcpy(out, tmp + 4, *outlen);
228 OPENSSL_clear_free(tmp, inlen);
233 static int kek_wrap_key(unsigned char *out, size_t *outlen,
234 const unsigned char *in, size_t inlen,
237 size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
241 * First decide length of output buffer: need header and round up to
242 * multiple of block length.
244 olen = (inlen + 4 + blocklen - 1) / blocklen;
246 if (olen < 2 * blocklen) {
256 out[0] = (unsigned char)inlen;
257 out[1] = in[0] ^ 0xFF;
258 out[2] = in[1] ^ 0xFF;
259 out[3] = in[2] ^ 0xFF;
260 memcpy(out + 4, in, inlen);
261 /* Add random padding to end */
263 && RAND_bytes(out + 4 + inlen, olen - 4 - inlen) <= 0)
266 if (!EVP_EncryptUpdate(ctx, out, &dummy, out, olen)
267 || !EVP_EncryptUpdate(ctx, out, &dummy, out, olen))
276 /* Encrypt/Decrypt content key in PWRI recipient info */
278 int cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
281 CMS_EncryptedContentInfo *ec;
282 CMS_PasswordRecipientInfo *pwri;
284 X509_ALGOR *algtmp, *kekalg = NULL;
285 EVP_CIPHER_CTX *kekctx = NULL;
286 const EVP_CIPHER *kekcipher;
287 unsigned char *key = NULL;
290 ec = cms->d.envelopedData->encryptedContentInfo;
294 if (pwri->pass == NULL) {
295 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD);
298 algtmp = pwri->keyEncryptionAlgorithm;
300 if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) {
301 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
302 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
306 kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
309 if (kekalg == NULL) {
310 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
311 CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);
315 kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
318 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNKNOWN_CIPHER);
322 kekctx = EVP_CIPHER_CTX_new();
323 if (kekctx == NULL) {
324 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);
327 /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
328 if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de))
330 EVP_CIPHER_CTX_set_padding(kekctx, 0);
331 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0) {
332 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
333 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
337 algtmp = pwri->keyDerivationAlgorithm;
339 /* Finish password based key derivation to setup key in "ctx" */
341 if (EVP_PBE_CipherInit(algtmp->algorithm,
342 (char *)pwri->pass, pwri->passlen,
343 algtmp->parameter, kekctx, en_de) < 0) {
344 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB);
348 /* Finally wrap/unwrap the key */
352 if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx))
355 key = OPENSSL_malloc(keylen);
360 if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx))
362 pwri->encryptedKey->data = key;
363 pwri->encryptedKey->length = keylen;
365 key = OPENSSL_malloc(pwri->encryptedKey->length);
368 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);
371 if (!kek_unwrap_key(key, &keylen,
372 pwri->encryptedKey->data,
373 pwri->encryptedKey->length, kekctx)) {
374 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNWRAP_FAILURE);
378 OPENSSL_clear_free(ec->key, ec->keylen);
388 EVP_CIPHER_CTX_free(kekctx);
392 X509_ALGOR_free(kekalg);