]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ex_data.c
Locking issues.
[thirdparty/openssl.git] / crypto / ex_data.c
1 /* crypto/ex_data.c */
2
3 /*
4 * This is not thread-safe, nor can it be changed to become thread-safe
5 * without changing various function prototypes and using a lot of locking.
6 * Luckily, it's not really used anywhere except in ssl_verify_cert_chain
7 * via SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c), where
8 * new_func, dup_func, and free_func all are 0.
9 *
10 * Any multi-threaded application crazy enough to use ex_data for its own
11 * purposes had better make sure that SSL_get_ex_data_X509_STORE_CTX_idx
12 * is called once before multiple threads are created.
13 */
14
15 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
16 * All rights reserved.
17 *
18 * This package is an SSL implementation written
19 * by Eric Young (eay@cryptsoft.com).
20 * The implementation was written so as to conform with Netscapes SSL.
21 *
22 * This library is free for commercial and non-commercial use as long as
23 * the following conditions are aheared to. The following conditions
24 * apply to all code found in this distribution, be it the RC4, RSA,
25 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
26 * included with this distribution is covered by the same copyright terms
27 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
28 *
29 * Copyright remains Eric Young's, and as such any Copyright notices in
30 * the code are not to be removed.
31 * If this package is used in a product, Eric Young should be given attribution
32 * as the author of the parts of the library used.
33 * This can be in the form of a textual message at program startup or
34 * in documentation (online or textual) provided with the package.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * "This product includes cryptographic software written by
47 * Eric Young (eay@cryptsoft.com)"
48 * The word 'cryptographic' can be left out if the rouines from the library
49 * being used are not cryptographic related :-).
50 * 4. If you include any Windows specific code (or a derivative thereof) from
51 * the apps directory (application code) you must include an acknowledgement:
52 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
53 *
54 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * The licence and distribution terms for any publically available version or
67 * derivative of this code cannot be changed. i.e. this code cannot simply be
68 * copied and put under another distribution licence
69 * [including the GNU Public Licence.]
70 */
71
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <openssl/buffer.h>
75 #include <openssl/bio.h>
76 #include <openssl/lhash.h>
77 #include "cryptlib.h"
78
79 int CRYPTO_get_ex_new_index(int idx, STACK_OF(CRYPTO_EX_DATA_FUNCS) **skp, long argl, void *argp,
80 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
81 {
82 int ret= -1;
83 CRYPTO_EX_DATA_FUNCS *a;
84
85 MemCheck_off();
86 if (*skp == NULL)
87 *skp=sk_CRYPTO_EX_DATA_FUNCS_new_null();
88 if (*skp == NULL)
89 {
90 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE);
91 goto err;
92 }
93 a=(CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS));
94 if (a == NULL)
95 {
96 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE);
97 goto err;
98 }
99 a->argl=argl;
100 a->argp=argp;
101 a->new_func=new_func;
102 a->dup_func=dup_func;
103 a->free_func=free_func;
104 while (sk_CRYPTO_EX_DATA_FUNCS_num(*skp) <= idx)
105 {
106 if (!sk_CRYPTO_EX_DATA_FUNCS_push(*skp,NULL))
107 {
108 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE);
109 OPENSSL_free(a);
110 goto err;
111 }
112 }
113 sk_CRYPTO_EX_DATA_FUNCS_set(*skp,idx, a);
114 ret=idx;
115 err:
116 MemCheck_on();
117 return(ret);
118 }
119
120 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
121 {
122 int i;
123
124 if (ad->sk == NULL)
125 {
126 if ((ad->sk=sk_new_null()) == NULL)
127 {
128 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
129 return(0);
130 }
131 }
132 i=sk_num(ad->sk);
133
134 while (i <= idx)
135 {
136 if (!sk_push(ad->sk,NULL))
137 {
138 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
139 return(0);
140 }
141 i++;
142 }
143 sk_set(ad->sk,idx,val);
144 return(1);
145 }
146
147 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
148 {
149 if (ad->sk == NULL)
150 return(0);
151 else if (idx >= sk_num(ad->sk))
152 return(0);
153 else
154 return(sk_value(ad->sk,idx));
155 }
156
157 /* The callback is called with the 'object', which is the original data object
158 * being duplicated, a pointer to the
159 * 'new' object to be inserted, the index, and the argi/argp
160 */
161 int CRYPTO_dup_ex_data(STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth, CRYPTO_EX_DATA *to,
162 CRYPTO_EX_DATA *from)
163 {
164 int i,j,m,r;
165 CRYPTO_EX_DATA_FUNCS *mm;
166 char *from_d;
167
168 if (meth == NULL) return(1);
169 if (from->sk == NULL) return(1);
170 m=sk_CRYPTO_EX_DATA_FUNCS_num(meth);
171 j=sk_num(from->sk);
172 for (i=0; i<j; i++)
173 {
174 from_d=CRYPTO_get_ex_data(from,i);
175 if (i < m)
176 {
177 mm=sk_CRYPTO_EX_DATA_FUNCS_value(meth,i);
178 if (mm->dup_func != NULL)
179 r=mm->dup_func(to,from,(char **)&from_d,i,
180 mm->argl,mm->argp);
181 }
182 CRYPTO_set_ex_data(to,i,from_d);
183 }
184 return(1);
185 }
186
187 /* Call each free callback */
188 void CRYPTO_free_ex_data(STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth, void *obj, CRYPTO_EX_DATA *ad)
189 {
190 CRYPTO_EX_DATA_FUNCS *m;
191 void *ptr;
192 int i,max;
193
194 if (meth != NULL)
195 {
196 max=sk_CRYPTO_EX_DATA_FUNCS_num(meth);
197 for (i=0; i<max; i++)
198 {
199 m=sk_CRYPTO_EX_DATA_FUNCS_value(meth,i);
200 if ((m != NULL) && (m->free_func != NULL))
201 {
202 ptr=CRYPTO_get_ex_data(ad,i);
203 m->free_func(obj,ptr,ad,i,m->argl,m->argp);
204 }
205 }
206 }
207 if (ad->sk != NULL)
208 {
209 sk_free(ad->sk);
210 ad->sk=NULL;
211 }
212 }
213
214 void CRYPTO_new_ex_data(STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth, void *obj, CRYPTO_EX_DATA *ad)
215 {
216 CRYPTO_EX_DATA_FUNCS *m;
217 void *ptr;
218 int i,max;
219
220 ad->sk=NULL;
221 if (meth != NULL)
222 {
223 max=sk_CRYPTO_EX_DATA_FUNCS_num(meth);
224 for (i=0; i<max; i++)
225 {
226 m=sk_CRYPTO_EX_DATA_FUNCS_value(meth,i);
227 if ((m != NULL) && (m->new_func != NULL))
228 {
229 ptr=CRYPTO_get_ex_data(ad,i);
230 m->new_func(obj,ptr,ad,i,m->argl,m->argp);
231 }
232 }
233 }
234 }
235
236 IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS)