2015-10-09 00:28:24 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Random_* Compatibility Library
|
|
|
|
* for using the new PHP 7 random_* API in PHP 5 projects
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Paragon Initiative Enterprises
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('PHP_VERSION_ID')) {
|
|
|
|
// This constant was introduced in PHP 5.2.7
|
2015-10-23 00:22:26 -04:00
|
|
|
$RandomCompatversion = explode('.', PHP_VERSION);
|
|
|
|
define('PHP_VERSION_ID', ($RandomCompatversion[0] * 10000 + $RandomCompatversion[1] * 100 + $RandomCompatversion[2]));
|
2015-10-27 21:12:24 -04:00
|
|
|
$RandomCompatversion = null;
|
2015-10-09 00:28:24 -04:00
|
|
|
}
|
|
|
|
if (PHP_VERSION_ID < 70000) {
|
|
|
|
if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
|
|
|
|
define('RANDOM_COMPAT_READ_BUFFER', 8);
|
|
|
|
}
|
2015-10-27 21:12:24 -04:00
|
|
|
$RandomCompatDIR = dirname(__FILE__);
|
|
|
|
require_once $RandomCompatDIR.'/byte_safe_strings.php';
|
|
|
|
require_once $RandomCompatDIR.'/cast_to_int.php';
|
|
|
|
require_once $RandomCompatDIR.'/error_polyfill.php';
|
2015-10-09 00:28:24 -04:00
|
|
|
if (!function_exists('random_bytes')) {
|
|
|
|
/**
|
|
|
|
* PHP 5.2.0 - 5.6.x way to implement random_bytes()
|
|
|
|
*
|
|
|
|
* We use conditional statements here to define the function in accordance
|
|
|
|
* to the operating environment. It's a micro-optimization.
|
|
|
|
*
|
|
|
|
* In order of preference:
|
2015-10-23 00:22:26 -04:00
|
|
|
* 1. Use libsodium if available.
|
|
|
|
* 2. fread() /dev/urandom if available (never on Windows)
|
|
|
|
* 3. mcrypt_create_iv($bytes, MCRYPT_CREATE_IV)
|
|
|
|
* 4. COM('CAPICOM.Utilities.1')->GetRandom()
|
|
|
|
* 5. openssl_random_pseudo_bytes() (absolute last resort)
|
2015-10-09 00:28:24 -04:00
|
|
|
*
|
|
|
|
* See ERRATA.md for our reasoning behind this particular order
|
|
|
|
*/
|
2015-10-23 00:22:26 -04:00
|
|
|
if (extension_loaded('libsodium')) {
|
|
|
|
// See random_bytes_libsodium.php
|
2015-10-27 21:12:24 -04:00
|
|
|
require_once $RandomCompatDIR.'/random_bytes_libsodium.php';
|
2015-10-23 00:22:26 -04:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
!function_exists('random_bytes') &&
|
|
|
|
DIRECTORY_SEPARATOR === '/' &&
|
|
|
|
@is_readable('/dev/urandom')
|
|
|
|
) {
|
|
|
|
// DIRECTORY_SEPARATOR === '/' on Unix-like OSes -- this is a fast
|
|
|
|
// way to exclude Windows.
|
|
|
|
//
|
|
|
|
// Error suppression on is_readable() in case of an open_basedir or
|
|
|
|
// safe_mode failure. All we care about is whether or not we can
|
|
|
|
// read it at this point. If the PHP environment is going to panic
|
|
|
|
// over trying to see if the file can be read in the first place,
|
|
|
|
// that is not helpful to us here.
|
|
|
|
|
2015-10-09 00:28:24 -04:00
|
|
|
// See random_bytes_dev_urandom.php
|
2015-10-27 21:12:24 -04:00
|
|
|
require_once $RandomCompatDIR.'/random_bytes_dev_urandom.php';
|
2015-10-23 00:22:26 -04:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
!function_exists('random_bytes') &&
|
|
|
|
PHP_VERSION_ID >= 50307 &&
|
|
|
|
extension_loaded('mcrypt')
|
|
|
|
) {
|
2015-10-09 00:28:24 -04:00
|
|
|
// See random_bytes_mcrypt.php
|
2015-10-27 21:12:24 -04:00
|
|
|
require_once $RandomCompatDIR.'/random_bytes_mcrypt.php';
|
2015-10-23 00:22:26 -04:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
!function_exists('random_bytes') &&
|
|
|
|
extension_loaded('com_dotnet') &&
|
|
|
|
class_exists('COM')
|
|
|
|
) {
|
2015-12-14 02:46:28 -05:00
|
|
|
$RandomCompat_disabled_classes = preg_split(
|
|
|
|
'#\s*,\s*#',
|
|
|
|
strtolower(ini_get('disable_classes'))
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!in_array('com', $RandomCompat_disabled_classes)) {
|
|
|
|
try {
|
|
|
|
$RandomCompatCOMtest = new COM('CAPICOM.Utilities.1');
|
|
|
|
if (method_exists($RandomCompatCOMtest, 'GetRandom')) {
|
|
|
|
// See random_bytes_com_dotnet.php
|
|
|
|
require_once $RandomCompatDIR.'/random_bytes_com_dotnet.php';
|
|
|
|
}
|
|
|
|
} catch (com_exception $e) {
|
|
|
|
// Don't try to use it.
|
2015-10-23 00:22:26 -04:00
|
|
|
}
|
|
|
|
}
|
2015-12-14 02:46:28 -05:00
|
|
|
$RandomCompat_disabled_classes = null;
|
2015-10-27 21:12:24 -04:00
|
|
|
$RandomCompatCOMtest = null;
|
2015-10-23 00:22:26 -04:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
!function_exists('random_bytes') &&
|
|
|
|
extension_loaded('openssl') &&
|
2015-10-27 21:12:24 -04:00
|
|
|
(
|
|
|
|
// Unix-like with PHP >= 5.3.0 or
|
|
|
|
(
|
|
|
|
DIRECTORY_SEPARATOR === '/' &&
|
|
|
|
PHP_VERSION_ID >= 50300
|
|
|
|
) ||
|
2015-11-10 07:00:30 -05:00
|
|
|
// Windows with PHP >= 5.4.1
|
|
|
|
PHP_VERSION_ID >= 50401
|
2015-10-27 21:12:24 -04:00
|
|
|
)
|
2015-10-23 00:22:26 -04:00
|
|
|
) {
|
2015-10-09 00:28:24 -04:00
|
|
|
// See random_bytes_openssl.php
|
2015-10-27 21:12:24 -04:00
|
|
|
require_once $RandomCompatDIR.'/random_bytes_openssl.php';
|
2015-10-23 00:22:26 -04:00
|
|
|
}
|
|
|
|
if (!function_exists('random_bytes')) {
|
2015-10-09 00:28:24 -04:00
|
|
|
/**
|
|
|
|
* We don't have any more options, so let's throw an exception right now
|
|
|
|
* and hope the developer won't let it fail silently.
|
|
|
|
*/
|
|
|
|
function random_bytes()
|
|
|
|
{
|
|
|
|
throw new Exception(
|
|
|
|
'There is no suitable CSPRNG installed on your system'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!function_exists('random_int')) {
|
2015-10-27 21:12:24 -04:00
|
|
|
require_once $RandomCompatDIR.'/random_int.php';
|
2015-10-09 00:28:24 -04:00
|
|
|
}
|
2015-10-27 21:12:24 -04:00
|
|
|
$RandomCompatDIR = null;
|
2015-10-09 00:28:24 -04:00
|
|
|
}
|