From 54f0f2131bf13a8f4e968118586ef7cc82f416c5 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Tue, 10 Sep 2013 11:07:56 -0500 Subject: [PATCH] RSA, BigInteger: add more openssl checks --- phpseclib/Crypt/RSA.php | 26 +++++++++++++++++++++++++- phpseclib/Math/BigInteger.php | 26 ++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 784494a2..1e990137 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -469,7 +469,31 @@ class Crypt_RSA { if ( !defined('CRYPT_RSA_MODE') ) { switch (true) { case extension_loaded('openssl') && version_compare(PHP_VERSION, '4.2.0', '>=') && file_exists($this->configFile): - define('CRYPT_RSA_MODE', CRYPT_RSA_MODE_OPENSSL); + // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work + ob_start(); + phpinfo(); + $content = ob_get_contents(); + ob_end_clean(); + + preg_match_all('#OpenSSL (Header|Library) Version(.*)#im', $content, $matches); + + $versions = array(); + if (!empty($matches[1])) { + for ($i = 0; $i < count($matches[1]); $i++) { + $versions[$matches[1][$i]] = trim(str_replace('=>', '', strip_tags($matches[2][$i]))); + } + } + + switch (true) { + case !isset($versions['Header']): + case !isset($versions['Library']): + case $versions['Header'] != $versions['Library']: + define('CRYPT_RSA_MODE', CRYPT_RSA_MODE_INTERNAL); + define('MATH_BIGINTEGER_OPENSSL_DISABLE', true); + break; + default: + define('CRYPT_RSA_MODE', CRYPT_RSA_MODE_OPENSSL); + } break; default: define('CRYPT_RSA_MODE', CRYPT_RSA_MODE_INTERNAL); diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 4fe15669..fea3f630 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -272,7 +272,30 @@ class Math_BigInteger { } if (function_exists('openssl_public_encrypt') && !defined('MATH_BIGINTEGER_OPENSSL_DISABLE') && !defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { - define('MATH_BIGINTEGER_OPENSSL_ENABLED', true); + // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work + ob_start(); + phpinfo(); + $content = ob_get_contents(); + ob_end_clean(); + + preg_match_all('#OpenSSL (Header|Library) Version(.*)#im', $content, $matches); + + $versions = array(); + if (!empty($matches[1])) { + for ($i = 0; $i < count($matches[1]); $i++) { + $versions[$matches[1][$i]] = trim(str_replace('=>', '', strip_tags($matches[2][$i]))); + } + } + + switch (true) { + case !isset($versions['Header']): + case !isset($versions['Library']): + case $versions['Header'] != $versions['Library']: + define('MATH_BIGINTEGER_OPENSSL_DISABLE', true); + break; + default: + define('MATH_BIGINTEGER_OPENSSL_ENABLED', true); + } } if (!defined('PHP_INT_SIZE')) { @@ -438,7 +461,6 @@ class Math_BigInteger { } $x = str_pad($x, strlen($x) + ((MATH_BIGINTEGER_MAX10_LEN - 1) * strlen($x)) % MATH_BIGINTEGER_MAX10_LEN, 0, STR_PAD_LEFT); - while (strlen($x)) { $temp = $temp->multiply($multiplier); $temp = $temp->add(new Math_BigInteger($this->_int2bytes(substr($x, 0, MATH_BIGINTEGER_MAX10_LEN)), 256));