2016-05-18 02:20:24 +08:00
|
|
|
/*
|
2021-01-28 20:54:57 +08:00
|
|
|
* Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2015-10-26 00:43:55 +08:00
|
|
|
*
|
2018-12-06 20:05:25 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:20:24 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2015-10-26 00:43:55 +08:00
|
|
|
*/
|
|
|
|
|
2021-02-26 00:27:46 +08:00
|
|
|
/* test_multi below tests the thread safety of a deprecated function */
|
|
|
|
#define OPENSSL_SUPPRESS_DEPRECATED
|
|
|
|
|
2016-06-26 19:40:15 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2020-12-10 23:39:58 +08:00
|
|
|
#include <string.h>
|
2015-10-26 00:43:55 +08:00
|
|
|
#include <openssl/crypto.h>
|
2021-03-20 20:49:08 +08:00
|
|
|
#include <openssl/rsa.h>
|
2020-12-10 23:39:58 +08:00
|
|
|
#include <openssl/aes.h>
|
|
|
|
#include <openssl/rsa.h>
|
2017-04-12 12:24:21 +08:00
|
|
|
#include "testutil.h"
|
2015-10-26 00:43:55 +08:00
|
|
|
|
2020-12-10 23:39:58 +08:00
|
|
|
static int do_fips = 0;
|
2021-01-26 21:30:06 +08:00
|
|
|
static char *privkey;
|
2021-05-13 08:34:42 +08:00
|
|
|
static char *config_file = NULL;
|
2020-12-10 23:39:58 +08:00
|
|
|
|
2015-10-26 00:43:55 +08:00
|
|
|
#if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
|
|
|
|
|
|
|
|
typedef unsigned int thread_t;
|
|
|
|
|
|
|
|
static int run_thread(thread_t *t, void (*f)(void))
|
|
|
|
{
|
|
|
|
f();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wait_for_thread(thread_t thread)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(OPENSSL_SYS_WINDOWS)
|
|
|
|
|
|
|
|
typedef HANDLE thread_t;
|
|
|
|
|
|
|
|
static DWORD WINAPI thread_run(LPVOID arg)
|
|
|
|
{
|
|
|
|
void (*f)(void);
|
|
|
|
|
|
|
|
*(void **) (&f) = arg;
|
|
|
|
|
|
|
|
f();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_thread(thread_t *t, void (*f)(void))
|
|
|
|
{
|
|
|
|
*t = CreateThread(NULL, 0, thread_run, *(void **) &f, 0, NULL);
|
|
|
|
return *t != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wait_for_thread(thread_t thread)
|
|
|
|
{
|
|
|
|
return WaitForSingleObject(thread, INFINITE) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
typedef pthread_t thread_t;
|
|
|
|
|
|
|
|
static void *thread_run(void *arg)
|
|
|
|
{
|
|
|
|
void (*f)(void);
|
|
|
|
|
|
|
|
*(void **) (&f) = arg;
|
|
|
|
|
|
|
|
f();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_thread(thread_t *t, void (*f)(void))
|
|
|
|
{
|
|
|
|
return pthread_create(t, NULL, thread_run, *(void **) &f) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wait_for_thread(thread_t thread)
|
|
|
|
{
|
|
|
|
return pthread_join(thread, NULL) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int test_lock(void)
|
|
|
|
{
|
|
|
|
CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
|
2021-03-18 08:29:05 +08:00
|
|
|
int res;
|
2015-10-26 00:43:55 +08:00
|
|
|
|
2021-03-18 08:29:05 +08:00
|
|
|
res = TEST_true(CRYPTO_THREAD_read_lock(lock))
|
|
|
|
&& TEST_true(CRYPTO_THREAD_unlock(lock));
|
2015-10-26 00:43:55 +08:00
|
|
|
|
|
|
|
CRYPTO_THREAD_lock_free(lock);
|
|
|
|
|
2021-03-18 08:29:05 +08:00
|
|
|
return res;
|
2015-10-26 00:43:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
|
|
|
|
static unsigned once_run_count = 0;
|
|
|
|
|
|
|
|
static void once_do_run(void)
|
|
|
|
{
|
|
|
|
once_run_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void once_run_thread_cb(void)
|
|
|
|
{
|
|
|
|
CRYPTO_THREAD_run_once(&once_run, once_do_run);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_once(void)
|
|
|
|
{
|
|
|
|
thread_t thread;
|
|
|
|
|
2017-04-12 12:24:21 +08:00
|
|
|
if (!TEST_true(run_thread(&thread, once_run_thread_cb))
|
|
|
|
|| !TEST_true(wait_for_thread(thread))
|
|
|
|
|| !CRYPTO_THREAD_run_once(&once_run, once_do_run)
|
|
|
|
|| !TEST_int_eq(once_run_count, 1))
|
2015-10-26 00:43:55 +08:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CRYPTO_THREAD_LOCAL thread_local_key;
|
|
|
|
static unsigned destructor_run_count = 0;
|
|
|
|
static int thread_local_thread_cb_ok = 0;
|
|
|
|
|
|
|
|
static void thread_local_destructor(void *arg)
|
|
|
|
{
|
|
|
|
unsigned *count;
|
|
|
|
|
|
|
|
if (arg == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
count = arg;
|
|
|
|
|
|
|
|
(*count)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void thread_local_thread_cb(void)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = CRYPTO_THREAD_get_local(&thread_local_key);
|
2017-04-12 12:24:21 +08:00
|
|
|
if (!TEST_ptr_null(ptr)
|
|
|
|
|| !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
|
|
|
|
&destructor_run_count)))
|
2015-10-26 00:43:55 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
ptr = CRYPTO_THREAD_get_local(&thread_local_key);
|
2017-04-12 12:24:21 +08:00
|
|
|
if (!TEST_ptr_eq(ptr, &destructor_run_count))
|
2015-10-26 00:43:55 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
thread_local_thread_cb_ok = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_thread_local(void)
|
|
|
|
{
|
|
|
|
thread_t thread;
|
|
|
|
void *ptr = NULL;
|
|
|
|
|
2017-04-12 12:24:21 +08:00
|
|
|
if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
|
|
|
|
thread_local_destructor)))
|
2015-10-26 00:43:55 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
ptr = CRYPTO_THREAD_get_local(&thread_local_key);
|
2017-04-12 12:24:21 +08:00
|
|
|
if (!TEST_ptr_null(ptr)
|
|
|
|
|| !TEST_true(run_thread(&thread, thread_local_thread_cb))
|
|
|
|
|| !TEST_true(wait_for_thread(thread))
|
|
|
|
|| !TEST_int_eq(thread_local_thread_cb_ok, 1))
|
2015-10-26 00:43:55 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
|
|
|
|
|
|
|
|
ptr = CRYPTO_THREAD_get_local(&thread_local_key);
|
2017-04-12 12:24:21 +08:00
|
|
|
if (!TEST_ptr_null(ptr))
|
2015-10-26 00:43:55 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
# if !defined(OPENSSL_SYS_WINDOWS)
|
2017-04-12 12:24:21 +08:00
|
|
|
if (!TEST_int_eq(destructor_run_count, 1))
|
2015-10-26 00:43:55 +08:00
|
|
|
return 0;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2017-04-12 12:24:21 +08:00
|
|
|
if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
|
2015-10-26 00:43:55 +08:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-12-23 19:35:54 +08:00
|
|
|
static int test_atomic(void)
|
|
|
|
{
|
|
|
|
int val = 0, ret = 0, testresult = 0;
|
|
|
|
uint64_t val64 = 1, ret64 = 0;
|
|
|
|
CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
|
|
|
|
|
|
|
|
if (!TEST_ptr(lock))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) {
|
|
|
|
/* This succeeds therefore we're on a platform with lockless atomics */
|
|
|
|
if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
|
|
|
|
goto err;
|
|
|
|
} else {
|
|
|
|
/* This failed therefore we're on a platform without lockless atomics */
|
|
|
|
if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
val = 0;
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock)))
|
|
|
|
goto err;
|
|
|
|
if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) {
|
|
|
|
/* This succeeds therefore we're on a platform with lockless atomics */
|
|
|
|
if (!TEST_uint_eq((unsigned int)val64, 3)
|
|
|
|
|| !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
|
|
|
|
goto err;
|
|
|
|
} else {
|
|
|
|
/* This failed therefore we're on a platform without lockless atomics */
|
|
|
|
if (!TEST_uint_eq((unsigned int)val64, 1)
|
|
|
|
|| !TEST_int_eq((unsigned int)ret64, 0))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
val64 = 1;
|
|
|
|
ret64 = 0;
|
|
|
|
|
|
|
|
if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock)))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!TEST_uint_eq((unsigned int)val64, 3)
|
|
|
|
|| !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret64 = 0;
|
|
|
|
if (CRYPTO_atomic_load(&val64, &ret64, NULL)) {
|
|
|
|
/* This succeeds therefore we're on a platform with lockless atomics */
|
|
|
|
if (!TEST_uint_eq((unsigned int)val64, 3)
|
|
|
|
|| !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
|
|
|
|
goto err;
|
|
|
|
} else {
|
|
|
|
/* This failed therefore we're on a platform without lockless atomics */
|
|
|
|
if (!TEST_uint_eq((unsigned int)val64, 3)
|
|
|
|
|| !TEST_int_eq((unsigned int)ret64, 0))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret64 = 0;
|
|
|
|
if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock)))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!TEST_uint_eq((unsigned int)val64, 3)
|
|
|
|
|| !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
testresult = 1;
|
|
|
|
err:
|
|
|
|
CRYPTO_THREAD_lock_free(lock);
|
|
|
|
return testresult;
|
|
|
|
}
|
|
|
|
|
2020-12-10 23:39:58 +08:00
|
|
|
static OSSL_LIB_CTX *multi_libctx = NULL;
|
|
|
|
static int multi_success;
|
|
|
|
|
2021-01-12 01:01:07 +08:00
|
|
|
static void thread_general_worker(void)
|
2020-12-10 23:39:58 +08:00
|
|
|
{
|
|
|
|
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
|
|
|
|
EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
|
|
|
|
EVP_CIPHER_CTX *cipherctx = EVP_CIPHER_CTX_new();
|
|
|
|
EVP_CIPHER *ciph = EVP_CIPHER_fetch(multi_libctx, "AES-128-CBC", NULL);
|
|
|
|
const char *message = "Hello World";
|
|
|
|
size_t messlen = strlen(message);
|
|
|
|
/* Should be big enough for encryption output too */
|
|
|
|
unsigned char out[EVP_MAX_MD_SIZE];
|
|
|
|
const unsigned char key[AES_BLOCK_SIZE] = {
|
|
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
|
|
|
|
0x0c, 0x0d, 0x0e, 0x0f
|
|
|
|
};
|
|
|
|
const unsigned char iv[AES_BLOCK_SIZE] = {
|
|
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
|
|
|
|
0x0c, 0x0d, 0x0e, 0x0f
|
|
|
|
};
|
|
|
|
unsigned int mdoutl;
|
|
|
|
int ciphoutl;
|
|
|
|
EVP_PKEY *pkey = NULL;
|
|
|
|
int testresult = 0;
|
|
|
|
int i, isfips;
|
|
|
|
|
|
|
|
isfips = OSSL_PROVIDER_available(multi_libctx, "fips");
|
|
|
|
|
|
|
|
if (!TEST_ptr(mdctx)
|
|
|
|
|| !TEST_ptr(md)
|
|
|
|
|| !TEST_ptr(cipherctx)
|
|
|
|
|| !TEST_ptr(ciph))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Do some work */
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
if (!TEST_true(EVP_DigestInit_ex(mdctx, md, NULL))
|
|
|
|
|| !TEST_true(EVP_DigestUpdate(mdctx, message, messlen))
|
|
|
|
|| !TEST_true(EVP_DigestFinal(mdctx, out, &mdoutl)))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
if (!TEST_true(EVP_EncryptInit_ex(cipherctx, ciph, NULL, key, iv))
|
|
|
|
|| !TEST_true(EVP_EncryptUpdate(cipherctx, out, &ciphoutl,
|
|
|
|
(unsigned char *)message,
|
|
|
|
messlen))
|
|
|
|
|| !TEST_true(EVP_EncryptFinal(cipherctx, out, &ciphoutl)))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2021-03-20 20:49:08 +08:00
|
|
|
/*
|
|
|
|
* We want the test to run quickly - not securely.
|
|
|
|
* Therefore we use an insecure bit length where we can (512).
|
|
|
|
* In the FIPS module though we must use a longer length.
|
|
|
|
*/
|
|
|
|
pkey = EVP_PKEY_Q_keygen(multi_libctx, NULL, "RSA", isfips ? 2048 : 512);
|
|
|
|
if (!TEST_ptr(pkey))
|
2020-12-10 23:39:58 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
testresult = 1;
|
|
|
|
err:
|
|
|
|
EVP_MD_CTX_free(mdctx);
|
|
|
|
EVP_MD_free(md);
|
|
|
|
EVP_CIPHER_CTX_free(cipherctx);
|
|
|
|
EVP_CIPHER_free(ciph);
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
if (!testresult)
|
|
|
|
multi_success = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-12 01:01:07 +08:00
|
|
|
static void thread_multi_simple_fetch(void)
|
|
|
|
{
|
2021-03-30 10:27:44 +08:00
|
|
|
EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
|
2021-01-12 01:01:07 +08:00
|
|
|
|
|
|
|
if (md != NULL)
|
|
|
|
EVP_MD_free(md);
|
|
|
|
else
|
|
|
|
multi_success = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:30:06 +08:00
|
|
|
static EVP_PKEY *shared_evp_pkey = NULL;
|
|
|
|
|
|
|
|
static void thread_shared_evp_pkey(void)
|
|
|
|
{
|
|
|
|
char *msg = "Hello World";
|
|
|
|
unsigned char ctbuf[256];
|
|
|
|
unsigned char ptbuf[256];
|
|
|
|
size_t ptlen = sizeof(ptbuf), ctlen = sizeof(ctbuf);
|
|
|
|
EVP_PKEY_CTX *ctx = NULL;
|
|
|
|
int success = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 1 + do_fips; i++) {
|
|
|
|
if (i > 0)
|
|
|
|
EVP_PKEY_CTX_free(ctx);
|
|
|
|
ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey,
|
|
|
|
i == 0 ? "provider=default"
|
|
|
|
: "provider=fips");
|
|
|
|
if (!TEST_ptr(ctx))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx), 0)
|
|
|
|
|| !TEST_int_ge(EVP_PKEY_encrypt(ctx, ctbuf, &ctlen,
|
|
|
|
(unsigned char *)msg, strlen(msg)),
|
|
|
|
0))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
EVP_PKEY_CTX_free(ctx);
|
|
|
|
ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey, NULL);
|
|
|
|
|
|
|
|
if (!TEST_ptr(ctx))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx), 0)
|
|
|
|
|| !TEST_int_ge(EVP_PKEY_decrypt(ctx, ptbuf, &ptlen, ctbuf, ctlen),
|
|
|
|
0)
|
|
|
|
|| !TEST_mem_eq(msg, strlen(msg), ptbuf, ptlen))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
success = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
EVP_PKEY_CTX_free(ctx);
|
|
|
|
if (!success)
|
|
|
|
multi_success = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-30 01:25:33 +08:00
|
|
|
static void thread_downgrade_shared_evp_pkey(void)
|
|
|
|
{
|
|
|
|
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
|
|
|
/*
|
|
|
|
* This test is only relevant for deprecated functions that perform
|
|
|
|
* downgrading
|
|
|
|
*/
|
2021-03-09 04:36:10 +08:00
|
|
|
if (EVP_PKEY_get0_RSA(shared_evp_pkey) == NULL)
|
2021-01-30 01:25:33 +08:00
|
|
|
multi_success = 0;
|
|
|
|
#else
|
|
|
|
/* Shouldn't ever get here */
|
|
|
|
multi_success = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-04-23 21:10:07 +08:00
|
|
|
static void thread_provider_load_unload(void)
|
|
|
|
{
|
|
|
|
OSSL_PROVIDER *deflt = OSSL_PROVIDER_load(multi_libctx, "default");
|
|
|
|
|
|
|
|
if (!TEST_ptr(deflt)
|
|
|
|
|| !TEST_true(OSSL_PROVIDER_available(multi_libctx, "default")))
|
|
|
|
multi_success = 0;
|
|
|
|
|
|
|
|
OSSL_PROVIDER_unload(deflt);
|
|
|
|
}
|
2021-01-30 01:25:33 +08:00
|
|
|
|
2020-12-10 23:39:58 +08:00
|
|
|
/*
|
|
|
|
* Do work in multiple worker threads at the same time.
|
2021-01-12 01:01:07 +08:00
|
|
|
* Test 0: General worker, using the default provider
|
|
|
|
* Test 1: General worker, using the fips provider
|
|
|
|
* Test 2: Simple fetch worker
|
2021-01-30 01:25:33 +08:00
|
|
|
* Test 3: Worker downgrading a shared EVP_PKEY
|
|
|
|
* Test 4: Worker using a shared EVP_PKEY
|
2021-04-23 21:10:07 +08:00
|
|
|
* Test 5: Workder loading and unloading a provider
|
2020-12-10 23:39:58 +08:00
|
|
|
*/
|
|
|
|
static int test_multi(int idx)
|
|
|
|
{
|
|
|
|
thread_t thread1, thread2;
|
|
|
|
int testresult = 0;
|
2021-01-26 21:30:06 +08:00
|
|
|
OSSL_PROVIDER *prov = NULL, *prov2 = NULL;
|
2021-01-30 01:25:33 +08:00
|
|
|
void (*worker)(void) = NULL;
|
|
|
|
void (*worker2)(void) = NULL;
|
2021-04-23 21:10:07 +08:00
|
|
|
EVP_MD *sha256 = NULL;
|
2020-12-10 23:39:58 +08:00
|
|
|
|
|
|
|
if (idx == 1 && !do_fips)
|
|
|
|
return TEST_skip("FIPS not supported");
|
|
|
|
|
2021-01-30 01:25:33 +08:00
|
|
|
#ifdef OPENSSL_NO_DEPRECATED_3_0
|
|
|
|
if (idx == 3)
|
|
|
|
return TEST_skip("Skipping tests for deprected functions");
|
|
|
|
#endif
|
|
|
|
|
2020-12-10 23:39:58 +08:00
|
|
|
multi_success = 1;
|
2021-05-13 08:34:42 +08:00
|
|
|
if (!TEST_true(test_get_libctx(&multi_libctx, NULL, config_file,
|
|
|
|
NULL, NULL)))
|
|
|
|
return 0;
|
|
|
|
|
2021-01-12 01:01:07 +08:00
|
|
|
prov = OSSL_PROVIDER_load(multi_libctx, (idx == 1) ? "fips" : "default");
|
2020-12-10 23:39:58 +08:00
|
|
|
if (!TEST_ptr(prov))
|
|
|
|
goto err;
|
|
|
|
|
2021-01-12 01:01:07 +08:00
|
|
|
switch (idx) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
worker = thread_general_worker;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
worker = thread_multi_simple_fetch;
|
|
|
|
break;
|
2021-01-26 21:30:06 +08:00
|
|
|
case 3:
|
2021-01-30 01:25:33 +08:00
|
|
|
worker2 = thread_downgrade_shared_evp_pkey;
|
|
|
|
/* fall through */
|
|
|
|
case 4:
|
2021-01-26 21:30:06 +08:00
|
|
|
/*
|
|
|
|
* If available we have both the default and fips providers for this
|
|
|
|
* test
|
|
|
|
*/
|
|
|
|
if (do_fips
|
|
|
|
&& !TEST_ptr(prov2 = OSSL_PROVIDER_load(multi_libctx, "fips")))
|
|
|
|
goto err;
|
|
|
|
if (!TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx)))
|
|
|
|
goto err;
|
|
|
|
worker = thread_shared_evp_pkey;
|
|
|
|
break;
|
2021-04-23 21:10:07 +08:00
|
|
|
case 5:
|
|
|
|
/*
|
|
|
|
* We ensure we get an md from the default provider, and then unload the
|
|
|
|
* provider. This ensures the provider remains around but in a
|
|
|
|
* deactivated state.
|
|
|
|
*/
|
|
|
|
sha256 = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
|
|
|
|
OSSL_PROVIDER_unload(prov);
|
|
|
|
prov = NULL;
|
|
|
|
worker = thread_provider_load_unload;
|
|
|
|
break;
|
2021-01-12 01:01:07 +08:00
|
|
|
default:
|
|
|
|
TEST_error("Invalid test index");
|
|
|
|
goto err;
|
|
|
|
}
|
2021-01-30 01:25:33 +08:00
|
|
|
if (worker2 == NULL)
|
|
|
|
worker2 = worker;
|
2021-01-12 01:01:07 +08:00
|
|
|
|
|
|
|
if (!TEST_true(run_thread(&thread1, worker))
|
2021-01-30 01:25:33 +08:00
|
|
|
|| !TEST_true(run_thread(&thread2, worker2)))
|
2020-12-10 23:39:58 +08:00
|
|
|
goto err;
|
|
|
|
|
2021-01-12 01:01:07 +08:00
|
|
|
worker();
|
2020-12-10 23:39:58 +08:00
|
|
|
|
|
|
|
if (!TEST_true(wait_for_thread(thread1))
|
|
|
|
|| !TEST_true(wait_for_thread(thread2))
|
|
|
|
|| !TEST_true(multi_success))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
testresult = 1;
|
|
|
|
|
|
|
|
err:
|
2021-04-23 21:10:07 +08:00
|
|
|
EVP_MD_free(sha256);
|
2020-12-10 23:39:58 +08:00
|
|
|
OSSL_PROVIDER_unload(prov);
|
2021-01-26 21:30:06 +08:00
|
|
|
OSSL_PROVIDER_unload(prov2);
|
2020-12-10 23:39:58 +08:00
|
|
|
OSSL_LIB_CTX_free(multi_libctx);
|
2021-01-26 21:30:06 +08:00
|
|
|
EVP_PKEY_free(shared_evp_pkey);
|
|
|
|
shared_evp_pkey = NULL;
|
2021-03-30 10:27:44 +08:00
|
|
|
multi_libctx = NULL;
|
2020-12-10 23:39:58 +08:00
|
|
|
return testresult;
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:31:34 +08:00
|
|
|
/*
|
|
|
|
* This test attempts to load several providers at the same time, and if
|
|
|
|
* run with a thread sanitizer, should crash if the core provider code
|
|
|
|
* doesn't synchronize well enough.
|
|
|
|
*/
|
|
|
|
#define MULTI_LOAD_THREADS 3
|
|
|
|
static void test_multi_load_worker(void)
|
|
|
|
{
|
|
|
|
OSSL_PROVIDER *prov;
|
|
|
|
|
2021-03-19 07:46:03 +08:00
|
|
|
(void)TEST_ptr(prov = OSSL_PROVIDER_load(NULL, "default"));
|
|
|
|
(void)TEST_true(OSSL_PROVIDER_unload(prov));
|
2021-03-01 23:31:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int test_multi_load(void)
|
|
|
|
{
|
|
|
|
thread_t threads[MULTI_LOAD_THREADS];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MULTI_LOAD_THREADS; i++)
|
2021-03-19 07:46:03 +08:00
|
|
|
(void)TEST_true(run_thread(&threads[i], test_multi_load_worker));
|
2021-03-01 23:31:34 +08:00
|
|
|
|
|
|
|
for (i = 0; i < MULTI_LOAD_THREADS; i++)
|
2021-03-19 07:46:03 +08:00
|
|
|
(void)TEST_true(wait_for_thread(threads[i]));
|
2021-03-01 23:31:34 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-30 10:27:44 +08:00
|
|
|
static int test_multi_default(void)
|
|
|
|
{
|
|
|
|
thread_t thread1, thread2;
|
|
|
|
int testresult = 0;
|
|
|
|
OSSL_PROVIDER *prov = NULL;
|
|
|
|
|
|
|
|
multi_success = 1;
|
|
|
|
multi_libctx = NULL;
|
|
|
|
prov = OSSL_PROVIDER_load(multi_libctx, "default");
|
|
|
|
if (!TEST_ptr(prov))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!TEST_true(run_thread(&thread1, thread_multi_simple_fetch))
|
|
|
|
|| !TEST_true(run_thread(&thread2, thread_multi_simple_fetch)))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
thread_multi_simple_fetch();
|
|
|
|
|
|
|
|
if (!TEST_true(wait_for_thread(thread1))
|
|
|
|
|| !TEST_true(wait_for_thread(thread2))
|
|
|
|
|| !TEST_true(multi_success))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
testresult = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
OSSL_PROVIDER_unload(prov);
|
|
|
|
return testresult;
|
|
|
|
}
|
|
|
|
|
2020-12-10 23:39:58 +08:00
|
|
|
typedef enum OPTION_choice {
|
|
|
|
OPT_ERR = -1,
|
|
|
|
OPT_EOF = 0,
|
2021-05-13 08:34:42 +08:00
|
|
|
OPT_FIPS, OPT_CONFIG_FILE,
|
2020-12-10 23:39:58 +08:00
|
|
|
OPT_TEST_ENUM
|
|
|
|
} OPTION_CHOICE;
|
|
|
|
|
|
|
|
const OPTIONS *test_get_options(void)
|
|
|
|
{
|
|
|
|
static const OPTIONS options[] = {
|
|
|
|
OPT_TEST_OPTIONS_DEFAULT_USAGE,
|
|
|
|
{ "fips", OPT_FIPS, '-', "Test the FIPS provider" },
|
2021-05-13 08:34:42 +08:00
|
|
|
{ "config", OPT_CONFIG_FILE, '<',
|
|
|
|
"The configuration file to use for the libctx" },
|
2020-12-10 23:39:58 +08:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2017-07-18 09:48:27 +08:00
|
|
|
int setup_tests(void)
|
2015-10-26 00:43:55 +08:00
|
|
|
{
|
2020-12-10 23:39:58 +08:00
|
|
|
OPTION_CHOICE o;
|
2021-01-26 21:30:06 +08:00
|
|
|
char *datadir;
|
2020-12-10 23:39:58 +08:00
|
|
|
|
|
|
|
while ((o = opt_next()) != OPT_EOF) {
|
|
|
|
switch (o) {
|
|
|
|
case OPT_FIPS:
|
|
|
|
do_fips = 1;
|
|
|
|
break;
|
2021-05-13 08:34:42 +08:00
|
|
|
case OPT_CONFIG_FILE:
|
|
|
|
config_file = opt_arg();
|
|
|
|
break;
|
2020-12-10 23:39:58 +08:00
|
|
|
case OPT_TEST_CASES:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:30:06 +08:00
|
|
|
if (!TEST_ptr(datadir = test_get_argument(0)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
privkey = test_mk_file_path(datadir, "rsakey.pem");
|
|
|
|
if (!TEST_ptr(privkey))
|
|
|
|
return 0;
|
|
|
|
|
2021-03-30 10:27:44 +08:00
|
|
|
/* Keep first to validate auto creation of default library context */
|
|
|
|
ADD_TEST(test_multi_default);
|
|
|
|
|
2017-04-12 12:24:21 +08:00
|
|
|
ADD_TEST(test_lock);
|
|
|
|
ADD_TEST(test_once);
|
|
|
|
ADD_TEST(test_thread_local);
|
2020-12-23 19:35:54 +08:00
|
|
|
ADD_TEST(test_atomic);
|
2021-03-01 23:31:34 +08:00
|
|
|
ADD_TEST(test_multi_load);
|
2021-04-23 21:10:07 +08:00
|
|
|
ADD_ALL_TESTS(test_multi, 6);
|
2017-07-18 09:48:27 +08:00
|
|
|
return 1;
|
2015-10-26 00:43:55 +08:00
|
|
|
}
|
2021-01-26 21:30:06 +08:00
|
|
|
|
|
|
|
void cleanup_tests(void)
|
|
|
|
{
|
|
|
|
OPENSSL_free(privkey);
|
|
|
|
}
|