pajoye                                   Mon, 01 Feb 2010 14:56:51 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=294307

Log:
- 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)

Changed paths:
    U   php/php-src/branches/PHP_5_3/Zend/zend_alloc.c
    U   php/php-src/trunk/Zend/zend_alloc.c

Modified: php/php-src/branches/PHP_5_3/Zend/zend_alloc.c
===================================================================
--- php/php-src/branches/PHP_5_3/Zend/zend_alloc.c      2010-02-01 14:09:23 UTC 
(rev 294306)
+++ php/php-src/branches/PHP_5_3/Zend/zend_alloc.c      2010-02-01 14:56:51 UTC 
(rev 294307)
@@ -944,15 +944,27 @@
 #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;
+       int has_context = 0;

-       if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 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 @@
                                        i++;
                                }
                                if (i == size) {
-                                   return;
+                                       return;
                                }
                   }
                } while (0);
@@ -990,6 +1002,7 @@
                t = buf[i++] << 1;
     }
 }
+/* }}} */
 #endif

 /* Notes:

Modified: php/php-src/trunk/Zend/zend_alloc.c
===================================================================
--- php/php-src/trunk/Zend/zend_alloc.c 2010-02-01 14:09:23 UTC (rev 294306)
+++ php/php-src/trunk/Zend/zend_alloc.c 2010-02-01 14:56:51 UTC (rev 294307)
@@ -979,8 +979,20 @@

 #ifdef ZEND_WIN32
        HCRYPTPROV   hCryptProv;
+       int has_context = 0;

-       if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 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);
@@ -989,7 +1001,7 @@
                                        i++;
                                }
                                if (i == size) {
-                                   return;
+                                       return;
                                }
                   }
                } while (0);

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to