GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/ex_data.c Lines: 127 191 66.5 %
Date: 2017-11-13 Branches: 60 112 53.6 %

Line Branch Exec Source
1
/* $OpenBSD: ex_data.c,v 1.19 2017/01/29 17:49:22 beck Exp $ */
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
 * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
89
 *
90
 * Redistribution and use in source and binary forms, with or without
91
 * modification, are permitted provided that the following conditions
92
 * are met:
93
 *
94
 * 1. Redistributions of source code must retain the above copyright
95
 *    notice, this list of conditions and the following disclaimer.
96
 *
97
 * 2. Redistributions in binary form must reproduce the above copyright
98
 *    notice, this list of conditions and the following disclaimer in
99
 *    the documentation and/or other materials provided with the
100
 *    distribution.
101
 *
102
 * 3. All advertising materials mentioning features or use of this
103
 *    software must display the following acknowledgment:
104
 *    "This product includes software developed by the OpenSSL Project
105
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
106
 *
107
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
108
 *    endorse or promote products derived from this software without
109
 *    prior written permission. For written permission, please contact
110
 *    openssl-core@openssl.org.
111
 *
112
 * 5. Products derived from this software may not be called "OpenSSL"
113
 *    nor may "OpenSSL" appear in their names without prior written
114
 *    permission of the OpenSSL Project.
115
 *
116
 * 6. Redistributions of any form whatsoever must retain the following
117
 *    acknowledgment:
118
 *    "This product includes software developed by the OpenSSL Project
119
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
120
 *
121
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
122
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
123
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
124
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
125
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
126
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
127
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
128
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
129
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
130
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
131
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
132
 * OF THE POSSIBILITY OF SUCH DAMAGE.
133
 * ====================================================================
134
 *
135
 * This product includes cryptographic software written by Eric Young
136
 * (eay@cryptsoft.com).  This product includes software written by Tim
137
 * Hudson (tjh@cryptsoft.com).
138
 *
139
 */
140
141
#include <openssl/err.h>
142
#include <openssl/lhash.h>
143
144
/* What an "implementation of ex_data functionality" looks like */
145
struct st_CRYPTO_EX_DATA_IMPL {
146
	/*********************/
147
	/* GLOBAL OPERATIONS */
148
	/* Return a new class index */
149
	int (*cb_new_class)(void);
150
	/* Cleanup all state used by the implementation */
151
	void (*cb_cleanup)(void);
152
	/************************/
153
	/* PER-CLASS OPERATIONS */
154
	/* Get a new method index within a class */
155
	int (*cb_get_new_index)(int class_index, long argl, void *argp,
156
	    CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
157
	    CRYPTO_EX_free *free_func);
158
	/* Initialise a new CRYPTO_EX_DATA of a given class */
159
	int (*cb_new_ex_data)(int class_index, void *obj,
160
	    CRYPTO_EX_DATA *ad);
161
	/* Duplicate a CRYPTO_EX_DATA of a given class onto a copy */
162
	int (*cb_dup_ex_data)(int class_index, CRYPTO_EX_DATA *to,
163
	    CRYPTO_EX_DATA *from);
164
	/* Cleanup a CRYPTO_EX_DATA of a given class */
165
	void (*cb_free_ex_data)(int class_index, void *obj,
166
	    CRYPTO_EX_DATA *ad);
167
};
168
169
/* The implementation we use at run-time */
170
static const CRYPTO_EX_DATA_IMPL *impl = NULL;
171
172
/* To call "impl" functions, use this macro rather than referring to 'impl' directly, eg.
173
 * EX_IMPL(get_new_index)(...);
174
*/
175
#define EX_IMPL(a) impl->cb_##a
176
177
/* Predeclare the "default" ex_data implementation */
178
static int int_new_class(void);
179
static void int_cleanup(void);
180
static int int_get_new_index(int class_index, long argl, void *argp,
181
    CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
182
    CRYPTO_EX_free *free_func);
