/src/openssl/crypto/core_fetch.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved. |
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 <openssl/trace.h> |
14 | | #include "internal/cryptlib.h" |
15 | | #include "internal/core.h" |
16 | | #include "internal/property.h" |
17 | | #include "internal/provider.h" |
18 | | |
19 | | struct construct_data_st { |
20 | | OSSL_LIB_CTX *libctx; |
21 | | OSSL_METHOD_STORE *store; |
22 | | int operation_id; |
23 | | int force_store; |
24 | | OSSL_METHOD_CONSTRUCT_METHOD *mcm; |
25 | | void *mcm_data; |
26 | | }; |
27 | | |
28 | | static int is_temporary_method_store(int no_store, void *cbdata) |
29 | 3.94k | { |
30 | 3.94k | struct construct_data_st *data = cbdata; |
31 | | |
32 | 3.94k | return no_store && !data->force_store; |
33 | 3.94k | } |
34 | | |
35 | | static int ossl_method_construct_reserve_store(int no_store, void *cbdata) |
36 | 1.96k | { |
37 | 1.96k | struct construct_data_st *data = cbdata; |
38 | | |
39 | 1.96k | if (is_temporary_method_store(no_store, data) && data->store == NULL) { |
40 | | /* |
41 | | * If we have been told not to store the method "permanently", we |
42 | | * ask for a temporary store, and store the method there. |
43 | | * The owner of |data->mcm| is completely responsible for managing |
44 | | * that temporary store. |
45 | | */ |
46 | 0 | if ((data->store = data->mcm->get_tmp_store(data->mcm_data)) == NULL) |
47 | 0 | return 0; |
48 | 0 | } |
49 | | |
50 | 1.96k | return data->mcm->lock_store(data->store, data->mcm_data); |
51 | 1.96k | } |
52 | | |
53 | | static int ossl_method_construct_unreserve_store(void *cbdata) |
54 | 1.96k | { |
55 | 1.96k | struct construct_data_st *data = cbdata; |
56 | | |
57 | 1.96k | return data->mcm->unlock_store(data->store, data->mcm_data); |
58 | 1.96k | } |
59 | | |
60 | | static int ossl_method_construct_precondition(OSSL_PROVIDER *provider, |
61 | | int operation_id, int no_store, |
62 | | void *cbdata, int *result) |
63 | 1.96k | { |
64 | 1.96k | if (!ossl_assert(result != NULL)) { |
65 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); |
66 | 0 | return 0; |
67 | 0 | } |
68 | | |
69 | | /* Assume that no bits are set */ |
70 | 1.96k | *result = 0; |
71 | | |
72 | | /* No flag bits for temporary stores */ |
73 | 1.96k | if (!is_temporary_method_store(no_store, cbdata) |
74 | 1.96k | && !ossl_provider_test_operation_bit(provider, operation_id, result)) |
75 | 0 | return 0; |
76 | | |
77 | | /* |
78 | | * The result we get tells if methods have already been constructed. |
79 | | * However, we want to tell whether construction should happen (true) |
80 | | * or not (false), which is the opposite of what we got. |
81 | | */ |
82 | 1.96k | *result = !*result; |
83 | | |
84 | 1.96k | return 1; |
85 | 1.96k | } |
86 | | |
87 | | static int ossl_method_construct_postcondition(OSSL_PROVIDER *provider, |
88 | | int operation_id, int no_store, |
89 | | void *cbdata, int *result) |
90 | 9 | { |
91 | 9 | if (!ossl_assert(result != NULL)) { |
92 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | 9 | *result = 1; |
97 | | |
98 | | /* No flag bits for temporary stores */ |
99 | 9 | return is_temporary_method_store(no_store, cbdata) |
100 | 9 | || ossl_provider_set_operation_bit(provider, operation_id); |
101 | 9 | } |
102 | | |
103 | | static void ossl_method_construct_this(OSSL_PROVIDER *provider, |
104 | | const OSSL_ALGORITHM *algo, |
105 | | int no_store, void *cbdata) |
106 | 278 | { |
107 | 278 | struct construct_data_st *data = cbdata; |
108 | 278 | void *method = NULL; |
109 | | |
110 | 278 | if ((method = data->mcm->construct(algo, provider, data->mcm_data)) |
111 | 278 | == NULL) |
112 | 0 | return; |
113 | | |
114 | 278 | OSSL_TRACE2(QUERY, |
115 | 278 | "ossl_method_construct_this: putting an algo to the store %p with no_store %d\n", |
116 | 278 | (void *)data->store, no_store); |
117 | | /* |
118 | | * Note regarding putting the method in stores: |
119 | | * |
120 | | * we don't need to care if it actually got in or not here. |
121 | | * If it didn't get in, it will simply not be available when |
122 | | * ossl_method_construct() tries to get it from the store. |
123 | | * |
124 | | * It is *expected* that the put function increments the refcnt |
125 | | * of the passed method. |
126 | | */ |
127 | 278 | data->mcm->put(no_store ? data->store : NULL, method, provider, algo->algorithm_names, |
128 | 278 | algo->property_definition, data->mcm_data); |
129 | | |
130 | | /* refcnt-- because we're dropping the reference */ |
131 | 278 | data->mcm->destruct(method, data->mcm_data); |
132 | 278 | } |
133 | | |
134 | | void *ossl_method_construct(OSSL_LIB_CTX *libctx, int operation_id, |
135 | | OSSL_PROVIDER **provider_rw, int force_store, |
136 | | OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data) |
137 | 985 | { |
138 | 985 | void *method = NULL; |
139 | 985 | OSSL_PROVIDER *provider = provider_rw != NULL ? *provider_rw : NULL; |
140 | 985 | struct construct_data_st cbdata; |
141 | | |
142 | | /* |
143 | | * We might be tempted to try to look into the method store without |
144 | | * constructing to see if we can find our method there already. |
145 | | * Unfortunately that does not work well if the query contains |
146 | | * optional properties as newly loaded providers can match them better. |
147 | | * We trust that ossl_method_construct_precondition() and |
148 | | * ossl_method_construct_postcondition() make sure that the |
149 | | * ossl_algorithm_do_all() does very little when methods from |
150 | | * a provider have already been constructed. |
151 | | */ |
152 | | |
153 | 985 | cbdata.store = NULL; |
154 | 985 | cbdata.force_store = force_store; |
155 | 985 | cbdata.mcm = mcm; |
156 | 985 | cbdata.mcm_data = mcm_data; |
157 | 985 | ossl_algorithm_do_all(libctx, operation_id, provider, |
158 | 985 | ossl_method_construct_precondition, |
159 | 985 | ossl_method_construct_reserve_store, |
160 | 985 | ossl_method_construct_this, |
161 | 985 | ossl_method_construct_unreserve_store, |
162 | 985 | ossl_method_construct_postcondition, |
163 | 985 | &cbdata); |
164 | | |
165 | | /* If there is a temporary store, try there first */ |
166 | 985 | if (cbdata.store != NULL) |
167 | 0 | method = mcm->get(cbdata.store, (const OSSL_PROVIDER **)provider_rw, |
168 | 0 | mcm_data); |
169 | | |
170 | | /* If no method was found yet, try the global store */ |
171 | 985 | if (method == NULL) |
172 | 985 | method = mcm->get(NULL, (const OSSL_PROVIDER **)provider_rw, mcm_data); |
173 | | |
174 | 985 | return method; |
175 | 985 | } |