From 5573187f3d22f18e2a0acd4946009f191d0b383a Mon Sep 17 00:00:00 2001 From: terrafrost Date: Fri, 28 Jun 2019 05:32:38 -0500 Subject: [PATCH] rm $type parameter from AsymmetricKey::load and add loadFormat() --- phpseclib/Crypt/Common/AsymmetricKey.php | 59 ++++++++++++++++-------- phpseclib/Crypt/DSA.php | 14 ++---- phpseclib/Crypt/EC.php | 15 ++---- phpseclib/Crypt/PublicKeyLoader.php | 6 +-- phpseclib/Crypt/RSA.php | 16 ++----- phpseclib/File/X509.php | 16 +++---- phpseclib/Net/SSH2.php | 6 +-- 7 files changed, 65 insertions(+), 67 deletions(-) diff --git a/phpseclib/Crypt/Common/AsymmetricKey.php b/phpseclib/Crypt/Common/AsymmetricKey.php index 380e5502..82e15cdf 100644 --- a/phpseclib/Crypt/Common/AsymmetricKey.php +++ b/phpseclib/Crypt/Common/AsymmetricKey.php @@ -146,31 +146,22 @@ abstract class AsymmetricKey * Load the key * * @param string $key - * @param string $type - * @param string $password - * @return array|bool + * @param string $password optional + * @return AsymmetricKey */ - protected static function load($key, $type, $password) + public static function load($key, $password = false) { self::initialize_static_variables(); $components = false; - if ($type === false) { - foreach (self::$plugins[static::ALGORITHM]['Keys'] as $format) { - try { - $components = $format::load($key, $password); - } catch (\Exception $e) { - $components = false; - } - if ($components !== false) { - break; - } - } - } else { - $format = strtolower($type); - if (isset(self::$plugins[static::ALGORITHM]['Keys'][$format])) { - $format = self::$plugins[static::ALGORITHM]['Keys'][$format]; + foreach (self::$plugins[static::ALGORITHM]['Keys'] as $format) { + try { $components = $format::load($key, $password); + } catch (\Exception $e) { + $components = false; + } + if ($components !== false) { + break; } } @@ -180,7 +171,35 @@ abstract class AsymmetricKey $components['format'] = $format; - return $components; + return static::onLoad($components); + } + + /** + * Load the key, assuming a specific format + * + * @param string $key + * @param string $type + * @param string $password optional + * @return AsymmetricKey + */ + public static function loadFormat($type, $key, $password = false) + { + self::initialize_static_variables(); + + $components = false; + $format = strtolower($type); + if (isset(self::$plugins[static::ALGORITHM]['Keys'][$format])) { + $format = self::$plugins[static::ALGORITHM]['Keys'][$format]; + $components = $format::load($key, $password); + } + + if ($components === false) { + throw new NoKeyLoadedException('Unable to read key'); + } + + $components['format'] = $format; + + return static::onLoad($components); } /** diff --git a/phpseclib/Crypt/DSA.php b/phpseclib/Crypt/DSA.php index 6139122c..919d83e7 100644 --- a/phpseclib/Crypt/DSA.php +++ b/phpseclib/Crypt/DSA.php @@ -224,24 +224,18 @@ abstract class DSA extends AsymmetricKey } /** - * Loads a public or private key + * OnLoad Handler * - * Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed) * @return bool - * @access public - * @param string $key - * @param string $type optional - * @param string $password optional + * @access protected + * @param array $components */ - public static function load($key, $type = false, $password = false) + protected static function onLoad($components) { - self::initialize_static_variables(); - if (!isset(self::$engines['PHP'])) { self::useBestEngine(); } - $components = parent::load($key, $type, $password); if (!isset($components['x']) && !isset($components['y'])) { $new = new Parameters; } else if (isset($components['x'])) { diff --git a/phpseclib/Crypt/EC.php b/phpseclib/Crypt/EC.php index f86795f7..5c0f7494 100644 --- a/phpseclib/Crypt/EC.php +++ b/phpseclib/Crypt/EC.php @@ -187,25 +187,18 @@ abstract class EC extends AsymmetricKey } /** - * Loads a public or private key + * OnLoad Handler * - * Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed) * @return bool - * @access public - * @param string $key - * @param string $type optional - * @param string $password optional + * @access protected + * @param array $components */ - public static function load($key, $type = false, $password = false) + protected static function onLoad($components) { - self::initialize_static_variables(); - if (!isset(self::$engines['PHP'])) { self::useBestEngine(); } - $components = parent::load($key, $type, $password); - if (!isset($components['dA']) && !isset($components['QA'])) { $new = new Parameters; $new->curve = $components['curve']; diff --git a/phpseclib/Crypt/PublicKeyLoader.php b/phpseclib/Crypt/PublicKeyLoader.php index 147cc86c..b65c5b2b 100644 --- a/phpseclib/Crypt/PublicKeyLoader.php +++ b/phpseclib/Crypt/PublicKeyLoader.php @@ -39,18 +39,18 @@ abstract class PublicKeyLoader public static function load($key, $password = false) { try { - $new = EC::load($key, false, $password); + $new = EC::load($key, $password); } catch (\Exception $e) {} if (!isset($new)) { try { - $new = RSA::load($key, false, $password); + $new = RSA::load($key, $password); } catch (\Exception $e) {} } if (!isset($new)) { try { - $new = DSA::load($key, false, $password); + $new = DSA::load($key, $password); } catch (\Exception $e) {} } diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index d1187bae..978509b4 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -387,22 +387,14 @@ abstract class RSA extends AsymmetricKey } /** - * Loads a public or private key - * - * Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed) + * OnLoad Handler * * @return bool - * @access public - * @param string $key - * @param string $type optional - * @param string $password optional + * @access protected + * @param array $components */ - public static function load($key, $type = false, $password = false) + protected static function onLoad($components) { - self::initialize_static_variables(); - - $components = parent::load($key, $type, $password); - $key = $components['isPublicKey'] ? new PublicKey : new PrivateKey; diff --git a/phpseclib/File/X509.php b/phpseclib/File/X509.php index b607f8a8..7e70ff3d 100644 --- a/phpseclib/File/X509.php +++ b/phpseclib/File/X509.php @@ -1358,10 +1358,10 @@ class X509 { switch ($publicKeyAlgorithm) { case 'id-RSASSA-PSS': - $key = RSA::load($publicKey, 'PSS'); + $key = RSA::loadFormat('PSS', $publicKey); break; case 'rsaEncryption': - $key = RSA::load($publicKey, 'PKCS8'); + $key = RSA::loadFormat('PKCS8', $publicKey); switch ($signatureAlgorithm) { case 'md2WithRSAEncryption': case 'md5WithRSAEncryption': @@ -1380,10 +1380,10 @@ class X509 break; case 'id-Ed25519': case 'id-Ed448': - $key = EC::load($publicKey, 'PKCS8'); + $key = EC::loadFormat('PKCS8', $publicKey); break; case 'id-ecPublicKey': - $key = EC::load($publicKey, 'PKCS8'); + $key = EC::loadFormat('PKCS8', $publicKey); switch ($signatureAlgorithm) { case 'ecdsa-with-SHA1': case 'ecdsa-with-SHA224': @@ -1398,7 +1398,7 @@ class X509 } break; case 'id-dsa': - $key = DSA::load($publicKey, 'PKCS8'); + $key = DSA::loadFormat('PKCS8', $publicKey); switch ($signatureAlgorithm) { case 'id-dsa-with-sha1': case 'id-dsa-with-sha224': @@ -2089,13 +2089,13 @@ class X509 switch ($keyinfo['algorithm']['algorithm']) { case 'rsaEncryption': - return RSA::load($key, 'PKCS8'); + return RSA::loadFormat('PKCS8', $key); case 'id-ecPublicKey': case 'id-Ed25519': case 'id-Ed448': - return EC::load($key, 'PKCS8'); + return EC::loadFormat('PKCS8', $key); case 'id-dsa': - return DSA::load($key, 'PKCS8'); + return DSA::loadFormat('PKCS8', $key); } return false; diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 7bec4923..ec1a1277 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -4601,7 +4601,7 @@ class SSH2 case 'ecdsa-sha2-nistp256': case 'ecdsa-sha2-nistp384': case 'ecdsa-sha2-nistp521': - $key = EC::load($server_public_host_key, 'OpenSSH') + $key = EC::loadFormat('OpenSSH', $server_public_host_key) ->withSignatureFormat('SSH2'); switch ($this->signature_format) { case 'ssh-ed25519': @@ -4620,7 +4620,7 @@ class SSH2 $key = $key->withHash($hash); break; case 'ssh-dss': - $key = DSA::load($server_public_host_key, 'OpenSSH') + $key = DSA::loadFormat('OpenSSH', $server_public_host_key) ->withSignatureFormat('SSH2') ->withHash('sha1'); break; @@ -4634,7 +4634,7 @@ class SSH2 $temp = unpack('Nlength', Strings::shift($signature, 4)); $signature = Strings::shift($signature, $temp['length']); - $key = RSA::load($server_public_host_key, 'OpenSSH') + $key = RSA::loadFormat('OpenSSH', $server_public_host_key) ->withPadding(RSA::SIGNATURE_PKCS1); switch ($this->signature_format) { case 'rsa-sha2-512':