183
static int int_new_ex_data(int class_index, void *obj,
184
    CRYPTO_EX_DATA *ad);
185
static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
186
    CRYPTO_EX_DATA *from);
187
static void int_free_ex_data(int class_index, void *obj,
188
    CRYPTO_EX_DATA *ad);
189
190
static CRYPTO_EX_DATA_IMPL impl_default = {
191
	int_new_class,
192
	int_cleanup,
193
	int_get_new_index,
194
	int_new_ex_data,
195
	int_dup_ex_data,
196
	int_free_ex_data
197
};
198
199
/* Internal function that checks whether "impl" is set and if not, sets it to
200
 * the default. */
201
static void
202
impl_check(void)
203
{
204
8926
	CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
205
4463
	if (!impl)
206
4463
		impl = &impl_default;
207
4463
	CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
208
4463
}
209
/* A macro wrapper for impl_check that first uses a non-locked test before
210
 * invoking the function (which checks again inside a lock). */
211
#define IMPL_CHECK if(!impl) impl_check();
212
213
/* API functions to get/set the "ex_data" implementation */
214
const CRYPTO_EX_DATA_IMPL *
215
CRYPTO_get_ex_data_implementation(void)
216
{
217
	IMPL_CHECK
218
	return impl;
219
}
220
221
int
222
CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i)
223
{
224
	int toret = 0;
225
	CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
226
	if (!impl) {
227
		impl = i;
228
		toret = 1;
229
	}
230
	CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
231
	return toret;
232
}
233
234
/****************************************************************************/
235
/* Interal (default) implementation of "ex_data" support. API functions are
236
 * further down. */
237
238
/* The type that represents what each "class" used to implement locally. A STACK
239
 * of CRYPTO_EX_DATA_FUNCS plus a index-counter. The 'class_index' is the global
240
 * value representing the class that is used to distinguish these items. */
241
typedef struct st_ex_class_item {
242
	int class_index;
243
	STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
244
	int meth_num;
245
} EX_CLASS_ITEM;
246
247
/* When assigning new class indexes, this is our counter */
248
static int ex_class = CRYPTO_EX_INDEX_USER;
249
250
/* The global hash table of EX_CLASS_ITEM items */
251
DECLARE_LHASH_OF(EX_CLASS_ITEM);
252
static LHASH_OF(EX_CLASS_ITEM) *ex_data = NULL;
253
254
/* The callbacks required in the "ex_data" hash table */
255
static unsigned long
256
ex_class_item_hash(const EX_CLASS_ITEM *a)
257
{
258
361592
	return a->class_index;
259
}
260
261
361592
static IMPLEMENT_LHASH_HASH_FN(ex_class_item, EX_CLASS_ITEM)
262
263
static int
264
ex_class_item_cmp(const EX_CLASS_ITEM *a, const EX_CLASS_ITEM *b)
265
{
266
329536
	return a->class_index - b->class_index;
267
}
268
269
329536
static IMPLEMENT_LHASH_COMP_FN(ex_class_item, EX_CLASS_ITEM)
270
271
/* Internal functions used by the "impl_default" implementation to access the
272
 * state */
273
274
static int
275
ex_data_check(void)
276
{
277
	int toret = 1;
278
8926
	CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
279

8926
	if (!ex_data &&
280
4463
	    (ex_data = lh_EX_CLASS_ITEM_new()) == NULL)
281
		toret = 0;
282
4463
	CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
283
4463
	return toret;
284
}
285
/* This macros helps reduce the locking from repeated checks because the
286
 * ex_data_check() function checks ex_data again inside a lock. */
287
#define EX_DATA_CHECK(iffail) if(!ex_data && !ex_data_check()) {iffail}
288
289
/* This "inner" callback is used by the callback function that follows it */
290
static void
291
def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS *funcs)
292
{
293
330
	free(funcs);
294
165
}
295
296
/* This callback is used in lh_doall to destroy all EX_CLASS_ITEM values from
297
 * "ex_data" prior to the ex_data hash table being itself destroyed. Doesn't do
298
 * any locking. */
