From 814c2018e11c99aeb3d84e0fee2b3943ff4039c8 Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Mon, 21 Jun 2021 12:08:39 +0100 Subject: [PATCH] Merge ossl_provider_activate() and ossl_provider_activate_child() These 2 functions have become so close to each other that they may as well be just one function. Reviewed-by: Paul Dale Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/15854) --- crypto/provider.c | 2 +- crypto/provider_child.c | 4 ++-- crypto/provider_conf.c | 2 +- crypto/provider_core.c | 45 ++++++++--------------------------- include/internal/provider.h | 5 +--- test/provider_internal_test.c | 2 +- 6 files changed, 16 insertions(+), 44 deletions(-) diff --git a/crypto/provider.c b/crypto/provider.c index f5dbc4f94a4..6b5b7beefec 100644 --- a/crypto/provider.c +++ b/crypto/provider.c @@ -26,7 +26,7 @@ OSSL_PROVIDER *OSSL_PROVIDER_try_load(OSSL_LIB_CTX *libctx, const char *name, isnew = 1; } - if (!ossl_provider_activate(prov, 1)) { + if (!ossl_provider_activate(prov, 1, 0)) { ossl_provider_free(prov); return NULL; } diff --git a/crypto/provider_child.c b/crypto/provider_child.c index b077e95ffcb..3cad1c564f4 100644 --- a/crypto/provider_child.c +++ b/crypto/provider_child.c @@ -137,7 +137,7 @@ static int provider_create_child_cb(const OSSL_CORE_HANDLE *prov, void *cbdata) * or it could have been explicitly loaded. If explicitly loaded we * ignore it - i.e. we don't start treating it like a child. */ - if (!ossl_provider_activate_child(cprov, prov, ossl_child_provider_init)) + if (!ossl_provider_activate(cprov, 0, 1)) goto err; } else { /* @@ -148,7 +148,7 @@ static int provider_create_child_cb(const OSSL_CORE_HANDLE *prov, void *cbdata) 1)) == NULL) goto err; - if (!ossl_provider_activate(cprov, 0)) + if (!ossl_provider_activate(cprov, 0, 0)) goto err; if (!ossl_provider_set_child(cprov, prov) diff --git a/crypto/provider_conf.c b/crypto/provider_conf.c index 1e59e959e3a..398340c3d2b 100644 --- a/crypto/provider_conf.c +++ b/crypto/provider_conf.c @@ -171,7 +171,7 @@ static int provider_conf_load(OSSL_LIB_CTX *libctx, const char *name, ok = provider_conf_params(prov, NULL, NULL, value, cnf); if (ok) { - if (!ossl_provider_activate(prov, 1)) { + if (!ossl_provider_activate(prov, 1, 0)) { ok = 0; } else if (!ossl_provider_add_to_store(prov, 0)) { ossl_provider_deactivate(prov); diff --git a/crypto/provider_core.c b/crypto/provider_core.c index 83c6bf28f05..e41c49e0ad2 100644 --- a/crypto/provider_core.c +++ b/crypto/provider_core.c @@ -431,7 +431,7 @@ int ossl_provider_up_ref(OSSL_PROVIDER *prov) static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate) { if (activate) - return ossl_provider_activate(prov, 1); + return ossl_provider_activate(prov, 1, 0); return ossl_provider_up_ref(prov); } @@ -1027,12 +1027,20 @@ static int provider_flush_store_cache(const OSSL_PROVIDER *prov) return 1; } -int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls) +int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild) { int count; if (prov == NULL) return 0; +#ifndef FIPS_MODULE + /* + * If aschild is true, then we only actually do the activation if the + * provider is a child. If its not, this is still success. + */ + if (aschild && !prov->ischild) + return 1; +#endif if ((count = provider_activate(prov, 1, upcalls)) > 0) return count == 1 ? provider_flush_store_cache(prov) : 1; @@ -1462,39 +1470,6 @@ int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle) return 1; } -int ossl_provider_activate_child(OSSL_PROVIDER *prov, - const OSSL_CORE_HANDLE *handle, - OSSL_provider_init_fn *init_function) -{ - int flush = 0; - - if (!CRYPTO_THREAD_write_lock(prov->store->lock)) - return 0; - if (!CRYPTO_THREAD_write_lock(prov->flag_lock)) { - CRYPTO_THREAD_unlock(prov->store->lock); - return 0; - } - /* - * The provider could be in one of two states: (1) Already a child, - * (2) Not a child (not eligible to be one). - */ - if (prov->ischild && provider_activate(prov, 0, 0)) - flush = 1; - - CRYPTO_THREAD_unlock(prov->flag_lock); - CRYPTO_THREAD_unlock(prov->store->lock); - - if (flush) - provider_flush_store_cache(prov); - - /* - * We report success whether or not the provider was a child. If its not - * a child then it has been explicitly loaded as a non child provider and - * we should keep it like that. - */ - return 1; -} - int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props) { #ifndef FIPS_MODULE diff --git a/include/internal/provider.h b/include/internal/provider.h index b6e413f7a41..9b1d9495dda 100644 --- a/include/internal/provider.h +++ b/include/internal/provider.h @@ -44,9 +44,6 @@ int ossl_provider_add_parameter(OSSL_PROVIDER *prov, const char *name, int ossl_provider_is_child(const OSSL_PROVIDER *prov); int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle); -int ossl_provider_activate_child(OSSL_PROVIDER *prov, - const OSSL_CORE_HANDLE *handle, - OSSL_provider_init_fn *init_function); const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov); int ossl_provider_up_ref_parent(OSSL_PROVIDER *prov, int activate); int ossl_provider_free_parent(OSSL_PROVIDER *prov, int deactivate); @@ -59,7 +56,7 @@ int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx); * Activate the Provider * If the Provider is a module, the module will be loaded */ -int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls); +int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild); int ossl_provider_deactivate(OSSL_PROVIDER *prov); int ossl_provider_add_to_store(OSSL_PROVIDER *prov, int retain_fallbacks); diff --git a/test/provider_internal_test.c b/test/provider_internal_test.c index 87906c1bdcb..d9cc68d59dc 100644 --- a/test/provider_internal_test.c +++ b/test/provider_internal_test.c @@ -26,7 +26,7 @@ static int test_provider(OSSL_PROVIDER *prov, const char *expected_greeting) int ret = 0; ret = - TEST_true(ossl_provider_activate(prov, 1)) + TEST_true(ossl_provider_activate(prov, 1, 0)) && TEST_true(ossl_provider_get_params(prov, greeting_request)) && TEST_ptr(greeting = greeting_request[0].data) && TEST_size_t_gt(greeting_request[0].data_size, 0) -- 2.47.2