]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/provider_core.c
Rework and make DEBUG macros consistent.
[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 * 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);
990 }
991
992 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
993 int (*cb)(OSSL_PROVIDER *provider,
994 void *cbdata),
995 void *cbdata)
996 {
997 int ret = 0, curr, max;
998 struct provider_store_st *store = get_provider_store(ctx);
999 STACK_OF(OSSL_PROVIDER) *provs = NULL;
1000
1001 #ifndef FIPS_MODULE
1002 /*
1003 * Make sure any providers are loaded from config before we try to use
1004 * them.
1005 */
1006 if (ossl_lib_ctx_is_default(ctx))
1007 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
1008 #endif
1009
1010 if (store == NULL)
1011 return 1;
1012 provider_activate_fallbacks(store);
1013
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 */
1018 if (!CRYPTO_THREAD_read_lock(store->lock))
1019 return 0;
1020 provs = sk_OSSL_PROVIDER_dup(store->providers);
1021 if (provs == NULL) {
1022 CRYPTO_THREAD_unlock(store->lock);
1023 return 0;
1024 }
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))
1034 goto err_unlock;
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 */
1044 if (provider_activate(prov, 0, 1) < 0) {
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 }
1055 CRYPTO_THREAD_unlock(store->lock);
1056
1057 /*
1058 * Now, we sweep through all providers not under lock
1059 */
1060 for (curr = 0; curr < max; curr++) {
1061 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1062
1063 if (!cb(prov, cbdata))
1064 goto finish;
1065 }
1066 curr = -1;
1067
1068 ret = 1;
1069 goto finish;
1070
1071 err_unlock:
1072 CRYPTO_THREAD_unlock(store->lock);
1073 finish:
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 }
1085 sk_OSSL_PROVIDER_free(provs);
1086 return ret;
1087 }
1088
1089 int ossl_provider_available(OSSL_PROVIDER *prov)
1090 {
1091 int ret;
1092
1093 if (prov != NULL) {
1094 provider_activate_fallbacks(prov->store);
1095
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;
1101 }
1102 return 0;
1103 }
1104
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
1115 /* Getters of Provider Object data */
1116 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
1117 {
1118 return prov->name;
1119 }
1120
1121 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
1122 {
1123 return prov->module;
1124 }
1125
1126 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
1127 {
1128 #ifdef FIPS_MODULE
1129 return NULL;
1130 #else
1131 return DSO_get_filename(prov->module);
1132 #endif
1133 }
1134
1135 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
1136 {
1137 #ifdef FIPS_MODULE
1138 return NULL;
1139 #else
1140 /* FIXME: Ensure it's a full path */
1141 return DSO_get_filename(prov->module);
1142 #endif
1143 }
1144
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
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
1161 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
1162 {
1163 return prov != NULL ? prov->libctx : NULL;
1164 }
1165
1166 /* Wrappers around calls to the provider */
1167 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
1168 {
1169 if (prov->teardown != NULL
1170 #ifndef FIPS_MODULE
1171 && !prov->ischild
1172 #endif
1173 )
1174 prov->teardown(prov->provctx);
1175 }
1176
1177 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1178 {
1179 return prov->gettable_params == NULL
1180 ? NULL : prov->gettable_params(prov->provctx);
1181 }
1182
1183 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1184 {
1185 return prov->get_params == NULL
1186 ? 0 : prov->get_params(prov->provctx, params);
1187 }
1188
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)
1197 (void)provider_flush_store_cache(prov);
1198 return ret;
1199 }
1200
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
1207 ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
1208 }
1209
1210 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1211 int operation_id,
1212 int *no_cache)
1213 {
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;
1225 }
1226
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
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
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
1267 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1268 return 0;
1269 if (provider->operation_bits_sz <= byte) {
1270 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1271 byte + 1);
1272
1273 if (tmp == NULL) {
1274 CRYPTO_THREAD_unlock(provider->opbits_lock);
1275 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1276 return 0;
1277 }
1278 provider->operation_bits = tmp;
1279 memset(provider->operation_bits + provider->operation_bits_sz,
1280 '\0', byte + 1 - provider->operation_bits_sz);
1281 provider->operation_bits_sz = byte + 1;
1282 }
1283 provider->operation_bits[byte] |= bit;
1284 CRYPTO_THREAD_unlock(provider->opbits_lock);
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;
1300 if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1301 return 0;
1302 if (provider->operation_bits_sz > byte)
1303 *result = ((provider->operation_bits[byte] & bit) != 0);
1304 CRYPTO_THREAD_unlock(provider->opbits_lock);
1305 return 1;
1306 }
1307
1308 #ifndef FIPS_MODULE
1309 const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
1310 {
1311 return prov->handle;
1312 }
1313
1314 int ossl_provider_is_child(const OSSL_PROVIDER *prov)
1315 {
1316 return prov->ischild;
1317 }
1318
1319 int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
1320 {
1321 prov->handle = handle;
1322 prov->ischild = 1;
1323
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))
1334 return 0;
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 }
1352
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 */
1364 return 1;
1365 }
1366
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
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),
1395 int (*remove_cb)(
1396 const OSSL_CORE_HANDLE *provider,
1397 void *cbdata),
1398 int (*global_props_cb)(
1399 const char *props,
1400 void *cbdata),
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;
1413 char *propsstr = NULL;
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;
1424 child_cb->global_props_cb = global_props_cb;
1425 child_cb->cbdata = cbdata;
1426
1427 if (!CRYPTO_THREAD_write_lock(store->lock)) {
1428 OPENSSL_free(child_cb);
1429 return 0;
1430 }
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 }
1437 max = sk_OSSL_PROVIDER_num(store->providers);
1438 for (i = 0; i < max; i++) {
1439 prov = sk_OSSL_PROVIDER_value(store->providers, i);
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;
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
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 */
1520 static const OSSL_PARAM param_types[] = {
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
1528 OSSL_PARAM_END
1529 };
1530
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 */
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;
1539 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1540 #ifndef FIPS_MODULE
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;
1547 #endif
1548
1549 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1550 {
1551 return param_types;
1552 }
1553
1554 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1555 {
1556 int i;
1557 OSSL_PARAM *p;
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;
1563
1564 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1565 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1566 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1567 OSSL_PARAM_set_utf8_ptr(p, prov->name);
1568
1569 #ifndef FIPS_MODULE
1570 if ((p = OSSL_PARAM_locate(params,
1571 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
1572 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1573 #endif
1574
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);
1583 }
1584 return 1;
1585 }
1586
1587 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
1588 {
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
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 */
1601 assert(prov != NULL);
1602 return (OPENSSL_CORE_CTX *)prov->libctx;
1603 }
1604
1605 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
1606 OSSL_thread_stop_handler_fn handfn,
1607 void *arg)
1608 {
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
1615 return ossl_init_thread_start(prov, arg, handfn);
1616 }
1617
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 */
1623 #ifndef FIPS_MODULE
1624 /*
1625 * These error functions should use |handle| to select the proper
1626 * library context to report in the correct error stack if error
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 */
1631 static void core_new_error(const OSSL_CORE_HANDLE *handle)
1632 {
1633 ERR_new();
1634 }
1635
1636 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
1637 const char *file, int line, const char *func)
1638 {
1639 ERR_set_debug(file, line, func);
1640 }
1641
1642 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
1643 uint32_t reason, const char *fmt, va_list args)
1644 {
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
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) {
1657 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
1658 } else {
1659 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
1660 }
1661 }
1662
1663 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
1664 {
1665 return ERR_set_mark();
1666 }
1667
1668 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
1669 {
1670 return ERR_clear_last_mark();
1671 }
1672
1673 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
1674 {
1675 return ERR_pop_to_mark();
1676 }
1677 #endif /* FIPS_MODULE */
1678
1679 /*
1680 * Functions provided by the core.
1681 */
1682 static const OSSL_DISPATCH core_dispatch_[] = {
1683 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
1684 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
1685 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
1686 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
1687 #ifndef FIPS_MODULE
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 },
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 },
1694 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
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 },
1705 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
1706 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
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 },
1711 #endif
1712 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
1713 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
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 },
1726 #ifndef FIPS_MODULE
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,
1732 (void (*)(void))OSSL_PROVIDER_name },
1733 { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
1734 (void (*)(void))OSSL_PROVIDER_get0_provider_ctx },
1735 { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
1736 (void (*)(void))OSSL_PROVIDER_get0_dispatch },
1737 { OSSL_FUNC_PROVIDER_UP_REF,
1738 (void (*)(void))provider_up_ref_intern },
1739 { OSSL_FUNC_PROVIDER_FREE,
1740 (void (*)(void))provider_free_intern },
1741 #endif
1742 { 0, NULL }
1743 };
1744 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;