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