2015-03-26 11:32:08 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Clark Tomlinson <fallen013@gmail.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-04-09 11:54:53 +03:00
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
2015-04-07 18:02:49 +03:00
|
|
|
*
|
|
|
|
* @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/>
|
2015-03-26 11:32:08 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Encryption\Crypto;
|
|
|
|
|
|
|
|
|
2015-04-01 16:41:31 +03:00
|
|
|
use OCA\Encryption\Util;
|
2015-03-26 11:32:08 +03:00
|
|
|
use OCP\Encryption\IEncryptionModule;
|
2015-03-26 14:23:36 +03:00
|
|
|
use OCA\Encryption\KeyManager;
|
2015-03-26 11:32:08 +03:00
|
|
|
|
|
|
|
class Encryption implements IEncryptionModule {
|
|
|
|
|
2015-04-01 16:42:08 +03:00
|
|
|
const ID = 'OC_DEFAULT_MODULE';
|
2015-03-26 14:23:36 +03:00
|
|
|
|
2015-03-26 11:32:08 +03:00
|
|
|
/**
|
|
|
|
* @var Crypt
|
|
|
|
*/
|
|
|
|
private $crypt;
|
|
|
|
|
2015-03-26 14:23:36 +03:00
|
|
|
/** @var string */
|
|
|
|
private $cipher;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $path;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $fileKey;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $writeCache;
|
|
|
|
|
|
|
|
/** @var KeyManager */
|
2015-04-01 16:41:31 +03:00
|
|
|
private $keyManager;
|
2015-03-26 14:23:36 +03:00
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
private $accessList;
|
|
|
|
|
|
|
|
/** @var boolean */
|
|
|
|
private $isWriteOperation;
|
|
|
|
|
2015-04-01 16:41:31 +03:00
|
|
|
/** @var Util */
|
2015-03-28 13:02:26 +03:00
|
|
|
private $util;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param \OCA\Encryption\Crypto\Crypt $crypt
|
2015-04-01 16:41:31 +03:00
|
|
|
* @param KeyManager $keyManager
|
|
|
|
* @param Util $util
|
2015-03-28 13:02:26 +03:00
|
|
|
*/
|
2015-04-01 16:41:31 +03:00
|
|
|
public function __construct(Crypt $crypt, KeyManager $keyManager, Util $util) {
|
2015-03-26 11:32:08 +03:00
|
|
|
$this->crypt = $crypt;
|
2015-04-01 16:41:31 +03:00
|
|
|
$this->keyManager = $keyManager;
|
2015-03-28 13:02:26 +03:00
|
|
|
$this->util = $util;
|
2015-03-26 11:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string defining the technical unique id
|
|
|
|
*/
|
|
|
|
public function getId() {
|
2015-03-26 14:23:36 +03:00
|
|
|
return self::ID;
|
2015-03-26 11:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In comparison to getKey() this function returns a human readable (maybe translated) name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDisplayName() {
|
|
|
|
return 'ownCloud Default Encryption';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start receiving chunks from a file. This is the place where you can
|
|
|
|
* perform some initial step before starting encrypting/decrypting the
|
|
|
|
* chunks
|
|
|
|
*
|
|
|
|
* @param string $path to the file
|
2015-03-26 14:23:36 +03:00
|
|
|
* @param string $user who read/write the file
|
2015-03-26 11:32:08 +03:00
|
|
|
* @param array $header contains the header data read from the file
|
|
|
|
* @param array $accessList who has access to the file contains the key 'users' and 'public'
|
|
|
|
*
|
2015-03-27 03:35:36 +03:00
|
|
|
* @return array $header contain data as key-value pairs which should be
|
2015-03-26 11:32:08 +03:00
|
|
|
* written to the header, in case of a write operation
|
|
|
|
* or if no additional data is needed return a empty array
|
|
|
|
*/
|
2015-03-26 14:23:36 +03:00
|
|
|
public function begin($path, $user, $header, $accessList) {
|
|
|
|
|
|
|
|
if (isset($header['cipher'])) {
|
|
|
|
$this->cipher = $header['cipher'];
|
|
|
|
} else {
|
|
|
|
$this->cipher = $this->crypt->getCipher();
|
|
|
|
}
|
|
|
|
|
2015-04-02 16:29:10 +03:00
|
|
|
$this->path = $this->getPathToRealFile($path);
|
2015-03-26 14:23:36 +03:00
|
|
|
$this->accessList = $accessList;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->writeCache = '';
|
|
|
|
$this->isWriteOperation = false;
|
2015-03-26 11:32:08 +03:00
|
|
|
|
2015-04-02 16:29:10 +03:00
|
|
|
$this->fileKey = $this->keyManager->getFileKey($this->path, $this->user);
|
2015-03-26 14:23:36 +03:00
|
|
|
|
|
|
|
return array('cipher' => $this->cipher);
|
2015-03-26 11:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* last chunk received. This is the place where you can perform some final
|
|
|
|
* operation and return some remaining data if something is left in your
|
|
|
|
* buffer.
|
|
|
|
*
|
|
|
|
* @param string $path to the file
|
|
|
|
* @return string remained data which should be written to the file in case
|
|
|
|
* of a write operation
|
|
|
|
*/
|
|
|
|
public function end($path) {
|
2015-03-26 14:23:36 +03:00
|
|
|
$result = '';
|
|
|
|
if ($this->isWriteOperation) {
|
|
|
|
if (!empty($this->writeCache)) {
|
|
|
|
$result = $this->crypt->symmetricEncryptFileContent($this->writeCache, $this->fileKey);
|
|
|
|
$this->writeCache = '';
|
|
|
|
}
|
|
|
|
$publicKeys = array();
|
2015-03-26 15:37:14 +03:00
|
|
|
foreach ($this->accessList['users'] as $uid) {
|
2015-04-01 16:41:31 +03:00
|
|
|
$publicKeys[$uid] = $this->keyManager->getPublicKey($uid);
|
2015-03-26 14:23:36 +03:00
|
|
|
}
|
|
|
|
|
2015-04-01 16:41:31 +03:00
|
|
|
$publicKeys = $this->keyManager->addSystemKeys($this->accessList, $publicKeys);
|
2015-03-31 18:13:36 +03:00
|
|
|
|
2015-03-26 15:37:14 +03:00
|
|
|
$encryptedKeyfiles = $this->crypt->multiKeyEncrypt($this->fileKey, $publicKeys);
|
2015-04-02 16:29:10 +03:00
|
|
|
$this->keyManager->setAllFileKeys($this->path, $encryptedKeyfiles);
|
2015-03-26 14:23:36 +03:00
|
|
|
}
|
|
|
|
return $result;
|
2015-03-26 11:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* encrypt data
|
|
|
|
*
|
|
|
|
* @param string $data you want to encrypt
|
|
|
|
* @return mixed encrypted data
|
|
|
|
*/
|
|
|
|
public function encrypt($data) {
|
2015-03-26 14:23:36 +03:00
|
|
|
$this->isWriteOperation = true;
|
|
|
|
if (empty($this->fileKey)) {
|
|
|
|
$this->fileKey = $this->crypt->generateFileKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If extra data is left over from the last round, make sure it
|
|
|
|
// is integrated into the next 6126 / 8192 block
|
|
|
|
if ($this->writeCache) {
|
|
|
|
|
|
|
|
// Concat writeCache to start of $data
|
|
|
|
$data = $this->writeCache . $data;
|
|
|
|
|
|
|
|
// Clear the write cache, ready for reuse - it has been
|
|
|
|
// flushed and its old contents processed
|
|
|
|
$this->writeCache = '';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$encrypted = '';
|
|
|
|
// While there still remains some data to be processed & written
|
|
|
|
while (strlen($data) > 0) {
|
|
|
|
|
|
|
|
// Remaining length for this iteration, not of the
|
|
|
|
// entire file (may be greater than 8192 bytes)
|
|
|
|
$remainingLength = strlen($data);
|
|
|
|
|
|
|
|
// If data remaining to be written is less than the
|
|
|
|
// size of 1 6126 byte block
|
|
|
|
if ($remainingLength < 6126) {
|
|
|
|
|
|
|
|
// Set writeCache to contents of $data
|
|
|
|
// The writeCache will be carried over to the
|
|
|
|
// next write round, and added to the start of
|
|
|
|
// $data to ensure that written blocks are
|
|
|
|
// always the correct length. If there is still
|
|
|
|
// data in writeCache after the writing round
|
|
|
|
// has finished, then the data will be written
|
|
|
|
// to disk by $this->flush().
|
|
|
|
$this->writeCache = $data;
|
|
|
|
|
|
|
|
// Clear $data ready for next round
|
|
|
|
$data = '';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Read the chunk from the start of $data
|
|
|
|
$chunk = substr($data, 0, 6126);
|
|
|
|
|
|
|
|
$encrypted .= $this->crypt->symmetricEncryptFileContent($chunk, $this->fileKey);
|
|
|
|
|
|
|
|
// Remove the chunk we just processed from
|
|
|
|
// $data, leaving only unprocessed data in $data
|
|
|
|
// var, for handling on the next round
|
|
|
|
$data = substr($data, 6126);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $encrypted;
|
2015-03-26 11:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* decrypt data
|
|
|
|
*
|
|
|
|
* @param string $data you want to decrypt
|
|
|
|
* @return mixed decrypted data
|
|
|
|
*/
|
2015-03-26 14:23:36 +03:00
|
|
|
public function decrypt($data) {
|
|
|
|
$result = '';
|
|
|
|
if (!empty($data)) {
|
|
|
|
$result = $this->crypt->symmetricDecryptFileContent($data, $this->fileKey);
|
|
|
|
}
|
|
|
|
return $result;
|
2015-03-26 11:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update encrypted file, e.g. give additional users access to the file
|
|
|
|
*
|
|
|
|
* @param string $path path to the file which should be updated
|
2015-03-27 13:43:02 +03:00
|
|
|
* @param string $uid of the user who performs the operation
|
2015-03-26 11:32:08 +03:00
|
|
|
* @param array $accessList who has access to the file contains the key 'users' and 'public'
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-03-27 13:43:02 +03:00
|
|
|
public function update($path, $uid, $accessList) {
|
2015-04-01 16:41:31 +03:00
|
|
|
$fileKey = $this->keyManager->getFileKey($path, $uid);
|
2015-03-27 20:10:32 +03:00
|
|
|
$publicKeys = array();
|
2015-03-27 13:43:02 +03:00
|
|
|
foreach ($accessList['users'] as $user) {
|
2015-04-01 16:41:31 +03:00
|
|
|
$publicKeys[$user] = $this->keyManager->getPublicKey($user);
|
2015-03-27 13:43:02 +03:00
|
|
|
}
|
2015-03-27 20:10:32 +03:00
|
|
|
|
2015-04-01 16:41:31 +03:00
|
|
|
$publicKeys = $this->keyManager->addSystemKeys($accessList, $publicKeys);
|
2015-03-27 20:10:32 +03:00
|
|
|
|
2015-03-27 13:43:02 +03:00
|
|
|
$encryptedFileKey = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys);
|
|
|
|
|
2015-04-01 16:41:31 +03:00
|
|
|
$this->keyManager->deleteAllFileKeys($path);
|
2015-03-27 13:43:02 +03:00
|
|
|
|
2015-04-01 16:41:31 +03:00
|
|
|
$this->keyManager->setAllFileKeys($path, $encryptedFileKey);
|
2015-03-27 13:43:02 +03:00
|
|
|
|
|
|
|
return true;
|
2015-03-26 11:32:08 +03:00
|
|
|
}
|
|
|
|
|
2015-03-28 13:02:26 +03:00
|
|
|
/**
|
|
|
|
* add system keys such as the public share key and the recovery key
|
|
|
|
*
|
|
|
|
* @param array $accessList
|
|
|
|
* @param array $publicKeys
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function addSystemKeys(array $accessList, array $publicKeys) {
|
|
|
|
if (!empty($accessList['public'])) {
|
2015-04-01 16:41:31 +03:00
|
|
|
$publicKeys[$this->keyManager->getPublicShareKeyId()] = $this->keyManager->getPublicShareKey();
|
2015-03-28 13:02:26 +03:00
|
|
|
}
|
|
|
|
|
2015-04-01 16:41:31 +03:00
|
|
|
if ($this->keyManager->recoveryKeyExists() &&
|
2015-04-08 10:29:35 +03:00
|
|
|
$this->util->isRecoveryEnabledForUser()) {
|
2015-03-28 13:02:26 +03:00
|
|
|
|
2015-04-01 16:41:31 +03:00
|
|
|
$publicKeys[$this->keyManager->getRecoveryKeyId()] = $this->keyManager->getRecoveryKey();
|
2015-03-28 13:02:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $publicKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-26 11:32:08 +03:00
|
|
|
/**
|
|
|
|
* should the file be encrypted or not
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function shouldEncrypt($path) {
|
2015-04-01 14:58:50 +03:00
|
|
|
$parts = explode('/', $path);
|
|
|
|
if (count($parts) < 3) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-01 16:41:31 +03:00
|
|
|
if ($parts[2] == 'files') {
|
2015-04-01 14:58:50 +03:00
|
|
|
return true;
|
|
|
|
}
|
2015-04-01 16:41:31 +03:00
|
|
|
if ($parts[2] == 'files_versions') {
|
2015-04-01 14:58:50 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-03-26 11:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* calculate unencrypted size
|
|
|
|
*
|
|
|
|
* @param string $path to file
|
|
|
|
* @return integer unencrypted size
|
|
|
|
*/
|
|
|
|
public function calculateUnencryptedSize($path) {
|
|
|
|
// TODO: Implement calculateUnencryptedSize() method.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get size of the unencrypted payload per block.
|
|
|
|
* ownCloud read/write files with a block size of 8192 byte
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getUnencryptedBlockSize() {
|
2015-03-26 14:23:36 +03:00
|
|
|
return 6126;
|
2015-03-26 11:32:08 +03:00
|
|
|
}
|
2015-04-02 16:29:10 +03:00
|
|
|
|
2015-04-09 11:54:53 +03:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-02 16:29:10 +03:00
|
|
|
protected function getPathToRealFile($path) {
|
|
|
|
$realPath = $path;
|
|
|
|
$parts = explode('/', $path);
|
|
|
|
if ($parts[2] === 'files_versions') {
|
|
|
|
$realPath = '/' . $parts[1] . '/files/' . implode('/', array_slice($parts, 3));
|
|
|
|
$length = strrpos($realPath, '.');
|
|
|
|
$realPath = substr($realPath, 0, $length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $realPath;
|
|
|
|
}
|
2015-03-26 11:32:08 +03:00
|
|
|
}
|