Welcome to mirror list, hosted at ThFree Co, Russian Federation.

session.php « lib « files_encryption « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 10e4c061b3034960b0a92ec55ab0401afd668d54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
 * @author Björn Schießle <schiessle@owncloud.com>
 * @author Florin Peter <github@florin-peter.de>
 * @author Joas Schilling <nickvergessen@owncloud.com>
 * @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>
 *
 * @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\Files_Encryption;

/**
 * Class for handling encryption related session data
 */

class Session {

	private $view;
	private static $publicShareKey = false;

	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
	 *
	 * @note The ownCloud key pair is used to allow public link sharing even if encryption is enabled
	 */
	public function __construct($view) {

		$this->view = $view;

		if (!$this->view->is_dir('files_encryption')) {

			$this->view->mkdir('files_encryption');

		}

		$appConfig = \OC::$server->getAppConfig();

		$publicShareKeyId = Helper::getPublicShareKeyId();

		if ($publicShareKeyId === false) {
			$publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8);
			$appConfig->setValue('files_encryption', 'publicShareKeyId', $publicShareKeyId);
		}

		if (!Keymanager::publicShareKeyExists($view)) {

			$keypair = Crypt::createKeypair();


			// Save public key
			Keymanager::setPublicKey($keypair['publicKey'], $publicShareKeyId);

			// Encrypt private key empty passphrase
			$cipher = Helper::getCipher();
			$encryptedKey = Crypt::symmetricEncryptFileContent($keypair['privateKey'], '', $cipher);
			if ($encryptedKey) {
				Keymanager::setPrivateSystemKey($encryptedKey, $publicShareKeyId);
			} else {
				\OCP\Util::writeLog('files_encryption', 'Could not create public share keys', \OCP\Util::ERROR);
			}

		}

		if (Helper::isPublicAccess() && !self::getPublicSharePrivateKey()) {
			// 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);

			\OC_FileProxy::$enabled = $proxyStatus;
		}
	}

	/**
	 * Sets user private key to session
	 * @param string $privateKey
	 * @return bool
	 *
	 * @note this should only be set on login
	 */
	public function setPrivateKey($privateKey) {

		\OC::$server->getSession()->set('privateKey', $privateKey);

		return true;

	}

	/**
	 * remove keys from session
	 */
	public function removeKeys() {
		\OC::$server->getSession()->remove('publicSharePrivateKey');
		\OC::$server->getSession()->remove('privateKey');
	}

	/**
	 * Sets status of encryption app
	 * @param string $init INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
	 * @return bool
	 *
	 * @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
	 * @return string init status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
	 *
	 * @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 (Helper::isPublicAccess() && self::getPublicSharePrivateKey()) {
			return self::INIT_SUCCESSFUL;
		} else {
			return self::NOT_INITIALIZED;
		}
	}

	/**
	 * Gets user or public share private key from session
	 * @return string $privateKey The user's plaintext private key
	 *
	 */
	public function getPrivateKey() {
		// return the public share private key if this is a public access
		if (Helper::isPublicAccess()) {
			return self::getPublicSharePrivateKey();
		} else {
			if (!is_null(\OC::$server->getSession()->get('privateKey'))) {
				return \OC::$server->getSession()->get('privateKey');
			} else {
				return false;
			}
		}
	}

	/**
	 * Sets public user private key to session
	 * @param string $privateKey
	 * @return bool
	 */
	private static function setPublicSharePrivateKey($privateKey) {
		self::$publicShareKey = $privateKey;
		return true;
	}

	/**
	 * Gets public share private key from session
	 * @return string $privateKey
	 *
	 */
	private static function getPublicSharePrivateKey() {
		return self::$publicShareKey;
	}

}