DRBG: make locking api truly private
[openssl.git] / crypto / ex_data.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 "internal/cryptlib_int.h"
11 #include "internal/thread_once.h"
12
13 /*
14  * Each structure type (sometimes called a class), that supports
15  * exdata has a stack of callbacks for each instance.
16  */
17 struct ex_callback_st {
18     long argl;                  /* Arbitrary long */
19     void *argp;                 /* Arbitrary void * */
20     CRYPTO_EX_new *new_func;
21     CRYPTO_EX_free *free_func;
22     CRYPTO_EX_dup *dup_func;
23 };
24
25 /*
26  * The state for each class.  This could just be a typedef, but
27  * a structure allows future changes.
28  */
29 typedef struct ex_callbacks_st {
30     STACK_OF(EX_CALLBACK) *meth;
31 } EX_CALLBACKS;
32
33 static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
34
35 static CRYPTO_RWLOCK *ex_data_lock = NULL;
36 static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT;
37
38 DEFINE_RUN_ONCE_STATIC(do_ex_data_init)
39 {
40     OPENSSL_init_crypto(0, NULL);
41     ex_data_lock = CRYPTO_THREAD_lock_new();
42     return ex_data_lock != NULL;
43 }
44
45 /*
46  * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
47  * a given class.  On success, *holds the lock.*
48  */
49 static EX_CALLBACKS *get_and_lock(int class_index)
50 {
51     EX_CALLBACKS *ip;
52
53     if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
54         CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);
55         return NULL;
56     }
57
58     if (!RUN_ONCE(&ex_data_init, do_ex_data_init)) {
59         CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
60         return NULL;
61     }
62
63     if (ex_data_lock == NULL) {
64         /*
65          * This can happen in normal operation when using CRYPTO_mem_leaks().
66          * The CRYPTO_mem_leaks() function calls OPENSSL_cleanup() which cleans
67          * up the locks. Subsequently the BIO that CRYPTO_mem_leaks() uses gets
68          * freed, which also attempts to free the ex_data. However
69          * CRYPTO_mem_leaks() ensures that the ex_data is freed early (i.e.
70          * before OPENSSL_cleanup() is called), so if we get here we can safely
71          * ignore this operation. We just treat it as an error.
72          */
73          return NULL;
74     }
75
76     ip = &ex_data[class_index];
77     CRYPTO_THREAD_write_lock(ex_data_lock);
78     return ip;
79 }
80
81 static void cleanup_cb(EX_CALLBACK *funcs)
82 {
83     OPENSSL_free(funcs);
84 }
85
86 /*
87  * Release all "ex_data" state to prevent memory leaks. This can't be made
88  * thread-safe without overhauling a lot of stuff, and shouldn't really be
89  * called under potential race-conditions anyway (it's for program shutdown
90  * after all).
91  */
92 void crypto_cleanup_all_ex_data_int(void)
93 {
94     int i;
95
96     for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
97         EX_CALLBACKS *ip = &ex_data[i];
98
99         sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
100         ip->meth = NULL;
101     }
102
103     CRYPTO_THREAD_lock_free(ex_data_lock);
104     ex_data_lock = NULL;
105 }
106
107
108 /*
109  * Unregister a new index by replacing the callbacks with no-ops.
110  * Any in-use instances are leaked.
111  */
112 static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
113                      long argl, void *argp)
114 {
115 }
116
117 static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
118                        long argl, void *argp)
119 {
120 }
121
122 static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
123                      void *from_d, int idx,
124                      long argl, void *argp)
125 {
126     return 1;
127 }
128
129 int CRYPTO_free_ex_index(int class_index, int idx)
130 {
131     EX_CALLBACKS *ip = get_and_lock(class_index);
132     EX_CALLBACK *a;
133     int toret = 0;
134
135     if (ip == NULL)
136         return 0;
137     if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
138         goto err;
139     a = sk_EX_CALLBACK_value(ip->meth, idx);
140     if (a == NULL)
141         goto err;
142     a->new_func = dummy_new;
143     a->dup_func = dummy_dup;
144     a->free_func = dummy_free;
145     toret = 1;
146 err:
147     CRYPTO_THREAD_unlock(ex_data_lock);
148     return toret;
149 }
150
151 /*
152  * Register a new index.
153  */
154 int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
155                             CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
156                             CRYPTO_EX_free *free_func)
157 {
158     int toret = -1;
159     EX_CALLBACK *a;
160     EX_CALLBACKS *ip = get_and_lock(class_index);
161
162     if (ip == NULL)
163         return -1;
164
165     if (ip->meth == NULL) {
166         ip->meth = sk_EX_CALLBACK_new_null();
167         /* We push an initial value on the stack because the SSL
168          * "app_data" routines use ex_data index zero.  See RT 3710. */
169         if (ip->meth == NULL
170             || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
171             CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
172             goto err;
173         }
174     }
175
176     a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
177     if (a == NULL) {
178         CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
179         goto err;
180     }
181     a->argl = argl;
182     a->argp = argp;
183     a->new_func = new_func;
184     a->dup_func = dup_func;
185     a->free_func = free_func;
186
187     if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
188         CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
189         OPENSSL_free(a);
190         goto err;
191     }
192     toret = sk_EX_CALLBACK_num(ip->meth) - 1;
193     (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
194
195  err:
196     CRYPTO_THREAD_unlock(ex_data_lock);
197     return toret;
198 }
199
200 /*
201  * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
202  * calling new() callbacks for each index in the class used by this variable
203  * Thread-safe by copying a class's array of "EX_CALLBACK" entries
204  * in the lock, then using them outside the lock. Note this only applies
205  * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
206  */
207 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
208 {
209     int mx, i;
210     void *ptr;
211     EX_CALLBACK **storage = NULL;
212     EX_CALLBACK *stack[10];
213     EX_CALLBACKS *ip = get_and_lock(class_index);
214
215     if (ip == NULL)
216         return 0;
217
218     ad->sk = NULL;
219
220     mx = sk_EX_CALLBACK_num(ip->meth);
221     if (mx > 0) {
222         if (mx < (int)OSSL_NELEM(stack))
223             storage = stack;
224         else
225             storage = OPENSSL_malloc(sizeof(*storage) * mx);
226         if (storage != NULL)
227             for (i = 0; i < mx; i++)
228                 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
229     }
230     CRYPTO_THREAD_unlock(ex_data_lock);
231
232     if (mx > 0 && storage == NULL) {
233         CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
234         return 0;
235     }
236     for (i = 0; i < mx; i++) {
237         if (storage[i] && storage[i]->new_func) {
238             ptr = CRYPTO_get_ex_data(ad, i);
239             storage[i]->new_func(obj, ptr, ad, i,
240                                  storage[i]->argl, storage[i]->argp);
241         }
242     }
243     if (storage != stack)
244         OPENSSL_free(storage);
245     return 1;
246 }
247
248 /*
249  * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
250  * for each index in the class used by this variable
251  */
252 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
253                        const CRYPTO_EX_DATA *from)
254 {
255     int mx, j, i;
256     void *ptr;
257     EX_CALLBACK *stack[10];
258     EX_CALLBACK **storage = NULL;
259     EX_CALLBACKS *ip;
260     int toret = 0;
261
262     if (from->sk == NULL)
263         /* Nothing to copy over */
264         return 1;
265     if ((ip = get_and_lock(class_index)) == NULL)
266         return 0;
267
268     mx = sk_EX_CALLBACK_num(ip->meth);
269     j = sk_void_num(from->sk);
270     if (j < mx)
271         mx = j;
272     if (mx > 0) {
273         if (mx < (int)OSSL_NELEM(stack))
274             storage = stack;
275         else
276             storage = OPENSSL_malloc(sizeof(*storage) * mx);
277         if (storage != NULL)
278             for (i = 0; i < mx; i++)
279                 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
280     }
281     CRYPTO_THREAD_unlock(ex_data_lock);
282
283     if (mx == 0)
284         return 1;
285     if (storage == NULL) {
286         CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
287         return 0;
288     }
289     /*
290      * Make sure the ex_data stack is at least |mx| elements long to avoid
291      * issues in the for loop that follows; so go get the |mx|'th element
292      * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
293      * to itself. This is normally a no-op; but ensures the stack is the
294      * proper size
295      */
296     if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
297         goto err;
298
299     for (i = 0; i < mx; i++) {
300         ptr = CRYPTO_get_ex_data(from, i);
301         if (storage[i] && storage[i]->dup_func)
302             if (!storage[i]->dup_func(to, from, &ptr, i,
303                                       storage[i]->argl, storage[i]->argp))
304                 goto err;
305         CRYPTO_set_ex_data(to, i, ptr);
306     }
307     toret = 1;
308  err:
309     if (storage != stack)
310         OPENSSL_free(storage);
311     return toret;
312 }
313
314
315 /*
316  * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
317  * each index in the class used by this variable
318  */
319 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
320 {
321     int mx, i;
322     EX_CALLBACKS *ip;
323     void *ptr;
324     EX_CALLBACK *f;
325     EX_CALLBACK *stack[10];
326     EX_CALLBACK **storage = NULL;
327
328     if ((ip = get_and_lock(class_index)) == NULL)
329         goto err;
330
331     mx = sk_EX_CALLBACK_num(ip->meth);
332     if (mx > 0) {
333         if (mx < (int)OSSL_NELEM(stack))
334             storage = stack;
335         else
336             storage = OPENSSL_malloc(sizeof(*storage) * mx);
337         if (storage != NULL)
338             for (i = 0; i < mx; i++)
339                 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
340     }
341     CRYPTO_THREAD_unlock(ex_data_lock);
342
343     for (i = 0; i < mx; i++) {
344         if (storage != NULL)
345             f = storage[i];
346         else {
347             CRYPTO_THREAD_write_lock(ex_data_lock);
348             f = sk_EX_CALLBACK_value(ip->meth, i);
349             CRYPTO_THREAD_unlock(ex_data_lock);
350         }
351         if (f != NULL && f->free_func != NULL) {
352             ptr = CRYPTO_get_ex_data(ad, i);
353             f->free_func(obj, ptr, ad, i, f->argl, f->argp);
354         }
355     }
356
357     if (storage != stack)
358         OPENSSL_free(storage);
359  err:
360     sk_void_free(ad->sk);
361     ad->sk = NULL;
362 }
363
364 /*
365  * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
366  * particular index in the class used by this variable
367  */
368 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
369 {
370     int i;
371
372     if (ad->sk == NULL) {
373         if ((ad->sk = sk_void_new_null()) == NULL) {
374             CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
375             return 0;
376         }
377     }
378
379     for (i = sk_void_num(ad->sk); i <= idx; ++i) {
380         if (!sk_void_push(ad->sk, NULL)) {
381             CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
382             return 0;
383         }
384     }
385     sk_void_set(ad->sk, idx, val);
386     return 1;
387 }
388
389 /*
390  * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
391  * particular index in the class used by this variable
392  */
393 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
394 {
395     if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
396         return NULL;
397     return sk_void_value(ad->sk, idx);
398 }