2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/core.h>
13 #include "internal/cryptlib.h"
14 #include "internal/core.h"
15 #include "internal/property.h"
16 #include "internal/provider.h"
18 struct construct_data_st {
20 OSSL_METHOD_STORE *store;
23 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
27 static int is_temporary_method_store(int no_store, void *cbdata)
29 struct construct_data_st *data = cbdata;
31 return no_store && !data->force_store;
34 static int ossl_method_construct_reserve_store(int no_store, void *cbdata)
36 struct construct_data_st *data = cbdata;
38 if (is_temporary_method_store(no_store, data) && data->store == NULL) {
40 * If we have been told not to store the method "permanently", we
41 * ask for a temporary store, and store the method there.
42 * The owner of |data->mcm| is completely responsible for managing
43 * that temporary store.
45 if ((data->store = data->mcm->get_tmp_store(data->mcm_data)) == NULL)
49 return data->mcm->lock_store(data->store, data->mcm_data);
52 static int ossl_method_construct_unreserve_store(void *cbdata)
54 struct construct_data_st *data = cbdata;
56 return data->mcm->unlock_store(data->store, data->mcm_data);
59 static int ossl_method_construct_precondition(OSSL_PROVIDER *provider,
60 int operation_id, int no_store,
61 void *cbdata, int *result)
63 if (!ossl_assert(result != NULL)) {
64 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
68 /* Assume that no bits are set */
71 /* No flag bits for temporary stores */
72 if (!is_temporary_method_store(no_store, cbdata)
73 && !ossl_provider_test_operation_bit(provider, operation_id, result))
77 * The result we get tells if methods have already been constructed.
78 * However, we want to tell whether construction should happen (true)
79 * or not (false), which is the opposite of what we got.
86 static int ossl_method_construct_postcondition(OSSL_PROVIDER *provider,
87 int operation_id, int no_store,
88 void *cbdata, int *result)
90 if (!ossl_assert(result != NULL)) {
91 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
97 /* No flag bits for temporary stores */
98 return is_temporary_method_store(no_store, cbdata)
99 || ossl_provider_set_operation_bit(provider, operation_id);
102 static void ossl_method_construct_this(OSSL_PROVIDER *provider,
103 const OSSL_ALGORITHM *algo,
104 int no_store, void *cbdata)
106 struct construct_data_st *data = cbdata;
109 if ((method = data->mcm->construct(algo, provider, data->mcm_data))
114 * Note regarding putting the method in stores:
116 * we don't need to care if it actually got in or not here.
117 * If it didn't get in, it will simply not be available when
118 * ossl_method_construct() tries to get it from the store.
120 * It is *expected* that the put function increments the refcnt
121 * of the passed method.
123 data->mcm->put(data->store, method, provider, algo->algorithm_names,
124 algo->property_definition, data->mcm_data);
126 /* refcnt-- because we're dropping the reference */
127 data->mcm->destruct(method, data->mcm_data);
130 void *ossl_method_construct(OSSL_LIB_CTX *libctx, int operation_id,
131 OSSL_PROVIDER **provider_rw, int force_store,
132 OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data)
135 OSSL_PROVIDER *provider = provider_rw != NULL ? *provider_rw : NULL;
136 struct construct_data_st cbdata;
139 * We might be tempted to try to look into the method store without
140 * constructing to see if we can find our method there already.
141 * Unfortunately that does not work well if the query contains
142 * optional properties as newly loaded providers can match them better.
143 * We trust that ossl_method_construct_precondition() and
144 * ossl_method_construct_postcondition() make sure that the
145 * ossl_algorithm_do_all() does very little when methods from
146 * a provider have already been constructed.
150 cbdata.force_store = force_store;
152 cbdata.mcm_data = mcm_data;
153 ossl_algorithm_do_all(libctx, operation_id, provider,
154 ossl_method_construct_precondition,
155 ossl_method_construct_reserve_store,
156 ossl_method_construct_this,
157 ossl_method_construct_unreserve_store,
158 ossl_method_construct_postcondition,
161 /* If there is a temporary store, try there first */
162 if (cbdata.store != NULL)
163 method = mcm->get(cbdata.store, (const OSSL_PROVIDER **)provider_rw,
166 /* If no method was found yet, try the global store */
168 method = mcm->get(NULL, (const OSSL_PROVIDER **)provider_rw, mcm_data);