]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/provider_core.c
Describe OSSL_PARAM as a parameter descriptor
[thirdparty/openssl.git] / crypto / provider_core.c
CommitLineData
4c2883a9
RL
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>
ac1055ef 12#include <openssl/params.h>
4c2883a9 13#include <openssl/opensslv.h>
da747958 14#include "internal/cryptlib_int.h"
c41f3ae0 15#include "internal/nelem.h"
4c2883a9
RL
16#include "internal/thread_once.h"
17#include "internal/provider.h"
18#include "internal/refcount.h"
c41f3ae0
RL
19#include "provider_local.h"
20
21static OSSL_PROVIDER *provider_new(const char *name,
22 OSSL_provider_init_fn *init_function);
4c2883a9
RL
23
24/*-
25 * Provider Object structure
26 * =========================
27 */
28
ac1055ef
RL
29typedef struct {
30 char *name;
31 char *value;
32} INFOPAIR;
33DEFINE_STACK_OF(INFOPAIR)
34
4c2883a9
RL
35struct provider_store_st; /* Forward declaration */
36
37struct ossl_provider_st {
38 /* Flag bits */
39 unsigned int flag_initialized:1;
e55008a9 40 unsigned int flag_fallback:1;
4c2883a9
RL
41
42 /* OpenSSL library side data */
43 CRYPTO_REF_COUNT refcnt;
085bef9f 44 CRYPTO_RWLOCK *refcnt_lock; /* For the ref counter */
4c2883a9 45 char *name;
ac1055ef 46 char *path;
4c2883a9
RL
47 DSO *module;
48 OSSL_provider_init_fn *init_function;
ac1055ef 49 STACK_OF(INFOPAIR) *parameters;
e7706e63 50 OPENSSL_CTX *libctx; /* The library context this instance is in */
e55008a9 51 struct provider_store_st *store; /* The store this instance belongs to */
6592ab81
RL
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 */
6ebc2f56 57 int error_lib; /* ERR library number, one for each provider */
6592ab81 58# ifndef OPENSSL_NO_ERR
6ebc2f56 59 ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */
6592ab81 60# endif
6ebc2f56 61#endif
4c2883a9
RL
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;
099bd339 67 OSSL_provider_query_operation_fn *query_operation;
a39eb840
RL
68
69 /* Provider side data */
70 void *provctx;
4c2883a9
RL
71};
72DEFINE_STACK_OF(OSSL_PROVIDER)
73
74static 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
88struct provider_store_st {
89 STACK_OF(OSSL_PROVIDER) *providers;
90 CRYPTO_RWLOCK *lock;
e55008a9 91 unsigned int use_fallbacks:1;
4c2883a9 92};
4c2883a9
RL
93
94static 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
1aedc35f 105static void *provider_store_new(OPENSSL_CTX *ctx)
4c2883a9
RL
106{
107 struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
c41f3ae0 108 const struct predefined_providers_st *p = NULL;
4c2883a9
RL
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);
e55008a9 114 return NULL;
4c2883a9 115 }
e55008a9 116 store->use_fallbacks = 1;
c41f3ae0
RL
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 }
e7706e63 134 prov->libctx = ctx;
c41f3ae0 135 prov->store = store;
6592ab81 136#ifndef FIPS_MODE
6ebc2f56 137 prov->error_lib = ERR_get_next_error_library();
6592ab81 138#endif
c41f3ae0
RL
139 if(p->is_fallback)
140 ossl_provider_set_fallback(prov);
141 }
142
4c2883a9
RL
143 return store;
144}
145
146static const OPENSSL_CTX_METHOD provider_store_method = {
147 provider_store_new,
148 provider_store_free,
149};
150
4c2883a9
RL
151static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
152{
153 struct provider_store_st *store = NULL;
154
1aedc35f
MC
155 store = openssl_ctx_get_data(libctx, OPENSSL_CTX_PROVIDER_STORE_INDEX,
156 &provider_store_method);
4c2883a9
RL
157 if (store == NULL)
158 CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
159 return store;
160}
161
4c2883a9
RL
162OSSL_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
7c95390e 175 || !ossl_provider_up_ref(prov))
4c2883a9
RL
176 prov = NULL;
177 CRYPTO_THREAD_unlock(store->lock);
178 }
179
180 return prov;
181}
182
c41f3ae0
RL
183/*-
184 * Provider Object methods
185 * =======================
186 */
187
188static 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
7c95390e 197 || !ossl_provider_up_ref(prov) /* +1 One reference to be returned */
c41f3ae0
RL
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
7c95390e 208int ossl_provider_up_ref(OSSL_PROVIDER *prov)
c41f3ae0
RL
209{
210 int ref = 0;
211
ad14e8e5
SL
212 if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0)
213 return 0;
c41f3ae0
RL
214 return ref;
215}
216
4c2883a9
RL
217OSSL_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
c41f3ae0
RL
234 /* provider_new() generates an error, so no need here */
235 if ((prov = provider_new(name, init_function)) == NULL)
4c2883a9 236 return NULL;
4c2883a9
RL
237
238 CRYPTO_THREAD_write_lock(store->lock);
7c95390e 239 if (!ossl_provider_up_ref(prov)) { /* +1 One reference for the store */
4c2883a9
RL
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;
e55008a9 246 } else {
e7706e63 247 prov->libctx = libctx;
e55008a9 248 prov->store = store;
6592ab81 249#ifndef FIPS_MODE
6ebc2f56 250 prov->error_lib = ERR_get_next_error_library();
6592ab81 251#endif
4c2883a9
RL
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
ac1055ef
RL
266static void free_infopair(INFOPAIR *pair)
267{
268 OPENSSL_free(pair->name);
269 OPENSSL_free(pair->value);
270 OPENSSL_free(pair);
271}
272
4c2883a9
RL
273void ossl_provider_free(OSSL_PROVIDER *prov)
274{
275 if (prov != NULL) {
276 int ref = 0;
277
085bef9f 278 CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock);
4c2883a9
RL
279
280 /*
e55008a9
RL
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)
4c2883a9
RL
285 * When that happens, the provider is inactivated.
286 */
e55008a9 287 if (ref < 2 && prov->flag_initialized) {
6913f5fe
MC
288#ifndef FIPS_MODE
289 ossl_init_thread_deregister(prov);
290#endif
4c2883a9 291 if (prov->teardown != NULL)
a39eb840 292 prov->teardown(prov->provctx);
6ebc2f56
RL
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
4c2883a9
RL
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) {
3593266d 310#ifndef FIPS_MODE
4c2883a9 311 DSO_free(prov->module);
3593266d 312#endif
4c2883a9 313 OPENSSL_free(prov->name);
ac1055ef
RL
314 OPENSSL_free(prov->path);
315 sk_INFOPAIR_pop_free(prov->parameters, free_infopair);
085bef9f
RL
316#ifndef HAVE_ATOMICS
317 CRYPTO_THREAD_lock_free(prov->refcnt_lock);
318#endif
4c2883a9
RL
319 OPENSSL_free(prov);
320 }
321 }
322}
323
ac1055ef
RL
324/* Setters */
325int 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
336int 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
4c2883a9
RL
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 */
370static const OSSL_DISPATCH *core_dispatch; /* Define further down */
371
e55008a9
RL
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
e7706e63 375 * appropriate.
e55008a9 376 */
e7706e63 377static int provider_activate(OSSL_PROVIDER *prov)
4c2883a9
RL
378{
379 const OSSL_DISPATCH *provider_dispatch = NULL;
6ebc2f56 380#ifndef OPENSSL_NO_ERR
6592ab81 381# ifndef FIPS_MODE
6ebc2f56 382 OSSL_provider_get_reason_strings_fn *p_get_reason_strings = NULL;
6592ab81 383# endif
6ebc2f56 384#endif
4c2883a9
RL
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) {
3593266d
MC
394#ifdef FIPS_MODE
395 return 0;
396#else
4c2883a9 397 if (prov->module == NULL) {
ac1055ef
RL
398 char *allocated_path = NULL;
399 const char *module_path = NULL;
400 char *merged_path = NULL;
4c2883a9
RL
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);
ac1055ef
RL
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) {
4c2883a9
RL
423 DSO_free(prov->module);
424 prov->module = NULL;
425 }
426
ac1055ef
RL
427 OPENSSL_free(merged_path);
428 OPENSSL_free(allocated_path);
4c2883a9
RL
429 }
430
431 if (prov->module != NULL)
432 prov->init_function = (OSSL_provider_init_fn *)
433 DSO_bind_func(prov->module, "OSSL_provider_init");
3593266d 434#endif
4c2883a9
RL
435 }
436
e7706e63 437 /* Call the initialise function for the provider. */
4c2883a9 438 if (prov->init_function == NULL
a39eb840
RL
439 || !prov->init_function(prov, core_dispatch, &provider_dispatch,
440 &prov->provctx)) {
e55008a9 441 CRYPTOerr(CRYPTO_F_PROVIDER_ACTIVATE, ERR_R_INIT_FAIL);
4c2883a9 442 ERR_add_error_data(2, "name=", prov->name);
3593266d 443#ifndef FIPS_MODE
4c2883a9
RL
444 DSO_free(prov->module);
445 prov->module = NULL;
3593266d 446#endif
4c2883a9
RL
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;
099bd339
RL
464 case OSSL_FUNC_PROVIDER_QUERY_OPERATION:
465 prov->query_operation =
466 OSSL_get_provider_query_operation(provider_dispatch);
467 break;
6ebc2f56 468#ifndef OPENSSL_NO_ERR
6592ab81 469# ifndef FIPS_MODE
6ebc2f56
RL
470 case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS:
471 p_get_reason_strings =
472 OSSL_get_provider_get_reason_strings(provider_dispatch);
473 break;
6592ab81 474# endif
6ebc2f56
RL
475#endif
476 }
477 }
478
479#ifndef OPENSSL_NO_ERR
6592ab81 480# ifndef FIPS_MODE
6ebc2f56
RL
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;
4c2883a9 517 }
6ebc2f56
RL
518
519 ERR_load_strings(prov->error_lib, prov->error_strings);
4c2883a9 520 }
6592ab81 521# endif
6ebc2f56 522#endif
4c2883a9
RL
523
524 /* With this flag set, this provider has become fully "loaded". */
525 prov->flag_initialized = 1;
526
527 return 1;
528}
529
e55008a9
RL
530int ossl_provider_activate(OSSL_PROVIDER *prov)
531{
e7706e63 532 if (provider_activate(prov)) {
e55008a9
RL
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
a39eb840
RL
542void *ossl_provider_ctx(const OSSL_PROVIDER *prov)
543{
544 return prov->provctx;
545}
546
e55008a9
RL
547
548static 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
85e2417c
RL
575int 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) {
e55008a9
RL
585 int found_activated = 0;
586
85e2417c 587 CRYPTO_THREAD_read_lock(store->lock);
e55008a9 588 ret = provider_forall_loaded(store, &found_activated, cb, cbdata);
85e2417c 589
e55008a9
RL
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++;
e7706e63 609 provider_activate(prov);
e55008a9
RL
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 }
85e2417c
RL
629 }
630 CRYPTO_THREAD_unlock(store->lock);
631 }
632
633 return ret;
634}
635
e55008a9
RL
636/* Setters of Provider Object data */
637int 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
4c2883a9 646/* Getters of Provider Object data */
24626a47 647const char *ossl_provider_name(const OSSL_PROVIDER *prov)
4c2883a9
RL
648{
649 return prov->name;
650}
651
24626a47 652const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov)
4c2883a9
RL
653{
654 return prov->module;
655}
656
24626a47 657const char *ossl_provider_module_name(const OSSL_PROVIDER *prov)
4c2883a9 658{
3593266d
MC
659#ifdef FIPS_MODE
660 return NULL;
661#else
4c2883a9 662 return DSO_get_filename(prov->module);
3593266d 663#endif
4c2883a9
RL
664}
665
24626a47 666const char *ossl_provider_module_path(const OSSL_PROVIDER *prov)
4c2883a9 667{
3593266d
MC
668#ifdef FIPS_MODE
669 return NULL;
670#else
4c2883a9
RL
671 /* FIXME: Ensure it's a full path */
672 return DSO_get_filename(prov->module);
3593266d 673#endif
4c2883a9
RL
674}
675
676/* Wrappers around calls to the provider */
677void ossl_provider_teardown(const OSSL_PROVIDER *prov)
678{
679 if (prov->teardown != NULL)
a39eb840 680 prov->teardown(prov->provctx);
4c2883a9
RL
681}
682
683const OSSL_ITEM *ossl_provider_get_param_types(const OSSL_PROVIDER *prov)
684{
a39eb840
RL
685 return prov->get_param_types == NULL
686 ? NULL : prov->get_param_types(prov->provctx);
4c2883a9
RL
687}
688
4e7991b4 689int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
4c2883a9 690{
a39eb840
RL
691 return prov->get_params == NULL
692 ? 0 : prov->get_params(prov->provctx, params);
4c2883a9
RL
693}
694
099bd339
RL
695
696const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
697 int operation_id,
698 int *no_cache)
699{
a39eb840 700 return prov->query_operation(prov->provctx, operation_id, no_cache);
099bd339
RL
701}
702
4c2883a9
RL
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 */
715static const OSSL_ITEM param_types[] = {
e2146e12
RL
716 { OSSL_PARAM_UTF8_PTR, "openssl-version" },
717 { OSSL_PARAM_UTF8_PTR, "provider-name" },
4c2883a9
RL
718 { 0, NULL }
719};
720
721static const OSSL_ITEM *core_get_param_types(const OSSL_PROVIDER *prov)
722{
723 return param_types;
724}
725
4e7991b4 726static int core_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
4c2883a9
RL
727{
728 int i;
4e7991b4 729 OSSL_PARAM *p;
4c2883a9 730
ac1055ef
RL
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);
4c2883a9
RL
744 }
745
746 return 1;
747}
748
e7706e63
RL
749static OSSL_core_get_library_context_fn core_get_libctx; /* Check */
750static OPENSSL_CTX *core_get_libctx(const OSSL_PROVIDER *prov)
751{
752 return prov->libctx;
753}
754
da747958
MC
755static int core_thread_start(const OSSL_PROVIDER *prov,
756 OSSL_thread_stop_handler_fn handfn)
757{
6913f5fe 758 return ossl_init_thread_start(prov, prov->provctx, handfn);
da747958
MC
759}
760
6592ab81
RL
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
6ebc2f56
RL
767static 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 */
792static void core_add_error_vdata(const OSSL_PROVIDER *prov,
793 int num, va_list args)
794{
795 ERR_add_error_vdata(num, args);
796}
6592ab81 797#endif
6ebc2f56 798
b60cba3c
RS
799/*
800 * Functions provided by the core. Blank line separates "families" of related
801 * functions.
802 */
4c2883a9
RL
803static 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 },
e7706e63 806 { OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT, (void (*)(void))core_get_libctx },
da747958 807 { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start },
6592ab81 808#ifndef FIPS_MODE
6ebc2f56
RL
809 { OSSL_FUNC_CORE_PUT_ERROR, (void (*)(void))core_put_error },
810 { OSSL_FUNC_CORE_ADD_ERROR_VDATA, (void (*)(void))core_add_error_vdata },
6592ab81 811#endif
b60cba3c
RS
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
4c2883a9
RL
832 { 0, NULL }
833};
834static const OSSL_DISPATCH *core_dispatch = core_dispatch_;