Skip to content

Commit 1d357d9

Browse files
committed
- init win32 rng context once per process
1 parent 175f844 commit 1d357d9

File tree

3 files changed

+71
-13
lines changed

3 files changed

+71
-13
lines changed

main/main.c

+15-1
Original file line numberDiff line numberDiff line change
@@ -1885,11 +1885,21 @@ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_mod
18851885
#else
18861886
php_os=PHP_OS;
18871887
#endif
1888-
1888+
{
1889+
char dll_dir[MAX_PATH];
1890+
sprintf(dll_dir, "%s\\%s", module_path, "..\\..\\deps\\bin");
1891+
SetDllDirectory(dll_dir);
1892+
}
1893+
// GetModuleFileName (NULL, module_path, MAX_PATH);
1894+
//__debugbreak();
18891895
#ifdef ZTS
18901896
tsrm_ls = ts_resource(0);
18911897
#endif
18921898

1899+
#ifdef PHP_WIN32
1900+
php_win32_init_rng_lock();
1901+
#endif
1902+
18931903
module_shutdown = 0;
18941904
module_startup = 1;
18951905
sapi_initialize_empty_request(TSRMLS_C);
@@ -2241,6 +2251,10 @@ void php_module_shutdown(TSRMLS_D)
22412251
WSACleanup();
22422252
#endif
22432253

2254+
#ifdef PHP_WIN32
2255+
php_win32_free_rng_lock();
2256+
#endif
2257+
22442258
sapi_flush(TSRMLS_C);
22452259

22462260
zend_shutdown(TSRMLS_C);

win32/winutil.c

+48-12
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,66 @@ int php_win32_check_trailing_space(const char * path, const int path_len) {
4949
}
5050
}
5151

52+
HCRYPTPROV hCryptProv;
53+
unsigned int has_crypto_ctx = 0;
54+
55+
#ifdef ZTS
56+
MUTEX_T php_lock_win32_cryptoctx;
57+
void php_win32_init_rng_lock()
58+
{
59+
php_lock_win32_cryptoctx = tsrm_mutex_alloc();
60+
}
61+
62+
void php_win32_free_rng_lock()
63+
{
64+
tsrm_mutex_lock(php_lock_win32_cryptoctx);
65+
CryptReleaseContext(hCryptProv, 0);
66+
has_crypto_ctx = 0;
67+
tsrm_mutex_unlock(php_lock_win32_cryptoctx);
68+
tsrm_mutex_free(php_lock_win32_cryptoctx);
69+
70+
}
71+
#else
72+
#define php_win32_init_rng_lock();
73+
#define php_win32_free_rng_lock();
74+
#endif
75+
76+
77+
5278
PHPAPI int php_win32_get_random_bytes(unsigned char *buf, size_t size) { /* {{{ */
53-
HCRYPTPROV hCryptProv;
54-
int has_context = 0;
79+
80+
unsigned int has_contextg = 0;
81+
5582
BOOL ret;
5683
size_t i = 0;
5784

58-
if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) {
59-
/* Could mean that the key container does not exist, let try
60-
again by asking for a new one */
61-
if (GetLastError() == NTE_BAD_KEYSET) {
62-
if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
63-
has_context = 1;
64-
} else {
65-
return FAILURE;
85+
tsrm_mutex_lock(php_lock_win32_cryptoctx);
86+
if (has_crypto_ctx == 0) {
87+
if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET)) {
88+
/* Could mean that the key container does not exist, let try
89+
again by asking for a new one */
90+
if (GetLastError() == NTE_BAD_KEYSET) {
91+
if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
92+
has_crypto_ctx = 1;
93+
} else {
94+
has_crypto_ctx = 0;
95+
}
6696
}
6797
}
6898
}
99+
tsrm_mutex_unlock(php_lock_win32_cryptoctx);
100+
101+
if (has_crypto_ctx == 0) {
102+
return FAILURE;
103+
}
69104

70105
ret = CryptGenRandom(hCryptProv, size, buf);
71-
CryptReleaseContext(hCryptProv, 0);
106+
72107
if (ret) {
73108
return SUCCESS;
109+
} else {
110+
return FAILURE;
74111
}
75-
return FAILURE;
76112
}
77113
/* }}} */
78114

win32/winutil.h

+8
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ PHPAPI char *php_win32_error_to_msg(int error);
2121
#define php_win_err() php_win32_error_to_msg(GetLastError())
2222
int php_win32_check_trailing_space(const char * path, const int path_len);
2323
PHPAPI php_win32_get_random_bytes(unsigned char *buf, size_t size);
24+
25+
#ifdef ZTS
26+
void php_win32_init_rng_lock();
27+
void php_win32_free_rng_lock();
28+
#else
29+
#define php_win32_init_rng_lock();
30+
#define php_win32_free_rng_lock();
31+
#endif

0 commit comments

Comments
 (0)