Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Tests/Cipher/CryptoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

use Defuse\Crypto\Key as DefuseKey;
use Joomla\Crypt\Cipher\Crypto as CryptoCipher;
use Joomla\Crypt\Exception\InvalidKeyException;
use Joomla\Crypt\Key;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Symfony\Polyfill\Util\Binary;

Expand Down Expand Up @@ -76,6 +78,29 @@ public function testDataEncryptionAndDecryption($data)
$this->assertSame($data, $decrypted);
}

/**
* @testdox A malformed key is rejected with InvalidKeyException without exposing the key
*
* @param string $method The cipher method to call
*/
#[TestWith(['encrypt'])]
#[TestWith(['decrypt'])]
public function testMalformedKeyIsRejectedWithoutExposingIt(string $method)
{
$exceptionIgnoreArgs = ini_set('zend.exception_ignore_args', '0');

try {
(new CryptoCipher())->$method('data', new Key('crypto', 'not-a-key-SECRET-MATERIAL', ''));

$this->fail('Expected an InvalidKeyException');
} catch (InvalidKeyException $e) {
$this->assertNull($e->getPrevious());
$this->assertStringNotContainsString('SECRET-MATERIAL', (string) $e);
} finally {
ini_set('zend.exception_ignore_args', $exceptionIgnoreArgs);
}
}

/**
* @testdox Validates keys are correctly generated
*
Expand Down
2 changes: 1 addition & 1 deletion docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ try {
|---|---|
| `EncryptionException` | Encryption failed |
| `DecryptionException` | Decryption failed or the ciphertext was modified |
| `InvalidKeyException` | A key could not be generated |
| `InvalidKeyException` | A key could not be generated, or (`Crypto`) the key is malformed |
| `InvalidKeyTypeException` | The key type does not match the cipher |
| `UnsupportedCipherException` | Declared for unsupported environments, but never actually thrown |

Expand Down
14 changes: 14 additions & 0 deletions src/Cipher/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Joomla\Crypt\Cipher;

use Defuse\Crypto\Crypto as DefuseCrypto;
use Defuse\Crypto\Exception\BadFormatException;
use Defuse\Crypto\Exception\CryptoException;
use Defuse\Crypto\Exception\EnvironmentIsBrokenException;
use Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException;
use Defuse\Crypto\Key as DefuseKey;
Expand Down Expand Up @@ -38,6 +40,7 @@ class Crypto implements CipherInterface
*
* @since 2.0.0
* @throws DecryptionException if the data cannot be decrypted
* @throws InvalidKeyException if the key is malformed
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function decrypt($data, Key $key)
Expand All @@ -54,6 +57,11 @@ public function decrypt($data, Key $key)
throw new DecryptionException('DANGER! DANGER! The ciphertext has been tampered with!', $ex->getCode(), $ex);
} catch (EnvironmentIsBrokenException $ex) {
throw new DecryptionException('Cannot safely perform decryption', $ex->getCode(), $ex);
} catch (BadFormatException $ex) {
// The previous exception is not chained, its stack trace contains the key
throw new InvalidKeyException('The key is not a valid crypto key.', $ex->getCode());
} catch (CryptoException $ex) {
throw new DecryptionException('Failed to decrypt data', $ex->getCode());
}
}

Expand All @@ -67,6 +75,7 @@ public function decrypt($data, Key $key)
*
* @since 2.0.0
* @throws EncryptionException if the data cannot be encrypted
* @throws InvalidKeyException if the key is malformed
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function encrypt($data, Key $key)
Expand All @@ -81,6 +90,11 @@ public function encrypt($data, Key $key)
return DefuseCrypto::encrypt($data, DefuseKey::loadFromAsciiSafeString($key->getPrivate()));
} catch (EnvironmentIsBrokenException $ex) {
throw new EncryptionException('Cannot safely perform encryption', $ex->getCode(), $ex);
} catch (BadFormatException $ex) {
// The previous exception is not chained, its stack trace contains the key
throw new InvalidKeyException('The key is not a valid crypto key.', $ex->getCode());
} catch (CryptoException $ex) {
throw new EncryptionException('Unable to encrypt data', $ex->getCode());
}
}

Expand Down
Loading