#include <stddef.h>
#include <openssl/core.h>
#include <openssl/trace.h>
#include "internal/cryptlib.h"
#include "internal/core.h"
#include "internal/property.h"
#include "internal/provider.h"
struct construct_data_st {
OSSL_LIB_CTX *libctx;
OSSL_METHOD_STORE *store;
int operation_id;
int force_store;
OSSL_METHOD_CONSTRUCT_METHOD *mcm;
void *mcm_data;
};
static int is_temporary_method_store(int no_store, void *cbdata)
{
struct construct_data_st *data = cbdata;
return no_store && !data->force_store;
}
static int ossl_method_construct_reserve_store(int no_store, void *cbdata)
{
struct construct_data_st *data = cbdata;
if (is_temporary_method_store(no_store, data) && data->store == NULL) {
if ((data->store = data->mcm->get_tmp_store(data->mcm_data)) == NULL)
return 0;
}
return data->mcm->lock_store(data->store, data->mcm_data);
}
static int ossl_method_construct_unreserve_store(void *cbdata)
{
struct construct_data_st *data = cbdata;
return data->mcm->unlock_store(data->store, data->mcm_data);
}
static int ossl_method_construct_precondition(OSSL_PROVIDER *provider,
int operation_id, int no_store,
void *cbdata, int *result)
{
if (!ossl_assert(result != NULL)) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
*result = 0;
if (!is_temporary_method_store(no_store, cbdata)
&& !ossl_provider_test_operation_bit(provider, operation_id, result))
return 0;
*result = !*result;
return 1;
}
static int ossl_method_construct_postcondition(OSSL_PROVIDER *provider,
int operation_id, int no_store,
void *cbdata, int *result)
{
if (!ossl_assert(result != NULL)) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
*result = 1;
return is_temporary_method_store(no_store, cbdata)
|| ossl_provider_set_operation_bit(provider, operation_id);
}
static void ossl_method_construct_this(OSSL_PROVIDER *provider,
const OSSL_ALGORITHM *algo,
int no_store, void *cbdata)
{
struct construct_data_st *data = cbdata;
void *method = NULL;
if ((method = data->mcm->construct(algo, provider, data->mcm_data))
== NULL)
return;
OSSL_TRACE2(QUERY,
"ossl_method_construct_this: putting an algo to the store %p with no_store %d\n",
(void *)data->store, no_store);
data->mcm->put(no_store ? data->store : NULL, method, provider, algo->algorithm_names,
algo->property_definition, data->mcm_data);
data->mcm->destruct(method, data->mcm_data);
}
void *ossl_method_construct(OSSL_LIB_CTX *libctx, int operation_id,
OSSL_PROVIDER **provider_rw, int force_store,
OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data)
{
void *method = NULL;
OSSL_PROVIDER *provider = provider_rw != NULL ? *provider_rw : NULL;
struct construct_data_st cbdata;
cbdata.store = NULL;
cbdata.force_store = force_store;
cbdata.mcm = mcm;
cbdata.mcm_data = mcm_data;
ossl_algorithm_do_all(libctx, operation_id, provider,
ossl_method_construct_precondition,
ossl_method_construct_reserve_store,
ossl_method_construct_this,
ossl_method_construct_unreserve_store,
ossl_method_construct_postcondition,
&cbdata);
if (cbdata.store != NULL)
method = mcm->get(cbdata.store, (const OSSL_PROVIDER **)provider_rw,
mcm_data);
if (method == NULL)
method = mcm->get(NULL, (const OSSL_PROVIDER **)provider_rw, mcm_data);
return method;
}