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