allow strinable objects to be loaded instead of just strings

This commit is contained in:
terrafrost 2020-04-13 07:58:00 -05:00
parent 94d3403ed3
commit 0f8486cc87
15 changed files with 38 additions and 15 deletions

View File

@ -175,7 +175,7 @@ abstract class Strings
$result.= pack('N', $element);
break;
case 's':
if (!is_string($element)) {
if (!self::is_stringable($element)) {
throw new \InvalidArgumentException('A string was expected.');
}
$result.= pack('Na*', strlen($element), $element);
@ -372,4 +372,16 @@ abstract class Strings
return $var;
}
/**
* Find whether the type of a variable is string (or could be converted to one)
*
* @param string|object $var
* @return boolean
* @access public
*/
public static function is_stringable($var)
{
return is_string($var) || (is_object($var) && method_exists($var, '__toString'));
}
}

View File

@ -70,7 +70,7 @@ abstract class OpenSSH
*/
public static function load($key, $password = '')
{
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -22,6 +22,7 @@ use phpseclib3\Crypt\AES;
use phpseclib3\Crypt\DES;
use phpseclib3\Crypt\TripleDES;
use phpseclib3\File\ASN1;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Exception\UnsupportedAlgorithmException;
/**
@ -127,7 +128,7 @@ abstract class PKCS1 extends PKCS
*/
protected static function load($key, $password)
{
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -37,6 +37,7 @@ use phpseclib3\Crypt\Random;
use phpseclib3\Math\BigInteger;
use phpseclib3\File\ASN1;
use phpseclib3\File\ASN1\Maps;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Exception\UnsupportedAlgorithmException;
/**
@ -333,7 +334,7 @@ abstract class PKCS8 extends PKCS
{
self::initialize_static_variables();
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -80,7 +80,7 @@ abstract class PuTTY
*/
public static function load($key, $password)
{
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -25,6 +25,7 @@ use phpseclib3\Math\BigInteger;
use phpseclib3\Crypt\Common\Formats\Keys\PKCS8 as Progenitor;
use phpseclib3\File\ASN1;
use phpseclib3\File\ASN1\Maps;
use phpseclib3\Common\Functions\Strings;
/**
* PKCS#8 Formatted DH Key Handler
@ -69,7 +70,7 @@ abstract class PKCS8 extends Progenitor
*/
public static function load($key, $password = '')
{
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -29,6 +29,7 @@ use phpseclib3\Math\BigInteger;
use phpseclib3\Crypt\Common\Formats\Keys\PKCS8 as Progenitor;
use phpseclib3\File\ASN1;
use phpseclib3\File\ASN1\Maps;
use phpseclib3\Common\Functions\Strings;
/**
* PKCS#8 Formatted DSA Key Handler
@ -73,7 +74,7 @@ abstract class PKCS8 extends Progenitor
*/
public static function load($key, $password = '')
{
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -23,6 +23,7 @@ namespace phpseclib3\Crypt\DSA\Formats\Keys;
use ParagonIE\ConstantTime\Base64;
use phpseclib3\Math\BigInteger;
use phpseclib3\Common\Functions\Strings;
/**
* XML Formatted DSA Key Handler
@ -43,7 +44,7 @@ abstract class XML
*/
public static function load($key, $password = '')
{
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -36,6 +36,7 @@ use phpseclib3\Math\Common\FiniteField\Integer;
use phpseclib3\Crypt\EC\Curves\Ed25519;
use phpseclib3\Crypt\EC\Curves\Ed448;
use phpseclib3\Exception\UnsupportedCurveException;
use phpseclib3\Common\Functions\Strings;
/**
* PKCS#8 Formatted EC Key Handler
@ -81,7 +82,7 @@ abstract class PKCS8 extends Progenitor
// one that's called
self::initialize_static_variables();
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -26,6 +26,7 @@ use phpseclib3\Crypt\EC\BaseCurves\Base as BaseCurve;
use phpseclib3\Crypt\EC\BaseCurves\Prime as PrimeCurve;
use phpseclib3\Crypt\EC\BaseCurves\TwistedEdwards as TwistedEdwardsCurve;
use phpseclib3\Crypt\EC\BaseCurves\Montgomery as MontgomeryCurve;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Exception\UnsupportedCurveException;
/**
@ -65,7 +66,7 @@ abstract class XML
{
self::initialize_static_variables();
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -76,7 +76,7 @@ abstract class MSBLOB
*/
public static function load($key, $password = '')
{
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -28,6 +28,7 @@ use phpseclib3\Math\BigInteger;
use phpseclib3\Crypt\Common\Formats\Keys\PKCS1 as Progenitor;
use phpseclib3\File\ASN1;
use phpseclib3\File\ASN1\Maps;
use phpseclib3\Common\Functions\Strings;
/**
* PKCS#1 Formatted RSA Key Handler
@ -48,7 +49,7 @@ abstract class PKCS1 extends Progenitor
*/
public static function load($key, $password = '')
{
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -30,6 +30,7 @@ namespace phpseclib3\Crypt\RSA\Formats\Keys;
use phpseclib3\Math\BigInteger;
use phpseclib3\Crypt\Common\Formats\Keys\PKCS8 as Progenitor;
use phpseclib3\File\ASN1;
use phpseclib3\Common\Functions\Strings;
/**
* PKCS#8 Formatted RSA Key Handler
@ -74,7 +75,7 @@ abstract class PKCS8 extends Progenitor
*/
public static function load($key, $password = '')
{
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -29,6 +29,7 @@ use phpseclib3\Math\BigInteger;
use phpseclib3\Crypt\Common\Formats\Keys\PKCS8 as Progenitor;
use phpseclib3\File\ASN1;
use phpseclib3\File\ASN1\Maps;
use phpseclib3\Common\Functions\Strings;
/**
* PKCS#8 Formatted RSA-PSS Key Handler
@ -107,7 +108,7 @@ abstract class PSS extends Progenitor
{
self::initialize_static_variables();
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}

View File

@ -24,6 +24,7 @@ namespace phpseclib3\Crypt\RSA\Formats\Keys;
use ParagonIE\ConstantTime\Base64;
use phpseclib3\Math\BigInteger;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Exception\UnsupportedFormatException;
/**
@ -45,7 +46,7 @@ abstract class XML
*/
public static function load($key, $password = '')
{
if (!is_string($key)) {
if (!Strings::is_stringable($key)) {
throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
}