2015-02-24 21:05:19 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Clark Tomlinson <fallen013@gmail.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
2015-02-24 21:05:19 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Encryption\Hooks;
|
|
|
|
|
|
|
|
|
2016-04-19 17:59:08 +03:00
|
|
|
use OC\Files\Filesystem;
|
2018-08-16 16:06:23 +03:00
|
|
|
use OCP\Encryption\Exceptions\GenericEncryptionException;
|
2015-09-11 22:18:13 +03:00
|
|
|
use OCP\IUserManager;
|
2015-03-25 00:29:10 +03:00
|
|
|
use OCP\Util as OCUtil;
|
2015-02-24 21:05:19 +03:00
|
|
|
use OCA\Encryption\Hooks\Contracts\IHook;
|
|
|
|
use OCA\Encryption\KeyManager;
|
2015-04-01 15:24:56 +03:00
|
|
|
use OCA\Encryption\Crypto\Crypt;
|
2015-02-24 21:05:19 +03:00
|
|
|
use OCA\Encryption\Users\Setup;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IUserSession;
|
2015-03-25 00:29:10 +03:00
|
|
|
use OCA\Encryption\Util;
|
2015-03-31 12:15:47 +03:00
|
|
|
use OCA\Encryption\Session;
|
2015-03-31 13:01:51 +03:00
|
|
|
use OCA\Encryption\Recovery;
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
class UserHooks implements IHook {
|
2017-01-02 23:24:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* list of user for which we perform a password reset
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $passwordResetUsers = [];
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
|
|
|
* @var KeyManager
|
|
|
|
*/
|
|
|
|
private $keyManager;
|
2015-09-11 22:18:13 +03:00
|
|
|
/**
|
|
|
|
* @var IUserManager
|
|
|
|
*/
|
|
|
|
private $userManager;
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
|
|
|
* @var ILogger
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
/**
|
|
|
|
* @var Setup
|
|
|
|
*/
|
|
|
|
private $userSetup;
|
|
|
|
/**
|
|
|
|
* @var IUserSession
|
|
|
|
*/
|
2019-08-26 16:22:00 +03:00
|
|
|
private $userSession;
|
2015-03-25 00:29:10 +03:00
|
|
|
/**
|
|
|
|
* @var Util
|
|
|
|
*/
|
|
|
|
private $util;
|
2015-03-27 03:35:36 +03:00
|
|
|
/**
|
2015-03-31 12:15:47 +03:00
|
|
|
* @var Session
|
2015-03-27 03:35:36 +03:00
|
|
|
*/
|
|
|
|
private $session;
|
2015-03-31 13:01:51 +03:00
|
|
|
/**
|
|
|
|
* @var Recovery
|
|
|
|
*/
|
|
|
|
private $recovery;
|
2015-04-01 15:24:56 +03:00
|
|
|
/**
|
|
|
|
* @var Crypt
|
|
|
|
*/
|
|
|
|
private $crypt;
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UserHooks constructor.
|
|
|
|
*
|
|
|
|
* @param KeyManager $keyManager
|
2015-09-11 22:18:13 +03:00
|
|
|
* @param IUserManager $userManager
|
2015-02-24 21:05:19 +03:00
|
|
|
* @param ILogger $logger
|
|
|
|
* @param Setup $userSetup
|
2019-08-26 16:22:00 +03:00
|
|
|
* @param IUserSession $userSession
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param Util $util
|
2015-03-31 12:15:47 +03:00
|
|
|
* @param Session $session
|
2015-04-01 15:24:56 +03:00
|
|
|
* @param Crypt $crypt
|
2015-03-31 13:01:51 +03:00
|
|
|
* @param Recovery $recovery
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2015-03-27 03:35:36 +03:00
|
|
|
public function __construct(KeyManager $keyManager,
|
2015-09-11 22:18:13 +03:00
|
|
|
IUserManager $userManager,
|
2015-03-27 03:35:36 +03:00
|
|
|
ILogger $logger,
|
|
|
|
Setup $userSetup,
|
2019-08-26 16:22:00 +03:00
|
|
|
IUserSession $userSession,
|
2015-03-27 03:35:36 +03:00
|
|
|
Util $util,
|
2015-03-31 13:01:51 +03:00
|
|
|
Session $session,
|
2015-04-01 15:24:56 +03:00
|
|
|
Crypt $crypt,
|
2015-03-31 13:01:51 +03:00
|
|
|
Recovery $recovery) {
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
$this->keyManager = $keyManager;
|
2015-09-11 22:18:13 +03:00
|
|
|
$this->userManager = $userManager;
|
2015-02-24 21:05:19 +03:00
|
|
|
$this->logger = $logger;
|
|
|
|
$this->userSetup = $userSetup;
|
2019-08-26 16:22:00 +03:00
|
|
|
$this->userSession = $userSession;
|
2015-03-25 00:29:10 +03:00
|
|
|
$this->util = $util;
|
2015-03-27 03:35:36 +03:00
|
|
|
$this->session = $session;
|
2015-03-31 13:01:51 +03:00
|
|
|
$this->recovery = $recovery;
|
2015-04-01 15:24:56 +03:00
|
|
|
$this->crypt = $crypt;
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects Hooks
|
|
|
|
*
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function addHooks() {
|
2015-03-25 00:29:10 +03:00
|
|
|
OCUtil::connectHook('OC_User', 'post_login', $this, 'login');
|
|
|
|
OCUtil::connectHook('OC_User', 'logout', $this, 'logout');
|
2016-03-02 15:58:06 +03:00
|
|
|
|
|
|
|
// this hooks only make sense if no master key is used
|
|
|
|
if ($this->util->isMasterKeyEnabled() === false) {
|
|
|
|
OCUtil::connectHook('OC_User',
|
|
|
|
'post_setPassword',
|
|
|
|
$this,
|
|
|
|
'setPassphrase');
|
|
|
|
|
|
|
|
OCUtil::connectHook('OC_User',
|
|
|
|
'pre_setPassword',
|
|
|
|
$this,
|
|
|
|
'preSetPassphrase');
|
|
|
|
|
2017-01-02 23:24:37 +03:00
|
|
|
OCUtil::connectHook('\OC\Core\LostPassword\Controller\LostController',
|
|
|
|
'post_passwordReset',
|
|
|
|
$this,
|
|
|
|
'postPasswordReset');
|
|
|
|
|
|
|
|
OCUtil::connectHook('\OC\Core\LostPassword\Controller\LostController',
|
|
|
|
'pre_passwordReset',
|
|
|
|
$this,
|
|
|
|
'prePasswordReset');
|
|
|
|
|
2016-03-02 15:58:06 +03:00
|
|
|
OCUtil::connectHook('OC_User',
|
|
|
|
'post_createUser',
|
|
|
|
$this,
|
|
|
|
'postCreateUser');
|
|
|
|
|
|
|
|
OCUtil::connectHook('OC_User',
|
|
|
|
'post_deleteUser',
|
|
|
|
$this,
|
|
|
|
'postDeleteUser');
|
|
|
|
}
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Startup encryption backend upon user login
|
|
|
|
*
|
|
|
|
* @note This method should never be called for users using client side encryption
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param array $params
|
2015-11-27 18:32:44 +03:00
|
|
|
* @return boolean|null
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
|
|
|
public function login($params) {
|
|
|
|
// ensure filesystem is loaded
|
|
|
|
if (!\OC\Files\Filesystem::$loaded) {
|
2016-03-02 15:58:06 +03:00
|
|
|
$this->setupFS($params['uid']);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
2016-03-02 15:58:06 +03:00
|
|
|
if ($this->util->isMasterKeyEnabled() === false) {
|
|
|
|
$this->userSetup->setupUser($params['uid'], $params['password']);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
$this->keyManager->init($params['uid'], $params['password']);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove keys from session during logout
|
|
|
|
*/
|
|
|
|
public function logout() {
|
2015-03-31 13:01:51 +03:00
|
|
|
$this->session->clear();
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setup encryption backend upon user created
|
|
|
|
*
|
|
|
|
* @note This method should never be called for users using client side encryption
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param array $params
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
|
|
|
public function postCreateUser($params) {
|
2017-10-24 00:47:06 +03:00
|
|
|
$this->userSetup->setupUser($params['uid'], $params['password']);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* cleanup encryption backend upon user deleted
|
|
|
|
*
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param array $params : uid, password
|
2015-02-24 21:05:19 +03:00
|
|
|
* @note This method should never be called for users using client side encryption
|
|
|
|
*/
|
|
|
|
public function postDeleteUser($params) {
|
2017-10-24 00:47:06 +03:00
|
|
|
$this->keyManager->deletePublicKey($params['uid']);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
2017-01-02 23:24:37 +03:00
|
|
|
public function prePasswordReset($params) {
|
2017-10-24 00:47:06 +03:00
|
|
|
$user = $params['uid'];
|
|
|
|
self::$passwordResetUsers[$user] = true;
|
2017-01-02 23:24:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function postPasswordReset($params) {
|
|
|
|
$uid = $params['uid'];
|
|
|
|
$password = $params['password'];
|
|
|
|
$this->keyManager->backupUserKeys('passwordReset', $uid);
|
|
|
|
$this->keyManager->deleteUserKeys($uid);
|
|
|
|
$this->userSetup->setupUser($uid, $password);
|
|
|
|
unset(self::$passwordResetUsers[$uid]);
|
|
|
|
}
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
2017-04-12 07:16:27 +03:00
|
|
|
* If the password can't be changed within Nextcloud, than update the key password in advance.
|
2015-02-24 21:05:19 +03:00
|
|
|
*
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param array $params : uid, password
|
2015-11-27 18:32:44 +03:00
|
|
|
* @return boolean|null
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2015-03-25 00:29:10 +03:00
|
|
|
public function preSetPassphrase($params) {
|
2017-01-02 23:24:37 +03:00
|
|
|
$user = $this->userManager->get($params['uid']);
|
2015-04-17 11:52:40 +03:00
|
|
|
|
2017-01-02 23:24:37 +03:00
|
|
|
if ($user && !$user->canChangePassword()) {
|
|
|
|
$this->setPassphrase($params);
|
2015-03-31 13:01:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 14:12:05 +03:00
|
|
|
/**
|
2015-03-31 13:01:51 +03:00
|
|
|
* Change a user's encryption passphrase
|
|
|
|
*
|
|
|
|
* @param array $params keys: uid, password
|
2015-11-27 18:32:44 +03:00
|
|
|
* @return boolean|null
|
2015-03-31 13:01:51 +03:00
|
|
|
*/
|
|
|
|
public function setPassphrase($params) {
|
|
|
|
|
2017-01-02 23:24:37 +03:00
|
|
|
// if we are in the process to resetting a user password, we have nothing
|
|
|
|
// to do here
|
|
|
|
if (isset(self::$passwordResetUsers[$params['uid']])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-31 13:01:51 +03:00
|
|
|
// Get existing decrypted private key
|
2019-08-26 16:22:00 +03:00
|
|
|
$user = $this->userSession->getUser();
|
2015-03-31 13:01:51 +03:00
|
|
|
|
2015-04-17 11:52:40 +03:00
|
|
|
// current logged in user changes his own password
|
2018-08-16 16:06:23 +03:00
|
|
|
if ($user && $params['uid'] === $user->getUID()) {
|
|
|
|
|
|
|
|
$privateKey = $this->session->getPrivateKey();
|
2015-03-31 13:01:51 +03:00
|
|
|
|
|
|
|
// Encrypt private key with new user pwd as passphrase
|
2015-08-07 16:51:43 +03:00
|
|
|
$encryptedPrivateKey = $this->crypt->encryptPrivateKey($privateKey, $params['password'], $params['uid']);
|
2015-03-31 13:01:51 +03:00
|
|
|
|
|
|
|
// Save private key
|
|
|
|
if ($encryptedPrivateKey) {
|
2019-08-26 16:22:00 +03:00
|
|
|
$this->keyManager->setPrivateKey($user->getUID(),
|
2015-04-28 13:57:30 +03:00
|
|
|
$this->crypt->generateHeader() . $encryptedPrivateKey);
|
2015-03-31 13:01:51 +03:00
|
|
|
} else {
|
2015-04-02 14:12:05 +03:00
|
|
|
$this->logger->error('Encryption could not update users encryption password');
|
2015-03-31 13:01:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Session does not need to be updated as the
|
|
|
|
// private key has not changed, only the passphrase
|
|
|
|
// used to decrypt it has changed
|
2015-04-17 11:52:40 +03:00
|
|
|
} else { // admin changed the password for a different user, create new keys and re-encrypt file keys
|
2019-08-26 16:22:00 +03:00
|
|
|
$userId = $params['uid'];
|
|
|
|
$this->initMountPoints($userId);
|
2015-03-31 13:01:51 +03:00
|
|
|
$recoveryPassword = isset($params['recoveryPassword']) ? $params['recoveryPassword'] : null;
|
|
|
|
|
2018-08-16 16:06:23 +03:00
|
|
|
$recoveryKeyId = $this->keyManager->getRecoveryKeyId();
|
|
|
|
$recoveryKey = $this->keyManager->getSystemPrivateKey($recoveryKeyId);
|
|
|
|
try {
|
|
|
|
$decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $recoveryPassword);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$decryptedRecoveryKey = false;
|
|
|
|
}
|
|
|
|
if ($decryptedRecoveryKey === false) {
|
|
|
|
$message = 'Can not decrypt the recovery key. Maybe you provided the wrong password. Try again.';
|
|
|
|
throw new GenericEncryptionException($message, $message);
|
|
|
|
}
|
|
|
|
|
2015-03-31 13:01:51 +03:00
|
|
|
// we generate new keys if...
|
|
|
|
// ...we have a recovery password and the user enabled the recovery key
|
|
|
|
// ...encryption was activated for the first time (no keys exists)
|
|
|
|
// ...the user doesn't have any files
|
2015-04-01 15:24:56 +03:00
|
|
|
if (
|
2019-08-26 16:22:00 +03:00
|
|
|
($this->recovery->isRecoveryEnabledForUser($userId) && $recoveryPassword)
|
|
|
|
|| !$this->keyManager->userHasKeys($userId)
|
|
|
|
|| !$this->util->userHasFiles($userId)
|
2015-03-31 13:01:51 +03:00
|
|
|
) {
|
|
|
|
|
|
|
|
// backup old keys
|
2015-04-01 15:24:56 +03:00
|
|
|
//$this->backupAllKeys('recovery');
|
2015-03-31 13:01:51 +03:00
|
|
|
|
|
|
|
$newUserPassword = $params['password'];
|
|
|
|
|
|
|
|
$keyPair = $this->crypt->createKeyPair();
|
|
|
|
|
|
|
|
// Save public key
|
2019-08-26 16:22:00 +03:00
|
|
|
$this->keyManager->setPublicKey($userId, $keyPair['publicKey']);
|
2015-03-31 13:01:51 +03:00
|
|
|
|
|
|
|
// Encrypt private key with new password
|
2019-08-26 16:22:00 +03:00
|
|
|
$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $newUserPassword, $userId);
|
2015-03-31 13:01:51 +03:00
|
|
|
|
|
|
|
if ($encryptedKey) {
|
2019-08-26 16:22:00 +03:00
|
|
|
$this->keyManager->setPrivateKey($userId, $this->crypt->generateHeader() . $encryptedKey);
|
2015-03-31 13:01:51 +03:00
|
|
|
|
|
|
|
if ($recoveryPassword) { // if recovery key is set we can re-encrypt the key files
|
2019-08-26 16:22:00 +03:00
|
|
|
$this->recovery->recoverUsersFiles($recoveryPassword, $userId);
|
2015-03-31 13:01:51 +03:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-02 14:12:05 +03:00
|
|
|
$this->logger->error('Encryption Could not update users encryption password');
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 17:59:08 +03:00
|
|
|
/**
|
|
|
|
* init mount points for given user
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @throws \OC\User\NoUserException
|
|
|
|
*/
|
|
|
|
protected function initMountPoints($user) {
|
|
|
|
Filesystem::initMountPoints($user);
|
|
|
|
}
|
2015-03-25 00:29:10 +03:00
|
|
|
|
2016-03-02 15:58:06 +03:00
|
|
|
/**
|
|
|
|
* setup file system for user
|
|
|
|
*
|
|
|
|
* @param string $uid user id
|
|
|
|
*/
|
|
|
|
protected function setupFS($uid) {
|
|
|
|
\OC_Util::setupFS($uid);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
}
|