#include <openssl/cms.h>
#include <openssl/rand.h>
#include "crypto/evp.h"
+#include "crypto/asn1.h"
#include "cms_local.h"
/* CMS EncryptedData Utilities */
b = BIO_new(BIO_f_cipher());
if (b == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_BIO_LIB);
return NULL;
}
if (enc) {
calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_get_type(ctx));
+ if (calg->algorithm == NULL || calg->algorithm->nid == NID_undef) {
+ ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_ENCRYPTION_ALGORITHM);
+ goto err;
+ }
/* Generate a random IV if we need one */
ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
+ if (ivlen < 0) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
+ goto err;
+ }
+
if (ivlen > 0) {
if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
goto err;
/* Generate random session key */
if (!enc || !ec->key) {
tkey = OPENSSL_malloc(tkeylen);
- if (tkey == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ if (tkey == NULL)
goto err;
- }
if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
goto err;
}
if (enc) {
calg->parameter = ASN1_TYPE_new();
if (calg->parameter == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
goto err;
}
if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
{
ec->cipher = cipher;
if (key) {
- if ((ec->key = OPENSSL_malloc(keylen)) == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ if ((ec->key = OPENSSL_malloc(keylen)) == NULL)
return 0;
- }
memcpy(ec->key, key, keylen);
}
ec->keylen = keylen;
if (ciph) {
cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
if (!cms->d.encryptedData) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
return 0;
}
cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);