/src/openssl/crypto/provider_core.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2023 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 <assert.h> |
11 | | #include <openssl/core.h> |
12 | | #include <openssl/core_dispatch.h> |
13 | | #include <openssl/core_names.h> |
14 | | #include <openssl/provider.h> |
15 | | #include <openssl/params.h> |
16 | | #include <openssl/opensslv.h> |
17 | | #include "crypto/cryptlib.h" |
18 | | #ifndef FIPS_MODULE |
19 | | #include "crypto/decoder.h" /* ossl_decoder_store_cache_flush */ |
20 | | #include "crypto/encoder.h" /* ossl_encoder_store_cache_flush */ |
21 | | #include "crypto/store.h" /* ossl_store_loader_store_cache_flush */ |
22 | | #endif |
23 | | #include "crypto/evp.h" /* evp_method_store_cache_flush */ |
24 | | #include "crypto/rand.h" |
25 | | #include "internal/nelem.h" |
26 | | #include "internal/thread_once.h" |
27 | | #include "internal/provider.h" |
28 | | #include "internal/refcount.h" |
29 | | #include "internal/bio.h" |
30 | | #include "internal/core.h" |
31 | | #include "provider_local.h" |
32 | | #include "crypto/context.h" |
33 | | #ifndef FIPS_MODULE |
34 | | # include <openssl/self_test.h> |
35 | | #endif |
36 | | |
37 | | /* |
38 | | * This file defines and uses a number of different structures: |
39 | | * |
40 | | * OSSL_PROVIDER (provider_st): Used to represent all information related to a |
41 | | * single instance of a provider. |
42 | | * |
43 | | * provider_store_st: Holds information about the collection of providers that |
44 | | * are available within the current library context (OSSL_LIB_CTX). It also |
45 | | * holds configuration information about providers that could be loaded at some |
46 | | * future point. |
47 | | * |
48 | | * OSSL_PROVIDER_CHILD_CB: An instance of this structure holds the callbacks |
49 | | * that have been registered for a child library context and the associated |
50 | | * provider that registered those callbacks. |
51 | | * |
52 | | * Where a child library context exists then it has its own instance of the |
53 | | * provider store. Each provider that exists in the parent provider store, has |
54 | | * an associated child provider in the child library context's provider store. |
55 | | * As providers get activated or deactivated this needs to be mirrored in the |
56 | | * associated child providers. |
57 | | * |
58 | | * LOCKING |
59 | | * ======= |
60 | | * |
61 | | * There are a number of different locks used in this file and it is important |
62 | | * to understand how they should be used in order to avoid deadlocks. |
63 | | * |
64 | | * Fields within a structure can often be "write once" on creation, and then |
65 | | * "read many". Creation of a structure is done by a single thread, and |
66 | | * therefore no lock is required for the "write once/read many" fields. It is |
67 | | * safe for multiple threads to read these fields without a lock, because they |
68 | | * will never be changed. |
69 | | * |
70 | | * However some fields may be changed after a structure has been created and |
71 | | * shared between multiple threads. Where this is the case a lock is required. |
72 | | * |
73 | | * The locks available are: |
74 | | * |
75 | | * The provider flag_lock: Used to control updates to the various provider |
76 | | * "flags" (flag_initialized and flag_activated). |
77 | | * |
78 | | * The provider activatecnt_lock: Used to control updates to the provider |
79 | | * activatecnt value. |
80 | | * |
81 | | * The provider optbits_lock: Used to control access to the provider's |
82 | | * operation_bits and operation_bits_sz fields. |
83 | | * |
84 | | * The store default_path_lock: Used to control access to the provider store's |
85 | | * default search path value (default_path) |
86 | | * |
87 | | * The store lock: Used to control the stack of provider's held within the |
88 | | * provider store, as well as the stack of registered child provider callbacks. |
89 | | * |
90 | | * As a general rule-of-thumb it is best to: |
91 | | * - keep the scope of the code that is protected by a lock to the absolute |
92 | | * minimum possible; |
93 | | * - try to keep the scope of the lock to within a single function (i.e. avoid |
94 | | * making calls to other functions while holding a lock); |
95 | | * - try to only ever hold one lock at a time. |
96 | | * |
97 | | * Unfortunately, it is not always possible to stick to the above guidelines. |
98 | | * Where they are not adhered to there is always a danger of inadvertently |
99 | | * introducing the possibility of deadlock. The following rules MUST be adhered |
100 | | * to in order to avoid that: |
101 | | * - Holding multiple locks at the same time is only allowed for the |
102 | | * provider store lock, the provider activatecnt_lock and the provider flag_lock. |
103 | | * - When holding multiple locks they must be acquired in the following order of |
104 | | * precedence: |
105 | | * 1) provider store lock |
106 | | * 2) provider flag_lock |
107 | | * 3) provider activatecnt_lock |
108 | | * - When releasing locks they must be released in the reverse order to which |
109 | | * they were acquired |
110 | | * - No locks may be held when making an upcall. NOTE: Some common functions |
111 | | * can make upcalls as part of their normal operation. If you need to call |
112 | | * some other function while holding a lock make sure you know whether it |
113 | | * will make any upcalls or not. For example ossl_provider_up_ref() can call |
114 | | * ossl_provider_up_ref_parent() which can call the c_prov_up_ref() upcall. |
115 | | * - It is permissible to hold the store and flag locks when calling child |
116 | | * provider callbacks. No other locks may be held during such callbacks. |
117 | | */ |
118 | | |
119 | | static OSSL_PROVIDER *provider_new(const char *name, |
120 | | OSSL_provider_init_fn *init_function, |
121 | | STACK_OF(INFOPAIR) *parameters); |
122 | | |
123 | | /*- |
124 | | * Provider Object structure |
125 | | * ========================= |
126 | | */ |
127 | | |
128 | | #ifndef FIPS_MODULE |
129 | | typedef struct { |
130 | | OSSL_PROVIDER *prov; |
131 | | int (*create_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata); |
132 | | int (*remove_cb)(const OSSL_CORE_HANDLE *provider, void *cbdata); |
133 | | int (*global_props_cb)(const char *props, void *cbdata); |
134 | | void *cbdata; |
135 | | } OSSL_PROVIDER_CHILD_CB; |
136 | | DEFINE_STACK_OF(OSSL_PROVIDER_CHILD_CB) |
137 | | #endif |
138 | | |
139 | | struct provider_store_st; /* Forward declaration */ |
140 | | |
141 | | struct ossl_provider_st { |
142 | | /* Flag bits */ |
143 | | unsigned int flag_initialized:1; |
144 | | unsigned int flag_activated:1; |
145 | | |
146 | | /* Getting and setting the flags require synchronization */ |
147 | | CRYPTO_RWLOCK *flag_lock; |
148 | | |
149 | | /* OpenSSL library side data */ |
150 | | CRYPTO_REF_COUNT refcnt; |
151 | | CRYPTO_RWLOCK *activatecnt_lock; /* For the activatecnt counter */ |
152 | | int activatecnt; |
153 | | char *name; |
154 | | char *path; |
155 | | DSO *module; |
156 | | OSSL_provider_init_fn *init_function; |
157 | | STACK_OF(INFOPAIR) *parameters; |
158 | | OSSL_LIB_CTX *libctx; /* The library context this instance is in */ |
159 | | struct provider_store_st *store; /* The store this instance belongs to */ |
160 | | #ifndef FIPS_MODULE |
161 | | /* |
162 | | * In the FIPS module inner provider, this isn't needed, since the |
163 | | * error upcalls are always direct calls to the outer provider. |
164 | | */ |
165 | | int error_lib; /* ERR library number, one for each provider */ |
166 | | # ifndef OPENSSL_NO_ERR |
167 | | ERR_STRING_DATA *error_strings; /* Copy of what the provider gives us */ |
168 | | # endif |
169 | | #endif |
170 | | |
171 | | /* Provider side functions */ |
172 | | OSSL_FUNC_provider_teardown_fn *teardown; |
173 | | OSSL_FUNC_provider_gettable_params_fn *gettable_params; |
174 | | OSSL_FUNC_provider_get_params_fn *get_params; |
175 | | OSSL_FUNC_provider_get_capabilities_fn *get_capabilities; |
176 | | OSSL_FUNC_provider_self_test_fn *self_test; |
177 | | OSSL_FUNC_provider_query_operation_fn *query_operation; |
178 | | OSSL_FUNC_provider_unquery_operation_fn *unquery_operation; |
179 | | |
180 | | /* |
181 | | * Cache of bit to indicate of query_operation() has been called on |
182 | | * a specific operation or not. |
183 | | */ |
184 | | unsigned char *operation_bits; |
185 | | size_t operation_bits_sz; |
186 | | CRYPTO_RWLOCK *opbits_lock; |
187 | | |
188 | | #ifndef FIPS_MODULE |
189 | | /* Whether this provider is the child of some other provider */ |
190 | | const OSSL_CORE_HANDLE *handle; |
191 | | unsigned int ischild:1; |
192 | | #endif |
193 | | |
194 | | /* Provider side data */ |
195 | | void *provctx; |
196 | | const OSSL_DISPATCH *dispatch; |
197 | | }; |
198 | | DEFINE_STACK_OF(OSSL_PROVIDER) |
199 | | |
200 | | static int ossl_provider_cmp(const OSSL_PROVIDER * const *a, |
201 | | const OSSL_PROVIDER * const *b) |
202 | 0 | { |
203 | 0 | return strcmp((*a)->name, (*b)->name); |
204 | 0 | } |
205 | | |
206 | | /*- |
207 | | * Provider Object store |
208 | | * ===================== |
209 | | * |
210 | | * The Provider Object store is a library context object, and therefore needs |
211 | | * an index. |
212 | | */ |
213 | | |
214 | | struct provider_store_st { |
215 | | OSSL_LIB_CTX *libctx; |
216 | | STACK_OF(OSSL_PROVIDER) *providers; |
217 | | STACK_OF(OSSL_PROVIDER_CHILD_CB) *child_cbs; |
218 | | CRYPTO_RWLOCK *default_path_lock; |
219 | | CRYPTO_RWLOCK *lock; |
220 | | char *default_path; |
221 | | OSSL_PROVIDER_INFO *provinfo; |
222 | | size_t numprovinfo; |
223 | | size_t provinfosz; |
224 | | unsigned int use_fallbacks:1; |
225 | | unsigned int freeing:1; |
226 | | }; |
227 | | |
228 | | /* |
229 | | * provider_deactivate_free() is a wrapper around ossl_provider_deactivate() |
230 | | * and ossl_provider_free(), called as needed. |
231 | | * Since this is only called when the provider store is being emptied, we |
232 | | * don't need to care about any lock. |
233 | | */ |
234 | | static void provider_deactivate_free(OSSL_PROVIDER *prov) |
235 | 15 | { |
236 | 15 | if (prov->flag_activated) |
237 | 15 | ossl_provider_deactivate(prov, 1); |
238 | 15 | ossl_provider_free(prov); |
239 | 15 | } |
240 | | |
241 | | #ifndef FIPS_MODULE |
242 | | static void ossl_provider_child_cb_free(OSSL_PROVIDER_CHILD_CB *cb) |
243 | 0 | { |
244 | 0 | OPENSSL_free(cb); |
245 | 0 | } |
246 | | #endif |
247 | | |
248 | | static void infopair_free(INFOPAIR *pair) |
249 | 0 | { |
250 | 0 | OPENSSL_free(pair->name); |
251 | 0 | OPENSSL_free(pair->value); |
252 | 0 | OPENSSL_free(pair); |
253 | 0 | } |
254 | | |
255 | | static INFOPAIR *infopair_copy(const INFOPAIR *src) |
256 | 0 | { |
257 | 0 | INFOPAIR *dest = OPENSSL_zalloc(sizeof(*dest)); |
258 | |
|
259 | 0 | if (dest == NULL) |
260 | 0 | return NULL; |
261 | 0 | if (src->name != NULL) { |
262 | 0 | dest->name = OPENSSL_strdup(src->name); |
263 | 0 | if (dest->name == NULL) |
264 | 0 | goto err; |
265 | 0 | } |
266 | 0 | if (src->value != NULL) { |
267 | 0 | dest->value = OPENSSL_strdup(src->value); |
268 | 0 | if (dest->value == NULL) |
269 | 0 | goto err; |
270 | 0 | } |
271 | 0 | return dest; |
272 | 0 | err: |
273 | 0 | OPENSSL_free(dest->name); |
274 | 0 | OPENSSL_free(dest); |
275 | 0 | return NULL; |
276 | 0 | } |
277 | | |
278 | | void ossl_provider_info_clear(OSSL_PROVIDER_INFO *info) |
279 | 0 | { |
280 | 0 | OPENSSL_free(info->name); |
281 | 0 | OPENSSL_free(info->path); |
282 | 0 | sk_INFOPAIR_pop_free(info->parameters, infopair_free); |
283 | 0 | } |
284 | | |
285 | | void ossl_provider_store_free(void *vstore) |
286 | 15 | { |
287 | 15 | struct provider_store_st *store = vstore; |
288 | 15 | size_t i; |
289 | | |
290 | 15 | if (store == NULL) |
291 | 0 | return; |
292 | 15 | store->freeing = 1; |
293 | 15 | OPENSSL_free(store->default_path); |
294 | 15 | sk_OSSL_PROVIDER_pop_free(store->providers, provider_deactivate_free); |
295 | 15 | #ifndef FIPS_MODULE |
296 | 15 | sk_OSSL_PROVIDER_CHILD_CB_pop_free(store->child_cbs, |
297 | 15 | ossl_provider_child_cb_free); |
298 | 15 | #endif |
299 | 15 | CRYPTO_THREAD_lock_free(store->default_path_lock); |
300 | 15 | CRYPTO_THREAD_lock_free(store->lock); |
301 | 15 | for (i = 0; i < store->numprovinfo; i++) |
302 | 0 | ossl_provider_info_clear(&store->provinfo[i]); |
303 | 15 | OPENSSL_free(store->provinfo); |
304 | 15 | OPENSSL_free(store); |
305 | 15 | } |
306 | | |
307 | | void *ossl_provider_store_new(OSSL_LIB_CTX *ctx) |
308 | 15 | { |
309 | 15 | struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store)); |
310 | | |
311 | 15 | if (store == NULL |
312 | 15 | || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL |
313 | 15 | || (store->default_path_lock = CRYPTO_THREAD_lock_new()) == NULL |
314 | 15 | #ifndef FIPS_MODULE |
315 | 15 | || (store->child_cbs = sk_OSSL_PROVIDER_CHILD_CB_new_null()) == NULL |
316 | 15 | #endif |
317 | 15 | || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) { |
318 | 0 | ossl_provider_store_free(store); |
319 | 0 | return NULL; |
320 | 0 | } |
321 | 15 | store->libctx = ctx; |
322 | 15 | store->use_fallbacks = 1; |
323 | | |
324 | 15 | return store; |
325 | 15 | } |
326 | | |
327 | | static struct provider_store_st *get_provider_store(OSSL_LIB_CTX *libctx) |
328 | 25.0k | { |
329 | 25.0k | struct provider_store_st *store = NULL; |
330 | | |
331 | 25.0k | store = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_STORE_INDEX); |
332 | 25.0k | if (store == NULL) |
333 | 25.0k | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
334 | 25.0k | return store; |
335 | 25.0k | } |
336 | | |
337 | | int ossl_provider_disable_fallback_loading(OSSL_LIB_CTX *libctx) |
338 | 0 | { |
339 | 0 | struct provider_store_st *store; |
340 | |
|
341 | 0 | if ((store = get_provider_store(libctx)) != NULL) { |
342 | 0 | if (!CRYPTO_THREAD_write_lock(store->lock)) |
343 | 0 | return 0; |
344 | 0 | store->use_fallbacks = 0; |
345 | 0 | CRYPTO_THREAD_unlock(store->lock); |
346 | 0 | return 1; |
347 | 0 | } |
348 | 0 | return 0; |
349 | 0 | } |
350 | | |
351 | 0 | #define BUILTINS_BLOCK_SIZE 10 |
352 | | |
353 | | int ossl_provider_info_add_to_store(OSSL_LIB_CTX *libctx, |
354 | | OSSL_PROVIDER_INFO *entry) |
355 | 0 | { |
356 | 0 | struct provider_store_st *store = get_provider_store(libctx); |
357 | 0 | int ret = 0; |
358 | |
|
359 | 0 | if (entry->name == NULL) { |
360 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); |
361 | 0 | return 0; |
362 | 0 | } |
363 | | |
364 | 0 | if (store == NULL) { |
365 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
366 | 0 | return 0; |
367 | 0 | } |
368 | | |
369 | 0 | if (!CRYPTO_THREAD_write_lock(store->lock)) |
370 | 0 | return 0; |
371 | 0 | if (store->provinfosz == 0) { |
372 | 0 | store->provinfo = OPENSSL_zalloc(sizeof(*store->provinfo) |
373 | 0 | * BUILTINS_BLOCK_SIZE); |
374 | 0 | if (store->provinfo == NULL) |
375 | 0 | goto err; |
376 | 0 | store->provinfosz = BUILTINS_BLOCK_SIZE; |
377 | 0 | } else if (store->numprovinfo == store->provinfosz) { |
378 | 0 | OSSL_PROVIDER_INFO *tmpbuiltins; |
379 | 0 | size_t newsz = store->provinfosz + BUILTINS_BLOCK_SIZE; |
380 | |
|
381 | 0 | tmpbuiltins = OPENSSL_realloc(store->provinfo, |
382 | 0 | sizeof(*store->provinfo) * newsz); |
383 | 0 | if (tmpbuiltins == NULL) |
384 | 0 | goto err; |
385 | 0 | store->provinfo = tmpbuiltins; |
386 | 0 | store->provinfosz = newsz; |
387 | 0 | } |
388 | 0 | store->provinfo[store->numprovinfo] = *entry; |
389 | 0 | store->numprovinfo++; |
390 | |
|
391 | 0 | ret = 1; |
392 | 0 | err: |
393 | 0 | CRYPTO_THREAD_unlock(store->lock); |
394 | 0 | return ret; |
395 | 0 | } |
396 | | |
397 | | OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name, |
398 | | ossl_unused int noconfig) |
399 | 0 | { |
400 | 0 | struct provider_store_st *store = NULL; |
401 | 0 | OSSL_PROVIDER *prov = NULL; |
402 | |
|
403 | 0 | if ((store = get_provider_store(libctx)) != NULL) { |
404 | 0 | OSSL_PROVIDER tmpl = { 0, }; |
405 | 0 | int i; |
406 | |
|
407 | 0 | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG) |
408 | | /* |
409 | | * Make sure any providers are loaded from config before we try to find |
410 | | * them. |
411 | | */ |
412 | 0 | if (!noconfig) { |
413 | 0 | if (ossl_lib_ctx_is_default(libctx)) |
414 | 0 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); |
415 | 0 | } |
416 | 0 | #endif |
417 | |
|
418 | 0 | tmpl.name = (char *)name; |
419 | 0 | if (!CRYPTO_THREAD_write_lock(store->lock)) |
420 | 0 | return NULL; |
421 | 0 | sk_OSSL_PROVIDER_sort(store->providers); |
422 | 0 | if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) != -1) |
423 | 0 | prov = sk_OSSL_PROVIDER_value(store->providers, i); |
424 | 0 | CRYPTO_THREAD_unlock(store->lock); |
425 | 0 | if (prov != NULL && !ossl_provider_up_ref(prov)) |
426 | 0 | prov = NULL; |
427 | 0 | } |
428 | | |
429 | 0 | return prov; |
430 | 0 | } |
431 | | |
432 | | /*- |
433 | | * Provider Object methods |
434 | | * ======================= |
435 | | */ |
436 | | |
437 | | static OSSL_PROVIDER *provider_new(const char *name, |
438 | | OSSL_provider_init_fn *init_function, |
439 | | STACK_OF(INFOPAIR) *parameters) |
440 | 15 | { |
441 | 15 | OSSL_PROVIDER *prov = NULL; |
442 | | |
443 | 15 | if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL) |
444 | 0 | return NULL; |
445 | 15 | if (!CRYPTO_NEW_REF(&prov->refcnt, 1)) { |
446 | 0 | OPENSSL_free(prov); |
447 | 0 | return NULL; |
448 | 0 | } |
449 | | #ifndef HAVE_ATOMICS |
450 | | if ((prov->activatecnt_lock = CRYPTO_THREAD_lock_new()) == NULL) { |
451 | | ossl_provider_free(prov); |
452 | | ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB); |
453 | | return NULL; |
454 | | } |
455 | | #endif |
456 | | |
457 | 15 | if ((prov->opbits_lock = CRYPTO_THREAD_lock_new()) == NULL |
458 | 15 | || (prov->flag_lock = CRYPTO_THREAD_lock_new()) == NULL |
459 | 15 | || (prov->parameters = sk_INFOPAIR_deep_copy(parameters, |
460 | 15 | infopair_copy, |
461 | 15 | infopair_free)) == NULL) { |
462 | 0 | ossl_provider_free(prov); |
463 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB); |
464 | 0 | return NULL; |
465 | 0 | } |
466 | 15 | if ((prov->name = OPENSSL_strdup(name)) == NULL) { |
467 | 0 | ossl_provider_free(prov); |
468 | 0 | return NULL; |
469 | 0 | } |
470 | | |
471 | 15 | prov->init_function = init_function; |
472 | | |
473 | 15 | return prov; |
474 | 15 | } |
475 | | |
476 | | int ossl_provider_up_ref(OSSL_PROVIDER *prov) |
477 | 2.99k | { |
478 | 2.99k | int ref = 0; |
479 | | |
480 | 2.99k | if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0) |
481 | 0 | return 0; |
482 | | |
483 | 2.99k | #ifndef FIPS_MODULE |
484 | 2.99k | if (prov->ischild) { |
485 | 0 | if (!ossl_provider_up_ref_parent(prov, 0)) { |
486 | 0 | ossl_provider_free(prov); |
487 | 0 | return 0; |
488 | 0 | } |
489 | 0 | } |
490 | 2.99k | #endif |
491 | | |
492 | 2.99k | return ref; |
493 | 2.99k | } |
494 | | |
495 | | #ifndef FIPS_MODULE |
496 | | static int provider_up_ref_intern(OSSL_PROVIDER *prov, int activate) |
497 | 0 | { |
498 | 0 | if (activate) |
499 | 0 | return ossl_provider_activate(prov, 1, 0); |
500 | | |
501 | 0 | return ossl_provider_up_ref(prov); |
502 | 0 | } |
503 | | |
504 | | static int provider_free_intern(OSSL_PROVIDER *prov, int deactivate) |
505 | 0 | { |
506 | 0 | if (deactivate) |
507 | 0 | return ossl_provider_deactivate(prov, 1); |
508 | | |
509 | 0 | ossl_provider_free(prov); |
510 | 0 | return 1; |
511 | 0 | } |
512 | | #endif |
513 | | |
514 | | /* |
515 | | * We assume that the requested provider does not already exist in the store. |
516 | | * The caller should check. If it does exist then adding it to the store later |
517 | | * will fail. |
518 | | */ |
519 | | OSSL_PROVIDER *ossl_provider_new(OSSL_LIB_CTX *libctx, const char *name, |
520 | | OSSL_provider_init_fn *init_function, |
521 | | OSSL_PARAM *params, int noconfig) |
522 | 0 | { |
523 | 0 | struct provider_store_st *store = NULL; |
524 | 0 | OSSL_PROVIDER_INFO template; |
525 | 0 | OSSL_PROVIDER *prov = NULL; |
526 | |
|
527 | 0 | if ((store = get_provider_store(libctx)) == NULL) |
528 | 0 | return NULL; |
529 | | |
530 | 0 | memset(&template, 0, sizeof(template)); |
531 | 0 | if (init_function == NULL) { |
532 | 0 | const OSSL_PROVIDER_INFO *p; |
533 | 0 | size_t i; |
534 | | |
535 | | /* Check if this is a predefined builtin provider */ |
536 | 0 | for (p = ossl_predefined_providers; p->name != NULL; p++) { |
537 | 0 | if (strcmp(p->name, name) == 0) { |
538 | 0 | template = *p; |
539 | 0 | break; |
540 | 0 | } |
541 | 0 | } |
542 | 0 | if (p->name == NULL) { |
543 | | /* Check if this is a user added provider */ |
544 | 0 | if (!CRYPTO_THREAD_read_lock(store->lock)) |
545 | 0 | return NULL; |
546 | 0 | for (i = 0, p = store->provinfo; i < store->numprovinfo; p++, i++) { |
547 | 0 | if (strcmp(p->name, name) == 0) { |
548 | 0 | template = *p; |
549 | 0 | break; |
550 | 0 | } |
551 | 0 | } |
552 | 0 | CRYPTO_THREAD_unlock(store->lock); |
553 | 0 | } |
554 | 0 | } else { |
555 | 0 | template.init = init_function; |
556 | 0 | } |
557 | | |
558 | 0 | if (params != NULL) { |
559 | 0 | int i; |
560 | |
|
561 | 0 | template.parameters = sk_INFOPAIR_new_null(); |
562 | 0 | if (template.parameters == NULL) |
563 | 0 | return NULL; |
564 | | |
565 | 0 | for (i = 0; params[i].key != NULL; i++) { |
566 | 0 | if (params[i].data_type != OSSL_PARAM_UTF8_STRING) |
567 | 0 | continue; |
568 | 0 | if (ossl_provider_info_add_parameter(&template, params[i].key, |
569 | 0 | (char *)params[i].data) <= 0) |
570 | 0 | return NULL; |
571 | 0 | } |
572 | 0 | } |
573 | | |
574 | | /* provider_new() generates an error, so no need here */ |
575 | 0 | prov = provider_new(name, template.init, template.parameters); |
576 | |
|
577 | 0 | if (params != NULL) /* We copied the parameters, let's free them */ |
578 | 0 | sk_INFOPAIR_pop_free(template.parameters, infopair_free); |
579 | |
|
580 | 0 | if (prov == NULL) |
581 | 0 | return NULL; |
582 | | |
583 | 0 | prov->libctx = libctx; |
584 | 0 | #ifndef FIPS_MODULE |
585 | 0 | prov->error_lib = ERR_get_next_error_library(); |
586 | 0 | #endif |
587 | | |
588 | | /* |
589 | | * At this point, the provider is only partially "loaded". To be |
590 | | * fully "loaded", ossl_provider_activate() must also be called and it must |
591 | | * then be added to the provider store. |
592 | | */ |
593 | |
|
594 | 0 | return prov; |
595 | 0 | } |
596 | | |
597 | | /* Assumes that the store lock is held */ |
598 | | static int create_provider_children(OSSL_PROVIDER *prov) |
599 | 0 | { |
600 | 0 | int ret = 1; |
601 | 0 | #ifndef FIPS_MODULE |
602 | 0 | struct provider_store_st *store = prov->store; |
603 | 0 | OSSL_PROVIDER_CHILD_CB *child_cb; |
604 | 0 | int i, max; |
605 | |
|
606 | 0 | max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs); |
607 | 0 | for (i = 0; i < max; i++) { |
608 | | /* |
609 | | * This is newly activated (activatecnt == 1), so we need to |
610 | | * create child providers as necessary. |
611 | | */ |
612 | 0 | child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i); |
613 | 0 | ret &= child_cb->create_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata); |
614 | 0 | } |
615 | 0 | #endif |
616 | |
|
617 | 0 | return ret; |
618 | 0 | } |
619 | | |
620 | | int ossl_provider_add_to_store(OSSL_PROVIDER *prov, OSSL_PROVIDER **actualprov, |
621 | | int retain_fallbacks) |
622 | 0 | { |
623 | 0 | struct provider_store_st *store; |
624 | 0 | int idx; |
625 | 0 | OSSL_PROVIDER tmpl = { 0, }; |
626 | 0 | OSSL_PROVIDER *actualtmp = NULL; |
627 | |
|
628 | 0 | if (actualprov != NULL) |
629 | 0 | *actualprov = NULL; |
630 | |
|
631 | 0 | if ((store = get_provider_store(prov->libctx)) == NULL) |
632 | 0 | return 0; |
633 | | |
634 | 0 | if (!CRYPTO_THREAD_write_lock(store->lock)) |
635 | 0 | return 0; |
636 | | |
637 | 0 | tmpl.name = (char *)prov->name; |
638 | 0 | idx = sk_OSSL_PROVIDER_find(store->providers, &tmpl); |
639 | 0 | if (idx == -1) |
640 | 0 | actualtmp = prov; |
641 | 0 | else |
642 | 0 | actualtmp = sk_OSSL_PROVIDER_value(store->providers, idx); |
643 | |
|
644 | 0 | if (idx == -1) { |
645 | 0 | if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) |
646 | 0 | goto err; |
647 | 0 | prov->store = store; |
648 | 0 | if (!create_provider_children(prov)) { |
649 | 0 | sk_OSSL_PROVIDER_delete_ptr(store->providers, prov); |
650 | 0 | goto err; |
651 | 0 | } |
652 | 0 | if (!retain_fallbacks) |
653 | 0 | store->use_fallbacks = 0; |
654 | 0 | } |
655 | | |
656 | 0 | CRYPTO_THREAD_unlock(store->lock); |
657 | |
|
658 | 0 | if (actualprov != NULL) { |
659 | 0 | if (!ossl_provider_up_ref(actualtmp)) { |
660 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB); |
661 | 0 | actualtmp = NULL; |
662 | 0 | return 0; |
663 | 0 | } |
664 | 0 | *actualprov = actualtmp; |
665 | 0 | } |
666 | | |
667 | 0 | if (idx >= 0) { |
668 | | /* |
669 | | * The provider is already in the store. Probably two threads |
670 | | * independently initialised their own provider objects with the same |
671 | | * name and raced to put them in the store. This thread lost. We |
672 | | * deactivate the one we just created and use the one that already |
673 | | * exists instead. |
674 | | * If we get here then we know we did not create provider children |
675 | | * above, so we inform ossl_provider_deactivate not to attempt to remove |
676 | | * any. |
677 | | */ |
678 | 0 | ossl_provider_deactivate(prov, 0); |
679 | 0 | ossl_provider_free(prov); |
680 | 0 | } |
681 | 0 | #ifndef FIPS_MODULE |
682 | 0 | else { |
683 | | /* |
684 | | * This can be done outside the lock. We tolerate other threads getting |
685 | | * the wrong result briefly when creating OSSL_DECODER_CTXs. |
686 | | */ |
687 | 0 | ossl_decoder_cache_flush(prov->libctx); |
688 | 0 | } |
689 | 0 | #endif |
690 | |
|
691 | 0 | return 1; |
692 | | |
693 | 0 | err: |
694 | 0 | CRYPTO_THREAD_unlock(store->lock); |
695 | 0 | return 0; |
696 | 0 | } |
697 | | |
698 | | void ossl_provider_free(OSSL_PROVIDER *prov) |
699 | 3.01k | { |
700 | 3.01k | if (prov != NULL) { |
701 | 3.01k | int ref = 0; |
702 | | |
703 | 3.01k | CRYPTO_DOWN_REF(&prov->refcnt, &ref); |
704 | | |
705 | | /* |
706 | | * When the refcount drops to zero, we clean up the provider. |
707 | | * Note that this also does teardown, which may seem late, |
708 | | * considering that init happens on first activation. However, |
709 | | * there may be other structures hanging on to the provider after |
710 | | * the last deactivation and may therefore need full access to the |
711 | | * provider's services. Therefore, we deinit late. |
712 | | */ |
713 | 3.01k | if (ref == 0) { |
714 | 15 | if (prov->flag_initialized) { |
715 | 15 | ossl_provider_teardown(prov); |
716 | 15 | #ifndef OPENSSL_NO_ERR |
717 | 15 | # ifndef FIPS_MODULE |
718 | 15 | if (prov->error_strings != NULL) { |
719 | 0 | ERR_unload_strings(prov->error_lib, prov->error_strings); |
720 | 0 | OPENSSL_free(prov->error_strings); |
721 | 0 | prov->error_strings = NULL; |
722 | 0 | } |
723 | 15 | # endif |
724 | 15 | #endif |
725 | 15 | OPENSSL_free(prov->operation_bits); |
726 | 15 | prov->operation_bits = NULL; |
727 | 15 | prov->operation_bits_sz = 0; |
728 | 15 | prov->flag_initialized = 0; |
729 | 15 | } |
730 | | |
731 | 15 | #ifndef FIPS_MODULE |
732 | | /* |
733 | | * We deregister thread handling whether or not the provider was |
734 | | * initialized. If init was attempted but was not successful then |
735 | | * the provider may still have registered a thread handler. |
736 | | */ |
737 | 15 | ossl_init_thread_deregister(prov); |
738 | 15 | DSO_free(prov->module); |
739 | 15 | #endif |
740 | 15 | OPENSSL_free(prov->name); |
741 | 15 | OPENSSL_free(prov->path); |
742 | 15 | sk_INFOPAIR_pop_free(prov->parameters, infopair_free); |
743 | 15 | CRYPTO_THREAD_lock_free(prov->opbits_lock); |
744 | 15 | CRYPTO_THREAD_lock_free(prov->flag_lock); |
745 | | #ifndef HAVE_ATOMICS |
746 | | CRYPTO_THREAD_lock_free(prov->activatecnt_lock); |
747 | | #endif |
748 | 15 | CRYPTO_FREE_REF(&prov->refcnt); |
749 | 15 | OPENSSL_free(prov); |
750 | 15 | } |
751 | 2.99k | #ifndef FIPS_MODULE |
752 | 2.99k | else if (prov->ischild) { |
753 | 0 | ossl_provider_free_parent(prov, 0); |
754 | 0 | } |
755 | 3.01k | #endif |
756 | 3.01k | } |
757 | 3.01k | } |
758 | | |
759 | | /* Setters */ |
760 | | int ossl_provider_set_module_path(OSSL_PROVIDER *prov, const char *module_path) |
761 | 0 | { |
762 | 0 | OPENSSL_free(prov->path); |
763 | 0 | prov->path = NULL; |
764 | 0 | if (module_path == NULL) |
765 | 0 | return 1; |
766 | 0 | if ((prov->path = OPENSSL_strdup(module_path)) != NULL) |
767 | 0 | return 1; |
768 | 0 | return 0; |
769 | 0 | } |
770 | | |
771 | | static int infopair_add(STACK_OF(INFOPAIR) **infopairsk, const char *name, |
772 | | const char *value) |
773 | 0 | { |
774 | 0 | INFOPAIR *pair = NULL; |
775 | |
|
776 | 0 | if ((pair = OPENSSL_zalloc(sizeof(*pair))) == NULL |
777 | 0 | || (pair->name = OPENSSL_strdup(name)) == NULL |
778 | 0 | || (pair->value = OPENSSL_strdup(value)) == NULL) |
779 | 0 | goto err; |
780 | | |
781 | 0 | if ((*infopairsk == NULL |
782 | 0 | && (*infopairsk = sk_INFOPAIR_new_null()) == NULL) |
783 | 0 | || sk_INFOPAIR_push(*infopairsk, pair) <= 0) { |
784 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB); |
785 | 0 | goto err; |
786 | 0 | } |
787 | | |
788 | 0 | return 1; |
789 | | |
790 | 0 | err: |
791 | 0 | if (pair != NULL) { |
792 | 0 | OPENSSL_free(pair->name); |
793 | 0 | OPENSSL_free(pair->value); |
794 | 0 | OPENSSL_free(pair); |
795 | 0 | } |
796 | 0 | return 0; |
797 | 0 | } |
798 | | |
799 | | int ossl_provider_add_parameter(OSSL_PROVIDER *prov, |
800 | | const char *name, const char *value) |
801 | 0 | { |
802 | 0 | return infopair_add(&prov->parameters, name, value); |
803 | 0 | } |
804 | | |
805 | | int ossl_provider_info_add_parameter(OSSL_PROVIDER_INFO *provinfo, |
806 | | const char *name, |
807 | | const char *value) |
808 | 0 | { |
809 | 0 | return infopair_add(&provinfo->parameters, name, value); |
810 | 0 | } |
811 | | |
812 | | /* |
813 | | * Provider activation. |
814 | | * |
815 | | * What "activation" means depends on the provider form; for built in |
816 | | * providers (in the library or the application alike), the provider |
817 | | * can already be considered to be loaded, all that's needed is to |
818 | | * initialize it. However, for dynamically loadable provider modules, |
819 | | * we must first load that module. |
820 | | * |
821 | | * Built in modules are distinguished from dynamically loaded modules |
822 | | * with an already assigned init function. |
823 | | */ |
824 | | static const OSSL_DISPATCH *core_dispatch; /* Define further down */ |
825 | | |
826 | | int OSSL_PROVIDER_set_default_search_path(OSSL_LIB_CTX *libctx, |
827 | | const char *path) |
828 | 0 | { |
829 | 0 | struct provider_store_st *store; |
830 | 0 | char *p = NULL; |
831 | |
|
832 | 0 | if (path != NULL) { |
833 | 0 | p = OPENSSL_strdup(path); |
834 | 0 | if (p == NULL) |
835 | 0 | return 0; |
836 | 0 | } |
837 | 0 | if ((store = get_provider_store(libctx)) != NULL |
838 | 0 | && CRYPTO_THREAD_write_lock(store->default_path_lock)) { |
839 | 0 | OPENSSL_free(store->default_path); |
840 | 0 | store->default_path = p; |
841 | 0 | CRYPTO_THREAD_unlock(store->default_path_lock); |
842 | 0 | return 1; |
843 | 0 | } |
844 | 0 | OPENSSL_free(p); |
845 | 0 | return 0; |
846 | 0 | } |
847 | | |
848 | | const char *OSSL_PROVIDER_get0_default_search_path(OSSL_LIB_CTX *libctx) |
849 | 0 | { |
850 | 0 | struct provider_store_st *store; |
851 | 0 | char *path = NULL; |
852 | |
|
853 | 0 | if ((store = get_provider_store(libctx)) != NULL |
854 | 0 | && CRYPTO_THREAD_read_lock(store->default_path_lock)) { |
855 | 0 | path = store->default_path; |
856 | 0 | CRYPTO_THREAD_unlock(store->default_path_lock); |
857 | 0 | } |
858 | 0 | return path; |
859 | 0 | } |
860 | | |
861 | | /* |
862 | | * Internal version that doesn't affect the store flags, and thereby avoid |
863 | | * locking. Direct callers must remember to set the store flags when |
864 | | * appropriate. |
865 | | */ |
866 | | static int provider_init(OSSL_PROVIDER *prov) |
867 | 15 | { |
868 | 15 | const OSSL_DISPATCH *provider_dispatch = NULL; |
869 | 15 | void *tmp_provctx = NULL; /* safety measure */ |
870 | 15 | #ifndef OPENSSL_NO_ERR |
871 | 15 | # ifndef FIPS_MODULE |
872 | 15 | OSSL_FUNC_provider_get_reason_strings_fn *p_get_reason_strings = NULL; |
873 | 15 | # endif |
874 | 15 | #endif |
875 | 15 | int ok = 0; |
876 | | |
877 | 15 | if (!ossl_assert(!prov->flag_initialized)) { |
878 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
879 | 0 | goto end; |
880 | 0 | } |
881 | | |
882 | | /* |
883 | | * If the init function isn't set, it indicates that this provider is |
884 | | * a loadable module. |
885 | | */ |
886 | 15 | if (prov->init_function == NULL) { |
887 | | #ifdef FIPS_MODULE |
888 | | goto end; |
889 | | #else |
890 | 0 | if (prov->module == NULL) { |
891 | 0 | char *allocated_path = NULL; |
892 | 0 | const char *module_path = NULL; |
893 | 0 | char *merged_path = NULL; |
894 | 0 | const char *load_dir = NULL; |
895 | 0 | char *allocated_load_dir = NULL; |
896 | 0 | struct provider_store_st *store; |
897 | |
|
898 | 0 | if ((prov->module = DSO_new()) == NULL) { |
899 | | /* DSO_new() generates an error already */ |
900 | 0 | goto end; |
901 | 0 | } |
902 | | |
903 | 0 | if ((store = get_provider_store(prov->libctx)) == NULL |
904 | 0 | || !CRYPTO_THREAD_read_lock(store->default_path_lock)) |
905 | 0 | goto end; |
906 | | |
907 | 0 | if (store->default_path != NULL) { |
908 | 0 | allocated_load_dir = OPENSSL_strdup(store->default_path); |
909 | 0 | CRYPTO_THREAD_unlock(store->default_path_lock); |
910 | 0 | if (allocated_load_dir == NULL) |
911 | 0 | goto end; |
912 | 0 | load_dir = allocated_load_dir; |
913 | 0 | } else { |
914 | 0 | CRYPTO_THREAD_unlock(store->default_path_lock); |
915 | 0 | } |
916 | | |
917 | 0 | if (load_dir == NULL) { |
918 | 0 | load_dir = ossl_safe_getenv("OPENSSL_MODULES"); |
919 | 0 | if (load_dir == NULL) |
920 | 0 | load_dir = MODULESDIR; |
921 | 0 | } |
922 | |
|
923 | 0 | DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS, |
924 | 0 | DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL); |
925 | |
|
926 | 0 | module_path = prov->path; |
927 | 0 | if (module_path == NULL) |
928 | 0 | module_path = allocated_path = |
929 | 0 | DSO_convert_filename(prov->module, prov->name); |
930 | 0 | if (module_path != NULL) |
931 | 0 | merged_path = DSO_merge(prov->module, module_path, load_dir); |
932 | |
|
933 | 0 | if (merged_path == NULL |
934 | 0 | || (DSO_load(prov->module, merged_path, NULL, 0)) == NULL) { |
935 | 0 | DSO_free(prov->module); |
936 | 0 | prov->module = NULL; |
937 | 0 | } |
938 | |
|
939 | 0 | OPENSSL_free(merged_path); |
940 | 0 | OPENSSL_free(allocated_path); |
941 | 0 | OPENSSL_free(allocated_load_dir); |
942 | 0 | } |
943 | | |
944 | 0 | if (prov->module == NULL) { |
945 | | /* DSO has already recorded errors, this is just a tracepoint */ |
946 | 0 | ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_DSO_LIB, |
947 | 0 | "name=%s", prov->name); |
948 | 0 | goto end; |
949 | 0 | } |
950 | | |
951 | 0 | prov->init_function = (OSSL_provider_init_fn *) |
952 | 0 | DSO_bind_func(prov->module, "OSSL_provider_init"); |
953 | 0 | #endif |
954 | 0 | } |
955 | | |
956 | | /* Check for and call the initialise function for the provider. */ |
957 | 15 | if (prov->init_function == NULL) { |
958 | 0 | ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED, |
959 | 0 | "name=%s, provider has no provider init function", |
960 | 0 | prov->name); |
961 | 0 | goto end; |
962 | 0 | } |
963 | | |
964 | 15 | if (!prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch, |
965 | 15 | &provider_dispatch, &tmp_provctx)) { |
966 | 0 | ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL, |
967 | 0 | "name=%s", prov->name); |
968 | 0 | goto end; |
969 | 0 | } |
970 | 15 | prov->provctx = tmp_provctx; |
971 | 15 | prov->dispatch = provider_dispatch; |
972 | | |
973 | 90 | for (; provider_dispatch->function_id != 0; provider_dispatch++) { |
974 | 75 | switch (provider_dispatch->function_id) { |
975 | 15 | case OSSL_FUNC_PROVIDER_TEARDOWN: |
976 | 15 | prov->teardown = |
977 | 15 | OSSL_FUNC_provider_teardown(provider_dispatch); |
978 | 15 | break; |
979 | 15 | case OSSL_FUNC_PROVIDER_GETTABLE_PARAMS: |
980 | 15 | prov->gettable_params = |
981 | 15 | OSSL_FUNC_provider_gettable_params(provider_dispatch); |
982 | 15 | break; |
983 | 15 | case OSSL_FUNC_PROVIDER_GET_PARAMS: |
984 | 15 | prov->get_params = |
985 | 15 | OSSL_FUNC_provider_get_params(provider_dispatch); |
986 | 15 | break; |
987 | 0 | case OSSL_FUNC_PROVIDER_SELF_TEST: |
988 | 0 | prov->self_test = |
989 | 0 | OSSL_FUNC_provider_self_test(provider_dispatch); |
990 | 0 | break; |
991 | 15 | case OSSL_FUNC_PROVIDER_GET_CAPABILITIES: |
992 | 15 | prov->get_capabilities = |
993 | 15 | OSSL_FUNC_provider_get_capabilities(provider_dispatch); |
994 | 15 | break; |
995 | 15 | case OSSL_FUNC_PROVIDER_QUERY_OPERATION: |
996 | 15 | prov->query_operation = |
997 | 15 | OSSL_FUNC_provider_query_operation(provider_dispatch); |
998 | 15 | break; |
999 | 0 | case OSSL_FUNC_PROVIDER_UNQUERY_OPERATION: |
1000 | 0 | prov->unquery_operation = |
1001 | 0 | OSSL_FUNC_provider_unquery_operation(provider_dispatch); |
1002 | 0 | break; |
1003 | 0 | #ifndef OPENSSL_NO_ERR |
1004 | 0 | # ifndef FIPS_MODULE |
1005 | 0 | case OSSL_FUNC_PROVIDER_GET_REASON_STRINGS: |
1006 | 0 | p_get_reason_strings = |
1007 | 0 | OSSL_FUNC_provider_get_reason_strings(provider_dispatch); |
1008 | 0 | break; |
1009 | 75 | # endif |
1010 | 75 | #endif |
1011 | 75 | } |
1012 | 75 | } |
1013 | | |
1014 | 15 | #ifndef OPENSSL_NO_ERR |
1015 | 15 | # ifndef FIPS_MODULE |
1016 | 15 | if (p_get_reason_strings != NULL) { |
1017 | 0 | const OSSL_ITEM *reasonstrings = p_get_reason_strings(prov->provctx); |
1018 | 0 | size_t cnt, cnt2; |
1019 | | |
1020 | | /* |
1021 | | * ERR_load_strings() handles ERR_STRING_DATA rather than OSSL_ITEM, |
1022 | | * although they are essentially the same type. |
1023 | | * Furthermore, ERR_load_strings() patches the array's error number |
1024 | | * with the error library number, so we need to make a copy of that |
1025 | | * array either way. |
1026 | | */ |
1027 | 0 | cnt = 0; |
1028 | 0 | while (reasonstrings[cnt].id != 0) { |
1029 | 0 | if (ERR_GET_LIB(reasonstrings[cnt].id) != 0) |
1030 | 0 | goto end; |
1031 | 0 | cnt++; |
1032 | 0 | } |
1033 | 0 | cnt++; /* One for the terminating item */ |
1034 | | |
1035 | | /* Allocate one extra item for the "library" name */ |
1036 | 0 | prov->error_strings = |
1037 | 0 | OPENSSL_zalloc(sizeof(ERR_STRING_DATA) * (cnt + 1)); |
1038 | 0 | if (prov->error_strings == NULL) |
1039 | 0 | goto end; |
1040 | | |
1041 | | /* |
1042 | | * Set the "library" name. |
1043 | | */ |
1044 | 0 | prov->error_strings[0].error = ERR_PACK(prov->error_lib, 0, 0); |
1045 | 0 | prov->error_strings[0].string = prov->name; |
1046 | | /* |
1047 | | * Copy reasonstrings item 0..cnt-1 to prov->error_trings positions |
1048 | | * 1..cnt. |
1049 | | */ |
1050 | 0 | for (cnt2 = 1; cnt2 <= cnt; cnt2++) { |
1051 | 0 | prov->error_strings[cnt2].error = (int)reasonstrings[cnt2-1].id; |
1052 | 0 | prov->error_strings[cnt2].string = reasonstrings[cnt2-1].ptr; |
1053 | 0 | } |
1054 | |
|
1055 | 0 | ERR_load_strings(prov->error_lib, prov->error_strings); |
1056 | 0 | } |
1057 | 15 | # endif |
1058 | 15 | #endif |
1059 | | |
1060 | | /* With this flag set, this provider has become fully "loaded". */ |
1061 | 15 | prov->flag_initialized = 1; |
1062 | 15 | ok = 1; |
1063 | | |
1064 | 15 | end: |
1065 | 15 | return ok; |
1066 | 15 | } |
1067 | | |
1068 | | /* |
1069 | | * Deactivate a provider. If upcalls is 0 then we suppress any upcalls to a |
1070 | | * parent provider. If removechildren is 0 then we suppress any calls to remove |
1071 | | * child providers. |
1072 | | * Return -1 on failure and the activation count on success |
1073 | | */ |
1074 | | static int provider_deactivate(OSSL_PROVIDER *prov, int upcalls, |
1075 | | int removechildren) |
1076 | 15 | { |
1077 | 15 | int count; |
1078 | 15 | struct provider_store_st *store; |
1079 | 15 | #ifndef FIPS_MODULE |
1080 | 15 | int freeparent = 0; |
1081 | 15 | #endif |
1082 | 15 | int lock = 1; |
1083 | | |
1084 | 15 | if (!ossl_assert(prov != NULL)) |
1085 | 0 | return -1; |
1086 | | |
1087 | | /* |
1088 | | * No need to lock if we've got no store because we've not been shared with |
1089 | | * other threads. |
1090 | | */ |
1091 | 15 | store = get_provider_store(prov->libctx); |
1092 | 15 | if (store == NULL) |
1093 | 0 | lock = 0; |
1094 | | |
1095 | 15 | if (lock && !CRYPTO_THREAD_read_lock(store->lock)) |
1096 | 0 | return -1; |
1097 | 15 | if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) { |
1098 | 0 | CRYPTO_THREAD_unlock(store->lock); |
1099 | 0 | return -1; |
1100 | 0 | } |
1101 | | |
1102 | 15 | CRYPTO_atomic_add(&prov->activatecnt, -1, &count, prov->activatecnt_lock); |
1103 | 15 | #ifndef FIPS_MODULE |
1104 | 15 | if (count >= 1 && prov->ischild && upcalls) { |
1105 | | /* |
1106 | | * We have had a direct activation in this child libctx so we need to |
1107 | | * now down the ref count in the parent provider. We do the actual down |
1108 | | * ref outside of the flag_lock, since it could involve getting other |
1109 | | * locks. |
1110 | | */ |
1111 | 0 | freeparent = 1; |
1112 | 0 | } |
1113 | 15 | #endif |
1114 | | |
1115 | 15 | if (count < 1) |
1116 | 15 | prov->flag_activated = 0; |
1117 | 0 | #ifndef FIPS_MODULE |
1118 | 0 | else |
1119 | 0 | removechildren = 0; |
1120 | 15 | #endif |
1121 | | |
1122 | 15 | #ifndef FIPS_MODULE |
1123 | 15 | if (removechildren && store != NULL) { |
1124 | 15 | int i, max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs); |
1125 | 15 | OSSL_PROVIDER_CHILD_CB *child_cb; |
1126 | | |
1127 | 15 | for (i = 0; i < max; i++) { |
1128 | 0 | child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i); |
1129 | 0 | child_cb->remove_cb((OSSL_CORE_HANDLE *)prov, child_cb->cbdata); |
1130 | 0 | } |
1131 | 15 | } |
1132 | 15 | #endif |
1133 | 15 | if (lock) { |
1134 | 15 | CRYPTO_THREAD_unlock(prov->flag_lock); |
1135 | 15 | CRYPTO_THREAD_unlock(store->lock); |
1136 | | /* |
1137 | | * This can be done outside the lock. We tolerate other threads getting |
1138 | | * the wrong result briefly when creating OSSL_DECODER_CTXs. |
1139 | | */ |
1140 | 15 | #ifndef FIPS_MODULE |
1141 | 15 | if (count < 1) |
1142 | 15 | ossl_decoder_cache_flush(prov->libctx); |
1143 | 15 | #endif |
1144 | 15 | } |
1145 | 15 | #ifndef FIPS_MODULE |
1146 | 15 | if (freeparent) |
1147 | 0 | ossl_provider_free_parent(prov, 1); |
1148 | 15 | #endif |
1149 | | |
1150 | | /* We don't deinit here, that's done in ossl_provider_free() */ |
1151 | 15 | return count; |
1152 | 15 | } |
1153 | | |
1154 | | /* |
1155 | | * Activate a provider. |
1156 | | * Return -1 on failure and the activation count on success |
1157 | | */ |
1158 | | static int provider_activate(OSSL_PROVIDER *prov, int lock, int upcalls) |
1159 | 15 | { |
1160 | 15 | int count = -1; |
1161 | 15 | struct provider_store_st *store; |
1162 | 15 | int ret = 1; |
1163 | | |
1164 | 15 | store = prov->store; |
1165 | | /* |
1166 | | * If the provider hasn't been added to the store, then we don't need |
1167 | | * any locks because we've not shared it with other threads. |
1168 | | */ |
1169 | 15 | if (store == NULL) { |
1170 | 15 | lock = 0; |
1171 | 15 | if (!provider_init(prov)) |
1172 | 0 | return -1; |
1173 | 15 | } |
1174 | | |
1175 | 15 | #ifndef FIPS_MODULE |
1176 | 15 | if (prov->ischild && upcalls && !ossl_provider_up_ref_parent(prov, 1)) |
1177 | 0 | return -1; |
1178 | 15 | #endif |
1179 | | |
1180 | 15 | if (lock && !CRYPTO_THREAD_read_lock(store->lock)) { |
1181 | 0 | #ifndef FIPS_MODULE |
1182 | 0 | if (prov->ischild && upcalls) |
1183 | 0 | ossl_provider_free_parent(prov, 1); |
1184 | 0 | #endif |
1185 | 0 | return -1; |
1186 | 0 | } |
1187 | | |
1188 | 15 | if (lock && !CRYPTO_THREAD_write_lock(prov->flag_lock)) { |
1189 | 0 | CRYPTO_THREAD_unlock(store->lock); |
1190 | 0 | #ifndef FIPS_MODULE |
1191 | 0 | if (prov->ischild && upcalls) |
1192 | 0 | ossl_provider_free_parent(prov, 1); |
1193 | 0 | #endif |
1194 | 0 | return -1; |
1195 | 0 | } |
1196 | 15 | if (CRYPTO_atomic_add(&prov->activatecnt, 1, &count, prov->activatecnt_lock)) { |
1197 | 15 | prov->flag_activated = 1; |
1198 | | |
1199 | 15 | if (count == 1 && store != NULL) { |
1200 | 0 | ret = create_provider_children(prov); |
1201 | 0 | } |
1202 | 15 | } |
1203 | 15 | if (lock) { |
1204 | 0 | CRYPTO_THREAD_unlock(prov->flag_lock); |
1205 | 0 | CRYPTO_THREAD_unlock(store->lock); |
1206 | | /* |
1207 | | * This can be done outside the lock. We tolerate other threads getting |
1208 | | * the wrong result briefly when creating OSSL_DECODER_CTXs. |
1209 | | */ |
1210 | 0 | #ifndef FIPS_MODULE |
1211 | 0 | if (count == 1) |
1212 | 0 | ossl_decoder_cache_flush(prov->libctx); |
1213 | 0 | #endif |
1214 | 0 | } |
1215 | | |
1216 | 15 | if (!ret) |
1217 | 0 | return -1; |
1218 | | |
1219 | 15 | return count; |
1220 | 15 | } |
1221 | | |
1222 | | static int provider_flush_store_cache(const OSSL_PROVIDER *prov) |
1223 | 0 | { |
1224 | 0 | struct provider_store_st *store; |
1225 | 0 | int freeing; |
1226 | |
|
1227 | 0 | if ((store = get_provider_store(prov->libctx)) == NULL) |
1228 | 0 | return 0; |
1229 | | |
1230 | 0 | if (!CRYPTO_THREAD_read_lock(store->lock)) |
1231 | 0 | return 0; |
1232 | 0 | freeing = store->freeing; |
1233 | 0 | CRYPTO_THREAD_unlock(store->lock); |
1234 | |
|
1235 | 0 | if (!freeing) { |
1236 | 0 | int acc |
1237 | 0 | = evp_method_store_cache_flush(prov->libctx) |
1238 | 0 | #ifndef FIPS_MODULE |
1239 | 0 | + ossl_encoder_store_cache_flush(prov->libctx) |
1240 | 0 | + ossl_decoder_store_cache_flush(prov->libctx) |
1241 | 0 | + ossl_store_loader_store_cache_flush(prov->libctx) |
1242 | 0 | #endif |
1243 | 0 | ; |
1244 | |
|
1245 | 0 | #ifndef FIPS_MODULE |
1246 | 0 | return acc == 4; |
1247 | | #else |
1248 | | return acc == 1; |
1249 | | #endif |
1250 | 0 | } |
1251 | 0 | return 1; |
1252 | 0 | } |
1253 | | |
1254 | | static int provider_remove_store_methods(OSSL_PROVIDER *prov) |
1255 | 15 | { |
1256 | 15 | struct provider_store_st *store; |
1257 | 15 | int freeing; |
1258 | | |
1259 | 15 | if ((store = get_provider_store(prov->libctx)) == NULL) |
1260 | 0 | return 0; |
1261 | | |
1262 | 15 | if (!CRYPTO_THREAD_read_lock(store->lock)) |
1263 | 0 | return 0; |
1264 | 15 | freeing = store->freeing; |
1265 | 15 | CRYPTO_THREAD_unlock(store->lock); |
1266 | | |
1267 | 15 | if (!freeing) { |
1268 | 0 | int acc; |
1269 | |
|
1270 | 0 | if (!CRYPTO_THREAD_write_lock(prov->opbits_lock)) |
1271 | 0 | return 0; |
1272 | 0 | OPENSSL_free(prov->operation_bits); |
1273 | 0 | prov->operation_bits = NULL; |
1274 | 0 | prov->operation_bits_sz = 0; |
1275 | 0 | CRYPTO_THREAD_unlock(prov->opbits_lock); |
1276 | |
|
1277 | 0 | acc = evp_method_store_remove_all_provided(prov) |
1278 | 0 | #ifndef FIPS_MODULE |
1279 | 0 | + ossl_encoder_store_remove_all_provided(prov) |
1280 | 0 | + ossl_decoder_store_remove_all_provided(prov) |
1281 | 0 | + ossl_store_loader_store_remove_all_provided(prov) |
1282 | 0 | #endif |
1283 | 0 | ; |
1284 | |
|
1285 | 0 | #ifndef FIPS_MODULE |
1286 | 0 | return acc == 4; |
1287 | | #else |
1288 | | return acc == 1; |
1289 | | #endif |
1290 | 0 | } |
1291 | 15 | return 1; |
1292 | 15 | } |
1293 | | |
1294 | | int ossl_provider_activate(OSSL_PROVIDER *prov, int upcalls, int aschild) |
1295 | 0 | { |
1296 | 0 | int count; |
1297 | |
|
1298 | 0 | if (prov == NULL) |
1299 | 0 | return 0; |
1300 | 0 | #ifndef FIPS_MODULE |
1301 | | /* |
1302 | | * If aschild is true, then we only actually do the activation if the |
1303 | | * provider is a child. If its not, this is still success. |
1304 | | */ |
1305 | 0 | if (aschild && !prov->ischild) |
1306 | 0 | return 1; |
1307 | 0 | #endif |
1308 | 0 | if ((count = provider_activate(prov, 1, upcalls)) > 0) |
1309 | 0 | return count == 1 ? provider_flush_store_cache(prov) : 1; |
1310 | | |
1311 | 0 | return 0; |
1312 | 0 | } |
1313 | | |
1314 | | int ossl_provider_deactivate(OSSL_PROVIDER *prov, int removechildren) |
1315 | 15 | { |
1316 | 15 | int count; |
1317 | | |
1318 | 15 | if (prov == NULL |
1319 | 15 | || (count = provider_deactivate(prov, 1, removechildren)) < 0) |
1320 | 0 | return 0; |
1321 | 15 | return count == 0 ? provider_remove_store_methods(prov) : 1; |
1322 | 15 | } |
1323 | | |
1324 | | void *ossl_provider_ctx(const OSSL_PROVIDER *prov) |
1325 | 405k | { |
1326 | 405k | return prov != NULL ? prov->provctx : NULL; |
1327 | 405k | } |
1328 | | |
1329 | | /* |
1330 | | * This function only does something once when store->use_fallbacks == 1, |
1331 | | * and then sets store->use_fallbacks = 0, so the second call and so on is |
1332 | | * effectively a no-op. |
1333 | | */ |
1334 | | static int provider_activate_fallbacks(struct provider_store_st *store) |
1335 | 24.9k | { |
1336 | 24.9k | int use_fallbacks; |
1337 | 24.9k | int activated_fallback_count = 0; |
1338 | 24.9k | int ret = 0; |
1339 | 24.9k | const OSSL_PROVIDER_INFO *p; |
1340 | | |
1341 | 24.9k | if (!CRYPTO_THREAD_read_lock(store->lock)) |
1342 | 0 | return 0; |
1343 | 24.9k | use_fallbacks = store->use_fallbacks; |
1344 | 24.9k | CRYPTO_THREAD_unlock(store->lock); |
1345 | 24.9k | if (!use_fallbacks) |
1346 | 24.9k | return 1; |
1347 | | |
1348 | 15 | if (!CRYPTO_THREAD_write_lock(store->lock)) |
1349 | 0 | return 0; |
1350 | | /* Check again, just in case another thread changed it */ |
1351 | 15 | use_fallbacks = store->use_fallbacks; |
1352 | 15 | if (!use_fallbacks) { |
1353 | 0 | CRYPTO_THREAD_unlock(store->lock); |
1354 | 0 | return 1; |
1355 | 0 | } |
1356 | | |
1357 | 60 | for (p = ossl_predefined_providers; p->name != NULL; p++) { |
1358 | 45 | OSSL_PROVIDER *prov = NULL; |
1359 | | |
1360 | 45 | if (!p->is_fallback) |
1361 | 30 | continue; |
1362 | | /* |
1363 | | * We use the internal constructor directly here, |
1364 | | * otherwise we get a call loop |
1365 | | */ |
1366 | 15 | prov = provider_new(p->name, p->init, NULL); |
1367 | 15 | if (prov == NULL) |
1368 | 0 | goto err; |
1369 | 15 | prov->libctx = store->libctx; |
1370 | 15 | #ifndef FIPS_MODULE |
1371 | 15 | prov->error_lib = ERR_get_next_error_library(); |
1372 | 15 | #endif |
1373 | | |
1374 | | /* |
1375 | | * We are calling provider_activate while holding the store lock. This |
1376 | | * means the init function will be called while holding a lock. Normally |
1377 | | * we try to avoid calling a user callback while holding a lock. |
1378 | | * However, fallbacks are never third party providers so we accept this. |
1379 | | */ |
1380 | 15 | if (provider_activate(prov, 0, 0) < 0) { |
1381 | 0 | ossl_provider_free(prov); |
1382 | 0 | goto err; |
1383 | 0 | } |
1384 | 15 | prov->store = store; |
1385 | 15 | if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) { |
1386 | 0 | ossl_provider_free(prov); |
1387 | 0 | goto err; |
1388 | 0 | } |
1389 | 15 | activated_fallback_count++; |
1390 | 15 | } |
1391 | | |
1392 | 15 | if (activated_fallback_count > 0) { |
1393 | 15 | store->use_fallbacks = 0; |
1394 | 15 | ret = 1; |
1395 | 15 | } |
1396 | 15 | err: |
1397 | 15 | CRYPTO_THREAD_unlock(store->lock); |
1398 | 15 | return ret; |
1399 | 15 | } |
1400 | | |
1401 | | int ossl_provider_doall_activated(OSSL_LIB_CTX *ctx, |
1402 | | int (*cb)(OSSL_PROVIDER *provider, |
1403 | | void *cbdata), |
1404 | | void *cbdata) |
1405 | 24.9k | { |
1406 | 24.9k | int ret = 0, curr, max, ref = 0; |
1407 | 24.9k | struct provider_store_st *store = get_provider_store(ctx); |
1408 | 24.9k | STACK_OF(OSSL_PROVIDER) *provs = NULL; |
1409 | | |
1410 | 24.9k | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG) |
1411 | | /* |
1412 | | * Make sure any providers are loaded from config before we try to use |
1413 | | * them. |
1414 | | */ |
1415 | 24.9k | if (ossl_lib_ctx_is_default(ctx)) |
1416 | 24.9k | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL); |
1417 | 24.9k | #endif |
1418 | | |
1419 | 24.9k | if (store == NULL) |
1420 | 0 | return 1; |
1421 | 24.9k | if (!provider_activate_fallbacks(store)) |
1422 | 0 | return 0; |
1423 | | |
1424 | | /* |
1425 | | * Under lock, grab a copy of the provider list and up_ref each |
1426 | | * provider so that they don't disappear underneath us. |
1427 | | */ |
1428 | 24.9k | if (!CRYPTO_THREAD_read_lock(store->lock)) |
1429 | 0 | return 0; |
1430 | 24.9k | provs = sk_OSSL_PROVIDER_dup(store->providers); |
1431 | 24.9k | if (provs == NULL) { |
1432 | 0 | CRYPTO_THREAD_unlock(store->lock); |
1433 | 0 | return 0; |
1434 | 0 | } |
1435 | 24.9k | max = sk_OSSL_PROVIDER_num(provs); |
1436 | | /* |
1437 | | * We work backwards through the stack so that we can safely delete items |
1438 | | * as we go. |
1439 | | */ |
1440 | 49.9k | for (curr = max - 1; curr >= 0; curr--) { |
1441 | 24.9k | OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr); |
1442 | | |
1443 | 24.9k | if (!CRYPTO_THREAD_read_lock(prov->flag_lock)) |
1444 | 0 | goto err_unlock; |
1445 | 24.9k | if (prov->flag_activated) { |
1446 | | /* |
1447 | | * We call CRYPTO_UP_REF directly rather than ossl_provider_up_ref |
1448 | | * to avoid upping the ref count on the parent provider, which we |
1449 | | * must not do while holding locks. |
1450 | | */ |
1451 | 24.9k | if (CRYPTO_UP_REF(&prov->refcnt, &ref) <= 0) { |
1452 | 0 | CRYPTO_THREAD_unlock(prov->flag_lock); |
1453 | 0 | goto err_unlock; |
1454 | 0 | } |
1455 | | /* |
1456 | | * It's already activated, but we up the activated count to ensure |
1457 | | * it remains activated until after we've called the user callback. |
1458 | | * In theory this could mean the parent provider goes inactive, |
1459 | | * whilst still activated in the child for a short period. That's ok. |
1460 | | */ |
1461 | 24.9k | if (!CRYPTO_atomic_add(&prov->activatecnt, 1, &ref, |
1462 | 24.9k | prov->activatecnt_lock)) { |
1463 | 0 | CRYPTO_DOWN_REF(&prov->refcnt, &ref); |
1464 | 0 | CRYPTO_THREAD_unlock(prov->flag_lock); |
1465 | 0 | goto err_unlock; |
1466 | 0 | } |
1467 | 24.9k | } else { |
1468 | 0 | sk_OSSL_PROVIDER_delete(provs, curr); |
1469 | 0 | max--; |
1470 | 0 | } |
1471 | 24.9k | CRYPTO_THREAD_unlock(prov->flag_lock); |
1472 | 24.9k | } |
1473 | 24.9k | CRYPTO_THREAD_unlock(store->lock); |
1474 | | |
1475 | | /* |
1476 | | * Now, we sweep through all providers not under lock |
1477 | | */ |
1478 | 49.9k | for (curr = 0; curr < max; curr++) { |
1479 | 24.9k | OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr); |
1480 | | |
1481 | 24.9k | if (!cb(prov, cbdata)) { |
1482 | 0 | curr = -1; |
1483 | 0 | goto finish; |
1484 | 0 | } |
1485 | 24.9k | } |
1486 | 24.9k | curr = -1; |
1487 | | |
1488 | 24.9k | ret = 1; |
1489 | 24.9k | goto finish; |
1490 | | |
1491 | 0 | err_unlock: |
1492 | 0 | CRYPTO_THREAD_unlock(store->lock); |
1493 | 24.9k | finish: |
1494 | | /* |
1495 | | * The pop_free call doesn't do what we want on an error condition. We |
1496 | | * either start from the first item in the stack, or part way through if |
1497 | | * we only processed some of the items. |
1498 | | */ |
1499 | 49.9k | for (curr++; curr < max; curr++) { |
1500 | 24.9k | OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(provs, curr); |
1501 | | |
1502 | 24.9k | if (!CRYPTO_atomic_add(&prov->activatecnt, -1, &ref, |
1503 | 24.9k | prov->activatecnt_lock)) { |
1504 | 0 | ret = 0; |
1505 | 0 | continue; |
1506 | 0 | } |
1507 | 24.9k | if (ref < 1) { |
1508 | | /* |
1509 | | * Looks like we need to deactivate properly. We could just have |
1510 | | * done this originally, but it involves taking a write lock so |
1511 | | * we avoid it. We up the count again and do a full deactivation |
1512 | | */ |
1513 | 0 | if (CRYPTO_atomic_add(&prov->activatecnt, 1, &ref, |
1514 | 0 | prov->activatecnt_lock)) |
1515 | 0 | provider_deactivate(prov, 0, 1); |
1516 | 0 | else |
1517 | 0 | ret = 0; |
1518 | 0 | } |
1519 | | /* |
1520 | | * As above where we did the up-ref, we don't call ossl_provider_free |
1521 | | * to avoid making upcalls. There should always be at least one ref |
1522 | | * to the provider in the store, so this should never drop to 0. |
1523 | | */ |
1524 | 24.9k | if (!CRYPTO_DOWN_REF(&prov->refcnt, &ref)) { |
1525 | 0 | ret = 0; |
1526 | 0 | continue; |
1527 | 0 | } |
1528 | | /* |
1529 | | * Not much we can do if this assert ever fails. So we don't use |
1530 | | * ossl_assert here. |
1531 | | */ |
1532 | 24.9k | assert(ref > 0); |
1533 | 24.9k | } |
1534 | 24.9k | sk_OSSL_PROVIDER_free(provs); |
1535 | 24.9k | return ret; |
1536 | 24.9k | } |
1537 | | |
1538 | | int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name) |
1539 | 0 | { |
1540 | 0 | OSSL_PROVIDER *prov = NULL; |
1541 | 0 | int available = 0; |
1542 | 0 | struct provider_store_st *store = get_provider_store(libctx); |
1543 | |
|
1544 | 0 | if (store == NULL || !provider_activate_fallbacks(store)) |
1545 | 0 | return 0; |
1546 | | |
1547 | 0 | prov = ossl_provider_find(libctx, name, 0); |
1548 | 0 | if (prov != NULL) { |
1549 | 0 | if (!CRYPTO_THREAD_read_lock(prov->flag_lock)) |
1550 | 0 | return 0; |
1551 | 0 | available = prov->flag_activated; |
1552 | 0 | CRYPTO_THREAD_unlock(prov->flag_lock); |
1553 | 0 | ossl_provider_free(prov); |
1554 | 0 | } |
1555 | 0 | return available; |
1556 | 0 | } |
1557 | | |
1558 | | /* Getters of Provider Object data */ |
1559 | | const char *ossl_provider_name(const OSSL_PROVIDER *prov) |
1560 | 0 | { |
1561 | 0 | return prov->name; |
1562 | 0 | } |
1563 | | |
1564 | | const DSO *ossl_provider_dso(const OSSL_PROVIDER *prov) |
1565 | 0 | { |
1566 | 0 | return prov->module; |
1567 | 0 | } |
1568 | | |
1569 | | const char *ossl_provider_module_name(const OSSL_PROVIDER *prov) |
1570 | 0 | { |
1571 | | #ifdef FIPS_MODULE |
1572 | | return NULL; |
1573 | | #else |
1574 | 0 | return DSO_get_filename(prov->module); |
1575 | 0 | #endif |
1576 | 0 | } |
1577 | | |
1578 | | const char *ossl_provider_module_path(const OSSL_PROVIDER *prov) |
1579 | 0 | { |
1580 | | #ifdef FIPS_MODULE |
1581 | | return NULL; |
1582 | | #else |
1583 | | /* FIXME: Ensure it's a full path */ |
1584 | 0 | return DSO_get_filename(prov->module); |
1585 | 0 | #endif |
1586 | 0 | } |
1587 | | |
1588 | | void *ossl_provider_prov_ctx(const OSSL_PROVIDER *prov) |
1589 | 2.35M | { |
1590 | 2.35M | if (prov != NULL) |
1591 | 2.35M | return prov->provctx; |
1592 | | |
1593 | 0 | return NULL; |
1594 | 2.35M | } |
1595 | | |
1596 | | const OSSL_DISPATCH *ossl_provider_get0_dispatch(const OSSL_PROVIDER *prov) |
1597 | 0 | { |
1598 | 0 | if (prov != NULL) |
1599 | 0 | return prov->dispatch; |
1600 | | |
1601 | 0 | return NULL; |
1602 | 0 | } |
1603 | | |
1604 | | OSSL_LIB_CTX *ossl_provider_libctx(const OSSL_PROVIDER *prov) |
1605 | 3.74M | { |
1606 | 3.74M | return prov != NULL ? prov->libctx : NULL; |
1607 | 3.74M | } |
1608 | | |
1609 | | /* Wrappers around calls to the provider */ |
1610 | | void ossl_provider_teardown(const OSSL_PROVIDER *prov) |
1611 | 15 | { |
1612 | 15 | if (prov->teardown != NULL |
1613 | 15 | #ifndef FIPS_MODULE |
1614 | 15 | && !prov->ischild |
1615 | 15 | #endif |
1616 | 15 | ) |
1617 | 15 | prov->teardown(prov->provctx); |
1618 | 15 | } |
1619 | | |
1620 | | const OSSL_PARAM *ossl_provider_gettable_params(const OSSL_PROVIDER *prov) |
1621 | 0 | { |
1622 | 0 | return prov->gettable_params == NULL |
1623 | 0 | ? NULL : prov->gettable_params(prov->provctx); |
1624 | 0 | } |
1625 | | |
1626 | | int ossl_provider_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[]) |
1627 | 0 | { |
1628 | 0 | return prov->get_params == NULL |
1629 | 0 | ? 0 : prov->get_params(prov->provctx, params); |
1630 | 0 | } |
1631 | | |
1632 | | int ossl_provider_self_test(const OSSL_PROVIDER *prov) |
1633 | 0 | { |
1634 | 0 | int ret; |
1635 | |
|
1636 | 0 | if (prov->self_test == NULL) |
1637 | 0 | return 1; |
1638 | 0 | ret = prov->self_test(prov->provctx); |
1639 | 0 | if (ret == 0) |
1640 | 0 | (void)provider_remove_store_methods((OSSL_PROVIDER *)prov); |
1641 | 0 | return ret; |
1642 | 0 | } |
1643 | | |
1644 | | int ossl_provider_get_capabilities(const OSSL_PROVIDER *prov, |
1645 | | const char *capability, |
1646 | | OSSL_CALLBACK *cb, |
1647 | | void *arg) |
1648 | 2.73k | { |
1649 | 2.73k | return prov->get_capabilities == NULL |
1650 | 2.73k | ? 1 : prov->get_capabilities(prov->provctx, capability, cb, arg); |
1651 | 2.73k | } |
1652 | | |
1653 | | const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov, |
1654 | | int operation_id, |
1655 | | int *no_cache) |
1656 | 22.2k | { |
1657 | 22.2k | const OSSL_ALGORITHM *res; |
1658 | | |
1659 | 22.2k | if (prov->query_operation == NULL) |
1660 | 0 | return NULL; |
1661 | 22.2k | res = prov->query_operation(prov->provctx, operation_id, no_cache); |
1662 | | #if defined(OPENSSL_NO_CACHED_FETCH) |
1663 | | /* Forcing the non-caching of queries */ |
1664 | | if (no_cache != NULL) |
1665 | | *no_cache = 1; |
1666 | | #endif |
1667 | 22.2k | return res; |
1668 | 22.2k | } |
1669 | | |
1670 | | void ossl_provider_unquery_operation(const OSSL_PROVIDER *prov, |
1671 | | int operation_id, |
1672 | | const OSSL_ALGORITHM *algs) |
1673 | 22.2k | { |
1674 | 22.2k | if (prov->unquery_operation != NULL) |
1675 | 0 | prov->unquery_operation(prov->provctx, operation_id, algs); |
1676 | 22.2k | } |
1677 | | |
1678 | | int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum) |
1679 | 77 | { |
1680 | 77 | size_t byte = bitnum / 8; |
1681 | 77 | unsigned char bit = (1 << (bitnum % 8)) & 0xFF; |
1682 | | |
1683 | 77 | if (!CRYPTO_THREAD_write_lock(provider->opbits_lock)) |
1684 | 0 | return 0; |
1685 | 77 | if (provider->operation_bits_sz <= byte) { |
1686 | 33 | unsigned char *tmp = OPENSSL_realloc(provider->operation_bits, |
1687 | 33 | byte + 1); |
1688 | | |
1689 | 33 | if (tmp == NULL) { |
1690 | 0 | CRYPTO_THREAD_unlock(provider->opbits_lock); |
1691 | 0 | return 0; |
1692 | 0 | } |
1693 | 33 | provider->operation_bits = tmp; |
1694 | 33 | memset(provider->operation_bits + provider->operation_bits_sz, |
1695 | 33 | '\0', byte + 1 - provider->operation_bits_sz); |
1696 | 33 | provider->operation_bits_sz = byte + 1; |
1697 | 33 | } |
1698 | 77 | provider->operation_bits[byte] |= bit; |
1699 | 77 | CRYPTO_THREAD_unlock(provider->opbits_lock); |
1700 | 77 | return 1; |
1701 | 77 | } |
1702 | | |
1703 | | int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum, |
1704 | | int *result) |
1705 | 22.2k | { |
1706 | 22.2k | size_t byte = bitnum / 8; |
1707 | 22.2k | unsigned char bit = (1 << (bitnum % 8)) & 0xFF; |
1708 | | |
1709 | 22.2k | if (!ossl_assert(result != NULL)) { |
1710 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); |
1711 | 0 | return 0; |
1712 | 0 | } |
1713 | | |
1714 | 22.2k | *result = 0; |
1715 | 22.2k | if (!CRYPTO_THREAD_read_lock(provider->opbits_lock)) |
1716 | 0 | return 0; |
1717 | 22.2k | if (provider->operation_bits_sz > byte) |
1718 | 22.2k | *result = ((provider->operation_bits[byte] & bit) != 0); |
1719 | 22.2k | CRYPTO_THREAD_unlock(provider->opbits_lock); |
1720 | 22.2k | return 1; |
1721 | 22.2k | } |
1722 | | |
1723 | | #ifndef FIPS_MODULE |
1724 | | const OSSL_CORE_HANDLE *ossl_provider_get_parent(OSSL_PROVIDER *prov) |
1725 | 0 | { |
1726 | 0 | return prov->handle; |
1727 | 0 | } |
1728 | | |
1729 | | int ossl_provider_is_child(const OSSL_PROVIDER *prov) |
1730 | 0 | { |
1731 | 0 | return prov->ischild; |
1732 | 0 | } |
1733 | | |
1734 | | int ossl_provider_set_child(OSSL_PROVIDER *prov, const OSSL_CORE_HANDLE *handle) |
1735 | 0 | { |
1736 | 0 | prov->handle = handle; |
1737 | 0 | prov->ischild = 1; |
1738 | |
|
1739 | 0 | return 1; |
1740 | 0 | } |
1741 | | |
1742 | | int ossl_provider_default_props_update(OSSL_LIB_CTX *libctx, const char *props) |
1743 | 0 | { |
1744 | 0 | #ifndef FIPS_MODULE |
1745 | 0 | struct provider_store_st *store = NULL; |
1746 | 0 | int i, max; |
1747 | 0 | OSSL_PROVIDER_CHILD_CB *child_cb; |
1748 | |
|
1749 | 0 | if ((store = get_provider_store(libctx)) == NULL) |
1750 | 0 | return 0; |
1751 | | |
1752 | 0 | if (!CRYPTO_THREAD_read_lock(store->lock)) |
1753 | 0 | return 0; |
1754 | | |
1755 | 0 | max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs); |
1756 | 0 | for (i = 0; i < max; i++) { |
1757 | 0 | child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i); |
1758 | 0 | child_cb->global_props_cb(props, child_cb->cbdata); |
1759 | 0 | } |
1760 | |
|
1761 | 0 | CRYPTO_THREAD_unlock(store->lock); |
1762 | 0 | #endif |
1763 | 0 | return 1; |
1764 | 0 | } |
1765 | | |
1766 | | static int ossl_provider_register_child_cb(const OSSL_CORE_HANDLE *handle, |
1767 | | int (*create_cb)( |
1768 | | const OSSL_CORE_HANDLE *provider, |
1769 | | void *cbdata), |
1770 | | int (*remove_cb)( |
1771 | | const OSSL_CORE_HANDLE *provider, |
1772 | | void *cbdata), |
1773 | | int (*global_props_cb)( |
1774 | | const char *props, |
1775 | | void *cbdata), |
1776 | | void *cbdata) |
1777 | 0 | { |
1778 | | /* |
1779 | | * This is really an OSSL_PROVIDER that we created and cast to |
1780 | | * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back. |
1781 | | */ |
1782 | 0 | OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle; |
1783 | 0 | OSSL_PROVIDER *prov; |
1784 | 0 | OSSL_LIB_CTX *libctx = thisprov->libctx; |
1785 | 0 | struct provider_store_st *store = NULL; |
1786 | 0 | int ret = 0, i, max; |
1787 | 0 | OSSL_PROVIDER_CHILD_CB *child_cb; |
1788 | 0 | char *propsstr = NULL; |
1789 | |
|
1790 | 0 | if ((store = get_provider_store(libctx)) == NULL) |
1791 | 0 | return 0; |
1792 | | |
1793 | 0 | child_cb = OPENSSL_malloc(sizeof(*child_cb)); |
1794 | 0 | if (child_cb == NULL) |
1795 | 0 | return 0; |
1796 | 0 | child_cb->prov = thisprov; |
1797 | 0 | child_cb->create_cb = create_cb; |
1798 | 0 | child_cb->remove_cb = remove_cb; |
1799 | 0 | child_cb->global_props_cb = global_props_cb; |
1800 | 0 | child_cb->cbdata = cbdata; |
1801 | |
|
1802 | 0 | if (!CRYPTO_THREAD_write_lock(store->lock)) { |
1803 | 0 | OPENSSL_free(child_cb); |
1804 | 0 | return 0; |
1805 | 0 | } |
1806 | 0 | propsstr = evp_get_global_properties_str(libctx, 0); |
1807 | |
|
1808 | 0 | if (propsstr != NULL) { |
1809 | 0 | global_props_cb(propsstr, cbdata); |
1810 | 0 | OPENSSL_free(propsstr); |
1811 | 0 | } |
1812 | 0 | max = sk_OSSL_PROVIDER_num(store->providers); |
1813 | 0 | for (i = 0; i < max; i++) { |
1814 | 0 | int activated; |
1815 | |
|
1816 | 0 | prov = sk_OSSL_PROVIDER_value(store->providers, i); |
1817 | |
|
1818 | 0 | if (!CRYPTO_THREAD_read_lock(prov->flag_lock)) |
1819 | 0 | break; |
1820 | 0 | activated = prov->flag_activated; |
1821 | 0 | CRYPTO_THREAD_unlock(prov->flag_lock); |
1822 | | /* |
1823 | | * We hold the store lock while calling the user callback. This means |
1824 | | * that the user callback must be short and simple and not do anything |
1825 | | * likely to cause a deadlock. We don't hold the flag_lock during this |
1826 | | * call. In theory this means that another thread could deactivate it |
1827 | | * while we are calling create. This is ok because the other thread |
1828 | | * will also call remove_cb, but won't be able to do so until we release |
1829 | | * the store lock. |
1830 | | */ |
1831 | 0 | if (activated && !create_cb((OSSL_CORE_HANDLE *)prov, cbdata)) |
1832 | 0 | break; |
1833 | 0 | } |
1834 | 0 | if (i == max) { |
1835 | | /* Success */ |
1836 | 0 | ret = sk_OSSL_PROVIDER_CHILD_CB_push(store->child_cbs, child_cb); |
1837 | 0 | } |
1838 | 0 | if (i != max || ret <= 0) { |
1839 | | /* Failed during creation. Remove everything we just added */ |
1840 | 0 | for (; i >= 0; i--) { |
1841 | 0 | prov = sk_OSSL_PROVIDER_value(store->providers, i); |
1842 | 0 | remove_cb((OSSL_CORE_HANDLE *)prov, cbdata); |
1843 | 0 | } |
1844 | 0 | OPENSSL_free(child_cb); |
1845 | 0 | ret = 0; |
1846 | 0 | } |
1847 | 0 | CRYPTO_THREAD_unlock(store->lock); |
1848 | |
|
1849 | 0 | return ret; |
1850 | 0 | } |
1851 | | |
1852 | | static void ossl_provider_deregister_child_cb(const OSSL_CORE_HANDLE *handle) |
1853 | 0 | { |
1854 | | /* |
1855 | | * This is really an OSSL_PROVIDER that we created and cast to |
1856 | | * OSSL_CORE_HANDLE originally. Therefore it is safe to cast it back. |
1857 | | */ |
1858 | 0 | OSSL_PROVIDER *thisprov = (OSSL_PROVIDER *)handle; |
1859 | 0 | OSSL_LIB_CTX *libctx = thisprov->libctx; |
1860 | 0 | struct provider_store_st *store = NULL; |
1861 | 0 | int i, max; |
1862 | 0 | OSSL_PROVIDER_CHILD_CB *child_cb; |
1863 | |
|
1864 | 0 | if ((store = get_provider_store(libctx)) == NULL) |
1865 | 0 | return; |
1866 | | |
1867 | 0 | if (!CRYPTO_THREAD_write_lock(store->lock)) |
1868 | 0 | return; |
1869 | 0 | max = sk_OSSL_PROVIDER_CHILD_CB_num(store->child_cbs); |
1870 | 0 | for (i = 0; i < max; i++) { |
1871 | 0 | child_cb = sk_OSSL_PROVIDER_CHILD_CB_value(store->child_cbs, i); |
1872 | 0 | if (child_cb->prov == thisprov) { |
1873 | | /* Found an entry */ |
1874 | 0 | sk_OSSL_PROVIDER_CHILD_CB_delete(store->child_cbs, i); |
1875 | 0 | OPENSSL_free(child_cb); |
1876 | 0 | break; |
1877 | 0 | } |
1878 | 0 | } |
1879 | 0 | CRYPTO_THREAD_unlock(store->lock); |
1880 | 0 | } |
1881 | | #endif |
1882 | | |
1883 | | /*- |
1884 | | * Core functions for the provider |
1885 | | * =============================== |
1886 | | * |
1887 | | * This is the set of functions that the core makes available to the provider |
1888 | | */ |
1889 | | |
1890 | | /* |
1891 | | * This returns a list of Provider Object parameters with their types, for |
1892 | | * discovery. We do not expect that many providers will use this, but one |
1893 | | * never knows. |
1894 | | */ |
1895 | | static const OSSL_PARAM param_types[] = { |
1896 | | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0), |
1897 | | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_PROV_NAME, OSSL_PARAM_UTF8_PTR, |
1898 | | NULL, 0), |
1899 | | #ifndef FIPS_MODULE |
1900 | | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, OSSL_PARAM_UTF8_PTR, |
1901 | | NULL, 0), |
1902 | | #endif |
1903 | | OSSL_PARAM_END |
1904 | | }; |
1905 | | |
1906 | | /* |
1907 | | * Forward declare all the functions that are provided aa dispatch. |
1908 | | * This ensures that the compiler will complain if they aren't defined |
1909 | | * with the correct signature. |
1910 | | */ |
1911 | | static OSSL_FUNC_core_gettable_params_fn core_gettable_params; |
1912 | | static OSSL_FUNC_core_get_params_fn core_get_params; |
1913 | | static OSSL_FUNC_core_get_libctx_fn core_get_libctx; |
1914 | | static OSSL_FUNC_core_thread_start_fn core_thread_start; |
1915 | | #ifndef FIPS_MODULE |
1916 | | static OSSL_FUNC_core_new_error_fn core_new_error; |
1917 | | static OSSL_FUNC_core_set_error_debug_fn core_set_error_debug; |
1918 | | static OSSL_FUNC_core_vset_error_fn core_vset_error; |
1919 | | static OSSL_FUNC_core_set_error_mark_fn core_set_error_mark; |
1920 | | static OSSL_FUNC_core_clear_last_error_mark_fn core_clear_last_error_mark; |
1921 | | static OSSL_FUNC_core_pop_error_to_mark_fn core_pop_error_to_mark; |
1922 | | OSSL_FUNC_BIO_new_file_fn ossl_core_bio_new_file; |
1923 | | OSSL_FUNC_BIO_new_membuf_fn ossl_core_bio_new_mem_buf; |
1924 | | OSSL_FUNC_BIO_read_ex_fn ossl_core_bio_read_ex; |
1925 | | OSSL_FUNC_BIO_write_ex_fn ossl_core_bio_write_ex; |
1926 | | OSSL_FUNC_BIO_gets_fn ossl_core_bio_gets; |
1927 | | OSSL_FUNC_BIO_puts_fn ossl_core_bio_puts; |
1928 | | OSSL_FUNC_BIO_up_ref_fn ossl_core_bio_up_ref; |
1929 | | OSSL_FUNC_BIO_free_fn ossl_core_bio_free; |
1930 | | OSSL_FUNC_BIO_vprintf_fn ossl_core_bio_vprintf; |
1931 | | OSSL_FUNC_BIO_vsnprintf_fn BIO_vsnprintf; |
1932 | | static OSSL_FUNC_self_test_cb_fn core_self_test_get_callback; |
1933 | | static OSSL_FUNC_get_entropy_fn rand_get_entropy; |
1934 | | static OSSL_FUNC_get_user_entropy_fn rand_get_user_entropy; |
1935 | | static OSSL_FUNC_cleanup_entropy_fn rand_cleanup_entropy; |
1936 | | static OSSL_FUNC_cleanup_user_entropy_fn rand_cleanup_user_entropy; |
1937 | | static OSSL_FUNC_get_nonce_fn rand_get_nonce; |
1938 | | static OSSL_FUNC_get_user_nonce_fn rand_get_user_nonce; |
1939 | | static OSSL_FUNC_cleanup_nonce_fn rand_cleanup_nonce; |
1940 | | static OSSL_FUNC_cleanup_user_nonce_fn rand_cleanup_user_nonce; |
1941 | | #endif |
1942 | | OSSL_FUNC_CRYPTO_malloc_fn CRYPTO_malloc; |
1943 | | OSSL_FUNC_CRYPTO_zalloc_fn CRYPTO_zalloc; |
1944 | | OSSL_FUNC_CRYPTO_free_fn CRYPTO_free; |
1945 | | OSSL_FUNC_CRYPTO_clear_free_fn CRYPTO_clear_free; |
1946 | | OSSL_FUNC_CRYPTO_realloc_fn CRYPTO_realloc; |
1947 | | OSSL_FUNC_CRYPTO_clear_realloc_fn CRYPTO_clear_realloc; |
1948 | | OSSL_FUNC_CRYPTO_secure_malloc_fn CRYPTO_secure_malloc; |
1949 | | OSSL_FUNC_CRYPTO_secure_zalloc_fn CRYPTO_secure_zalloc; |
1950 | | OSSL_FUNC_CRYPTO_secure_free_fn CRYPTO_secure_free; |
1951 | | OSSL_FUNC_CRYPTO_secure_clear_free_fn CRYPTO_secure_clear_free; |
1952 | | OSSL_FUNC_CRYPTO_secure_allocated_fn CRYPTO_secure_allocated; |
1953 | | OSSL_FUNC_OPENSSL_cleanse_fn OPENSSL_cleanse; |
1954 | | #ifndef FIPS_MODULE |
1955 | | OSSL_FUNC_provider_register_child_cb_fn ossl_provider_register_child_cb; |
1956 | | OSSL_FUNC_provider_deregister_child_cb_fn ossl_provider_deregister_child_cb; |
1957 | | static OSSL_FUNC_provider_name_fn core_provider_get0_name; |
1958 | | static OSSL_FUNC_provider_get0_provider_ctx_fn core_provider_get0_provider_ctx; |
1959 | | static OSSL_FUNC_provider_get0_dispatch_fn core_provider_get0_dispatch; |
1960 | | static OSSL_FUNC_provider_up_ref_fn core_provider_up_ref_intern; |
1961 | | static OSSL_FUNC_provider_free_fn core_provider_free_intern; |
1962 | | static OSSL_FUNC_core_obj_add_sigid_fn core_obj_add_sigid; |
1963 | | static OSSL_FUNC_core_obj_create_fn core_obj_create; |
1964 | | #endif |
1965 | | |
1966 | | static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle) |
1967 | 0 | { |
1968 | 0 | return param_types; |
1969 | 0 | } |
1970 | | |
1971 | | static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[]) |
1972 | 0 | { |
1973 | 0 | int i; |
1974 | 0 | OSSL_PARAM *p; |
1975 | | /* |
1976 | | * We created this object originally and we know it is actually an |
1977 | | * OSSL_PROVIDER *, so the cast is safe |
1978 | | */ |
1979 | 0 | OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle; |
1980 | |
|
1981 | 0 | if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_VERSION)) != NULL) |
1982 | 0 | OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR); |
1983 | 0 | if ((p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL) |
1984 | 0 | OSSL_PARAM_set_utf8_ptr(p, prov->name); |
1985 | |
|
1986 | 0 | #ifndef FIPS_MODULE |
1987 | 0 | if ((p = OSSL_PARAM_locate(params, |
1988 | 0 | OSSL_PROV_PARAM_CORE_MODULE_FILENAME)) != NULL) |
1989 | 0 | OSSL_PARAM_set_utf8_ptr(p, ossl_provider_module_path(prov)); |
1990 | 0 | #endif |
1991 | |
|
1992 | 0 | if (prov->parameters == NULL) |
1993 | 0 | return 1; |
1994 | | |
1995 | 0 | for (i = 0; i < sk_INFOPAIR_num(prov->parameters); i++) { |
1996 | 0 | INFOPAIR *pair = sk_INFOPAIR_value(prov->parameters, i); |
1997 | |
|
1998 | 0 | if ((p = OSSL_PARAM_locate(params, pair->name)) != NULL) |
1999 | 0 | OSSL_PARAM_set_utf8_ptr(p, pair->value); |
2000 | 0 | } |
2001 | 0 | return 1; |
2002 | 0 | } |
2003 | | |
2004 | | static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle) |
2005 | 15 | { |
2006 | | /* |
2007 | | * We created this object originally and we know it is actually an |
2008 | | * OSSL_PROVIDER *, so the cast is safe |
2009 | | */ |
2010 | 15 | OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle; |
2011 | | |
2012 | | /* |
2013 | | * Using ossl_provider_libctx would be wrong as that returns |
2014 | | * NULL for |prov| == NULL and NULL libctx has a special meaning |
2015 | | * that does not apply here. Here |prov| == NULL can happen only in |
2016 | | * case of a coding error. |
2017 | | */ |
2018 | 15 | assert(prov != NULL); |
2019 | 15 | return (OPENSSL_CORE_CTX *)prov->libctx; |
2020 | 15 | } |
2021 | | |
2022 | | static int core_thread_start(const OSSL_CORE_HANDLE *handle, |
2023 | | OSSL_thread_stop_handler_fn handfn, |
2024 | | void *arg) |
2025 | 0 | { |
2026 | | /* |
2027 | | * We created this object originally and we know it is actually an |
2028 | | * OSSL_PROVIDER *, so the cast is safe |
2029 | | */ |
2030 | 0 | OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle; |
2031 | |
|
2032 | 0 | return ossl_init_thread_start(prov, arg, handfn); |
2033 | 0 | } |
2034 | | |
2035 | | /* |
2036 | | * The FIPS module inner provider doesn't implement these. They aren't |
2037 | | * needed there, since the FIPS module upcalls are always the outer provider |
2038 | | * ones. |
2039 | | */ |
2040 | | #ifndef FIPS_MODULE |
2041 | | /* |
2042 | | * These error functions should use |handle| to select the proper |
2043 | | * library context to report in the correct error stack if error |
2044 | | * stacks become tied to the library context. |
2045 | | * We cannot currently do that since there's no support for it in the |
2046 | | * ERR subsystem. |
2047 | | */ |
2048 | | static void core_new_error(const OSSL_CORE_HANDLE *handle) |
2049 | 0 | { |
2050 | 0 | ERR_new(); |
2051 | 0 | } |
2052 | | |
2053 | | static void core_set_error_debug(const OSSL_CORE_HANDLE *handle, |
2054 | | const char *file, int line, const char *func) |
2055 | 0 | { |
2056 | 0 | ERR_set_debug(file, line, func); |
2057 | 0 | } |
2058 | | |
2059 | | static void core_vset_error(const OSSL_CORE_HANDLE *handle, |
2060 | | uint32_t reason, const char *fmt, va_list args) |
2061 | 0 | { |
2062 | | /* |
2063 | | * We created this object originally and we know it is actually an |
2064 | | * OSSL_PROVIDER *, so the cast is safe |
2065 | | */ |
2066 | 0 | OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle; |
2067 | | |
2068 | | /* |
2069 | | * If the uppermost 8 bits are non-zero, it's an OpenSSL library |
2070 | | * error and will be treated as such. Otherwise, it's a new style |
2071 | | * provider error and will be treated as such. |
2072 | | */ |
2073 | 0 | if (ERR_GET_LIB(reason) != 0) { |
2074 | 0 | ERR_vset_error(ERR_GET_LIB(reason), ERR_GET_REASON(reason), fmt, args); |
2075 | 0 | } else { |
2076 | 0 | ERR_vset_error(prov->error_lib, (int)reason, fmt, args); |
2077 | 0 | } |
2078 | 0 | } |
2079 | | |
2080 | | static int core_set_error_mark(const OSSL_CORE_HANDLE *handle) |
2081 | 0 | { |
2082 | 0 | return ERR_set_mark(); |
2083 | 0 | } |
2084 | | |
2085 | | static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle) |
2086 | 0 | { |
2087 | 0 | return ERR_clear_last_mark(); |
2088 | 0 | } |
2089 | | |
2090 | | static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle) |
2091 | 0 | { |
2092 | 0 | return ERR_pop_to_mark(); |
2093 | 0 | } |
2094 | | |
2095 | | static void core_self_test_get_callback(OPENSSL_CORE_CTX *libctx, |
2096 | | OSSL_CALLBACK **cb, void **cbarg) |
2097 | 0 | { |
2098 | 0 | OSSL_SELF_TEST_get_callback((OSSL_LIB_CTX *)libctx, cb, cbarg); |
2099 | 0 | } |
2100 | | |
2101 | | static size_t rand_get_entropy(const OSSL_CORE_HANDLE *handle, |
2102 | | unsigned char **pout, int entropy, |
2103 | | size_t min_len, size_t max_len) |
2104 | 0 | { |
2105 | 0 | return ossl_rand_get_entropy((OSSL_LIB_CTX *)core_get_libctx(handle), |
2106 | 0 | pout, entropy, min_len, max_len); |
2107 | 0 | } |
2108 | | |
2109 | | static size_t rand_get_user_entropy(const OSSL_CORE_HANDLE *handle, |
2110 | | unsigned char **pout, int entropy, |
2111 | | size_t min_len, size_t max_len) |
2112 | 0 | { |
2113 | 0 | return ossl_rand_get_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle), |
2114 | 0 | pout, entropy, min_len, max_len); |
2115 | 0 | } |
2116 | | |
2117 | | static void rand_cleanup_entropy(const OSSL_CORE_HANDLE *handle, |
2118 | | unsigned char *buf, size_t len) |
2119 | 0 | { |
2120 | 0 | ossl_rand_cleanup_entropy((OSSL_LIB_CTX *)core_get_libctx(handle), |
2121 | 0 | buf, len); |
2122 | 0 | } |
2123 | | |
2124 | | static void rand_cleanup_user_entropy(const OSSL_CORE_HANDLE *handle, |
2125 | | unsigned char *buf, size_t len) |
2126 | 0 | { |
2127 | 0 | ossl_rand_cleanup_user_entropy((OSSL_LIB_CTX *)core_get_libctx(handle), |
2128 | 0 | buf, len); |
2129 | 0 | } |
2130 | | |
2131 | | static size_t rand_get_nonce(const OSSL_CORE_HANDLE *handle, |
2132 | | unsigned char **pout, |
2133 | | size_t min_len, size_t max_len, |
2134 | | const void *salt, size_t salt_len) |
2135 | 0 | { |
2136 | 0 | return ossl_rand_get_nonce((OSSL_LIB_CTX *)core_get_libctx(handle), |
2137 | 0 | pout, min_len, max_len, salt, salt_len); |
2138 | 0 | } |
2139 | | |
2140 | | static size_t rand_get_user_nonce(const OSSL_CORE_HANDLE *handle, |
2141 | | unsigned char **pout, |
2142 | | size_t min_len, size_t max_len, |
2143 | | const void *salt, size_t salt_len) |
2144 | 0 | { |
2145 | 0 | return ossl_rand_get_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle), |
2146 | 0 | pout, min_len, max_len, salt, salt_len); |
2147 | 0 | } |
2148 | | |
2149 | | static void rand_cleanup_nonce(const OSSL_CORE_HANDLE *handle, |
2150 | | unsigned char *buf, size_t len) |
2151 | 0 | { |
2152 | 0 | ossl_rand_cleanup_nonce((OSSL_LIB_CTX *)core_get_libctx(handle), |
2153 | 0 | buf, len); |
2154 | 0 | } |
2155 | | |
2156 | | static void rand_cleanup_user_nonce(const OSSL_CORE_HANDLE *handle, |
2157 | | unsigned char *buf, size_t len) |
2158 | 0 | { |
2159 | 0 | ossl_rand_cleanup_user_nonce((OSSL_LIB_CTX *)core_get_libctx(handle), |
2160 | 0 | buf, len); |
2161 | 0 | } |
2162 | | |
2163 | | static const char *core_provider_get0_name(const OSSL_CORE_HANDLE *prov) |
2164 | 0 | { |
2165 | 0 | return OSSL_PROVIDER_get0_name((const OSSL_PROVIDER *)prov); |
2166 | 0 | } |
2167 | | |
2168 | | static void *core_provider_get0_provider_ctx(const OSSL_CORE_HANDLE *prov) |
2169 | 0 | { |
2170 | 0 | return OSSL_PROVIDER_get0_provider_ctx((const OSSL_PROVIDER *)prov); |
2171 | 0 | } |
2172 | | |
2173 | | static const OSSL_DISPATCH * |
2174 | | core_provider_get0_dispatch(const OSSL_CORE_HANDLE *prov) |
2175 | 0 | { |
2176 | 0 | return OSSL_PROVIDER_get0_dispatch((const OSSL_PROVIDER *)prov); |
2177 | 0 | } |
2178 | | |
2179 | | static int core_provider_up_ref_intern(const OSSL_CORE_HANDLE *prov, |
2180 | | int activate) |
2181 | 0 | { |
2182 | 0 | return provider_up_ref_intern((OSSL_PROVIDER *)prov, activate); |
2183 | 0 | } |
2184 | | |
2185 | | static int core_provider_free_intern(const OSSL_CORE_HANDLE *prov, |
2186 | | int deactivate) |
2187 | 0 | { |
2188 | 0 | return provider_free_intern((OSSL_PROVIDER *)prov, deactivate); |
2189 | 0 | } |
2190 | | |
2191 | | static int core_obj_add_sigid(const OSSL_CORE_HANDLE *prov, |
2192 | | const char *sign_name, const char *digest_name, |
2193 | | const char *pkey_name) |
2194 | 0 | { |
2195 | 0 | int sign_nid = OBJ_txt2nid(sign_name); |
2196 | 0 | int digest_nid = NID_undef; |
2197 | 0 | int pkey_nid = OBJ_txt2nid(pkey_name); |
2198 | |
|
2199 | 0 | if (digest_name != NULL && digest_name[0] != '\0' |
2200 | 0 | && (digest_nid = OBJ_txt2nid(digest_name)) == NID_undef) |
2201 | 0 | return 0; |
2202 | | |
2203 | 0 | if (sign_nid == NID_undef) |
2204 | 0 | return 0; |
2205 | | |
2206 | | /* |
2207 | | * Check if it already exists. This is a success if so (even if we don't |
2208 | | * have nids for the digest/pkey) |
2209 | | */ |
2210 | 0 | if (OBJ_find_sigid_algs(sign_nid, NULL, NULL)) |
2211 | 0 | return 1; |
2212 | | |
2213 | 0 | if (pkey_nid == NID_undef) |
2214 | 0 | return 0; |
2215 | | |
2216 | 0 | return OBJ_add_sigid(sign_nid, digest_nid, pkey_nid); |
2217 | 0 | } |
2218 | | |
2219 | | static int core_obj_create(const OSSL_CORE_HANDLE *prov, const char *oid, |
2220 | | const char *sn, const char *ln) |
2221 | 0 | { |
2222 | | /* Check if it already exists and create it if not */ |
2223 | 0 | return OBJ_txt2nid(oid) != NID_undef |
2224 | 0 | || OBJ_create(oid, sn, ln) != NID_undef; |
2225 | 0 | } |
2226 | | #endif /* FIPS_MODULE */ |
2227 | | |
2228 | | /* |
2229 | | * Functions provided by the core. |
2230 | | */ |
2231 | | static const OSSL_DISPATCH core_dispatch_[] = { |
2232 | | { OSSL_FUNC_CORE_GETTABLE_PARAMS, (void (*)(void))core_gettable_params }, |
2233 | | { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params }, |
2234 | | { OSSL_FUNC_CORE_GET_LIBCTX, (void (*)(void))core_get_libctx }, |
2235 | | { OSSL_FUNC_CORE_THREAD_START, (void (*)(void))core_thread_start }, |
2236 | | #ifndef FIPS_MODULE |
2237 | | { OSSL_FUNC_CORE_NEW_ERROR, (void (*)(void))core_new_error }, |
2238 | | { OSSL_FUNC_CORE_SET_ERROR_DEBUG, (void (*)(void))core_set_error_debug }, |
2239 | | { OSSL_FUNC_CORE_VSET_ERROR, (void (*)(void))core_vset_error }, |
2240 | | { OSSL_FUNC_CORE_SET_ERROR_MARK, (void (*)(void))core_set_error_mark }, |
2241 | | { OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK, |
2242 | | (void (*)(void))core_clear_last_error_mark }, |
2243 | | { OSSL_FUNC_CORE_POP_ERROR_TO_MARK, (void (*)(void))core_pop_error_to_mark }, |
2244 | | { OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))ossl_core_bio_new_file }, |
2245 | | { OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))ossl_core_bio_new_mem_buf }, |
2246 | | { OSSL_FUNC_BIO_READ_EX, (void (*)(void))ossl_core_bio_read_ex }, |
2247 | | { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))ossl_core_bio_write_ex }, |
2248 | | { OSSL_FUNC_BIO_GETS, (void (*)(void))ossl_core_bio_gets }, |
2249 | | { OSSL_FUNC_BIO_PUTS, (void (*)(void))ossl_core_bio_puts }, |
2250 | | { OSSL_FUNC_BIO_CTRL, (void (*)(void))ossl_core_bio_ctrl }, |
2251 | | { OSSL_FUNC_BIO_UP_REF, (void (*)(void))ossl_core_bio_up_ref }, |
2252 | | { OSSL_FUNC_BIO_FREE, (void (*)(void))ossl_core_bio_free }, |
2253 | | { OSSL_FUNC_BIO_VPRINTF, (void (*)(void))ossl_core_bio_vprintf }, |
2254 | | { OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf }, |
2255 | | { OSSL_FUNC_SELF_TEST_CB, (void (*)(void))core_self_test_get_callback }, |
2256 | | { OSSL_FUNC_GET_ENTROPY, (void (*)(void))rand_get_entropy }, |
2257 | | { OSSL_FUNC_GET_USER_ENTROPY, (void (*)(void))rand_get_user_entropy }, |
2258 | | { OSSL_FUNC_CLEANUP_ENTROPY, (void (*)(void))rand_cleanup_entropy }, |
2259 | | { OSSL_FUNC_CLEANUP_USER_ENTROPY, (void (*)(void))rand_cleanup_user_entropy }, |
2260 | | { OSSL_FUNC_GET_NONCE, (void (*)(void))rand_get_nonce }, |
2261 | | { OSSL_FUNC_GET_USER_NONCE, (void (*)(void))rand_get_user_nonce }, |
2262 | | { OSSL_FUNC_CLEANUP_NONCE, (void (*)(void))rand_cleanup_nonce }, |
2263 | | { OSSL_FUNC_CLEANUP_USER_NONCE, (void (*)(void))rand_cleanup_user_nonce }, |
2264 | | #endif |
2265 | | { OSSL_FUNC_CRYPTO_MALLOC, (void (*)(void))CRYPTO_malloc }, |
2266 | | { OSSL_FUNC_CRYPTO_ZALLOC, (void (*)(void))CRYPTO_zalloc }, |
2267 | | { OSSL_FUNC_CRYPTO_FREE, (void (*)(void))CRYPTO_free }, |
2268 | | { OSSL_FUNC_CRYPTO_CLEAR_FREE, (void (*)(void))CRYPTO_clear_free }, |
2269 | | { OSSL_FUNC_CRYPTO_REALLOC, (void (*)(void))CRYPTO_realloc }, |
2270 | | { OSSL_FUNC_CRYPTO_CLEAR_REALLOC, (void (*)(void))CRYPTO_clear_realloc }, |
2271 | | { OSSL_FUNC_CRYPTO_SECURE_MALLOC, (void (*)(void))CRYPTO_secure_malloc }, |
2272 | | { OSSL_FUNC_CRYPTO_SECURE_ZALLOC, (void (*)(void))CRYPTO_secure_zalloc }, |
2273 | | { OSSL_FUNC_CRYPTO_SECURE_FREE, (void (*)(void))CRYPTO_secure_free }, |
2274 | | { OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE, |
2275 | | (void (*)(void))CRYPTO_secure_clear_free }, |
2276 | | { OSSL_FUNC_CRYPTO_SECURE_ALLOCATED, |
2277 | | (void (*)(void))CRYPTO_secure_allocated }, |
2278 | | { OSSL_FUNC_OPENSSL_CLEANSE, (void (*)(void))OPENSSL_cleanse }, |
2279 | | #ifndef FIPS_MODULE |
2280 | | { OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB, |
2281 | | (void (*)(void))ossl_provider_register_child_cb }, |
2282 | | { OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB, |
2283 | | (void (*)(void))ossl_provider_deregister_child_cb }, |
2284 | | { OSSL_FUNC_PROVIDER_NAME, |
2285 | | (void (*)(void))core_provider_get0_name }, |
2286 | | { OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX, |
2287 | | (void (*)(void))core_provider_get0_provider_ctx }, |
2288 | | { OSSL_FUNC_PROVIDER_GET0_DISPATCH, |
2289 | | (void (*)(void))core_provider_get0_dispatch }, |
2290 | | { OSSL_FUNC_PROVIDER_UP_REF, |
2291 | | (void (*)(void))core_provider_up_ref_intern }, |
2292 | | { OSSL_FUNC_PROVIDER_FREE, |
2293 | | (void (*)(void))core_provider_free_intern }, |
2294 | | { OSSL_FUNC_CORE_OBJ_ADD_SIGID, (void (*)(void))core_obj_add_sigid }, |
2295 | | { OSSL_FUNC_CORE_OBJ_CREATE, (void (*)(void))core_obj_create }, |
2296 | | #endif |
2297 | | OSSL_DISPATCH_END |
2298 | | }; |
2299 | | static const OSSL_DISPATCH *core_dispatch = core_dispatch_; |