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