299
static void
300
def_cleanup_cb(void *a_void)
301
{
302
6354
	EX_CLASS_ITEM *item = (EX_CLASS_ITEM *)a_void;
303
3177
	sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, def_cleanup_util_cb);
304
3177
	free(item);
305
3177
}
306
307
/* Return the EX_CLASS_ITEM from the "ex_data" hash table that corresponds to a
308
 * given class. Handles locking. */
309
static EX_CLASS_ITEM *
310
def_get_class(int class_index)
311
{
312
345564
	EX_CLASS_ITEM d, *p, *gen;
313

177239
	EX_DATA_CHECK(return NULL;)
314
172782
	d.class_index = class_index;
315
172782
	CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
316
172782
	p = lh_EX_CLASS_ITEM_retrieve(ex_data, &d);
317
172782
	if (!p) {
318
8014
		gen = malloc(sizeof(EX_CLASS_ITEM));
319
8014
		if (gen) {
320
8014
			gen->class_index = class_index;
321
8014
			gen->meth_num = 0;
322
8014
			gen->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null();
323
8014
			if (!gen->meth)
324
				free(gen);
325
			else {
326
				/* Because we're inside the ex_data lock, the
327
				 * return value from the insert will be NULL */
328
8014
				(void)lh_EX_CLASS_ITEM_insert(ex_data, gen);
329
				p = gen;
330
			}
331
		}
332
	}
333
172782
	CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
334
172782
	if (!p)
335
		CRYPTOerror(ERR_R_MALLOC_FAILURE);
336
172782
	return p;
337
172782
}
338
339
/* Add a new method to the given EX_CLASS_ITEM and return the corresponding
340
 * index (or -1 for error). Handles locking. */
341
static int
342
def_add_index(EX_CLASS_ITEM *item, long argl, void *argp,
343
    CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
344
{
345
	int toret = -1;
346
626
	CRYPTO_EX_DATA_FUNCS *a = malloc(sizeof(CRYPTO_EX_DATA_FUNCS));
347
348
313
	if (!a) {
349
		CRYPTOerror(ERR_R_MALLOC_FAILURE);
350
		return -1;
351
	}
352
313
	a->argl = argl;
353
313
	a->argp = argp;
354
313
	a->new_func = new_func;
355
313
	a->dup_func = dup_func;
356
313
	a->free_func = free_func;
357
313
	CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
358
1252
	while (sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) <= item->meth_num) {
359
313
		if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, NULL)) {
360
			CRYPTOerror(ERR_R_MALLOC_FAILURE);
361
			free(a);
362
			goto err;
363
		}
364
	}
365
313
	toret = item->meth_num++;
366
313
	(void)sk_CRYPTO_EX_DATA_FUNCS_set(item->meth, toret, a);
367
err:
368
313
	CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
369
313
	return toret;
370
313
}
371
372
/**************************************************************/
373
/* The functions in the default CRYPTO_EX_DATA_IMPL structure */
374
375
static int
376
int_new_class(void)
377
{
378
	int toret;
379
380
	CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
381
	toret = ex_class++;
382
	CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
383
	return toret;
384
}
385
386
static void
387
int_cleanup(void)
388
{
389

3146
	EX_DATA_CHECK(return;)
390
1570
	lh_EX_CLASS_ITEM_doall(ex_data, def_cleanup_cb);
391
1570
	lh_EX_CLASS_ITEM_free(ex_data);
392
1570
	ex_data = NULL;
393
1570
	impl = NULL;
394
3140
}
395
396
static int
397
int_get_new_index(int class_index, long argl, void *argp,
398
    CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
399
    CRYPTO_EX_free *free_func)
