From 07e9d2296892eb779e8cfcbcab4349ffa2cd6961 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Fri, 8 Jan 2016 03:28:28 +0000 Subject: [PATCH] Update to Random_Compat 1.1.5 to fix an issue with older libSodium modules. Props sarciszewski. Fixes #35327 for trunk. Built from https://develop.svn.wordpress.org/trunk@36220 git-svn-id: http://core.svn.wordpress.org/trunk@36187 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/random_compat/random.php | 10 ++- .../random_bytes_libsodium_legacy.php | 84 +++++++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 wp-includes/random_compat/random_bytes_libsodium_legacy.php diff --git a/wp-includes/random_compat/random.php b/wp-includes/random_compat/random.php index 8898a35bcd..fb21ef2e01 100644 --- a/wp-includes/random_compat/random.php +++ b/wp-includes/random_compat/random.php @@ -58,7 +58,11 @@ if (PHP_VERSION_ID < 70000) { */ 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') && @@ -75,8 +79,8 @@ if (PHP_VERSION_ID < 70000) { // that is not helpful to us here. // See random_bytes_dev_urandom.php - require_once $RandomCompatDIR.'/random_bytes_dev_urandom.php'; - } + require_once $RandomCompatDIR.'/random_bytes_dev_urandom.php'; + } if ( !function_exists('random_bytes') && PHP_VERSION_ID >= 50307 && diff --git a/wp-includes/random_compat/random_bytes_libsodium_legacy.php b/wp-includes/random_compat/random_bytes_libsodium_legacy.php new file mode 100644 index 0000000000..4c76ff96d2 --- /dev/null +++ b/wp-includes/random_compat/random_bytes_libsodium_legacy.php @@ -0,0 +1,84 @@ + 2147483647) { + $buf = ''; + for ($i = 0; $i < $bytes; $i += 1073741824) { + $n = ($bytes - $i) > 1073741824 + ? 1073741824 + : $bytes - $i; + $buf .= Sodium::randombytes_buf($n); + } + } else { + $buf = Sodium::randombytes_buf($bytes); + } + + if ($buf !== false) { + if (RandomCompat_strlen($buf) === $bytes) { + return $buf; + } + } + + /** + * If we reach here, PHP has failed us. + */ + throw new Exception( + 'Could not gather sufficient random data' + ); +} diff --git a/wp-includes/version.php b/wp-includes/version.php index 7e4265d7dc..1554caa5df 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.5-alpha-36219'; +$wp_version = '4.5-alpha-36220'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.