1 /*
2 * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <assert.h>
11 #include <openssl/core.h>
12 #include <openssl/core_dispatch.h>
13 #include <openssl/core_names.h>
14 #include <openssl/provider.h>
15 #include <openssl/params.h>
16 #include <openssl/opensslv.h>
17 #include "crypto/cryptlib.h"
18 #ifndef FIPS_MODULE
19 #include "crypto/decoder.h" /* ossl_decoder_store_cache_flush */
20 #include "crypto/encoder.h" /* ossl_encoder_store_cache_flush */
21 #include "crypto/store.h" /* ossl_store_loader_store_cache_flush */
22 #endif
23 #include "crypto/evp.h" /* evp_method_store_cache_flush */
24 #include "crypto/rand.h"
25 #include "internal/nelem.h"
26 #include "internal/thread_once.h"
27 #include "internal/provider.h"
28 #include "internal/refcount.h"
29 #include "internal/bio.h"
30 #include "internal/core.h"
31 #include "provider_local.h"
32 #include "crypto/context.h"
33 #ifndef FIPS_MODULE
34 # include <openssl/self_test.h>
35 # include <openssl/indicator.h>
36 #endif
37
38 /*
39 * This file defines and uses a number of different structures:
40 *
41 * OSSL_PROVIDER (provider_st): Used to represent all information related to a
42 * single instance of a provider.
43 *
44 * provider_store_st: Holds information about the collection of providers that
45 * are available within the current library context (OSSL_LIB_CTX). It also
46 * holds configuration information about providers that could be loaded at some
47 * future point.
48 *
49 * OSSL_PROVIDER_CHILD_CB: An instance of this structure holds the callbacks
50 * that have been registered for a child library context and the associated
51 * provider that registered those callbacks.
52 *
53 * Where a child library context exists then it has its own instance of the
54 * provider store. Each provider that exists in the parent provider store, has
55 * an associated child provider in the child library context's provider store.
56 * As providers get activated or deactivated this needs to be mirrored in the
57 * associated child providers.
58 *
59 * LOCKING
60 * =======
61 *
62 * There are a number of different locks used in this file and it is important
63 * to understand how they should be used in order to avoid deadlocks.
64 *
65 * Fields within a structure can often be "write once" on creation, and then
66 * "read many". Creation of a structure is done by a single thread, and
67 * therefore no lock is required for the "write once/read many" fields. It is
68 * safe for multiple threads to read these fields without a lock, because they
69 * will never be changed.
70 *
71 * However some fields may be changed after a structure has been created and
72 * shared between multiple threads. Where this is the case a lock is required.
73 *
74 * The locks available are:
75 *
76 * The provider flag_lock: Used to control updates to the various provider
77 * "flags" (flag_initialized and flag_activated).
78 *
79 * The provider activatecnt_lock: Used to control updates to the provider
80 * activatecnt value.
81 *
82 * The provider optbits_lock: Used to control access to the provider's
83 * operation_bits and operation_bits_sz fields.
84 *
85 * The store default_path_lock: Used to control access to the provider store's
86 * default search path value (default_path)
87 *
88 * The store lock: Used to control the stack of provider's held within the
89 * provider store, as well as the stack of registered child provider callbacks.
90 *
91 * As a general rule-of-thumb it is best to:
92 * - keep the scope of the code that is protected by a lock to the absolute
93 * minimum possible;
94 * - try to keep the scope of the lock to within a single function (i.e. avoid
95 * making calls to other functions while holding a lock);
96 * - try to only ever hold one lock at a time.
97 *
98 * Unfortunately, it is not always possible to stick to the above guidelines.
99 * Where they are not adhered to there is always a danger of inadvertently
100 * introducing the possibility of deadlock. The following rules MUST be adhered
101 * to in order to avoid that:
102 * - Holding multiple locks at the same time is only allowed for the
103 * provider store lock, the provider activatecnt_lock and the provider flag_lock.
104 * - When holding multiple locks they must be acquired in the following order of
105 * precedence:
106 * 1) provider store lock
107 * 2) provider flag_lock
108 * 3) provider activatecnt_lock
109 * - When releasing locks they must be released in the reverse order to which
110 * they were acquired
111 * - No locks may be held when making an upcall. NOTE: Some common functions
112 * can make upcalls as part of their normal operation. If you need to call
113 * some other function while holding a lock make sure you know whether it
114 * will make any upcalls or not. For example ossl_provider_up_ref() can call
115 * ossl_provider_up_ref_parent() which can call the c_prov_up_ref() upcall.
116 * - It is permissible to hold the store and flag locks when calling child
117 * provider callbacks. No other locks may be held during such callbacks.
118 */
119
120 static OSSL_PROVIDER *provider_new(const char *name,
121 OSSL_provider_init_fn *init_function,
122 STACK_OF(INFOPAIR) *parameters);
123
124 /*-
125 * Provider Object structure
126 * =========================
127 */
128
129 #ifndef FIPS_MODULE
130 typedef struct {
131 OSSL_PROVIDER *prov;
132 int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
133 int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
134 int (*global_props_cb)(const char *props, void *cbdata);
135 void *cbdata;
136 } OSSL_PROVIDER_CHILD_CB;
137 DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB)
138 #endif
139
140 struct provider_store_st; /* Forward declaration */
141
142 struct ossl_provider_st {
143 /* Flag bits */
144 unsigned int flag_initialized:1;
145 unsigned int flag_activated:1;
146
147 /* Getting and setting the flags require synchronization */
148 CRYPTO_RWLOCK *flag_lock;
149
150 /* OpenSSL library side data */
151 CRYPTO_REF_COUNT refcnt;
152 CRYPTO_RWLOCK *activatecnt_lock; /* For the activatecnt counter */
153 int activatecnt;
154 char *name;
155 char *path;
156 DSO *module;
157 OSSL_provider_init_fn *init_function;
158 STACK_OF(INFOPAIR) *parameters;
159 OSSL_LIB_CTX *libctx; /* The library context this instance is in */
160 struct provider_store_st *store; /* The store this instance belongs to */
161 #ifndef FIPS_MODULE
162 /*
163 * In the FIPS module inner provider, this isn't needed, since the
164 * error upcalls are always direct calls to the outer provider.
165 */
166 int error_lib; /* ERR library number, one for each provider */
167 # ifndef OPENSSL_NO_ERR
168 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
169 # endif
170 #endif
171
172 /* Provider side functions */
173 OSSL_FUNC_provider_teardown_fn *teardown;
174 OSSL_FUNC_provider_gettable_params_fn *gettable_params;
175 OSSL_FUNC_provider_get_params_fn *get_params;
176 OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
177 OSSL_FUNC_provider_self_test_fn *self_test;
178 OSSL_FUNC_provider_query_operation_fn *query_operation;
179 OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
180
181 /*
182 * Cache of bit to indicate of query_operation() has been called on
183 * a specific operation or not.
184 */
185 unsigned char *operation_bits;
186 size_t operation_bits_sz;
187 CRYPTO_RWLOCK *opbits_lock;
188
189 #ifndef FIPS_MODULE
190 /* Whether this provider is the child of some other provider */
191 const OSSL_CORE_HANDLE *handle;
192 unsigned int ischild:1;
193 #endif
194
195 /* Provider side data */
196 void *provctx;
197 const OSSL_DISPATCH *dispatch;
198 };
DEFINE_STACK_OF(OSSL_PROVIDER)199 DEFINE_STACK_OF(OSSL_PROVIDER)
200
201 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
202 const OSSL_PROVIDER * const *b)
203 {
204 return strcmp((*a)->name, (*b)->name);
205 }
206
207 /*-
208 * Provider Object store
209 * =====================
210 *
211 * The Provider Object store is a library context object, and therefore needs
212 * an index.
213 */
214
215 struct provider_store_st {
216 OSSL_LIB_CTX *libctx;
217 STACK_OF(OSSL_PROVIDER) *providers;
218 STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs;
219 CRYPTO_RWLOCK *default_path_lock;
220 CRYPTO_RWLOCK *lock;
221 char *default_path;
222 OSSL_PROVIDER_INFO *provinfo;
223 size_t numprovinfo;
224 size_t provinfosz;
225 unsigned int use_fallbacks:1;
226 unsigned int freeing:1;
227 };
228
229 /*
230 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
231 * and ossl_provider_free(), called as needed.
232 * Since this is only called when the provider store is being emptied, we
233 * don't need to care about any lock.
234 */
provider_deactivate_free(OSSL_PROVIDER * prov)235 static void provider_deactivate_free(OSSL_PROVIDER *prov)
236 {
237 if (prov->flag_activated)
238 ossl_provider_deactivate(prov, 1);
239 ossl_provider_free(prov);
240 }
241
242 #ifndef FIPS_MODULE
ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB * cb)243 static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb)
244 {
245 OPENSSL_free(cb);
246 }
247 #endif
248
infopair_free(INFOPAIR * pair)249 static void infopair_free(INFOPAIR *pair)
250 {
251 OPENSSL_free(pair->name);
252 OPENSSL_free(pair->value);
253 OPENSSL_free(pair);
254 }
255
infopair_copy(const INFOPAIR * src)256 static INFOPAIR *infopair_copy(const INFOPAIR *src)
257 {
258 INFOPAIR *dest = OPENSSL_zalloc(sizeof(*dest));
259
260 if (dest == NULL)
261 return NULL;
262 if (src->name != NULL) {
263 dest->name = OPENSSL_strdup(src->name);
264 if (dest->name == NULL)
265 goto err;
266 }
267 if (src->value != NULL) {
268 dest->value = OPENSSL_strdup(src->value);
269 if (dest->value == NULL)
270 goto err;
271 }
272 return dest;
273 err:
274 OPENSSL_free(dest->name);
275 OPENSSL_free(dest);
276 return NULL;
277 }
278
ossl_provider_info_clear(OSSL_PROVIDER_INFO * info)279 void ossl_provider_info_clear(OSSL_PROVIDER_INFO *info)
280 {
281 OPENSSL_free(info->name);
282 OPENSSL_free(info->path);
283 sk_INFOPAIR_pop_free(info->parameters, infopair_free);
284 }
285
ossl_provider_store_free(void * vstore)286 void ossl_provider_store_free(void *vstore)
287 {
288 struct provider_store_st *store = vstore;
289 size_t i;
290
291 if (store == NULL)
292 return;
293 store->freeing = 1;
294 OPENSSL_free(store->default_path);
295 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
296 #ifndef FIPS_MODULE
297 sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs,
298 ossl_provider_child_cb_free);
299 #endif
300 CRYPTO_THREAD_lock_free(store->default_path_lock);
301 CRYPTO_THREAD_lock_free(store->lock);
302 for (i = 0; i < store->numprovinfo; i++)
303 ossl_provider_info_clear(&store->provinfo[i]);
304 OPENSSL_free(store->provinfo);
305 OPENSSL_free(store);
306 }
307
ossl_provider_store_new(OSSL_LIB_CTX * ctx)308 void *ossl_provider_store_new(OSSL_LIB_CTX *ctx)
309 {
310 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
311
312 if (store == NULL
313 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
314 || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
315 #ifndef FIPS_MODULE
316 || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
317 #endif
318 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
319 ossl_provider_store_free(store);
320 return NULL;
321 }
322 store->libctx = ctx;
323 store->use_fallbacks = 1;
324
325 return store;
326 }
327
get_provider_store(OSSL_LIB_CTX * libctx)328 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
329 {
330 struct provider_store_st *store = NULL;
331
332 store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX);
333 if (store == NULL)
334 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
335 return store;
336 }
337
ossl_provider_disable_fallback_loading(OSSL_LIB_CTX * libctx)338 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
339 {
340 struct provider_store_st *store;
341
342 if ((store = get_provider_store(libctx)) != NULL) {
343 if (!CRYPTO_THREAD_write_lock(store->lock))
344 return 0;
345 store->use_fallbacks = 0;
346 CRYPTO_THREAD_unlock(store->lock);
347 return 1;
348 }
349 return 0;
350 }
351
352 #define BUILTINS_BLOCK_SIZE 10
353
ossl_provider_info_add_to_store(OSSL_LIB_CTX * libctx,OSSL_PROVIDER_INFO * entry)354 int ossl_provider_info_add_to_store(OSSL_LIB_CTX *libctx,
355 OSSL_PROVIDER_INFO *entry)
356 {
357 struct provider_store_st *store = get_provider_store(libctx);
358 int ret = 0;
359
360 if (entry->name == NULL) {
361 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
362 return 0;
363 }
364
365 if (store == NULL) {
366 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
367 return 0;
368 }
369
370 if (!CRYPTO_THREAD_write_lock(store->lock))
371 return 0;
372 if (store->provinfosz == 0) {
373 store->provinfo = OPENSSL_zalloc(sizeof(*store->provinfo)
374 * BUILTINS_BLOCK_SIZE);
375 if (store->provinfo == NULL)
376 goto err;
377 store->provinfosz = BUILTINS_BLOCK_SIZE;
378 } else if (store->numprovinfo == store->provinfosz) {
379 OSSL_PROVIDER_INFO *tmpbuiltins;
380 size_t newsz = store->provinfosz + BUILTINS_BLOCK_SIZE;
381
382 tmpbuiltins = OPENSSL_realloc(store->provinfo,
383 sizeof(*store->provinfo) * newsz);
384 if (tmpbuiltins == NULL)
385 goto err;
386 store->provinfo = tmpbuiltins;
387 store->provinfosz = newsz;
388 }
389 store->provinfo[store->numprovinfo] = *entry;
390 store->numprovinfo++;
391
392 ret = 1;
393 err:
394 CRYPTO_THREAD_unlock(store->lock);
395 return ret;
396 }
397
ossl_provider_find(OSSL_LIB_CTX * libctx,const char * name,ossl_unused int noconfig)398 OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
399 ossl_unused int noconfig)
400 {
401 struct provider_store_st *store = NULL;
402 OSSL_PROVIDER *prov = NULL;
403
404 if ((store = get_provider_store(libctx)) != NULL) {
405 OSSL_PROVIDER tmpl = { 0, };
406 int i;
407
408 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
409 /*
410 * Make sure any providers are loaded from config before we try to find
411 * them.
412 */
413 if (!noconfig) {
414 if (ossl_lib_ctx_is_default(libctx))
415 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
416 }
417 #endif
418
419 tmpl.name = (char *)name;
420 if (!CRYPTO_THREAD_write_lock(store->lock))
421 return NULL;
422 sk_OSSL_PROVIDER_sort(store->providers);
423 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) != -1)
424 prov = sk_OSSL_PROVIDER_value(store->providers, i);
425 CRYPTO_THREAD_unlock(store->lock);
426 if (prov != NULL && !ossl_provider_up_ref(prov))
427 prov = NULL;
428 }
429
430 return prov;
431 }
432
433 /*-
434 * Provider Object methods
435 * =======================
436 */
437
provider_new(const char * name,OSSL_provider_init_fn * init_function,STACK_OF (INFOPAIR)* parameters)438 static OSSL_PROVIDER *provider_new(const char *name,
439 OSSL_provider_init_fn *init_function,
440 STACK_OF(INFOPAIR) *parameters)
441 {
442 OSSL_PROVIDER *prov = NULL;
443
444 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL)
445 return NULL;
446 if (!CRYPTO_NEW_REF(&prov->refcnt, 1)) {
447 OPENSSL_free(prov);
448 return NULL;
449 }
450 if ((prov->activatecnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
451 ossl_provider_free(prov);
452 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
453 return NULL;
454 }
455
456 if ((prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
457 || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
458 || (prov->parameters = sk_INFOPAIR_deep_copy(parameters,
459 infopair_copy,
460 infopair_free)) == NULL) {
461 ossl_provider_free(prov);
462 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
463 return NULL;
464 }
465 if ((prov->name = OPENSSL_strdup(name)) == NULL) {
466 ossl_provider_free(prov);
467 return NULL;
468 }
469
470 prov->init_function = init_function;
471
472 return prov;
473 }
474
ossl_provider_up_ref(OSSL_PROVIDER * prov)475 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
476 {
477 int ref = 0;
478
479 if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0)
480 return 0;
481
482 #ifndef FIPS_MODULE
483 if (prov->ischild) {
484 if (!ossl_provider_up_ref_parent(prov, 0)) {
485 ossl_provider_free(prov);
486 return 0;
487 }
488 }
489 #endif
490
491 return ref;
492 }
493
494 #ifndef FIPS_MODULE
provider_up_ref_intern(OSSL_PROVIDER * prov,int activate)495 static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate)
496 {
497 if (activate)
498 return ossl_provider_activate(prov, 1, 0);
499
500 return ossl_provider_up_ref(prov);
501 }
502
provider_free_intern(OSSL_PROVIDER * prov,int deactivate)503 static int provider_free_intern(OSSL_PROVIDER *prov, int deactivate)
504 {
505 if (deactivate)
506 return ossl_provider_deactivate(prov, 1);
507
508 ossl_provider_free(prov);
509 return 1;
510 }
511 #endif
512
513 /*
514 * We assume that the requested provider does not already exist in the store.
515 * The caller should check. If it does exist then adding it to the store later
516 * will fail.
517 */
ossl_provider_new(OSSL_LIB_CTX * libctx,const char * name,OSSL_provider_init_fn * init_function,OSSL_PARAM * params,int noconfig)518 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
519 OSSL_provider_init_fn *init_function,
520 OSSL_PARAM *params, int noconfig)
521 {
522 struct provider_store_st *store = NULL;
523 OSSL_PROVIDER_INFO template;
524 OSSL_PROVIDER *prov = NULL;
525
526 if ((store = get_provider_store(libctx)) == NULL)
527 return NULL;
528
529 memset(&template, 0, sizeof(template));
530 if (init_function == NULL) {
531 const OSSL_PROVIDER_INFO *p;
532 size_t i;
533
534 /* Check if this is a predefined builtin provider */
535 for (p = ossl_predefined_providers; p->name != NULL; p++) {
536 if (strcmp(p->name, name) == 0) {
537 template = *p;
538 break;
539 }
540 }
541 if (p->name == NULL) {
542 /* Check if this is a user added provider */
543 if (!CRYPTO_THREAD_read_lock(store->lock))
544 return NULL;
545 for (i = 0, p = store->provinfo; i < store->numprovinfo; p++, i++) {
546 if (strcmp(p->name, name) == 0) {
547 template = *p;
548 break;
549 }
550 }
551 CRYPTO_THREAD_unlock(store->lock);
552 }
553 } else {
554 template.init = init_function;
555 }
556
557 if (params != NULL) {
558 int i;
559
560 template.parameters = sk_INFOPAIR_new_null();
561 if (template.parameters == NULL)
562 return NULL;
563
564 for (i = 0; params[i].key != NULL; i++) {
565 if (params[i].data_type != OSSL_PARAM_UTF8_STRING)
566 continue;
567 if (ossl_provider_info_add_parameter(&template, params[i].key,
568 (char *)params[i].data) <= 0) {
569 sk_INFOPAIR_pop_free(template.parameters, infopair_free);
570 return NULL;
571 }
572 }
573 }
574
575 /* provider_new() generates an error, so no need here */
576 prov = provider_new(name, template.init, template.parameters);
577
578 if (params != NULL) /* We copied the parameters, let's free them */
579 sk_INFOPAIR_pop_free(template.parameters, infopair_free);
580
581 if (prov == NULL)
582 return NULL;
583
584 if (!ossl_provider_set_module_path(prov, template.path)) {
585 ossl_provider_free(prov);
586 return NULL;
587 }
588
589 prov->libctx = libctx;
590 #ifndef FIPS_MODULE
591 prov->error_lib = ERR_get_next_error_library();
592 #endif
593
594 /*
595 * At this point, the provider is only partially "loaded". To be
596 * fully "loaded", ossl_provider_activate() must also be called and it must
597 * then be added to the provider store.
598 */
599
600 return prov;
601 }
602
603 /* Assumes that the store lock is held */
create_provider_children(OSSL_PROVIDER * prov)604 static int create_provider_children(OSSL_PROVIDER *prov)
605 {
606 int ret = 1;
607 #ifndef FIPS_MODULE
608 struct provider_store_st *store = prov->store;
609 OSSL_PROVIDER_CHILD_CB *child_cb;
610 int i, max;
611
612 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
613 for (i = 0; i < max; i++) {
614 /*
615 * This is newly activated (activatecnt == 1), so we need to
616 * create child providers as necessary.
617 */
618 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
619 ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
620 }
621 #endif
622
623 return ret;
624 }
625
ossl_provider_add_to_store(OSSL_PROVIDER * prov,OSSL_PROVIDER ** actualprov,int retain_fallbacks)626 int ossl_provider_add_to_store(OSSL_PROVIDER *prov, OSSL_PROVIDER **actualprov,
627 int retain_fallbacks)
628 {
629 struct provider_store_st *store;
630 int idx;
631 OSSL_PROVIDER tmpl = { 0, };
632 OSSL_PROVIDER *actualtmp = NULL;
633
634 if (actualprov != NULL)
635 *actualprov = NULL;
636
637 if ((store = get_provider_store(prov->libctx)) == NULL)
638 return 0;
639
640 if (!CRYPTO_THREAD_write_lock(store->lock))
641 return 0;
642
643 tmpl.name = (char *)prov->name;
644 idx = sk_OSSL_PROVIDER_find(store->providers, &tmpl);
645 if (idx == -1)
646 actualtmp = prov;
647 else
648 actualtmp = sk_OSSL_PROVIDER_value(store->providers, idx);
649
650 if (idx == -1) {
651 if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0)
652 goto err;
653 prov->store = store;
654 if (!create_provider_children(prov)) {
655 sk_OSSL_PROVIDER_delete_ptr(store->providers, prov);
656 goto err;
657 }
658 if (!retain_fallbacks)
659 store->use_fallbacks = 0;
660 }
661
662 CRYPTO_THREAD_unlock(store->lock);
663
664 if (actualprov != NULL) {
665 if (!ossl_provider_up_ref(actualtmp)) {
666 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
667 actualtmp = NULL;
668 return 0;
669 }
670 *actualprov = actualtmp;
671 }
672
673 if (idx >= 0) {
674 /*
675 * The provider is already in the store. Probably two threads
676 * independently initialised their own provider objects with the same
677 * name and raced to put them in the store. This thread lost. We
678 * deactivate the one we just created and use the one that already
679 * exists instead.
680 * If we get here then we know we did not create provider children
681 * above, so we inform ossl_provider_deactivate not to attempt to remove
682 * any.
683 */
684 ossl_provider_deactivate(prov, 0);
685 ossl_provider_free(prov);
686 }
687 #ifndef FIPS_MODULE
688 else {
689 /*
690 * This can be done outside the lock. We tolerate other threads getting
691 * the wrong result briefly when creating OSSL_DECODER_CTXs.
692 */
693 ossl_decoder_cache_flush(prov->libctx);
694 }
695 #endif
696
697 return 1;
698
699 err:
700 CRYPTO_THREAD_unlock(store->lock);
701 return 0;
702 }
703
ossl_provider_free(OSSL_PROVIDER * prov)704 void ossl_provider_free(OSSL_PROVIDER *prov)
705 {
706 if (prov != NULL) {
707 int ref = 0;
708
709 CRYPTO_DOWN_REF(&prov->refcnt, &ref);
710
711 /*
712 * When the refcount drops to zero, we clean up the provider.
713 * Note that this also does teardown, which may seem late,
714 * considering that init happens on first activation. However,
715 * there may be other structures hanging on to the provider after
716 * the last deactivation and may therefore need full access to the
717 * provider's services. Therefore, we deinit late.
718 */
719 if (ref == 0) {
720 if (prov->flag_initialized) {
721 ossl_provider_teardown(prov);
722 #ifndef OPENSSL_NO_ERR
723 # ifndef FIPS_MODULE
724 if (prov->error_strings != NULL) {
725 ERR_unload_strings(prov->error_lib, prov->error_strings);
726 OPENSSL_free(prov->error_strings);
727 prov->error_strings = NULL;
728 }
729 # endif
730 #endif
731 OPENSSL_free(prov->operation_bits);
732 prov->operation_bits = NULL;
733 prov->operation_bits_sz = 0;
734 prov->flag_initialized = 0;
735 }
736
737 #ifndef FIPS_MODULE
738 /*
739 * We deregister thread handling whether or not the provider was
740 * initialized. If init was attempted but was not successful then
741 * the provider may still have registered a thread handler.
742 */
743 ossl_init_thread_deregister(prov);
744 DSO_free(prov->module);
745 #endif
746 OPENSSL_free(prov->name);
747 OPENSSL_free(prov->path);
748 sk_INFOPAIR_pop_free(prov->parameters, infopair_free);
749 CRYPTO_THREAD_lock_free(prov->opbits_lock);
750 CRYPTO_THREAD_lock_free(prov->flag_lock);
751 CRYPTO_THREAD_lock_free(prov->activatecnt_lock);
752 CRYPTO_FREE_REF(&prov->refcnt);
753 OPENSSL_free(prov);
754 }
755 #ifndef FIPS_MODULE
756 else if (prov->ischild) {
757 ossl_provider_free_parent(prov, 0);
758 }
759 #endif
760 }
761 }
762
763 /* Setters */
ossl_provider_set_module_path(OSSL_PROVIDER * prov,const char * module_path)764 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
765 {
766 OPENSSL_free(prov->path);
767 prov->path = NULL;
768 if (module_path == NULL)
769 return 1;
770 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
771 return 1;
772 return 0;
773 }
774
infopair_add(STACK_OF (INFOPAIR)** infopairsk,const char * name,const char * value)775 static int infopair_add(STACK_OF(INFOPAIR) **infopairsk, const char *name,
776 const char *value)
777 {
778 INFOPAIR *pair = NULL;
779
780 if ((pair = OPENSSL_zalloc(sizeof(*pair))) == NULL
781 || (pair->name = OPENSSL_strdup(name)) == NULL
782 || (pair->value = OPENSSL_strdup(value)) == NULL)
783 goto err;
784
785 if ((*infopairsk == NULL
786 && (*infopairsk = sk_INFOPAIR_new_null()) == NULL)
787 || sk_INFOPAIR_push(*infopairsk, pair) <= 0) {
788 ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
789 goto err;
790 }
791
792 return 1;
793
794 err:
795 if (pair != NULL) {
796 OPENSSL_free(pair->name);
797 OPENSSL_free(pair->value);
798 OPENSSL_free(pair);
799 }
800 return 0;
801 }
802
ossl_provider_add_parameter(OSSL_PROVIDER * prov,const char * name,const char * value)803 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
804 const char *name, const char *value)
805 {
806 return infopair_add(&prov->parameters, name, value);
807 }
808
ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO * provinfo,const char * name,const char * value)809 int ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO *provinfo,
810 const char *name,
811 const char *value)
812 {
813 return infopair_add(&provinfo->parameters, name, value);
814 }
815
816 /*
817 * Provider activation.
818 *
819 * What "activation" means depends on the provider form; for built in
820 * providers (in the library or the application alike), the provider
821 * can already be considered to be loaded, all that's needed is to
822 * initialize it. However, for dynamically loadable provider modules,
823 * we must first load that module.
824 *
825 * Built in modules are distinguished from dynamically loaded modules
826 * with an already assigned init function.
827 */
828 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
829
OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX * libctx,const char * path)830 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
831 const char *path)
832 {
833 struct provider_store_st *store;
834 char *p = NULL;
835
836 if (path != NULL) {
837 p = OPENSSL_strdup(path);
838 if (p == NULL)
839 return 0;
840 }
841 if ((store = get_provider_store(libctx)) != NULL
842 && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
843 OPENSSL_free(store->default_path);
844 store->default_path = p;
845 CRYPTO_THREAD_unlock(store->default_path_lock);
846 return 1;
847 }
848 OPENSSL_free(p);
849 return 0;
850 }
851
OSSL_PROVIDER_get0_default_search_path(OSSL_LIB_CTX * libctx)852 const char *OSSL_PROVIDER_get0_default_search_path(OSSL_LIB_CTX *libctx)
853 {
854 struct provider_store_st *store;
855 char *path = NULL;
856
857 if ((store = get_provider_store(libctx)) != NULL
858 && CRYPTO_THREAD_read_lock(store->default_path_lock)) {
859 path = store->default_path;
860 CRYPTO_THREAD_unlock(store->default_path_lock);
861 }
862 return path;
863 }
864
865 /*
866 * Internal version that doesn't affect the store flags, and thereby avoid
867 * locking. Direct callers must remember to set the store flags when
868 * appropriate.
869 */
provider_init(OSSL_PROVIDER * prov)870 static int provider_init(OSSL_PROVIDER *prov)
871 {
872 const OSSL_DISPATCH *provider_dispatch = NULL;
873 void *tmp_provctx = NULL; /* safety measure */
874 #ifndef OPENSSL_NO_ERR
875 # ifndef FIPS_MODULE
876 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
877 # endif
878 #endif
879 int ok = 0;
880
881 if (!ossl_assert(!prov->flag_initialized)) {
882 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
883 goto end;
884 }
885
886 /*
887 * If the init function isn't set, it indicates that this provider is
888 * a loadable module.
889 */
890 if (prov->init_function == NULL) {
891 #ifdef FIPS_MODULE
892 goto end;
893 #else
894 if (prov->module == NULL) {
895 char *allocated_path = NULL;
896 const char *module_path = NULL;
897 char *merged_path = NULL;
898 const char *load_dir = NULL;
899 char *allocated_load_dir = NULL;
900 struct provider_store_st *store;
901
902 if ((prov->module = DSO_new()) == NULL) {
903 /* DSO_new() generates an error already */
904 goto end;
905 }
906
907 if ((store = get_provider_store(prov->libctx)) == NULL
908 || !CRYPTO_THREAD_read_lock(store->default_path_lock))
909 goto end;
910
911 if (store->default_path != NULL) {
912 allocated_load_dir = OPENSSL_strdup(store->default_path);
913 CRYPTO_THREAD_unlock(store->default_path_lock);
914 if (allocated_load_dir == NULL)
915 goto end;
916 load_dir = allocated_load_dir;
917 } else {
918 CRYPTO_THREAD_unlock(store->default_path_lock);
919 }
920
921 if (load_dir == NULL) {
922 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
923 if (load_dir == NULL)
924 load_dir = ossl_get_modulesdir();
925 }
926
927 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
928 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
929
930 module_path = prov->path;
931 if (module_path == NULL)
932 module_path = allocated_path =
933 DSO_convert_filename(prov->module, prov->name);
934 if (module_path != NULL)
935 merged_path = DSO_merge(prov->module, module_path, load_dir);
936
937 if (merged_path == NULL
938 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
939 DSO_free(prov->module);
940 prov->module = NULL;
941 }
942
943 OPENSSL_free(merged_path);
944 OPENSSL_free(allocated_path);
945 OPENSSL_free(allocated_load_dir);
946 }
947
948 if (prov->module == NULL) {
949 /* DSO has already recorded errors, this is just a tracepoint */
950 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_DSO_LIB,
951 "name=%s", prov->name);
952 goto end;
953 }
954
955 prov->init_function = (OSSL_provider_init_fn *)
956 DSO_bind_func(prov->module, "OSSL_provider_init");
957 #endif
958 }
959
960 /* Check for and call the initialise function for the provider. */
961 if (prov->init_function == NULL) {
962 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED,
963 "name=%s, provider has no provider init function",
964 prov->name);
965 goto end;
966 }
967 #ifndef FIPS_MODULE
968 OSSL_TRACE_BEGIN(PROVIDER) {
969 BIO_printf(trc_out,
970 "(provider %s) initalizing\n", prov->name);
971 } OSSL_TRACE_END(PROVIDER);
972 #endif
973
974 if (!prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
975 &provider_dispatch, &tmp_provctx)) {
976 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
977 "name=%s", prov->name);
978 goto end;
979 }
980 prov->provctx = tmp_provctx;
981 prov->dispatch = provider_dispatch;
982
983 if (provider_dispatch != NULL) {
984 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
985 switch (provider_dispatch->function_id) {
986 case OSSL_FUNC_PROVIDER_TEARDOWN:
987 prov->teardown =
988 OSSL_FUNC_provider_teardown(provider_dispatch);
989 break;
990 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
991 prov->gettable_params =
992 OSSL_FUNC_provider_gettable_params(provider_dispatch);
993 break;
994 case OSSL_FUNC_PROVIDER_GET_PARAMS:
995 prov->get_params =
996 OSSL_FUNC_provider_get_params(provider_dispatch);
997 break;
998 case OSSL_FUNC_PROVIDER_SELF_TEST:
999 prov->self_test =
1000 OSSL_FUNC_provider_self_test(provider_dispatch);
1001 break;
1002 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
1003 prov->get_capabilities =
1004 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
1005 break;
1006 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
1007 prov->query_operation =
1008 OSSL_FUNC_provider_query_operation(provider_dispatch);
1009 break;
1010 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
1011 prov->unquery_operation =
1012 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
1013 break;
1014 #ifndef OPENSSL_NO_ERR
1015 # ifndef FIPS_MODULE
1016 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
1017 p_get_reason_strings =
1018 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
1019 break;
1020 # endif
1021 #endif
1022 }
1023 }
1024 }
1025
1026 #ifndef OPENSSL_NO_ERR
1027 # ifndef FIPS_MODULE
1028 if (p_get_reason_strings != NULL) {
1029 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
1030 size_t cnt, cnt2;
1031
1032 /*
1033 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
1034 * although they are essentially the same type.
1035 * Furthermore, ERR_load_strings() patches the array's error number
1036 * with the error library number, so we need to make a copy of that
1037 * array either way.
1038 */
1039 cnt = 0;
1040 while (reasonstrings[cnt].id != 0) {
1041 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
1042 goto end;
1043 cnt++;
1044 }
1045 cnt++; /* One for the terminating item */
1046
1047 /* Allocate one extra item for the "library" name */
1048 prov->error_strings =
1049 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
1050 if (prov->error_strings == NULL)
1051 goto end;
1052
1053 /*
1054 * Set the "library" name.
1055 */
1056 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
1057 prov->error_strings[0].string = prov->name;
1058 /*
1059 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
1060 * 1..cnt.
1061 */
1062 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
1063 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
1064 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
1065 }
1066
1067 ERR_load_strings(prov->error_lib, prov->error_strings);
1068 }
1069 # endif
1070 #endif
1071
1072 /* With this flag set, this provider has become fully "loaded". */
1073 prov->flag_initialized = 1;
1074 ok = 1;
1075
1076 end:
1077 return ok;
1078 }
1079
1080 /*
1081 * Deactivate a provider. If upcalls is 0 then we suppress any upcalls to a
1082 * parent provider. If removechildren is 0 then we suppress any calls to remove
1083 * child providers.
1084 * Return -1 on failure and the activation count on success
1085 */
provider_deactivate(OSSL_PROVIDER * prov,int upcalls,int removechildren)1086 static int provider_deactivate(OSSL_PROVIDER *prov, int upcalls,
1087 int removechildren)
1088 {
1089 int count;
1090 struct provider_store_st *store;
1091 #ifndef FIPS_MODULE
1092 int freeparent = 0;
1093 #endif
1094 int lock = 1;
1095
1096 if (!ossl_assert(prov != NULL))
1097 return -1;
1098
1099 /*
1100 * No need to lock if we've got no store because we've not been shared with
1101 * other threads.
1102 */
1103 store = get_provider_store(prov->libctx);
1104 if (store == NULL)
1105 lock = 0;
1106
1107 if (lock && !CRYPTO_THREAD_read_lock(store->lock))
1108 return -1;
1109 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1110 CRYPTO_THREAD_unlock(store->lock);
1111 return -1;
1112 }
1113
1114 CRYPTO_atomic_add(&prov->activatecnt, -1, &count, prov->activatecnt_lock);
1115 #ifndef FIPS_MODULE
1116 if (count >= 1 && prov->ischild && upcalls) {
1117 /*
1118 * We have had a direct activation in this child libctx so we need to
1119 * now down the ref count in the parent provider. We do the actual down
1120 * ref outside of the flag_lock, since it could involve getting other
1121 * locks.
1122 */
1123 freeparent = 1;
1124 }
1125 #endif
1126
1127 if (count < 1)
1128 prov->flag_activated = 0;
1129 #ifndef FIPS_MODULE
1130 else
1131 removechildren = 0;
1132 #endif
1133
1134 #ifndef FIPS_MODULE
1135 if (removechildren && store != NULL) {
1136 int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1137 OSSL_PROVIDER_CHILD_CB *child_cb;
1138
1139 for (i = 0; i < max; i++) {
1140 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1141 child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
1142 }
1143 }
1144 #endif
1145 if (lock) {
1146 CRYPTO_THREAD_unlock(prov->flag_lock);
1147 CRYPTO_THREAD_unlock(store->lock);
1148 /*
1149 * This can be done outside the lock. We tolerate other threads getting
1150 * the wrong result briefly when creating OSSL_DECODER_CTXs.
1151 */
1152 #ifndef FIPS_MODULE
1153 if (count < 1)
1154 ossl_decoder_cache_flush(prov->libctx);
1155 #endif
1156 }
1157 #ifndef FIPS_MODULE
1158 if (freeparent)
1159 ossl_provider_free_parent(prov, 1);
1160 #endif
1161
1162 /* We don't deinit here, that's done in ossl_provider_free() */
1163 return count;
1164 }
1165
1166 /*
1167 * Activate a provider.
1168 * Return -1 on failure and the activation count on success
1169 */
provider_activate(OSSL_PROVIDER * prov,int lock,int upcalls)1170 static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
1171 {
1172 int count = -1;
1173 struct provider_store_st *store;
1174 int ret = 1;
1175
1176 store = prov->store;
1177 /*
1178 * If the provider hasn't been added to the store, then we don't need
1179 * any locks because we've not shared it with other threads.
1180 */
1181 if (store == NULL) {
1182 lock = 0;
1183 if (!provider_init(prov))
1184 return -1;
1185 }
1186
1187 #ifndef FIPS_MODULE
1188 if (prov->ischild && upcalls && !ossl_provider_up_ref_parent(prov, 1))
1189 return -1;
1190 #endif
1191
1192 if (lock && !CRYPTO_THREAD_read_lock(store->lock)) {
1193 #ifndef FIPS_MODULE
1194 if (prov->ischild && upcalls)
1195 ossl_provider_free_parent(prov, 1);
1196 #endif
1197 return -1;
1198 }
1199
1200 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1201 CRYPTO_THREAD_unlock(store->lock);
1202 #ifndef FIPS_MODULE
1203 if (prov->ischild && upcalls)
1204 ossl_provider_free_parent(prov, 1);
1205 #endif
1206 return -1;
1207 }
1208 if (CRYPTO_atomic_add(&prov->activatecnt, 1, &count, prov->activatecnt_lock)) {
1209 prov->flag_activated = 1;
1210
1211 if (count == 1 && store != NULL) {
1212 ret = create_provider_children(prov);
1213 }
1214 }
1215 if (lock) {
1216 CRYPTO_THREAD_unlock(prov->flag_lock);
1217 CRYPTO_THREAD_unlock(store->lock);
1218 /*
1219 * This can be done outside the lock. We tolerate other threads getting
1220 * the wrong result briefly when creating OSSL_DECODER_CTXs.
1221 */
1222 #ifndef FIPS_MODULE
1223 if (count == 1)
1224 ossl_decoder_cache_flush(prov->libctx);
1225 #endif
1226 }
1227
1228 if (!ret)
1229 return -1;
1230
1231 return count;
1232 }
1233
provider_flush_store_cache(const OSSL_PROVIDER * prov)1234 static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
1235 {
1236 struct provider_store_st *store;
1237 int freeing;
1238
1239 if ((store = get_provider_store(prov->libctx)) == NULL)
1240 return 0;
1241
1242 if (!CRYPTO_THREAD_read_lock(store->lock))
1243 return 0;
1244 freeing = store->freeing;
1245 CRYPTO_THREAD_unlock(store->lock);
1246
1247 if (!freeing) {
1248 int acc
1249 = evp_method_store_cache_flush(prov->libctx)
1250 #ifndef FIPS_MODULE
1251 + ossl_encoder_store_cache_flush(prov->libctx)
1252 + ossl_decoder_store_cache_flush(prov->libctx)
1253 + ossl_store_loader_store_cache_flush(prov->libctx)
1254 #endif
1255 ;
1256
1257 #ifndef FIPS_MODULE
1258 return acc == 4;
1259 #else
1260 return acc == 1;
1261 #endif
1262 }
1263 return 1;
1264 }
1265
provider_remove_store_methods(OSSL_PROVIDER * prov)1266 static int provider_remove_store_methods(OSSL_PROVIDER *prov)
1267 {
1268 struct provider_store_st *store;
1269 int freeing;
1270
1271 if ((store = get_provider_store(prov->libctx)) == NULL)
1272 return 0;
1273
1274 if (!CRYPTO_THREAD_read_lock(store->lock))
1275 return 0;
1276 freeing = store->freeing;
1277 CRYPTO_THREAD_unlock(store->lock);
1278
1279 if (!freeing) {
1280 int acc;
1281
1282 if (!CRYPTO_THREAD_write_lock(prov->opbits_lock))
1283 return 0;
1284 OPENSSL_free(prov->operation_bits);
1285 prov->operation_bits = NULL;
1286 prov->operation_bits_sz = 0;
1287 CRYPTO_THREAD_unlock(prov->opbits_lock);
1288
1289 acc = evp_method_store_remove_all_provided(prov)
1290 #ifndef FIPS_MODULE
1291 + ossl_encoder_store_remove_all_provided(prov)
1292 + ossl_decoder_store_remove_all_provided(prov)
1293 + ossl_store_loader_store_remove_all_provided(prov)
1294 #endif
1295 ;
1296
1297 #ifndef FIPS_MODULE
1298 return acc == 4;
1299 #else
1300 return acc == 1;
1301 #endif
1302 }
1303 return 1;
1304 }
1305
ossl_provider_activate(OSSL_PROVIDER * prov,int upcalls,int aschild)1306 int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild)
1307 {
1308 int count;
1309
1310 if (prov == NULL)
1311 return 0;
1312 #ifndef FIPS_MODULE
1313 /*
1314 * If aschild is true, then we only actually do the activation if the
1315 * provider is a child. If its not, this is still success.
1316 */
1317 if (aschild && !prov->ischild)
1318 return 1;
1319 #endif
1320 if ((count = provider_activate(prov, 1, upcalls)) > 0)
1321 return count == 1 ? provider_flush_store_cache(prov) : 1;
1322
1323 return 0;
1324 }
1325
ossl_provider_deactivate(OSSL_PROVIDER * prov,int removechildren)1326 int ossl_provider_deactivate(OSSL_PROVIDER *prov, int removechildren)
1327 {
1328 int count;
1329
1330 if (prov == NULL
1331 || (count = provider_deactivate(prov, 1, removechildren)) < 0)
1332 return 0;
1333 return count == 0 ? provider_remove_store_methods(prov) : 1;
1334 }
1335
ossl_provider_ctx(const OSSL_PROVIDER * prov)1336 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
1337 {
1338 return prov != NULL ? prov->provctx : NULL;
1339 }
1340
1341 /*
1342 * This function only does something once when store->use_fallbacks == 1,
1343 * and then sets store->use_fallbacks = 0, so the second call and so on is
1344 * effectively a no-op.
1345 */
provider_activate_fallbacks(struct provider_store_st * store)1346 static int provider_activate_fallbacks(struct provider_store_st *store)
1347 {
1348 int use_fallbacks;
1349 int activated_fallback_count = 0;
1350 int ret = 0;
1351 const OSSL_PROVIDER_INFO *p;
1352
1353 if (!CRYPTO_THREAD_read_lock(store->lock))
1354 return 0;
1355 use_fallbacks = store->use_fallbacks;
1356 CRYPTO_THREAD_unlock(store->lock);
1357 if (!use_fallbacks)
1358 return 1;
1359
1360 if (!CRYPTO_THREAD_write_lock(store->lock))
1361 return 0;
1362 /* Check again, just in case another thread changed it */
1363 use_fallbacks = store->use_fallbacks;
1364 if (!use_fallbacks) {
1365 CRYPTO_THREAD_unlock(store->lock);
1366 return 1;
1367 }
1368
1369 for (p = ossl_predefined_providers; p->name != NULL; p++) {
1370 OSSL_PROVIDER *prov = NULL;
1371
1372 if (!p->is_fallback)
1373 continue;
1374 /*
1375 * We use the internal constructor directly here,
1376 * otherwise we get a call loop
1377 */
1378 prov = provider_new(p->name, p->init, NULL);
1379 if (prov == NULL)
1380 goto err;
1381 prov->libctx = store->libctx;
1382 #ifndef FIPS_MODULE
1383 prov->error_lib = ERR_get_next_error_library();
1384 #endif
1385
1386 /*
1387 * We are calling provider_activate while holding the store lock. This
1388 * means the init function will be called while holding a lock. Normally
1389 * we try to avoid calling a user callback while holding a lock.
1390 * However, fallbacks are never third party providers so we accept this.
1391 */
1392 if (provider_activate(prov, 0, 0) < 0) {
1393 ossl_provider_free(prov);
1394 goto err;
1395 }
1396 prov->store = store;
1397 if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
1398 ossl_provider_free(prov);
1399 goto err;
1400 }
1401 activated_fallback_count++;
1402 }
1403
1404 if (activated_fallback_count > 0) {
1405 store->use_fallbacks = 0;
1406 ret = 1;
1407 }
1408 err:
1409 CRYPTO_THREAD_unlock(store->lock);
1410 return ret;
1411 }
1412
ossl_provider_doall_activated(OSSL_LIB_CTX * ctx,int (* cb)(OSSL_PROVIDER * provider,void * cbdata),void * cbdata)1413 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
1414 int (*cb)(OSSL_PROVIDER *provider,
1415 void *cbdata),
1416 void *cbdata)
1417 {
1418 int ret = 0, curr, max, ref = 0;
1419 struct provider_store_st *store = get_provider_store(ctx);
1420 STACK_OF(OSSL_PROVIDER) *provs = NULL;
1421
1422 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG)
1423 /*
1424 * Make sure any providers are loaded from config before we try to use
1425 * them.
1426 */
1427 if (ossl_lib_ctx_is_default(ctx))
1428 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
1429 #endif
1430
1431 if (store == NULL)
1432 return 1;
1433 if (!provider_activate_fallbacks(store))
1434 return 0;
1435
1436 /*
1437 * Under lock, grab a copy of the provider list and up_ref each
1438 * provider so that they don't disappear underneath us.
1439 */
1440 if (!CRYPTO_THREAD_read_lock(store->lock))
1441 return 0;
1442 provs = sk_OSSL_PROVIDER_dup(store->providers);
1443 if (provs == NULL) {
1444 CRYPTO_THREAD_unlock(store->lock);
1445 return 0;
1446 }
1447 max = sk_OSSL_PROVIDER_num(provs);
1448 /*
1449 * We work backwards through the stack so that we can safely delete items
1450 * as we go.
1451 */
1452 for (curr = max - 1; curr >= 0; curr--) {
1453 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1454
1455 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1456 goto err_unlock;
1457 if (prov->flag_activated) {
1458 /*
1459 * We call CRYPTO_UP_REF directly rather than ossl_provider_up_ref
1460 * to avoid upping the ref count on the parent provider, which we
1461 * must not do while holding locks.
1462 */
1463 if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0) {
1464 CRYPTO_THREAD_unlock(prov->flag_lock);
1465 goto err_unlock;
1466 }
1467 /*
1468 * It's already activated, but we up the activated count to ensure
1469 * it remains activated until after we've called the user callback.
1470 * In theory this could mean the parent provider goes inactive,
1471 * whilst still activated in the child for a short period. That's ok.
1472 */
1473 if (!CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
1474 prov->activatecnt_lock)) {
1475 CRYPTO_DOWN_REF(&prov->refcnt, &ref);
1476 CRYPTO_THREAD_unlock(prov->flag_lock);
1477 goto err_unlock;
1478 }
1479 } else {
1480 sk_OSSL_PROVIDER_delete(provs, curr);
1481 max--;
1482 }
1483 CRYPTO_THREAD_unlock(prov->flag_lock);
1484 }
1485 CRYPTO_THREAD_unlock(store->lock);
1486
1487 /*
1488 * Now, we sweep through all providers not under lock
1489 */
1490 for (curr = 0; curr < max; curr++) {
1491 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1492
1493 if (!cb(prov, cbdata)) {
1494 curr = -1;
1495 goto finish;
1496 }
1497 }
1498 curr = -1;
1499
1500 ret = 1;
1501 goto finish;
1502
1503 err_unlock:
1504 CRYPTO_THREAD_unlock(store->lock);
1505 finish:
1506 /*
1507 * The pop_free call doesn't do what we want on an error condition. We
1508 * either start from the first item in the stack, or part way through if
1509 * we only processed some of the items.
1510 */
1511 for (curr++; curr < max; curr++) {
1512 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1513
1514 if (!CRYPTO_atomic_add(&prov->activatecnt, -1, &ref,
1515 prov->activatecnt_lock)) {
1516 ret = 0;
1517 continue;
1518 }
1519 if (ref < 1) {
1520 /*
1521 * Looks like we need to deactivate properly. We could just have
1522 * done this originally, but it involves taking a write lock so
1523 * we avoid it. We up the count again and do a full deactivation
1524 */
1525 if (CRYPTO_atomic_add(&prov->activatecnt, 1, &ref,
1526 prov->activatecnt_lock))
1527 provider_deactivate(prov, 0, 1);
1528 else
1529 ret = 0;
1530 }
1531 /*
1532 * As above where we did the up-ref, we don't call ossl_provider_free
1533 * to avoid making upcalls. There should always be at least one ref
1534 * to the provider in the store, so this should never drop to 0.
1535 */
1536 if (!CRYPTO_DOWN_REF(&prov->refcnt, &ref)) {
1537 ret = 0;
1538 continue;
1539 }
1540 /*
1541 * Not much we can do if this assert ever fails. So we don't use
1542 * ossl_assert here.
1543 */
1544 assert(ref > 0);
1545 }
1546 sk_OSSL_PROVIDER_free(provs);
1547 return ret;
1548 }
1549
OSSL_PROVIDER_available(OSSL_LIB_CTX * libctx,const char * name)1550 int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
1551 {
1552 OSSL_PROVIDER *prov = NULL;
1553 int available = 0;
1554 struct provider_store_st *store = get_provider_store(libctx);
1555
1556 if (store == NULL || !provider_activate_fallbacks(store))
1557 return 0;
1558
1559 prov = ossl_provider_find(libctx, name, 0);
1560 if (prov != NULL) {
1561 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1562 return 0;
1563 available = prov->flag_activated;
1564 CRYPTO_THREAD_unlock(prov->flag_lock);
1565 ossl_provider_free(prov);
1566 }
1567 return available;
1568 }
1569
1570 /* Getters of Provider Object data */
ossl_provider_name(const OSSL_PROVIDER * prov)1571 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
1572 {
1573 return prov->name;
1574 }
1575
ossl_provider_dso(const OSSL_PROVIDER * prov)1576 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
1577 {
1578 return prov->module;
1579 }
1580
ossl_provider_module_name(const OSSL_PROVIDER * prov)1581 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
1582 {
1583 #ifdef FIPS_MODULE
1584 return NULL;
1585 #else
1586 return DSO_get_filename(prov->module);
1587 #endif
1588 }
1589
ossl_provider_module_path(const OSSL_PROVIDER * prov)1590 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
1591 {
1592 #ifdef FIPS_MODULE
1593 return NULL;
1594 #else
1595 /* FIXME: Ensure it's a full path */
1596 return DSO_get_filename(prov->module);
1597 #endif
1598 }
1599
ossl_provider_prov_ctx(const OSSL_PROVIDER * prov)1600 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
1601 {
1602 if (prov != NULL)
1603 return prov->provctx;
1604
1605 return NULL;
1606 }
1607
ossl_provider_get0_dispatch(const OSSL_PROVIDER * prov)1608 const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
1609 {
1610 if (prov != NULL)
1611 return prov->dispatch;
1612
1613 return NULL;
1614 }
1615
ossl_provider_libctx(const OSSL_PROVIDER * prov)1616 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
1617 {
1618 return prov != NULL ? prov->libctx : NULL;
1619 }
1620
1621 /**
1622 * @brief Tears down the given provider.
1623 *
1624 * This function calls the `teardown` callback of the given provider to release
1625 * any resources associated with it. The teardown is skipped if the callback is
1626 * not defined or, in non-FIPS builds, if the provider is a child.
1627 *
1628 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1629 *
1630 * If tracing is enabled, a message is printed indicating that the teardown is
1631 * being called.
1632 */
ossl_provider_teardown(const OSSL_PROVIDER * prov)1633 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
1634 {
1635 if (prov->teardown != NULL
1636 #ifndef FIPS_MODULE
1637 && !prov->ischild
1638 #endif
1639 ) {
1640 #ifndef FIPS_MODULE
1641 OSSL_TRACE_BEGIN(PROVIDER) {
1642 BIO_printf(trc_out, "(provider %s) calling teardown\n",
1643 ossl_provider_name(prov));
1644 } OSSL_TRACE_END(PROVIDER);
1645 #endif
1646 prov->teardown(prov->provctx);
1647 }
1648 }
1649
1650 /**
1651 * @brief Retrieves the parameters that can be obtained from a provider.
1652 *
1653 * This function calls the `gettable_params` callback of the given provider to
1654 * get a list of parameters that can be retrieved.
1655 *
1656 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1657 *
1658 * @return Pointer to an array of OSSL_PARAM structures that represent the
1659 * gettable parameters, or NULL if the callback is not defined.
1660 *
1661 * If tracing is enabled, the gettable parameters are printed for debugging.
1662 */
ossl_provider_gettable_params(const OSSL_PROVIDER * prov)1663 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1664 {
1665 const OSSL_PARAM *ret = NULL;
1666
1667 if (prov->gettable_params != NULL)
1668 ret = prov->gettable_params(prov->provctx);
1669
1670 #ifndef FIPS_MODULE
1671 OSSL_TRACE_BEGIN(PROVIDER) {
1672 char *buf = NULL;
1673
1674 BIO_printf(trc_out, "(provider %s) gettable params\n",
1675 ossl_provider_name(prov));
1676 BIO_printf(trc_out, "Parameters:\n");
1677 if (prov->gettable_params != NULL) {
1678 if (!OSSL_PARAM_print_to_bio(ret, trc_out, 0))
1679 BIO_printf(trc_out, "Failed to parse param values\n");
1680 OPENSSL_free(buf);
1681 } else {
1682 BIO_printf(trc_out, "Provider doesn't implement gettable_params\n");
1683 }
1684 } OSSL_TRACE_END(PROVIDER);
1685 #endif
1686
1687 return ret;
1688 }
1689
1690 /**
1691 * @brief Retrieves parameters from a provider.
1692 *
1693 * This function calls the `get_params` callback of the given provider to
1694 * retrieve its parameters. If the callback is defined, it is invoked with the
1695 * provider context and the parameters array.
1696 *
1697 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1698 * @param params Array of OSSL_PARAM structures to store the retrieved parameters.
1699 *
1700 * @return 1 on success, 0 if the `get_params` callback is not defined or fails.
1701 *
1702 * If tracing is enabled, the retrieved parameters are printed for debugging.
1703 */
ossl_provider_get_params(const OSSL_PROVIDER * prov,OSSL_PARAM params[])1704 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1705 {
1706 int ret;
1707
1708 if (prov->get_params == NULL)
1709 return 0;
1710
1711 ret = prov->get_params(prov->provctx, params);
1712 #ifndef FIPS_MODULE
1713 OSSL_TRACE_BEGIN(PROVIDER) {
1714
1715 BIO_printf(trc_out,
1716 "(provider %s) calling get_params\n", prov->name);
1717 if (ret == 1) {
1718 BIO_printf(trc_out, "Parameters:\n");
1719 if (!OSSL_PARAM_print_to_bio(params, trc_out, 1))
1720 BIO_printf(trc_out, "Failed to parse param values\n");
1721 } else {
1722 BIO_printf(trc_out, "get_params call failed\n");
1723 }
1724 } OSSL_TRACE_END(PROVIDER);
1725 #endif
1726 return ret;
1727 }
1728
1729 /**
1730 * @brief Performs a self-test on the given provider.
1731 *
1732 * This function calls the `self_test` callback of the given provider to
1733 * perform a self-test. If the callback is not defined, it assumes the test
1734 * passed.
1735 *
1736 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1737 *
1738 * @return 1 if the self-test passes or the callback is not defined, 0 on failure.
1739 *
1740 * If tracing is enabled, the result of the self-test is printed for debugging.
1741 * If the test fails, the provider's store methods are removed.
1742 */
ossl_provider_self_test(const OSSL_PROVIDER * prov)1743 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1744 {
1745 int ret = 1;
1746
1747 if (prov->self_test != NULL)
1748 ret = prov->self_test(prov->provctx);
1749
1750 #ifndef FIPS_MODULE
1751 OSSL_TRACE_BEGIN(PROVIDER) {
1752 if (prov->self_test != NULL)
1753 BIO_printf(trc_out,
1754 "(provider %s) Calling self_test, ret = %d\n",
1755 prov->name, ret);
1756 else
1757 BIO_printf(trc_out,
1758 "(provider %s) doesn't implement self_test\n",
1759 prov->name);
1760 } OSSL_TRACE_END(PROVIDER);
1761 #endif
1762 if (ret == 0)
1763 (void)provider_remove_store_methods((OSSL_PROVIDER *)prov);
1764 return ret;
1765 }
1766
1767 /**
1768 * @brief Retrieves capabilities from the given provider.
1769 *
1770 * This function calls the `get_capabilities` callback of the specified provider
1771 * to retrieve capabilities information. The callback is invoked with the
1772 * provider context, capability name, a callback function, and an argument.
1773 *
1774 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1775 * @param capability String representing the capability to be retrieved.
1776 * @param cb Callback function to process the capability data.
1777 * @param arg Argument to be passed to the callback function.
1778 *
1779 * @return 1 if the capabilities are successfully retrieved or if the callback
1780 * is not defined, otherwise the value returned by `get_capabilities`.
1781 *
1782 * If tracing is enabled, a message is printed indicating the requested
1783 * capabilities.
1784 */
ossl_provider_get_capabilities(const OSSL_PROVIDER * prov,const char * capability,OSSL_CALLBACK * cb,void * arg)1785 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1786 const char *capability,
1787 OSSL_CALLBACK *cb,
1788 void *arg)
1789 {
1790 if (prov->get_capabilities != NULL) {
1791 #ifndef FIPS_MODULE
1792 OSSL_TRACE_BEGIN(PROVIDER) {
1793 BIO_printf(trc_out,
1794 "(provider %s) Calling get_capabilities "
1795 "with capabilities %s\n", prov->name,
1796 capability == NULL ? "none" : capability);
1797 } OSSL_TRACE_END(PROVIDER);
1798 #endif
1799 return prov->get_capabilities(prov->provctx, capability, cb, arg);
1800 }
1801 return 1;
1802 }
1803
1804 /**
1805 * @brief Queries the provider for available algorithms for a given operation.
1806 *
1807 * This function calls the `query_operation` callback of the specified provider
1808 * to obtain a list of algorithms that can perform the given operation. It may
1809 * also set a flag indicating whether the result should be cached.
1810 *
1811 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1812 * @param operation_id Identifier of the operation to query.
1813 * @param no_cache Pointer to an integer flag to indicate whether caching is allowed.
1814 *
1815 * @return Pointer to an array of OSSL_ALGORITHM structures representing the
1816 * available algorithms, or NULL if the callback is not defined or
1817 * there are no available algorithms.
1818 *
1819 * If tracing is enabled, the available algorithms and their properties are
1820 * printed for debugging.
1821 */
ossl_provider_query_operation(const OSSL_PROVIDER * prov,int operation_id,int * no_cache)1822 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1823 int operation_id,
1824 int *no_cache)
1825 {
1826 const OSSL_ALGORITHM *res;
1827
1828 if (prov->query_operation == NULL) {
1829 #ifndef FIPS_MODULE
1830 OSSL_TRACE_BEGIN(PROVIDER) {
1831 BIO_printf(trc_out, "provider %s lacks query operation!\n",
1832 prov->name);
1833 } OSSL_TRACE_END(PROVIDER);
1834 #endif
1835 return NULL;
1836 }
1837
1838 res = prov->query_operation(prov->provctx, operation_id, no_cache);
1839 #ifndef FIPS_MODULE
1840 OSSL_TRACE_BEGIN(PROVIDER) {
1841 const OSSL_ALGORITHM *idx;
1842 if (res != NULL) {
1843 BIO_printf(trc_out,
1844 "(provider %s) Calling query, available algs are:\n", prov->name);
1845
1846 for (idx = res; idx->algorithm_names != NULL; idx++) {
1847 BIO_printf(trc_out,
1848 "(provider %s) names %s, prop_def %s, desc %s\n",
1849 prov->name,
1850 res->algorithm_names == NULL ? "none" :
1851 res->algorithm_names,
1852 res->property_definition == NULL ? "none" :
1853 res->property_definition,
1854 res->algorithm_description == NULL ? "none" :
1855 res->algorithm_description);
1856 }
1857 } else {
1858 BIO_printf(trc_out, "(provider %s) query_operation failed\n", prov->name);
1859 }
1860 } OSSL_TRACE_END(PROVIDER);
1861 #endif
1862
1863 #if defined(OPENSSL_NO_CACHED_FETCH)
1864 /* Forcing the non-caching of queries */
1865 if (no_cache != NULL)
1866 *no_cache = 1;
1867 #endif
1868 return res;
1869 }
1870
1871 /**
1872 * @brief Releases resources associated with a queried operation.
1873 *
1874 * This function calls the `unquery_operation` callback of the specified
1875 * provider to release any resources related to a previously queried operation.
1876 *
1877 * @param prov Pointer to the OSSL_PROVIDER structure representing the provider.
1878 * @param operation_id Identifier of the operation to unquery.
1879 * @param algs Pointer to the OSSL_ALGORITHM structures representing the
1880 * algorithms associated with the operation.
1881 *
1882 * If tracing is enabled, a message is printed indicating that the operation
1883 * is being unqueried.
1884 */
ossl_provider_unquery_operation(const OSSL_PROVIDER * prov,int operation_id,const OSSL_ALGORITHM * algs)1885 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1886 int operation_id,
1887 const OSSL_ALGORITHM *algs)
1888 {
1889 if (prov->unquery_operation != NULL) {
1890 #ifndef FIPS_MODULE
1891 OSSL_TRACE_BEGIN(PROVIDER) {
1892 BIO_printf(trc_out,
1893 "(provider %s) Calling unquery"
1894 " with operation %d\n",
1895 prov->name,
1896 operation_id);
1897 } OSSL_TRACE_END(PROVIDER);
1898 #endif
1899 prov->unquery_operation(prov->provctx, operation_id, algs);
1900 }
1901 }
1902
ossl_provider_set_operation_bit(OSSL_PROVIDER * provider,size_t bitnum)1903 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1904 {
1905 size_t byte = bitnum / 8;
1906 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1907
1908 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1909 return 0;
1910 if (provider->operation_bits_sz <= byte) {
1911 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1912 byte + 1);
1913
1914 if (tmp == NULL) {
1915 CRYPTO_THREAD_unlock(provider->opbits_lock);
1916 return 0;
1917 }
1918 provider->operation_bits = tmp;
1919 memset(provider->operation_bits + provider->operation_bits_sz,
1920 '\0', byte + 1 - provider->operation_bits_sz);
1921 provider->operation_bits_sz = byte + 1;
1922 }
1923 provider->operation_bits[byte] |= bit;
1924 CRYPTO_THREAD_unlock(provider->opbits_lock);
1925 return 1;
1926 }
1927
ossl_provider_test_operation_bit(OSSL_PROVIDER * provider,size_t bitnum,int * result)1928 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1929 int *result)
1930 {
1931 size_t byte = bitnum / 8;
1932 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1933
1934 if (!ossl_assert(result != NULL)) {
1935 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1936 return 0;
1937 }
1938
1939 *result = 0;
1940 if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1941 return 0;
1942 if (provider->operation_bits_sz > byte)
1943 *result = ((provider->operation_bits[byte] & bit) != 0);
1944 CRYPTO_THREAD_unlock(provider->opbits_lock);
1945 return 1;
1946 }
1947
1948 #ifndef FIPS_MODULE
ossl_provider_get_parent(OSSL_PROVIDER * prov)1949 const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
1950 {
1951 return prov->handle;
1952 }
1953
ossl_provider_is_child(const OSSL_PROVIDER * prov)1954 int ossl_provider_is_child(const OSSL_PROVIDER *prov)
1955 {
1956 return prov->ischild;
1957 }
1958
ossl_provider_set_child(OSSL_PROVIDER * prov,const OSSL_CORE_HANDLE * handle)1959 int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
1960 {
1961 prov->handle = handle;
1962 prov->ischild = 1;
1963
1964 return 1;
1965 }
1966
ossl_provider_default_props_update(OSSL_LIB_CTX * libctx,const char * props)1967 int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
1968 {
1969 #ifndef FIPS_MODULE
1970 struct provider_store_st *store = NULL;
1971 int i, max;
1972 OSSL_PROVIDER_CHILD_CB *child_cb;
1973
1974 if ((store = get_provider_store(libctx)) == NULL)
1975 return 0;
1976
1977 if (!CRYPTO_THREAD_read_lock(store->lock))
1978 return 0;
1979
1980 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1981 for (i = 0; i < max; i++) {
1982 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1983 child_cb->global_props_cb(props, child_cb->cbdata);
1984 }
1985
1986 CRYPTO_THREAD_unlock(store->lock);
1987 #endif
1988 return 1;
1989 }
1990
ossl_provider_register_child_cb(const OSSL_CORE_HANDLE * handle,int (* create_cb)(const OSSL_CORE_HANDLE * provider,void * cbdata),int (* remove_cb)(const OSSL_CORE_HANDLE * provider,void * cbdata),int (* global_props_cb)(const char * props,void * cbdata),void * cbdata)1991 static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
1992 int (*create_cb)(
1993 const OSSL_CORE_HANDLE *provider,
1994 void *cbdata),
1995 int (*remove_cb)(
1996 const OSSL_CORE_HANDLE *provider,
1997 void *cbdata),
1998 int (*global_props_cb)(
1999 const char *props,
2000 void *cbdata),
2001 void *cbdata)
2002 {
2003 /*
2004 * This is really an OSSL_PROVIDER that we created and cast to
2005 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
2006 */
2007 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
2008 OSSL_PROVIDER *prov;
2009 OSSL_LIB_CTX *libctx = thisprov->libctx;
2010 struct provider_store_st *store = NULL;
2011 int ret = 0, i, max;
2012 OSSL_PROVIDER_CHILD_CB *child_cb;
2013 char *propsstr = NULL;
2014
2015 if ((store = get_provider_store(libctx)) == NULL)
2016 return 0;
2017
2018 child_cb = OPENSSL_malloc(sizeof(*child_cb));
2019 if (child_cb == NULL)
2020 return 0;
2021 child_cb->prov = thisprov;
2022 child_cb->create_cb = create_cb;
2023 child_cb->remove_cb = remove_cb;
2024 child_cb->global_props_cb = global_props_cb;
2025 child_cb->cbdata = cbdata;
2026
2027 if (!CRYPTO_THREAD_write_lock(store->lock)) {
2028 OPENSSL_free(child_cb);
2029 return 0;
2030 }
2031 propsstr = evp_get_global_properties_str(libctx, 0);
2032
2033 if (propsstr != NULL) {
2034 global_props_cb(propsstr, cbdata);
2035 OPENSSL_free(propsstr);
2036 }
2037 max = sk_OSSL_PROVIDER_num(store->providers);
2038 for (i = 0; i < max; i++) {
2039 int activated;
2040
2041 prov = sk_OSSL_PROVIDER_value(store->providers, i);
2042
2043 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
2044 break;
2045 activated = prov->flag_activated;
2046 CRYPTO_THREAD_unlock(prov->flag_lock);
2047 /*
2048 * We hold the store lock while calling the user callback. This means
2049 * that the user callback must be short and simple and not do anything
2050 * likely to cause a deadlock. We don't hold the flag_lock during this
2051 * call. In theory this means that another thread could deactivate it
2052 * while we are calling create. This is ok because the other thread
2053 * will also call remove_cb, but won't be able to do so until we release
2054 * the store lock.
2055 */
2056 if (activated && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
2057 break;
2058 }
2059 if (i == max) {
2060 /* Success */
2061 ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
2062 }
2063 if (i != max || ret <= 0) {
2064 /* Failed during creation. Remove everything we just added */
2065 for (; i >= 0; i--) {
2066 prov = sk_OSSL_PROVIDER_value(store->providers, i);
2067 remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
2068 }
2069 OPENSSL_free(child_cb);
2070 ret = 0;
2071 }
2072 CRYPTO_THREAD_unlock(store->lock);
2073
2074 return ret;
2075 }
2076
ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE * handle)2077 static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
2078 {
2079 /*
2080 * This is really an OSSL_PROVIDER that we created and cast to
2081 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
2082 */
2083 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
2084 OSSL_LIB_CTX *libctx = thisprov->libctx;
2085 struct provider_store_st *store = NULL;
2086 int i, max;
2087 OSSL_PROVIDER_CHILD_CB *child_cb;
2088
2089 if ((store = get_provider_store(libctx)) == NULL)
2090 return;
2091
2092 if (!CRYPTO_THREAD_write_lock(store->lock))
2093 return;
2094 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
2095 for (i = 0; i < max; i++) {
2096 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
2097 if (child_cb->prov == thisprov) {
2098 /* Found an entry */
2099 sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
2100 OPENSSL_free(child_cb);
2101 break;
2102 }
2103 }
2104 CRYPTO_THREAD_unlock(store->lock);
2105 }
2106 #endif
2107
2108 /*-
2109 * Core functions for the provider
2110 * ===============================
2111 *
2112 * This is the set of functions that the core makes available to the provider
2113 */
2114
2115 /*
2116 * This returns a list of Provider Object parameters with their types, for
2117 * discovery. We do not expect that many providers will use this, but one
2118 * never knows.
2119 */
2120 static const OSSL_PARAM param_types[] = {
2121 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
2122 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
2123 NULL, 0),
2124 #ifndef FIPS_MODULE
2125 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
2126 NULL, 0),
2127 #endif
2128 OSSL_PARAM_END
2129 };
2130
2131 /*
2132 * Forward declare all the functions that are provided aa dispatch.
2133 * This ensures that the compiler will complain if they aren't defined
2134 * with the correct signature.
2135 */
2136 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
2137 static OSSL_FUNC_core_get_params_fn core_get_params;
2138 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
2139 static OSSL_FUNC_core_thread_start_fn core_thread_start;
2140 #ifndef FIPS_MODULE
2141 static OSSL_FUNC_core_new_error_fn core_new_error;
2142 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
2143 static OSSL_FUNC_core_vset_error_fn core_vset_error;
2144 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
2145 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
2146 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
2147 OSSL_FUNC_BIO_new_file_fn ossl_core_bio_new_file;
2148 OSSL_FUNC_BIO_new_membuf_fn ossl_core_bio_new_mem_buf;
2149 OSSL_FUNC_BIO_read_ex_fn ossl_core_bio_read_ex;
2150 OSSL_FUNC_BIO_write_ex_fn ossl_core_bio_write_ex;
2151 OSSL_FUNC_BIO_gets_fn ossl_core_bio_gets;
2152 OSSL_FUNC_BIO_puts_fn ossl_core_bio_puts;
2153 OSSL_FUNC_BIO_up_ref_fn ossl_core_bio_up_ref;
2154 OSSL_FUNC_BIO_free_fn ossl_core_bio_free;
2155 OSSL_FUNC_BIO_vprintf_fn ossl_core_bio_vprintf;
2156 OSSL_FUNC_BIO_vsnprintf_fn BIO_vsnprintf;
2157 static OSSL_FUNC_indicator_cb_fn core_indicator_get_callback;
2158 static OSSL_FUNC_self_test_cb_fn core_self_test_get_callback;
2159 static OSSL_FUNC_get_entropy_fn rand_get_entropy;
2160 static OSSL_FUNC_get_user_entropy_fn rand_get_user_entropy;
2161 static OSSL_FUNC_cleanup_entropy_fn rand_cleanup_entropy;
2162 static OSSL_FUNC_cleanup_user_entropy_fn rand_cleanup_user_entropy;
2163 static OSSL_FUNC_get_nonce_fn rand_get_nonce;
2164 static OSSL_FUNC_get_user_nonce_fn rand_get_user_nonce;
2165 static OSSL_FUNC_cleanup_nonce_fn rand_cleanup_nonce;
2166 static OSSL_FUNC_cleanup_user_nonce_fn rand_cleanup_user_nonce;
2167 #endif
2168 OSSL_FUNC_CRYPTO_malloc_fn CRYPTO_malloc;
2169 OSSL_FUNC_CRYPTO_zalloc_fn CRYPTO_zalloc;
2170 OSSL_FUNC_CRYPTO_free_fn CRYPTO_free;
2171 OSSL_FUNC_CRYPTO_clear_free_fn CRYPTO_clear_free;
2172 OSSL_FUNC_CRYPTO_realloc_fn CRYPTO_realloc;
2173 OSSL_FUNC_CRYPTO_clear_realloc_fn CRYPTO_clear_realloc;
2174 OSSL_FUNC_CRYPTO_secure_malloc_fn CRYPTO_secure_malloc;
2175 OSSL_FUNC_CRYPTO_secure_zalloc_fn CRYPTO_secure_zalloc;
2176 OSSL_FUNC_CRYPTO_secure_free_fn CRYPTO_secure_free;
2177 OSSL_FUNC_CRYPTO_secure_clear_free_fn CRYPTO_secure_clear_free;
2178 OSSL_FUNC_CRYPTO_secure_allocated_fn CRYPTO_secure_allocated;
2179 OSSL_FUNC_OPENSSL_cleanse_fn OPENSSL_cleanse;
2180 #ifndef FIPS_MODULE
2181 OSSL_FUNC_provider_register_child_cb_fn ossl_provider_register_child_cb;
2182 OSSL_FUNC_provider_deregister_child_cb_fn ossl_provider_deregister_child_cb;
2183 static OSSL_FUNC_provider_name_fn core_provider_get0_name;
2184 static OSSL_FUNC_provider_get0_provider_ctx_fn core_provider_get0_provider_ctx;
2185 static OSSL_FUNC_provider_get0_dispatch_fn core_provider_get0_dispatch;
2186 static OSSL_FUNC_provider_up_ref_fn core_provider_up_ref_intern;
2187 static OSSL_FUNC_provider_free_fn core_provider_free_intern;
2188 static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid;
2189 static OSSL_FUNC_core_obj_create_fn core_obj_create;
2190 #endif
2191
core_gettable_params(const OSSL_CORE_HANDLE * handle)2192 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
2193 {
2194 return param_types;
2195 }
2196
core_get_params(const OSSL_CORE_HANDLE * handle,OSSL_PARAM params[])2197 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
2198 {
2199 int i;
2200 OSSL_PARAM *p;
2201 /*
2202 * We created this object originally and we know it is actually an
2203 * OSSL_PROVIDER *, so the cast is safe
2204 */
2205 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2206
2207 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
2208 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
2209 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
2210 OSSL_PARAM_set_utf8_ptr(p, prov->name);
2211
2212 #ifndef FIPS_MODULE
2213 if ((p = OSSL_PARAM_locate(params,
2214 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
2215 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
2216 #endif
2217
2218 if (prov->parameters == NULL)
2219 return 1;
2220
2221 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
2222 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
2223
2224 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
2225 OSSL_PARAM_set_utf8_ptr(p, pair->value);
2226 }
2227 return 1;
2228 }
2229
core_get_libctx(const OSSL_CORE_HANDLE * handle)2230 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
2231 {
2232 /*
2233 * We created this object originally and we know it is actually an
2234 * OSSL_PROVIDER *, so the cast is safe
2235 */
2236 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2237
2238 /*
2239 * Using ossl_provider_libctx would be wrong as that returns
2240 * NULL for |prov| == NULL and NULL libctx has a special meaning
2241 * that does not apply here. Here |prov| == NULL can happen only in
2242 * case of a coding error.
2243 */
2244 assert(prov != NULL);
2245 return (OPENSSL_CORE_CTX *)prov->libctx;
2246 }
2247
core_thread_start(const OSSL_CORE_HANDLE * handle,OSSL_thread_stop_handler_fn handfn,void * arg)2248 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
2249 OSSL_thread_stop_handler_fn handfn,
2250 void *arg)
2251 {
2252 /*
2253 * We created this object originally and we know it is actually an
2254 * OSSL_PROVIDER *, so the cast is safe
2255 */
2256 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2257
2258 return ossl_init_thread_start(prov, arg, handfn);
2259 }
2260
2261 /*
2262 * The FIPS module inner provider doesn't implement these. They aren't
2263 * needed there, since the FIPS module upcalls are always the outer provider
2264 * ones.
2265 */
2266 #ifndef FIPS_MODULE
2267 /*
2268 * These error functions should use |handle| to select the proper
2269 * library context to report in the correct error stack if error
2270 * stacks become tied to the library context.
2271 * We cannot currently do that since there's no support for it in the
2272 * ERR subsystem.
2273 */
core_new_error(const OSSL_CORE_HANDLE * handle)2274 static void core_new_error(const OSSL_CORE_HANDLE *handle)
2275 {
2276 ERR_new();
2277 }
2278
core_set_error_debug(const OSSL_CORE_HANDLE * handle,const char * file,int line,const char * func)2279 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
2280 const char *file, int line, const char *func)
2281 {
2282 ERR_set_debug(file, line, func);
2283 }
2284
core_vset_error(const OSSL_CORE_HANDLE * handle,uint32_t reason,const char * fmt,va_list args)2285 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
2286 uint32_t reason, const char *fmt, va_list args)
2287 {
2288 /*
2289 * We created this object originally and we know it is actually an
2290 * OSSL_PROVIDER *, so the cast is safe
2291 */
2292 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
2293
2294 /*
2295 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
2296 * error and will be treated as such. Otherwise, it's a new style
2297 * provider error and will be treated as such.
2298 */
2299 if (ERR_GET_LIB(reason) != 0) {
2300 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
2301 } else {
2302 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
2303 }
2304 }
2305
core_set_error_mark(const OSSL_CORE_HANDLE * handle)2306 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
2307 {
2308 return ERR_set_mark();
2309 }
2310
core_clear_last_error_mark(const OSSL_CORE_HANDLE * handle)2311 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
2312 {
2313 return ERR_clear_last_mark();
2314 }
2315
core_pop_error_to_mark(const OSSL_CORE_HANDLE * handle)2316 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
2317 {
2318 return ERR_pop_to_mark();
2319 }
2320
core_indicator_get_callback(OPENSSL_CORE_CTX * libctx,OSSL_INDICATOR_CALLBACK ** cb)2321 static void core_indicator_get_callback(OPENSSL_CORE_CTX *libctx,
2322 OSSL_INDICATOR_CALLBACK **cb)
2323 {
2324 OSSL_INDICATOR_get_callback((OSSL_LIB_CTX *)libctx, cb);
2325 }
2326
core_self_test_get_callback(OPENSSL_CORE_CTX * libctx,OSSL_CALLBACK ** cb,void ** cbarg)2327 static void core_self_test_get_callback(OPENSSL_CORE_CTX *libctx,
2328 OSSL_CALLBACK **cb, void **cbarg)
2329 {
2330 OSSL_SELF_TEST_get_callback((OSSL_LIB_CTX *)libctx, cb, cbarg);
2331 }
2332
rand_get_entropy(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,int entropy,size_t min_len,size_t max_len)2333 static size_t rand_get_entropy(const OSSL_CORE_HANDLE *handle,
2334 unsigned char **pout, int entropy,
2335 size_t min_len, size_t max_len)
2336 {
2337 return ossl_rand_get_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2338 pout, entropy, min_len, max_len);
2339 }
2340
rand_get_user_entropy(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,int entropy,size_t min_len,size_t max_len)2341 static size_t rand_get_user_entropy(const OSSL_CORE_HANDLE *handle,
2342 unsigned char **pout, int entropy,
2343 size_t min_len, size_t max_len)
2344 {
2345 return ossl_rand_get_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2346 pout, entropy, min_len, max_len);
2347 }
2348
rand_cleanup_entropy(const OSSL_CORE_HANDLE * handle,unsigned char * buf,size_t len)2349 static void rand_cleanup_entropy(const OSSL_CORE_HANDLE *handle,
2350 unsigned char *buf, size_t len)
2351 {
2352 ossl_rand_cleanup_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2353 buf, len);
2354 }
2355
rand_cleanup_user_entropy(const OSSL_CORE_HANDLE * handle,unsigned char * buf,size_t len)2356 static void rand_cleanup_user_entropy(const OSSL_CORE_HANDLE *handle,
2357 unsigned char *buf, size_t len)
2358 {
2359 ossl_rand_cleanup_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle),
2360 buf, len);
2361 }
2362
rand_get_nonce(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,size_t min_len,size_t max_len,const void * salt,size_t salt_len)2363 static size_t rand_get_nonce(const OSSL_CORE_HANDLE *handle,
2364 unsigned char **pout,
2365 size_t min_len, size_t max_len,
2366 const void *salt, size_t salt_len)
2367 {
2368 return ossl_rand_get_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2369 pout, min_len, max_len, salt, salt_len);
2370 }
2371
rand_get_user_nonce(const OSSL_CORE_HANDLE * handle,unsigned char ** pout,size_t min_len,size_t max_len,const void * salt,size_t salt_len)2372 static size_t rand_get_user_nonce(const OSSL_CORE_HANDLE *handle,
2373 unsigned char **pout,
2374 size_t min_len, size_t max_len,
2375 const void *salt, size_t salt_len)
2376 {
2377 return ossl_rand_get_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2378 pout, min_len, max_len, salt, salt_len);
2379 }
2380
rand_cleanup_nonce(const OSSL_CORE_HANDLE * handle,unsigned char * buf,size_t len)2381 static void rand_cleanup_nonce(const OSSL_CORE_HANDLE *handle,
2382 unsigned char *buf, size_t len)
2383 {
2384 ossl_rand_cleanup_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2385 buf, len);
2386 }
2387
rand_cleanup_user_nonce(const OSSL_CORE_HANDLE * handle,unsigned char * buf,size_t len)2388 static void rand_cleanup_user_nonce(const OSSL_CORE_HANDLE *handle,
2389 unsigned char *buf, size_t len)
2390 {
2391 ossl_rand_cleanup_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle),
2392 buf, len);
2393 }
2394
core_provider_get0_name(const OSSL_CORE_HANDLE * prov)2395 static const char *core_provider_get0_name(const OSSL_CORE_HANDLE *prov)
2396 {
2397 return OSSL_PROVIDER_get0_name((const OSSL_PROVIDER *)prov);
2398 }
2399
core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE * prov)2400 static void *core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE *prov)
2401 {
2402 return OSSL_PROVIDER_get0_provider_ctx((const OSSL_PROVIDER *)prov);
2403 }
2404
2405 static const OSSL_DISPATCH *
core_provider_get0_dispatch(const OSSL_CORE_HANDLE * prov)2406 core_provider_get0_dispatch(const OSSL_CORE_HANDLE *prov)
2407 {
2408 return OSSL_PROVIDER_get0_dispatch((const OSSL_PROVIDER *)prov);
2409 }
2410
core_provider_up_ref_intern(const OSSL_CORE_HANDLE * prov,int activate)2411 static int core_provider_up_ref_intern(const OSSL_CORE_HANDLE *prov,
2412 int activate)
2413 {
2414 return provider_up_ref_intern((OSSL_PROVIDER *)prov, activate);
2415 }
2416
core_provider_free_intern(const OSSL_CORE_HANDLE * prov,int deactivate)2417 static int core_provider_free_intern(const OSSL_CORE_HANDLE *prov,
2418 int deactivate)
2419 {
2420 return provider_free_intern((OSSL_PROVIDER *)prov, deactivate);
2421 }
2422
core_obj_add_sigid(const OSSL_CORE_HANDLE * prov,const char * sign_name,const char * digest_name,const char * pkey_name)2423 static int core_obj_add_sigid(const OSSL_CORE_HANDLE *prov,
2424 const char *sign_name, const char *digest_name,
2425 const char *pkey_name)
2426 {
2427 int sign_nid = OBJ_txt2nid(sign_name);
2428 int digest_nid = NID_undef;
2429 int pkey_nid = OBJ_txt2nid(pkey_name);
2430
2431 if (digest_name != NULL && digest_name[0] != '\0'
2432 && (digest_nid = OBJ_txt2nid(digest_name)) == NID_undef)
2433 return 0;
2434
2435 if (sign_nid == NID_undef)
2436 return 0;
2437
2438 /*
2439 * Check if it already exists. This is a success if so (even if we don't
2440 * have nids for the digest/pkey)
2441 */
2442 if (OBJ_find_sigid_algs(sign_nid, NULL, NULL))
2443 return 1;
2444
2445 if (pkey_nid == NID_undef)
2446 return 0;
2447
2448 return OBJ_add_sigid(sign_nid, digest_nid, pkey_nid);
2449 }
2450
core_obj_create(const OSSL_CORE_HANDLE * prov,const char * oid,const char * sn,const char * ln)2451 static int core_obj_create(const OSSL_CORE_HANDLE *prov, const char *oid,
2452 const char *sn, const char *ln)
2453 {
2454 /* Check if it already exists and create it if not */
2455 return OBJ_txt2nid(oid) != NID_undef
2456 || OBJ_create(oid, sn, ln) != NID_undef;
2457 }
2458 #endif /* FIPS_MODULE */
2459
2460 /*
2461 * Functions provided by the core.
2462 */
2463 static const OSSL_DISPATCH core_dispatch_[] = {
2464 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
2465 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
2466 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
2467 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
2468 #ifndef FIPS_MODULE
2469 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
2470 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
2471 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
2472 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
2473 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
2474 (void (*)(void))core_clear_last_error_mark },
2475 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
2476 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
2477 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
2478 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
2479 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
2480 { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
2481 { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
2482 { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
2483 { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
2484 { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
2485 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
2486 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
2487 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))core_self_test_get_callback },
2488 { OSSL_FUNC_INDICATOR_CB, (void (*)(void))core_indicator_get_callback },
2489 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))rand_get_entropy },
2490 { OSSL_FUNC_GET_USER_ENTROPY, (void (*)(void))rand_get_user_entropy },
2491 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))rand_cleanup_entropy },
2492 { OSSL_FUNC_CLEANUP_USER_ENTROPY, (void (*)(void))rand_cleanup_user_entropy },
2493 { OSSL_FUNC_GET_NONCE, (void (*)(void))rand_get_nonce },
2494 { OSSL_FUNC_GET_USER_NONCE, (void (*)(void))rand_get_user_nonce },
2495 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))rand_cleanup_nonce },
2496 { OSSL_FUNC_CLEANUP_USER_NONCE, (void (*)(void))rand_cleanup_user_nonce },
2497 #endif
2498 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
2499 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
2500 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
2501 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
2502 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
2503 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
2504 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
2505 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
2506 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
2507 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
2508 (void (*)(void))CRYPTO_secure_clear_free },
2509 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
2510 (void (*)(void))CRYPTO_secure_allocated },
2511 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
2512 #ifndef FIPS_MODULE
2513 { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
2514 (void (*)(void))ossl_provider_register_child_cb },
2515 { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
2516 (void (*)(void))ossl_provider_deregister_child_cb },
2517 { OSSL_FUNC_PROVIDER_NAME,
2518 (void (*)(void))core_provider_get0_name },
2519 { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
2520 (void (*)(void))core_provider_get0_provider_ctx },
2521 { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
2522 (void (*)(void))core_provider_get0_dispatch },
2523 { OSSL_FUNC_PROVIDER_UP_REF,
2524 (void (*)(void))core_provider_up_ref_intern },
2525 { OSSL_FUNC_PROVIDER_FREE,
2526 (void (*)(void))core_provider_free_intern },
2527 { OSSL_FUNC_CORE_OBJ_ADD_SIGID, (void (*)(void))core_obj_add_sigid },
2528 { OSSL_FUNC_CORE_OBJ_CREATE, (void (*)(void))core_obj_create },
2529 #endif
2530 OSSL_DISPATCH_END
2531 };
2532 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;
2533