2 * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
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
11 * The test_multi_downgrade_shared_pkey function tests the thread safety of a
12 * deprecated function.
14 #ifndef OPENSSL_NO_DEPRECATED_3_0
15 # define OPENSSL_SUPPRESS_DEPRECATED
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 "internal/tsan_assist.h"
29 #include "internal/nelem.h"
31 #include "threadstest.h"
33 /* Limit the maximum number of threads */
34 #define MAXIMUM_THREADS 10
36 /* Limit the maximum number of providers loaded into a library context */
37 #define MAXIMUM_PROVIDERS 4
39 static int do_fips = 0;
41 static char *config_file = NULL;
42 static int multidefault_run = 0;
44 static const char *default_provider[] = { "default", NULL };
45 static const char *fips_provider[] = { "fips", NULL };
46 static const char *fips_and_default_providers[] = { "default", "fips", NULL };
48 static CRYPTO_RWLOCK *global_lock;
50 #ifdef TSAN_REQUIRES_LOCKING
51 static CRYPTO_RWLOCK *tsan_lock;
54 /* Grab a globally unique integer value, return 0 on failure */
55 static int get_new_uid(void)
58 * Start with a nice large number to avoid potential conflicts when
59 * we generate a new OID.
61 static TSAN_QUALIFIER int current_uid = 1 << (sizeof(int) * 8 - 2);
62 #ifdef TSAN_REQUIRES_LOCKING
65 if (!TEST_true(CRYPTO_THREAD_write_lock(tsan_lock)))
68 if (!TEST_true(CRYPTO_THREAD_unlock(tsan_lock)))
73 return tsan_counter(¤t_uid);
77 static int test_lock(void)
79 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
82 res = TEST_true(CRYPTO_THREAD_read_lock(lock))
83 && TEST_true(CRYPTO_THREAD_unlock(lock))
84 && TEST_true(CRYPTO_THREAD_write_lock(lock))
85 && TEST_true(CRYPTO_THREAD_unlock(lock));
87 CRYPTO_THREAD_lock_free(lock);
92 static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
93 static unsigned once_run_count = 0;
95 static void once_do_run(void)
100 static void once_run_thread_cb(void)
102 CRYPTO_THREAD_run_once(&once_run, once_do_run);
105 static int test_once(void)
109 if (!TEST_true(run_thread(&thread, once_run_thread_cb))
110 || !TEST_true(wait_for_thread(thread))
111 || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
112 || !TEST_int_eq(once_run_count, 1))
117 static CRYPTO_THREAD_LOCAL thread_local_key;
118 static unsigned destructor_run_count = 0;
119 static int thread_local_thread_cb_ok = 0;
121 static void thread_local_destructor(void *arg)
133 static void thread_local_thread_cb(void)
137 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
138 if (!TEST_ptr_null(ptr)
139 || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
140 &destructor_run_count)))
143 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
144 if (!TEST_ptr_eq(ptr, &destructor_run_count))
147 thread_local_thread_cb_ok = 1;
150 static int test_thread_local(void)
155 if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
156 thread_local_destructor)))
159 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
160 if (!TEST_ptr_null(ptr)
161 || !TEST_true(run_thread(&thread, thread_local_thread_cb))
162 || !TEST_true(wait_for_thread(thread))
163 || !TEST_int_eq(thread_local_thread_cb_ok, 1))
166 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
168 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
169 if (!TEST_ptr_null(ptr))
172 # if !defined(OPENSSL_SYS_WINDOWS)
173 if (!TEST_int_eq(destructor_run_count, 1))
178 if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
183 static int test_atomic(void)
185 int val = 0, ret = 0, testresult = 0;
186 uint64_t val64 = 1, ret64 = 0;
187 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
192 if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) {
193 /* This succeeds therefore we're on a platform with lockless atomics */
194 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
197 /* This failed therefore we're on a platform without lockless atomics */
198 if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret))
204 if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock)))
206 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
209 if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) {
210 /* This succeeds therefore we're on a platform with lockless atomics */
211 if (!TEST_uint_eq((unsigned int)val64, 3)
212 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
215 /* This failed therefore we're on a platform without lockless atomics */
216 if (!TEST_uint_eq((unsigned int)val64, 1)
217 || !TEST_int_eq((unsigned int)ret64, 0))
223 if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock)))
226 if (!TEST_uint_eq((unsigned int)val64, 3)
227 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
231 if (CRYPTO_atomic_load(&val64, &ret64, NULL)) {
232 /* This succeeds therefore we're on a platform with lockless atomics */
233 if (!TEST_uint_eq((unsigned int)val64, 3)
234 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
237 /* This failed therefore we're on a platform without lockless atomics */
238 if (!TEST_uint_eq((unsigned int)val64, 3)
239 || !TEST_int_eq((unsigned int)ret64, 0))
244 if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock)))
247 if (!TEST_uint_eq((unsigned int)val64, 3)
248 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
253 CRYPTO_THREAD_lock_free(lock);
257 static OSSL_LIB_CTX *multi_libctx = NULL;
258 static int multi_success;
259 static OSSL_PROVIDER *multi_provider[MAXIMUM_PROVIDERS + 1];
260 static size_t multi_num_threads;
261 static thread_t multi_threads[MAXIMUM_THREADS];
263 static void multi_intialise(void)
267 multi_num_threads = 0;
268 memset(multi_threads, 0, sizeof(multi_threads));
269 memset(multi_provider, 0, sizeof(multi_provider));
272 static void multi_set_success(int ok)
274 if (CRYPTO_THREAD_write_lock(global_lock) == 0) {
275 /* not synchronized, but better than not reporting failure */
282 CRYPTO_THREAD_unlock(global_lock);
285 static void thead_teardown_libctx(void)
289 for (p = multi_provider; *p != NULL; p++)
290 OSSL_PROVIDER_unload(*p);
291 OSSL_LIB_CTX_free(multi_libctx);
295 static int thread_setup_libctx(int libctx, const char *providers[])
299 if (libctx && !TEST_true(test_get_libctx(&multi_libctx, NULL, config_file,
303 if (providers != NULL)
304 for (n = 0; providers[n] != NULL; n++)
305 if (!TEST_size_t_lt(n, MAXIMUM_PROVIDERS)
306 || !TEST_ptr(multi_provider[n] = OSSL_PROVIDER_load(multi_libctx,
308 thead_teardown_libctx();
314 static int teardown_threads(void)
318 for (i = 0; i < multi_num_threads; i++)
319 if (!TEST_true(wait_for_thread(multi_threads[i])))
324 static int start_threads(size_t n, void (*thread_func)(void))
328 if (!TEST_size_t_le(multi_num_threads + n, MAXIMUM_THREADS))
331 for (i = 0 ; i < n; i++)
332 if (!TEST_true(run_thread(multi_threads + multi_num_threads++, thread_func)))
337 /* Template multi-threaded test function */
338 static int thread_run_test(void (*main_func)(void),
339 size_t num_threads, void (*thread_func)(void),
340 int libctx, const char *providers[])
345 if (!thread_setup_libctx(libctx, providers)
346 || !start_threads(num_threads, thread_func))
349 if (main_func != NULL)
352 if (!teardown_threads()
353 || !TEST_true(multi_success))
357 thead_teardown_libctx();
361 static void thread_general_worker(void)
363 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
364 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
365 EVP_CIPHER_CTX *cipherctx = EVP_CIPHER_CTX_new();
366 EVP_CIPHER *ciph = EVP_CIPHER_fetch(multi_libctx, "AES-128-CBC", NULL);
367 const char *message = "Hello World";
368 size_t messlen = strlen(message);
369 /* Should be big enough for encryption output too */
370 unsigned char out[EVP_MAX_MD_SIZE];
371 const unsigned char key[AES_BLOCK_SIZE] = {
372 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
373 0x0c, 0x0d, 0x0e, 0x0f
375 const unsigned char iv[AES_BLOCK_SIZE] = {
376 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
377 0x0c, 0x0d, 0x0e, 0x0f
381 EVP_PKEY *pkey = NULL;
385 isfips = OSSL_PROVIDER_available(multi_libctx, "fips");
389 || !TEST_ptr(cipherctx)
394 for (i = 0; i < 5; i++) {
395 if (!TEST_true(EVP_DigestInit_ex(mdctx, md, NULL))
396 || !TEST_true(EVP_DigestUpdate(mdctx, message, messlen))
397 || !TEST_true(EVP_DigestFinal(mdctx, out, &mdoutl)))
400 for (i = 0; i < 5; i++) {
401 if (!TEST_true(EVP_EncryptInit_ex(cipherctx, ciph, NULL, key, iv))
402 || !TEST_true(EVP_EncryptUpdate(cipherctx, out, &ciphoutl,
403 (unsigned char *)message,
405 || !TEST_true(EVP_EncryptFinal(cipherctx, out, &ciphoutl)))
410 * We want the test to run quickly - not securely.
411 * Therefore we use an insecure bit length where we can (512).
412 * In the FIPS module though we must use a longer length.
414 pkey = EVP_PKEY_Q_keygen(multi_libctx, NULL, "RSA", isfips ? 2048 : 512);
420 EVP_MD_CTX_free(mdctx);
422 EVP_CIPHER_CTX_free(cipherctx);
423 EVP_CIPHER_free(ciph);
426 multi_set_success(0);
429 static void thread_multi_simple_fetch(void)
431 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
436 multi_set_success(0);
439 static EVP_PKEY *shared_evp_pkey = NULL;
441 static void thread_shared_evp_pkey(void)
443 char *msg = "Hello World";
444 unsigned char ctbuf[256];
445 unsigned char ptbuf[256];
446 size_t ptlen, ctlen = sizeof(ctbuf);
447 EVP_PKEY_CTX *ctx = NULL;
451 for (i = 0; i < 1 + do_fips; i++) {
453 EVP_PKEY_CTX_free(ctx);
454 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey,
455 i == 0 ? "provider=default"
460 if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx), 0)
461 || !TEST_int_ge(EVP_PKEY_encrypt(ctx, ctbuf, &ctlen,
462 (unsigned char *)msg, strlen(msg)),
466 EVP_PKEY_CTX_free(ctx);
467 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey, NULL);
472 ptlen = sizeof(ptbuf);
473 if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx), 0)
474 || !TEST_int_gt(EVP_PKEY_decrypt(ctx, ptbuf, &ptlen, ctbuf, ctlen),
476 || !TEST_mem_eq(msg, strlen(msg), ptbuf, ptlen))
483 EVP_PKEY_CTX_free(ctx);
485 multi_set_success(0);
488 static void thread_provider_load_unload(void)
490 OSSL_PROVIDER *deflt = OSSL_PROVIDER_load(multi_libctx, "default");
493 || !TEST_true(OSSL_PROVIDER_available(multi_libctx, "default")))
494 multi_set_success(0);
496 OSSL_PROVIDER_unload(deflt);
499 static int test_multi_general_worker_default_provider(void)
501 return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
502 1, default_provider);
505 static int test_multi_general_worker_fips_provider(void)
508 return TEST_skip("FIPS not supported");
509 return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
513 static int test_multi_fetch_worker(void)
515 return thread_run_test(&thread_multi_simple_fetch,
516 2, &thread_multi_simple_fetch, 1, default_provider);
519 static int test_multi_shared_pkey_common(void (*worker)(void))
524 if (!thread_setup_libctx(1, do_fips ? fips_and_default_providers
526 || !TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx))
527 || !start_threads(1, &thread_shared_evp_pkey)
528 || !start_threads(1, worker))
531 thread_shared_evp_pkey();
533 if (!teardown_threads()
534 || !TEST_true(multi_success))
538 EVP_PKEY_free(shared_evp_pkey);
539 thead_teardown_libctx();
543 #ifndef OPENSSL_NO_DEPRECATED_3_0
544 static void thread_downgrade_shared_evp_pkey(void)
547 * This test is only relevant for deprecated functions that perform
550 if (EVP_PKEY_get0_RSA(shared_evp_pkey) == NULL)
551 multi_set_success(0);
554 static int test_multi_downgrade_shared_pkey(void)
556 return test_multi_shared_pkey_common(&thread_downgrade_shared_evp_pkey);
560 static int test_multi_shared_pkey(void)
562 return test_multi_shared_pkey_common(&thread_shared_evp_pkey);
565 static int test_multi_load_unload_provider(void)
567 EVP_MD *sha256 = NULL;
568 OSSL_PROVIDER *prov = NULL;
572 if (!thread_setup_libctx(1, NULL)
573 || !TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, "default"))
574 || !TEST_ptr(sha256 = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL))
575 || !TEST_true(OSSL_PROVIDER_unload(prov)))
579 if (!start_threads(2, &thread_provider_load_unload))
582 thread_provider_load_unload();
584 if (!teardown_threads()
585 || !TEST_true(multi_success))
589 OSSL_PROVIDER_unload(prov);
591 thead_teardown_libctx();
595 static char *multi_load_provider = "legacy";
597 * This test attempts to load several providers at the same time, and if
598 * run with a thread sanitizer, should crash if the core provider code
599 * doesn't synchronize well enough.
601 static void test_multi_load_worker(void)
605 if (!TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, multi_load_provider))
606 || !TEST_true(OSSL_PROVIDER_unload(prov)))
607 multi_set_success(0);
610 static int test_multi_default(void)
612 /* Avoid running this test twice */
613 if (multidefault_run) {
614 TEST_skip("multi default test already run");
617 multidefault_run = 1;
619 return thread_run_test(&thread_multi_simple_fetch,
620 2, &thread_multi_simple_fetch, 0, default_provider);
623 static int test_multi_load(void)
628 /* The multidefault test must run prior to this test */
629 if (!multidefault_run) {
630 TEST_info("Running multi default test first");
631 res = test_multi_default();
635 * We use the legacy provider in test_multi_load_worker because it uses a
636 * child libctx that might hit more codepaths that might be sensitive to
637 * threading issues. But in a no-legacy build that won't be loadable so
638 * we use the default provider instead.
640 prov = OSSL_PROVIDER_load(NULL, "legacy");
642 TEST_info("Cannot load legacy provider - assuming this is a no-legacy build");
643 multi_load_provider = "default";
645 OSSL_PROVIDER_unload(prov);
647 return thread_run_test(NULL, MAXIMUM_THREADS, &test_multi_load_worker, 0,
651 static void test_obj_create_one(void)
653 char tids[12], oid[40], sn[30], ln[30];
654 int id = get_new_uid();
656 BIO_snprintf(tids, sizeof(tids), "%d", id);
657 BIO_snprintf(oid, sizeof(oid), "1.3.6.1.4.1.16604.%s", tids);
658 BIO_snprintf(sn, sizeof(sn), "short-name-%s", tids);
659 BIO_snprintf(ln, sizeof(ln), "long-name-%s", tids);
660 if (!TEST_int_ne(id, 0)
661 || !TEST_true(id = OBJ_create(oid, sn, ln))
662 || !TEST_true(OBJ_add_sigid(id, NID_sha3_256, NID_rsa)))
663 multi_set_success(0);
666 static int test_obj_add(void)
668 return thread_run_test(&test_obj_create_one,
669 MAXIMUM_THREADS, &test_obj_create_one,
670 1, default_provider);
673 static void test_lib_ctx_load_config_worker(void)
675 if (!TEST_int_eq(OSSL_LIB_CTX_load_config(multi_libctx, config_file), 1))
676 multi_set_success(0);
679 static int test_lib_ctx_load_config(void)
681 return thread_run_test(&test_lib_ctx_load_config_worker,
682 MAXIMUM_THREADS, &test_lib_ctx_load_config_worker,
683 1, default_provider);
686 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
687 static BIO *multi_bio1, *multi_bio2;
689 static void test_bio_dgram_pair_worker(void)
696 size_t num_processed = 0;
698 if (!TEST_int_eq(RAND_bytes_ex(multi_libctx, &ch, 1, 64), 1))
702 msg.data_len = sizeof(scratch);
705 * We do not test for failure here as recvmmsg may fail if no sendmmsg
706 * has been called yet. The purpose of this code is to exercise tsan.
709 r = BIO_sendmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg,
710 sizeof(BIO_MSG), 1, 0, &num_processed);
712 r = BIO_recvmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg,
713 sizeof(BIO_MSG), 1, 0, &num_processed);
718 multi_set_success(0);
721 static int test_bio_dgram_pair(void)
724 BIO *bio1 = NULL, *bio2 = NULL;
726 r = BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0);
727 if (!TEST_int_eq(r, 1))
733 r = thread_run_test(&test_bio_dgram_pair_worker,
734 MAXIMUM_THREADS, &test_bio_dgram_pair_worker,
735 1, default_provider);
744 typedef enum OPTION_choice {
747 OPT_FIPS, OPT_CONFIG_FILE,
751 const OPTIONS *test_get_options(void)
753 static const OPTIONS options[] = {
754 OPT_TEST_OPTIONS_DEFAULT_USAGE,
755 { "fips", OPT_FIPS, '-', "Test the FIPS provider" },
756 { "config", OPT_CONFIG_FILE, '<',
757 "The configuration file to use for the libctx" },
763 int setup_tests(void)
768 while ((o = opt_next()) != OPT_EOF) {
773 case OPT_CONFIG_FILE:
774 config_file = opt_arg();
783 if (!TEST_ptr(datadir = test_get_argument(0)))
786 privkey = test_mk_file_path(datadir, "rsakey.pem");
787 if (!TEST_ptr(privkey))
790 if (!TEST_ptr(global_lock = CRYPTO_THREAD_lock_new()))
793 #ifdef TSAN_REQUIRES_LOCKING
794 if (!TEST_ptr(tsan_lock = CRYPTO_THREAD_lock_new()))
798 /* Keep first to validate auto creation of default library context */
799 ADD_TEST(test_multi_default);
803 ADD_TEST(test_thread_local);
804 ADD_TEST(test_atomic);
805 ADD_TEST(test_multi_load);
806 ADD_TEST(test_multi_general_worker_default_provider);
807 ADD_TEST(test_multi_general_worker_fips_provider);
808 ADD_TEST(test_multi_fetch_worker);
809 ADD_TEST(test_multi_shared_pkey);
810 #ifndef OPENSSL_NO_DEPRECATED_3_0
811 ADD_TEST(test_multi_downgrade_shared_pkey);
813 ADD_TEST(test_multi_load_unload_provider);
814 ADD_TEST(test_obj_add);
815 ADD_TEST(test_lib_ctx_load_config);
816 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
817 ADD_TEST(test_bio_dgram_pair);
822 void cleanup_tests(void)
824 OPENSSL_free(privkey);
825 #ifdef TSAN_REQUIRES_LOCKING
826 CRYPTO_THREAD_lock_free(tsan_lock);
828 CRYPTO_THREAD_lock_free(global_lock);