2 * Copyright 2019-2020 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 ossl_method_construct_precondition(OSSL_PROVIDER
*provider
,
28 int operation_id
, void *cbdata
,
31 if (!ossl_assert(result
!= NULL
)) {
32 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_PASSED_NULL_PARAMETER
);
36 if (!ossl_provider_test_operation_bit(provider
, operation_id
, result
))
40 * The result we get tells if methods have already been constructed.
41 * However, we want to tell whether construction should happen (true)
42 * or not (false), which is the opposite of what we got.
49 static int ossl_method_construct_postcondition(OSSL_PROVIDER
*provider
,
50 int operation_id
, int no_store
,
51 void *cbdata
, int *result
)
53 if (!ossl_assert(result
!= NULL
)) {
54 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_PASSED_NULL_PARAMETER
);
60 || ossl_provider_set_operation_bit(provider
, operation_id
);
63 static void ossl_method_construct_this(OSSL_PROVIDER
*provider
,
64 const OSSL_ALGORITHM
*algo
,
65 int no_store
, void *cbdata
)
67 struct construct_data_st
*data
= cbdata
;
70 if ((method
= data
->mcm
->construct(algo
, provider
, data
->mcm_data
))
75 * Note regarding putting the method in stores:
77 * we don't need to care if it actually got in or not here.
78 * If it didn't get in, it will simply not be available when
79 * ossl_method_construct() tries to get it from the store.
81 * It is *expected* that the put function increments the refcnt
82 * of the passed method.
85 if (data
->force_store
|| !no_store
) {
87 * If we haven't been told not to store,
88 * add to the global store
90 data
->mcm
->put(data
->libctx
, NULL
, method
, provider
,
91 data
->operation_id
, algo
->algorithm_names
,
92 algo
->property_definition
, data
->mcm_data
);
95 data
->mcm
->put(data
->libctx
, data
->store
, method
, provider
,
96 data
->operation_id
, algo
->algorithm_names
,
97 algo
->property_definition
, data
->mcm_data
);
99 /* refcnt-- because we're dropping the reference */
100 data
->mcm
->destruct(method
, data
->mcm_data
);
103 void *ossl_method_construct(OSSL_LIB_CTX
*libctx
, int operation_id
,
105 OSSL_METHOD_CONSTRUCT_METHOD
*mcm
, void *mcm_data
)
109 if ((method
= mcm
->get(libctx
, NULL
, mcm_data
)) == NULL
) {
110 struct construct_data_st cbdata
;
113 * We have a temporary store to be able to easily search among new
114 * items, or items that should find themselves in the global store.
116 if ((cbdata
.store
= mcm
->alloc_tmp_store(libctx
)) == NULL
)
119 cbdata
.libctx
= libctx
;
120 cbdata
.operation_id
= operation_id
;
121 cbdata
.force_store
= force_store
;
123 cbdata
.mcm_data
= mcm_data
;
124 ossl_algorithm_do_all(libctx
, operation_id
, NULL
,
125 ossl_method_construct_precondition
,
126 ossl_method_construct_this
,
127 ossl_method_construct_postcondition
,
130 method
= mcm
->get(libctx
, cbdata
.store
, mcm_data
);
131 mcm
->dealloc_tmp_store(cbdata
.store
);