2015-02-24 21:05:19 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Clark Tomlinson <clark@owncloud.com>
|
|
|
|
* @since 2/19/15, 10:02 AM
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
|
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\App;
|
|
|
|
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 {
|
|
|
|
/**
|
|
|
|
* @var KeyManager
|
|
|
|
*/
|
|
|
|
private $keyManager;
|
|
|
|
/**
|
|
|
|
* @var ILogger
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
/**
|
|
|
|
* @var Setup
|
|
|
|
*/
|
|
|
|
private $userSetup;
|
|
|
|
/**
|
|
|
|
* @var IUserSession
|
|
|
|
*/
|
|
|
|
private $user;
|
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
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param Setup $userSetup
|
|
|
|
* @param IUserSession $user
|
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,
|
|
|
|
ILogger $logger,
|
|
|
|
Setup $userSetup,
|
|
|
|
IUserSession $user,
|
|
|
|
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;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->userSetup = $userSetup;
|
|
|
|
$this->user = $user;
|
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');
|
|
|
|
OCUtil::connectHook('OC_User',
|
|
|
|
'post_setPassword',
|
|
|
|
$this,
|
|
|
|
'setPassphrase');
|
|
|
|
OCUtil::connectHook('OC_User',
|
|
|
|
'pre_setPassword',
|
|
|
|
$this,
|
|
|
|
'preSetPassphrase');
|
|
|
|
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
|
|
|
|
* @return bool
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
|
|
|
public function login($params) {
|
|
|
|
|
|
|
|
if (!App::isEnabled('encryption')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure filesystem is loaded
|
|
|
|
// Todo: update?
|
|
|
|
if (!\OC\Files\Filesystem::$loaded) {
|
|
|
|
\OC_Util::setupFS($params['uid']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup user, if user not ready force relogin
|
2015-03-25 00:29:10 +03:00
|
|
|
if (!$this->userSetup->setupUser($params['uid'], $params['password'])) {
|
2015-02-24 21:05:19 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
if (App::isEnabled('encryption')) {
|
|
|
|
$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) {
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
if (App::isEnabled('encryption')) {
|
|
|
|
$this->keyManager->deletePublicKey($params['uid']);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the password can't be changed within ownCloud, than update the key password in advance.
|
|
|
|
*
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param array $params : uid, password
|
|
|
|
* @return bool
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2015-03-25 00:29:10 +03:00
|
|
|
public function preSetPassphrase($params) {
|
|
|
|
if (App::isEnabled('encryption')) {
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
if (!$this->user->getUser()->canChangePassword()) {
|
2015-03-31 13:01:51 +03:00
|
|
|
$this->setPassphrase($params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function setPassphrase($params) {
|
|
|
|
|
|
|
|
// Get existing decrypted private key
|
|
|
|
$privateKey = $this->session->getPrivateKey();
|
|
|
|
|
|
|
|
if ($params['uid'] === $this->user->getUser()->getUID() && $privateKey) {
|
|
|
|
|
|
|
|
// Encrypt private key with new user pwd as passphrase
|
|
|
|
$encryptedPrivateKey = $this->crypt->symmetricEncryptFileContent($privateKey,
|
|
|
|
$params['password']);
|
|
|
|
|
|
|
|
// Save private key
|
|
|
|
if ($encryptedPrivateKey) {
|
2015-04-01 15:24:56 +03:00
|
|
|
$this->keyManager->setPrivateKey($this->user->getUser()->getUID(),
|
2015-03-31 13:01:51 +03:00
|
|
|
$encryptedPrivateKey);
|
|
|
|
} 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
|
|
|
|
} else { // admin changed the password for a different user, create new keys and reencrypt file keys
|
|
|
|
$user = $params['uid'];
|
|
|
|
$recoveryPassword = isset($params['recoveryPassword']) ? $params['recoveryPassword'] : null;
|
|
|
|
|
|
|
|
// 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 (
|
|
|
|
($this->recovery->isRecoveryEnabledForUser($user) && $recoveryPassword)
|
|
|
|
|| !$this->keyManager->userHasKeys($user)
|
|
|
|
|| !$this->util->userHasFiles($user)
|
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
|
2015-04-01 15:24:56 +03:00
|
|
|
$this->keyManager->setPublicKey($user, $keyPair['publicKey']);
|
2015-03-31 13:01:51 +03:00
|
|
|
|
|
|
|
// Encrypt private key with new password
|
|
|
|
$encryptedKey = $this->crypt->symmetricEncryptFileContent($keyPair['privateKey'],
|
|
|
|
$newUserPassword);
|
|
|
|
|
|
|
|
if ($encryptedKey) {
|
2015-04-01 15:24:56 +03:00
|
|
|
$this->keyManager->setPrivateKey($user, $encryptedKey);
|
2015-03-31 13:01:51 +03:00
|
|
|
|
|
|
|
if ($recoveryPassword) { // if recovery key is set we can re-encrypt the key files
|
2015-04-01 15:24:56 +03:00
|
|
|
$this->recovery->recoverUsersFiles($recoveryPassword, $user);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
|
2015-03-31 13:01:51 +03:00
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
|
|
|
* after password reset we create a new key pair for the user
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function postPasswordReset($params) {
|
|
|
|
$password = $params['password'];
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
$this->keyManager->replaceUserKeys($params['uid']);
|
|
|
|
$this->userSetup->setupServerSide($params['uid'], $password);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
}
|