]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ex_data.c
Rewrite crypto/ex_data
[thirdparty/openssl.git] / crypto / ex_data.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57 /* ====================================================================
58 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110
111 #include "internal/cryptlib.h"
112 #include <openssl/lhash.h>
113
114
115 typedef struct {
116 long argl; /* Arbitary long */
117 void *argp; /* Arbitary void * */
118 CRYPTO_EX_new *new_func;
119 CRYPTO_EX_free *free_func;
120 CRYPTO_EX_dup *dup_func;
121 } CRYPTO_EX_DATA_FUNCS;
122
123 DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS)
124
125 /*
126 * State for each class; could just be a typedef, but this allows future
127 * changes.
128 */
129 typedef struct {
130 STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
131 } EX_CLASS_ITEM;
132
133 static EX_CLASS_ITEM ex_data[CRYPTO_EX_INDEX__COUNT];
134
135 /*
136 * Return the EX_CLASS_ITEM from the "ex_data" array that corresponds to
137 * a given class. On success, *holds the lock.*
138 */
139 static EX_CLASS_ITEM *def_get_class(int class_index)
140 {
141 EX_CLASS_ITEM *ip;
142
143 if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
144 CRYPTOerr(CRYPTO_F_DEF_GET_CLASS, ERR_R_MALLOC_FAILURE);
145 return NULL;
146 }
147
148 ip = &ex_data[class_index];
149 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
150 if (ip->meth == NULL) {
151 ip->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null();
152 /* We push an initial value on the stack because the SSL
153 * "app_data" routines use ex_data index zero. See RT 3710. */
154 if (ip->meth == NULL
155 || !sk_CRYPTO_EX_DATA_FUNCS_push(ip->meth, NULL)) {
156 CRYPTOerr(CRYPTO_F_DEF_GET_CLASS, ERR_R_MALLOC_FAILURE);
157 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
158 return NULL;
159 }
160 }
161 return ip;
162 }
163
164 static void cleanup_cb(CRYPTO_EX_DATA_FUNCS *funcs)
165 {
166 OPENSSL_free(funcs);
167 }
168
169 /*
170 * Release all "ex_data" state to prevent memory leaks. This can't be made
171 * thread-safe without overhauling a lot of stuff, and shouldn't really be
172 * called under potential race-conditions anyway (it's for program shutdown
173 * after all).
174 */
175 void CRYPTO_cleanup_all_ex_data(void)
176 {
177 int i;
178
179 for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
180 EX_CLASS_ITEM *ip = &ex_data[i];
181
182 sk_CRYPTO_EX_DATA_FUNCS_pop_free(ip->meth, cleanup_cb);
183 ip->meth = NULL;
184 }
185 }
186
187 /*
188 * Inside an existing class, get/register a new index.
189 */
190 int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
191 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
192 CRYPTO_EX_free *free_func)
193 {
194 int toret = -1;
195 CRYPTO_EX_DATA_FUNCS *a;
196 EX_CLASS_ITEM *ip = def_get_class(class_index);
197
198 if (!ip)
199 return -1;
200 a = (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(sizeof(*a));
201 if (!a) {
202 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
203 goto err;
204 }
205 a->argl = argl;
206 a->argp = argp;
207 a->new_func = new_func;
208 a->dup_func = dup_func;
209 a->free_func = free_func;
210
211 if (!sk_CRYPTO_EX_DATA_FUNCS_push(ip->meth, NULL)) {
212 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
213 OPENSSL_free(a);
214 goto err;
215 }
216 toret = sk_CRYPTO_EX_DATA_FUNCS_num(ip->meth) - 1;
217 (void)sk_CRYPTO_EX_DATA_FUNCS_set(ip->meth, toret, a);
218
219 err:
220 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
221 return toret;
222 }
223
224 /*
225 * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
226 * calling new() callbacks for each index in the class used by this variable
227 * Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries
228 * in the lock, then using them outside the lock. Note this only applies
229 * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
230 */
231 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
232 {
233 int mx, i;
234 void *ptr;
235 CRYPTO_EX_DATA_FUNCS **storage = NULL;
236 CRYPTO_EX_DATA_FUNCS *stack[10];
237 EX_CLASS_ITEM *ip = def_get_class(class_index);
238
239 if (!ip)
240 return 0;
241
242 ad->sk = NULL;
243
244 mx = sk_CRYPTO_EX_DATA_FUNCS_num(ip->meth);
245 if (mx > 0) {
246 if (mx < (int)OSSL_NELEM(stack))
247 storage = stack;
248 else
249 storage = OPENSSL_malloc(sizeof(*storage) * mx);
250 if (storage)
251 for (i = 0; i < mx; i++)
252 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(ip->meth, i);
253 }
254 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
255
256 if (mx > 0 && storage == NULL) {
257 CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
258 return 0;
259 }
260 for (i = 0; i < mx; i++) {
261 if (storage[i] && storage[i]->new_func) {
262 ptr = CRYPTO_get_ex_data(ad, i);
263 storage[i]->new_func(obj, ptr, ad, i,
264 storage[i]->argl, storage[i]->argp);
265 }
266 }
267 if (storage != stack)
268 OPENSSL_free(storage);
269 return 1;
270 }
271
272 /*
273 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
274 * for each index in the class used by this variable
275 */
276 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
277 CRYPTO_EX_DATA *from)
278 {
279 int mx, j, i;
280 char *ptr;
281 CRYPTO_EX_DATA_FUNCS *stack[10];
282 CRYPTO_EX_DATA_FUNCS **storage = NULL;
283 EX_CLASS_ITEM *ip;
284
285 if (from->sk == NULL)
286 /* Nothing to copy over */
287 return 1;
288 if ((ip = def_get_class(class_index)) == NULL)
289 return 0;
290
291 mx = sk_CRYPTO_EX_DATA_FUNCS_num(ip->meth);
292 j = sk_void_num(from->sk);
293 if (j < mx)
294 mx = j;
295 if (mx > 0) {
296 if (mx < (int)OSSL_NELEM(stack))
297 storage = stack;
298 else
299 storage = OPENSSL_malloc(sizeof(*storage) * mx);
300 if (storage)
301 for (i = 0; i < mx; i++)
302 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(ip->meth, i);
303 }
304 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
305
306 if (mx > 0 && storage == NULL) {
307 CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
308 return 0;
309 }
310
311 for (i = 0; i < mx; i++) {
312 ptr = CRYPTO_get_ex_data(from, i);
313 if (storage[i] && storage[i]->dup_func)
314 storage[i]->dup_func(to, from, &ptr, i,
315 storage[i]->argl, storage[i]->argp);
316 CRYPTO_set_ex_data(to, i, ptr);
317 }
318 if (storage != stack)
319 OPENSSL_free(storage);
320 return 1;
321 }
322
323
324 /*
325 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
326 * each index in the class used by this variable
327 */
328 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
329 {
330 int mx, i;
331 EX_CLASS_ITEM *ip;
332 void *ptr;
333 CRYPTO_EX_DATA_FUNCS *stack[10];
334 CRYPTO_EX_DATA_FUNCS **storage = NULL;
335
336 if ((ip = def_get_class(class_index)) == NULL)
337 return;
338
339 mx = sk_CRYPTO_EX_DATA_FUNCS_num(ip->meth);
340 if (mx > 0) {
341 if (mx < (int)OSSL_NELEM(stack))
342 storage = stack;
343 else
344 storage = OPENSSL_malloc(sizeof(*storage) * mx);
345 if (storage)
346 for (i = 0; i < mx; i++)
347 storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(ip->meth, i);
348 }
349 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
350
351 if (mx > 0 && storage == NULL) {
352 CRYPTOerr(CRYPTO_F_CRYPTO_FREE_EX_DATA, ERR_R_MALLOC_FAILURE);
353 return;
354 }
355 for (i = 0; i < mx; i++) {
356 if (storage[i] && storage[i]->free_func) {
357 ptr = CRYPTO_get_ex_data(ad, i);
358 storage[i]->free_func(obj, ptr, ad, i,
359 storage[i]->argl, storage[i]->argp);
360 }
361 }
362
363 if (storage != stack)
364 OPENSSL_free(storage);
365 sk_void_free(ad->sk);
366 ad->sk = NULL;
367 }
368
369 /*
370 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
371 * particular index in the class used by this variable
372 */
373 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
374 {
375 int i;
376
377 if (ad->sk == NULL) {
378 if ((ad->sk = sk_void_new_null()) == NULL) {
379 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
380 return 0;
381 }
382 }
383
384 for (i = sk_void_num(ad->sk); i <= idx; ++i) {
385 if (!sk_void_push(ad->sk, NULL)) {
386 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
387 return 0;
388 }
389 }
390 sk_void_set(ad->sk, idx, val);
391 return 1;
392 }
393
394 /*
395 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
396 * particular index in the class used by this variable
397 */
398 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
399 {
400 if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
401 return NULL;
402 return sk_void_value(ad->sk, idx);
403 }