From 3a6d505a68ce36fd93c7c7ef795ebee94577f31b Mon Sep 17 00:00:00 2001 From: Paragon Initiative Enterprises Date: Thu, 19 Nov 2015 17:00:08 -0500 Subject: [PATCH] Use typed exceptions. --- src/EasyRSA.php | 15 ++++++++++----- src/Exception/InvalidChecksumException.php | 7 +++++++ src/Exception/InvalidCiphertextException.php | 7 +++++++ src/Exception/InvalidKeyException.php | 7 +++++++ test/EncryptionTest.php | 6 +++--- 5 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 src/Exception/InvalidChecksumException.php create mode 100644 src/Exception/InvalidCiphertextException.php create mode 100644 src/Exception/InvalidKeyException.php diff --git a/src/EasyRSA.php b/src/EasyRSA.php index 389a146..eed5933 100644 --- a/src/EasyRSA.php +++ b/src/EasyRSA.php @@ -1,9 +1,14 @@ createKey($size); @@ -84,10 +89,10 @@ public static function decrypt($ciphertext, $rsaPrivateKey) { $split = explode(self::SEPARATOR, $ciphertext); if (\count($split) !== 4) { - throw new \Exception('Invalid ciphertext message'); + throw new InvalidCiphertextException('Invalid ciphertext message'); } if (!\hash_equals($split[0], self::VERSION_TAG)) { - throw new \Exception('Invalid version tag'); + throw new InvalidCiphertextException('Invalid version tag'); } $checksum = \substr( \hash('sha256', implode('$', array_slice($split, 0, 3))), @@ -95,7 +100,7 @@ public static function decrypt($ciphertext, $rsaPrivateKey) 16 ); if (!\hash_equals($split[3], $checksum)) { - throw new \Exception('Invalid checksum'); + throw new InvalidChecksumException('Invalid checksum'); } $key = self::rsaDecrypt( @@ -177,7 +182,7 @@ protected static function rsaDecrypt($ciphertext, $rsaPrivateKey) $return = @$rsa->decrypt($ciphertext); if ($return === false) { - throw new \Exception('Decryption failed'); + throw new InvalidCiphertextException('Decryption failed'); } return $return; } diff --git a/src/Exception/InvalidChecksumException.php b/src/Exception/InvalidChecksumException.php new file mode 100644 index 0000000..296028b --- /dev/null +++ b/src/Exception/InvalidChecksumException.php @@ -0,0 +1,7 @@ +assertInstanceOf('\\Exception', $ex); + $this->assertInstanceOf('\ParagonIE\EasyRSA\Exception\InvalidChecksumException', $ex); } $dissect[3] = substr( hash('sha256', implode('$', array_slice($dissect, 0, 3))), @@ -67,7 +67,7 @@ public function testFailure() $dummy = EasyRSA::decrypt(implode('$', $dissect), $secretKey); $this->fail('This should not have passed.'); } catch (\Exception $ex) { - $this->assertInstanceOf('\\Exception', $ex); + $this->assertInstanceOf('\ParagonIE\EasyRSA\Exception\InvalidCiphertextException', $ex); } /////////////////////////////////////////////////////////////////////// @@ -87,7 +87,7 @@ public function testFailure() unset($dummy); return; } catch (\Exception $ex) { - $this->assertInstanceOf('\\Exception', $ex); + $this->assertInstanceOf('\ParagonIE\EasyRSA\Exception\InvalidChecksumException', $ex); } $dissect[3] = substr( hash('sha256', implode('$', array_slice($dissect, 0, 3))),