mirror of
https://github.com/phpseclib/phpseclib.git
synced 2025-01-27 17:18:25 +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;
|
var $password = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loaded File Format
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
var $format = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OpenSSL configuration file name.
|
* OpenSSL configuration file name.
|
||||||
*
|
*
|
||||||
@ -327,14 +335,24 @@ class RSA
|
|||||||
static $configFile;
|
static $configFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Supported file formats
|
* Supported file formats (lower case)
|
||||||
*
|
*
|
||||||
* @see self::load()
|
* @see self::_initialize_static_variables()
|
||||||
* @var array
|
* @var array
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
static $fileFormats = false;
|
static $fileFormats = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supported file formats (original case)
|
||||||
|
*
|
||||||
|
* @see self::_initialize_static_variables()
|
||||||
|
* @var array
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
static $origFileFormats = false;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize static variables
|
* Initialize static variables
|
||||||
*
|
*
|
||||||
@ -355,6 +373,7 @@ class RSA
|
|||||||
$meta = new \ReflectionClass($type);
|
$meta = new \ReflectionClass($type);
|
||||||
if (!$meta->isAbstract()) {
|
if (!$meta->isAbstract()) {
|
||||||
self::$fileFormats[strtolower($name)] = $type;
|
self::$fileFormats[strtolower($name)] = $type;
|
||||||
|
self::$origFileFormats[] = $name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -650,11 +669,25 @@ class RSA
|
|||||||
|
|
||||||
if (class_exists($fullname)) {
|
if (class_exists($fullname)) {
|
||||||
$meta = new \ReflectionClass($path);
|
$meta = new \ReflectionClass($path);
|
||||||
$shortname = strtolower($meta->getShortName());
|
$shortname = $meta->getShortName();
|
||||||
self::$fileFormats[$shortname] = $fullname;
|
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
|
* Loads a public or private key
|
||||||
*
|
*
|
||||||
@ -724,11 +757,11 @@ class RSA
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$type = strtolower($type);
|
$format = strtolower($type);
|
||||||
if (isset(self::$fileFormats[$type])) {
|
if (isset(self::$fileFormats[$format])) {
|
||||||
$type = self::$fileFormats[$type];
|
$format = self::$fileFormats[$format];
|
||||||
try {
|
try {
|
||||||
$components = $type::load($key, $this->password);
|
$components = $format::load($key, $this->password);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$components = false;
|
$components = false;
|
||||||
}
|
}
|
||||||
@ -736,9 +769,12 @@ class RSA
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($components === false) {
|
if ($components === false) {
|
||||||
|
$this->format = false;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->format = $format;
|
||||||
|
|
||||||
$this->modulus = $components['modulus'];
|
$this->modulus = $components['modulus'];
|
||||||
$this->k = strlen($this->modulus->toBytes());
|
$this->k = strlen($this->modulus->toBytes());
|
||||||
$this->exponent = isset($components['privateExponent']) ? $components['privateExponent'] : $components['publicExponent'];
|
$this->exponent = isset($components['privateExponent']) ? $components['privateExponent'] : $components['publicExponent'];
|
||||||
@ -761,7 +797,27 @@ class RSA
|
|||||||
return true;
|
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
|
* Returns the private key
|
||||||
*
|
*
|
||||||
* The private key is only returned if the currently loaded key contains the constituent prime numbers.
|
* The private key is only returned if the currently loaded key contains the constituent prime numbers.
|
||||||
@ -871,11 +927,11 @@ class RSA
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$type = strtolower($type);
|
$format = strtolower($type);
|
||||||
if (isset(self::$fileFormats[$type])) {
|
if (isset(self::$fileFormats[$format])) {
|
||||||
$type = self::$fileFormats[$type];
|
$format = self::$fileFormats[$format];
|
||||||
try {
|
try {
|
||||||
$components = $type::load($key, $this->password);
|
$components = $format::load($key, $this->password);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$components = false;
|
$components = false;
|
||||||
}
|
}
|
||||||
@ -883,9 +939,12 @@ class RSA
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($components === false) {
|
if ($components === false) {
|
||||||
|
$this->format = false;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->format = $format;
|
||||||
|
|
||||||
if (empty($this->modulus) || !$this->modulus->equals($components['modulus'])) {
|
if (empty($this->modulus) || !$this->modulus->equals($components['modulus'])) {
|
||||||
$this->modulus = $components['modulus'];
|
$this->modulus = $components['modulus'];
|
||||||
$this->exponent = $this->publicExponent = $components['publicExponent'];
|
$this->exponent = $this->publicExponent = $components['publicExponent'];
|
||||||
|
@ -409,7 +409,7 @@ Private-MAC: 35134b7434bf828b21404099861d455e660e8740';
|
|||||||
$this->assertGreaterThanOrEqual(1, strlen("$rsa"));
|
$this->assertGreaterThanOrEqual(1, strlen("$rsa"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPrivateBlob()
|
public function testPrivateMSBlob()
|
||||||
{
|
{
|
||||||
$key = 'BwIAAACkAABSU0EyAAQAAAEAAQAnh6FFs6kYe/gmb9dzqsQKmtjFE9mxNAe9mEU3OwOEEfyI' .
|
$key = 'BwIAAACkAABSU0EyAAQAAAEAAQAnh6FFs6kYe/gmb9dzqsQKmtjFE9mxNAe9mEU3OwOEEfyI' .
|
||||||
'wkAx0/8dwh12fuP4wzNbdZAq4mmqCE6Lo8wTNNIJVNYEhKq5chHg1+hPDgfETFgtEO54JZSg' .
|
'wkAx0/8dwh12fuP4wzNbdZAq4mmqCE6Lo8wTNNIJVNYEhKq5chHg1+hPDgfETFgtEO54JZSg' .
|
||||||
@ -428,6 +428,8 @@ Private-MAC: 35134b7434bf828b21404099861d455e660e8740';
|
|||||||
$privKey = new RSA();
|
$privKey = new RSA();
|
||||||
$privKey->load($key);
|
$privKey->load($key);
|
||||||
|
|
||||||
|
$this->assertSame($privKey->getLoadedFormat(), 'MSBLOB');
|
||||||
|
|
||||||
$this->assertGreaterThanOrEqual(1, strlen("$privKey"));
|
$this->assertGreaterThanOrEqual(1, strlen("$privKey"));
|
||||||
|
|
||||||
$pubKey = new RSA();
|
$pubKey = new RSA();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user