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