]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/provider_core.c
d96e2144f493052c45e513ac99279570371454a2
[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_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 {
164 struct provider_store_st *store = NULL;
165 OSSL_PROVIDER *prov = NULL;
166
167 if ((store = get_provider_store(libctx)) != NULL) {
168 OSSL_PROVIDER tmpl = { 0, };
169 int i;
170
171 tmpl.name = (char *)name;
172 CRYPTO_THREAD_write_lock(store->lock);
173 if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
174 || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
175 || !ossl_provider_up_ref(prov))
176 prov = NULL;
177 CRYPTO_THREAD_unlock(store->lock);
178 }
179
180 return prov;
181 }
182
183 /*-
184 * Provider Object methods
185 * =======================
186 */
187
188 static OSSL_PROVIDER *provider_new(const char *name,
189 OSSL_provider_init_fn *init_function)
190 {
191 OSSL_PROVIDER *prov = NULL;
192
193 if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
194 #ifndef HAVE_ATOMICS
195 || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL
196 #endif
197 || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
198 || (prov->name = OPENSSL_strdup(name)) == NULL) {
199 ossl_provider_free(prov);
200 CRYPTOerr(CRYPTO_F_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
201 return NULL;
202 }
203
204 prov->init_function = init_function;
205 return prov;
206 }
207
208 int ossl_provider_up_ref(OSSL_PROVIDER *prov)
209 {
210 int ref = 0;
211
212 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
213 return 0;
214 return ref;
215 }
216
217 OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
218 OSSL_provider_init_fn *init_function)
219 {
220 struct provider_store_st *store = NULL;
221 OSSL_PROVIDER *prov = NULL;
222
223 if ((store = get_provider_store(libctx)) == NULL)
224 return NULL;
225
226 if ((prov = ossl_provider_find(libctx, name)) != NULL) { /* refcount +1 */
227 ossl_provider_free(prov); /* refcount -1 */
228 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW,
229 CRYPTO_R_PROVIDER_ALREADY_EXISTS);
230 ERR_add_error_data(2, "name=", name);
231 return NULL;
232 }
233
234 /* provider_new() generates an error, so no need here */
235 if ((prov = provider_new(name, init_function)) == NULL)
236 return NULL;
237
238 CRYPTO_THREAD_write_lock(store->lock);
239 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
240 ossl_provider_free(prov); /* -1 Reference that was to be returned */
241 prov = NULL;
242 } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
243 ossl_provider_free(prov); /* -1 Store reference */
244 ossl_provider_free(prov); /* -1 Reference that was to be returned */
245 prov = NULL;
246 } else {
247 prov->libctx = libctx;
248 prov->store = store;
249 #ifndef FIPS_MODE
250 prov->error_lib = ERR_get_next_error_library();
251 #endif
252 }
253 CRYPTO_THREAD_unlock(store->lock);
254
255 if (prov == NULL)
256 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
257
258 /*
259 * At this point, the provider is only partially "loaded". To be
260 * fully "loaded", ossl_provider_activate() must also be called.
261 */
262
263 return prov;
264 }
265
266 static void free_infopair(INFOPAIR *pair)
267 {
268 OPENSSL_free(pair->name);
269 OPENSSL_free(pair->value);
270 OPENSSL_free(pair);
271 }
272
273 void ossl_provider_free(OSSL_PROVIDER *prov)
274 {
275 if (prov != NULL) {
276 int ref = 0;
277
278 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
279
280 /*
281 * When the refcount drops below two, the store is the only
282 * possible reference, or it has already been taken away from
283 * the store (this may happen if a provider was activated
284 * because it's a fallback, but isn't currently used)
285 * When that happens, the provider is inactivated.
286 */
287 if (ref < 2 && prov->flag_initialized) {
288 #ifndef FIPS_MODE
289 ossl_init_thread_deregister(prov);
290 #endif
291 if (prov->teardown != NULL)
292 prov->teardown(prov->provctx);
293 #ifndef OPENSSL_NO_ERR
294 # ifndef FIPS_MODE
295 if (prov->error_strings != NULL) {
296 ERR_unload_strings(prov->error_lib, prov->error_strings);
297 OPENSSL_free(prov->error_strings);
298 prov->error_strings = NULL;
299 }
300 # endif
301 #endif
302 prov->flag_initialized = 0;
303 }
304
305 /*
306 * When the refcount drops to zero, it has been taken out of
307 * the store. All we have to do here is clean it out.
308 */
309 if (ref == 0) {
310 #ifndef FIPS_MODE
311 DSO_free(prov->module);
312 #endif
313 OPENSSL_free(prov->name);
314 OPENSSL_free(prov->path);
315 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
316 #ifndef HAVE_ATOMICS
317 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
318 #endif
319 OPENSSL_free(prov);
320 }
321 }
322 }
323
324 /* Setters */
325 int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path)
326 {
327 OPENSSL_free(prov->path);
328 if (module_path == NULL)
329 return 1;
330 if ((prov->path = OPENSSL_strdup(module_path)) != NULL)
331 return 1;
332 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_SET_MODULE_PATH, ERR_R_MALLOC_FAILURE);
333 return 0;
334 }
335
336 int ossl_provider_add_parameter(OSSL_PROVIDER *prov,
337 const char *name, const char *value)
338 {
339 INFOPAIR *pair = NULL;
340
341 if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL
342 && (prov->parameters != NULL
343 || (prov->parameters = sk_INFOPAIR_new_null()) != NULL)
344 && (pair->name = OPENSSL_strdup(name)) != NULL
345 && (pair->value = OPENSSL_strdup(value)) != NULL
346 && sk_INFOPAIR_push(prov->parameters, pair) > 0)
347 return 1;
348
349 if (pair != NULL) {
350 OPENSSL_free(pair->name);
351 OPENSSL_free(pair->value);
352 OPENSSL_free(pair);
353 }
354 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_PARAMETER, ERR_R_MALLOC_FAILURE);
355 return 0;
356 }
357
358 /*
359 * Provider activation.
360 *
361 * What "activation" means depends on the provider form; for built in
362 * providers (in the library or the application alike), the provider
363 * can already be considered to be loaded, all that's needed is to
364 * initialize it. However, for dynamically loadable provider modules,
365 * we must first load that module.
366 *
367 * Built in modules are distinguished from dynamically loaded modules
368 * with an already assigned init function.
369 */
370 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
371
372 /*
373 * Internal version that doesn't affect the store flags, and thereby avoid
374 * locking. Direct callers must remember to set the store flags when
375 * appropriate.
376 */
377 static int provider_activate(OSSL_PROVIDER *prov)
378 {
379 const OSSL_DISPATCH *provider_dispatch = NULL;
380 #ifndef OPENSSL_NO_ERR
381 # ifndef FIPS_MODE
382 OSSL_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
383 # endif
384 #endif
385
386 if (prov->flag_initialized)
387 return 1;
388
389 /*
390 * If the init function isn't set, it indicates that this provider is
391 * a loadable module.
392 */
393 if (prov->init_function == NULL) {
394 #ifdef FIPS_MODE
395 return 0;
396 #else
397 if (prov->module == NULL) {
398 char *allocated_path = NULL;
399 const char *module_path = NULL;
400 char *merged_path = NULL;
401 const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES");
402
403 if ((prov->module = DSO_new()) == NULL) {
404 /* DSO_new() generates an error already */
405 return 0;
406 }
407
408 if (load_dir == NULL)
409 load_dir = MODULESDIR;
410
411 DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
412 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
413
414 module_path = prov->path;
415 if (module_path == NULL)
416 module_path = allocated_path =
417 DSO_convert_filename(prov->module, prov->name);
418 if (module_path != NULL)
419 merged_path = DSO_merge(prov->module, module_path, load_dir);
420
421 if (merged_path == NULL
422 || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) {
423 DSO_free(prov->module);
424 prov->module = NULL;
425 }
426
427 OPENSSL_free(merged_path);
428 OPENSSL_free(allocated_path);
429 }
430
431 if (prov->module != NULL)
432 prov->init_function = (OSSL_provider_init_fn *)
433 DSO_bind_func(prov->module, "OSSL_provider_init");
434 #endif
435 }
436
437 /* Call the initialise function for the provider. */
438 if (prov->init_function == NULL
439 || !prov->init_function(prov, core_dispatch, &provider_dispatch,
440 &prov->provctx)) {
441 CRYPTOerr(CRYPTO_F_PROVIDER_ACTIVATE, ERR_R_INIT_FAIL);
442 ERR_add_error_data(2, "name=", prov->name);
443 #ifndef FIPS_MODE
444 DSO_free(prov->module);
445 prov->module = NULL;
446 #endif
447 return 0;
448 }
449
450 for (; provider_dispatch->function_id != 0; provider_dispatch++) {
451 switch (provider_dispatch->function_id) {
452 case OSSL_FUNC_PROVIDER_TEARDOWN:
453 prov->teardown =
454 OSSL_get_provider_teardown(provider_dispatch);
455 break;
456 case OSSL_FUNC_PROVIDER_GET_PARAM_TYPES:
457 prov->get_param_types =
458 OSSL_get_provider_get_param_types(provider_dispatch);
459 break;
460 case OSSL_FUNC_PROVIDER_GET_PARAMS:
461 prov->get_params =
462 OSSL_get_provider_get_params(provider_dispatch);
463 break;
464 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
465 prov->query_operation =
466 OSSL_get_provider_query_operation(provider_dispatch);
467 break;
468 #ifndef OPENSSL_NO_ERR
469 # ifndef FIPS_MODE
470 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
471 p_get_reason_strings =
472 OSSL_get_provider_get_reason_strings(provider_dispatch);
473 break;
474 # endif
475 #endif
476 }
477 }
478
479 #ifndef OPENSSL_NO_ERR
480 # ifndef FIPS_MODE
481 if (p_get_reason_strings != NULL) {
482 const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx);
483 size_t cnt, cnt2;
484
485 /*
486 * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM,
487 * although they are essentially the same type.
488 * Furthermore, ERR_load_strings() patches the array's error number
489 * with the error library number, so we need to make a copy of that
490 * array either way.
491 */
492 cnt = 1; /* One for the terminating item */
493 while (reasonstrings[cnt].id != 0) {
494 if (ERR_GET_LIB(reasonstrings[cnt].id) != 0)
495 return 0;
496 cnt++;
497 }
498
499 /* Allocate one extra item for the "library" name */
500 prov->error_strings =
501 OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1));
502 if (prov->error_strings == NULL)
503 return 0;
504
505 /*
506 * Set the "library" name.
507 */
508 prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0);
509 prov->error_strings[0].string = prov->name;
510 /*
511 * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions
512 * 1..cnt.
513 */
514 for (cnt2 = 1; cnt2 <= cnt; cnt2++) {
515 prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id;
516 prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr;
517 }
518
519 ERR_load_strings(prov->error_lib, prov->error_strings);
520 }
521 # endif
522 #endif
523
524 /* With this flag set, this provider has become fully "loaded". */
525 prov->flag_initialized = 1;
526
527 return 1;
528 }
529
530 int ossl_provider_activate(OSSL_PROVIDER *prov)
531 {
532 if (provider_activate(prov)) {
533 CRYPTO_THREAD_write_lock(prov->store->lock);
534 prov->store->use_fallbacks = 0;
535 CRYPTO_THREAD_unlock(prov->store->lock);
536 return 1;
537 }
538
539 return 0;
540 }
541
542 void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
543 {
544 return prov->provctx;
545 }
546
547
548 static int provider_forall_loaded(struct provider_store_st *store,
549 int *found_activated,
550 int (*cb)(OSSL_PROVIDER *provider,
551 void *cbdata),
552 void *cbdata)
553 {
554 int i;
555 int ret = 1;
556 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
557
558 if (found_activated != NULL)
559 *found_activated = 0;
560 for (i = 0; i < num_provs; i++) {
561 OSSL_PROVIDER *prov =
562 sk_OSSL_PROVIDER_value(store->providers, i);
563
564 if (prov->flag_initialized) {
565 if (found_activated != NULL)
566 *found_activated = 1;
567 if (!(ret = cb(prov, cbdata)))
568 break;
569 }
570 }
571
572 return ret;
573 }
574
575 int ossl_provider_forall_loaded(OPENSSL_CTX *ctx,
576 int (*cb)(OSSL_PROVIDER *provider,
577 void *cbdata),
578 void *cbdata)
579 {
580 int ret = 1;
581 int i;
582 struct provider_store_st *store = get_provider_store(ctx);
583
584 if (store != NULL) {
585 int found_activated = 0;
586
587 CRYPTO_THREAD_read_lock(store->lock);
588 ret = provider_forall_loaded(store, &found_activated, cb, cbdata);
589
590 /*
591 * If there's nothing activated ever in this store, try to activate
592 * all fallbacks.
593 */
594 if (!found_activated && store->use_fallbacks) {
595 int num_provs = sk_OSSL_PROVIDER_num(store->providers);
596 int activated_fallback_count = 0;
597
598 for (i = 0; i < num_provs; i++) {
599 OSSL_PROVIDER *prov =
600 sk_OSSL_PROVIDER_value(store->providers, i);
601
602 /*
603 * Note that we don't care if the activation succeeds or
604 * not. If it doesn't succeed, then the next loop will
605 * fail anyway.
606 */
607 if (prov->flag_fallback) {
608 activated_fallback_count++;
609 provider_activate(prov);
610 }
611 }
612
613 if (activated_fallback_count > 0) {
614 /*
615 * We assume that all fallbacks have been added to the store
616 * before any fallback is activated.
617 * TODO: We may have to reconsider this, IF we find ourselves
618 * adding fallbacks after any previous fallback has been
619 * activated.
620 */
621 store->use_fallbacks = 0;
622
623 /*
624 * Now that we've activated available fallbacks, try a
625 * second sweep
626 */
627 ret = provider_forall_loaded(store, NULL, cb, cbdata);
628 }
629 }
630 CRYPTO_THREAD_unlock(store->lock);
631 }
632
633 return ret;
634 }
635
636 /* Setters of Provider Object data */
637 int ossl_provider_set_fallback(OSSL_PROVIDER *prov)
638 {
639 if (prov == NULL)
640 return 0;
641
642 prov->flag_fallback = 1;
643 return 1;
644 }
645
646 /* Getters of Provider Object data */
647 const char *ossl_provider_name(const OSSL_PROVIDER *prov)
648 {
649 return prov->name;
650 }
651
652 const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
653 {
654 return prov->module;
655 }
656
657 const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
658 {
659 #ifdef FIPS_MODE
660 return NULL;
661 #else
662 return DSO_get_filename(prov->module);
663 #endif
664 }
665
666 const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
667 {
668 #ifdef FIPS_MODE
669 return NULL;
670 #else
671 /* FIXME: Ensure it's a full path */
672 return DSO_get_filename(prov->module);
673 #endif
674 }
675
676 /* Wrappers around calls to the provider */
677 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
678 {
679 if (prov->teardown != NULL)
680 prov->teardown(prov->provctx);
681 }
682
683 const OSSL_PARAM *ossl_provider_get_param_types(const OSSL_PROVIDER *prov)
684 {
685 return prov->get_param_types == NULL
686 ? NULL : prov->get_param_types(prov->provctx);
687 }
688
689 int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
690 {
691 return prov->get_params == NULL
692 ? 0 : prov->get_params(prov->provctx, params);
693 }
694
695
696 const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
697 int operation_id,
698 int *no_cache)
699 {
700 return prov->query_operation(prov->provctx, operation_id, no_cache);
701 }
702
703 /*-
704 * Core functions for the provider
705 * ===============================
706 *
707 * This is the set of functions that the core makes available to the provider
708 */
709
710 /*
711 * This returns a list of Provider Object parameters with their types, for
712 * discovery. We do not expect that many providers will use this, but one
713 * never knows.
714 */
715 static const OSSL_PARAM param_types[] = {
716 OSSL_PARAM_DEFN("openssl-verstion", OSSL_PARAM_UTF8_PTR, NULL, 0),
717 OSSL_PARAM_DEFN("provider-name", OSSL_PARAM_UTF8_PTR, NULL, 0),
718 OSSL_PARAM_END
719 };
720
721 static const OSSL_PARAM *core_get_param_types(const OSSL_PROVIDER *prov)
722 {
723 return param_types;
724 }
725
726 static int core_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
727 {
728 int i;
729 OSSL_PARAM *p;
730
731 if ((p = OSSL_PARAM_locate(params, "openssl-version")) != NULL)
732 OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
733 if ((p = OSSL_PARAM_locate(params, "provider-name")) != NULL)
734 OSSL_PARAM_set_utf8_ptr(p, prov->name);
735
736 if (prov->parameters == NULL)
737 return 1;
738
739 for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) {
740 INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i);
741
742 if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL)
743 OSSL_PARAM_set_utf8_ptr(p, pair->value);
744 }
745
746 return 1;
747 }
748
749 static OSSL_core_get_library_context_fn core_get_libctx; /* Check */
750 static OPENSSL_CTX *core_get_libctx(const OSSL_PROVIDER *prov)
751 {
752 return prov->libctx;
753 }
754
755 static int core_thread_start(const OSSL_PROVIDER *prov,
756 OSSL_thread_stop_handler_fn handfn)
757 {
758 return ossl_init_thread_start(prov, prov->provctx, handfn);
759 }
760
761 /*
762 * The FIPS module inner provider doesn't implement these. They aren't
763 * needed there, since the FIPS module upcalls are always the outer provider
764 * ones.
765 */
766 #ifndef FIPS_MODE
767 static void core_put_error(const OSSL_PROVIDER *prov,
768 uint32_t reason, const char *file, int line)
769 {
770 /*
771 * If the uppermost 8 bits are non-zero, it's an OpenSSL library
772 * error and will be treated as such. Otherwise, it's a new style
773 * provider error and will be treated as such.
774 */
775 if (ERR_GET_LIB(reason) != 0) {
776 ERR_PUT_error(ERR_GET_LIB(reason),
777 ERR_GET_FUNC(reason),
778 ERR_GET_REASON(reason),
779 file, line);
780 } else {
781 ERR_PUT_error(prov->error_lib, 0, (int)reason, file, line);
782 }
783 }
784
785 /*
786 * TODO(3.0) This, as well as core_put_error above, should use |prov|
787 * to select the proper library context to report in the correct error
788 * stack, at least if error stacks become tied to the library context.
789 * We cannot currently do that since there's no support for it in the
790 * ERR subsystem.
791 */
792 static void core_add_error_vdata(const OSSL_PROVIDER *prov,
793 int num, va_list args)
794 {
795 ERR_add_error_vdata(num, args);
796 }
797 #endif
798
799 /*
800 * Functions provided by the core. Blank line separates "families" of related
801 * functions.
802 */
803 static const OSSL_DISPATCH core_dispatch_[] = {
804 { OSSL_FUNC_CORE_GET_PARAM_TYPES, (void (*)(void))core_get_param_types },
805 { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
806 { OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT, (void (*)(void))core_get_libctx },
807 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
808 #ifndef FIPS_MODE
809 { OSSL_FUNC_CORE_PUT_ERROR, (void (*)(void))core_put_error },
810 { OSSL_FUNC_CORE_ADD_ERROR_VDATA, (void (*)(void))core_add_error_vdata },
811 #endif
812
813 { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc },
814 { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc },
815 { OSSL_FUNC_CRYPTO_MEMDUP, (void (*)(void))CRYPTO_memdup },
816 { OSSL_FUNC_CRYPTO_STRDUP, (void (*)(void))CRYPTO_strdup },
817 { OSSL_FUNC_CRYPTO_STRNDUP, (void (*)(void))CRYPTO_strndup },
818 { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free },
819 { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free },
820 { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc },
821 { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc },
822 { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc },
823 { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc },
824 { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free },
825 { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE,
826 (void (*)(void))CRYPTO_secure_clear_free },
827 { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED,
828 (void (*)(void))CRYPTO_secure_allocated },
829 { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse },
830 { OSSL_FUNC_OPENSSL_HEXSTR2BUF, (void (*)(void))OPENSSL_hexstr2buf },
831
832 { 0, NULL }
833 };
834 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;