ex_data.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. #include "internal/cryptlib.h"
  111. #include <openssl/lhash.h>
  112. /*
  113. * Each structure type (sometimes called a class), that supports
  114. * exdata has a stack of callbacks for each instance.
  115. */
  116. struct ex_callback_st {
  117. long argl; /* Arbitary long */
  118. void *argp; /* Arbitary void * */
  119. CRYPTO_EX_new *new_func;
  120. CRYPTO_EX_free *free_func;
  121. CRYPTO_EX_dup *dup_func;
  122. };
  123. /*
  124. * The state for each class. This could just be a typedef, but
  125. * a structure allows future changes.
  126. */
  127. typedef struct ex_callbacks_st {
  128. STACK_OF(EX_CALLBACK) *meth;
  129. } EX_CALLBACKS;
  130. static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
  131. /*
  132. * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
  133. * a given class. On success, *holds the lock.*
  134. */
  135. static EX_CALLBACKS *get_and_lock(int class_index)
  136. {
  137. EX_CALLBACKS *ip;
  138. if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
  139. CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
  140. return NULL;
  141. }
  142. ip = &ex_data[class_index];
  143. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  144. if (ip->meth == NULL) {
  145. ip->meth = sk_EX_CALLBACK_new_null();
  146. /* We push an initial value on the stack because the SSL
  147. * "app_data" routines use ex_data index zero. See RT 3710. */
  148. if (ip->meth == NULL
  149. || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
  150. CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
  151. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  152. return NULL;
  153. }
  154. }
  155. return ip;
  156. }
  157. static void cleanup_cb(EX_CALLBACK *funcs)
  158. {
  159. OPENSSL_free(funcs);
  160. }
  161. /*
  162. * Release all "ex_data" state to prevent memory leaks. This can't be made
  163. * thread-safe without overhauling a lot of stuff, and shouldn't really be
  164. * called under potential race-conditions anyway (it's for program shutdown
  165. * after all).
  166. */
  167. void CRYPTO_cleanup_all_ex_data(void)
  168. {
  169. int i;
  170. for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
  171. EX_CALLBACKS *ip = &ex_data[i];
  172. sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
  173. ip->meth = NULL;
  174. }
  175. }
  176. /*
  177. * Unregister a new index by replacing the callbacks with no-ops.
  178. * Any in-use instances are leaked.
  179. */
  180. static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
  181. long argl, void *argp)
  182. {
  183. }
  184. static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
  185. long argl, void *argp)
  186. {
  187. }
  188. static int dummy_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from,
  189. void *from_d, int idx,
  190. long argl, void *argp)
  191. {
  192. return 0;
  193. }
  194. int CRYPTO_free_ex_index(int class_index, int idx)
  195. {
  196. EX_CALLBACKS *ip = get_and_lock(class_index);
  197. EX_CALLBACK *a;
  198. int toret = 0;
  199. if (ip == NULL)
  200. return 0;
  201. if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
  202. goto err;
  203. a = sk_EX_CALLBACK_value(ip->meth, idx);
  204. if (a == NULL)
  205. goto err;
  206. a->new_func = dummy_new;
  207. a->dup_func = dummy_dup;
  208. a->free_func = dummy_free;
  209. toret = 1;
  210. err:
  211. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  212. return toret;
  213. }
  214. /*
  215. * Register a new index.
  216. */
  217. int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
  218. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  219. CRYPTO_EX_free *free_func)
  220. {
  221. int toret = -1;
  222. EX_CALLBACK *a;
  223. EX_CALLBACKS *ip = get_and_lock(class_index);
  224. if (ip == NULL)
  225. return -1;
  226. a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
  227. if (a == NULL) {
  228. CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
  229. goto err;
  230. }
  231. a->argl = argl;
  232. a->argp = argp;
  233. a->new_func = new_func;
  234. a->dup_func = dup_func;
  235. a->free_func = free_func;
  236. if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
  237. CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
  238. OPENSSL_free(a);
  239. goto err;
  240. }
  241. toret = sk_EX_CALLBACK_num(ip->meth) - 1;
  242. (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
  243. err:
  244. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  245. return toret;
  246. }
  247. /*
  248. * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
  249. * calling new() callbacks for each index in the class used by this variable
  250. * Thread-safe by copying a class's array of "EX_CALLBACK" entries
  251. * in the lock, then using them outside the lock. Note this only applies
  252. * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
  253. */
  254. int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  255. {
  256. int mx, i;
  257. void *ptr;
  258. EX_CALLBACK **storage = NULL;
  259. EX_CALLBACK *stack[10];
  260. EX_CALLBACKS *ip = get_and_lock(class_index);
  261. if (ip == NULL)
  262. return 0;
  263. ad->sk = NULL;
  264. mx = sk_EX_CALLBACK_num(ip->meth);
  265. if (mx > 0) {
  266. if (mx < (int)OSSL_NELEM(stack))
  267. storage = stack;
  268. else
  269. storage = OPENSSL_malloc(sizeof(*storage) * mx);
  270. if (storage != NULL)
  271. for (i = 0; i < mx; i++)
  272. storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
  273. }
  274. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  275. if (mx > 0 && storage == NULL) {
  276. CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
  277. return 0;
  278. }
  279. for (i = 0; i < mx; i++) {
  280. if (storage[i] && storage[i]->new_func) {
  281. ptr = CRYPTO_get_ex_data(ad, i);
  282. storage[i]->new_func(obj, ptr, ad, i,
  283. storage[i]->argl, storage[i]->argp);
  284. }
  285. }
  286. if (storage != stack)
  287. OPENSSL_free(storage);
  288. return 1;
  289. }
  290. /*
  291. * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
  292. * for each index in the class used by this variable
  293. */
  294. int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
  295. CRYPTO_EX_DATA *from)
  296. {
  297. int mx, j, i;
  298. char *ptr;
  299. EX_CALLBACK *stack[10];
  300. EX_CALLBACK **storage = NULL;
  301. EX_CALLBACKS *ip;
  302. if (from->sk == NULL)
  303. /* Nothing to copy over */
  304. return 1;
  305. if ((ip = get_and_lock(class_index)) == NULL)
  306. return 0;
  307. mx = sk_EX_CALLBACK_num(ip->meth);
  308. j = sk_void_num(from->sk);
  309. if (j < mx)
  310. mx = j;
  311. if (mx > 0) {
  312. if (mx < (int)OSSL_NELEM(stack))
  313. storage = stack;
  314. else
  315. storage = OPENSSL_malloc(sizeof(*storage) * mx);
  316. if (storage != NULL)
  317. for (i = 0; i < mx; i++)
  318. storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
  319. }
  320. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  321. if (mx > 0 && storage == NULL) {
  322. CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
  323. return 0;
  324. }
  325. for (i = 0; i < mx; i++) {
  326. ptr = CRYPTO_get_ex_data(from, i);
  327. if (storage[i] && storage[i]->dup_func)
  328. storage[i]->dup_func(to, from, &ptr, i,
  329. storage[i]->argl, storage[i]->argp);
  330. CRYPTO_set_ex_data(to, i, ptr);
  331. }
  332. if (storage != stack)
  333. OPENSSL_free(storage);
  334. return 1;
  335. }
  336. /*
  337. * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
  338. * each index in the class used by this variable
  339. */
  340. void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  341. {
  342. int mx, i;
  343. EX_CALLBACKS *ip;
  344. void *ptr;
  345. EX_CALLBACK *stack[10];
  346. EX_CALLBACK **storage = NULL;
  347. if ((ip = get_and_lock(class_index)) == NULL)
  348. return;
  349. mx = sk_EX_CALLBACK_num(ip->meth);
  350. if (mx > 0) {
  351. if (mx < (int)OSSL_NELEM(stack))
  352. storage = stack;
  353. else
  354. storage = OPENSSL_malloc(sizeof(*storage) * mx);
  355. if (storage != NULL)
  356. for (i = 0; i < mx; i++)
  357. storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
  358. }
  359. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  360. if (mx > 0 && storage == NULL) {
  361. CRYPTOerr(CRYPTO_F_CRYPTO_FREE_EX_DATA, ERR_R_MALLOC_FAILURE);
  362. return;
  363. }
  364. for (i = 0; i < mx; i++) {
  365. if (storage[i] && storage[i]->free_func) {
  366. ptr = CRYPTO_get_ex_data(ad, i);
  367. storage[i]->free_func(obj, ptr, ad, i,
  368. storage[i]->argl, storage[i]->argp);
  369. }
  370. }
  371. if (storage != stack)
  372. OPENSSL_free(storage);
  373. sk_void_free(ad->sk);
  374. ad->sk = NULL;
  375. }
  376. /*
  377. * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
  378. * particular index in the class used by this variable
  379. */
  380. int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
  381. {
  382. int i;
  383. if (ad->sk == NULL) {
  384. if ((ad->sk = sk_void_new_null()) == NULL) {
  385. CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
  386. return 0;
  387. }
  388. }
  389. for (i = sk_void_num(ad->sk); i <= idx; ++i) {
  390. if (!sk_void_push(ad->sk, NULL)) {
  391. CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
  392. return 0;
  393. }
  394. }
  395. sk_void_set(ad->sk, idx, val);
  396. return 1;
  397. }
  398. /*
  399. * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
  400. * particular index in the class used by this variable
  401. */
  402. void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
  403. {
  404. if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
  405. return NULL;
  406. return sk_void_value(ad->sk, idx);
  407. }