400
{
401
626
	EX_CLASS_ITEM *item = def_get_class(class_index);
402
403
313
	if (!item)
404
		return -1;
405
313
	return def_add_index(item, argl, argp, new_func, dup_func, free_func);
406
313
}
407
408
/* Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries in
409
 * the lock, then using them outside the lock. NB: Thread-safety only applies to
410
 * the global "ex_data" state (ie. class definitions), not thread-safe on 'ad'
411
 * itself. */
412
static int
413
int_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
414
{
415
	int mx, i;
416
	void *ptr;
417
	CRYPTO_EX_DATA_FUNCS **storage = NULL;
418
173768
	EX_CLASS_ITEM *item = def_get_class(class_index);
419
420
86884
	if (!item)
421
		/* error is already set */
422
		return 0;
423
86884
	ad->sk = NULL;
424
86884
	CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
425
86884
	mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
426
86884
	if (mx > 0) {
427
1480
		storage = reallocarray(NULL, mx, sizeof(CRYPTO_EX_DATA_FUNCS*));
428
1480
		if (!storage)
429
			goto skip;
430
5920
		for (i = 0; i < mx; i++)
431
1480
			storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(
432
			    item->meth, i);
433
	}
434
skip:
435
86884
	CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
436
86884
	if ((mx > 0) && !storage) {
437
		CRYPTOerror(ERR_R_MALLOC_FAILURE);
438
		return 0;
439
	}
440
176728
	for (i = 0; i < mx; i++) {
441

2960
		if (storage[i] && storage[i]->new_func) {
442
			ptr = CRYPTO_get_ex_data(ad, i);
443
			storage[i]->new_func(obj, ptr, ad, i,
444
			    storage[i]->argl, storage[i]->argp);
445
		}
446
	}
447
86884
	free(storage);
448
86884
	return 1;
449
86884
}
450
451
/* Same thread-safety notes as for "int_new_ex_data" */
452
static int
453
int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from)
454
{
455
	int mx, j, i;
456
4
	char *ptr;
457
	CRYPTO_EX_DATA_FUNCS **storage = NULL;
458
	EX_CLASS_ITEM *item;
459
460
2
	if (!from->sk)
461
		/* 'to' should be "blank" which *is* just like 'from' */
462
2
		return 1;
463
	if ((item = def_get_class(class_index)) == NULL)
464
		return 0;
465
	CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
466
	mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
467
	j = sk_void_num(from->sk);
468
	if (j < mx)
469
		mx = j;
470
	if (mx > 0) {
471
		storage = reallocarray(NULL, mx, sizeof(CRYPTO_EX_DATA_FUNCS*));
472
		if (!storage)
473
			goto skip;
474
		for (i = 0; i < mx; i++)
475
			storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(
476
			    item->meth, i);
477
	}
478
skip:
479
	CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
480
	if ((mx > 0) && !storage) {
481
		CRYPTOerror(ERR_R_MALLOC_FAILURE);
482
		return 0;
483
	}
484
	for (i = 0; i < mx; i++) {
485
		ptr = CRYPTO_get_ex_data(from, i);
486
		if (storage[i] && storage[i]->dup_func)
487
			storage[i]->dup_func(to, from, &ptr, i,
488
			    storage[i]->argl, storage[i]->argp);
489
		CRYPTO_set_ex_data(to, i, ptr);
490
	}
491
	free(storage);
492
	return 1;
493
2
}
494
495
/* Same thread-safety notes as for "int_new_ex_data" */
496
static void
497
int_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
498
{
499
	int mx, i;
500
	EX_CLASS_ITEM *item;
501
	void *ptr;
502
	CRYPTO_EX_DATA_FUNCS **storage = NULL;
503
171170
	if ((item = def_get_class(class_index)) == NULL)
504
		return;
505
85585
	CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
506
85585
	mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
507
85585
	if (mx > 0) {
508
1480
		storage = reallocarray(NULL, mx, sizeof(CRYPTO_EX_DATA_FUNCS*));
509
1480
		if (!storage)
510
			goto skip;
511
5920
		for (i = 0; i < mx; i++)
512
1480
			storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(
513
			    item->meth, i);
514
	}
515
skip:
516
85585
	CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
517
85585
	if ((mx > 0) && !storage) {
518
		CRYPTOerror(ERR_R_MALLOC_FAILURE);
519
		return;
520
	}
521
174130
	for (i = 0; i < mx; i++) {
522

2960
		if (storage[i] && storage[i]->free_func) {
523
			ptr = CRYPTO_get_ex_data(ad, i);
524
			storage[i]->free_func(obj, ptr, ad, i,
525
			    storage[i]->argl, storage[i]->argp);
526
		}
527
	}
528
85585
	free(storage);
529
85585
	if (ad->sk) {
530
1327
		sk_void_free(ad->sk);
531
1327
		ad->sk = NULL;
532
1327
	}
533
171170
}
534
535
/********************************************************************/
536
/* API functions that defer all "state" operations to the "ex_data"
537
 * implementation we have set. */
