Convert RSA blinding to new multi-threading API
[openssl.git] / crypto / ex_data.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57 /* ====================================================================
58  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  *    notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  *    notice, this list of conditions and the following disclaimer in
69  *    the documentation and/or other materials provided with the
70  *    distribution.
71  *
72  * 3. All advertising materials mentioning features or use of this
73  *    software must display the following acknowledgment:
74  *    "This product includes software developed by the OpenSSL Project
75  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76  *
77  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78  *    endorse or promote products derived from this software without
79  *    prior written permission. For written permission, please contact
80  *    openssl-core@openssl.org.
81  *
82  * 5. Products derived from this software may not be called "OpenSSL"
83  *    nor may "OpenSSL" appear in their names without prior written
84  *    permission of the OpenSSL Project.
85  *
86  * 6. Redistributions of any form whatsoever must retain the following
87  *    acknowledgment:
88  *    "This product includes software developed by the OpenSSL Project
89  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90  *
91  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
95  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102  * OF THE POSSIBILITY OF SUCH DAMAGE.
103  * ====================================================================
104  *
105  * This product includes cryptographic software written by Eric Young
106  * (eay@cryptsoft.com).  This product includes software written by Tim
107  * Hudson (tjh@cryptsoft.com).
108  *
109  */
110
111 #include "internal/cryptlib.h"
112 #include "internal/threads.h"
113 #include <openssl/lhash.h>
114
115 /*
116  * Each structure type (sometimes called a class), that supports
117  * exdata has a stack of callbacks for each instance.
118  */
119 struct ex_callback_st {
120     long argl;                  /* Arbitrary long */
121     void *argp;                 /* Arbitrary void * */
122     CRYPTO_EX_new *new_func;
123     CRYPTO_EX_free *free_func;
124     CRYPTO_EX_dup *dup_func;
125 };
126
127 /*
128  * The state for each class.  This could just be a typedef, but
129  * a structure allows future changes.
130  */
131 typedef struct ex_callbacks_st {
132     STACK_OF(EX_CALLBACK) *meth;
133 } EX_CALLBACKS;
134
135 static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
136
137 static CRYPTO_RWLOCK *ex_data_lock;
138 static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT;
139
140 static void do_ex_data_init(void)
141 {
142     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
143     ex_data_lock = CRYPTO_THREAD_lock_new();
144     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
145 }
146
147 /*
148  * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
149  * a given class.  On success, *holds the lock.*
150  */
151 static EX_CALLBACKS *get_and_lock(int class_index)
152 {
153     EX_CALLBACKS *ip;
154
155     if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
156         CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
157         return NULL;
158     }
159
160     CRYPTO_THREAD_run_once(&ex_data_init, do_ex_data_init);
161
162     ip = &ex_data[class_index];
163     CRYPTO_THREAD_write_lock(ex_data_lock);
164     if (ip->meth == NULL) {
165         ip->meth = sk_EX_CALLBACK_new_null();
166         /* We push an initial value on the stack because the SSL
167          * "app_data" routines use ex_data index zero.  See RT 3710. */
168         if (ip->meth == NULL
169             || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
170             CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
171             CRYPTO_THREAD_unlock(ex_data_lock);
172             return NULL;
173         }
174     }
175     return ip;
176 }
177
178 static void cleanup_cb(EX_CALLBACK *funcs)
179 {
180     OPENSSL_free(funcs);
181 }
182
183 /*
184  * Release all "ex_data" state to prevent memory leaks. This can't be made
185  * thread-safe without overhauling a lot of stuff, and shouldn't really be
186  * called under potential race-conditions anyway (it's for program shutdown
187  * after all).
188  */
189 void CRYPTO_cleanup_all_ex_data(void)
190 {
191     int i;
192
193     for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
194         EX_CALLBACKS *ip = &ex_data[i];
195
196         sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
197         ip->meth = NULL;
198     }
199 }
200
201
202 /*
203  * Unregister a new index by replacing the callbacks with no-ops.
204  * Any in-use instances are leaked.
205  */
206 static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
207                      long argl, void *argp)
208 {
209 }
210
211 static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
212                        long argl, void *argp)
213 {
214 }
215
216 static int dummy_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from,
217                      void *from_d, int idx,
218                      long argl, void *argp)
219 {
220     return 0;
221 }
222
223 int CRYPTO_free_ex_index(int class_index, int idx)
224 {
225     EX_CALLBACKS *ip = get_and_lock(class_index);
226     EX_CALLBACK *a;
227     int toret = 0;
228
229     if (ip == NULL)
230         return 0;
231     if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
232         goto err;
233     a = sk_EX_CALLBACK_value(ip->meth, idx);
234     if (a == NULL)
235         goto err;
236     a->new_func = dummy_new;
237     a->dup_func = dummy_dup;
238     a->free_func = dummy_free;
239     toret = 1;
240 err:
241     CRYPTO_THREAD_unlock(ex_data_lock);
242     return toret;
243 }
244
245 /*
246  * Register a new index.
247  */
248 int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
249                             CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
250                             CRYPTO_EX_free *free_func)
251 {
252     int toret = -1;
253     EX_CALLBACK *a;
254     EX_CALLBACKS *ip = get_and_lock(class_index);
255
256     if (ip == NULL)
257         return -1;
258     a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
259     if (a == NULL) {
260         CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
261         goto err;
262     }
263     a->argl = argl;
264     a->argp = argp;
265     a->new_func = new_func;
266     a->dup_func = dup_func;
267     a->free_func = free_func;
268
269     if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
270         CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
271         OPENSSL_free(a);
272         goto err;
273     }
274     toret = sk_EX_CALLBACK_num(ip->meth) - 1;
275     (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
276
277  err:
278     CRYPTO_THREAD_unlock(ex_data_lock);
279     return toret;
280 }
281
282 /*
283  * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
284  * calling new() callbacks for each index in the class used by this variable
285  * Thread-safe by copying a class's array of "EX_CALLBACK" entries
286  * in the lock, then using them outside the lock. Note this only applies
287  * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
288  */
289 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
290 {
291     int mx, i;
292     void *ptr;
293     EX_CALLBACK **storage = NULL;
294     EX_CALLBACK *stack[10];
295     EX_CALLBACKS *ip = get_and_lock(class_index);
296
297     if (ip == NULL)
298         return 0;
299
300     ad->sk = NULL;
301
302     mx = sk_EX_CALLBACK_num(ip->meth);
303     if (mx > 0) {
304         if (mx < (int)OSSL_NELEM(stack))
305             storage = stack;
306         else
307             storage = OPENSSL_malloc(sizeof(*storage) * mx);
308         if (storage != NULL)
309             for (i = 0; i < mx; i++)
310                 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
311     }
312     CRYPTO_THREAD_unlock(ex_data_lock);
313
314     if (mx > 0 && storage == NULL) {
315         CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
316         return 0;
317     }
318     for (i = 0; i < mx; i++) {
319         if (storage[i] && storage[i]->new_func) {
320             ptr = CRYPTO_get_ex_data(ad, i);
321             storage[i]->new_func(obj, ptr, ad, i,
322                                  storage[i]->argl, storage[i]->argp);
323         }
324     }
325     if (storage != stack)
326         OPENSSL_free(storage);
327     return 1;
328 }
329
330 /*
331  * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
332  * for each index in the class used by this variable
333  */
334 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
335                        CRYPTO_EX_DATA *from)
336 {
337     int mx, j, i;
338     char *ptr;
339     EX_CALLBACK *stack[10];
340     EX_CALLBACK **storage = NULL;
341     EX_CALLBACKS *ip;
342
343     if (from->sk == NULL)
344         /* Nothing to copy over */
345         return 1;
346     if ((ip = get_and_lock(class_index)) == NULL)
347         return 0;
348
349     mx = sk_EX_CALLBACK_num(ip->meth);
350     j = sk_void_num(from->sk);
351     if (j < mx)
352         mx = j;
353     if (mx > 0) {
354         if (mx < (int)OSSL_NELEM(stack))
355             storage = stack;
356         else
357             storage = OPENSSL_malloc(sizeof(*storage) * mx);
358         if (storage != NULL)
359             for (i = 0; i < mx; i++)
360                 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
361     }
362     CRYPTO_THREAD_unlock(ex_data_lock);
363
364     if (mx > 0 && storage == NULL) {
365         CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
366         return 0;
367     }
368
369     for (i = 0; i < mx; i++) {
370         ptr = CRYPTO_get_ex_data(from, i);
371         if (storage[i] && storage[i]->dup_func)
372             storage[i]->dup_func(to, from, &ptr, i,
373                                  storage[i]->argl, storage[i]->argp);
374         CRYPTO_set_ex_data(to, i, ptr);
375     }
376     if (storage != stack)
377         OPENSSL_free(storage);
378     return 1;
379 }
380
381
382 /*
383  * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
384  * each index in the class used by this variable
385  */
386 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
387 {
388     int mx, i;
389     EX_CALLBACKS *ip;
390     void *ptr;
391     EX_CALLBACK *stack[10];
392     EX_CALLBACK **storage = NULL;
393
394     if ((ip = get_and_lock(class_index)) == NULL)
395         return;
396
397     mx = sk_EX_CALLBACK_num(ip->meth);
398     if (mx > 0) {
399         if (mx < (int)OSSL_NELEM(stack))
400             storage = stack;
401         else
402             storage = OPENSSL_malloc(sizeof(*storage) * mx);
403         if (storage != NULL)
404             for (i = 0; i < mx; i++)
405                 storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
406     }
407     CRYPTO_THREAD_unlock(ex_data_lock);
408
409     if (mx > 0 && storage == NULL) {
410         CRYPTOerr(CRYPTO_F_CRYPTO_FREE_EX_DATA, ERR_R_MALLOC_FAILURE);
411         return;
412     }
413     for (i = 0; i < mx; i++) {
414         if (storage[i] && storage[i]->free_func) {
415             ptr = CRYPTO_get_ex_data(ad, i);
416             storage[i]->free_func(obj, ptr, ad, i,
417                                   storage[i]->argl, storage[i]->argp);
418         }
419     }
420
421     if (storage != stack)
422         OPENSSL_free(storage);
423     sk_void_free(ad->sk);
424     ad->sk = NULL;
425 }
426
427 /*
428  * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
429  * particular index in the class used by this variable
430  */
431 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
432 {
433     int i;
434
435     if (ad->sk == NULL) {
436         if ((ad->sk = sk_void_new_null()) == NULL) {
437             CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
438             return 0;
439         }
440     }
441
442     for (i = sk_void_num(ad->sk); i <= idx; ++i) {
443         if (!sk_void_push(ad->sk, NULL)) {
444             CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
445             return 0;
446         }
447     }
448     sk_void_set(ad->sk, idx, val);
449     return 1;
450 }
451
452 /*
453  * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
454  * particular index in the class used by this variable
455  */
456 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
457 {
458     if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
459         return NULL;
460     return sk_void_value(ad->sk, idx);
461 }