Back to index

php5  5.3.10
winutil.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Zeev Suraski <zeev@zend.com>                                 |
00016    *         Pierre Joye <pierre@php.net>                                 |
00017    +----------------------------------------------------------------------+
00018  */
00019 
00020 /* $Id: winutil.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #include "php.h"
00023 #include <wincrypt.h>
00024 
00025 PHPAPI char *php_win_err(int error)
00026 {
00027        char *buf = NULL;
00028 
00029        FormatMessage(
00030               FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |  FORMAT_MESSAGE_IGNORE_INSERTS,
00031               NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, 0, NULL
00032        );
00033 
00034        return (buf ? (char *) buf : "");
00035 }
00036 
00037 int php_win32_check_trailing_space(const char * path, const int path_len) {
00038        if (path_len < 1) {
00039               return 1;
00040        }
00041        if (path) {
00042               if (path[0] == ' ' || path[path_len - 1] == ' ') {
00043                      return 0;
00044               } else {
00045                      return 1;
00046               }
00047        } else {
00048               return 0;
00049        }
00050 }
00051 
00052 HCRYPTPROV   hCryptProv;
00053 unsigned int has_crypto_ctx = 0;
00054 
00055 #ifdef ZTS
00056 MUTEX_T php_lock_win32_cryptoctx;
00057 void php_win32_init_rng_lock()
00058 {
00059        php_lock_win32_cryptoctx = tsrm_mutex_alloc();
00060 }
00061 
00062 void php_win32_free_rng_lock()
00063 {
00064        tsrm_mutex_lock(php_lock_win32_cryptoctx);
00065        CryptReleaseContext(hCryptProv, 0);
00066        has_crypto_ctx = 0;
00067        tsrm_mutex_unlock(php_lock_win32_cryptoctx);
00068        tsrm_mutex_free(php_lock_win32_cryptoctx);
00069 
00070 }
00071 #else
00072 #define php_win32_init_rng_lock();
00073 #define php_win32_free_rng_lock();
00074 #endif
00075 
00076 
00077 
00078 PHPAPI int php_win32_get_random_bytes(unsigned char *buf, size_t size) {  /* {{{ */
00079 
00080        unsigned int has_contextg = 0;
00081 
00082        BOOL ret;
00083        size_t i = 0;
00084 
00085 #ifdef ZTS
00086        tsrm_mutex_lock(php_lock_win32_cryptoctx);
00087 #endif
00088 
00089        if (has_crypto_ctx == 0) {
00090               /* CRYPT_VERIFYCONTEXT > only hashing&co-like use, no need to acces prv keys */
00091               if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET|CRYPT_VERIFYCONTEXT )) {
00092                      /* Could mean that the key container does not exist, let try 
00093                         again by asking for a new one. If it fails here, it surely means that the user running 
00094                this process does not have the permission(s) to use this container.
00095              */
00096                      if (GetLastError() == NTE_BAD_KEYSET) {
00097                             if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET | CRYPT_VERIFYCONTEXT )) {
00098                                    has_crypto_ctx = 1;
00099                             } else {
00100                                    has_crypto_ctx = 0;
00101                             }
00102                      }
00103               } else {
00104                      has_crypto_ctx = 1;
00105               }
00106        }
00107 
00108 #ifdef ZTS
00109        tsrm_mutex_unlock(php_lock_win32_cryptoctx);
00110 #endif
00111 
00112        if (has_crypto_ctx == 0) {
00113               return FAILURE;
00114        }
00115 
00116        ret = CryptGenRandom(hCryptProv, size, buf);
00117 
00118        if (ret) {
00119               return SUCCESS;
00120        } else {
00121               return FAILURE;
00122        }
00123 }
00124 /* }}} */
00125