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