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 "internal/tsan_assist.h"
28 #include "internal/nelem.h"
30 #include "threadstest.h"
32 /* Limit the maximum number of threads */
33 #define MAXIMUM_THREADS 10
35 /* Limit the maximum number of providers loaded into a library context */
36 #define MAXIMUM_PROVIDERS 4
38 static int do_fips = 0;
40 static char *config_file = NULL;
41 static int multidefault_run = 0;
43 static const char *default_provider[] = { "default", NULL };
44 static const char *fips_provider[] = { "fips", NULL };
45 static const char *fips_and_default_providers[] = { "default", "fips", NULL };
47 #ifdef TSAN_REQUIRES_LOCKING
48 static CRYPTO_RWLOCK *tsan_lock;
51 /* Grab a globally unique integer value, return 0 on failure */
52 static int get_new_uid(void)
55 * Start with a nice large number to avoid potential conflicts when
56 * we generate a new OID.
58 static TSAN_QUALIFIER int current_uid = 1 << (sizeof(int) * 8 - 2);
59 #ifdef TSAN_REQUIRES_LOCKING
62 if (!TEST_true(CRYPTO_THREAD_write_lock(tsan_lock)))
65 if (!TEST_true(CRYPTO_THREAD_unlock(tsan_lock)))
70 return tsan_counter(¤t_uid);
74 static int test_lock(void)
76 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
79 res = TEST_true(CRYPTO_THREAD_read_lock(lock))
80 && TEST_true(CRYPTO_THREAD_unlock(lock))
81 && TEST_true(CRYPTO_THREAD_write_lock(lock))
82 && TEST_true(CRYPTO_THREAD_unlock(lock));
84 CRYPTO_THREAD_lock_free(lock);
89 static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
90 static unsigned once_run_count = 0;
92 static void once_do_run(void)
97 static void once_run_thread_cb(void)
99 CRYPTO_THREAD_run_once(&once_run, once_do_run);
102 static int test_once(void)
106 if (!TEST_true(run_thread(&thread, once_run_thread_cb))
107 || !TEST_true(wait_for_thread(thread))
108 || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
109 || !TEST_int_eq(once_run_count, 1))
114 static CRYPTO_THREAD_LOCAL thread_local_key;
115 static unsigned destructor_run_count = 0;
116 static int thread_local_thread_cb_ok = 0;
118 static void thread_local_destructor(void *arg)
130 static void thread_local_thread_cb(void)
134 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
135 if (!TEST_ptr_null(ptr)
136 || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
137 &destructor_run_count)))
140 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
141 if (!TEST_ptr_eq(ptr, &destructor_run_count))
144 thread_local_thread_cb_ok = 1;
147 static int test_thread_local(void)
152 if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
153 thread_local_destructor)))
156 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
157 if (!TEST_ptr_null(ptr)
158 || !TEST_true(run_thread(&thread, thread_local_thread_cb))
159 || !TEST_true(wait_for_thread(thread))
160 || !TEST_int_eq(thread_local_thread_cb_ok, 1))
163 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
165 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
166 if (!TEST_ptr_null(ptr))
169 # if !defined(OPENSSL_SYS_WINDOWS)
170 if (!TEST_int_eq(destructor_run_count, 1))
175 if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
180 static int test_atomic(void)
182 int val = 0, ret = 0, testresult = 0;
183 uint64_t val64 = 1, ret64 = 0;
184 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
189 if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) {
190 /* This succeeds therefore we're on a platform with lockless atomics */
191 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
194 /* This failed therefore we're on a platform without lockless atomics */
195 if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret))
201 if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock)))
203 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
206 if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) {
207 /* This succeeds therefore we're on a platform with lockless atomics */
208 if (!TEST_uint_eq((unsigned int)val64, 3)
209 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
212 /* This failed therefore we're on a platform without lockless atomics */
213 if (!TEST_uint_eq((unsigned int)val64, 1)
214 || !TEST_int_eq((unsigned int)ret64, 0))
220 if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock)))
223 if (!TEST_uint_eq((unsigned int)val64, 3)
224 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
228 if (CRYPTO_atomic_load(&val64, &ret64, NULL)) {
229 /* This succeeds therefore we're on a platform with lockless atomics */
230 if (!TEST_uint_eq((unsigned int)val64, 3)
231 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
234 /* This failed therefore we're on a platform without lockless atomics */
235 if (!TEST_uint_eq((unsigned int)val64, 3)
236 || !TEST_int_eq((unsigned int)ret64, 0))
241 if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock)))
244 if (!TEST_uint_eq((unsigned int)val64, 3)
245 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
250 CRYPTO_THREAD_lock_free(lock);
254 static OSSL_LIB_CTX *multi_libctx = NULL;
255 static int multi_success;
256 static OSSL_PROVIDER *multi_provider[MAXIMUM_PROVIDERS + 1];
257 static size_t multi_num_threads;
258 static thread_t multi_threads[MAXIMUM_THREADS];
260 static void multi_intialise(void)
264 multi_num_threads = 0;
265 memset(multi_threads, 0, sizeof(multi_threads));
266 memset(multi_provider, 0, sizeof(multi_provider));
269 static void thead_teardown_libctx(void)
273 for (p = multi_provider; *p != NULL; p++)
274 OSSL_PROVIDER_unload(*p);
275 OSSL_LIB_CTX_free(multi_libctx);
279 static int thread_setup_libctx(int libctx, const char *providers[])
283 if (libctx && !TEST_true(test_get_libctx(&multi_libctx, NULL, config_file,
287 if (providers != NULL)
288 for (n = 0; providers[n] != NULL; n++)
289 if (!TEST_size_t_lt(n, MAXIMUM_PROVIDERS)
290 || !TEST_ptr(multi_provider[n] = OSSL_PROVIDER_load(multi_libctx,
292 thead_teardown_libctx();
298 static int teardown_threads(void)
302 for (i = 0; i < multi_num_threads; i++)
303 if (!TEST_true(wait_for_thread(multi_threads[i])))
308 static int start_threads(size_t n, void (*thread_func)(void))
312 if (!TEST_size_t_le(multi_num_threads + n, MAXIMUM_THREADS))
315 for (i = 0 ; i < n; i++)
316 if (!TEST_true(run_thread(multi_threads + multi_num_threads++, thread_func)))
321 /* Template multi-threaded test function */
322 static int thread_run_test(void (*main_func)(void),
323 size_t num_threads, void (*thread_func)(void),
324 int libctx, const char *providers[])
329 if (!thread_setup_libctx(libctx, providers)
330 || !start_threads(num_threads, thread_func))
333 if (main_func != NULL)
336 if (!teardown_threads()
337 || !TEST_true(multi_success))
341 thead_teardown_libctx();
345 static void thread_general_worker(void)
347 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
348 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
349 EVP_CIPHER_CTX *cipherctx = EVP_CIPHER_CTX_new();
350 EVP_CIPHER *ciph = EVP_CIPHER_fetch(multi_libctx, "AES-128-CBC", NULL);
351 const char *message = "Hello World";
352 size_t messlen = strlen(message);
353 /* Should be big enough for encryption output too */
354 unsigned char out[EVP_MAX_MD_SIZE];
355 const unsigned char key[AES_BLOCK_SIZE] = {
356 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
357 0x0c, 0x0d, 0x0e, 0x0f
359 const unsigned char iv[AES_BLOCK_SIZE] = {
360 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
361 0x0c, 0x0d, 0x0e, 0x0f
365 EVP_PKEY *pkey = NULL;
369 isfips = OSSL_PROVIDER_available(multi_libctx, "fips");
373 || !TEST_ptr(cipherctx)
378 for (i = 0; i < 5; i++) {
379 if (!TEST_true(EVP_DigestInit_ex(mdctx, md, NULL))
380 || !TEST_true(EVP_DigestUpdate(mdctx, message, messlen))
381 || !TEST_true(EVP_DigestFinal(mdctx, out, &mdoutl)))
384 for (i = 0; i < 5; i++) {
385 if (!TEST_true(EVP_EncryptInit_ex(cipherctx, ciph, NULL, key, iv))
386 || !TEST_true(EVP_EncryptUpdate(cipherctx, out, &ciphoutl,
387 (unsigned char *)message,
389 || !TEST_true(EVP_EncryptFinal(cipherctx, out, &ciphoutl)))
394 * We want the test to run quickly - not securely.
395 * Therefore we use an insecure bit length where we can (512).
396 * In the FIPS module though we must use a longer length.
398 pkey = EVP_PKEY_Q_keygen(multi_libctx, NULL, "RSA", isfips ? 2048 : 512);
404 EVP_MD_CTX_free(mdctx);
406 EVP_CIPHER_CTX_free(cipherctx);
407 EVP_CIPHER_free(ciph);
413 static void thread_multi_simple_fetch(void)
415 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
423 static EVP_PKEY *shared_evp_pkey = NULL;
425 static void thread_shared_evp_pkey(void)
427 char *msg = "Hello World";
428 unsigned char ctbuf[256];
429 unsigned char ptbuf[256];
430 size_t ptlen, ctlen = sizeof(ctbuf);
431 EVP_PKEY_CTX *ctx = NULL;
435 for (i = 0; i < 1 + do_fips; i++) {
437 EVP_PKEY_CTX_free(ctx);
438 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey,
439 i == 0 ? "provider=default"
444 if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx), 0)
445 || !TEST_int_ge(EVP_PKEY_encrypt(ctx, ctbuf, &ctlen,
446 (unsigned char *)msg, strlen(msg)),
450 EVP_PKEY_CTX_free(ctx);
451 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey, NULL);
456 ptlen = sizeof(ptbuf);
457 if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx), 0)
458 || !TEST_int_gt(EVP_PKEY_decrypt(ctx, ptbuf, &ptlen, ctbuf, ctlen),
460 || !TEST_mem_eq(msg, strlen(msg), ptbuf, ptlen))
467 EVP_PKEY_CTX_free(ctx);
472 static void thread_provider_load_unload(void)
474 OSSL_PROVIDER *deflt = OSSL_PROVIDER_load(multi_libctx, "default");
477 || !TEST_true(OSSL_PROVIDER_available(multi_libctx, "default")))
480 OSSL_PROVIDER_unload(deflt);
483 static int test_multi_general_worker_default_provider(void)
485 return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
486 1, default_provider);
489 static int test_multi_general_worker_fips_provider(void)
492 return TEST_skip("FIPS not supported");
493 return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
497 static int test_multi_fetch_worker(void)
499 return thread_run_test(&thread_multi_simple_fetch,
500 2, &thread_multi_simple_fetch, 1, default_provider);
503 static int test_multi_shared_pkey_common(void (*worker)(void))
508 if (!thread_setup_libctx(1, do_fips ? fips_and_default_providers
510 || !TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx))
511 || !start_threads(1, &thread_shared_evp_pkey)
512 || !start_threads(1, worker))
515 thread_shared_evp_pkey();
517 if (!teardown_threads()
518 || !TEST_true(multi_success))
522 EVP_PKEY_free(shared_evp_pkey);
523 thead_teardown_libctx();
527 #ifndef OPENSSL_NO_DEPRECATED_3_0
528 static void thread_downgrade_shared_evp_pkey(void)
531 * This test is only relevant for deprecated functions that perform
534 if (EVP_PKEY_get0_RSA(shared_evp_pkey) == NULL)
538 static int test_multi_downgrade_shared_pkey(void)
540 return test_multi_shared_pkey_common(&thread_downgrade_shared_evp_pkey);
544 static int test_multi_shared_pkey(void)
546 return test_multi_shared_pkey_common(&thread_shared_evp_pkey);
549 static int test_multi_load_unload_provider(void)
551 EVP_MD *sha256 = NULL;
552 OSSL_PROVIDER *prov = NULL;
556 if (!thread_setup_libctx(1, NULL)
557 || !TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, "default"))
558 || !TEST_ptr(sha256 = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL))
559 || !TEST_true(OSSL_PROVIDER_unload(prov)))
563 if (!start_threads(2, &thread_provider_load_unload))
566 thread_provider_load_unload();
568 if (!teardown_threads()
569 || !TEST_true(multi_success))
573 OSSL_PROVIDER_unload(prov);
575 thead_teardown_libctx();
579 static char *multi_load_provider = "legacy";
581 * This test attempts to load several providers at the same time, and if
582 * run with a thread sanitizer, should crash if the core provider code
583 * doesn't synchronize well enough.
585 static void test_multi_load_worker(void)
589 if (!TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, multi_load_provider))
590 || !TEST_true(OSSL_PROVIDER_unload(prov)))
594 static int test_multi_default(void)
596 /* Avoid running this test twice */
597 if (multidefault_run) {
598 TEST_skip("multi default test already run");
601 multidefault_run = 1;
603 return thread_run_test(&thread_multi_simple_fetch,
604 2, &thread_multi_simple_fetch, 0, default_provider);
607 static int test_multi_load(void)
612 /* The multidefault test must run prior to this test */
613 if (!multidefault_run) {
614 TEST_info("Running multi default test first");
615 res = test_multi_default();
619 * We use the legacy provider in test_multi_load_worker because it uses a
620 * child libctx that might hit more codepaths that might be sensitive to
621 * threading issues. But in a no-legacy build that won't be loadable so
622 * we use the default provider instead.
624 prov = OSSL_PROVIDER_load(NULL, "legacy");
626 TEST_info("Cannot load legacy provider - assuming this is a no-legacy build");
627 multi_load_provider = "default";
629 OSSL_PROVIDER_unload(prov);
631 return thread_run_test(NULL, MAXIMUM_THREADS, &test_multi_load_worker, 0,
635 static void test_obj_create_one(void)
637 char tids[12], oid[40], sn[30], ln[30];
638 int id = get_new_uid();
640 BIO_snprintf(tids, sizeof(tids), "%d", id);
641 BIO_snprintf(oid, sizeof(oid), "1.3.6.1.4.1.16604.%s", tids);
642 BIO_snprintf(sn, sizeof(sn), "short-name-%s", tids);
643 BIO_snprintf(ln, sizeof(ln), "long-name-%s", tids);
644 if (!TEST_int_ne(id, 0)
645 || !TEST_true(id = OBJ_create(oid, sn, ln))
646 || !TEST_true(OBJ_add_sigid(id, NID_sha3_256, NID_rsa)))
650 static int test_obj_add(void)
652 return thread_run_test(&test_obj_create_one,
653 MAXIMUM_THREADS, &test_obj_create_one,
654 1, default_provider);
657 typedef enum OPTION_choice {
660 OPT_FIPS, OPT_CONFIG_FILE,
664 const OPTIONS *test_get_options(void)
666 static const OPTIONS options[] = {
667 OPT_TEST_OPTIONS_DEFAULT_USAGE,
668 { "fips", OPT_FIPS, '-', "Test the FIPS provider" },
669 { "config", OPT_CONFIG_FILE, '<',
670 "The configuration file to use for the libctx" },
676 int setup_tests(void)
681 while ((o = opt_next()) != OPT_EOF) {
686 case OPT_CONFIG_FILE:
687 config_file = opt_arg();
696 if (!TEST_ptr(datadir = test_get_argument(0)))
699 privkey = test_mk_file_path(datadir, "rsakey.pem");
700 if (!TEST_ptr(privkey))
703 #ifdef TSAN_REQUIRES_LOCKING
704 if (!TEST_ptr(tsan_lock = CRYPTO_THREAD_lock_new()))
708 /* Keep first to validate auto creation of default library context */
709 ADD_TEST(test_multi_default);
713 ADD_TEST(test_thread_local);
714 ADD_TEST(test_atomic);
715 ADD_TEST(test_multi_load);
716 ADD_TEST(test_multi_general_worker_default_provider);
717 ADD_TEST(test_multi_general_worker_fips_provider);
718 ADD_TEST(test_multi_fetch_worker);
719 ADD_TEST(test_multi_shared_pkey);
720 #ifndef OPENSSL_NO_DEPRECATED_3_0
721 ADD_TEST(test_multi_downgrade_shared_pkey);
723 ADD_TEST(test_multi_load_unload_provider);
724 ADD_TEST(test_obj_add);
728 void cleanup_tests(void)
730 OPENSSL_free(privkey);
731 #ifdef TSAN_REQUIRES_LOCKING
732 CRYPTO_THREAD_lock_free(tsan_lock);