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