Load the default provider into the p_test provider later
authorMatt Caswell <matt@openssl.org>
Thu, 13 May 2021 14:52:19 +0000 (15:52 +0100)
committerMatt Caswell <matt@openssl.org>
Sat, 15 May 2021 09:33:52 +0000 (10:33 +0100)
Loading it earlier causes some of the later testing to pass when it should
fail and masked a bug.

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

test/p_test.c

index 8c7bdaf7b86e5afd284ea1c4d54ad9c5ecdaee68..22bf8648fe10f1e7d2c8c3c28d016e6702053e28 100644 (file)
@@ -39,7 +39,6 @@ typedef struct p_test_ctx {
     char *thisfunc;
     const OSSL_CORE_HANDLE *handle;
     OSSL_LIB_CTX *libctx;
-    OSSL_PROVIDER *deflt;
 } P_TEST_CTX;
 
 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
@@ -61,6 +60,18 @@ static OSSL_FUNC_provider_get_params_fn p_get_params;
 static OSSL_FUNC_provider_get_reason_strings_fn p_get_reason_strings;
 static OSSL_FUNC_provider_teardown_fn p_teardown;
 
+static void p_set_error(int lib, int reason, const char *file, int line,
+                        const char *func, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    c_new_error(NULL);
+    c_set_error_debug(NULL, file, line, func);
+    c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, ap);
+    va_end(ap);
+}
+
 static const OSSL_PARAM *p_gettable_params(void *_)
 {
     return p_param_types;
@@ -128,6 +139,20 @@ static int p_get_params(void *provctx, OSSL_PARAM params[])
             EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
             const char *msg = "Hello world";
             unsigned char out[16];
+            OSSL_PROVIDER *deflt;
+
+            /*
+            * "default" has not been loaded into the parent libctx. We should be able
+            * to explicitly load it as a non-child provider.
+            */
+            deflt = OSSL_PROVIDER_load(ctx->libctx, "default");
+            if (deflt == NULL
+                    || !OSSL_PROVIDER_available(ctx->libctx, "default")) {
+                /* We set error "3" for a failure to load the default provider */
+                p_set_error(ERR_LIB_PROV, 3, ctx->thisfile, OPENSSL_LINE,
+                            ctx->thisfunc, NULL);
+                ok = 0;
+            }
 
             /*
              * We should have the default provider available that we loaded
@@ -135,7 +160,8 @@ static int p_get_params(void *provctx, OSSL_PARAM params[])
              * from the parent libctx. We should also have "this" provider
              * available.
              */
-            if (OSSL_PROVIDER_available(ctx->libctx, "default")
+            if (ok
+                    && OSSL_PROVIDER_available(ctx->libctx, "default")
                     && OSSL_PROVIDER_available(ctx->libctx, "base")
                     && OSSL_PROVIDER_available(ctx->libctx, "legacy")
                     && OSSL_PROVIDER_available(ctx->libctx, "p_test")
@@ -144,11 +170,12 @@ static int p_get_params(void *provctx, OSSL_PARAM params[])
                 if (EVP_DigestInit_ex(mdctx, md4, NULL)
                         && EVP_DigestUpdate(mdctx, (const unsigned char *)msg,
                                             strlen(msg))
-                        &&EVP_DigestFinal(mdctx, out, NULL))
+                        && EVP_DigestFinal(mdctx, out, NULL))
                     digestsuccess = 1;
             }
             EVP_MD_CTX_free(mdctx);
             EVP_MD_free(md4);
+            OSSL_PROVIDER_unload(deflt);
 #endif
             if (p->data_size >= sizeof(digestsuccess)) {
                 *(unsigned int *)p->data = digestsuccess;
@@ -161,18 +188,6 @@ static int p_get_params(void *provctx, OSSL_PARAM params[])
     return ok;
 }
 
-static void p_set_error(int lib, int reason, const char *file, int line,
-                        const char *func, const char *fmt, ...)
-{
-    va_list ap;
-
-    va_start(ap, fmt);
-    c_new_error(NULL);
-    c_set_error_debug(NULL, file, line, func);
-    c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, ap);
-    va_end(ap);
-}
-
 static const OSSL_ITEM *p_get_reason_strings(void *_)
 {
     static const OSSL_ITEM reason_strings[] = {
@@ -251,19 +266,6 @@ int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
         p_teardown(ctx);
         return 0;
     }
-    /*
-     * "default" has not been loaded into the parent libctx. We should be able
-     * to explicitly load it as a non-child provider.
-     */
-    ctx->deflt = OSSL_PROVIDER_load(ctx->libctx, "default");
-    if (ctx->deflt == NULL
-            || !OSSL_PROVIDER_available(ctx->libctx, "default")) {
-        /* We set error "3" for a failure to load the default provider */
-        p_set_error(ERR_LIB_PROV, 3, ctx->thisfile, OPENSSL_LINE, ctx->thisfunc,
-                    NULL);
-        p_teardown(ctx);
-        return 0;
-    }
 #endif
 
     /*
@@ -282,7 +284,6 @@ static void p_teardown(void *provctx)
     P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;
 
 #ifdef PROVIDER_INIT_FUNCTION_NAME
-    OSSL_PROVIDER_unload(ctx->deflt);
     OSSL_LIB_CTX_free(ctx->libctx);
 #endif
     free(ctx->thisfile);