538
539
/* Obtain an index for a new class (not the same as getting a new index within
540
 * an existing class - this is actually getting a new *class*) */
541
int
542
CRYPTO_ex_data_new_class(void)
543
{
544
	IMPL_CHECK
545
	return EX_IMPL(new_class)();
546
}
547
548
/* Release all "ex_data" state to prevent memory leaks. This can't be made
549
 * thread-safe without overhauling a lot of stuff, and shouldn't really be
550
 * called under potential race-conditions anyway (it's for program shutdown
551
 * after all). */
552
void
553
CRYPTO_cleanup_all_ex_data(void)
554
{
555
3146
	IMPL_CHECK
556
1570
	EX_IMPL(cleanup)();
557
1570
}
558
559
/* Inside an existing class, get/register a new index. */
560
int
561
CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
562
    CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
563
{
564
	int ret = -1;
565
566
666
	IMPL_CHECK
567
313
	ret = EX_IMPL(get_new_index)(class_index,
568
	    argl, argp, new_func, dup_func, free_func);
569
313
	return ret;
570
}
571
572
/* Initialise a new CRYPTO_EX_DATA for use in a particular class - including
573
 * calling new() callbacks for each index in the class used by this variable */
574
int
575
CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
576
{
577
176630
	IMPL_CHECK
578
86884
	return EX_IMPL(new_ex_data)(class_index, obj, ad);
579
}
580
581
/* Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks for
582
 * each index in the class used by this variable */
583
int
584
CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from)
585
{
586
4
	IMPL_CHECK
587
2
	return EX_IMPL(dup_ex_data)(class_index, to, from);
588
}
589
590
/* Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
591
 * each index in the class used by this variable */
592
void
593
CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
594
{
595
172725
	IMPL_CHECK
596
85585
	EX_IMPL(free_ex_data)(class_index, obj, ad);
597
85585
}
598
599
/* For a given CRYPTO_EX_DATA variable, set the value corresponding to a
600
 * particular index in the class used by this variable */
601
int
602
CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
603
{
604
	int i;
605
606
2654
	if (ad->sk == NULL) {
607
1327
		if ((ad->sk = sk_void_new_null()) == NULL) {
608
			CRYPTOerror(ERR_R_MALLOC_FAILURE);
609
			return (0);
610
		}
611
	}
612
1327
	i = sk_void_num(ad->sk);
613
614
5308
	while (i <= idx) {
615
1327
		if (!sk_void_push(ad->sk, NULL)) {
616
			CRYPTOerror(ERR_R_MALLOC_FAILURE);
617
			return (0);
618
		}
619
1327
		i++;
620
	}
621
1327
	sk_void_set(ad->sk, idx, val);
622
1327
	return (1);
623
1327
}
624
625
/* For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
626
 * particular index in the class used by this variable */
627
void *
628
CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
629
{
630
72
	if (ad->sk == NULL)
631
		return (0);
632
36
	else if (idx >= sk_void_num(ad->sk))
633
		return (0);
634
	else
635
36
		return (sk_void_value(ad->sk, idx));
636
36
}