threadstest.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. /*
  2. * Copyright 2016-2025 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. /*
  10. * The test_multi_downgrade_shared_pkey function tests the thread safety of a
  11. * deprecated function.
  12. */
  13. #ifndef OPENSSL_NO_DEPRECATED_3_0
  14. # define OPENSSL_SUPPRESS_DEPRECATED
  15. #endif
  16. #if defined(_WIN32)
  17. # include <windows.h>
  18. #endif
  19. #include <string.h>
  20. #include <openssl/crypto.h>
  21. #include <openssl/rsa.h>
  22. #include <openssl/aes.h>
  23. #include <openssl/err.h>
  24. #include <openssl/rand.h>
  25. #include <openssl/pem.h>
  26. #include <openssl/evp.h>
  27. #include "internal/tsan_assist.h"
  28. #include "internal/nelem.h"
  29. #include "internal/time.h"
  30. #include "internal/rcu.h"
  31. #include "testutil.h"
  32. #include "threadstest.h"
  33. #ifdef __SANITIZE_THREAD__
  34. #include <sanitizer/tsan_interface.h>
  35. #define TSAN_ACQUIRE(s) __tsan_acquire(s)
  36. #else
  37. #define TSAN_ACQUIRE(s)
  38. #endif
  39. /* Limit the maximum number of threads */
  40. #define MAXIMUM_THREADS 10
  41. /* Limit the maximum number of providers loaded into a library context */
  42. #define MAXIMUM_PROVIDERS 4
  43. static int do_fips = 0;
  44. static char *privkey;
  45. static char *storedir;
  46. static char *config_file = NULL;
  47. static int multidefault_run = 0;
  48. static const char *default_provider[] = { "default", NULL };
  49. static const char *fips_provider[] = { "fips", NULL };
  50. static const char *fips_and_default_providers[] = { "default", "fips", NULL };
  51. static CRYPTO_RWLOCK *global_lock;
  52. #ifdef TSAN_REQUIRES_LOCKING
  53. static CRYPTO_RWLOCK *tsan_lock;
  54. #endif
  55. /* Grab a globally unique integer value, return 0 on failure */
  56. static int get_new_uid(void)
  57. {
  58. /*
  59. * Start with a nice large number to avoid potential conflicts when
  60. * we generate a new OID.
  61. */
  62. static TSAN_QUALIFIER int current_uid = 1 << (sizeof(int) * 8 - 2);
  63. #ifdef TSAN_REQUIRES_LOCKING
  64. int r;
  65. if (!TEST_true(CRYPTO_THREAD_write_lock(tsan_lock)))
  66. return 0;
  67. r = ++current_uid;
  68. if (!TEST_true(CRYPTO_THREAD_unlock(tsan_lock)))
  69. return 0;
  70. return r;
  71. #else
  72. return tsan_counter(&current_uid);
  73. #endif
  74. }
  75. static int test_lock(void)
  76. {
  77. CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
  78. int res;
  79. if (!TEST_ptr(lock))
  80. return 0;
  81. res = TEST_true(CRYPTO_THREAD_read_lock(lock))
  82. && TEST_true(CRYPTO_THREAD_unlock(lock))
  83. && TEST_true(CRYPTO_THREAD_write_lock(lock))
  84. && TEST_true(CRYPTO_THREAD_unlock(lock));
  85. CRYPTO_THREAD_lock_free(lock);
  86. return res;
  87. }
  88. #if defined(OPENSSL_THREADS)
  89. static int contention = 0;
  90. static int rwwriter1_done = 0;
  91. static int rwwriter2_done = 0;
  92. static int rwreader1_iterations = 0;
  93. static int rwreader2_iterations = 0;
  94. static int rwwriter1_iterations = 0;
  95. static int rwwriter2_iterations = 0;
  96. static int *rwwriter_ptr = NULL;
  97. static int rw_torture_result = 1;
  98. static CRYPTO_RWLOCK *rwtorturelock = NULL;
  99. static CRYPTO_RWLOCK *atomiclock = NULL;
  100. static void rwwriter_fn(int id, int *iterations)
  101. {
  102. int count;
  103. int *old, *new;
  104. OSSL_TIME t1, t2;
  105. t1 = ossl_time_now();
  106. for (count = 0; ; count++) {
  107. new = CRYPTO_zalloc(sizeof (int), NULL, 0);
  108. if (contention == 0)
  109. OSSL_sleep(1000);
  110. if (!CRYPTO_THREAD_write_lock(rwtorturelock))
  111. abort();
  112. if (rwwriter_ptr != NULL) {
  113. *new = *rwwriter_ptr + 1;
  114. } else {
  115. *new = 0;
  116. }
  117. old = rwwriter_ptr;
  118. rwwriter_ptr = new;
  119. if (!CRYPTO_THREAD_unlock(rwtorturelock))
  120. abort();
  121. if (old != NULL)
  122. CRYPTO_free(old, __FILE__, __LINE__);
  123. t2 = ossl_time_now();
  124. if ((ossl_time2seconds(t2) - ossl_time2seconds(t1)) >= 4)
  125. break;
  126. }
  127. *iterations = count;
  128. return;
  129. }
  130. static void rwwriter1_fn(void)
  131. {
  132. int local;
  133. TEST_info("Starting writer1");
  134. rwwriter_fn(1, &rwwriter1_iterations);
  135. CRYPTO_atomic_add(&rwwriter1_done, 1, &local, atomiclock);
  136. }
  137. static void rwwriter2_fn(void)
  138. {
  139. int local;
  140. TEST_info("Starting writer 2");
  141. rwwriter_fn(2, &rwwriter2_iterations);
  142. CRYPTO_atomic_add(&rwwriter2_done, 1, &local, atomiclock);
  143. }
  144. static void rwreader_fn(int *iterations)
  145. {
  146. unsigned int count = 0;
  147. int old = 0;
  148. int lw1 = 0;
  149. int lw2 = 0;
  150. if (CRYPTO_THREAD_read_lock(rwtorturelock) == 0)
  151. abort();
  152. while (lw1 != 1 || lw2 != 1) {
  153. CRYPTO_atomic_add(&rwwriter1_done, 0, &lw1, atomiclock);
  154. CRYPTO_atomic_add(&rwwriter2_done, 0, &lw2, atomiclock);
  155. count++;
  156. if (rwwriter_ptr != NULL) {
  157. if (old > *rwwriter_ptr) {
  158. TEST_info("rwwriter pointer went backwards! %d : %d\n",
  159. old, *rwwriter_ptr);
  160. rw_torture_result = 0;
  161. }
  162. old = *rwwriter_ptr;
  163. }
  164. if (CRYPTO_THREAD_unlock(rwtorturelock) == 0)
  165. abort();
  166. if (rw_torture_result == 0) {
  167. *iterations = count;
  168. return;
  169. }
  170. if (CRYPTO_THREAD_read_lock(rwtorturelock) == 0)
  171. abort();
  172. }
  173. *iterations = count;
  174. if (CRYPTO_THREAD_unlock(rwtorturelock) == 0)
  175. abort();
  176. }
  177. static void rwreader1_fn(void)
  178. {
  179. TEST_info("Starting reader 1");
  180. rwreader_fn(&rwreader1_iterations);
  181. }
  182. static void rwreader2_fn(void)
  183. {
  184. TEST_info("Starting reader 2");
  185. rwreader_fn(&rwreader2_iterations);
  186. }
  187. static thread_t rwwriter1;
  188. static thread_t rwwriter2;
  189. static thread_t rwreader1;
  190. static thread_t rwreader2;
  191. static int _torture_rw(void)
  192. {
  193. double tottime = 0;
  194. int ret = 0;
  195. double avr, avw;
  196. OSSL_TIME t1, t2;
  197. struct timeval dtime;
  198. rwtorturelock = CRYPTO_THREAD_lock_new();
  199. atomiclock = CRYPTO_THREAD_lock_new();
  200. if (!TEST_ptr(rwtorturelock) || !TEST_ptr(atomiclock))
  201. goto out;
  202. rwwriter1_iterations = 0;
  203. rwwriter2_iterations = 0;
  204. rwreader1_iterations = 0;
  205. rwreader2_iterations = 0;
  206. rwwriter1_done = 0;
  207. rwwriter2_done = 0;
  208. rw_torture_result = 1;
  209. memset(&rwwriter1, 0, sizeof(thread_t));
  210. memset(&rwwriter2, 0, sizeof(thread_t));
  211. memset(&rwreader1, 0, sizeof(thread_t));
  212. memset(&rwreader2, 0, sizeof(thread_t));
  213. TEST_info("Staring rw torture");
  214. t1 = ossl_time_now();
  215. if (!TEST_true(run_thread(&rwreader1, rwreader1_fn))
  216. || !TEST_true(run_thread(&rwreader2, rwreader2_fn))
  217. || !TEST_true(run_thread(&rwwriter1, rwwriter1_fn))
  218. || !TEST_true(run_thread(&rwwriter2, rwwriter2_fn))
  219. || !TEST_true(wait_for_thread(rwwriter1))
  220. || !TEST_true(wait_for_thread(rwwriter2))
  221. || !TEST_true(wait_for_thread(rwreader1))
  222. || !TEST_true(wait_for_thread(rwreader2)))
  223. goto out;
  224. t2 = ossl_time_now();
  225. dtime = ossl_time_to_timeval(ossl_time_subtract(t2, t1));
  226. tottime = dtime.tv_sec + (dtime.tv_usec / 1e6);
  227. TEST_info("rw_torture_result is %d\n", rw_torture_result);
  228. TEST_info("performed %d reads and %d writes over 2 read and 2 write threads in %e seconds",
  229. rwreader1_iterations + rwreader2_iterations,
  230. rwwriter1_iterations + rwwriter2_iterations, tottime);
  231. if ((rwreader1_iterations + rwreader2_iterations == 0)
  232. || (rwwriter1_iterations + rwwriter2_iterations == 0)) {
  233. TEST_info("Threads did not iterate\n");
  234. goto out;
  235. }
  236. avr = tottime / (rwreader1_iterations + rwreader2_iterations);
  237. avw = (tottime / (rwwriter1_iterations + rwwriter2_iterations));
  238. TEST_info("Average read time %e/read", avr);
  239. TEST_info("Averate write time %e/write", avw);
  240. if (TEST_int_eq(rw_torture_result, 1))
  241. ret = 1;
  242. out:
  243. CRYPTO_THREAD_lock_free(rwtorturelock);
  244. CRYPTO_THREAD_lock_free(atomiclock);
  245. rwtorturelock = NULL;
  246. return ret;
  247. }
  248. static int torture_rw_low(void)
  249. {
  250. contention = 0;
  251. return _torture_rw();
  252. }
  253. static int torture_rw_high(void)
  254. {
  255. contention = 1;
  256. return _torture_rw();
  257. }
  258. static CRYPTO_RCU_LOCK *rcu_lock = NULL;
  259. static int writer1_done = 0;
  260. static int writer2_done = 0;
  261. static int reader1_iterations = 0;
  262. static int reader2_iterations = 0;
  263. static int writer1_iterations = 0;
  264. static int writer2_iterations = 0;
  265. static uint64_t *writer_ptr = NULL;
  266. static uint64_t global_ctr = 0;
  267. static int rcu_torture_result = 1;
  268. static void free_old_rcu_data(void *data)
  269. {
  270. CRYPTO_free(data, NULL, 0);
  271. }
  272. static void writer_fn(int id, int *iterations)
  273. {
  274. int count;
  275. OSSL_TIME t1, t2;
  276. uint64_t *old, *new;
  277. t1 = ossl_time_now();
  278. for (count = 0; ; count++) {
  279. new = CRYPTO_malloc(sizeof(uint64_t), NULL, 0);
  280. *new = (uint64_t)0xBAD;
  281. if (contention == 0)
  282. OSSL_sleep(1000);
  283. ossl_rcu_write_lock(rcu_lock);
  284. old = ossl_rcu_deref(&writer_ptr);
  285. TSAN_ACQUIRE(&writer_ptr);
  286. *new = global_ctr++;
  287. ossl_rcu_assign_ptr(&writer_ptr, &new);
  288. if (contention == 0)
  289. ossl_rcu_call(rcu_lock, free_old_rcu_data, old);
  290. ossl_rcu_write_unlock(rcu_lock);
  291. if (contention != 0) {
  292. ossl_synchronize_rcu(rcu_lock);
  293. CRYPTO_free(old, NULL, 0);
  294. }
  295. t2 = ossl_time_now();
  296. if ((ossl_time2seconds(t2) - ossl_time2seconds(t1)) >= 4)
  297. break;
  298. }
  299. *iterations = count;
  300. return;
  301. }
  302. static void writer1_fn(void)
  303. {
  304. int local;
  305. TEST_info("Starting writer1");
  306. writer_fn(1, &writer1_iterations);
  307. CRYPTO_atomic_add(&writer1_done, 1, &local, atomiclock);
  308. }
  309. static void writer2_fn(void)
  310. {
  311. int local;
  312. TEST_info("Starting writer2");
  313. writer_fn(2, &writer2_iterations);
  314. CRYPTO_atomic_add(&writer2_done, 1, &local, atomiclock);
  315. }
  316. static void reader_fn(int *iterations)
  317. {
  318. unsigned int count = 0;
  319. uint64_t *valp;
  320. uint64_t val;
  321. uint64_t oldval = 0;
  322. int lw1 = 0;
  323. int lw2 = 0;
  324. while (lw1 != 1 || lw2 != 1) {
  325. CRYPTO_atomic_add(&writer1_done, 0, &lw1, atomiclock);
  326. CRYPTO_atomic_add(&writer2_done, 0, &lw2, atomiclock);
  327. count++;
  328. ossl_rcu_read_lock(rcu_lock);
  329. valp = ossl_rcu_deref(&writer_ptr);
  330. val = (valp == NULL) ? 0 : *valp;
  331. if (oldval > val) {
  332. TEST_info("rcu torture value went backwards! %llu : %llu", (unsigned long long)oldval, (unsigned long long)val);
  333. if (valp == NULL)
  334. TEST_info("ossl_rcu_deref did return NULL!");
  335. rcu_torture_result = 0;
  336. }
  337. oldval = val; /* just try to deref the pointer */
  338. ossl_rcu_read_unlock(rcu_lock);
  339. if (rcu_torture_result == 0) {
  340. *iterations = count;
  341. return;
  342. }
  343. }
  344. *iterations = count;
  345. }
  346. static void reader1_fn(void)
  347. {
  348. TEST_info("Starting reader 1");
  349. reader_fn(&reader1_iterations);
  350. }
  351. static void reader2_fn(void)
  352. {
  353. TEST_info("Starting reader 2");
  354. reader_fn(&reader2_iterations);
  355. }
  356. static thread_t writer1;
  357. static thread_t writer2;
  358. static thread_t reader1;
  359. static thread_t reader2;
  360. static int _torture_rcu(void)
  361. {
  362. OSSL_TIME t1, t2;
  363. struct timeval dtime;
  364. double tottime;
  365. double avr, avw;
  366. int rc = 0;
  367. atomiclock = CRYPTO_THREAD_lock_new();
  368. if (!TEST_ptr(atomiclock))
  369. goto out;
  370. memset(&writer1, 0, sizeof(thread_t));
  371. memset(&writer2, 0, sizeof(thread_t));
  372. memset(&reader1, 0, sizeof(thread_t));
  373. memset(&reader2, 0, sizeof(thread_t));
  374. writer1_iterations = 0;
  375. writer2_iterations = 0;
  376. reader1_iterations = 0;
  377. reader2_iterations = 0;
  378. writer1_done = 0;
  379. writer2_done = 0;
  380. rcu_torture_result = 1;
  381. rcu_lock = ossl_rcu_lock_new(contention == 2 ? 4 : 1, NULL);
  382. if (rcu_lock == NULL)
  383. goto out;
  384. TEST_info("Staring rcu torture");
  385. t1 = ossl_time_now();
  386. if (!TEST_true(run_thread(&reader1, reader1_fn))
  387. || !TEST_true(run_thread(&reader2, reader2_fn))
  388. || !TEST_true(run_thread(&writer1, writer1_fn))
  389. || !TEST_true(run_thread(&writer2, writer2_fn))
  390. || !TEST_true(wait_for_thread(writer1))
  391. || !TEST_true(wait_for_thread(writer2))
  392. || !TEST_true(wait_for_thread(reader1))
  393. || !TEST_true(wait_for_thread(reader2)))
  394. goto out;
  395. t2 = ossl_time_now();
  396. dtime = ossl_time_to_timeval(ossl_time_subtract(t2, t1));
  397. tottime = dtime.tv_sec + (dtime.tv_usec / 1e6);
  398. TEST_info("rcu_torture_result is %d\n", rcu_torture_result);
  399. TEST_info("performed %d reads and %d writes over 2 read and 2 write threads in %e seconds",
  400. reader1_iterations + reader2_iterations,
  401. writer1_iterations + writer2_iterations, tottime);
  402. if ((reader1_iterations + reader2_iterations == 0)
  403. || (writer1_iterations + writer2_iterations == 0)) {
  404. TEST_info("Threads did not iterate\n");
  405. goto out;
  406. }
  407. avr = tottime / (reader1_iterations + reader2_iterations);
  408. avw = tottime / (writer1_iterations + writer2_iterations);
  409. TEST_info("Average read time %e/read", avr);
  410. TEST_info("Average write time %e/write", avw);
  411. if (!TEST_int_eq(rcu_torture_result, 1))
  412. goto out;
  413. rc = 1;
  414. out:
  415. ossl_rcu_lock_free(rcu_lock);
  416. CRYPTO_THREAD_lock_free(atomiclock);
  417. if (!TEST_int_eq(rcu_torture_result, 1))
  418. return 0;
  419. return rc;
  420. }
  421. static int torture_rcu_low(void)
  422. {
  423. contention = 0;
  424. return _torture_rcu();
  425. }
  426. static int torture_rcu_high(void)
  427. {
  428. contention = 1;
  429. return _torture_rcu();
  430. }
  431. static int torture_rcu_high2(void)
  432. {
  433. contention = 2;
  434. return _torture_rcu();
  435. }
  436. #endif
  437. static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
  438. static unsigned once_run_count = 0;
  439. static void once_do_run(void)
  440. {
  441. once_run_count++;
  442. }
  443. static void once_run_thread_cb(void)
  444. {
  445. CRYPTO_THREAD_run_once(&once_run, once_do_run);
  446. }
  447. static int test_once(void)
  448. {
  449. thread_t thread;
  450. if (!TEST_true(run_thread(&thread, once_run_thread_cb))
  451. || !TEST_true(wait_for_thread(thread))
  452. || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
  453. || !TEST_int_eq(once_run_count, 1))
  454. return 0;
  455. return 1;
  456. }
  457. static CRYPTO_THREAD_LOCAL thread_local_key;
  458. static unsigned destructor_run_count = 0;
  459. static int thread_local_thread_cb_ok = 0;
  460. static void thread_local_destructor(void *arg)
  461. {
  462. unsigned *count;
  463. if (arg == NULL)
  464. return;
  465. count = arg;
  466. (*count)++;
  467. }
  468. static void thread_local_thread_cb(void)
  469. {
  470. void *ptr;
  471. ptr = CRYPTO_THREAD_get_local(&thread_local_key);
  472. if (!TEST_ptr_null(ptr)
  473. || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
  474. &destructor_run_count)))
  475. return;
  476. ptr = CRYPTO_THREAD_get_local(&thread_local_key);
  477. if (!TEST_ptr_eq(ptr, &destructor_run_count))
  478. return;
  479. thread_local_thread_cb_ok = 1;
  480. }
  481. static int test_thread_local(void)
  482. {
  483. thread_t thread;
  484. void *ptr = NULL;
  485. if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
  486. thread_local_destructor)))
  487. return 0;
  488. ptr = CRYPTO_THREAD_get_local(&thread_local_key);
  489. if (!TEST_ptr_null(ptr)
  490. || !TEST_true(run_thread(&thread, thread_local_thread_cb))
  491. || !TEST_true(wait_for_thread(thread))
  492. || !TEST_int_eq(thread_local_thread_cb_ok, 1))
  493. return 0;
  494. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  495. ptr = CRYPTO_THREAD_get_local(&thread_local_key);
  496. if (!TEST_ptr_null(ptr))
  497. return 0;
  498. # if !defined(OPENSSL_SYS_WINDOWS)
  499. if (!TEST_int_eq(destructor_run_count, 1))
  500. return 0;
  501. # endif
  502. #endif
  503. if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
  504. return 0;
  505. return 1;
  506. }
  507. /*
  508. * Basic test to ensure that we can repeatedly create and
  509. * destroy local keys without leaking anything
  510. */
  511. static int test_thread_local_multi_key(void)
  512. {
  513. int dummy;
  514. int i;
  515. for (i = 0; i < 1000; i++) {
  516. if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
  517. thread_local_destructor)))
  518. return 0;
  519. if (!TEST_true(CRYPTO_THREAD_set_local(&thread_local_key, &dummy)))
  520. return 0;
  521. if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
  522. return 0;
  523. }
  524. return 1;
  525. }
  526. static int test_atomic(void)
  527. {
  528. int val = 0, ret = 0, testresult = 0;
  529. uint64_t val64 = 1, ret64 = 0;
  530. CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
  531. if (!TEST_ptr(lock))
  532. return 0;
  533. if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) {
  534. /* This succeeds therefore we're on a platform with lockless atomics */
  535. if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
  536. goto err;
  537. } else {
  538. /* This failed therefore we're on a platform without lockless atomics */
  539. if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret))
  540. goto err;
  541. }
  542. val = 0;
  543. ret = 0;
  544. if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock)))
  545. goto err;
  546. if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
  547. goto err;
  548. if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) {
  549. /* This succeeds therefore we're on a platform with lockless atomics */
  550. if (!TEST_uint_eq((unsigned int)val64, 3)
  551. || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
  552. goto err;
  553. } else {
  554. /* This failed therefore we're on a platform without lockless atomics */
  555. if (!TEST_uint_eq((unsigned int)val64, 1)
  556. || !TEST_int_eq((unsigned int)ret64, 0))
  557. goto err;
  558. }
  559. val64 = 1;
  560. ret64 = 0;
  561. if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock)))
  562. goto err;
  563. if (!TEST_uint_eq((unsigned int)val64, 3)
  564. || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
  565. goto err;
  566. ret64 = 0;
  567. if (CRYPTO_atomic_load(&val64, &ret64, NULL)) {
  568. /* This succeeds therefore we're on a platform with lockless atomics */
  569. if (!TEST_uint_eq((unsigned int)val64, 3)
  570. || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
  571. goto err;
  572. } else {
  573. /* This failed therefore we're on a platform without lockless atomics */
  574. if (!TEST_uint_eq((unsigned int)val64, 3)
  575. || !TEST_int_eq((unsigned int)ret64, 0))
  576. goto err;
  577. }
  578. ret64 = 0;
  579. if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock)))
  580. goto err;
  581. if (!TEST_uint_eq((unsigned int)val64, 3)
  582. || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
  583. goto err;
  584. testresult = 1;
  585. err:
  586. CRYPTO_THREAD_lock_free(lock);
  587. return testresult;
  588. }
  589. static OSSL_LIB_CTX *multi_libctx = NULL;
  590. static int multi_success;
  591. static OSSL_PROVIDER *multi_provider[MAXIMUM_PROVIDERS + 1];
  592. static size_t multi_num_threads;
  593. static thread_t multi_threads[MAXIMUM_THREADS];
  594. static void multi_intialise(void)
  595. {
  596. multi_success = 1;
  597. multi_libctx = NULL;
  598. multi_num_threads = 0;
  599. memset(multi_threads, 0, sizeof(multi_threads));
  600. memset(multi_provider, 0, sizeof(multi_provider));
  601. }
  602. static void multi_set_success(int ok)
  603. {
  604. if (CRYPTO_THREAD_write_lock(global_lock) == 0) {
  605. /* not synchronized, but better than not reporting failure */
  606. multi_success = ok;
  607. return;
  608. }
  609. multi_success = ok;
  610. CRYPTO_THREAD_unlock(global_lock);
  611. }
  612. static void thead_teardown_libctx(void)
  613. {
  614. OSSL_PROVIDER **p;
  615. for (p = multi_provider; *p != NULL; p++)
  616. OSSL_PROVIDER_unload(*p);
  617. OSSL_LIB_CTX_free(multi_libctx);
  618. multi_intialise();
  619. }
  620. static int thread_setup_libctx(int libctx, const char *providers[])
  621. {
  622. size_t n;
  623. if (libctx && !TEST_true(test_get_libctx(&multi_libctx, NULL, config_file,
  624. NULL, NULL)))
  625. return 0;
  626. if (providers != NULL)
  627. for (n = 0; providers[n] != NULL; n++)
  628. if (!TEST_size_t_lt(n, MAXIMUM_PROVIDERS)
  629. || !TEST_ptr(multi_provider[n] = OSSL_PROVIDER_load(multi_libctx,
  630. providers[n]))) {
  631. thead_teardown_libctx();
  632. return 0;
  633. }
  634. return 1;
  635. }
  636. static int teardown_threads(void)
  637. {
  638. size_t i;
  639. for (i = 0; i < multi_num_threads; i++)
  640. if (!TEST_true(wait_for_thread(multi_threads[i])))
  641. return 0;
  642. return 1;
  643. }
  644. static int start_threads(size_t n, void (*thread_func)(void))
  645. {
  646. size_t i;
  647. if (!TEST_size_t_le(multi_num_threads + n, MAXIMUM_THREADS))
  648. return 0;
  649. for (i = 0 ; i < n; i++)
  650. if (!TEST_true(run_thread(multi_threads + multi_num_threads++, thread_func)))
  651. return 0;
  652. return 1;
  653. }
  654. /* Template multi-threaded test function */
  655. static int thread_run_test(void (*main_func)(void),
  656. size_t num_threads, void (*thread_func)(void),
  657. int libctx, const char *providers[])
  658. {
  659. int testresult = 0;
  660. multi_intialise();
  661. if (!thread_setup_libctx(libctx, providers)
  662. || !start_threads(num_threads, thread_func))
  663. goto err;
  664. if (main_func != NULL)
  665. main_func();
  666. if (!teardown_threads()
  667. || !TEST_true(multi_success))
  668. goto err;
  669. testresult = 1;
  670. err:
  671. thead_teardown_libctx();
  672. return testresult;
  673. }
  674. static void thread_general_worker(void)
  675. {
  676. EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
  677. EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
  678. EVP_CIPHER_CTX *cipherctx = EVP_CIPHER_CTX_new();
  679. EVP_CIPHER *ciph = EVP_CIPHER_fetch(multi_libctx, "AES-128-CBC", NULL);
  680. const char *message = "Hello World";
  681. size_t messlen = strlen(message);
  682. /* Should be big enough for encryption output too */
  683. unsigned char out[EVP_MAX_MD_SIZE];
  684. const unsigned char key[AES_BLOCK_SIZE] = {
  685. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  686. 0x0c, 0x0d, 0x0e, 0x0f
  687. };
  688. const unsigned char iv[AES_BLOCK_SIZE] = {
  689. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  690. 0x0c, 0x0d, 0x0e, 0x0f
  691. };
  692. unsigned int mdoutl;
  693. int ciphoutl;
  694. EVP_PKEY *pkey = NULL;
  695. int testresult = 0;
  696. int i, isfips;
  697. isfips = OSSL_PROVIDER_available(multi_libctx, "fips");
  698. if (!TEST_ptr(mdctx)
  699. || !TEST_ptr(md)
  700. || !TEST_ptr(cipherctx)
  701. || !TEST_ptr(ciph))
  702. goto err;
  703. /* Do some work */
  704. for (i = 0; i < 5; i++) {
  705. if (!TEST_true(EVP_DigestInit_ex(mdctx, md, NULL))
  706. || !TEST_true(EVP_DigestUpdate(mdctx, message, messlen))
  707. || !TEST_true(EVP_DigestFinal(mdctx, out, &mdoutl)))
  708. goto err;
  709. }
  710. for (i = 0; i < 5; i++) {
  711. if (!TEST_true(EVP_EncryptInit_ex(cipherctx, ciph, NULL, key, iv))
  712. || !TEST_true(EVP_EncryptUpdate(cipherctx, out, &ciphoutl,
  713. (unsigned char *)message,
  714. messlen))
  715. || !TEST_true(EVP_EncryptFinal(cipherctx, out, &ciphoutl)))
  716. goto err;
  717. }
  718. /*
  719. * We want the test to run quickly - not securely.
  720. * Therefore we use an insecure bit length where we can (512).
  721. * In the FIPS module though we must use a longer length.
  722. */
  723. pkey = EVP_PKEY_Q_keygen(multi_libctx, NULL, "RSA", (size_t)(isfips ? 2048 : 512));
  724. if (!TEST_ptr(pkey))
  725. goto err;
  726. testresult = 1;
  727. err:
  728. EVP_MD_CTX_free(mdctx);
  729. EVP_MD_free(md);
  730. EVP_CIPHER_CTX_free(cipherctx);
  731. EVP_CIPHER_free(ciph);
  732. EVP_PKEY_free(pkey);
  733. if (!testresult)
  734. multi_set_success(0);
  735. }
  736. static void thread_multi_simple_fetch(void)
  737. {
  738. EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
  739. if (md != NULL)
  740. EVP_MD_free(md);
  741. else
  742. multi_set_success(0);
  743. }
  744. static EVP_PKEY *shared_evp_pkey = NULL;
  745. static void thread_shared_evp_pkey(void)
  746. {
  747. char *msg = "Hello World";
  748. unsigned char ctbuf[256];
  749. unsigned char ptbuf[256];
  750. size_t ptlen, ctlen = sizeof(ctbuf);
  751. EVP_PKEY_CTX *ctx = NULL;
  752. int success = 0;
  753. int i;
  754. for (i = 0; i < 1 + do_fips; i++) {
  755. if (i > 0)
  756. EVP_PKEY_CTX_free(ctx);
  757. ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey,
  758. i == 0 ? "provider=default"
  759. : "provider=fips");
  760. if (!TEST_ptr(ctx))
  761. goto err;
  762. if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx), 0)
  763. || !TEST_int_ge(EVP_PKEY_encrypt(ctx, ctbuf, &ctlen,
  764. (unsigned char *)msg, strlen(msg)),
  765. 0))
  766. goto err;
  767. EVP_PKEY_CTX_free(ctx);
  768. ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey, NULL);
  769. if (!TEST_ptr(ctx))
  770. goto err;
  771. ptlen = sizeof(ptbuf);
  772. if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx), 0)
  773. || !TEST_int_gt(EVP_PKEY_decrypt(ctx, ptbuf, &ptlen, ctbuf, ctlen),
  774. 0)
  775. || !TEST_mem_eq(msg, strlen(msg), ptbuf, ptlen))
  776. goto err;
  777. }
  778. success = 1;
  779. err:
  780. EVP_PKEY_CTX_free(ctx);
  781. if (!success)
  782. multi_set_success(0);
  783. }
  784. static void thread_provider_load_unload(void)
  785. {
  786. OSSL_PROVIDER *deflt = OSSL_PROVIDER_load(multi_libctx, "default");
  787. if (!TEST_ptr(deflt)
  788. || !TEST_true(OSSL_PROVIDER_available(multi_libctx, "default")))
  789. multi_set_success(0);
  790. OSSL_PROVIDER_unload(deflt);
  791. }
  792. static int test_multi_general_worker_default_provider(void)
  793. {
  794. return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
  795. 1, default_provider);
  796. }
  797. static int test_multi_general_worker_fips_provider(void)
  798. {
  799. if (!do_fips)
  800. return TEST_skip("FIPS not supported");
  801. return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
  802. 1, fips_provider);
  803. }
  804. static int test_multi_fetch_worker(void)
  805. {
  806. return thread_run_test(&thread_multi_simple_fetch,
  807. 2, &thread_multi_simple_fetch, 1, default_provider);
  808. }
  809. static int test_multi_shared_pkey_common(void (*worker)(void))
  810. {
  811. int testresult = 0;
  812. multi_intialise();
  813. if (!thread_setup_libctx(1, do_fips ? fips_and_default_providers
  814. : default_provider)
  815. || !TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx))
  816. || !start_threads(1, &thread_shared_evp_pkey)
  817. || !start_threads(1, worker))
  818. goto err;
  819. thread_shared_evp_pkey();
  820. if (!teardown_threads()
  821. || !TEST_true(multi_success))
  822. goto err;
  823. testresult = 1;
  824. err:
  825. EVP_PKEY_free(shared_evp_pkey);
  826. thead_teardown_libctx();
  827. return testresult;
  828. }
  829. #ifndef OPENSSL_NO_DEPRECATED_3_0
  830. static void thread_downgrade_shared_evp_pkey(void)
  831. {
  832. /*
  833. * This test is only relevant for deprecated functions that perform
  834. * downgrading
  835. */
  836. if (EVP_PKEY_get0_RSA(shared_evp_pkey) == NULL)
  837. multi_set_success(0);
  838. }
  839. static int test_multi_downgrade_shared_pkey(void)
  840. {
  841. return test_multi_shared_pkey_common(&thread_downgrade_shared_evp_pkey);
  842. }
  843. #endif
  844. static int test_multi_shared_pkey(void)
  845. {
  846. return test_multi_shared_pkey_common(&thread_shared_evp_pkey);
  847. }
  848. static void thread_release_shared_pkey(void)
  849. {
  850. OSSL_sleep(0);
  851. EVP_PKEY_free(shared_evp_pkey);
  852. }
  853. static int test_multi_shared_pkey_release(void)
  854. {
  855. int testresult = 0;
  856. size_t i = 1;
  857. multi_intialise();
  858. shared_evp_pkey = NULL;
  859. if (!thread_setup_libctx(1, do_fips ? fips_and_default_providers
  860. : default_provider)
  861. || !TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx)))
  862. goto err;
  863. for (; i < 10; ++i) {
  864. if (!TEST_true(EVP_PKEY_up_ref(shared_evp_pkey)))
  865. goto err;
  866. }
  867. if (!start_threads(10, &thread_release_shared_pkey))
  868. goto err;
  869. i = 0;
  870. if (!teardown_threads()
  871. || !TEST_true(multi_success))
  872. goto err;
  873. testresult = 1;
  874. err:
  875. while (i > 0) {
  876. EVP_PKEY_free(shared_evp_pkey);
  877. --i;
  878. }
  879. thead_teardown_libctx();
  880. return testresult;
  881. }
  882. static int test_multi_load_unload_provider(void)
  883. {
  884. EVP_MD *sha256 = NULL;
  885. OSSL_PROVIDER *prov = NULL;
  886. int testresult = 0;
  887. multi_intialise();
  888. if (!thread_setup_libctx(1, NULL)
  889. || !TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, "default"))
  890. || !TEST_ptr(sha256 = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL))
  891. || !TEST_true(OSSL_PROVIDER_unload(prov)))
  892. goto err;
  893. prov = NULL;
  894. if (!start_threads(2, &thread_provider_load_unload))
  895. goto err;
  896. thread_provider_load_unload();
  897. if (!teardown_threads()
  898. || !TEST_true(multi_success))
  899. goto err;
  900. testresult = 1;
  901. err:
  902. OSSL_PROVIDER_unload(prov);
  903. EVP_MD_free(sha256);
  904. thead_teardown_libctx();
  905. return testresult;
  906. }
  907. static char *multi_load_provider = "legacy";
  908. /*
  909. * This test attempts to load several providers at the same time, and if
  910. * run with a thread sanitizer, should crash if the core provider code
  911. * doesn't synchronize well enough.
  912. */
  913. static void test_multi_load_worker(void)
  914. {
  915. OSSL_PROVIDER *prov;
  916. if (!TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, multi_load_provider))
  917. || !TEST_true(OSSL_PROVIDER_unload(prov)))
  918. multi_set_success(0);
  919. }
  920. static int test_multi_default(void)
  921. {
  922. /* Avoid running this test twice */
  923. if (multidefault_run) {
  924. TEST_skip("multi default test already run");
  925. return 1;
  926. }
  927. multidefault_run = 1;
  928. return thread_run_test(&thread_multi_simple_fetch,
  929. 2, &thread_multi_simple_fetch, 0, NULL);
  930. }
  931. static int test_multi_load(void)
  932. {
  933. int res = 1;
  934. OSSL_PROVIDER *prov;
  935. /* The multidefault test must run prior to this test */
  936. if (!multidefault_run) {
  937. TEST_info("Running multi default test first");
  938. res = test_multi_default();
  939. }
  940. /*
  941. * We use the legacy provider in test_multi_load_worker because it uses a
  942. * child libctx that might hit more codepaths that might be sensitive to
  943. * threading issues. But in a no-legacy build that won't be loadable so
  944. * we use the default provider instead.
  945. */
  946. prov = OSSL_PROVIDER_load(NULL, "legacy");
  947. if (prov == NULL) {
  948. TEST_info("Cannot load legacy provider - assuming this is a no-legacy build");
  949. multi_load_provider = "default";
  950. }
  951. OSSL_PROVIDER_unload(prov);
  952. return thread_run_test(NULL, MAXIMUM_THREADS, &test_multi_load_worker, 0,
  953. NULL) && res;
  954. }
  955. static void test_obj_create_one(void)
  956. {
  957. char tids[12], oid[40], sn[30], ln[30];
  958. int id = get_new_uid();
  959. BIO_snprintf(tids, sizeof(tids), "%d", id);
  960. BIO_snprintf(oid, sizeof(oid), "1.3.6.1.4.1.16604.%s", tids);
  961. BIO_snprintf(sn, sizeof(sn), "short-name-%s", tids);
  962. BIO_snprintf(ln, sizeof(ln), "long-name-%s", tids);
  963. if (!TEST_int_ne(id, 0)
  964. || !TEST_true(id = OBJ_create(oid, sn, ln))
  965. || !TEST_true(OBJ_add_sigid(id, NID_sha3_256, NID_rsa)))
  966. multi_set_success(0);
  967. }
  968. static int test_obj_add(void)
  969. {
  970. return thread_run_test(&test_obj_create_one,
  971. MAXIMUM_THREADS, &test_obj_create_one,
  972. 1, default_provider);
  973. }
  974. static void test_lib_ctx_load_config_worker(void)
  975. {
  976. if (!TEST_int_eq(OSSL_LIB_CTX_load_config(multi_libctx, config_file), 1))
  977. multi_set_success(0);
  978. }
  979. static int test_lib_ctx_load_config(void)
  980. {
  981. return thread_run_test(&test_lib_ctx_load_config_worker,
  982. MAXIMUM_THREADS, &test_lib_ctx_load_config_worker,
  983. 1, default_provider);
  984. }
  985. #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
  986. static BIO *multi_bio1, *multi_bio2;
  987. static void test_bio_dgram_pair_worker(void)
  988. {
  989. ossl_unused int r;
  990. int ok = 0;
  991. uint8_t ch = 0;
  992. uint8_t scratch[64];
  993. BIO_MSG msg = {0};
  994. size_t num_processed = 0;
  995. if (!TEST_int_eq(RAND_bytes_ex(multi_libctx, &ch, 1, 64), 1))
  996. goto err;
  997. msg.data = scratch;
  998. msg.data_len = sizeof(scratch);
  999. /*
  1000. * We do not test for failure here as recvmmsg may fail if no sendmmsg
  1001. * has been called yet. The purpose of this code is to exercise tsan.
  1002. */
  1003. if (ch & 2)
  1004. r = BIO_sendmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg,
  1005. sizeof(BIO_MSG), 1, 0, &num_processed);
  1006. else
  1007. r = BIO_recvmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg,
  1008. sizeof(BIO_MSG), 1, 0, &num_processed);
  1009. ok = 1;
  1010. err:
  1011. if (ok == 0)
  1012. multi_set_success(0);
  1013. }
  1014. static int test_bio_dgram_pair(void)
  1015. {
  1016. int r;
  1017. BIO *bio1 = NULL, *bio2 = NULL;
  1018. r = BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0);
  1019. if (!TEST_int_eq(r, 1))
  1020. goto err;
  1021. multi_bio1 = bio1;
  1022. multi_bio2 = bio2;
  1023. r = thread_run_test(&test_bio_dgram_pair_worker,
  1024. MAXIMUM_THREADS, &test_bio_dgram_pair_worker,
  1025. 1, default_provider);
  1026. err:
  1027. BIO_free(bio1);
  1028. BIO_free(bio2);
  1029. return r;
  1030. }
  1031. #endif
  1032. static const char *pemdataraw[] = {
  1033. "-----BEGIN RSA PRIVATE KEY-----\n",
  1034. "MIIBOgIBAAJBAMFcGsaxxdgiuuGmCkVImy4h99CqT7jwY3pexPGcnUFtR2Fh36Bp\n",
  1035. "oncwtkZ4cAgtvd4Qs8PkxUdp6p/DlUmObdkCAwEAAQJAUR44xX6zB3eaeyvTRzms\n",
  1036. "kHADrPCmPWnr8dxsNwiDGHzrMKLN+i/HAam+97HxIKVWNDH2ba9Mf1SA8xu9dcHZ\n",
  1037. "AQIhAOHPCLxbtQFVxlnhSyxYeb7O323c3QulPNn3bhOipElpAiEA2zZpBE8ZXVnL\n",
  1038. "74QjG4zINlDfH+EOEtjJJ3RtaYDugvECIBtsQDxXytChsRgDQ1TcXdStXPcDppie\n",
  1039. "dZhm8yhRTTBZAiAZjE/U9rsIDC0ebxIAZfn3iplWh84yGB3pgUI3J5WkoQIhAInE\n",
  1040. "HTUY5WRj5riZtkyGnbm3DvF+1eMtO2lYV+OuLcfE\n",
  1041. "-----END RSA PRIVATE KEY-----\n",
  1042. NULL
  1043. };
  1044. static void test_pem_read_one(void)
  1045. {
  1046. EVP_PKEY *key = NULL;
  1047. BIO *pem = NULL;
  1048. char *pemdata;
  1049. size_t len;
  1050. pemdata = glue_strings(pemdataraw, &len);
  1051. if (pemdata == NULL) {
  1052. multi_set_success(0);
  1053. goto err;
  1054. }
  1055. pem = BIO_new_mem_buf(pemdata, len);
  1056. if (pem == NULL) {
  1057. multi_set_success(0);
  1058. goto err;
  1059. }
  1060. key = PEM_read_bio_PrivateKey(pem, NULL, NULL, NULL);
  1061. if (key == NULL)
  1062. multi_set_success(0);
  1063. err:
  1064. EVP_PKEY_free(key);
  1065. BIO_free(pem);
  1066. OPENSSL_free(pemdata);
  1067. }
  1068. /* Test reading PEM files in multiple threads */
  1069. static int test_pem_read(void)
  1070. {
  1071. return thread_run_test(&test_pem_read_one, MAXIMUM_THREADS,
  1072. &test_pem_read_one, 1, default_provider);
  1073. }
  1074. static X509_STORE *store = NULL;
  1075. static void test_x509_store_by_subject(void)
  1076. {
  1077. X509_STORE_CTX *ctx;
  1078. X509_OBJECT *obj = NULL;
  1079. X509_NAME *name = NULL;
  1080. int success = 0;
  1081. ctx = X509_STORE_CTX_new();
  1082. if (!TEST_ptr(ctx))
  1083. goto err;
  1084. if (!TEST_true(X509_STORE_CTX_init(ctx, store, NULL, NULL)))
  1085. goto err;
  1086. name = X509_NAME_new();
  1087. if (!TEST_ptr(name))
  1088. goto err;
  1089. if (!TEST_true(X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
  1090. (unsigned char *)"Root CA",
  1091. -1, -1, 0)))
  1092. goto err;
  1093. obj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509, name);
  1094. if (!TEST_ptr(obj))
  1095. goto err;
  1096. success = 1;
  1097. err:
  1098. X509_OBJECT_free(obj);
  1099. X509_STORE_CTX_free(ctx);
  1100. X509_NAME_free(name);
  1101. if (!success)
  1102. multi_set_success(0);
  1103. }
  1104. /* Test accessing an X509_STORE from multiple threads */
  1105. static int test_x509_store(void)
  1106. {
  1107. int ret = 0;
  1108. store = X509_STORE_new();
  1109. if (!TEST_ptr(store))
  1110. return 0;
  1111. if (!TEST_true(X509_STORE_load_store(store, storedir)))
  1112. goto err;
  1113. ret = thread_run_test(&test_x509_store_by_subject, MAXIMUM_THREADS,
  1114. &test_x509_store_by_subject, 0, NULL);
  1115. err:
  1116. X509_STORE_free(store);
  1117. store = NULL;
  1118. return ret;
  1119. }
  1120. typedef enum OPTION_choice {
  1121. OPT_ERR = -1,
  1122. OPT_EOF = 0,
  1123. OPT_FIPS, OPT_CONFIG_FILE,
  1124. OPT_TEST_ENUM
  1125. } OPTION_CHOICE;
  1126. const OPTIONS *test_get_options(void)
  1127. {
  1128. static const OPTIONS options[] = {
  1129. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  1130. { "fips", OPT_FIPS, '-', "Test the FIPS provider" },
  1131. { "config", OPT_CONFIG_FILE, '<',
  1132. "The configuration file to use for the libctx" },
  1133. { NULL }
  1134. };
  1135. return options;
  1136. }
  1137. int setup_tests(void)
  1138. {
  1139. OPTION_CHOICE o;
  1140. char *datadir;
  1141. while ((o = opt_next()) != OPT_EOF) {
  1142. switch (o) {
  1143. case OPT_FIPS:
  1144. do_fips = 1;
  1145. break;
  1146. case OPT_CONFIG_FILE:
  1147. config_file = opt_arg();
  1148. break;
  1149. case OPT_TEST_CASES:
  1150. break;
  1151. default:
  1152. return 0;
  1153. }
  1154. }
  1155. if (!TEST_ptr(datadir = test_get_argument(0)))
  1156. return 0;
  1157. privkey = test_mk_file_path(datadir, "rsakey.pem");
  1158. if (!TEST_ptr(privkey))
  1159. return 0;
  1160. storedir = test_mk_file_path(datadir, "store");
  1161. if (!TEST_ptr(storedir))
  1162. return 0;
  1163. if (!TEST_ptr(global_lock = CRYPTO_THREAD_lock_new()))
  1164. return 0;
  1165. #ifdef TSAN_REQUIRES_LOCKING
  1166. if (!TEST_ptr(tsan_lock = CRYPTO_THREAD_lock_new()))
  1167. return 0;
  1168. #endif
  1169. /* Keep first to validate auto creation of default library context */
  1170. ADD_TEST(test_multi_default);
  1171. ADD_TEST(test_lock);
  1172. #if defined(OPENSSL_THREADS)
  1173. ADD_TEST(torture_rw_low);
  1174. ADD_TEST(torture_rw_high);
  1175. ADD_TEST(torture_rcu_low);
  1176. ADD_TEST(torture_rcu_high);
  1177. ADD_TEST(torture_rcu_high2);
  1178. #endif
  1179. ADD_TEST(test_once);
  1180. ADD_TEST(test_thread_local);
  1181. ADD_TEST(test_thread_local_multi_key);
  1182. ADD_TEST(test_atomic);
  1183. ADD_TEST(test_multi_load);
  1184. ADD_TEST(test_multi_general_worker_default_provider);
  1185. ADD_TEST(test_multi_general_worker_fips_provider);
  1186. ADD_TEST(test_multi_fetch_worker);
  1187. ADD_TEST(test_multi_shared_pkey);
  1188. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1189. ADD_TEST(test_multi_downgrade_shared_pkey);
  1190. #endif
  1191. ADD_TEST(test_multi_shared_pkey_release);
  1192. ADD_TEST(test_multi_load_unload_provider);
  1193. ADD_TEST(test_obj_add);
  1194. ADD_TEST(test_lib_ctx_load_config);
  1195. #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
  1196. ADD_TEST(test_bio_dgram_pair);
  1197. #endif
  1198. ADD_TEST(test_pem_read);
  1199. ADD_TEST(test_x509_store);
  1200. return 1;
  1201. }
  1202. void cleanup_tests(void)
  1203. {
  1204. OPENSSL_free(privkey);
  1205. OPENSSL_free(storedir);
  1206. #ifdef TSAN_REQUIRES_LOCKING
  1207. CRYPTO_THREAD_lock_free(tsan_lock);
  1208. #endif
  1209. CRYPTO_THREAD_lock_free(global_lock);
  1210. }