From 3bddf4d9621f36195ecab8a65c7e24b9c9855dbc Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 8 Feb 2021 23:44:24 -0600 Subject: [PATCH] PublicKeyLoader: add loadPublic, loadPrivate and loadParameters --- phpseclib/Crypt/Common/AsymmetricKey.php | 101 +++++++++++++++++++++++ phpseclib/Crypt/PublicKeyLoader.php | 51 ++++++++++++ 2 files changed, 152 insertions(+) diff --git a/phpseclib/Crypt/Common/AsymmetricKey.php b/phpseclib/Crypt/Common/AsymmetricKey.php index 9c368ebb..8c2d520c 100644 --- a/phpseclib/Crypt/Common/AsymmetricKey.php +++ b/phpseclib/Crypt/Common/AsymmetricKey.php @@ -180,6 +180,55 @@ abstract class AsymmetricKey $new; } + /** + * Loads a private key + * + * @return PrivateKey + * @access public + * @param string|array $key + * @param string $password optional + */ + public function loadPrivate($key, $password = '') + { + $key = self::load($key, $password); + if (!$key instanceof PrivateKey) { + throw new NoKeyLoadedException('The key that was loaded was not a private key'); + } + return $key; + } + + /** + * Loads a public key + * + * @return PublicKey + * @access public + * @param string|array $key + */ + public function loadPublic($key) + { + $key = self::load($key); + if (!$key instanceof PublicKey) { + throw new NoKeyLoadedException('The key that was loaded was not a public key'); + } + return $key; + } + + /** + * Loads parameters + * + * @return AsymmetricKey + * @access public + * @param string|array $key + */ + public function loadParameters($key) + { + $key = self::load($key); + if (!$key instanceof PrivateKey && !$key instanceof PublicKey) { + throw new NoKeyLoadedException('The key that was loaded was not a parameter'); + } + return $key; + } + /** * Load the key, assuming a specific format * @@ -212,6 +261,58 @@ abstract class AsymmetricKey $new; } + /** + * Loads a private key + * + * @return PrivateKey + * @access public + * @param string $type + * @param string $key + * @param string $password optional + */ + public function loadPrivateFormat($type, $key, $password = false) + { + $key = self::loadFormat($type, $key, $password); + if (!$key instanceof PrivateKey) { + throw new NoKeyLoadedException('The key that was loaded was not a private key'); + } + return $key; + } + + /** + * Loads a public key + * + * @return PublicKey + * @access public + * @param string $type + * @param string $key + */ + public function loadPublicFormat($type, $key) + { + $key = self::loadFormat($type, $key); + if (!$key instanceof PublicKey) { + throw new NoKeyLoadedException('The key that was loaded was not a public key'); + } + return $key; + } + + /** + * Loads parameters + * + * @return AsymmetricKey + * @access public + * @param string $type + * @param string|array $key + */ + public function loadParametersFormat($type, $key) + { + $key = self::loadFormat($type, $key); + if (!$key instanceof PrivateKey && !$key instanceof PublicKey) { + throw new NoKeyLoadedException('The key that was loaded was not a parameter'); + } + return $key; + } + /** * Validate Plugin * diff --git a/phpseclib/Crypt/PublicKeyLoader.php b/phpseclib/Crypt/PublicKeyLoader.php index c7c7e0af..33fb3b89 100644 --- a/phpseclib/Crypt/PublicKeyLoader.php +++ b/phpseclib/Crypt/PublicKeyLoader.php @@ -16,6 +16,8 @@ namespace phpseclib3\Crypt; use phpseclib3\Crypt\Common\AsymmetricKey; +use phpseclib3\Crypt\Common\PublicKey; +use phpseclib3\Crypt\Common\PrivateKey; use phpseclib3\Exception\NoKeyLoadedException; use phpseclib3\File\X509; @@ -61,4 +63,53 @@ abstract class PublicKeyLoader throw new NoKeyLoadedException('Unable to read key'); } + + /** + * Loads a private key + * + * @return PrivateKey + * @access public + * @param string|array $key + * @param string $password optional + */ + public function loadPrivate($key, $password = false) + { + $key = self::load($key, $password); + if (!$key instanceof PrivateKey) { + throw new NoKeyLoadedException('The key that was loaded was not a private key'); + } + return $key; + } + + /** + * Loads a public key + * + * @return PublicKey + * @access public + * @param string|array $key + */ + public function loadPublic($key) + { + $key = self::load($key); + if (!$key instanceof PublicKey) { + throw new NoKeyLoadedException('The key that was loaded was not a public key'); + } + return $key; + } + + /** + * Loads parameters + * + * @return AsymmetricKey + * @access public + * @param string|array $key + */ + public function loadParameters($key) + { + $key = self::load($key); + if (!$key instanceof PrivateKey && !$key instanceof PublicKey) { + throw new NoKeyLoadedException('The key that was loaded was not a parameter'); + } + return $key; + } }