rm $type parameter from AsymmetricKey::load and add loadFormat()

This commit is contained in:
terrafrost 2019-06-28 05:32:38 -05:00
parent 289ae55f9f
commit 5573187f3d
7 changed files with 65 additions and 67 deletions

View File

@ -146,31 +146,22 @@ abstract class AsymmetricKey
* Load the key * Load the key
* *
* @param string $key * @param string $key
* @param string $type * @param string $password optional
* @param string $password * @return AsymmetricKey
* @return array|bool
*/ */
protected static function load($key, $type, $password) public static function load($key, $password = false)
{ {
self::initialize_static_variables(); self::initialize_static_variables();
$components = false; $components = false;
if ($type === false) { foreach (self::$plugins[static::ALGORITHM]['Keys'] as $format) {
foreach (self::$plugins[static::ALGORITHM]['Keys'] as $format) { try {
try {
$components = $format::load($key, $password);
} catch (\Exception $e) {
$components = false;
}
if ($components !== false) {
break;
}
}
} else {
$format = strtolower($type);
if (isset(self::$plugins[static::ALGORITHM]['Keys'][$format])) {
$format = self::$plugins[static::ALGORITHM]['Keys'][$format];
$components = $format::load($key, $password); $components = $format::load($key, $password);
} catch (\Exception $e) {
$components = false;
}
if ($components !== false) {
break;
} }
} }
@ -180,7 +171,35 @@ abstract class AsymmetricKey
$components['format'] = $format; $components['format'] = $format;
return $components; return static::onLoad($components);
}
/**
* Load the key, assuming a specific format
*
* @param string $key
* @param string $type
* @param string $password optional
* @return AsymmetricKey
*/
public static function loadFormat($type, $key, $password = false)
{
self::initialize_static_variables();
$components = false;
$format = strtolower($type);
if (isset(self::$plugins[static::ALGORITHM]['Keys'][$format])) {
$format = self::$plugins[static::ALGORITHM]['Keys'][$format];
$components = $format::load($key, $password);
}
if ($components === false) {
throw new NoKeyLoadedException('Unable to read key');
}
$components['format'] = $format;
return static::onLoad($components);
} }
/** /**

View File

@ -224,24 +224,18 @@ abstract class DSA extends AsymmetricKey
} }
/** /**
* Loads a public or private key * OnLoad Handler
* *
* Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed)
* @return bool * @return bool
* @access public * @access protected
* @param string $key * @param array $components
* @param string $type optional
* @param string $password optional
*/ */
public static function load($key, $type = false, $password = false) protected static function onLoad($components)
{ {
self::initialize_static_variables();
if (!isset(self::$engines['PHP'])) { if (!isset(self::$engines['PHP'])) {
self::useBestEngine(); self::useBestEngine();
} }
$components = parent::load($key, $type, $password);
if (!isset($components['x']) && !isset($components['y'])) { if (!isset($components['x']) && !isset($components['y'])) {
$new = new Parameters; $new = new Parameters;
} else if (isset($components['x'])) { } else if (isset($components['x'])) {

View File

@ -187,25 +187,18 @@ abstract class EC extends AsymmetricKey
} }
/** /**
* Loads a public or private key * OnLoad Handler
* *
* Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed)
* @return bool * @return bool
* @access public * @access protected
* @param string $key * @param array $components
* @param string $type optional
* @param string $password optional
*/ */
public static function load($key, $type = false, $password = false) protected static function onLoad($components)
{ {
self::initialize_static_variables();
if (!isset(self::$engines['PHP'])) { if (!isset(self::$engines['PHP'])) {
self::useBestEngine(); self::useBestEngine();
} }
$components = parent::load($key, $type, $password);
if (!isset($components['dA']) && !isset($components['QA'])) { if (!isset($components['dA']) && !isset($components['QA'])) {
$new = new Parameters; $new = new Parameters;
$new->curve = $components['curve']; $new->curve = $components['curve'];

View File

@ -39,18 +39,18 @@ abstract class PublicKeyLoader
public static function load($key, $password = false) public static function load($key, $password = false)
{ {
try { try {
$new = EC::load($key, false, $password); $new = EC::load($key, $password);
} catch (\Exception $e) {} } catch (\Exception $e) {}
if (!isset($new)) { if (!isset($new)) {
try { try {
$new = RSA::load($key, false, $password); $new = RSA::load($key, $password);
} catch (\Exception $e) {} } catch (\Exception $e) {}
} }
if (!isset($new)) { if (!isset($new)) {
try { try {
$new = DSA::load($key, false, $password); $new = DSA::load($key, $password);
} catch (\Exception $e) {} } catch (\Exception $e) {}
} }

View File

@ -387,22 +387,14 @@ abstract class RSA extends AsymmetricKey
} }
/** /**
* Loads a public or private key * OnLoad Handler
*
* Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed)
* *
* @return bool * @return bool
* @access public * @access protected
* @param string $key * @param array $components
* @param string $type optional
* @param string $password optional
*/ */
public static function load($key, $type = false, $password = false) protected static function onLoad($components)
{ {
self::initialize_static_variables();
$components = parent::load($key, $type, $password);
$key = $components['isPublicKey'] ? $key = $components['isPublicKey'] ?
new PublicKey : new PublicKey :
new PrivateKey; new PrivateKey;

View File

@ -1358,10 +1358,10 @@ class X509
{ {
switch ($publicKeyAlgorithm) { switch ($publicKeyAlgorithm) {
case 'id-RSASSA-PSS': case 'id-RSASSA-PSS':
$key = RSA::load($publicKey, 'PSS'); $key = RSA::loadFormat('PSS', $publicKey);
break; break;
case 'rsaEncryption': case 'rsaEncryption':
$key = RSA::load($publicKey, 'PKCS8'); $key = RSA::loadFormat('PKCS8', $publicKey);
switch ($signatureAlgorithm) { switch ($signatureAlgorithm) {
case 'md2WithRSAEncryption': case 'md2WithRSAEncryption':
case 'md5WithRSAEncryption': case 'md5WithRSAEncryption':
@ -1380,10 +1380,10 @@ class X509
break; break;
case 'id-Ed25519': case 'id-Ed25519':
case 'id-Ed448': case 'id-Ed448':
$key = EC::load($publicKey, 'PKCS8'); $key = EC::loadFormat('PKCS8', $publicKey);
break; break;
case 'id-ecPublicKey': case 'id-ecPublicKey':
$key = EC::load($publicKey, 'PKCS8'); $key = EC::loadFormat('PKCS8', $publicKey);
switch ($signatureAlgorithm) { switch ($signatureAlgorithm) {
case 'ecdsa-with-SHA1': case 'ecdsa-with-SHA1':
case 'ecdsa-with-SHA224': case 'ecdsa-with-SHA224':
@ -1398,7 +1398,7 @@ class X509
} }
break; break;
case 'id-dsa': case 'id-dsa':
$key = DSA::load($publicKey, 'PKCS8'); $key = DSA::loadFormat('PKCS8', $publicKey);
switch ($signatureAlgorithm) { switch ($signatureAlgorithm) {
case 'id-dsa-with-sha1': case 'id-dsa-with-sha1':
case 'id-dsa-with-sha224': case 'id-dsa-with-sha224':
@ -2089,13 +2089,13 @@ class X509
switch ($keyinfo['algorithm']['algorithm']) { switch ($keyinfo['algorithm']['algorithm']) {
case 'rsaEncryption': case 'rsaEncryption':
return RSA::load($key, 'PKCS8'); return RSA::loadFormat('PKCS8', $key);
case 'id-ecPublicKey': case 'id-ecPublicKey':
case 'id-Ed25519': case 'id-Ed25519':
case 'id-Ed448': case 'id-Ed448':
return EC::load($key, 'PKCS8'); return EC::loadFormat('PKCS8', $key);
case 'id-dsa': case 'id-dsa':
return DSA::load($key, 'PKCS8'); return DSA::loadFormat('PKCS8', $key);
} }
return false; return false;

View File

@ -4601,7 +4601,7 @@ class SSH2
case 'ecdsa-sha2-nistp256': case 'ecdsa-sha2-nistp256':
case 'ecdsa-sha2-nistp384': case 'ecdsa-sha2-nistp384':
case 'ecdsa-sha2-nistp521': case 'ecdsa-sha2-nistp521':
$key = EC::load($server_public_host_key, 'OpenSSH') $key = EC::loadFormat('OpenSSH', $server_public_host_key)
->withSignatureFormat('SSH2'); ->withSignatureFormat('SSH2');
switch ($this->signature_format) { switch ($this->signature_format) {
case 'ssh-ed25519': case 'ssh-ed25519':
@ -4620,7 +4620,7 @@ class SSH2
$key = $key->withHash($hash); $key = $key->withHash($hash);
break; break;
case 'ssh-dss': case 'ssh-dss':
$key = DSA::load($server_public_host_key, 'OpenSSH') $key = DSA::loadFormat('OpenSSH', $server_public_host_key)
->withSignatureFormat('SSH2') ->withSignatureFormat('SSH2')
->withHash('sha1'); ->withHash('sha1');
break; break;
@ -4634,7 +4634,7 @@ class SSH2
$temp = unpack('Nlength', Strings::shift($signature, 4)); $temp = unpack('Nlength', Strings::shift($signature, 4));
$signature = Strings::shift($signature, $temp['length']); $signature = Strings::shift($signature, $temp['length']);
$key = RSA::load($server_public_host_key, 'OpenSSH') $key = RSA::loadFormat('OpenSSH', $server_public_host_key)
->withPadding(RSA::SIGNATURE_PKCS1); ->withPadding(RSA::SIGNATURE_PKCS1);
switch ($this->signature_format) { switch ($this->signature_format) {
case 'rsa-sha2-512': case 'rsa-sha2-512':