2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2018-04-17 13:18:40 +00:00
|
|
|
* Copyright 2008-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2008-03-14 00:58:43 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2008-03-14 00:58:43 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2008-03-14 00:58:43 +00:00
|
|
|
#include <openssl/asn1t.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/x509v3.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/cms.h>
|
|
|
|
#include <openssl/rand.h>
|
2019-09-27 21:58:06 +00:00
|
|
|
#include "cms_local.h"
|
2008-03-14 00:58:43 +00:00
|
|
|
|
|
|
|
/* CMS EncryptedData Utilities */
|
|
|
|
|
2008-03-14 19:37:56 +00:00
|
|
|
/* Return BIO based on EncryptedContentInfo and key */
|
|
|
|
|
|
|
|
BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
EVP_CIPHER_CTX *ctx;
|
|
|
|
const EVP_CIPHER *ciph;
|
|
|
|
X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
|
|
|
|
unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
|
|
|
|
unsigned char *tkey = NULL;
|
|
|
|
size_t tkeylen = 0;
|
2008-03-14 00:58:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
int ok = 0;
|
2008-03-14 23:30:56 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
int enc, keep_key = 0;
|
2008-03-14 00:58:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
enc = ec->cipher ? 1 : 0;
|
2008-03-14 00:58:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
b = BIO_new(BIO_f_cipher());
|
2015-10-30 11:12:26 +00:00
|
|
|
if (b == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-14 00:58:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
BIO_get_cipher_ctx(b, &ctx);
|
2008-03-14 00:58:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (enc) {
|
|
|
|
ciph = ec->cipher;
|
|
|
|
/*
|
|
|
|
* If not keeping key set cipher to NULL so subsequent calls decrypt.
|
|
|
|
*/
|
|
|
|
if (ec->key)
|
|
|
|
ec->cipher = NULL;
|
|
|
|
} else {
|
|
|
|
ciph = EVP_get_cipherbyobj(calg->algorithm);
|
2008-03-14 00:58:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!ciph) {
|
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, CMS_R_UNKNOWN_CIPHER);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2008-03-14 00:58:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (EVP_CipherInit_ex(ctx, ciph, NULL, NULL, NULL, enc) <= 0) {
|
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
|
|
|
|
CMS_R_CIPHER_INITIALISATION_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2008-03-14 00:58:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (enc) {
|
|
|
|
int ivlen;
|
|
|
|
calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
|
|
|
|
/* Generate a random IV if we need one */
|
|
|
|
ivlen = EVP_CIPHER_CTX_iv_length(ctx);
|
|
|
|
if (ivlen > 0) {
|
2015-02-26 11:57:37 +00:00
|
|
|
if (RAND_bytes(iv, ivlen) <= 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
piv = iv;
|
|
|
|
}
|
|
|
|
} else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0) {
|
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
|
|
|
|
CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
tkeylen = EVP_CIPHER_CTX_key_length(ctx);
|
|
|
|
/* Generate random session key */
|
|
|
|
if (!enc || !ec->key) {
|
|
|
|
tkey = OPENSSL_malloc(tkeylen);
|
2015-10-30 11:12:26 +00:00
|
|
|
if (tkey == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
|
|
|
|
goto err;
|
|
|
|
}
|
2012-03-12 16:31:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!ec->key) {
|
|
|
|
ec->key = tkey;
|
|
|
|
ec->keylen = tkeylen;
|
|
|
|
tkey = NULL;
|
|
|
|
if (enc)
|
|
|
|
keep_key = 1;
|
|
|
|
else
|
|
|
|
ERR_clear_error();
|
2012-03-12 16:31:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2008-03-15 13:37:32 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ec->keylen != tkeylen) {
|
|
|
|
/* If necessary set key length */
|
|
|
|
if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
|
|
|
|
/*
|
|
|
|
* Only reveal failure if debugging so we don't leak information
|
|
|
|
* which may be useful in MMA.
|
|
|
|
*/
|
|
|
|
if (enc || ec->debug) {
|
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
|
|
|
|
CMS_R_INVALID_KEY_LENGTH);
|
|
|
|
goto err;
|
|
|
|
} else {
|
|
|
|
/* Use random key */
|
2015-04-30 21:57:32 +00:00
|
|
|
OPENSSL_clear_free(ec->key, ec->keylen);
|
2015-01-22 03:40:55 +00:00
|
|
|
ec->key = tkey;
|
|
|
|
ec->keylen = tkeylen;
|
|
|
|
tkey = NULL;
|
|
|
|
ERR_clear_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-14 00:58:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
|
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
|
|
|
|
CMS_R_CIPHER_INITIALISATION_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2016-05-06 18:27:49 +00:00
|
|
|
if (enc) {
|
|
|
|
calg->parameter = ASN1_TYPE_new();
|
|
|
|
if (calg->parameter == NULL) {
|
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (EVP_CIPHER_param_to_asn1(ctx, calg->parameter) <= 0) {
|
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
|
|
|
|
CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
/* If parameter type not set omit parameter */
|
|
|
|
if (calg->parameter->type == V_ASN1_UNDEF) {
|
|
|
|
ASN1_TYPE_free(calg->parameter);
|
|
|
|
calg->parameter = NULL;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
ok = 1;
|
2008-03-14 00:58:43 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
err:
|
2015-08-01 14:37:44 +00:00
|
|
|
if (!keep_key || !ok) {
|
2015-04-30 21:57:32 +00:00
|
|
|
OPENSSL_clear_free(ec->key, ec->keylen);
|
2015-01-22 03:40:55 +00:00
|
|
|
ec->key = NULL;
|
|
|
|
}
|
2015-04-30 21:57:32 +00:00
|
|
|
OPENSSL_clear_free(tkey, tkeylen);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ok)
|
|
|
|
return b;
|
|
|
|
BIO_free(b);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
|
|
|
|
const EVP_CIPHER *cipher,
|
|
|
|
const unsigned char *key, size_t keylen)
|
|
|
|
{
|
|
|
|
ec->cipher = cipher;
|
|
|
|
if (key) {
|
2018-04-03 15:31:16 +00:00
|
|
|
if ((ec->key = OPENSSL_malloc(keylen)) == NULL) {
|
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT, ERR_R_MALLOC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
return 0;
|
2018-04-03 15:31:16 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
memcpy(ec->key, key, keylen);
|
|
|
|
}
|
|
|
|
ec->keylen = keylen;
|
|
|
|
if (cipher)
|
|
|
|
ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-14 19:37:56 +00:00
|
|
|
|
|
|
|
int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *key, size_t keylen)
|
|
|
|
{
|
|
|
|
CMS_EncryptedContentInfo *ec;
|
|
|
|
if (!key || !keylen) {
|
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NO_KEY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ciph) {
|
|
|
|
cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
|
|
|
|
if (!cms->d.encryptedData) {
|
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
|
|
|
|
cms->d.encryptedData->version = 0;
|
|
|
|
} else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) {
|
|
|
|
CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NOT_ENCRYPTED_DATA);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ec = cms->d.encryptedData->encryptedContentInfo;
|
|
|
|
return cms_EncryptedContent_init(ec, ciph, key, keylen);
|
|
|
|
}
|
2008-03-14 19:37:56 +00:00
|
|
|
|
|
|
|
BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
CMS_EncryptedData *enc = cms->d.encryptedData;
|
|
|
|
if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
|
|
|
|
enc->version = 2;
|
|
|
|
return cms_EncryptedContent_init_bio(enc->encryptedContentInfo);
|
|
|
|
}
|