Coverage Report

Created: 2025-12-10 06:24

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