2012-12-11 19:10:56 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2014-11-14 14:20:59 +03:00
|
|
|
* @author Bjoern Schiessle <schiessle@owncloud.com>
|
2015-02-23 13:28:53 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Florin Peter <github@florin-peter.de>
|
|
|
|
* @author Joas Schilling <nickvergessen@gmx.de>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
|
|
|
|
* @author Sam Tuke <mail@samtuke.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2012-12-11 19:10:56 +04:00
|
|
|
*
|
2015-02-23 13:28:53 +03:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
2012-12-11 19:10:56 +04:00
|
|
|
*
|
2015-02-23 13:28:53 +03:00
|
|
|
* 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,
|
2012-12-11 19:10:56 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-02-23 13:28:53 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2012-12-11 19:10:56 +04:00
|
|
|
*
|
2015-02-23 13:28:53 +03:00
|
|
|
* 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/>
|
2012-12-11 19:10:56 +04:00
|
|
|
*
|
|
|
|
*/
|
2014-12-03 12:57:16 +03:00
|
|
|
namespace OCA\Files_Encryption;
|
2012-12-11 19:10:56 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for handling encryption related session data
|
|
|
|
*/
|
|
|
|
|
2013-05-27 19:26:58 +04:00
|
|
|
class Session {
|
2013-04-09 21:11:38 +04:00
|
|
|
|
|
|
|
private $view;
|
2014-11-25 12:12:30 +03:00
|
|
|
private static $publicShareKey = false;
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2013-09-06 14:27:40 +04:00
|
|
|
const NOT_INITIALIZED = '0';
|
|
|
|
const INIT_EXECUTED = '1';
|
|
|
|
const INIT_SUCCESSFUL = '2';
|
|
|
|
|
|
|
|
|
2013-02-13 20:57:45 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* if session is started, check if ownCloud key pair is set up, if not create it
|
2014-05-12 18:30:39 +04:00
|
|
|
* @param \OC\Files\View $view
|
2013-05-20 03:24:36 +04:00
|
|
|
*
|
|
|
|
* @note The ownCloud key pair is used to allow public link sharing even if encryption is enabled
|
2013-02-13 20:57:45 +04:00
|
|
|
*/
|
2013-05-27 19:26:58 +04:00
|
|
|
public function __construct($view) {
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2013-04-09 21:11:38 +04:00
|
|
|
$this->view = $view;
|
2013-04-10 18:46:02 +04:00
|
|
|
|
2014-11-18 19:25:36 +03:00
|
|
|
if (!$this->view->is_dir('files_encryption')) {
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-11-18 19:25:36 +03:00
|
|
|
$this->view->mkdir('files_encryption');
|
2013-04-10 18:46:02 +04:00
|
|
|
|
2013-02-13 20:57:45 +04:00
|
|
|
}
|
2013-05-13 17:15:35 +04:00
|
|
|
|
2014-02-13 19:28:49 +04:00
|
|
|
$appConfig = \OC::$server->getAppConfig();
|
|
|
|
|
2014-11-14 19:30:38 +03:00
|
|
|
$publicShareKeyId = Helper::getPublicShareKeyId();
|
2013-05-13 17:15:35 +04:00
|
|
|
|
2014-11-14 19:30:38 +03:00
|
|
|
if ($publicShareKeyId === false) {
|
2013-05-27 19:26:58 +04:00
|
|
|
$publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8);
|
2014-02-13 19:28:49 +04:00
|
|
|
$appConfig->setValue('files_encryption', 'publicShareKeyId', $publicShareKeyId);
|
2013-05-13 17:15:35 +04:00
|
|
|
}
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-11-14 19:30:38 +03:00
|
|
|
if (!Keymanager::publicShareKeyExists($view)) {
|
2013-05-20 03:24:36 +04:00
|
|
|
|
|
|
|
$keypair = Crypt::createKeypair();
|
|
|
|
|
|
|
|
|
|
|
|
// Save public key
|
2014-11-14 19:30:38 +03:00
|
|
|
Keymanager::setPublicKey($keypair['publicKey'], $publicShareKeyId);
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2013-05-24 01:56:31 +04:00
|
|
|
// Encrypt private key empty passphrase
|
2014-12-03 18:52:44 +03:00
|
|
|
$cipher = Helper::getCipher();
|
|
|
|
$encryptedKey = Crypt::symmetricEncryptFileContent($keypair['privateKey'], '', $cipher);
|
2014-07-21 15:02:28 +04:00
|
|
|
if ($encryptedKey) {
|
2014-11-14 19:30:38 +03:00
|
|
|
Keymanager::setPrivateSystemKey($encryptedKey, $publicShareKeyId);
|
2014-07-21 15:02:28 +04:00
|
|
|
} else {
|
|
|
|
\OCP\Util::writeLog('files_encryption', 'Could not create public share keys', \OCP\Util::ERROR);
|
|
|
|
}
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2013-02-13 20:57:45 +04:00
|
|
|
}
|
2013-05-13 23:24:59 +04:00
|
|
|
|
2014-12-03 18:52:44 +03:00
|
|
|
if (Helper::isPublicAccess() && !self::getPublicSharePrivateKey()) {
|
2013-05-20 03:24:36 +04:00
|
|
|
// Disable encryption proxy to prevent recursive calls
|
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = false;
|
2013-05-13 23:24:59 +04:00
|
|
|
|
2014-11-14 19:30:38 +03:00
|
|
|
$encryptedKey = Keymanager::getPrivateSystemKey($publicShareKeyId);
|
2013-06-03 16:19:31 +04:00
|
|
|
$privateKey = Crypt::decryptPrivateKey($encryptedKey, '');
|
2014-11-25 12:12:30 +03:00
|
|
|
self::setPublicSharePrivateKey($privateKey);
|
2014-05-20 19:54:14 +04:00
|
|
|
|
2013-05-20 03:24:36 +04:00
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
|
|
|
}
|
2013-02-13 20:57:45 +04:00
|
|
|
}
|
2012-12-11 19:10:56 +04:00
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Sets user private key to session
|
2013-05-20 00:31:00 +04:00
|
|
|
* @param string $privateKey
|
2012-12-11 19:10:56 +04:00
|
|
|
* @return bool
|
2013-05-28 11:27:04 +04:00
|
|
|
*
|
|
|
|
* @note this should only be set on login
|
2012-12-11 19:10:56 +04:00
|
|
|
*/
|
2013-05-27 19:26:58 +04:00
|
|
|
public function setPrivateKey($privateKey) {
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-07-16 21:40:22 +04:00
|
|
|
\OC::$server->getSession()->set('privateKey', $privateKey);
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2012-12-11 19:10:56 +04:00
|
|
|
return true;
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2012-12-11 19:10:56 +04:00
|
|
|
}
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-08-07 17:26:09 +04:00
|
|
|
/**
|
|
|
|
* remove keys from session
|
|
|
|
*/
|
|
|
|
public function removeKeys() {
|
2014-11-25 18:15:32 +03:00
|
|
|
\OC::$server->getSession()->remove('publicSharePrivateKey');
|
|
|
|
\OC::$server->getSession()->remove('privateKey');
|
2014-08-07 17:26:09 +04:00
|
|
|
}
|
|
|
|
|
2013-09-02 13:26:11 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Sets status of encryption app
|
2014-05-16 00:47:28 +04:00
|
|
|
* @param string $init INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
|
2013-09-02 13:26:11 +04:00
|
|
|
* @return bool
|
2013-09-06 14:27:40 +04:00
|
|
|
*
|
2013-09-02 13:34:28 +04:00
|
|
|
* @note this doesn not indicate of the init was successful, we just remeber the try!
|
2013-09-02 13:26:11 +04:00
|
|
|
*/
|
|
|
|
public function setInitialized($init) {
|
|
|
|
|
2014-07-16 21:40:22 +04:00
|
|
|
\OC::$server->getSession()->set('encryptionInitialized', $init);
|
2013-09-02 13:26:11 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-26 20:18:38 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* remove encryption keys and init status from session
|
2014-02-26 20:18:38 +04:00
|
|
|
*/
|
|
|
|
public function closeSession() {
|
2014-07-16 21:40:22 +04:00
|
|
|
\OC::$server->getSession()->remove('encryptionInitialized');
|
|
|
|
\OC::$server->getSession()->remove('privateKey');
|
2014-02-26 20:18:38 +04:00
|
|
|
}
|
|
|
|
|
2013-09-02 13:26:11 +04:00
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Gets status if we already tried to initialize the encryption app
|
2014-05-13 15:29:25 +04:00
|
|
|
* @return string init status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
|
2013-09-02 13:26:11 +04:00
|
|
|
*
|
2013-09-02 13:34:28 +04:00
|
|
|
* @note this doesn not indicate of the init was successful, we just remeber the try!
|
2013-09-02 13:26:11 +04:00
|
|
|
*/
|
|
|
|
public function getInitialized() {
|
2014-07-16 21:40:22 +04:00
|
|
|
if (!is_null(\OC::$server->getSession()->get('encryptionInitialized'))) {
|
|
|
|
return \OC::$server->getSession()->get('encryptionInitialized');
|
2014-12-03 18:52:44 +03:00
|
|
|
} else if (Helper::isPublicAccess() && self::getPublicSharePrivateKey()) {
|
2014-11-25 12:12:30 +03:00
|
|
|
return self::INIT_SUCCESSFUL;
|
2013-09-02 13:26:11 +04:00
|
|
|
} else {
|
2013-09-06 14:27:40 +04:00
|
|
|
return self::NOT_INITIALIZED;
|
2013-09-02 13:26:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-11 19:10:56 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Gets user or public share private key from session
|
2014-05-13 14:36:01 +04:00
|
|
|
* @return string $privateKey The user's plaintext private key
|
2012-12-11 19:10:56 +04:00
|
|
|
*
|
|
|
|
*/
|
2013-05-24 01:56:31 +04:00
|
|
|
public function getPrivateKey() {
|
2013-05-28 11:27:04 +04:00
|
|
|
// return the public share private key if this is a public access
|
2014-12-03 18:52:44 +03:00
|
|
|
if (Helper::isPublicAccess()) {
|
2014-11-25 12:12:30 +03:00
|
|
|
return self::getPublicSharePrivateKey();
|
2013-05-28 11:27:04 +04:00
|
|
|
} else {
|
2014-07-16 21:40:22 +04:00
|
|
|
if (!is_null(\OC::$server->getSession()->get('privateKey'))) {
|
|
|
|
return \OC::$server->getSession()->get('privateKey');
|
2013-05-28 11:27:04 +04:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2013-05-28 11:27:04 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Sets public user private key to session
|
2013-05-28 11:27:04 +04:00
|
|
|
* @param string $privateKey
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-11-25 12:12:30 +03:00
|
|
|
private static function setPublicSharePrivateKey($privateKey) {
|
|
|
|
self::$publicShareKey = $privateKey;
|
2013-05-28 11:27:04 +04:00
|
|
|
return true;
|
|
|
|
}
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2013-05-28 11:27:04 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Gets public share private key from session
|
2014-05-13 14:36:01 +04:00
|
|
|
* @return string $privateKey
|
2013-05-28 11:27:04 +04:00
|
|
|
*
|
|
|
|
*/
|
2014-11-25 12:12:30 +03:00
|
|
|
private static function getPublicSharePrivateKey() {
|
|
|
|
return self::$publicShareKey;
|
2012-12-11 19:10:56 +04:00
|
|
|
}
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2013-05-28 03:04:09 +04:00
|
|
|
}
|