Files
nextcloud-server-mirror/apps/encryption/lib/Controller/SettingsController.php
Ghassen kefi 7259f0864c feat(encryption): log recovery key and key management actions
Enabling and disabling the recovery key, changing its password,
changing a user's recovery setting, recovering a user's files,
regenerating a user's keys after an admin password reset, changing the
private key password and toggling home storage encryption are now
audited. Passwords and keys are never logged.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ghassen kefi <ghassen.kefi.dev@gmail.com>
2026-11-05 16:34:43 +01:00

141 lines
4.2 KiB
PHP

<?php
/**
* SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Encryption\Controller;
use OCA\Encryption\Crypto\Crypt;
use OCA\Encryption\KeyManager;
use OCA\Encryption\Session;
use OCA\Encryption\Util;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\UseSession;
use OCP\AppFramework\Http\DataResponse;
use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IL10N;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Log\Audit\CriticalActionPerformedEvent;
class SettingsController extends Controller {
/**
* @param string $AppName
* @param IRequest $request
* @param IL10N $l
* @param IUserManager $userManager
* @param IUserSession $userSession
* @param KeyManager $keyManager
* @param Crypt $crypt
* @param Session $session
* @param ISession $ocSession
* @param Util $util
*/
public function __construct(
$appName,
IRequest $request,
private IL10N $l,
private IUserManager $userManager,
private IUserSession $userSession,
private KeyManager $keyManager,
private Crypt $crypt,
private Session $session,
private ISession $ocSession,
private Util $util,
private IEventDispatcher $eventDispatcher,
) {
parent::__construct($appName, $request);
}
#[NoAdminRequired]
#[UseSession]
public function updatePrivateKeyPassword(
#[\SensitiveParameter]
string $oldPassword,
#[\SensitiveParameter]
string $newPassword,
): DataResponse {
$result = false;
$uid = $this->userSession->getUser()->getUID();
$errorMessage = $this->l->t('Could not update the private key password.');
//check if password is correct
$passwordCorrect = $this->userManager->checkPassword($uid, $newPassword);
if ($passwordCorrect === false) {
// if check with uid fails we need to check the password with the login name
// e.g. in the ldap case. For local user we need to check the password with
// the uid because in this case the login name is case insensitive
$loginName = $this->ocSession->get('loginname');
$passwordCorrect = $this->userManager->checkPassword($loginName, $newPassword);
}
if ($passwordCorrect !== false) {
$encryptedKey = $this->keyManager->getPrivateKey($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);
$header = $this->crypt->generateHeader();
if ($encryptedKey) {
$this->keyManager->setPrivateKey($uid, $header . $encryptedKey);
$this->session->setPrivateKey($decryptedKey);
$result = true;
}
} else {
$errorMessage = $this->l->t('The old password was not correct, please try again.');
}
} else {
$errorMessage = $this->l->t('The current log-in password was not correct, please try again.');
}
if ($result === true) {
$this->session->setStatus(Session::INIT_SUCCESSFUL);
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent(
'Private key password of user "%s" was changed',
['userId' => $uid],
false,
'encryption.private_key.password_changed',
));
return new DataResponse(
['message' => $this->l->t('Private key password successfully updated.')]
);
} else {
return new DataResponse(
['message' => $errorMessage],
Http::STATUS_BAD_REQUEST
);
}
}
/**
* @param bool $encryptHomeStorage
* @return DataResponse
*/
#[UseSession]
public function setEncryptHomeStorage($encryptHomeStorage) {
$this->util->setEncryptHomeStorage($encryptHomeStorage);
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent(
'Encryption of home storages was %s',
['status' => $encryptHomeStorage ? 'enabled' : 'disabled'],
false,
'encryption.home_storage.updated',
));
return new DataResponse();
}
}