Annotation of embedaddon/php/win32/winutil.c, revision 1.1.1.2
1.1 misho 1: /*
2: +----------------------------------------------------------------------+
3: | PHP Version 5 |
4: +----------------------------------------------------------------------+
5: | Copyright (c) 1997-2012 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:
1.1.1.2 ! misho 20: /* $Id$ */
1.1 misho 21:
22: #include "php.h"
23: #include <wincrypt.h>
24:
1.1.1.2 ! misho 25: PHPAPI char *php_win32_error_to_msg(int error)
1.1 misho 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);
1.1.1.2 ! misho 65: if (has_crypto_ctx == 1) {
! 66: CryptReleaseContext(hCryptProv, 0);
! 67: has_crypto_ctx = 0;
! 68: }
1.1 misho 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: unsigned int has_contextg = 0;
83:
84: BOOL ret;
85: size_t i = 0;
86:
87: #ifdef ZTS
88: tsrm_mutex_lock(php_lock_win32_cryptoctx);
89: #endif
90:
91: if (has_crypto_ctx == 0) {
92: /* CRYPT_VERIFYCONTEXT > only hashing&co-like use, no need to acces prv keys */
93: if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET|CRYPT_VERIFYCONTEXT )) {
94: /* Could mean that the key container does not exist, let try
95: again by asking for a new one. If it fails here, it surely means that the user running
96: this process does not have the permission(s) to use this container.
97: */
98: if (GetLastError() == NTE_BAD_KEYSET) {
99: if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET | CRYPT_VERIFYCONTEXT )) {
100: has_crypto_ctx = 1;
101: } else {
102: has_crypto_ctx = 0;
103: }
104: }
105: } else {
106: has_crypto_ctx = 1;
107: }
108: }
109:
110: #ifdef ZTS
111: tsrm_mutex_unlock(php_lock_win32_cryptoctx);
112: #endif
113:
114: if (has_crypto_ctx == 0) {
115: return FAILURE;
116: }
117:
118: ret = CryptGenRandom(hCryptProv, size, buf);
119:
120: if (ret) {
121: return SUCCESS;
122: } else {
123: return FAILURE;
124: }
125: }
126: /* }}} */
127:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>