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