Skip to content

Commit

Permalink
Throw exception if decryption fails
Browse files Browse the repository at this point in the history
For #11868

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
  • Loading branch information
rullzer committed Nov 14, 2018
1 parent fef5189 commit be5c050
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/private/Security/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,16 @@ public function encrypt(string $plaintext, string $password = ''): string {
* @param string $password Password to encrypt, if not specified the secret from config.php will be taken
* @return string plaintext
* @throws \Exception If the HMAC does not match
* @throws \Exception If the decryption failed
*/
public function decrypt(string $authenticatedCiphertext, string $password = ''): string {
if($password === '') {
if ($password === '') {
$password = $this->config->getSystemValue('secret');
}
$this->cipher->setPassword($password);

$parts = explode('|', $authenticatedCiphertext);
if(\count($parts) !== 3) {
if (\count($parts) !== 3) {
throw new \Exception('Authenticated ciphertext could not be decoded.');
}

Expand All @@ -126,11 +127,16 @@ public function decrypt(string $authenticatedCiphertext, string $password = ''):

$this->cipher->setIV($iv);

if(!hash_equals($this->calculateHMAC($parts[0].$parts[1], $password), $hmac)) {
if (!hash_equals($this->calculateHMAC($parts[0] . $parts[1], $password), $hmac)) {
throw new \Exception('HMAC does not match.');
}

return $this->cipher->decrypt($ciphertext);
$result = $this->cipher->decrypt($ciphertext);
if ($result === false) {
throw new \Exception('Decryption failed');
}

return $result;
}

}
1 change: 1 addition & 0 deletions lib/public/Security/ICrypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function encrypt(string $plaintext, string $password = ''): string;
* @param string $password Password to encrypt, if not specified the secret from config.php will be taken
* @return string plaintext
* @throws \Exception If the HMAC does not match
* @throws \Exception If the decryption failed
* @since 8.0.0
*/
public function decrypt(string $authenticatedCiphertext, string $password = ''): string;
Expand Down

0 comments on commit be5c050

Please sign in to comment.