2
0

ex_data.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdlib.h>
  10. #include "crypto/cryptlib.h"
  11. #include "internal/thread_once.h"
  12. int ossl_do_ex_data_init(OSSL_LIB_CTX *ctx)
  13. {
  14. OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
  15. if (global == NULL)
  16. return 0;
  17. global->ex_data_lock = CRYPTO_THREAD_lock_new();
  18. return global->ex_data_lock != NULL;
  19. }
  20. /*
  21. * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
  22. * a given class. On success, *holds the lock.*
  23. * The |global| parameter is assumed to be non null (checked by the caller).
  24. */
  25. static EX_CALLBACKS *get_and_lock(OSSL_EX_DATA_GLOBAL *global, int class_index)
  26. {
  27. EX_CALLBACKS *ip;
  28. if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
  29. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
  30. return NULL;
  31. }
  32. if (global->ex_data_lock == NULL) {
  33. /*
  34. * If we get here, someone (who?) cleaned up the lock, so just
  35. * treat it as an error.
  36. */
  37. return NULL;
  38. }
  39. if (!CRYPTO_THREAD_write_lock(global->ex_data_lock))
  40. return NULL;
  41. ip = &global->ex_data[class_index];
  42. return ip;
  43. }
  44. static void cleanup_cb(EX_CALLBACK *funcs)
  45. {
  46. OPENSSL_free(funcs);
  47. }
  48. /*
  49. * Release all "ex_data" state to prevent memory leaks. This can't be made
  50. * thread-safe without overhauling a lot of stuff, and shouldn't really be
  51. * called under potential race-conditions anyway (it's for program shutdown
  52. * after all).
  53. */
  54. void ossl_crypto_cleanup_all_ex_data_int(OSSL_LIB_CTX *ctx)
  55. {
  56. int i;
  57. OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
  58. if (global == NULL)
  59. return;
  60. for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
  61. EX_CALLBACKS *ip = &global->ex_data[i];
  62. sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
  63. ip->meth = NULL;
  64. }
  65. CRYPTO_THREAD_lock_free(global->ex_data_lock);
  66. global->ex_data_lock = NULL;
  67. }
  68. /*
  69. * Unregister a new index by replacing the callbacks with no-ops.
  70. * Any in-use instances are leaked.
  71. */
  72. static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
  73. long argl, void *argp)
  74. {
  75. }
  76. static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
  77. long argl, void *argp)
  78. {
  79. }
  80. static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
  81. void **from_d, int idx,
  82. long argl, void *argp)
  83. {
  84. return 1;
  85. }
  86. int ossl_crypto_free_ex_index_ex(OSSL_LIB_CTX *ctx, int class_index, int idx)
  87. {
  88. EX_CALLBACKS *ip;
  89. EX_CALLBACK *a;
  90. int toret = 0;
  91. OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
  92. if (global == NULL)
  93. return 0;
  94. ip = get_and_lock(global, class_index);
  95. if (ip == NULL)
  96. return 0;
  97. if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
  98. goto err;
  99. a = sk_EX_CALLBACK_value(ip->meth, idx);
  100. if (a == NULL)
  101. goto err;
  102. a->new_func = dummy_new;
  103. a->dup_func = dummy_dup;
  104. a->free_func = dummy_free;
  105. toret = 1;
  106. err:
  107. CRYPTO_THREAD_unlock(global->ex_data_lock);
  108. return toret;
  109. }
  110. int CRYPTO_free_ex_index(int class_index, int idx)
  111. {
  112. return ossl_crypto_free_ex_index_ex(NULL, class_index, idx);
  113. }
  114. /*
  115. * Register a new index.
  116. */
  117. int ossl_crypto_get_ex_new_index_ex(OSSL_LIB_CTX *ctx, int class_index,
  118. long argl, void *argp,
  119. CRYPTO_EX_new *new_func,
  120. CRYPTO_EX_dup *dup_func,
  121. CRYPTO_EX_free *free_func,
  122. int priority)
  123. {
  124. int toret = -1;
  125. EX_CALLBACK *a;
  126. EX_CALLBACKS *ip;
  127. OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
  128. if (global == NULL)
  129. return -1;
  130. ip = get_and_lock(global, class_index);
  131. if (ip == NULL)
  132. return -1;
  133. if (ip->meth == NULL) {
  134. ip->meth = sk_EX_CALLBACK_new_null();
  135. /* We push an initial value on the stack because the SSL
  136. * "app_data" routines use ex_data index zero. See RT 3710. */
  137. if (ip->meth == NULL
  138. || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
  139. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  140. goto err;
  141. }
  142. }
  143. a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
  144. if (a == NULL) {
  145. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  146. goto err;
  147. }
  148. a->argl = argl;
  149. a->argp = argp;
  150. a->new_func = new_func;
  151. a->dup_func = dup_func;
  152. a->free_func = free_func;
  153. a->priority = priority;
  154. if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
  155. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  156. OPENSSL_free(a);
  157. goto err;
  158. }
  159. toret = sk_EX_CALLBACK_num(ip->meth) - 1;
  160. (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
  161. err:
  162. CRYPTO_THREAD_unlock(global->ex_data_lock);
  163. return toret;
  164. }
  165. int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
  166. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  167. CRYPTO_EX_free *free_func)
  168. {
  169. return ossl_crypto_get_ex_new_index_ex(NULL, class_index, argl, argp,
  170. new_func, dup_func, free_func, 0);
  171. }
  172. /*
  173. * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
  174. * calling new() callbacks for each index in the class used by this variable
  175. * Thread-safe by copying a class's array of "EX_CALLBACK" entries
  176. * in the lock, then using them outside the lock. Note this only applies
  177. * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
  178. */
  179. int ossl_crypto_new_ex_data_ex(OSSL_LIB_CTX *ctx, int class_index, void *obj,
  180. CRYPTO_EX_DATA *ad)
  181. {
  182. int mx, i;
  183. void *ptr;
  184. EX_CALLBACK **storage = NULL;
  185. EX_CALLBACK *stack[10];
  186. EX_CALLBACKS *ip;
  187. OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
  188. if (global == NULL)
  189. return 0;
  190. ip = get_and_lock(global, class_index);
  191. if (ip == NULL)
  192. return 0;
  193. ad->ctx = ctx;
  194. ad->sk = NULL;
  195. mx = sk_EX_CALLBACK_num(ip->meth);
  196. if (mx > 0) {
  197. if (mx < (int)OSSL_NELEM(stack))
  198. storage = stack;
  199. else
  200. storage = OPENSSL_malloc(sizeof(*storage) * mx);
  201. if (storage != NULL)
  202. for (i = 0; i < mx; i++)
  203. storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
  204. }
  205. CRYPTO_THREAD_unlock(global->ex_data_lock);
  206. if (mx > 0 && storage == NULL) {
  207. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  208. return 0;
  209. }
  210. for (i = 0; i < mx; i++) {
  211. if (storage[i] != NULL && storage[i]->new_func != NULL) {
  212. ptr = CRYPTO_get_ex_data(ad, i);
  213. storage[i]->new_func(obj, ptr, ad, i,
  214. storage[i]->argl, storage[i]->argp);
  215. }
  216. }
  217. if (storage != stack)
  218. OPENSSL_free(storage);
  219. return 1;
  220. }
  221. int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  222. {
  223. return ossl_crypto_new_ex_data_ex(NULL, class_index, obj, ad);
  224. }
  225. /*
  226. * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
  227. * for each index in the class used by this variable
  228. */
  229. int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
  230. const CRYPTO_EX_DATA *from)
  231. {
  232. int mx, j, i;
  233. void *ptr;
  234. EX_CALLBACK *stack[10];
  235. EX_CALLBACK **storage = NULL;
  236. EX_CALLBACKS *ip;
  237. int toret = 0;
  238. OSSL_EX_DATA_GLOBAL *global;
  239. to->ctx = from->ctx;
  240. if (from->sk == NULL)
  241. /* Nothing to copy over */
  242. return 1;
  243. global = ossl_lib_ctx_get_ex_data_global(from->ctx);
  244. if (global == NULL)
  245. return 0;
  246. ip = get_and_lock(global, class_index);
  247. if (ip == NULL)
  248. return 0;
  249. mx = sk_EX_CALLBACK_num(ip->meth);
  250. j = sk_void_num(from->sk);
  251. if (j < mx)
  252. mx = j;
  253. if (mx > 0) {
  254. if (mx < (int)OSSL_NELEM(stack))
  255. storage = stack;
  256. else
  257. storage = OPENSSL_malloc(sizeof(*storage) * mx);
  258. if (storage != NULL)
  259. for (i = 0; i < mx; i++)
  260. storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
  261. }
  262. CRYPTO_THREAD_unlock(global->ex_data_lock);
  263. if (mx == 0)
  264. return 1;
  265. if (storage == NULL) {
  266. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  267. return 0;
  268. }
  269. /*
  270. * Make sure the ex_data stack is at least |mx| elements long to avoid
  271. * issues in the for loop that follows; so go get the |mx|'th element
  272. * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
  273. * to itself. This is normally a no-op; but ensures the stack is the
  274. * proper size
  275. */
  276. if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
  277. goto err;
  278. for (i = 0; i < mx; i++) {
  279. ptr = CRYPTO_get_ex_data(from, i);
  280. if (storage[i] != NULL && storage[i]->dup_func != NULL)
  281. if (!storage[i]->dup_func(to, from, &ptr, i,
  282. storage[i]->argl, storage[i]->argp))
  283. goto err;
  284. CRYPTO_set_ex_data(to, i, ptr);
  285. }
  286. toret = 1;
  287. err:
  288. if (storage != stack)
  289. OPENSSL_free(storage);
  290. return toret;
  291. }
  292. struct ex_callback_entry {
  293. const EX_CALLBACK *excb;
  294. int index;
  295. };
  296. static int ex_callback_compare(const void *a, const void *b)
  297. {
  298. const struct ex_callback_entry *ap = (const struct ex_callback_entry *)a;
  299. const struct ex_callback_entry *bp = (const struct ex_callback_entry *)b;
  300. if (ap->excb == bp->excb)
  301. return 0;
  302. if (ap->excb == NULL)
  303. return 1;
  304. if (bp->excb == NULL)
  305. return -1;
  306. if (ap->excb->priority == bp->excb->priority)
  307. return 0;
  308. return ap->excb->priority > bp->excb->priority ? -1 : 1;
  309. }
  310. /*
  311. * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
  312. * each index in the class used by this variable
  313. */
  314. void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  315. {
  316. int mx, i;
  317. EX_CALLBACKS *ip;
  318. void *ptr;
  319. const EX_CALLBACK *f;
  320. struct ex_callback_entry stack[10];
  321. struct ex_callback_entry *storage = NULL;
  322. OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ad->ctx);
  323. if (global == NULL)
  324. goto err;
  325. ip = get_and_lock(global, class_index);
  326. if (ip == NULL)
  327. goto err;
  328. mx = sk_EX_CALLBACK_num(ip->meth);
  329. if (mx > 0) {
  330. if (mx < (int)OSSL_NELEM(stack))
  331. storage = stack;
  332. else
  333. storage = OPENSSL_malloc(sizeof(*storage) * mx);
  334. if (storage != NULL)
  335. for (i = 0; i < mx; i++) {
  336. storage[i].excb = sk_EX_CALLBACK_value(ip->meth, i);
  337. storage[i].index = i;
  338. }
  339. }
  340. CRYPTO_THREAD_unlock(global->ex_data_lock);
  341. if (storage != NULL) {
  342. /* Sort according to priority. High priority first */
  343. qsort(storage, mx, sizeof(*storage), ex_callback_compare);
  344. for (i = 0; i < mx; i++) {
  345. f = storage[i].excb;
  346. if (f != NULL && f->free_func != NULL) {
  347. ptr = CRYPTO_get_ex_data(ad, storage[i].index);
  348. f->free_func(obj, ptr, ad, storage[i].index, f->argl, f->argp);
  349. }
  350. }
  351. }
  352. if (storage != stack)
  353. OPENSSL_free(storage);
  354. err:
  355. sk_void_free(ad->sk);
  356. ad->sk = NULL;
  357. ad->ctx = NULL;
  358. }
  359. /*
  360. * Allocate a given CRYPTO_EX_DATA item using the class specific allocation
  361. * function
  362. */
  363. int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad,
  364. int idx)
  365. {
  366. void *curval;
  367. curval = CRYPTO_get_ex_data(ad, idx);
  368. /* Already there, no need to allocate */
  369. if (curval != NULL)
  370. return 1;
  371. return ossl_crypto_alloc_ex_data_intern(class_index, obj, ad, idx);
  372. }
  373. int ossl_crypto_alloc_ex_data_intern(int class_index, void *obj,
  374. CRYPTO_EX_DATA *ad, int idx)
  375. {
  376. EX_CALLBACK *f;
  377. EX_CALLBACKS *ip;
  378. OSSL_EX_DATA_GLOBAL *global;
  379. global = ossl_lib_ctx_get_ex_data_global(ad->ctx);
  380. if (global == NULL)
  381. return 0;
  382. ip = get_and_lock(global, class_index);
  383. if (ip == NULL)
  384. return 0;
  385. f = sk_EX_CALLBACK_value(ip->meth, idx);
  386. CRYPTO_THREAD_unlock(global->ex_data_lock);
  387. /*
  388. * This should end up calling CRYPTO_set_ex_data(), which allocates
  389. * everything necessary to support placing the new data in the right spot.
  390. */
  391. if (f->new_func == NULL)
  392. return 0;
  393. f->new_func(obj, NULL, ad, idx, f->argl, f->argp);
  394. return 1;
  395. }
  396. /*
  397. * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
  398. * particular index in the class used by this variable
  399. */
  400. int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
  401. {
  402. int i;
  403. if (ad->sk == NULL) {
  404. if ((ad->sk = sk_void_new_null()) == NULL) {
  405. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  406. return 0;
  407. }
  408. }
  409. for (i = sk_void_num(ad->sk); i <= idx; ++i) {
  410. if (!sk_void_push(ad->sk, NULL)) {
  411. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  412. return 0;
  413. }
  414. }
  415. if (sk_void_set(ad->sk, idx, val) != val) {
  416. /* Probably the index is out of bounds */
  417. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
  418. return 0;
  419. }
  420. return 1;
  421. }
  422. /*
  423. * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
  424. * particular index in the class used by this variable
  425. */
  426. void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
  427. {
  428. if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
  429. return NULL;
  430. return sk_void_value(ad->sk, idx);
  431. }
  432. OSSL_LIB_CTX *ossl_crypto_ex_data_get_ossl_lib_ctx(const CRYPTO_EX_DATA *ad)
  433. {
  434. return ad->ctx;
  435. }