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