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