a135858c696e9d8c83fdf658d5f83015cc9a03b3
[openssl.git] / crypto / ex_data.c
1 /* crypto/ex_data.c */
2
3 /*
4  * Overhaul notes;
5  *
6  * 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]
29  */
30
31 /* 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.
37  * 
38  * 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).
44  * 
45  * 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.
51  * 
52  * 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 :-).
66  * 4. If you include any Windows specific code (or a derivative thereof) from 
67  *    the apps directory (application code) you must include an acknowledgement:
68  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
69  * 
70  * 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.
81  * 
82  * 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  */
87 /* ====================================================================
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
95  *    notice, this list of conditions and the following disclaimer. 
96  *
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  */
140
141 #include <stdio.h>
142 #include <stdlib.h>
143 #include <openssl/buffer.h>
144 #include <openssl/bio.h>
145 #include <openssl/lhash.h>
146 #include "cryptlib.h"
147
148 /* What an "implementation of ex_data functionality" looks like */
149 struct st_CRYPTO_EX_DATA_IMPL
150         {
151         /*********************/
152         /* GLOBAL OPERATIONS */
153         /* Return a new class index */
154         int (*cb_new_class)(void);
155         /* Cleanup all state used by the implementation */
156         void (*cb_cleanup)(void);
157         /************************/
158         /* PER-CLASS OPERATIONS */
159         /* Get a new method index within a class */
160         int (*cb_get_new_index)(int class_index, long argl, void *argp,
161                         CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
162                         CRYPTO_EX_free *free_func);
163         /* Initialise a new CRYPTO_EX_DATA of a given class */
164         int (*cb_new_ex_data)(int class_index, void *obj,
165                         CRYPTO_EX_DATA *ad);
166         /* Duplicate a CRYPTO_EX_DATA of a given class onto a copy */
167         int (*cb_dup_ex_data)(int class_index, CRYPTO_EX_DATA *to,
168                         CRYPTO_EX_DATA *from);
169         /* Cleanup a CRYPTO_EX_DATA of a given class */
170         void (*cb_free_ex_data)(int class_index, void *obj,
171                         CRYPTO_EX_DATA *ad);
172         };
173
174 /* The implementation we use at run-time */
175 static const CRYPTO_EX_DATA_IMPL *impl = NULL;
176
177 /* To call "impl" functions, use this macro rather than referring to 'impl' directly, eg.
178  * EX_IMPL(get_new_index)(...); */
179 #define EX_IMPL(a) impl->cb_##a
180
181 /* Predeclare the "default" ex_data implementation */
182 static int int_new_class(void);
183 static void int_cleanup(void);
184 static int int_get_new_index(int class_index, long argl, void *argp,
185                 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
186                 CRYPTO_EX_free *free_func);
187 static int int_new_ex_data(int class_index, void *obj,
188                 CRYPTO_EX_DATA *ad);
189 static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
190                 CRYPTO_EX_DATA *from);
191 static void int_free_ex_data(int class_index, void *obj,
192                 CRYPTO_EX_DATA *ad);
193 static CRYPTO_EX_DATA_IMPL impl_default =
194         {
195         int_new_class,
196         int_cleanup,
197         int_get_new_index,
198         int_new_ex_data,
199         int_dup_ex_data,
200         int_free_ex_data
201         };
202
203 /* Internal function that checks whether "impl" is set and if not, sets it to
204  * the default. */
205 static void impl_check(void)
206         {
207         CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
208         if(!impl)
209                 impl = &impl_default;
210         CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
211         }
212 /* A macro wrapper for impl_check that first uses a non-locked test before
213  * invoking the function (which checks again inside a lock). */
214 #define IMPL_CHECK if(!impl) impl_check();
215
216 /* API functions to get/set the "ex_data" implementation */
217 const CRYPTO_EX_DATA_IMPL *CRYPTO_get_ex_data_implementation(void)
218         {
219         IMPL_CHECK
220         return impl;
221         }
222 int CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i)
223         {
224         int toret = 0;
225         CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
226         if(!impl)
227                 {
228                 impl = i;
229                 toret = 1;
230                 }
231         CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
232         return toret;
233         }
234
235 /****************************************************************************/
236 /* Interal (default) implementation of "ex_data" support. API functions are
237  * further down. */
238
239 /* The type that represents what each "class" used to implement locally. A STACK
240  * of CRYPTO_EX_DATA_FUNCS plus a index-counter. The 'class_index' is the global
241  * value representing the class that is used to distinguish these items. */
242 typedef struct st_ex_class_item {
243         int class_index;
244         STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
245         int meth_num;
246 } EX_CLASS_ITEM;
247
248 /* When assigning new class indexes, this is our counter */
249 static int ex_class = CRYPTO_EX_INDEX_USER;
250
251 /* The global hash table of EX_CLASS_ITEM items */
252 static LHASH *ex_data = NULL;
253
254 /* The callbacks required in the "ex_data" hash table */
255 static unsigned long ex_hash_cb(const void *a_void)
256         {
257         return ((const EX_CLASS_ITEM *)a_void)->class_index;
258         }
259 static int ex_cmp_cb(const void *a_void, const void *b_void)
260         {
261         return (((const EX_CLASS_ITEM *)a_void)->class_index -
262                 ((const EX_CLASS_ITEM *)b_void)->class_index);
263         }
264
265 /* Internal functions used by the "impl_default" implementation to access the
266  * state */
267
268 static int ex_data_check(void)
269         {
270         int toret = 1;
271         CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
272         if(!ex_data && ((ex_data = lh_new(ex_hash_cb, ex_cmp_cb)) == NULL))
273                 toret = 0;
274         CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
275         return toret;
276         }
277 /* This macros helps reduce the locking from repeated checks because the
278  * ex_data_check() function checks ex_data again inside a lock. */
279 #define EX_DATA_CHECK(iffail) if(!ex_data && !ex_data_check()) {iffail}
280
281 /* This "inner" callback is used by the callback function that follows it */
282 static void def_cleanup_util_cb(void *v)
283         {
284         CRYPTO_EX_DATA_FUNCS *funcs = (CRYPTO_EX_DATA_FUNCS *)v;
285         OPENSSL_free(funcs);
286         }
287
288 /* This callback is used in lh_doall to destroy all EX_CLASS_ITEM values from
289  * "ex_data" prior to the ex_data hash table being itself destroyed. Doesn't do
290  * any locking. */
291 static void def_cleanup_cb(const void *a_void)
292         {
293         EX_CLASS_ITEM *item = (EX_CLASS_ITEM *)a_void;
294         sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, def_cleanup_util_cb);
295         OPENSSL_free(item);
296         }
297
298 /* Return the EX_CLASS_ITEM from the "ex_data" hash table that corresponds to a
299  * given class. Handles locking. */
300 static EX_CLASS_ITEM *def_get_class(int class_index)
301         {
302         EX_CLASS_ITEM d, *p, *gen;
303         EX_DATA_CHECK(return NULL;)
304         d.class_index = class_index;
305         CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
306         p = lh_retrieve(ex_data, &d);
307         if(!p)
308                 {
309                 gen = OPENSSL_malloc(sizeof(EX_CLASS_ITEM));
310                 if(gen)
311                         {
312                         gen->class_index = class_index;
313                         gen->meth_num = 0;
314                         gen->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null();
315                         if(!gen->meth)
316                                 OPENSSL_free(gen);
317                         else
318                                 {
319                                 /* Because we're inside the ex_data lock, the
320                                  * return value from the insert will be NULL */
321                                 lh_insert(ex_data, gen);
322                                 p = gen;
323                                 }
324                         }
325                 }
326         CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
327         if(!p)
328                 CRYPTOerr(CRYPTO_F_DEF_GET_CLASS,ERR_R_MALLOC_FAILURE);
329         return p;
330         }
331
332 /* Add a new method to the given EX_CLASS_ITEM and return the corresponding
333  * index (or -1 for error). Handles locking. */
334 static int def_add_index(EX_CLASS_ITEM *item, long argl, void *argp,
335                 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
336                 CRYPTO_EX_free *free_func)
337         {
338         int toret = -1;
339         CRYPTO_EX_DATA_FUNCS *a = (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(
340                                         sizeof(CRYPTO_EX_DATA_FUNCS));
341         if(!a)
342                 {
343                 CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX,ERR_R_MALLOC_FAILURE);
344                 return -1;
345                 }
346         a->argl=argl;
347         a->argp=argp;
348         a->new_func=new_func;
349         a->dup_func=dup_func;
350         a->free_func=free_func;
351         CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
352         while (sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) <= item->meth_num)
353                 {
354                 if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, NULL))
355                         {
356                         CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX,ERR_R_MALLOC_FAILURE);
357                         OPENSSL_free(a);
358                         goto err;
359                         }
360                 }
361         toret = item->meth_num++;
362         sk_CRYPTO_EX_DATA_FUNCS_set(item->meth, toret, a);
363 err:
364         CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
365         return toret;
366         }
367
368 /**************************************************************/
369 /* The functions in the default CRYPTO_EX_DATA_IMPL structure */
370
371 static int int_new_class(void)
372         {
373         int toret;
374         CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
375         toret = ex_class++;
376         CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
377         return toret;
378         }
379
380 static void int_cleanup(void)
381         {
382         EX_DATA_CHECK(return;)
383         lh_doall(ex_data, def_cleanup_cb);
384         lh_free(ex_data);
385         ex_data = NULL;
386         impl = NULL;
387         }
388
389 static int int_get_new_index(int class_index, long argl, void *argp,
390                 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
391                 CRYPTO_EX_free *free_func)
392         {
393         EX_CLASS_ITEM *item = def_get_class(class_index);
394         if(!item)
395                 return -1;
396         return def_add_index(item, argl, argp, new_func, dup_func, free_func);
397         }
398
399 /* Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries in
400  * the lock, then using them outside the lock. NB: Thread-safety only applies to
401  * the global "ex_data" state (ie. class definitions), not thread-safe on 'ad'
402  * itself. */
403 static int int_new_ex_data(int class_index, void *obj,
404                 CRYPTO_EX_DATA *ad)
405         {
406         int mx,i;
407         void *ptr;
408         CRYPTO_EX_DATA_FUNCS **storage = NULL;
409         EX_CLASS_ITEM *item = def_get_class(class_index);
410         if(!item)
411                 /* error is already set */
412                 return 0;
413         ad->sk = NULL;
414         CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
415         mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
416         if(mx > 0)
417                 {
418                 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
419                 if(!storage)
420                         goto skip;
421                 for(i = 0; i < mx; i++)
422                         storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i);
423                 }
424 skip:
425         CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
426         if((mx > 0) && !storage)
427                 {
428                 CRYPTOerr(CRYPTO_F_INT_NEW_EX_DATA,ERR_R_MALLOC_FAILURE);
429                 return 0;
430                 }
431         for(i = 0; i < mx; i++)
432                 {
433                 if(storage[i] && storage[i]->new_func)
434                         {
435                         ptr = CRYPTO_get_ex_data(ad, i);
436                         storage[i]->new_func(obj,ptr,ad,i,
437                                 storage[i]->argl,storage[i]->argp);
438                         }
439                 }
440         if(storage)
441                 OPENSSL_free(storage);
442         return 1;
443         }
444
445 /* Same thread-safety notes as for "int_new_ex_data" */
446 static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
447                 CRYPTO_EX_DATA *from)
448         {
449         int mx, j, i;
450         char *ptr;
451         CRYPTO_EX_DATA_FUNCS **storage = NULL;
452         EX_CLASS_ITEM *item;
453         if(!from->sk)
454                 /* 'to' should be "blank" which *is* just like 'from' */
455                 return 1;
456         if((item = def_get_class(class_index)) == NULL)
457                 return 0;
458         CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
459         mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
460         j = sk_num(from->sk);
461         if(j < mx)
462                 mx = j;
463         if(mx > 0)
464                 {
465                 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
466                 if(!storage)
467                         goto skip;
468                 for(i = 0; i < mx; i++)
469                         storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i);
470                 }
471 skip:
472         CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
473         if((mx > 0) && !storage)
474                 {
475                 CRYPTOerr(CRYPTO_F_INT_DUP_EX_DATA,ERR_R_MALLOC_FAILURE);
476                 return 0;
477                 }
478         for(i = 0; i < mx; i++)
479                 {
480                 ptr = CRYPTO_get_ex_data(from, i);
481                 if(storage[i] && storage[i]->dup_func)
482                         storage[i]->dup_func(to,from,&ptr,i,
483                                 storage[i]->argl,storage[i]->argp);
484                 CRYPTO_set_ex_data(to,i,ptr);
485                 }
486         if(storage)
487                 OPENSSL_free(storage);
488         return 1;
489         }
490
491 /* Same thread-safety notes as for "int_new_ex_data" */
492 static void int_free_ex_data(int class_index, void *obj,
493                 CRYPTO_EX_DATA *ad)
494         {
495         int mx,i;
496         EX_CLASS_ITEM *item;
497         void *ptr;
498         CRYPTO_EX_DATA_FUNCS **storage = NULL;
499         if((item = def_get_class(class_index)) == NULL)
500                 return;
501         CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
502         mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
503         if(mx > 0)
504                 {
505                 storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
506                 if(!storage)
507                         goto skip;
508                 for(i = 0; i < mx; i++)
509                         storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i);
510                 }
511 skip:
512         CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
513         if((mx > 0) && !storage)
514                 {
515                 CRYPTOerr(CRYPTO_F_INT_FREE_EX_DATA,ERR_R_MALLOC_FAILURE);
516                 return;
517                 }
518         for(i = 0; i < mx; i++)
519                 {
520                 if(storage[i] && storage[i]->free_func)
521                         {
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                 {
531                 sk_free(ad->sk);
532                 ad->sk=NULL;
533                 }
534         }
535
536 /********************************************************************/
537 /* API functions that defer all "state" operations to the "ex_data"
538  * implementation we have set. */
539
540 /* Obtain an index for a new class (not the same as getting a new index within
541  * an existing class - this is actually getting a new *class*) */
542 int CRYPTO_ex_data_new_class(void)
543         {
544         IMPL_CHECK
545         return EX_IMPL(new_class)();
546         }
547
548 /* Release all "ex_data" state to prevent memory leaks. This can't be made
549  * thread-safe without overhauling a lot of stuff, and shouldn't really be
550  * called under potential race-conditions anyway (it's for program shutdown
551  * after all). */
552 void CRYPTO_cleanup_all_ex_data(void)
553         {
554         IMPL_CHECK
555         EX_IMPL(cleanup)();
556         }
557
558 /* Inside an existing class, get/register a new index. */
559 int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
560                 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
561                 CRYPTO_EX_free *free_func)
562         {
563         int ret = -1;
564
565         IMPL_CHECK
566         ret = EX_IMPL(get_new_index)(class_index,
567                         argl, argp, new_func, dup_func, free_func);
568         return ret;
569         }
570
571 /* Initialise a new CRYPTO_EX_DATA for use in a particular class - including
572  * calling new() callbacks for each index in the class used by this variable */
573 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
574         {
575         IMPL_CHECK
576         return EX_IMPL(new_ex_data)(class_index, obj, ad);
577         }
578
579 /* Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks for
580  * each index in the class used by this variable */
581 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
582              CRYPTO_EX_DATA *from)
583         {
584         IMPL_CHECK
585         return EX_IMPL(dup_ex_data)(class_index, to, from);
586         }
587
588 /* Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
589  * each index in the class used by this variable */
590 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
591         {
592         IMPL_CHECK
593         EX_IMPL(free_ex_data)(class_index, obj, ad);
594         }
595
596 /* For a given CRYPTO_EX_DATA variable, set the value corresponding to a
597  * particular index in the class used by this variable */
598 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
599         {
600         int i;
601
602         if (ad->sk == NULL)
603                 {
604                 if ((ad->sk=sk_new_null()) == NULL)
605                         {
606                         CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
607                         return(0);
608                         }
609                 }
610         i=sk_num(ad->sk);
611
612         while (i <= idx)
613                 {
614                 if (!sk_push(ad->sk,NULL))
615                         {
616                         CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
617                         return(0);
618                         }
619                 i++;
620                 }
621         sk_set(ad->sk,idx,val);
622         return(1);
623         }
624
625 /* For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
626  * particular index in the class used by this variable */
627 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
628         {
629         if (ad->sk == NULL)
630                 return(0);
631         else if (idx >= sk_num(ad->sk))
632                 return(0);
633         else
634                 return(sk_value(ad->sk,idx));
635         }
636
637 IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS)