2
0

ex_data.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. static LHASH *ex_data = NULL;
  236. /* The callbacks required in the "ex_data" hash table */
  237. static unsigned long ex_hash_cb(const void *a_void)
  238. {
  239. return ((const EX_CLASS_ITEM *)a_void)->class_index;
  240. }
  241. static int ex_cmp_cb(const void *a_void, const void *b_void)
  242. {
  243. return (((const EX_CLASS_ITEM *)a_void)->class_index -
  244. ((const EX_CLASS_ITEM *)b_void)->class_index);
  245. }
  246. /* Internal functions used by the "impl_default" implementation to access the
  247. * state */
  248. static int ex_data_check(void)
  249. {
  250. int toret = 1;
  251. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  252. if(!ex_data && ((ex_data = lh_new(ex_hash_cb, ex_cmp_cb)) == NULL))
  253. toret = 0;
  254. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  255. return toret;
  256. }
  257. /* This macros helps reduce the locking from repeated checks because the
  258. * ex_data_check() function checks ex_data again inside a lock. */
  259. #define EX_DATA_CHECK(iffail) if(!ex_data && !ex_data_check()) {iffail}
  260. /* This "inner" callback is used by the callback function that follows it */
  261. static void def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS *funcs)
  262. {
  263. OPENSSL_free(funcs);
  264. }
  265. /* This callback is used in lh_doall to destroy all EX_CLASS_ITEM values from
  266. * "ex_data" prior to the ex_data hash table being itself destroyed. Doesn't do
  267. * any locking. */
  268. static void def_cleanup_cb(void *a_void)
  269. {
  270. EX_CLASS_ITEM *item = (EX_CLASS_ITEM *)a_void;
  271. sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, def_cleanup_util_cb);
  272. OPENSSL_free(item);
  273. }
  274. /* Return the EX_CLASS_ITEM from the "ex_data" hash table that corresponds to a
  275. * given class. Handles locking. */
  276. static EX_CLASS_ITEM *def_get_class(int class_index)
  277. {
  278. EX_CLASS_ITEM d, *p, *gen;
  279. EX_DATA_CHECK(return NULL;)
  280. d.class_index = class_index;
  281. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  282. p = lh_retrieve(ex_data, &d);
  283. if(!p)
  284. {
  285. gen = OPENSSL_malloc(sizeof(EX_CLASS_ITEM));
  286. if(gen)
  287. {
  288. gen->class_index = class_index;
  289. gen->meth_num = 0;
  290. gen->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null();
  291. if(!gen->meth)
  292. OPENSSL_free(gen);
  293. else
  294. {
  295. /* Because we're inside the ex_data lock, the
  296. * return value from the insert will be NULL */
  297. lh_insert(ex_data, gen);
  298. p = gen;
  299. }
  300. }
  301. }
  302. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  303. if(!p)
  304. CRYPTOerr(CRYPTO_F_DEF_GET_CLASS,ERR_R_MALLOC_FAILURE);
  305. return p;
  306. }
  307. /* Add a new method to the given EX_CLASS_ITEM and return the corresponding
  308. * index (or -1 for error). Handles locking. */
  309. static int def_add_index(EX_CLASS_ITEM *item, long argl, void *argp,
  310. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  311. CRYPTO_EX_free *free_func)
  312. {
  313. int toret = -1;
  314. CRYPTO_EX_DATA_FUNCS *a = (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(
  315. sizeof(CRYPTO_EX_DATA_FUNCS));
  316. if(!a)
  317. {
  318. CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX,ERR_R_MALLOC_FAILURE);
  319. return -1;
  320. }
  321. a->argl=argl;
  322. a->argp=argp;
  323. a->new_func=new_func;
  324. a->dup_func=dup_func;
  325. a->free_func=free_func;
  326. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  327. while (sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) <= item->meth_num)
  328. {
  329. if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, NULL))
  330. {
  331. CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX,ERR_R_MALLOC_FAILURE);
  332. OPENSSL_free(a);
  333. goto err;
  334. }
  335. }
  336. toret = item->meth_num++;
  337. sk_CRYPTO_EX_DATA_FUNCS_set(item->meth, toret, a);
  338. err:
  339. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  340. return toret;
  341. }
  342. /**************************************************************/
  343. /* The functions in the default CRYPTO_EX_DATA_IMPL structure */
  344. static int int_new_class(void)
  345. {
  346. int toret;
  347. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  348. toret = ex_class++;
  349. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  350. return toret;
  351. }
  352. static void int_cleanup(void)
  353. {
  354. EX_DATA_CHECK(return;)
  355. lh_doall(ex_data, def_cleanup_cb);
  356. lh_free(ex_data);
  357. ex_data = NULL;
  358. impl = NULL;
  359. }
  360. static int int_get_new_index(int class_index, long argl, void *argp,
  361. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  362. CRYPTO_EX_free *free_func)
  363. {
  364. EX_CLASS_ITEM *item = def_get_class(class_index);
  365. if(!item)
  366. return -1;
  367. return def_add_index(item, argl, argp, new_func, dup_func, free_func);
  368. }
  369. /* Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries in
  370. * the lock, then using them outside the lock. NB: Thread-safety only applies to
  371. * the global "ex_data" state (ie. class definitions), not thread-safe on 'ad'
  372. * itself. */
  373. static int int_new_ex_data(int class_index, void *obj,
  374. CRYPTO_EX_DATA *ad)
  375. {
  376. int mx,i;
  377. void *ptr;
  378. CRYPTO_EX_DATA_FUNCS **storage = NULL;
  379. EX_CLASS_ITEM *item = def_get_class(class_index);
  380. if(!item)
  381. /* error is already set */
  382. return 0;
  383. ad->sk = NULL;
  384. CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
  385. mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
  386. if(mx > 0)
  387. {
  388. storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
  389. if(!storage)
  390. goto skip;
  391. for(i = 0; i < mx; i++)
  392. storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i);
  393. }
  394. skip:
  395. CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
  396. if((mx > 0) && !storage)
  397. {
  398. CRYPTOerr(CRYPTO_F_INT_NEW_EX_DATA,ERR_R_MALLOC_FAILURE);
  399. return 0;
  400. }
  401. for(i = 0; i < mx; i++)
  402. {
  403. if(storage[i] && storage[i]->new_func)
  404. {
  405. ptr = CRYPTO_get_ex_data(ad, i);
  406. storage[i]->new_func(obj,ptr,ad,i,
  407. storage[i]->argl,storage[i]->argp);
  408. }
  409. }
  410. if(storage)
  411. OPENSSL_free(storage);
  412. return 1;
  413. }
  414. /* Same thread-safety notes as for "int_new_ex_data" */
  415. static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
  416. CRYPTO_EX_DATA *from)
  417. {
  418. int mx, j, i;
  419. char *ptr;
  420. CRYPTO_EX_DATA_FUNCS **storage = NULL;
  421. EX_CLASS_ITEM *item;
  422. if(!from->sk)
  423. /* 'to' should be "blank" which *is* just like 'from' */
  424. return 1;
  425. if((item = def_get_class(class_index)) == NULL)
  426. return 0;
  427. CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
  428. mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
  429. j = sk_num(from->sk);
  430. if(j < mx)
  431. mx = j;
  432. if(mx > 0)
  433. {
  434. storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
  435. if(!storage)
  436. goto skip;
  437. for(i = 0; i < mx; i++)
  438. storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i);
  439. }
  440. skip:
  441. CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
  442. if((mx > 0) && !storage)
  443. {
  444. CRYPTOerr(CRYPTO_F_INT_DUP_EX_DATA,ERR_R_MALLOC_FAILURE);
  445. return 0;
  446. }
  447. for(i = 0; i < mx; i++)
  448. {
  449. ptr = CRYPTO_get_ex_data(from, i);
  450. if(storage[i] && storage[i]->dup_func)
  451. storage[i]->dup_func(to,from,&ptr,i,
  452. storage[i]->argl,storage[i]->argp);
  453. CRYPTO_set_ex_data(to,i,ptr);
  454. }
  455. if(storage)
  456. OPENSSL_free(storage);
  457. return 1;
  458. }
  459. /* Same thread-safety notes as for "int_new_ex_data" */
  460. static void int_free_ex_data(int class_index, void *obj,
  461. CRYPTO_EX_DATA *ad)
  462. {
  463. int mx,i;
  464. EX_CLASS_ITEM *item;
  465. void *ptr;
  466. CRYPTO_EX_DATA_FUNCS **storage = NULL;
  467. if((item = def_get_class(class_index)) == NULL)
  468. return;
  469. CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
  470. mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
  471. if(mx > 0)
  472. {
  473. storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS*));
  474. if(!storage)
  475. goto skip;
  476. for(i = 0; i < mx; i++)
  477. storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth,i);
  478. }
  479. skip:
  480. CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
  481. if((mx > 0) && !storage)
  482. {
  483. CRYPTOerr(CRYPTO_F_INT_FREE_EX_DATA,ERR_R_MALLOC_FAILURE);
  484. return;
  485. }
  486. for(i = 0; i < mx; i++)
  487. {
  488. if(storage[i] && storage[i]->free_func)
  489. {
  490. ptr = CRYPTO_get_ex_data(ad,i);
  491. storage[i]->free_func(obj,ptr,ad,i,
  492. storage[i]->argl,storage[i]->argp);
  493. }
  494. }
  495. if(storage)
  496. OPENSSL_free(storage);
  497. if(ad->sk)
  498. {
  499. sk_free(ad->sk);
  500. ad->sk=NULL;
  501. }
  502. }
  503. /********************************************************************/
  504. /* API functions that defer all "state" operations to the "ex_data"
  505. * implementation we have set. */
  506. /* Obtain an index for a new class (not the same as getting a new index within
  507. * an existing class - this is actually getting a new *class*) */
  508. int CRYPTO_ex_data_new_class(void)
  509. {
  510. IMPL_CHECK
  511. return EX_IMPL(new_class)();
  512. }
  513. /* Release all "ex_data" state to prevent memory leaks. This can't be made
  514. * thread-safe without overhauling a lot of stuff, and shouldn't really be
  515. * called under potential race-conditions anyway (it's for program shutdown
  516. * after all). */
  517. void CRYPTO_cleanup_all_ex_data(void)
  518. {
  519. IMPL_CHECK
  520. EX_IMPL(cleanup)();
  521. }
  522. /* Inside an existing class, get/register a new index. */
  523. int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
  524. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  525. CRYPTO_EX_free *free_func)
  526. {
  527. int ret = -1;
  528. IMPL_CHECK
  529. ret = EX_IMPL(get_new_index)(class_index,
  530. argl, argp, new_func, dup_func, free_func);
  531. return ret;
  532. }
  533. /* Initialise a new CRYPTO_EX_DATA for use in a particular class - including
  534. * calling new() callbacks for each index in the class used by this variable */
  535. int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  536. {
  537. IMPL_CHECK
  538. return EX_IMPL(new_ex_data)(class_index, obj, ad);
  539. }
  540. /* Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks for
  541. * each index in the class used by this variable */
  542. int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
  543. CRYPTO_EX_DATA *from)
  544. {
  545. IMPL_CHECK
  546. return EX_IMPL(dup_ex_data)(class_index, to, from);
  547. }
  548. /* Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
  549. * each index in the class used by this variable */
  550. void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  551. {
  552. IMPL_CHECK
  553. EX_IMPL(free_ex_data)(class_index, obj, ad);
  554. }
  555. /* For a given CRYPTO_EX_DATA variable, set the value corresponding to a
  556. * particular index in the class used by this variable */
  557. int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
  558. {
  559. int i;
  560. if (ad->sk == NULL)
  561. {
  562. if ((ad->sk=sk_new_null()) == NULL)
  563. {
  564. CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
  565. return(0);
  566. }
  567. }
  568. i=sk_num(ad->sk);
  569. while (i <= idx)
  570. {
  571. if (!sk_push(ad->sk,NULL))
  572. {
  573. CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
  574. return(0);
  575. }
  576. i++;
  577. }
  578. sk_set(ad->sk,idx,val);
  579. return(1);
  580. }
  581. /* For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
  582. * particular index in the class used by this variable */
  583. void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
  584. {
  585. if (ad->sk == NULL)
  586. return(0);
  587. else if (idx >= sk_num(ad->sk))
  588. return(0);
  589. else
  590. return(sk_value(ad->sk,idx));
  591. }
  592. IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS)