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