]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ex_data.c
Fix no-sm2
[thirdparty/openssl.git] / crypto / ex_data.c
CommitLineData
aa6bb135 1/*
6738bf14 2 * Copyright 1995-2018 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
1aedc35f 13int do_ex_data_init(OPENSSL_CTX *ctx)
f7520011 14{
1aedc35f
MC
15 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
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.*
0f113f3e 27 */
1aedc35f 28static EX_CALLBACKS *get_and_lock(OPENSSL_CTX *ctx, int class_index)
0f113f3e 29{
e6390aca 30 EX_CALLBACKS *ip;
1aedc35f 31 OSSL_EX_DATA_GLOBAL *global = NULL;
7e5363ab
RS
32
33 if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
de705824 34 CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);
7e5363ab
RS
35 return NULL;
36 }
37
1aedc35f 38 global = openssl_ctx_get_ex_data_global(ctx);
f2d20f0b 39 if (global == NULL || 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
1aedc35f 47 CRYPTO_THREAD_write_lock(global->ex_data_lock);
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 */
1aedc35f 63void crypto_cleanup_all_ex_data_int(OPENSSL_CTX *ctx)
0f113f3e 64{
7e5363ab 65 int i;
1aedc35f
MC
66 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
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,
e6390aca
RS
98 void *from_d, int idx,
99 long argl, void *argp)
100{
b3c31a65 101 return 1;
e6390aca
RS
102}
103
1aedc35f 104int crypto_free_ex_index_ex(OPENSSL_CTX *ctx, int class_index, int idx)
e6390aca 105{
0c080f73 106 EX_CALLBACKS *ip;
e6390aca
RS
107 EX_CALLBACK *a;
108 int toret = 0;
1aedc35f
MC
109 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
110
111 if (global == NULL)
97ee8af4 112 return 0;
e6390aca 113
1aedc35f 114 ip = get_and_lock(ctx, class_index);
e6390aca
RS
115 if (ip == NULL)
116 return 0;
117 if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
118 goto err;
119 a = sk_EX_CALLBACK_value(ip->meth, idx);
120 if (a == NULL)
121 goto err;
122 a->new_func = dummy_new;
123 a->dup_func = dummy_dup;
124 a->free_func = dummy_free;
125 toret = 1;
126err:
1aedc35f 127 CRYPTO_THREAD_unlock(global->ex_data_lock);
e6390aca
RS
128 return toret;
129}
130
1aedc35f
MC
131int CRYPTO_free_ex_index(int class_index, int idx)
132{
133 return crypto_free_ex_index_ex(NULL, class_index, idx);
134}
135
0f113f3e 136/*
e6390aca 137 * Register a new index.
0f113f3e 138 */
1aedc35f
MC
139int 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)
0f113f3e
MC
143{
144 int toret = -1;
e6390aca 145 EX_CALLBACK *a;
1aedc35f
MC
146 EX_CALLBACKS *ip;
147 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
7e5363ab 148
1aedc35f 149 if (global == NULL)
1f760760 150 return -1;
1aedc35f
MC
151
152 ip = get_and_lock(ctx, class_index);
e6390aca 153 if (ip == NULL)
0f113f3e 154 return -1;
8cab4e9b
EK
155
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. */
160 if (ip->meth == NULL
161 || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
1aedc35f 162 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
8cab4e9b
EK
163 goto err;
164 }
165 }
166
e6390aca 167 a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
90945fa3 168 if (a == NULL) {
1aedc35f 169 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
7e5363ab 170 goto err;
0f113f3e
MC
171 }
172 a->argl = argl;
173 a->argp = argp;
174 a->new_func = new_func;
175 a->dup_func = dup_func;
176 a->free_func = free_func;
3a079997 177
e6390aca 178 if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
1aedc35f 179 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
7e5363ab
RS
180 OPENSSL_free(a);
181 goto err;
182 }
e6390aca
RS
183 toret = sk_EX_CALLBACK_num(ip->meth) - 1;
184 (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
3a079997 185
7e5363ab 186 err:
1aedc35f 187 CRYPTO_THREAD_unlock(global->ex_data_lock);
0f113f3e
MC
188 return toret;
189}
3a079997 190
1aedc35f
MC
191int 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)
194{
195 return crypto_get_ex_new_index_ex(NULL, class_index, argl, argp, new_func,
196 dup_func, free_func);
197}
198
0f113f3e 199/*
7e5363ab
RS
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
e6390aca 202 * Thread-safe by copying a class's array of "EX_CALLBACK" entries
7e5363ab
RS
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.
0f113f3e 205 */
1aedc35f
MC
206int crypto_new_ex_data_ex(OPENSSL_CTX *ctx, int class_index, void *obj,
207 CRYPTO_EX_DATA *ad)
0f113f3e
MC
208{
209 int mx, i;
210 void *ptr;
e6390aca
RS
211 EX_CALLBACK **storage = NULL;
212 EX_CALLBACK *stack[10];
1aedc35f
MC
213 EX_CALLBACKS *ip;
214 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
215
216 if (global == NULL)
217 return 0;
7e5363ab 218
1aedc35f 219 ip = get_and_lock(ctx, class_index);
e6390aca 220 if (ip == NULL)
0f113f3e 221 return 0;
7e5363ab 222
1aedc35f 223 ad->ctx = ctx;
0f113f3e 224 ad->sk = NULL;
7e5363ab 225
e6390aca 226 mx = sk_EX_CALLBACK_num(ip->meth);
0f113f3e 227 if (mx > 0) {
7e5363ab
RS
228 if (mx < (int)OSSL_NELEM(stack))
229 storage = stack;
230 else
231 storage = OPENSSL_malloc(sizeof(*storage) * mx);
90945fa3 232 if (storage != NULL)
7e5363ab 233 for (i = 0; i < mx; i++)
e6390aca 234 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
0f113f3e 235 }
1aedc35f 236 CRYPTO_THREAD_unlock(global->ex_data_lock);
7e5363ab
RS
237
238 if (mx > 0 && storage == NULL) {
1aedc35f 239 CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA_EX, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
240 return 0;
241 }
242 for (i = 0; i < mx; i++) {
23dc8feb 243 if (storage[i] != NULL && storage[i]->new_func != NULL) {
0f113f3e
MC
244 ptr = CRYPTO_get_ex_data(ad, i);
245 storage[i]->new_func(obj, ptr, ad, i,
246 storage[i]->argl, storage[i]->argp);
247 }
248 }
7e5363ab
RS
249 if (storage != stack)
250 OPENSSL_free(storage);
0f113f3e
MC
251 return 1;
252}
3a079997 253
1aedc35f
MC
254int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
255{
256 return crypto_new_ex_data_ex(NULL, class_index, obj, ad);
257}
258
7e5363ab
RS
259/*
260 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
261 * for each index in the class used by this variable
262 */
263int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
3c853776 264 const CRYPTO_EX_DATA *from)
0f113f3e
MC
265{
266 int mx, j, i;
b3c31a65 267 void *ptr;
e6390aca
RS
268 EX_CALLBACK *stack[10];
269 EX_CALLBACK **storage = NULL;
270 EX_CALLBACKS *ip;
b3c31a65 271 int toret = 0;
1aedc35f
MC
272 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(from->ctx);
273
274 if (global == NULL)
275 return 0;
7e5363ab 276
1aedc35f 277 to->ctx = from->ctx;
7e5363ab
RS
278 if (from->sk == NULL)
279 /* Nothing to copy over */
0f113f3e 280 return 1;
1aedc35f 281 if ((ip = get_and_lock(from->ctx, class_index)) == NULL)
0f113f3e 282 return 0;
7e5363ab 283
e6390aca 284 mx = sk_EX_CALLBACK_num(ip->meth);
0f113f3e
MC
285 j = sk_void_num(from->sk);
286 if (j < mx)
287 mx = j;
288 if (mx > 0) {
7e5363ab
RS
289 if (mx < (int)OSSL_NELEM(stack))
290 storage = stack;
291 else
292 storage = OPENSSL_malloc(sizeof(*storage) * mx);
90945fa3 293 if (storage != NULL)
7e5363ab 294 for (i = 0; i < mx; i++)
e6390aca 295 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
0f113f3e 296 }
1aedc35f 297 CRYPTO_THREAD_unlock(global->ex_data_lock);
7e5363ab 298
b3c31a65
BE
299 if (mx == 0)
300 return 1;
301 if (storage == NULL) {
7e5363ab 302 CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
303 return 0;
304 }
1ee21259
TS
305 /*
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
310 * proper size
311 */
312 if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
b3c31a65 313 goto err;
7e5363ab 314
0f113f3e
MC
315 for (i = 0; i < mx; i++) {
316 ptr = CRYPTO_get_ex_data(from, i);
23dc8feb 317 if (storage[i] != NULL && storage[i]->dup_func != NULL)
b3c31a65
BE
318 if (!storage[i]->dup_func(to, from, &ptr, i,
319 storage[i]->argl, storage[i]->argp))
320 goto err;
0f113f3e
MC
321 CRYPTO_set_ex_data(to, i, ptr);
322 }
b3c31a65
BE
323 toret = 1;
324 err:
7e5363ab
RS
325 if (storage != stack)
326 OPENSSL_free(storage);
b3c31a65 327 return toret;
0f113f3e 328}
3a079997 329
7e5363ab
RS
330
331/*
332 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
333 * each index in the class used by this variable
334 */
335void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
0f113f3e
MC
336{
337 int mx, i;
e6390aca 338 EX_CALLBACKS *ip;
0f113f3e 339 void *ptr;
83b4049a 340 EX_CALLBACK *f;
e6390aca
RS
341 EX_CALLBACK *stack[10];
342 EX_CALLBACK **storage = NULL;
1aedc35f 343 OSSL_EX_DATA_GLOBAL *global;
7e5363ab 344
1aedc35f
MC
345 if ((ip = get_and_lock(ad->ctx, class_index)) == NULL)
346 goto err;
347 global = openssl_ctx_get_ex_data_global(ad->ctx);
348 if (global == NULL)
83b4049a 349 goto err;
7e5363ab 350
e6390aca 351 mx = sk_EX_CALLBACK_num(ip->meth);
0f113f3e 352 if (mx > 0) {
7e5363ab
RS
353 if (mx < (int)OSSL_NELEM(stack))
354 storage = stack;
355 else
356 storage = OPENSSL_malloc(sizeof(*storage) * mx);
90945fa3 357 if (storage != NULL)
7e5363ab 358 for (i = 0; i < mx; i++)
e6390aca 359 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
0f113f3e 360 }
1aedc35f 361 CRYPTO_THREAD_unlock(global->ex_data_lock);
7e5363ab 362
0f113f3e 363 for (i = 0; i < mx; i++) {
83b4049a
BE
364 if (storage != NULL)
365 f = storage[i];
366 else {
1aedc35f 367 CRYPTO_THREAD_write_lock(global->ex_data_lock);
83b4049a 368 f = sk_EX_CALLBACK_value(ip->meth, i);
1aedc35f 369 CRYPTO_THREAD_unlock(global->ex_data_lock);
83b4049a
BE
370 }
371 if (f != NULL && f->free_func != NULL) {
0f113f3e 372 ptr = CRYPTO_get_ex_data(ad, i);
83b4049a 373 f->free_func(obj, ptr, ad, i, f->argl, f->argp);
0f113f3e
MC
374 }
375 }
7e5363ab
RS
376
377 if (storage != stack)
378 OPENSSL_free(storage);
83b4049a 379 err:
25aaa98a
RS
380 sk_void_free(ad->sk);
381 ad->sk = NULL;
1aedc35f 382 ad->ctx = NULL;
0f113f3e 383}
3a079997 384
e17f5b6a
RL
385/*
386 * Allocate a given CRYPTO_EX_DATA item using the class specific allocation
387 * function
388 */
389int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad,
390 int idx)
391{
392 EX_CALLBACK *f;
393 EX_CALLBACKS *ip;
394 void *curval;
1aedc35f
MC
395 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ad->ctx);
396
397 if (global == NULL)
398 return 0;
e17f5b6a
RL
399
400 curval = CRYPTO_get_ex_data(ad, idx);
401
402 /* Already there, no need to allocate */
403 if (curval != NULL)
404 return 1;
405
1aedc35f 406 ip = get_and_lock(ad->ctx, class_index);
23dc8feb
F
407 if (ip == NULL)
408 return 0;
e17f5b6a 409 f = sk_EX_CALLBACK_value(ip->meth, idx);
1aedc35f 410 CRYPTO_THREAD_unlock(global->ex_data_lock);
e17f5b6a
RL
411
412 /*
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.
415 */
23dc8feb
F
416 if (f->new_func == NULL)
417 return 0;
418
823ee00a 419 f->new_func(obj, NULL, ad, idx, f->argl, f->argp);
e17f5b6a
RL
420
421 return 1;
422}
423
0f113f3e
MC
424/*
425 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
426 * particular index in the class used by this variable
427 */
dd9d233e 428int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
0f113f3e
MC
429{
430 int i;
431
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);
7e5363ab 435 return 0;
0f113f3e
MC
436 }
437 }
0f113f3e 438
7e5363ab 439 for (i = sk_void_num(ad->sk); i <= idx; ++i) {
0f113f3e
MC
440 if (!sk_void_push(ad->sk, NULL)) {
441 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
7e5363ab 442 return 0;
0f113f3e 443 }
0f113f3e
MC
444 }
445 sk_void_set(ad->sk, idx, val);
7e5363ab 446 return 1;
0f113f3e
MC
447}
448
449/*
450 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
451 * particular index in the class used by this variable
452 */
bbbc96a8 453void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
0f113f3e 454{
7e5363ab
RS
455 if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
456 return NULL;
457 return sk_void_value(ad->sk, idx);
0f113f3e 458}
1aedc35f
MC
459
460OPENSSL_CTX *crypto_ex_data_get_openssl_ctx(const CRYPTO_EX_DATA *ad)
461{
462 return ad->ctx;
463}