2015-03-31 12:15:47 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-22 10:45:36 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
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>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-31 12:15:47 +03:00
|
|
|
*
|
2015-04-07 18:02:49 +03:00
|
|
|
* @license AGPL-3.0
|
2015-03-31 12:15:47 +03:00
|
|
|
*
|
2015-04-07 18:02:49 +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.
|
2015-03-31 12:15:47 +03:00
|
|
|
*
|
2015-04-07 18:02:49 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2015-03-31 12:15:47 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-04-07 18:02:49 +03:00
|
|
|
* 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/>
|
2015-03-31 12:15:47 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Encryption;
|
|
|
|
|
2015-08-24 13:03:53 +03:00
|
|
|
use OCA\Encryption\Exceptions\PrivateKeyMissingException;
|
2015-03-31 12:15:47 +03:00
|
|
|
use \OCP\ISession;
|
|
|
|
|
|
|
|
class Session {
|
|
|
|
|
|
|
|
/** @var ISession */
|
|
|
|
protected $session;
|
|
|
|
|
|
|
|
const NOT_INITIALIZED = '0';
|
|
|
|
const INIT_EXECUTED = '1';
|
|
|
|
const INIT_SUCCESSFUL = '2';
|
2015-05-27 12:10:06 +03:00
|
|
|
const RUN_MIGRATION = '3';
|
2015-03-31 12:15:47 +03:00
|
|
|
|
2015-04-09 11:54:53 +03:00
|
|
|
/**
|
|
|
|
* @param ISession $session
|
|
|
|
*/
|
2015-03-31 12:15:47 +03:00
|
|
|
public function __construct(ISession $session) {
|
|
|
|
$this->session = $session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets status of encryption app
|
|
|
|
*
|
|
|
|
* @param string $status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
|
|
|
|
*/
|
|
|
|
public function setStatus($status) {
|
|
|
|
$this->session->set('encryptionInitialized', $status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets status if we already tried to initialize the encryption app
|
|
|
|
*
|
|
|
|
* @return string init status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
|
|
|
|
*/
|
|
|
|
public function getStatus() {
|
|
|
|
$status = $this->session->get('encryptionInitialized');
|
2015-03-31 19:21:30 +03:00
|
|
|
if (is_null($status)) {
|
2015-03-31 12:15:47 +03:00
|
|
|
$status = self::NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets user or public share private key from session
|
|
|
|
*
|
|
|
|
* @return string $privateKey The user's plaintext private key
|
|
|
|
* @throws Exceptions\PrivateKeyMissingException
|
|
|
|
*/
|
|
|
|
public function getPrivateKey() {
|
|
|
|
$key = $this->session->get('privateKey');
|
2015-03-31 19:21:30 +03:00
|
|
|
if (is_null($key)) {
|
2015-04-01 18:46:33 +03:00
|
|
|
throw new Exceptions\PrivateKeyMissingException('please try to log-out and log-in again', 0);
|
2015-03-31 12:15:47 +03:00
|
|
|
}
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if private key is set
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isPrivateKeySet() {
|
|
|
|
$key = $this->session->get('privateKey');
|
2015-03-31 19:21:30 +03:00
|
|
|
if (is_null($key)) {
|
2015-03-31 12:15:47 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets user private key to session
|
|
|
|
*
|
|
|
|
* @param string $key users private key
|
|
|
|
*
|
|
|
|
* @note this should only be set on login
|
|
|
|
*/
|
|
|
|
public function setPrivateKey($key) {
|
|
|
|
$this->session->set('privateKey', $key);
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:03:53 +03:00
|
|
|
/**
|
|
|
|
* store data needed for the decrypt all operation in the session
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @param string $key
|
|
|
|
*/
|
|
|
|
public function prepareDecryptAll($user, $key) {
|
|
|
|
$this->session->set('decryptAll', true);
|
|
|
|
$this->session->set('decryptAllKey', $key);
|
|
|
|
$this->session->set('decryptAllUid', $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if we are in decrypt all mode
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function decryptAllModeActivated() {
|
|
|
|
$decryptAll = $this->session->get('decryptAll');
|
|
|
|
return ($decryptAll === true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get uid used for decrypt all operation
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function getDecryptAllUid() {
|
|
|
|
$uid = $this->session->get('decryptAllUid');
|
|
|
|
if (is_null($uid) && $this->decryptAllModeActivated()) {
|
|
|
|
throw new \Exception('No uid found while in decrypt all mode');
|
|
|
|
} elseif (is_null($uid)) {
|
|
|
|
throw new \Exception('Please activate decrypt all mode first');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get private key for decrypt all operation
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws PrivateKeyMissingException
|
|
|
|
*/
|
|
|
|
public function getDecryptAllKey() {
|
|
|
|
$privateKey = $this->session->get('decryptAllKey');
|
|
|
|
if (is_null($privateKey) && $this->decryptAllModeActivated()) {
|
|
|
|
throw new PrivateKeyMissingException('No private key found while in decrypt all mode');
|
|
|
|
} elseif (is_null($privateKey)) {
|
|
|
|
throw new PrivateKeyMissingException('Please activate decrypt all mode first');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $privateKey;
|
|
|
|
}
|
2015-03-31 13:01:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* remove keys from session
|
|
|
|
*/
|
|
|
|
public function clear() {
|
|
|
|
$this->session->remove('publicSharePrivateKey');
|
|
|
|
$this->session->remove('privateKey');
|
|
|
|
$this->session->remove('encryptionInitialized');
|
2015-08-24 13:03:53 +03:00
|
|
|
$this->session->remove('decryptAll');
|
|
|
|
$this->session->remove('decryptAllKey');
|
|
|
|
$this->session->remove('decryptAllUid');
|
2015-03-31 13:01:51 +03:00
|
|
|
}
|
|
|
|
|
2015-03-31 19:17:01 +03:00
|
|
|
}
|