]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Extend test case for reused PEM_ASN1_read_bio
authorBernd Edlinger <bernd.edlinger@hotmail.de>
Fri, 24 Nov 2023 06:02:35 +0000 (07:02 +0100)
committerTomas Mraz <tomas@openssl.org>
Fri, 16 Aug 2024 08:07:52 +0000 (10:07 +0200)
This is related to #22780, simply add test cases
for the different failure modes of PEM_ASN1_read_bio.
Depending on whether the PEM or the DER format is valid or not,
the passed in CRL may be deleted ot not, therefore a statement
like this:

reused_crl = PEM_read_bio_X509_CRL(b, &reused_crl, NULL, NULL);

must be avoided, because it can create memory leaks.

Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22809)

test/crltest.c

index 66ee17381d8b676080855f59c01a01e3c73a00a3..76b53f46dc837f59f4b0c9d1ba86708f870bb3d9 100644 (file)
@@ -101,6 +101,13 @@ static const char *kRevokedCRL[] = {
     NULL
 };
 
+static const char *kInvalidCRL[] = {
+    "-----BEGIN X509 CRL-----\n",
+    "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n",
+    "-----END X509 CRL-----\n",
+    NULL
+};
+
 static const char *kBadIssuerCRL[] = {
     "-----BEGIN X509 CRL-----\n",
     "MIIBwjCBqwIBATANBgkqhkiG9w0BAQsFADBSMQswCQYDVQQGEwJVUzETMBEGA1UE\n",
@@ -371,24 +378,45 @@ static int test_unknown_critical_crl(int n)
     return r;
 }
 
-static int test_reuse_crl(void)
+static int test_reuse_crl(int idx)
 {
-    X509_CRL *reused_crl = CRL_from_strings(kBasicCRL);
+    X509_CRL *result, *reused_crl = CRL_from_strings(kBasicCRL);
     char *p;
-    BIO *b = glue2bio(kRevokedCRL, &p);
+    BIO *b = glue2bio(idx == 2 ? kRevokedCRL : kInvalidCRL + idx, &p);
+    int r = 0;
 
-    if (b == NULL) {
-        OPENSSL_free(p);
-        X509_CRL_free(reused_crl);
-        return 0;
+    if (!TEST_ptr(reused_crl)
+            || !TEST_ptr(b))
+        goto err;
+
+    result = PEM_read_bio_X509_CRL(b, &reused_crl, NULL, NULL);
+
+    switch (idx) {
+    case 0: /* valid PEM + invalid DER */
+        if (!TEST_ptr_null(result)
+                || !TEST_ptr_null(reused_crl))
+            goto err;
+        break;
+    case 1: /* invalid PEM */
+        if (!TEST_ptr_null(result)
+                || !TEST_ptr(reused_crl))
+            goto err;
+        break;
+    case 2:
+        if (!TEST_ptr(result)
+                || !TEST_ptr(reused_crl)
+                || !TEST_ptr_eq(result, reused_crl))
+            goto err;
+        break;
     }
 
-    reused_crl = PEM_read_bio_X509_CRL(b, &reused_crl, NULL, NULL);
+    r = 1;
 
+ err:
     OPENSSL_free(p);
     BIO_free(b);
     X509_CRL_free(reused_crl);
-    return 1;
+    return r;
 }
 
 int setup_tests(void)
@@ -402,7 +430,7 @@ int setup_tests(void)
     ADD_TEST(test_bad_issuer_crl);
     ADD_TEST(test_known_critical_crl);
     ADD_ALL_TESTS(test_unknown_critical_crl, OSSL_NELEM(unknown_critical_crls));
-    ADD_TEST(test_reuse_crl);
+    ADD_ALL_TESTS(test_reuse_crl, 3);
     return 1;
 }