summaryrefslogtreecommitdiffstats
path: root/crypto/cms
diff options
context:
space:
mode:
authorShane Lontis <shane.lontis@oracle.com>2020-12-11 19:19:37 +1000
committerTomas Mraz <tomas@openssl.org>2021-01-18 15:01:26 +0100
commit84af8027c5f2132a9166673e7a47b0f31c7cfe1d (patch)
tree1bc2caf1cc20b6362811fc8daff01b60a93ba732 /crypto/cms
parent0d83b7b9036feea680ba45751df028ff5e86cd63 (diff)
CMS: Fix NULL access if d2i_CMS_bio() is not passed a CMS_ContentInfo**.
Fixes #13624 Reviewed-by: Tim Hudson <tjh@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from https://github.com/openssl/openssl/pull/13668)
Diffstat (limited to 'crypto/cms')
-rw-r--r--crypto/cms/cms_enc.c7
-rw-r--r--crypto/cms/cms_env.c20
-rw-r--r--crypto/cms/cms_ess.c5
-rw-r--r--crypto/cms/cms_io.c4
-rw-r--r--crypto/cms/cms_kari.c19
-rw-r--r--crypto/cms/cms_lib.c19
-rw-r--r--crypto/cms/cms_pwri.c7
-rw-r--r--crypto/cms/cms_sd.c28
-rw-r--r--crypto/cms/cms_smime.c6
9 files changed, 68 insertions, 47 deletions
diff --git a/crypto/cms/cms_enc.c b/crypto/cms/cms_enc.c
index 0069bde939..c7583f4088 100644
--- a/crypto/cms/cms_enc.c
+++ b/crypto/cms/cms_enc.c
@@ -37,6 +37,8 @@ BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
size_t tkeylen = 0;
int ok = 0;
int enc, keep_key = 0;
+ OSSL_LIB_CTX *libctx = cms_ctx_get0_libctx(cms_ctx);
+ const char *propq = cms_ctx_get0_propq(cms_ctx);
enc = ec->cipher ? 1 : 0;
@@ -60,8 +62,7 @@ BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
cipher = EVP_get_cipherbyobj(calg->algorithm);
}
if (cipher != NULL) {
- fetched_ciph = EVP_CIPHER_fetch(cms_ctx->libctx, EVP_CIPHER_name(cipher),
- cms_ctx->propq);
+ fetched_ciph = EVP_CIPHER_fetch(libctx, EVP_CIPHER_name(cipher), propq);
if (fetched_ciph != NULL)
cipher = fetched_ciph;
}
@@ -82,7 +83,7 @@ BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
/* Generate a random IV if we need one */
ivlen = EVP_CIPHER_CTX_iv_length(ctx);
if (ivlen > 0) {
- if (RAND_bytes_ex(cms_ctx->libctx, iv, ivlen) <= 0)
+ if (RAND_bytes_ex(libctx, iv, ivlen) <= 0)
goto err;
piv = iv;
}
diff --git a/crypto/cms/cms_env.c b/crypto/cms/cms_env.c
index 15ebe1b86b..d2f630146e 100644
--- a/crypto/cms/cms_env.c
+++ b/crypto/cms/cms_env.c
@@ -181,7 +181,8 @@ void cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms)
break;
case CMS_RECIPINFO_TRANS:
ri->d.ktri->cms_ctx = ctx;
- x509_set0_libctx(ri->d.ktri->recip, ctx->libctx, ctx->propq);
+ x509_set0_libctx(ri->d.ktri->recip, cms_ctx_get0_libctx(ctx),
+ cms_ctx_get0_propq(ctx));
break;
case CMS_RECIPINFO_KEK:
ri->d.kekri->cms_ctx = ctx;
@@ -310,8 +311,9 @@ static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
ktri->recip = recip;
if (flags & CMS_KEY_PARAM) {
- ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, ktri->pkey,
- ctx->propq);
+ ktri->pctx = EVP_PKEY_CTX_new_from_pkey(cms_ctx_get0_libctx(ctx),
+ ktri->pkey,
+ cms_ctx_get0_propq(ctx));
if (ktri->pctx == NULL)
return 0;
if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
@@ -470,7 +472,8 @@ static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
if (!cms_env_asn1_ctrl(ri, 0))
goto err;
} else {
- pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, ktri->pkey, ctx->propq);
+ pctx = EVP_PKEY_CTX_new_from_pkey(cms_ctx_get0_libctx(ctx), ktri->pkey,
+ cms_ctx_get0_propq(ctx));
if (pctx == NULL)
return 0;
@@ -524,6 +527,8 @@ static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
EVP_CIPHER *fetched_cipher = NULL;
CMS_EncryptedContentInfo *ec;
const CMS_CTX *ctx = cms_get0_cmsctx(cms);
+ OSSL_LIB_CTX *libctx = cms_ctx_get0_libctx(ctx);
+ const char *propq = cms_ctx_get0_propq(ctx);
ec = cms_get0_env_enc_content(cms);
@@ -538,7 +543,7 @@ static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
const char *name = OBJ_nid2sn(OBJ_obj2nid(calg->algorithm));
(void)ERR_set_mark();
- fetched_cipher = EVP_CIPHER_fetch(ctx->libctx, name, ctx->propq);
+ fetched_cipher = EVP_CIPHER_fetch(libctx, name, propq);
if (fetched_cipher != NULL)
cipher = fetched_cipher;
@@ -555,7 +560,7 @@ static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
EVP_CIPHER_free(fetched_cipher);
}
- ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, pkey, ctx->propq);
+ ktri->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
if (ktri->pctx == NULL)
goto err;
@@ -808,7 +813,8 @@ static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx)
default:
return NULL;
}
- return EVP_CIPHER_fetch(ctx->libctx, alg, ctx->propq);
+ return EVP_CIPHER_fetch(cms_ctx_get0_libctx(ctx), alg,
+ cms_ctx_get0_propq(ctx));
}
diff --git a/crypto/cms/cms_ess.c b/crypto/cms/cms_ess.c
index ba05d4695c..9b02bb4363 100644
--- a/crypto/cms/cms_ess.c
+++ b/crypto/cms/cms_ess.c
@@ -219,8 +219,9 @@ static int cms_msgSigDigest(CMS_SignerInfo *si,
if (md == NULL)
return 0;
if (!asn1_item_digest_ex(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
- si->signedAttrs, dig, diglen, si->cms_ctx->libctx,
- si->cms_ctx->propq))
+ si->signedAttrs, dig, diglen,
+ cms_ctx_get0_libctx(si->cms_ctx),
+ cms_ctx_get0_propq(si->cms_ctx)))
return 0;
return 1;
}
diff --git a/crypto/cms/cms_io.c b/crypto/cms/cms_io.c
index a12ac0d0f4..f09038a158 100644
--- a/crypto/cms/cms_io.c
+++ b/crypto/cms/cms_io.c
@@ -38,7 +38,7 @@ CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms)
CMS_ContentInfo *ci;
ci = ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
- if (ci != NULL && cms != NULL)
+ if (ci != NULL)
cms_resolve_libctx(ci);
return ci;
}
@@ -97,7 +97,7 @@ CMS_ContentInfo *SMIME_read_CMS_ex(BIO *bio, BIO **bcont, CMS_ContentInfo **cms)
ci = (CMS_ContentInfo *)SMIME_read_ASN1_ex(bio, bcont,
ASN1_ITEM_rptr(CMS_ContentInfo),
(ASN1_VALUE **)cms);
- if (ci != NULL && cms != NULL)
+ if (ci != NULL)
cms_resolve_libctx(ci);
return ci;
}
diff --git a/crypto/cms/cms_kari.c b/crypto/cms/cms_kari.c
index a9a35b4300..b343bf4c95 100644
--- a/crypto/cms/cms_kari.c
+++ b/crypto/cms/cms_kari.c
@@ -168,8 +168,8 @@ int CMS_RecipientInfo_kari_set0_pkey_and_peer(CMS_RecipientInfo *ri, EVP_PKEY *p
if (pk == NULL)
return 1;
- pctx = EVP_PKEY_CTX_new_from_pkey(kari->cms_ctx->libctx, pk,
- kari->cms_ctx->propq);
+ pctx = EVP_PKEY_CTX_new_from_pkey(cms_ctx_get0_libctx(kari->cms_ctx), pk,
+ cms_ctx_get0_propq(kari->cms_ctx));
if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0)
goto err;
@@ -284,8 +284,10 @@ static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari,
EVP_PKEY *ekey = NULL;
int rv = 0;
const CMS_CTX *ctx = kari->cms_ctx;
+ OSSL_LIB_CTX *libctx = cms_ctx_get0_libctx(ctx);
+ const char *propq = cms_ctx_get0_propq(ctx);
- pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, pk, ctx->propq);
+ pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propq);
if (pctx == NULL)
goto err;
if (EVP_PKEY_keygen_init(pctx) <= 0)
@@ -293,7 +295,7 @@ static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari,
if (EVP_PKEY_keygen(pctx, &ekey) <= 0)
goto err;
EVP_PKEY_CTX_free(pctx);
- pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, ekey, ctx->propq);
+ pctx = EVP_PKEY_CTX_new_from_pkey(libctx, ekey, propq);
if (pctx == NULL)
goto err;
if (EVP_PKEY_derive_init(pctx) <= 0)
@@ -315,7 +317,9 @@ static int cms_kari_set_originator_private_key(CMS_KeyAgreeRecipientInfo *kari,
int rv = 0;
const CMS_CTX *ctx = kari->cms_ctx;
- pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, originatorPrivKey, ctx->propq);
+ pctx = EVP_PKEY_CTX_new_from_pkey(cms_ctx_get0_libctx(ctx),
+ originatorPrivKey,
+ cms_ctx_get0_propq(ctx));
if (pctx == NULL)
goto err;
if (EVP_PKEY_derive_init(pctx) <= 0)
@@ -455,8 +459,9 @@ static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari,
else
kekcipher_name = SN_id_aes256_wrap;
enc:
- fetched_kekcipher = EVP_CIPHER_fetch(cms_ctx->libctx, kekcipher_name,
- cms_ctx->propq);
+ fetched_kekcipher = EVP_CIPHER_fetch(cms_ctx_get0_libctx(cms_ctx),
+ kekcipher_name,
+ cms_ctx_get0_propq(cms_ctx));
if (fetched_kekcipher == NULL)
return 0;
ret = EVP_EncryptInit_ex(ctx, fetched_kekcipher, NULL, NULL, NULL);
diff --git a/crypto/cms/cms_lib.c b/crypto/cms/cms_lib.c
index 427f62c56c..62029b8931 100644
--- a/crypto/cms/cms_lib.c
+++ b/crypto/cms/cms_lib.c
@@ -30,7 +30,7 @@ CMS_ContentInfo *d2i_CMS_ContentInfo(CMS_ContentInfo **a,
ci = (CMS_ContentInfo *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
(CMS_ContentInfo_it()));
- if (ci != NULL && a != NULL)
+ if (ci != NULL)
cms_resolve_libctx(ci);
return ci;
}
@@ -80,12 +80,12 @@ const CMS_CTX *cms_get0_cmsctx(const CMS_ContentInfo *cms)
OSSL_LIB_CTX *cms_ctx_get0_libctx(const CMS_CTX *ctx)
{
- return ctx->libctx;
+ return ctx != NULL ? ctx->libctx : NULL;
}
const char *cms_ctx_get0_propq(const CMS_CTX *ctx)
{
- return ctx->propq;
+ return ctx != NULL ? ctx->propq : NULL;
}
void cms_resolve_libctx(CMS_ContentInfo *ci)
@@ -93,12 +93,10 @@ void cms_resolve_libctx(CMS_ContentInfo *ci)
int i;
CMS_CertificateChoices *cch;
STACK_OF(CMS_CertificateChoices) **pcerts;
- const CMS_CTX *ctx;
+ const CMS_CTX *ctx = cms_get0_cmsctx(ci);
+ OSSL_LIB_CTX *libctx = cms_ctx_get0_libctx(ctx);
+ const char *propq = cms_ctx_get0_propq(ctx);
- if (ci == NULL)
- return;
-
- ctx = cms_get0_cmsctx(ci);
cms_SignerInfos_set_cmsctx(ci);
cms_RecipientInfos_set_cmsctx(ci);
@@ -107,7 +105,7 @@ void cms_resolve_libctx(CMS_ContentInfo *ci)
for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
cch = sk_CMS_CertificateChoices_value(*pcerts, i);
if (cch->type == CMS_CERTCHOICE_CERT)
- x509_set0_libctx(cch->d.certificate, ctx->libctx, ctx->propq);
+ x509_set0_libctx(cch->d.certificate, libctx, propq);
}
}
}
@@ -411,7 +409,8 @@ BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
alg = OBJ_nid2sn(OBJ_obj2nid(digestoid));
(void)ERR_set_mark();
- fetched_digest = EVP_MD_fetch(ctx->libctx, alg, ctx->propq);
+ fetched_digest = EVP_MD_fetch(cms_ctx_get0_libctx(ctx), alg,
+ cms_ctx_get0_propq(ctx));
if (fetched_digest != NULL)
digest = fetched_digest;
diff --git a/crypto/cms/cms_pwri.c b/crypto/cms/cms_pwri.c
index 8b55f7d0a8..ecfa1f5ffe 100644
--- a/crypto/cms/cms_pwri.c
+++ b/crypto/cms/cms_pwri.c
@@ -93,7 +93,7 @@ CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
ivlen = EVP_CIPHER_CTX_iv_length(ctx);
if (ivlen > 0) {
- if (RAND_bytes_ex(cms_ctx->libctx, iv, ivlen) <= 0)
+ if (RAND_bytes_ex(cms_ctx_get0_libctx(cms_ctx), iv, ivlen) <= 0)
goto err;
if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0) {
ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
@@ -262,7 +262,7 @@ static int kek_wrap_key(unsigned char *out, size_t *outlen,
memcpy(out + 4, in, inlen);
/* Add random padding to end */
if (olen > inlen + 4
- && RAND_bytes_ex(cms_ctx->libctx, out + 4 + inlen,
+ && RAND_bytes_ex(cms_ctx_get0_libctx(cms_ctx), out + 4 + inlen,
olen - 4 - inlen) <= 0)
return 0;
/* Encrypt twice */
@@ -316,7 +316,8 @@ int cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms,
}
name = OBJ_nid2sn(OBJ_obj2nid(kekalg->algorithm));
- kekcipher = EVP_CIPHER_fetch(cms_ctx->libctx, name, cms_ctx->propq);
+ kekcipher = EVP_CIPHER_fetch(cms_ctx_get0_libctx(cms_ctx), name,
+ cms_ctx_get0_propq(cms_ctx));
if (kekcipher == NULL) {
ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
diff --git a/crypto/cms/cms_sd.c b/crypto/cms/cms_sd.c
index 96b92bdc63..d803a6c006 100644
--- a/crypto/cms/cms_sd.c
+++ b/crypto/cms/cms_sd.c
@@ -407,8 +407,9 @@ CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
if (flags & CMS_KEY_PARAM) {
if (flags & CMS_NOATTR) {
- si->pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, si->pkey,
- ctx->propq);
+ si->pctx = EVP_PKEY_CTX_new_from_pkey(cms_ctx_get0_libctx(ctx),
+ si->pkey,
+ cms_ctx_get0_propq(ctx));
if (si->pctx == NULL)
goto err;
if (EVP_PKEY_sign_init(si->pctx) <= 0)
@@ -416,7 +417,8 @@ CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
goto err;
} else if (EVP_DigestSignInit_ex(si->mctx, &si->pctx, EVP_MD_name(md),
- ctx->libctx, ctx->propq, pk) <= 0) {
+ cms_ctx_get0_libctx(ctx),
+ cms_ctx_get0_propq(ctx), pk) <= 0) {
goto err;
}
}
@@ -678,8 +680,9 @@ static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
goto err;
}
- if (!EVP_SignFinal_ex(mctx, sig, &siglen, si->pkey, ctx->libctx,
- ctx->propq)) {
+ if (!EVP_SignFinal_ex(mctx, sig, &siglen, si->pkey,
+ cms_ctx_get0_libctx(ctx),
+ cms_ctx_get0_propq(ctx))) {
ERR_raise(ERR_LIB_CMS, CMS_R_SIGNFINAL_ERROR);
OPENSSL_free(sig);
goto err;
@@ -737,8 +740,8 @@ int CMS_SignerInfo_sign(CMS_SignerInfo *si)
pctx = si->pctx;
else {
EVP_MD_CTX_reset(mctx);
- if (EVP_DigestSignInit_ex(mctx, &pctx, md_name, ctx->libctx, ctx->propq,
- si->pkey) <= 0)
+ if (EVP_DigestSignInit_ex(mctx, &pctx, md_name, cms_ctx_get0_libctx(ctx),
+ cms_ctx_get0_propq(ctx), si->pkey) <= 0)
goto err;
si->pctx = pctx;
}
@@ -815,6 +818,8 @@ int CMS_SignerInfo_verify(CMS_SignerInfo *si)
const EVP_MD *md;
EVP_MD *fetched_md = NULL;
const CMS_CTX *ctx = si->cms_ctx;
+ OSSL_LIB_CTX *libctx = cms_ctx_get0_libctx(ctx);
+ const char *propq = cms_ctx_get0_propq(ctx);
if (si->pkey == NULL) {
ERR_raise(ERR_LIB_CMS, CMS_R_NO_PUBLIC_KEY);
@@ -827,7 +832,7 @@ int CMS_SignerInfo_verify(CMS_SignerInfo *si)
name = OBJ_nid2sn(OBJ_obj2nid(si->digestAlgorithm->algorithm));
(void)ERR_set_mark();
- fetched_md = EVP_MD_fetch(ctx->libctx, name, ctx->propq);
+ fetched_md = EVP_MD_fetch(libctx, name, propq);
if (fetched_md != NULL)
md = fetched_md;
@@ -845,8 +850,8 @@ int CMS_SignerInfo_verify(CMS_SignerInfo *si)
goto err;
}
mctx = si->mctx;
- if (EVP_DigestVerifyInit_ex(mctx, &si->pctx, EVP_MD_name(md), ctx->libctx,
- NULL, si->pkey) <= 0)
+ if (EVP_DigestVerifyInit_ex(mctx, &si->pctx, EVP_MD_name(md), libctx,
+ propq, si->pkey) <= 0)
goto err;
if (!cms_sd_asn1_ctrl(si, 1))
@@ -953,7 +958,8 @@ int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
const EVP_MD *md = EVP_MD_CTX_md(mctx);
const CMS_CTX *ctx = si->cms_ctx;
- pkctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, si->pkey, ctx->propq);
+ pkctx = EVP_PKEY_CTX_new_from_pkey(cms_ctx_get0_libctx(ctx), si->pkey,
+ cms_ctx_get0_propq(ctx));
if (pkctx == NULL)
goto err;
if (EVP_PKEY_verify_init(pkctx) <= 0)
diff --git a/crypto/cms/cms_smime.c b/crypto/cms/cms_smime.c
index 5c903f204b..6c62d904eb 100644
--- a/crypto/cms/cms_smime.c
+++ b/crypto/cms/cms_smime.c
@@ -264,7 +264,8 @@ static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
X509 *signer;
int i, j, r = 0;
- ctx = X509_STORE_CTX_new_ex(cms_ctx->libctx, cms_ctx->propq);
+ ctx = X509_STORE_CTX_new_ex(cms_ctx_get0_libctx(cms_ctx),
+ cms_ctx_get0_propq(cms_ctx));
if (ctx == NULL) {
ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
goto err;
@@ -567,7 +568,8 @@ CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
/* Initialize signed data */
- cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags, ctx->libctx, ctx->propq);
+ cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags, cms_ctx_get0_libctx(ctx),
+ cms_ctx_get0_propq(ctx));
if (cms == NULL)
goto err;