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