]>
Commit | Line | Data |
---|---|---|
4c2883a9 | 1 | /* |
4333b89f | 2 | * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. |
4c2883a9 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 | ||
2217d4c9 | 10 | #include <assert.h> |
4c2883a9 | 11 | #include <openssl/core.h> |
23c48d94 | 12 | #include <openssl/core_dispatch.h> |
25e60144 | 13 | #include <openssl/core_names.h> |
6bd4e3f2 | 14 | #include <openssl/provider.h> |
ac1055ef | 15 | #include <openssl/params.h> |
4c2883a9 | 16 | #include <openssl/opensslv.h> |
25f2138b | 17 | #include "crypto/cryptlib.h" |
04cb5ec0 | 18 | #include "crypto/evp.h" /* evp_method_store_flush */ |
03bede0c | 19 | #include "crypto/rand.h" |
c41f3ae0 | 20 | #include "internal/nelem.h" |
4c2883a9 RL |
21 | #include "internal/thread_once.h" |
22 | #include "internal/provider.h" | |
23 | #include "internal/refcount.h" | |
141cc94e | 24 | #include "internal/bio.h" |
f12a5690 | 25 | #include "internal/core.h" |
c41f3ae0 | 26 | #include "provider_local.h" |
f844f9eb | 27 | #ifndef FIPS_MODULE |
36fc5fc6 SL |
28 | # include <openssl/self_test.h> |
29 | #endif | |
c41f3ae0 RL |
30 | |
31 | static OSSL_PROVIDER *provider_new(const char *name, | |
32 | OSSL_provider_init_fn *init_function); | |
4c2883a9 RL |
33 | |
34 | /*- | |
35 | * Provider Object structure | |
36 | * ========================= | |
37 | */ | |
38 | ||
ac1055ef RL |
39 | typedef struct { |
40 | char *name; | |
41 | char *value; | |
42 | } INFOPAIR; | |
43 | DEFINE_STACK_OF(INFOPAIR) | |
44 | ||
c1fb5e07 | 45 | #ifndef FIPS_MODULE |
7b88c184 MC |
46 | typedef struct { |
47 | OSSL_PROVIDER *prov; | |
48 | int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata); | |
b1956770 MC |
49 | int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata); |
50 | int (*global_props_cb)(const char *props, void *cbdata); | |
7b88c184 MC |
51 | void *cbdata; |
52 | } OSSL_PROVIDER_CHILD_CB; | |
53 | DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB) | |
c1fb5e07 | 54 | #endif |
7b88c184 | 55 | |
4c2883a9 RL |
56 | struct provider_store_st; /* Forward declaration */ |
57 | ||
58 | struct ossl_provider_st { | |
59 | /* Flag bits */ | |
60 | unsigned int flag_initialized:1; | |
390f9bad | 61 | unsigned int flag_activated:1; |
c8567c39 | 62 | unsigned int flag_fallback:1; /* Can be used as fallback */ |
c1fb5e07 | 63 | #ifndef FIPS_MODULE |
abaa2dd2 | 64 | unsigned int flag_couldbechild:1; |
c1fb5e07 | 65 | #endif |
4c2883a9 | 66 | |
c2ec2bb7 RL |
67 | /* Getting and setting the flags require synchronization */ |
68 | CRYPTO_RWLOCK *flag_lock; | |
69 | ||
4c2883a9 RL |
70 | /* OpenSSL library side data */ |
71 | CRYPTO_REF_COUNT refcnt; | |
085bef9f | 72 | CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */ |
2d569501 | 73 | int activatecnt; |
4c2883a9 | 74 | char *name; |
ac1055ef | 75 | char *path; |
4c2883a9 RL |
76 | DSO *module; |
77 | OSSL_provider_init_fn *init_function; | |
ac1055ef | 78 | STACK_OF(INFOPAIR) *parameters; |
b4250010 | 79 | OSSL_LIB_CTX *libctx; /* The library context this instance is in */ |
e55008a9 | 80 | struct provider_store_st *store; /* The store this instance belongs to */ |
f844f9eb | 81 | #ifndef FIPS_MODULE |
6592ab81 RL |
82 | /* |
83 | * In the FIPS module inner provider, this isn't needed, since the | |
84 | * error upcalls are always direct calls to the outer provider. | |
85 | */ | |
6ebc2f56 | 86 | int error_lib; /* ERR library number, one for each provider */ |
6592ab81 | 87 | # ifndef OPENSSL_NO_ERR |
6ebc2f56 | 88 | ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */ |
6592ab81 | 89 | # endif |
6ebc2f56 | 90 | #endif |
4c2883a9 RL |
91 | |
92 | /* Provider side functions */ | |
363b1e5d DMSP |
93 | OSSL_FUNC_provider_teardown_fn *teardown; |
94 | OSSL_FUNC_provider_gettable_params_fn *gettable_params; | |
95 | OSSL_FUNC_provider_get_params_fn *get_params; | |
96 | OSSL_FUNC_provider_get_capabilities_fn *get_capabilities; | |
04cb5ec0 | 97 | OSSL_FUNC_provider_self_test_fn *self_test; |
363b1e5d | 98 | OSSL_FUNC_provider_query_operation_fn *query_operation; |
b0001d0c | 99 | OSSL_FUNC_provider_unquery_operation_fn *unquery_operation; |
a39eb840 | 100 | |
5a29b628 RL |
101 | /* |
102 | * Cache of bit to indicate of query_operation() has been called on | |
103 | * a specific operation or not. | |
104 | */ | |
105 | unsigned char *operation_bits; | |
106 | size_t operation_bits_sz; | |
c25a1524 | 107 | CRYPTO_RWLOCK *opbits_lock; |
5a29b628 | 108 | |
c1fb5e07 | 109 | #ifndef FIPS_MODULE |
f12a5690 | 110 | /* Whether this provider is the child of some other provider */ |
8c627075 | 111 | const OSSL_CORE_HANDLE *handle; |
f12a5690 | 112 | unsigned int ischild:1; |
c1fb5e07 | 113 | #endif |
f12a5690 | 114 | |
a39eb840 RL |
115 | /* Provider side data */ |
116 | void *provctx; | |
f12a5690 | 117 | const OSSL_DISPATCH *dispatch; |
4c2883a9 RL |
118 | }; |
119 | DEFINE_STACK_OF(OSSL_PROVIDER) | |
120 | ||
121 | static int ossl_provider_cmp(const OSSL_PROVIDER * const *a, | |
122 | const OSSL_PROVIDER * const *b) | |
123 | { | |
124 | return strcmp((*a)->name, (*b)->name); | |
125 | } | |
126 | ||
127 | /*- | |
128 | * Provider Object store | |
129 | * ===================== | |
130 | * | |
131 | * The Provider Object store is a library context object, and therefore needs | |
132 | * an index. | |
133 | */ | |
134 | ||
135 | struct provider_store_st { | |
8c627075 | 136 | OSSL_LIB_CTX *libctx; |
4c2883a9 | 137 | STACK_OF(OSSL_PROVIDER) *providers; |
7b88c184 | 138 | STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs; |
86522324 | 139 | CRYPTO_RWLOCK *default_path_lock; |
4c2883a9 | 140 | CRYPTO_RWLOCK *lock; |
6bd4e3f2 | 141 | char *default_path; |
e55008a9 | 142 | unsigned int use_fallbacks:1; |
0090e508 | 143 | unsigned int freeing:1; |
4c2883a9 | 144 | }; |
4c2883a9 | 145 | |
c8567c39 | 146 | /* |
390f9bad RL |
147 | * provider_deactivate_free() is a wrapper around ossl_provider_deactivate() |
148 | * and ossl_provider_free(), called as needed. | |
c8567c39 RL |
149 | * Since this is only called when the provider store is being emptied, we |
150 | * don't need to care about any lock. | |
151 | */ | |
152 | static void provider_deactivate_free(OSSL_PROVIDER *prov) | |
153 | { | |
390f9bad RL |
154 | if (prov->flag_activated) |
155 | ossl_provider_deactivate(prov); | |
c8567c39 RL |
156 | ossl_provider_free(prov); |
157 | } | |
158 | ||
c1fb5e07 | 159 | #ifndef FIPS_MODULE |
7b88c184 MC |
160 | static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb) |
161 | { | |
162 | OPENSSL_free(cb); | |
163 | } | |
c1fb5e07 | 164 | #endif |
7b88c184 | 165 | |
4c2883a9 RL |
166 | static void provider_store_free(void *vstore) |
167 | { | |
168 | struct provider_store_st *store = vstore; | |
169 | ||
170 | if (store == NULL) | |
171 | return; | |
0090e508 | 172 | store->freeing = 1; |
6bd4e3f2 | 173 | OPENSSL_free(store->default_path); |
c8567c39 | 174 | sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free); |
c1fb5e07 | 175 | #ifndef FIPS_MODULE |
7b88c184 MC |
176 | sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs, |
177 | ossl_provider_child_cb_free); | |
c1fb5e07 | 178 | #endif |
86522324 | 179 | CRYPTO_THREAD_lock_free(store->default_path_lock); |
4c2883a9 RL |
180 | CRYPTO_THREAD_lock_free(store->lock); |
181 | OPENSSL_free(store); | |
182 | } | |
183 | ||
b4250010 | 184 | static void *provider_store_new(OSSL_LIB_CTX *ctx) |
4c2883a9 RL |
185 | { |
186 | struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store)); | |
c41f3ae0 | 187 | const struct predefined_providers_st *p = NULL; |
4c2883a9 RL |
188 | |
189 | if (store == NULL | |
190 | || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL | |
86522324 | 191 | || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL |
c1fb5e07 | 192 | #ifndef FIPS_MODULE |
7b88c184 | 193 | || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL |
c1fb5e07 | 194 | #endif |
4c2883a9 RL |
195 | || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) { |
196 | provider_store_free(store); | |
e55008a9 | 197 | return NULL; |
4c2883a9 | 198 | } |
8c627075 | 199 | store->libctx = ctx; |
e55008a9 | 200 | store->use_fallbacks = 1; |
c41f3ae0 | 201 | |
c8830891 | 202 | for (p = ossl_predefined_providers; p->name != NULL; p++) { |
c41f3ae0 RL |
203 | OSSL_PROVIDER *prov = NULL; |
204 | ||
205 | /* | |
206 | * We use the internal constructor directly here, | |
207 | * otherwise we get a call loop | |
208 | */ | |
209 | prov = provider_new(p->name, p->init); | |
210 | ||
211 | if (prov == NULL | |
212 | || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) { | |
213 | ossl_provider_free(prov); | |
214 | provider_store_free(store); | |
9311d0c4 | 215 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
c41f3ae0 RL |
216 | return NULL; |
217 | } | |
e7706e63 | 218 | prov->libctx = ctx; |
c41f3ae0 | 219 | prov->store = store; |
f844f9eb | 220 | #ifndef FIPS_MODULE |
6ebc2f56 | 221 | prov->error_lib = ERR_get_next_error_library(); |
6592ab81 | 222 | #endif |
c41f3ae0 RL |
223 | if(p->is_fallback) |
224 | ossl_provider_set_fallback(prov); | |
225 | } | |
226 | ||
4c2883a9 RL |
227 | return store; |
228 | } | |
229 | ||
b4250010 | 230 | static const OSSL_LIB_CTX_METHOD provider_store_method = { |
8c627075 MC |
231 | /* Needs to be freed before the child provider data is freed */ |
232 | OSSL_LIB_CTX_METHOD_PRIORITY_1, | |
4c2883a9 RL |
233 | provider_store_new, |
234 | provider_store_free, | |
235 | }; | |
236 | ||
b4250010 | 237 | static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx) |
4c2883a9 RL |
238 | { |
239 | struct provider_store_st *store = NULL; | |
240 | ||
b4250010 DMSP |
241 | store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX, |
242 | &provider_store_method); | |
4c2883a9 | 243 | if (store == NULL) |
9311d0c4 | 244 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
4c2883a9 RL |
245 | return store; |
246 | } | |
247 | ||
b4250010 | 248 | int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx) |
ebe3f24b P |
249 | { |
250 | struct provider_store_st *store; | |
251 | ||
252 | if ((store = get_provider_store(libctx)) != NULL) { | |
cd3f8c1b RS |
253 | if (!CRYPTO_THREAD_write_lock(store->lock)) |
254 | return 0; | |
ebe3f24b | 255 | store->use_fallbacks = 0; |
d60a8e0a | 256 | CRYPTO_THREAD_unlock(store->lock); |
ebe3f24b P |
257 | return 1; |
258 | } | |
259 | return 0; | |
260 | } | |
261 | ||
b4250010 | 262 | OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name, |
29dc6e00 | 263 | int noconfig) |
4c2883a9 RL |
264 | { |
265 | struct provider_store_st *store = NULL; | |
266 | OSSL_PROVIDER *prov = NULL; | |
267 | ||
268 | if ((store = get_provider_store(libctx)) != NULL) { | |
269 | OSSL_PROVIDER tmpl = { 0, }; | |
270 | int i; | |
271 | ||
f844f9eb | 272 | #ifndef FIPS_MODULE |
29dc6e00 MC |
273 | /* |
274 | * Make sure any providers are loaded from config before we try to find | |
275 | * them. | |
276 | */ | |
f12a5690 MC |
277 | if (!noconfig) { |
278 | if (ossl_lib_ctx_is_default(libctx)) | |
279 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); | |
f12a5690 | 280 | } |
29dc6e00 MC |
281 | #endif |
282 | ||
4c2883a9 | 283 | tmpl.name = (char *)name; |
4ed1f0bc | 284 | if (!CRYPTO_THREAD_read_lock(store->lock)) |
cd3f8c1b | 285 | return NULL; |
4c2883a9 RL |
286 | if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1 |
287 | || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL | |
7c95390e | 288 | || !ossl_provider_up_ref(prov)) |
4c2883a9 RL |
289 | prov = NULL; |
290 | CRYPTO_THREAD_unlock(store->lock); | |
291 | } | |
292 | ||
293 | return prov; | |
294 | } | |
295 | ||
c41f3ae0 RL |
296 | /*- |
297 | * Provider Object methods | |
298 | * ======================= | |
299 | */ | |
300 | ||
301 | static OSSL_PROVIDER *provider_new(const char *name, | |
302 | OSSL_provider_init_fn *init_function) | |
303 | { | |
304 | OSSL_PROVIDER *prov = NULL; | |
305 | ||
306 | if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL | |
307 | #ifndef HAVE_ATOMICS | |
308 | || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL | |
309 | #endif | |
c25a1524 | 310 | || (prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL |
c2ec2bb7 | 311 | || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL |
c41f3ae0 RL |
312 | || (prov->name = OPENSSL_strdup(name)) == NULL) { |
313 | ossl_provider_free(prov); | |
9311d0c4 | 314 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
c41f3ae0 RL |
315 | return NULL; |
316 | } | |
317 | ||
634da876 | 318 | prov->refcnt = 1; /* 1 One reference to be returned */ |
c41f3ae0 | 319 | prov->init_function = init_function; |
c1fb5e07 | 320 | #ifndef FIPS_MODULE |
abaa2dd2 | 321 | prov->flag_couldbechild = 1; |
c1fb5e07 | 322 | #endif |
c41f3ae0 RL |
323 | return prov; |
324 | } | |
325 | ||
7c95390e | 326 | int ossl_provider_up_ref(OSSL_PROVIDER *prov) |
c41f3ae0 RL |
327 | { |
328 | int ref = 0; | |
329 | ||
ad14e8e5 SL |
330 | if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0) |
331 | return 0; | |
8c627075 MC |
332 | |
333 | #ifndef FIPS_MODULE | |
334 | if (prov->ischild) { | |
335 | if (!ossl_provider_up_ref_parent(prov, 0)) { | |
336 | ossl_provider_free(prov); | |
337 | return 0; | |
338 | } | |
339 | } | |
340 | #endif | |
341 | ||
c41f3ae0 RL |
342 | return ref; |
343 | } | |
344 | ||
8c627075 MC |
345 | #ifndef FIPS_MODULE |
346 | static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate) | |
347 | { | |
348 | if (activate) | |
349 | return ossl_provider_activate(prov, 0, 1); | |
350 | ||
351 | return ossl_provider_up_ref(prov); | |
352 | } | |
353 | ||
354 | static int provider_free_intern(OSSL_PROVIDER *prov, int deactivate) | |
355 | { | |
356 | if (deactivate) | |
357 | return ossl_provider_deactivate(prov); | |
358 | ||
359 | ossl_provider_free(prov); | |
360 | return 1; | |
361 | } | |
362 | #endif | |
363 | ||
b4250010 | 364 | OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name, |
29dc6e00 MC |
365 | OSSL_provider_init_fn *init_function, |
366 | int noconfig) | |
4c2883a9 RL |
367 | { |
368 | struct provider_store_st *store = NULL; | |
369 | OSSL_PROVIDER *prov = NULL; | |
370 | ||
371 | if ((store = get_provider_store(libctx)) == NULL) | |
372 | return NULL; | |
373 | ||
29dc6e00 MC |
374 | if ((prov = ossl_provider_find(libctx, name, |
375 | noconfig)) != NULL) { /* refcount +1 */ | |
4c2883a9 | 376 | ossl_provider_free(prov); /* refcount -1 */ |
105d01f1 | 377 | ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS, |
49c64346 | 378 | "name=%s", name); |
4c2883a9 RL |
379 | return NULL; |
380 | } | |
381 | ||
c41f3ae0 RL |
382 | /* provider_new() generates an error, so no need here */ |
383 | if ((prov = provider_new(name, init_function)) == NULL) | |
4c2883a9 | 384 | return NULL; |
4c2883a9 | 385 | |
cd3f8c1b RS |
386 | if (!CRYPTO_THREAD_write_lock(store->lock)) |
387 | return NULL; | |
7c95390e | 388 | if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */ |
4c2883a9 RL |
389 | ossl_provider_free(prov); /* -1 Reference that was to be returned */ |
390 | prov = NULL; | |
391 | } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) { | |
392 | ossl_provider_free(prov); /* -1 Store reference */ | |
393 | ossl_provider_free(prov); /* -1 Reference that was to be returned */ | |
394 | prov = NULL; | |
e55008a9 | 395 | } else { |
e7706e63 | 396 | prov->libctx = libctx; |
e55008a9 | 397 | prov->store = store; |
f844f9eb | 398 | #ifndef FIPS_MODULE |
6ebc2f56 | 399 | prov->error_lib = ERR_get_next_error_library(); |
6592ab81 | 400 | #endif |
4c2883a9 RL |
401 | } |
402 | CRYPTO_THREAD_unlock(store->lock); | |
403 | ||
404 | if (prov == NULL) | |
9311d0c4 | 405 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
4c2883a9 RL |
406 | |
407 | /* | |
408 | * At this point, the provider is only partially "loaded". To be | |
409 | * fully "loaded", ossl_provider_activate() must also be called. | |
410 | */ | |
411 | ||
412 | return prov; | |
413 | } | |
414 | ||
ac1055ef RL |
415 | static void free_infopair(INFOPAIR *pair) |
416 | { | |
417 | OPENSSL_free(pair->name); | |
418 | OPENSSL_free(pair->value); | |
419 | OPENSSL_free(pair); | |
420 | } | |
421 | ||
4c2883a9 RL |
422 | void ossl_provider_free(OSSL_PROVIDER *prov) |
423 | { | |
424 | if (prov != NULL) { | |
425 | int ref = 0; | |
426 | ||
085bef9f | 427 | CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock); |
4c2883a9 RL |
428 | |
429 | /* | |
390f9bad RL |
430 | * When the refcount drops to zero, we clean up the provider. |
431 | * Note that this also does teardown, which may seem late, | |
432 | * considering that init happens on first activation. However, | |
433 | * there may be other structures hanging on to the provider after | |
434 | * the last deactivation and may therefore need full access to the | |
435 | * provider's services. Therefore, we deinit late. | |
4c2883a9 | 436 | */ |
390f9bad RL |
437 | if (ref == 0) { |
438 | if (prov->flag_initialized) { | |
f12a5690 | 439 | ossl_provider_teardown(prov); |
6ebc2f56 | 440 | #ifndef OPENSSL_NO_ERR |
f844f9eb | 441 | # ifndef FIPS_MODULE |
390f9bad RL |
442 | if (prov->error_strings != NULL) { |
443 | ERR_unload_strings(prov->error_lib, prov->error_strings); | |
444 | OPENSSL_free(prov->error_strings); | |
445 | prov->error_strings = NULL; | |
446 | } | |
6ebc2f56 RL |
447 | # endif |
448 | #endif | |
390f9bad RL |
449 | OPENSSL_free(prov->operation_bits); |
450 | prov->operation_bits = NULL; | |
451 | prov->operation_bits_sz = 0; | |
452 | prov->flag_initialized = 0; | |
453 | } | |
4c2883a9 | 454 | |
f844f9eb | 455 | #ifndef FIPS_MODULE |
ee067bc0 MC |
456 | /* |
457 | * We deregister thread handling whether or not the provider was | |
458 | * initialized. If init was attempted but was not successful then | |
459 | * the provider may still have registered a thread handler. | |
460 | */ | |
461 | ossl_init_thread_deregister(prov); | |
4c2883a9 | 462 | DSO_free(prov->module); |
3593266d | 463 | #endif |
4c2883a9 | 464 | OPENSSL_free(prov->name); |
ac1055ef RL |
465 | OPENSSL_free(prov->path); |
466 | sk_INFOPAIR_pop_free(prov->parameters, free_infopair); | |
c25a1524 | 467 | CRYPTO_THREAD_lock_free(prov->opbits_lock); |
c2ec2bb7 | 468 | CRYPTO_THREAD_lock_free(prov->flag_lock); |
085bef9f RL |
469 | #ifndef HAVE_ATOMICS |
470 | CRYPTO_THREAD_lock_free(prov->refcnt_lock); | |
471 | #endif | |
4c2883a9 RL |
472 | OPENSSL_free(prov); |
473 | } | |
8c627075 MC |
474 | #ifndef FIPS_MODULE |
475 | else if (prov->ischild) { | |
476 | ossl_provider_free_parent(prov, 0); | |
477 | } | |
478 | #endif | |
4c2883a9 RL |
479 | } |
480 | } | |
481 | ||
ac1055ef RL |
482 | /* Setters */ |
483 | int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path) | |
484 | { | |
485 | OPENSSL_free(prov->path); | |
486 | if (module_path == NULL) | |
487 | return 1; | |
488 | if ((prov->path = OPENSSL_strdup(module_path)) != NULL) | |
489 | return 1; | |
9311d0c4 | 490 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
ac1055ef RL |
491 | return 0; |
492 | } | |
493 | ||
494 | int ossl_provider_add_parameter(OSSL_PROVIDER *prov, | |
495 | const char *name, const char *value) | |
496 | { | |
497 | INFOPAIR *pair = NULL; | |
498 | ||
499 | if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL | |
500 | && (prov->parameters != NULL | |
501 | || (prov->parameters = sk_INFOPAIR_new_null()) != NULL) | |
502 | && (pair->name = OPENSSL_strdup(name)) != NULL | |
503 | && (pair->value = OPENSSL_strdup(value)) != NULL | |
504 | && sk_INFOPAIR_push(prov->parameters, pair) > 0) | |
505 | return 1; | |
506 | ||
507 | if (pair != NULL) { | |
508 | OPENSSL_free(pair->name); | |
509 | OPENSSL_free(pair->value); | |
510 | OPENSSL_free(pair); | |
511 | } | |
9311d0c4 | 512 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
ac1055ef RL |
513 | return 0; |
514 | } | |
515 | ||
4c2883a9 RL |
516 | /* |
517 | * Provider activation. | |
518 | * | |
519 | * What "activation" means depends on the provider form; for built in | |
520 | * providers (in the library or the application alike), the provider | |
521 | * can already be considered to be loaded, all that's needed is to | |
522 | * initialize it. However, for dynamically loadable provider modules, | |
523 | * we must first load that module. | |
524 | * | |
525 | * Built in modules are distinguished from dynamically loaded modules | |
526 | * with an already assigned init function. | |
527 | */ | |
528 | static const OSSL_DISPATCH *core_dispatch; /* Define further down */ | |
529 | ||
b4250010 DMSP |
530 | int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx, |
531 | const char *path) | |
6bd4e3f2 P |
532 | { |
533 | struct provider_store_st *store; | |
534 | char *p = NULL; | |
535 | ||
536 | if (path != NULL) { | |
537 | p = OPENSSL_strdup(path); | |
538 | if (p == NULL) { | |
9311d0c4 | 539 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
6bd4e3f2 P |
540 | return 0; |
541 | } | |
542 | } | |
543 | if ((store = get_provider_store(libctx)) != NULL | |
86522324 | 544 | && CRYPTO_THREAD_write_lock(store->default_path_lock)) { |
6bd4e3f2 P |
545 | OPENSSL_free(store->default_path); |
546 | store->default_path = p; | |
86522324 | 547 | CRYPTO_THREAD_unlock(store->default_path_lock); |
6bd4e3f2 P |
548 | return 1; |
549 | } | |
550 | OPENSSL_free(p); | |
551 | return 0; | |
552 | } | |
553 | ||
e55008a9 RL |
554 | /* |
555 | * Internal version that doesn't affect the store flags, and thereby avoid | |
556 | * locking. Direct callers must remember to set the store flags when | |
e7706e63 | 557 | * appropriate. |
e55008a9 | 558 | */ |
2d569501 | 559 | static int provider_init(OSSL_PROVIDER *prov, int flag_lock) |
4c2883a9 RL |
560 | { |
561 | const OSSL_DISPATCH *provider_dispatch = NULL; | |
914db66d | 562 | void *tmp_provctx = NULL; /* safety measure */ |
6ebc2f56 | 563 | #ifndef OPENSSL_NO_ERR |
f844f9eb | 564 | # ifndef FIPS_MODULE |
363b1e5d | 565 | OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL; |
6592ab81 | 566 | # endif |
6ebc2f56 | 567 | #endif |
c2ec2bb7 | 568 | int ok = 0; |
4c2883a9 | 569 | |
c2ec2bb7 RL |
570 | /* |
571 | * The flag lock is used to lock init, not only because the flag is | |
572 | * checked here and set at the end, but also because this function | |
573 | * modifies a number of things in the provider structure that this | |
574 | * function needs to perform under lock anyway. | |
575 | */ | |
2d569501 | 576 | if (flag_lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) |
cd3f8c1b | 577 | goto end; |
c2ec2bb7 RL |
578 | if (prov->flag_initialized) { |
579 | ok = 1; | |
580 | goto end; | |
581 | } | |
4c2883a9 RL |
582 | |
583 | /* | |
584 | * If the init function isn't set, it indicates that this provider is | |
585 | * a loadable module. | |
586 | */ | |
587 | if (prov->init_function == NULL) { | |
f844f9eb | 588 | #ifdef FIPS_MODULE |
c2ec2bb7 | 589 | goto end; |
3593266d | 590 | #else |
4c2883a9 | 591 | if (prov->module == NULL) { |
ac1055ef RL |
592 | char *allocated_path = NULL; |
593 | const char *module_path = NULL; | |
594 | char *merged_path = NULL; | |
6bd4e3f2 | 595 | const char *load_dir = NULL; |
86522324 | 596 | char *allocated_load_dir = NULL; |
6bd4e3f2 | 597 | struct provider_store_st *store; |
4c2883a9 RL |
598 | |
599 | if ((prov->module = DSO_new()) == NULL) { | |
600 | /* DSO_new() generates an error already */ | |
c2ec2bb7 | 601 | goto end; |
4c2883a9 RL |
602 | } |
603 | ||
6bd4e3f2 | 604 | if ((store = get_provider_store(prov->libctx)) == NULL |
86522324 | 605 | || !CRYPTO_THREAD_read_lock(store->default_path_lock)) |
c2ec2bb7 | 606 | goto end; |
86522324 SP |
607 | |
608 | if (store->default_path != NULL) { | |
609 | allocated_load_dir = OPENSSL_strdup(store->default_path); | |
610 | CRYPTO_THREAD_unlock(store->default_path_lock); | |
611 | if (allocated_load_dir == NULL) { | |
612 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); | |
613 | goto end; | |
614 | } | |
615 | load_dir = allocated_load_dir; | |
616 | } else { | |
617 | CRYPTO_THREAD_unlock(store->default_path_lock); | |
618 | } | |
6bd4e3f2 P |
619 | |
620 | if (load_dir == NULL) { | |
621 | load_dir = ossl_safe_getenv("OPENSSL_MODULES"); | |
622 | if (load_dir == NULL) | |
623 | load_dir = MODULESDIR; | |
624 | } | |
4c2883a9 RL |
625 | |
626 | DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS, | |
627 | DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL); | |
ac1055ef RL |
628 | |
629 | module_path = prov->path; | |
630 | if (module_path == NULL) | |
631 | module_path = allocated_path = | |
632 | DSO_convert_filename(prov->module, prov->name); | |
633 | if (module_path != NULL) | |
634 | merged_path = DSO_merge(prov->module, module_path, load_dir); | |
635 | ||
636 | if (merged_path == NULL | |
637 | || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) { | |
4c2883a9 RL |
638 | DSO_free(prov->module); |
639 | prov->module = NULL; | |
640 | } | |
641 | ||
ac1055ef RL |
642 | OPENSSL_free(merged_path); |
643 | OPENSSL_free(allocated_path); | |
86522324 | 644 | OPENSSL_free(allocated_load_dir); |
4c2883a9 RL |
645 | } |
646 | ||
647 | if (prov->module != NULL) | |
648 | prov->init_function = (OSSL_provider_init_fn *) | |
649 | DSO_bind_func(prov->module, "OSSL_provider_init"); | |
3593266d | 650 | #endif |
4c2883a9 RL |
651 | } |
652 | ||
e7706e63 | 653 | /* Call the initialise function for the provider. */ |
4c2883a9 | 654 | if (prov->init_function == NULL |
d40b42ab MC |
655 | || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch, |
656 | &provider_dispatch, &tmp_provctx)) { | |
105d01f1 | 657 | ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL, |
49c64346 | 658 | "name=%s", prov->name); |
c2ec2bb7 | 659 | goto end; |
4c2883a9 | 660 | } |
914db66d | 661 | prov->provctx = tmp_provctx; |
f12a5690 | 662 | prov->dispatch = provider_dispatch; |
c1fb5e07 | 663 | #ifndef FIPS_MODULE |
abaa2dd2 | 664 | prov->flag_couldbechild = 0; |
c1fb5e07 | 665 | #endif |
4c2883a9 RL |
666 | |
667 | for (; provider_dispatch->function_id != 0; provider_dispatch++) { | |
668 | switch (provider_dispatch->function_id) { | |
669 | case OSSL_FUNC_PROVIDER_TEARDOWN: | |
670 | prov->teardown = | |
363b1e5d | 671 | OSSL_FUNC_provider_teardown(provider_dispatch); |
4c2883a9 | 672 | break; |
dca97d00 RL |
673 | case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS: |
674 | prov->gettable_params = | |
363b1e5d | 675 | OSSL_FUNC_provider_gettable_params(provider_dispatch); |
4c2883a9 RL |
676 | break; |
677 | case OSSL_FUNC_PROVIDER_GET_PARAMS: | |
678 | prov->get_params = | |
363b1e5d | 679 | OSSL_FUNC_provider_get_params(provider_dispatch); |
4c2883a9 | 680 | break; |
04cb5ec0 SL |
681 | case OSSL_FUNC_PROVIDER_SELF_TEST: |
682 | prov->self_test = | |
683 | OSSL_FUNC_provider_self_test(provider_dispatch); | |
684 | break; | |
82ec09ec MC |
685 | case OSSL_FUNC_PROVIDER_GET_CAPABILITIES: |
686 | prov->get_capabilities = | |
363b1e5d | 687 | OSSL_FUNC_provider_get_capabilities(provider_dispatch); |
82ec09ec | 688 | break; |
099bd339 RL |
689 | case OSSL_FUNC_PROVIDER_QUERY_OPERATION: |
690 | prov->query_operation = | |
363b1e5d | 691 | OSSL_FUNC_provider_query_operation(provider_dispatch); |
099bd339 | 692 | break; |
b0001d0c P |
693 | case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION: |
694 | prov->unquery_operation = | |
695 | OSSL_FUNC_provider_unquery_operation(provider_dispatch); | |
696 | break; | |
6ebc2f56 | 697 | #ifndef OPENSSL_NO_ERR |
f844f9eb | 698 | # ifndef FIPS_MODULE |
6ebc2f56 RL |
699 | case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS: |
700 | p_get_reason_strings = | |
363b1e5d | 701 | OSSL_FUNC_provider_get_reason_strings(provider_dispatch); |
6ebc2f56 | 702 | break; |
6592ab81 | 703 | # endif |
6ebc2f56 RL |
704 | #endif |
705 | } | |
706 | } | |
707 | ||
708 | #ifndef OPENSSL_NO_ERR | |
f844f9eb | 709 | # ifndef FIPS_MODULE |
6ebc2f56 RL |
710 | if (p_get_reason_strings != NULL) { |
711 | const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx); | |
712 | size_t cnt, cnt2; | |
713 | ||
714 | /* | |
715 | * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM, | |
716 | * although they are essentially the same type. | |
717 | * Furthermore, ERR_load_strings() patches the array's error number | |
718 | * with the error library number, so we need to make a copy of that | |
719 | * array either way. | |
720 | */ | |
fd03868b | 721 | cnt = 0; |
6ebc2f56 RL |
722 | while (reasonstrings[cnt].id != 0) { |
723 | if (ERR_GET_LIB(reasonstrings[cnt].id) != 0) | |
c2ec2bb7 | 724 | goto end; |
6ebc2f56 RL |
725 | cnt++; |
726 | } | |
fd03868b | 727 | cnt++; /* One for the terminating item */ |
6ebc2f56 RL |
728 | |
729 | /* Allocate one extra item for the "library" name */ | |
730 | prov->error_strings = | |
731 | OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1)); | |
732 | if (prov->error_strings == NULL) | |
c2ec2bb7 | 733 | goto end; |
6ebc2f56 RL |
734 | |
735 | /* | |
736 | * Set the "library" name. | |
737 | */ | |
738 | prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0); | |
739 | prov->error_strings[0].string = prov->name; | |
740 | /* | |
741 | * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions | |
742 | * 1..cnt. | |
743 | */ | |
744 | for (cnt2 = 1; cnt2 <= cnt; cnt2++) { | |
745 | prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id; | |
746 | prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr; | |
4c2883a9 | 747 | } |
6ebc2f56 RL |
748 | |
749 | ERR_load_strings(prov->error_lib, prov->error_strings); | |
4c2883a9 | 750 | } |
6592ab81 | 751 | # endif |
6ebc2f56 | 752 | #endif |
4c2883a9 RL |
753 | |
754 | /* With this flag set, this provider has become fully "loaded". */ | |
755 | prov->flag_initialized = 1; | |
c2ec2bb7 RL |
756 | ok = 1; |
757 | ||
758 | end: | |
2d569501 MC |
759 | if (flag_lock) |
760 | CRYPTO_THREAD_unlock(prov->flag_lock); | |
c2ec2bb7 | 761 | return ok; |
4c2883a9 RL |
762 | } |
763 | ||
0090e508 P |
764 | /* |
765 | * Deactivate a provider. | |
766 | * Return -1 on failure and the activation count on success | |
767 | */ | |
390f9bad RL |
768 | static int provider_deactivate(OSSL_PROVIDER *prov) |
769 | { | |
0090e508 | 770 | int count; |
7b88c184 | 771 | struct provider_store_st *store; |
0090e508 | 772 | |
390f9bad | 773 | if (!ossl_assert(prov != NULL)) |
0090e508 | 774 | return -1; |
390f9bad | 775 | |
7b88c184 MC |
776 | store = get_provider_store(prov->libctx); |
777 | if (store == NULL) | |
c1fb5e07 | 778 | return -1; |
7b88c184 MC |
779 | |
780 | if (!CRYPTO_THREAD_read_lock(store->lock)) | |
0090e508 | 781 | return -1; |
c1fb5e07 MC |
782 | if (!CRYPTO_THREAD_write_lock(prov->flag_lock)) { |
783 | CRYPTO_THREAD_unlock(store->lock); | |
784 | return -1; | |
785 | } | |
390f9bad | 786 | |
8c627075 MC |
787 | #ifndef FIPS_MODULE |
788 | if (prov->activatecnt == 2 && prov->ischild) { | |
789 | /* | |
790 | * We have had a direct activation in this child libctx so we need to | |
791 | * now down the ref count in the parent provider. | |
792 | */ | |
793 | ossl_provider_free_parent(prov, 1); | |
794 | } | |
795 | #endif | |
796 | ||
7b88c184 | 797 | if ((count = --prov->activatecnt) < 1) { |
390f9bad | 798 | prov->flag_activated = 0; |
c1fb5e07 MC |
799 | #ifndef FIPS_MODULE |
800 | { | |
801 | int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs); | |
802 | OSSL_PROVIDER_CHILD_CB *child_cb; | |
803 | ||
804 | for (i = 0; i < max; i++) { | |
805 | child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i); | |
806 | child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata); | |
807 | } | |
7b88c184 | 808 | } |
c1fb5e07 | 809 | #endif |
7b88c184 | 810 | } |
2d569501 MC |
811 | |
812 | CRYPTO_THREAD_unlock(prov->flag_lock); | |
7b88c184 | 813 | CRYPTO_THREAD_unlock(store->lock); |
390f9bad RL |
814 | |
815 | /* We don't deinit here, that's done in ossl_provider_free() */ | |
0090e508 | 816 | return count; |
390f9bad RL |
817 | } |
818 | ||
0090e508 P |
819 | /* |
820 | * Activate a provider. | |
821 | * Return -1 on failure and the activation count on success | |
822 | */ | |
8c627075 | 823 | static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls) |
390f9bad | 824 | { |
7b88c184 | 825 | int count = -1; |
0090e508 | 826 | |
7b88c184 | 827 | if (provider_init(prov, lock)) { |
8c627075 | 828 | int ret = 1; |
7b88c184 MC |
829 | struct provider_store_st *store; |
830 | ||
831 | store = get_provider_store(prov->libctx); | |
832 | if (store == NULL) | |
c1fb5e07 | 833 | return -1; |
390f9bad | 834 | |
7b88c184 | 835 | if (lock && !CRYPTO_THREAD_read_lock(store->lock)) |
c1fb5e07 | 836 | return -1; |
7b88c184 | 837 | |
c1fb5e07 MC |
838 | if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) { |
839 | CRYPTO_THREAD_unlock(store->lock); | |
840 | return -1; | |
841 | } | |
8c627075 MC |
842 | |
843 | #ifndef FIPS_MODULE | |
844 | if (prov->ischild && upcalls) | |
845 | ret = ossl_provider_up_ref_parent(prov, 1); | |
846 | #endif | |
847 | ||
7b88c184 MC |
848 | if (ret) { |
849 | count = ++prov->activatecnt; | |
850 | prov->flag_activated = 1; | |
851 | ||
c1fb5e07 | 852 | #ifndef FIPS_MODULE |
8c627075 | 853 | if (prov->activatecnt == 1) { |
c1fb5e07 MC |
854 | OSSL_PROVIDER_CHILD_CB *child_cb; |
855 | int i, max; | |
856 | ||
857 | max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs); | |
8c627075 MC |
858 | for (i = 0; i < max; i++) { |
859 | /* | |
860 | * This is newly activated (activatecnt == 1), so we need to | |
861 | * create child providers as necessary. | |
862 | */ | |
863 | child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, | |
864 | i); | |
865 | ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov, | |
866 | child_cb->cbdata); | |
867 | } | |
7b88c184 | 868 | } |
c1fb5e07 | 869 | #endif |
7b88c184 | 870 | } |
8c627075 | 871 | |
7b88c184 MC |
872 | if (lock) { |
873 | CRYPTO_THREAD_unlock(prov->flag_lock); | |
874 | CRYPTO_THREAD_unlock(store->lock); | |
875 | } | |
8c627075 MC |
876 | if (!ret) |
877 | return -1; | |
390f9bad RL |
878 | } |
879 | ||
7b88c184 | 880 | return count; |
0090e508 P |
881 | } |
882 | ||
883 | static int provider_flush_store_cache(const OSSL_PROVIDER *prov) | |
884 | { | |
885 | struct provider_store_st *store; | |
886 | int freeing; | |
887 | ||
888 | if ((store = get_provider_store(prov->libctx)) == NULL) | |
889 | return 0; | |
890 | ||
891 | if (!CRYPTO_THREAD_read_lock(store->lock)) | |
892 | return 0; | |
893 | freeing = store->freeing; | |
894 | CRYPTO_THREAD_unlock(store->lock); | |
895 | ||
896 | if (!freeing) | |
897 | return evp_method_store_flush(prov->libctx); | |
898 | return 1; | |
390f9bad RL |
899 | } |
900 | ||
8c627075 MC |
901 | int ossl_provider_activate(OSSL_PROVIDER *prov, int retain_fallbacks, |
902 | int upcalls) | |
e55008a9 | 903 | { |
0090e508 P |
904 | int count; |
905 | ||
390f9bad RL |
906 | if (prov == NULL) |
907 | return 0; | |
8c627075 | 908 | if ((count = provider_activate(prov, 1, upcalls)) > 0) { |
299f5ff3 | 909 | if (!retain_fallbacks) { |
cd3f8c1b RS |
910 | if (!CRYPTO_THREAD_write_lock(prov->store->lock)) { |
911 | provider_deactivate(prov); | |
912 | return 0; | |
913 | } | |
299f5ff3 P |
914 | prov->store->use_fallbacks = 0; |
915 | CRYPTO_THREAD_unlock(prov->store->lock); | |
916 | } | |
0090e508 | 917 | return count == 1 ? provider_flush_store_cache(prov) : 1; |
e55008a9 | 918 | } |
e55008a9 RL |
919 | return 0; |
920 | } | |
921 | ||
390f9bad RL |
922 | int ossl_provider_deactivate(OSSL_PROVIDER *prov) |
923 | { | |
0090e508 P |
924 | int count; |
925 | ||
926 | if (prov == NULL || (count = provider_deactivate(prov)) < 0) | |
390f9bad | 927 | return 0; |
0090e508 | 928 | return count == 0 ? provider_flush_store_cache(prov) : 1; |
390f9bad RL |
929 | } |
930 | ||
a39eb840 RL |
931 | void *ossl_provider_ctx(const OSSL_PROVIDER *prov) |
932 | { | |
933 | return prov->provctx; | |
934 | } | |
935 | ||
36f5ec55 RL |
936 | /* |
937 | * This function only does something once when store->use_fallbacks == 1, | |
938 | * and then sets store->use_fallbacks = 0, so the second call and so on is | |
939 | * effectively a no-op. | |
940 | */ | |
941 | static void provider_activate_fallbacks(struct provider_store_st *store) | |
942 | { | |
7dd2cb56 MC |
943 | int use_fallbacks; |
944 | int num_provs; | |
945 | int activated_fallback_count = 0; | |
946 | int i; | |
947 | ||
cd3f8c1b RS |
948 | if (!CRYPTO_THREAD_read_lock(store->lock)) |
949 | return; | |
7dd2cb56 MC |
950 | use_fallbacks = store->use_fallbacks; |
951 | CRYPTO_THREAD_unlock(store->lock); | |
952 | if (!use_fallbacks) | |
953 | return; | |
954 | ||
cd3f8c1b RS |
955 | if (!CRYPTO_THREAD_write_lock(store->lock)) |
956 | return; | |
7dd2cb56 MC |
957 | /* Check again, just in case another thread changed it */ |
958 | use_fallbacks = store->use_fallbacks; | |
959 | if (!use_fallbacks) { | |
960 | CRYPTO_THREAD_unlock(store->lock); | |
961 | return; | |
962 | } | |
36f5ec55 | 963 | |
7dd2cb56 MC |
964 | num_provs = sk_OSSL_PROVIDER_num(store->providers); |
965 | for (i = 0; i < num_provs; i++) { | |
966 | OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i); | |
36f5ec55 | 967 | |
7dd2cb56 | 968 | if (ossl_provider_up_ref(prov)) { |
7b88c184 MC |
969 | if (CRYPTO_THREAD_write_lock(prov->flag_lock)) { |
970 | if (prov->flag_fallback) { | |
8c627075 | 971 | if (provider_activate(prov, 0, 0) > 0) |
7b88c184 MC |
972 | activated_fallback_count++; |
973 | } | |
974 | CRYPTO_THREAD_unlock(prov->flag_lock); | |
36f5ec55 | 975 | } |
7dd2cb56 | 976 | ossl_provider_free(prov); |
36f5ec55 | 977 | } |
36f5ec55 | 978 | } |
7dd2cb56 MC |
979 | |
980 | /* | |
981 | * We assume that all fallbacks have been added to the store before | |
982 | * any fallback is activated. | |
983 | * TODO: We may have to reconsider this, IF we find ourselves adding | |
984 | * fallbacks after any previous fallback has been activated. | |
985 | */ | |
986 | if (activated_fallback_count > 0) | |
987 | store->use_fallbacks = 0; | |
988 | ||
989 | CRYPTO_THREAD_unlock(store->lock); | |
36f5ec55 RL |
990 | } |
991 | ||
8f089576 P |
992 | int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx, |
993 | int (*cb)(OSSL_PROVIDER *provider, | |
994 | void *cbdata), | |
995 | void *cbdata) | |
85e2417c | 996 | { |
2d569501 | 997 | int ret = 0, curr, max; |
85e2417c | 998 | struct provider_store_st *store = get_provider_store(ctx); |
7bbfbc82 | 999 | STACK_OF(OSSL_PROVIDER) *provs = NULL; |
85e2417c | 1000 | |
f844f9eb | 1001 | #ifndef FIPS_MODULE |
5c5cdcd8 MC |
1002 | /* |
1003 | * Make sure any providers are loaded from config before we try to use | |
1004 | * them. | |
1005 | */ | |
d07af736 MC |
1006 | if (ossl_lib_ctx_is_default(ctx)) |
1007 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); | |
5c5cdcd8 MC |
1008 | #endif |
1009 | ||
7bbfbc82 P |
1010 | if (store == NULL) |
1011 | return 1; | |
1012 | provider_activate_fallbacks(store); | |
e55008a9 | 1013 | |
7bbfbc82 P |
1014 | /* |
1015 | * Under lock, grab a copy of the provider list and up_ref each | |
1016 | * provider so that they don't disappear underneath us. | |
1017 | */ | |
cd3f8c1b RS |
1018 | if (!CRYPTO_THREAD_read_lock(store->lock)) |
1019 | return 0; | |
7bbfbc82 P |
1020 | provs = sk_OSSL_PROVIDER_dup(store->providers); |
1021 | if (provs == NULL) { | |
85e2417c | 1022 | CRYPTO_THREAD_unlock(store->lock); |
7bbfbc82 | 1023 | return 0; |
85e2417c | 1024 | } |
2d569501 MC |
1025 | max = sk_OSSL_PROVIDER_num(provs); |
1026 | /* | |
1027 | * We work backwards through the stack so that we can safely delete items | |
1028 | * as we go. | |
1029 | */ | |
1030 | for (curr = max - 1; curr >= 0; curr--) { | |
1031 | OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr); | |
1032 | ||
1033 | if (!CRYPTO_THREAD_write_lock(prov->flag_lock)) | |
7bbfbc82 | 1034 | goto err_unlock; |
2d569501 MC |
1035 | if (prov->flag_activated) { |
1036 | if (!ossl_provider_up_ref(prov)){ | |
1037 | CRYPTO_THREAD_unlock(prov->flag_lock); | |
1038 | goto err_unlock; | |
1039 | } | |
1040 | /* | |
1041 | * It's already activated, but we up the activated count to ensure | |
1042 | * it remains activated until after we've called the user callback. | |
1043 | */ | |
8c627075 | 1044 | if (provider_activate(prov, 0, 1) < 0) { |
2d569501 MC |
1045 | ossl_provider_free(prov); |
1046 | CRYPTO_THREAD_unlock(prov->flag_lock); | |
1047 | goto err_unlock; | |
1048 | } | |
1049 | } else { | |
1050 | sk_OSSL_PROVIDER_delete(provs, curr); | |
1051 | max--; | |
1052 | } | |
1053 | CRYPTO_THREAD_unlock(prov->flag_lock); | |
1054 | } | |
7bbfbc82 | 1055 | CRYPTO_THREAD_unlock(store->lock); |
85e2417c | 1056 | |
7bbfbc82 P |
1057 | /* |
1058 | * Now, we sweep through all providers not under lock | |
1059 | */ | |
2d569501 MC |
1060 | for (curr = 0; curr < max; curr++) { |
1061 | OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr); | |
7bbfbc82 | 1062 | |
2d569501 | 1063 | if (!cb(prov, cbdata)) |
7bbfbc82 P |
1064 | goto finish; |
1065 | } | |
2d569501 | 1066 | curr = -1; |
7bbfbc82 P |
1067 | |
1068 | ret = 1; | |
1069 | goto finish; | |
1070 | ||
1071 | err_unlock: | |
1072 | CRYPTO_THREAD_unlock(store->lock); | |
1073 | finish: | |
2d569501 MC |
1074 | /* |
1075 | * The pop_free call doesn't do what we want on an error condition. We | |
1076 | * either start from the first item in the stack, or part way through if | |
1077 | * we only processed some of the items. | |
1078 | */ | |
1079 | for (curr++; curr < max; curr++) { | |
1080 | OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr); | |
1081 | ||
1082 | provider_deactivate(prov); | |
1083 | ossl_provider_free(prov); | |
1084 | } | |
7bbfbc82 | 1085 | sk_OSSL_PROVIDER_free(provs); |
85e2417c RL |
1086 | return ret; |
1087 | } | |
1088 | ||
36f5ec55 RL |
1089 | int ossl_provider_available(OSSL_PROVIDER *prov) |
1090 | { | |
2d569501 MC |
1091 | int ret; |
1092 | ||
36f5ec55 | 1093 | if (prov != NULL) { |
36f5ec55 | 1094 | provider_activate_fallbacks(prov->store); |
36f5ec55 | 1095 | |
2d569501 MC |
1096 | if (!CRYPTO_THREAD_read_lock(prov->flag_lock)) |
1097 | return 0; | |
1098 | ret = prov->flag_activated; | |
1099 | CRYPTO_THREAD_unlock(prov->flag_lock); | |
1100 | return ret; | |
36f5ec55 RL |
1101 | } |
1102 | return 0; | |
1103 | } | |
1104 | ||
e55008a9 RL |
1105 | /* Setters of Provider Object data */ |
1106 | int ossl_provider_set_fallback(OSSL_PROVIDER *prov) | |
1107 | { | |
1108 | if (prov == NULL) | |
1109 | return 0; | |
1110 | ||
1111 | prov->flag_fallback = 1; | |
1112 | return 1; | |
1113 | } | |
1114 | ||
4c2883a9 | 1115 | /* Getters of Provider Object data */ |
24626a47 | 1116 | const char *ossl_provider_name(const OSSL_PROVIDER *prov) |
4c2883a9 RL |
1117 | { |
1118 | return prov->name; | |
1119 | } | |
1120 | ||
24626a47 | 1121 | const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov) |
4c2883a9 RL |
1122 | { |
1123 | return prov->module; | |
1124 | } | |
1125 | ||
24626a47 | 1126 | const char *ossl_provider_module_name(const OSSL_PROVIDER *prov) |
4c2883a9 | 1127 | { |
f844f9eb | 1128 | #ifdef FIPS_MODULE |
3593266d MC |
1129 | return NULL; |
1130 | #else | |
4c2883a9 | 1131 | return DSO_get_filename(prov->module); |
3593266d | 1132 | #endif |
4c2883a9 RL |
1133 | } |
1134 | ||
24626a47 | 1135 | const char *ossl_provider_module_path(const OSSL_PROVIDER *prov) |
4c2883a9 | 1136 | { |
f844f9eb | 1137 | #ifdef FIPS_MODULE |
3593266d MC |
1138 | return NULL; |
1139 | #else | |
4c2883a9 RL |
1140 | /* FIXME: Ensure it's a full path */ |
1141 | return DSO_get_filename(prov->module); | |
3593266d | 1142 | #endif |
4c2883a9 RL |
1143 | } |
1144 | ||
d01d3752 MC |
1145 | void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov) |
1146 | { | |
1147 | if (prov != NULL) | |
1148 | return prov->provctx; | |
1149 | ||
1150 | return NULL; | |
1151 | } | |
1152 | ||
f12a5690 MC |
1153 | const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov) |
1154 | { | |
1155 | if (prov != NULL) | |
1156 | return prov->dispatch; | |
1157 | ||
1158 | return NULL; | |
1159 | } | |
1160 | ||
a829b735 | 1161 | OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov) |
e74bd290 | 1162 | { |
185ce3d9 | 1163 | return prov != NULL ? prov->libctx : NULL; |
e74bd290 RL |
1164 | } |
1165 | ||
4c2883a9 RL |
1166 | /* Wrappers around calls to the provider */ |
1167 | void ossl_provider_teardown(const OSSL_PROVIDER *prov) | |
1168 | { | |
c1fb5e07 MC |
1169 | if (prov->teardown != NULL |
1170 | #ifndef FIPS_MODULE | |
1171 | && !prov->ischild | |
1172 | #endif | |
1173 | ) | |
a39eb840 | 1174 | prov->teardown(prov->provctx); |
4c2883a9 RL |
1175 | } |
1176 | ||
dca97d00 | 1177 | const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov) |
4c2883a9 | 1178 | { |
dca97d00 RL |
1179 | return prov->gettable_params == NULL |
1180 | ? NULL : prov->gettable_params(prov->provctx); | |
4c2883a9 RL |
1181 | } |
1182 | ||
4e7991b4 | 1183 | int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[]) |
4c2883a9 | 1184 | { |
a39eb840 RL |
1185 | return prov->get_params == NULL |
1186 | ? 0 : prov->get_params(prov->provctx, params); | |
4c2883a9 RL |
1187 | } |
1188 | ||
04cb5ec0 SL |
1189 | int ossl_provider_self_test(const OSSL_PROVIDER *prov) |
1190 | { | |
1191 | int ret; | |
1192 | ||
1193 | if (prov->self_test == NULL) | |
1194 | return 1; | |
1195 | ret = prov->self_test(prov->provctx); | |
1196 | if (ret == 0) | |
0090e508 | 1197 | (void)provider_flush_store_cache(prov); |
04cb5ec0 SL |
1198 | return ret; |
1199 | } | |
1200 | ||
82ec09ec MC |
1201 | int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov, |
1202 | const char *capability, | |
1203 | OSSL_CALLBACK *cb, | |
1204 | void *arg) | |
1205 | { | |
1206 | return prov->get_capabilities == NULL | |
08a1c9f2 | 1207 | ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg); |
82ec09ec MC |
1208 | } |
1209 | ||
099bd339 RL |
1210 | const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov, |
1211 | int operation_id, | |
1212 | int *no_cache) | |
1213 | { | |
7dce37e2 P |
1214 | const OSSL_ALGORITHM *res; |
1215 | ||
1216 | if (prov->query_operation == NULL) | |
1217 | return NULL; | |
1218 | res = prov->query_operation(prov->provctx, operation_id, no_cache); | |
1219 | #if defined(OPENSSL_NO_CACHED_FETCH) | |
1220 | /* Forcing the non-caching of queries */ | |
1221 | if (no_cache != NULL) | |
1222 | *no_cache = 1; | |
1223 | #endif | |
1224 | return res; | |
099bd339 RL |
1225 | } |
1226 | ||
b0001d0c P |
1227 | void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov, |
1228 | int operation_id, | |
1229 | const OSSL_ALGORITHM *algs) | |
1230 | { | |
1231 | if (prov->unquery_operation != NULL) | |
1232 | prov->unquery_operation(prov->provctx, operation_id, algs); | |
1233 | } | |
1234 | ||
0090e508 P |
1235 | int ossl_provider_clear_all_operation_bits(OSSL_LIB_CTX *libctx) |
1236 | { | |
1237 | struct provider_store_st *store; | |
1238 | OSSL_PROVIDER *provider; | |
1239 | int i, num, res = 1; | |
1240 | ||
1241 | if ((store = get_provider_store(libctx)) != NULL) { | |
1242 | if (!CRYPTO_THREAD_read_lock(store->lock)) | |
1243 | return 0; | |
1244 | num = sk_OSSL_PROVIDER_num(store->providers); | |
1245 | for (i = 0; i < num; i++) { | |
1246 | provider = sk_OSSL_PROVIDER_value(store->providers, i); | |
1247 | if (!CRYPTO_THREAD_write_lock(provider->opbits_lock)) { | |
1248 | res = 0; | |
1249 | continue; | |
1250 | } | |
1251 | if (provider->operation_bits != NULL) | |
1252 | memset(provider->operation_bits, 0, | |
1253 | provider->operation_bits_sz); | |
1254 | CRYPTO_THREAD_unlock(provider->opbits_lock); | |
1255 | } | |
1256 | CRYPTO_THREAD_unlock(store->lock); | |
1257 | return res; | |
1258 | } | |
1259 | return 0; | |
1260 | } | |
1261 | ||
5a29b628 RL |
1262 | int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum) |
1263 | { | |
1264 | size_t byte = bitnum / 8; | |
1265 | unsigned char bit = (1 << (bitnum % 8)) & 0xFF; | |
1266 | ||
cd3f8c1b RS |
1267 | if (!CRYPTO_THREAD_write_lock(provider->opbits_lock)) |
1268 | return 0; | |
5a29b628 | 1269 | if (provider->operation_bits_sz <= byte) { |
2b748d72 TS |
1270 | unsigned char *tmp = OPENSSL_realloc(provider->operation_bits, |
1271 | byte + 1); | |
1272 | ||
1273 | if (tmp == NULL) { | |
c25a1524 | 1274 | CRYPTO_THREAD_unlock(provider->opbits_lock); |
5a29b628 RL |
1275 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
1276 | return 0; | |
1277 | } | |
2b748d72 | 1278 | provider->operation_bits = tmp; |
5a29b628 RL |
1279 | memset(provider->operation_bits + provider->operation_bits_sz, |
1280 | '\0', byte + 1 - provider->operation_bits_sz); | |
2b748d72 | 1281 | provider->operation_bits_sz = byte + 1; |
5a29b628 RL |
1282 | } |
1283 | provider->operation_bits[byte] |= bit; | |
c25a1524 | 1284 | CRYPTO_THREAD_unlock(provider->opbits_lock); |
5a29b628 RL |
1285 | return 1; |
1286 | } | |
1287 | ||
1288 | int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum, | |
1289 | int *result) | |
1290 | { | |
1291 | size_t byte = bitnum / 8; | |
1292 | unsigned char bit = (1 << (bitnum % 8)) & 0xFF; | |
1293 | ||
1294 | if (!ossl_assert(result != NULL)) { | |
1295 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); | |
1296 | return 0; | |
1297 | } | |
1298 | ||
1299 | *result = 0; | |
cd3f8c1b RS |
1300 | if (!CRYPTO_THREAD_read_lock(provider->opbits_lock)) |
1301 | return 0; | |
5a29b628 RL |
1302 | if (provider->operation_bits_sz > byte) |
1303 | *result = ((provider->operation_bits[byte] & bit) != 0); | |
c25a1524 | 1304 | CRYPTO_THREAD_unlock(provider->opbits_lock); |
5a29b628 RL |
1305 | return 1; |
1306 | } | |
1307 | ||
c1fb5e07 | 1308 | #ifndef FIPS_MODULE |
8c627075 MC |
1309 | const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov) |
1310 | { | |
1311 | return prov->handle; | |
1312 | } | |
1313 | ||
abaa2dd2 | 1314 | int ossl_provider_is_child(const OSSL_PROVIDER *prov) |
f12a5690 | 1315 | { |
abaa2dd2 MC |
1316 | return prov->ischild; |
1317 | } | |
8c627075 | 1318 | |
abaa2dd2 MC |
1319 | int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle) |
1320 | { | |
8c627075 | 1321 | prov->handle = handle; |
f12a5690 | 1322 | prov->ischild = 1; |
8c627075 | 1323 | |
abaa2dd2 MC |
1324 | return 1; |
1325 | } | |
1326 | ||
1327 | int ossl_provider_convert_to_child(OSSL_PROVIDER *prov, | |
1328 | const OSSL_CORE_HANDLE *handle, | |
1329 | OSSL_provider_init_fn *init_function) | |
1330 | { | |
1331 | int flush = 0; | |
1332 | ||
1333 | if (!CRYPTO_THREAD_write_lock(prov->store->lock)) | |
8c627075 | 1334 | return 0; |
abaa2dd2 MC |
1335 | if (!CRYPTO_THREAD_write_lock(prov->flag_lock)) { |
1336 | CRYPTO_THREAD_unlock(prov->store->lock); | |
1337 | return 0; | |
1338 | } | |
1339 | /* | |
1340 | * The provider could be in one of three states: (1) Already a child, | |
1341 | * (2) Not a child (but eligible to be one), or (3) Not a child (not | |
1342 | * eligible to be one). | |
1343 | */ | |
1344 | if (prov->flag_couldbechild) { | |
1345 | ossl_provider_set_child(prov, handle); | |
1346 | prov->init_function = init_function; | |
1347 | } | |
1348 | if (prov->ischild && provider_activate(prov, 0, 0)) { | |
1349 | flush = 1; | |
1350 | prov->store->use_fallbacks = 0; | |
1351 | } | |
8c627075 | 1352 | |
abaa2dd2 MC |
1353 | CRYPTO_THREAD_unlock(prov->flag_lock); |
1354 | CRYPTO_THREAD_unlock(prov->store->lock); | |
1355 | ||
1356 | if (flush) | |
1357 | provider_flush_store_cache(prov); | |
1358 | ||
1359 | /* | |
1360 | * We report success whether or not the provider was eligible for conversion | |
1361 | * to a child. If its not elgibile then it has already been loaded as a non | |
1362 | * child provider and we should keep it like that. | |
1363 | */ | |
8c627075 | 1364 | return 1; |
f12a5690 MC |
1365 | } |
1366 | ||
447588b6 MC |
1367 | int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props) |
1368 | { | |
1369 | #ifndef FIPS_MODULE | |
1370 | struct provider_store_st *store = NULL; | |
1371 | int i, max; | |
1372 | OSSL_PROVIDER_CHILD_CB *child_cb; | |
1373 | ||
1374 | if ((store = get_provider_store(libctx)) == NULL) | |
1375 | return 0; | |
1376 | ||
1377 | if (!CRYPTO_THREAD_read_lock(store->lock)) | |
1378 | return 0; | |
1379 | ||
1380 | max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs); | |
1381 | for (i = 0; i < max; i++) { | |
1382 | child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i); | |
1383 | child_cb->global_props_cb(props, child_cb->cbdata); | |
1384 | } | |
1385 | ||
1386 | CRYPTO_THREAD_unlock(store->lock); | |
1387 | #endif | |
1388 | return 1; | |
1389 | } | |
1390 | ||
7b88c184 MC |
1391 | static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle, |
1392 | int (*create_cb)( | |
1393 | const OSSL_CORE_HANDLE *provider, | |
1394 | void *cbdata), | |
b1956770 | 1395 | int (*remove_cb)( |
7b88c184 MC |
1396 | const OSSL_CORE_HANDLE *provider, |
1397 | void *cbdata), | |
b1956770 | 1398 | int (*global_props_cb)( |
447588b6 MC |
1399 | const char *props, |
1400 | void *cbdata), | |
7b88c184 MC |
1401 | void *cbdata) |
1402 | { | |
1403 | /* | |
1404 | * This is really an OSSL_PROVIDER that we created and cast to | |
1405 | * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back. | |
1406 | */ | |
1407 | OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle; | |
1408 | OSSL_PROVIDER *prov; | |
1409 | OSSL_LIB_CTX *libctx = thisprov->libctx; | |
1410 | struct provider_store_st *store = NULL; | |
1411 | int ret = 0, i, max; | |
1412 | OSSL_PROVIDER_CHILD_CB *child_cb; | |
447588b6 | 1413 | char *propsstr = NULL; |
7b88c184 MC |
1414 | |
1415 | if ((store = get_provider_store(libctx)) == NULL) | |
1416 | return 0; | |
1417 | ||
1418 | child_cb = OPENSSL_malloc(sizeof(*child_cb)); | |
1419 | if (child_cb == NULL) | |
1420 | return 0; | |
1421 | child_cb->prov = thisprov; | |
1422 | child_cb->create_cb = create_cb; | |
1423 | child_cb->remove_cb = remove_cb; | |
447588b6 | 1424 | child_cb->global_props_cb = global_props_cb; |
7b88c184 MC |
1425 | child_cb->cbdata = cbdata; |
1426 | ||
1427 | if (!CRYPTO_THREAD_write_lock(store->lock)) { | |
1428 | OPENSSL_free(child_cb); | |
1429 | return 0; | |
1430 | } | |
447588b6 MC |
1431 | propsstr = evp_get_global_properties_str(libctx, 0); |
1432 | ||
1433 | if (propsstr != NULL) { | |
1434 | global_props_cb(propsstr, cbdata); | |
1435 | OPENSSL_free(propsstr); | |
1436 | } | |
7b88c184 MC |
1437 | max = sk_OSSL_PROVIDER_num(store->providers); |
1438 | for (i = 0; i < max; i++) { | |
1439 | prov = sk_OSSL_PROVIDER_value(store->providers, i); | |
abaa2dd2 MC |
1440 | /* |
1441 | * We require register_child_cb to be called during a provider init | |
1442 | * function. The currently initing provider will never be activated yet | |
1443 | * and we we should not attempt to aquire the flag_lock for it. | |
1444 | */ | |
1445 | if (prov == thisprov) | |
1446 | continue; | |
7b88c184 MC |
1447 | if (!CRYPTO_THREAD_read_lock(prov->flag_lock)) |
1448 | break; | |
1449 | /* | |
1450 | * We hold the lock while calling the user callback. This means that the | |
1451 | * user callback must be short and simple and not do anything likely to | |
1452 | * cause a deadlock. | |
1453 | */ | |
1454 | if (prov->flag_activated | |
1455 | && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata)) | |
1456 | break; | |
1457 | CRYPTO_THREAD_unlock(prov->flag_lock); | |
1458 | } | |
1459 | if (i == max) { | |
1460 | /* Success */ | |
1461 | ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb); | |
1462 | } | |
1463 | if (i != max || ret <= 0) { | |
1464 | /* Failed during creation. Remove everything we just added */ | |
1465 | for (; i >= 0; i--) { | |
1466 | prov = sk_OSSL_PROVIDER_value(store->providers, i); | |
1467 | remove_cb((OSSL_CORE_HANDLE *)prov, cbdata); | |
1468 | } | |
1469 | OPENSSL_free(child_cb); | |
1470 | ret = 0; | |
1471 | } | |
1472 | CRYPTO_THREAD_unlock(store->lock); | |
1473 | ||
1474 | return ret; | |
1475 | } | |
1476 | ||
1477 | static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle) | |
1478 | { | |
1479 | /* | |
1480 | * This is really an OSSL_PROVIDER that we created and cast to | |
1481 | * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back. | |
1482 | */ | |
1483 | OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle; | |
1484 | OSSL_LIB_CTX *libctx = thisprov->libctx; | |
1485 | struct provider_store_st *store = NULL; | |
1486 | int i, max; | |
1487 | OSSL_PROVIDER_CHILD_CB *child_cb; | |
1488 | ||
1489 | if ((store = get_provider_store(libctx)) == NULL) | |
1490 | return; | |
1491 | ||
1492 | if (!CRYPTO_THREAD_write_lock(store->lock)) | |
1493 | return; | |
1494 | max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs); | |
1495 | for (i = 0; i < max; i++) { | |
1496 | child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i); | |
1497 | if (child_cb->prov == thisprov) { | |
1498 | /* Found an entry */ | |
1499 | sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i); | |
1500 | OPENSSL_free(child_cb); | |
1501 | break; | |
1502 | } | |
1503 | } | |
1504 | CRYPTO_THREAD_unlock(store->lock); | |
1505 | } | |
1506 | #endif | |
1507 | ||
4c2883a9 RL |
1508 | /*- |
1509 | * Core functions for the provider | |
1510 | * =============================== | |
1511 | * | |
1512 | * This is the set of functions that the core makes available to the provider | |
1513 | */ | |
1514 | ||
1515 | /* | |
1516 | * This returns a list of Provider Object parameters with their types, for | |
1517 | * discovery. We do not expect that many providers will use this, but one | |
1518 | * never knows. | |
1519 | */ | |
26175013 | 1520 | static const OSSL_PARAM param_types[] = { |
b8086652 SL |
1521 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0), |
1522 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR, | |
1523 | NULL, 0), | |
1524 | #ifndef FIPS_MODULE | |
1525 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR, | |
1526 | NULL, 0), | |
1527 | #endif | |
26175013 | 1528 | OSSL_PARAM_END |
4c2883a9 RL |
1529 | }; |
1530 | ||
49c64346 RL |
1531 | /* |
1532 | * Forward declare all the functions that are provided aa dispatch. | |
1533 | * This ensures that the compiler will complain if they aren't defined | |
1534 | * with the correct signature. | |
1535 | */ | |
363b1e5d DMSP |
1536 | static OSSL_FUNC_core_gettable_params_fn core_gettable_params; |
1537 | static OSSL_FUNC_core_get_params_fn core_get_params; | |
1538 | static OSSL_FUNC_core_thread_start_fn core_thread_start; | |
a829b735 | 1539 | static OSSL_FUNC_core_get_libctx_fn core_get_libctx; |
f844f9eb | 1540 | #ifndef FIPS_MODULE |
363b1e5d DMSP |
1541 | static OSSL_FUNC_core_new_error_fn core_new_error; |
1542 | static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug; | |
1543 | static OSSL_FUNC_core_vset_error_fn core_vset_error; | |
1544 | static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark; | |
1545 | static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark; | |
1546 | static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark; | |
49c64346 RL |
1547 | #endif |
1548 | ||
d40b42ab | 1549 | static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle) |
4c2883a9 RL |
1550 | { |
1551 | return param_types; | |
1552 | } | |
1553 | ||
d40b42ab | 1554 | static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[]) |
4c2883a9 RL |
1555 | { |
1556 | int i; | |
4e7991b4 | 1557 | OSSL_PARAM *p; |
d40b42ab MC |
1558 | /* |
1559 | * We created this object originally and we know it is actually an | |
1560 | * OSSL_PROVIDER *, so the cast is safe | |
1561 | */ | |
1562 | OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle; | |
4c2883a9 | 1563 | |
b8086652 | 1564 | if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL) |
ac1055ef | 1565 | OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR); |
b8086652 | 1566 | if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL) |
ac1055ef RL |
1567 | OSSL_PARAM_set_utf8_ptr(p, prov->name); |
1568 | ||
f844f9eb | 1569 | #ifndef FIPS_MODULE |
b8086652 SL |
1570 | if ((p = OSSL_PARAM_locate(params, |
1571 | OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL) | |
25e60144 SL |
1572 | OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov)); |
1573 | #endif | |
1574 | ||
ac1055ef RL |
1575 | if (prov->parameters == NULL) |
1576 | return 1; | |
1577 | ||
1578 | for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) { | |
1579 | INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i); | |
1580 | ||
1581 | if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL) | |
1582 | OSSL_PARAM_set_utf8_ptr(p, pair->value); | |
4c2883a9 | 1583 | } |
4c2883a9 RL |
1584 | return 1; |
1585 | } | |
1586 | ||
d40b42ab | 1587 | static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle) |
e7706e63 | 1588 | { |
d40b42ab MC |
1589 | /* |
1590 | * We created this object originally and we know it is actually an | |
1591 | * OSSL_PROVIDER *, so the cast is safe | |
1592 | */ | |
1593 | OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle; | |
1594 | ||
a23deef2 TM |
1595 | /* |
1596 | * Using ossl_provider_libctx would be wrong as that returns | |
1597 | * NULL for |prov| == NULL and NULL libctx has a special meaning | |
1598 | * that does not apply here. Here |prov| == NULL can happen only in | |
1599 | * case of a coding error. | |
1600 | */ | |
2217d4c9 | 1601 | assert(prov != NULL); |
a23deef2 | 1602 | return (OPENSSL_CORE_CTX *)prov->libctx; |
e7706e63 RL |
1603 | } |
1604 | ||
d40b42ab | 1605 | static int core_thread_start(const OSSL_CORE_HANDLE *handle, |
c9732f09 MC |
1606 | OSSL_thread_stop_handler_fn handfn, |
1607 | void *arg) | |
da747958 | 1608 | { |
d40b42ab MC |
1609 | /* |
1610 | * We created this object originally and we know it is actually an | |
1611 | * OSSL_PROVIDER *, so the cast is safe | |
1612 | */ | |
1613 | OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle; | |
1614 | ||
c9732f09 | 1615 | return ossl_init_thread_start(prov, arg, handfn); |
da747958 MC |
1616 | } |
1617 | ||
6592ab81 RL |
1618 | /* |
1619 | * The FIPS module inner provider doesn't implement these. They aren't | |
1620 | * needed there, since the FIPS module upcalls are always the outer provider | |
1621 | * ones. | |
1622 | */ | |
f844f9eb | 1623 | #ifndef FIPS_MODULE |
49c64346 | 1624 | /* |
a23deef2 TM |
1625 | * These error functions should use |handle| to select the proper |
1626 | * library context to report in the correct error stack if error | |
49c64346 RL |
1627 | * stacks become tied to the library context. |
1628 | * We cannot currently do that since there's no support for it in the | |
1629 | * ERR subsystem. | |
1630 | */ | |
d40b42ab | 1631 | static void core_new_error(const OSSL_CORE_HANDLE *handle) |
49c64346 RL |
1632 | { |
1633 | ERR_new(); | |
1634 | } | |
1635 | ||
d40b42ab | 1636 | static void core_set_error_debug(const OSSL_CORE_HANDLE *handle, |
49c64346 RL |
1637 | const char *file, int line, const char *func) |
1638 | { | |
1639 | ERR_set_debug(file, line, func); | |
1640 | } | |
1641 | ||
d40b42ab | 1642 | static void core_vset_error(const OSSL_CORE_HANDLE *handle, |
49c64346 | 1643 | uint32_t reason, const char *fmt, va_list args) |
6ebc2f56 | 1644 | { |
d40b42ab MC |
1645 | /* |
1646 | * We created this object originally and we know it is actually an | |
1647 | * OSSL_PROVIDER *, so the cast is safe | |
1648 | */ | |
1649 | OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle; | |
1650 | ||
6ebc2f56 RL |
1651 | /* |
1652 | * If the uppermost 8 bits are non-zero, it's an OpenSSL library | |
1653 | * error and will be treated as such. Otherwise, it's a new style | |
1654 | * provider error and will be treated as such. | |
1655 | */ | |
1656 | if (ERR_GET_LIB(reason) != 0) { | |
49c64346 | 1657 | ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args); |
6ebc2f56 | 1658 | } else { |
49c64346 | 1659 | ERR_vset_error(prov->error_lib, (int)reason, fmt, args); |
6ebc2f56 RL |
1660 | } |
1661 | } | |
7b131de2 | 1662 | |
d40b42ab | 1663 | static int core_set_error_mark(const OSSL_CORE_HANDLE *handle) |
7b131de2 RL |
1664 | { |
1665 | return ERR_set_mark(); | |
1666 | } | |
1667 | ||
d40b42ab | 1668 | static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle) |
7b131de2 RL |
1669 | { |
1670 | return ERR_clear_last_mark(); | |
1671 | } | |
1672 | ||
d40b42ab | 1673 | static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle) |
7b131de2 RL |
1674 | { |
1675 | return ERR_pop_to_mark(); | |
1676 | } | |
f844f9eb | 1677 | #endif /* FIPS_MODULE */ |
6ebc2f56 | 1678 | |
b60cba3c | 1679 | /* |
03bede0c | 1680 | * Functions provided by the core. |
b60cba3c | 1681 | */ |
4c2883a9 | 1682 | static const OSSL_DISPATCH core_dispatch_[] = { |
dca97d00 | 1683 | { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params }, |
4c2883a9 | 1684 | { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params }, |
a829b735 | 1685 | { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx }, |
da747958 | 1686 | { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start }, |
f844f9eb | 1687 | #ifndef FIPS_MODULE |
49c64346 RL |
1688 | { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error }, |
1689 | { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug }, | |
1690 | { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error }, | |
7b131de2 RL |
1691 | { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark }, |
1692 | { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK, | |
1693 | (void (*)(void))core_clear_last_error_mark }, | |
d16d0b71 | 1694 | { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark }, |
141cc94e P |
1695 | { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file }, |
1696 | { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf }, | |
1697 | { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex }, | |
1698 | { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex }, | |
1699 | { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets }, | |
1700 | { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts }, | |
1701 | { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl }, | |
1702 | { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref }, | |
1703 | { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free }, | |
1704 | { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf }, | |
d16d0b71 | 1705 | { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf }, |
36fc5fc6 | 1706 | { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback }, |
03bede0c P |
1707 | { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy }, |
1708 | { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy }, | |
1709 | { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce }, | |
1710 | { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce }, | |
6592ab81 | 1711 | #endif |
b60cba3c RS |
1712 | { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc }, |
1713 | { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc }, | |
b60cba3c RS |
1714 | { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free }, |
1715 | { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free }, | |
1716 | { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc }, | |
1717 | { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc }, | |
1718 | { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc }, | |
1719 | { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc }, | |
1720 | { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free }, | |
1721 | { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE, | |
1722 | (void (*)(void))CRYPTO_secure_clear_free }, | |
1723 | { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED, | |
1724 | (void (*)(void))CRYPTO_secure_allocated }, | |
1725 | { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse }, | |
f12a5690 | 1726 | #ifndef FIPS_MODULE |
7b88c184 MC |
1727 | { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB, |
1728 | (void (*)(void))ossl_provider_register_child_cb }, | |
1729 | { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB, | |
1730 | (void (*)(void))ossl_provider_deregister_child_cb }, | |
1731 | { OSSL_FUNC_PROVIDER_NAME, | |
f12a5690 | 1732 | (void (*)(void))OSSL_PROVIDER_name }, |
7b88c184 | 1733 | { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX, |
f12a5690 | 1734 | (void (*)(void))OSSL_PROVIDER_get0_provider_ctx }, |
7b88c184 | 1735 | { OSSL_FUNC_PROVIDER_GET0_DISPATCH, |
f12a5690 | 1736 | (void (*)(void))OSSL_PROVIDER_get0_dispatch }, |
8c627075 MC |
1737 | { OSSL_FUNC_PROVIDER_UP_REF, |
1738 | (void (*)(void))provider_up_ref_intern }, | |
1739 | { OSSL_FUNC_PROVIDER_FREE, | |
1740 | (void (*)(void))provider_free_intern }, | |
f12a5690 | 1741 | #endif |
4c2883a9 RL |
1742 | { 0, NULL } |
1743 | }; | |
1744 | static const OSSL_DISPATCH *core_dispatch = core_dispatch_; |