diff --git a/apps/encryption/lib/Controller/SettingsController.php b/apps/encryption/lib/Controller/SettingsController.php index fd3637e0d7963..537ffa39a865d 100644 --- a/apps/encryption/lib/Controller/SettingsController.php +++ b/apps/encryption/lib/Controller/SettingsController.php @@ -16,6 +16,7 @@ use OCP\AppFramework\Http\Attribute\NoAdminRequired; use OCP\AppFramework\Http\Attribute\UseSession; use OCP\AppFramework\Http\DataResponse; +use OCP\Encryption\Exceptions\GenericEncryptionException; use OCP\IL10N; use OCP\IRequest; use OCP\ISession; @@ -76,7 +77,13 @@ public function updatePrivateKeyPassword($oldPassword, $newPassword) { if ($passwordCorrect !== false) { $encryptedKey = $this->keyManager->getPrivateKey($uid); - $decryptedKey = $this->crypt->decryptPrivateKey($encryptedKey, $oldPassword, $uid); + try { + $decryptedKey = $this->crypt->decryptPrivateKey($encryptedKey, $oldPassword, $uid); + } catch (GenericEncryptionException) { + // A wrong passphrase does not only make decrypting return false, it can + // also fail the signature check or the decryption itself + $decryptedKey = false; + } if ($decryptedKey) { $encryptedKey = $this->crypt->encryptPrivateKey($decryptedKey, $newPassword, $uid); diff --git a/apps/encryption/tests/Controller/SettingsControllerTest.php b/apps/encryption/tests/Controller/SettingsControllerTest.php index bee20f67cecdb..b61c4d76371e3 100644 --- a/apps/encryption/tests/Controller/SettingsControllerTest.php +++ b/apps/encryption/tests/Controller/SettingsControllerTest.php @@ -9,6 +9,7 @@ */ namespace OCA\Encryption\Tests\Controller; +use OC\Encryption\Exceptions\DecryptionFailedException; use OCA\Encryption\Controller\SettingsController; use OCA\Encryption\Crypto\Crypt; use OCA\Encryption\KeyManager; @@ -146,6 +147,28 @@ public function testUpdatePrivateKeyPasswordWrongOldPassword(): void { $data['message']); } + /** + * test updatePrivateKeyPassword() if decrypting the private key with the given + * old password fails with an exception instead of returning false + */ + public function testUpdatePrivateKeyPasswordUndecryptableKey(): void { + $this->userManagerMock + ->expects($this->once()) + ->method('checkPassword') + ->willReturn(true); + + $this->cryptMock + ->expects($this->once()) + ->method('decryptPrivateKey') + ->willThrowException(new DecryptionFailedException('Decryption failed')); + + $result = $this->controller->updatePrivateKeyPassword('old', 'new'); + + $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus()); + $this->assertSame('The old password was not correct, please try again.', + $result->getData()['message']); + } + /** * test updatePrivateKeyPassword() with the correct old and new password */