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