ex_data.c 14 KB

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