summaryrefslogtreecommitdiff
path: root/win32
diff options
context:
space:
mode:
authorPierre Joye <pajoye@php.net>2011-06-16 01:31:10 (GMT)
committerPierre Joye <pajoye@php.net>2011-06-16 01:31:10 (GMT)
commit1d357d964c9667ce8bdff4e62f23bf7be3e5bc30 (patch)
tree2746204c1a4341614b3e42570a85de8e87b7ebaf /win32
parent175f84436c97f8811f807fcb4915a28fee96bab0 (diff)
downloadphp-1d357d964c9667ce8bdff4e62f23bf7be3e5bc30.tar.gz
- init win32 rng context once per process
Diffstat (limited to 'win32')
-rw-r--r--win32/winutil.c60
-rw-r--r--win32/winutil.h8
2 files changed, 56 insertions, 12 deletions
diff --git a/win32/winutil.c b/win32/winutil.c
index 29f1f07..b94fcc4 100644
--- a/win32/winutil.c
+++ b/win32/winutil.c
@@ -49,30 +49,66 @@ int php_win32_check_trailing_space(const char * path, const int path_len) {
}
}
+HCRYPTPROV hCryptProv;
+unsigned int has_crypto_ctx = 0;
+
+#ifdef ZTS
+MUTEX_T php_lock_win32_cryptoctx;
+void php_win32_init_rng_lock()
+{
+ php_lock_win32_cryptoctx = tsrm_mutex_alloc();
+}
+
+void php_win32_free_rng_lock()
+{
+ tsrm_mutex_lock(php_lock_win32_cryptoctx);
+ CryptReleaseContext(hCryptProv, 0);
+ has_crypto_ctx = 0;
+ tsrm_mutex_unlock(php_lock_win32_cryptoctx);
+ tsrm_mutex_free(php_lock_win32_cryptoctx);
+
+}
+#else
+#define php_win32_init_rng_lock();
+#define php_win32_free_rng_lock();
+#endif
+
+
+
PHPAPI int php_win32_get_random_bytes(unsigned char *buf, size_t size) { /* {{{ */
- HCRYPTPROV hCryptProv;
- int has_context = 0;
+
+ unsigned int has_contextg = 0;
+
BOOL ret;
size_t i = 0;
- if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) {
- /* Could mean that the key container does not exist, let try
- again by asking for a new one */
- if (GetLastError() == NTE_BAD_KEYSET) {
- if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
- has_context = 1;
- } else {
- return FAILURE;
+ tsrm_mutex_lock(php_lock_win32_cryptoctx);
+ if (has_crypto_ctx == 0) {
+ if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET)) {
+ /* Could mean that the key container does not exist, let try
+ again by asking for a new one */
+ if (GetLastError() == NTE_BAD_KEYSET) {
+ if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
+ has_crypto_ctx = 1;
+ } else {
+ has_crypto_ctx = 0;
+ }
}
}
}
+ tsrm_mutex_unlock(php_lock_win32_cryptoctx);
+
+ if (has_crypto_ctx == 0) {
+ return FAILURE;
+ }
ret = CryptGenRandom(hCryptProv, size, buf);
- CryptReleaseContext(hCryptProv, 0);
+
if (ret) {
return SUCCESS;
+ } else {
+ return FAILURE;
}
- return FAILURE;
}
/* }}} */
diff --git a/win32/winutil.h b/win32/winutil.h
index 3272169..3cf8a5a 100644
--- a/win32/winutil.h
+++ b/win32/winutil.h
@@ -21,3 +21,11 @@ PHPAPI char *php_win32_error_to_msg(int error);
#define php_win_err() php_win32_error_to_msg(GetLastError())
int php_win32_check_trailing_space(const char * path, const int path_len);
PHPAPI php_win32_get_random_bytes(unsigned char *buf, size_t size);
+
+#ifdef ZTS
+void php_win32_init_rng_lock();
+void php_win32_free_rng_lock();
+#else
+#define php_win32_init_rng_lock();
+#define php_win32_free_rng_lock();
+#endif