X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/9441756a895fb4fdc4bcf20e0d228cef622663ca..16e7b37c7914d753890c1a05a9335f3b43751eb8:/wp-includes/random_compat/random.php?ds=sidebyside diff --git a/wp-includes/random_compat/random.php b/wp-includes/random_compat/random.php index 8898a35b..595115e5 100644 --- a/wp-includes/random_compat/random.php +++ b/wp-includes/random_compat/random.php @@ -1,22 +1,25 @@ GetRandom() * 5. openssl_random_pseudo_bytes() (absolute last resort) - * + * * See ERRATA.md for our reasoning behind this particular order */ if (extension_loaded('libsodium')) { // See random_bytes_libsodium.php - require_once $RandomCompatDIR.'/random_bytes_libsodium.php'; + if (PHP_VERSION_ID >= 50300 && function_exists('\\Sodium\\randombytes_buf')) { + require_once $RandomCompatDIR.'/random_bytes_libsodium.php'; + } elseif (method_exists('Sodium', 'randombytes_buf')) { + require_once $RandomCompatDIR.'/random_bytes_libsodium_legacy.php'; + } } - if ( - !function_exists('random_bytes') && - DIRECTORY_SEPARATOR === '/' && - @is_readable('/dev/urandom') - ) { + + /** + * Reading directly from /dev/urandom: + */ + if (DIRECTORY_SEPARATOR === '/') { // 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. - - // See random_bytes_dev_urandom.php - require_once $RandomCompatDIR.'/random_bytes_dev_urandom.php'; + $RandomCompatUrandom = true; + $RandomCompat_basedir = ini_get('open_basedir'); + + if (!empty($RandomCompat_basedir)) { + $RandomCompat_open_basedir = explode( + PATH_SEPARATOR, + strtolower($RandomCompat_basedir) + ); + $RandomCompatUrandom = in_array( + '/dev', + $RandomCompat_open_basedir + ); + $RandomCompat_open_basedir = null; + } + + if ( + !function_exists('random_bytes') + && + $RandomCompatUrandom + && + @is_readable('/dev/urandom') + ) { + // 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. + + // See random_bytes_dev_urandom.php + require_once $RandomCompatDIR.'/random_bytes_dev_urandom.php'; + } + // Unset variables after use + $RandomCompat_basedir = null; + $RandomCompatUrandom = null; } + + /** + * mcrypt_create_iv() + */ if ( - !function_exists('random_bytes') && - PHP_VERSION_ID >= 50307 && + !function_exists('random_bytes') + && + PHP_VERSION_ID >= 50307 + && extension_loaded('mcrypt') ) { - // See random_bytes_mcrypt.php - require_once $RandomCompatDIR.'/random_bytes_mcrypt.php'; + // Prevent this code from hanging indefinitely on non-Windows; + // see https://bugs.php.net/bug.php?id=69833 + if ( + DIRECTORY_SEPARATOR !== '/' || + (PHP_VERSION_ID <= 50609 || PHP_VERSION_ID >= 50613) + ) { + // See random_bytes_mcrypt.php + require_once $RandomCompatDIR.'/random_bytes_mcrypt.php'; + } } + if ( - !function_exists('random_bytes') && - extension_loaded('com_dotnet') && + !function_exists('random_bytes') + && + extension_loaded('com_dotnet') + && class_exists('COM') ) { $RandomCompat_disabled_classes = preg_split( - '#\s*,\s*#', + '#\s*,\s*#', strtolower(ini_get('disable_classes')) ); - + if (!in_array('com', $RandomCompat_disabled_classes)) { try { $RandomCompatCOMtest = new COM('CAPICOM.Utilities.1'); @@ -109,28 +167,40 @@ if (PHP_VERSION_ID < 70000) { $RandomCompat_disabled_classes = null; $RandomCompatCOMtest = null; } + + /** + * openssl_random_pseudo_bytes() + */ if ( - !function_exists('random_bytes') && - extension_loaded('openssl') && ( // Unix-like with PHP >= 5.3.0 or ( - DIRECTORY_SEPARATOR === '/' && + DIRECTORY_SEPARATOR === '/' + && PHP_VERSION_ID >= 50300 - ) || + ) + || // Windows with PHP >= 5.4.1 PHP_VERSION_ID >= 50401 ) + && + !function_exists('random_bytes') + && + extension_loaded('openssl') ) { // See random_bytes_openssl.php require_once $RandomCompatDIR.'/random_bytes_openssl.php'; } + + /** + * throw new Exception + */ if (!function_exists('random_bytes')) { /** * 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() + function random_bytes($length) { throw new Exception( 'There is no suitable CSPRNG installed on your system' @@ -138,8 +208,10 @@ if (PHP_VERSION_ID < 70000) { } } } + if (!function_exists('random_int')) { require_once $RandomCompatDIR.'/random_int.php'; } + $RandomCompatDIR = null; }