From: Pierre Joye Date: Mon, 1 Feb 2010 14:56:51 +0000 (+0000) Subject: - try again to get a crypto context when the key container did not exist (which is... X-Git-Tag: php-5.4.0alpha1~346 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d839e3b9a40586b11560c887805f3dc71183fb81;p=php - try again to get a crypto context when the key container did not exist (which is likely to be the case, on a 1st call) --- diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 8c7397a722..e71456ab37 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -944,15 +944,27 @@ static void zend_mm_free_cache(zend_mm_heap *heap) #endif #if ZEND_MM_HEAP_PROTECTION || ZEND_MM_COOKIES -static void zend_mm_random(unsigned char *buf, size_t size) +static void zend_mm_random(unsigned char *buf, size_t size) /* {{{ */ { size_t i = 0; unsigned char t; #ifdef ZEND_WIN32 HCRYPTPROV hCryptProv; - - if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) { + int has_context = 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 { + has_context = 1; + } + if (has_context) { do { BOOL ret = CryptGenRandom(hCryptProv, size, buf); CryptReleaseContext(hCryptProv, 0); @@ -961,7 +973,7 @@ static void zend_mm_random(unsigned char *buf, size_t size) i++; } if (i == size) { - return; + return; } } } while (0); @@ -990,6 +1002,7 @@ static void zend_mm_random(unsigned char *buf, size_t size) t = buf[i++] << 1; } } +/* }}} */ #endif /* Notes: