Add negative test cases for PEM_read_bio_PrivateKey
authorTomas Mraz <tomas@openssl.org>
Tue, 25 May 2021 07:58:35 +0000 (09:58 +0200)
committerTomas Mraz <tomas@openssl.org>
Wed, 26 May 2021 11:04:38 +0000 (13:04 +0200)
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15441)

test/evp_extra_test2.c

index 3f7edac27832fd408abb3659577e0c0793b95831..e480ae9555e3216653f68a800687eb07f18d1bc9 100644 (file)
@@ -414,6 +414,41 @@ static int test_d2i_PrivateKey_ex(int testid)
     return ok;
 }
 
+static int test_PEM_read_bio_negative(int testid)
+{
+    int ok = 0;
+    OSSL_PROVIDER *provider = NULL;
+    BIO *key_bio = NULL;
+    EVP_PKEY *pkey = NULL;
+
+    if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[testid].kder, keydata[testid].size)))
+        goto err;
+    ERR_clear_error();
+    if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
+        goto err;
+    if (!TEST_int_ne(ERR_peek_error(), 0))
+        goto err;
+    if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
+        goto err;
+    if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
+        goto err;
+    ERR_clear_error();
+    if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
+        goto err;
+    if (!TEST_int_ne(ERR_peek_error(), 0))
+        goto err;
+
+    ok = 1;
+
+ err:
+    test_openssl_errors();
+    EVP_PKEY_free(pkey);
+    BIO_free(key_bio);
+    OSSL_PROVIDER_unload(provider);
+
+    return ok;
+}
+
 static int do_fromdata_key_is_equal(const OSSL_PARAM params[],
                                     const EVP_PKEY *expected, const char *type)
 {
@@ -807,6 +842,7 @@ int setup_tests(void)
 #ifndef OPENSSL_NO_DES
     ADD_TEST(test_pkcs8key_nid_bio);
 #endif
+    ADD_ALL_TESTS(test_PEM_read_bio_negative, OSSL_NELEM(keydata));
     return 1;
 }