summaryrefslogtreecommitdiffstats
path: root/crypto/cms
diff options
context:
space:
mode:
authorRichard Levitte <levitte@openssl.org>2022-09-29 13:57:34 +0200
committerRichard Levitte <levitte@openssl.org>2022-10-05 14:02:03 +0200
commite077455e9e57ed4ee4676996b4a9aa11df6327a6 (patch)
treeedcb7412024f95fbc97c2c7a780f78ad05d586e3 /crypto/cms
parent9167a47f78159b0578bc032401ab1d66e14eecdb (diff)
Stop raising ERR_R_MALLOC_FAILURE in most places
Since OPENSSL_malloc() and friends report ERR_R_MALLOC_FAILURE, and at least handle the file name and line number they are called from, there's no need to report ERR_R_MALLOC_FAILURE where they are called directly, or when SSLfatal() and RLAYERfatal() is used, the reason `ERR_R_MALLOC_FAILURE` is changed to `ERR_R_CRYPTO_LIB`. There were a number of places where `ERR_R_MALLOC_FAILURE` was reported even though it was a function from a different sub-system that was called. Those places are changed to report ERR_R_{lib}_LIB, where {lib} is the name of that sub-system. Some of them are tricky to get right, as we have a lot of functions that belong in the ASN1 sub-system, and all the `sk_` calls or from the CRYPTO sub-system. Some extra adaptation was necessary where there were custom OPENSSL_malloc() wrappers, and some bugs are fixed alongside these changes. Reviewed-by: Tomas Mraz <tomas@openssl.org> Reviewed-by: Hugo Landau <hlandau@openssl.org> (Merged from https://github.com/openssl/openssl/pull/19301)
Diffstat (limited to 'crypto/cms')
-rw-r--r--crypto/cms/cms_dd.c2
-rw-r--r--crypto/cms/cms_enc.c14
-rw-r--r--crypto/cms/cms_env.c79
-rw-r--r--crypto/cms/cms_ess.c34
-rw-r--r--crypto/cms/cms_io.c3
-rw-r--r--crypto/cms/cms_lib.c18
-rw-r--r--crypto/cms/cms_pwri.c56
-rw-r--r--crypto/cms/cms_sd.c78
-rw-r--r--crypto/cms/cms_smime.c41
9 files changed, 171 insertions, 154 deletions
diff --git a/crypto/cms/cms_dd.c b/crypto/cms/cms_dd.c
index 6a7c049ef3..40b20249a3 100644
--- a/crypto/cms/cms_dd.c
+++ b/crypto/cms/cms_dd.c
@@ -66,7 +66,7 @@ int ossl_cms_DigestedData_do_final(const CMS_ContentInfo *cms, BIO *chain,
CMS_DigestedData *dd;
if (mctx == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
goto err;
}
diff --git a/crypto/cms/cms_enc.c b/crypto/cms/cms_enc.c
index 150b9ee4e1..1bca2f7c62 100644
--- a/crypto/cms/cms_enc.c
+++ b/crypto/cms/cms_enc.c
@@ -44,7 +44,7 @@ BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
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;
}
@@ -116,10 +116,8 @@ BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
/* 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;
}
@@ -163,7 +161,7 @@ BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
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)) {
@@ -206,10 +204,8 @@ int ossl_cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
{
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;
@@ -230,7 +226,7 @@ int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
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);
diff --git a/crypto/cms/cms_env.c b/crypto/cms/cms_env.c
index 4648cd1372..7887defe25 100644
--- a/crypto/cms/cms_env.c
+++ b/crypto/cms/cms_env.c
@@ -66,7 +66,7 @@ static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
if (cms->d.other == NULL) {
cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
if (cms->d.envelopedData == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
return NULL;
}
cms->d.envelopedData->version = 0;
@@ -85,7 +85,7 @@ cms_auth_enveloped_data_init(CMS_ContentInfo *cms)
if (cms->d.other == NULL) {
cms->d.authEnvelopedData = M_ASN1_new_of(CMS_AuthEnvelopedData);
if (cms->d.authEnvelopedData == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
return NULL;
}
/* Defined in RFC 5083 - Section 2.1. "AuthEnvelopedData Type" */
@@ -222,18 +222,18 @@ CMS_ContentInfo *CMS_EnvelopedData_create_ex(const EVP_CIPHER *cipher,
cms = CMS_ContentInfo_new_ex(libctx, propq);
if (cms == NULL)
- goto merr;
+ goto err;
env = cms_enveloped_data_init(cms);
if (env == NULL)
- goto merr;
+ goto err;
if (!ossl_cms_EncryptedContent_init(env->encryptedContentInfo, cipher, NULL,
0, ossl_cms_get0_cmsctx(cms)))
- goto merr;
+ goto err;
return cms;
- merr:
+ err:
CMS_ContentInfo_free(cms);
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
return NULL;
}
@@ -299,7 +299,7 @@ CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER *cipher, OSSL_LIB_CTX *libctx,
return cms;
merr:
CMS_ContentInfo_free(cms);
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
return NULL;
}
@@ -382,8 +382,10 @@ CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
/* Initialize recipient info */
ri = M_ASN1_new_of(CMS_RecipientInfo);
- if (ri == NULL)
- goto merr;
+ if (ri == NULL) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
+ }
pk = X509_get0_pubkey(recip);
if (pk == NULL) {
@@ -410,13 +412,13 @@ CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
}
- if (!sk_CMS_RecipientInfo_push(ris, ri))
- goto merr;
+ if (!sk_CMS_RecipientInfo_push(ris, ri)) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
+ goto err;
+ }
return ri;
- merr:
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
err:
M_ASN1_free_of(ri, CMS_RecipientInfo);
return NULL;
@@ -527,11 +529,8 @@ static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
goto err;
ek = OPENSSL_malloc(eklen);
-
- if (ek == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ if (ek == NULL)
goto err;
- }
if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
goto err;
@@ -614,10 +613,8 @@ static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
goto err;
ek = OPENSSL_malloc(eklen);
- if (ek == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ if (ek == NULL)
goto err;
- }
if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
ktri->encryptedKey->data,
@@ -732,24 +729,32 @@ CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
/* Initialize recipient info */
ri = M_ASN1_new_of(CMS_RecipientInfo);
- if (!ri)
- goto merr;
+ if (!ri) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
+ }
ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
- if (!ri->d.kekri)
- goto merr;
+ if (!ri->d.kekri) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
+ }
ri->type = CMS_RECIPINFO_KEK;
kekri = ri->d.kekri;
if (otherTypeId) {
kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
- if (kekri->kekid->other == NULL)
- goto merr;
+ if (kekri->kekid->other == NULL) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
+ }
}
- if (!sk_CMS_RecipientInfo_push(ris, ri))
- goto merr;
+ if (!sk_CMS_RecipientInfo_push(ris, ri)) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
+ goto err;
+ }
/* After this point no calls can fail */
@@ -772,8 +777,6 @@ CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
return ri;
- merr:
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
err:
M_ASN1_free_of(ri, CMS_RecipientInfo);
return NULL;
@@ -884,14 +887,12 @@ static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
/* 8 byte prefix for AES wrap ciphers */
wkey = OPENSSL_malloc(ec->keylen + 8);
- if (wkey == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ if (wkey == NULL)
goto err;
- }
ctx = EVP_CIPHER_CTX_new();
if (ctx == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
goto err;
}
@@ -967,14 +968,12 @@ static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
}
ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
- if (ukey == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ if (ukey == NULL)
goto err;
- }
ctx = EVP_CIPHER_CTX_new();
if (ctx == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
goto err;
}
@@ -1272,7 +1271,7 @@ int ossl_cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain)
env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null();
if (env->unprotectedAttrs == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
return 0;
}
diff --git a/crypto/cms/cms_ess.c b/crypto/cms/cms_ess.c
index 6c43dd102a..0885a68216 100644
--- a/crypto/cms/cms_ess.c
+++ b/crypto/cms/cms_ess.c
@@ -121,13 +121,17 @@ CMS_ReceiptRequest *CMS_ReceiptRequest_create0_ex(
CMS_ReceiptRequest *rr;
rr = CMS_ReceiptRequest_new();
- if (rr == NULL)
- goto merr;
+ if (rr == NULL) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
+ goto err;
+ }
if (id)
ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
else {
- if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
- goto merr;
+ if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32)) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
+ }
if (RAND_bytes_ex(libctx, rr->signedContentIdentifier->data, 32,
0) <= 0)
goto err;
@@ -146,9 +150,6 @@ CMS_ReceiptRequest *CMS_ReceiptRequest_create0_ex(
return rr;
- merr:
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
-
err:
CMS_ReceiptRequest_free(rr);
return NULL;
@@ -169,19 +170,20 @@ int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
int rrderlen, r = 0;
rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
- if (rrderlen < 0)
- goto merr;
+ if (rrderlen < 0) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
+ goto err;
+ }
if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
- V_ASN1_SEQUENCE, rrder, rrderlen))
- goto merr;
+ V_ASN1_SEQUENCE, rrder, rrderlen)) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
+ goto err;
+ }
r = 1;
- merr:
- if (!r)
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
-
+ err:
OPENSSL_free(rrder);
return r;
@@ -241,7 +243,7 @@ int ossl_cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
}
if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
V_ASN1_OCTET_STRING, dig, diglen)) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
return 0;
}
return 1;
diff --git a/crypto/cms/cms_io.c b/crypto/cms/cms_io.c
index dab70af73c..f5d70e84ce 100644
--- a/crypto/cms/cms_io.c
+++ b/crypto/cms/cms_io.c
@@ -18,6 +18,7 @@
int CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms)
{
ASN1_OCTET_STRING **pos;
+
pos = CMS_get0_content(cms);
if (pos == NULL)
return 0;
@@ -29,7 +30,7 @@ int CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms)
*boundary = &(*pos)->data;
return 1;
}
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
return 0;
}
diff --git a/crypto/cms/cms_lib.c b/crypto/cms/cms_lib.c
index e39fde9e43..d92772d41d 100644
--- a/crypto/cms/cms_lib.c
+++ b/crypto/cms/cms_lib.c
@@ -60,7 +60,6 @@ CMS_ContentInfo *CMS_ContentInfo_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
if (ci->ctx.propq == NULL) {
CMS_ContentInfo_free(ci);
ci = NULL;
- ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
}
}
}
@@ -404,7 +403,7 @@ int CMS_set_detached(CMS_ContentInfo *cms, int detached)
(*pos)->flags |= ASN1_STRING_FLAG_CONT;
return 1;
}
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
return 0;
}
@@ -702,18 +701,23 @@ int ossl_cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
{
CMS_IssuerAndSerialNumber *ias;
ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
- if (!ias)
+ if (!ias) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
goto err;
- if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
+ }
+ if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert))) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
goto err;
- if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert)))
+ }
+ if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert))) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
goto err;
+ }
M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
*pias = ias;
return 1;
err:
M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
return 0;
}
@@ -728,7 +732,7 @@ int ossl_cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
}
keyid = ASN1_STRING_dup(cert_keyid);
if (!keyid) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
return 0;
}
ASN1_OCTET_STRING_free(*pkeyid);
diff --git a/crypto/cms/cms_pwri.c b/crypto/cms/cms_pwri.c
index 1f73cb1008..8b5beb2157 100644
--- a/crypto/cms/cms_pwri.c
+++ b/crypto/cms/cms_pwri.c
@@ -82,11 +82,12 @@ CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
/* Setup algorithm identifier for cipher */
encalg = X509_ALGOR_new();
if (encalg == NULL) {
- goto merr;
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
}
ctx = EVP_CIPHER_CTX_new();
if (ctx == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
goto err;
}
@@ -110,7 +111,7 @@ CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
}
encalg->parameter = ASN1_TYPE_new();
if (!encalg->parameter) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
goto err;
}
if (EVP_CIPHER_param_to_asn1(ctx, encalg->parameter) <= 0) {
@@ -126,12 +127,16 @@ CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
/* Initialize recipient info */
ri = M_ASN1_new_of(CMS_RecipientInfo);
- if (ri == NULL)
- goto merr;
+ if (ri == NULL) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
+ }
ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo);
- if (ri->d.pwri == NULL)
- goto merr;
+ if (ri->d.pwri == NULL) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
+ }
ri->type = CMS_RECIPINFO_PASS;
pwri = ri->d.pwri;
@@ -139,17 +144,23 @@ CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
/* Since this is overwritten, free up empty structure already there */
X509_ALGOR_free(pwri->keyEncryptionAlgorithm);
pwri->keyEncryptionAlgorithm = X509_ALGOR_new();
- if (pwri->keyEncryptionAlgorithm == NULL)
- goto merr;
+ if (pwri->keyEncryptionAlgorithm == NULL) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
+ }
pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid);
pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new();
- if (pwri->keyEncryptionAlgorithm->parameter == NULL)
- goto merr;
+ if (pwri->keyEncryptionAlgorithm->parameter == NULL) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
+ }
if (!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR),
&pwri->keyEncryptionAlgorithm->parameter->
- value.sequence))
- goto merr;
+ value.sequence)) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
+ }
pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE;
X509_ALGOR_free(encalg);
@@ -165,13 +176,13 @@ CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
CMS_RecipientInfo_set0_password(ri, pass, passlen);
pwri->version = 0;
- if (!sk_CMS_RecipientInfo_push(ris, ri))
- goto merr;
+ if (!sk_CMS_RecipientInfo_push(ris, ri)) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
+ goto err;
+ }
return ri;
- merr:
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
err:
EVP_CIPHER_CTX_free(ctx);
if (ri)
@@ -201,10 +212,8 @@ static int kek_unwrap_key(unsigned char *out, size_t *outlen,
/* Invalid size */
return 0;
}
- if ((tmp = OPENSSL_malloc(inlen)) == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ if ((tmp = OPENSSL_malloc(inlen)) == NULL)
return 0;
- }
/* setup IV by decrypting last two blocks */
if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
in + inlen - 2 * blocklen, blocklen * 2)
@@ -335,7 +344,7 @@ int ossl_cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms,
kekctx = EVP_CIPHER_CTX_new();
if (kekctx == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
goto err;
}
/* Fixup cipher based on AlgorithmIdentifier to set IV etc */
@@ -376,11 +385,8 @@ int ossl_cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms,
pwri->encryptedKey->length = keylen;
} else {
key = OPENSSL_malloc(pwri->encryptedKey->length);
-
- if (key == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ if (key == NULL)
goto err;
- }
if (!kek_unwrap_key(key, &keylen,
pwri->encryptedKey->data,
pwri->encryptedKey->length, kekctx)) {
diff --git a/crypto/cms/cms_sd.c b/crypto/cms/cms_sd.c
index 63f90f1173..be8834d5f1 100644
--- a/crypto/cms/cms_sd.c
+++ b/crypto/cms/cms_sd.c
@@ -38,7 +38,7 @@ static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
if (cms->d.other == NULL) {
cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
if (!cms->d.signedData) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
return NULL;
}
cms->d.signedData->version = 1;
@@ -349,8 +349,10 @@ CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
if (!sd)
goto err;
si = M_ASN1_new_of(CMS_SignerInfo);
- if (!si)
- goto merr;
+ if (!si) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
+ }
/* Call for side-effect of computing hash and caching extensions */
X509_check_purpose(signer, -1, -1);
@@ -364,7 +366,7 @@ CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
si->pctx = NULL;
if (si->mctx == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
goto err;
}
@@ -413,12 +415,15 @@ CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
}
if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
- if ((alg = X509_ALGOR_new()) == NULL)
- goto merr;
+ if ((alg = X509_ALGOR_new()) == NULL) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
+ goto err;
+ }
X509_ALGOR_set_md(alg, md);
if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
X509_ALGOR_free(alg);
- goto merr;
+ ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
+ goto err;
}
}
@@ -431,8 +436,10 @@ CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
*/
if (!si->signedAttrs) {
si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
- if (!si->signedAttrs)
- goto merr;
+ if (!si->signedAttrs) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
+ goto err;
+ }
}
if (!(flags & CMS_NOSMIMECAP)) {
@@ -442,8 +449,10 @@ CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
if (i)
i = CMS_add_smimecap(si, smcap);
sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
- if (!i)
- goto merr;
+ if (!i) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
+ goto err;
+ }
}
if (flags & CMS_CADES) {
ESS_SIGNING_CERT *sc = NULL;
@@ -479,8 +488,10 @@ CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
if (!(flags & CMS_NOCERTS)) {
/* NB ignore -1 return for duplicate cert */
- if (!CMS_add1_cert(cms, signer))
- goto merr;
+ if (!CMS_add1_cert(cms, signer)) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
+ goto err;
+ }
}
if (flags & CMS_KEY_PARAM) {
@@ -503,15 +514,15 @@ CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
}
}
- if (!sd->signerInfos)
+ if (sd->signerInfos == NULL)
sd->signerInfos = sk_CMS_SignerInfo_new_null();
- if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si))
- goto merr;
+ if (sd->signerInfos == NULL || !sk_CMS_SignerInfo_push(sd->signerInfos, si)) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
+ goto err;
+ }
return si;
- merr:
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
err:
M_ASN1_free_of(si, CMS_SignerInfo);
return NULL;
@@ -546,21 +557,22 @@ static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
else
tt = X509_gmtime_adj(NULL, 0);
- if (tt == NULL)
- goto merr;
+ if (tt == NULL) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
+ goto err;
+ }
if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
- tt->type, tt, -1) <= 0)
- goto merr;
+ tt->type, tt, -1) <= 0) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
+ goto err;
+ }
r = 1;
- merr:
+ err:
if (t == NULL)
ASN1_TIME_free(tt);
- if (!r)
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
-
return r;
}
@@ -703,7 +715,7 @@ static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
if (mctx == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
return 0;
}
@@ -751,10 +763,8 @@ static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
}
siglen = EVP_PKEY_get_size(si->pkey);
sig = OPENSSL_malloc(siglen);
- if (sig == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ if (sig == NULL)
goto err;
- }
if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
OPENSSL_free(sig);
goto err;
@@ -769,10 +779,8 @@ static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
goto err;
}
sig = OPENSSL_malloc(EVP_PKEY_get_size(si->pkey));
- if (sig == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ if (sig == NULL)
goto err;
- }
if (!EVP_SignFinal_ex(mctx, sig, &siglen, si->pkey,
ossl_cms_ctx_get0_libctx(ctx),
ossl_cms_ctx_get0_propq(ctx))) {
@@ -909,7 +917,7 @@ int CMS_SignerInfo_verify(CMS_SignerInfo *si)
(void)ERR_pop_to_mark();
if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
goto err;
}
mctx = si->mctx;
@@ -982,7 +990,7 @@ int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
unsigned int mlen;
if (mctx == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
goto err;
}
/* If we have any signed attributes look for messageDigest value */
diff --git a/crypto/cms/cms_smime.c b/crypto/cms/cms_smime.c
index ea40873e6a..fe0850b93e 100644
--- a/crypto/cms/cms_smime.c
+++ b/crypto/cms/cms_smime.c
@@ -39,7 +39,7 @@ static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
tmpout = cms_get_text_bio(out, flags);
if (tmpout == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
goto err;
}
@@ -271,7 +271,7 @@ static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
ctx = X509_STORE_CTX_new_ex(ossl_cms_ctx_get0_libctx(cms_ctx),
ossl_cms_ctx_get0_propq(cms_ctx));
if (ctx == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
goto err;
}
CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
@@ -356,10 +356,8 @@ int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
if (cadesVerify) {
/* Certificate trust chain is required to check CAdES signature */
si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
- if (si_chains == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ if (si_chains == NULL)
goto err;
- }
}
cms_certs = CMS_get1_certs(cms);
if (!(flags & CMS_NOCRL))
@@ -406,7 +404,7 @@ int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
len = BIO_get_mem_data(dcont, &ptr);
tmpin = (len == 0) ? dcont : BIO_new_mem_buf(ptr, len);
if (tmpin == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_BIO_LIB);
goto err2;
}
} else {
@@ -423,7 +421,7 @@ int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
*/
tmpout = cms_get_text_bio(out, flags);
if (tmpout == NULL) {
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
goto err;
}
cmsbio = CMS_dataInit(cms, tmpout);
@@ -511,12 +509,16 @@ CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
int i;
cms = CMS_ContentInfo_new_ex(libctx, propq);
- if (cms == NULL || !CMS_SignedData_init(cms))
- goto merr;
+ if (cms == NULL || !CMS_SignedData_init(cms)) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
+ goto err;
+ }
if (flags & CMS_ASCIICRLF
&& !CMS_set1_eContentType(cms,
- OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
+ OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF))) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
goto err;
+ }
if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
@@ -526,8 +528,10 @@ CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
for (i = 0; i < sk_X509_num(certs); i++) {
X509 *x = sk_X509_value(certs, i);
- if (!CMS_add1_cert(cms, x))
- goto merr;
+ if (!CMS_add1_cert(cms, x)) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
+ goto err;
+ }
}
if (!(flags & CMS_DETACHED))
@@ -539,9 +543,6 @@ CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
else
goto err;
- merr:
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
-
err:
CMS_ContentInfo_free(cms);
return NULL;
@@ -637,8 +638,10 @@ CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
cms = (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq)
: CMS_EnvelopedData_create_ex(cipher, libctx, propq);
- if (cms == NULL)
- goto merr;
+ if (cms == NULL) {
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
+ goto err;
+ }
for (i = 0; i < sk_X509_num(certs); i++) {
recip = sk_X509_value(certs, i);
if (!CMS_add1_recipient_cert(cms, recip, flags)) {
@@ -654,10 +657,8 @@ CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
|| CMS_final(cms, data, NULL, flags))
return cms;
else
- goto err;
+ ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
- merr:
- ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
err:
CMS_ContentInfo_free(cms);
return NULL;