2 * Copyright 1995-2018 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 "internal/cryptlib_int.h"
11 #include "internal/thread_once.h"
13 int do_ex_data_init(OPENSSL_CTX *ctx)
15 OSSL_EX_DATA_GLOBAL *global = openssl_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.*
28 static EX_CALLBACKS *get_and_lock(OPENSSL_CTX *ctx, int class_index)
31 OSSL_EX_DATA_GLOBAL *global = NULL;
33 if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
34 CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);
38 global = openssl_ctx_get_ex_data_global(ctx);
39 if (global == NULL || global->ex_data_lock == NULL) {
41 * This can happen in normal operation when using CRYPTO_mem_leaks().
42 * The CRYPTO_mem_leaks() function calls OPENSSL_cleanup() which cleans
43 * up the locks. Subsequently the BIO that CRYPTO_mem_leaks() uses gets
44 * freed, which also attempts to free the ex_data. However
45 * CRYPTO_mem_leaks() ensures that the ex_data is freed early (i.e.
46 * before OPENSSL_cleanup() is called), so if we get here we can safely
47 * ignore this operation. We just treat it as an error.
52 ip = &global->ex_data[class_index];
53 CRYPTO_THREAD_write_lock(global->ex_data_lock);
57 static void cleanup_cb(EX_CALLBACK *funcs)
63 * Release all "ex_data" state to prevent memory leaks. This can't be made
64 * thread-safe without overhauling a lot of stuff, and shouldn't really be
65 * called under potential race-conditions anyway (it's for program shutdown
68 void crypto_cleanup_all_ex_data_int(OPENSSL_CTX *ctx)
71 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
76 for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
77 EX_CALLBACKS *ip = &global->ex_data[i];
79 sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
83 CRYPTO_THREAD_lock_free(global->ex_data_lock);
84 global->ex_data_lock = NULL;
89 * Unregister a new index by replacing the callbacks with no-ops.
90 * Any in-use instances are leaked.
92 static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
93 long argl, void *argp)
97 static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
98 long argl, void *argp)
102 static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
103 void *from_d, int idx,
104 long argl, void *argp)
109 int crypto_free_ex_index_ex(OPENSSL_CTX *ctx, int class_index, int idx)
111 EX_CALLBACKS *ip = get_and_lock(ctx, class_index);
114 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
119 ip = get_and_lock(ctx, class_index);
122 if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
124 a = sk_EX_CALLBACK_value(ip->meth, idx);
127 a->new_func = dummy_new;
128 a->dup_func = dummy_dup;
129 a->free_func = dummy_free;
132 CRYPTO_THREAD_unlock(global->ex_data_lock);
136 int CRYPTO_free_ex_index(int class_index, int idx)
138 return crypto_free_ex_index_ex(NULL, class_index, idx);
142 * Register a new index.
144 int crypto_get_ex_new_index_ex(OPENSSL_CTX *ctx, int class_index, long argl,
145 void *argp, CRYPTO_EX_new *new_func,
146 CRYPTO_EX_dup *dup_func,
147 CRYPTO_EX_free *free_func)
152 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
157 ip = get_and_lock(ctx, class_index);
161 if (ip->meth == NULL) {
162 ip->meth = sk_EX_CALLBACK_new_null();
163 /* We push an initial value on the stack because the SSL
164 * "app_data" routines use ex_data index zero. See RT 3710. */
166 || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
167 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
172 a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
174 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
179 a->new_func = new_func;
180 a->dup_func = dup_func;
181 a->free_func = free_func;
183 if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
184 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, 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 crypto_get_ex_new_index_ex(NULL, class_index, argl, argp, new_func,
201 dup_func, free_func);
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 crypto_new_ex_data_ex(OPENSSL_CTX *ctx, int class_index, void *obj,
216 EX_CALLBACK **storage = NULL;
217 EX_CALLBACK *stack[10];
219 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
224 ip = get_and_lock(ctx, class_index);
231 mx = sk_EX_CALLBACK_num(ip->meth);
233 if (mx < (int)OSSL_NELEM(stack))
236 storage = OPENSSL_malloc(sizeof(*storage) * mx);
238 for (i = 0; i < mx; i++)
239 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
241 CRYPTO_THREAD_unlock(global->ex_data_lock);
243 if (mx > 0 && storage == NULL) {
244 CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA_EX, ERR_R_MALLOC_FAILURE);
247 for (i = 0; i < mx; i++) {
248 if (storage[i] != NULL && storage[i]->new_func != NULL) {
249 ptr = CRYPTO_get_ex_data(ad, i);
250 storage[i]->new_func(obj, ptr, ad, i,
251 storage[i]->argl, storage[i]->argp);
254 if (storage != stack)
255 OPENSSL_free(storage);
259 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
261 return crypto_new_ex_data_ex(NULL, class_index, obj, ad);
265 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
266 * for each index in the class used by this variable
268 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
269 const CRYPTO_EX_DATA *from)
273 EX_CALLBACK *stack[10];
274 EX_CALLBACK **storage = NULL;
277 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(from->ctx);
283 if (from->sk == NULL)
284 /* Nothing to copy over */
286 if ((ip = get_and_lock(from->ctx, class_index)) == NULL)
289 mx = sk_EX_CALLBACK_num(ip->meth);
290 j = sk_void_num(from->sk);
294 if (mx < (int)OSSL_NELEM(stack))
297 storage = OPENSSL_malloc(sizeof(*storage) * mx);
299 for (i = 0; i < mx; i++)
300 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
302 CRYPTO_THREAD_unlock(global->ex_data_lock);
306 if (storage == NULL) {
307 CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
311 * Make sure the ex_data stack is at least |mx| elements long to avoid
312 * issues in the for loop that follows; so go get the |mx|'th element
313 * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
314 * to itself. This is normally a no-op; but ensures the stack is the
317 if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
320 for (i = 0; i < mx; i++) {
321 ptr = CRYPTO_get_ex_data(from, i);
322 if (storage[i] != NULL && storage[i]->dup_func != NULL)
323 if (!storage[i]->dup_func(to, from, &ptr, i,
324 storage[i]->argl, storage[i]->argp))
326 CRYPTO_set_ex_data(to, i, ptr);
330 if (storage != stack)
331 OPENSSL_free(storage);
337 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
338 * each index in the class used by this variable
340 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
346 EX_CALLBACK *stack[10];
347 EX_CALLBACK **storage = NULL;
348 OSSL_EX_DATA_GLOBAL *global;
350 if ((ip = get_and_lock(ad->ctx, class_index)) == NULL)
352 global = openssl_ctx_get_ex_data_global(ad->ctx);
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 CRYPTO_THREAD_write_lock(global->ex_data_lock);
373 f = sk_EX_CALLBACK_value(ip->meth, i);
374 CRYPTO_THREAD_unlock(global->ex_data_lock);
376 if (f != NULL && f->free_func != NULL) {
377 ptr = CRYPTO_get_ex_data(ad, i);
378 f->free_func(obj, ptr, ad, i, f->argl, f->argp);
382 if (storage != stack)
383 OPENSSL_free(storage);
385 sk_void_free(ad->sk);
391 * Allocate a given CRYPTO_EX_DATA item using the class specific allocation
394 int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad,
400 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ad->ctx);
405 curval = CRYPTO_get_ex_data(ad, idx);
407 /* Already there, no need to allocate */
411 ip = get_and_lock(ad->ctx, class_index);
414 f = sk_EX_CALLBACK_value(ip->meth, idx);
415 CRYPTO_THREAD_unlock(global->ex_data_lock);
418 * This should end up calling CRYPTO_set_ex_data(), which allocates
419 * everything necessary to support placing the new data in the right spot.
421 if (f->new_func == NULL)
424 f->new_func(obj, curval, ad, idx, f->argl, f->argp);
430 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
431 * particular index in the class used by this variable
433 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
437 if (ad->sk == NULL) {
438 if ((ad->sk = sk_void_new_null()) == NULL) {
439 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
444 for (i = sk_void_num(ad->sk); i <= idx; ++i) {
445 if (!sk_void_push(ad->sk, NULL)) {
446 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
450 sk_void_set(ad->sk, idx, val);
455 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
456 * particular index in the class used by this variable
458 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
460 if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
462 return sk_void_value(ad->sk, idx);
465 OPENSSL_CTX *crypto_ex_data_get_openssl_ctx(const CRYPTO_EX_DATA *ad)