2 * Copyright 2016-2021 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 /* Grab a globally unique integer value */
48 static int get_new_uid(void)
51 * Start with a nice large number to avoid potential conflicts when
52 * we generate a new OID.
54 static TSAN_QUALIFIER int current_uid = 1 << (sizeof(int) * 8 - 2);
56 return tsan_counter(¤t_uid);
59 static int test_lock(void)
61 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
64 res = TEST_true(CRYPTO_THREAD_read_lock(lock))
65 && TEST_true(CRYPTO_THREAD_unlock(lock));
67 CRYPTO_THREAD_lock_free(lock);
72 static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
73 static unsigned once_run_count = 0;
75 static void once_do_run(void)
80 static void once_run_thread_cb(void)
82 CRYPTO_THREAD_run_once(&once_run, once_do_run);
85 static int test_once(void)
89 if (!TEST_true(run_thread(&thread, once_run_thread_cb))
90 || !TEST_true(wait_for_thread(thread))
91 || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
92 || !TEST_int_eq(once_run_count, 1))
97 static CRYPTO_THREAD_LOCAL thread_local_key;
98 static unsigned destructor_run_count = 0;
99 static int thread_local_thread_cb_ok = 0;
101 static void thread_local_destructor(void *arg)
113 static void thread_local_thread_cb(void)
117 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
118 if (!TEST_ptr_null(ptr)
119 || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
120 &destructor_run_count)))
123 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
124 if (!TEST_ptr_eq(ptr, &destructor_run_count))
127 thread_local_thread_cb_ok = 1;
130 static int test_thread_local(void)
135 if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
136 thread_local_destructor)))
139 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
140 if (!TEST_ptr_null(ptr)
141 || !TEST_true(run_thread(&thread, thread_local_thread_cb))
142 || !TEST_true(wait_for_thread(thread))
143 || !TEST_int_eq(thread_local_thread_cb_ok, 1))
146 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
148 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
149 if (!TEST_ptr_null(ptr))
152 # if !defined(OPENSSL_SYS_WINDOWS)
153 if (!TEST_int_eq(destructor_run_count, 1))
158 if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
163 static int test_atomic(void)
165 int val = 0, ret = 0, testresult = 0;
166 uint64_t val64 = 1, ret64 = 0;
167 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
172 if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) {
173 /* This succeeds therefore we're on a platform with lockless atomics */
174 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
177 /* This failed therefore we're on a platform without lockless atomics */
178 if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret))
184 if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock)))
186 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
189 if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) {
190 /* This succeeds therefore we're on a platform with lockless atomics */
191 if (!TEST_uint_eq((unsigned int)val64, 3)
192 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
195 /* This failed therefore we're on a platform without lockless atomics */
196 if (!TEST_uint_eq((unsigned int)val64, 1)
197 || !TEST_int_eq((unsigned int)ret64, 0))
203 if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock)))
206 if (!TEST_uint_eq((unsigned int)val64, 3)
207 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
211 if (CRYPTO_atomic_load(&val64, &ret64, NULL)) {
212 /* This succeeds therefore we're on a platform with lockless atomics */
213 if (!TEST_uint_eq((unsigned int)val64, 3)
214 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
217 /* This failed therefore we're on a platform without lockless atomics */
218 if (!TEST_uint_eq((unsigned int)val64, 3)
219 || !TEST_int_eq((unsigned int)ret64, 0))
224 if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock)))
227 if (!TEST_uint_eq((unsigned int)val64, 3)
228 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
233 CRYPTO_THREAD_lock_free(lock);
237 static OSSL_LIB_CTX *multi_libctx = NULL;
238 static int multi_success;
239 static OSSL_PROVIDER *multi_provider[MAXIMUM_PROVIDERS + 1];
240 static size_t multi_num_threads;
241 static thread_t multi_threads[MAXIMUM_THREADS];
243 static void multi_intialise(void)
247 multi_num_threads = 0;
248 memset(multi_threads, 0, sizeof(multi_threads));
249 memset(multi_provider, 0, sizeof(multi_provider));
252 static void thead_teardown_libctx(void)
256 for (p = multi_provider; *p != NULL; p++)
257 OSSL_PROVIDER_unload(*p);
258 OSSL_LIB_CTX_free(multi_libctx);
262 static int thread_setup_libctx(int libctx, const char *providers[])
266 if (libctx && !TEST_true(test_get_libctx(&multi_libctx, NULL, config_file,
270 if (providers != NULL)
271 for (n = 0; providers[n] != NULL; n++)
272 if (!TEST_size_t_lt(n, MAXIMUM_PROVIDERS)
273 || !TEST_ptr(multi_provider[n] = OSSL_PROVIDER_load(multi_libctx,
275 thead_teardown_libctx();
281 static int teardown_threads(void)
285 for (i = 0; i < multi_num_threads; i++)
286 if (!TEST_true(wait_for_thread(multi_threads[i])))
291 static int start_threads(size_t n, void (*thread_func)(void))
295 if (!TEST_size_t_le(multi_num_threads + n, MAXIMUM_THREADS))
298 for (i = 0 ; i < n; i++)
299 if (!TEST_true(run_thread(multi_threads + multi_num_threads++, thread_func)))
304 /* Template multi-threaded test function */
305 static int thread_run_test(void (*main_func)(void),
306 size_t num_threads, void (*thread_func)(void),
307 int libctx, const char *providers[])
312 if (!thread_setup_libctx(libctx, providers)
313 || !start_threads(num_threads, thread_func))
316 if (main_func != NULL)
319 if (!teardown_threads()
320 || !TEST_true(multi_success))
324 thead_teardown_libctx();
328 static void thread_general_worker(void)
330 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
331 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
332 EVP_CIPHER_CTX *cipherctx = EVP_CIPHER_CTX_new();
333 EVP_CIPHER *ciph = EVP_CIPHER_fetch(multi_libctx, "AES-128-CBC", NULL);
334 const char *message = "Hello World";
335 size_t messlen = strlen(message);
336 /* Should be big enough for encryption output too */
337 unsigned char out[EVP_MAX_MD_SIZE];
338 const unsigned char key[AES_BLOCK_SIZE] = {
339 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
340 0x0c, 0x0d, 0x0e, 0x0f
342 const unsigned char iv[AES_BLOCK_SIZE] = {
343 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
344 0x0c, 0x0d, 0x0e, 0x0f
348 EVP_PKEY *pkey = NULL;
352 isfips = OSSL_PROVIDER_available(multi_libctx, "fips");
356 || !TEST_ptr(cipherctx)
361 for (i = 0; i < 5; i++) {
362 if (!TEST_true(EVP_DigestInit_ex(mdctx, md, NULL))
363 || !TEST_true(EVP_DigestUpdate(mdctx, message, messlen))
364 || !TEST_true(EVP_DigestFinal(mdctx, out, &mdoutl)))
367 for (i = 0; i < 5; i++) {
368 if (!TEST_true(EVP_EncryptInit_ex(cipherctx, ciph, NULL, key, iv))
369 || !TEST_true(EVP_EncryptUpdate(cipherctx, out, &ciphoutl,
370 (unsigned char *)message,
372 || !TEST_true(EVP_EncryptFinal(cipherctx, out, &ciphoutl)))
377 * We want the test to run quickly - not securely.
378 * Therefore we use an insecure bit length where we can (512).
379 * In the FIPS module though we must use a longer length.
381 pkey = EVP_PKEY_Q_keygen(multi_libctx, NULL, "RSA", isfips ? 2048 : 512);
387 EVP_MD_CTX_free(mdctx);
389 EVP_CIPHER_CTX_free(cipherctx);
390 EVP_CIPHER_free(ciph);
396 static void thread_multi_simple_fetch(void)
398 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
406 static EVP_PKEY *shared_evp_pkey = NULL;
408 static void thread_shared_evp_pkey(void)
410 char *msg = "Hello World";
411 unsigned char ctbuf[256];
412 unsigned char ptbuf[256];
413 size_t ptlen, ctlen = sizeof(ctbuf);
414 EVP_PKEY_CTX *ctx = NULL;
418 for (i = 0; i < 1 + do_fips; i++) {
420 EVP_PKEY_CTX_free(ctx);
421 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey,
422 i == 0 ? "provider=default"
427 if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx), 0)
428 || !TEST_int_ge(EVP_PKEY_encrypt(ctx, ctbuf, &ctlen,
429 (unsigned char *)msg, strlen(msg)),
433 EVP_PKEY_CTX_free(ctx);
434 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey, NULL);
439 ptlen = sizeof(ptbuf);
440 if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx), 0)
441 || !TEST_int_gt(EVP_PKEY_decrypt(ctx, ptbuf, &ptlen, ctbuf, ctlen),
443 || !TEST_mem_eq(msg, strlen(msg), ptbuf, ptlen))
450 EVP_PKEY_CTX_free(ctx);
455 static void thread_provider_load_unload(void)
457 OSSL_PROVIDER *deflt = OSSL_PROVIDER_load(multi_libctx, "default");
460 || !TEST_true(OSSL_PROVIDER_available(multi_libctx, "default")))
463 OSSL_PROVIDER_unload(deflt);
466 static int test_multi_general_worker_default_provider(void)
468 return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
469 1, default_provider);
472 static int test_multi_general_worker_fips_provider(void)
475 return TEST_skip("FIPS not supported");
476 return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
480 static int test_multi_fetch_worker(void)
482 return thread_run_test(&thread_multi_simple_fetch,
483 2, &thread_multi_simple_fetch, 1, default_provider);
486 static int test_multi_shared_pkey_common(void (*worker)(void))
491 if (!thread_setup_libctx(1, do_fips ? fips_and_default_providers
493 || !TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx))
494 || !start_threads(1, &thread_shared_evp_pkey)
495 || !start_threads(1, worker))
498 thread_shared_evp_pkey();
500 if (!teardown_threads()
501 || !TEST_true(multi_success))
505 EVP_PKEY_free(shared_evp_pkey);
506 thead_teardown_libctx();
510 #ifndef OPENSSL_NO_DEPRECATED_3_0
511 static void thread_downgrade_shared_evp_pkey(void)
514 * This test is only relevant for deprecated functions that perform
517 if (EVP_PKEY_get0_RSA(shared_evp_pkey) == NULL)
521 static int test_multi_downgrade_shared_pkey(void)
523 return test_multi_shared_pkey_common(&thread_downgrade_shared_evp_pkey);
527 static int test_multi_shared_pkey(void)
529 return test_multi_shared_pkey_common(&thread_shared_evp_pkey);
532 static int test_multi_load_unload_provider(void)
534 EVP_MD *sha256 = NULL;
535 OSSL_PROVIDER *prov = NULL;
539 if (!thread_setup_libctx(1, NULL)
540 || !TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, "default"))
541 || !TEST_ptr(sha256 = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL))
542 || !TEST_true(OSSL_PROVIDER_unload(prov)))
546 if (!start_threads(2, &thread_provider_load_unload))
549 thread_provider_load_unload();
551 if (!teardown_threads()
552 || !TEST_true(multi_success))
556 OSSL_PROVIDER_unload(prov);
558 thead_teardown_libctx();
562 static char *multi_load_provider = "legacy";
564 * This test attempts to load several providers at the same time, and if
565 * run with a thread sanitizer, should crash if the core provider code
566 * doesn't synchronize well enough.
568 static void test_multi_load_worker(void)
572 if (!TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, multi_load_provider))
573 || !TEST_true(OSSL_PROVIDER_unload(prov)))
577 static int test_multi_default(void)
579 /* Avoid running this test twice */
580 if (multidefault_run) {
581 TEST_skip("multi default test already run");
584 multidefault_run = 1;
586 return thread_run_test(&thread_multi_simple_fetch,
587 2, &thread_multi_simple_fetch, 0, default_provider);
590 static int test_multi_load(void)
595 /* The multidefault test must run prior to this test */
596 if (!multidefault_run) {
597 TEST_info("Running multi default test first");
598 res = test_multi_default();
602 * We use the legacy provider in test_multi_load_worker because it uses a
603 * child libctx that might hit more codepaths that might be sensitive to
604 * threading issues. But in a no-legacy build that won't be loadable so
605 * we use the default provider instead.
607 prov = OSSL_PROVIDER_load(NULL, "legacy");
609 TEST_info("Cannot load legacy provider - assuming this is a no-legacy build");
610 multi_load_provider = "default";
612 OSSL_PROVIDER_unload(prov);
614 return thread_run_test(NULL, MAXIMUM_THREADS, &test_multi_load_worker, 0,
618 static void test_obj_create_one(void)
620 char tids[12], oid[40], sn[30], ln[30];
621 int id = get_new_uid();
623 BIO_snprintf(tids, sizeof(tids), "%d", id);
624 BIO_snprintf(oid, sizeof(oid), "1.3.6.1.4.1.16604.%s", tids);
625 BIO_snprintf(sn, sizeof(sn), "short-name-%s", tids);
626 BIO_snprintf(ln, sizeof(ln), "long-name-%s", tids);
627 if (!TEST_true(id = OBJ_create(oid, sn, ln))
628 || !TEST_true(OBJ_add_sigid(id, NID_sha3_256, NID_rsa)))
632 static int test_obj_add(void)
634 return thread_run_test(&test_obj_create_one,
635 MAXIMUM_THREADS, &test_obj_create_one,
636 1, default_provider);
639 typedef enum OPTION_choice {
642 OPT_FIPS, OPT_CONFIG_FILE,
646 const OPTIONS *test_get_options(void)
648 static const OPTIONS options[] = {
649 OPT_TEST_OPTIONS_DEFAULT_USAGE,
650 { "fips", OPT_FIPS, '-', "Test the FIPS provider" },
651 { "config", OPT_CONFIG_FILE, '<',
652 "The configuration file to use for the libctx" },
658 int setup_tests(void)
663 while ((o = opt_next()) != OPT_EOF) {
668 case OPT_CONFIG_FILE:
669 config_file = opt_arg();
678 if (!TEST_ptr(datadir = test_get_argument(0)))
681 privkey = test_mk_file_path(datadir, "rsakey.pem");
682 if (!TEST_ptr(privkey))
685 /* Keep first to validate auto creation of default library context */
686 ADD_TEST(test_multi_default);
690 ADD_TEST(test_thread_local);
691 ADD_TEST(test_atomic);
692 ADD_TEST(test_multi_load);
693 ADD_TEST(test_multi_general_worker_default_provider);
694 ADD_TEST(test_multi_general_worker_fips_provider);
695 ADD_TEST(test_multi_fetch_worker);
696 ADD_TEST(test_multi_shared_pkey);
697 #ifndef OPENSSL_NO_DEPRECATED_3_0
698 ADD_TEST(test_multi_downgrade_shared_pkey);
700 ADD_TEST(test_multi_load_unload_provider);
701 ADD_TEST(test_obj_add);
705 void cleanup_tests(void)
707 OPENSSL_free(privkey);