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

updates to Exceptions

This commit is contained in:
terrafrost 2018-12-31 09:06:12 -06:00
parent c6f9807633
commit 17e6938fba
15 changed files with 170 additions and 52 deletions

View File

@ -40,6 +40,11 @@ use phpseclib\Crypt\Hash;
use phpseclib\Common\Functions\Strings;
use phpseclib\Math\BigInteger;
use phpseclib\Math\BinaryField;
use phpseclib\Exception\BadDecryptionException;
use phpseclib\Exception\BadModeException;
use phpseclib\Exception\InconsistentSetupException;
use phpseclib\Exception\InsufficientSetupException;
use phpseclib\Exception\UnsupportedAlgorithmException;
/**
* Base Class for all \phpseclib\Crypt\* cipher classes
@ -580,7 +585,7 @@ abstract class SymmetricKey
*
* @param string $mode
* @access public
* @throws \InvalidArgumentException if an invalid / unsupported mode is provided
* @throws BadModeException if an invalid / unsupported mode is provided
*/
public function __construct($mode)
{
@ -588,7 +593,7 @@ abstract class SymmetricKey
// necessary because of 5.6 compatibility; we can't do isset(self::MODE_MAP[$mode]) in 5.6
$map = self::MODE_MAP;
if (!isset($map[$mode])) {
throw new \InvalidArgumentException('No valid mode has been specified');
throw new BadModeException('No valid mode has been specified');
}
$mode = self::MODE_MAP[$mode];
@ -608,7 +613,7 @@ abstract class SymmetricKey
break;
case self::MODE_GCM:
if ($this->block_size != 16) {
throw new \InvalidArgumentException('GCM is only valid for block ciphers with a block size of 128 bits');
throw new BadModeException('GCM is only valid for block ciphers with a block size of 128 bits');
}
if (!isset(self::$gcmField)) {
self::$gcmField = new BinaryField(128, 7, 2, 1, 0);
@ -616,7 +621,7 @@ abstract class SymmetricKey
$this->paddable = false;
break;
default:
throw new \InvalidArgumentException('No valid mode has been specified');
throw new BadModeException('No valid mode has been specified');
}
$this->mode = $mode;
@ -630,21 +635,21 @@ abstract class SymmetricKey
* @access public
* @param string $iv
* @throws \LengthException if the IV length isn't equal to the block size
* @throws \InvalidArgumentException if an IV is provided when one shouldn't be
* @throws \BadMethodCallException if an IV is provided when one shouldn't be
* @internal Can be overwritten by a sub class, but does not have to be
*/
public function setIV($iv)
{
if ($this->mode == self::MODE_ECB) {
throw new \InvalidArgumentException('This mode does not require an IV.');
throw new \BadMethodCallException('This mode does not require an IV.');
}
if ($this->mode == self::MODE_GCM) {
throw new \InvalidArgumentException('Use setNonce instead');
throw new \BadMethodCallException('Use setNonce instead');
}
if (!$this->usesIV()) {
throw new \InvalidArgumentException('This algorithm does not use an IV.');
throw new \BadMethodCallExceptionn('This algorithm does not use an IV.');
}
if (strlen($iv) != $this->block_size) {
@ -667,7 +672,7 @@ abstract class SymmetricKey
public function setNonce($nonce)
{
if ($this->mode != self::MODE_GCM) {
throw new \RuntimeException('Nonces are only used in GCM mode.');
throw new \BadMethodCallException('Nonces are only used in GCM mode.');
}
$this->nonce = $nonce;
@ -762,7 +767,7 @@ abstract class SymmetricKey
if (is_string($this->key) && strlen($this->key) != $this->explicit_key_length) {
$this->key = false;
throw new \LengthException('Key has already been set and is not ' .$this->explicit_key_length . ' bytes long');
throw new InconsistentSetupException('Key has already been set and is not ' .$this->explicit_key_length . ' bytes long');
}
}
@ -783,7 +788,7 @@ abstract class SymmetricKey
public function setKey($key)
{
if ($this->explicit_key_length !== false && strlen($key) != $this->explicit_key_length) {
throw new \LengthException('Key length has already been set to ' . $this->explicit_key_length . ' bytes and this key is ' . strlen($key) . ' bytes');
throw new InconsistentSetupException('Key length has already been set to ' . $this->explicit_key_length . ' bytes and this key is ' . strlen($key) . ' bytes');
}
$this->key = $key;
@ -932,7 +937,7 @@ abstract class SymmetricKey
}
break;
default:
throw new \InvalidArgumentException($method . ' is not a supported password hashing method');
throw new UnsupportedAlgorithmException($method . ' is not a supported password hashing method');
}
$this->setKey($key);
@ -1359,7 +1364,7 @@ abstract class SymmetricKey
if ($this->mode == self::MODE_GCM) {
if ($this->oldtag === false) {
throw new \UnexpectedValueException('Authentication Tag has not been set');
throw new InsufficientSetupException('Authentication Tag has not been set');
}
$oldIV = $this->iv;
@ -1379,7 +1384,7 @@ abstract class SymmetricKey
$newtag = $cipher->encrypt($s);
if ($this->oldtag != substr($newtag, 0, strlen($newtag))) {
$this->oldtag = false;
throw new \UnexpectedValueException('Derived authentication tag and supplied authentication tag do not match');
throw new BadDecryptionException('Derived authentication tag and supplied authentication tag do not match');
}
$this->oldtag = false;
return $plaintext;
@ -1679,7 +1684,7 @@ abstract class SymmetricKey
public function getTag($length = 16)
{
if ($this->mode != self::MODE_GCM) {
throw new \RuntimeException('Only GCM mode utilizes authentication tags');
throw new \BadMethodCallException('Only GCM mode utilizes authentication tags');
}
// the tag is basically a single encrypted block of a 128-bit cipher. it can't be greater than 16
@ -1710,7 +1715,7 @@ abstract class SymmetricKey
public function setTag($tag)
{
if ($this->mode != self::MODE_GCM) {
throw new \RuntimeException('Only GCM mode utilizes authentication tags');
throw new \BadMethodCallException('Only GCM mode utilizes authentication tags');
}
$length = strlen($tag);
@ -1981,7 +1986,7 @@ abstract class SymmetricKey
}
if ($this->mode == self::MODE_GCM) {
throw new \RuntimeException('This mode does not run in continuous mode');
throw new \BadMethodCallException('This mode does not run in continuous mode');
}
$this->continuousBuffer = true;
@ -2246,7 +2251,7 @@ abstract class SymmetricKey
if ($this->mode == self::MODE_GCM) {
if ($this->nonce === false) {
throw new \UnexpectedValueException('No nonce has been defined');
throw new InsufficientSetupException('No nonce has been defined');
}
if (!in_array($this->engine, [self::ENGINE_LIBSODIUM, self::ENGINE_OPENSSL_GCM])) {
$this->setupGCM();
@ -2257,12 +2262,12 @@ abstract class SymmetricKey
if ($this->iv === false && !in_array($this->mode, [self::MODE_STREAM, self::MODE_ECB])) {
if ($this->mode != self::MODE_GCM || !in_array($this->engine, [self::ENGINE_LIBSODIUM, self::ENGINE_OPENSSL_GCM])) {
throw new \UnexpectedValueException('No IV has been defined');
throw new InsufficientSetupException('No IV has been defined');
}
}
if ($this->key === false) {
throw new \UnexpectedValueException('No key has been defined');
throw new InsufficientSetupException('No key has been defined');
}
$this->encryptIV = $this->decryptIV = $this->iv;
@ -2360,7 +2365,7 @@ abstract class SymmetricKey
$length = ord($text[strlen($text) - 1]);
if (!$length || $length > $this->block_size) {
throw new \LengthException("The ciphertext has an invalid padding length ($length) compared to the block size ({$this->block_size})");
throw new BadDecryptionException("The ciphertext has an invalid padding length ($length) compared to the block size ({$this->block_size})");
}
return substr($text, 0, -$length);

View File

@ -43,6 +43,7 @@
namespace phpseclib\Crypt;
use phpseclib\Crypt\Common\BlockCipher;
use phpseclib\Exception\BadModeException;
/**
* Pure-PHP implementation of DES.
@ -585,12 +586,12 @@ class DES extends BlockCipher
*
* @param int $mode
* @access public
* @throws \InvalidArgumentException if an invalid / unsupported mode is provided
* @throws BadModeException if an invalid / unsupported mode is provided
*/
public function __construct($mode)
{
if ($mode == self::MODE_STREAM) {
throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode');
throw new BadModeException('Block ciphers cannot be ran in stream mode');
}
parent::__construct($mode);

View File

@ -38,6 +38,7 @@ use phpseclib\Math\PrimeField;
use phpseclib\Crypt\ECDSA\Signature\ASN1 as ASN1Signature;
use phpseclib\Exception\UnsupportedOperationException;
use phpseclib\Exception\NoKeyLoadedException;
use phpseclib\Exception\InsufficientSetupException;
/**
* Pure-PHP FIPS 186-4 compliant implementation of DSA.
@ -200,7 +201,7 @@ class DSA extends AsymmetricKey
} else if (!count($args)) {
$private = self::createParameters();
} else {
throw new \InvalidArgumentException('Valid parameters are either two integers (L and N), a single DSA object or no parameters at all.');
throw new InsufficientSetupException('Valid parameters are either two integers (L and N), a single DSA object or no parameters at all.');
}
$private->x = BigInteger::randomRange(self::$one, $private->q->subtract(self::$one));
@ -465,7 +466,7 @@ class DSA extends AsymmetricKey
}
if (empty($this->p)) {
throw new \RuntimeException('DSA Prime P is not set');
throw new InsufficientSetupException('DSA Prime P is not set');
}
if (self::$engines['OpenSSL'] && in_array($this->hash->getHash(), openssl_get_md_methods())) {
@ -552,7 +553,7 @@ class DSA extends AsymmetricKey
}
if (empty($this->p)) {
throw new \RuntimeException('DSA Prime P is not set');
throw new InsufficientSetupException('DSA Prime P is not set');
}
if (self::$engines['OpenSSL'] && in_array($this->hash->getHash(), openssl_get_md_methods())) {

View File

@ -34,7 +34,9 @@ use phpseclib\Math\BigInteger;
use phpseclib\Crypt\Common\AsymmetricKey;
use phpseclib\Exception\UnsupportedCurveException;
use phpseclib\Exception\UnsupportedOperationException;
use phpseclib\Exception\UnsupportedAlgorithmException;
use phpseclib\Exception\NoKeyLoadedException;
use phpseclib\Exception\InsufficientSetupException;
use phpseclib\File\ASN1;
use phpseclib\File\ASN1\Maps\ECParameters;
use phpseclib\Crypt\ECDSA\BaseCurves\TwistedEdwards as TwistedEdwardsCurve;
@ -216,11 +218,11 @@ class ECDSA extends AsymmetricKey
if ($components['curve'] instanceof Ed25519 && $this->hashManuallySet && $this->hash->getHash() != 'sha512') {
$this->clearKey();
throw new \RuntimeException('Ed25519 only supports sha512 as a hash');
throw new UnsupportedAlgorithmException('Ed25519 only supports sha512 as a hash');
}
if ($components['curve'] instanceof Ed448 && $this->hashManuallySet && $this->hash->getHash() != 'shake256-912') {
$this->clearKey();
throw new \RuntimeException('Ed448 only supports shake256 with a length of 114 bytes');
throw new UnsupportedAlgorithmException('Ed448 only supports shake256 with a length of 114 bytes');
}
$this->curve = $components['curve'];
@ -426,7 +428,7 @@ class ECDSA extends AsymmetricKey
public function getEngine()
{
if (!isset($this->curve)) {
throw new \RuntimeException('getEngine should not be called until after a key has been loaded');
throw new InsufficientSetupException('getEngine should not be called until after a key has been loaded');
}
if ($this->curve instanceof TwistedEdwardsCurve) {
@ -455,10 +457,10 @@ class ECDSA extends AsymmetricKey
return;
}
if (!is_string($context)) {
throw new \RuntimeException('setContext expects a string');
throw new \InvalidArgumentException('setContext expects a string');
}
if (strlen($context) > 255) {
throw new \RuntimeException('The context is supposed to be, at most, 255 bytes long');
throw new \LengthException('The context is supposed to be, at most, 255 bytes long');
}
$this->context = $context;
}
@ -472,10 +474,10 @@ class ECDSA extends AsymmetricKey
public function setHash($hash)
{
if ($this->curve instanceof Ed25519 && $this->hash != 'sha512') {
throw new \RuntimeException('Ed25519 only supports sha512 as a hash');
throw new UnsupportedAlgorithmException('Ed25519 only supports sha512 as a hash');
}
if ($this->curve instanceof Ed448 && $this->hash != 'shake256-912') {
throw new \RuntimeException('Ed448 only supports shake256 with a length of 114 bytes');
throw new UnsupportedAlgorithmException('Ed448 only supports shake256 with a length of 114 bytes');
}
parent::setHash($hash);

View File

@ -36,6 +36,7 @@
namespace phpseclib\Crypt;
use phpseclib\Crypt\Common\BlockCipher;
use phpseclib\Exception\BadModeException;
/**
* Pure-PHP implementation of RC2.
@ -271,7 +272,7 @@ class RC2 extends BlockCipher
public function __construct($mode)
{
if ($mode == self::MODE_STREAM) {
throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode');
throw new BadModeException('Block ciphers cannot be ran in stream mode');
}
parent::__construct($mode);

View File

@ -1139,7 +1139,7 @@ class RSA extends AsymmetricKey
* @access private
* @param string $m
* @param string $l
* @throws \OutOfBoundsException if strlen($m) > $this->k - 2 * $this->hLen - 2
* @throws \LengthException if strlen($m) > $this->k - 2 * $this->hLen - 2
* @return string
*/
private function rsaes_oaep_encrypt($m, $l = '')
@ -1152,7 +1152,7 @@ class RSA extends AsymmetricKey
// be output.
if ($mLen > $this->k - 2 * $this->hLen - 2) {
throw new \OutOfBoundsException('Message too long');
throw new \LengthException('Message too long');
}
// EME-OAEP encoding
@ -1257,12 +1257,12 @@ class RSA extends AsymmetricKey
* @access private
* @param string $m
* @return bool|string
* @throws \OutOfBoundsException if strlen($m) > $this->k
* @throws \LengthException if strlen($m) > $this->k
*/
private function raw_encrypt($m)
{
if (strlen($m) > $this->k) {
throw new \OutOfBoundsException('Message too long');
throw new \LengthException('Message too long');
}
$temp = $this->os2ip($m);
@ -1278,7 +1278,7 @@ class RSA extends AsymmetricKey
* @access private
* @param string $m
* @param bool $pkcs15_compat optional
* @throws \OutOfBoundsException if strlen($m) > $this->k - 11
* @throws \LengthException if strlen($m) > $this->k - 11
* @return bool|string
*/
private function rsaes_pkcs1_v1_5_encrypt($m, $pkcs15_compat = false)
@ -1288,7 +1288,7 @@ class RSA extends AsymmetricKey
// Length checking
if ($mLen > $this->k - 11) {
throw new \OutOfBoundsException('Message too long');
throw new \LengthException('Message too long');
}
// EME-PKCS1-v1_5 encoding

View File

@ -57,6 +57,9 @@ namespace phpseclib\Crypt;
use phpseclib\Crypt\Common\BlockCipher;
use phpseclib\Common\Functions\Strings;
use phpseclib\Exception\BadModeException;
use phpseclib\Exception\InsufficientSetupException;
use phpseclib\Exception\BadDecryptionException;
/**
* Pure-PHP implementation of Rijndael.
@ -172,7 +175,7 @@ class Rijndael extends BlockCipher
public function __construct($mode)
{
if ($mode == self::MODE_STREAM) {
throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode');
throw new BadModeException('Block ciphers cannot be ran in stream mode');
}
parent::__construct($mode);
@ -980,7 +983,7 @@ class Rijndael extends BlockCipher
switch ($this->engine) {
case self::ENGINE_LIBSODIUM:
if ($this->oldtag === false) {
throw new \UnexpectedValueException('Authentication Tag has not been set');
throw new InsufficientSetupException('Authentication Tag has not been set');
}
if (strlen($this->oldtag) != 16) {
break;
@ -988,12 +991,12 @@ class Rijndael extends BlockCipher
$plaintext = sodium_crypto_aead_aes256gcm_decrypt($ciphertext . $this->oldtag, $this->aad, $this->nonce, $this->key);
if ($plaintext === false) {
$this->oldtag = false;
throw new \UnexpectedValueException('Error decrypting ciphertext with libsodium');
throw new BadDecryptionException('Error decrypting ciphertext with libsodium');
}
return $plaintext;
case self::ENGINE_OPENSSL_GCM:
if ($this->oldtag === false) {
throw new \UnexpectedValueException('Authentication Tag has not been set');
throw new InsufficientSetupException('Authentication Tag has not been set');
}
$plaintext = openssl_decrypt(
$ciphertext,
@ -1006,7 +1009,7 @@ class Rijndael extends BlockCipher
);
if ($plaintext === false) {
$this->oldtag = false;
throw new \UnexpectedValueException('Error decrypting ciphertext with OpenSSL');
throw new BadDecryptionException('Error decrypting ciphertext with OpenSSL');
}
return $plaintext;
}

View File

@ -38,6 +38,7 @@
namespace phpseclib\Crypt;
use phpseclib\Crypt\Common\BlockCipher;
use phpseclib\Exception\BadModeException;
/**
* Pure-PHP implementation of Twofish.
@ -375,12 +376,12 @@ class Twofish extends BlockCipher
*
* @param int $mode
* @access public
* @throws \InvalidArgumentException if an invalid / unsupported mode is provided
* @throws BadModeException if an invalid / unsupported mode is provided
*/
public function __construct($mode)
{
if ($mode == self::MODE_STREAM) {
throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode');
throw new BadModeException('Block ciphers cannot be ran in stream mode');
}
parent::__construct($mode);

View File

@ -0,0 +1,26 @@
<?php
/**
* BadDecryptionException
*
* PHP version 5
*
* @category Exception
* @package BadDecryptionException
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
namespace phpseclib\Exception;
/**
* BadDecryptionException
*
* @package BadDecryptionException
* @author Jim Wigginton <terrafrost@php.net>
*/
class BadDecryptionException extends \RuntimeException
{
}

View File

@ -0,0 +1,26 @@
<?php
/**
* BadModeException
*
* PHP version 5
*
* @category Exception
* @package BadModeException
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
namespace phpseclib\Exception;
/**
* BadModeException
*
* @package BadModeException
* @author Jim Wigginton <terrafrost@php.net>
*/
class BadModeException extends \RuntimeException
{
}

View File

@ -0,0 +1,26 @@
<?php
/**
* InconsistentSetupException
*
* PHP version 5
*
* @category Exception
* @package InconsistentSetupException
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
namespace phpseclib\Exception;
/**
* InconsistentSetupException
*
* @package InconsistentSetupException
* @author Jim Wigginton <terrafrost@php.net>
*/
class InconsistentSetupException extends \RuntimeException
{
}

View File

@ -0,0 +1,26 @@
<?php
/**
* InsufficientSetupException
*
* PHP version 5
*
* @category Exception
* @package InsufficientSetupException
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
namespace phpseclib\Exception;
/**
* InsufficientSetupException
*
* @package InsufficientSetupException
* @author Jim Wigginton <terrafrost@php.net>
*/
class InsufficientSetupException extends \RuntimeException
{
}

View File

@ -1633,7 +1633,7 @@ class SSH2
extract(unpack('Ctype', Strings::shift($response, 1)));
/** @var integer $type */
if ($type != NET_SSH2_MSG_KEXDH_GEX_GROUP) {
throw new \RuntimeException('Expected SSH_MSG_KEX_DH_GEX_GROUP');
throw new \UnexpectedValueException('Expected SSH_MSG_KEX_DH_GEX_GROUP');
}
if (strlen($response) < 4) {

View File

@ -348,7 +348,7 @@ abstract class Unit_Crypt_AES_TestCase extends PhpseclibTestCase
}
/**
* @expectedException \LengthException
* @expectedException \phpseclib\Exception\InconsistentSetupException
*/
public function testSetKeyLengthWithLargerKey()
{
@ -363,7 +363,7 @@ abstract class Unit_Crypt_AES_TestCase extends PhpseclibTestCase
}
/**
* @expectedException \LengthException
* @expectedException \phpseclib\Exception\InconsistentSetupException
*/
public function testSetKeyLengthWithSmallerKey()
{
@ -409,7 +409,7 @@ abstract class Unit_Crypt_AES_TestCase extends PhpseclibTestCase
}
/**
* @expectedException \UnexpectedValueException
* @expectedException \phpseclib\Exception\InsufficientSetupException
*/
public function testNoKey()
{

View File

@ -67,7 +67,7 @@ p0GbMJDyR4e9T04ZZwIDAQAB
}
/**
* @expectedException \OutOfBoundsException
* @expectedException \LengthException
*/
public function testSmallModulo()
{