ex_data.c 21 KB

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