You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

402 rivejä
13 KiB

  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. #include <openssl/ex_data.h>
  109. #include <assert.h>
  110. #include <openssl/err.h>
  111. #include <openssl/lhash.h>
  112. #include <openssl/mem.h>
  113. #include <openssl/stack.h>
  114. #include <openssl/thread.h>
  115. #include "crypto_error.h"
  116. #include "internal.h"
  117. typedef struct crypto_ex_data_func_st {
  118. long argl; /* Arbitary long */
  119. void *argp; /* Arbitary void pointer */
  120. CRYPTO_EX_new *new_func;
  121. CRYPTO_EX_free *free_func;
  122. CRYPTO_EX_dup *dup_func;
  123. } CRYPTO_EX_DATA_FUNCS;
  124. typedef struct st_ex_class_item {
  125. STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth;
  126. int class_value;
  127. } EX_CLASS_ITEM;
  128. static LHASH_OF(EX_CLASS_ITEM) *global_classes = NULL;
  129. static int global_next_class = 100;
  130. static int new_class(void) {
  131. int ret;
  132. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  133. ret = global_next_class++;
  134. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  135. return ret;
  136. }
  137. /* class_hash is a hash function used by an LHASH of |EX_CLASS_ITEM|
  138. * structures. */
  139. static uint32_t class_hash(const EX_CLASS_ITEM *a) {
  140. return a->class_value;
  141. }
  142. /* class_cmp is a comparison function for an LHASH of |EX_CLASS_ITEM|
  143. * structures. */
  144. static int class_cmp(const EX_CLASS_ITEM *a, const EX_CLASS_ITEM *b) {
  145. return a->class_value - b->class_value;
  146. }
  147. /* data_funcs_free is a callback function from |sk_pop_free| that frees a
  148. * |CRYPTO_EX_DATA_FUNCS|. */
  149. static void data_funcs_free(CRYPTO_EX_DATA_FUNCS *funcs) {
  150. OPENSSL_free(funcs);
  151. }
  152. /* class_free is a callback function from lh_doall to free the EX_CLASS_ITEM
  153. * structures. */
  154. static void class_free(EX_CLASS_ITEM *item) {
  155. sk_CRYPTO_EX_DATA_FUNCS_pop_free(item->meth, data_funcs_free);
  156. OPENSSL_free(item);
  157. }
  158. static LHASH_OF(EX_CLASS_ITEM) *get_classes(void) {
  159. LHASH_OF(EX_CLASS_ITEM) *ret;
  160. CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
  161. ret = global_classes;
  162. CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
  163. if (ret != NULL) {
  164. return ret;
  165. }
  166. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  167. if (global_classes == NULL) {
  168. global_classes = lh_EX_CLASS_ITEM_new(class_hash, class_cmp);
  169. }
  170. ret = global_classes;
  171. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  172. return ret;
  173. }
  174. static void cleanup(void) {
  175. LHASH_OF(EX_CLASS_ITEM) *classes = get_classes();
  176. if (classes != NULL) {
  177. lh_EX_CLASS_ITEM_doall(classes, class_free);
  178. lh_EX_CLASS_ITEM_free(classes);
  179. }
  180. global_classes = NULL;
  181. }
  182. static EX_CLASS_ITEM *get_class(int class_value) {
  183. LHASH_OF(EX_CLASS_ITEM) *const classes = get_classes();
  184. EX_CLASS_ITEM template, *class_item;
  185. int ok = 0;
  186. if (classes == NULL) {
  187. return NULL;
  188. }
  189. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  190. template.class_value = class_value;
  191. class_item = lh_EX_CLASS_ITEM_retrieve(classes, &template);
  192. if (class_item != NULL) {
  193. ok = 1;
  194. } else {
  195. class_item = OPENSSL_malloc(sizeof(EX_CLASS_ITEM));
  196. if (class_item) {
  197. class_item->class_value = class_value;
  198. class_item->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null();
  199. if (class_item->meth != NULL) {
  200. EX_CLASS_ITEM *old_data;
  201. ok = lh_EX_CLASS_ITEM_insert(classes, &old_data, class_item);
  202. assert(old_data == NULL);
  203. }
  204. }
  205. }
  206. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  207. if (!ok) {
  208. if (class_item) {
  209. if (class_item->meth) {
  210. sk_CRYPTO_EX_DATA_FUNCS_free(class_item->meth);
  211. }
  212. OPENSSL_free(class_item);
  213. class_item = NULL;
  214. }
  215. OPENSSL_PUT_ERROR(CRYPTO, get_class, ERR_R_MALLOC_FAILURE);
  216. }
  217. return class_item;
  218. }
  219. static int get_new_index(int class_value, long argl, void *argp,
  220. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  221. CRYPTO_EX_free *free_func) {
  222. EX_CLASS_ITEM *const item = get_class(class_value);
  223. CRYPTO_EX_DATA_FUNCS *funcs;
  224. int ret = -1;
  225. if (!item) {
  226. return -1;
  227. }
  228. funcs = OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS));
  229. if (funcs == NULL) {
  230. OPENSSL_PUT_ERROR(CRYPTO, get_new_index, ERR_R_MALLOC_FAILURE);
  231. return -1;
  232. }
  233. funcs->argl = argl;
  234. funcs->argp = argp;
  235. funcs->new_func = new_func;
  236. funcs->dup_func = dup_func;
  237. funcs->free_func = free_func;
  238. CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA);
  239. if (!sk_CRYPTO_EX_DATA_FUNCS_push(item->meth, funcs)) {
  240. OPENSSL_PUT_ERROR(CRYPTO, get_new_index, ERR_R_MALLOC_FAILURE);
  241. OPENSSL_free(funcs);
  242. goto err;
  243. }
  244. ret = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth) - 1;
  245. err:
  246. CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA);
  247. return ret;
  248. }
  249. /* get_func_pointers takes a copy of the CRYPTO_EX_DATA_FUNCS pointers, if any,
  250. * for the given class. If there are some pointers, it sets |*out| to point to
  251. * a fresh stack of them. Otherwise it sets |*out| to NULL. It returns one on
  252. * success or zero on error. */
  253. static int get_func_pointers(STACK_OF(CRYPTO_EX_DATA_FUNCS) **out,
  254. int class_value) {
  255. EX_CLASS_ITEM *const item = get_class(class_value);
  256. size_t n;
  257. if (!item) {
  258. return 0;
  259. }
  260. *out = NULL;
  261. /* CRYPTO_EX_DATA_FUNCS structures are static once set, so we can take a
  262. * shallow copy of the list under lock and then use the structures without
  263. * the lock held. */
  264. CRYPTO_r_lock(CRYPTO_LOCK_EX_DATA);
  265. n = sk_CRYPTO_EX_DATA_FUNCS_num(item->meth);
  266. if (n > 0) {
  267. *out = sk_CRYPTO_EX_DATA_FUNCS_dup(item->meth);
  268. }
  269. CRYPTO_r_unlock(CRYPTO_LOCK_EX_DATA);
  270. if (n > 0 && *out == NULL) {
  271. OPENSSL_PUT_ERROR(CRYPTO, get_func_pointers, ERR_R_MALLOC_FAILURE);
  272. return 0;
  273. }
  274. return 1;
  275. }
  276. static int new_ex_data(int class_value, void *obj, CRYPTO_EX_DATA *ad) {
  277. STACK_OF(CRYPTO_EX_DATA_FUNCS) *func_pointers;
  278. size_t i;
  279. ad->sk = NULL;
  280. if (!get_func_pointers(&func_pointers, class_value)) {
  281. return 0;
  282. }
  283. for (i = 0; i < sk_CRYPTO_EX_DATA_FUNCS_num(func_pointers); i++) {
  284. CRYPTO_EX_DATA_FUNCS *func_pointer =
  285. sk_CRYPTO_EX_DATA_FUNCS_value(func_pointers, i);
  286. if (func_pointer->new_func) {
  287. func_pointer->new_func(obj, NULL, ad, i, func_pointer->argl,
  288. func_pointer->argp);
  289. }
  290. }
  291. sk_CRYPTO_EX_DATA_FUNCS_free(func_pointers);
  292. return 1;
  293. }
  294. static int dup_ex_data(int class_value, CRYPTO_EX_DATA *to,
  295. const CRYPTO_EX_DATA *from) {
  296. STACK_OF(CRYPTO_EX_DATA_FUNCS) *func_pointers;
  297. size_t i;
  298. if (!from->sk) {
  299. /* In this case, |from| is blank, which is also the initial state of |to|,
  300. * so there's nothing to do. */
  301. return 1;
  302. }
  303. if (!get_func_pointers(&func_pointers, class_value)) {
  304. return 0;
  305. }
  306. for (i = 0; i < sk_CRYPTO_EX_DATA_FUNCS_num(func_pointers); i++) {
  307. CRYPTO_EX_DATA_FUNCS *func_pointer =
  308. sk_CRYPTO_EX_DATA_FUNCS_value(func_pointers, i);
  309. void *ptr = CRYPTO_get_ex_data(from, i);
  310. if (func_pointer->dup_func) {
  311. func_pointer->dup_func(to, from, &ptr, i, func_pointer->argl,
  312. func_pointer->argp);
  313. }
  314. CRYPTO_set_ex_data(to, i, ptr);
  315. }
  316. sk_CRYPTO_EX_DATA_FUNCS_free(func_pointers);
  317. return 1;
  318. }
  319. static void free_ex_data(int class_value, void *obj, CRYPTO_EX_DATA *ad) {
  320. STACK_OF(CRYPTO_EX_DATA_FUNCS) *func_pointers;
  321. size_t i;
  322. if (!get_func_pointers(&func_pointers, class_value)) {
  323. return;
  324. }
  325. for (i = 0; i < sk_CRYPTO_EX_DATA_FUNCS_num(func_pointers); i++) {
  326. CRYPTO_EX_DATA_FUNCS *func_pointer =
  327. sk_CRYPTO_EX_DATA_FUNCS_value(func_pointers, i);
  328. if (func_pointer->free_func) {
  329. void *ptr = CRYPTO_get_ex_data(ad, i);
  330. func_pointer->free_func(obj, ptr, ad, i, func_pointer->argl,
  331. func_pointer->argp);
  332. }
  333. }
  334. sk_CRYPTO_EX_DATA_FUNCS_free(func_pointers);
  335. if (ad->sk) {
  336. sk_void_free(ad->sk);
  337. ad->sk = NULL;
  338. }
  339. }
  340. const CRYPTO_EX_DATA_IMPL ex_data_default_impl = {
  341. new_class, cleanup, get_new_index, new_ex_data, dup_ex_data, free_ex_data};