From ad55d3cd7528cf32f0638ff5ff5cb9cec98b216b Mon Sep 17 00:00:00 2001 From: terrafrost Date: Fri, 16 Oct 2015 10:31:43 -0500 Subject: [PATCH] RSA: fixes to last non-merge commit --- phpseclib/Crypt/RSA.php | 3 ++- phpseclib/Crypt/RSA/OpenSSH.php | 4 ++++ phpseclib/Crypt/RSA/PKCS.php | 4 ++++ phpseclib/Crypt/RSA/PuTTY.php | 6 +++++- phpseclib/Crypt/RSA/XML.php | 4 ++++ tests/Unit/Crypt/RSA/LoadKeyTest.php | 5 +++-- 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index ef308473..e3368cf0 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -1009,6 +1009,7 @@ class RSA * @see self::getPrivateKey() * @access private * @param string $type optional + * @return mixed */ function _getPrivatePublicKey($type = 'PKCS8') { @@ -2177,4 +2178,4 @@ class RSA return $this->_rsassa_pss_verify($message, $signature); } } -} \ No newline at end of file +} diff --git a/phpseclib/Crypt/RSA/OpenSSH.php b/phpseclib/Crypt/RSA/OpenSSH.php index 96af6485..7665e998 100644 --- a/phpseclib/Crypt/RSA/OpenSSH.php +++ b/phpseclib/Crypt/RSA/OpenSSH.php @@ -56,6 +56,10 @@ class OpenSSH */ static function load($key, $password = '') { + if (!is_string($key)) { + return false; + } + $parts = explode(' ', $key, 3); $key = isset($parts[1]) ? base64_decode($parts[1]) : false; diff --git a/phpseclib/Crypt/RSA/PKCS.php b/phpseclib/Crypt/RSA/PKCS.php index eaf8e797..f25e20b0 100644 --- a/phpseclib/Crypt/RSA/PKCS.php +++ b/phpseclib/Crypt/RSA/PKCS.php @@ -134,6 +134,10 @@ abstract class PKCS */ static function load($key, $password = '') { + if (!is_string($key)) { + return false; + } + $components = array('isPublicKey' => strpos($key, 'PUBLIC') !== false); /* Although PKCS#1 proposes a format that public and private keys can use, encrypting them is diff --git a/phpseclib/Crypt/RSA/PuTTY.php b/phpseclib/Crypt/RSA/PuTTY.php index a4d804bc..2323e555 100644 --- a/phpseclib/Crypt/RSA/PuTTY.php +++ b/phpseclib/Crypt/RSA/PuTTY.php @@ -76,6 +76,10 @@ class PuTTY */ static function load($key, $password = '') { + if (!is_string($key)) { + return false; + } + static $one; if (!isset($one)) { $one = new BigInteger(1); @@ -88,7 +92,7 @@ class PuTTY return false; } $encryption = trim(preg_replace('#Encryption: (.+)#', '$1', $key[1])); - $comment = trim(preg_replace('#Comment: (.+)#', '$1', $key[2])); + $components['comment'] = trim(preg_replace('#Comment: (.+)#', '$1', $key[2])); $publicLength = trim(preg_replace('#Public-Lines: (\d+)#', '$1', $key[3])); $public = base64_decode(implode('', array_map('trim', array_slice($key, 4, $publicLength)))); diff --git a/phpseclib/Crypt/RSA/XML.php b/phpseclib/Crypt/RSA/XML.php index b2ae953b..00b4d910 100644 --- a/phpseclib/Crypt/RSA/XML.php +++ b/phpseclib/Crypt/RSA/XML.php @@ -40,6 +40,10 @@ class XML */ static function load($key, $password = '') { + if (!is_string($key)) { + return false; + } + $components = array( 'isPublicKey' => false, 'primes' => array(), diff --git a/tests/Unit/Crypt/RSA/LoadKeyTest.php b/tests/Unit/Crypt/RSA/LoadKeyTest.php index ec858630..f309c7d9 100644 --- a/tests/Unit/Crypt/RSA/LoadKeyTest.php +++ b/tests/Unit/Crypt/RSA/LoadKeyTest.php @@ -7,6 +7,7 @@ use phpseclib\Crypt\RSA; use phpseclib\Crypt\RSA\PKCS1; +use phpseclib\Crypt\RSA\PuTTY; use phpseclib\Math\BigInteger; class Unit_Crypt_RSA_LoadKeyTest extends PhpseclibTestCase @@ -368,8 +369,8 @@ Private-MAC: 03e2cb74e1d67652fbad063d2ed0478f31bdf256 $rsa = new RSA(); $key = array( - 'e' => BigInteger('10001', 16), - 'n' => BigInteger('aa18aba43b50deef38598faf87d2ab634e4571c130a9bca7b878267414faab8b471bd8965f5c9fc3' . + 'e' => new BigInteger('10001', 16), + 'n' => new BigInteger('aa18aba43b50deef38598faf87d2ab634e4571c130a9bca7b878267414faab8b471bd8965f5c9fc3' . '818485eaf529c26246f3055064a8de19c8c338be5496cbaeb059dc0b358143b44a35449eb2641131' . '21a455bd7fde3fac919e94b56fb9bb4f651cdb23ead439d6cd523eb08191e75b35fd13a7419b3090' . 'f24787bd4f4e1967', 16)