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