Clear incorrectly reported errors in d2i_CMS_ContentInfo
authorDaniel Fiala <daniel@openssl.org>
Wed, 21 Sep 2022 13:29:51 +0000 (15:29 +0200)
committerTomas Mraz <tomas@openssl.org>
Fri, 23 Sep 2022 09:14:59 +0000 (11:14 +0200)
Fixes openssl#19003

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19255)

(cherry picked from commit 678b489a2ae8af289cef939a538235686b448c0e)

crypto/cms/cms_lib.c
test/cmsapitest.c

index 4ad9302910f3ea2f7869453f881802a9cd41bea6..4aeb542eda819990921e19c40b79a6f33303ff60 100644 (file)
@@ -34,8 +34,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;
 }
 
index b40089becdca51c7371b8235e4a2394a7a285c2b..a19fb0683c404a098dd0fe49c882d2eac77f46a7 100644 (file)
@@ -289,18 +289,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;
 
@@ -308,6 +353,7 @@ static int test_d2i_CMS_bio_file_encrypted_data(void)
 end:
     CMS_ContentInfo_free(cms);
     BIO_free(bio);
+    OPENSSL_free(buf);
 
     return ret;
 }
@@ -357,7 +403,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;
 }