1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
111 #include "internal/cryptlib_int.h"
112 #include <openssl/lhash.h>
115 * Each structure type (sometimes called a class), that supports
116 * exdata has a stack of callbacks for each instance.
118 struct ex_callback_st {
119 long argl; /* Arbitrary long */
120 void *argp; /* Arbitrary void * */
121 CRYPTO_EX_new *new_func;
122 CRYPTO_EX_free *free_func;
123 CRYPTO_EX_dup *dup_func;
127 * The state for each class. This could just be a typedef, but
128 * a structure allows future changes.
130 typedef struct ex_callbacks_st {
131 STACK_OF(EX_CALLBACK) *meth;
134 static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
136 static CRYPTO_RWLOCK *ex_data_lock = NULL;
137 static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT;
139 static void do_ex_data_init(void)
141 ex_data_lock = CRYPTO_THREAD_lock_new();
145 * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
146 * a given class. On success, *holds the lock.*
148 static EX_CALLBACKS *get_and_lock(int class_index)
152 if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
153 CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);
157 CRYPTO_THREAD_run_once(&ex_data_init, do_ex_data_init);
159 if (ex_data_lock == NULL) {
161 * This can happen in normal operation when using CRYPTO_mem_leaks().
162 * The CRYPTO_mem_leaks() function calls OPENSSL_cleanup() which cleans
163 * up the locks. Subsequently the BIO that CRYPTO_mem_leaks() uses gets
164 * freed, which also attempts to free the ex_data. However
165 * CRYPTO_mem_leaks() ensures that the ex_data is freed early (i.e.
166 * before OPENSSL_cleanup() is called), so if we get here we can safely
167 * ignore this operation. We just treat it as an error.
172 ip = &ex_data[class_index];
173 CRYPTO_THREAD_write_lock(ex_data_lock);
177 static void cleanup_cb(EX_CALLBACK *funcs)
183 * Release all "ex_data" state to prevent memory leaks. This can't be made
184 * thread-safe without overhauling a lot of stuff, and shouldn't really be
185 * called under potential race-conditions anyway (it's for program shutdown
188 void crypto_cleanup_all_ex_data_int(void)
192 for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
193 EX_CALLBACKS *ip = &ex_data[i];
195 sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
199 CRYPTO_THREAD_lock_free(ex_data_lock);
205 * Unregister a new index by replacing the callbacks with no-ops.
206 * Any in-use instances are leaked.
208 static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
209 long argl, void *argp)
213 static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
214 long argl, void *argp)
218 static int dummy_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from,
219 void *from_d, int idx,
220 long argl, void *argp)
225 int CRYPTO_free_ex_index(int class_index, int idx)
227 EX_CALLBACKS *ip = get_and_lock(class_index);
233 if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
235 a = sk_EX_CALLBACK_value(ip->meth, idx);
238 a->new_func = dummy_new;
239 a->dup_func = dummy_dup;
240 a->free_func = dummy_free;
243 CRYPTO_THREAD_unlock(ex_data_lock);
248 * Register a new index.
250 int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
251 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
252 CRYPTO_EX_free *free_func)
256 EX_CALLBACKS *ip = get_and_lock(class_index);
261 if (ip->meth == NULL) {
262 ip->meth = sk_EX_CALLBACK_new_null();
263 /* We push an initial value on the stack because the SSL
264 * "app_data" routines use ex_data index zero. See RT 3710. */
266 || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
267 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
272 a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
274 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
279 a->new_func = new_func;
280 a->dup_func = dup_func;
281 a->free_func = free_func;
283 if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
284 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
288 toret = sk_EX_CALLBACK_num(ip->meth) - 1;
289 (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
292 CRYPTO_THREAD_unlock(ex_data_lock);
297 * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
298 * calling new() callbacks for each index in the class used by this variable
299 * Thread-safe by copying a class's array of "EX_CALLBACK" entries
300 * in the lock, then using them outside the lock. Note this only applies
301 * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
303 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
307 EX_CALLBACK **storage = NULL;
308 EX_CALLBACK *stack[10];
309 EX_CALLBACKS *ip = get_and_lock(class_index);
316 mx = sk_EX_CALLBACK_num(ip->meth);
318 if (mx < (int)OSSL_NELEM(stack))
321 storage = OPENSSL_malloc(sizeof(*storage) * mx);
323 for (i = 0; i < mx; i++)
324 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
326 CRYPTO_THREAD_unlock(ex_data_lock);
328 if (mx > 0 && storage == NULL) {
329 CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
332 for (i = 0; i < mx; i++) {
333 if (storage[i] && storage[i]->new_func) {
334 ptr = CRYPTO_get_ex_data(ad, i);
335 storage[i]->new_func(obj, ptr, ad, i,
336 storage[i]->argl, storage[i]->argp);
339 if (storage != stack)
340 OPENSSL_free(storage);
345 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
346 * for each index in the class used by this variable
348 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
349 CRYPTO_EX_DATA *from)
353 EX_CALLBACK *stack[10];
354 EX_CALLBACK **storage = NULL;
357 if (from->sk == NULL)
358 /* Nothing to copy over */
360 if ((ip = get_and_lock(class_index)) == NULL)
363 mx = sk_EX_CALLBACK_num(ip->meth);
364 j = sk_void_num(from->sk);
368 if (mx < (int)OSSL_NELEM(stack))
371 storage = OPENSSL_malloc(sizeof(*storage) * mx);
373 for (i = 0; i < mx; i++)
374 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
376 CRYPTO_THREAD_unlock(ex_data_lock);
378 if (mx > 0 && storage == NULL) {
379 CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
383 for (i = 0; i < mx; i++) {
384 ptr = CRYPTO_get_ex_data(from, i);
385 if (storage[i] && storage[i]->dup_func)
386 storage[i]->dup_func(to, from, &ptr, i,
387 storage[i]->argl, storage[i]->argp);
388 CRYPTO_set_ex_data(to, i, ptr);
390 if (storage != stack)
391 OPENSSL_free(storage);
397 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
398 * each index in the class used by this variable
400 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
405 EX_CALLBACK *stack[10];
406 EX_CALLBACK **storage = NULL;
408 if ((ip = get_and_lock(class_index)) == NULL)
411 mx = sk_EX_CALLBACK_num(ip->meth);
413 if (mx < (int)OSSL_NELEM(stack))
416 storage = OPENSSL_malloc(sizeof(*storage) * mx);
418 for (i = 0; i < mx; i++)
419 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
421 CRYPTO_THREAD_unlock(ex_data_lock);
423 if (mx > 0 && storage == NULL) {
424 CRYPTOerr(CRYPTO_F_CRYPTO_FREE_EX_DATA, ERR_R_MALLOC_FAILURE);
427 for (i = 0; i < mx; i++) {
428 if (storage[i] && storage[i]->free_func) {
429 ptr = CRYPTO_get_ex_data(ad, i);
430 storage[i]->free_func(obj, ptr, ad, i,
431 storage[i]->argl, storage[i]->argp);
435 if (storage != stack)
436 OPENSSL_free(storage);
437 sk_void_free(ad->sk);
442 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
443 * particular index in the class used by this variable
445 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
449 if (ad->sk == NULL) {
450 if ((ad->sk = sk_void_new_null()) == NULL) {
451 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
456 for (i = sk_void_num(ad->sk); i <= idx; ++i) {
457 if (!sk_void_push(ad->sk, NULL)) {
458 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
462 sk_void_set(ad->sk, idx, val);
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);