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,16 +146,14 @@ abstract class AsymmetricKey
* Load the key
*
* @param string $key
* @param string $type
* @param string $password
* @return array|bool
* @param string $password optional
* @return AsymmetricKey
*/
protected static function load($key, $type, $password)
public static function load($key, $password = false)
{
self::initialize_static_variables();
$components = false;
if ($type === false) {
foreach (self::$plugins[static::ALGORITHM]['Keys'] as $format) {
try {
$components = $format::load($key, $password);
@ -166,13 +164,34 @@ abstract class AsymmetricKey
break;
}
}
} else {
if ($components === false) {
throw new NoKeyLoadedException('Unable to read key');
}
$components['format'] = $format;
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');
@ -180,7 +199,7 @@ abstract class AsymmetricKey
$components['format'] = $format;
return $components;
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
* @access public
* @param string $key
* @param string $type optional
* @param string $password optional
* @access protected
* @param array $components
*/
public static function load($key, $type = false, $password = false)
protected static function onLoad($components)
{
self::initialize_static_variables();
if (!isset(self::$engines['PHP'])) {
self::useBestEngine();
}
$components = parent::load($key, $type, $password);
if (!isset($components['x']) && !isset($components['y'])) {
$new = new Parameters;
} 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
* @access public
* @param string $key
* @param string $type optional
* @param string $password optional
* @access protected
* @param array $components
*/
public static function load($key, $type = false, $password = false)
protected static function onLoad($components)
{
self::initialize_static_variables();
if (!isset(self::$engines['PHP'])) {
self::useBestEngine();
}
$components = parent::load($key, $type, $password);
if (!isset($components['dA']) && !isset($components['QA'])) {
$new = new Parameters;
$new->curve = $components['curve'];

View File

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

View File

@ -387,22 +387,14 @@ abstract class RSA extends AsymmetricKey
}
/**
* Loads a public or private key
*
* Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed)
* OnLoad Handler
*
* @return bool
* @access public
* @param string $key
* @param string $type optional
* @param string $password optional
* @access protected
* @param array $components
*/
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'] ?
new PublicKey :
new PrivateKey;

View File

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

View File

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