ex_data.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. /* GLOBAL OPERATIONS */
  144. /* Return a new class index */
  145. int (*cb_new_class) (void);
  146. /* Cleanup all state used by the implementation */
  147. void (*cb_cleanup) (void);
  148. /************************/
  149. /* PER-CLASS OPERATIONS */
  150. /* Get a new method index within a class */
  151. int (*cb_get_new_index) (int class_index, long argl, void *argp,
  152. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  153. CRYPTO_EX_free *free_func);
  154. /* Initialise a new CRYPTO_EX_DATA of a given class */
  155. int (*cb_new_ex_data) (int class_index, void *obj, CRYPTO_EX_DATA *ad);
  156. /* Duplicate a CRYPTO_EX_DATA of a given class onto a copy */
  157. int (*cb_dup_ex_data) (int class_index, CRYPTO_EX_DATA *to,
  158. CRYPTO_EX_DATA *from);
  159. /* Cleanup a CRYPTO_EX_DATA of a given class */
  160. void (*cb_free_ex_data) (int class_index, void *obj, CRYPTO_EX_DATA *ad);
  161. };
  162. /* The implementation we use at run-time */
  163. static const CRYPTO_EX_DATA_IMPL *impl = NULL;
  164. /*
  165. * To call "impl" functions, use this macro rather than referring to 'impl'
  166. * directly, eg. EX_IMPL(get_new_index)(...);
  167. */
  168. #define EX_IMPL(a) impl->cb_##a
  169. /* Predeclare the "default" ex_data implementation */
  170. static int int_new_class(void);
  171. static void int_cleanup(void);
  172. static int int_get_new_index(int class_index, long argl, void *argp,
  173. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  174. CRYPTO_EX_free *free_func);
  175. static int int_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
  176. static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
  177. CRYPTO_EX_DATA *from);
  178. static void int_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
  179. static CRYPTO_EX_DATA_IMPL impl_default = {
  180. int_new_class,
  181. int_cleanup,
  182. int_get_new_index,
  183. int_new_ex_data,
  184. int_dup_ex_data,
  185. int_free_ex_data
  186. };
  187. /*
  188. * Internal function that checks whether "impl" is set and if not, sets it to
  189. * the default.
  190. */
  191. static void impl_check(void)
  192. {
  193. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  194. if (!impl)
  195. impl = &impl_default;
  196. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  197. }
  198. /*
  199. * A macro wrapper for impl_check that first uses a non-locked test before
  200. * invoking the function (which checks again inside a lock).
  201. */
  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 return impl;
  207. }
  208. int CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i)
  209. {
  210. int toret = 0;
  211. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  212. if (!impl) {
  213. impl = i;
  214. toret = 1;
  215. }
  216. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  217. return toret;
  218. }
  219. /****************************************************************************/
  220. /*
  221. * Interal (default) implementation of "ex_data" support. API functions are
  222. * further down.
  223. */
  224. /*
  225. * The type that represents what each "class" used to implement locally. A
  226. * STACK of CRYPTO_EX_DATA_FUNCS plus a index-counter. The 'class_index' is
  227. * the global value representing the class that is used to distinguish these
  228. * items.
  229. */
  230. typedef struct st_ex_class_item {
  231. int class_index;
  232. STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
  233. int meth_num;
  234. } EX_CLASS_ITEM;
  235. /* When assigning new class indexes, this is our counter */
  236. static int ex_class = CRYPTO_EX_INDEX_USER;
  237. /* The global hash table of EX_CLASS_ITEM items */
  238. static LHASH *ex_data = NULL;
  239. /* The callbacks required in the "ex_data" hash table */
  240. static unsigned long ex_hash_cb(const void *a_void)
  241. {
  242. return ((const EX_CLASS_ITEM *)a_void)->class_index;
  243. }
  244. static int ex_cmp_cb(const void *a_void, const void *b_void)
  245. {
  246. return (((const EX_CLASS_ITEM *)a_void)->class_index -
  247. ((const EX_CLASS_ITEM *)b_void)->class_index);
  248. }
  249. /*
  250. * Internal functions used by the "impl_default" implementation to access the
  251. * state
  252. */
  253. static int ex_data_check(void)
  254. {
  255. int toret = 1;
  256. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  257. if (!ex_data && ((ex_data = lh_new(ex_hash_cb, ex_cmp_cb)) == NULL))
  258. toret = 0;
  259. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  260. return toret;
  261. }
  262. /*
  263. * This macros helps reduce the locking from repeated checks because the
  264. * ex_data_check() function checks ex_data again inside a lock.
  265. */
  266. #define EX_DATA_CHECK(iffail) if(!ex_data && !ex_data_check()) {iffail}
  267. /* This "inner" callback is used by the callback function that follows it */
  268. static void def_cleanup_util_cb(CRYPTO_EX_DATA_FUNCS *funcs)
  269. {
  270. OPENSSL_free(funcs);
  271. }
  272. /*
  273. * This callback is used in lh_doall to destroy all EX_CLASS_ITEM values from
  274. * "ex_data" prior to the ex_data hash table being itself destroyed. Doesn't
  275. * do any locking.
  276. */
  277. static void def_cleanup_cb(void *a_void)
  278. {
  279. EX_CLASS_ITEM *item = (EX_CLASS_ITEM *)a_void;
  280. sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, def_cleanup_util_cb);
  281. OPENSSL_free(item);
  282. }
  283. /*
  284. * Return the EX_CLASS_ITEM from the "ex_data" hash table that corresponds to
  285. * a given class. Handles locking.
  286. */
  287. static EX_CLASS_ITEM *def_get_class(int class_index)
  288. {
  289. EX_CLASS_ITEM d, *p, *gen;
  290. EX_DATA_CHECK(return NULL;)
  291. d.class_index = class_index;
  292. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  293. p = lh_retrieve(ex_data, &d);
  294. if (!p) {
  295. gen = OPENSSL_malloc(sizeof(EX_CLASS_ITEM));
  296. if (gen) {
  297. gen->class_index = class_index;
  298. gen->meth_num = 0;
  299. gen->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null();
  300. if (!gen->meth)
  301. OPENSSL_free(gen);
  302. else {
  303. /*
  304. * Because we're inside the ex_data lock, the return value
  305. * from the insert will be NULL
  306. */
  307. lh_insert(ex_data, gen);
  308. p = gen;
  309. }
  310. }
  311. }
  312. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  313. if (!p)
  314. CRYPTOerr(CRYPTO_F_DEF_GET_CLASS, ERR_R_MALLOC_FAILURE);
  315. return p;
  316. }
  317. /*
  318. * Add a new method to the given EX_CLASS_ITEM and return the corresponding
  319. * index (or -1 for error). Handles locking.
  320. */
  321. static int def_add_index(EX_CLASS_ITEM *item, long argl, void *argp,
  322. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  323. CRYPTO_EX_free *free_func)
  324. {
  325. int toret = -1;
  326. CRYPTO_EX_DATA_FUNCS *a =
  327. (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS));
  328. if (!a) {
  329. CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX, ERR_R_MALLOC_FAILURE);
  330. return -1;
  331. }
  332. a->argl = argl;
  333. a->argp = argp;
  334. a->new_func = new_func;
  335. a->dup_func = dup_func;
  336. a->free_func = free_func;
  337. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  338. while (sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) <= item->meth_num) {
  339. if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, NULL)) {
  340. CRYPTOerr(CRYPTO_F_DEF_ADD_INDEX, ERR_R_MALLOC_FAILURE);
  341. OPENSSL_free(a);
  342. goto err;
  343. }
  344. }
  345. toret = item->meth_num++;
  346. (void)sk_CRYPTO_EX_DATA_FUNCS_set(item->meth, toret, a);
  347. err:
  348. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  349. return toret;
  350. }
  351. /**************************************************************/
  352. /* The functions in the default CRYPTO_EX_DATA_IMPL structure */
  353. static int int_new_class(void)
  354. {
  355. int toret;
  356. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  357. toret = ex_class++;
  358. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  359. return toret;
  360. }
  361. static void int_cleanup(void)
  362. {
  363. EX_DATA_CHECK(return;)
  364. lh_doall(ex_data, def_cleanup_cb);
  365. lh_free(ex_data);
  366. ex_data = NULL;
  367. impl = NULL;
  368. }
  369. static int int_get_new_index(int class_index, long argl, void *argp,
  370. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  371. CRYPTO_EX_free *free_func)
  372. {
  373. EX_CLASS_ITEM *item = def_get_class(class_index);
  374. if (!item)
  375. return -1;
  376. return def_add_index(item, argl, argp, new_func, dup_func, free_func);
  377. }
  378. /*
  379. * Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries
  380. * in the lock, then using them outside the lock. NB: Thread-safety only
  381. * applies to the global "ex_data" state (ie. class definitions), not
  382. * thread-safe on 'ad' itself.
  383. */
  384. static int int_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  385. {
  386. int mx, i;
  387. void *ptr;
  388. CRYPTO_EX_DATA_FUNCS **storage = NULL;
  389. EX_CLASS_ITEM *item = def_get_class(class_index);
  390. if (!item)
  391. /* error is already set */
  392. return 0;
  393. ad->sk = NULL;
  394. CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
  395. mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
  396. if (mx > 0) {
  397. storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS *));
  398. if (!storage)
  399. goto skip;
  400. for (i = 0; i < mx; i++)
  401. storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
  402. }
  403. skip:
  404. CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
  405. if ((mx > 0) && !storage) {
  406. CRYPTOerr(CRYPTO_F_INT_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
  407. return 0;
  408. }
  409. for (i = 0; i < mx; i++) {
  410. if (storage[i] && storage[i]->new_func) {
  411. ptr = CRYPTO_get_ex_data(ad, i);
  412. storage[i]->new_func(obj, ptr, ad, i,
  413. storage[i]->argl, storage[i]->argp);
  414. }
  415. }
  416. if (storage)
  417. OPENSSL_free(storage);
  418. return 1;
  419. }
  420. /* Same thread-safety notes as for "int_new_ex_data" */
  421. static int int_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
  422. CRYPTO_EX_DATA *from)
  423. {
  424. int mx, j, i;
  425. char *ptr;
  426. CRYPTO_EX_DATA_FUNCS **storage = NULL;
  427. EX_CLASS_ITEM *item;
  428. if (!from->sk)
  429. /* 'to' should be "blank" which *is* just like 'from' */
  430. return 1;
  431. if ((item = def_get_class(class_index)) == NULL)
  432. return 0;
  433. CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
  434. mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
  435. j = sk_num(from->sk);
  436. if (j < mx)
  437. mx = j;
  438. if (mx > 0) {
  439. storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS *));
  440. if (!storage)
  441. goto skip;
  442. for (i = 0; i < mx; i++)
  443. storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
  444. }
  445. skip:
  446. CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
  447. if ((mx > 0) && !storage) {
  448. CRYPTOerr(CRYPTO_F_INT_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
  449. return 0;
  450. }
  451. for (i = 0; i < mx; i++) {
  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, CRYPTO_EX_DATA *ad)
  464. {
  465. int mx, i;
  466. EX_CLASS_ITEM *item;
  467. void *ptr;
  468. CRYPTO_EX_DATA_FUNCS **storage = NULL;
  469. if ((item = def_get_class(class_index)) == NULL)
  470. return;
  471. CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
  472. mx = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
  473. if (mx > 0) {
  474. storage = OPENSSL_malloc(mx * sizeof(CRYPTO_EX_DATA_FUNCS *));
  475. if (!storage)
  476. goto skip;
  477. for (i = 0; i < mx; i++)
  478. storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(item->meth, i);
  479. }
  480. skip:
  481. CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
  482. if ((mx > 0) && !storage) {
  483. CRYPTOerr(CRYPTO_F_INT_FREE_EX_DATA, ERR_R_MALLOC_FAILURE);
  484. return;
  485. }
  486. for (i = 0; i < mx; i++) {
  487. if (storage[i] && storage[i]->free_func) {
  488. ptr = CRYPTO_get_ex_data(ad, i);
  489. storage[i]->free_func(obj, ptr, ad, i,
  490. storage[i]->argl, storage[i]->argp);
  491. }
  492. }
  493. if (storage)
  494. OPENSSL_free(storage);
  495. if (ad->sk) {
  496. sk_free(ad->sk);
  497. ad->sk = NULL;
  498. }
  499. }
  500. /********************************************************************/
  501. /*
  502. * API functions that defer all "state" operations to the "ex_data"
  503. * implementation we have set.
  504. */
  505. /*
  506. * Obtain an index for a new class (not the same as getting a new index
  507. * within an existing class - this is actually getting a new *class*)
  508. */
  509. int CRYPTO_ex_data_new_class(void)
  510. {
  511. IMPL_CHECK return EX_IMPL(new_class) ();
  512. }
  513. /*
  514. * Release all "ex_data" state to prevent memory leaks. This can't be made
  515. * thread-safe without overhauling a lot of stuff, and shouldn't really be
  516. * called under potential race-conditions anyway (it's for program shutdown
  517. * after all).
  518. */
  519. void CRYPTO_cleanup_all_ex_data(void)
  520. {
  521. IMPL_CHECK EX_IMPL(cleanup) ();
  522. }
  523. /* Inside an existing class, get/register a new index. */
  524. int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
  525. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  526. CRYPTO_EX_free *free_func)
  527. {
  528. int ret = -1;
  529. IMPL_CHECK
  530. ret = EX_IMPL(get_new_index) (class_index,
  531. argl, argp, new_func, dup_func,
  532. free_func);
  533. return ret;
  534. }
  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. */
  539. int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  540. {
  541. IMPL_CHECK return EX_IMPL(new_ex_data) (class_index, obj, ad);
  542. }
  543. /*
  544. * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
  545. * for each index in the class used by this variable
  546. */
  547. int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
  548. CRYPTO_EX_DATA *from)
  549. {
  550. IMPL_CHECK return EX_IMPL(dup_ex_data) (class_index, to, from);
  551. }
  552. /*
  553. * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
  554. * each index in the class used by this variable
  555. */
  556. void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  557. {
  558. IMPL_CHECK EX_IMPL(free_ex_data) (class_index, obj, ad);
  559. }
  560. /*
  561. * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
  562. * particular index in the class used by this variable
  563. */
  564. int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
  565. {
  566. int i;
  567. if (ad->sk == NULL) {
  568. if ((ad->sk = sk_new_null()) == NULL) {
  569. CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
  570. return (0);
  571. }
  572. }
  573. i = sk_num(ad->sk);
  574. while (i <= idx) {
  575. if (!sk_push(ad->sk, NULL)) {
  576. CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
  577. return (0);
  578. }
  579. i++;
  580. }
  581. sk_set(ad->sk, idx, val);
  582. return (1);
  583. }
  584. /*
  585. * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
  586. * particular index in the class used by this variable
  587. */
  588. void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
  589. {
  590. if (ad->sk == NULL)
  591. return (0);
  592. else if (idx >= sk_num(ad->sk))
  593. return (0);
  594. else
  595. return (sk_value(ad->sk, idx));
  596. }
  597. IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS)