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