]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/provider_core.c
30fa44d789a0f054ef54be37a7e28be6776e0a41
[thirdparty/openssl.git] / crypto / provider_core.c
1 /*
2 * Copyright 2019-2021 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 <assert.h>
11 #include <openssl/core.h>
12 #include <openssl/core_dispatch.h>
13 #include <openssl/core_names.h>
14 #include <openssl/provider.h>
15 #include <openssl/params.h>
16 #include <openssl/opensslv.h>
17 #include "crypto/cryptlib.h"
18 #include "crypto/evp.h" /* evp_method_store_flush */
19 #include "crypto/rand.h"
20 #include "internal/nelem.h"
21 #include "internal/thread_once.h"
22 #include "internal/provider.h"
23 #include "internal/refcount.h"
24 #include "internal/bio.h"
25 #include "internal/core.h"
26 #include "provider_local.h"
27 #ifndef FIPS_MODULE
28 # include <openssl/self_test.h>
29 #endif
30
31 static OSSL_PROVIDER *provider_new(const char *name,
32 OSSL_provider_init_fn *init_function);
33
34 /*-
35 * Provider Object structure
36 * =========================
37 */
38
39 typedef struct {
40 char *name;
41 char *value;
42 } INFOPAIR;
43 DEFINE_STACK_OF(INFOPAIR)
44
45 #ifndef FIPS_MODULE
46 typedef struct {
47 OSSL_PROVIDER *prov;
48 int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
49 int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
50 int (*global_props_cb)(const char *props, void *cbdata);
51 void *cbdata;
52 } OSSL_PROVIDER_CHILD_CB;
53 DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB)
54 #endif
55
56 struct provider_store_st; /* Forward declaration */
57
58 struct ossl_provider_st {
59 /* Flag bits */
60 unsigned int flag_initialized:1;
61 unsigned int flag_activated:1;
62 unsigned int flag_fallback:1; /* Can be used as fallback */
63 #ifndef FIPS_MODULE
64 unsigned int flag_couldbechild:1;
65 #endif
66
67 /* Getting and setting the flags require synchronization */
68 CRYPTO_RWLOCK *flag_lock;
69
70 /* OpenSSL library side data */
71 CRYPTO_REF_COUNT refcnt;
72 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
73 int activatecnt;
74 char *name;
75 char *path;
76 DSO *module;
77 OSSL_provider_init_fn *init_function;
78 STACK_OF(INFOPAIR) *parameters;
79 OSSL_LIB_CTX *libctx; /* The library context this instance is in */
80 struct provider_store_st *store; /* The store this instance belongs to */
81 #ifndef FIPS_MODULE
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 */
86 int error_lib; /* ERR library number, one for each provider */
87 # ifndef OPENSSL_NO_ERR
88 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
89 # endif
90 #endif
91
92 /* Provider side functions */
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;
97 OSSL_FUNC_provider_self_test_fn *self_test;
98 OSSL_FUNC_provider_query_operation_fn *query_operation;
99 OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
100
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;
107 CRYPTO_RWLOCK *opbits_lock;
108
109 #ifndef FIPS_MODULE
110 /* Whether this provider is the child of some other provider */
111 const OSSL_CORE_HANDLE *handle;
112 unsigned int ischild:1;
113 #endif
114
115 /* Provider side data */
116 void *provctx;
117 const OSSL_DISPATCH *dispatch;
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 {
136 OSSL_LIB_CTX *libctx;
137 STACK_OF(OSSL_PROVIDER) *providers;
138 STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs;
139 CRYPTO_RWLOCK *default_path_lock;
140 CRYPTO_RWLOCK *lock;
141 char *default_path;
142 unsigned int use_fallbacks:1;
143 unsigned int freeing:1;
144 };
145
146 /*
147 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
148 * and ossl_provider_free(), called as needed.
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 {
154 if (prov->flag_activated)
155 ossl_provider_deactivate(prov);
156 ossl_provider_free(prov);
157 }
158
159 #ifndef FIPS_MODULE
160 static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb)
161 {
162 OPENSSL_free(cb);
163 }
164 #endif
165
166 static void provider_store_free(void *vstore)
167 {
168 struct provider_store_st *store = vstore;
169
170 if (store == NULL)
171 return;
172 store->freeing = 1;
173 OPENSSL_free(store->default_path);
174 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
175 #ifndef FIPS_MODULE
176 sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs,
177 ossl_provider_child_cb_free);
178 #endif
179 CRYPTO_THREAD_lock_free(store->default_path_lock);
180 CRYPTO_THREAD_lock_free(store->lock);
181 OPENSSL_free(store);
182 }
183
184 static void *provider_store_new(OSSL_LIB_CTX *ctx)
185 {
186 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
187 const struct predefined_providers_st *p = NULL;
188
189 if (store == NULL
190 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
191 || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
192 #ifndef FIPS_MODULE
193 || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
194 #endif
195 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
196 provider_store_free(store);
197 return NULL;
198 }
199 store->libctx = ctx;
200 store->use_fallbacks = 1;
201
202 for (p = ossl_predefined_providers; p->name != NULL; p++) {
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);
215 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
216 return NULL;
217 }
218 prov->libctx = ctx;
219 prov->store = store;
220 #ifndef FIPS_MODULE
221 prov->error_lib = ERR_get_next_error_library();
222 #endif
223 if(p->is_fallback)
224 ossl_provider_set_fallback(prov);
225 }
226
227 return store;
228 }
229
230 static const OSSL_LIB_CTX_METHOD provider_store_method = {
231 /* Needs to be freed before the child provider data is freed */
232 OSSL_LIB_CTX_METHOD_PRIORITY_1,
233 provider_store_new,
234 provider_store_free,
235 };
236
237 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
238 {
239 struct provider_store_st *store = NULL;
240
241 store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
242 &provider_store_method);
243 if (store == NULL)
244 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
245 return store;
246 }
247
248 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
249 {
250 struct provider_store_st *store;
251
252 if ((store = get_provider_store(libctx)) != NULL) {
253 if (!CRYPTO_THREAD_write_lock(store->lock))
254 return 0;
255 store->use_fallbacks = 0;
256 CRYPTO_THREAD_unlock(store->lock);
257 return 1;
258 }
259 return 0;
260 }
261
262 OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
263 int noconfig)
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
272 #ifndef FIPS_MODULE
273 /*
274 * Make sure any providers are loaded from config before we try to find
275 * them.
276 */
277 if (!noconfig) {
278 if (ossl_lib_ctx_is_default(libctx))
279 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
280 }
281 #endif
282
283 tmpl.name = (char *)name;
284 if (!CRYPTO_THREAD_read_lock(store->lock))
285 return NULL;
286 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
287 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
288 || !ossl_provider_up_ref(prov))
289 prov = NULL;
290 CRYPTO_THREAD_unlock(store->lock);
291 }
292
293 return prov;
294 }
295
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
310 || (prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
311 || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
312 || (prov->name = OPENSSL_strdup(name)) == NULL) {
313 ossl_provider_free(prov);
314 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
315 return NULL;
316 }
317
318 prov->refcnt = 1; /* 1 One reference to be returned */
319 prov->init_function = init_function;
320 #ifndef FIPS_MODULE
321 prov->flag_couldbechild = 1;
322 #endif
323 return prov;
324 }
325
326 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
327 {
328 int ref = 0;
329
330 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
331 return 0;
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
342 return ref;
343 }
344
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
364 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
365 OSSL_provider_init_fn *init_function,
366 int noconfig)
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
374 if ((prov = ossl_provider_find(libctx, name,
375 noconfig)) != NULL) { /* refcount +1 */
376 ossl_provider_free(prov); /* refcount -1 */
377 ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS,
378 "name=%s", name);
379 return NULL;
380 }
381
382 /* provider_new() generates an error, so no need here */
383 if ((prov = provider_new(name, init_function)) == NULL)
384 return NULL;
385
386 if (!CRYPTO_THREAD_write_lock(store->lock))
387 return NULL;
388 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
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;
395 } else {
396 prov->libctx = libctx;
397 prov->store = store;
398 #ifndef FIPS_MODULE
399 prov->error_lib = ERR_get_next_error_library();
400 #endif
401 }
402 CRYPTO_THREAD_unlock(store->lock);
403
404 if (prov == NULL)
405 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
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
415 static void free_infopair(INFOPAIR *pair)
416 {
417 OPENSSL_free(pair->name);
418 OPENSSL_free(pair->value);
419 OPENSSL_free(pair);
420 }
421
422 void ossl_provider_free(OSSL_PROVIDER *prov)
423 {
424 if (prov != NULL) {
425 int ref = 0;
426
427 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
428
429 /*
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.
436 */
437 if (ref == 0) {
438 if (prov->flag_initialized) {
439 ossl_provider_teardown(prov);
440 #ifndef OPENSSL_NO_ERR
441 # ifndef FIPS_MODULE
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 }
447 # endif
448 #endif
449 OPENSSL_free(prov->operation_bits);
450 prov->operation_bits = NULL;
451 prov->operation_bits_sz = 0;
452 prov->flag_initialized = 0;
453 }
454
455 #ifndef FIPS_MODULE
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);
462 DSO_free(prov->module);
463 #endif
464 OPENSSL_free(prov->name);
465 OPENSSL_free(prov->path);
466 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
467 CRYPTO_THREAD_lock_free(prov->opbits_lock);
468 CRYPTO_THREAD_lock_free(prov->flag_lock);
469 #ifndef HAVE_ATOMICS
470 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
471 #endif
472 OPENSSL_free(prov);
473 }
474 #ifndef FIPS_MODULE
475 else if (prov->ischild) {
476 ossl_provider_free_parent(prov, 0);
477 }
478 #endif
479 }
480 }
481
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;
490 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
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 }
512 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
513 return 0;
514 }
515
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
530 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
531 const char *path)
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) {
539 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
540 return 0;
541 }
542 }
543 if ((store = get_provider_store(libctx)) != NULL
544 && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
545 OPENSSL_free(store->default_path);
546 store->default_path = p;
547 CRYPTO_THREAD_unlock(store->default_path_lock);
548 return 1;
549 }
550 OPENSSL_free(p);
551 return 0;
552 }
553
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
557 * appropriate.
558 */
559 static int provider_init(OSSL_PROVIDER *prov, int flag_lock)
560 {
561 const OSSL_DISPATCH *provider_dispatch = NULL;
562 void *tmp_provctx = NULL; /* safety measure */
563 #ifndef OPENSSL_NO_ERR
564 # ifndef FIPS_MODULE
565 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
566 # endif
567 #endif
568 int ok = 0;
569
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 */
576 if (flag_lock && !CRYPTO_THREAD_write_lock(prov->flag_lock))
577 goto end;
578 if (prov->flag_initialized) {
579 ok = 1;
580 goto end;
581 }
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) {
588 #ifdef FIPS_MODULE
589 goto end;
590 #else
591 if (prov->module == NULL) {
592 char *allocated_path = NULL;
593 const char *module_path = NULL;
594 char *merged_path = NULL;
595 const char *load_dir = NULL;
596 char *allocated_load_dir = NULL;
597 struct provider_store_st *store;
598
599 if ((prov->module = DSO_new()) == NULL) {
600 /* DSO_new() generates an error already */
601 goto end;
602 }
603
604 if ((store = get_provider_store(prov->libctx)) == NULL
605 || !CRYPTO_THREAD_read_lock(store->default_path_lock))
606 goto end;
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 }
619
620 if (load_dir == NULL) {
621 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
622 if (load_dir == NULL)
623 load_dir = MODULESDIR;
624 }
625
626 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
627 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
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) {
638 DSO_free(prov->module);
639 prov->module = NULL;
640 }
641
642 OPENSSL_free(merged_path);
643 OPENSSL_free(allocated_path);
644 OPENSSL_free(allocated_load_dir);
645 }
646
647 if (prov->module != NULL)
648 prov->init_function = (OSSL_provider_init_fn *)
649 DSO_bind_func(prov->module, "OSSL_provider_init");
650 #endif
651 }
652
653 /* Call the initialise function for the provider. */
654 if (prov->init_function == NULL
655 || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
656 &provider_dispatch, &tmp_provctx)) {
657 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
658 "name=%s", prov->name);
659 goto end;
660 }
661 prov->provctx = tmp_provctx;
662 prov->dispatch = provider_dispatch;
663 #ifndef FIPS_MODULE
664 prov->flag_couldbechild = 0;
665 #endif
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 =
671 OSSL_FUNC_provider_teardown(provider_dispatch);
672 break;
673 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
674 prov->gettable_params =
675 OSSL_FUNC_provider_gettable_params(provider_dispatch);
676 break;
677 case OSSL_FUNC_PROVIDER_GET_PARAMS:
678 prov->get_params =
679 OSSL_FUNC_provider_get_params(provider_dispatch);
680 break;
681 case OSSL_FUNC_PROVIDER_SELF_TEST:
682 prov->self_test =
683 OSSL_FUNC_provider_self_test(provider_dispatch);
684 break;
685 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
686 prov->get_capabilities =
687 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
688 break;
689 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
690 prov->query_operation =
691 OSSL_FUNC_provider_query_operation(provider_dispatch);
692 break;
693 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
694 prov->unquery_operation =
695 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
696 break;
697 #ifndef OPENSSL_NO_ERR
698 # ifndef FIPS_MODULE
699 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
700 p_get_reason_strings =
701 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
702 break;
703 # endif
704 #endif
705 }
706 }
707
708 #ifndef OPENSSL_NO_ERR
709 # ifndef FIPS_MODULE
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 */
721 cnt = 0;
722 while (reasonstrings[cnt].id != 0) {
723 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
724 goto end;
725 cnt++;
726 }
727 cnt++; /* One for the terminating item */
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)
733 goto end;
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;
747 }
748
749 ERR_load_strings(prov->error_lib, prov->error_strings);
750 }
751 # endif
752 #endif
753
754 /* With this flag set, this provider has become fully "loaded". */
755 prov->flag_initialized = 1;
756 ok = 1;
757
758 end:
759 if (flag_lock)
760 CRYPTO_THREAD_unlock(prov->flag_lock);
761 return ok;
762 }
763
764 /*
765 * Deactivate a provider.
766 * Return -1 on failure and the activation count on success
767 */
768 static int provider_deactivate(OSSL_PROVIDER *prov)
769 {
770 int count;
771 struct provider_store_st *store;
772
773 if (!ossl_assert(prov != NULL))
774 return -1;
775
776 store = get_provider_store(prov->libctx);
777 if (store == NULL)
778 return -1;
779
780 if (!CRYPTO_THREAD_read_lock(store->lock))
781 return -1;
782 if (!CRYPTO_THREAD_write_lock(prov->flag_lock)) {
783 CRYPTO_THREAD_unlock(store->lock);
784 return -1;
785 }
786
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
797 if ((count = --prov->activatecnt) < 1) {
798 prov->flag_activated = 0;
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 }
808 }
809 #endif
810 }
811
812 CRYPTO_THREAD_unlock(prov->flag_lock);
813 CRYPTO_THREAD_unlock(store->lock);
814
815 /* We don't deinit here, that's done in ossl_provider_free() */
816 return count;
817 }
818
819 /*
820 * Activate a provider.
821 * Return -1 on failure and the activation count on success
822 */
823 static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
824 {
825 int count = -1;
826
827 if (provider_init(prov, lock)) {
828 int ret = 1;
829 struct provider_store_st *store;
830
831 store = get_provider_store(prov->libctx);
832 if (store == NULL)
833 return -1;
834
835 if (lock && !CRYPTO_THREAD_read_lock(store->lock))
836 return -1;
837
838 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
839 CRYPTO_THREAD_unlock(store->lock);
840 return -1;
841 }
842
843 #ifndef FIPS_MODULE
844 if (prov->ischild && upcalls)
845 ret = ossl_provider_up_ref_parent(prov, 1);
846 #endif
847
848 if (ret) {
849 count = ++prov->activatecnt;
850 prov->flag_activated = 1;
851
852 #ifndef FIPS_MODULE
853 if (prov->activatecnt == 1) {
854 OSSL_PROVIDER_CHILD_CB *child_cb;
855 int i, max;
856
857 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
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 }
868 }
869 #endif
870 }
871
872 if (lock) {
873 CRYPTO_THREAD_unlock(prov->flag_lock);
874 CRYPTO_THREAD_unlock(store->lock);
875 }
876 if (!ret)
877 return -1;
878 }
879
880 return count;
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;
899 }
900
901 int ossl_provider_activate(OSSL_PROVIDER *prov, int retain_fallbacks,
902 int upcalls)
903 {
904 int count;
905
906 if (prov == NULL)
907 return 0;
908 if ((count = provider_activate(prov, 1, upcalls)) > 0) {
909 if (!retain_fallbacks) {
910 if (!CRYPTO_THREAD_write_lock(prov->store->lock)) {
911 provider_deactivate(prov);
912 return 0;
913 }
914 prov->store->use_fallbacks = 0;
915 CRYPTO_THREAD_unlock(prov->store->lock);
916 }
917 return count == 1 ? provider_flush_store_cache(prov) : 1;
918 }
919 return 0;
920 }
921
922 int ossl_provider_deactivate(OSSL_PROVIDER *prov)
923 {
924 int count;
925
926 if (prov == NULL || (count = provider_deactivate(prov)) < 0)
927 return 0;
928 return count == 0 ? provider_flush_store_cache(prov) : 1;
929 }
930
931 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
932 {
933 return prov->provctx;
934 }
935
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 {
943 int use_fallbacks;
944 int num_provs;
945 int activated_fallback_count = 0;
946 int i;
947
948 if (!CRYPTO_THREAD_read_lock(store->lock))
949 return;
950 use_fallbacks = store->use_fallbacks;
951 CRYPTO_THREAD_unlock(store->lock);
952 if (!use_fallbacks)
953 return;
954
955 if (!CRYPTO_THREAD_write_lock(store->lock))
956 return;
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 }
963
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);
967
968 if (ossl_provider_up_ref(prov)) {
969 if (CRYPTO_THREAD_write_lock(prov->flag_lock)) {
970 if (prov->flag_fallback) {
971 if (provider_activate(prov, 0, 0) > 0)
972 activated_fallback_count++;
973 }
974 CRYPTO_THREAD_unlock(prov->flag_lock);
975 }
976 ossl_provider_free(prov);
977 }
978 }
979
980 /*
981 * We assume that all fallbacks have been added to the store before
982 * any fallback is activated.
983 */
984 if (activated_fallback_count > 0)
985 store->use_fallbacks = 0;
986
987 CRYPTO_THREAD_unlock(store->lock);
988 }
989
990 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
991 int (*cb)(OSSL_PROVIDER *provider,
992 void *cbdata),
993 void *cbdata)
994 {
995 int ret = 0, curr, max;
996 struct provider_store_st *store = get_provider_store(ctx);
997 STACK_OF(OSSL_PROVIDER) *provs = NULL;
998
999 #ifndef FIPS_MODULE
1000 /*
1001 * Make sure any providers are loaded from config before we try to use
1002 * them.
1003 */
1004 if (ossl_lib_ctx_is_default(ctx))
1005 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
1006 #endif
1007
1008 if (store == NULL)
1009 return 1;
1010 provider_activate_fallbacks(store);
1011
1012 /*
1013 * Under lock, grab a copy of the provider list and up_ref each
1014 * provider so that they don't disappear underneath us.
1015 */
1016 if (!CRYPTO_THREAD_read_lock(store->lock))
1017 return 0;
1018 provs = sk_OSSL_PROVIDER_dup(store->providers);
1019 if (provs == NULL) {
1020 CRYPTO_THREAD_unlock(store->lock);
1021 return 0;
1022 }
1023 max = sk_OSSL_PROVIDER_num(provs);
1024 /*
1025 * We work backwards through the stack so that we can safely delete items
1026 * as we go.
1027 */
1028 for (curr = max - 1; curr >= 0; curr--) {
1029 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1030
1031 if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
1032 goto err_unlock;
1033 if (prov->flag_activated) {
1034 if (!ossl_provider_up_ref(prov)){
1035 CRYPTO_THREAD_unlock(prov->flag_lock);
1036 goto err_unlock;
1037 }
1038 /*
1039 * It's already activated, but we up the activated count to ensure
1040 * it remains activated until after we've called the user callback.
1041 */
1042 if (provider_activate(prov, 0, 1) < 0) {
1043 ossl_provider_free(prov);
1044 CRYPTO_THREAD_unlock(prov->flag_lock);
1045 goto err_unlock;
1046 }
1047 } else {
1048 sk_OSSL_PROVIDER_delete(provs, curr);
1049 max--;
1050 }
1051 CRYPTO_THREAD_unlock(prov->flag_lock);
1052 }
1053 CRYPTO_THREAD_unlock(store->lock);
1054
1055 /*
1056 * Now, we sweep through all providers not under lock
1057 */
1058 for (curr = 0; curr < max; curr++) {
1059 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1060
1061 if (!cb(prov, cbdata))
1062 goto finish;
1063 }
1064 curr = -1;
1065
1066 ret = 1;
1067 goto finish;
1068
1069 err_unlock:
1070 CRYPTO_THREAD_unlock(store->lock);
1071 finish:
1072 /*
1073 * The pop_free call doesn't do what we want on an error condition. We
1074 * either start from the first item in the stack, or part way through if
1075 * we only processed some of the items.
1076 */
1077 for (curr++; curr < max; curr++) {
1078 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1079
1080 provider_deactivate(prov);
1081 ossl_provider_free(prov);
1082 }
1083 sk_OSSL_PROVIDER_free(provs);
1084 return ret;
1085 }
1086
1087 int ossl_provider_available(OSSL_PROVIDER *prov)
1088 {
1089 int ret;
1090
1091 if (prov != NULL) {
1092 provider_activate_fallbacks(prov->store);
1093
1094 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1095 return 0;
1096 ret = prov->flag_activated;
1097 CRYPTO_THREAD_unlock(prov->flag_lock);
1098 return ret;
1099 }
1100 return 0;
1101 }
1102
1103 /* Setters of Provider Object data */
1104 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
1105 {
1106 if (prov == NULL)
1107 return 0;
1108
1109 prov->flag_fallback = 1;
1110 return 1;
1111 }
1112
1113 /* Getters of Provider Object data */
1114 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
1115 {
1116 return prov->name;
1117 }
1118
1119 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
1120 {
1121 return prov->module;
1122 }
1123
1124 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
1125 {
1126 #ifdef FIPS_MODULE
1127 return NULL;
1128 #else
1129 return DSO_get_filename(prov->module);
1130 #endif
1131 }
1132
1133 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
1134 {
1135 #ifdef FIPS_MODULE
1136 return NULL;
1137 #else
1138 /* FIXME: Ensure it's a full path */
1139 return DSO_get_filename(prov->module);
1140 #endif
1141 }
1142
1143 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
1144 {
1145 if (prov != NULL)
1146 return prov->provctx;
1147
1148 return NULL;
1149 }
1150
1151 const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
1152 {
1153 if (prov != NULL)
1154 return prov->dispatch;
1155
1156 return NULL;
1157 }
1158
1159 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
1160 {
1161 return prov != NULL ? prov->libctx : NULL;
1162 }
1163
1164 /* Wrappers around calls to the provider */
1165 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
1166 {
1167 if (prov->teardown != NULL
1168 #ifndef FIPS_MODULE
1169 && !prov->ischild
1170 #endif
1171 )
1172 prov->teardown(prov->provctx);
1173 }
1174
1175 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1176 {
1177 return prov->gettable_params == NULL
1178 ? NULL : prov->gettable_params(prov->provctx);
1179 }
1180
1181 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1182 {
1183 return prov->get_params == NULL
1184 ? 0 : prov->get_params(prov->provctx, params);
1185 }
1186
1187 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1188 {
1189 int ret;
1190
1191 if (prov->self_test == NULL)
1192 return 1;
1193 ret = prov->self_test(prov->provctx);
1194 if (ret == 0)
1195 (void)provider_flush_store_cache(prov);
1196 return ret;
1197 }
1198
1199 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1200 const char *capability,
1201 OSSL_CALLBACK *cb,
1202 void *arg)
1203 {
1204 return prov->get_capabilities == NULL
1205 ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
1206 }
1207
1208 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1209 int operation_id,
1210 int *no_cache)
1211 {
1212 const OSSL_ALGORITHM *res;
1213
1214 if (prov->query_operation == NULL)
1215 return NULL;
1216 res = prov->query_operation(prov->provctx, operation_id, no_cache);
1217 #if defined(OPENSSL_NO_CACHED_FETCH)
1218 /* Forcing the non-caching of queries */
1219 if (no_cache != NULL)
1220 *no_cache = 1;
1221 #endif
1222 return res;
1223 }
1224
1225 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1226 int operation_id,
1227 const OSSL_ALGORITHM *algs)
1228 {
1229 if (prov->unquery_operation != NULL)
1230 prov->unquery_operation(prov->provctx, operation_id, algs);
1231 }
1232
1233 int ossl_provider_clear_all_operation_bits(OSSL_LIB_CTX *libctx)
1234 {
1235 struct provider_store_st *store;
1236 OSSL_PROVIDER *provider;
1237 int i, num, res = 1;
1238
1239 if ((store = get_provider_store(libctx)) != NULL) {
1240 if (!CRYPTO_THREAD_read_lock(store->lock))
1241 return 0;
1242 num = sk_OSSL_PROVIDER_num(store->providers);
1243 for (i = 0; i < num; i++) {
1244 provider = sk_OSSL_PROVIDER_value(store->providers, i);
1245 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock)) {
1246 res = 0;
1247 continue;
1248 }
1249 if (provider->operation_bits != NULL)
1250 memset(provider->operation_bits, 0,
1251 provider->operation_bits_sz);
1252 CRYPTO_THREAD_unlock(provider->opbits_lock);
1253 }
1254 CRYPTO_THREAD_unlock(store->lock);
1255 return res;
1256 }
1257 return 0;
1258 }
1259
1260 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1261 {
1262 size_t byte = bitnum / 8;
1263 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1264
1265 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1266 return 0;
1267 if (provider->operation_bits_sz <= byte) {
1268 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1269 byte + 1);
1270
1271 if (tmp == NULL) {
1272 CRYPTO_THREAD_unlock(provider->opbits_lock);
1273 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1274 return 0;
1275 }
1276 provider->operation_bits = tmp;
1277 memset(provider->operation_bits + provider->operation_bits_sz,
1278 '\0', byte + 1 - provider->operation_bits_sz);
1279 provider->operation_bits_sz = byte + 1;
1280 }
1281 provider->operation_bits[byte] |= bit;
1282 CRYPTO_THREAD_unlock(provider->opbits_lock);
1283 return 1;
1284 }
1285
1286 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1287 int *result)
1288 {
1289 size_t byte = bitnum / 8;
1290 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1291
1292 if (!ossl_assert(result != NULL)) {
1293 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1294 return 0;
1295 }
1296
1297 *result = 0;
1298 if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1299 return 0;
1300 if (provider->operation_bits_sz > byte)
1301 *result = ((provider->operation_bits[byte] & bit) != 0);
1302 CRYPTO_THREAD_unlock(provider->opbits_lock);
1303 return 1;
1304 }
1305
1306 #ifndef FIPS_MODULE
1307 const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
1308 {
1309 return prov->handle;
1310 }
1311
1312 int ossl_provider_is_child(const OSSL_PROVIDER *prov)
1313 {
1314 return prov->ischild;
1315 }
1316
1317 int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
1318 {
1319 prov->handle = handle;
1320 prov->ischild = 1;
1321
1322 return 1;
1323 }
1324
1325 int ossl_provider_convert_to_child(OSSL_PROVIDER *prov,
1326 const OSSL_CORE_HANDLE *handle,
1327 OSSL_provider_init_fn *init_function)
1328 {
1329 int flush = 0;
1330
1331 if (!CRYPTO_THREAD_write_lock(prov->store->lock))
1332 return 0;
1333 if (!CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1334 CRYPTO_THREAD_unlock(prov->store->lock);
1335 return 0;
1336 }
1337 /*
1338 * The provider could be in one of three states: (1) Already a child,
1339 * (2) Not a child (but eligible to be one), or (3) Not a child (not
1340 * eligible to be one).
1341 */
1342 if (prov->flag_couldbechild) {
1343 ossl_provider_set_child(prov, handle);
1344 prov->init_function = init_function;
1345 }
1346 if (prov->ischild && provider_activate(prov, 0, 0)) {
1347 flush = 1;
1348 prov->store->use_fallbacks = 0;
1349 }
1350
1351 CRYPTO_THREAD_unlock(prov->flag_lock);
1352 CRYPTO_THREAD_unlock(prov->store->lock);
1353
1354 if (flush)
1355 provider_flush_store_cache(prov);
1356
1357 /*
1358 * We report success whether or not the provider was eligible for conversion
1359 * to a child. If its not elgibile then it has already been loaded as a non
1360 * child provider and we should keep it like that.
1361 */
1362 return 1;
1363 }
1364
1365 int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
1366 {
1367 #ifndef FIPS_MODULE
1368 struct provider_store_st *store = NULL;
1369 int i, max;
1370 OSSL_PROVIDER_CHILD_CB *child_cb;
1371
1372 if ((store = get_provider_store(libctx)) == NULL)
1373 return 0;
1374
1375 if (!CRYPTO_THREAD_read_lock(store->lock))
1376 return 0;
1377
1378 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1379 for (i = 0; i < max; i++) {
1380 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1381 child_cb->global_props_cb(props, child_cb->cbdata);
1382 }
1383
1384 CRYPTO_THREAD_unlock(store->lock);
1385 #endif
1386 return 1;
1387 }
1388
1389 static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
1390 int (*create_cb)(
1391 const OSSL_CORE_HANDLE *provider,
1392 void *cbdata),
1393 int (*remove_cb)(
1394 const OSSL_CORE_HANDLE *provider,
1395 void *cbdata),
1396 int (*global_props_cb)(
1397 const char *props,
1398 void *cbdata),
1399 void *cbdata)
1400 {
1401 /*
1402 * This is really an OSSL_PROVIDER that we created and cast to
1403 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1404 */
1405 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1406 OSSL_PROVIDER *prov;
1407 OSSL_LIB_CTX *libctx = thisprov->libctx;
1408 struct provider_store_st *store = NULL;
1409 int ret = 0, i, max;
1410 OSSL_PROVIDER_CHILD_CB *child_cb;
1411 char *propsstr = NULL;
1412
1413 if ((store = get_provider_store(libctx)) == NULL)
1414 return 0;
1415
1416 child_cb = OPENSSL_malloc(sizeof(*child_cb));
1417 if (child_cb == NULL)
1418 return 0;
1419 child_cb->prov = thisprov;
1420 child_cb->create_cb = create_cb;
1421 child_cb->remove_cb = remove_cb;
1422 child_cb->global_props_cb = global_props_cb;
1423 child_cb->cbdata = cbdata;
1424
1425 if (!CRYPTO_THREAD_write_lock(store->lock)) {
1426 OPENSSL_free(child_cb);
1427 return 0;
1428 }
1429 propsstr = evp_get_global_properties_str(libctx, 0);
1430
1431 if (propsstr != NULL) {
1432 global_props_cb(propsstr, cbdata);
1433 OPENSSL_free(propsstr);
1434 }
1435 max = sk_OSSL_PROVIDER_num(store->providers);
1436 for (i = 0; i < max; i++) {
1437 prov = sk_OSSL_PROVIDER_value(store->providers, i);
1438 /*
1439 * We require register_child_cb to be called during a provider init
1440 * function. The currently initing provider will never be activated yet
1441 * and we we should not attempt to aquire the flag_lock for it.
1442 */
1443 if (prov == thisprov)
1444 continue;
1445 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1446 break;
1447 /*
1448 * We hold the lock while calling the user callback. This means that the
1449 * user callback must be short and simple and not do anything likely to
1450 * cause a deadlock.
1451 */
1452 if (prov->flag_activated
1453 && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
1454 break;
1455 CRYPTO_THREAD_unlock(prov->flag_lock);
1456 }
1457 if (i == max) {
1458 /* Success */
1459 ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
1460 }
1461 if (i != max || ret <= 0) {
1462 /* Failed during creation. Remove everything we just added */
1463 for (; i >= 0; i--) {
1464 prov = sk_OSSL_PROVIDER_value(store->providers, i);
1465 remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
1466 }
1467 OPENSSL_free(child_cb);
1468 ret = 0;
1469 }
1470 CRYPTO_THREAD_unlock(store->lock);
1471
1472 return ret;
1473 }
1474
1475 static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
1476 {
1477 /*
1478 * This is really an OSSL_PROVIDER that we created and cast to
1479 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1480 */
1481 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1482 OSSL_LIB_CTX *libctx = thisprov->libctx;
1483 struct provider_store_st *store = NULL;
1484 int i, max;
1485 OSSL_PROVIDER_CHILD_CB *child_cb;
1486
1487 if ((store = get_provider_store(libctx)) == NULL)
1488 return;
1489
1490 if (!CRYPTO_THREAD_write_lock(store->lock))
1491 return;
1492 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1493 for (i = 0; i < max; i++) {
1494 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1495 if (child_cb->prov == thisprov) {
1496 /* Found an entry */
1497 sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
1498 OPENSSL_free(child_cb);
1499 break;
1500 }
1501 }
1502 CRYPTO_THREAD_unlock(store->lock);
1503 }
1504 #endif
1505
1506 /*-
1507 * Core functions for the provider
1508 * ===============================
1509 *
1510 * This is the set of functions that the core makes available to the provider
1511 */
1512
1513 /*
1514 * This returns a list of Provider Object parameters with their types, for
1515 * discovery. We do not expect that many providers will use this, but one
1516 * never knows.
1517 */
1518 static const OSSL_PARAM param_types[] = {
1519 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1520 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1521 NULL, 0),
1522 #ifndef FIPS_MODULE
1523 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1524 NULL, 0),
1525 #endif
1526 OSSL_PARAM_END
1527 };
1528
1529 /*
1530 * Forward declare all the functions that are provided aa dispatch.
1531 * This ensures that the compiler will complain if they aren't defined
1532 * with the correct signature.
1533 */
1534 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1535 static OSSL_FUNC_core_get_params_fn core_get_params;
1536 static OSSL_FUNC_core_thread_start_fn core_thread_start;
1537 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1538 #ifndef FIPS_MODULE
1539 static OSSL_FUNC_core_new_error_fn core_new_error;
1540 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1541 static OSSL_FUNC_core_vset_error_fn core_vset_error;
1542 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1543 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1544 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
1545 #endif
1546
1547 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1548 {
1549 return param_types;
1550 }
1551
1552 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1553 {
1554 int i;
1555 OSSL_PARAM *p;
1556 /*
1557 * We created this object originally and we know it is actually an
1558 * OSSL_PROVIDER *, so the cast is safe
1559 */
1560 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1561
1562 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1563 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1564 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1565 OSSL_PARAM_set_utf8_ptr(p, prov->name);
1566
1567 #ifndef FIPS_MODULE
1568 if ((p = OSSL_PARAM_locate(params,
1569 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
1570 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1571 #endif
1572
1573 if (prov->parameters == NULL)
1574 return 1;
1575
1576 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
1577 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
1578
1579 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
1580 OSSL_PARAM_set_utf8_ptr(p, pair->value);
1581 }
1582 return 1;
1583 }
1584
1585 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
1586 {
1587 /*
1588 * We created this object originally and we know it is actually an
1589 * OSSL_PROVIDER *, so the cast is safe
1590 */
1591 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1592
1593 /*
1594 * Using ossl_provider_libctx would be wrong as that returns
1595 * NULL for |prov| == NULL and NULL libctx has a special meaning
1596 * that does not apply here. Here |prov| == NULL can happen only in
1597 * case of a coding error.
1598 */
1599 assert(prov != NULL);
1600 return (OPENSSL_CORE_CTX *)prov->libctx;
1601 }
1602
1603 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
1604 OSSL_thread_stop_handler_fn handfn,
1605 void *arg)
1606 {
1607 /*
1608 * We created this object originally and we know it is actually an
1609 * OSSL_PROVIDER *, so the cast is safe
1610 */
1611 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1612
1613 return ossl_init_thread_start(prov, arg, handfn);
1614 }
1615
1616 /*
1617 * The FIPS module inner provider doesn't implement these. They aren't
1618 * needed there, since the FIPS module upcalls are always the outer provider
1619 * ones.
1620 */
1621 #ifndef FIPS_MODULE
1622 /*
1623 * These error functions should use |handle| to select the proper
1624 * library context to report in the correct error stack if error
1625 * stacks become tied to the library context.
1626 * We cannot currently do that since there's no support for it in the
1627 * ERR subsystem.
1628 */
1629 static void core_new_error(const OSSL_CORE_HANDLE *handle)
1630 {
1631 ERR_new();
1632 }
1633
1634 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
1635 const char *file, int line, const char *func)
1636 {
1637 ERR_set_debug(file, line, func);
1638 }
1639
1640 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
1641 uint32_t reason, const char *fmt, va_list args)
1642 {
1643 /*
1644 * We created this object originally and we know it is actually an
1645 * OSSL_PROVIDER *, so the cast is safe
1646 */
1647 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1648
1649 /*
1650 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1651 * error and will be treated as such. Otherwise, it's a new style
1652 * provider error and will be treated as such.
1653 */
1654 if (ERR_GET_LIB(reason) != 0) {
1655 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
1656 } else {
1657 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
1658 }
1659 }
1660
1661 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
1662 {
1663 return ERR_set_mark();
1664 }
1665
1666 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
1667 {
1668 return ERR_clear_last_mark();
1669 }
1670
1671 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
1672 {
1673 return ERR_pop_to_mark();
1674 }
1675 #endif /* FIPS_MODULE */
1676
1677 /*
1678 * Functions provided by the core.
1679 */
1680 static const OSSL_DISPATCH core_dispatch_[] = {
1681 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
1682 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
1683 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
1684 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
1685 #ifndef FIPS_MODULE
1686 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
1687 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
1688 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
1689 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
1690 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
1691 (void (*)(void))core_clear_last_error_mark },
1692 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
1693 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
1694 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
1695 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
1696 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
1697 { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
1698 { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
1699 { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
1700 { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
1701 { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
1702 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
1703 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
1704 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
1705 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
1706 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
1707 { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
1708 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
1709 #endif
1710 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
1711 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
1712 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
1713 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
1714 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
1715 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
1716 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
1717 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
1718 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
1719 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
1720 (void (*)(void))CRYPTO_secure_clear_free },
1721 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
1722 (void (*)(void))CRYPTO_secure_allocated },
1723 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
1724 #ifndef FIPS_MODULE
1725 { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
1726 (void (*)(void))ossl_provider_register_child_cb },
1727 { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
1728 (void (*)(void))ossl_provider_deregister_child_cb },
1729 { OSSL_FUNC_PROVIDER_NAME,
1730 (void (*)(void))OSSL_PROVIDER_get0_name },
1731 { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
1732 (void (*)(void))OSSL_PROVIDER_get0_provider_ctx },
1733 { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
1734 (void (*)(void))OSSL_PROVIDER_get0_dispatch },
1735 { OSSL_FUNC_PROVIDER_UP_REF,
1736 (void (*)(void))provider_up_ref_intern },
1737 { OSSL_FUNC_PROVIDER_FREE,
1738 (void (*)(void))provider_free_intern },
1739 #endif
1740 { 0, NULL }
1741 };
1742 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;