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