2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "crypto/cryptlib.h"
12 #include "internal/thread_once.h"
14 int ossl_do_ex_data_init(OSSL_LIB_CTX *ctx)
16 OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
21 global->ex_data_lock = CRYPTO_THREAD_lock_new();
22 return global->ex_data_lock != NULL;
26 * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
27 * a given class. On success, *holds the lock.*
28 * The |global| parameter is assumed to be non null (checked by the caller).
30 static EX_CALLBACKS *get_and_lock(OSSL_EX_DATA_GLOBAL *global, int class_index)
34 if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
35 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
39 if (global->ex_data_lock == NULL) {
41 * If we get here, someone (who?) cleaned up the lock, so just
42 * treat it as an error.
47 if (!CRYPTO_THREAD_write_lock(global->ex_data_lock))
49 ip = &global->ex_data[class_index];
53 static void cleanup_cb(EX_CALLBACK *funcs)
59 * Release all "ex_data" state to prevent memory leaks. This can't be made
60 * thread-safe without overhauling a lot of stuff, and shouldn't really be
61 * called under potential race-conditions anyway (it's for program shutdown
64 void ossl_crypto_cleanup_all_ex_data_int(OSSL_LIB_CTX *ctx)
67 OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
72 for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
73 EX_CALLBACKS *ip = &global->ex_data[i];
75 sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
79 CRYPTO_THREAD_lock_free(global->ex_data_lock);
80 global->ex_data_lock = NULL;
85 * Unregister a new index by replacing the callbacks with no-ops.
86 * Any in-use instances are leaked.
88 static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
89 long argl, void *argp)
93 static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
94 long argl, void *argp)
98 static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
99 void **from_d, int idx,
100 long argl, void *argp)
105 int ossl_crypto_free_ex_index_ex(OSSL_LIB_CTX *ctx, int class_index, int idx)
110 OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
115 ip = get_and_lock(global, class_index);
119 if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
121 a = sk_EX_CALLBACK_value(ip->meth, idx);
124 a->new_func = dummy_new;
125 a->dup_func = dummy_dup;
126 a->free_func = dummy_free;
129 CRYPTO_THREAD_unlock(global->ex_data_lock);
133 int CRYPTO_free_ex_index(int class_index, int idx)
135 return ossl_crypto_free_ex_index_ex(NULL, class_index, idx);
139 * Register a new index.
141 int ossl_crypto_get_ex_new_index_ex(OSSL_LIB_CTX *ctx, int class_index,
142 long argl, void *argp,
143 CRYPTO_EX_new *new_func,
144 CRYPTO_EX_dup *dup_func,
145 CRYPTO_EX_free *free_func,
151 OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
156 ip = get_and_lock(global, class_index);
160 if (ip->meth == NULL) {
161 ip->meth = sk_EX_CALLBACK_new_null();
162 /* We push an initial value on the stack because the SSL
163 * "app_data" routines use ex_data index zero. See RT 3710. */
165 || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
166 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
171 a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
173 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
178 a->new_func = new_func;
179 a->dup_func = dup_func;
180 a->free_func = free_func;
181 a->priority = priority;
183 if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
184 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
188 toret = sk_EX_CALLBACK_num(ip->meth) - 1;
189 (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
192 CRYPTO_THREAD_unlock(global->ex_data_lock);
196 int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
197 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
198 CRYPTO_EX_free *free_func)
200 return ossl_crypto_get_ex_new_index_ex(NULL, class_index, argl, argp,
201 new_func, dup_func, free_func, 0);
205 * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
206 * calling new() callbacks for each index in the class used by this variable
207 * Thread-safe by copying a class's array of "EX_CALLBACK" entries
208 * in the lock, then using them outside the lock. Note this only applies
209 * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
211 int ossl_crypto_new_ex_data_ex(OSSL_LIB_CTX *ctx, int class_index, void *obj,
216 EX_CALLBACK **storage = NULL;
217 EX_CALLBACK *stack[10];
219 OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
224 ip = get_and_lock(global, class_index);
230 mx = sk_EX_CALLBACK_num(ip->meth);
232 if (mx < (int)OSSL_NELEM(stack))
235 storage = OPENSSL_malloc(sizeof(*storage) * mx);
237 for (i = 0; i < mx; i++)
238 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
240 CRYPTO_THREAD_unlock(global->ex_data_lock);
242 if (mx > 0 && storage == NULL) {
243 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
246 for (i = 0; i < mx; i++) {
247 if (storage[i] != NULL && storage[i]->new_func != NULL) {
248 ptr = CRYPTO_get_ex_data(ad, i);
249 storage[i]->new_func(obj, ptr, ad, i,
250 storage[i]->argl, storage[i]->argp);
253 if (storage != stack)
254 OPENSSL_free(storage);
258 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
260 return ossl_crypto_new_ex_data_ex(NULL, class_index, obj, ad);
264 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
265 * for each index in the class used by this variable
267 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
268 const CRYPTO_EX_DATA *from)
272 EX_CALLBACK *stack[10];
273 EX_CALLBACK **storage = NULL;
276 OSSL_EX_DATA_GLOBAL *global;
279 if (from->sk == NULL)
280 /* Nothing to copy over */
283 global = ossl_lib_ctx_get_ex_data_global(from->ctx);
287 ip = get_and_lock(global, class_index);
291 mx = sk_EX_CALLBACK_num(ip->meth);
292 j = sk_void_num(from->sk);
296 if (mx < (int)OSSL_NELEM(stack))
299 storage = OPENSSL_malloc(sizeof(*storage) * mx);
301 for (i = 0; i < mx; i++)
302 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
304 CRYPTO_THREAD_unlock(global->ex_data_lock);
308 if (storage == NULL) {
309 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
313 * Make sure the ex_data stack is at least |mx| elements long to avoid
314 * issues in the for loop that follows; so go get the |mx|'th element
315 * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
316 * to itself. This is normally a no-op; but ensures the stack is the
319 if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
322 for (i = 0; i < mx; i++) {
323 ptr = CRYPTO_get_ex_data(from, i);
324 if (storage[i] != NULL && storage[i]->dup_func != NULL)
325 if (!storage[i]->dup_func(to, from, &ptr, i,
326 storage[i]->argl, storage[i]->argp))
328 CRYPTO_set_ex_data(to, i, ptr);
332 if (storage != stack)
333 OPENSSL_free(storage);
337 struct ex_callback_entry {
338 const EX_CALLBACK *excb;
342 static int ex_callback_compare(const void *a, const void *b)
344 const struct ex_callback_entry *ap = (const struct ex_callback_entry *)a;
345 const struct ex_callback_entry *bp = (const struct ex_callback_entry *)b;
347 if (ap->excb == bp->excb)
350 if (ap->excb == NULL)
352 if (bp->excb == NULL)
354 if (ap->excb->priority == bp->excb->priority)
356 return ap->excb->priority > bp->excb->priority ? -1 : 1;
360 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
361 * each index in the class used by this variable
363 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
368 const EX_CALLBACK *f;
369 struct ex_callback_entry stack[10];
370 struct ex_callback_entry *storage = NULL;
371 OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ad->ctx);
376 ip = get_and_lock(global, class_index);
380 mx = sk_EX_CALLBACK_num(ip->meth);
382 if (mx < (int)OSSL_NELEM(stack))
385 storage = OPENSSL_malloc(sizeof(*storage) * mx);
387 for (i = 0; i < mx; i++) {
388 storage[i].excb = sk_EX_CALLBACK_value(ip->meth, i);
389 storage[i].index = i;
392 CRYPTO_THREAD_unlock(global->ex_data_lock);
394 if (storage != NULL) {
395 /* Sort according to priority. High priority first */
396 qsort(storage, mx, sizeof(*storage), ex_callback_compare);
397 for (i = 0; i < mx; i++) {
400 if (f != NULL && f->free_func != NULL) {
401 ptr = CRYPTO_get_ex_data(ad, storage[i].index);
402 f->free_func(obj, ptr, ad, storage[i].index, f->argl, f->argp);
407 if (storage != stack)
408 OPENSSL_free(storage);
410 sk_void_free(ad->sk);
416 * Allocate a given CRYPTO_EX_DATA item using the class specific allocation
419 int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad,
424 curval = CRYPTO_get_ex_data(ad, idx);
425 /* Already there, no need to allocate */
429 return ossl_crypto_alloc_ex_data_intern(class_index, obj, ad, idx);
432 int ossl_crypto_alloc_ex_data_intern(int class_index, void *obj,
433 CRYPTO_EX_DATA *ad, int idx)
437 OSSL_EX_DATA_GLOBAL *global;
439 global = ossl_lib_ctx_get_ex_data_global(ad->ctx);
443 ip = get_and_lock(global, class_index);
446 f = sk_EX_CALLBACK_value(ip->meth, idx);
447 CRYPTO_THREAD_unlock(global->ex_data_lock);
450 * This should end up calling CRYPTO_set_ex_data(), which allocates
451 * everything necessary to support placing the new data in the right spot.
453 if (f->new_func == NULL)
456 f->new_func(obj, NULL, ad, idx, f->argl, f->argp);
462 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
463 * particular index in the class used by this variable
465 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
469 if (ad->sk == NULL) {
470 if ((ad->sk = sk_void_new_null()) == NULL) {
471 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
476 for (i = sk_void_num(ad->sk); i <= idx; ++i) {
477 if (!sk_void_push(ad->sk, NULL)) {
478 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
482 if (sk_void_set(ad->sk, idx, val) != val) {
483 /* Probably the index is out of bounds */
484 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
491 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
492 * particular index in the class used by this variable
494 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
496 if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
498 return sk_void_value(ad->sk, idx);
501 OSSL_LIB_CTX *ossl_crypto_ex_data_get_ossl_lib_ctx(const CRYPTO_EX_DATA *ad)