2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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"
12 #include <openssl/lhash.h>
15 * Each structure type (sometimes called a class), that supports
16 * exdata has a stack of callbacks for each instance.
18 struct ex_callback_st {
19 long argl; /* Arbitrary long */
20 void *argp; /* Arbitrary void * */
21 CRYPTO_EX_new *new_func;
22 CRYPTO_EX_free *free_func;
23 CRYPTO_EX_dup *dup_func;
27 * The state for each class. This could just be a typedef, but
28 * a structure allows future changes.
30 typedef struct ex_callbacks_st {
31 STACK_OF(EX_CALLBACK) *meth;
34 static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
36 static CRYPTO_RWLOCK *ex_data_lock = NULL;
37 static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT;
39 DEFINE_RUN_ONCE_STATIC(do_ex_data_init)
41 if (!OPENSSL_init_crypto(0, NULL))
43 ex_data_lock = CRYPTO_THREAD_lock_new();
44 return ex_data_lock != NULL;
48 * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
49 * a given class. On success, *holds the lock.*
51 static EX_CALLBACKS *get_and_lock(int class_index)
55 if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
56 CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);
60 if (!RUN_ONCE(&ex_data_init, do_ex_data_init)) {
61 CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
65 if (ex_data_lock == NULL) {
67 * This can happen in normal operation when using CRYPTO_mem_leaks().
68 * The CRYPTO_mem_leaks() function calls OPENSSL_cleanup() which cleans
69 * up the locks. Subsequently the BIO that CRYPTO_mem_leaks() uses gets
70 * freed, which also attempts to free the ex_data. However
71 * CRYPTO_mem_leaks() ensures that the ex_data is freed early (i.e.
72 * before OPENSSL_cleanup() is called), so if we get here we can safely
73 * ignore this operation. We just treat it as an error.
78 ip = &ex_data[class_index];
79 CRYPTO_THREAD_write_lock(ex_data_lock);
83 static void cleanup_cb(EX_CALLBACK *funcs)
89 * Release all "ex_data" state to prevent memory leaks. This can't be made
90 * thread-safe without overhauling a lot of stuff, and shouldn't really be
91 * called under potential race-conditions anyway (it's for program shutdown
94 void crypto_cleanup_all_ex_data_int(void)
98 for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
99 EX_CALLBACKS *ip = &ex_data[i];
101 sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
105 CRYPTO_THREAD_lock_free(ex_data_lock);
111 * Unregister a new index by replacing the callbacks with no-ops.
112 * Any in-use instances are leaked.
114 static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
115 long argl, void *argp)
119 static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
120 long argl, void *argp)
124 static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
125 void *from_d, int idx,
126 long argl, void *argp)
131 int CRYPTO_free_ex_index(int class_index, int idx)
133 EX_CALLBACKS *ip = get_and_lock(class_index);
139 if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
141 a = sk_EX_CALLBACK_value(ip->meth, idx);
144 a->new_func = dummy_new;
145 a->dup_func = dummy_dup;
146 a->free_func = dummy_free;
149 CRYPTO_THREAD_unlock(ex_data_lock);
154 * Register a new index.
156 int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
157 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
158 CRYPTO_EX_free *free_func)
162 EX_CALLBACKS *ip = get_and_lock(class_index);
167 if (ip->meth == NULL) {
168 ip->meth = sk_EX_CALLBACK_new_null();
169 /* We push an initial value on the stack because the SSL
170 * "app_data" routines use ex_data index zero. See RT 3710. */
172 || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
173 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
178 a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
180 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
185 a->new_func = new_func;
186 a->dup_func = dup_func;
187 a->free_func = free_func;
189 if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
190 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
194 toret = sk_EX_CALLBACK_num(ip->meth) - 1;
195 (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
198 CRYPTO_THREAD_unlock(ex_data_lock);
203 * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
204 * calling new() callbacks for each index in the class used by this variable
205 * Thread-safe by copying a class's array of "EX_CALLBACK" entries
206 * in the lock, then using them outside the lock. Note this only applies
207 * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
209 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
213 EX_CALLBACK **storage = NULL;
214 EX_CALLBACK *stack[10];
215 EX_CALLBACKS *ip = get_and_lock(class_index);
222 mx = sk_EX_CALLBACK_num(ip->meth);
224 if (mx < (int)OSSL_NELEM(stack))
227 storage = OPENSSL_malloc(sizeof(*storage) * mx);
229 for (i = 0; i < mx; i++)
230 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
232 CRYPTO_THREAD_unlock(ex_data_lock);
234 if (mx > 0 && storage == NULL) {
235 CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
238 for (i = 0; i < mx; i++) {
239 if (storage[i] && storage[i]->new_func) {
240 ptr = CRYPTO_get_ex_data(ad, i);
241 storage[i]->new_func(obj, ptr, ad, i,
242 storage[i]->argl, storage[i]->argp);
245 if (storage != stack)
246 OPENSSL_free(storage);
251 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
252 * for each index in the class used by this variable
254 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
255 const CRYPTO_EX_DATA *from)
259 EX_CALLBACK *stack[10];
260 EX_CALLBACK **storage = NULL;
264 if (from->sk == NULL)
265 /* Nothing to copy over */
267 if ((ip = get_and_lock(class_index)) == NULL)
270 mx = sk_EX_CALLBACK_num(ip->meth);
271 j = sk_void_num(from->sk);
275 if (mx < (int)OSSL_NELEM(stack))
278 storage = OPENSSL_malloc(sizeof(*storage) * mx);
280 for (i = 0; i < mx; i++)
281 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
283 CRYPTO_THREAD_unlock(ex_data_lock);
287 if (storage == NULL) {
288 CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
292 * Make sure the ex_data stack is at least |mx| elements long to avoid
293 * issues in the for loop that follows; so go get the |mx|'th element
294 * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
295 * to itself. This is normally a no-op; but ensures the stack is the
298 if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
301 for (i = 0; i < mx; i++) {
302 ptr = CRYPTO_get_ex_data(from, i);
303 if (storage[i] && storage[i]->dup_func)
304 if (!storage[i]->dup_func(to, from, &ptr, i,
305 storage[i]->argl, storage[i]->argp))
307 CRYPTO_set_ex_data(to, i, ptr);
311 if (storage != stack)
312 OPENSSL_free(storage);
318 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
319 * each index in the class used by this variable
321 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
327 EX_CALLBACK *stack[10];
328 EX_CALLBACK **storage = NULL;
330 if ((ip = get_and_lock(class_index)) == NULL)
333 mx = sk_EX_CALLBACK_num(ip->meth);
335 if (mx < (int)OSSL_NELEM(stack))
338 storage = OPENSSL_malloc(sizeof(*storage) * mx);
340 for (i = 0; i < mx; i++)
341 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
343 CRYPTO_THREAD_unlock(ex_data_lock);
345 for (i = 0; i < mx; i++) {
349 CRYPTO_THREAD_write_lock(ex_data_lock);
350 f = sk_EX_CALLBACK_value(ip->meth, i);
351 CRYPTO_THREAD_unlock(ex_data_lock);
353 if (f != NULL && f->free_func != NULL) {
354 ptr = CRYPTO_get_ex_data(ad, i);
355 f->free_func(obj, ptr, ad, i, f->argl, f->argp);
359 if (storage != stack)
360 OPENSSL_free(storage);
362 sk_void_free(ad->sk);
367 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
368 * particular index in the class used by this variable
370 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
374 if (ad->sk == NULL) {
375 if ((ad->sk = sk_void_new_null()) == NULL) {
376 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
381 for (i = sk_void_num(ad->sk); i <= idx; ++i) {
382 if (!sk_void_push(ad->sk, NULL)) {
383 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
387 sk_void_set(ad->sk, idx, val);
392 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
393 * particular index in the class used by this variable
395 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
397 if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
399 return sk_void_value(ad->sk, idx);