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
10 /* test_multi below tests the thread safety of a deprecated function */
11 #define OPENSSL_SUPPRESS_DEPRECATED
18 #include <openssl/crypto.h>
19 #include <openssl/rsa.h>
20 #include <openssl/aes.h>
21 #include <openssl/rsa.h>
24 static int do_fips
= 0;
26 static char *config_file
= NULL
;
28 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
30 typedef unsigned int thread_t
;
32 static int run_thread(thread_t
*t
, void (*f
)(void))
38 static int wait_for_thread(thread_t thread
)
43 #elif defined(OPENSSL_SYS_WINDOWS)
45 typedef HANDLE thread_t
;
47 static DWORD WINAPI
thread_run(LPVOID arg
)
51 *(void **) (&f
) = arg
;
57 static int run_thread(thread_t
*t
, void (*f
)(void))
59 *t
= CreateThread(NULL
, 0, thread_run
, *(void **) &f
, 0, NULL
);
63 static int wait_for_thread(thread_t thread
)
65 return WaitForSingleObject(thread
, INFINITE
) == 0;
70 typedef pthread_t thread_t
;
72 static void *thread_run(void *arg
)
76 *(void **) (&f
) = arg
;
82 static int run_thread(thread_t
*t
, void (*f
)(void))
84 return pthread_create(t
, NULL
, thread_run
, *(void **) &f
) == 0;
87 static int wait_for_thread(thread_t thread
)
89 return pthread_join(thread
, NULL
) == 0;
94 static int test_lock(void)
96 CRYPTO_RWLOCK
*lock
= CRYPTO_THREAD_lock_new();
99 res
= TEST_true(CRYPTO_THREAD_read_lock(lock
))
100 && TEST_true(CRYPTO_THREAD_unlock(lock
));
102 CRYPTO_THREAD_lock_free(lock
);
107 static CRYPTO_ONCE once_run
= CRYPTO_ONCE_STATIC_INIT
;
108 static unsigned once_run_count
= 0;
110 static void once_do_run(void)
115 static void once_run_thread_cb(void)
117 CRYPTO_THREAD_run_once(&once_run
, once_do_run
);
120 static int test_once(void)
124 if (!TEST_true(run_thread(&thread
, once_run_thread_cb
))
125 || !TEST_true(wait_for_thread(thread
))
126 || !CRYPTO_THREAD_run_once(&once_run
, once_do_run
)
127 || !TEST_int_eq(once_run_count
, 1))
132 static CRYPTO_THREAD_LOCAL thread_local_key
;
133 static unsigned destructor_run_count
= 0;
134 static int thread_local_thread_cb_ok
= 0;
136 static void thread_local_destructor(void *arg
)
148 static void thread_local_thread_cb(void)
152 ptr
= CRYPTO_THREAD_get_local(&thread_local_key
);
153 if (!TEST_ptr_null(ptr
)
154 || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key
,
155 &destructor_run_count
)))
158 ptr
= CRYPTO_THREAD_get_local(&thread_local_key
);
159 if (!TEST_ptr_eq(ptr
, &destructor_run_count
))
162 thread_local_thread_cb_ok
= 1;
165 static int test_thread_local(void)
170 if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key
,
171 thread_local_destructor
)))
174 ptr
= CRYPTO_THREAD_get_local(&thread_local_key
);
175 if (!TEST_ptr_null(ptr
)
176 || !TEST_true(run_thread(&thread
, thread_local_thread_cb
))
177 || !TEST_true(wait_for_thread(thread
))
178 || !TEST_int_eq(thread_local_thread_cb_ok
, 1))
181 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
183 ptr
= CRYPTO_THREAD_get_local(&thread_local_key
);
184 if (!TEST_ptr_null(ptr
))
187 # if !defined(OPENSSL_SYS_WINDOWS)
188 if (!TEST_int_eq(destructor_run_count
, 1))
193 if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key
)))
198 static int test_atomic(void)
200 int val
= 0, ret
= 0, testresult
= 0;
201 uint64_t val64
= 1, ret64
= 0;
202 CRYPTO_RWLOCK
*lock
= CRYPTO_THREAD_lock_new();
207 if (CRYPTO_atomic_add(&val
, 1, &ret
, NULL
)) {
208 /* This succeeds therefore we're on a platform with lockless atomics */
209 if (!TEST_int_eq(val
, 1) || !TEST_int_eq(val
, ret
))
212 /* This failed therefore we're on a platform without lockless atomics */
213 if (!TEST_int_eq(val
, 0) || !TEST_int_eq(val
, ret
))
219 if (!TEST_true(CRYPTO_atomic_add(&val
, 1, &ret
, lock
)))
221 if (!TEST_int_eq(val
, 1) || !TEST_int_eq(val
, ret
))
224 if (CRYPTO_atomic_or(&val64
, 2, &ret64
, NULL
)) {
225 /* This succeeds therefore we're on a platform with lockless atomics */
226 if (!TEST_uint_eq((unsigned int)val64
, 3)
227 || !TEST_uint_eq((unsigned int)val64
, (unsigned int)ret64
))
230 /* This failed therefore we're on a platform without lockless atomics */
231 if (!TEST_uint_eq((unsigned int)val64
, 1)
232 || !TEST_int_eq((unsigned int)ret64
, 0))
238 if (!TEST_true(CRYPTO_atomic_or(&val64
, 2, &ret64
, lock
)))
241 if (!TEST_uint_eq((unsigned int)val64
, 3)
242 || !TEST_uint_eq((unsigned int)val64
, (unsigned int)ret64
))
246 if (CRYPTO_atomic_load(&val64
, &ret64
, NULL
)) {
247 /* This succeeds therefore we're on a platform with lockless atomics */
248 if (!TEST_uint_eq((unsigned int)val64
, 3)
249 || !TEST_uint_eq((unsigned int)val64
, (unsigned int)ret64
))
252 /* This failed therefore we're on a platform without lockless atomics */
253 if (!TEST_uint_eq((unsigned int)val64
, 3)
254 || !TEST_int_eq((unsigned int)ret64
, 0))
259 if (!TEST_true(CRYPTO_atomic_load(&val64
, &ret64
, lock
)))
262 if (!TEST_uint_eq((unsigned int)val64
, 3)
263 || !TEST_uint_eq((unsigned int)val64
, (unsigned int)ret64
))
268 CRYPTO_THREAD_lock_free(lock
);
272 static OSSL_LIB_CTX
*multi_libctx
= NULL
;
273 static int multi_success
;
275 static void thread_general_worker(void)
277 EVP_MD_CTX
*mdctx
= EVP_MD_CTX_new();
278 EVP_MD
*md
= EVP_MD_fetch(multi_libctx
, "SHA2-256", NULL
);
279 EVP_CIPHER_CTX
*cipherctx
= EVP_CIPHER_CTX_new();
280 EVP_CIPHER
*ciph
= EVP_CIPHER_fetch(multi_libctx
, "AES-128-CBC", NULL
);
281 const char *message
= "Hello World";
282 size_t messlen
= strlen(message
);
283 /* Should be big enough for encryption output too */
284 unsigned char out
[EVP_MAX_MD_SIZE
];
285 const unsigned char key
[AES_BLOCK_SIZE
] = {
286 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
287 0x0c, 0x0d, 0x0e, 0x0f
289 const unsigned char iv
[AES_BLOCK_SIZE
] = {
290 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
291 0x0c, 0x0d, 0x0e, 0x0f
295 EVP_PKEY
*pkey
= NULL
;
299 isfips
= OSSL_PROVIDER_available(multi_libctx
, "fips");
303 || !TEST_ptr(cipherctx
)
308 for (i
= 0; i
< 5; i
++) {
309 if (!TEST_true(EVP_DigestInit_ex(mdctx
, md
, NULL
))
310 || !TEST_true(EVP_DigestUpdate(mdctx
, message
, messlen
))
311 || !TEST_true(EVP_DigestFinal(mdctx
, out
, &mdoutl
)))
314 for (i
= 0; i
< 5; i
++) {
315 if (!TEST_true(EVP_EncryptInit_ex(cipherctx
, ciph
, NULL
, key
, iv
))
316 || !TEST_true(EVP_EncryptUpdate(cipherctx
, out
, &ciphoutl
,
317 (unsigned char *)message
,
319 || !TEST_true(EVP_EncryptFinal(cipherctx
, out
, &ciphoutl
)))
324 * We want the test to run quickly - not securely.
325 * Therefore we use an insecure bit length where we can (512).
326 * In the FIPS module though we must use a longer length.
328 pkey
= EVP_PKEY_Q_keygen(multi_libctx
, NULL
, "RSA", isfips
? 2048 : 512);
334 EVP_MD_CTX_free(mdctx
);
336 EVP_CIPHER_CTX_free(cipherctx
);
337 EVP_CIPHER_free(ciph
);
343 static void thread_multi_simple_fetch(void)
345 EVP_MD
*md
= EVP_MD_fetch(multi_libctx
, "SHA2-256", NULL
);
353 static EVP_PKEY
*shared_evp_pkey
= NULL
;
355 static void thread_shared_evp_pkey(void)
357 char *msg
= "Hello World";
358 unsigned char ctbuf
[256];
359 unsigned char ptbuf
[256];
360 size_t ptlen
= sizeof(ptbuf
), ctlen
= sizeof(ctbuf
);
361 EVP_PKEY_CTX
*ctx
= NULL
;
365 for (i
= 0; i
< 1 + do_fips
; i
++) {
367 EVP_PKEY_CTX_free(ctx
);
368 ctx
= EVP_PKEY_CTX_new_from_pkey(multi_libctx
, shared_evp_pkey
,
369 i
== 0 ? "provider=default"
374 if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx
), 0)
375 || !TEST_int_ge(EVP_PKEY_encrypt(ctx
, ctbuf
, &ctlen
,
376 (unsigned char *)msg
, strlen(msg
)),
380 EVP_PKEY_CTX_free(ctx
);
381 ctx
= EVP_PKEY_CTX_new_from_pkey(multi_libctx
, shared_evp_pkey
, NULL
);
386 if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx
), 0)
387 || !TEST_int_ge(EVP_PKEY_decrypt(ctx
, ptbuf
, &ptlen
, ctbuf
, ctlen
),
389 || !TEST_mem_eq(msg
, strlen(msg
), ptbuf
, ptlen
))
396 EVP_PKEY_CTX_free(ctx
);
401 static void thread_downgrade_shared_evp_pkey(void)
403 #ifndef OPENSSL_NO_DEPRECATED_3_0
405 * This test is only relevant for deprecated functions that perform
408 if (EVP_PKEY_get0_RSA(shared_evp_pkey
) == NULL
)
411 /* Shouldn't ever get here */
416 static void thread_provider_load_unload(void)
418 OSSL_PROVIDER
*deflt
= OSSL_PROVIDER_load(multi_libctx
, "default");
421 || !TEST_true(OSSL_PROVIDER_available(multi_libctx
, "default")))
424 OSSL_PROVIDER_unload(deflt
);
428 * Do work in multiple worker threads at the same time.
429 * Test 0: General worker, using the default provider
430 * Test 1: General worker, using the fips provider
431 * Test 2: Simple fetch worker
432 * Test 3: Worker downgrading a shared EVP_PKEY
433 * Test 4: Worker using a shared EVP_PKEY
434 * Test 5: Workder loading and unloading a provider
436 static int test_multi(int idx
)
438 thread_t thread1
, thread2
;
440 OSSL_PROVIDER
*prov
= NULL
, *prov2
= NULL
;
441 void (*worker
)(void) = NULL
;
442 void (*worker2
)(void) = NULL
;
443 EVP_MD
*sha256
= NULL
;
445 if (idx
== 1 && !do_fips
)
446 return TEST_skip("FIPS not supported");
448 #ifdef OPENSSL_NO_DEPRECATED_3_0
450 return TEST_skip("Skipping tests for deprected functions");
454 if (!TEST_true(test_get_libctx(&multi_libctx
, NULL
, config_file
,
458 prov
= OSSL_PROVIDER_load(multi_libctx
, (idx
== 1) ? "fips" : "default");
465 worker
= thread_general_worker
;
468 worker
= thread_multi_simple_fetch
;
471 worker2
= thread_downgrade_shared_evp_pkey
;
475 * If available we have both the default and fips providers for this
479 && !TEST_ptr(prov2
= OSSL_PROVIDER_load(multi_libctx
, "fips")))
481 if (!TEST_ptr(shared_evp_pkey
= load_pkey_pem(privkey
, multi_libctx
)))
483 worker
= thread_shared_evp_pkey
;
487 * We ensure we get an md from the default provider, and then unload the
488 * provider. This ensures the provider remains around but in a
491 sha256
= EVP_MD_fetch(multi_libctx
, "SHA2-256", NULL
);
492 OSSL_PROVIDER_unload(prov
);
494 worker
= thread_provider_load_unload
;
497 TEST_error("Invalid test index");
503 if (!TEST_true(run_thread(&thread1
, worker
))
504 || !TEST_true(run_thread(&thread2
, worker2
)))
509 if (!TEST_true(wait_for_thread(thread1
))
510 || !TEST_true(wait_for_thread(thread2
))
511 || !TEST_true(multi_success
))
518 OSSL_PROVIDER_unload(prov
);
519 OSSL_PROVIDER_unload(prov2
);
520 OSSL_LIB_CTX_free(multi_libctx
);
521 EVP_PKEY_free(shared_evp_pkey
);
522 shared_evp_pkey
= NULL
;
528 * This test attempts to load several providers at the same time, and if
529 * run with a thread sanitizer, should crash if the core provider code
530 * doesn't synchronize well enough.
532 #define MULTI_LOAD_THREADS 3
533 static void test_multi_load_worker(void)
537 (void)TEST_ptr(prov
= OSSL_PROVIDER_load(NULL
, "default"));
538 (void)TEST_true(OSSL_PROVIDER_unload(prov
));
541 static int test_multi_load(void)
543 thread_t threads
[MULTI_LOAD_THREADS
];
546 for (i
= 0; i
< MULTI_LOAD_THREADS
; i
++)
547 (void)TEST_true(run_thread(&threads
[i
], test_multi_load_worker
));
549 for (i
= 0; i
< MULTI_LOAD_THREADS
; i
++)
550 (void)TEST_true(wait_for_thread(threads
[i
]));
555 static int test_multi_default(void)
557 thread_t thread1
, thread2
;
559 OSSL_PROVIDER
*prov
= NULL
;
563 prov
= OSSL_PROVIDER_load(multi_libctx
, "default");
567 if (!TEST_true(run_thread(&thread1
, thread_multi_simple_fetch
))
568 || !TEST_true(run_thread(&thread2
, thread_multi_simple_fetch
)))
571 thread_multi_simple_fetch();
573 if (!TEST_true(wait_for_thread(thread1
))
574 || !TEST_true(wait_for_thread(thread2
))
575 || !TEST_true(multi_success
))
581 OSSL_PROVIDER_unload(prov
);
585 typedef enum OPTION_choice
{
588 OPT_FIPS
, OPT_CONFIG_FILE
,
592 const OPTIONS
*test_get_options(void)
594 static const OPTIONS options
[] = {
595 OPT_TEST_OPTIONS_DEFAULT_USAGE
,
596 { "fips", OPT_FIPS
, '-', "Test the FIPS provider" },
597 { "config", OPT_CONFIG_FILE
, '<',
598 "The configuration file to use for the libctx" },
604 int setup_tests(void)
609 while ((o
= opt_next()) != OPT_EOF
) {
614 case OPT_CONFIG_FILE
:
615 config_file
= opt_arg();
624 if (!TEST_ptr(datadir
= test_get_argument(0)))
627 privkey
= test_mk_file_path(datadir
, "rsakey.pem");
628 if (!TEST_ptr(privkey
))
631 /* Keep first to validate auto creation of default library context */
632 ADD_TEST(test_multi_default
);
636 ADD_TEST(test_thread_local
);
637 ADD_TEST(test_atomic
);
638 ADD_TEST(test_multi_load
);
639 ADD_ALL_TESTS(test_multi
, 6);
643 void cleanup_tests(void)
645 OPENSSL_free(privkey
);