/src/openssl/crypto/ex_data.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #include <stdlib.h> |
11 | | #include "crypto/cryptlib.h" |
12 | | #include "internal/thread_once.h" |
13 | | |
14 | | int ossl_do_ex_data_init(OSSL_LIB_CTX *ctx) |
15 | 4 | { |
16 | 4 | OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx); |
17 | | |
18 | 4 | if (global == NULL) |
19 | 0 | return 0; |
20 | | |
21 | 4 | global->ex_data_lock = CRYPTO_THREAD_lock_new(); |
22 | 4 | return global->ex_data_lock != NULL; |
23 | 4 | } |
24 | | |
25 | | /* |
26 | | * Return the EX_CALLBACKS from the |ex_data| array that corresponds to |
27 | | * a given class. On success, *holds the lock.* |
28 | | * The |global| parameter is assumed to be non null (checked by the caller). |
29 | | * If |read| is 1 then a read lock is obtained. Otherwise it is a write lock. |
30 | | */ |
31 | | static EX_CALLBACKS *get_and_lock(OSSL_EX_DATA_GLOBAL *global, int class_index, |
32 | | int read) |
33 | 161k | { |
34 | 161k | EX_CALLBACKS *ip; |
35 | | |
36 | 161k | if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) { |
37 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
38 | 0 | return NULL; |
39 | 0 | } |
40 | | |
41 | 161k | if (global->ex_data_lock == NULL) { |
42 | | /* |
43 | | * If we get here, someone (who?) cleaned up the lock, so just |
44 | | * treat it as an error. |
45 | | */ |
46 | 0 | return NULL; |
47 | 0 | } |
48 | | |
49 | 161k | if (read) { |
50 | 161k | if (!CRYPTO_THREAD_read_lock(global->ex_data_lock)) |
51 | 0 | return NULL; |
52 | 161k | } else { |
53 | 2 | if (!CRYPTO_THREAD_write_lock(global->ex_data_lock)) |
54 | 0 | return NULL; |
55 | 2 | } |
56 | | |
57 | 161k | ip = &global->ex_data[class_index]; |
58 | 161k | return ip; |
59 | 161k | } |
60 | | |
61 | | static void cleanup_cb(EX_CALLBACK *funcs) |
62 | 0 | { |
63 | 0 | OPENSSL_free(funcs); |
64 | 0 | } |
65 | | |
66 | | /* |
67 | | * Release all "ex_data" state to prevent memory leaks. This can't be made |
68 | | * thread-safe without overhauling a lot of stuff, and shouldn't really be |
69 | | * called under potential race-conditions anyway (it's for program shutdown |
70 | | * after all). |
71 | | */ |
72 | | void ossl_crypto_cleanup_all_ex_data_int(OSSL_LIB_CTX *ctx) |
73 | 2 | { |
74 | 2 | int i; |
75 | 2 | OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx); |
76 | | |
77 | 2 | if (global == NULL) |
78 | 0 | return; |
79 | | |
80 | 38 | for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) { |
81 | 36 | EX_CALLBACKS *ip = &global->ex_data[i]; |
82 | | |
83 | 36 | sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb); |
84 | 36 | ip->meth = NULL; |
85 | 36 | } |
86 | | |
87 | 2 | CRYPTO_THREAD_lock_free(global->ex_data_lock); |
88 | 2 | global->ex_data_lock = NULL; |
89 | 2 | } |
90 | | |
91 | | |
92 | | /* |
93 | | * Unregister a new index by replacing the callbacks with no-ops. |
94 | | * Any in-use instances are leaked. |
95 | | */ |
96 | | static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, |
97 | | long argl, void *argp) |
98 | 0 | { |
99 | 0 | } |
100 | | |
101 | | static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, |
102 | | long argl, void *argp) |
103 | 0 | { |
104 | 0 | } |
105 | | |
106 | | static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, |
107 | | void **from_d, int idx, |
108 | | long argl, void *argp) |
109 | 0 | { |
110 | 0 | return 1; |
111 | 0 | } |
112 | | |
113 | | int ossl_crypto_free_ex_index_ex(OSSL_LIB_CTX *ctx, int class_index, int idx) |
114 | 2 | { |
115 | 2 | EX_CALLBACKS *ip; |
116 | 2 | EX_CALLBACK *a; |
117 | 2 | int toret = 0; |
118 | 2 | OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx); |
119 | | |
120 | 2 | if (global == NULL) |
121 | 0 | return 0; |
122 | | |
123 | 2 | ip = get_and_lock(global, class_index, 0); |
124 | 2 | if (ip == NULL) |
125 | 0 | return 0; |
126 | | |
127 | 2 | if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth)) |
128 | 2 | goto err; |
129 | 0 | a = sk_EX_CALLBACK_value(ip->meth, idx); |
130 | 0 | if (a == NULL) |
131 | 0 | goto err; |
132 | 0 | a->new_func = dummy_new; |
133 | 0 | a->dup_func = dummy_dup; |
134 | 0 | a->free_func = dummy_free; |
135 | 0 | toret = 1; |
136 | 2 | err: |
137 | 2 | CRYPTO_THREAD_unlock(global->ex_data_lock); |
138 | 2 | return toret; |
139 | 0 | } |
140 | | |
141 | | int CRYPTO_free_ex_index(int class_index, int idx) |
142 | 2 | { |
143 | 2 | return ossl_crypto_free_ex_index_ex(NULL, class_index, idx); |
144 | 2 | } |
145 | | |
146 | | /* |
147 | | * Register a new index. |
148 | | */ |
149 | | int ossl_crypto_get_ex_new_index_ex(OSSL_LIB_CTX *ctx, int class_index, |
150 | | long argl, void *argp, |
151 | | CRYPTO_EX_new *new_func, |
152 | | CRYPTO_EX_dup *dup_func, |
153 | | CRYPTO_EX_free *free_func, |
154 | | int priority) |
155 | 0 | { |
156 | 0 | int toret = -1; |
157 | 0 | EX_CALLBACK *a; |
158 | 0 | EX_CALLBACKS *ip; |
159 | 0 | OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx); |
160 | |
|
161 | 0 | if (global == NULL) |
162 | 0 | return -1; |
163 | | |
164 | 0 | ip = get_and_lock(global, class_index, 0); |
165 | 0 | if (ip == NULL) |
166 | 0 | return -1; |
167 | | |
168 | 0 | if (ip->meth == NULL) { |
169 | 0 | ip->meth = sk_EX_CALLBACK_new_null(); |
170 | | /* We push an initial value on the stack because the SSL |
171 | | * "app_data" routines use ex_data index zero. See RT 3710. */ |
172 | 0 | if (ip->meth == NULL |
173 | 0 | || !sk_EX_CALLBACK_push(ip->meth, NULL)) { |
174 | 0 | sk_EX_CALLBACK_free(ip->meth); |
175 | 0 | ip->meth = NULL; |
176 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB); |
177 | 0 | goto err; |
178 | 0 | } |
179 | 0 | } |
180 | | |
181 | 0 | a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a)); |
182 | 0 | if (a == NULL) |
183 | 0 | goto err; |
184 | 0 | a->argl = argl; |
185 | 0 | a->argp = argp; |
186 | 0 | a->new_func = new_func; |
187 | 0 | a->dup_func = dup_func; |
188 | 0 | a->free_func = free_func; |
189 | 0 | a->priority = priority; |
190 | |
|
191 | 0 | if (!sk_EX_CALLBACK_push(ip->meth, NULL)) { |
192 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB); |
193 | 0 | OPENSSL_free(a); |
194 | 0 | goto err; |
195 | 0 | } |
196 | 0 | toret = sk_EX_CALLBACK_num(ip->meth) - 1; |
197 | 0 | (void)sk_EX_CALLBACK_set(ip->meth, toret, a); |
198 | |
|
199 | 0 | err: |
200 | 0 | CRYPTO_THREAD_unlock(global->ex_data_lock); |
201 | 0 | return toret; |
202 | 0 | } |
203 | | |
204 | | int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp, |
205 | | CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, |
206 | | CRYPTO_EX_free *free_func) |
207 | 0 | { |
208 | 0 | return ossl_crypto_get_ex_new_index_ex(NULL, class_index, argl, argp, |
209 | 0 | new_func, dup_func, free_func, 0); |
210 | 0 | } |
211 | | |
212 | | /* |
213 | | * Initialise a new CRYPTO_EX_DATA for use in a particular class - including |
214 | | * calling new() callbacks for each index in the class used by this variable |
215 | | * Thread-safe by copying a class's array of "EX_CALLBACK" entries |
216 | | * in the lock, then using them outside the lock. Note this only applies |
217 | | * to the global "ex_data" state (ie. class definitions), not 'ad' itself. |
218 | | */ |
219 | | int ossl_crypto_new_ex_data_ex(OSSL_LIB_CTX *ctx, int class_index, void *obj, |
220 | | CRYPTO_EX_DATA *ad) |
221 | 80.2k | { |
222 | 80.2k | int mx, i; |
223 | 80.2k | void *ptr; |
224 | 80.2k | EX_CALLBACK **storage = NULL; |
225 | 80.2k | EX_CALLBACK *stack[10]; |
226 | 80.2k | EX_CALLBACKS *ip; |
227 | 80.2k | OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx); |
228 | | |
229 | 80.2k | if (global == NULL) |
230 | 0 | return 0; |
231 | | |
232 | 80.2k | ip = get_and_lock(global, class_index, 1); |
233 | 80.2k | if (ip == NULL) |
234 | 0 | return 0; |
235 | | |
236 | 80.2k | ad->ctx = ctx; |
237 | 80.2k | ad->sk = NULL; |
238 | 80.2k | mx = sk_EX_CALLBACK_num(ip->meth); |
239 | 80.2k | if (mx > 0) { |
240 | 0 | if (mx < (int)OSSL_NELEM(stack)) |
241 | 0 | storage = stack; |
242 | 0 | else |
243 | 0 | storage = OPENSSL_malloc(sizeof(*storage) * mx); |
244 | 0 | if (storage != NULL) |
245 | 0 | for (i = 0; i < mx; i++) |
246 | 0 | storage[i] = sk_EX_CALLBACK_value(ip->meth, i); |
247 | 0 | } |
248 | 80.2k | CRYPTO_THREAD_unlock(global->ex_data_lock); |
249 | | |
250 | 80.2k | if (mx > 0 && storage == NULL) |
251 | 0 | return 0; |
252 | 80.2k | for (i = 0; i < mx; i++) { |
253 | 0 | if (storage[i] != NULL && storage[i]->new_func != NULL) { |
254 | 0 | ptr = CRYPTO_get_ex_data(ad, i); |
255 | 0 | storage[i]->new_func(obj, ptr, ad, i, |
256 | 0 | storage[i]->argl, storage[i]->argp); |
257 | 0 | } |
258 | 0 | } |
259 | 80.2k | if (storage != stack) |
260 | 80.2k | OPENSSL_free(storage); |
261 | 80.2k | return 1; |
262 | 80.2k | } |
263 | | |
264 | | int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) |
265 | 80.0k | { |
266 | 80.0k | return ossl_crypto_new_ex_data_ex(NULL, class_index, obj, ad); |
267 | 80.0k | } |
268 | | |
269 | | /* |
270 | | * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks |
271 | | * for each index in the class used by this variable |
272 | | */ |
273 | | int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, |
274 | | const CRYPTO_EX_DATA *from) |
275 | 0 | { |
276 | 0 | int mx, j, i; |
277 | 0 | void *ptr; |
278 | 0 | EX_CALLBACK *stack[10]; |
279 | 0 | EX_CALLBACK **storage = NULL; |
280 | 0 | EX_CALLBACKS *ip; |
281 | 0 | int toret = 0; |
282 | 0 | OSSL_EX_DATA_GLOBAL *global; |
283 | |
|
284 | 0 | to->ctx = from->ctx; |
285 | 0 | if (from->sk == NULL) |
286 | | /* Nothing to copy over */ |
287 | 0 | return 1; |
288 | | |
289 | 0 | global = ossl_lib_ctx_get_ex_data_global(from->ctx); |
290 | 0 | if (global == NULL) |
291 | 0 | return 0; |
292 | | |
293 | 0 | ip = get_and_lock(global, class_index, 1); |
294 | 0 | if (ip == NULL) |
295 | 0 | return 0; |
296 | | |
297 | 0 | mx = sk_EX_CALLBACK_num(ip->meth); |
298 | 0 | j = sk_void_num(from->sk); |
299 | 0 | if (j < mx) |
300 | 0 | mx = j; |
301 | 0 | if (mx > 0) { |
302 | 0 | if (mx < (int)OSSL_NELEM(stack)) |
303 | 0 | storage = stack; |
304 | 0 | else |
305 | 0 | storage = OPENSSL_malloc(sizeof(*storage) * mx); |
306 | 0 | if (storage != NULL) |
307 | 0 | for (i = 0; i < mx; i++) |
308 | 0 | storage[i] = sk_EX_CALLBACK_value(ip->meth, i); |
309 | 0 | } |
310 | 0 | CRYPTO_THREAD_unlock(global->ex_data_lock); |
311 | |
|
312 | 0 | if (mx == 0) |
313 | 0 | return 1; |
314 | 0 | if (storage == NULL) |
315 | 0 | return 0; |
316 | | /* |
317 | | * Make sure the ex_data stack is at least |mx| elements long to avoid |
318 | | * issues in the for loop that follows; so go get the |mx|'th element |
319 | | * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign |
320 | | * to itself. This is normally a no-op; but ensures the stack is the |
321 | | * proper size |
322 | | */ |
323 | 0 | if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1))) |
324 | 0 | goto err; |
325 | | |
326 | 0 | for (i = 0; i < mx; i++) { |
327 | 0 | ptr = CRYPTO_get_ex_data(from, i); |
328 | 0 | if (storage[i] != NULL && storage[i]->dup_func != NULL) |
329 | 0 | if (!storage[i]->dup_func(to, from, &ptr, i, |
330 | 0 | storage[i]->argl, storage[i]->argp)) |
331 | 0 | goto err; |
332 | 0 | CRYPTO_set_ex_data(to, i, ptr); |
333 | 0 | } |
334 | 0 | toret = 1; |
335 | 0 | err: |
336 | 0 | if (storage != stack) |
337 | 0 | OPENSSL_free(storage); |
338 | 0 | return toret; |
339 | 0 | } |
340 | | |
341 | | struct ex_callback_entry { |
342 | | const EX_CALLBACK *excb; |
343 | | int index; |
344 | | }; |
345 | | |
346 | | static int ex_callback_compare(const void *a, const void *b) |
347 | 0 | { |
348 | 0 | const struct ex_callback_entry *ap = (const struct ex_callback_entry *)a; |
349 | 0 | const struct ex_callback_entry *bp = (const struct ex_callback_entry *)b; |
350 | |
|
351 | 0 | if (ap->excb == bp->excb) |
352 | 0 | return 0; |
353 | | |
354 | 0 | if (ap->excb == NULL) |
355 | 0 | return 1; |
356 | 0 | if (bp->excb == NULL) |
357 | 0 | return -1; |
358 | 0 | if (ap->excb->priority == bp->excb->priority) |
359 | 0 | return 0; |
360 | 0 | return ap->excb->priority > bp->excb->priority ? -1 : 1; |
361 | 0 | } |
362 | | |
363 | | /* |
364 | | * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for |
365 | | * each index in the class used by this variable |
366 | | */ |
367 | | void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) |
368 | 81.5k | { |
369 | 81.5k | int mx, i; |
370 | 81.5k | EX_CALLBACKS *ip; |
371 | 81.5k | void *ptr; |
372 | 81.5k | const EX_CALLBACK *f; |
373 | 81.5k | struct ex_callback_entry stack[10]; |
374 | 81.5k | struct ex_callback_entry *storage = NULL; |
375 | 81.5k | OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ad->ctx); |
376 | | |
377 | 81.5k | if (global == NULL) |
378 | 0 | goto err; |
379 | | |
380 | 81.5k | ip = get_and_lock(global, class_index, 1); |
381 | 81.5k | if (ip == NULL) |
382 | 0 | goto err; |
383 | | |
384 | 81.5k | mx = sk_EX_CALLBACK_num(ip->meth); |
385 | 81.5k | if (mx > 0) { |
386 | 0 | if (mx < (int)OSSL_NELEM(stack)) |
387 | 0 | storage = stack; |
388 | 0 | else |
389 | 0 | storage = OPENSSL_malloc(sizeof(*storage) * mx); |
390 | 0 | if (storage != NULL) |
391 | 0 | for (i = 0; i < mx; i++) { |
392 | 0 | storage[i].excb = sk_EX_CALLBACK_value(ip->meth, i); |
393 | 0 | storage[i].index = i; |
394 | 0 | } |
395 | 0 | } |
396 | 81.5k | CRYPTO_THREAD_unlock(global->ex_data_lock); |
397 | | |
398 | 81.5k | if (storage != NULL) { |
399 | | /* Sort according to priority. High priority first */ |
400 | 0 | qsort(storage, mx, sizeof(*storage), ex_callback_compare); |
401 | 0 | for (i = 0; i < mx; i++) { |
402 | 0 | f = storage[i].excb; |
403 | |
|
404 | 0 | if (f != NULL && f->free_func != NULL) { |
405 | 0 | ptr = CRYPTO_get_ex_data(ad, storage[i].index); |
406 | 0 | f->free_func(obj, ptr, ad, storage[i].index, f->argl, f->argp); |
407 | 0 | } |
408 | 0 | } |
409 | 0 | } |
410 | | |
411 | 81.5k | if (storage != stack) |
412 | 81.5k | OPENSSL_free(storage); |
413 | 81.5k | err: |
414 | 81.5k | sk_void_free(ad->sk); |
415 | 81.5k | ad->sk = NULL; |
416 | 81.5k | ad->ctx = NULL; |
417 | 81.5k | } |
418 | | |
419 | | /* |
420 | | * Allocate a given CRYPTO_EX_DATA item using the class specific allocation |
421 | | * function |
422 | | */ |
423 | | int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad, |
424 | | int idx) |
425 | 0 | { |
426 | 0 | void *curval; |
427 | |
|
428 | 0 | curval = CRYPTO_get_ex_data(ad, idx); |
429 | | /* Already there, no need to allocate */ |
430 | 0 | if (curval != NULL) |
431 | 0 | return 1; |
432 | | |
433 | 0 | return ossl_crypto_alloc_ex_data_intern(class_index, obj, ad, idx); |
434 | 0 | } |
435 | | |
436 | | int ossl_crypto_alloc_ex_data_intern(int class_index, void *obj, |
437 | | CRYPTO_EX_DATA *ad, int idx) |
438 | 0 | { |
439 | 0 | EX_CALLBACK *f; |
440 | 0 | EX_CALLBACKS *ip; |
441 | 0 | OSSL_EX_DATA_GLOBAL *global; |
442 | |
|
443 | 0 | global = ossl_lib_ctx_get_ex_data_global(ad->ctx); |
444 | 0 | if (global == NULL) |
445 | 0 | return 0; |
446 | | |
447 | 0 | ip = get_and_lock(global, class_index, 1); |
448 | 0 | if (ip == NULL) |
449 | 0 | return 0; |
450 | 0 | f = sk_EX_CALLBACK_value(ip->meth, idx); |
451 | 0 | CRYPTO_THREAD_unlock(global->ex_data_lock); |
452 | | |
453 | | /* |
454 | | * This should end up calling CRYPTO_set_ex_data(), which allocates |
455 | | * everything necessary to support placing the new data in the right spot. |
456 | | */ |
457 | 0 | if (f->new_func == NULL) |
458 | 0 | return 0; |
459 | | |
460 | 0 | f->new_func(obj, NULL, ad, idx, f->argl, f->argp); |
461 | |
|
462 | 0 | return 1; |
463 | 0 | } |
464 | | |
465 | | /* |
466 | | * For a given CRYPTO_EX_DATA variable, set the value corresponding to a |
467 | | * particular index in the class used by this variable |
468 | | */ |
469 | | int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val) |
470 | 0 | { |
471 | 0 | int i; |
472 | |
|
473 | 0 | if (ad->sk == NULL) { |
474 | 0 | if ((ad->sk = sk_void_new_null()) == NULL) { |
475 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB); |
476 | 0 | return 0; |
477 | 0 | } |
478 | 0 | } |
479 | | |
480 | 0 | for (i = sk_void_num(ad->sk); i <= idx; ++i) { |
481 | 0 | if (!sk_void_push(ad->sk, NULL)) { |
482 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB); |
483 | 0 | return 0; |
484 | 0 | } |
485 | 0 | } |
486 | 0 | if (sk_void_set(ad->sk, idx, val) != val) { |
487 | | /* Probably the index is out of bounds */ |
488 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
489 | 0 | return 0; |
490 | 0 | } |
491 | 0 | return 1; |
492 | 0 | } |
493 | | |
494 | | /* |
495 | | * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a |
496 | | * particular index in the class used by this variable |
497 | | */ |
498 | | void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx) |
499 | 0 | { |
500 | 0 | if (ad->sk == NULL || idx >= sk_void_num(ad->sk)) |
501 | 0 | return NULL; |
502 | 0 | return sk_void_value(ad->sk, idx); |
503 | 0 | } |
504 | | |
505 | | OSSL_LIB_CTX *ossl_crypto_ex_data_get_ossl_lib_ctx(const CRYPTO_EX_DATA *ad) |
506 | 0 | { |
507 | 0 | return ad->ctx; |
508 | 0 | } |