]>
Commit | Line | Data |
---|---|---|
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 RL |
13 | #include <openssl/opensslv.h> |
14 | #include "internal/cryptlib.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 | ||
21 | static 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 |
29 | typedef struct { |
30 | char *name; | |
31 | char *value; | |
32 | } INFOPAIR; | |
33 | DEFINE_STACK_OF(INFOPAIR) | |
34 | ||
4c2883a9 RL |
35 | struct provider_store_st; /* Forward declaration */ |
36 | ||
37 | struct 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; |
e55008a9 | 50 | struct provider_store_st *store; /* The store this instance belongs to */ |
4c2883a9 RL |
51 | |
52 | /* Provider side functions */ | |
53 | OSSL_provider_teardown_fn *teardown; | |
54 | OSSL_provider_get_param_types_fn *get_param_types; | |
55 | OSSL_provider_get_params_fn *get_params; | |
099bd339 | 56 | OSSL_provider_query_operation_fn *query_operation; |
a39eb840 RL |
57 | |
58 | /* Provider side data */ | |
59 | void *provctx; | |
4c2883a9 RL |
60 | }; |
61 | DEFINE_STACK_OF(OSSL_PROVIDER) | |
62 | ||
63 | static int ossl_provider_cmp(const OSSL_PROVIDER * const *a, | |
64 | const OSSL_PROVIDER * const *b) | |
65 | { | |
66 | return strcmp((*a)->name, (*b)->name); | |
67 | } | |
68 | ||
69 | /*- | |
70 | * Provider Object store | |
71 | * ===================== | |
72 | * | |
73 | * The Provider Object store is a library context object, and therefore needs | |
74 | * an index. | |
75 | */ | |
76 | ||
77 | struct provider_store_st { | |
78 | STACK_OF(OSSL_PROVIDER) *providers; | |
79 | CRYPTO_RWLOCK *lock; | |
e55008a9 | 80 | unsigned int use_fallbacks:1; |
4c2883a9 | 81 | }; |
4c2883a9 RL |
82 | |
83 | static void provider_store_free(void *vstore) | |
84 | { | |
85 | struct provider_store_st *store = vstore; | |
86 | ||
87 | if (store == NULL) | |
88 | return; | |
89 | sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free); | |
90 | CRYPTO_THREAD_lock_free(store->lock); | |
91 | OPENSSL_free(store); | |
92 | } | |
93 | ||
1aedc35f | 94 | static void *provider_store_new(OPENSSL_CTX *ctx) |
4c2883a9 RL |
95 | { |
96 | struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store)); | |
c41f3ae0 | 97 | const struct predefined_providers_st *p = NULL; |
4c2883a9 RL |
98 | |
99 | if (store == NULL | |
100 | || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL | |
101 | || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) { | |
102 | provider_store_free(store); | |
e55008a9 | 103 | return NULL; |
4c2883a9 | 104 | } |
e55008a9 | 105 | store->use_fallbacks = 1; |
c41f3ae0 RL |
106 | |
107 | for (p = predefined_providers; p->name != NULL; p++) { | |
108 | OSSL_PROVIDER *prov = NULL; | |
109 | ||
110 | /* | |
111 | * We use the internal constructor directly here, | |
112 | * otherwise we get a call loop | |
113 | */ | |
114 | prov = provider_new(p->name, p->init); | |
115 | ||
116 | if (prov == NULL | |
117 | || sk_OSSL_PROVIDER_push(store->providers, prov) == 0) { | |
118 | ossl_provider_free(prov); | |
119 | provider_store_free(store); | |
120 | CRYPTOerr(CRYPTO_F_PROVIDER_STORE_NEW, ERR_R_INTERNAL_ERROR); | |
121 | return NULL; | |
122 | } | |
123 | prov->store = store; | |
124 | if(p->is_fallback) | |
125 | ossl_provider_set_fallback(prov); | |
126 | } | |
127 | ||
4c2883a9 RL |
128 | return store; |
129 | } | |
130 | ||
131 | static const OPENSSL_CTX_METHOD provider_store_method = { | |
132 | provider_store_new, | |
133 | provider_store_free, | |
134 | }; | |
135 | ||
4c2883a9 RL |
136 | static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx) |
137 | { | |
138 | struct provider_store_st *store = NULL; | |
139 | ||
1aedc35f MC |
140 | store = openssl_ctx_get_data(libctx, OPENSSL_CTX_PROVIDER_STORE_INDEX, |
141 | &provider_store_method); | |
4c2883a9 RL |
142 | if (store == NULL) |
143 | CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR); | |
144 | return store; | |
145 | } | |
146 | ||
4c2883a9 RL |
147 | OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name) |
148 | { | |
149 | struct provider_store_st *store = NULL; | |
150 | OSSL_PROVIDER *prov = NULL; | |
151 | ||
152 | if ((store = get_provider_store(libctx)) != NULL) { | |
153 | OSSL_PROVIDER tmpl = { 0, }; | |
154 | int i; | |
155 | ||
156 | tmpl.name = (char *)name; | |
157 | CRYPTO_THREAD_write_lock(store->lock); | |
158 | if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1 | |
159 | || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL | |
160 | || !ossl_provider_upref(prov)) | |
161 | prov = NULL; | |
162 | CRYPTO_THREAD_unlock(store->lock); | |
163 | } | |
164 | ||
165 | return prov; | |
166 | } | |
167 | ||
c41f3ae0 RL |
168 | /*- |
169 | * Provider Object methods | |
170 | * ======================= | |
171 | */ | |
172 | ||
173 | static OSSL_PROVIDER *provider_new(const char *name, | |
174 | OSSL_provider_init_fn *init_function) | |
175 | { | |
176 | OSSL_PROVIDER *prov = NULL; | |
177 | ||
178 | if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL | |
179 | #ifndef HAVE_ATOMICS | |
180 | || (prov->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL | |
181 | #endif | |
182 | || !ossl_provider_upref(prov) /* +1 One reference to be returned */ | |
183 | || (prov->name = OPENSSL_strdup(name)) == NULL) { | |
184 | ossl_provider_free(prov); | |
185 | CRYPTOerr(CRYPTO_F_PROVIDER_NEW, ERR_R_MALLOC_FAILURE); | |
186 | return NULL; | |
187 | } | |
188 | ||
189 | prov->init_function = init_function; | |
190 | return prov; | |
191 | } | |
192 | ||
193 | int ossl_provider_upref(OSSL_PROVIDER *prov) | |
194 | { | |
195 | int ref = 0; | |
196 | ||
ad14e8e5 SL |
197 | if (CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock) <= 0) |
198 | return 0; | |
c41f3ae0 RL |
199 | return ref; |
200 | } | |
201 | ||
4c2883a9 RL |
202 | OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name, |
203 | OSSL_provider_init_fn *init_function) | |
204 | { | |
205 | struct provider_store_st *store = NULL; | |
206 | OSSL_PROVIDER *prov = NULL; | |
207 | ||
208 | if ((store = get_provider_store(libctx)) == NULL) | |
209 | return NULL; | |
210 | ||
211 | if ((prov = ossl_provider_find(libctx, name)) != NULL) { /* refcount +1 */ | |
212 | ossl_provider_free(prov); /* refcount -1 */ | |
213 | CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, | |
214 | CRYPTO_R_PROVIDER_ALREADY_EXISTS); | |
215 | ERR_add_error_data(2, "name=", name); | |
216 | return NULL; | |
217 | } | |
218 | ||
c41f3ae0 RL |
219 | /* provider_new() generates an error, so no need here */ |
220 | if ((prov = provider_new(name, init_function)) == NULL) | |
4c2883a9 | 221 | return NULL; |
4c2883a9 RL |
222 | |
223 | CRYPTO_THREAD_write_lock(store->lock); | |
224 | if (!ossl_provider_upref(prov)) { /* +1 One reference for the store */ | |
225 | ossl_provider_free(prov); /* -1 Reference that was to be returned */ | |
226 | prov = NULL; | |
227 | } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) { | |
228 | ossl_provider_free(prov); /* -1 Store reference */ | |
229 | ossl_provider_free(prov); /* -1 Reference that was to be returned */ | |
230 | prov = NULL; | |
e55008a9 RL |
231 | } else { |
232 | prov->store = store; | |
4c2883a9 RL |
233 | } |
234 | CRYPTO_THREAD_unlock(store->lock); | |
235 | ||
236 | if (prov == NULL) | |
237 | CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE); | |
238 | ||
239 | /* | |
240 | * At this point, the provider is only partially "loaded". To be | |
241 | * fully "loaded", ossl_provider_activate() must also be called. | |
242 | */ | |
243 | ||
244 | return prov; | |
245 | } | |
246 | ||
ac1055ef RL |
247 | static void free_infopair(INFOPAIR *pair) |
248 | { | |
249 | OPENSSL_free(pair->name); | |
250 | OPENSSL_free(pair->value); | |
251 | OPENSSL_free(pair); | |
252 | } | |
253 | ||
4c2883a9 RL |
254 | void ossl_provider_free(OSSL_PROVIDER *prov) |
255 | { | |
256 | if (prov != NULL) { | |
257 | int ref = 0; | |
258 | ||
085bef9f | 259 | CRYPTO_DOWN_REF(&prov->refcnt, &ref, prov->refcnt_lock); |
4c2883a9 RL |
260 | |
261 | /* | |
e55008a9 RL |
262 | * When the refcount drops below two, the store is the only |
263 | * possible reference, or it has already been taken away from | |
264 | * the store (this may happen if a provider was activated | |
265 | * because it's a fallback, but isn't currently used) | |
4c2883a9 RL |
266 | * When that happens, the provider is inactivated. |
267 | */ | |
e55008a9 | 268 | if (ref < 2 && prov->flag_initialized) { |
4c2883a9 | 269 | if (prov->teardown != NULL) |
a39eb840 | 270 | prov->teardown(prov->provctx); |
4c2883a9 RL |
271 | prov->flag_initialized = 0; |
272 | } | |
273 | ||
274 | /* | |
275 | * When the refcount drops to zero, it has been taken out of | |
276 | * the store. All we have to do here is clean it out. | |
277 | */ | |
278 | if (ref == 0) { | |
3593266d | 279 | #ifndef FIPS_MODE |
4c2883a9 | 280 | DSO_free(prov->module); |
3593266d | 281 | #endif |
4c2883a9 | 282 | OPENSSL_free(prov->name); |
ac1055ef RL |
283 | OPENSSL_free(prov->path); |
284 | sk_INFOPAIR_pop_free(prov->parameters, free_infopair); | |
085bef9f RL |
285 | #ifndef HAVE_ATOMICS |
286 | CRYPTO_THREAD_lock_free(prov->refcnt_lock); | |
287 | #endif | |
4c2883a9 RL |
288 | OPENSSL_free(prov); |
289 | } | |
290 | } | |
291 | } | |
292 | ||
ac1055ef RL |
293 | /* Setters */ |
294 | int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path) | |
295 | { | |
296 | OPENSSL_free(prov->path); | |
297 | if (module_path == NULL) | |
298 | return 1; | |
299 | if ((prov->path = OPENSSL_strdup(module_path)) != NULL) | |
300 | return 1; | |
301 | CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_SET_MODULE_PATH, ERR_R_MALLOC_FAILURE); | |
302 | return 0; | |
303 | } | |
304 | ||
305 | int ossl_provider_add_parameter(OSSL_PROVIDER *prov, | |
306 | const char *name, const char *value) | |
307 | { | |
308 | INFOPAIR *pair = NULL; | |
309 | ||
310 | if ((pair = OPENSSL_zalloc(sizeof(*pair))) != NULL | |
311 | && (prov->parameters != NULL | |
312 | || (prov->parameters = sk_INFOPAIR_new_null()) != NULL) | |
313 | && (pair->name = OPENSSL_strdup(name)) != NULL | |
314 | && (pair->value = OPENSSL_strdup(value)) != NULL | |
315 | && sk_INFOPAIR_push(prov->parameters, pair) > 0) | |
316 | return 1; | |
317 | ||
318 | if (pair != NULL) { | |
319 | OPENSSL_free(pair->name); | |
320 | OPENSSL_free(pair->value); | |
321 | OPENSSL_free(pair); | |
322 | } | |
323 | CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_PARAMETER, ERR_R_MALLOC_FAILURE); | |
324 | return 0; | |
325 | } | |
326 | ||
4c2883a9 RL |
327 | /* |
328 | * Provider activation. | |
329 | * | |
330 | * What "activation" means depends on the provider form; for built in | |
331 | * providers (in the library or the application alike), the provider | |
332 | * can already be considered to be loaded, all that's needed is to | |
333 | * initialize it. However, for dynamically loadable provider modules, | |
334 | * we must first load that module. | |
335 | * | |
336 | * Built in modules are distinguished from dynamically loaded modules | |
337 | * with an already assigned init function. | |
338 | */ | |
339 | static const OSSL_DISPATCH *core_dispatch; /* Define further down */ | |
340 | ||
e55008a9 RL |
341 | /* |
342 | * Internal version that doesn't affect the store flags, and thereby avoid | |
343 | * locking. Direct callers must remember to set the store flags when | |
b1eb3fd7 MC |
344 | * appropriate. The libctx parameter is only necessary when FIPS_MODE is set |
345 | * (i.e. we are being called from inside the FIPS module) - it is ignored for | |
346 | * other uses. | |
e55008a9 | 347 | */ |
b1eb3fd7 | 348 | static int provider_activate(OSSL_PROVIDER *prov, OPENSSL_CTX *libctx) |
4c2883a9 RL |
349 | { |
350 | const OSSL_DISPATCH *provider_dispatch = NULL; | |
351 | ||
352 | if (prov->flag_initialized) | |
353 | return 1; | |
354 | ||
355 | /* | |
356 | * If the init function isn't set, it indicates that this provider is | |
357 | * a loadable module. | |
358 | */ | |
359 | if (prov->init_function == NULL) { | |
3593266d MC |
360 | #ifdef FIPS_MODE |
361 | return 0; | |
362 | #else | |
4c2883a9 | 363 | if (prov->module == NULL) { |
ac1055ef RL |
364 | char *allocated_path = NULL; |
365 | const char *module_path = NULL; | |
366 | char *merged_path = NULL; | |
4c2883a9 RL |
367 | const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES"); |
368 | ||
369 | if ((prov->module = DSO_new()) == NULL) { | |
370 | /* DSO_new() generates an error already */ | |
371 | return 0; | |
372 | } | |
373 | ||
374 | if (load_dir == NULL) | |
375 | load_dir = MODULESDIR; | |
376 | ||
377 | DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS, | |
378 | DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL); | |
ac1055ef RL |
379 | |
380 | module_path = prov->path; | |
381 | if (module_path == NULL) | |
382 | module_path = allocated_path = | |
383 | DSO_convert_filename(prov->module, prov->name); | |
384 | if (module_path != NULL) | |
385 | merged_path = DSO_merge(prov->module, module_path, load_dir); | |
386 | ||
387 | if (merged_path == NULL | |
388 | || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) { | |
4c2883a9 RL |
389 | DSO_free(prov->module); |
390 | prov->module = NULL; | |
391 | } | |
392 | ||
ac1055ef RL |
393 | OPENSSL_free(merged_path); |
394 | OPENSSL_free(allocated_path); | |
4c2883a9 RL |
395 | } |
396 | ||
397 | if (prov->module != NULL) | |
398 | prov->init_function = (OSSL_provider_init_fn *) | |
399 | DSO_bind_func(prov->module, "OSSL_provider_init"); | |
3593266d | 400 | #endif |
4c2883a9 RL |
401 | } |
402 | ||
b1eb3fd7 MC |
403 | /* |
404 | * We call the initialise function for the provider. | |
405 | * | |
406 | * If FIPS_MODE is defined then we are inside the FIPS module and are about | |
407 | * to recursively initialise ourselves. We need to do this so that we can | |
408 | * get all the provider callback functions set up in order for us to be able | |
409 | * to make EVP calls from within the FIPS module itself. Only algorithms | |
410 | * from the FIPS module itself are available via the FIPS module EVP | |
411 | * interface, i.e. we only ever have one provider available inside the FIPS | |
412 | * module - the FIPS provider itself. | |
413 | * | |
414 | * For modules in general we cannot know what value will be used for the | |
415 | * provctx - it is a "black box". But for the FIPS module we know that the | |
416 | * provctx is really a library context. We default the provctx value to the | |
417 | * same library context as was used for the EVP call that caused this call | |
418 | * to "provider_activate". | |
419 | */ | |
420 | #ifdef FIPS_MODE | |
421 | prov->provctx = libctx; | |
422 | #endif | |
4c2883a9 | 423 | if (prov->init_function == NULL |
a39eb840 RL |
424 | || !prov->init_function(prov, core_dispatch, &provider_dispatch, |
425 | &prov->provctx)) { | |
e55008a9 | 426 | CRYPTOerr(CRYPTO_F_PROVIDER_ACTIVATE, ERR_R_INIT_FAIL); |
4c2883a9 | 427 | ERR_add_error_data(2, "name=", prov->name); |
3593266d | 428 | #ifndef FIPS_MODE |
4c2883a9 RL |
429 | DSO_free(prov->module); |
430 | prov->module = NULL; | |
3593266d | 431 | #endif |
4c2883a9 RL |
432 | return 0; |
433 | } | |
434 | ||
435 | for (; provider_dispatch->function_id != 0; provider_dispatch++) { | |
436 | switch (provider_dispatch->function_id) { | |
437 | case OSSL_FUNC_PROVIDER_TEARDOWN: | |
438 | prov->teardown = | |
439 | OSSL_get_provider_teardown(provider_dispatch); | |
440 | break; | |
441 | case OSSL_FUNC_PROVIDER_GET_PARAM_TYPES: | |
442 | prov->get_param_types = | |
443 | OSSL_get_provider_get_param_types(provider_dispatch); | |
444 | break; | |
445 | case OSSL_FUNC_PROVIDER_GET_PARAMS: | |
446 | prov->get_params = | |
447 | OSSL_get_provider_get_params(provider_dispatch); | |
448 | break; | |
099bd339 RL |
449 | case OSSL_FUNC_PROVIDER_QUERY_OPERATION: |
450 | prov->query_operation = | |
451 | OSSL_get_provider_query_operation(provider_dispatch); | |
452 | break; | |
4c2883a9 RL |
453 | } |
454 | } | |
455 | ||
456 | /* With this flag set, this provider has become fully "loaded". */ | |
457 | prov->flag_initialized = 1; | |
458 | ||
459 | return 1; | |
460 | } | |
461 | ||
e55008a9 RL |
462 | int ossl_provider_activate(OSSL_PROVIDER *prov) |
463 | { | |
b1eb3fd7 | 464 | if (provider_activate(prov, NULL)) { |
e55008a9 RL |
465 | CRYPTO_THREAD_write_lock(prov->store->lock); |
466 | prov->store->use_fallbacks = 0; | |
467 | CRYPTO_THREAD_unlock(prov->store->lock); | |
468 | return 1; | |
469 | } | |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
a39eb840 RL |
474 | void *ossl_provider_ctx(const OSSL_PROVIDER *prov) |
475 | { | |
476 | return prov->provctx; | |
477 | } | |
478 | ||
e55008a9 RL |
479 | |
480 | static int provider_forall_loaded(struct provider_store_st *store, | |
481 | int *found_activated, | |
482 | int (*cb)(OSSL_PROVIDER *provider, | |
483 | void *cbdata), | |
484 | void *cbdata) | |
485 | { | |
486 | int i; | |
487 | int ret = 1; | |
488 | int num_provs = sk_OSSL_PROVIDER_num(store->providers); | |
489 | ||
490 | if (found_activated != NULL) | |
491 | *found_activated = 0; | |
492 | for (i = 0; i < num_provs; i++) { | |
493 | OSSL_PROVIDER *prov = | |
494 | sk_OSSL_PROVIDER_value(store->providers, i); | |
495 | ||
496 | if (prov->flag_initialized) { | |
497 | if (found_activated != NULL) | |
498 | *found_activated = 1; | |
499 | if (!(ret = cb(prov, cbdata))) | |
500 | break; | |
501 | } | |
502 | } | |
503 | ||
504 | return ret; | |
505 | } | |
506 | ||
85e2417c RL |
507 | int ossl_provider_forall_loaded(OPENSSL_CTX *ctx, |
508 | int (*cb)(OSSL_PROVIDER *provider, | |
509 | void *cbdata), | |
510 | void *cbdata) | |
511 | { | |
512 | int ret = 1; | |
513 | int i; | |
514 | struct provider_store_st *store = get_provider_store(ctx); | |
515 | ||
516 | if (store != NULL) { | |
e55008a9 RL |
517 | int found_activated = 0; |
518 | ||
85e2417c | 519 | CRYPTO_THREAD_read_lock(store->lock); |
e55008a9 | 520 | ret = provider_forall_loaded(store, &found_activated, cb, cbdata); |
85e2417c | 521 | |
e55008a9 RL |
522 | /* |
523 | * If there's nothing activated ever in this store, try to activate | |
524 | * all fallbacks. | |
525 | */ | |
526 | if (!found_activated && store->use_fallbacks) { | |
527 | int num_provs = sk_OSSL_PROVIDER_num(store->providers); | |
528 | int activated_fallback_count = 0; | |
529 | ||
530 | for (i = 0; i < num_provs; i++) { | |
531 | OSSL_PROVIDER *prov = | |
532 | sk_OSSL_PROVIDER_value(store->providers, i); | |
533 | ||
534 | /* | |
535 | * Note that we don't care if the activation succeeds or | |
536 | * not. If it doesn't succeed, then the next loop will | |
537 | * fail anyway. | |
538 | */ | |
539 | if (prov->flag_fallback) { | |
540 | activated_fallback_count++; | |
b1eb3fd7 | 541 | provider_activate(prov, ctx); |
e55008a9 RL |
542 | } |
543 | } | |
544 | ||
545 | if (activated_fallback_count > 0) { | |
546 | /* | |
547 | * We assume that all fallbacks have been added to the store | |
548 | * before any fallback is activated. | |
549 | * TODO: We may have to reconsider this, IF we find ourselves | |
550 | * adding fallbacks after any previous fallback has been | |
551 | * activated. | |
552 | */ | |
553 | store->use_fallbacks = 0; | |
554 | ||
555 | /* | |
556 | * Now that we've activated available fallbacks, try a | |
557 | * second sweep | |
558 | */ | |
559 | ret = provider_forall_loaded(store, NULL, cb, cbdata); | |
560 | } | |
85e2417c RL |
561 | } |
562 | CRYPTO_THREAD_unlock(store->lock); | |
563 | } | |
564 | ||
565 | return ret; | |
566 | } | |
567 | ||
e55008a9 RL |
568 | /* Setters of Provider Object data */ |
569 | int ossl_provider_set_fallback(OSSL_PROVIDER *prov) | |
570 | { | |
571 | if (prov == NULL) | |
572 | return 0; | |
573 | ||
574 | prov->flag_fallback = 1; | |
575 | return 1; | |
576 | } | |
577 | ||
4c2883a9 | 578 | /* Getters of Provider Object data */ |
24626a47 | 579 | const char *ossl_provider_name(const OSSL_PROVIDER *prov) |
4c2883a9 RL |
580 | { |
581 | return prov->name; | |
582 | } | |
583 | ||
24626a47 | 584 | const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov) |
4c2883a9 RL |
585 | { |
586 | return prov->module; | |
587 | } | |
588 | ||
24626a47 | 589 | const char *ossl_provider_module_name(const OSSL_PROVIDER *prov) |
4c2883a9 | 590 | { |
3593266d MC |
591 | #ifdef FIPS_MODE |
592 | return NULL; | |
593 | #else | |
4c2883a9 | 594 | return DSO_get_filename(prov->module); |
3593266d | 595 | #endif |
4c2883a9 RL |
596 | } |
597 | ||
24626a47 | 598 | const char *ossl_provider_module_path(const OSSL_PROVIDER *prov) |
4c2883a9 | 599 | { |
3593266d MC |
600 | #ifdef FIPS_MODE |
601 | return NULL; | |
602 | #else | |
4c2883a9 RL |
603 | /* FIXME: Ensure it's a full path */ |
604 | return DSO_get_filename(prov->module); | |
3593266d | 605 | #endif |
4c2883a9 RL |
606 | } |
607 | ||
608 | /* Wrappers around calls to the provider */ | |
609 | void ossl_provider_teardown(const OSSL_PROVIDER *prov) | |
610 | { | |
611 | if (prov->teardown != NULL) | |
a39eb840 | 612 | prov->teardown(prov->provctx); |
4c2883a9 RL |
613 | } |
614 | ||
615 | const OSSL_ITEM *ossl_provider_get_param_types(const OSSL_PROVIDER *prov) | |
616 | { | |
a39eb840 RL |
617 | return prov->get_param_types == NULL |
618 | ? NULL : prov->get_param_types(prov->provctx); | |
4c2883a9 RL |
619 | } |
620 | ||
621 | int ossl_provider_get_params(const OSSL_PROVIDER *prov, | |
622 | const OSSL_PARAM params[]) | |
623 | { | |
a39eb840 RL |
624 | return prov->get_params == NULL |
625 | ? 0 : prov->get_params(prov->provctx, params); | |
4c2883a9 RL |
626 | } |
627 | ||
099bd339 RL |
628 | |
629 | const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov, | |
630 | int operation_id, | |
631 | int *no_cache) | |
632 | { | |
a39eb840 | 633 | return prov->query_operation(prov->provctx, operation_id, no_cache); |
099bd339 RL |
634 | } |
635 | ||
4c2883a9 RL |
636 | /*- |
637 | * Core functions for the provider | |
638 | * =============================== | |
639 | * | |
640 | * This is the set of functions that the core makes available to the provider | |
641 | */ | |
642 | ||
643 | /* | |
644 | * This returns a list of Provider Object parameters with their types, for | |
645 | * discovery. We do not expect that many providers will use this, but one | |
646 | * never knows. | |
647 | */ | |
648 | static const OSSL_ITEM param_types[] = { | |
e2146e12 RL |
649 | { OSSL_PARAM_UTF8_PTR, "openssl-version" }, |
650 | { OSSL_PARAM_UTF8_PTR, "provider-name" }, | |
4c2883a9 RL |
651 | { 0, NULL } |
652 | }; | |
653 | ||
654 | static const OSSL_ITEM *core_get_param_types(const OSSL_PROVIDER *prov) | |
655 | { | |
656 | return param_types; | |
657 | } | |
658 | ||
659 | static int core_get_params(const OSSL_PROVIDER *prov, const OSSL_PARAM params[]) | |
660 | { | |
661 | int i; | |
ac1055ef | 662 | const OSSL_PARAM *p; |
4c2883a9 | 663 | |
ac1055ef RL |
664 | if ((p = OSSL_PARAM_locate(params, "openssl-version")) != NULL) |
665 | OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR); | |
666 | if ((p = OSSL_PARAM_locate(params, "provider-name")) != NULL) | |
667 | OSSL_PARAM_set_utf8_ptr(p, prov->name); | |
668 | ||
669 | if (prov->parameters == NULL) | |
670 | return 1; | |
671 | ||
672 | for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) { | |
673 | INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i); | |
674 | ||
675 | if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL) | |
676 | OSSL_PARAM_set_utf8_ptr(p, pair->value); | |
4c2883a9 RL |
677 | } |
678 | ||
679 | return 1; | |
680 | } | |
681 | ||
682 | static const OSSL_DISPATCH core_dispatch_[] = { | |
683 | { OSSL_FUNC_CORE_GET_PARAM_TYPES, (void (*)(void))core_get_param_types }, | |
684 | { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params }, | |
3593266d MC |
685 | { OSSL_FUNC_CORE_PUT_ERROR, (void (*)(void))ERR_put_error }, |
686 | { OSSL_FUNC_CORE_ADD_ERROR_VDATA, (void (*)(void))ERR_add_error_vdata }, | |
4c2883a9 RL |
687 | { 0, NULL } |
688 | }; | |
689 | static const OSSL_DISPATCH *core_dispatch = core_dispatch_; |