1ebfedea0SLionel Sambuc /* crypto/ex_data.c */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Overhaul notes;
5ebfedea0SLionel Sambuc *
6ebfedea0SLionel Sambuc * This code is now *mostly* thread-safe. It is now easier to understand in what
7ebfedea0SLionel Sambuc * ways it is safe and in what ways it is not, which is an improvement. Firstly,
8ebfedea0SLionel Sambuc * all per-class stacks and index-counters for ex_data are stored in the same
9ebfedea0SLionel Sambuc * global LHASH table (keyed by class). This hash table uses locking for all
10ebfedea0SLionel Sambuc * access with the exception of CRYPTO_cleanup_all_ex_data(), which must only be
11ebfedea0SLionel Sambuc * called when no other threads can possibly race against it (even if it was
12ebfedea0SLionel Sambuc * locked, the race would mean it's possible the hash table might have been
13ebfedea0SLionel Sambuc * recreated after the cleanup). As classes can only be added to the hash table,
14ebfedea0SLionel Sambuc * and within each class, the stack of methods can only be incremented, the
15ebfedea0SLionel Sambuc * locking mechanics are simpler than they would otherwise be. For example, the
16ebfedea0SLionel Sambuc * new/dup/free ex_data functions will lock the hash table, copy the method
17ebfedea0SLionel Sambuc * pointers it needs from the relevant class, then unlock the hash table before
18ebfedea0SLionel Sambuc * actually applying those method pointers to the task of the new/dup/free
19ebfedea0SLionel Sambuc * operations. As they can't be removed from the method-stack, only
20ebfedea0SLionel Sambuc * supplemented, there's no race conditions associated with using them outside
21ebfedea0SLionel Sambuc * the lock. The get/set_ex_data functions are not locked because they do not
22ebfedea0SLionel Sambuc * involve this global state at all - they operate directly with a previously
23ebfedea0SLionel Sambuc * obtained per-class method index and a particular "ex_data" variable. These
24ebfedea0SLionel Sambuc * variables are usually instantiated per-context (eg. each RSA structure has
25ebfedea0SLionel Sambuc * one) so locking on read/write access to that variable can be locked locally
26ebfedea0SLionel Sambuc * if required (eg. using the "RSA" lock to synchronise access to a
27ebfedea0SLionel Sambuc * per-RSA-structure ex_data variable if required).
28ebfedea0SLionel Sambuc * [Geoff]
29ebfedea0SLionel Sambuc */
30ebfedea0SLionel Sambuc
31ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
32ebfedea0SLionel Sambuc * All rights reserved.
33ebfedea0SLionel Sambuc *
34ebfedea0SLionel Sambuc * This package is an SSL implementation written
35ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
36ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
37ebfedea0SLionel Sambuc *
38ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
39ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
40ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
41ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
42ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
43ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
44ebfedea0SLionel Sambuc *
45ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
46ebfedea0SLionel Sambuc * the code are not to be removed.
47ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
48ebfedea0SLionel Sambuc * as the author of the parts of the library used.
49ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
50ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
51ebfedea0SLionel Sambuc *
52ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
53ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
54ebfedea0SLionel Sambuc * are met:
55ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
56ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
57ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
58ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
59ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
60ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
61ebfedea0SLionel Sambuc * must display the following acknowledgement:
62ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
63ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
64ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
65ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
66ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
67ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
68ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
69ebfedea0SLionel Sambuc *
70ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
71ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
72ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
73ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
74ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
75ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
76ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
77ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
78ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
79ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
80ebfedea0SLionel Sambuc * SUCH DAMAGE.
81ebfedea0SLionel Sambuc *
82ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
83ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
84ebfedea0SLionel Sambuc * copied and put under another distribution licence
85ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
86ebfedea0SLionel Sambuc */
87ebfedea0SLionel Sambuc /* ====================================================================
88ebfedea0SLionel Sambuc * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
89ebfedea0SLionel Sambuc *
90ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
91ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
92ebfedea0SLionel Sambuc * are met:
93ebfedea0SLionel Sambuc *
94ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
95ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
96ebfedea0SLionel Sambuc *
97ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
98ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
99ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
100ebfedea0SLionel Sambuc * distribution.
101ebfedea0SLionel Sambuc *
102ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
103ebfedea0SLionel Sambuc * software must display the following acknowledgment:
104ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
105ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
106ebfedea0SLionel Sambuc *
107ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
108ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
109ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
110ebfedea0SLionel Sambuc * openssl-core@openssl.org.
111ebfedea0SLionel Sambuc *
112ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
113ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
114ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
115ebfedea0SLionel Sambuc *
116ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
117ebfedea0SLionel Sambuc * acknowledgment:
118ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
119ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
120ebfedea0SLionel Sambuc *
121ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
122ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
123ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
124ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
125ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
126ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
127ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
128ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
129ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
130ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
131ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
132ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
133ebfedea0SLionel Sambuc * ====================================================================
134ebfedea0SLionel Sambuc *
135ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
136ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
137ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
138ebfedea0SLionel Sambuc *
139ebfedea0SLionel Sambuc */
140ebfedea0SLionel Sambuc
141ebfedea0SLionel Sambuc #include "cryptlib.h"
142ebfedea0SLionel Sambuc #include <openssl/lhash.h>
143ebfedea0SLionel Sambuc
144ebfedea0SLionel Sambuc /* What an "implementation of ex_data functionality" looks like */
145*0a6a1f1dSLionel Sambuc struct st_CRYPTO_EX_DATA_IMPL {
146ebfedea0SLionel Sambuc /*********************/
147ebfedea0SLionel Sambuc /* GLOBAL OPERATIONS */
148ebfedea0SLionel Sambuc /* Return a new class index */
149ebfedea0SLionel Sambuc int (*cb_new_class) (void);
150ebfedea0SLionel Sambuc /* Cleanup all state used by the implementation */
151ebfedea0SLionel Sambuc void (*cb_cleanup) (void);
152ebfedea0SLionel Sambuc /************************/
153ebfedea0SLionel Sambuc /* PER-CLASS OPERATIONS */
154ebfedea0SLionel Sambuc /* Get a new method index within a class */
155ebfedea0SLionel Sambuc int (*cb_get_new_index) (int class_index, long argl, void *argp,
156ebfedea0SLionel Sambuc CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
157ebfedea0SLionel Sambuc CRYPTO_EX_free *free_func);
158ebfedea0SLionel Sambuc /* Initialise a new CRYPTO_EX_DATA of a given class */
159*0a6a1f1dSLionel Sambuc int (*cb_new_ex_data) (int class_index, void *obj, CRYPTO_EX_DATA *ad);
160ebfedea0SLionel Sambuc /* Duplicate a CRYPTO_EX_DATA of a given class onto a copy */
161ebfedea0SLionel Sambuc int (*cb_dup_ex_data) (int class_index, CRYPTO_EX_DATA *to,
162ebfedea0SLionel Sambuc CRYPTO_EX_DATA *from);
163ebfedea0SLionel Sambuc /* Cleanup a CRYPTO_EX_DATA of a given class */
164*0a6a1f1dSLionel Sambuc void (*cb_free_ex_data) (int class_index, void *obj, CRYPTO_EX_DATA *ad);
165ebfedea0SLionel Sambuc };
166ebfedea0SLionel Sambuc
167ebfedea0SLionel Sambuc /* The implementation we use at run-time */
168ebfedea0SLionel Sambuc static const CRYPTO_EX_DATA_IMPL *impl = NULL;
169ebfedea0SLionel Sambuc
170*0a6a1f1dSLionel Sambuc /*
171*0a6a1f1dSLionel Sambuc * To call "impl" functions, use this macro rather than referring to 'impl'
172*0a6a1f1dSLionel Sambuc * directly, eg. EX_IMPL(get_new_index)(...);
173*0a6a1f1dSLionel Sambuc */
174ebfedea0SLionel Sambuc #define EX_IMPL(a) impl->cb_##a
175ebfedea0SLionel Sambuc
176ebfedea0SLionel Sambuc /* Predeclare the "default" ex_data implementation */
177ebfedea0SLionel Sambuc static int int_new_class(void);
178ebfedea0SLionel Sambuc static void int_cleanup(void);
179ebfedea0SLionel Sambuc static int int_get_new_index(int class_index, long argl, void *argp,
180ebfedea0SLionel Sambuc CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
181ebfedea0SLionel Sambuc CRYPTO_EX_free *free_func);
182*0a6a1f1dSLionel Sambuc static int int_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
183ebfedea0SLionel Sambuc static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
184ebfedea0SLionel Sambuc CRYPTO_EX_DATA *from);
185*0a6a1f1dSLionel Sambuc static void int_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
186*0a6a1f1dSLionel Sambuc static CRYPTO_EX_DATA_IMPL impl_default = {
187ebfedea0SLionel Sambuc int_new_class,
188ebfedea0SLionel Sambuc int_cleanup,
189ebfedea0SLionel Sambuc int_get_new_index,
190ebfedea0SLionel Sambuc int_new_ex_data,
191ebfedea0SLionel Sambuc int_dup_ex_data,
192ebfedea0SLionel Sambuc int_free_ex_data
193ebfedea0SLionel Sambuc };
194ebfedea0SLionel Sambuc
195*0a6a1f1dSLionel Sambuc /*
196*0a6a1f1dSLionel Sambuc * Internal function that checks whether "impl" is set and if not, sets it to
197*0a6a1f1dSLionel Sambuc * the default.
198*0a6a1f1dSLionel Sambuc */
impl_check(void)199ebfedea0SLionel Sambuc static void impl_check(void)
200ebfedea0SLionel Sambuc {
201ebfedea0SLionel Sambuc CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
202ebfedea0SLionel Sambuc if (!impl)
203ebfedea0SLionel Sambuc impl = &impl_default;
204ebfedea0SLionel Sambuc CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
205ebfedea0SLionel Sambuc }
206*0a6a1f1dSLionel Sambuc
207*0a6a1f1dSLionel Sambuc /*
208*0a6a1f1dSLionel Sambuc * A macro wrapper for impl_check that first uses a non-locked test before
209*0a6a1f1dSLionel Sambuc * invoking the function (which checks again inside a lock).
210*0a6a1f1dSLionel Sambuc */
211ebfedea0SLionel Sambuc #define IMPL_CHECK if(!impl) impl_check();
212ebfedea0SLionel Sambuc
213ebfedea0SLionel Sambuc /* API functions to get/set the "ex_data" implementation */
CRYPTO_get_ex_data_implementation(void)214ebfedea0SLionel Sambuc const CRYPTO_EX_DATA_IMPL *CRYPTO_get_ex_data_implementation(void)
215ebfedea0SLionel Sambuc {
216*0a6a1f1dSLionel Sambuc IMPL_CHECK return impl;
217ebfedea0SLionel Sambuc }
218*0a6a1f1dSLionel Sambuc
CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL * i)219ebfedea0SLionel Sambuc int CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i)
220ebfedea0SLionel Sambuc {
221ebfedea0SLionel Sambuc int toret = 0;
222ebfedea0SLionel Sambuc CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
223*0a6a1f1dSLionel Sambuc if (!impl) {
224ebfedea0SLionel Sambuc impl = i;
225ebfedea0SLionel Sambuc toret = 1;
226ebfedea0SLionel Sambuc }
227ebfedea0SLionel Sambuc CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
228ebfedea0SLionel Sambuc return toret;
229ebfedea0SLionel Sambuc }
230ebfedea0SLionel Sambuc
231ebfedea0SLionel Sambuc /****************************************************************************/
232*0a6a1f1dSLionel Sambuc /*
233*0a6a1f1dSLionel Sambuc * Interal (default) implementation of "ex_data" support. API functions are
234*0a6a1f1dSLionel Sambuc * further down.
235*0a6a1f1dSLionel Sambuc */
236ebfedea0SLionel Sambuc
237*0a6a1f1dSLionel Sambuc /*
238*0a6a1f1dSLionel Sambuc * The type that represents what each "class" used to implement locally. A
239*0a6a1f1dSLionel Sambuc * STACK of CRYPTO_EX_DATA_FUNCS plus a index-counter. The 'class_index' is
240*0a6a1f1dSLionel Sambuc * the global value representing the class that is used to distinguish these
241*0a6a1f1dSLionel Sambuc * items.
242*0a6a1f1dSLionel Sambuc */
243ebfedea0SLionel Sambuc typedef struct st_ex_class_item {
244ebfedea0SLionel Sambuc int class_index;
245ebfedea0SLionel Sambuc STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
246ebfedea0SLionel Sambuc int meth_num;
247ebfedea0SLionel Sambuc } EX_CLASS_ITEM;
248ebfedea0SLionel Sambuc
249ebfedea0SLionel Sambuc /* When assigning new class indexes, this is our counter */
250ebfedea0SLionel Sambuc static int ex_class = CRYPTO_EX_INDEX_USER;
251ebfedea0SLionel Sambuc
252ebfedea0SLionel Sambuc /* The global hash table of EX_CLASS_ITEM items */
253ebfedea0SLionel Sambuc DECLARE_LHASH_OF(EX_CLASS_ITEM);
254ebfedea0SLionel Sambuc static LHASH_OF(EX_CLASS_ITEM) *ex_data = NULL;
255ebfedea0SLionel Sambuc
256ebfedea0SLionel Sambuc /* The callbacks required in the "ex_data" hash table */
ex_class_item_hash(const EX_CLASS_ITEM * a)257ebfedea0SLionel Sambuc static unsigned long ex_class_item_hash(const EX_CLASS_ITEM *a)
258ebfedea0SLionel Sambuc {
259ebfedea0SLionel Sambuc return a->class_index;
260ebfedea0SLionel Sambuc }
261*0a6a1f1dSLionel Sambuc
IMPLEMENT_LHASH_HASH_FN(ex_class_item,EX_CLASS_ITEM)262ebfedea0SLionel Sambuc static IMPLEMENT_LHASH_HASH_FN(ex_class_item, EX_CLASS_ITEM)
263ebfedea0SLionel Sambuc
264ebfedea0SLionel Sambuc static int ex_class_item_cmp(const EX_CLASS_ITEM *a, const EX_CLASS_ITEM *b)
265ebfedea0SLionel Sambuc {
266ebfedea0SLionel Sambuc return a->class_index - b->class_index;
267ebfedea0SLionel Sambuc }
268*0a6a1f1dSLionel Sambuc
IMPLEMENT_LHASH_COMP_FN(ex_class_item,EX_CLASS_ITEM)269ebfedea0SLionel Sambuc static IMPLEMENT_LHASH_COMP_FN(ex_class_item, EX_CLASS_ITEM)
270ebfedea0SLionel Sambuc
271*0a6a1f1dSLionel Sambuc /*
272*0a6a1f1dSLionel Sambuc * Internal functions used by the "impl_default" implementation to access the
273*0a6a1f1dSLionel Sambuc * state
274*0a6a1f1dSLionel Sambuc */
275ebfedea0SLionel Sambuc static int ex_data_check(void)
276ebfedea0SLionel Sambuc {
277ebfedea0SLionel Sambuc int toret = 1;
278ebfedea0SLionel Sambuc CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
279*0a6a1f1dSLionel Sambuc if (!ex_data && (ex_data = lh_EX_CLASS_ITEM_new()) == NULL)
280ebfedea0SLionel Sambuc toret = 0;
281ebfedea0SLionel Sambuc CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
282ebfedea0SLionel Sambuc return toret;
283ebfedea0SLionel Sambuc }
284*0a6a1f1dSLionel Sambuc
285*0a6a1f1dSLionel Sambuc /*
286*0a6a1f1dSLionel Sambuc * This macros helps reduce the locking from repeated checks because the
287*0a6a1f1dSLionel Sambuc * ex_data_check() function checks ex_data again inside a lock.
288*0a6a1f1dSLionel Sambuc */
289ebfedea0SLionel Sambuc #define EX_DATA_CHECK(iffail) if(!ex_data && !ex_data_check()) {iffail}
290ebfedea0SLionel Sambuc
291ebfedea0SLionel Sambuc /* This "inner" callback is used by the callback function that follows it */
def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS * funcs)292ebfedea0SLionel Sambuc static void def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS *funcs)
293ebfedea0SLionel Sambuc {
294ebfedea0SLionel Sambuc OPENSSL_free(funcs);
295ebfedea0SLionel Sambuc }
296ebfedea0SLionel Sambuc
297*0a6a1f1dSLionel Sambuc /*
298*0a6a1f1dSLionel Sambuc * This callback is used in lh_doall to destroy all EX_CLASS_ITEM values from
299*0a6a1f1dSLionel Sambuc * "ex_data" prior to the ex_data hash table being itself destroyed. Doesn't
300*0a6a1f1dSLionel Sambuc * do any locking.
301*0a6a1f1dSLionel Sambuc */
def_cleanup_cb(void * a_void)302ebfedea0SLionel Sambuc static void def_cleanup_cb(void *a_void)
303ebfedea0SLionel Sambuc {
304ebfedea0SLionel Sambuc EX_CLASS_ITEM *item = (EX_CLASS_ITEM *)a_void;
305ebfedea0SLionel Sambuc sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, def_cleanup_util_cb);
306ebfedea0SLionel Sambuc OPENSSL_free(item);
307ebfedea0SLionel Sambuc }
308ebfedea0SLionel Sambuc
309*0a6a1f1dSLionel Sambuc /*
310*0a6a1f1dSLionel Sambuc * Return the EX_CLASS_ITEM from the "ex_data" hash table that corresponds to
311*0a6a1f1dSLionel Sambuc * a given class. Handles locking.
312*0a6a1f1dSLionel Sambuc */
def_get_class(int class_index)313ebfedea0SLionel Sambuc static EX_CLASS_ITEM *def_get_class(int class_index)
314ebfedea0SLionel Sambuc {
315ebfedea0SLionel Sambuc EX_CLASS_ITEM d, *p, *gen;
316ebfedea0SLionel Sambuc EX_DATA_CHECK(return NULL;)
317ebfedea0SLionel Sambuc d.class_index = class_index;
318ebfedea0SLionel Sambuc CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
319ebfedea0SLionel Sambuc p = lh_EX_CLASS_ITEM_retrieve(ex_data, &d);
320*0a6a1f1dSLionel Sambuc if (!p) {
321ebfedea0SLionel Sambuc gen = OPENSSL_malloc(sizeof(EX_CLASS_ITEM));
322*0a6a1f1dSLionel Sambuc if (gen) {
323ebfedea0SLionel Sambuc gen->class_index = class_index;
324ebfedea0SLionel Sambuc gen->meth_num = 0;
325ebfedea0SLionel Sambuc gen->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null();
326ebfedea0SLionel Sambuc if (!gen->meth)
327ebfedea0SLionel Sambuc OPENSSL_free(gen);
328*0a6a1f1dSLionel Sambuc else {
329*0a6a1f1dSLionel Sambuc /*
330*0a6a1f1dSLionel Sambuc * Because we're inside the ex_data lock, the return value
331*0a6a1f1dSLionel Sambuc * from the insert will be NULL
332*0a6a1f1dSLionel Sambuc */
333ebfedea0SLionel Sambuc (void)lh_EX_CLASS_ITEM_insert(ex_data, gen);
334ebfedea0SLionel Sambuc p = gen;
335ebfedea0SLionel Sambuc }
336ebfedea0SLionel Sambuc }
337ebfedea0SLionel Sambuc }
338ebfedea0SLionel Sambuc CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
339ebfedea0SLionel Sambuc if (!p)
340ebfedea0SLionel Sambuc CRYPTOerr(CRYPTO_F_DEF_GET_CLASS, ERR_R_MALLOC_FAILURE);
341ebfedea0SLionel Sambuc return p;
342ebfedea0SLionel Sambuc }
343ebfedea0SLionel Sambuc
344*0a6a1f1dSLionel Sambuc /*
345*0a6a1f1dSLionel Sambuc * Add a new method to the given EX_CLASS_ITEM and return the corresponding
346*0a6a1f1dSLionel Sambuc * index (or -1 for error). Handles locking.
347*0a6a1f1dSLionel Sambuc */
def_add_index(EX_CLASS_ITEM * item,long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)348ebfedea0SLionel Sambuc static int def_add_index(EX_CLASS_ITEM *item, long argl, void *argp,
349ebfedea0SLionel Sambuc CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
350ebfedea0SLionel Sambuc CRYPTO_EX_free *free_func)
351ebfedea0SLionel Sambuc {
352ebfedea0SLionel Sambuc int toret = -1;
353*0a6a1f1dSLionel Sambuc CRYPTO_EX_DATA_FUNCS *a =
354*0a6a1f1dSLionel Sambuc (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS));
355*0a6a1f1dSLionel Sambuc if (!a) {
356ebfedea0SLionel Sambuc CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX, ERR_R_MALLOC_FAILURE);
357ebfedea0SLionel Sambuc return -1;
358ebfedea0SLionel Sambuc }
359ebfedea0SLionel Sambuc a->argl = argl;
360ebfedea0SLionel Sambuc a->argp = argp;
361ebfedea0SLionel Sambuc a->new_func = new_func;
362ebfedea0SLionel Sambuc a->dup_func = dup_func;
363ebfedea0SLionel Sambuc a->free_func = free_func;
364ebfedea0SLionel Sambuc CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
365*0a6a1f1dSLionel Sambuc while (sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) <= item->meth_num) {
366*0a6a1f1dSLionel Sambuc if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, NULL)) {
367ebfedea0SLionel Sambuc CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX, ERR_R_MALLOC_FAILURE);
368ebfedea0SLionel Sambuc OPENSSL_free(a);
369ebfedea0SLionel Sambuc goto err;
370ebfedea0SLionel Sambuc }
371ebfedea0SLionel Sambuc }
372ebfedea0SLionel Sambuc toret = item->meth_num++;
373ebfedea0SLionel Sambuc (void)sk_CRYPTO_EX_DATA_FUNCS_set(item->meth, toret, a);
374ebfedea0SLionel Sambuc err:
375ebfedea0SLionel Sambuc CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
376ebfedea0SLionel Sambuc return toret;
377ebfedea0SLionel Sambuc }
378ebfedea0SLionel Sambuc
379ebfedea0SLionel Sambuc /**************************************************************/
380ebfedea0SLionel Sambuc /* The functions in the default CRYPTO_EX_DATA_IMPL structure */
381ebfedea0SLionel Sambuc
int_new_class(void)382ebfedea0SLionel Sambuc static int int_new_class(void)
383ebfedea0SLionel Sambuc {
384ebfedea0SLionel Sambuc int toret;
385ebfedea0SLionel Sambuc CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
386ebfedea0SLionel Sambuc toret = ex_class++;
387ebfedea0SLionel Sambuc CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
388ebfedea0SLionel Sambuc return toret;
389ebfedea0SLionel Sambuc }
390ebfedea0SLionel Sambuc
int_cleanup(void)391ebfedea0SLionel Sambuc static void int_cleanup(void)
392ebfedea0SLionel Sambuc {
393ebfedea0SLionel Sambuc EX_DATA_CHECK(return;)
394ebfedea0SLionel Sambuc lh_EX_CLASS_ITEM_doall(ex_data, def_cleanup_cb);
395ebfedea0SLionel Sambuc lh_EX_CLASS_ITEM_free(ex_data);
396ebfedea0SLionel Sambuc ex_data = NULL;
397ebfedea0SLionel Sambuc impl = NULL;
398ebfedea0SLionel Sambuc }
399ebfedea0SLionel Sambuc
int_get_new_index(int class_index,long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)400ebfedea0SLionel Sambuc static int int_get_new_index(int class_index, long argl, void *argp,
401ebfedea0SLionel Sambuc CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
402ebfedea0SLionel Sambuc CRYPTO_EX_free *free_func)
403ebfedea0SLionel Sambuc {
404ebfedea0SLionel Sambuc EX_CLASS_ITEM *item = def_get_class(class_index);
405ebfedea0SLionel Sambuc if (!item)
406ebfedea0SLionel Sambuc return -1;
407ebfedea0SLionel Sambuc return def_add_index(item, argl, argp, new_func, dup_func, free_func);
408ebfedea0SLionel Sambuc }
409ebfedea0SLionel Sambuc
410*0a6a1f1dSLionel Sambuc /*
411*0a6a1f1dSLionel Sambuc * Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries
412*0a6a1f1dSLionel Sambuc * in the lock, then using them outside the lock. NB: Thread-safety only
413*0a6a1f1dSLionel Sambuc * applies to the global "ex_data" state (ie. class definitions), not
414*0a6a1f1dSLionel Sambuc * thread-safe on 'ad' itself.
415*0a6a1f1dSLionel Sambuc */
int_new_ex_data(int class_index,void * obj,CRYPTO_EX_DATA * ad)416*0a6a1f1dSLionel Sambuc static int int_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
417ebfedea0SLionel Sambuc {
418ebfedea0SLionel Sambuc int mx, i;
419ebfedea0SLionel Sambuc void *ptr;
420ebfedea0SLionel Sambuc CRYPTO_EX_DATA_FUNCS **storage = NULL;
421ebfedea0SLionel Sambuc EX_CLASS_ITEM *item = def_get_class(class_index);
422ebfedea0SLionel Sambuc if (!item)
423ebfedea0SLionel Sambuc /* error is already set */
424ebfedea0SLionel Sambuc return 0;
425ebfedea0SLionel Sambuc ad->sk = NULL;
426ebfedea0SLionel Sambuc CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
427ebfedea0SLionel Sambuc mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
428*0a6a1f1dSLionel Sambuc if (mx > 0) {
429ebfedea0SLionel Sambuc storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS *));
430ebfedea0SLionel Sambuc if (!storage)
431ebfedea0SLionel Sambuc goto skip;
432ebfedea0SLionel Sambuc for (i = 0; i < mx; i++)
433ebfedea0SLionel Sambuc storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
434ebfedea0SLionel Sambuc }
435ebfedea0SLionel Sambuc skip:
436ebfedea0SLionel Sambuc CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
437*0a6a1f1dSLionel Sambuc if ((mx > 0) && !storage) {
438ebfedea0SLionel Sambuc CRYPTOerr(CRYPTO_F_INT_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
439ebfedea0SLionel Sambuc return 0;
440ebfedea0SLionel Sambuc }
441*0a6a1f1dSLionel Sambuc for (i = 0; i < mx; i++) {
442*0a6a1f1dSLionel Sambuc if (storage[i] && storage[i]->new_func) {
443ebfedea0SLionel Sambuc ptr = CRYPTO_get_ex_data(ad, i);
444ebfedea0SLionel Sambuc storage[i]->new_func(obj, ptr, ad, i,
445ebfedea0SLionel Sambuc storage[i]->argl, storage[i]->argp);
446ebfedea0SLionel Sambuc }
447ebfedea0SLionel Sambuc }
448ebfedea0SLionel Sambuc if (storage)
449ebfedea0SLionel Sambuc OPENSSL_free(storage);
450ebfedea0SLionel Sambuc return 1;
451ebfedea0SLionel Sambuc }
452ebfedea0SLionel Sambuc
453ebfedea0SLionel Sambuc /* Same thread-safety notes as for "int_new_ex_data" */
int_dup_ex_data(int class_index,CRYPTO_EX_DATA * to,CRYPTO_EX_DATA * from)454ebfedea0SLionel Sambuc static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
455ebfedea0SLionel Sambuc CRYPTO_EX_DATA *from)
456ebfedea0SLionel Sambuc {
457ebfedea0SLionel Sambuc int mx, j, i;
458ebfedea0SLionel Sambuc char *ptr;
459ebfedea0SLionel Sambuc CRYPTO_EX_DATA_FUNCS **storage = NULL;
460ebfedea0SLionel Sambuc EX_CLASS_ITEM *item;
461ebfedea0SLionel Sambuc if (!from->sk)
462ebfedea0SLionel Sambuc /* 'to' should be "blank" which *is* just like 'from' */
463ebfedea0SLionel Sambuc return 1;
464ebfedea0SLionel Sambuc if ((item = def_get_class(class_index)) == NULL)
465ebfedea0SLionel Sambuc return 0;
466ebfedea0SLionel Sambuc CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
467ebfedea0SLionel Sambuc mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
468ebfedea0SLionel Sambuc j = sk_void_num(from->sk);
469ebfedea0SLionel Sambuc if (j < mx)
470ebfedea0SLionel Sambuc mx = j;
471*0a6a1f1dSLionel Sambuc if (mx > 0) {
472ebfedea0SLionel Sambuc storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS *));
473ebfedea0SLionel Sambuc if (!storage)
474ebfedea0SLionel Sambuc goto skip;
475ebfedea0SLionel Sambuc for (i = 0; i < mx; i++)
476ebfedea0SLionel Sambuc storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
477ebfedea0SLionel Sambuc }
478ebfedea0SLionel Sambuc skip:
479ebfedea0SLionel Sambuc CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
480*0a6a1f1dSLionel Sambuc if ((mx > 0) && !storage) {
481ebfedea0SLionel Sambuc CRYPTOerr(CRYPTO_F_INT_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
482ebfedea0SLionel Sambuc return 0;
483ebfedea0SLionel Sambuc }
484*0a6a1f1dSLionel Sambuc for (i = 0; i < mx; i++) {
485ebfedea0SLionel Sambuc ptr = CRYPTO_get_ex_data(from, i);
486ebfedea0SLionel Sambuc if (storage[i] && storage[i]->dup_func)
487ebfedea0SLionel Sambuc storage[i]->dup_func(to, from, &ptr, i,
488ebfedea0SLionel Sambuc storage[i]->argl, storage[i]->argp);
489ebfedea0SLionel Sambuc CRYPTO_set_ex_data(to, i, ptr);
490ebfedea0SLionel Sambuc }
491ebfedea0SLionel Sambuc if (storage)
492ebfedea0SLionel Sambuc OPENSSL_free(storage);
493ebfedea0SLionel Sambuc return 1;
494ebfedea0SLionel Sambuc }
495ebfedea0SLionel Sambuc
496ebfedea0SLionel Sambuc /* Same thread-safety notes as for "int_new_ex_data" */
int_free_ex_data(int class_index,void * obj,CRYPTO_EX_DATA * ad)497*0a6a1f1dSLionel Sambuc static void int_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
498ebfedea0SLionel Sambuc {
499ebfedea0SLionel Sambuc int mx, i;
500ebfedea0SLionel Sambuc EX_CLASS_ITEM *item;
501ebfedea0SLionel Sambuc void *ptr;
502ebfedea0SLionel Sambuc CRYPTO_EX_DATA_FUNCS **storage = NULL;
503*0a6a1f1dSLionel Sambuc if (ex_data == NULL)
504*0a6a1f1dSLionel Sambuc return;
505ebfedea0SLionel Sambuc if ((item = def_get_class(class_index)) == NULL)
506ebfedea0SLionel Sambuc return;
507ebfedea0SLionel Sambuc CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
508ebfedea0SLionel Sambuc mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
509*0a6a1f1dSLionel Sambuc if (mx > 0) {
510ebfedea0SLionel Sambuc storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS *));
511ebfedea0SLionel Sambuc if (!storage)
512ebfedea0SLionel Sambuc goto skip;
513ebfedea0SLionel Sambuc for (i = 0; i < mx; i++)
514ebfedea0SLionel Sambuc storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
515ebfedea0SLionel Sambuc }
516ebfedea0SLionel Sambuc skip:
517ebfedea0SLionel Sambuc CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
518*0a6a1f1dSLionel Sambuc if ((mx > 0) && !storage) {
519ebfedea0SLionel Sambuc CRYPTOerr(CRYPTO_F_INT_FREE_EX_DATA, ERR_R_MALLOC_FAILURE);
520ebfedea0SLionel Sambuc return;
521ebfedea0SLionel Sambuc }
522*0a6a1f1dSLionel Sambuc for (i = 0; i < mx; i++) {
523*0a6a1f1dSLionel Sambuc if (storage[i] && storage[i]->free_func) {
524ebfedea0SLionel Sambuc ptr = CRYPTO_get_ex_data(ad, i);
525ebfedea0SLionel Sambuc storage[i]->free_func(obj, ptr, ad, i,
526ebfedea0SLionel Sambuc storage[i]->argl, storage[i]->argp);
527ebfedea0SLionel Sambuc }
528ebfedea0SLionel Sambuc }
529ebfedea0SLionel Sambuc if (storage)
530ebfedea0SLionel Sambuc OPENSSL_free(storage);
531*0a6a1f1dSLionel Sambuc if (ad->sk) {
532ebfedea0SLionel Sambuc sk_void_free(ad->sk);
533ebfedea0SLionel Sambuc ad->sk = NULL;
534ebfedea0SLionel Sambuc }
535ebfedea0SLionel Sambuc }
536ebfedea0SLionel Sambuc
537ebfedea0SLionel Sambuc /********************************************************************/
538*0a6a1f1dSLionel Sambuc /*
539*0a6a1f1dSLionel Sambuc * API functions that defer all "state" operations to the "ex_data"
540*0a6a1f1dSLionel Sambuc * implementation we have set.
541*0a6a1f1dSLionel Sambuc */
542ebfedea0SLionel Sambuc
543*0a6a1f1dSLionel Sambuc /*
544*0a6a1f1dSLionel Sambuc * Obtain an index for a new class (not the same as getting a new index
545*0a6a1f1dSLionel Sambuc * within an existing class - this is actually getting a new *class*)
546*0a6a1f1dSLionel Sambuc */
CRYPTO_ex_data_new_class(void)547ebfedea0SLionel Sambuc int CRYPTO_ex_data_new_class(void)
548ebfedea0SLionel Sambuc {
549*0a6a1f1dSLionel Sambuc IMPL_CHECK return EX_IMPL(new_class) ();
550ebfedea0SLionel Sambuc }
551ebfedea0SLionel Sambuc
552*0a6a1f1dSLionel Sambuc /*
553*0a6a1f1dSLionel Sambuc * Release all "ex_data" state to prevent memory leaks. This can't be made
554ebfedea0SLionel Sambuc * thread-safe without overhauling a lot of stuff, and shouldn't really be
555ebfedea0SLionel Sambuc * called under potential race-conditions anyway (it's for program shutdown
556*0a6a1f1dSLionel Sambuc * after all).
557*0a6a1f1dSLionel Sambuc */
CRYPTO_cleanup_all_ex_data(void)558ebfedea0SLionel Sambuc void CRYPTO_cleanup_all_ex_data(void)
559ebfedea0SLionel Sambuc {
560*0a6a1f1dSLionel Sambuc IMPL_CHECK EX_IMPL(cleanup) ();
561ebfedea0SLionel Sambuc }
562ebfedea0SLionel Sambuc
563ebfedea0SLionel Sambuc /* Inside an existing class, get/register a new index. */
CRYPTO_get_ex_new_index(int class_index,long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)564ebfedea0SLionel Sambuc int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
565ebfedea0SLionel Sambuc CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
566ebfedea0SLionel Sambuc CRYPTO_EX_free *free_func)
567ebfedea0SLionel Sambuc {
568ebfedea0SLionel Sambuc int ret = -1;
569ebfedea0SLionel Sambuc
570ebfedea0SLionel Sambuc IMPL_CHECK
571ebfedea0SLionel Sambuc ret = EX_IMPL(get_new_index) (class_index,
572*0a6a1f1dSLionel Sambuc argl, argp, new_func, dup_func,
573*0a6a1f1dSLionel Sambuc free_func);
574ebfedea0SLionel Sambuc return ret;
575ebfedea0SLionel Sambuc }
576ebfedea0SLionel Sambuc
577*0a6a1f1dSLionel Sambuc /*
578*0a6a1f1dSLionel Sambuc * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
579*0a6a1f1dSLionel Sambuc * calling new() callbacks for each index in the class used by this variable
580*0a6a1f1dSLionel Sambuc */
CRYPTO_new_ex_data(int class_index,void * obj,CRYPTO_EX_DATA * ad)581ebfedea0SLionel Sambuc int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
582ebfedea0SLionel Sambuc {
583*0a6a1f1dSLionel Sambuc IMPL_CHECK return EX_IMPL(new_ex_data) (class_index, obj, ad);
584ebfedea0SLionel Sambuc }
585ebfedea0SLionel Sambuc
586*0a6a1f1dSLionel Sambuc /*
587*0a6a1f1dSLionel Sambuc * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
588*0a6a1f1dSLionel Sambuc * for each index in the class used by this variable
589*0a6a1f1dSLionel Sambuc */
CRYPTO_dup_ex_data(int class_index,CRYPTO_EX_DATA * to,CRYPTO_EX_DATA * from)590ebfedea0SLionel Sambuc int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
591ebfedea0SLionel Sambuc CRYPTO_EX_DATA *from)
592ebfedea0SLionel Sambuc {
593*0a6a1f1dSLionel Sambuc IMPL_CHECK return EX_IMPL(dup_ex_data) (class_index, to, from);
594ebfedea0SLionel Sambuc }
595ebfedea0SLionel Sambuc
596*0a6a1f1dSLionel Sambuc /*
597*0a6a1f1dSLionel Sambuc * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
598*0a6a1f1dSLionel Sambuc * each index in the class used by this variable
599*0a6a1f1dSLionel Sambuc */
CRYPTO_free_ex_data(int class_index,void * obj,CRYPTO_EX_DATA * ad)600ebfedea0SLionel Sambuc void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
601ebfedea0SLionel Sambuc {
602*0a6a1f1dSLionel Sambuc IMPL_CHECK EX_IMPL(free_ex_data) (class_index, obj, ad);
603ebfedea0SLionel Sambuc }
604ebfedea0SLionel Sambuc
605*0a6a1f1dSLionel Sambuc /*
606*0a6a1f1dSLionel Sambuc * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
607*0a6a1f1dSLionel Sambuc * particular index in the class used by this variable
608*0a6a1f1dSLionel Sambuc */
CRYPTO_set_ex_data(CRYPTO_EX_DATA * ad,int idx,void * val)609ebfedea0SLionel Sambuc int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
610ebfedea0SLionel Sambuc {
611ebfedea0SLionel Sambuc int i;
612ebfedea0SLionel Sambuc
613*0a6a1f1dSLionel Sambuc if (ad->sk == NULL) {
614*0a6a1f1dSLionel Sambuc if ((ad->sk = sk_void_new_null()) == NULL) {
615ebfedea0SLionel Sambuc CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
616ebfedea0SLionel Sambuc return (0);
617ebfedea0SLionel Sambuc }
618ebfedea0SLionel Sambuc }
619ebfedea0SLionel Sambuc i = sk_void_num(ad->sk);
620ebfedea0SLionel Sambuc
621*0a6a1f1dSLionel Sambuc while (i <= idx) {
622*0a6a1f1dSLionel Sambuc if (!sk_void_push(ad->sk, NULL)) {
623ebfedea0SLionel Sambuc CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
624ebfedea0SLionel Sambuc return (0);
625ebfedea0SLionel Sambuc }
626ebfedea0SLionel Sambuc i++;
627ebfedea0SLionel Sambuc }
628ebfedea0SLionel Sambuc sk_void_set(ad->sk, idx, val);
629ebfedea0SLionel Sambuc return (1);
630ebfedea0SLionel Sambuc }
631ebfedea0SLionel Sambuc
632*0a6a1f1dSLionel Sambuc /*
633*0a6a1f1dSLionel Sambuc * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
634*0a6a1f1dSLionel Sambuc * particular index in the class used by this variable
635*0a6a1f1dSLionel Sambuc */
CRYPTO_get_ex_data(const CRYPTO_EX_DATA * ad,int idx)636ebfedea0SLionel Sambuc void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
637ebfedea0SLionel Sambuc {
638ebfedea0SLionel Sambuc if (ad->sk == NULL)
639ebfedea0SLionel Sambuc return (0);
640ebfedea0SLionel Sambuc else if (idx >= sk_void_num(ad->sk))
641ebfedea0SLionel Sambuc return (0);
642ebfedea0SLionel Sambuc else
643ebfedea0SLionel Sambuc return (sk_void_value(ad->sk, idx));
644ebfedea0SLionel Sambuc }
645ebfedea0SLionel Sambuc
646ebfedea0SLionel Sambuc IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS)
647