diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/ex_data.c | 646 |
1 files changed, 523 insertions, 123 deletions
diff --git a/src/lib/libcrypto/ex_data.c b/src/lib/libcrypto/ex_data.c index c858b518ff..5b2e345c27 100644 --- a/src/lib/libcrypto/ex_data.c +++ b/src/lib/libcrypto/ex_data.c | |||
| @@ -1,4 +1,33 @@ | |||
| 1 | /* crypto/ex_data.c */ | 1 | /* crypto/ex_data.c */ |
| 2 | |||
| 3 | /* | ||
| 4 | * Overhaul notes; | ||
| 5 | * | ||
| 6 | * This code is now *mostly* thread-safe. It is now easier to understand in what | ||
| 7 | * ways it is safe and in what ways it is not, which is an improvement. Firstly, | ||
| 8 | * all per-class stacks and index-counters for ex_data are stored in the same | ||
| 9 | * global LHASH table (keyed by class). This hash table uses locking for all | ||
| 10 | * access with the exception of CRYPTO_cleanup_all_ex_data(), which must only be | ||
| 11 | * called when no other threads can possibly race against it (even if it was | ||
| 12 | * locked, the race would mean it's possible the hash table might have been | ||
| 13 | * recreated after the cleanup). As classes can only be added to the hash table, | ||
| 14 | * and within each class, the stack of methods can only be incremented, the | ||
| 15 | * locking mechanics are simpler than they would otherwise be. For example, the | ||
| 16 | * new/dup/free ex_data functions will lock the hash table, copy the method | ||
| 17 | * pointers it needs from the relevant class, then unlock the hash table before | ||
| 18 | * actually applying those method pointers to the task of the new/dup/free | ||
| 19 | * operations. As they can't be removed from the method-stack, only | ||
| 20 | * supplemented, there's no race conditions associated with using them outside | ||
| 21 | * the lock. The get/set_ex_data functions are not locked because they do not | ||
| 22 | * involve this global state at all - they operate directly with a previously | ||
| 23 | * obtained per-class method index and a particular "ex_data" variable. These | ||
| 24 | * variables are usually instantiated per-context (eg. each RSA structure has | ||
| 25 | * one) so locking on read/write access to that variable can be locked locally | ||
| 26 | * if required (eg. using the "RSA" lock to synchronise access to a | ||
| 27 | * per-RSA-structure ex_data variable if required). | ||
| 28 | * [Geoff] | ||
| 29 | */ | ||
| 30 | |||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 31 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 32 | * All rights reserved. |
| 4 | * | 33 | * |
| @@ -55,182 +84,553 @@ | |||
| 55 | * copied and put under another distribution licence | 84 | * copied and put under another distribution licence |
| 56 | * [including the GNU Public Licence.] | 85 | * [including the GNU Public Licence.] |
| 57 | */ | 86 | */ |
| 87 | /* ==================================================================== | ||
| 88 | * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. | ||
| 89 | * | ||
| 90 | * Redistribution and use in source and binary forms, with or without | ||
| 91 | * modification, are permitted provided that the following conditions | ||
| 92 | * are met: | ||
| 93 | * | ||
| 94 | * 1. Redistributions of source code must retain the above copyright | ||
| 95 | * notice, this list of conditions and the following disclaimer. | ||
| 96 | * | ||
| 97 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 98 | * notice, this list of conditions and the following disclaimer in | ||
| 99 | * the documentation and/or other materials provided with the | ||
| 100 | * distribution. | ||
| 101 | * | ||
| 102 | * 3. All advertising materials mentioning features or use of this | ||
| 103 | * software must display the following acknowledgment: | ||
| 104 | * "This product includes software developed by the OpenSSL Project | ||
| 105 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
| 106 | * | ||
| 107 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 108 | * endorse or promote products derived from this software without | ||
| 109 | * prior written permission. For written permission, please contact | ||
| 110 | * openssl-core@openssl.org. | ||
| 111 | * | ||
| 112 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 113 | * nor may "OpenSSL" appear in their names without prior written | ||
| 114 | * permission of the OpenSSL Project. | ||
| 115 | * | ||
| 116 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 117 | * acknowledgment: | ||
| 118 | * "This product includes software developed by the OpenSSL Project | ||
| 119 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
| 120 | * | ||
| 121 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 122 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 123 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 124 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 125 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 126 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 127 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 128 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 129 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 130 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 131 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 132 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 133 | * ==================================================================== | ||
| 134 | * | ||
| 135 | * This product includes cryptographic software written by Eric Young | ||
| 136 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 137 | * Hudson (tjh@cryptsoft.com). | ||
| 138 | * | ||
| 139 | */ | ||
| 58 | 140 | ||
| 59 | #include <stdio.h> | 141 | #include <stdio.h> |
| 60 | #include <stdlib.h> | 142 | #include <stdlib.h> |
| 61 | #include "buffer.h" | 143 | #include <openssl/buffer.h> |
| 62 | #include "bio.h" | 144 | #include <openssl/bio.h> |
| 63 | #include "lhash.h" | 145 | #include <openssl/lhash.h> |
| 64 | #include "cryptlib.h" | 146 | #include "cryptlib.h" |
| 65 | 147 | ||
| 66 | int CRYPTO_get_ex_new_index(idx,skp,argl,argp,new_func,dup_func,free_func) | 148 | /* What an "implementation of ex_data functionality" looks like */ |
| 67 | int idx; | 149 | struct st_CRYPTO_EX_DATA_IMPL |
| 68 | STACK **skp; | 150 | { |
| 69 | long argl; | 151 | /*********************/ |
| 70 | char *argp; | 152 | /* GLOBAL OPERATIONS */ |
| 71 | int (*new_func)(); | 153 | /* Return a new class index */ |
| 72 | int (*dup_func)(); | 154 | int (*cb_new_class)(void); |
| 73 | void (*free_func)(); | 155 | /* Cleanup all state used by the implementation */ |
| 156 | void (*cb_cleanup)(void); | ||
| 157 | /************************/ | ||
| 158 | /* PER-CLASS OPERATIONS */ | ||
| 159 | /* Get a new method index within a class */ | ||
| 160 | int (*cb_get_new_index)(int class_index, long argl, void *argp, | ||
| 161 | CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, | ||
| 162 | CRYPTO_EX_free *free_func); | ||
| 163 | /* Initialise a new CRYPTO_EX_DATA of a given class */ | ||
| 164 | int (*cb_new_ex_data)(int class_index, void *obj, | ||
| 165 | CRYPTO_EX_DATA *ad); | ||
| 166 | /* Duplicate a CRYPTO_EX_DATA of a given class onto a copy */ | ||
| 167 | int (*cb_dup_ex_data)(int class_index, CRYPTO_EX_DATA *to, | ||
| 168 | CRYPTO_EX_DATA *from); | ||
| 169 | /* Cleanup a CRYPTO_EX_DATA of a given class */ | ||
| 170 | void (*cb_free_ex_data)(int class_index, void *obj, | ||
| 171 | CRYPTO_EX_DATA *ad); | ||
| 172 | }; | ||
| 173 | |||
| 174 | /* The implementation we use at run-time */ | ||
| 175 | static const CRYPTO_EX_DATA_IMPL *impl = NULL; | ||
| 176 | |||
| 177 | /* To call "impl" functions, use this macro rather than referring to 'impl' directly, eg. | ||
| 178 | * EX_IMPL(get_new_index)(...); */ | ||
| 179 | #define EX_IMPL(a) impl->cb_##a | ||
| 180 | |||
| 181 | /* Predeclare the "default" ex_data implementation */ | ||
| 182 | static int int_new_class(void); | ||
| 183 | static void int_cleanup(void); | ||
| 184 | static int int_get_new_index(int class_index, long argl, void *argp, | ||
| 185 | CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, | ||
| 186 | CRYPTO_EX_free *free_func); | ||
| 187 | static int int_new_ex_data(int class_index, void *obj, | ||
| 188 | CRYPTO_EX_DATA *ad); | ||
| 189 | static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, | ||
| 190 | CRYPTO_EX_DATA *from); | ||
| 191 | static void int_free_ex_data(int class_index, void *obj, | ||
| 192 | CRYPTO_EX_DATA *ad); | ||
| 193 | static CRYPTO_EX_DATA_IMPL impl_default = | ||
| 194 | { | ||
| 195 | int_new_class, | ||
| 196 | int_cleanup, | ||
| 197 | int_get_new_index, | ||
| 198 | int_new_ex_data, | ||
| 199 | int_dup_ex_data, | ||
| 200 | int_free_ex_data | ||
| 201 | }; | ||
| 202 | |||
| 203 | /* Internal function that checks whether "impl" is set and if not, sets it to | ||
| 204 | * the default. */ | ||
| 205 | static void impl_check(void) | ||
| 206 | { | ||
| 207 | CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); | ||
| 208 | if(!impl) | ||
| 209 | impl = &impl_default; | ||
| 210 | CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); | ||
| 211 | } | ||
| 212 | /* A macro wrapper for impl_check that first uses a non-locked test before | ||
| 213 | * invoking the function (which checks again inside a lock). */ | ||
| 214 | #define IMPL_CHECK if(!impl) impl_check(); | ||
| 215 | |||
| 216 | /* API functions to get/set the "ex_data" implementation */ | ||
| 217 | const CRYPTO_EX_DATA_IMPL *CRYPTO_get_ex_data_implementation(void) | ||
| 218 | { | ||
| 219 | IMPL_CHECK | ||
| 220 | return impl; | ||
| 221 | } | ||
| 222 | int CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i) | ||
| 223 | { | ||
| 224 | int toret = 0; | ||
| 225 | CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); | ||
| 226 | if(!impl) | ||
| 227 | { | ||
| 228 | impl = i; | ||
| 229 | toret = 1; | ||
| 230 | } | ||
| 231 | CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); | ||
| 232 | return toret; | ||
| 233 | } | ||
| 234 | |||
| 235 | /****************************************************************************/ | ||
| 236 | /* Interal (default) implementation of "ex_data" support. API functions are | ||
| 237 | * further down. */ | ||
| 238 | |||
| 239 | /* The type that represents what each "class" used to implement locally. A STACK | ||
| 240 | * of CRYPTO_EX_DATA_FUNCS plus a index-counter. The 'class_index' is the global | ||
| 241 | * value representing the class that is used to distinguish these items. */ | ||
| 242 | typedef struct st_ex_class_item { | ||
| 243 | int class_index; | ||
| 244 | STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth; | ||
| 245 | int meth_num; | ||
| 246 | } EX_CLASS_ITEM; | ||
| 247 | |||
| 248 | /* When assigning new class indexes, this is our counter */ | ||
| 249 | static int ex_class = CRYPTO_EX_INDEX_USER; | ||
| 250 | |||
| 251 | /* The global hash table of EX_CLASS_ITEM items */ | ||
| 252 | static LHASH *ex_data = NULL; | ||
| 253 | |||
| 254 | /* The callbacks required in the "ex_data" hash table */ | ||
| 255 | static unsigned long ex_hash_cb(const void *a_void) | ||
| 256 | { | ||
| 257 | return ((const EX_CLASS_ITEM *)a_void)->class_index; | ||
| 258 | } | ||
| 259 | static int ex_cmp_cb(const void *a_void, const void *b_void) | ||
| 260 | { | ||
| 261 | return (((const EX_CLASS_ITEM *)a_void)->class_index - | ||
| 262 | ((const EX_CLASS_ITEM *)b_void)->class_index); | ||
| 263 | } | ||
| 264 | |||
| 265 | /* Internal functions used by the "impl_default" implementation to access the | ||
| 266 | * state */ | ||
| 267 | |||
| 268 | static int ex_data_check(void) | ||
| 269 | { | ||
| 270 | int toret = 1; | ||
| 271 | CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); | ||
| 272 | if(!ex_data && ((ex_data = lh_new(ex_hash_cb, ex_cmp_cb)) == NULL)) | ||
| 273 | toret = 0; | ||
| 274 | CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); | ||
| 275 | return toret; | ||
| 276 | } | ||
| 277 | /* This macros helps reduce the locking from repeated checks because the | ||
| 278 | * ex_data_check() function checks ex_data again inside a lock. */ | ||
| 279 | #define EX_DATA_CHECK(iffail) if(!ex_data && !ex_data_check()) {iffail} | ||
| 280 | |||
| 281 | /* This "inner" callback is used by the callback function that follows it */ | ||
| 282 | static void def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS *funcs) | ||
| 283 | { | ||
| 284 | OPENSSL_free(funcs); | ||
| 285 | } | ||
| 286 | |||
| 287 | /* This callback is used in lh_doall to destroy all EX_CLASS_ITEM values from | ||
| 288 | * "ex_data" prior to the ex_data hash table being itself destroyed. Doesn't do | ||
| 289 | * any locking. */ | ||
| 290 | static void def_cleanup_cb(const void *a_void) | ||
| 74 | { | 291 | { |
| 75 | CRYPTO_EX_DATA_FUNCS *a; | 292 | EX_CLASS_ITEM *item = (EX_CLASS_ITEM *)a_void; |
| 293 | sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, def_cleanup_util_cb); | ||
| 294 | OPENSSL_free(item); | ||
| 295 | } | ||
| 76 | 296 | ||
| 77 | if (*skp == NULL) | 297 | /* Return the EX_CLASS_ITEM from the "ex_data" hash table that corresponds to a |
| 78 | *skp=sk_new_null(); | 298 | * given class. Handles locking. */ |
| 79 | if (*skp == NULL) | 299 | static EX_CLASS_ITEM *def_get_class(int class_index) |
| 300 | { | ||
| 301 | EX_CLASS_ITEM d, *p, *gen; | ||
| 302 | EX_DATA_CHECK(return NULL;) | ||
| 303 | d.class_index = class_index; | ||
| 304 | CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); | ||
| 305 | p = lh_retrieve(ex_data, &d); | ||
| 306 | if(!p) | ||
| 80 | { | 307 | { |
| 81 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE); | 308 | gen = OPENSSL_malloc(sizeof(EX_CLASS_ITEM)); |
| 82 | return(-1); | 309 | if(gen) |
| 310 | { | ||
| 311 | gen->class_index = class_index; | ||
| 312 | gen->meth_num = 0; | ||
| 313 | gen->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null(); | ||
| 314 | if(!gen->meth) | ||
| 315 | OPENSSL_free(gen); | ||
| 316 | else | ||
| 317 | { | ||
| 318 | /* Because we're inside the ex_data lock, the | ||
| 319 | * return value from the insert will be NULL */ | ||
| 320 | lh_insert(ex_data, gen); | ||
| 321 | p = gen; | ||
| 322 | } | ||
| 323 | } | ||
| 83 | } | 324 | } |
| 84 | a=(CRYPTO_EX_DATA_FUNCS *)Malloc(sizeof(CRYPTO_EX_DATA_FUNCS)); | 325 | CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); |
| 85 | if (a == NULL) | 326 | if(!p) |
| 327 | CRYPTOerr(CRYPTO_F_DEF_GET_CLASS,ERR_R_MALLOC_FAILURE); | ||
| 328 | return p; | ||
| 329 | } | ||
| 330 | |||
| 331 | /* Add a new method to the given EX_CLASS_ITEM and return the corresponding | ||
| 332 | * index (or -1 for error). Handles locking. */ | ||
| 333 | static int def_add_index(EX_CLASS_ITEM *item, long argl, void *argp, | ||
| 334 | CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, | ||
| 335 | CRYPTO_EX_free *free_func) | ||
| 336 | { | ||
| 337 | int toret = -1; | ||
| 338 | CRYPTO_EX_DATA_FUNCS *a = (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc( | ||
| 339 | sizeof(CRYPTO_EX_DATA_FUNCS)); | ||
| 340 | if(!a) | ||
| 86 | { | 341 | { |
| 87 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE); | 342 | CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX,ERR_R_MALLOC_FAILURE); |
| 88 | return(-1); | 343 | return -1; |
| 89 | } | 344 | } |
| 90 | a->argl=argl; | 345 | a->argl=argl; |
| 91 | a->argp=argp; | 346 | a->argp=argp; |
| 92 | a->new_func=new_func; | 347 | a->new_func=new_func; |
| 93 | a->dup_func=dup_func; | 348 | a->dup_func=dup_func; |
| 94 | a->free_func=free_func; | 349 | a->free_func=free_func; |
| 95 | while (sk_num(*skp) <= idx) | 350 | CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); |
| 351 | while (sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) <= item->meth_num) | ||
| 96 | { | 352 | { |
| 97 | if (!sk_push(*skp,NULL)) | 353 | if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, NULL)) |
| 98 | { | 354 | { |
| 99 | CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE); | 355 | CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX,ERR_R_MALLOC_FAILURE); |
| 100 | Free(a); | 356 | OPENSSL_free(a); |
| 101 | return(-1); | 357 | goto err; |
| 102 | } | 358 | } |
| 103 | } | 359 | } |
| 104 | sk_value(*skp,idx)=(char *)a; | 360 | toret = item->meth_num++; |
| 105 | return(idx); | 361 | sk_CRYPTO_EX_DATA_FUNCS_set(item->meth, toret, a); |
| 362 | err: | ||
| 363 | CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); | ||
| 364 | return toret; | ||
| 106 | } | 365 | } |
| 107 | 366 | ||
| 108 | int CRYPTO_set_ex_data(ad,idx,val) | 367 | /**************************************************************/ |
| 109 | CRYPTO_EX_DATA *ad; | 368 | /* The functions in the default CRYPTO_EX_DATA_IMPL structure */ |
| 110 | int idx; | ||
| 111 | char *val; | ||
| 112 | { | ||
| 113 | int i; | ||
| 114 | 369 | ||
| 115 | if (ad->sk == NULL) | 370 | static int int_new_class(void) |
| 116 | { | 371 | { |
| 117 | if ((ad->sk=sk_new_null()) == NULL) | 372 | int toret; |
| 118 | { | 373 | CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); |
| 119 | CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE); | 374 | toret = ex_class++; |
| 120 | return(0); | 375 | CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); |
| 121 | } | 376 | return toret; |
| 122 | } | 377 | } |
| 123 | i=sk_num(ad->sk); | ||
| 124 | 378 | ||
| 125 | while (i <= idx) | 379 | static void int_cleanup(void) |
| 126 | { | 380 | { |
| 127 | if (!sk_push(ad->sk,NULL)) | 381 | EX_DATA_CHECK(return;) |
| 128 | { | 382 | lh_doall(ex_data, def_cleanup_cb); |
| 129 | CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE); | 383 | lh_free(ex_data); |
| 130 | return(0); | 384 | ex_data = NULL; |
| 131 | } | 385 | impl = NULL; |
| 132 | i++; | ||
| 133 | } | ||
| 134 | sk_value(ad->sk,idx)=val; | ||
| 135 | return(1); | ||
| 136 | } | 386 | } |
| 137 | 387 | ||
| 138 | char *CRYPTO_get_ex_data(ad,idx) | 388 | static int int_get_new_index(int class_index, long argl, void *argp, |
| 139 | CRYPTO_EX_DATA *ad; | 389 | CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, |
| 140 | int idx; | 390 | CRYPTO_EX_free *free_func) |
| 141 | { | 391 | { |
| 142 | if (ad->sk == NULL) | 392 | EX_CLASS_ITEM *item = def_get_class(class_index); |
| 143 | return(0); | 393 | if(!item) |
| 144 | else if (idx >= sk_num(ad->sk)) | 394 | return -1; |
| 145 | return(0); | 395 | return def_add_index(item, argl, argp, new_func, dup_func, free_func); |
| 146 | else | ||
| 147 | return(sk_value(ad->sk,idx)); | ||
| 148 | } | 396 | } |
| 149 | 397 | ||
| 150 | /* The callback is called with the 'object', which is the origional data object | 398 | /* Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries in |
| 151 | * being duplicated, a pointer to the | 399 | * the lock, then using them outside the lock. NB: Thread-safety only applies to |
| 152 | * 'new' object to be inserted, the index, and the argi/argp | 400 | * the global "ex_data" state (ie. class definitions), not thread-safe on 'ad' |
| 153 | */ | 401 | * itself. */ |
| 154 | int CRYPTO_dup_ex_data(meth,to,from) | 402 | static int int_new_ex_data(int class_index, void *obj, |
| 155 | STACK *meth; | 403 | CRYPTO_EX_DATA *ad) |
| 156 | CRYPTO_EX_DATA *to,*from; | 404 | { |
| 157 | { | 405 | int mx,i; |
| 158 | int i,j,m,r; | 406 | void *ptr; |
| 159 | CRYPTO_EX_DATA_FUNCS *mm; | 407 | CRYPTO_EX_DATA_FUNCS **storage = NULL; |
| 160 | char *from_d; | 408 | EX_CLASS_ITEM *item = def_get_class(class_index); |
| 161 | 409 | if(!item) | |
| 162 | if (meth == NULL) return(1); | 410 | /* error is already set */ |
| 163 | if (from->sk == NULL) return(1); | 411 | return 0; |
| 164 | m=sk_num(meth); | 412 | ad->sk = NULL; |
| 165 | j=sk_num(from->sk); | 413 | CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA); |
| 166 | for (i=0; i<j; i++) | 414 | mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth); |
| 167 | { | 415 | if(mx > 0) |
| 168 | from_d=CRYPTO_get_ex_data(from,i); | 416 | { |
| 169 | if (i < m) | 417 | storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*)); |
| 418 | if(!storage) | ||
| 419 | goto skip; | ||
| 420 | for(i = 0; i < mx; i++) | ||
| 421 | storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i); | ||
| 422 | } | ||
| 423 | skip: | ||
| 424 | CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA); | ||
| 425 | if((mx > 0) && !storage) | ||
| 426 | { | ||
| 427 | CRYPTOerr(CRYPTO_F_INT_NEW_EX_DATA,ERR_R_MALLOC_FAILURE); | ||
| 428 | return 0; | ||
| 429 | } | ||
| 430 | for(i = 0; i < mx; i++) | ||
| 431 | { | ||
| 432 | if(storage[i] && storage[i]->new_func) | ||
| 170 | { | 433 | { |
| 171 | mm=(CRYPTO_EX_DATA_FUNCS *)sk_value(meth,i); | 434 | ptr = CRYPTO_get_ex_data(ad, i); |
| 172 | if (mm->dup_func != NULL) | 435 | storage[i]->new_func(obj,ptr,ad,i, |
| 173 | r=mm->dup_func(to,from,(char **)&from_d,i, | 436 | storage[i]->argl,storage[i]->argp); |
| 174 | mm->argl,mm->argp); | ||
| 175 | } | 437 | } |
| 176 | CRYPTO_set_ex_data(to,i,from_d); | ||
| 177 | } | 438 | } |
| 178 | return(1); | 439 | if(storage) |
| 440 | OPENSSL_free(storage); | ||
| 441 | return 1; | ||
| 179 | } | 442 | } |
| 180 | 443 | ||
| 181 | /* Call each free callback */ | 444 | /* Same thread-safety notes as for "int_new_ex_data" */ |
| 182 | void CRYPTO_free_ex_data(meth,obj,ad) | 445 | static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, |
| 183 | STACK *meth; | 446 | CRYPTO_EX_DATA *from) |
| 184 | char *obj; | ||
| 185 | CRYPTO_EX_DATA *ad; | ||
| 186 | { | 447 | { |
| 187 | CRYPTO_EX_DATA_FUNCS *m; | 448 | int mx, j, i; |
| 188 | char *ptr; | 449 | char *ptr; |
| 189 | int i,max; | 450 | CRYPTO_EX_DATA_FUNCS **storage = NULL; |
| 451 | EX_CLASS_ITEM *item; | ||
| 452 | if(!from->sk) | ||
| 453 | /* 'to' should be "blank" which *is* just like 'from' */ | ||
| 454 | return 1; | ||
| 455 | if((item = def_get_class(class_index)) == NULL) | ||
| 456 | return 0; | ||
| 457 | CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA); | ||
| 458 | mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth); | ||
| 459 | j = sk_num(from->sk); | ||
| 460 | if(j < mx) | ||
| 461 | mx = j; | ||
| 462 | if(mx > 0) | ||
| 463 | { | ||
| 464 | storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*)); | ||
| 465 | if(!storage) | ||
| 466 | goto skip; | ||
| 467 | for(i = 0; i < mx; i++) | ||
| 468 | storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i); | ||
| 469 | } | ||
| 470 | skip: | ||
| 471 | CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA); | ||
| 472 | if((mx > 0) && !storage) | ||
| 473 | { | ||
| 474 | CRYPTOerr(CRYPTO_F_INT_DUP_EX_DATA,ERR_R_MALLOC_FAILURE); | ||
| 475 | return 0; | ||
| 476 | } | ||
| 477 | for(i = 0; i < mx; i++) | ||
| 478 | { | ||
| 479 | ptr = CRYPTO_get_ex_data(from, i); | ||
| 480 | if(storage[i] && storage[i]->dup_func) | ||
| 481 | storage[i]->dup_func(to,from,&ptr,i, | ||
| 482 | storage[i]->argl,storage[i]->argp); | ||
| 483 | CRYPTO_set_ex_data(to,i,ptr); | ||
| 484 | } | ||
| 485 | if(storage) | ||
| 486 | OPENSSL_free(storage); | ||
| 487 | return 1; | ||
| 488 | } | ||
| 190 | 489 | ||
| 191 | if (meth != NULL) | 490 | /* Same thread-safety notes as for "int_new_ex_data" */ |
| 491 | static void int_free_ex_data(int class_index, void *obj, | ||
| 492 | CRYPTO_EX_DATA *ad) | ||
| 493 | { | ||
| 494 | int mx,i; | ||
| 495 | EX_CLASS_ITEM *item; | ||
| 496 | void *ptr; | ||
| 497 | CRYPTO_EX_DATA_FUNCS **storage = NULL; | ||
| 498 | if((item = def_get_class(class_index)) == NULL) | ||
| 499 | return; | ||
| 500 | CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA); | ||
| 501 | mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth); | ||
| 502 | if(mx > 0) | ||
| 503 | { | ||
| 504 | storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*)); | ||
| 505 | if(!storage) | ||
| 506 | goto skip; | ||
| 507 | for(i = 0; i < mx; i++) | ||
| 508 | storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i); | ||
| 509 | } | ||
| 510 | skip: | ||
| 511 | CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA); | ||
| 512 | if((mx > 0) && !storage) | ||
| 513 | { | ||
| 514 | CRYPTOerr(CRYPTO_F_INT_FREE_EX_DATA,ERR_R_MALLOC_FAILURE); | ||
| 515 | return; | ||
| 516 | } | ||
| 517 | for(i = 0; i < mx; i++) | ||
| 192 | { | 518 | { |
| 193 | max=sk_num(meth); | 519 | if(storage[i] && storage[i]->free_func) |
| 194 | for (i=0; i<max; i++) | ||
| 195 | { | 520 | { |
| 196 | m=(CRYPTO_EX_DATA_FUNCS *)sk_value(meth,i); | 521 | ptr = CRYPTO_get_ex_data(ad,i); |
| 197 | if ((m != NULL) && (m->free_func != NULL)) | 522 | storage[i]->free_func(obj,ptr,ad,i, |
| 198 | { | 523 | storage[i]->argl,storage[i]->argp); |
| 199 | ptr=CRYPTO_get_ex_data(ad,i); | ||
| 200 | m->free_func(obj,ptr,ad,i,m->argl,m->argp); | ||
| 201 | } | ||
| 202 | } | 524 | } |
| 203 | } | 525 | } |
| 204 | if (ad->sk != NULL) | 526 | if(storage) |
| 527 | OPENSSL_free(storage); | ||
| 528 | if(ad->sk) | ||
| 205 | { | 529 | { |
| 206 | sk_free(ad->sk); | 530 | sk_free(ad->sk); |
| 207 | ad->sk=NULL; | 531 | ad->sk=NULL; |
| 208 | } | 532 | } |
| 209 | } | 533 | } |
| 210 | 534 | ||
| 211 | void CRYPTO_new_ex_data(meth,obj,ad) | 535 | /********************************************************************/ |
| 212 | STACK *meth; | 536 | /* API functions that defer all "state" operations to the "ex_data" |
| 213 | char *obj; | 537 | * implementation we have set. */ |
| 214 | CRYPTO_EX_DATA *ad; | 538 | |
| 539 | /* Obtain an index for a new class (not the same as getting a new index within | ||
| 540 | * an existing class - this is actually getting a new *class*) */ | ||
| 541 | int CRYPTO_ex_data_new_class(void) | ||
| 215 | { | 542 | { |
| 216 | CRYPTO_EX_DATA_FUNCS *m; | 543 | IMPL_CHECK |
| 217 | char *ptr; | 544 | return EX_IMPL(new_class)(); |
| 218 | int i,max; | 545 | } |
| 546 | |||
| 547 | /* Release all "ex_data" state to prevent memory leaks. This can't be made | ||
| 548 | * thread-safe without overhauling a lot of stuff, and shouldn't really be | ||
| 549 | * called under potential race-conditions anyway (it's for program shutdown | ||
| 550 | * after all). */ | ||
| 551 | void CRYPTO_cleanup_all_ex_data(void) | ||
| 552 | { | ||
| 553 | IMPL_CHECK | ||
| 554 | EX_IMPL(cleanup)(); | ||
| 555 | } | ||
| 556 | |||
| 557 | /* Inside an existing class, get/register a new index. */ | ||
| 558 | int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp, | ||
| 559 | CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, | ||
| 560 | CRYPTO_EX_free *free_func) | ||
| 561 | { | ||
| 562 | int ret = -1; | ||
| 219 | 563 | ||
| 220 | ad->sk=NULL; | 564 | IMPL_CHECK |
| 221 | if (meth != NULL) | 565 | ret = EX_IMPL(get_new_index)(class_index, |
| 566 | argl, argp, new_func, dup_func, free_func); | ||
| 567 | return ret; | ||
| 568 | } | ||
| 569 | |||
| 570 | /* Initialise a new CRYPTO_EX_DATA for use in a particular class - including | ||
| 571 | * calling new() callbacks for each index in the class used by this variable */ | ||
| 572 | int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) | ||
| 573 | { | ||
| 574 | IMPL_CHECK | ||
| 575 | return EX_IMPL(new_ex_data)(class_index, obj, ad); | ||
| 576 | } | ||
| 577 | |||
| 578 | /* Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks for | ||
| 579 | * each index in the class used by this variable */ | ||
| 580 | int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, | ||
| 581 | CRYPTO_EX_DATA *from) | ||
| 582 | { | ||
| 583 | IMPL_CHECK | ||
| 584 | return EX_IMPL(dup_ex_data)(class_index, to, from); | ||
| 585 | } | ||
| 586 | |||
| 587 | /* Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for | ||
| 588 | * each index in the class used by this variable */ | ||
| 589 | void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) | ||
| 590 | { | ||
| 591 | IMPL_CHECK | ||
| 592 | EX_IMPL(free_ex_data)(class_index, obj, ad); | ||
| 593 | } | ||
| 594 | |||
| 595 | /* For a given CRYPTO_EX_DATA variable, set the value corresponding to a | ||
| 596 | * particular index in the class used by this variable */ | ||
| 597 | int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val) | ||
| 598 | { | ||
| 599 | int i; | ||
| 600 | |||
| 601 | if (ad->sk == NULL) | ||
| 222 | { | 602 | { |
| 223 | max=sk_num(meth); | 603 | if ((ad->sk=sk_new_null()) == NULL) |
| 224 | for (i=0; i<max; i++) | ||
| 225 | { | 604 | { |
| 226 | m=(CRYPTO_EX_DATA_FUNCS *)sk_value(meth,i); | 605 | CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE); |
| 227 | if ((m != NULL) && (m->new_func != NULL)) | 606 | return(0); |
| 228 | { | ||
| 229 | ptr=CRYPTO_get_ex_data(ad,i); | ||
| 230 | m->new_func(obj,ptr,ad,i,m->argl,m->argp); | ||
| 231 | } | ||
| 232 | } | 607 | } |
| 233 | } | 608 | } |
| 609 | i=sk_num(ad->sk); | ||
| 610 | |||
| 611 | while (i <= idx) | ||
| 612 | { | ||
| 613 | if (!sk_push(ad->sk,NULL)) | ||
| 614 | { | ||
| 615 | CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE); | ||
| 616 | return(0); | ||
| 617 | } | ||
| 618 | i++; | ||
| 619 | } | ||
| 620 | sk_set(ad->sk,idx,val); | ||
| 621 | return(1); | ||
| 234 | } | 622 | } |
| 235 | 623 | ||
| 624 | /* For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a | ||
| 625 | * particular index in the class used by this variable */ | ||
| 626 | void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx) | ||
| 627 | { | ||
| 628 | if (ad->sk == NULL) | ||
| 629 | return(0); | ||
| 630 | else if (idx >= sk_num(ad->sk)) | ||
| 631 | return(0); | ||
| 632 | else | ||
| 633 | return(sk_value(ad->sk,idx)); | ||
| 634 | } | ||
| 236 | 635 | ||
| 636 | IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS) | ||
