RSA: make it so plugin names are not case sensitive

This commit is contained in:
terrafrost 2015-10-04 23:11:38 -05:00
parent cfcf554531
commit 829ce9a3ec

View File

@ -346,6 +346,18 @@ class RSA
self::$zero= new BigInteger(0); self::$zero= new BigInteger(0);
self::$one = new BigInteger(1); self::$one = new BigInteger(1);
self::$configFile = __DIR__ . '/../openssl.cnf'; self::$configFile = __DIR__ . '/../openssl.cnf';
if (self::$fileFormats === false) {
self::$fileFormats = array();
foreach (glob(__DIR__ . '/RSA/*.php') as $file) {
$name = pathinfo($file, PATHINFO_FILENAME);
$type = 'phpseclib\Crypt\RSA\\' . $name;
$meta = new \ReflectionClass($type);
if (!$meta->isAbstract()) {
self::$fileFormats[strtolower($name)] = $type;
}
}
}
} }
} }
@ -622,22 +634,24 @@ class RSA
} }
/** /**
* Pre-loads all the key format plugins * Add a fileformat plugin
* *
* @see load() * The plugin needs to either already be loaded or be auto-loadable.
* @access private * Loading a plugin whose shortname overwrite an existing shortname will overwrite the old plugin.
*
* @see \phpseclib\Crypt\RSA::load()
* @param string $fullname
* @access public
* @return bool
*/ */
function _loadFileFormats() static function addFileFormat($fullname)
{ {
if (self::$fileFormats === false) { self::_initialize_static_variables();
self::$fileFormats = array();
foreach (glob(__DIR__ . '/RSA/*.php') as $file) { if (class_exists($fullname)) {
$type = 'phpseclib\Crypt\RSA\\' . pathinfo($file, PATHINFO_FILENAME); $meta = new \ReflectionClass($path);
$meta = new \ReflectionClass($type); $shortname = strtolower($meta->getShortName());
if (!$meta->isAbstract()) { self::$fileFormats[$shortname] = $fullname;
self::$fileFormats[] = $type;
}
}
} }
} }
@ -699,7 +713,6 @@ class RSA
$components = false; $components = false;
if ($type === false) { if ($type === false) {
$this->_loadFileFormats();
foreach (self::$fileFormats as $format) { foreach (self::$fileFormats as $format) {
try { try {
$components = $format::load($key, $this->password); $components = $format::load($key, $this->password);
@ -711,10 +724,9 @@ class RSA
} }
} }
} else { } else {
if ($type[0] != '\\') { $type = strtolower($type);
$type = '\phpseclib\Crypt\RSA\\' . $type; if (isset(self::$fileFormats[$type])) {
} $type = self::$fileFormats[$type];
if (class_exists($type)) {
try { try {
$components = $type::load($key, $this->password); $components = $type::load($key, $this->password);
} catch (Exception $e) { } catch (Exception $e) {
@ -760,10 +772,12 @@ class RSA
*/ */
function getPrivateKey($type = 'PKCS1') function getPrivateKey($type = 'PKCS1')
{ {
if ($type[0] != '\\') { $type = strtolower($type);
$type = '\phpseclib\Crypt\RSA\\' . $type; if (!isset(self::$fileFormats[$type])) {
return false;
} }
if (!class_exists($type) || !method_exists($type, 'savePrivateKey')) { $type = self::$fileFormats[$type];
if (!method_exists($type, 'savePrivateKey')) {
return false; return false;
} }
@ -842,7 +856,6 @@ class RSA
$components = false; $components = false;
if ($type === false) { if ($type === false) {
$this->_loadFileFormats();
foreach (self::$fileFormats as $format) { foreach (self::$fileFormats as $format) {
if (!method_exists($format, 'savePublicKey')) { if (!method_exists($format, 'savePublicKey')) {
continue; continue;
@ -857,10 +870,9 @@ class RSA
} }
} }
} else { } else {
if ($type[0] != '\\') { $type = strtolower($type);
$type = '\phpseclib\Crypt\RSA\\' . $type; if (isset(self::$fileFormats[$type])) {
} $type = self::$fileFormats[$type];
if (class_exists($type)) {
try { try {
$components = $type::load($key, $this->password); $components = $type::load($key, $this->password);
} catch (Exception $e) { } catch (Exception $e) {
@ -931,10 +943,12 @@ class RSA
*/ */
function getPublicKey($type = 'PKCS8') function getPublicKey($type = 'PKCS8')
{ {
if ($type[0] != '\\') { $type = strtolower($type);
$type = '\phpseclib\Crypt\RSA\\' . $type; if (!isset(self::$fileFormats[$type])) {
return false;
} }
if (!class_exists($type) || !method_exists($type, 'savePublicKey')) { $type = self::$fileFormats[$type];
if (!method_exists($type, 'savePublicKey')) {
return false; return false;
} }
@ -995,10 +1009,12 @@ class RSA
*/ */
function _getPrivatePublicKey($type = 'PKCS8') function _getPrivatePublicKey($type = 'PKCS8')
{ {
if ($type[0] != '\\') { $type = strtolower($type);
$type = '\phpseclib\Crypt\RSA\\' . $type; if (!isset(self::$fileFormats[$type])) {
return false;
} }
if (!class_exists($type) || !method_exists($type, 'savePublicKey')) { $type = self::$fileFormats[$type];
if (!method_exists($type, 'savePublicKey')) {
return false; return false;
} }