10Sstevel@tonic-gate /* crypto/ex_data.c */
20Sstevel@tonic-gate
30Sstevel@tonic-gate /*
40Sstevel@tonic-gate * Overhaul notes;
50Sstevel@tonic-gate *
60Sstevel@tonic-gate * This code is now *mostly* thread-safe. It is now easier to understand in what
70Sstevel@tonic-gate * ways it is safe and in what ways it is not, which is an improvement. Firstly,
80Sstevel@tonic-gate * all per-class stacks and index-counters for ex_data are stored in the same
90Sstevel@tonic-gate * global LHASH table (keyed by class). This hash table uses locking for all
100Sstevel@tonic-gate * access with the exception of CRYPTO_cleanup_all_ex_data(), which must only be
110Sstevel@tonic-gate * called when no other threads can possibly race against it (even if it was
120Sstevel@tonic-gate * locked, the race would mean it's possible the hash table might have been
130Sstevel@tonic-gate * recreated after the cleanup). As classes can only be added to the hash table,
140Sstevel@tonic-gate * and within each class, the stack of methods can only be incremented, the
150Sstevel@tonic-gate * locking mechanics are simpler than they would otherwise be. For example, the
160Sstevel@tonic-gate * new/dup/free ex_data functions will lock the hash table, copy the method
170Sstevel@tonic-gate * pointers it needs from the relevant class, then unlock the hash table before
180Sstevel@tonic-gate * actually applying those method pointers to the task of the new/dup/free
190Sstevel@tonic-gate * operations. As they can't be removed from the method-stack, only
200Sstevel@tonic-gate * supplemented, there's no race conditions associated with using them outside
210Sstevel@tonic-gate * the lock. The get/set_ex_data functions are not locked because they do not
220Sstevel@tonic-gate * involve this global state at all - they operate directly with a previously
230Sstevel@tonic-gate * obtained per-class method index and a particular "ex_data" variable. These
240Sstevel@tonic-gate * variables are usually instantiated per-context (eg. each RSA structure has
250Sstevel@tonic-gate * one) so locking on read/write access to that variable can be locked locally
260Sstevel@tonic-gate * if required (eg. using the "RSA" lock to synchronise access to a
270Sstevel@tonic-gate * per-RSA-structure ex_data variable if required).
280Sstevel@tonic-gate * [Geoff]
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
320Sstevel@tonic-gate * All rights reserved.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * This package is an SSL implementation written
350Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
360Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
370Sstevel@tonic-gate *
380Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
390Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
400Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
410Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
420Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
430Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
440Sstevel@tonic-gate *
450Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
460Sstevel@tonic-gate * the code are not to be removed.
470Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
480Sstevel@tonic-gate * as the author of the parts of the library used.
490Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
500Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
510Sstevel@tonic-gate *
520Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
530Sstevel@tonic-gate * modification, are permitted provided that the following conditions
540Sstevel@tonic-gate * are met:
550Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
560Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
570Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
580Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
590Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
600Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
610Sstevel@tonic-gate * must display the following acknowledgement:
620Sstevel@tonic-gate * "This product includes cryptographic software written by
630Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
640Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
650Sstevel@tonic-gate * being used are not cryptographic related :-).
660Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
670Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
680Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
690Sstevel@tonic-gate *
700Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
710Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
720Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
730Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
740Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
750Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
760Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
770Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
780Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
790Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
800Sstevel@tonic-gate * SUCH DAMAGE.
810Sstevel@tonic-gate *
820Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
830Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
840Sstevel@tonic-gate * copied and put under another distribution licence
850Sstevel@tonic-gate * [including the GNU Public Licence.]
860Sstevel@tonic-gate */
870Sstevel@tonic-gate /* ====================================================================
880Sstevel@tonic-gate * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
890Sstevel@tonic-gate *
900Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
910Sstevel@tonic-gate * modification, are permitted provided that the following conditions
920Sstevel@tonic-gate * are met:
930Sstevel@tonic-gate *
940Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
950Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
960Sstevel@tonic-gate *
970Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
980Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
990Sstevel@tonic-gate * the documentation and/or other materials provided with the
1000Sstevel@tonic-gate * distribution.
1010Sstevel@tonic-gate *
1020Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
1030Sstevel@tonic-gate * software must display the following acknowledgment:
1040Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
1050Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
1060Sstevel@tonic-gate *
1070Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1080Sstevel@tonic-gate * endorse or promote products derived from this software without
1090Sstevel@tonic-gate * prior written permission. For written permission, please contact
1100Sstevel@tonic-gate * openssl-core@openssl.org.
1110Sstevel@tonic-gate *
1120Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
1130Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
1140Sstevel@tonic-gate * permission of the OpenSSL Project.
1150Sstevel@tonic-gate *
1160Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
1170Sstevel@tonic-gate * acknowledgment:
1180Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
1190Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
1200Sstevel@tonic-gate *
1210Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1220Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1230Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1240Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1250Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1260Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1270Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1280Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1290Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1300Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1310Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1320Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
1330Sstevel@tonic-gate * ====================================================================
1340Sstevel@tonic-gate *
1350Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
1360Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
1370Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
1380Sstevel@tonic-gate *
1390Sstevel@tonic-gate */
1400Sstevel@tonic-gate
141*2139Sjp161948 #include "cryptlib.h"
1420Sstevel@tonic-gate #include <openssl/lhash.h>
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate /* What an "implementation of ex_data functionality" looks like */
1450Sstevel@tonic-gate struct st_CRYPTO_EX_DATA_IMPL
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate /*********************/
1480Sstevel@tonic-gate /* GLOBAL OPERATIONS */
1490Sstevel@tonic-gate /* Return a new class index */
1500Sstevel@tonic-gate int (*cb_new_class)(void);
1510Sstevel@tonic-gate /* Cleanup all state used by the implementation */
1520Sstevel@tonic-gate void (*cb_cleanup)(void);
1530Sstevel@tonic-gate /************************/
1540Sstevel@tonic-gate /* PER-CLASS OPERATIONS */
1550Sstevel@tonic-gate /* Get a new method index within a class */
1560Sstevel@tonic-gate int (*cb_get_new_index)(int class_index, long argl, void *argp,
1570Sstevel@tonic-gate CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
1580Sstevel@tonic-gate CRYPTO_EX_free *free_func);
1590Sstevel@tonic-gate /* Initialise a new CRYPTO_EX_DATA of a given class */
1600Sstevel@tonic-gate int (*cb_new_ex_data)(int class_index, void *obj,
1610Sstevel@tonic-gate CRYPTO_EX_DATA *ad);
1620Sstevel@tonic-gate /* Duplicate a CRYPTO_EX_DATA of a given class onto a copy */
1630Sstevel@tonic-gate int (*cb_dup_ex_data)(int class_index, CRYPTO_EX_DATA *to,
1640Sstevel@tonic-gate CRYPTO_EX_DATA *from);
1650Sstevel@tonic-gate /* Cleanup a CRYPTO_EX_DATA of a given class */
1660Sstevel@tonic-gate void (*cb_free_ex_data)(int class_index, void *obj,
1670Sstevel@tonic-gate CRYPTO_EX_DATA *ad);
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /* The implementation we use at run-time */
1710Sstevel@tonic-gate static const CRYPTO_EX_DATA_IMPL *impl = NULL;
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /* To call "impl" functions, use this macro rather than referring to 'impl' directly, eg.
1740Sstevel@tonic-gate * EX_IMPL(get_new_index)(...); */
1750Sstevel@tonic-gate #define EX_IMPL(a) impl->cb_##a
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate /* Predeclare the "default" ex_data implementation */
1780Sstevel@tonic-gate static int int_new_class(void);
1790Sstevel@tonic-gate static void int_cleanup(void);
1800Sstevel@tonic-gate static int int_get_new_index(int class_index, long argl, void *argp,
1810Sstevel@tonic-gate CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
1820Sstevel@tonic-gate CRYPTO_EX_free *free_func);
1830Sstevel@tonic-gate static int int_new_ex_data(int class_index, void *obj,
1840Sstevel@tonic-gate CRYPTO_EX_DATA *ad);
1850Sstevel@tonic-gate static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
1860Sstevel@tonic-gate CRYPTO_EX_DATA *from);
1870Sstevel@tonic-gate static void int_free_ex_data(int class_index, void *obj,
1880Sstevel@tonic-gate CRYPTO_EX_DATA *ad);
1890Sstevel@tonic-gate static CRYPTO_EX_DATA_IMPL impl_default =
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate int_new_class,
1920Sstevel@tonic-gate int_cleanup,
1930Sstevel@tonic-gate int_get_new_index,
1940Sstevel@tonic-gate int_new_ex_data,
1950Sstevel@tonic-gate int_dup_ex_data,
1960Sstevel@tonic-gate int_free_ex_data
1970Sstevel@tonic-gate };
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate /* Internal function that checks whether "impl" is set and if not, sets it to
2000Sstevel@tonic-gate * the default. */
impl_check(void)2010Sstevel@tonic-gate static void impl_check(void)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
2040Sstevel@tonic-gate if(!impl)
2050Sstevel@tonic-gate impl = &impl_default;
2060Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate /* A macro wrapper for impl_check that first uses a non-locked test before
2090Sstevel@tonic-gate * invoking the function (which checks again inside a lock). */
2100Sstevel@tonic-gate #define IMPL_CHECK if(!impl) impl_check();
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /* API functions to get/set the "ex_data" implementation */
CRYPTO_get_ex_data_implementation(void)2130Sstevel@tonic-gate const CRYPTO_EX_DATA_IMPL *CRYPTO_get_ex_data_implementation(void)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate IMPL_CHECK
2160Sstevel@tonic-gate return impl;
2170Sstevel@tonic-gate }
CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL * i)2180Sstevel@tonic-gate int CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate int toret = 0;
2210Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
2220Sstevel@tonic-gate if(!impl)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate impl = i;
2250Sstevel@tonic-gate toret = 1;
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
2280Sstevel@tonic-gate return toret;
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate /****************************************************************************/
2320Sstevel@tonic-gate /* Interal (default) implementation of "ex_data" support. API functions are
2330Sstevel@tonic-gate * further down. */
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate /* The type that represents what each "class" used to implement locally. A STACK
2360Sstevel@tonic-gate * of CRYPTO_EX_DATA_FUNCS plus a index-counter. The 'class_index' is the global
2370Sstevel@tonic-gate * value representing the class that is used to distinguish these items. */
2380Sstevel@tonic-gate typedef struct st_ex_class_item {
2390Sstevel@tonic-gate int class_index;
2400Sstevel@tonic-gate STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
2410Sstevel@tonic-gate int meth_num;
2420Sstevel@tonic-gate } EX_CLASS_ITEM;
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate /* When assigning new class indexes, this is our counter */
2450Sstevel@tonic-gate static int ex_class = CRYPTO_EX_INDEX_USER;
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate /* The global hash table of EX_CLASS_ITEM items */
2480Sstevel@tonic-gate static LHASH *ex_data = NULL;
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate /* The callbacks required in the "ex_data" hash table */
ex_hash_cb(const void * a_void)2510Sstevel@tonic-gate static unsigned long ex_hash_cb(const void *a_void)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate return ((const EX_CLASS_ITEM *)a_void)->class_index;
2540Sstevel@tonic-gate }
ex_cmp_cb(const void * a_void,const void * b_void)2550Sstevel@tonic-gate static int ex_cmp_cb(const void *a_void, const void *b_void)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate return (((const EX_CLASS_ITEM *)a_void)->class_index -
2580Sstevel@tonic-gate ((const EX_CLASS_ITEM *)b_void)->class_index);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /* Internal functions used by the "impl_default" implementation to access the
2620Sstevel@tonic-gate * state */
2630Sstevel@tonic-gate
ex_data_check(void)2640Sstevel@tonic-gate static int ex_data_check(void)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate int toret = 1;
2670Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
2680Sstevel@tonic-gate if(!ex_data && ((ex_data = lh_new(ex_hash_cb, ex_cmp_cb)) == NULL))
2690Sstevel@tonic-gate toret = 0;
2700Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
2710Sstevel@tonic-gate return toret;
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate /* This macros helps reduce the locking from repeated checks because the
2740Sstevel@tonic-gate * ex_data_check() function checks ex_data again inside a lock. */
2750Sstevel@tonic-gate #define EX_DATA_CHECK(iffail) if(!ex_data && !ex_data_check()) {iffail}
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate /* This "inner" callback is used by the callback function that follows it */
def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS * funcs)2780Sstevel@tonic-gate static void def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS *funcs)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate OPENSSL_free(funcs);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate /* This callback is used in lh_doall to destroy all EX_CLASS_ITEM values from
2840Sstevel@tonic-gate * "ex_data" prior to the ex_data hash table being itself destroyed. Doesn't do
2850Sstevel@tonic-gate * any locking. */
def_cleanup_cb(void * a_void)286*2139Sjp161948 static void def_cleanup_cb(void *a_void)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate EX_CLASS_ITEM *item = (EX_CLASS_ITEM *)a_void;
2890Sstevel@tonic-gate sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, def_cleanup_util_cb);
2900Sstevel@tonic-gate OPENSSL_free(item);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate /* Return the EX_CLASS_ITEM from the "ex_data" hash table that corresponds to a
2940Sstevel@tonic-gate * given class. Handles locking. */
def_get_class(int class_index)2950Sstevel@tonic-gate static EX_CLASS_ITEM *def_get_class(int class_index)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate EX_CLASS_ITEM d, *p, *gen;
2980Sstevel@tonic-gate EX_DATA_CHECK(return NULL;)
2990Sstevel@tonic-gate d.class_index = class_index;
3000Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
3010Sstevel@tonic-gate p = lh_retrieve(ex_data, &d);
3020Sstevel@tonic-gate if(!p)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate gen = OPENSSL_malloc(sizeof(EX_CLASS_ITEM));
3050Sstevel@tonic-gate if(gen)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate gen->class_index = class_index;
3080Sstevel@tonic-gate gen->meth_num = 0;
3090Sstevel@tonic-gate gen->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null();
3100Sstevel@tonic-gate if(!gen->meth)
3110Sstevel@tonic-gate OPENSSL_free(gen);
3120Sstevel@tonic-gate else
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate /* Because we're inside the ex_data lock, the
3150Sstevel@tonic-gate * return value from the insert will be NULL */
3160Sstevel@tonic-gate lh_insert(ex_data, gen);
3170Sstevel@tonic-gate p = gen;
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
3220Sstevel@tonic-gate if(!p)
3230Sstevel@tonic-gate CRYPTOerr(CRYPTO_F_DEF_GET_CLASS,ERR_R_MALLOC_FAILURE);
3240Sstevel@tonic-gate return p;
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate /* Add a new method to the given EX_CLASS_ITEM and return the corresponding
3280Sstevel@tonic-gate * index (or -1 for error). Handles locking. */
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)3290Sstevel@tonic-gate static int def_add_index(EX_CLASS_ITEM *item, long argl, void *argp,
3300Sstevel@tonic-gate CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
3310Sstevel@tonic-gate CRYPTO_EX_free *free_func)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate int toret = -1;
3340Sstevel@tonic-gate CRYPTO_EX_DATA_FUNCS *a = (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(
3350Sstevel@tonic-gate sizeof(CRYPTO_EX_DATA_FUNCS));
3360Sstevel@tonic-gate if(!a)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX,ERR_R_MALLOC_FAILURE);
3390Sstevel@tonic-gate return -1;
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate a->argl=argl;
3420Sstevel@tonic-gate a->argp=argp;
3430Sstevel@tonic-gate a->new_func=new_func;
3440Sstevel@tonic-gate a->dup_func=dup_func;
3450Sstevel@tonic-gate a->free_func=free_func;
3460Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
3470Sstevel@tonic-gate while (sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) <= item->meth_num)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, NULL))
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX,ERR_R_MALLOC_FAILURE);
3520Sstevel@tonic-gate OPENSSL_free(a);
3530Sstevel@tonic-gate goto err;
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate toret = item->meth_num++;
3570Sstevel@tonic-gate sk_CRYPTO_EX_DATA_FUNCS_set(item->meth, toret, a);
3580Sstevel@tonic-gate err:
3590Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
3600Sstevel@tonic-gate return toret;
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate /**************************************************************/
3640Sstevel@tonic-gate /* The functions in the default CRYPTO_EX_DATA_IMPL structure */
3650Sstevel@tonic-gate
int_new_class(void)3660Sstevel@tonic-gate static int int_new_class(void)
3670Sstevel@tonic-gate {
3680Sstevel@tonic-gate int toret;
3690Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
3700Sstevel@tonic-gate toret = ex_class++;
3710Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
3720Sstevel@tonic-gate return toret;
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate
int_cleanup(void)3750Sstevel@tonic-gate static void int_cleanup(void)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate EX_DATA_CHECK(return;)
3780Sstevel@tonic-gate lh_doall(ex_data, def_cleanup_cb);
3790Sstevel@tonic-gate lh_free(ex_data);
3800Sstevel@tonic-gate ex_data = NULL;
3810Sstevel@tonic-gate impl = NULL;
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate
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)3840Sstevel@tonic-gate static int int_get_new_index(int class_index, long argl, void *argp,
3850Sstevel@tonic-gate CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
3860Sstevel@tonic-gate CRYPTO_EX_free *free_func)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate EX_CLASS_ITEM *item = def_get_class(class_index);
3890Sstevel@tonic-gate if(!item)
3900Sstevel@tonic-gate return -1;
3910Sstevel@tonic-gate return def_add_index(item, argl, argp, new_func, dup_func, free_func);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate /* Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries in
3950Sstevel@tonic-gate * the lock, then using them outside the lock. NB: Thread-safety only applies to
3960Sstevel@tonic-gate * the global "ex_data" state (ie. class definitions), not thread-safe on 'ad'
3970Sstevel@tonic-gate * itself. */
int_new_ex_data(int class_index,void * obj,CRYPTO_EX_DATA * ad)3980Sstevel@tonic-gate static int int_new_ex_data(int class_index, void *obj,
3990Sstevel@tonic-gate CRYPTO_EX_DATA *ad)
4000Sstevel@tonic-gate {
4010Sstevel@tonic-gate int mx,i;
4020Sstevel@tonic-gate void *ptr;
4030Sstevel@tonic-gate CRYPTO_EX_DATA_FUNCS **storage = NULL;
4040Sstevel@tonic-gate EX_CLASS_ITEM *item = def_get_class(class_index);
4050Sstevel@tonic-gate if(!item)
4060Sstevel@tonic-gate /* error is already set */
4070Sstevel@tonic-gate return 0;
4080Sstevel@tonic-gate ad->sk = NULL;
4090Sstevel@tonic-gate CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
4100Sstevel@tonic-gate mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
4110Sstevel@tonic-gate if(mx > 0)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
4140Sstevel@tonic-gate if(!storage)
4150Sstevel@tonic-gate goto skip;
4160Sstevel@tonic-gate for(i = 0; i < mx; i++)
4170Sstevel@tonic-gate storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate skip:
4200Sstevel@tonic-gate CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
4210Sstevel@tonic-gate if((mx > 0) && !storage)
4220Sstevel@tonic-gate {
4230Sstevel@tonic-gate CRYPTOerr(CRYPTO_F_INT_NEW_EX_DATA,ERR_R_MALLOC_FAILURE);
4240Sstevel@tonic-gate return 0;
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate for(i = 0; i < mx; i++)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate if(storage[i] && storage[i]->new_func)
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate ptr = CRYPTO_get_ex_data(ad, i);
4310Sstevel@tonic-gate storage[i]->new_func(obj,ptr,ad,i,
4320Sstevel@tonic-gate storage[i]->argl,storage[i]->argp);
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate if(storage)
4360Sstevel@tonic-gate OPENSSL_free(storage);
4370Sstevel@tonic-gate return 1;
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate /* 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)4410Sstevel@tonic-gate static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
4420Sstevel@tonic-gate CRYPTO_EX_DATA *from)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate int mx, j, i;
4450Sstevel@tonic-gate char *ptr;
4460Sstevel@tonic-gate CRYPTO_EX_DATA_FUNCS **storage = NULL;
4470Sstevel@tonic-gate EX_CLASS_ITEM *item;
4480Sstevel@tonic-gate if(!from->sk)
4490Sstevel@tonic-gate /* 'to' should be "blank" which *is* just like 'from' */
4500Sstevel@tonic-gate return 1;
4510Sstevel@tonic-gate if((item = def_get_class(class_index)) == NULL)
4520Sstevel@tonic-gate return 0;
4530Sstevel@tonic-gate CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
4540Sstevel@tonic-gate mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
4550Sstevel@tonic-gate j = sk_num(from->sk);
4560Sstevel@tonic-gate if(j < mx)
4570Sstevel@tonic-gate mx = j;
4580Sstevel@tonic-gate if(mx > 0)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
4610Sstevel@tonic-gate if(!storage)
4620Sstevel@tonic-gate goto skip;
4630Sstevel@tonic-gate for(i = 0; i < mx; i++)
4640Sstevel@tonic-gate storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate skip:
4670Sstevel@tonic-gate CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
4680Sstevel@tonic-gate if((mx > 0) && !storage)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate CRYPTOerr(CRYPTO_F_INT_DUP_EX_DATA,ERR_R_MALLOC_FAILURE);
4710Sstevel@tonic-gate return 0;
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate for(i = 0; i < mx; i++)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate ptr = CRYPTO_get_ex_data(from, i);
4760Sstevel@tonic-gate if(storage[i] && storage[i]->dup_func)
4770Sstevel@tonic-gate storage[i]->dup_func(to,from,&ptr,i,
4780Sstevel@tonic-gate storage[i]->argl,storage[i]->argp);
4790Sstevel@tonic-gate CRYPTO_set_ex_data(to,i,ptr);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate if(storage)
4820Sstevel@tonic-gate OPENSSL_free(storage);
4830Sstevel@tonic-gate return 1;
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate /* Same thread-safety notes as for "int_new_ex_data" */
int_free_ex_data(int class_index,void * obj,CRYPTO_EX_DATA * ad)4870Sstevel@tonic-gate static void int_free_ex_data(int class_index, void *obj,
4880Sstevel@tonic-gate CRYPTO_EX_DATA *ad)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate int mx,i;
4910Sstevel@tonic-gate EX_CLASS_ITEM *item;
4920Sstevel@tonic-gate void *ptr;
4930Sstevel@tonic-gate CRYPTO_EX_DATA_FUNCS **storage = NULL;
4940Sstevel@tonic-gate if((item = def_get_class(class_index)) == NULL)
4950Sstevel@tonic-gate return;
4960Sstevel@tonic-gate CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
4970Sstevel@tonic-gate mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
4980Sstevel@tonic-gate if(mx > 0)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
5010Sstevel@tonic-gate if(!storage)
5020Sstevel@tonic-gate goto skip;
5030Sstevel@tonic-gate for(i = 0; i < mx; i++)
5040Sstevel@tonic-gate storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i);
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate skip:
5070Sstevel@tonic-gate CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
5080Sstevel@tonic-gate if((mx > 0) && !storage)
5090Sstevel@tonic-gate {
5100Sstevel@tonic-gate CRYPTOerr(CRYPTO_F_INT_FREE_EX_DATA,ERR_R_MALLOC_FAILURE);
5110Sstevel@tonic-gate return;
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate for(i = 0; i < mx; i++)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate if(storage[i] && storage[i]->free_func)
5160Sstevel@tonic-gate {
5170Sstevel@tonic-gate ptr = CRYPTO_get_ex_data(ad,i);
5180Sstevel@tonic-gate storage[i]->free_func(obj,ptr,ad,i,
5190Sstevel@tonic-gate storage[i]->argl,storage[i]->argp);
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate if(storage)
5230Sstevel@tonic-gate OPENSSL_free(storage);
5240Sstevel@tonic-gate if(ad->sk)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate sk_free(ad->sk);
5270Sstevel@tonic-gate ad->sk=NULL;
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate
5310Sstevel@tonic-gate /********************************************************************/
5320Sstevel@tonic-gate /* API functions that defer all "state" operations to the "ex_data"
5330Sstevel@tonic-gate * implementation we have set. */
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate /* Obtain an index for a new class (not the same as getting a new index within
5360Sstevel@tonic-gate * an existing class - this is actually getting a new *class*) */
CRYPTO_ex_data_new_class(void)5370Sstevel@tonic-gate int CRYPTO_ex_data_new_class(void)
5380Sstevel@tonic-gate {
5390Sstevel@tonic-gate IMPL_CHECK
5400Sstevel@tonic-gate return EX_IMPL(new_class)();
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate
5430Sstevel@tonic-gate /* Release all "ex_data" state to prevent memory leaks. This can't be made
5440Sstevel@tonic-gate * thread-safe without overhauling a lot of stuff, and shouldn't really be
5450Sstevel@tonic-gate * called under potential race-conditions anyway (it's for program shutdown
5460Sstevel@tonic-gate * after all). */
CRYPTO_cleanup_all_ex_data(void)5470Sstevel@tonic-gate void CRYPTO_cleanup_all_ex_data(void)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate IMPL_CHECK
5500Sstevel@tonic-gate EX_IMPL(cleanup)();
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate
5530Sstevel@tonic-gate /* 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)5540Sstevel@tonic-gate int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
5550Sstevel@tonic-gate CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
5560Sstevel@tonic-gate CRYPTO_EX_free *free_func)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate int ret = -1;
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate IMPL_CHECK
5610Sstevel@tonic-gate ret = EX_IMPL(get_new_index)(class_index,
5620Sstevel@tonic-gate argl, argp, new_func, dup_func, free_func);
5630Sstevel@tonic-gate return ret;
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate /* Initialise a new CRYPTO_EX_DATA for use in a particular class - including
5670Sstevel@tonic-gate * calling new() callbacks for each index in the class used by this variable */
CRYPTO_new_ex_data(int class_index,void * obj,CRYPTO_EX_DATA * ad)5680Sstevel@tonic-gate int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate IMPL_CHECK
5710Sstevel@tonic-gate return EX_IMPL(new_ex_data)(class_index, obj, ad);
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate /* Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks for
5750Sstevel@tonic-gate * each index in the class used by this variable */
CRYPTO_dup_ex_data(int class_index,CRYPTO_EX_DATA * to,CRYPTO_EX_DATA * from)5760Sstevel@tonic-gate int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
5770Sstevel@tonic-gate CRYPTO_EX_DATA *from)
5780Sstevel@tonic-gate {
5790Sstevel@tonic-gate IMPL_CHECK
5800Sstevel@tonic-gate return EX_IMPL(dup_ex_data)(class_index, to, from);
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate /* Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
5840Sstevel@tonic-gate * each index in the class used by this variable */
CRYPTO_free_ex_data(int class_index,void * obj,CRYPTO_EX_DATA * ad)5850Sstevel@tonic-gate void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate IMPL_CHECK
5880Sstevel@tonic-gate EX_IMPL(free_ex_data)(class_index, obj, ad);
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate /* For a given CRYPTO_EX_DATA variable, set the value corresponding to a
5920Sstevel@tonic-gate * particular index in the class used by this variable */
CRYPTO_set_ex_data(CRYPTO_EX_DATA * ad,int idx,void * val)5930Sstevel@tonic-gate int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
5940Sstevel@tonic-gate {
5950Sstevel@tonic-gate int i;
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate if (ad->sk == NULL)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate if ((ad->sk=sk_new_null()) == NULL)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
6020Sstevel@tonic-gate return(0);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate i=sk_num(ad->sk);
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate while (i <= idx)
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate if (!sk_push(ad->sk,NULL))
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
6120Sstevel@tonic-gate return(0);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate i++;
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate sk_set(ad->sk,idx,val);
6170Sstevel@tonic-gate return(1);
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate /* For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
6210Sstevel@tonic-gate * particular index in the class used by this variable */
CRYPTO_get_ex_data(const CRYPTO_EX_DATA * ad,int idx)6220Sstevel@tonic-gate void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
6230Sstevel@tonic-gate {
6240Sstevel@tonic-gate if (ad->sk == NULL)
6250Sstevel@tonic-gate return(0);
6260Sstevel@tonic-gate else if (idx >= sk_num(ad->sk))
6270Sstevel@tonic-gate return(0);
6280Sstevel@tonic-gate else
6290Sstevel@tonic-gate return(sk_value(ad->sk,idx));
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS)
633