blob: 5a3546a2d2715142d1c0713e86e5e8dbfd4eb502 [file] [log] [blame]
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00001/* crypto/ex_data.c */
Bodo Möller3ac82fa2000-12-15 16:40:35 +00002
3/*
Geoff Thorpe3a079992001-09-01 19:56:46 +00004 * Overhaul notes;
Bodo Möller3ac82fa2000-12-15 16:40:35 +00005 *
Geoff Thorpe3a079992001-09-01 19:56:46 +00006 * This code is now *mostly* thread-safe. It is now easier to understand in what
7 * ways it is safe and in what ways it is not, which is an improvement. Firstly,
8 * all per-class stacks and index-counters for ex_data are stored in the same
9 * global LHASH table (keyed by class). This hash table uses locking for all
10 * access with the exception of CRYPTO_cleanup_all_ex_data(), which must only be
11 * called when no other threads can possibly race against it (even if it was
12 * locked, the race would mean it's possible the hash table might have been
13 * recreated after the cleanup). As classes can only be added to the hash table,
14 * and within each class, the stack of methods can only be incremented, the
15 * locking mechanics are simpler than they would otherwise be. For example, the
16 * new/dup/free ex_data functions will lock the hash table, copy the method
17 * pointers it needs from the relevant class, then unlock the hash table before
18 * actually applying those method pointers to the task of the new/dup/free
19 * operations. As they can't be removed from the method-stack, only
20 * supplemented, there's no race conditions associated with using them outside
21 * the lock. The get/set_ex_data functions are not locked because they do not
22 * involve this global state at all - they operate directly with a previously
23 * obtained per-class method index and a particular "ex_data" variable. These
24 * variables are usually instantiated per-context (eg. each RSA structure has
25 * one) so locking on read/write access to that variable can be locked locally
26 * if required (eg. using the "RSA" lock to synchronise access to a
27 * per-RSA-structure ex_data variable if required).
28 * [Geoff]
Bodo Möller3ac82fa2000-12-15 16:40:35 +000029 */
30
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000031/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
32 * All rights reserved.
33 *
34 * This package is an SSL implementation written
35 * by Eric Young (eay@cryptsoft.com).
36 * The implementation was written so as to conform with Netscapes SSL.
Matt Caswell0f113f32015-01-22 03:40:55 +000037 *
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000038 * This library is free for commercial and non-commercial use as long as
39 * the following conditions are aheared to. The following conditions
40 * apply to all code found in this distribution, be it the RC4, RSA,
41 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
42 * included with this distribution is covered by the same copyright terms
43 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
Matt Caswell0f113f32015-01-22 03:40:55 +000044 *
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000045 * Copyright remains Eric Young's, and as such any Copyright notices in
46 * the code are not to be removed.
47 * If this package is used in a product, Eric Young should be given attribution
48 * as the author of the parts of the library used.
49 * This can be in the form of a textual message at program startup or
50 * in documentation (online or textual) provided with the package.
Matt Caswell0f113f32015-01-22 03:40:55 +000051 *
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000052 * Redistribution and use in source and binary forms, with or without
53 * modification, are permitted provided that the following conditions
54 * are met:
55 * 1. Redistributions of source code must retain the copyright
56 * notice, this list of conditions and the following disclaimer.
57 * 2. Redistributions in binary form must reproduce the above copyright
58 * notice, this list of conditions and the following disclaimer in the
59 * documentation and/or other materials provided with the distribution.
60 * 3. All advertising materials mentioning features or use of this software
61 * must display the following acknowledgement:
62 * "This product includes cryptographic software written by
63 * Eric Young (eay@cryptsoft.com)"
64 * The word 'cryptographic' can be left out if the rouines from the library
65 * being used are not cryptographic related :-).
Matt Caswell0f113f32015-01-22 03:40:55 +000066 * 4. If you include any Windows specific code (or a derivative thereof) from
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000067 * the apps directory (application code) you must include an acknowledgement:
68 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Matt Caswell0f113f32015-01-22 03:40:55 +000069 *
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000070 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
71 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
72 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
73 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
74 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
75 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
76 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
77 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
78 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
79 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
80 * SUCH DAMAGE.
Matt Caswell0f113f32015-01-22 03:40:55 +000081 *
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000082 * The licence and distribution terms for any publically available version or
83 * derivative of this code cannot be changed. i.e. this code cannot simply be
84 * copied and put under another distribution licence
85 * [including the GNU Public Licence.]
86 */
Bodo Möller435037d2001-09-04 11:02:23 +000087/* ====================================================================
88 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
89 *
90 * Redistribution and use in source and binary forms, with or without
91 * modification, are permitted provided that the following conditions
92 * are met:
93 *
94 * 1. Redistributions of source code must retain the above copyright
Matt Caswell0f113f32015-01-22 03:40:55 +000095 * notice, this list of conditions and the following disclaimer.
Bodo Möller435037d2001-09-04 11:02:23 +000096 *
97 * 2. Redistributions in binary form must reproduce the above copyright
98 * notice, this list of conditions and the following disclaimer in
99 * the documentation and/or other materials provided with the
100 * distribution.
101 *
102 * 3. All advertising materials mentioning features or use of this
103 * software must display the following acknowledgment:
104 * "This product includes software developed by the OpenSSL Project
105 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
106 *
107 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
108 * endorse or promote products derived from this software without
109 * prior written permission. For written permission, please contact
110 * openssl-core@openssl.org.
111 *
112 * 5. Products derived from this software may not be called "OpenSSL"
113 * nor may "OpenSSL" appear in their names without prior written
114 * permission of the OpenSSL Project.
115 *
116 * 6. Redistributions of any form whatsoever must retain the following
117 * acknowledgment:
118 * "This product includes software developed by the OpenSSL Project
119 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
120 *
121 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
122 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
123 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
124 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
125 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
126 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
127 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
128 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
129 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
130 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
131 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
132 * OF THE POSSIBILITY OF SUCH DAMAGE.
133 * ====================================================================
134 *
135 * This product includes cryptographic software written by Eric Young
136 * (eay@cryptsoft.com). This product includes software written by Tim
137 * Hudson (tjh@cryptsoft.com).
138 *
139 */
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000140
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000141#include "cryptlib.h"
Geoff Thorpe60a938c2004-04-19 18:09:28 +0000142#include <openssl/lhash.h>
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000143
Geoff Thorpe3a079992001-09-01 19:56:46 +0000144/* What an "implementation of ex_data functionality" looks like */
Matt Caswell0f113f32015-01-22 03:40:55 +0000145struct st_CRYPTO_EX_DATA_IMPL {
146 /*********************/
147 /* GLOBAL OPERATIONS */
148 /* Return a new class index */
149 int (*cb_new_class) (void);
150 /* Cleanup all state used by the implementation */
151 void (*cb_cleanup) (void);
152 /************************/
153 /* PER-CLASS OPERATIONS */
154 /* Get a new method index within a class */
155 int (*cb_get_new_index) (int class_index, long argl, void *argp,
156 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
157 CRYPTO_EX_free *free_func);
158 /* Initialise a new CRYPTO_EX_DATA of a given class */
159 int (*cb_new_ex_data) (int class_index, void *obj, CRYPTO_EX_DATA *ad);
160 /* Duplicate a CRYPTO_EX_DATA of a given class onto a copy */
161 int (*cb_dup_ex_data) (int class_index, CRYPTO_EX_DATA *to,
162 CRYPTO_EX_DATA *from);
163 /* Cleanup a CRYPTO_EX_DATA of a given class */
164 void (*cb_free_ex_data) (int class_index, void *obj, CRYPTO_EX_DATA *ad);
165};
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000166
Geoff Thorpe3a079992001-09-01 19:56:46 +0000167/* The implementation we use at run-time */
168static const CRYPTO_EX_DATA_IMPL *impl = NULL;
169
Matt Caswell0f113f32015-01-22 03:40:55 +0000170/*
171 * To call "impl" functions, use this macro rather than referring to 'impl'
172 * directly, eg. EX_IMPL(get_new_index)(...);
173 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000174#define EX_IMPL(a) impl->cb_##a
175
176/* Predeclare the "default" ex_data implementation */
177static int int_new_class(void);
178static void int_cleanup(void);
179static int int_get_new_index(int class_index, long argl, void *argp,
Matt Caswell0f113f32015-01-22 03:40:55 +0000180 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
181 CRYPTO_EX_free *free_func);
182static int int_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
Geoff Thorpe3a079992001-09-01 19:56:46 +0000183static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
Matt Caswell0f113f32015-01-22 03:40:55 +0000184 CRYPTO_EX_DATA *from);
185static void int_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
186static CRYPTO_EX_DATA_IMPL impl_default = {
187 int_new_class,
188 int_cleanup,
189 int_get_new_index,
190 int_new_ex_data,
191 int_dup_ex_data,
192 int_free_ex_data
193};
Geoff Thorpe3a079992001-09-01 19:56:46 +0000194
Matt Caswell0f113f32015-01-22 03:40:55 +0000195/*
196 * Internal function that checks whether "impl" is set and if not, sets it to
197 * the default.
198 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000199static void impl_check(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000200{
201 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
202 if (!impl)
203 impl = &impl_default;
204 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
205}
206
207/*
208 * A macro wrapper for impl_check that first uses a non-locked test before
209 * invoking the function (which checks again inside a lock).
210 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000211#define IMPL_CHECK if(!impl) impl_check();
212
213/* API functions to get/set the "ex_data" implementation */
214const CRYPTO_EX_DATA_IMPL *CRYPTO_get_ex_data_implementation(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000215{
216 IMPL_CHECK return impl;
217}
218
Geoff Thorpe3a079992001-09-01 19:56:46 +0000219int CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i)
Matt Caswell0f113f32015-01-22 03:40:55 +0000220{
221 int toret = 0;
222 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
223 if (!impl) {
224 impl = i;
225 toret = 1;
226 }
227 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
228 return toret;
229}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000230
231/****************************************************************************/
Matt Caswell0f113f32015-01-22 03:40:55 +0000232/*
233 * Interal (default) implementation of "ex_data" support. API functions are
234 * further down.
235 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000236
Matt Caswell0f113f32015-01-22 03:40:55 +0000237/*
238 * The type that represents what each "class" used to implement locally. A
239 * STACK of CRYPTO_EX_DATA_FUNCS plus a index-counter. The 'class_index' is
240 * the global value representing the class that is used to distinguish these
241 * items.
242 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000243typedef struct st_ex_class_item {
Matt Caswell0f113f32015-01-22 03:40:55 +0000244 int class_index;
245 STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
246 int meth_num;
Geoff Thorpe3a079992001-09-01 19:56:46 +0000247} EX_CLASS_ITEM;
248
249/* When assigning new class indexes, this is our counter */
250static int ex_class = CRYPTO_EX_INDEX_USER;
251
252/* The global hash table of EX_CLASS_ITEM items */
Ben Laurie3c1d6bb2008-05-26 11:24:29 +0000253DECLARE_LHASH_OF(EX_CLASS_ITEM);
254static LHASH_OF(EX_CLASS_ITEM) *ex_data = NULL;
Geoff Thorpe3a079992001-09-01 19:56:46 +0000255
256/* The callbacks required in the "ex_data" hash table */
Ben Laurie3c1d6bb2008-05-26 11:24:29 +0000257static unsigned long ex_class_item_hash(const EX_CLASS_ITEM *a)
Matt Caswell0f113f32015-01-22 03:40:55 +0000258{
259 return a->class_index;
260}
261
Dr. Stephen Hensonab3eafd2008-05-31 19:17:25 +0000262static IMPLEMENT_LHASH_HASH_FN(ex_class_item, EX_CLASS_ITEM)
Ben Laurie3c1d6bb2008-05-26 11:24:29 +0000263
264static int ex_class_item_cmp(const EX_CLASS_ITEM *a, const EX_CLASS_ITEM *b)
Matt Caswell0f113f32015-01-22 03:40:55 +0000265{
266 return a->class_index - b->class_index;
267}
268
Dr. Stephen Hensonab3eafd2008-05-31 19:17:25 +0000269static IMPLEMENT_LHASH_COMP_FN(ex_class_item, EX_CLASS_ITEM)
Geoff Thorpe3a079992001-09-01 19:56:46 +0000270
Matt Caswell0f113f32015-01-22 03:40:55 +0000271/*
272 * Internal functions used by the "impl_default" implementation to access the
273 * state
274 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000275static int ex_data_check(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000276{
277 int toret = 1;
278 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
279 if (!ex_data && (ex_data = lh_EX_CLASS_ITEM_new()) == NULL)
280 toret = 0;
281 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
282 return toret;
283}
284
285/*
286 * This macros helps reduce the locking from repeated checks because the
287 * ex_data_check() function checks ex_data again inside a lock.
288 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000289#define EX_DATA_CHECK(iffail) if(!ex_data && !ex_data_check()) {iffail}
290
291/* This "inner" callback is used by the callback function that follows it */
Ben Laurie546ec5a2001-09-07 11:43:30 +0000292static void def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS *funcs)
Matt Caswell0f113f32015-01-22 03:40:55 +0000293{
294 OPENSSL_free(funcs);
295}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000296
Matt Caswell0f113f32015-01-22 03:40:55 +0000297/*
298 * This callback is used in lh_doall to destroy all EX_CLASS_ITEM values from
299 * "ex_data" prior to the ex_data hash table being itself destroyed. Doesn't
300 * do any locking.
301 */
Geoff Thorpe8dc344c2003-10-29 04:57:05 +0000302static void def_cleanup_cb(void *a_void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000303{
304 EX_CLASS_ITEM *item = (EX_CLASS_ITEM *)a_void;
305 sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, def_cleanup_util_cb);
306 OPENSSL_free(item);
307}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000308
Matt Caswell0f113f32015-01-22 03:40:55 +0000309/*
310 * Return the EX_CLASS_ITEM from the "ex_data" hash table that corresponds to
311 * a given class. Handles locking.
312 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000313static EX_CLASS_ITEM *def_get_class(int class_index)
Matt Caswell0f113f32015-01-22 03:40:55 +0000314{
315 EX_CLASS_ITEM d, *p, *gen;
316 EX_DATA_CHECK(return NULL;)
317 d.class_index = class_index;
318 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
319 p = lh_EX_CLASS_ITEM_retrieve(ex_data, &d);
320 if (!p) {
321 gen = OPENSSL_malloc(sizeof(EX_CLASS_ITEM));
322 if (gen) {
323 gen->class_index = class_index;
324 gen->meth_num = 0;
325 gen->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null();
326 if (!gen->meth)
327 OPENSSL_free(gen);
328 else {
329 /*
330 * Because we're inside the ex_data lock, the return value
331 * from the insert will be NULL
332 */
333 (void)lh_EX_CLASS_ITEM_insert(ex_data, gen);
334 p = gen;
335 }
336 }
337 }
338 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
339 if (!p)
340 CRYPTOerr(CRYPTO_F_DEF_GET_CLASS, ERR_R_MALLOC_FAILURE);
341 return p;
342}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000343
Matt Caswell0f113f32015-01-22 03:40:55 +0000344/*
345 * Add a new method to the given EX_CLASS_ITEM and return the corresponding
346 * index (or -1 for error). Handles locking.
347 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000348static int def_add_index(EX_CLASS_ITEM *item, long argl, void *argp,
Matt Caswell0f113f32015-01-22 03:40:55 +0000349 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
350 CRYPTO_EX_free *free_func)
351{
352 int toret = -1;
353 CRYPTO_EX_DATA_FUNCS *a =
354 (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS));
355 if (!a) {
356 CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX, ERR_R_MALLOC_FAILURE);
357 return -1;
358 }
359 a->argl = argl;
360 a->argp = argp;
361 a->new_func = new_func;
362 a->dup_func = dup_func;
363 a->free_func = free_func;
364 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
365 while (sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) <= item->meth_num) {
366 if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, NULL)) {
367 CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX, ERR_R_MALLOC_FAILURE);
368 OPENSSL_free(a);
369 goto err;
370 }
371 }
372 toret = item->meth_num++;
373 (void)sk_CRYPTO_EX_DATA_FUNCS_set(item->meth, toret, a);
374 err:
375 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
376 return toret;
377}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000378
Geoff Thorpe3a079992001-09-01 19:56:46 +0000379/**************************************************************/
380/* The functions in the default CRYPTO_EX_DATA_IMPL structure */
381
382static int int_new_class(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000383{
384 int toret;
385 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
386 toret = ex_class++;
387 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
388 return toret;
389}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000390
391static void int_cleanup(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000392{
393 EX_DATA_CHECK(return;)
394 lh_EX_CLASS_ITEM_doall(ex_data, def_cleanup_cb);
395 lh_EX_CLASS_ITEM_free(ex_data);
396 ex_data = NULL;
397 impl = NULL;
398}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000399
400static int int_get_new_index(int class_index, long argl, void *argp,
Matt Caswell0f113f32015-01-22 03:40:55 +0000401 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
402 CRYPTO_EX_free *free_func)
403{
404 EX_CLASS_ITEM *item = def_get_class(class_index);
405 if (!item)
406 return -1;
407 return def_add_index(item, argl, argp, new_func, dup_func, free_func);
408}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000409
Matt Caswell0f113f32015-01-22 03:40:55 +0000410/*
411 * Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries
412 * in the lock, then using them outside the lock. NB: Thread-safety only
413 * applies to the global "ex_data" state (ie. class definitions), not
414 * thread-safe on 'ad' itself.
415 */
416static int int_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
417{
418 int mx, i;
419 void *ptr;
420 CRYPTO_EX_DATA_FUNCS **storage = NULL;
421 EX_CLASS_ITEM *item = def_get_class(class_index);
422 if (!item)
423 /* error is already set */
424 return 0;
425 ad->sk = NULL;
426 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
427 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
428 if (mx > 0) {
429 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS *));
430 if (!storage)
431 goto skip;
432 for (i = 0; i < mx; i++)
433 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
434 }
435 skip:
436 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
437 if ((mx > 0) && !storage) {
438 CRYPTOerr(CRYPTO_F_INT_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
439 return 0;
440 }
441 for (i = 0; i < mx; i++) {
442 if (storage[i] && storage[i]->new_func) {
443 ptr = CRYPTO_get_ex_data(ad, i);
444 storage[i]->new_func(obj, ptr, ad, i,
445 storage[i]->argl, storage[i]->argp);
446 }
447 }
448 if (storage)
449 OPENSSL_free(storage);
450 return 1;
451}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000452
453/* Same thread-safety notes as for "int_new_ex_data" */
454static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
Matt Caswell0f113f32015-01-22 03:40:55 +0000455 CRYPTO_EX_DATA *from)
456{
457 int mx, j, i;
458 char *ptr;
459 CRYPTO_EX_DATA_FUNCS **storage = NULL;
460 EX_CLASS_ITEM *item;
461 if (!from->sk)
462 /* 'to' should be "blank" which *is* just like 'from' */
463 return 1;
464 if ((item = def_get_class(class_index)) == NULL)
465 return 0;
466 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
467 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
468 j = sk_void_num(from->sk);
469 if (j < mx)
470 mx = j;
471 if (mx > 0) {
472 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS *));
473 if (!storage)
474 goto skip;
475 for (i = 0; i < mx; i++)
476 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
477 }
478 skip:
479 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
480 if ((mx > 0) && !storage) {
481 CRYPTOerr(CRYPTO_F_INT_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
482 return 0;
483 }
484 for (i = 0; i < mx; i++) {
485 ptr = CRYPTO_get_ex_data(from, i);
486 if (storage[i] && storage[i]->dup_func)
487 storage[i]->dup_func(to, from, &ptr, i,
488 storage[i]->argl, storage[i]->argp);
489 CRYPTO_set_ex_data(to, i, ptr);
490 }
491 if (storage)
492 OPENSSL_free(storage);
493 return 1;
494}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000495
496/* Same thread-safety notes as for "int_new_ex_data" */
Matt Caswell0f113f32015-01-22 03:40:55 +0000497static void int_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
498{
499 int mx, i;
500 EX_CLASS_ITEM *item;
501 void *ptr;
502 CRYPTO_EX_DATA_FUNCS **storage = NULL;
503 if ((item = def_get_class(class_index)) == NULL)
504 return;
505 CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
506 mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
507 if (mx > 0) {
508 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS *));
509 if (!storage)
510 goto skip;
511 for (i = 0; i < mx; i++)
512 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
513 }
514 skip:
515 CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
516 if ((mx > 0) && !storage) {
517 CRYPTOerr(CRYPTO_F_INT_FREE_EX_DATA, ERR_R_MALLOC_FAILURE);
518 return;
519 }
520 for (i = 0; i < mx; i++) {
521 if (storage[i] && storage[i]->free_func) {
522 ptr = CRYPTO_get_ex_data(ad, i);
523 storage[i]->free_func(obj, ptr, ad, i,
524 storage[i]->argl, storage[i]->argp);
525 }
526 }
527 if (storage)
528 OPENSSL_free(storage);
529 if (ad->sk) {
530 sk_void_free(ad->sk);
531 ad->sk = NULL;
532 }
533}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000534
535/********************************************************************/
Matt Caswell0f113f32015-01-22 03:40:55 +0000536/*
537 * API functions that defer all "state" operations to the "ex_data"
538 * implementation we have set.
539 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000540
Matt Caswell0f113f32015-01-22 03:40:55 +0000541/*
542 * Obtain an index for a new class (not the same as getting a new index
543 * within an existing class - this is actually getting a new *class*)
544 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000545int CRYPTO_ex_data_new_class(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000546{
547 IMPL_CHECK return EX_IMPL(new_class) ();
548}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000549
Matt Caswell0f113f32015-01-22 03:40:55 +0000550/*
551 * Release all "ex_data" state to prevent memory leaks. This can't be made
Geoff Thorpe3a079992001-09-01 19:56:46 +0000552 * thread-safe without overhauling a lot of stuff, and shouldn't really be
553 * called under potential race-conditions anyway (it's for program shutdown
Matt Caswell0f113f32015-01-22 03:40:55 +0000554 * after all).
555 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000556void CRYPTO_cleanup_all_ex_data(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000557{
558 IMPL_CHECK EX_IMPL(cleanup) ();
559}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000560
561/* Inside an existing class, get/register a new index. */
562int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
Matt Caswell0f113f32015-01-22 03:40:55 +0000563 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
564 CRYPTO_EX_free *free_func)
565{
566 int ret = -1;
Geoff Thorpe3a079992001-09-01 19:56:46 +0000567
Matt Caswell0f113f32015-01-22 03:40:55 +0000568 IMPL_CHECK
569 ret = EX_IMPL(get_new_index) (class_index,
570 argl, argp, new_func, dup_func,
571 free_func);
572 return ret;
573}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000574
Matt Caswell0f113f32015-01-22 03:40:55 +0000575/*
576 * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
577 * calling new() callbacks for each index in the class used by this variable
578 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000579int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
Matt Caswell0f113f32015-01-22 03:40:55 +0000580{
581 IMPL_CHECK return EX_IMPL(new_ex_data) (class_index, obj, ad);
582}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000583
Matt Caswell0f113f32015-01-22 03:40:55 +0000584/*
585 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
586 * for each index in the class used by this variable
587 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000588int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
Matt Caswell0f113f32015-01-22 03:40:55 +0000589 CRYPTO_EX_DATA *from)
590{
591 IMPL_CHECK return EX_IMPL(dup_ex_data) (class_index, to, from);
592}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000593
Matt Caswell0f113f32015-01-22 03:40:55 +0000594/*
595 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
596 * each index in the class used by this variable
597 */
Geoff Thorpe3a079992001-09-01 19:56:46 +0000598void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
Matt Caswell0f113f32015-01-22 03:40:55 +0000599{
600 IMPL_CHECK EX_IMPL(free_ex_data) (class_index, obj, ad);
601}
Geoff Thorpe3a079992001-09-01 19:56:46 +0000602
Matt Caswell0f113f32015-01-22 03:40:55 +0000603/*
604 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
605 * particular index in the class used by this variable
606 */
Dr. Stephen Hensondd9d2332000-01-23 23:41:49 +0000607int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
Matt Caswell0f113f32015-01-22 03:40:55 +0000608{
609 int i;
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000610
Matt Caswell0f113f32015-01-22 03:40:55 +0000611 if (ad->sk == NULL) {
612 if ((ad->sk = sk_void_new_null()) == NULL) {
613 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
614 return (0);
615 }
616 }
617 i = sk_void_num(ad->sk);
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000618
Matt Caswell0f113f32015-01-22 03:40:55 +0000619 while (i <= idx) {
620 if (!sk_void_push(ad->sk, NULL)) {
621 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
622 return (0);
623 }
624 i++;
625 }
626 sk_void_set(ad->sk, idx, val);
627 return (1);
628}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000629
Matt Caswell0f113f32015-01-22 03:40:55 +0000630/*
631 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
632 * particular index in the class used by this variable
633 */
Richard Levittebbbc96a2000-11-06 23:24:59 +0000634void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
Matt Caswell0f113f32015-01-22 03:40:55 +0000635{
636 if (ad->sk == NULL)
637 return (0);
638 else if (idx >= sk_void_num(ad->sk))
639 return (0);
640 else
641 return (sk_void_value(ad->sk, idx));
642}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000643
Dr. Stephen Hensondd9d2332000-01-23 23:41:49 +0000644IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS)