From 3ecc62912d7241195749fc766780ed81ab377dbe Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 2 Dec 2018 07:17:17 -0600 Subject: [PATCH] throw exceptions when signing / verification is attempted w/o a key --- phpseclib/Crypt/DSA.php | 24 ++++++++++++++++++++---- phpseclib/Crypt/ECDSA.php | 12 ++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/phpseclib/Crypt/DSA.php b/phpseclib/Crypt/DSA.php index e26d4c73..b8a85903 100644 --- a/phpseclib/Crypt/DSA.php +++ b/phpseclib/Crypt/DSA.php @@ -36,6 +36,8 @@ use phpseclib\Math\BigInteger; use phpseclib\Crypt\Common\AsymmetricKey; use phpseclib\Math\PrimeField; use phpseclib\Crypt\ECDSA\Signature\ASN1 as ASN1Signature; +use phpseclib\Exception\UnsupportedOperationException; +use phpseclib\Exception\NoKeyLoadedException; /** * Pure-PHP FIPS 186-4 compliant implementation of DSA. @@ -455,8 +457,15 @@ class DSA extends AsymmetricKey return false; } - if (empty($this->x) || empty($this->p)) { - return false; + if (empty($this->x)) { + if (empty($this->y)) { + throw new NoKeyLoadedException('No key has been loaded'); + } + throw new UnsupportedOperationException('A public key cannot be used to sign data'); + } + + if (empty($this->p)) { + throw new \RuntimeException('DSA Prime P is not set'); } if (self::$engines['OpenSSL'] && in_array($this->hash->getHash(), openssl_get_md_methods())) { @@ -535,8 +544,15 @@ class DSA extends AsymmetricKey } extract($params); - if (empty($this->y) || empty($this->p)) { - return false; + if (empty($this->y)) { + if (empty($this->x)) { + throw new NoKeyLoadedException('No key has been loaded'); + } + throw new UnsupportedOperationException('A private key cannot be used to sign data'); + } + + if (empty($this->p)) { + throw new \RuntimeException('DSA Prime P is not set'); } if (self::$engines['OpenSSL'] && in_array($this->hash->getHash(), openssl_get_md_methods())) { diff --git a/phpseclib/Crypt/ECDSA.php b/phpseclib/Crypt/ECDSA.php index 7bd27347..0224cc70 100644 --- a/phpseclib/Crypt/ECDSA.php +++ b/phpseclib/Crypt/ECDSA.php @@ -33,6 +33,8 @@ namespace phpseclib\Crypt; use phpseclib\Math\BigInteger; use phpseclib\Crypt\Common\AsymmetricKey; use phpseclib\Exception\UnsupportedCurveException; +use phpseclib\Exception\UnsupportedOperationException; +use phpseclib\Exception\NoKeyLoadedException; use phpseclib\File\ASN1; use phpseclib\File\ASN1\Maps\ECParameters; use phpseclib\Crypt\ECDSA\BaseCurves\TwistedEdwards as TwistedEdwardsCurve; @@ -491,7 +493,10 @@ class ECDSA extends AsymmetricKey public function sign($message, $format = 'ASN1') { if (!isset($this->dA)) { - return false; + if (!isset($this->QA)) { + throw new NoKeyLoadedException('No key has been loaded'); + } + throw new UnsupportedOperationException('A public key cannot be used to sign data'); } $dA = $this->dA->toBigInteger(); @@ -630,7 +635,10 @@ class ECDSA extends AsymmetricKey public function verify($message, $signature, $format = 'ASN1') { if (!isset($this->QA)) { - return false; + if (!isset($this->dA)) { + throw new NoKeyLoadedException('No key has been loaded'); + } + throw new UnsupportedOperationException('A private key cannot be used to verify data'); } $order = $this->curve->getOrder();