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