@@ -49,30 +49,66 @@ int php_win32_check_trailing_space(const char * path, const int path_len) {
49
49
}
50
50
}
51
51
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
+
52
78
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
+
55
82
BOOL ret ;
56
83
size_t i = 0 ;
57
84
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
+ }
66
96
}
67
97
}
68
98
}
99
+ tsrm_mutex_unlock (php_lock_win32_cryptoctx );
100
+
101
+ if (has_crypto_ctx == 0 ) {
102
+ return FAILURE ;
103
+ }
69
104
70
105
ret = CryptGenRandom (hCryptProv , size , buf );
71
- CryptReleaseContext ( hCryptProv , 0 );
106
+
72
107
if (ret ) {
73
108
return SUCCESS ;
109
+ } else {
110
+ return FAILURE ;
74
111
}
75
- return FAILURE ;
76
112
}
77
113
/* }}} */
78
114
0 commit comments