nextcloud/apps/files_encryption/lib/session.php

199 lines
5.3 KiB
PHP
Raw Normal View History

<?php
/**
* ownCloud
*
2014-11-14 14:20:59 +03:00
* @copyright (C) 2014 ownCloud, Inc.
*
* @author Sam Tuke <samtuke@owncloud.com>
* @author Bjoern Schiessle <schiessle@owncloud.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Files_Encryption;
/**
* Class for handling encryption related session data
*/
2013-05-27 19:26:58 +04:00
class Session {
private $view;
private static $publicShareKey = false;
2013-05-20 03:24:36 +04:00
const NOT_INITIALIZED = '0';
const INIT_EXECUTED = '1';
const INIT_SUCCESSFUL = '2';
/**
* if session is started, check if ownCloud key pair is set up, if not create it
* @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-05-27 19:26:58 +04:00
public function __construct($view) {
2013-05-20 03:24:36 +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
}
2014-02-13 19:28:49 +04:00
$appConfig = \OC::$server->getAppConfig();
$publicShareKeyId = Helper::getPublicShareKeyId();
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-20 03:24:36 +04:00
if (!Keymanager::publicShareKeyExists($view)) {
2013-05-20 03:24:36 +04:00
$keypair = Crypt::createKeypair();
// Save public key
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-07-21 15:02:28 +04:00
$cipher = \OCA\Encryption\Helper::getCipher();
$encryptedKey = \OCA\Encryption\Crypt::symmetricEncryptFileContent($keypair['privateKey'], '', $cipher);
if ($encryptedKey) {
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
}
if (\OCA\Encryption\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;
$encryptedKey = Keymanager::getPrivateSystemKey($publicShareKeyId);
$privateKey = Crypt::decryptPrivateKey($encryptedKey, '');
self::setPublicSharePrivateKey($privateKey);
2013-05-20 03:24:36 +04:00
\OC_FileProxy::$enabled = $proxyStatus;
}
}
/**
* Sets user private key to session
2013-05-20 00:31:00 +04:00
* @param string $privateKey
* @return bool
*
* @note this should only be set on login
*/
2013-05-27 19:26:58 +04:00
public function setPrivateKey($privateKey) {
2013-05-20 03:24:36 +04:00
\OC::$server->getSession()->set('privateKey', $privateKey);
2013-05-20 03:24:36 +04:00
return true;
2013-05-20 03:24:36 +04:00
}
2013-05-20 03:24:36 +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');
}
/**
* Sets status of encryption app
2014-05-16 00:47:28 +04:00
* @param string $init INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
* @return bool
*
2013-09-02 13:34:28 +04:00
* @note this doesn not indicate of the init was successful, we just remeber the try!
*/
public function setInitialized($init) {
\OC::$server->getSession()->set('encryptionInitialized', $init);
return true;
}
/**
* remove encryption keys and init status from session
*/
public function closeSession() {
\OC::$server->getSession()->remove('encryptionInitialized');
\OC::$server->getSession()->remove('privateKey');
}
/**
* 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:34:28 +04:00
* @note this doesn not indicate of the init was successful, we just remeber the try!
*/
public function getInitialized() {
if (!is_null(\OC::$server->getSession()->get('encryptionInitialized'))) {
return \OC::$server->getSession()->get('encryptionInitialized');
} else if (\OCA\Encryption\Helper::isPublicAccess() && self::getPublicSharePrivateKey()) {
return self::INIT_SUCCESSFUL;
} else {
return self::NOT_INITIALIZED;
}
}
/**
* 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
*
*/
2013-05-24 01:56:31 +04:00
public function getPrivateKey() {
// return the public share private key if this is a public access
if (\OCA\Encryption\Helper::isPublicAccess()) {
return self::getPublicSharePrivateKey();
} else {
if (!is_null(\OC::$server->getSession()->get('privateKey'))) {
return \OC::$server->getSession()->get('privateKey');
} else {
return false;
}
}
}
2013-05-20 03:24:36 +04:00
/**
* Sets public user private key to session
* @param string $privateKey
* @return bool
*/
private static function setPublicSharePrivateKey($privateKey) {
self::$publicShareKey = $privateKey;
return true;
}
2013-05-20 03:24:36 +04:00
/**
* Gets public share private key from session
2014-05-13 14:36:01 +04:00
* @return string $privateKey
*
*/
private static function getPublicSharePrivateKey() {
return self::$publicShareKey;
}
2013-05-20 03:24:36 +04:00
2013-05-28 03:04:09 +04:00
}