BIO_f_zlib: Properly handle BIO_CTRL_PENDING and BIO_CTRL_WPENDING calls.
[openssl.git] / crypto / ex_data.c
1 /*
2  * Copyright 1995-2018 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 "internal/cryptlib_int.h"
11 #include "internal/thread_once.h"
12
13 int do_ex_data_init(OPENSSL_CTX *ctx)
14 {
15     OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
16
17     if (global == NULL)
18         return 0;
19
20     global->ex_data_lock = CRYPTO_THREAD_lock_new();
21     return global->ex_data_lock != NULL;
22 }
23
24 /*
25  * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
26  * a given class.  On success, *holds the lock.*
27  */
28 static EX_CALLBACKS *get_and_lock(OPENSSL_CTX *ctx, int class_index)
29 {
30     EX_CALLBACKS *ip;
31     OSSL_EX_DATA_GLOBAL *global = NULL;
32
33     if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
34         CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);
35         return NULL;
36     }
37
38     global = openssl_ctx_get_ex_data_global(ctx);
39     if (global == NULL || global->ex_data_lock == NULL) {
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
52     CRYPTO_THREAD_write_lock(global->ex_data_lock);
53     ip = &global->ex_data[class_index];
54     return ip;
55 }
56
57 static void cleanup_cb(EX_CALLBACK *funcs)
58 {
59     OPENSSL_free(funcs);
60 }
61
62 /*
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).
67  */
68 void crypto_cleanup_all_ex_data_int(OPENSSL_CTX *ctx)
69 {
70     int i;
71     OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
72
73     if (global == NULL)
74         return;
75
76     for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
77         EX_CALLBACKS *ip = &global->ex_data[i];
78
79         sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
80         ip->meth = NULL;
81     }
82
83     CRYPTO_THREAD_lock_free(global->ex_data_lock);
84     global->ex_data_lock = NULL;
85 }
86
87
88 /*
89  * Unregister a new index by replacing the callbacks with no-ops.
90  * Any in-use instances are leaked.
91  */
92 static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
93                      long argl, void *argp)
94 {
95 }
96
97 static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
98                        long argl, void *argp)
99 {
100 }
101
102 static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
103                      void *from_d, int idx,
104                      long argl, void *argp)
105 {
106     return 1;
107 }
108
109 int crypto_free_ex_index_ex(OPENSSL_CTX *ctx, int class_index, int idx)
110 {
111     EX_CALLBACKS *ip = get_and_lock(ctx, class_index);
112     EX_CALLBACK *a;
113     int toret = 0;
114     OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
115
116     if (global == NULL)
117         return 0;
118
119     ip = get_and_lock(ctx, class_index);
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;
131 err:
132     CRYPTO_THREAD_unlock(global->ex_data_lock);
133     return toret;
134 }
135
136 int CRYPTO_free_ex_index(int class_index, int idx)
137 {
138     return crypto_free_ex_index_ex(NULL, class_index, idx);
139 }
140
141 /*
142  * Register a new index.
143  */
144 int 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)
148 {
149     int toret = -1;
150     EX_CALLBACK *a;
151     EX_CALLBACKS *ip;
152     OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ctx);
153
154     if (global == NULL)
155         return -1;
156
157     ip = get_and_lock(ctx, class_index);
158     if (ip == NULL)
159         return -1;
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)) {
167             CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
168             goto err;
169         }
170     }
171
172     a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
173     if (a == NULL) {
174         CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
175         goto err;
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;
182
183     if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
184         CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX_EX, ERR_R_MALLOC_FAILURE);
185         OPENSSL_free(a);
186         goto err;
187     }
188     toret = sk_EX_CALLBACK_num(ip->meth) - 1;
189     (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
190
191  err:
192     CRYPTO_THREAD_unlock(global->ex_data_lock);
193     return toret;
194 }
195
196 int 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
204 /*
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
207  * Thread-safe by copying a class's array of "EX_CALLBACK" entries
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.
210  */
211 int crypto_new_ex_data_ex(OPENSSL_CTX *ctx, int class_index, void *obj,
212                           CRYPTO_EX_DATA *ad)
213 {
214     int mx, i;
215     void *ptr;
216     EX_CALLBACK **storage = NULL;
217     EX_CALLBACK *stack[10];
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;
223
224     ip = get_and_lock(ctx, class_index);
225     if (ip == NULL)
226         return 0;
227
228     ad->ctx = ctx;
229     ad->sk = NULL;
230
231     mx = sk_EX_CALLBACK_num(ip->meth);
232     if (mx > 0) {
233         if (mx < (int)OSSL_NELEM(stack))
234             storage = stack;
235         else
236             storage = OPENSSL_malloc(sizeof(*storage) * mx);
237         if (storage != NULL)
238             for (i = 0; i < mx; i++)
239                 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
240     }
241     CRYPTO_THREAD_unlock(global->ex_data_lock);
242
243     if (mx > 0 && storage == NULL) {
244         CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA_EX, ERR_R_MALLOC_FAILURE);
245         return 0;
246     }
247     for (i = 0; i < mx; i++) {
248         if (storage[i] != NULL && storage[i]->new_func != NULL) {
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     }
254     if (storage != stack)
255         OPENSSL_free(storage);
256     return 1;
257 }
258
259 int 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
264 /*
265  * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
266  * for each index in the class used by this variable
267  */
268 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
269                        const CRYPTO_EX_DATA *from)
270 {
271     int mx, j, i;
272     void *ptr;
273     EX_CALLBACK *stack[10];
274     EX_CALLBACK **storage = NULL;
275     EX_CALLBACKS *ip;
276     int toret = 0;
277     OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(from->ctx);
278
279     if (global == NULL)
280         return 0;
281
282     to->ctx = from->ctx;
283     if (from->sk == NULL)
284         /* Nothing to copy over */
285         return 1;
286     if ((ip = get_and_lock(from->ctx, class_index)) == NULL)
287         return 0;
288
289     mx = sk_EX_CALLBACK_num(ip->meth);
290     j = sk_void_num(from->sk);
291     if (j < mx)
292         mx = j;
293     if (mx > 0) {
294         if (mx < (int)OSSL_NELEM(stack))
295             storage = stack;
296         else
297             storage = OPENSSL_malloc(sizeof(*storage) * mx);
298         if (storage != NULL)
299             for (i = 0; i < mx; i++)
300                 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
301     }
302     CRYPTO_THREAD_unlock(global->ex_data_lock);
303
304     if (mx == 0)
305         return 1;
306     if (storage == NULL) {
307         CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
308         return 0;
309     }
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)))
318         goto err;
319
320     for (i = 0; i < mx; i++) {
321         ptr = CRYPTO_get_ex_data(from, i);
322         if (storage[i] != NULL && storage[i]->dup_func != NULL)
323             if (!storage[i]->dup_func(to, from, &ptr, i,
324                                       storage[i]->argl, storage[i]->argp))
325                 goto err;
326         CRYPTO_set_ex_data(to, i, ptr);
327     }
328     toret = 1;
329  err:
330     if (storage != stack)
331         OPENSSL_free(storage);
332     return toret;
333 }
334
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  */
340 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
341 {
342     int mx, i;
343     EX_CALLBACKS *ip;
344     void *ptr;
345     EX_CALLBACK *f;
346     EX_CALLBACK *stack[10];
347     EX_CALLBACK **storage = NULL;
348     OSSL_EX_DATA_GLOBAL *global;
349
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)
354         goto err;
355
356     mx = sk_EX_CALLBACK_num(ip->meth);
357     if (mx > 0) {
358         if (mx < (int)OSSL_NELEM(stack))
359             storage = stack;
360         else
361             storage = OPENSSL_malloc(sizeof(*storage) * mx);
362         if (storage != NULL)
363             for (i = 0; i < mx; i++)
364                 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
365     }
366     CRYPTO_THREAD_unlock(global->ex_data_lock);
367
368     for (i = 0; i < mx; i++) {
369         if (storage != NULL)
370             f = storage[i];
371         else {
372             CRYPTO_THREAD_write_lock(global->ex_data_lock);
373             f = sk_EX_CALLBACK_value(ip->meth, i);
374             CRYPTO_THREAD_unlock(global->ex_data_lock);
375         }
376         if (f != NULL && f->free_func != NULL) {
377             ptr = CRYPTO_get_ex_data(ad, i);
378             f->free_func(obj, ptr, ad, i, f->argl, f->argp);
379         }
380     }
381
382     if (storage != stack)
383         OPENSSL_free(storage);
384  err:
385     sk_void_free(ad->sk);
386     ad->sk = NULL;
387     ad->ctx = NULL;
388 }
389
390 /*
391  * Allocate a given CRYPTO_EX_DATA item using the class specific allocation
392  * function
393  */
394 int 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;
400     OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ad->ctx);
401
402     if (global == NULL)
403         return 0;
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
411     ip = get_and_lock(ad->ctx, class_index);
412     if (ip == NULL)
413         return 0;
414     f = sk_EX_CALLBACK_value(ip->meth, idx);
415     CRYPTO_THREAD_unlock(global->ex_data_lock);
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      */
421     if (f->new_func == NULL)
422         return 0;
423
424     f->new_func(obj, NULL, ad, idx, f->argl, f->argp);
425
426     return 1;
427 }
428
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  */
433 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
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);
440             return 0;
441         }
442     }
443
444     for (i = sk_void_num(ad->sk); i <= idx; ++i) {
445         if (!sk_void_push(ad->sk, NULL)) {
446             CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
447             return 0;
448         }
449     }
450     sk_void_set(ad->sk, idx, val);
451     return 1;
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  */
458 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
459 {
460     if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
461         return NULL;
462     return sk_void_value(ad->sk, idx);
463 }
464
465 OPENSSL_CTX *crypto_ex_data_get_openssl_ctx(const CRYPTO_EX_DATA *ad)
466 {
467     return ad->ctx;
468 }