diff --git a/apps/encryption/lib/Controller/SettingsController.php b/apps/encryption/lib/Controller/SettingsController.php index f36ed60b56ebc..3ccd851f2c159 100644 --- a/apps/encryption/lib/Controller/SettingsController.php +++ b/apps/encryption/lib/Controller/SettingsController.php @@ -17,6 +17,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 6f1e0bb935dc3..edd55600a72e9 100644 --- a/apps/encryption/tests/Controller/SettingsControllerTest.php +++ b/apps/encryption/tests/Controller/SettingsControllerTest.php @@ -10,6 +10,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; @@ -147,6 +148,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 */