mirror of
https://github.com/phpseclib/phpseclib.git
synced 2025-01-13 18:02:58 +00:00
RSA: add getSupportedFormats() and getLoadedFormat()
This commit is contained in:
parent
efe36d67ce
commit
1522e6606b
@ -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'];
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user