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