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