]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ex_data.c
ex_data part 2: doc fixes and CRYPTO_free_ex_index.
[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
116 /*
117 * Each structure type (sometimes called a class), that supports
118 * exdata has a stack of callbacks for each instance.
119 */
120 typedef struct ex_callback_st {
121 long argl; /* Arbitary long */
122 void *argp; /* Arbitary void * */
123 CRYPTO_EX_new *new_func;
124 CRYPTO_EX_free *free_func;
125 CRYPTO_EX_dup *dup_func;
126 } EX_CALLBACK;
127
128 DECLARE_STACK_OF(EX_CALLBACK)
129
130 /*
131 * The state for each class. This could just be a typedef, but
132 * a structure allows future changes.
133 */
134 typedef struct ex_callbacks_st {
135 STACK_OF(EX_CALLBACK) *meth;
136 } EX_CALLBACKS;
137
138 static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
139
140 /*
141 * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
142 * a given class. On success, *holds the lock.*
143 */
144 static EX_CALLBACKS *get_and_lock(int class_index)
145 {
146 EX_CALLBACKS *ip;
147
148 if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
149 CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
150 return NULL;
151 }
152
153 ip = &ex_data[class_index];
154 CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
155 if (ip->meth == NULL) {
156 ip->meth = sk_EX_CALLBACK_new_null();
157 /* We push an initial value on the stack because the SSL
158 * "app_data" routines use ex_data index zero. See RT 3710. */
159 if (ip->meth == NULL
160 || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
161 CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
162 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
163 return NULL;
164 }
165 }
166 return ip;
167 }
168
169 static void cleanup_cb(EX_CALLBACK *funcs)
170 {
171 OPENSSL_free(funcs);
172 }
173
174 /*
175 * Release all "ex_data" state to prevent memory leaks. This can't be made
176 * thread-safe without overhauling a lot of stuff, and shouldn't really be
177 * called under potential race-conditions anyway (it's for program shutdown
178 * after all).
179 */
180 void CRYPTO_cleanup_all_ex_data(void)
181 {
182 int i;
183
184 for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
185 EX_CALLBACKS *ip = &ex_data[i];
186
187 sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
188 ip->meth = NULL;
189 }
190 }
191
192
193 /*
194 * Unregister a new index by replacing the callbacks with no-ops.
195 * Any in-use instances are leaked.
196 */
197 static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
198 long argl, void *argp)
199 {
200 }
201
202 static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
203 long argl, void *argp)
204 {
205 }
206
207 static int dummy_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from,
208 void *from_d, int idx,
209 long argl, void *argp)
210 {
211 return 0;
212 }
213
214 int CRYPTO_free_ex_index(int class_index, int idx)
215 {
216 EX_CALLBACKS *ip = get_and_lock(class_index);
217 EX_CALLBACK *a;
218 int toret = 0;
219
220 if (ip == NULL)
221 return 0;
222 if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
223 goto err;
224 a = sk_EX_CALLBACK_value(ip->meth, idx);
225 if (a == NULL)
226 goto err;
227 a->new_func = dummy_new;
228 a->dup_func = dummy_dup;
229 a->free_func = dummy_free;
230 toret = 1;
231 err:
232 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
233 return toret;
234 }
235
236 /*
237 * Register a new index.
238 */
239 int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
240 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
241 CRYPTO_EX_free *free_func)
242 {
243 int toret = -1;
244 EX_CALLBACK *a;
245 EX_CALLBACKS *ip = get_and_lock(class_index);
246
247 if (ip == NULL)
248 return -1;
249 a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
250 if (a == NULL) {
251 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
252 goto err;
253 }
254 a->argl = argl;
255 a->argp = argp;
256 a->new_func = new_func;
257 a->dup_func = dup_func;
258 a->free_func = free_func;
259
260 if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
261 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
262 OPENSSL_free(a);
263 goto err;
264 }
265 toret = sk_EX_CALLBACK_num(ip->meth) - 1;
266 (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
267
268 err:
269 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
270 return toret;
271 }
272
273 /*
274 * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
275 * calling new() callbacks for each index in the class used by this variable
276 * Thread-safe by copying a class's array of "EX_CALLBACK" entries
277 * in the lock, then using them outside the lock. Note this only applies
278 * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
279 */
280 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
281 {
282 int mx, i;
283 void *ptr;
284 EX_CALLBACK **storage = NULL;
285 EX_CALLBACK *stack[10];
286 EX_CALLBACKS *ip = get_and_lock(class_index);
287
288 if (ip == NULL)
289 return 0;
290
291 ad->sk = NULL;
292
293 mx = sk_EX_CALLBACK_num(ip->meth);
294 if (mx > 0) {
295 if (mx < (int)OSSL_NELEM(stack))
296 storage = stack;
297 else
298 storage = OPENSSL_malloc(sizeof(*storage) * mx);
299 if (storage != NULL)
300 for (i = 0; i < mx; i++)
301 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
302 }
303 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
304
305 if (mx > 0 && storage == NULL) {
306 CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
307 return 0;
308 }
309 for (i = 0; i < mx; i++) {
310 if (storage[i] && storage[i]->new_func) {
311 ptr = CRYPTO_get_ex_data(ad, i);
312 storage[i]->new_func(obj, ptr, ad, i,
313 storage[i]->argl, storage[i]->argp);
314 }
315 }
316 if (storage != stack)
317 OPENSSL_free(storage);
318 return 1;
319 }
320
321 /*
322 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
323 * for each index in the class used by this variable
324 */
325 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
326 CRYPTO_EX_DATA *from)
327 {
328 int mx, j, i;
329 char *ptr;
330 EX_CALLBACK *stack[10];
331 EX_CALLBACK **storage = NULL;
332 EX_CALLBACKS *ip;
333
334 if (from->sk == NULL)
335 /* Nothing to copy over */
336 return 1;
337 if ((ip = get_and_lock(class_index)) == NULL)
338 return 0;
339
340 mx = sk_EX_CALLBACK_num(ip->meth);
341 j = sk_void_num(from->sk);
342 if (j < mx)
343 mx = j;
344 if (mx > 0) {
345 if (mx < (int)OSSL_NELEM(stack))
346 storage = stack;
347 else
348 storage = OPENSSL_malloc(sizeof(*storage) * mx);
349 if (storage != NULL)
350 for (i = 0; i < mx; i++)
351 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
352 }
353 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
354
355 if (mx > 0 && storage == NULL) {
356 CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
357 return 0;
358 }
359
360 for (i = 0; i < mx; i++) {
361 ptr = CRYPTO_get_ex_data(from, i);
362 if (storage[i] && storage[i]->dup_func)
363 storage[i]->dup_func(to, from, &ptr, i,
364 storage[i]->argl, storage[i]->argp);
365 CRYPTO_set_ex_data(to, i, ptr);
366 }
367 if (storage != stack)
368 OPENSSL_free(storage);
369 return 1;
370 }
371
372
373 /*
374 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
375 * each index in the class used by this variable
376 */
377 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
378 {
379 int mx, i;
380 EX_CALLBACKS *ip;
381 void *ptr;
382 EX_CALLBACK *stack[10];
383 EX_CALLBACK **storage = NULL;
384
385 if ((ip = get_and_lock(class_index)) == NULL)
386 return;
387
388 mx = sk_EX_CALLBACK_num(ip->meth);
389 if (mx > 0) {
390 if (mx < (int)OSSL_NELEM(stack))
391 storage = stack;
392 else
393 storage = OPENSSL_malloc(sizeof(*storage) * mx);
394 if (storage != NULL)
395 for (i = 0; i < mx; i++)
396 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
397 }
398 CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
399
400 if (mx > 0 && storage == NULL) {
401 CRYPTOerr(CRYPTO_F_CRYPTO_FREE_EX_DATA, ERR_R_MALLOC_FAILURE);
402 return;
403 }
404 for (i = 0; i < mx; i++) {
405 if (storage[i] && storage[i]->free_func) {
406 ptr = CRYPTO_get_ex_data(ad, i);
407 storage[i]->free_func(obj, ptr, ad, i,
408 storage[i]->argl, storage[i]->argp);
409 }
410 }
411
412 if (storage != stack)
413 OPENSSL_free(storage);
414 sk_void_free(ad->sk);
415 ad->sk = NULL;
416 }
417
418 /*
419 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
420 * particular index in the class used by this variable
421 */
422 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
423 {
424 int i;
425
426 if (ad->sk == NULL) {
427 if ((ad->sk = sk_void_new_null()) == NULL) {
428 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
429 return 0;
430 }
431 }
432
433 for (i = sk_void_num(ad->sk); i <= idx; ++i) {
434 if (!sk_void_push(ad->sk, NULL)) {
435 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
436 return 0;
437 }
438 }
439 sk_void_set(ad->sk, idx, val);
440 return 1;
441 }
442
443 /*
444 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
445 * particular index in the class used by this variable
446 */
447 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
448 {
449 if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
450 return NULL;
451 return sk_void_value(ad->sk, idx);
452 }