]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/provider_core.c
Implement EVP_MAC_do_all_ex()
[thirdparty/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_gettable_params_fn *gettable_params;
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_GETTABLE_PARAMS:
468 prov->gettable_params =
469 OSSL_get_provider_gettable_params(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 num_provs = sk_OSSL_PROVIDER_num(store->providers);
570
571 if (found_activated != NULL)
572 *found_activated = 0;
573 for (i = 0; i < num_provs; i++) {
574 OSSL_PROVIDER *prov =
575 sk_OSSL_PROVIDER_value(store->providers, i);
576
577 if (prov->flag_initialized) {
578 if (found_activated != NULL)
579 *found_activated = 1;
580 if (!(ret = cb(prov, cbdata)))
581 break;
582 }
583 }
584
585 return ret;
586 }
587
588 /*
589 * This function only does something once when store->use_fallbacks == 1,
590 * and then sets store->use_fallbacks = 0, so the second call and so on is
591 * effectively a no-op.
592 */
593 static void provider_activate_fallbacks(struct provider_store_st *store)
594 {
595 if (store->use_fallbacks) {
596 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
597 int activated_fallback_count = 0;
598 int i;
599
600 for (i = 0; i < num_provs; i++) {
601 OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
602
603 /*
604 * Note that we don't care if the activation succeeds or not.
605 * If it doesn't succeed, then any attempt to use any of the
606 * fallback providers will fail anyway.
607 */
608 if (prov->flag_fallback) {
609 activated_fallback_count++;
610 provider_activate(prov);
611 }
612 }
613
614 /*
615 * We assume that all fallbacks have been added to the store before
616 * any fallback is activated.
617 * TODO: We may have to reconsider this, IF we find ourselves adding
618 * fallbacks after any previous fallback has been activated.
619 */
620 if (activated_fallback_count > 0)
621 store->use_fallbacks = 0;
622 }
623 }
624
625 int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
626 int (*cb)(OSSL_PROVIDER *provider,
627 void *cbdata),
628 void *cbdata)
629 {
630 int ret = 1;
631 struct provider_store_st *store = get_provider_store(ctx);
632
633 #ifndef FIPS_MODE
634 /*
635 * Make sure any providers are loaded from config before we try to use
636 * them.
637 */
638 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
639 #endif
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 OPENSSL_CTX *ossl_provider_library_context(const OSSL_PROVIDER *prov)
710 {
711 return prov->libctx;
712 }
713
714 /* Wrappers around calls to the provider */
715 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
716 {
717 if (prov->teardown != NULL)
718 prov->teardown(prov->provctx);
719 }
720
721 const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov)
722 {
723 return prov->gettable_params == NULL
724 ? NULL : prov->gettable_params(prov->provctx);
725 }
726
727 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
728 {
729 return prov->get_params == NULL
730 ? 0 : prov->get_params(prov->provctx, params);
731 }
732
733
734 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
735 int operation_id,
736 int *no_cache)
737 {
738 return prov->query_operation(prov->provctx, operation_id, no_cache);
739 }
740
741 /*-
742 * Core functions for the provider
743 * ===============================
744 *
745 * This is the set of functions that the core makes available to the provider
746 */
747
748 /*
749 * This returns a list of Provider Object parameters with their types, for
750 * discovery. We do not expect that many providers will use this, but one
751 * never knows.
752 */
753 static const OSSL_PARAM param_types[] = {
754 OSSL_PARAM_DEFN("openssl-verstion", OSSL_PARAM_UTF8_PTR, NULL, 0),
755 OSSL_PARAM_DEFN("provider-name", OSSL_PARAM_UTF8_PTR, NULL, 0),
756 OSSL_PARAM_END
757 };
758
759 /*
760 * Forward declare all the functions that are provided aa dispatch.
761 * This ensures that the compiler will complain if they aren't defined
762 * with the correct signature.
763 */
764 static OSSL_core_gettable_params_fn core_gettable_params;
765 static OSSL_core_get_params_fn core_get_params;
766 static OSSL_core_thread_start_fn core_thread_start;
767 static OSSL_core_get_library_context_fn core_get_libctx;
768 #ifndef FIPS_MODE
769 static OSSL_core_new_error_fn core_new_error;
770 static OSSL_core_set_error_debug_fn core_set_error_debug;
771 static OSSL_core_vset_error_fn core_vset_error;
772 #endif
773
774 static const OSSL_PARAM *core_gettable_params(const OSSL_PROVIDER *prov)
775 {
776 return param_types;
777 }
778
779 static int core_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
780 {
781 int i;
782 OSSL_PARAM *p;
783
784 #ifndef FIPS_MODE
785 /* Load config before we attempt to read any provider parameters */
786 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
787 #endif
788
789 if ((p = OSSL_PARAM_locate(params, "openssl-version")) != NULL)
790 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
791 if ((p = OSSL_PARAM_locate(params, "provider-name")) != NULL)
792 OSSL_PARAM_set_utf8_ptr(p, prov->name);
793
794 if (prov->parameters == NULL)
795 return 1;
796
797 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
798 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
799
800 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
801 OSSL_PARAM_set_utf8_ptr(p, pair->value);
802 }
803
804 return 1;
805 }
806
807 static OPENSSL_CTX *core_get_libctx(const OSSL_PROVIDER *prov)
808 {
809 return ossl_provider_library_context(prov);
810 }
811
812 static int core_thread_start(const OSSL_PROVIDER *prov,
813 OSSL_thread_stop_handler_fn handfn)
814 {
815 return ossl_init_thread_start(prov, prov->provctx, handfn);
816 }
817
818 /*
819 * The FIPS module inner provider doesn't implement these. They aren't
820 * needed there, since the FIPS module upcalls are always the outer provider
821 * ones.
822 */
823 #ifndef FIPS_MODE
824 /*
825 * TODO(3.0) These error functions should use |prov| to select the proper
826 * library context to report in the correct error stack, at least if error
827 * stacks become tied to the library context.
828 * We cannot currently do that since there's no support for it in the
829 * ERR subsystem.
830 */
831 static void core_new_error(const OSSL_PROVIDER *prov)
832 {
833 ERR_new();
834 }
835
836 static void core_set_error_debug(const OSSL_PROVIDER *prov,
837 const char *file, int line, const char *func)
838 {
839 ERR_set_debug(file, line, func);
840 }
841
842 static void core_vset_error(const OSSL_PROVIDER *prov,
843 uint32_t reason, const char *fmt, va_list args)
844 {
845 /*
846 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
847 * error and will be treated as such. Otherwise, it's a new style
848 * provider error and will be treated as such.
849 */
850 if (ERR_GET_LIB(reason) != 0) {
851 ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args);
852 } else {
853 ERR_vset_error(prov->error_lib, (int)reason, fmt, args);
854 }
855 }
856 #endif
857
858 /*
859 * Functions provided by the core. Blank line separates "families" of related
860 * functions.
861 */
862 static const OSSL_DISPATCH core_dispatch_[] = {
863 { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params },
864 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
865 { OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT, (void (*)(void))core_get_libctx },
866 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
867 #ifndef FIPS_MODE
868 { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error },
869 { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug },
870 { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error },
871 #endif
872
873 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
874 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
875 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
876 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
877 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
878 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
879 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
880 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
881 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
882 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
883 (void (*)(void))CRYPTO_secure_clear_free },
884 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
885 (void (*)(void))CRYPTO_secure_allocated },
886 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
887
888 { 0, NULL }
889 };
890 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;