1
0
mirror of https://github.com/danog/tgseclib.git synced 2024-11-30 04:39:02 +01:00

Merge pull request #581 from cnelissen/ReplaceGetClassFix

Replaced get_class() calls with instanceof operators

* cnelissen/ReplaceGetClassFix:
  Revert changes for abstract methods in Base class
  Replaced get_class() calls with instanceof operators
This commit is contained in:
Andreas Fischer 2015-01-03 17:22:39 +01:00
commit 8fae19a024
8 changed files with 50 additions and 64 deletions

View File

@ -1172,7 +1172,7 @@ class Base
*/
function _encryptBlock($in)
{
user_error((version_compare(PHP_VERSION, '5.0.0', '>=') ? __METHOD__ : __FUNCTION__) . '() must extend by class ' . get_class($this), E_USER_ERROR);
user_error(__METHOD__ . '() must extend by class ' . get_class($this), E_USER_ERROR);
}
/**
@ -1186,7 +1186,7 @@ class Base
*/
function _decryptBlock($in)
{
user_error((version_compare(PHP_VERSION, '5.0.0', '>=') ? __METHOD__ : __FUNCTION__) . '() must extend by class ' . get_class($this), E_USER_ERROR);
user_error(__METHOD__ . '() must extend by class ' . get_class($this), E_USER_ERROR);
}
/**
@ -1201,7 +1201,7 @@ class Base
*/
function _setupKey()
{
user_error((version_compare(PHP_VERSION, '5.0.0', '>=') ? __METHOD__ : __FUNCTION__) . '() must extend by class ' . get_class($this), E_USER_ERROR);
user_error(__METHOD__ . '() must extend by class ' . get_class($this), E_USER_ERROR);
}
/**

View File

@ -1454,7 +1454,7 @@ class RSA
*/
function loadKey($key, $type = false)
{
if (is_object($key) && get_class($key) == 'phpseclib\Crypt\RSA') {
if ($key instanceof RSA) {
$this->privateKeyFormat = $key->privateKeyFormat;
$this->publicKeyFormat = $key->publicKeyFormat;
$this->k = $key->k;

View File

@ -206,7 +206,7 @@ class ASN1
*/
function decodeBER($encoded)
{
if (is_object($encoded) && get_class($encoded) === 'phpseclib\File\ASN1\Element') {
if ($encoded instanceof Element) {
$encoded = $encoded->element;
}
@ -779,7 +779,7 @@ class ASN1
*/
function _encode_der($source, $mapping, $idx = null, $special = array())
{
if (is_object($source) && get_class($source) === 'phpseclib\File\ASN1\Element') {
if ($source instanceof Element) {
return $source->element;
}
@ -997,7 +997,7 @@ class ASN1
case !isset($source):
return $this->_encode_der(null, array('type' => self::TYPE_NULL) + $mapping, null, $special);
case is_int($source):
case $source instanceof \phpseclib\Math\BigInteger:
case $source instanceof BigInteger:
return $this->_encode_der($source, array('type' => self::TYPE_INTEGER) + $mapping, null, $special);
case is_float($source):
return $this->_encode_der($source, array('type' => self::TYPE_REAL) + $mapping, null, $special);

View File

@ -3601,25 +3601,24 @@ class X509
*/
function _sign($key, $signatureAlgorithm)
{
switch (get_class($key)) {
case 'phpseclib\Crypt\RSA':
switch ($signatureAlgorithm) {
case 'md2WithRSAEncryption':
case 'md5WithRSAEncryption':
case 'sha1WithRSAEncryption':
case 'sha224WithRSAEncryption':
case 'sha256WithRSAEncryption':
case 'sha384WithRSAEncryption':
case 'sha512WithRSAEncryption':
$key->setHash(preg_replace('#WithRSAEncryption$#', '', $signatureAlgorithm));
$key->setSignatureMode(RSA::SIGNATURE_PKCS1);
if ($key instanceof RSA) {
switch ($signatureAlgorithm) {
case 'md2WithRSAEncryption':
case 'md5WithRSAEncryption':
case 'sha1WithRSAEncryption':
case 'sha224WithRSAEncryption':
case 'sha256WithRSAEncryption':
case 'sha384WithRSAEncryption':
case 'sha512WithRSAEncryption':
$key->setHash(preg_replace('#WithRSAEncryption$#', '', $signatureAlgorithm));
$key->setSignatureMode(RSA::SIGNATURE_PKCS1);
$this->currentCert['signature'] = base64_encode("\0" . $key->sign($this->signatureSubject));
return $this->currentCert;
}
default:
return false;
$this->currentCert['signature'] = base64_encode("\0" . $key->sign($this->signatureSubject));
return $this->currentCert;
}
}
return false;
}
/**
@ -4162,7 +4161,7 @@ class X509
return $this->computeKeyIdentifier($key['certificationRequestInfo']['subjectPKInfo']['subjectPublicKey'], $method);
case !is_object($key):
return false;
case get_class($key) === 'phpseclib\File\ASN1\Element':
case $key instanceof Element:
// Assume the element is a bitstring-packed key.
$asn1 = new ASN1();
$decoded = $asn1->decodeBER($key->element);
@ -4184,7 +4183,7 @@ class X509
}
$key = $raw; // Is a public key.
break;
case get_class($key) === 'phpseclib\File\X509':
case $key instanceof X509:
if (isset($key->publicKey)) {
return $this->computeKeyIdentifier($key->publicKey, $method);
}
@ -4223,22 +4222,17 @@ class X509
*/
function _formatSubjectPublicKey()
{
if (!isset($this->publicKey) || !is_object($this->publicKey)) {
return false;
if ($this->publicKey instanceof RSA) {
// the following two return statements do the same thing. i dunno.. i just prefer the later for some reason.
// the former is a good example of how to do fuzzing on the public key
//return new Element(base64_decode(preg_replace('#-.+-|[\r\n]#', '', $this->publicKey->getPublicKey())));
return array(
'algorithm' => array('algorithm' => 'rsaEncryption'),
'subjectPublicKey' => $this->publicKey->getPublicKey(RSA::PUBLIC_FORMAT_PKCS1)
);
}
switch (get_class($this->publicKey)) {
case 'phpseclib\Crypt\RSA':
// the following two return statements do the same thing. i dunno.. i just prefer the later for some reason.
// the former is a good example of how to do fuzzing on the public key
//return new Element(base64_decode(preg_replace('#-.+-|[\r\n]#', '', $this->publicKey->getPublicKey())));
return array(
'algorithm' => array('algorithm' => 'rsaEncryption'),
'subjectPublicKey' => $this->publicKey->getPublicKey(RSA::PUBLIC_FORMAT_PKCS1)
);
default:
return false;
}
return false;
}
/**

View File

@ -332,7 +332,7 @@ class BigInteger
switch (true) {
case is_resource($x) && get_resource_type($x) == 'GMP integer':
// PHP 5.6 switched GMP from using resources to objects
case is_object($x) && get_class($x) == 'GMP':
case $x instanceof \GMP:
$this->value = $x;
return;
}

View File

@ -110,22 +110,15 @@ class SCP
*/
function __construct($ssh)
{
if (!is_object($ssh)) {
if ($ssh instanceof SSH2) {
$this->mode = self::MODE_SSH2;
} elseif ($ssh instanceof SSH1) {
$this->packet_size = 50000;
$this->mode = self::MODE_SSH1;
} else {
return;
}
switch (get_class($ssh)) {
case 'phpseclib\Net\SSH2':
$this->mode = self::MODE_SSH2;
break;
case 'phpseclib\Net\SSH1':
$this->packet_size = 50000;
$this->mode = self::MODE_SSH1;
break;
default:
return;
}
$this->ssh = $ssh;
}

View File

@ -17,6 +17,7 @@
namespace phpseclib\Net\SFTP;
use phpseclib\Crypt\RSA;
use phpseclib\Net\SFTP;
/**
@ -168,7 +169,7 @@ class Stream
if ($host[0] == '$') {
$host = substr($host, 1);
global $$host;
if (!is_object($$host) || get_class($$host) != 'phpseclib\Net\SFTP') {
if (($$host instanceof SFTP) === false) {
return false;
}
$this->sftp = $$host;
@ -182,7 +183,7 @@ class Stream
if (isset($context[$scheme]['sftp'])) {
$sftp = $context[$scheme]['sftp'];
}
if (isset($sftp) && is_object($sftp) && get_class($sftp) == 'phpseclib\Net\SFTP') {
if (isset($sftp) && $sftp instanceof SFTP) {
$this->sftp = $sftp;
return $path;
}
@ -192,7 +193,7 @@ class Stream
if (isset($context[$scheme]['password'])) {
$pass = $context[$scheme]['password'];
}
if (isset($context[$scheme]['privkey']) && is_object($context[$scheme]['privkey']) && get_class($context[$scheme]['privkey']) == 'phpseclib\Crypt\RSA') {
if (isset($context[$scheme]['privkey']) && $context[$scheme]['privkey'] instanceof RSA) {
$pass = $context[$scheme]['privkey'];
}

View File

@ -59,6 +59,7 @@ use phpseclib\Crypt\RSA;
use phpseclib\Crypt\TripleDES;
use phpseclib\Crypt\Twofish;
use phpseclib\Math\BigInteger; // Used to do Diffie-Hellman key exchange and DSA/RSA signature verification.
use phpseclib\System\SSH\Agent;
/**
* Pure-PHP implementation of SSHv2.
@ -1805,13 +1806,10 @@ class SSH2
return !is_string($password) && !is_array($password) ? false : $this->_keyboard_interactive_process($password);
}
if (is_object($password)) {
switch (get_class($password)) {
case 'phpseclib\Crypt\RSA':
return $this->_privatekey_login($username, $password);
case 'phpseclib\System\SSH\Agent':
return $this->_ssh_agent_login($username, $password);
}
if ($password instanceof RSA) {
return $this->_privatekey_login($username, $password);
} elseif ($password instanceof Agent) {
return $this->_ssh_agent_login($username, $password);
}
if (is_array($password)) {