]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/provider_core.c
Prepare EVP_MAC infrastructure for moving all MACs to providers
[thirdparty/openssl.git] / crypto / provider_core.c
CommitLineData
4c2883a9
RL
1/*
2 * Copyright 2019 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 <openssl/core.h>
11#include <openssl/core_numbers.h>
ac1055ef 12#include <openssl/params.h>
4c2883a9 13#include <openssl/opensslv.h>
da747958 14#include "internal/cryptlib_int.h"
c41f3ae0 15#include "internal/nelem.h"
4c2883a9
RL
16#include "internal/thread_once.h"
17#include "internal/provider.h"
18#include "internal/refcount.h"
c41f3ae0
RL
19#include "provider_local.h"
20
21static OSSL_PROVIDER *provider_new(const char *name,
22 OSSL_provider_init_fn *init_function);
4c2883a9
RL
23
24/*-
25 * Provider Object structure
26 * =========================
27 */
28
ac1055ef
RL
29typedef struct {
30 char *name;
31 char *value;
32} INFOPAIR;
33DEFINE_STACK_OF(INFOPAIR)
34
4c2883a9
RL
35struct provider_store_st; /* Forward declaration */
36
37struct ossl_provider_st {
38 /* Flag bits */
39 unsigned int flag_initialized:1;
e55008a9 40 unsigned int flag_fallback:1;
4c2883a9
RL
41
42 /* OpenSSL library side data */
43 CRYPTO_REF_COUNT refcnt;
085bef9f 44 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
4c2883a9 45 char *name;
ac1055ef 46 char *path;
4c2883a9
RL
47 DSO *module;
48 OSSL_provider_init_fn *init_function;
ac1055ef 49 STACK_OF(INFOPAIR) *parameters;
e7706e63 50 OPENSSL_CTX *libctx; /* The library context this instance is in */
e55008a9 51 struct provider_store_st *store; /* The store this instance belongs to */
6592ab81
RL
52#ifndef FIPS_MODE
53 /*
54 * In the FIPS module inner provider, this isn't needed, since the
55 * error upcalls are always direct calls to the outer provider.
56 */
6ebc2f56 57 int error_lib; /* ERR library number, one for each provider */
6592ab81 58# ifndef OPENSSL_NO_ERR
6ebc2f56 59 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
6592ab81 60# endif
6ebc2f56 61#endif
4c2883a9
RL
62
63 /* Provider side functions */
64 OSSL_provider_teardown_fn *teardown;
dca97d00 65 OSSL_provider_gettable_params_fn *gettable_params;
4c2883a9 66 OSSL_provider_get_params_fn *get_params;
099bd339 67 OSSL_provider_query_operation_fn *query_operation;
a39eb840
RL
68
69 /* Provider side data */
70 void *provctx;
4c2883a9
RL
71};
72DEFINE_STACK_OF(OSSL_PROVIDER)
73
74static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
75 const OSSL_PROVIDER * const *b)
76{
77 return strcmp((*a)->name, (*b)->name);
78}
79
80/*-
81 * Provider Object store
82 * =====================
83 *
84 * The Provider Object store is a library context object, and therefore needs
85 * an index.
86 */
87
88struct provider_store_st {
89 STACK_OF(OSSL_PROVIDER) *providers;
90 CRYPTO_RWLOCK *lock;
e55008a9 91 unsigned int use_fallbacks:1;
4c2883a9 92};
4c2883a9
RL
93
94static void provider_store_free(void *vstore)
95{
96 struct provider_store_st *store = vstore;
97
98 if (store == NULL)
99 return;
100 sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free);
101 CRYPTO_THREAD_lock_free(store->lock);
102 OPENSSL_free(store);
103}
104
1aedc35f 105static void *provider_store_new(OPENSSL_CTX *ctx)
4c2883a9
RL
106{
107 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
c41f3ae0 108 const struct predefined_providers_st *p = NULL;
4c2883a9
RL
109
110 if (store == NULL
111 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
112 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
113 provider_store_free(store);
e55008a9 114 return NULL;
4c2883a9 115 }
e55008a9 116 store->use_fallbacks = 1;
c41f3ae0
RL
117
118 for (p = predefined_providers; p->name != NULL; p++) {
119 OSSL_PROVIDER *prov = NULL;
120
121 /*
122 * We use the internal constructor directly here,
123 * otherwise we get a call loop
124 */
125 prov = provider_new(p->name, p->init);
126
127 if (prov == NULL
128 || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
129 ossl_provider_free(prov);
130 provider_store_free(store);
131 CRYPTOerr(CRYPTO_F_PROVIDER_STORE_NEW, ERR_R_INTERNAL_ERROR);
132 return NULL;
133 }
e7706e63 134 prov->libctx = ctx;
c41f3ae0 135 prov->store = store;
6592ab81 136#ifndef FIPS_MODE
6ebc2f56 137 prov->error_lib = ERR_get_next_error_library();
6592ab81 138#endif
c41f3ae0
RL
139 if(p->is_fallback)
140 ossl_provider_set_fallback(prov);
141 }
142
4c2883a9
RL
143 return store;
144}
145
146static const OPENSSL_CTX_METHOD provider_store_method = {
147 provider_store_new,
148 provider_store_free,
149};
150
4c2883a9
RL
151static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
152{
153 struct provider_store_st *store = NULL;
154
1aedc35f
MC
155 store = openssl_ctx_get_data(libctx, OPENSSL_CTX_PROVIDER_STORE_INDEX,
156 &provider_store_method);
4c2883a9
RL
157 if (store == NULL)
158 CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
159 return store;
160}
161
29dc6e00
MC
162OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name,
163 int noconfig)
4c2883a9
RL
164{
165 struct provider_store_st *store = NULL;
166 OSSL_PROVIDER *prov = NULL;
167
168 if ((store = get_provider_store(libctx)) != NULL) {
169 OSSL_PROVIDER tmpl = { 0, };
170 int i;
171
29dc6e00
MC
172#ifndef FIPS_MODE
173 /*
174 * Make sure any providers are loaded from config before we try to find
175 * them.
176 */
177 if (!noconfig)
178 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
179#endif
180
4c2883a9
RL
181 tmpl.name = (char *)name;
182 CRYPTO_THREAD_write_lock(store->lock);
183 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
184 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
7c95390e 185 || !ossl_provider_up_ref(prov))
4c2883a9
RL
186 prov = NULL;
187 CRYPTO_THREAD_unlock(store->lock);
188 }
189
190 return prov;
191}
192
c41f3ae0
RL
193/*-
194 * Provider Object methods
195 * =======================
196 */
197
198static OSSL_PROVIDER *provider_new(const char *name,
199 OSSL_provider_init_fn *init_function)
200{
201 OSSL_PROVIDER *prov = NULL;
202
203 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
204#ifndef HAVE_ATOMICS
205 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
206#endif
7c95390e 207 || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
c41f3ae0
RL
208 || (prov->name = OPENSSL_strdup(name)) == NULL) {
209 ossl_provider_free(prov);
210 CRYPTOerr(CRYPTO_F_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
211 return NULL;
212 }
213
214 prov->init_function = init_function;
215 return prov;
216}
217
7c95390e 218int ossl_provider_up_ref(OSSL_PROVIDER *prov)
c41f3ae0
RL
219{
220 int ref = 0;
221
ad14e8e5
SL
222 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
223 return 0;
c41f3ae0
RL
224 return ref;
225}
226
4c2883a9 227OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
29dc6e00
MC
228 OSSL_provider_init_fn *init_function,
229 int noconfig)
4c2883a9
RL
230{
231 struct provider_store_st *store = NULL;
232 OSSL_PROVIDER *prov = NULL;
233
234 if ((store = get_provider_store(libctx)) == NULL)
235 return NULL;
236
29dc6e00
MC
237 if ((prov = ossl_provider_find(libctx, name,
238 noconfig)) != NULL) { /* refcount +1 */
4c2883a9 239 ossl_provider_free(prov); /* refcount -1 */
49c64346
RL
240 ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_ALREADY_EXISTS, NULL,
241 "name=%s", name);
4c2883a9
RL
242 return NULL;
243 }
244
c41f3ae0
RL
245 /* provider_new() generates an error, so no need here */
246 if ((prov = provider_new(name, init_function)) == NULL)
4c2883a9 247 return NULL;
4c2883a9
RL
248
249 CRYPTO_THREAD_write_lock(store->lock);
7c95390e 250 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
4c2883a9
RL
251 ossl_provider_free(prov); /* -1 Reference that was to be returned */
252 prov = NULL;
253 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
254 ossl_provider_free(prov); /* -1 Store reference */
255 ossl_provider_free(prov); /* -1 Reference that was to be returned */
256 prov = NULL;
e55008a9 257 } else {
e7706e63 258 prov->libctx = libctx;
e55008a9 259 prov->store = store;
6592ab81 260#ifndef FIPS_MODE
6ebc2f56 261 prov->error_lib = ERR_get_next_error_library();
6592ab81 262#endif
4c2883a9
RL
263 }
264 CRYPTO_THREAD_unlock(store->lock);
265
266 if (prov == NULL)
267 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
268
269 /*
270 * At this point, the provider is only partially "loaded". To be
271 * fully "loaded", ossl_provider_activate() must also be called.
272 */
273
274 return prov;
275}
276
ac1055ef
RL
277static void free_infopair(INFOPAIR *pair)
278{
279 OPENSSL_free(pair->name);
280 OPENSSL_free(pair->value);
281 OPENSSL_free(pair);
282}
283
4c2883a9
RL
284void ossl_provider_free(OSSL_PROVIDER *prov)
285{
286 if (prov != NULL) {
287 int ref = 0;
288
085bef9f 289 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
4c2883a9
RL
290
291 /*
e55008a9
RL
292 * When the refcount drops below two, the store is the only
293 * possible reference, or it has already been taken away from
294 * the store (this may happen if a provider was activated
295 * because it's a fallback, but isn't currently used)
4c2883a9
RL
296 * When that happens, the provider is inactivated.
297 */
e55008a9 298 if (ref < 2 && prov->flag_initialized) {
6913f5fe
MC
299#ifndef FIPS_MODE
300 ossl_init_thread_deregister(prov);
301#endif
4c2883a9 302 if (prov->teardown != NULL)
a39eb840 303 prov->teardown(prov->provctx);
6ebc2f56
RL
304#ifndef OPENSSL_NO_ERR
305# ifndef FIPS_MODE
306 if (prov->error_strings != NULL) {
307 ERR_unload_strings(prov->error_lib, prov->error_strings);
308 OPENSSL_free(prov->error_strings);
309 prov->error_strings = NULL;
310 }
311# endif
312#endif
4c2883a9
RL
313 prov->flag_initialized = 0;
314 }
315
316 /*
317 * When the refcount drops to zero, it has been taken out of
318 * the store. All we have to do here is clean it out.
319 */
320 if (ref == 0) {
3593266d 321#ifndef FIPS_MODE
4c2883a9 322 DSO_free(prov->module);
3593266d 323#endif
4c2883a9 324 OPENSSL_free(prov->name);
ac1055ef
RL
325 OPENSSL_free(prov->path);
326 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
085bef9f
RL
327#ifndef HAVE_ATOMICS
328 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
329#endif
4c2883a9
RL
330 OPENSSL_free(prov);
331 }
332 }
333}
334
ac1055ef
RL
335/* Setters */
336int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
337{
338 OPENSSL_free(prov->path);
339 if (module_path == NULL)
340 return 1;
341 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
342 return 1;
343 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_SET_MODULE_PATH, ERR_R_MALLOC_FAILURE);
344 return 0;
345}
346
347int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
348 const char *name, const char *value)
349{
350 INFOPAIR *pair = NULL;
351
352 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
353 && (prov->parameters != NULL
354 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
355 && (pair->name = OPENSSL_strdup(name)) != NULL
356 && (pair->value = OPENSSL_strdup(value)) != NULL
357 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
358 return 1;
359
360 if (pair != NULL) {
361 OPENSSL_free(pair->name);
362 OPENSSL_free(pair->value);
363 OPENSSL_free(pair);
364 }
365 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_PARAMETER, ERR_R_MALLOC_FAILURE);
366 return 0;
367}
368
4c2883a9
RL
369/*
370 * Provider activation.
371 *
372 * What "activation" means depends on the provider form; for built in
373 * providers (in the library or the application alike), the provider
374 * can already be considered to be loaded, all that's needed is to
375 * initialize it. However, for dynamically loadable provider modules,
376 * we must first load that module.
377 *
378 * Built in modules are distinguished from dynamically loaded modules
379 * with an already assigned init function.
380 */
381static const OSSL_DISPATCH *core_dispatch; /* Define further down */
382
e55008a9
RL
383/*
384 * Internal version that doesn't affect the store flags, and thereby avoid
385 * locking. Direct callers must remember to set the store flags when
e7706e63 386 * appropriate.
e55008a9 387 */
e7706e63 388static int provider_activate(OSSL_PROVIDER *prov)
4c2883a9
RL
389{
390 const OSSL_DISPATCH *provider_dispatch = NULL;
6ebc2f56 391#ifndef OPENSSL_NO_ERR
6592ab81 392# ifndef FIPS_MODE
6ebc2f56 393 OSSL_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
6592ab81 394# endif
6ebc2f56 395#endif
4c2883a9
RL
396
397 if (prov->flag_initialized)
398 return 1;
399
400 /*
401 * If the init function isn't set, it indicates that this provider is
402 * a loadable module.
403 */
404 if (prov->init_function == NULL) {
3593266d
MC
405#ifdef FIPS_MODE
406 return 0;
407#else
4c2883a9 408 if (prov->module == NULL) {
ac1055ef
RL
409 char *allocated_path = NULL;
410 const char *module_path = NULL;
411 char *merged_path = NULL;
4c2883a9
RL
412 const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES");
413
414 if ((prov->module = DSO_new()) == NULL) {
415 /* DSO_new() generates an error already */
416 return 0;
417 }
418
419 if (load_dir == NULL)
420 load_dir = MODULESDIR;
421
422 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
423 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
ac1055ef
RL
424
425 module_path = prov->path;
426 if (module_path == NULL)
427 module_path = allocated_path =
428 DSO_convert_filename(prov->module, prov->name);
429 if (module_path != NULL)
430 merged_path = DSO_merge(prov->module, module_path, load_dir);
431
432 if (merged_path == NULL
433 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
4c2883a9
RL
434 DSO_free(prov->module);
435 prov->module = NULL;
436 }
437
ac1055ef
RL
438 OPENSSL_free(merged_path);
439 OPENSSL_free(allocated_path);
4c2883a9
RL
440 }
441
442 if (prov->module != NULL)
443 prov->init_function = (OSSL_provider_init_fn *)
444 DSO_bind_func(prov->module, "OSSL_provider_init");
3593266d 445#endif
4c2883a9
RL
446 }
447
e7706e63 448 /* Call the initialise function for the provider. */
4c2883a9 449 if (prov->init_function == NULL
a39eb840
RL
450 || !prov->init_function(prov, core_dispatch, &provider_dispatch,
451 &prov->provctx)) {
49c64346
RL
452 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL, NULL,
453 "name=%s", prov->name);
3593266d 454#ifndef FIPS_MODE
4c2883a9
RL
455 DSO_free(prov->module);
456 prov->module = NULL;
3593266d 457#endif
4c2883a9
RL
458 return 0;
459 }
460
461 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
462 switch (provider_dispatch->function_id) {
463 case OSSL_FUNC_PROVIDER_TEARDOWN:
464 prov->teardown =
465 OSSL_get_provider_teardown(provider_dispatch);
466 break;
dca97d00
RL
467 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
468 prov->gettable_params =
469 OSSL_get_provider_gettable_params(provider_dispatch);
4c2883a9
RL
470 break;
471 case OSSL_FUNC_PROVIDER_GET_PARAMS:
472 prov->get_params =
473 OSSL_get_provider_get_params(provider_dispatch);
474 break;
099bd339
RL
475 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
476 prov->query_operation =
477 OSSL_get_provider_query_operation(provider_dispatch);
478 break;
6ebc2f56 479#ifndef OPENSSL_NO_ERR
6592ab81 480# ifndef FIPS_MODE
6ebc2f56
RL
481 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
482 p_get_reason_strings =
483 OSSL_get_provider_get_reason_strings(provider_dispatch);
484 break;
6592ab81 485# endif
6ebc2f56
RL
486#endif
487 }
488 }
489
490#ifndef OPENSSL_NO_ERR
6592ab81 491# ifndef FIPS_MODE
6ebc2f56
RL
492 if (p_get_reason_strings != NULL) {
493 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
494 size_t cnt, cnt2;
495
496 /*
497 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
498 * although they are essentially the same type.
499 * Furthermore, ERR_load_strings() patches the array's error number
500 * with the error library number, so we need to make a copy of that
501 * array either way.
502 */
503 cnt = 1; /* One for the terminating item */
504 while (reasonstrings[cnt].id != 0) {
505 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
506 return 0;
507 cnt++;
508 }
509
510 /* Allocate one extra item for the "library" name */
511 prov->error_strings =
512 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
513 if (prov->error_strings == NULL)
514 return 0;
515
516 /*
517 * Set the "library" name.
518 */
519 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
520 prov->error_strings[0].string = prov->name;
521 /*
522 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
523 * 1..cnt.
524 */
525 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
526 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
527 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
4c2883a9 528 }
6ebc2f56
RL
529
530 ERR_load_strings(prov->error_lib, prov->error_strings);
4c2883a9 531 }
6592ab81 532# endif
6ebc2f56 533#endif
4c2883a9
RL
534
535 /* With this flag set, this provider has become fully "loaded". */
536 prov->flag_initialized = 1;
537
538 return 1;
539}
540
e55008a9
RL
541int ossl_provider_activate(OSSL_PROVIDER *prov)
542{
e7706e63 543 if (provider_activate(prov)) {
e55008a9
RL
544 CRYPTO_THREAD_write_lock(prov->store->lock);
545 prov->store->use_fallbacks = 0;
546 CRYPTO_THREAD_unlock(prov->store->lock);
547 return 1;
548 }
549
550 return 0;
551}
552
a39eb840
RL
553void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
554{
555 return prov->provctx;
556}
557
e55008a9
RL
558
559static int provider_forall_loaded(struct provider_store_st *store,
560 int *found_activated,
561 int (*cb)(OSSL_PROVIDER *provider,
562 void *cbdata),
563 void *cbdata)
564{
565 int i;
566 int ret = 1;
29dc6e00
MC
567 int num_provs;
568
29dc6e00 569 num_provs = sk_OSSL_PROVIDER_num(store->providers);
e55008a9
RL
570
571 if (found_activated != NULL)
572 *found_activated = 0;
573 for (i = 0; i < num_provs; i++) {
574 OSSL_PROVIDER *prov =
575 sk_OSSL_PROVIDER_value(store->providers, i);
576
577 if (prov->flag_initialized) {
578 if (found_activated != NULL)
579 *found_activated = 1;
580 if (!(ret = cb(prov, cbdata)))
581 break;
582 }
583 }
584
585 return ret;
586}
587
36f5ec55
RL
588/*
589 * This function only does something once when store->use_fallbacks == 1,
590 * and then sets store->use_fallbacks = 0, so the second call and so on is
591 * effectively a no-op.
592 */
593static void provider_activate_fallbacks(struct provider_store_st *store)
594{
595 if (store->use_fallbacks) {
596 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
597 int activated_fallback_count = 0;
598 int i;
599
600 for (i = 0; i < num_provs; i++) {
601 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
602
603 /*
604 * Note that we don't care if the activation succeeds or not.
605 * If it doesn't succeed, then any attempt to use any of the
606 * fallback providers will fail anyway.
607 */
608 if (prov->flag_fallback) {
609 activated_fallback_count++;
610 provider_activate(prov);
611 }
612 }
613
614 /*
615 * We assume that all fallbacks have been added to the store before
616 * any fallback is activated.
617 * TODO: We may have to reconsider this, IF we find ourselves adding
618 * fallbacks after any previous fallback has been activated.
619 */
620 if (activated_fallback_count > 0)
621 store->use_fallbacks = 0;
622 }
623}
624
85e2417c
RL
625int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
626 int (*cb)(OSSL_PROVIDER *provider,
627 void *cbdata),
628 void *cbdata)
629{
630 int ret = 1;
85e2417c
RL
631 struct provider_store_st *store = get_provider_store(ctx);
632
5c5cdcd8
MC
633#ifndef FIPS_MODE
634 /*
635 * Make sure any providers are loaded from config before we try to use
636 * them.
637 */
638 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
639#endif
640
85e2417c
RL
641 if (store != NULL) {
642 CRYPTO_THREAD_read_lock(store->lock);
36f5ec55
RL
643
644 provider_activate_fallbacks(store);
85e2417c 645
e55008a9 646 /*
36f5ec55 647 * Now, we sweep through all providers
e55008a9 648 */
36f5ec55 649 ret = provider_forall_loaded(store, NULL, cb, cbdata);
e55008a9 650
85e2417c
RL
651 CRYPTO_THREAD_unlock(store->lock);
652 }
653
654 return ret;
655}
656
36f5ec55
RL
657int ossl_provider_available(OSSL_PROVIDER *prov)
658{
659 if (prov != NULL) {
660 CRYPTO_THREAD_read_lock(prov->store->lock);
661 provider_activate_fallbacks(prov->store);
662 CRYPTO_THREAD_unlock(prov->store->lock);
663
664 return prov->flag_initialized;
665 }
666 return 0;
667}
668
e55008a9
RL
669/* Setters of Provider Object data */
670int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
671{
672 if (prov == NULL)
673 return 0;
674
675 prov->flag_fallback = 1;
676 return 1;
677}
678
4c2883a9 679/* Getters of Provider Object data */
24626a47 680const char *ossl_provider_name(const OSSL_PROVIDER *prov)
4c2883a9
RL
681{
682 return prov->name;
683}
684
24626a47 685const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
4c2883a9
RL
686{
687 return prov->module;
688}
689
24626a47 690const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
4c2883a9 691{
3593266d
MC
692#ifdef FIPS_MODE
693 return NULL;
694#else
4c2883a9 695 return DSO_get_filename(prov->module);
3593266d 696#endif
4c2883a9
RL
697}
698
24626a47 699const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
4c2883a9 700{
3593266d
MC
701#ifdef FIPS_MODE
702 return NULL;
703#else
4c2883a9
RL
704 /* FIXME: Ensure it's a full path */
705 return DSO_get_filename(prov->module);
3593266d 706#endif
4c2883a9
RL
707}
708
e74bd290
RL
709OPENSSL_CTX *ossl_provider_library_context(const OSSL_PROVIDER *prov)
710{
711 return prov->libctx;
712}
713
4c2883a9
RL
714/* Wrappers around calls to the provider */
715void ossl_provider_teardown(const OSSL_PROVIDER *prov)
716{
717 if (prov->teardown != NULL)
a39eb840 718 prov->teardown(prov->provctx);
4c2883a9
RL
719}
720
dca97d00 721const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
4c2883a9 722{
dca97d00
RL
723 return prov->gettable_params == NULL
724 ? NULL : prov->gettable_params(prov->provctx);
4c2883a9
RL
725}
726
4e7991b4 727int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
4c2883a9 728{
a39eb840
RL
729 return prov->get_params == NULL
730 ? 0 : prov->get_params(prov->provctx, params);
4c2883a9
RL
731}
732
099bd339
RL
733
734const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
735 int operation_id,
736 int *no_cache)
737{
a39eb840 738 return prov->query_operation(prov->provctx, operation_id, no_cache);
099bd339
RL
739}
740
4c2883a9
RL
741/*-
742 * Core functions for the provider
743 * ===============================
744 *
745 * This is the set of functions that the core makes available to the provider
746 */
747
748/*
749 * This returns a list of Provider Object parameters with their types, for
750 * discovery. We do not expect that many providers will use this, but one
751 * never knows.
752 */
26175013
RL
753static const OSSL_PARAM param_types[] = {
754 OSSL_PARAM_DEFN("openssl-verstion", OSSL_PARAM_UTF8_PTR, NULL, 0),
755 OSSL_PARAM_DEFN("provider-name", OSSL_PARAM_UTF8_PTR, NULL, 0),
756 OSSL_PARAM_END
4c2883a9
RL
757};
758
49c64346
RL
759/*
760 * Forward declare all the functions that are provided aa dispatch.
761 * This ensures that the compiler will complain if they aren't defined
762 * with the correct signature.
763 */
dca97d00 764static OSSL_core_gettable_params_fn core_gettable_params;
49c64346
RL
765static OSSL_core_get_params_fn core_get_params;
766static OSSL_core_thread_start_fn core_thread_start;
767static OSSL_core_get_library_context_fn core_get_libctx;
768#ifndef FIPS_MODE
769static OSSL_core_new_error_fn core_new_error;
770static OSSL_core_set_error_debug_fn core_set_error_debug;
771static OSSL_core_vset_error_fn core_vset_error;
772#endif
773
dca97d00 774static const OSSL_PARAM *core_gettable_params(const OSSL_PROVIDER *prov)
4c2883a9
RL
775{
776 return param_types;
777}
778
4e7991b4 779static int core_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
4c2883a9
RL
780{
781 int i;
4e7991b4 782 OSSL_PARAM *p;
4c2883a9 783
29dc6e00
MC
784#ifndef FIPS_MODE
785 /* Load config before we attempt to read any provider parameters */
786 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
787#endif
788
ac1055ef
RL
789 if ((p = OSSL_PARAM_locate(params, "openssl-version")) != NULL)
790 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
791 if ((p = OSSL_PARAM_locate(params, "provider-name")) != NULL)
792 OSSL_PARAM_set_utf8_ptr(p, prov->name);
793
794 if (prov->parameters == NULL)
795 return 1;
796
797 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
798 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
799
800 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
801 OSSL_PARAM_set_utf8_ptr(p, pair->value);
4c2883a9
RL
802 }
803
804 return 1;
805}
806
e7706e63
RL
807static OPENSSL_CTX *core_get_libctx(const OSSL_PROVIDER *prov)
808{
e74bd290 809 return ossl_provider_library_context(prov);
e7706e63
RL
810}
811
da747958
MC
812static int core_thread_start(const OSSL_PROVIDER *prov,
813 OSSL_thread_stop_handler_fn handfn)
814{
6913f5fe 815 return ossl_init_thread_start(prov, prov->provctx, handfn);
da747958
MC
816}
817
6592ab81
RL
818/*
819 * The FIPS module inner provider doesn't implement these. They aren't
820 * needed there, since the FIPS module upcalls are always the outer provider
821 * ones.
822 */
823#ifndef FIPS_MODE
49c64346
RL
824/*
825 * TODO(3.0) These error functions should use |prov| to select the proper
826 * library context to report in the correct error stack, at least if error
827 * stacks become tied to the library context.
828 * We cannot currently do that since there's no support for it in the
829 * ERR subsystem.
830 */
831static void core_new_error(const OSSL_PROVIDER *prov)
832{
833 ERR_new();
834}
835
836static void core_set_error_debug(const OSSL_PROVIDER *prov,
837 const char *file, int line, const char *func)
838{
839 ERR_set_debug(file, line, func);
840}
841
842static void core_vset_error(const OSSL_PROVIDER *prov,
843 uint32_t reason, const char *fmt, va_list args)
6ebc2f56
RL
844{
845 /*
846 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
847 * error and will be treated as such. Otherwise, it's a new style
848 * provider error and will be treated as such.
849 */
850 if (ERR_GET_LIB(reason) != 0) {
49c64346 851 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
6ebc2f56 852 } else {
49c64346 853 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
6ebc2f56
RL
854 }
855}
6592ab81 856#endif
6ebc2f56 857
b60cba3c
RS
858/*
859 * Functions provided by the core. Blank line separates "families" of related
860 * functions.
861 */
4c2883a9 862static const OSSL_DISPATCH core_dispatch_[] = {
dca97d00 863 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
4c2883a9 864 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
e7706e63 865 { OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT, (void (*)(void))core_get_libctx },
da747958 866 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
6592ab81 867#ifndef FIPS_MODE
49c64346
RL
868 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
869 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
870 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
6592ab81 871#endif
b60cba3c
RS
872
873 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
874 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
b60cba3c
RS
875 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
876 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
877 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
878 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
879 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
880 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
881 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
882 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
883 (void (*)(void))CRYPTO_secure_clear_free },
884 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
885 (void (*)(void))CRYPTO_secure_allocated },
886 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
b60cba3c 887
4c2883a9
RL
888 { 0, NULL }
889};
890static const OSSL_DISPATCH *core_dispatch = core_dispatch_;