Skip to content

Commit

Permalink
Use typed exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Nov 19, 2015
1 parent e87e899 commit 3a6d505
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/EasyRSA.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?php
namespace ParagonIE\EasyRSA;

// PHPSecLib:
use \phpseclib\Crypt\RSA;
// defuse/php-encryption:
use \Crypto;
// Typed Exceptions:
use \ParagonIE\EasyRSA\Exception\InvalidChecksumException;
use \ParagonIE\EasyRSA\Exception\InvalidCiphertextException;
use \ParagonIE\EasyRSA\Exception\InvalidKeyException;

class EasyRSA implements EasyRSAInterface
{
Expand All @@ -18,7 +23,7 @@ class EasyRSA implements EasyRSAInterface
public static function generateKeyPair($size = 2048)
{
if ($size < 2048) {
throw new \Exception('Key size must be at least 2048 bits.');
throw new InvalidKeyException('Key size must be at least 2048 bits.');
}
$rsa = new RSA();
$keypair = $rsa->createKey($size);
Expand Down Expand Up @@ -84,18 +89,18 @@ 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))),
0,
16
);
if (!\hash_equals($split[3], $checksum)) {
throw new \Exception('Invalid checksum');
throw new InvalidChecksumException('Invalid checksum');
}

$key = self::rsaDecrypt(
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/InvalidChecksumException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace ParagonIE\EasyRSA\Exception;

class InvalidChecksumException extends \Exception
{

}
7 changes: 7 additions & 0 deletions src/Exception/InvalidCiphertextException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace ParagonIE\EasyRSA\Exception;

class InvalidCiphertextException extends \Exception
{

}
7 changes: 7 additions & 0 deletions src/Exception/InvalidKeyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace ParagonIE\EasyRSA\Exception;

class InvalidKeyException extends \Exception
{

}
6 changes: 3 additions & 3 deletions test/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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))),
Expand All @@ -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);
}

///////////////////////////////////////////////////////////////////////
Expand All @@ -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))),
Expand Down

0 comments on commit 3a6d505

Please sign in to comment.