4 * This is not thread-safe, nor can it be changed to become thread-safe
5 * without changing various function prototypes and using a lot of locking.
6 * Luckily, it's not really used anywhere except in ssl_verify_cert_chain
7 * via SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c),
8 * where new_func, dup_func, and free_func all are 0, and in
9 * hwcrhk_init (crypto/engine/hw_ncipher.c), which is hopefully only
10 * ever used during program initialization.
12 * Any multi-threaded application crazy enough to use ex_data for its own
13 * purposes had better make sure that SSL_get_ex_data_X509_STORE_CTX_idx
14 * is called once before multiple threads are created.
17 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
18 * All rights reserved.
20 * This package is an SSL implementation written
21 * by Eric Young (eay@cryptsoft.com).
22 * The implementation was written so as to conform with Netscapes SSL.
24 * This library is free for commercial and non-commercial use as long as
25 * the following conditions are aheared to. The following conditions
26 * apply to all code found in this distribution, be it the RC4, RSA,
27 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
28 * included with this distribution is covered by the same copyright terms
29 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
31 * Copyright remains Eric Young's, and as such any Copyright notices in
32 * the code are not to be removed.
33 * If this package is used in a product, Eric Young should be given attribution
34 * as the author of the parts of the library used.
35 * This can be in the form of a textual message at program startup or
36 * in documentation (online or textual) provided with the package.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * "This product includes cryptographic software written by
49 * Eric Young (eay@cryptsoft.com)"
50 * The word 'cryptographic' can be left out if the rouines from the library
51 * being used are not cryptographic related :-).
52 * 4. If you include any Windows specific code (or a derivative thereof) from
53 * the apps directory (application code) you must include an acknowledgement:
54 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
56 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * The licence and distribution terms for any publically available version or
69 * derivative of this code cannot be changed. i.e. this code cannot simply be
70 * copied and put under another distribution licence
71 * [including the GNU Public Licence.]
76 #include <openssl/buffer.h>
77 #include <openssl/bio.h>
78 #include <openssl/lhash.h>
81 int CRYPTO_get_ex_new_index(int idx, STACK_OF(CRYPTO_EX_DATA_FUNCS) **skp, long argl, void *argp,
82 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
85 CRYPTO_EX_DATA_FUNCS *a;
89 *skp=sk_CRYPTO_EX_DATA_FUNCS_new_null();
92 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE);
95 a=(CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS));
98 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE);
103 a->new_func=new_func;
104 a->dup_func=dup_func;
105 a->free_func=free_func;
106 while (sk_CRYPTO_EX_DATA_FUNCS_num(*skp) <= idx)
108 if (!sk_CRYPTO_EX_DATA_FUNCS_push(*skp,NULL))
110 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE);
115 sk_CRYPTO_EX_DATA_FUNCS_set(*skp,idx, a);
122 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
128 if ((ad->sk=sk_new_null()) == NULL)
130 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
138 if (!sk_push(ad->sk,NULL))
140 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
145 sk_set(ad->sk,idx,val);
149 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
153 else if (idx >= sk_num(ad->sk))
156 return(sk_value(ad->sk,idx));
159 /* The callback is called with the 'object', which is the original data object
160 * being duplicated, a pointer to the
161 * 'new' object to be inserted, the index, and the argi/argp
163 int CRYPTO_dup_ex_data(STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth, CRYPTO_EX_DATA *to,
164 CRYPTO_EX_DATA *from)
167 CRYPTO_EX_DATA_FUNCS *mm;
170 if (meth == NULL) return(1);
171 if (from->sk == NULL) return(1);
172 m=sk_CRYPTO_EX_DATA_FUNCS_num(meth);
176 from_d=CRYPTO_get_ex_data(from,i);
179 mm=sk_CRYPTO_EX_DATA_FUNCS_value(meth,i);
180 if (mm->dup_func != NULL)
181 r=mm->dup_func(to,from,(char **)&from_d,i,
184 CRYPTO_set_ex_data(to,i,from_d);
189 /* Call each free callback */
190 void CRYPTO_free_ex_data(STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth, void *obj, CRYPTO_EX_DATA *ad)
192 CRYPTO_EX_DATA_FUNCS *m;
198 max=sk_CRYPTO_EX_DATA_FUNCS_num(meth);
199 for (i=0; i<max; i++)
201 m=sk_CRYPTO_EX_DATA_FUNCS_value(meth,i);
202 if ((m != NULL) && (m->free_func != NULL))
204 ptr=CRYPTO_get_ex_data(ad,i);
205 m->free_func(obj,ptr,ad,i,m->argl,m->argp);
216 void CRYPTO_new_ex_data(STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth, void *obj, CRYPTO_EX_DATA *ad)
218 CRYPTO_EX_DATA_FUNCS *m;
225 max=sk_CRYPTO_EX_DATA_FUNCS_num(meth);
226 for (i=0; i<max; i++)
228 m=sk_CRYPTO_EX_DATA_FUNCS_value(meth,i);
229 if ((m != NULL) && (m->new_func != NULL))
231 ptr=CRYPTO_get_ex_data(ad,i);
232 m->new_func(obj,ptr,ad,i,m->argl,m->argp);
238 IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS)