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
10 #include "crypto/cryptlib.h"
11 #include "internal/thread_once.h"
13 int ossl_do_ex_data_init(OSSL_LIB_CTX *ctx)
15 OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
20 global->ex_data_lock = CRYPTO_THREAD_lock_new();
21 return global->ex_data_lock != NULL;
25 * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
26 * a given class. On success, *holds the lock.*
27 * The |global| parameter is assumed to be non null (checked by the caller).
29 static EX_CALLBACKS *get_and_lock(OSSL_EX_DATA_GLOBAL *global, int class_index)
33 if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
34 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
38 if (global->ex_data_lock == NULL) {
40 * If we get here, someone (who?) cleaned up the lock, so just
41 * treat it as an error.
46 if (!CRYPTO_THREAD_write_lock(global->ex_data_lock))
48 ip = &global->ex_data[class_index];
52 static void cleanup_cb(EX_CALLBACK *funcs)
58 * Release all "ex_data" state to prevent memory leaks. This can't be made
59 * thread-safe without overhauling a lot of stuff, and shouldn't really be
60 * called under potential race-conditions anyway (it's for program shutdown
63 void ossl_crypto_cleanup_all_ex_data_int(OSSL_LIB_CTX *ctx)
66 OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
71 for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
72 EX_CALLBACKS *ip = &global->ex_data[i];
74 sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
78 CRYPTO_THREAD_lock_free(global->ex_data_lock);
79 global->ex_data_lock = NULL;
84 * Unregister a new index by replacing the callbacks with no-ops.
85 * Any in-use instances are leaked.
87 static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
88 long argl, void *argp)
92 static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
93 long argl, void *argp)
97 static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
98 void **from_d, int idx,
99 long argl, void *argp)
104 int ossl_crypto_free_ex_index_ex(OSSL_LIB_CTX *ctx, int class_index, int idx)
109 OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
114 ip = get_and_lock(global, class_index);
118 if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
120 a = sk_EX_CALLBACK_value(ip->meth, idx);
123 a->new_func = dummy_new;
124 a->dup_func = dummy_dup;
125 a->free_func = dummy_free;
128 CRYPTO_THREAD_unlock(global->ex_data_lock);
132 int CRYPTO_free_ex_index(int class_index, int idx)
134 return ossl_crypto_free_ex_index_ex(NULL, class_index, idx);
138 * Register a new index.
140 int ossl_crypto_get_ex_new_index_ex(OSSL_LIB_CTX *ctx, int class_index,
141 long argl, void *argp,
142 CRYPTO_EX_new *new_func,
143 CRYPTO_EX_dup *dup_func,
144 CRYPTO_EX_free *free_func)
149 OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
154 ip = get_and_lock(global, class_index);
158 if (ip->meth == NULL) {
159 ip->meth = sk_EX_CALLBACK_new_null();
160 /* We push an initial value on the stack because the SSL
161 * "app_data" routines use ex_data index zero. See RT 3710. */
163 || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
164 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
169 a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
171 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
176 a->new_func = new_func;
177 a->dup_func = dup_func;
178 a->free_func = free_func;
180 if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
181 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
185 toret = sk_EX_CALLBACK_num(ip->meth) - 1;
186 (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
189 CRYPTO_THREAD_unlock(global->ex_data_lock);
193 int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
194 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
195 CRYPTO_EX_free *free_func)
197 return ossl_crypto_get_ex_new_index_ex(NULL, class_index, argl, argp,
198 new_func, dup_func, free_func);
202 * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
203 * calling new() callbacks for each index in the class used by this variable
204 * Thread-safe by copying a class's array of "EX_CALLBACK" entries
205 * in the lock, then using them outside the lock. Note this only applies
206 * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
208 int ossl_crypto_new_ex_data_ex(OSSL_LIB_CTX *ctx, int class_index, void *obj,
213 EX_CALLBACK **storage = NULL;
214 EX_CALLBACK *stack[10];
216 OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
221 ip = get_and_lock(global, class_index);
227 mx = sk_EX_CALLBACK_num(ip->meth);
229 if (mx < (int)OSSL_NELEM(stack))
232 storage = OPENSSL_malloc(sizeof(*storage) * mx);
234 for (i = 0; i < mx; i++)
235 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
237 CRYPTO_THREAD_unlock(global->ex_data_lock);
239 if (mx > 0 && storage == NULL) {
240 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
243 for (i = 0; i < mx; i++) {
244 if (storage[i] != NULL && storage[i]->new_func != NULL) {
245 ptr = CRYPTO_get_ex_data(ad, i);
246 storage[i]->new_func(obj, ptr, ad, i,
247 storage[i]->argl, storage[i]->argp);
250 if (storage != stack)
251 OPENSSL_free(storage);
255 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
257 return ossl_crypto_new_ex_data_ex(NULL, class_index, obj, ad);
261 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
262 * for each index in the class used by this variable
264 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
265 const CRYPTO_EX_DATA *from)
269 EX_CALLBACK *stack[10];
270 EX_CALLBACK **storage = NULL;
273 OSSL_EX_DATA_GLOBAL *global;
276 if (from->sk == NULL)
277 /* Nothing to copy over */
280 global = ossl_lib_ctx_get_ex_data_global(from->ctx);
284 ip = get_and_lock(global, class_index);
288 mx = sk_EX_CALLBACK_num(ip->meth);
289 j = sk_void_num(from->sk);
293 if (mx < (int)OSSL_NELEM(stack))
296 storage = OPENSSL_malloc(sizeof(*storage) * mx);
298 for (i = 0; i < mx; i++)
299 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
301 CRYPTO_THREAD_unlock(global->ex_data_lock);
305 if (storage == NULL) {
306 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
310 * Make sure the ex_data stack is at least |mx| elements long to avoid
311 * issues in the for loop that follows; so go get the |mx|'th element
312 * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
313 * to itself. This is normally a no-op; but ensures the stack is the
316 if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
319 for (i = 0; i < mx; i++) {
320 ptr = CRYPTO_get_ex_data(from, i);
321 if (storage[i] != NULL && storage[i]->dup_func != NULL)
322 if (!storage[i]->dup_func(to, from, &ptr, i,
323 storage[i]->argl, storage[i]->argp))
325 CRYPTO_set_ex_data(to, i, ptr);
329 if (storage != stack)
330 OPENSSL_free(storage);
336 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
337 * each index in the class used by this variable
339 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
345 EX_CALLBACK *stack[10];
346 EX_CALLBACK **storage = NULL;
347 OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ad->ctx);
352 ip = get_and_lock(global, class_index);
356 mx = sk_EX_CALLBACK_num(ip->meth);
358 if (mx < (int)OSSL_NELEM(stack))
361 storage = OPENSSL_malloc(sizeof(*storage) * mx);
363 for (i = 0; i < mx; i++)
364 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
366 CRYPTO_THREAD_unlock(global->ex_data_lock);
368 for (i = 0; i < mx; i++) {
372 if (!CRYPTO_THREAD_write_lock(global->ex_data_lock))
374 f = sk_EX_CALLBACK_value(ip->meth, i);
375 CRYPTO_THREAD_unlock(global->ex_data_lock);
377 if (f != NULL && f->free_func != NULL) {
378 ptr = CRYPTO_get_ex_data(ad, i);
379 f->free_func(obj, ptr, ad, i, f->argl, f->argp);
383 if (storage != stack)
384 OPENSSL_free(storage);
386 sk_void_free(ad->sk);
392 * Allocate a given CRYPTO_EX_DATA item using the class specific allocation
395 int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad,
400 curval = CRYPTO_get_ex_data(ad, idx);
401 /* Already there, no need to allocate */
405 return ossl_crypto_alloc_ex_data_intern(class_index, obj, ad, idx);
408 int ossl_crypto_alloc_ex_data_intern(int class_index, void *obj,
409 CRYPTO_EX_DATA *ad, int idx)
413 OSSL_EX_DATA_GLOBAL *global;
415 global = ossl_lib_ctx_get_ex_data_global(ad->ctx);
419 ip = get_and_lock(global, class_index);
422 f = sk_EX_CALLBACK_value(ip->meth, idx);
423 CRYPTO_THREAD_unlock(global->ex_data_lock);
426 * This should end up calling CRYPTO_set_ex_data(), which allocates
427 * everything necessary to support placing the new data in the right spot.
429 if (f->new_func == NULL)
432 f->new_func(obj, NULL, ad, idx, f->argl, f->argp);
438 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
439 * particular index in the class used by this variable
441 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
445 if (ad->sk == NULL) {
446 if ((ad->sk = sk_void_new_null()) == NULL) {
447 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
452 for (i = sk_void_num(ad->sk); i <= idx; ++i) {
453 if (!sk_void_push(ad->sk, NULL)) {
454 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
458 if (sk_void_set(ad->sk, idx, val) != val) {
459 /* Probably the index is out of bounds */
460 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
467 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
468 * particular index in the class used by this variable
470 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
472 if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
474 return sk_void_value(ad->sk, idx);
477 OSSL_LIB_CTX *ossl_crypto_ex_data_get_ossl_lib_ctx(const CRYPTO_EX_DATA *ad)