ex_data.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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_CRYPTO_LIB);
  140. goto err;
  141. }
  142. }
  143. a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
  144. if (a == NULL)
  145. goto err;
  146. a->argl = argl;
  147. a->argp = argp;
  148. a->new_func = new_func;
  149. a->dup_func = dup_func;
  150. a->free_func = free_func;
  151. a->priority = priority;
  152. if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
  153. ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
  154. OPENSSL_free(a);
  155. goto err;
  156. }
  157. toret = sk_EX_CALLBACK_num(ip->meth) - 1;
  158. (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
  159. err:
  160. CRYPTO_THREAD_unlock(global->ex_data_lock);
  161. return toret;
  162. }
  163. int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
  164. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  165. CRYPTO_EX_free *free_func)
  166. {
  167. return ossl_crypto_get_ex_new_index_ex(NULL, class_index, argl, argp,
  168. new_func, dup_func, free_func, 0);
  169. }
  170. /*
  171. * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
  172. * calling new() callbacks for each index in the class used by this variable
  173. * Thread-safe by copying a class's array of "EX_CALLBACK" entries
  174. * in the lock, then using them outside the lock. Note this only applies
  175. * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
  176. */
  177. int ossl_crypto_new_ex_data_ex(OSSL_LIB_CTX *ctx, int class_index, void *obj,
  178. CRYPTO_EX_DATA *ad)
  179. {
  180. int mx, i;
  181. void *ptr;
  182. EX_CALLBACK **storage = NULL;
  183. EX_CALLBACK *stack[10];
  184. EX_CALLBACKS *ip;
  185. OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ctx);
  186. if (global == NULL)
  187. return 0;
  188. ip = get_and_lock(global, class_index);
  189. if (ip == NULL)
  190. return 0;
  191. ad->ctx = ctx;
  192. ad->sk = NULL;
  193. mx = sk_EX_CALLBACK_num(ip->meth);
  194. if (mx > 0) {
  195. if (mx < (int)OSSL_NELEM(stack))
  196. storage = stack;
  197. else
  198. storage = OPENSSL_malloc(sizeof(*storage) * mx);
  199. if (storage != NULL)
  200. for (i = 0; i < mx; i++)
  201. storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
  202. }
  203. CRYPTO_THREAD_unlock(global->ex_data_lock);
  204. if (mx > 0 && storage == NULL)
  205. return 0;
  206. for (i = 0; i < mx; i++) {
  207. if (storage[i] != NULL && storage[i]->new_func != NULL) {
  208. ptr = CRYPTO_get_ex_data(ad, i);
  209. storage[i]->new_func(obj, ptr, ad, i,
  210. storage[i]->argl, storage[i]->argp);
  211. }
  212. }
  213. if (storage != stack)
  214. OPENSSL_free(storage);
  215. return 1;
  216. }
  217. int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  218. {
  219. return ossl_crypto_new_ex_data_ex(NULL, class_index, obj, ad);
  220. }
  221. /*
  222. * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
  223. * for each index in the class used by this variable
  224. */
  225. int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
  226. const CRYPTO_EX_DATA *from)
  227. {
  228. int mx, j, i;
  229. void *ptr;
  230. EX_CALLBACK *stack[10];
  231. EX_CALLBACK **storage = NULL;
  232. EX_CALLBACKS *ip;
  233. int toret = 0;
  234. OSSL_EX_DATA_GLOBAL *global;
  235. to->ctx = from->ctx;
  236. if (from->sk == NULL)
  237. /* Nothing to copy over */
  238. return 1;
  239. global = ossl_lib_ctx_get_ex_data_global(from->ctx);
  240. if (global == NULL)
  241. return 0;
  242. ip = get_and_lock(global, class_index);
  243. if (ip == NULL)
  244. return 0;
  245. mx = sk_EX_CALLBACK_num(ip->meth);
  246. j = sk_void_num(from->sk);
  247. if (j < mx)
  248. mx = j;
  249. if (mx > 0) {
  250. if (mx < (int)OSSL_NELEM(stack))
  251. storage = stack;
  252. else
  253. storage = OPENSSL_malloc(sizeof(*storage) * mx);
  254. if (storage != NULL)
  255. for (i = 0; i < mx; i++)
  256. storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
  257. }
  258. CRYPTO_THREAD_unlock(global->ex_data_lock);
  259. if (mx == 0)
  260. return 1;
  261. if (storage == NULL)
  262. return 0;
  263. /*
  264. * Make sure the ex_data stack is at least |mx| elements long to avoid
  265. * issues in the for loop that follows; so go get the |mx|'th element
  266. * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
  267. * to itself. This is normally a no-op; but ensures the stack is the
  268. * proper size
  269. */
  270. if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
  271. goto err;
  272. for (i = 0; i < mx; i++) {
  273. ptr = CRYPTO_get_ex_data(from, i);
  274. if (storage[i] != NULL && storage[i]->dup_func != NULL)
  275. if (!storage[i]->dup_func(to, from, &ptr, i,
  276. storage[i]->argl, storage[i]->argp))
  277. goto err;
  278. CRYPTO_set_ex_data(to, i, ptr);
  279. }
  280. toret = 1;
  281. err:
  282. if (storage != stack)
  283. OPENSSL_free(storage);
  284. return toret;
  285. }
  286. struct ex_callback_entry {
  287. const EX_CALLBACK *excb;
  288. int index;
  289. };
  290. static int ex_callback_compare(const void *a, const void *b)
  291. {
  292. const struct ex_callback_entry *ap = (const struct ex_callback_entry *)a;
  293. const struct ex_callback_entry *bp = (const struct ex_callback_entry *)b;
  294. if (ap->excb == bp->excb)
  295. return 0;
  296. if (ap->excb == NULL)
  297. return 1;
  298. if (bp->excb == NULL)
  299. return -1;
  300. if (ap->excb->priority == bp->excb->priority)
  301. return 0;
  302. return ap->excb->priority > bp->excb->priority ? -1 : 1;
  303. }
  304. /*
  305. * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
  306. * each index in the class used by this variable
  307. */
  308. void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
  309. {
  310. int mx, i;
  311. EX_CALLBACKS *ip;
  312. void *ptr;
  313. const EX_CALLBACK *f;
  314. struct ex_callback_entry stack[10];
  315. struct ex_callback_entry *storage = NULL;
  316. OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ad->ctx);
  317. if (global == NULL)
  318. goto err;
  319. ip = get_and_lock(global, class_index);
  320. if (ip == NULL)
  321. goto err;
  322. mx = sk_EX_CALLBACK_num(ip->meth);
  323. if (mx > 0) {
  324. if (mx < (int)OSSL_NELEM(stack))
  325. storage = stack;
  326. else
  327. storage = OPENSSL_malloc(sizeof(*storage) * mx);
  328. if (storage != NULL)
  329. for (i = 0; i < mx; i++) {
  330. storage[i].excb = sk_EX_CALLBACK_value(ip->meth, i);
  331. storage[i].index = i;
  332. }
  333. }
  334. CRYPTO_THREAD_unlock(global->ex_data_lock);
  335. if (storage != NULL) {
  336. /* Sort according to priority. High priority first */
  337. qsort(storage, mx, sizeof(*storage), ex_callback_compare);
  338. for (i = 0; i < mx; i++) {
  339. f = storage[i].excb;
  340. if (f != NULL && f->free_func != NULL) {
  341. ptr = CRYPTO_get_ex_data(ad, storage[i].index);
  342. f->free_func(obj, ptr, ad, storage[i].index, f->argl, f->argp);
  343. }
  344. }
  345. }
  346. if (storage != stack)
  347. OPENSSL_free(storage);
  348. err:
  349. sk_void_free(ad->sk);
  350. ad->sk = NULL;
  351. ad->ctx = NULL;
  352. }
  353. /*
  354. * Allocate a given CRYPTO_EX_DATA item using the class specific allocation
  355. * function
  356. */
  357. int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad,
  358. int idx)
  359. {
  360. void *curval;
  361. curval = CRYPTO_get_ex_data(ad, idx);
  362. /* Already there, no need to allocate */
  363. if (curval != NULL)
  364. return 1;
  365. return ossl_crypto_alloc_ex_data_intern(class_index, obj, ad, idx);
  366. }
  367. int ossl_crypto_alloc_ex_data_intern(int class_index, void *obj,
  368. CRYPTO_EX_DATA *ad, int idx)
  369. {
  370. EX_CALLBACK *f;
  371. EX_CALLBACKS *ip;
  372. OSSL_EX_DATA_GLOBAL *global;
  373. global = ossl_lib_ctx_get_ex_data_global(ad->ctx);
  374. if (global == NULL)
  375. return 0;
  376. ip = get_and_lock(global, class_index);
  377. if (ip == NULL)
  378. return 0;
  379. f = sk_EX_CALLBACK_value(ip->meth, idx);
  380. CRYPTO_THREAD_unlock(global->ex_data_lock);
  381. /*
  382. * This should end up calling CRYPTO_set_ex_data(), which allocates
  383. * everything necessary to support placing the new data in the right spot.
  384. */
  385. if (f->new_func == NULL)
  386. return 0;
  387. f->new_func(obj, NULL, ad, idx, f->argl, f->argp);
  388. return 1;
  389. }
  390. /*
  391. * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
  392. * particular index in the class used by this variable
  393. */
  394. int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
  395. {
  396. int i;
  397. if (ad->sk == NULL) {
  398. if ((ad->sk = sk_void_new_null()) == NULL) {
  399. ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
  400. return 0;
  401. }
  402. }
  403. for (i = sk_void_num(ad->sk); i <= idx; ++i) {
  404. if (!sk_void_push(ad->sk, NULL)) {
  405. ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB);
  406. return 0;
  407. }
  408. }
  409. if (sk_void_set(ad->sk, idx, val) != val) {
  410. /* Probably the index is out of bounds */
  411. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
  412. return 0;
  413. }
  414. return 1;
  415. }
  416. /*
  417. * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
  418. * particular index in the class used by this variable
  419. */
  420. void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
  421. {
  422. if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
  423. return NULL;
  424. return sk_void_value(ad->sk, idx);
  425. }
  426. OSSL_LIB_CTX *ossl_crypto_ex_data_get_ossl_lib_ctx(const CRYPTO_EX_DATA *ad)
  427. {
  428. return ad->ctx;
  429. }