Skip to content

Commit 4cfaadf

Browse files
authored
Merge pull request #192 from tim-weller-wolfssl/support-14679
2 parents c809605 + 0b9639c commit 4cfaadf

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

src/we_internal.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,3 +1463,23 @@ int wolfengine_bind(ENGINE *e, const char *id)
14631463

14641464
return ret;
14651465
}
1466+
1467+
#if !defined(WE_SINGLE_THREADED) && defined(_WIN32) && defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION == 5
1468+
/**
1469+
* Windows DLL entry point when wolfEngine built as a DLL.
1470+
*
1471+
* Called for DLL process or thread events, such as creation (attach).
1472+
*
1473+
* @param hinstDLL [IN] A handle to the DLL module.
1474+
* @param fdwReason [IN] Reason why function being called.
1475+
* @param lpvReserved [IN] Reason-dependent extra data.
1476+
* @returns TRUE always
1477+
*/
1478+
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1479+
{
1480+
if (fdwReason == DLL_THREAD_ATTACH) {
1481+
wolfCrypt_SetPrivateKeyReadEnable_fips(1, WC_KEYTYPE_ALL);
1482+
}
1483+
return TRUE;
1484+
}
1485+
#endif /* !WE_SINGLE_THREADED && _WIN32 && HAVE_FIPS_VERSION && HAVE_FIPS_VERSION 5 */

test/test_dh.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,45 @@ int test_dh_pgen_pkey(ENGINE *e, void *data)
340340
return err;
341341
}
342342

343-
int test_dh_pkey(ENGINE *e, void *data)
343+
#if !defined(WE_SINGLE_THREADED) && defined(_WIN32)
344+
345+
#define TEST_MT_TIMEOUT 5000 /* Multi-threaded test timeout (ms) */
346+
347+
typedef struct {
348+
ENGINE* e;
349+
EVP_PKEY* params;
350+
} DH_KEYGEN_THREAD_VARS;
351+
352+
/* Windows thread entry function which will test private key read access. */
353+
static DWORD WINAPI DhKeyGenThreadFunc(LPVOID arg)
354+
{
355+
DH_KEYGEN_THREAD_VARS* vars = (DH_KEYGEN_THREAD_VARS*)arg;
356+
357+
return test_dh_pgen_pkey(vars->e, vars->params);
358+
}
359+
360+
/* Regression test for problem in multi-threaded Windows environment where only
361+
initial thread has private key read access while additionally created
362+
threads do not */
363+
int test_dh_key_gen_multithreaded(ENGINE* e, EVP_PKEY* params)
364+
{
365+
DH_KEYGEN_THREAD_VARS vars = {.e = e, .params = params};
366+
HANDLE thread;
367+
DWORD threadErr = 0;
368+
int err = 1;
369+
370+
thread = CreateThread(NULL, 0, DhKeyGenThreadFunc, &vars, 0, NULL);
371+
if (thread && (WaitForSingleObject(thread, TEST_MT_TIMEOUT) == WAIT_OBJECT_0)) {
372+
err = (GetExitCodeThread(thread, &threadErr) == 0 ? 1 : threadErr);
373+
CloseHandle(thread);
374+
}
375+
376+
return err;
377+
}
378+
379+
#endif /* !WE_SINGLE_THREADED && _WIN32 */
380+
381+
int test_dh_pkey(ENGINE* e, void* data)
344382
{
345383
int err;
346384
DH *dh;

test/unit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ TEST_CASE test_case[] = {
152152
#ifdef WE_HAVE_EVP_PKEY
153153
TEST_DECL(test_dh_pgen_pkey, NULL),
154154
TEST_DECL(test_dh_pkey, NULL),
155+
#if !defined(WE_SINGLE_THREADED) && defined(_WIN32)
156+
TEST_DECL(test_dh_key_gen_multithreaded, NULL),
157+
#endif /* !WE_SINGLE_THREADED && _WIN32 */
155158
#endif /* WE_HAVE_EVP_PKEY */
156159
#endif /* WE_HAVE_DH */
157160
#if OPENSSL_VERSION_NUMBER < 0x10100000L

test/unit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ int test_dh(ENGINE *e, void *data);
248248
#ifdef WE_HAVE_EVP_PKEY
249249
int test_dh_pgen_pkey(ENGINE *e, void *data);
250250
int test_dh_pkey(ENGINE *e, void *data);
251+
#if !defined(WE_SINGLE_THREADED) && defined(_WIN32)
252+
int test_dh_key_gen_multithreaded(ENGINE *e, void *data);
253+
#endif /* !WE_SINGLE_THREADED && _WIN32 */
251254
#endif /* WE_HAVE_EVP_PKEY */
252255
#endif /* WE_HAVE_DH */
253256

0 commit comments

Comments
 (0)