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