]>
Commit | Line | Data |
---|---|---|
9e11fe0d | 1 | /* |
4333b89f | 2 | * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. |
9e11fe0d RL |
3 | * |
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 | |
8 | */ | |
9 | ||
10 | #include <stddef.h> | |
11 | ||
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" | |
17 | ||
18 | struct construct_data_st { | |
b4250010 | 19 | OSSL_LIB_CTX *libctx; |
9e11fe0d RL |
20 | OSSL_METHOD_STORE *store; |
21 | int operation_id; | |
22 | int force_store; | |
23 | OSSL_METHOD_CONSTRUCT_METHOD *mcm; | |
24 | void *mcm_data; | |
25 | }; | |
26 | ||
5a29b628 RL |
27 | static int ossl_method_construct_precondition(OSSL_PROVIDER *provider, |
28 | int operation_id, void *cbdata, | |
29 | int *result) | |
30 | { | |
31 | if (!ossl_assert(result != NULL)) { | |
32 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); | |
33 | return 0; | |
34 | } | |
35 | ||
36 | if (!ossl_provider_test_operation_bit(provider, operation_id, result)) | |
37 | return 0; | |
38 | ||
39 | /* | |
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. | |
43 | */ | |
44 | *result = !*result; | |
45 | ||
46 | return 1; | |
47 | } | |
48 | ||
49 | static int ossl_method_construct_postcondition(OSSL_PROVIDER *provider, | |
50 | int operation_id, int no_store, | |
51 | void *cbdata, int *result) | |
52 | { | |
53 | if (!ossl_assert(result != NULL)) { | |
54 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); | |
55 | return 0; | |
56 | } | |
57 | ||
58 | *result = 1; | |
59 | return no_store != 0 | |
60 | || ossl_provider_set_operation_bit(provider, operation_id); | |
61 | } | |
62 | ||
84d167f6 RL |
63 | static void ossl_method_construct_this(OSSL_PROVIDER *provider, |
64 | const OSSL_ALGORITHM *algo, | |
65 | int no_store, void *cbdata) | |
9e11fe0d RL |
66 | { |
67 | struct construct_data_st *data = cbdata; | |
84d167f6 | 68 | void *method = NULL; |
9e11fe0d | 69 | |
36fa4d8a RL |
70 | if ((method = data->mcm->construct(algo, provider, data->mcm_data)) |
71 | == NULL) | |
84d167f6 RL |
72 | return; |
73 | ||
74 | /* | |
75 | * Note regarding putting the method in stores: | |
76 | * | |
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. | |
80 | * | |
81 | * It is *expected* that the put function increments the refcnt | |
82 | * of the passed method. | |
83 | */ | |
84 | ||
85 | if (data->force_store || !no_store) { | |
a3830831 | 86 | /* |
84d167f6 RL |
87 | * If we haven't been told not to store, |
88 | * add to the global store | |
a3830831 | 89 | */ |
c1d56231 | 90 | data->mcm->put(data->libctx, NULL, method, provider, |
695d195b | 91 | data->operation_id, algo->algorithm_names, |
84d167f6 | 92 | algo->property_definition, data->mcm_data); |
9e11fe0d RL |
93 | } |
94 | ||
c1d56231 | 95 | data->mcm->put(data->libctx, data->store, method, provider, |
695d195b | 96 | data->operation_id, algo->algorithm_names, |
c1d56231 | 97 | algo->property_definition, data->mcm_data); |
84d167f6 RL |
98 | |
99 | /* refcnt-- because we're dropping the reference */ | |
100 | data->mcm->destruct(method, data->mcm_data); | |
9e11fe0d RL |
101 | } |
102 | ||
b4250010 | 103 | void *ossl_method_construct(OSSL_LIB_CTX *libctx, int operation_id, |
9e11fe0d RL |
104 | int force_store, |
105 | OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data) | |
106 | { | |
107 | void *method = NULL; | |
108 | ||
f7c16d48 | 109 | if ((method = mcm->get(libctx, NULL, mcm_data)) == NULL) { |
9e11fe0d RL |
110 | struct construct_data_st cbdata; |
111 | ||
112 | /* | |
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. | |
115 | */ | |
1aedc35f | 116 | if ((cbdata.store = mcm->alloc_tmp_store(libctx)) == NULL) |
9e11fe0d RL |
117 | goto fin; |
118 | ||
119 | cbdata.libctx = libctx; | |
120 | cbdata.operation_id = operation_id; | |
121 | cbdata.force_store = force_store; | |
122 | cbdata.mcm = mcm; | |
123 | cbdata.mcm_data = mcm_data; | |
84d167f6 | 124 | ossl_algorithm_do_all(libctx, operation_id, NULL, |
5a29b628 RL |
125 | ossl_method_construct_precondition, |
126 | ossl_method_construct_this, | |
127 | ossl_method_construct_postcondition, | |
128 | &cbdata); | |
9e11fe0d | 129 | |
f7c16d48 | 130 | method = mcm->get(libctx, cbdata.store, mcm_data); |
b11ba50f MC |
131 | if (method == NULL) { |
132 | /* | |
133 | * If we get here then we did not construct the method that we | |
134 | * attempted to construct. It's possible that another thread got | |
135 | * there first and so we skipped construction (pre-condition | |
136 | * failed). We check the global store again to see if it has | |
137 | * appeared by now. | |
138 | */ | |
139 | method = mcm->get(libctx, NULL, mcm_data); | |
140 | } | |
9e11fe0d RL |
141 | mcm->dealloc_tmp_store(cbdata.store); |
142 | } | |
143 | ||
144 | fin: | |
145 | return method; | |
146 | } |