From be5c050acc9f9dffa6a28f04822f5f1fd7e73127 Mon Sep 17 00:00:00 2001
From: Roeland Jago Douma <roeland@famdouma.nl>
Date: Wed, 14 Nov 2018 12:47:35 +0100
Subject: [PATCH] Throw exception if decryption fails

For #11868

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
---
 lib/private/Security/Crypto.php | 14 ++++++++++----
 lib/public/Security/ICrypto.php |  1 +
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/lib/private/Security/Crypto.php b/lib/private/Security/Crypto.php
index 04d618bf373..876f159950c 100644
--- a/lib/private/Security/Crypto.php
+++ b/lib/private/Security/Crypto.php
@@ -108,15 +108,16 @@ class Crypto implements ICrypto {
 	 * @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.');
 		}
 
@@ -126,11 +127,16 @@ class Crypto implements ICrypto {
 
 		$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;
 	}
 
 }
diff --git a/lib/public/Security/ICrypto.php b/lib/public/Security/ICrypto.php
index ef5bd2bf7c9..3e17d461b64 100644
--- a/lib/public/Security/ICrypto.php
+++ b/lib/public/Security/ICrypto.php
@@ -60,6 +60,7 @@ interface ICrypto {
 	 * @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;
-- 
GitLab