Replumbing: Add an OSSL_PROVIDER iterator with callback
authorRichard Levitte <levitte@openssl.org>
Mon, 25 Feb 2019 00:53:34 +0000 (01:53 +0100)
committerRichard Levitte <levitte@openssl.org>
Tue, 12 Mar 2019 19:25:46 +0000 (20:25 +0100)
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/8340)

crypto/provider_core.c
doc/internal/man3/ossl_provider_new.pod
include/internal/provider.h

index 05a6bfff49be85c9f6b3368924c8f2133b7c2c46..e7cbee2e439b62ec890d58ceeda2634d39a2a7c4 100644 (file)
@@ -328,6 +328,30 @@ int ossl_provider_activate(OSSL_PROVIDER *prov)
     return 1;
 }
 
+int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
+                                int (*cb)(OSSL_PROVIDER *provider,
+                                          void *cbdata),
+                                void *cbdata)
+{
+    int ret = 1;
+    int i;
+    struct provider_store_st *store = get_provider_store(ctx);
+
+    if (store != NULL) {
+        CRYPTO_THREAD_read_lock(store->lock);
+        for (i = 0; i < sk_OSSL_PROVIDER_num(store->providers); i++) {
+            OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
+
+            if (prov->flag_initialized
+                && !(ret = cb(prov, cbdata)))
+                break;
+        }
+        CRYPTO_THREAD_unlock(store->lock);
+    }
+
+    return ret;
+}
+
 /* Getters of Provider Object data */
 const char *ossl_provider_name(OSSL_PROVIDER *prov)
 {
index 79964d6548fbd30ff0fbdf8092762c0142b82dd3..c21012bf19abf0b1429437ec608cc9c3946f0fc7 100644 (file)
@@ -4,7 +4,8 @@
 
 ossl_provider_find, ossl_provider_new, ossl_provider_upref,
 ossl_provider_free, ossl_provider_add_module_location,
-ossl_provider_activate, ossl_provider_name, ossl_provider_dso,
+ossl_provider_activate, ossl_provider_forall_loaded,
+ossl_provider_name, ossl_provider_dso,
 ossl_provider_module_name, ossl_provider_module_path,
 ossl_provider_teardown, ossl_provider_get_param_types,
 ossl_provider_get_params - internal provider routines
@@ -25,6 +26,12 @@ ossl_provider_get_params - internal provider routines
  /* Load and initialize the Provider */
  int ossl_provider_activate(OSSL_PROVIDER *prov);
 
+ /* Iterate over all loaded providers */
+ int ossl_provider_forall_loaded(OPENSSL_CTX *,
+                                 int (*cb)(OSSL_PROVIDER *provider,
+                                           void *cbdata),
+                                 void *cbdata);
+
  /* Getters for other library functions */
  const char *ossl_provider_name(OSSL_PROVIDER *prov);
  const DSO *ossl_provider_dso(OSSL_PROVIDER *prov);
@@ -102,6 +109,9 @@ be located in that module, and called.
 
 =back
 
+ossl_provider_forall_loaded() iterates over all the currently
+"activated" providers, and calls C<cb> for each of them.
+
 ossl_provider_name() returns the name that was given with
 ossl_provider_new().
 
index 44d1d70461b23ed3236690ec8a8ec1c5ba94c92d..dee5ee6b9f2606e9b3cf733382e7c35003f3bed6 100644 (file)
@@ -42,6 +42,12 @@ int ossl_provider_add_module_location(OSSL_PROVIDER *prov, const char *loc);
  */
 int ossl_provider_activate(OSSL_PROVIDER *prov);
 
+/* Iterate over all loaded providers */
+int ossl_provider_forall_loaded(OPENSSL_CTX *,
+                                int (*cb)(OSSL_PROVIDER *provider,
+                                          void *cbdata),
+                                void *cbdata);
+
 /* Getters for other library functions */
 const char *ossl_provider_name(OSSL_PROVIDER *prov);
 const DSO *ossl_provider_dso(OSSL_PROVIDER *prov);