root/win32/winutil.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. php_win32_error_to_msg
  2. php_win32_check_trailing_space
  3. php_win32_init_rng_lock
  4. php_win32_free_rng_lock
  5. php_win32_get_random_bytes

   1 /*
   2    +----------------------------------------------------------------------+
   3    | PHP Version 7                                                        |
   4    +----------------------------------------------------------------------+
   5    | Copyright (c) 1997-2016 The PHP Group                                |
   6    +----------------------------------------------------------------------+
   7    | This source file is subject to version 3.01 of the PHP license,      |
   8    | that is bundled with this package in the file LICENSE, and is        |
   9    | available through the world-wide-web at the following url:           |
  10    | http://www.php.net/license/3_01.txt                                  |
  11    | If you did not receive a copy of the PHP license and are unable to   |
  12    | obtain it through the world-wide-web, please send a note to          |
  13    | license@php.net so we can mail you a copy immediately.               |
  14    +----------------------------------------------------------------------+
  15    | Author: Zeev Suraski <zeev@zend.com>                                 |
  16    *         Pierre Joye <pierre@php.net>                                 |
  17    +----------------------------------------------------------------------+
  18  */
  19 
  20 /* $Id$ */
  21 
  22 #include "php.h"
  23 #include <wincrypt.h>
  24 
  25 PHPAPI char *php_win32_error_to_msg(HRESULT error)
  26 {
  27         char *buf = NULL;
  28 
  29         FormatMessage(
  30                 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |   FORMAT_MESSAGE_IGNORE_INSERTS,
  31                 NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, 0, NULL
  32         );
  33 
  34         return (buf ? (char *) buf : "");
  35 }
  36 
  37 int php_win32_check_trailing_space(const char * path, const int path_len) {
  38         if (path_len < 1) {
  39                 return 1;
  40         }
  41         if (path) {
  42                 if (path[0] == ' ' || path[path_len - 1] == ' ') {
  43                         return 0;
  44                 } else {
  45                         return 1;
  46                 }
  47         } else {
  48                 return 0;
  49         }
  50 }
  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         if (has_crypto_ctx == 1) {
  66                 CryptReleaseContext(hCryptProv, 0);
  67                 has_crypto_ctx = 0;
  68         }
  69         tsrm_mutex_unlock(php_lock_win32_cryptoctx);
  70         tsrm_mutex_free(php_lock_win32_cryptoctx);
  71 
  72 }
  73 #else
  74 #define php_win32_init_rng_lock();
  75 #define php_win32_free_rng_lock();
  76 #endif
  77 
  78 
  79 
  80 PHPAPI int php_win32_get_random_bytes(unsigned char *buf, size_t size) {  /* {{{ */
  81 
  82         BOOL ret;
  83 
  84 #ifdef ZTS
  85         tsrm_mutex_lock(php_lock_win32_cryptoctx);
  86 #endif
  87 
  88         if (has_crypto_ctx == 0) {
  89                 /* CRYPT_VERIFYCONTEXT > only hashing&co-like use, no need to acces prv keys */
  90                 if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET|CRYPT_VERIFYCONTEXT )) {
  91                         /* Could mean that the key container does not exist, let try
  92                            again by asking for a new one. If it fails here, it surely means that the user running
  93                this process does not have the permission(s) to use this container.
  94              */
  95                         if (GetLastError() == NTE_BAD_KEYSET) {
  96                                 if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET | CRYPT_VERIFYCONTEXT )) {
  97                                         has_crypto_ctx = 1;
  98                                 } else {
  99                                         has_crypto_ctx = 0;
 100                                 }
 101                         }
 102                 } else {
 103                         has_crypto_ctx = 1;
 104                 }
 105         }
 106 
 107 #ifdef ZTS
 108         tsrm_mutex_unlock(php_lock_win32_cryptoctx);
 109 #endif
 110 
 111         if (has_crypto_ctx == 0) {
 112                 return FAILURE;
 113         }
 114 
 115         /* XXX should go in the loop if size exceeds UINT_MAX */
 116         ret = CryptGenRandom(hCryptProv, (DWORD)size, buf);
 117 
 118         if (ret) {
 119                 return SUCCESS;
 120         } else {
 121                 return FAILURE;
 122         }
 123 }
 124 /* }}} */
 125 
 126 /*
 127  * Local variables:
 128  * tab-width: 4
 129  * c-basic-offset: 4
 130  * End:
 131  * vim600: sw=4 ts=4 fdm=marker
 132  * vim<600: sw=4 ts=4
 133  */

/* [<][>][^][v][top][bottom][index][help] */