-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See #272. This commit adds a lock to run `c_obj_create` and `OBJ_create` thread-safely in `OQS_PROVIDER_ENTRYPOINT_NAME`,
- Loading branch information
thb-sb
authored and
thb-sb
committed
Oct 24, 2023
1 parent
8a96fed
commit e05b809
Showing
3 changed files
with
173 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// SPDX-License-Identifier: Apache-2.0 AND MIT | ||
|
||
#ifdef _WIN32 | ||
|
||
# include <Windows.h> | ||
|
||
#else | ||
|
||
# include <pthread.h> | ||
|
||
#endif // ifdef _WIN32 | ||
|
||
#include <openssl/crypto.h> | ||
#include <openssl/provider.h> | ||
|
||
#include "test_common.h" | ||
|
||
static const size_t N_THREADS = 6; | ||
|
||
static const char *kModuleName = NULL; | ||
static const char *kConfigFile = NULL; | ||
|
||
/** \brief Loads the oqs-provider in an `OSSL_LIB_CTX` object. */ | ||
static int load_oqs_provider_thread(OSSL_LIB_CTX *libctx) | ||
{ | ||
OSSL_PROVIDER *default_provider = NULL; | ||
OSSL_PROVIDER *oqs_provider = NULL; | ||
int ret = -1; | ||
|
||
#ifdef OQS_PROVIDER_STATIC | ||
if ((default_provider = OSSL_PROVIDER_load(libctx, "default")) == NULL) { | ||
goto end; | ||
} | ||
if (OSSL_PROVIDER_add_builtin(libctx, "oqsprovider", | ||
OQS_PROVIDER_ENTRYPOINT_NAME) | ||
!= 1) { | ||
goto unload_default_provider; | ||
} | ||
if ((oqs_provider = OSSL_PROVIDER_load(libctx, "oqsprovider")) == NULL) { | ||
goto unload_default_provider; | ||
} | ||
ret = 0; | ||
OSS_PROVIDER_unload(oqs_provider); | ||
|
||
#else | ||
|
||
if (OSSL_LIB_CTX_load_config(libctx, kConfigFile) == 1 | ||
&& OSSL_PROVIDER_available(libctx, kModuleName)) { | ||
ret = 0; | ||
} | ||
goto end; | ||
|
||
#endif // ifdef OQS_PROVIDER_STATIC | ||
|
||
unload_default_provider: | ||
OSSL_PROVIDER_unload(default_provider); | ||
|
||
end: | ||
return ret; | ||
} | ||
|
||
/** \brief Creates an OSSL_LIB_CTX object and loads oqs-provider. */ | ||
static void *thread_create_ossl_libctx(void *arg) | ||
{ | ||
OSSL_LIB_CTX *libctx = NULL; | ||
int ret = -1; | ||
|
||
(void)arg; | ||
|
||
if ((libctx = OSSL_LIB_CTX_new()) == NULL) { | ||
goto end; | ||
} | ||
ret = load_oqs_provider_thread(libctx); | ||
OSSL_LIB_CTX_free(libctx); | ||
|
||
end: | ||
return (void *)(size_t)ret; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
#ifdef _WIN32 | ||
HANDLE threads[N_THREADS]; | ||
#else | ||
pthread_t threads[N_THREADS]; | ||
#endif // ifdef _WIN32 | ||
size_t i; | ||
void *result; | ||
int ret = 0; | ||
|
||
T(argc == 3); | ||
|
||
kModuleName = argv[1]; | ||
kConfigFile = argv[2]; | ||
|
||
for (i = 0; i < N_THREADS; ++i) { | ||
#ifdef _WIN32 | ||
threads[i] | ||
= CreateThread(NULL, 0, thread_create_ossl_libctx, NULL, 0, NULL); | ||
#else | ||
pthread_create(threads + i, NULL, thread_create_ossl_libctx, NULL); | ||
#endif // ifdef _WIN32 | ||
} | ||
|
||
#ifdef _WIN32 | ||
WaitForMultipleObjects(N_THREADS, threads, TRUE, INFINITE); | ||
#endif // ifdef _WIN32 | ||
|
||
for (i = 0; i < N_THREADS; ++i) { | ||
result = NULL; | ||
#ifdef _WIN32 | ||
ret |= CloseHandle(threads[i]); | ||
#else | ||
pthread_join(threads[i], &result); | ||
ret |= (int)(size_t)result; | ||
#endif // ifdef _WIN32 | ||
} | ||
|
||
return ret; | ||
} |