Constify X509|X509_CRL|X509_REVOKED_get_ext
[openssl.git] / crypto / ex_data.c
1 /*
2  * Copyright 1995-2016 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 #include <openssl/lhash.h>
13
14 /*
15  * Each structure type (sometimes called a class), that supports
16  * exdata has a stack of callbacks for each instance.
17  */
18 struct ex_callback_st {
19     long argl;                  /* Arbitrary long */
20     void *argp;                 /* Arbitrary void * */
21     CRYPTO_EX_new *new_func;
22     CRYPTO_EX_free *free_func;
23     CRYPTO_EX_dup *dup_func;
24 };
25
26 /*
27  * The state for each class.  This could just be a typedef, but
28  * a structure allows future changes.
29  */
30 typedef struct ex_callbacks_st {
31     STACK_OF(EX_CALLBACK) *meth;
32 } EX_CALLBACKS;
33
34 static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
35
36 static CRYPTO_RWLOCK *ex_data_lock = NULL;
37 static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT;
38
39 DEFINE_RUN_ONCE_STATIC(do_ex_data_init)
40 {
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, CRYPTO_EX_DATA *from,
123                      void *from_d, int idx,
124                      long argl, void *argp)
125 {
126     return 0;
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                        CRYPTO_EX_DATA *from)
254 {
255     int mx, j, i;
256     char *ptr;
257     EX_CALLBACK *stack[10];
258     EX_CALLBACK **storage = NULL;
259     EX_CALLBACKS *ip;
260
261     if (from->sk == NULL)
262         /* Nothing to copy over */
263         return 1;
264     if ((ip = get_and_lock(class_index)) == NULL)
265         return 0;
266
267     mx = sk_EX_CALLBACK_num(ip->meth);
268     j = sk_void_num(from->sk);
269     if (j < mx)
270         mx = j;
271     if (mx > 0) {
272         if (mx < (int)OSSL_NELEM(stack))
273             storage = stack;
274         else
275             storage = OPENSSL_malloc(sizeof(*storage) * mx);
276         if (storage != NULL)
277             for (i = 0; i < mx; i++)
278                 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
279     }
280     CRYPTO_THREAD_unlock(ex_data_lock);
281
282     if (mx > 0 && storage == NULL) {
283         CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
284         return 0;
285     }
286
287     for (i = 0; i < mx; i++) {
288         ptr = CRYPTO_get_ex_data(from, i);
289         if (storage[i] && storage[i]->dup_func)
290             storage[i]->dup_func(to, from, &ptr, i,
291                                  storage[i]->argl, storage[i]->argp);
292         CRYPTO_set_ex_data(to, i, ptr);
293     }
294     if (storage != stack)
295         OPENSSL_free(storage);
296     return 1;
297 }
298
299
300 /*
301  * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
302  * each index in the class used by this variable
303  */
304 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
305 {
306     int mx, i;
307     EX_CALLBACKS *ip;
308     void *ptr;
309     EX_CALLBACK *stack[10];
310     EX_CALLBACK **storage = NULL;
311
312     if ((ip = get_and_lock(class_index)) == NULL)
313         return;
314
315     mx = sk_EX_CALLBACK_num(ip->meth);
316     if (mx > 0) {
317         if (mx < (int)OSSL_NELEM(stack))
318             storage = stack;
319         else
320             storage = OPENSSL_malloc(sizeof(*storage) * mx);
321         if (storage != NULL)
322             for (i = 0; i < mx; i++)
323                 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
324     }
325     CRYPTO_THREAD_unlock(ex_data_lock);
326
327     if (mx > 0 && storage == NULL) {
328         CRYPTOerr(CRYPTO_F_CRYPTO_FREE_EX_DATA, ERR_R_MALLOC_FAILURE);
329         return;
330     }
331     for (i = 0; i < mx; i++) {
332         if (storage[i] && storage[i]->free_func) {
333             ptr = CRYPTO_get_ex_data(ad, i);
334             storage[i]->free_func(obj, ptr, ad, i,
335                                   storage[i]->argl, storage[i]->argp);
336         }
337     }
338
339     if (storage != stack)
340         OPENSSL_free(storage);
341     sk_void_free(ad->sk);
342     ad->sk = NULL;
343 }
344
345 /*
346  * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
347  * particular index in the class used by this variable
348  */
349 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
350 {
351     int i;
352
353     if (ad->sk == NULL) {
354         if ((ad->sk = sk_void_new_null()) == NULL) {
355             CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
356             return 0;
357         }
358     }
359
360     for (i = sk_void_num(ad->sk); i <= idx; ++i) {
361         if (!sk_void_push(ad->sk, NULL)) {
362             CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
363             return 0;
364         }
365     }
366     sk_void_set(ad->sk, idx, val);
367     return 1;
368 }
369
370 /*
371  * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
372  * particular index in the class used by this variable
373  */
374 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
375 {
376     if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
377         return NULL;
378     return sk_void_value(ad->sk, idx);
379 }