From 1522e6606b1264ecfd6deb66b69658c0145714f4 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Thu, 22 Oct 2015 10:57:05 -0500 Subject: [PATCH] RSA: add getSupportedFormats() and getLoadedFormat() --- phpseclib/Crypt/RSA.php | 85 +++++++++++++++++++++++----- tests/Unit/Crypt/RSA/LoadKeyTest.php | 4 +- 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index e3368cf0..1c1f0dcd 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -315,6 +315,14 @@ class RSA */ var $password = false; + /** + * Loaded File Format + * + * @var string + * @access private + */ + var $format = false; + /** * OpenSSL configuration file name. * @@ -327,14 +335,24 @@ class RSA static $configFile; /** - * Supported file formats + * Supported file formats (lower case) * - * @see self::load() + * @see self::_initialize_static_variables() * @var array * @access private */ static $fileFormats = false; + /** + * Supported file formats (original case) + * + * @see self::_initialize_static_variables() + * @var array + * @access private + */ + static $origFileFormats = false; + + /** * Initialize static variables * @@ -355,6 +373,7 @@ class RSA $meta = new \ReflectionClass($type); if (!$meta->isAbstract()) { self::$fileFormats[strtolower($name)] = $type; + self::$origFileFormats[] = $name; } } } @@ -650,11 +669,25 @@ class RSA if (class_exists($fullname)) { $meta = new \ReflectionClass($path); - $shortname = strtolower($meta->getShortName()); - self::$fileFormats[$shortname] = $fullname; + $shortname = $meta->getShortName(); + self::$fileFormats[strtolower($shortname)] = $fullname; + self::$origFileFormats[] = $shortname; } } + /** + * Returns a list of supported formats. + * + * @access public + * @return array + */ + static function getSupportedFormats() + { + self::_initialize_static_variables(); + + return self::$origFileFormats; + } + /** * Loads a public or private key * @@ -724,11 +757,11 @@ class RSA } } } else { - $type = strtolower($type); - if (isset(self::$fileFormats[$type])) { - $type = self::$fileFormats[$type]; + $format = strtolower($type); + if (isset(self::$fileFormats[$format])) { + $format = self::$fileFormats[$format]; try { - $components = $type::load($key, $this->password); + $components = $format::load($key, $this->password); } catch (Exception $e) { $components = false; } @@ -736,9 +769,12 @@ class RSA } if ($components === false) { + $this->format = false; return false; } + $this->format = $format; + $this->modulus = $components['modulus']; $this->k = strlen($this->modulus->toBytes()); $this->exponent = isset($components['privateExponent']) ? $components['privateExponent'] : $components['publicExponent']; @@ -761,7 +797,27 @@ class RSA return true; } - /** + /** + * Returns the format of the loaded key. + * + * If the key that was loaded wasn't in a valid or if the key was auto-generated + * with RSA::createKey() then this will return false. + * + * @see self::load() + * @access public + * @return mixed + */ + function getLoadedFormat() + { + if ($this->format === false) { + return false; + } + + $meta = new \ReflectionClass($this->format); + return $meta->getShortName(); + } + + /** * Returns the private key * * The private key is only returned if the currently loaded key contains the constituent prime numbers. @@ -871,11 +927,11 @@ class RSA } } } else { - $type = strtolower($type); - if (isset(self::$fileFormats[$type])) { - $type = self::$fileFormats[$type]; + $format = strtolower($type); + if (isset(self::$fileFormats[$format])) { + $format = self::$fileFormats[$format]; try { - $components = $type::load($key, $this->password); + $components = $format::load($key, $this->password); } catch (Exception $e) { $components = false; } @@ -883,9 +939,12 @@ class RSA } if ($components === false) { + $this->format = false; return false; } + $this->format = $format; + if (empty($this->modulus) || !$this->modulus->equals($components['modulus'])) { $this->modulus = $components['modulus']; $this->exponent = $this->publicExponent = $components['publicExponent']; diff --git a/tests/Unit/Crypt/RSA/LoadKeyTest.php b/tests/Unit/Crypt/RSA/LoadKeyTest.php index 28322e26..769e6cb5 100644 --- a/tests/Unit/Crypt/RSA/LoadKeyTest.php +++ b/tests/Unit/Crypt/RSA/LoadKeyTest.php @@ -409,7 +409,7 @@ Private-MAC: 35134b7434bf828b21404099861d455e660e8740'; $this->assertGreaterThanOrEqual(1, strlen("$rsa")); } - public function testPrivateBlob() + public function testPrivateMSBlob() { $key = 'BwIAAACkAABSU0EyAAQAAAEAAQAnh6FFs6kYe/gmb9dzqsQKmtjFE9mxNAe9mEU3OwOEEfyI' . 'wkAx0/8dwh12fuP4wzNbdZAq4mmqCE6Lo8wTNNIJVNYEhKq5chHg1+hPDgfETFgtEO54JZSg' . @@ -428,6 +428,8 @@ Private-MAC: 35134b7434bf828b21404099861d455e660e8740'; $privKey = new RSA(); $privKey->load($key); + $this->assertSame($privKey->getLoadedFormat(), 'MSBLOB'); + $this->assertGreaterThanOrEqual(1, strlen("$privKey")); $pubKey = new RSA();