2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "crypto/cryptlib.h"
11 #include "internal/thread_once.h"
13 int do_ex_data_init(OPENSSL_CTX
*ctx
)
15 OSSL_EX_DATA_GLOBAL
*global
= openssl_ctx_get_ex_data_global(ctx
);
20 global
->ex_data_lock
= CRYPTO_THREAD_lock_new();
21 return global
->ex_data_lock
!= NULL
;
25 * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
26 * a given class. On success, *holds the lock.*
28 static EX_CALLBACKS
*get_and_lock(OPENSSL_CTX
*ctx
, int class_index
)
31 OSSL_EX_DATA_GLOBAL
*global
= NULL
;
33 if (class_index
< 0 || class_index
>= CRYPTO_EX_INDEX__COUNT
) {
34 CRYPTOerr(CRYPTO_F_GET_AND_LOCK
, ERR_R_PASSED_INVALID_ARGUMENT
);
38 global
= openssl_ctx_get_ex_data_global(ctx
);
39 if (global
== NULL
|| global
->ex_data_lock
== NULL
) {
41 * If we get here, someone (who?) cleaned up the lock, so just
42 * treat it as an error.
47 CRYPTO_THREAD_write_lock(global
->ex_data_lock
);
48 ip
= &global
->ex_data
[class_index
];
52 static void cleanup_cb(EX_CALLBACK
*funcs
)
58 * Release all "ex_data" state to prevent memory leaks. This can't be made
59 * thread-safe without overhauling a lot of stuff, and shouldn't really be
60 * called under potential race-conditions anyway (it's for program shutdown
63 void crypto_cleanup_all_ex_data_int(OPENSSL_CTX
*ctx
)
66 OSSL_EX_DATA_GLOBAL
*global
= openssl_ctx_get_ex_data_global(ctx
);
71 for (i
= 0; i
< CRYPTO_EX_INDEX__COUNT
; ++i
) {
72 EX_CALLBACKS
*ip
= &global
->ex_data
[i
];
74 sk_EX_CALLBACK_pop_free(ip
->meth
, cleanup_cb
);
78 CRYPTO_THREAD_lock_free(global
->ex_data_lock
);
79 global
->ex_data_lock
= NULL
;
84 * Unregister a new index by replacing the callbacks with no-ops.
85 * Any in-use instances are leaked.
87 static void dummy_new(void *parent
, void *ptr
, CRYPTO_EX_DATA
*ad
, int idx
,
88 long argl
, void *argp
)
92 static void dummy_free(void *parent
, void *ptr
, CRYPTO_EX_DATA
*ad
, int idx
,
93 long argl
, void *argp
)
97 static int dummy_dup(CRYPTO_EX_DATA
*to
, const CRYPTO_EX_DATA
*from
,
98 void *from_d
, int idx
,
99 long argl
, void *argp
)
104 int crypto_free_ex_index_ex(OPENSSL_CTX
*ctx
, int class_index
, int idx
)
109 OSSL_EX_DATA_GLOBAL
*global
= openssl_ctx_get_ex_data_global(ctx
);
114 ip
= get_and_lock(ctx
, class_index
);
117 if (idx
< 0 || idx
>= sk_EX_CALLBACK_num(ip
->meth
))
119 a
= sk_EX_CALLBACK_value(ip
->meth
, idx
);
122 a
->new_func
= dummy_new
;
123 a
->dup_func
= dummy_dup
;
124 a
->free_func
= dummy_free
;
127 CRYPTO_THREAD_unlock(global
->ex_data_lock
);
131 int CRYPTO_free_ex_index(int class_index
, int idx
)
133 return crypto_free_ex_index_ex(NULL
, class_index
, idx
);
137 * Register a new index.
139 int crypto_get_ex_new_index_ex(OPENSSL_CTX
*ctx
, int class_index
, long argl
,
140 void *argp
, CRYPTO_EX_new
*new_func
,
141 CRYPTO_EX_dup
*dup_func
,
142 CRYPTO_EX_free
*free_func
)
147 OSSL_EX_DATA_GLOBAL
*global
= openssl_ctx_get_ex_data_global(ctx
);
152 ip
= get_and_lock(ctx
, class_index
);
156 if (ip
->meth
== NULL
) {
157 ip
->meth
= sk_EX_CALLBACK_new_null();
158 /* We push an initial value on the stack because the SSL
159 * "app_data" routines use ex_data index zero. See RT 3710. */
161 || !sk_EX_CALLBACK_push(ip
->meth
, NULL
)) {
162 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX
, ERR_R_MALLOC_FAILURE
);
167 a
= (EX_CALLBACK
*)OPENSSL_malloc(sizeof(*a
));
169 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX
, ERR_R_MALLOC_FAILURE
);
174 a
->new_func
= new_func
;
175 a
->dup_func
= dup_func
;
176 a
->free_func
= free_func
;
178 if (!sk_EX_CALLBACK_push(ip
->meth
, NULL
)) {
179 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX
, ERR_R_MALLOC_FAILURE
);
183 toret
= sk_EX_CALLBACK_num(ip
->meth
) - 1;
184 (void)sk_EX_CALLBACK_set(ip
->meth
, toret
, a
);
187 CRYPTO_THREAD_unlock(global
->ex_data_lock
);
191 int CRYPTO_get_ex_new_index(int class_index
, long argl
, void *argp
,
192 CRYPTO_EX_new
*new_func
, CRYPTO_EX_dup
*dup_func
,
193 CRYPTO_EX_free
*free_func
)
195 return crypto_get_ex_new_index_ex(NULL
, class_index
, argl
, argp
, new_func
,
196 dup_func
, free_func
);
200 * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
201 * calling new() callbacks for each index in the class used by this variable
202 * Thread-safe by copying a class's array of "EX_CALLBACK" entries
203 * in the lock, then using them outside the lock. Note this only applies
204 * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
206 int crypto_new_ex_data_ex(OPENSSL_CTX
*ctx
, int class_index
, void *obj
,
211 EX_CALLBACK
**storage
= NULL
;
212 EX_CALLBACK
*stack
[10];
214 OSSL_EX_DATA_GLOBAL
*global
= openssl_ctx_get_ex_data_global(ctx
);
219 ip
= get_and_lock(ctx
, class_index
);
226 mx
= sk_EX_CALLBACK_num(ip
->meth
);
228 if (mx
< (int)OSSL_NELEM(stack
))
231 storage
= OPENSSL_malloc(sizeof(*storage
) * mx
);
233 for (i
= 0; i
< mx
; i
++)
234 storage
[i
] = sk_EX_CALLBACK_value(ip
->meth
, i
);
236 CRYPTO_THREAD_unlock(global
->ex_data_lock
);
238 if (mx
> 0 && storage
== NULL
) {
239 CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA_EX
, ERR_R_MALLOC_FAILURE
);
242 for (i
= 0; i
< mx
; i
++) {
243 if (storage
[i
] != NULL
&& storage
[i
]->new_func
!= NULL
) {
244 ptr
= CRYPTO_get_ex_data(ad
, i
);
245 storage
[i
]->new_func(obj
, ptr
, ad
, i
,
246 storage
[i
]->argl
, storage
[i
]->argp
);
249 if (storage
!= stack
)
250 OPENSSL_free(storage
);
254 int CRYPTO_new_ex_data(int class_index
, void *obj
, CRYPTO_EX_DATA
*ad
)
256 return crypto_new_ex_data_ex(NULL
, class_index
, obj
, ad
);
260 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
261 * for each index in the class used by this variable
263 int CRYPTO_dup_ex_data(int class_index
, CRYPTO_EX_DATA
*to
,
264 const CRYPTO_EX_DATA
*from
)
268 EX_CALLBACK
*stack
[10];
269 EX_CALLBACK
**storage
= NULL
;
272 OSSL_EX_DATA_GLOBAL
*global
= openssl_ctx_get_ex_data_global(from
->ctx
);
278 if (from
->sk
== NULL
)
279 /* Nothing to copy over */
281 if ((ip
= get_and_lock(from
->ctx
, class_index
)) == NULL
)
284 mx
= sk_EX_CALLBACK_num(ip
->meth
);
285 j
= sk_void_num(from
->sk
);
289 if (mx
< (int)OSSL_NELEM(stack
))
292 storage
= OPENSSL_malloc(sizeof(*storage
) * mx
);
294 for (i
= 0; i
< mx
; i
++)
295 storage
[i
] = sk_EX_CALLBACK_value(ip
->meth
, i
);
297 CRYPTO_THREAD_unlock(global
->ex_data_lock
);
301 if (storage
== NULL
) {
302 CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA
, ERR_R_MALLOC_FAILURE
);
306 * Make sure the ex_data stack is at least |mx| elements long to avoid
307 * issues in the for loop that follows; so go get the |mx|'th element
308 * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
309 * to itself. This is normally a no-op; but ensures the stack is the
312 if (!CRYPTO_set_ex_data(to
, mx
- 1, CRYPTO_get_ex_data(to
, mx
- 1)))
315 for (i
= 0; i
< mx
; i
++) {
316 ptr
= CRYPTO_get_ex_data(from
, i
);
317 if (storage
[i
] != NULL
&& storage
[i
]->dup_func
!= NULL
)
318 if (!storage
[i
]->dup_func(to
, from
, &ptr
, i
,
319 storage
[i
]->argl
, storage
[i
]->argp
))
321 CRYPTO_set_ex_data(to
, i
, ptr
);
325 if (storage
!= stack
)
326 OPENSSL_free(storage
);
332 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
333 * each index in the class used by this variable
335 void CRYPTO_free_ex_data(int class_index
, void *obj
, CRYPTO_EX_DATA
*ad
)
341 EX_CALLBACK
*stack
[10];
342 EX_CALLBACK
**storage
= NULL
;
343 OSSL_EX_DATA_GLOBAL
*global
;
345 if ((ip
= get_and_lock(ad
->ctx
, class_index
)) == NULL
)
347 global
= openssl_ctx_get_ex_data_global(ad
->ctx
);
351 mx
= sk_EX_CALLBACK_num(ip
->meth
);
353 if (mx
< (int)OSSL_NELEM(stack
))
356 storage
= OPENSSL_malloc(sizeof(*storage
) * mx
);
358 for (i
= 0; i
< mx
; i
++)
359 storage
[i
] = sk_EX_CALLBACK_value(ip
->meth
, i
);
361 CRYPTO_THREAD_unlock(global
->ex_data_lock
);
363 for (i
= 0; i
< mx
; i
++) {
367 CRYPTO_THREAD_write_lock(global
->ex_data_lock
);
368 f
= sk_EX_CALLBACK_value(ip
->meth
, i
);
369 CRYPTO_THREAD_unlock(global
->ex_data_lock
);
371 if (f
!= NULL
&& f
->free_func
!= NULL
) {
372 ptr
= CRYPTO_get_ex_data(ad
, i
);
373 f
->free_func(obj
, ptr
, ad
, i
, f
->argl
, f
->argp
);
377 if (storage
!= stack
)
378 OPENSSL_free(storage
);
380 sk_void_free(ad
->sk
);
386 * Allocate a given CRYPTO_EX_DATA item using the class specific allocation
389 int CRYPTO_alloc_ex_data(int class_index
, void *obj
, CRYPTO_EX_DATA
*ad
,
395 OSSL_EX_DATA_GLOBAL
*global
= openssl_ctx_get_ex_data_global(ad
->ctx
);
400 curval
= CRYPTO_get_ex_data(ad
, idx
);
402 /* Already there, no need to allocate */
406 ip
= get_and_lock(ad
->ctx
, class_index
);
409 f
= sk_EX_CALLBACK_value(ip
->meth
, idx
);
410 CRYPTO_THREAD_unlock(global
->ex_data_lock
);
413 * This should end up calling CRYPTO_set_ex_data(), which allocates
414 * everything necessary to support placing the new data in the right spot.
416 if (f
->new_func
== NULL
)
419 f
->new_func(obj
, NULL
, ad
, idx
, f
->argl
, f
->argp
);
425 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
426 * particular index in the class used by this variable
428 int CRYPTO_set_ex_data(CRYPTO_EX_DATA
*ad
, int idx
, void *val
)
432 if (ad
->sk
== NULL
) {
433 if ((ad
->sk
= sk_void_new_null()) == NULL
) {
434 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA
, ERR_R_MALLOC_FAILURE
);
439 for (i
= sk_void_num(ad
->sk
); i
<= idx
; ++i
) {
440 if (!sk_void_push(ad
->sk
, NULL
)) {
441 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA
, ERR_R_MALLOC_FAILURE
);
445 sk_void_set(ad
->sk
, idx
, val
);
450 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
451 * particular index in the class used by this variable
453 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA
*ad
, int idx
)
455 if (ad
->sk
== NULL
|| idx
>= sk_void_num(ad
->sk
))
457 return sk_void_value(ad
->sk
, idx
);
460 OPENSSL_CTX
*crypto_ex_data_get_openssl_ctx(const CRYPTO_EX_DATA
*ad
)