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