2
0

ex_data.c 14 KB

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