1 /*
2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <assert.h>
11 #include <openssl/core.h>
12 #include <openssl/core_dispatch.h>
13 #include <openssl/core_names.h>
14 #include <openssl/provider.h>
15 #include <openssl/params.h>
16 #include <openssl/opensslv.h>
17 #include "crypto/cryptlib.h"
18 #include "crypto/evp.h" /* evp_method_store_flush */
19 #include "crypto/rand.h"
20 #include "internal/nelem.h"
21 #include "internal/thread_once.h"
22 #include "internal/provider.h"
23 #include "internal/refcount.h"
24 #include "internal/bio.h"
25 #include "internal/core.h"
26 #include "provider_local.h"
27 #ifndef FIPS_MODULE
28 # include <openssl/self_test.h>
29 #endif
30
31 /*
32 * This file defines and uses a number of different structures:
33 *
34 * OSSL_PROVIDER (provider_st): Used to represent all information related to a
35 * single instance of a provider.
36 *
37 * provider_store_st: Holds information about the collection of providers that
38 * are available within the current library context (OSSL_LIB_CTX). It also
39 * holds configuration information about providers that could be loaded at some
40 * future point.
41 *
42 * OSSL_PROVIDER_CHILD_CB: An instance of this structure holds the callbacks
43 * that have been registered for a child library context and the associated
44 * provider that registered those callbacks.
45 *
46 * Where a child library context exists then it has its own instance of the
47 * provider store. Each provider that exists in the parent provider store, has
48 * an associated child provider in the child library context's provider store.
49 * As providers get activated or deactivated this needs to be mirrored in the
50 * associated child providers.
51 *
52 * LOCKING
53 * =======
54 *
55 * There are a number of different locks used in this file and it is important
56 * to understand how they should be used in order to avoid deadlocks.
57 *
58 * Fields within a structure can often be "write once" on creation, and then
59 * "read many". Creation of a structure is done by a single thread, and
60 * therefore no lock is required for the "write once/read many" fields. It is
61 * safe for multiple threads to read these fields without a lock, because they
62 * will never be changed.
63 *
64 * However some fields may be changed after a structure has been created and
65 * shared between multiple threads. Where this is the case a lock is required.
66 *
67 * The locks available are:
68 *
69 * The provider flag_lock: Used to control updates to the various provider
70 * "flags" (flag_initialized, flag_activated, flag_fallback) and associated
71 * "counts" (activatecnt).
72 *
73 * The provider refcnt_lock: Only ever used to control updates to the provider
74 * refcnt value.
75 *
76 * The provider optbits_lock: Used to control access to the provider's
77 * operation_bits and operation_bits_sz fields.
78 *
79 * The store default_path_lock: Used to control access to the provider store's
80 * default search path value (default_path)
81 *
82 * The store lock: Used to control the stack of provider's held within the
83 * provider store, as well as the stack of registered child provider callbacks.
84 *
85 * As a general rule-of-thumb it is best to:
86 * - keep the scope of the code that is protected by a lock to the absolute
87 * minimum possible;
88 * - try to keep the scope of the lock to within a single function (i.e. avoid
89 * making calls to other functions while holding a lock);
90 * - try to only ever hold one lock at a time.
91 *
92 * Unfortunately, it is not always possible to stick to the above guidelines.
93 * Where they are not adhered to there is always a danger of inadvertently
94 * introducing the possibility of deadlock. The following rules MUST be adhered
95 * to in order to avoid that:
96 * - Holding multiple locks at the same time is only allowed for the
97 * provider store lock, the provider flag_lock and the provider refcnt_lock.
98 * - When holding multiple locks they must be acquired in the following order of
99 * precedence:
100 * 1) provider store lock
101 * 2) provider flag_lock
102 * 3) provider refcnt_lock
103 * - When releasing locks they must be released in the reverse order to which
104 * they were acquired
105 * - No locks may be held when making an upcall. NOTE: Some common functions
106 * can make upcalls as part of their normal operation. If you need to call
107 * some other function while holding a lock make sure you know whether it
108 * will make any upcalls or not. For example ossl_provider_up_ref() can call
109 * ossl_provider_up_ref_parent() which can call the c_prov_up_ref() upcall.
110 * - It is permissible to hold the store and flag locks when calling child
111 * provider callbacks. No other locks may be held during such callbacks.
112 */
113
114 static OSSL_PROVIDER *provider_new(const char *name,
115 OSSL_provider_init_fn *init_function,
116 STACK_OF(INFOPAIR) *parameters);
117
118 /*-
119 * Provider Object structure
120 * =========================
121 */
122
123 #ifndef FIPS_MODULE
124 typedef struct {
125 OSSL_PROVIDER *prov;
126 int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
127 int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata);
128 int (*global_props_cb)(const char *props, void *cbdata);
129 void *cbdata;
130 } OSSL_PROVIDER_CHILD_CB;
131 DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB)
132 #endif
133
134 struct provider_store_st; /* Forward declaration */
135
136 struct ossl_provider_st {
137 /* Flag bits */
138 unsigned int flag_initialized:1;
139 unsigned int flag_activated:1;
140 unsigned int flag_fallback:1; /* Can be used as fallback */
141
142 /* Getting and setting the flags require synchronization */
143 CRYPTO_RWLOCK *flag_lock;
144
145 /* OpenSSL library side data */
146 CRYPTO_REF_COUNT refcnt;
147 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
148 int activatecnt;
149 char *name;
150 char *path;
151 DSO *module;
152 OSSL_provider_init_fn *init_function;
153 STACK_OF(INFOPAIR) *parameters;
154 OSSL_LIB_CTX *libctx; /* The library context this instance is in */
155 struct provider_store_st *store; /* The store this instance belongs to */
156 #ifndef FIPS_MODULE
157 /*
158 * In the FIPS module inner provider, this isn't needed, since the
159 * error upcalls are always direct calls to the outer provider.
160 */
161 int error_lib; /* ERR library number, one for each provider */
162 # ifndef OPENSSL_NO_ERR
163 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
164 # endif
165 #endif
166
167 /* Provider side functions */
168 OSSL_FUNC_provider_teardown_fn *teardown;
169 OSSL_FUNC_provider_gettable_params_fn *gettable_params;
170 OSSL_FUNC_provider_get_params_fn *get_params;
171 OSSL_FUNC_provider_get_capabilities_fn *get_capabilities;
172 OSSL_FUNC_provider_self_test_fn *self_test;
173 OSSL_FUNC_provider_query_operation_fn *query_operation;
174 OSSL_FUNC_provider_unquery_operation_fn *unquery_operation;
175
176 /*
177 * Cache of bit to indicate of query_operation() has been called on
178 * a specific operation or not.
179 */
180 unsigned char *operation_bits;
181 size_t operation_bits_sz;
182 CRYPTO_RWLOCK *opbits_lock;
183
184 #ifndef FIPS_MODULE
185 /* Whether this provider is the child of some other provider */
186 const OSSL_CORE_HANDLE *handle;
187 unsigned int ischild:1;
188 #endif
189
190 /* Provider side data */
191 void *provctx;
192 const OSSL_DISPATCH *dispatch;
193 };
DEFINE_STACK_OF(OSSL_PROVIDER)194 DEFINE_STACK_OF(OSSL_PROVIDER)
195
196 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
197 const OSSL_PROVIDER * const *b)
198 {
199 return strcmp((*a)->name, (*b)->name);
200 }
201
202 /*-
203 * Provider Object store
204 * =====================
205 *
206 * The Provider Object store is a library context object, and therefore needs
207 * an index.
208 */
209
210 struct provider_store_st {
211 OSSL_LIB_CTX *libctx;
212 STACK_OF(OSSL_PROVIDER) *providers;
213 STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs;
214 CRYPTO_RWLOCK *default_path_lock;
215 CRYPTO_RWLOCK *lock;
216 char *default_path;
217 OSSL_PROVIDER_INFO *provinfo;
218 size_t numprovinfo;
219 size_t provinfosz;
220 unsigned int use_fallbacks:1;
221 unsigned int freeing:1;
222 };
223
224 /*
225 * provider_deactivate_free() is a wrapper around ossl_provider_deactivate()
226 * and ossl_provider_free(), called as needed.
227 * Since this is only called when the provider store is being emptied, we
228 * don't need to care about any lock.
229 */
provider_deactivate_free(OSSL_PROVIDER * prov)230 static void provider_deactivate_free(OSSL_PROVIDER *prov)
231 {
232 if (prov->flag_activated)
233 ossl_provider_deactivate(prov, 1);
234 ossl_provider_free(prov);
235 }
236
237 #ifndef FIPS_MODULE
ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB * cb)238 static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb)
239 {
240 OPENSSL_free(cb);
241 }
242 #endif
243
infopair_free(INFOPAIR * pair)244 static void infopair_free(INFOPAIR *pair)
245 {
246 OPENSSL_free(pair->name);
247 OPENSSL_free(pair->value);
248 OPENSSL_free(pair);
249 }
250
infopair_copy(const INFOPAIR * src)251 static INFOPAIR *infopair_copy(const INFOPAIR *src)
252 {
253 INFOPAIR *dest = OPENSSL_zalloc(sizeof(*dest));
254
255 if (dest == NULL)
256 return NULL;
257 if (src->name != NULL) {
258 dest->name = OPENSSL_strdup(src->name);
259 if (dest->name == NULL)
260 goto err;
261 }
262 if (src->value != NULL) {
263 dest->value = OPENSSL_strdup(src->value);
264 if (dest->value == NULL)
265 goto err;
266 }
267 return dest;
268 err:
269 OPENSSL_free(dest->name);
270 OPENSSL_free(dest);
271 return NULL;
272 }
273
ossl_provider_info_clear(OSSL_PROVIDER_INFO * info)274 void ossl_provider_info_clear(OSSL_PROVIDER_INFO *info)
275 {
276 OPENSSL_free(info->name);
277 OPENSSL_free(info->path);
278 sk_INFOPAIR_pop_free(info->parameters, infopair_free);
279 }
280
provider_store_free(void * vstore)281 static void provider_store_free(void *vstore)
282 {
283 struct provider_store_st *store = vstore;
284 size_t i;
285
286 if (store == NULL)
287 return;
288 store->freeing = 1;
289 OPENSSL_free(store->default_path);
290 sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free);
291 #ifndef FIPS_MODULE
292 sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs,
293 ossl_provider_child_cb_free);
294 #endif
295 CRYPTO_THREAD_lock_free(store->default_path_lock);
296 CRYPTO_THREAD_lock_free(store->lock);
297 for (i = 0; i < store->numprovinfo; i++)
298 ossl_provider_info_clear(&store->provinfo[i]);
299 OPENSSL_free(store->provinfo);
300 OPENSSL_free(store);
301 }
302
provider_store_new(OSSL_LIB_CTX * ctx)303 static void *provider_store_new(OSSL_LIB_CTX *ctx)
304 {
305 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
306
307 if (store == NULL
308 || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
309 || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL
310 #ifndef FIPS_MODULE
311 || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL
312 #endif
313 || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
314 provider_store_free(store);
315 return NULL;
316 }
317 store->libctx = ctx;
318 store->use_fallbacks = 1;
319
320 return store;
321 }
322
323 static const OSSL_LIB_CTX_METHOD provider_store_method = {
324 /* Needs to be freed before the child provider data is freed */
325 OSSL_LIB_CTX_METHOD_PRIORITY_1,
326 provider_store_new,
327 provider_store_free,
328 };
329
get_provider_store(OSSL_LIB_CTX * libctx)330 static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx)
331 {
332 struct provider_store_st *store = NULL;
333
334 store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX,
335 &provider_store_method);
336 if (store == NULL)
337 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
338 return store;
339 }
340
ossl_provider_disable_fallback_loading(OSSL_LIB_CTX * libctx)341 int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx)
342 {
343 struct provider_store_st *store;
344
345 if ((store = get_provider_store(libctx)) != NULL) {
346 if (!CRYPTO_THREAD_write_lock(store->lock))
347 return 0;
348 store->use_fallbacks = 0;
349 CRYPTO_THREAD_unlock(store->lock);
350 return 1;
351 }
352 return 0;
353 }
354
355 #define BUILTINS_BLOCK_SIZE 10
356
ossl_provider_info_add_to_store(OSSL_LIB_CTX * libctx,OSSL_PROVIDER_INFO * entry)357 int ossl_provider_info_add_to_store(OSSL_LIB_CTX *libctx,
358 OSSL_PROVIDER_INFO *entry)
359 {
360 struct provider_store_st *store = get_provider_store(libctx);
361 int ret = 0;
362
363 if (entry->name == NULL) {
364 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
365 return 0;
366 }
367
368 if (store == NULL) {
369 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
370 return 0;
371 }
372
373 if (!CRYPTO_THREAD_write_lock(store->lock))
374 return 0;
375 if (store->provinfosz == 0) {
376 store->provinfo = OPENSSL_zalloc(sizeof(*store->provinfo)
377 * BUILTINS_BLOCK_SIZE);
378 if (store->provinfo == NULL) {
379 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
380 goto err;
381 }
382 store->provinfosz = BUILTINS_BLOCK_SIZE;
383 } else if (store->numprovinfo == store->provinfosz) {
384 OSSL_PROVIDER_INFO *tmpbuiltins;
385 size_t newsz = store->provinfosz + BUILTINS_BLOCK_SIZE;
386
387 tmpbuiltins = OPENSSL_realloc(store->provinfo,
388 sizeof(*store->provinfo) * newsz);
389 if (tmpbuiltins == NULL) {
390 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
391 goto err;
392 }
393 store->provinfo = tmpbuiltins;
394 store->provinfosz = newsz;
395 }
396 store->provinfo[store->numprovinfo] = *entry;
397 store->numprovinfo++;
398
399 ret = 1;
400 err:
401 CRYPTO_THREAD_unlock(store->lock);
402 return ret;
403 }
404
ossl_provider_find(OSSL_LIB_CTX * libctx,const char * name,int noconfig)405 OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
406 int noconfig)
407 {
408 struct provider_store_st *store = NULL;
409 OSSL_PROVIDER *prov = NULL;
410
411 if ((store = get_provider_store(libctx)) != NULL) {
412 OSSL_PROVIDER tmpl = { 0, };
413 int i;
414
415 #ifndef FIPS_MODULE
416 /*
417 * Make sure any providers are loaded from config before we try to find
418 * them.
419 */
420 if (!noconfig) {
421 if (ossl_lib_ctx_is_default(libctx))
422 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
423 }
424 #endif
425
426 tmpl.name = (char *)name;
427 /*
428 * A "find" operation can sort the stack, and therefore a write lock is
429 * required.
430 */
431 if (!CRYPTO_THREAD_write_lock(store->lock))
432 return NULL;
433 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) != -1)
434 prov = sk_OSSL_PROVIDER_value(store->providers, i);
435 CRYPTO_THREAD_unlock(store->lock);
436 if (prov != NULL && !ossl_provider_up_ref(prov))
437 prov = NULL;
438 }
439
440 return prov;
441 }
442
443 /*-
444 * Provider Object methods
445 * =======================
446 */
447
provider_new(const char * name,OSSL_provider_init_fn * init_function,STACK_OF (INFOPAIR)* parameters)448 static OSSL_PROVIDER *provider_new(const char *name,
449 OSSL_provider_init_fn *init_function,
450 STACK_OF(INFOPAIR) *parameters)
451 {
452 OSSL_PROVIDER *prov = NULL;
453
454 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
455 #ifndef HAVE_ATOMICS
456 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
457 #endif
458 || (prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL
459 || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL
460 || (prov->name = OPENSSL_strdup(name)) == NULL
461 || (prov->parameters = sk_INFOPAIR_deep_copy(parameters,
462 infopair_copy,
463 infopair_free)) == NULL) {
464 ossl_provider_free(prov);
465 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
466 return NULL;
467 }
468
469 prov->refcnt = 1; /* 1 One reference to be returned */
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, prov->refcnt_lock) <= 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,int noconfig)518 OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name,
519 OSSL_provider_init_fn *init_function,
520 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 builtin 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 /* provider_new() generates an error, so no need here */
558 if ((prov = provider_new(name, template.init, template.parameters)) == NULL)
559 return NULL;
560
561 prov->libctx = libctx;
562 #ifndef FIPS_MODULE
563 prov->error_lib = ERR_get_next_error_library();
564 #endif
565
566 /*
567 * At this point, the provider is only partially "loaded". To be
568 * fully "loaded", ossl_provider_activate() must also be called and it must
569 * then be added to the provider store.
570 */
571
572 return prov;
573 }
574
575 /* Assumes that the store lock is held */
create_provider_children(OSSL_PROVIDER * prov)576 static int create_provider_children(OSSL_PROVIDER *prov)
577 {
578 int ret = 1;
579 #ifndef FIPS_MODULE
580 struct provider_store_st *store = prov->store;
581 OSSL_PROVIDER_CHILD_CB *child_cb;
582 int i, max;
583
584 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
585 for (i = 0; i < max; i++) {
586 /*
587 * This is newly activated (activatecnt == 1), so we need to
588 * create child providers as necessary.
589 */
590 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
591 ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
592 }
593 #endif
594
595 return ret;
596 }
597
ossl_provider_add_to_store(OSSL_PROVIDER * prov,OSSL_PROVIDER ** actualprov,int retain_fallbacks)598 int ossl_provider_add_to_store(OSSL_PROVIDER *prov, OSSL_PROVIDER **actualprov,
599 int retain_fallbacks)
600 {
601 struct provider_store_st *store;
602 int idx;
603 OSSL_PROVIDER tmpl = { 0, };
604 OSSL_PROVIDER *actualtmp = NULL;
605
606 if (actualprov != NULL)
607 *actualprov = NULL;
608
609 if ((store = get_provider_store(prov->libctx)) == NULL)
610 return 0;
611
612 if (!CRYPTO_THREAD_write_lock(store->lock))
613 return 0;
614
615 tmpl.name = (char *)prov->name;
616 idx = sk_OSSL_PROVIDER_find(store->providers, &tmpl);
617 if (idx == -1)
618 actualtmp = prov;
619 else
620 actualtmp = sk_OSSL_PROVIDER_value(store->providers, idx);
621
622 if (idx == -1) {
623 if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0)
624 goto err;
625 prov->store = store;
626 if (!create_provider_children(prov)) {
627 sk_OSSL_PROVIDER_delete_ptr(store->providers, prov);
628 goto err;
629 }
630 if (!retain_fallbacks)
631 store->use_fallbacks = 0;
632 }
633
634 CRYPTO_THREAD_unlock(store->lock);
635
636 if (actualprov != NULL) {
637 if (!ossl_provider_up_ref(actualtmp)) {
638 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
639 actualtmp = NULL;
640 goto err;
641 }
642 *actualprov = actualtmp;
643 }
644
645 if (idx >= 0) {
646 /*
647 * The provider is already in the store. Probably two threads
648 * independently initialised their own provider objects with the same
649 * name and raced to put them in the store. This thread lost. We
650 * deactivate the one we just created and use the one that already
651 * exists instead.
652 * If we get here then we know we did not create provider children
653 * above, so we inform ossl_provider_deactivate not to attempt to remove
654 * any.
655 */
656 ossl_provider_deactivate(prov, 0);
657 ossl_provider_free(prov);
658 }
659
660 return 1;
661
662 err:
663 CRYPTO_THREAD_unlock(store->lock);
664 if (actualprov != NULL)
665 ossl_provider_free(*actualprov);
666 return 0;
667 }
668
ossl_provider_free(OSSL_PROVIDER * prov)669 void ossl_provider_free(OSSL_PROVIDER *prov)
670 {
671 if (prov != NULL) {
672 int ref = 0;
673
674 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
675
676 /*
677 * When the refcount drops to zero, we clean up the provider.
678 * Note that this also does teardown, which may seem late,
679 * considering that init happens on first activation. However,
680 * there may be other structures hanging on to the provider after
681 * the last deactivation and may therefore need full access to the
682 * provider's services. Therefore, we deinit late.
683 */
684 if (ref == 0) {
685 if (prov->flag_initialized) {
686 ossl_provider_teardown(prov);
687 #ifndef OPENSSL_NO_ERR
688 # ifndef FIPS_MODULE
689 if (prov->error_strings != NULL) {
690 ERR_unload_strings(prov->error_lib, prov->error_strings);
691 OPENSSL_free(prov->error_strings);
692 prov->error_strings = NULL;
693 }
694 # endif
695 #endif
696 OPENSSL_free(prov->operation_bits);
697 prov->operation_bits = NULL;
698 prov->operation_bits_sz = 0;
699 prov->flag_initialized = 0;
700 }
701
702 #ifndef FIPS_MODULE
703 /*
704 * We deregister thread handling whether or not the provider was
705 * initialized. If init was attempted but was not successful then
706 * the provider may still have registered a thread handler.
707 */
708 ossl_init_thread_deregister(prov);
709 DSO_free(prov->module);
710 #endif
711 OPENSSL_free(prov->name);
712 OPENSSL_free(prov->path);
713 sk_INFOPAIR_pop_free(prov->parameters, infopair_free);
714 CRYPTO_THREAD_lock_free(prov->opbits_lock);
715 CRYPTO_THREAD_lock_free(prov->flag_lock);
716 #ifndef HAVE_ATOMICS
717 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
718 #endif
719 OPENSSL_free(prov);
720 }
721 #ifndef FIPS_MODULE
722 else if (prov->ischild) {
723 ossl_provider_free_parent(prov, 0);
724 }
725 #endif
726 }
727 }
728
729 /* Setters */
ossl_provider_set_module_path(OSSL_PROVIDER * prov,const char * module_path)730 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
731 {
732 OPENSSL_free(prov->path);
733 prov->path = NULL;
734 if (module_path == NULL)
735 return 1;
736 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
737 return 1;
738 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
739 return 0;
740 }
741
infopair_add(STACK_OF (INFOPAIR)** infopairsk,const char * name,const char * value)742 static int infopair_add(STACK_OF(INFOPAIR) **infopairsk, const char *name,
743 const char *value)
744 {
745 INFOPAIR *pair = NULL;
746
747 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
748 && (*infopairsk != NULL
749 || (*infopairsk = sk_INFOPAIR_new_null()) != NULL)
750 && (pair->name = OPENSSL_strdup(name)) != NULL
751 && (pair->value = OPENSSL_strdup(value)) != NULL
752 && sk_INFOPAIR_push(*infopairsk, pair) > 0)
753 return 1;
754
755 if (pair != NULL) {
756 OPENSSL_free(pair->name);
757 OPENSSL_free(pair->value);
758 OPENSSL_free(pair);
759 }
760 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
761 return 0;
762 }
763
ossl_provider_add_parameter(OSSL_PROVIDER * prov,const char * name,const char * value)764 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
765 const char *name, const char *value)
766 {
767 return infopair_add(&prov->parameters, name, value);
768 }
769
ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO * provinfo,const char * name,const char * value)770 int ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO *provinfo,
771 const char *name,
772 const char *value)
773 {
774 return infopair_add(&provinfo->parameters, name, value);
775 }
776
777 /*
778 * Provider activation.
779 *
780 * What "activation" means depends on the provider form; for built in
781 * providers (in the library or the application alike), the provider
782 * can already be considered to be loaded, all that's needed is to
783 * initialize it. However, for dynamically loadable provider modules,
784 * we must first load that module.
785 *
786 * Built in modules are distinguished from dynamically loaded modules
787 * with an already assigned init function.
788 */
789 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
790
OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX * libctx,const char * path)791 int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx,
792 const char *path)
793 {
794 struct provider_store_st *store;
795 char *p = NULL;
796
797 if (path != NULL) {
798 p = OPENSSL_strdup(path);
799 if (p == NULL) {
800 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
801 return 0;
802 }
803 }
804 if ((store = get_provider_store(libctx)) != NULL
805 && CRYPTO_THREAD_write_lock(store->default_path_lock)) {
806 OPENSSL_free(store->default_path);
807 store->default_path = p;
808 CRYPTO_THREAD_unlock(store->default_path_lock);
809 return 1;
810 }
811 OPENSSL_free(p);
812 return 0;
813 }
814
815 /*
816 * Internal version that doesn't affect the store flags, and thereby avoid
817 * locking. Direct callers must remember to set the store flags when
818 * appropriate.
819 */
provider_init(OSSL_PROVIDER * prov)820 static int provider_init(OSSL_PROVIDER *prov)
821 {
822 const OSSL_DISPATCH *provider_dispatch = NULL;
823 void *tmp_provctx = NULL; /* safety measure */
824 #ifndef OPENSSL_NO_ERR
825 # ifndef FIPS_MODULE
826 OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
827 # endif
828 #endif
829 int ok = 0;
830
831 if (!ossl_assert(!prov->flag_initialized)) {
832 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
833 goto end;
834 }
835
836 /*
837 * If the init function isn't set, it indicates that this provider is
838 * a loadable module.
839 */
840 if (prov->init_function == NULL) {
841 #ifdef FIPS_MODULE
842 goto end;
843 #else
844 if (prov->module == NULL) {
845 char *allocated_path = NULL;
846 const char *module_path = NULL;
847 char *merged_path = NULL;
848 const char *load_dir = NULL;
849 char *allocated_load_dir = NULL;
850 struct provider_store_st *store;
851
852 if ((prov->module = DSO_new()) == NULL) {
853 /* DSO_new() generates an error already */
854 goto end;
855 }
856
857 if ((store = get_provider_store(prov->libctx)) == NULL
858 || !CRYPTO_THREAD_read_lock(store->default_path_lock))
859 goto end;
860
861 if (store->default_path != NULL) {
862 allocated_load_dir = OPENSSL_strdup(store->default_path);
863 CRYPTO_THREAD_unlock(store->default_path_lock);
864 if (allocated_load_dir == NULL) {
865 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
866 goto end;
867 }
868 load_dir = allocated_load_dir;
869 } else {
870 CRYPTO_THREAD_unlock(store->default_path_lock);
871 }
872
873 if (load_dir == NULL) {
874 load_dir = ossl_safe_getenv("OPENSSL_MODULES");
875 if (load_dir == NULL)
876 load_dir = MODULESDIR;
877 }
878
879 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
880 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
881
882 module_path = prov->path;
883 if (module_path == NULL)
884 module_path = allocated_path =
885 DSO_convert_filename(prov->module, prov->name);
886 if (module_path != NULL)
887 merged_path = DSO_merge(prov->module, module_path, load_dir);
888
889 if (merged_path == NULL
890 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
891 DSO_free(prov->module);
892 prov->module = NULL;
893 }
894
895 OPENSSL_free(merged_path);
896 OPENSSL_free(allocated_path);
897 OPENSSL_free(allocated_load_dir);
898 }
899
900 if (prov->module != NULL)
901 prov->init_function = (OSSL_provider_init_fn *)
902 DSO_bind_func(prov->module, "OSSL_provider_init");
903 #endif
904 }
905
906 /* Call the initialise function for the provider. */
907 if (prov->init_function == NULL
908 || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
909 &provider_dispatch, &tmp_provctx)) {
910 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL,
911 "name=%s", prov->name);
912 goto end;
913 }
914 prov->provctx = tmp_provctx;
915 prov->dispatch = provider_dispatch;
916
917 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
918 switch (provider_dispatch->function_id) {
919 case OSSL_FUNC_PROVIDER_TEARDOWN:
920 prov->teardown =
921 OSSL_FUNC_provider_teardown(provider_dispatch);
922 break;
923 case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS:
924 prov->gettable_params =
925 OSSL_FUNC_provider_gettable_params(provider_dispatch);
926 break;
927 case OSSL_FUNC_PROVIDER_GET_PARAMS:
928 prov->get_params =
929 OSSL_FUNC_provider_get_params(provider_dispatch);
930 break;
931 case OSSL_FUNC_PROVIDER_SELF_TEST:
932 prov->self_test =
933 OSSL_FUNC_provider_self_test(provider_dispatch);
934 break;
935 case OSSL_FUNC_PROVIDER_GET_CAPABILITIES:
936 prov->get_capabilities =
937 OSSL_FUNC_provider_get_capabilities(provider_dispatch);
938 break;
939 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
940 prov->query_operation =
941 OSSL_FUNC_provider_query_operation(provider_dispatch);
942 break;
943 case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION:
944 prov->unquery_operation =
945 OSSL_FUNC_provider_unquery_operation(provider_dispatch);
946 break;
947 #ifndef OPENSSL_NO_ERR
948 # ifndef FIPS_MODULE
949 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
950 p_get_reason_strings =
951 OSSL_FUNC_provider_get_reason_strings(provider_dispatch);
952 break;
953 # endif
954 #endif
955 }
956 }
957
958 #ifndef OPENSSL_NO_ERR
959 # ifndef FIPS_MODULE
960 if (p_get_reason_strings != NULL) {
961 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
962 size_t cnt, cnt2;
963
964 /*
965 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
966 * although they are essentially the same type.
967 * Furthermore, ERR_load_strings() patches the array's error number
968 * with the error library number, so we need to make a copy of that
969 * array either way.
970 */
971 cnt = 0;
972 while (reasonstrings[cnt].id != 0) {
973 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
974 goto end;
975 cnt++;
976 }
977 cnt++; /* One for the terminating item */
978
979 /* Allocate one extra item for the "library" name */
980 prov->error_strings =
981 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
982 if (prov->error_strings == NULL)
983 goto end;
984
985 /*
986 * Set the "library" name.
987 */
988 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
989 prov->error_strings[0].string = prov->name;
990 /*
991 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
992 * 1..cnt.
993 */
994 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
995 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
996 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
997 }
998
999 ERR_load_strings(prov->error_lib, prov->error_strings);
1000 }
1001 # endif
1002 #endif
1003
1004 /* With this flag set, this provider has become fully "loaded". */
1005 prov->flag_initialized = 1;
1006 ok = 1;
1007
1008 end:
1009 return ok;
1010 }
1011
1012 /*
1013 * Deactivate a provider. If upcalls is 0 then we suppress any upcalls to a
1014 * parent provider. If removechildren is 0 then we suppress any calls to remove
1015 * child providers.
1016 * Return -1 on failure and the activation count on success
1017 */
provider_deactivate(OSSL_PROVIDER * prov,int upcalls,int removechildren)1018 static int provider_deactivate(OSSL_PROVIDER *prov, int upcalls,
1019 int removechildren)
1020 {
1021 int count;
1022 struct provider_store_st *store;
1023 #ifndef FIPS_MODULE
1024 int freeparent = 0;
1025 #endif
1026 int lock = 1;
1027
1028 if (!ossl_assert(prov != NULL))
1029 return -1;
1030
1031 /*
1032 * No need to lock if we've got no store because we've not been shared with
1033 * other threads.
1034 */
1035 store = get_provider_store(prov->libctx);
1036 if (store == NULL)
1037 lock = 0;
1038
1039 if (lock && !CRYPTO_THREAD_read_lock(store->lock))
1040 return -1;
1041 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1042 CRYPTO_THREAD_unlock(store->lock);
1043 return -1;
1044 }
1045
1046 #ifndef FIPS_MODULE
1047 if (prov->activatecnt >= 2 && prov->ischild && upcalls) {
1048 /*
1049 * We have had a direct activation in this child libctx so we need to
1050 * now down the ref count in the parent provider. We do the actual down
1051 * ref outside of the flag_lock, since it could involve getting other
1052 * locks.
1053 */
1054 freeparent = 1;
1055 }
1056 #endif
1057
1058 if ((count = --prov->activatecnt) < 1)
1059 prov->flag_activated = 0;
1060 #ifndef FIPS_MODULE
1061 else
1062 removechildren = 0;
1063 #endif
1064
1065 #ifndef FIPS_MODULE
1066 if (removechildren && store != NULL) {
1067 int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1068 OSSL_PROVIDER_CHILD_CB *child_cb;
1069
1070 for (i = 0; i < max; i++) {
1071 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1072 child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata);
1073 }
1074 }
1075 #endif
1076 if (lock) {
1077 CRYPTO_THREAD_unlock(prov->flag_lock);
1078 CRYPTO_THREAD_unlock(store->lock);
1079 }
1080 #ifndef FIPS_MODULE
1081 if (freeparent)
1082 ossl_provider_free_parent(prov, 1);
1083 #endif
1084
1085 /* We don't deinit here, that's done in ossl_provider_free() */
1086 return count;
1087 }
1088
1089 /*
1090 * Activate a provider.
1091 * Return -1 on failure and the activation count on success
1092 */
provider_activate(OSSL_PROVIDER * prov,int lock,int upcalls)1093 static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls)
1094 {
1095 int count = -1;
1096 struct provider_store_st *store;
1097 int ret = 1;
1098
1099 store = prov->store;
1100 /*
1101 * If the provider hasn't been added to the store, then we don't need
1102 * any locks because we've not shared it with other threads.
1103 */
1104 if (store == NULL) {
1105 lock = 0;
1106 if (!provider_init(prov))
1107 return -1;
1108 }
1109
1110 #ifndef FIPS_MODULE
1111 if (prov->ischild && upcalls && !ossl_provider_up_ref_parent(prov, 1))
1112 return -1;
1113 #endif
1114
1115 if (lock && !CRYPTO_THREAD_read_lock(store->lock)) {
1116 #ifndef FIPS_MODULE
1117 if (prov->ischild && upcalls)
1118 ossl_provider_free_parent(prov, 1);
1119 #endif
1120 return -1;
1121 }
1122
1123 if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) {
1124 CRYPTO_THREAD_unlock(store->lock);
1125 #ifndef FIPS_MODULE
1126 if (prov->ischild && upcalls)
1127 ossl_provider_free_parent(prov, 1);
1128 #endif
1129 return -1;
1130 }
1131
1132 count = ++prov->activatecnt;
1133 prov->flag_activated = 1;
1134
1135 if (prov->activatecnt == 1 && store != NULL) {
1136 ret = create_provider_children(prov);
1137 }
1138 if (lock) {
1139 CRYPTO_THREAD_unlock(prov->flag_lock);
1140 CRYPTO_THREAD_unlock(store->lock);
1141 }
1142
1143 if (!ret)
1144 return -1;
1145
1146 return count;
1147 }
1148
provider_flush_store_cache(const OSSL_PROVIDER * prov)1149 static int provider_flush_store_cache(const OSSL_PROVIDER *prov)
1150 {
1151 struct provider_store_st *store;
1152 int freeing;
1153
1154 if ((store = get_provider_store(prov->libctx)) == NULL)
1155 return 0;
1156
1157 if (!CRYPTO_THREAD_read_lock(store->lock))
1158 return 0;
1159 freeing = store->freeing;
1160 CRYPTO_THREAD_unlock(store->lock);
1161
1162 if (!freeing)
1163 return evp_method_store_flush(prov->libctx);
1164 return 1;
1165 }
1166
ossl_provider_activate(OSSL_PROVIDER * prov,int upcalls,int aschild)1167 int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild)
1168 {
1169 int count;
1170
1171 if (prov == NULL)
1172 return 0;
1173 #ifndef FIPS_MODULE
1174 /*
1175 * If aschild is true, then we only actually do the activation if the
1176 * provider is a child. If its not, this is still success.
1177 */
1178 if (aschild && !prov->ischild)
1179 return 1;
1180 #endif
1181 if ((count = provider_activate(prov, 1, upcalls)) > 0)
1182 return count == 1 ? provider_flush_store_cache(prov) : 1;
1183
1184 return 0;
1185 }
1186
ossl_provider_deactivate(OSSL_PROVIDER * prov,int removechildren)1187 int ossl_provider_deactivate(OSSL_PROVIDER *prov, int removechildren)
1188 {
1189 int count;
1190
1191 if (prov == NULL
1192 || (count = provider_deactivate(prov, 1, removechildren)) < 0)
1193 return 0;
1194 return count == 0 ? provider_flush_store_cache(prov) : 1;
1195 }
1196
ossl_provider_ctx(const OSSL_PROVIDER * prov)1197 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
1198 {
1199 return prov->provctx;
1200 }
1201
1202 /*
1203 * This function only does something once when store->use_fallbacks == 1,
1204 * and then sets store->use_fallbacks = 0, so the second call and so on is
1205 * effectively a no-op.
1206 */
provider_activate_fallbacks(struct provider_store_st * store)1207 static int provider_activate_fallbacks(struct provider_store_st *store)
1208 {
1209 int use_fallbacks;
1210 int activated_fallback_count = 0;
1211 int ret = 0;
1212 const OSSL_PROVIDER_INFO *p;
1213
1214 if (!CRYPTO_THREAD_read_lock(store->lock))
1215 return 0;
1216 use_fallbacks = store->use_fallbacks;
1217 CRYPTO_THREAD_unlock(store->lock);
1218 if (!use_fallbacks)
1219 return 1;
1220
1221 if (!CRYPTO_THREAD_write_lock(store->lock))
1222 return 0;
1223 /* Check again, just in case another thread changed it */
1224 use_fallbacks = store->use_fallbacks;
1225 if (!use_fallbacks) {
1226 CRYPTO_THREAD_unlock(store->lock);
1227 return 1;
1228 }
1229
1230 for (p = ossl_predefined_providers; p->name != NULL; p++) {
1231 OSSL_PROVIDER *prov = NULL;
1232
1233 if (!p->is_fallback)
1234 continue;
1235 /*
1236 * We use the internal constructor directly here,
1237 * otherwise we get a call loop
1238 */
1239 prov = provider_new(p->name, p->init, NULL);
1240 if (prov == NULL)
1241 goto err;
1242 prov->libctx = store->libctx;
1243 #ifndef FIPS_MODULE
1244 prov->error_lib = ERR_get_next_error_library();
1245 #endif
1246
1247 /*
1248 * We are calling provider_activate while holding the store lock. This
1249 * means the init function will be called while holding a lock. Normally
1250 * we try to avoid calling a user callback while holding a lock.
1251 * However, fallbacks are never third party providers so we accept this.
1252 */
1253 if (provider_activate(prov, 0, 0) < 0) {
1254 ossl_provider_free(prov);
1255 goto err;
1256 }
1257 prov->store = store;
1258 if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
1259 ossl_provider_free(prov);
1260 goto err;
1261 }
1262 activated_fallback_count++;
1263 }
1264
1265 if (activated_fallback_count > 0) {
1266 store->use_fallbacks = 0;
1267 ret = 1;
1268 }
1269 err:
1270 CRYPTO_THREAD_unlock(store->lock);
1271 return ret;
1272 }
1273
ossl_provider_doall_activated(OSSL_LIB_CTX * ctx,int (* cb)(OSSL_PROVIDER * provider,void * cbdata),void * cbdata)1274 int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx,
1275 int (*cb)(OSSL_PROVIDER *provider,
1276 void *cbdata),
1277 void *cbdata)
1278 {
1279 int ret = 0, curr, max, ref = 0;
1280 struct provider_store_st *store = get_provider_store(ctx);
1281 STACK_OF(OSSL_PROVIDER) *provs = NULL;
1282
1283 #ifndef FIPS_MODULE
1284 /*
1285 * Make sure any providers are loaded from config before we try to use
1286 * them.
1287 */
1288 if (ossl_lib_ctx_is_default(ctx))
1289 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
1290 #endif
1291
1292 if (store == NULL)
1293 return 1;
1294 if (!provider_activate_fallbacks(store))
1295 return 0;
1296
1297 /*
1298 * Under lock, grab a copy of the provider list and up_ref each
1299 * provider so that they don't disappear underneath us.
1300 */
1301 if (!CRYPTO_THREAD_read_lock(store->lock))
1302 return 0;
1303 provs = sk_OSSL_PROVIDER_dup(store->providers);
1304 if (provs == NULL) {
1305 CRYPTO_THREAD_unlock(store->lock);
1306 return 0;
1307 }
1308 max = sk_OSSL_PROVIDER_num(provs);
1309 /*
1310 * We work backwards through the stack so that we can safely delete items
1311 * as we go.
1312 */
1313 for (curr = max - 1; curr >= 0; curr--) {
1314 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1315
1316 if (!CRYPTO_THREAD_write_lock(prov->flag_lock))
1317 goto err_unlock;
1318 if (prov->flag_activated) {
1319 /*
1320 * We call CRYPTO_UP_REF directly rather than ossl_provider_up_ref
1321 * to avoid upping the ref count on the parent provider, which we
1322 * must not do while holding locks.
1323 */
1324 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0) {
1325 CRYPTO_THREAD_unlock(prov->flag_lock);
1326 goto err_unlock;
1327 }
1328 /*
1329 * It's already activated, but we up the activated count to ensure
1330 * it remains activated until after we've called the user callback.
1331 * We do this with no locking (because we already hold the locks)
1332 * and no upcalls (which must not be called when locks are held). In
1333 * theory this could mean the parent provider goes inactive, whilst
1334 * still activated in the child for a short period. That's ok.
1335 */
1336 if (provider_activate(prov, 0, 0) < 0) {
1337 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
1338 CRYPTO_THREAD_unlock(prov->flag_lock);
1339 goto err_unlock;
1340 }
1341 } else {
1342 sk_OSSL_PROVIDER_delete(provs, curr);
1343 max--;
1344 }
1345 CRYPTO_THREAD_unlock(prov->flag_lock);
1346 }
1347 CRYPTO_THREAD_unlock(store->lock);
1348
1349 /*
1350 * Now, we sweep through all providers not under lock
1351 */
1352 for (curr = 0; curr < max; curr++) {
1353 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1354
1355 if (!cb(prov, cbdata))
1356 goto finish;
1357 }
1358 curr = -1;
1359
1360 ret = 1;
1361 goto finish;
1362
1363 err_unlock:
1364 CRYPTO_THREAD_unlock(store->lock);
1365 finish:
1366 /*
1367 * The pop_free call doesn't do what we want on an error condition. We
1368 * either start from the first item in the stack, or part way through if
1369 * we only processed some of the items.
1370 */
1371 for (curr++; curr < max; curr++) {
1372 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr);
1373
1374 provider_deactivate(prov, 0, 1);
1375 /*
1376 * As above where we did the up-ref, we don't call ossl_provider_free
1377 * to avoid making upcalls. There should always be at least one ref
1378 * to the provider in the store, so this should never drop to 0.
1379 */
1380 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
1381 /*
1382 * Not much we can do if this assert ever fails. So we don't use
1383 * ossl_assert here.
1384 */
1385 assert(ref > 0);
1386 }
1387 sk_OSSL_PROVIDER_free(provs);
1388 return ret;
1389 }
1390
OSSL_PROVIDER_available(OSSL_LIB_CTX * libctx,const char * name)1391 int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
1392 {
1393 OSSL_PROVIDER *prov = NULL;
1394 int available = 0;
1395 struct provider_store_st *store = get_provider_store(libctx);
1396
1397 if (store == NULL || !provider_activate_fallbacks(store))
1398 return 0;
1399
1400 prov = ossl_provider_find(libctx, name, 0);
1401 if (prov != NULL) {
1402 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1403 return 0;
1404 available = prov->flag_activated;
1405 CRYPTO_THREAD_unlock(prov->flag_lock);
1406 ossl_provider_free(prov);
1407 }
1408 return available;
1409 }
1410
1411 /* Setters of Provider Object data */
ossl_provider_set_fallback(OSSL_PROVIDER * prov)1412 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
1413 {
1414 if (prov == NULL)
1415 return 0;
1416
1417 prov->flag_fallback = 1;
1418 return 1;
1419 }
1420
1421 /* Getters of Provider Object data */
ossl_provider_name(const OSSL_PROVIDER * prov)1422 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
1423 {
1424 return prov->name;
1425 }
1426
ossl_provider_dso(const OSSL_PROVIDER * prov)1427 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
1428 {
1429 return prov->module;
1430 }
1431
ossl_provider_module_name(const OSSL_PROVIDER * prov)1432 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
1433 {
1434 #ifdef FIPS_MODULE
1435 return NULL;
1436 #else
1437 return DSO_get_filename(prov->module);
1438 #endif
1439 }
1440
ossl_provider_module_path(const OSSL_PROVIDER * prov)1441 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
1442 {
1443 #ifdef FIPS_MODULE
1444 return NULL;
1445 #else
1446 /* FIXME: Ensure it's a full path */
1447 return DSO_get_filename(prov->module);
1448 #endif
1449 }
1450
ossl_provider_prov_ctx(const OSSL_PROVIDER * prov)1451 void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov)
1452 {
1453 if (prov != NULL)
1454 return prov->provctx;
1455
1456 return NULL;
1457 }
1458
ossl_provider_get0_dispatch(const OSSL_PROVIDER * prov)1459 const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov)
1460 {
1461 if (prov != NULL)
1462 return prov->dispatch;
1463
1464 return NULL;
1465 }
1466
ossl_provider_libctx(const OSSL_PROVIDER * prov)1467 OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov)
1468 {
1469 return prov != NULL ? prov->libctx : NULL;
1470 }
1471
1472 /* Wrappers around calls to the provider */
ossl_provider_teardown(const OSSL_PROVIDER * prov)1473 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
1474 {
1475 if (prov->teardown != NULL
1476 #ifndef FIPS_MODULE
1477 && !prov->ischild
1478 #endif
1479 )
1480 prov->teardown(prov->provctx);
1481 }
1482
ossl_provider_gettable_params(const OSSL_PROVIDER * prov)1483 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
1484 {
1485 return prov->gettable_params == NULL
1486 ? NULL : prov->gettable_params(prov->provctx);
1487 }
1488
ossl_provider_get_params(const OSSL_PROVIDER * prov,OSSL_PARAM params[])1489 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
1490 {
1491 return prov->get_params == NULL
1492 ? 0 : prov->get_params(prov->provctx, params);
1493 }
1494
ossl_provider_self_test(const OSSL_PROVIDER * prov)1495 int ossl_provider_self_test(const OSSL_PROVIDER *prov)
1496 {
1497 int ret;
1498
1499 if (prov->self_test == NULL)
1500 return 1;
1501 ret = prov->self_test(prov->provctx);
1502 if (ret == 0)
1503 (void)provider_flush_store_cache(prov);
1504 return ret;
1505 }
1506
ossl_provider_get_capabilities(const OSSL_PROVIDER * prov,const char * capability,OSSL_CALLBACK * cb,void * arg)1507 int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov,
1508 const char *capability,
1509 OSSL_CALLBACK *cb,
1510 void *arg)
1511 {
1512 return prov->get_capabilities == NULL
1513 ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg);
1514 }
1515
ossl_provider_query_operation(const OSSL_PROVIDER * prov,int operation_id,int * no_cache)1516 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
1517 int operation_id,
1518 int *no_cache)
1519 {
1520 const OSSL_ALGORITHM *res;
1521
1522 if (prov->query_operation == NULL)
1523 return NULL;
1524 res = prov->query_operation(prov->provctx, operation_id, no_cache);
1525 #if defined(OPENSSL_NO_CACHED_FETCH)
1526 /* Forcing the non-caching of queries */
1527 if (no_cache != NULL)
1528 *no_cache = 1;
1529 #endif
1530 return res;
1531 }
1532
ossl_provider_unquery_operation(const OSSL_PROVIDER * prov,int operation_id,const OSSL_ALGORITHM * algs)1533 void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov,
1534 int operation_id,
1535 const OSSL_ALGORITHM *algs)
1536 {
1537 if (prov->unquery_operation != NULL)
1538 prov->unquery_operation(prov->provctx, operation_id, algs);
1539 }
1540
ossl_provider_clear_all_operation_bits(OSSL_LIB_CTX * libctx)1541 int ossl_provider_clear_all_operation_bits(OSSL_LIB_CTX *libctx)
1542 {
1543 struct provider_store_st *store;
1544 OSSL_PROVIDER *provider;
1545 int i, num, res = 1;
1546
1547 if ((store = get_provider_store(libctx)) != NULL) {
1548 if (!CRYPTO_THREAD_read_lock(store->lock))
1549 return 0;
1550 num = sk_OSSL_PROVIDER_num(store->providers);
1551 for (i = 0; i < num; i++) {
1552 provider = sk_OSSL_PROVIDER_value(store->providers, i);
1553 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock)) {
1554 res = 0;
1555 continue;
1556 }
1557 if (provider->operation_bits != NULL)
1558 memset(provider->operation_bits, 0,
1559 provider->operation_bits_sz);
1560 CRYPTO_THREAD_unlock(provider->opbits_lock);
1561 }
1562 CRYPTO_THREAD_unlock(store->lock);
1563 return res;
1564 }
1565 return 0;
1566 }
1567
ossl_provider_set_operation_bit(OSSL_PROVIDER * provider,size_t bitnum)1568 int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
1569 {
1570 size_t byte = bitnum / 8;
1571 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1572
1573 if (!CRYPTO_THREAD_write_lock(provider->opbits_lock))
1574 return 0;
1575 if (provider->operation_bits_sz <= byte) {
1576 unsigned char *tmp = OPENSSL_realloc(provider->operation_bits,
1577 byte + 1);
1578
1579 if (tmp == NULL) {
1580 CRYPTO_THREAD_unlock(provider->opbits_lock);
1581 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1582 return 0;
1583 }
1584 provider->operation_bits = tmp;
1585 memset(provider->operation_bits + provider->operation_bits_sz,
1586 '\0', byte + 1 - provider->operation_bits_sz);
1587 provider->operation_bits_sz = byte + 1;
1588 }
1589 provider->operation_bits[byte] |= bit;
1590 CRYPTO_THREAD_unlock(provider->opbits_lock);
1591 return 1;
1592 }
1593
ossl_provider_test_operation_bit(OSSL_PROVIDER * provider,size_t bitnum,int * result)1594 int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
1595 int *result)
1596 {
1597 size_t byte = bitnum / 8;
1598 unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
1599
1600 if (!ossl_assert(result != NULL)) {
1601 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1602 return 0;
1603 }
1604
1605 *result = 0;
1606 if (!CRYPTO_THREAD_read_lock(provider->opbits_lock))
1607 return 0;
1608 if (provider->operation_bits_sz > byte)
1609 *result = ((provider->operation_bits[byte] & bit) != 0);
1610 CRYPTO_THREAD_unlock(provider->opbits_lock);
1611 return 1;
1612 }
1613
1614 #ifndef FIPS_MODULE
ossl_provider_get_parent(OSSL_PROVIDER * prov)1615 const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov)
1616 {
1617 return prov->handle;
1618 }
1619
ossl_provider_is_child(const OSSL_PROVIDER * prov)1620 int ossl_provider_is_child(const OSSL_PROVIDER *prov)
1621 {
1622 return prov->ischild;
1623 }
1624
ossl_provider_set_child(OSSL_PROVIDER * prov,const OSSL_CORE_HANDLE * handle)1625 int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle)
1626 {
1627 prov->handle = handle;
1628 prov->ischild = 1;
1629
1630 return 1;
1631 }
1632
ossl_provider_default_props_update(OSSL_LIB_CTX * libctx,const char * props)1633 int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props)
1634 {
1635 #ifndef FIPS_MODULE
1636 struct provider_store_st *store = NULL;
1637 int i, max;
1638 OSSL_PROVIDER_CHILD_CB *child_cb;
1639
1640 if ((store = get_provider_store(libctx)) == NULL)
1641 return 0;
1642
1643 if (!CRYPTO_THREAD_read_lock(store->lock))
1644 return 0;
1645
1646 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1647 for (i = 0; i < max; i++) {
1648 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1649 child_cb->global_props_cb(props, child_cb->cbdata);
1650 }
1651
1652 CRYPTO_THREAD_unlock(store->lock);
1653 #endif
1654 return 1;
1655 }
1656
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)1657 static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle,
1658 int (*create_cb)(
1659 const OSSL_CORE_HANDLE *provider,
1660 void *cbdata),
1661 int (*remove_cb)(
1662 const OSSL_CORE_HANDLE *provider,
1663 void *cbdata),
1664 int (*global_props_cb)(
1665 const char *props,
1666 void *cbdata),
1667 void *cbdata)
1668 {
1669 /*
1670 * This is really an OSSL_PROVIDER that we created and cast to
1671 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1672 */
1673 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1674 OSSL_PROVIDER *prov;
1675 OSSL_LIB_CTX *libctx = thisprov->libctx;
1676 struct provider_store_st *store = NULL;
1677 int ret = 0, i, max;
1678 OSSL_PROVIDER_CHILD_CB *child_cb;
1679 char *propsstr = NULL;
1680
1681 if ((store = get_provider_store(libctx)) == NULL)
1682 return 0;
1683
1684 child_cb = OPENSSL_malloc(sizeof(*child_cb));
1685 if (child_cb == NULL)
1686 return 0;
1687 child_cb->prov = thisprov;
1688 child_cb->create_cb = create_cb;
1689 child_cb->remove_cb = remove_cb;
1690 child_cb->global_props_cb = global_props_cb;
1691 child_cb->cbdata = cbdata;
1692
1693 if (!CRYPTO_THREAD_write_lock(store->lock)) {
1694 OPENSSL_free(child_cb);
1695 return 0;
1696 }
1697 propsstr = evp_get_global_properties_str(libctx, 0);
1698
1699 if (propsstr != NULL) {
1700 global_props_cb(propsstr, cbdata);
1701 OPENSSL_free(propsstr);
1702 }
1703 max = sk_OSSL_PROVIDER_num(store->providers);
1704 for (i = 0; i < max; i++) {
1705 int activated;
1706
1707 prov = sk_OSSL_PROVIDER_value(store->providers, i);
1708
1709 if (!CRYPTO_THREAD_read_lock(prov->flag_lock))
1710 break;
1711 activated = prov->flag_activated;
1712 CRYPTO_THREAD_unlock(prov->flag_lock);
1713 /*
1714 * We hold the store lock while calling the user callback. This means
1715 * that the user callback must be short and simple and not do anything
1716 * likely to cause a deadlock. We don't hold the flag_lock during this
1717 * call. In theory this means that another thread could deactivate it
1718 * while we are calling create. This is ok because the other thread
1719 * will also call remove_cb, but won't be able to do so until we release
1720 * the store lock.
1721 */
1722 if (activated && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata))
1723 break;
1724 }
1725 if (i == max) {
1726 /* Success */
1727 ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb);
1728 }
1729 if (i != max || ret <= 0) {
1730 /* Failed during creation. Remove everything we just added */
1731 for (; i >= 0; i--) {
1732 prov = sk_OSSL_PROVIDER_value(store->providers, i);
1733 remove_cb((OSSL_CORE_HANDLE *)prov, cbdata);
1734 }
1735 OPENSSL_free(child_cb);
1736 ret = 0;
1737 }
1738 CRYPTO_THREAD_unlock(store->lock);
1739
1740 return ret;
1741 }
1742
ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE * handle)1743 static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle)
1744 {
1745 /*
1746 * This is really an OSSL_PROVIDER that we created and cast to
1747 * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back.
1748 */
1749 OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle;
1750 OSSL_LIB_CTX *libctx = thisprov->libctx;
1751 struct provider_store_st *store = NULL;
1752 int i, max;
1753 OSSL_PROVIDER_CHILD_CB *child_cb;
1754
1755 if ((store = get_provider_store(libctx)) == NULL)
1756 return;
1757
1758 if (!CRYPTO_THREAD_write_lock(store->lock))
1759 return;
1760 max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs);
1761 for (i = 0; i < max; i++) {
1762 child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i);
1763 if (child_cb->prov == thisprov) {
1764 /* Found an entry */
1765 sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i);
1766 OPENSSL_free(child_cb);
1767 break;
1768 }
1769 }
1770 CRYPTO_THREAD_unlock(store->lock);
1771 }
1772 #endif
1773
1774 /*-
1775 * Core functions for the provider
1776 * ===============================
1777 *
1778 * This is the set of functions that the core makes available to the provider
1779 */
1780
1781 /*
1782 * This returns a list of Provider Object parameters with their types, for
1783 * discovery. We do not expect that many providers will use this, but one
1784 * never knows.
1785 */
1786 static const OSSL_PARAM param_types[] = {
1787 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
1788 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR,
1789 NULL, 0),
1790 #ifndef FIPS_MODULE
1791 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR,
1792 NULL, 0),
1793 #endif
1794 OSSL_PARAM_END
1795 };
1796
1797 /*
1798 * Forward declare all the functions that are provided aa dispatch.
1799 * This ensures that the compiler will complain if they aren't defined
1800 * with the correct signature.
1801 */
1802 static OSSL_FUNC_core_gettable_params_fn core_gettable_params;
1803 static OSSL_FUNC_core_get_params_fn core_get_params;
1804 static OSSL_FUNC_core_thread_start_fn core_thread_start;
1805 static OSSL_FUNC_core_get_libctx_fn core_get_libctx;
1806 #ifndef FIPS_MODULE
1807 static OSSL_FUNC_core_new_error_fn core_new_error;
1808 static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug;
1809 static OSSL_FUNC_core_vset_error_fn core_vset_error;
1810 static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark;
1811 static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark;
1812 static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark;
1813 static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid;
1814 static OSSL_FUNC_core_obj_create_fn core_obj_create;
1815 #endif
1816
core_gettable_params(const OSSL_CORE_HANDLE * handle)1817 static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
1818 {
1819 return param_types;
1820 }
1821
core_get_params(const OSSL_CORE_HANDLE * handle,OSSL_PARAM params[])1822 static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
1823 {
1824 int i;
1825 OSSL_PARAM *p;
1826 /*
1827 * We created this object originally and we know it is actually an
1828 * OSSL_PROVIDER *, so the cast is safe
1829 */
1830 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1831
1832 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL)
1833 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
1834 if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL)
1835 OSSL_PARAM_set_utf8_ptr(p, prov->name);
1836
1837 #ifndef FIPS_MODULE
1838 if ((p = OSSL_PARAM_locate(params,
1839 OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL)
1840 OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov));
1841 #endif
1842
1843 if (prov->parameters == NULL)
1844 return 1;
1845
1846 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
1847 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
1848
1849 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
1850 OSSL_PARAM_set_utf8_ptr(p, pair->value);
1851 }
1852 return 1;
1853 }
1854
core_get_libctx(const OSSL_CORE_HANDLE * handle)1855 static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
1856 {
1857 /*
1858 * We created this object originally and we know it is actually an
1859 * OSSL_PROVIDER *, so the cast is safe
1860 */
1861 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1862
1863 /*
1864 * Using ossl_provider_libctx would be wrong as that returns
1865 * NULL for |prov| == NULL and NULL libctx has a special meaning
1866 * that does not apply here. Here |prov| == NULL can happen only in
1867 * case of a coding error.
1868 */
1869 assert(prov != NULL);
1870 return (OPENSSL_CORE_CTX *)prov->libctx;
1871 }
1872
core_thread_start(const OSSL_CORE_HANDLE * handle,OSSL_thread_stop_handler_fn handfn,void * arg)1873 static int core_thread_start(const OSSL_CORE_HANDLE *handle,
1874 OSSL_thread_stop_handler_fn handfn,
1875 void *arg)
1876 {
1877 /*
1878 * We created this object originally and we know it is actually an
1879 * OSSL_PROVIDER *, so the cast is safe
1880 */
1881 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1882
1883 return ossl_init_thread_start(prov, arg, handfn);
1884 }
1885
1886 /*
1887 * The FIPS module inner provider doesn't implement these. They aren't
1888 * needed there, since the FIPS module upcalls are always the outer provider
1889 * ones.
1890 */
1891 #ifndef FIPS_MODULE
1892 /*
1893 * These error functions should use |handle| to select the proper
1894 * library context to report in the correct error stack if error
1895 * stacks become tied to the library context.
1896 * We cannot currently do that since there's no support for it in the
1897 * ERR subsystem.
1898 */
core_new_error(const OSSL_CORE_HANDLE * handle)1899 static void core_new_error(const OSSL_CORE_HANDLE *handle)
1900 {
1901 ERR_new();
1902 }
1903
core_set_error_debug(const OSSL_CORE_HANDLE * handle,const char * file,int line,const char * func)1904 static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
1905 const char *file, int line, const char *func)
1906 {
1907 ERR_set_debug(file, line, func);
1908 }
1909
core_vset_error(const OSSL_CORE_HANDLE * handle,uint32_t reason,const char * fmt,va_list args)1910 static void core_vset_error(const OSSL_CORE_HANDLE *handle,
1911 uint32_t reason, const char *fmt, va_list args)
1912 {
1913 /*
1914 * We created this object originally and we know it is actually an
1915 * OSSL_PROVIDER *, so the cast is safe
1916 */
1917 OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
1918
1919 /*
1920 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
1921 * error and will be treated as such. Otherwise, it's a new style
1922 * provider error and will be treated as such.
1923 */
1924 if (ERR_GET_LIB(reason) != 0) {
1925 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
1926 } else {
1927 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
1928 }
1929 }
1930
core_set_error_mark(const OSSL_CORE_HANDLE * handle)1931 static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
1932 {
1933 return ERR_set_mark();
1934 }
1935
core_clear_last_error_mark(const OSSL_CORE_HANDLE * handle)1936 static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
1937 {
1938 return ERR_clear_last_mark();
1939 }
1940
core_pop_error_to_mark(const OSSL_CORE_HANDLE * handle)1941 static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
1942 {
1943 return ERR_pop_to_mark();
1944 }
1945
core_obj_add_sigid(const OSSL_CORE_HANDLE * prov,const char * sign_name,const char * digest_name,const char * pkey_name)1946 static int core_obj_add_sigid(const OSSL_CORE_HANDLE *prov,
1947 const char *sign_name, const char *digest_name,
1948 const char *pkey_name)
1949 {
1950 int sign_nid = OBJ_txt2nid(sign_name);
1951 int digest_nid = NID_undef;
1952 int pkey_nid = OBJ_txt2nid(pkey_name);
1953
1954 if (digest_name != NULL && digest_name[0] != '\0'
1955 && (digest_nid = OBJ_txt2nid(digest_name)) == NID_undef)
1956 return 0;
1957
1958 if (sign_nid == NID_undef)
1959 return 0;
1960
1961 /*
1962 * Check if it already exists. This is a success if so (even if we don't
1963 * have nids for the digest/pkey)
1964 */
1965 if (OBJ_find_sigid_algs(sign_nid, NULL, NULL))
1966 return 1;
1967
1968 if (pkey_nid == NID_undef)
1969 return 0;
1970
1971 return OBJ_add_sigid(sign_nid, digest_nid, pkey_nid);
1972 }
1973
core_obj_create(const OSSL_CORE_HANDLE * prov,const char * oid,const char * sn,const char * ln)1974 static int core_obj_create(const OSSL_CORE_HANDLE *prov, const char *oid,
1975 const char *sn, const char *ln)
1976 {
1977 /* Check if it already exists and create it if not */
1978 return OBJ_txt2nid(oid) != NID_undef
1979 || OBJ_create(oid, sn, ln) != NID_undef;
1980 }
1981 #endif /* FIPS_MODULE */
1982
1983 /*
1984 * Functions provided by the core.
1985 */
1986 static const OSSL_DISPATCH core_dispatch_[] = {
1987 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
1988 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
1989 { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx },
1990 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
1991 #ifndef FIPS_MODULE
1992 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
1993 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
1994 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
1995 { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark },
1996 { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK,
1997 (void (*)(void))core_clear_last_error_mark },
1998 { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark },
1999 { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file },
2000 { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf },
2001 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex },
2002 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex },
2003 { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets },
2004 { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts },
2005 { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl },
2006 { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref },
2007 { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free },
2008 { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf },
2009 { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
2010 { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))OSSL_SELF_TEST_get_callback },
2011 { OSSL_FUNC_GET_ENTROPY, (void (*)(void))ossl_rand_get_entropy },
2012 { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))ossl_rand_cleanup_entropy },
2013 { OSSL_FUNC_GET_NONCE, (void (*)(void))ossl_rand_get_nonce },
2014 { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))ossl_rand_cleanup_nonce },
2015 #endif
2016 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
2017 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
2018 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
2019 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
2020 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
2021 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
2022 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
2023 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
2024 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
2025 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
2026 (void (*)(void))CRYPTO_secure_clear_free },
2027 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
2028 (void (*)(void))CRYPTO_secure_allocated },
2029 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
2030 #ifndef FIPS_MODULE
2031 { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB,
2032 (void (*)(void))ossl_provider_register_child_cb },
2033 { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB,
2034 (void (*)(void))ossl_provider_deregister_child_cb },
2035 { OSSL_FUNC_PROVIDER_NAME,
2036 (void (*)(void))OSSL_PROVIDER_get0_name },
2037 { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX,
2038 (void (*)(void))OSSL_PROVIDER_get0_provider_ctx },
2039 { OSSL_FUNC_PROVIDER_GET0_DISPATCH,
2040 (void (*)(void))OSSL_PROVIDER_get0_dispatch },
2041 { OSSL_FUNC_PROVIDER_UP_REF,
2042 (void (*)(void))provider_up_ref_intern },
2043 { OSSL_FUNC_PROVIDER_FREE,
2044 (void (*)(void))provider_free_intern },
2045 { OSSL_FUNC_CORE_OBJ_ADD_SIGID, (void (*)(void))core_obj_add_sigid },
2046 { OSSL_FUNC_CORE_OBJ_CREATE, (void (*)(void))core_obj_create },
2047 #endif
2048 { 0, NULL }
2049 };
2050 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;
2051