]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ex_data.c
Implement EVP_MAC_do_all_ex()
[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
ff234405 10#include "internal/cryptlib_int.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
MC
40 /*
41 * This can happen in normal operation when using CRYPTO_mem_leaks().
42 * The CRYPTO_mem_leaks() function calls OPENSSL_cleanup() which cleans
43 * up the locks. Subsequently the BIO that CRYPTO_mem_leaks() uses gets
44 * freed, which also attempts to free the ex_data. However
45 * CRYPTO_mem_leaks() ensures that the ex_data is freed early (i.e.
46 * before OPENSSL_cleanup() is called), so if we get here we can safely
47 * ignore this operation. We just treat it as an error.
48 */
49 return NULL;
50 }
51
1aedc35f 52 CRYPTO_THREAD_write_lock(global->ex_data_lock);
aee6e29f 53 ip = &global->ex_data[class_index];
7e5363ab 54 return ip;
0f113f3e
MC
55}
56
e6390aca 57static void cleanup_cb(EX_CALLBACK *funcs)
0f113f3e
MC
58{
59 OPENSSL_free(funcs);
60}
3a079997 61
0f113f3e 62/*
7e5363ab
RS
63 * Release all "ex_data" state to prevent memory leaks. This can't be made
64 * thread-safe without overhauling a lot of stuff, and shouldn't really be
65 * called under potential race-conditions anyway (it's for program shutdown
66 * after all).
0f113f3e 67 */
1aedc35f 68void crypto_cleanup_all_ex_data_int(OPENSSL_CTX *ctx)
0f113f3e 69{
7e5363ab 70 int i;
1aedc35f
MC
71 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
72
73 if (global == NULL)
74 return;
0f113f3e 75
7e5363ab 76 for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
1aedc35f 77 EX_CALLBACKS *ip = &global->ex_data[i];
7e5363ab 78
e6390aca 79 sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
7e5363ab 80 ip->meth = NULL;
0f113f3e 81 }
1ee7b8b9 82
1aedc35f
MC
83 CRYPTO_THREAD_lock_free(global->ex_data_lock);
84 global->ex_data_lock = NULL;
0f113f3e
MC
85}
86
e6390aca
RS
87
88/*
89 * Unregister a new index by replacing the callbacks with no-ops.
90 * Any in-use instances are leaked.
91 */
92static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
93 long argl, void *argp)
94{
95}
96
97static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
98 long argl, void *argp)
99{
100}
101
3c853776 102static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
e6390aca
RS
103 void *from_d, int idx,
104 long argl, void *argp)
105{
b3c31a65 106 return 1;
e6390aca
RS
107}
108
1aedc35f 109int crypto_free_ex_index_ex(OPENSSL_CTX *ctx, int class_index, int idx)
e6390aca 110{
1aedc35f 111 EX_CALLBACKS *ip = get_and_lock(ctx, class_index);
e6390aca
RS
112 EX_CALLBACK *a;
113 int toret = 0;
1aedc35f
MC
114 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
115
116 if (global == NULL)
97ee8af4 117 return 0;
e6390aca 118
1aedc35f 119 ip = get_and_lock(ctx, class_index);
e6390aca
RS
120 if (ip == NULL)
121 return 0;
122 if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
123 goto err;
124 a = sk_EX_CALLBACK_value(ip->meth, idx);
125 if (a == NULL)
126 goto err;
127 a->new_func = dummy_new;
128 a->dup_func = dummy_dup;
129 a->free_func = dummy_free;
130 toret = 1;
131err:
1aedc35f 132 CRYPTO_THREAD_unlock(global->ex_data_lock);
e6390aca
RS
133 return toret;
134}
135
1aedc35f
MC
136int CRYPTO_free_ex_index(int class_index, int idx)
137{
138 return crypto_free_ex_index_ex(NULL, class_index, idx);
139}
140
0f113f3e 141/*
e6390aca 142 * Register a new index.
0f113f3e 143 */
1aedc35f
MC
144int crypto_get_ex_new_index_ex(OPENSSL_CTX *ctx, int class_index, long argl,
145 void *argp, CRYPTO_EX_new *new_func,
146 CRYPTO_EX_dup *dup_func,
147 CRYPTO_EX_free *free_func)
0f113f3e
MC
148{
149 int toret = -1;
e6390aca 150 EX_CALLBACK *a;
1aedc35f
MC
151 EX_CALLBACKS *ip;
152 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
7e5363ab 153
1aedc35f 154 if (global == NULL)
1f760760 155 return -1;
1aedc35f
MC
156
157 ip = get_and_lock(ctx, class_index);
e6390aca 158 if (ip == NULL)
0f113f3e 159 return -1;
8cab4e9b
EK
160
161 if (ip->meth == NULL) {
162 ip->meth = sk_EX_CALLBACK_new_null();
163 /* We push an initial value on the stack because the SSL
164 * "app_data" routines use ex_data index zero. See RT 3710. */
165 if (ip->meth == NULL
166 || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
1aedc35f 167 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
8cab4e9b
EK
168 goto err;
169 }
170 }
171
e6390aca 172 a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
90945fa3 173 if (a == NULL) {
1aedc35f 174 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
7e5363ab 175 goto err;
0f113f3e
MC
176 }
177 a->argl = argl;
178 a->argp = argp;
179 a->new_func = new_func;
180 a->dup_func = dup_func;
181 a->free_func = free_func;
3a079997 182
e6390aca 183 if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
1aedc35f 184 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, 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{
200 return crypto_get_ex_new_index_ex(NULL, class_index, argl, argp, new_func,
201 dup_func, free_func);
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 */
1aedc35f
MC
211int crypto_new_ex_data_ex(OPENSSL_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
MC
218 EX_CALLBACKS *ip;
219 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
220
221 if (global == NULL)
222 return 0;
7e5363ab 223
1aedc35f 224 ip = get_and_lock(ctx, class_index);
e6390aca 225 if (ip == NULL)
0f113f3e 226 return 0;
7e5363ab 227
1aedc35f 228 ad->ctx = ctx;
0f113f3e 229 ad->sk = NULL;
7e5363ab 230
e6390aca 231 mx = sk_EX_CALLBACK_num(ip->meth);
0f113f3e 232 if (mx > 0) {
7e5363ab
RS
233 if (mx < (int)OSSL_NELEM(stack))
234 storage = stack;
235 else
236 storage = OPENSSL_malloc(sizeof(*storage) * mx);
90945fa3 237 if (storage != NULL)
7e5363ab 238 for (i = 0; i < mx; i++)
e6390aca 239 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
0f113f3e 240 }
1aedc35f 241 CRYPTO_THREAD_unlock(global->ex_data_lock);
7e5363ab
RS
242
243 if (mx > 0 && storage == NULL) {
1aedc35f 244 CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA_EX, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
245 return 0;
246 }
247 for (i = 0; i < mx; i++) {
23dc8feb 248 if (storage[i] != NULL && storage[i]->new_func != NULL) {
0f113f3e
MC
249 ptr = CRYPTO_get_ex_data(ad, i);
250 storage[i]->new_func(obj, ptr, ad, i,
251 storage[i]->argl, storage[i]->argp);
252 }
253 }
7e5363ab
RS
254 if (storage != stack)
255 OPENSSL_free(storage);
0f113f3e
MC
256 return 1;
257}
3a079997 258
1aedc35f
MC
259int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
260{
261 return crypto_new_ex_data_ex(NULL, class_index, obj, ad);
262}
263
7e5363ab
RS
264/*
265 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
266 * for each index in the class used by this variable
267 */
268int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
3c853776 269 const CRYPTO_EX_DATA *from)
0f113f3e
MC
270{
271 int mx, j, i;
b3c31a65 272 void *ptr;
e6390aca
RS
273 EX_CALLBACK *stack[10];
274 EX_CALLBACK **storage = NULL;
275 EX_CALLBACKS *ip;
b3c31a65 276 int toret = 0;
1aedc35f
MC
277 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(from->ctx);
278
279 if (global == NULL)
280 return 0;
7e5363ab 281
1aedc35f 282 to->ctx = from->ctx;
7e5363ab
RS
283 if (from->sk == NULL)
284 /* Nothing to copy over */
0f113f3e 285 return 1;
1aedc35f 286 if ((ip = get_and_lock(from->ctx, class_index)) == NULL)
0f113f3e 287 return 0;
7e5363ab 288
e6390aca 289 mx = sk_EX_CALLBACK_num(ip->meth);
0f113f3e
MC
290 j = sk_void_num(from->sk);
291 if (j < mx)
292 mx = j;
293 if (mx > 0) {
7e5363ab
RS
294 if (mx < (int)OSSL_NELEM(stack))
295 storage = stack;
296 else
297 storage = OPENSSL_malloc(sizeof(*storage) * mx);
90945fa3 298 if (storage != NULL)
7e5363ab 299 for (i = 0; i < mx; i++)
e6390aca 300 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
0f113f3e 301 }
1aedc35f 302 CRYPTO_THREAD_unlock(global->ex_data_lock);
7e5363ab 303
b3c31a65
BE
304 if (mx == 0)
305 return 1;
306 if (storage == NULL) {
7e5363ab 307 CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
308 return 0;
309 }
1ee21259
TS
310 /*
311 * Make sure the ex_data stack is at least |mx| elements long to avoid
312 * issues in the for loop that follows; so go get the |mx|'th element
313 * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
314 * to itself. This is normally a no-op; but ensures the stack is the
315 * proper size
316 */
317 if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
b3c31a65 318 goto err;
7e5363ab 319
0f113f3e
MC
320 for (i = 0; i < mx; i++) {
321 ptr = CRYPTO_get_ex_data(from, i);
23dc8feb 322 if (storage[i] != NULL && storage[i]->dup_func != NULL)
b3c31a65
BE
323 if (!storage[i]->dup_func(to, from, &ptr, i,
324 storage[i]->argl, storage[i]->argp))
325 goto err;
0f113f3e
MC
326 CRYPTO_set_ex_data(to, i, ptr);
327 }
b3c31a65
BE
328 toret = 1;
329 err:
7e5363ab
RS
330 if (storage != stack)
331 OPENSSL_free(storage);
b3c31a65 332 return toret;
0f113f3e 333}
3a079997 334
7e5363ab
RS
335
336/*
337 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
338 * each index in the class used by this variable
339 */
340void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
0f113f3e
MC
341{
342 int mx, i;
e6390aca 343 EX_CALLBACKS *ip;
0f113f3e 344 void *ptr;
83b4049a 345 EX_CALLBACK *f;
e6390aca
RS
346 EX_CALLBACK *stack[10];
347 EX_CALLBACK **storage = NULL;
1aedc35f 348 OSSL_EX_DATA_GLOBAL *global;
7e5363ab 349
1aedc35f
MC
350 if ((ip = get_and_lock(ad->ctx, class_index)) == NULL)
351 goto err;
352 global = openssl_ctx_get_ex_data_global(ad->ctx);
353 if (global == NULL)
83b4049a 354 goto err;
7e5363ab 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 {
1aedc35f 372 CRYPTO_THREAD_write_lock(global->ex_data_lock);
83b4049a 373 f = sk_EX_CALLBACK_value(ip->meth, i);
1aedc35f 374 CRYPTO_THREAD_unlock(global->ex_data_lock);
83b4049a
BE
375 }
376 if (f != NULL && f->free_func != NULL) {
0f113f3e 377 ptr = CRYPTO_get_ex_data(ad, i);
83b4049a 378 f->free_func(obj, ptr, ad, i, f->argl, f->argp);
0f113f3e
MC
379 }
380 }
7e5363ab
RS
381
382 if (storage != stack)
383 OPENSSL_free(storage);
83b4049a 384 err:
25aaa98a
RS
385 sk_void_free(ad->sk);
386 ad->sk = NULL;
1aedc35f 387 ad->ctx = NULL;
0f113f3e 388}
3a079997 389
e17f5b6a
RL
390/*
391 * Allocate a given CRYPTO_EX_DATA item using the class specific allocation
392 * function
393 */
394int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad,
395 int idx)
396{
397 EX_CALLBACK *f;
398 EX_CALLBACKS *ip;
399 void *curval;
1aedc35f
MC
400 OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ad->ctx);
401
402 if (global == NULL)
403 return 0;
e17f5b6a
RL
404
405 curval = CRYPTO_get_ex_data(ad, idx);
406
407 /* Already there, no need to allocate */
408 if (curval != NULL)
409 return 1;
410
1aedc35f 411 ip = get_and_lock(ad->ctx, class_index);
23dc8feb
F
412 if (ip == NULL)
413 return 0;
e17f5b6a 414 f = sk_EX_CALLBACK_value(ip->meth, idx);
1aedc35f 415 CRYPTO_THREAD_unlock(global->ex_data_lock);
e17f5b6a
RL
416
417 /*
418 * This should end up calling CRYPTO_set_ex_data(), which allocates
419 * everything necessary to support placing the new data in the right spot.
420 */
23dc8feb
F
421 if (f->new_func == NULL)
422 return 0;
423
823ee00a 424 f->new_func(obj, NULL, ad, idx, f->argl, f->argp);
e17f5b6a
RL
425
426 return 1;
427}
428
0f113f3e
MC
429/*
430 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
431 * particular index in the class used by this variable
432 */
dd9d233e 433int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
0f113f3e
MC
434{
435 int i;
436
437 if (ad->sk == NULL) {
438 if ((ad->sk = sk_void_new_null()) == NULL) {
439 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
7e5363ab 440 return 0;
0f113f3e
MC
441 }
442 }
0f113f3e 443
7e5363ab 444 for (i = sk_void_num(ad->sk); i <= idx; ++i) {
0f113f3e
MC
445 if (!sk_void_push(ad->sk, NULL)) {
446 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
7e5363ab 447 return 0;
0f113f3e 448 }
0f113f3e
MC
449 }
450 sk_void_set(ad->sk, idx, val);
7e5363ab 451 return 1;
0f113f3e
MC
452}
453
454/*
455 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
456 * particular index in the class used by this variable
457 */
bbbc96a8 458void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
0f113f3e 459{
7e5363ab
RS
460 if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
461 return NULL;
462 return sk_void_value(ad->sk, idx);
0f113f3e 463}
1aedc35f
MC
464
465OPENSSL_CTX *crypto_ex_data_get_openssl_ctx(const CRYPTO_EX_DATA *ad)
466{
467 return ad->ctx;
468}