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>
23 #include "threadstest.h"
25 static int do_fips
= 0;
27 static char *config_file
= NULL
;
29 static int test_lock(void)
31 CRYPTO_RWLOCK
*lock
= CRYPTO_THREAD_lock_new();
34 res
= TEST_true(CRYPTO_THREAD_read_lock(lock
))
35 && TEST_true(CRYPTO_THREAD_unlock(lock
));
37 CRYPTO_THREAD_lock_free(lock
);
42 static CRYPTO_ONCE once_run
= CRYPTO_ONCE_STATIC_INIT
;
43 static unsigned once_run_count
= 0;
45 static void once_do_run(void)
50 static void once_run_thread_cb(void)
52 CRYPTO_THREAD_run_once(&once_run
, once_do_run
);
55 static int test_once(void)
59 if (!TEST_true(run_thread(&thread
, once_run_thread_cb
))
60 || !TEST_true(wait_for_thread(thread
))
61 || !CRYPTO_THREAD_run_once(&once_run
, once_do_run
)
62 || !TEST_int_eq(once_run_count
, 1))
67 static CRYPTO_THREAD_LOCAL thread_local_key
;
68 static unsigned destructor_run_count
= 0;
69 static int thread_local_thread_cb_ok
= 0;
71 static void thread_local_destructor(void *arg
)
83 static void thread_local_thread_cb(void)
87 ptr
= CRYPTO_THREAD_get_local(&thread_local_key
);
88 if (!TEST_ptr_null(ptr
)
89 || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key
,
90 &destructor_run_count
)))
93 ptr
= CRYPTO_THREAD_get_local(&thread_local_key
);
94 if (!TEST_ptr_eq(ptr
, &destructor_run_count
))
97 thread_local_thread_cb_ok
= 1;
100 static int test_thread_local(void)
105 if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key
,
106 thread_local_destructor
)))
109 ptr
= CRYPTO_THREAD_get_local(&thread_local_key
);
110 if (!TEST_ptr_null(ptr
)
111 || !TEST_true(run_thread(&thread
, thread_local_thread_cb
))
112 || !TEST_true(wait_for_thread(thread
))
113 || !TEST_int_eq(thread_local_thread_cb_ok
, 1))
116 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
118 ptr
= CRYPTO_THREAD_get_local(&thread_local_key
);
119 if (!TEST_ptr_null(ptr
))
122 # if !defined(OPENSSL_SYS_WINDOWS)
123 if (!TEST_int_eq(destructor_run_count
, 1))
128 if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key
)))
133 static int test_atomic(void)
135 int val
= 0, ret
= 0, testresult
= 0;
136 uint64_t val64
= 1, ret64
= 0;
137 CRYPTO_RWLOCK
*lock
= CRYPTO_THREAD_lock_new();
142 if (CRYPTO_atomic_add(&val
, 1, &ret
, NULL
)) {
143 /* This succeeds therefore we're on a platform with lockless atomics */
144 if (!TEST_int_eq(val
, 1) || !TEST_int_eq(val
, ret
))
147 /* This failed therefore we're on a platform without lockless atomics */
148 if (!TEST_int_eq(val
, 0) || !TEST_int_eq(val
, ret
))
154 if (!TEST_true(CRYPTO_atomic_add(&val
, 1, &ret
, lock
)))
156 if (!TEST_int_eq(val
, 1) || !TEST_int_eq(val
, ret
))
159 if (CRYPTO_atomic_or(&val64
, 2, &ret64
, NULL
)) {
160 /* This succeeds therefore we're on a platform with lockless atomics */
161 if (!TEST_uint_eq((unsigned int)val64
, 3)
162 || !TEST_uint_eq((unsigned int)val64
, (unsigned int)ret64
))
165 /* This failed therefore we're on a platform without lockless atomics */
166 if (!TEST_uint_eq((unsigned int)val64
, 1)
167 || !TEST_int_eq((unsigned int)ret64
, 0))
173 if (!TEST_true(CRYPTO_atomic_or(&val64
, 2, &ret64
, lock
)))
176 if (!TEST_uint_eq((unsigned int)val64
, 3)
177 || !TEST_uint_eq((unsigned int)val64
, (unsigned int)ret64
))
181 if (CRYPTO_atomic_load(&val64
, &ret64
, NULL
)) {
182 /* This succeeds therefore we're on a platform with lockless atomics */
183 if (!TEST_uint_eq((unsigned int)val64
, 3)
184 || !TEST_uint_eq((unsigned int)val64
, (unsigned int)ret64
))
187 /* This failed therefore we're on a platform without lockless atomics */
188 if (!TEST_uint_eq((unsigned int)val64
, 3)
189 || !TEST_int_eq((unsigned int)ret64
, 0))
194 if (!TEST_true(CRYPTO_atomic_load(&val64
, &ret64
, lock
)))
197 if (!TEST_uint_eq((unsigned int)val64
, 3)
198 || !TEST_uint_eq((unsigned int)val64
, (unsigned int)ret64
))
203 CRYPTO_THREAD_lock_free(lock
);
207 static OSSL_LIB_CTX
*multi_libctx
= NULL
;
208 static int multi_success
;
210 static void thread_general_worker(void)
212 EVP_MD_CTX
*mdctx
= EVP_MD_CTX_new();
213 EVP_MD
*md
= EVP_MD_fetch(multi_libctx
, "SHA2-256", NULL
);
214 EVP_CIPHER_CTX
*cipherctx
= EVP_CIPHER_CTX_new();
215 EVP_CIPHER
*ciph
= EVP_CIPHER_fetch(multi_libctx
, "AES-128-CBC", NULL
);
216 const char *message
= "Hello World";
217 size_t messlen
= strlen(message
);
218 /* Should be big enough for encryption output too */
219 unsigned char out
[EVP_MAX_MD_SIZE
];
220 const unsigned char key
[AES_BLOCK_SIZE
] = {
221 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
222 0x0c, 0x0d, 0x0e, 0x0f
224 const unsigned char iv
[AES_BLOCK_SIZE
] = {
225 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
226 0x0c, 0x0d, 0x0e, 0x0f
230 EVP_PKEY
*pkey
= NULL
;
234 isfips
= OSSL_PROVIDER_available(multi_libctx
, "fips");
238 || !TEST_ptr(cipherctx
)
243 for (i
= 0; i
< 5; i
++) {
244 if (!TEST_true(EVP_DigestInit_ex(mdctx
, md
, NULL
))
245 || !TEST_true(EVP_DigestUpdate(mdctx
, message
, messlen
))
246 || !TEST_true(EVP_DigestFinal(mdctx
, out
, &mdoutl
)))
249 for (i
= 0; i
< 5; i
++) {
250 if (!TEST_true(EVP_EncryptInit_ex(cipherctx
, ciph
, NULL
, key
, iv
))
251 || !TEST_true(EVP_EncryptUpdate(cipherctx
, out
, &ciphoutl
,
252 (unsigned char *)message
,
254 || !TEST_true(EVP_EncryptFinal(cipherctx
, out
, &ciphoutl
)))
259 * We want the test to run quickly - not securely.
260 * Therefore we use an insecure bit length where we can (512).
261 * In the FIPS module though we must use a longer length.
263 pkey
= EVP_PKEY_Q_keygen(multi_libctx
, NULL
, "RSA", isfips
? 2048 : 512);
269 EVP_MD_CTX_free(mdctx
);
271 EVP_CIPHER_CTX_free(cipherctx
);
272 EVP_CIPHER_free(ciph
);
278 static void thread_multi_simple_fetch(void)
280 EVP_MD
*md
= EVP_MD_fetch(multi_libctx
, "SHA2-256", NULL
);
288 static EVP_PKEY
*shared_evp_pkey
= NULL
;
290 static void thread_shared_evp_pkey(void)
292 char *msg
= "Hello World";
293 unsigned char ctbuf
[256];
294 unsigned char ptbuf
[256];
295 size_t ptlen
= sizeof(ptbuf
), ctlen
= sizeof(ctbuf
);
296 EVP_PKEY_CTX
*ctx
= NULL
;
300 for (i
= 0; i
< 1 + do_fips
; i
++) {
302 EVP_PKEY_CTX_free(ctx
);
303 ctx
= EVP_PKEY_CTX_new_from_pkey(multi_libctx
, shared_evp_pkey
,
304 i
== 0 ? "provider=default"
309 if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx
), 0)
310 || !TEST_int_ge(EVP_PKEY_encrypt(ctx
, ctbuf
, &ctlen
,
311 (unsigned char *)msg
, strlen(msg
)),
315 EVP_PKEY_CTX_free(ctx
);
316 ctx
= EVP_PKEY_CTX_new_from_pkey(multi_libctx
, shared_evp_pkey
, NULL
);
321 if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx
), 0)
322 || !TEST_int_ge(EVP_PKEY_decrypt(ctx
, ptbuf
, &ptlen
, ctbuf
, ctlen
),
324 || !TEST_mem_eq(msg
, strlen(msg
), ptbuf
, ptlen
))
331 EVP_PKEY_CTX_free(ctx
);
336 static void thread_downgrade_shared_evp_pkey(void)
338 #ifndef OPENSSL_NO_DEPRECATED_3_0
340 * This test is only relevant for deprecated functions that perform
343 if (EVP_PKEY_get0_RSA(shared_evp_pkey
) == NULL
)
346 /* Shouldn't ever get here */
351 static void thread_provider_load_unload(void)
353 OSSL_PROVIDER
*deflt
= OSSL_PROVIDER_load(multi_libctx
, "default");
356 || !TEST_true(OSSL_PROVIDER_available(multi_libctx
, "default")))
359 OSSL_PROVIDER_unload(deflt
);
363 * Do work in multiple worker threads at the same time.
364 * Test 0: General worker, using the default provider
365 * Test 1: General worker, using the fips provider
366 * Test 2: Simple fetch worker
367 * Test 3: Worker downgrading a shared EVP_PKEY
368 * Test 4: Worker using a shared EVP_PKEY
369 * Test 5: Workder loading and unloading a provider
371 static int test_multi(int idx
)
373 thread_t thread1
, thread2
;
375 OSSL_PROVIDER
*prov
= NULL
, *prov2
= NULL
;
376 void (*worker
)(void) = NULL
;
377 void (*worker2
)(void) = NULL
;
378 EVP_MD
*sha256
= NULL
;
380 if (idx
== 1 && !do_fips
)
381 return TEST_skip("FIPS not supported");
383 #ifdef OPENSSL_NO_DEPRECATED_3_0
385 return TEST_skip("Skipping tests for deprected functions");
389 if (!TEST_true(test_get_libctx(&multi_libctx
, NULL
, config_file
,
393 prov
= OSSL_PROVIDER_load(multi_libctx
, (idx
== 1) ? "fips" : "default");
400 worker
= thread_general_worker
;
403 worker
= thread_multi_simple_fetch
;
406 worker2
= thread_downgrade_shared_evp_pkey
;
410 * If available we have both the default and fips providers for this
414 && !TEST_ptr(prov2
= OSSL_PROVIDER_load(multi_libctx
, "fips")))
416 if (!TEST_ptr(shared_evp_pkey
= load_pkey_pem(privkey
, multi_libctx
)))
418 worker
= thread_shared_evp_pkey
;
422 * We ensure we get an md from the default provider, and then unload the
423 * provider. This ensures the provider remains around but in a
426 sha256
= EVP_MD_fetch(multi_libctx
, "SHA2-256", NULL
);
427 OSSL_PROVIDER_unload(prov
);
429 worker
= thread_provider_load_unload
;
432 TEST_error("Invalid test index");
438 if (!TEST_true(run_thread(&thread1
, worker
))
439 || !TEST_true(run_thread(&thread2
, worker2
)))
444 if (!TEST_true(wait_for_thread(thread1
))
445 || !TEST_true(wait_for_thread(thread2
))
446 || !TEST_true(multi_success
))
453 OSSL_PROVIDER_unload(prov
);
454 OSSL_PROVIDER_unload(prov2
);
455 OSSL_LIB_CTX_free(multi_libctx
);
456 EVP_PKEY_free(shared_evp_pkey
);
457 shared_evp_pkey
= NULL
;
463 * This test attempts to load several providers at the same time, and if
464 * run with a thread sanitizer, should crash if the core provider code
465 * doesn't synchronize well enough.
467 #define MULTI_LOAD_THREADS 3
468 static void test_multi_load_worker(void)
472 (void)TEST_ptr(prov
= OSSL_PROVIDER_load(NULL
, "default"));
473 (void)TEST_true(OSSL_PROVIDER_unload(prov
));
476 static int test_multi_load(void)
478 thread_t threads
[MULTI_LOAD_THREADS
];
481 for (i
= 0; i
< MULTI_LOAD_THREADS
; i
++)
482 (void)TEST_true(run_thread(&threads
[i
], test_multi_load_worker
));
484 for (i
= 0; i
< MULTI_LOAD_THREADS
; i
++)
485 (void)TEST_true(wait_for_thread(threads
[i
]));
490 static int test_multi_default(void)
492 thread_t thread1
, thread2
;
494 OSSL_PROVIDER
*prov
= NULL
;
498 prov
= OSSL_PROVIDER_load(multi_libctx
, "default");
502 if (!TEST_true(run_thread(&thread1
, thread_multi_simple_fetch
))
503 || !TEST_true(run_thread(&thread2
, thread_multi_simple_fetch
)))
506 thread_multi_simple_fetch();
508 if (!TEST_true(wait_for_thread(thread1
))
509 || !TEST_true(wait_for_thread(thread2
))
510 || !TEST_true(multi_success
))
516 OSSL_PROVIDER_unload(prov
);
520 typedef enum OPTION_choice
{
523 OPT_FIPS
, OPT_CONFIG_FILE
,
527 const OPTIONS
*test_get_options(void)
529 static const OPTIONS options
[] = {
530 OPT_TEST_OPTIONS_DEFAULT_USAGE
,
531 { "fips", OPT_FIPS
, '-', "Test the FIPS provider" },
532 { "config", OPT_CONFIG_FILE
, '<',
533 "The configuration file to use for the libctx" },
539 int setup_tests(void)
544 while ((o
= opt_next()) != OPT_EOF
) {
549 case OPT_CONFIG_FILE
:
550 config_file
= opt_arg();
559 if (!TEST_ptr(datadir
= test_get_argument(0)))
562 privkey
= test_mk_file_path(datadir
, "rsakey.pem");
563 if (!TEST_ptr(privkey
))
566 /* Keep first to validate auto creation of default library context */
567 ADD_TEST(test_multi_default
);
571 ADD_TEST(test_thread_local
);
572 ADD_TEST(test_atomic
);
573 ADD_TEST(test_multi_load
);
574 ADD_ALL_TESTS(test_multi
, 6);
578 void cleanup_tests(void)
580 OPENSSL_free(privkey
);