throw exceptions when signing / verification is attempted w/o a key

This commit is contained in:
terrafrost 2018-12-02 07:17:17 -06:00
parent 2c1994805b
commit 3ecc62912d
2 changed files with 30 additions and 6 deletions

View File

@ -36,6 +36,8 @@ use phpseclib\Math\BigInteger;
use phpseclib\Crypt\Common\AsymmetricKey; use phpseclib\Crypt\Common\AsymmetricKey;
use phpseclib\Math\PrimeField; use phpseclib\Math\PrimeField;
use phpseclib\Crypt\ECDSA\Signature\ASN1 as ASN1Signature; 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. * Pure-PHP FIPS 186-4 compliant implementation of DSA.
@ -455,8 +457,15 @@ class DSA extends AsymmetricKey
return false; return false;
} }
if (empty($this->x) || empty($this->p)) { if (empty($this->x)) {
return false; 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())) { if (self::$engines['OpenSSL'] && in_array($this->hash->getHash(), openssl_get_md_methods())) {
@ -535,8 +544,15 @@ class DSA extends AsymmetricKey
} }
extract($params); extract($params);
if (empty($this->y) || empty($this->p)) { if (empty($this->y)) {
return false; 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())) { if (self::$engines['OpenSSL'] && in_array($this->hash->getHash(), openssl_get_md_methods())) {

View File

@ -33,6 +33,8 @@ namespace phpseclib\Crypt;
use phpseclib\Math\BigInteger; use phpseclib\Math\BigInteger;
use phpseclib\Crypt\Common\AsymmetricKey; use phpseclib\Crypt\Common\AsymmetricKey;
use phpseclib\Exception\UnsupportedCurveException; use phpseclib\Exception\UnsupportedCurveException;
use phpseclib\Exception\UnsupportedOperationException;
use phpseclib\Exception\NoKeyLoadedException;
use phpseclib\File\ASN1; use phpseclib\File\ASN1;
use phpseclib\File\ASN1\Maps\ECParameters; use phpseclib\File\ASN1\Maps\ECParameters;
use phpseclib\Crypt\ECDSA\BaseCurves\TwistedEdwards as TwistedEdwardsCurve; use phpseclib\Crypt\ECDSA\BaseCurves\TwistedEdwards as TwistedEdwardsCurve;
@ -491,7 +493,10 @@ class ECDSA extends AsymmetricKey
public function sign($message, $format = 'ASN1') public function sign($message, $format = 'ASN1')
{ {
if (!isset($this->dA)) { 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(); $dA = $this->dA->toBigInteger();
@ -630,7 +635,10 @@ class ECDSA extends AsymmetricKey
public function verify($message, $signature, $format = 'ASN1') public function verify($message, $signature, $format = 'ASN1')
{ {
if (!isset($this->QA)) { 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(); $order = $this->curve->getOrder();