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