2 * Copyright 2009-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
19 #include "internal/asn1_int.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 tmp = OPENSSL_malloc(inlen);
194 /* setup IV by decrypting last two blocks */
195 if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
196 in + inlen - 2 * blocklen, blocklen * 2)
198 * Do a decrypt of last decrypted block to set IV to correct value
199 * output it to start of buffer so we don't corrupt decrypted block
200 * this works because buffer is at least two block lengths long.
202 || !EVP_DecryptUpdate(ctx, tmp, &outl,
203 tmp + inlen - blocklen, blocklen)
204 /* Can now decrypt first n - 1 blocks */
205 || !EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen)
207 /* Reset IV to original value */
208 || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)
210 || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))
212 /* Check check bytes */
213 if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {
214 /* Check byte failure */
217 if (inlen < (size_t)(tmp[0] - 4)) {
218 /* Invalid length value */
221 *outlen = (size_t)tmp[0];
222 memcpy(out, tmp + 4, *outlen);
225 OPENSSL_clear_free(tmp, inlen);
230 static int kek_wrap_key(unsigned char *out, size_t *outlen,
231 const unsigned char *in, size_t inlen,
234 size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
238 * First decide length of output buffer: need header and round up to
239 * multiple of block length.
241 olen = (inlen + 4 + blocklen - 1) / blocklen;
243 if (olen < 2 * blocklen) {
253 out[0] = (unsigned char)inlen;
254 out[1] = in[0] ^ 0xFF;
255 out[2] = in[1] ^ 0xFF;
256 out[3] = in[2] ^ 0xFF;
257 memcpy(out + 4, in, inlen);
258 /* Add random padding to end */
260 && RAND_bytes(out + 4 + inlen, olen - 4 - inlen) <= 0)
263 if (!EVP_EncryptUpdate(ctx, out, &dummy, out, olen)
264 || !EVP_EncryptUpdate(ctx, out, &dummy, out, olen))
273 /* Encrypt/Decrypt content key in PWRI recipient info */
275 int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
278 CMS_EncryptedContentInfo *ec;
279 CMS_PasswordRecipientInfo *pwri;
281 X509_ALGOR *algtmp, *kekalg = NULL;
282 EVP_CIPHER_CTX *kekctx = NULL;
283 const EVP_CIPHER *kekcipher;
284 unsigned char *key = NULL;
287 ec = cms->d.envelopedData->encryptedContentInfo;
292 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD);
295 algtmp = pwri->keyEncryptionAlgorithm;
297 if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) {
298 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
299 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
303 kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
306 if (kekalg == NULL) {
307 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
308 CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);
312 kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
315 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNKNOWN_CIPHER);
319 kekctx = EVP_CIPHER_CTX_new();
320 if (kekctx == NULL) {
321 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);
324 /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
325 if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de))
327 EVP_CIPHER_CTX_set_padding(kekctx, 0);
328 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) < 0) {
329 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
330 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
334 algtmp = pwri->keyDerivationAlgorithm;
336 /* Finish password based key derivation to setup key in "ctx" */
338 if (EVP_PBE_CipherInit(algtmp->algorithm,
339 (char *)pwri->pass, pwri->passlen,
340 algtmp->parameter, kekctx, en_de) < 0) {
341 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB);
345 /* Finally wrap/unwrap the key */
349 if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx))
352 key = OPENSSL_malloc(keylen);
357 if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx))
359 pwri->encryptedKey->data = key;
360 pwri->encryptedKey->length = keylen;
362 key = OPENSSL_malloc(pwri->encryptedKey->length);
365 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);
368 if (!kek_unwrap_key(key, &keylen,
369 pwri->encryptedKey->data,
370 pwri->encryptedKey->length, kekctx)) {
371 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNWRAP_FAILURE);
384 EVP_CIPHER_CTX_free(kekctx);
388 X509_ALGOR_free(kekalg);