]> granicus.if.org Git - php/commitdiff
- init win32 rng context once per process
authorPierre Joye <pajoye@php.net>
Thu, 16 Jun 2011 01:31:10 +0000 (01:31 +0000)
committerPierre Joye <pajoye@php.net>
Thu, 16 Jun 2011 01:31:10 +0000 (01:31 +0000)
main/main.c
win32/winutil.c
win32/winutil.h

index 7f944239ad16b5165afe270c7b71c89877e62351..f6f6c2f63629179829e0bce2d38e6db9ae54f908 100644 (file)
@@ -1885,11 +1885,21 @@ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_mod
 #else
        php_os=PHP_OS;
 #endif
-
+       {
+               char dll_dir[MAX_PATH];
+               sprintf(dll_dir, "%s\\%s", module_path, "..\\..\\deps\\bin");
+               SetDllDirectory(dll_dir);
+       }
+//     GetModuleFileName (NULL, module_path, MAX_PATH);
+//__debugbreak();
 #ifdef ZTS
        tsrm_ls = ts_resource(0);
 #endif
 
+#ifdef PHP_WIN32
+       php_win32_init_rng_lock();
+#endif
+
        module_shutdown = 0;
        module_startup = 1;
        sapi_initialize_empty_request(TSRMLS_C);
@@ -2241,6 +2251,10 @@ void php_module_shutdown(TSRMLS_D)
        WSACleanup();
 #endif
 
+#ifdef PHP_WIN32
+       php_win32_free_rng_lock();
+#endif
+
        sapi_flush(TSRMLS_C);
 
        zend_shutdown(TSRMLS_C);
index 29f1f07cc38b66bca924cef043cf86297a0c6906..b94fcc482ddb2148e259867ba4e96b0a2e22a27d 100644 (file)
@@ -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;
 }
 /* }}} */
 
index 32721692b4e9bd4d0a4b032100734a3e3cd725ac..3cf8a5abf27b2155246733f2d29ae2fe0d8ca4a3 100644 (file)
@@ -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