summaryrefslogtreecommitdiffstats
path: root/crypto/core_fetch.c
diff options
context:
space:
mode:
authorRichard Levitte <levitte@openssl.org>2019-02-25 01:59:02 +0100
committerRichard Levitte <levitte@openssl.org>2019-03-12 20:25:46 +0100
commit9e11fe0d85c7d8bd2b77076c8b2e93433091e765 (patch)
tree2744ef817c2bd1631fed146293d55ef00c90966d /crypto/core_fetch.c
parent099bd33920e775eb75f4daee5f09b24f17bc136d (diff)
Replumbing: Add constructor of libcrypto internal method structures
This queries the provider for its available functionality (unless a matching method structured is already cached, in which case that's used instead), and creates method structure with the help of a passed constructor. The result is cached if the provider allows it (or if caching is forced). Reviewed-by: Paul Dale <paul.dale@oracle.com> (Merged from https://github.com/openssl/openssl/pull/8340)
Diffstat (limited to 'crypto/core_fetch.c')
-rw-r--r--crypto/core_fetch.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/crypto/core_fetch.c b/crypto/core_fetch.c
new file mode 100644
index 0000000000..d2d7766512
--- /dev/null
+++ b/crypto/core_fetch.c
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
+ * this file except in compliance with the License. You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <stddef.h>
+
+#include <openssl/core.h>
+#include "internal/cryptlib.h"
+#include "internal/core.h"
+#include "internal/property.h"
+#include "internal/provider.h"
+
+struct construct_data_st {
+ OPENSSL_CTX *libctx;
+ OSSL_METHOD_STORE *store;
+ int operation_id;
+ int force_store;
+ OSSL_METHOD_CONSTRUCT_METHOD *mcm;
+ void *mcm_data;
+};
+
+static int ossl_method_construct_this(OSSL_PROVIDER *provider, void *cbdata)
+{
+ struct construct_data_st *data = cbdata;
+ int no_store = 0; /* Assume caching is ok */
+ const OSSL_ALGORITHM *map =
+ ossl_provider_query_operation(provider, data->operation_id, &no_store);
+
+ while (map->algorithm_name != NULL) {
+ const OSSL_ALGORITHM *thismap = map++;
+ void *method = NULL;
+
+ if ((method = data->mcm->construct(thismap->implementation, provider,
+ data->mcm_data)) == NULL)
+ continue;
+
+ if (data->force_store || !no_store) {
+ /*
+ * If we haven't been told not to store,
+ * add to the global store
+ */
+ if (!data->mcm->put(data->libctx, NULL,
+ thismap->property_definition,
+ method, data->mcm_data)) {
+ data->mcm->destruct(method);
+ continue;
+ }
+ }
+
+ if (!data->mcm->put(data->libctx, data->store,
+ thismap->property_definition,
+ method, data->mcm_data)) {
+ data->mcm->destruct(method);
+ continue;
+ }
+ }
+
+ return 1;
+}
+
+void *ossl_method_construct(OPENSSL_CTX *libctx, int operation_id,
+ const char *name, const char *propquery,
+ int force_store,
+ OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data)
+{
+ void *method = NULL;
+
+ if ((method = mcm->get(libctx, NULL, propquery, mcm_data)) == NULL) {
+ struct construct_data_st cbdata;
+
+ /*
+ * We have a temporary store to be able to easily search among new
+ * items, or items that should find themselves in the global store.
+ */
+ if ((cbdata.store = mcm->alloc_tmp_store()) == NULL)
+ goto fin;
+
+ cbdata.libctx = libctx;
+ cbdata.operation_id = operation_id;
+ cbdata.force_store = force_store;
+ cbdata.mcm = mcm;
+ cbdata.mcm_data = mcm_data;
+ ossl_provider_forall_loaded(libctx, ossl_method_construct_this,
+ &cbdata);
+
+ method = mcm->get(libctx, cbdata.store, propquery, mcm_data);
+ mcm->dealloc_tmp_store(cbdata.store);
+ }
+
+ fin:
+ return method;
+}