winutil.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  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. /* $Id$ */
  20. #include "php.h"
  21. #include <wincrypt.h>
  22. PHPAPI char *php_win32_error_to_msg(int error)
  23. {
  24. char *buf = NULL;
  25. FormatMessage(
  26. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  27. NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, 0, NULL
  28. );
  29. return (buf ? (char *) buf : "");
  30. }
  31. int php_win32_check_trailing_space(const char * path, const int path_len) {
  32. if (path_len < 1) {
  33. return 1;
  34. }
  35. if (path) {
  36. if (path[0] == ' ' || path[path_len - 1] == ' ') {
  37. return 0;
  38. } else {
  39. return 1;
  40. }
  41. } else {
  42. return 0;
  43. }
  44. }
  45. HCRYPTPROV hCryptProv;
  46. unsigned int has_crypto_ctx = 0;
  47. #ifdef ZTS
  48. MUTEX_T php_lock_win32_cryptoctx;
  49. void php_win32_init_rng_lock()
  50. {
  51. php_lock_win32_cryptoctx = tsrm_mutex_alloc();
  52. }
  53. void php_win32_free_rng_lock()
  54. {
  55. tsrm_mutex_lock(php_lock_win32_cryptoctx);
  56. if (has_crypto_ctx == 1) {
  57. CryptReleaseContext(hCryptProv, 0);
  58. has_crypto_ctx = 0;
  59. }
  60. tsrm_mutex_unlock(php_lock_win32_cryptoctx);
  61. tsrm_mutex_free(php_lock_win32_cryptoctx);
  62. }
  63. #else
  64. #define php_win32_init_rng_lock();
  65. #define php_win32_free_rng_lock();
  66. #endif
  67. PHPAPI int php_win32_get_random_bytes(unsigned char *buf, size_t size) { /* {{{ */
  68. unsigned int has_contextg = 0;
  69. BOOL ret;
  70. size_t i = 0;
  71. #ifdef ZTS
  72. tsrm_mutex_lock(php_lock_win32_cryptoctx);
  73. #endif
  74. if (has_crypto_ctx == 0) {
  75. /* CRYPT_VERIFYCONTEXT > only hashing&co-like use, no need to acces prv keys */
  76. if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET|CRYPT_VERIFYCONTEXT )) {
  77. /* Could mean that the key container does not exist, let try
  78. again by asking for a new one. If it fails here, it surely means that the user running
  79. this process does not have the permission(s) to use this container.
  80. */
  81. if (GetLastError() == NTE_BAD_KEYSET) {
  82. if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET | CRYPT_VERIFYCONTEXT )) {
  83. has_crypto_ctx = 1;
  84. } else {
  85. has_crypto_ctx = 0;
  86. }
  87. }
  88. } else {
  89. has_crypto_ctx = 1;
  90. }
  91. }
  92. #ifdef ZTS
  93. tsrm_mutex_unlock(php_lock_win32_cryptoctx);
  94. #endif
  95. if (has_crypto_ctx == 0) {
  96. return FAILURE;
  97. }
  98. ret = CryptGenRandom(hCryptProv, size, buf);
  99. if (ret) {
  100. return SUCCESS;
  101. } else {
  102. return FAILURE;
  103. }
  104. }
  105. /* }}} */