From: Daniel Fiala Date: Wed, 21 Sep 2022 13:29:51 +0000 (+0200) Subject: Clear incorrectly reported errors in d2i_CMS_ContentInfo X-Git-Tag: openssl-3.2.0-alpha1~2047 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=678b489a2ae8af289cef939a538235686b448c0e;p=thirdparty%2Fopenssl.git Clear incorrectly reported errors in d2i_CMS_ContentInfo Fixes openssl#19003 Reviewed-by: Paul Dale Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/19255) --- diff --git a/crypto/cms/cms_lib.c b/crypto/cms/cms_lib.c index a2de6db5f27..e39fde9e432 100644 --- a/crypto/cms/cms_lib.c +++ b/crypto/cms/cms_lib.c @@ -33,8 +33,11 @@ CMS_ContentInfo *d2i_CMS_ContentInfo(CMS_ContentInfo **a, (CMS_ContentInfo_it()), ossl_cms_ctx_get0_libctx(ctx), ossl_cms_ctx_get0_propq(ctx)); - if (ci != NULL) + if (ci != NULL) { + ERR_set_mark(); ossl_cms_resolve_libctx(ci); + ERR_pop_to_mark(); + } return ci; } diff --git a/test/cmsapitest.c b/test/cmsapitest.c index df54edb43d0..8187fec7615 100644 --- a/test/cmsapitest.c +++ b/test/cmsapitest.c @@ -302,18 +302,63 @@ static int test_d2i_CMS_bio_NULL(void) return ret; } -static int test_d2i_CMS_bio_file_encrypted_data(void) +static unsigned char *read_all(BIO *bio, long *p_len) +{ + const int step = 256; + unsigned char *buf = NULL; + unsigned char *tmp = NULL; + int ret; + + *p_len = 0; + for (;;) { + tmp = OPENSSL_realloc(buf, *p_len + step); + if (tmp == NULL) + break; + buf = tmp; + ret = BIO_read(bio, buf + *p_len, step); + if (ret < 0) + break; + + *p_len += ret; + + if (ret < step) + return buf; + } + + /* Error */ + OPENSSL_free(buf); + *p_len = 0; + return NULL; +} + +static int test_d2i_CMS_decode(const int idx) { BIO *bio = NULL; CMS_ContentInfo *cms = NULL; + unsigned char *buf = NULL; + const unsigned char *tmp = NULL; + long buf_len = 0; int ret = 0; ERR_clear_error(); - if (!TEST_ptr(bio = BIO_new_file(derin, "r")) - || !TEST_ptr(cms = d2i_CMS_bio(bio, NULL))) + if (!TEST_ptr(bio = BIO_new_file(derin, "r"))) goto end; + switch (idx) { + case 0: + if (!TEST_ptr(cms = d2i_CMS_bio(bio, NULL))) + goto end; + break; + case 1: + if (!TEST_ptr(buf = read_all(bio, &buf_len))) + goto end; + tmp = buf; + if (!TEST_ptr(cms = d2i_CMS_ContentInfo(NULL, &tmp, buf_len))) + goto end; + break; + } + if (!TEST_int_eq(ERR_peek_error(), 0)) goto end; @@ -321,6 +366,7 @@ static int test_d2i_CMS_bio_file_encrypted_data(void) end: CMS_ContentInfo_free(cms); BIO_free(bio); + OPENSSL_free(buf); return ret; } @@ -370,7 +416,7 @@ int setup_tests(void) ADD_TEST(test_encrypt_decrypt_aes_192_gcm); ADD_TEST(test_encrypt_decrypt_aes_256_gcm); ADD_TEST(test_d2i_CMS_bio_NULL); - ADD_TEST(test_d2i_CMS_bio_file_encrypted_data); + ADD_ALL_TESTS(test_d2i_CMS_decode, 2); return 1; }