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 <openssl/lhash.h>
14 * Each structure type (sometimes called a class), that supports
15 * exdata has a stack of callbacks for each instance.
17 struct ex_callback_st {
18 long argl; /* Arbitrary long */
19 void *argp; /* Arbitrary void * */
20 CRYPTO_EX_new *new_func;
21 CRYPTO_EX_free *free_func;
22 CRYPTO_EX_dup *dup_func;
26 * The state for each class. This could just be a typedef, but
27 * a structure allows future changes.
29 typedef struct ex_callbacks_st {
30 STACK_OF(EX_CALLBACK) *meth;
33 static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
35 static CRYPTO_RWLOCK *ex_data_lock = NULL;
36 static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT;
38 static void do_ex_data_init(void)
40 ex_data_lock = CRYPTO_THREAD_lock_new();
44 * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
45 * a given class. On success, *holds the lock.*
47 static EX_CALLBACKS *get_and_lock(int class_index)
51 if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
52 CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);
56 CRYPTO_THREAD_run_once(&ex_data_init, do_ex_data_init);
58 if (ex_data_lock == NULL) {
60 * This can happen in normal operation when using CRYPTO_mem_leaks().
61 * The CRYPTO_mem_leaks() function calls OPENSSL_cleanup() which cleans
62 * up the locks. Subsequently the BIO that CRYPTO_mem_leaks() uses gets
63 * freed, which also attempts to free the ex_data. However
64 * CRYPTO_mem_leaks() ensures that the ex_data is freed early (i.e.
65 * before OPENSSL_cleanup() is called), so if we get here we can safely
66 * ignore this operation. We just treat it as an error.
71 ip = &ex_data[class_index];
72 CRYPTO_THREAD_write_lock(ex_data_lock);
76 static void cleanup_cb(EX_CALLBACK *funcs)
82 * Release all "ex_data" state to prevent memory leaks. This can't be made
83 * thread-safe without overhauling a lot of stuff, and shouldn't really be
84 * called under potential race-conditions anyway (it's for program shutdown
87 void crypto_cleanup_all_ex_data_int(void)
91 for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
92 EX_CALLBACKS *ip = &ex_data[i];
94 sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
98 CRYPTO_THREAD_lock_free(ex_data_lock);
104 * Unregister a new index by replacing the callbacks with no-ops.
105 * Any in-use instances are leaked.
107 static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
108 long argl, void *argp)
112 static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
113 long argl, void *argp)
117 static int dummy_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from,
118 void *from_d, int idx,
119 long argl, void *argp)
124 int CRYPTO_free_ex_index(int class_index, int idx)
126 EX_CALLBACKS *ip = get_and_lock(class_index);
132 if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
134 a = sk_EX_CALLBACK_value(ip->meth, idx);
137 a->new_func = dummy_new;
138 a->dup_func = dummy_dup;
139 a->free_func = dummy_free;
142 CRYPTO_THREAD_unlock(ex_data_lock);
147 * Register a new index.
149 int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
150 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
151 CRYPTO_EX_free *free_func)
155 EX_CALLBACKS *ip = get_and_lock(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 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
171 a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
173 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
178 a->new_func = new_func;
179 a->dup_func = dup_func;
180 a->free_func = free_func;
182 if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
183 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
187 toret = sk_EX_CALLBACK_num(ip->meth) - 1;
188 (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
191 CRYPTO_THREAD_unlock(ex_data_lock);
196 * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
197 * calling new() callbacks for each index in the class used by this variable
198 * Thread-safe by copying a class's array of "EX_CALLBACK" entries
199 * in the lock, then using them outside the lock. Note this only applies
200 * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
202 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
206 EX_CALLBACK **storage = NULL;
207 EX_CALLBACK *stack[10];
208 EX_CALLBACKS *ip = get_and_lock(class_index);
215 mx = sk_EX_CALLBACK_num(ip->meth);
217 if (mx < (int)OSSL_NELEM(stack))
220 storage = OPENSSL_malloc(sizeof(*storage) * mx);
222 for (i = 0; i < mx; i++)
223 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
225 CRYPTO_THREAD_unlock(ex_data_lock);
227 if (mx > 0 && storage == NULL) {
228 CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
231 for (i = 0; i < mx; i++) {
232 if (storage[i] && storage[i]->new_func) {
233 ptr = CRYPTO_get_ex_data(ad, i);
234 storage[i]->new_func(obj, ptr, ad, i,
235 storage[i]->argl, storage[i]->argp);
238 if (storage != stack)
239 OPENSSL_free(storage);
244 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
245 * for each index in the class used by this variable
247 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
248 CRYPTO_EX_DATA *from)
252 EX_CALLBACK *stack[10];
253 EX_CALLBACK **storage = NULL;
256 if (from->sk == NULL)
257 /* Nothing to copy over */
259 if ((ip = get_and_lock(class_index)) == NULL)
262 mx = sk_EX_CALLBACK_num(ip->meth);
263 j = sk_void_num(from->sk);
267 if (mx < (int)OSSL_NELEM(stack))
270 storage = OPENSSL_malloc(sizeof(*storage) * mx);
272 for (i = 0; i < mx; i++)
273 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
275 CRYPTO_THREAD_unlock(ex_data_lock);
277 if (mx > 0 && storage == NULL) {
278 CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
282 for (i = 0; i < mx; i++) {
283 ptr = CRYPTO_get_ex_data(from, i);
284 if (storage[i] && storage[i]->dup_func)
285 storage[i]->dup_func(to, from, &ptr, i,
286 storage[i]->argl, storage[i]->argp);
287 CRYPTO_set_ex_data(to, i, ptr);
289 if (storage != stack)
290 OPENSSL_free(storage);
296 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
297 * each index in the class used by this variable
299 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
304 EX_CALLBACK *stack[10];
305 EX_CALLBACK **storage = NULL;
307 if ((ip = get_and_lock(class_index)) == NULL)
310 mx = sk_EX_CALLBACK_num(ip->meth);
312 if (mx < (int)OSSL_NELEM(stack))
315 storage = OPENSSL_malloc(sizeof(*storage) * mx);
317 for (i = 0; i < mx; i++)
318 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
320 CRYPTO_THREAD_unlock(ex_data_lock);
322 if (mx > 0 && storage == NULL) {
323 CRYPTOerr(CRYPTO_F_CRYPTO_FREE_EX_DATA, ERR_R_MALLOC_FAILURE);
326 for (i = 0; i < mx; i++) {
327 if (storage[i] && storage[i]->free_func) {
328 ptr = CRYPTO_get_ex_data(ad, i);
329 storage[i]->free_func(obj, ptr, ad, i,
330 storage[i]->argl, storage[i]->argp);
334 if (storage != stack)
335 OPENSSL_free(storage);
336 sk_void_free(ad->sk);
341 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
342 * particular index in the class used by this variable
344 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
348 if (ad->sk == NULL) {
349 if ((ad->sk = sk_void_new_null()) == NULL) {
350 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
355 for (i = sk_void_num(ad->sk); i <= idx; ++i) {
356 if (!sk_void_push(ad->sk, NULL)) {
357 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
361 sk_void_set(ad->sk, idx, val);
366 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
367 * particular index in the class used by this variable
369 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
371 if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
373 return sk_void_value(ad->sk, idx);