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