ex_data.c 23 KB

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