2015-03-26 08:32:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2015-04-07 15:02:49 +00:00
|
|
|
* @author Clark Tomlinson <fallen013@gmail.com>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Jan-Christoph Borchardt <hey@jancborchardt.net>
|
2016-07-21 14:49:16 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-04-07 15:02:49 +00:00
|
|
|
*
|
|
|
|
* @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 08:32:08 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Encryption\Crypto;
|
|
|
|
|
|
|
|
|
2015-05-18 11:40:23 +00:00
|
|
|
use OC\Encryption\Exceptions\DecryptionFailedException;
|
2016-02-10 10:08:03 +00:00
|
|
|
use OC\Files\Cache\Scanner;
|
2016-02-09 21:27:23 +00:00
|
|
|
use OC\Files\View;
|
2015-04-13 13:56:36 +00:00
|
|
|
use OCA\Encryption\Exceptions\PublicKeyMissingException;
|
2015-08-24 10:03:53 +00:00
|
|
|
use OCA\Encryption\Session;
|
2015-04-01 13:41:31 +00:00
|
|
|
use OCA\Encryption\Util;
|
2015-03-26 08:32:08 +00:00
|
|
|
use OCP\Encryption\IEncryptionModule;
|
2015-03-26 11:23:36 +00:00
|
|
|
use OCA\Encryption\KeyManager;
|
2015-05-18 11:09:36 +00:00
|
|
|
use OCP\IL10N;
|
2015-04-28 15:29:10 +00:00
|
|
|
use OCP\ILogger;
|
2015-08-24 13:56:04 +00:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2015-03-26 08:32:08 +00:00
|
|
|
|
|
|
|
class Encryption implements IEncryptionModule {
|
|
|
|
|
2015-04-01 13:42:08 +00:00
|
|
|
const ID = 'OC_DEFAULT_MODULE';
|
2015-05-05 20:08:16 +00:00
|
|
|
const DISPLAY_NAME = 'Default encryption module';
|
2015-03-26 11:23:36 +00:00
|
|
|
|
2015-03-26 08:32:08 +00:00
|
|
|
/**
|
|
|
|
* @var Crypt
|
|
|
|
*/
|
|
|
|
private $crypt;
|
|
|
|
|
2015-03-26 11:23:36 +00:00
|
|
|
/** @var string */
|
|
|
|
private $cipher;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $path;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $user;
|
|
|
|
|
2017-07-17 11:13:27 +00:00
|
|
|
/** @var array */
|
2017-07-06 09:06:48 +00:00
|
|
|
private $owner;
|
|
|
|
|
2015-03-26 11:23:36 +00:00
|
|
|
/** @var string */
|
|
|
|
private $fileKey;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $writeCache;
|
|
|
|
|
|
|
|
/** @var KeyManager */
|
2015-04-01 13:41:31 +00:00
|
|
|
private $keyManager;
|
2015-03-26 11:23:36 +00:00
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
private $accessList;
|
|
|
|
|
|
|
|
/** @var boolean */
|
|
|
|
private $isWriteOperation;
|
|
|
|
|
2015-04-01 13:41:31 +00:00
|
|
|
/** @var Util */
|
2015-03-28 10:02:26 +00:00
|
|
|
private $util;
|
|
|
|
|
2015-08-24 10:03:53 +00:00
|
|
|
/** @var Session */
|
|
|
|
private $session;
|
|
|
|
|
2015-04-28 15:29:10 +00:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
2015-05-18 11:09:36 +00:00
|
|
|
/** @var IL10N */
|
|
|
|
private $l;
|
|
|
|
|
2015-08-24 13:56:04 +00:00
|
|
|
/** @var EncryptAll */
|
|
|
|
private $encryptAll;
|
|
|
|
|
2015-09-07 09:38:44 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $useMasterPassword;
|
|
|
|
|
2015-08-24 10:03:53 +00:00
|
|
|
/** @var DecryptAll */
|
|
|
|
private $decryptAll;
|
|
|
|
|
2016-01-05 14:29:44 +00:00
|
|
|
/** @var int unencrypted block size if block contains signature */
|
|
|
|
private $unencryptedBlockSizeSigned = 6072;
|
|
|
|
|
2016-01-05 11:51:05 +00:00
|
|
|
/** @var int unencrypted block size */
|
2016-01-05 14:29:44 +00:00
|
|
|
private $unencryptedBlockSize = 6126;
|
|
|
|
|
2016-02-02 19:00:36 +00:00
|
|
|
/** @var int Current version of the file */
|
|
|
|
private $version = 0;
|
|
|
|
|
2016-02-10 11:34:55 +00:00
|
|
|
/** @var array remember encryption signature version */
|
|
|
|
private static $rememberVersion = [];
|
|
|
|
|
2016-01-05 11:51:05 +00:00
|
|
|
|
2015-03-28 10:02:26 +00:00
|
|
|
/**
|
|
|
|
*
|
2015-04-28 15:29:10 +00:00
|
|
|
* @param Crypt $crypt
|
2015-04-01 13:41:31 +00:00
|
|
|
* @param KeyManager $keyManager
|
|
|
|
* @param Util $util
|
2015-08-24 10:03:53 +00:00
|
|
|
* @param Session $session
|
2015-08-24 13:56:04 +00:00
|
|
|
* @param EncryptAll $encryptAll
|
2015-08-24 10:03:53 +00:00
|
|
|
* @param DecryptAll $decryptAll
|
2015-04-28 15:29:10 +00:00
|
|
|
* @param ILogger $logger
|
2015-05-18 11:09:36 +00:00
|
|
|
* @param IL10N $il10n
|
2015-03-28 10:02:26 +00:00
|
|
|
*/
|
2015-04-28 15:29:10 +00:00
|
|
|
public function __construct(Crypt $crypt,
|
|
|
|
KeyManager $keyManager,
|
|
|
|
Util $util,
|
2015-08-24 10:03:53 +00:00
|
|
|
Session $session,
|
2015-08-24 13:56:04 +00:00
|
|
|
EncryptAll $encryptAll,
|
2015-08-24 10:03:53 +00:00
|
|
|
DecryptAll $decryptAll,
|
2015-05-18 11:09:36 +00:00
|
|
|
ILogger $logger,
|
|
|
|
IL10N $il10n) {
|
2015-03-26 08:32:08 +00:00
|
|
|
$this->crypt = $crypt;
|
2015-04-01 13:41:31 +00:00
|
|
|
$this->keyManager = $keyManager;
|
2015-03-28 10:02:26 +00:00
|
|
|
$this->util = $util;
|
2015-08-24 10:03:53 +00:00
|
|
|
$this->session = $session;
|
2015-08-24 13:56:04 +00:00
|
|
|
$this->encryptAll = $encryptAll;
|
2015-08-24 10:03:53 +00:00
|
|
|
$this->decryptAll = $decryptAll;
|
2015-04-28 15:29:10 +00:00
|
|
|
$this->logger = $logger;
|
2015-05-18 11:09:36 +00:00
|
|
|
$this->l = $il10n;
|
2017-07-17 11:13:27 +00:00
|
|
|
$this->owner = [];
|
2015-09-07 09:38:44 +00:00
|
|
|
$this->useMasterPassword = $util->isMasterKeyEnabled();
|
2015-03-26 08:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string defining the technical unique id
|
|
|
|
*/
|
|
|
|
public function getId() {
|
2015-03-26 11:23:36 +00:00
|
|
|
return self::ID;
|
2015-03-26 08:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In comparison to getKey() this function returns a human readable (maybe translated) name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDisplayName() {
|
2015-04-14 14:48:39 +00:00
|
|
|
return self::DISPLAY_NAME;
|
2015-03-26 08:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 11:23:36 +00:00
|
|
|
* @param string $user who read/write the file
|
2015-04-24 11:02:06 +00:00
|
|
|
* @param string $mode php stream open mode
|
2015-03-26 08:32:08 +00: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 00:35:36 +00:00
|
|
|
* @return array $header contain data as key-value pairs which should be
|
2015-03-26 08:32:08 +00:00
|
|
|
* written to the header, in case of a write operation
|
|
|
|
* or if no additional data is needed return a empty array
|
|
|
|
*/
|
2015-04-24 11:02:06 +00:00
|
|
|
public function begin($path, $user, $mode, array $header, array $accessList) {
|
2015-04-28 15:31:03 +00:00
|
|
|
$this->path = $this->getPathToRealFile($path);
|
|
|
|
$this->accessList = $accessList;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->isWriteOperation = false;
|
|
|
|
$this->writeCache = '';
|
|
|
|
|
2016-11-23 11:07:04 +00:00
|
|
|
if($this->session->isReady() === false) {
|
|
|
|
// if the master key is enabled we can initialize encryption
|
|
|
|
// with a empty password and user name
|
|
|
|
if ($this->util->isMasterKeyEnabled()) {
|
|
|
|
$this->keyManager->init('', '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:03:53 +00:00
|
|
|
if ($this->session->decryptAllModeActivated()) {
|
|
|
|
$encryptedFileKey = $this->keyManager->getEncryptedFileKey($this->path);
|
|
|
|
$shareKey = $this->keyManager->getShareKey($this->path, $this->session->getDecryptAllUid());
|
|
|
|
$this->fileKey = $this->crypt->multiKeyDecrypt($encryptedFileKey,
|
|
|
|
$shareKey,
|
|
|
|
$this->session->getDecryptAllKey());
|
|
|
|
} else {
|
|
|
|
$this->fileKey = $this->keyManager->getFileKey($this->path, $this->user);
|
|
|
|
}
|
2015-04-28 15:31:03 +00:00
|
|
|
|
2016-02-10 10:08:03 +00:00
|
|
|
// always use the version from the original file, also part files
|
|
|
|
// need to have a correct version number if they get moved over to the
|
|
|
|
// final location
|
2016-02-10 11:34:55 +00:00
|
|
|
$this->version = (int)$this->keyManager->getVersion($this->stripPartFileExtension($path), new View());
|
2016-02-02 19:00:36 +00:00
|
|
|
|
2015-04-28 15:31:03 +00:00
|
|
|
if (
|
2015-04-24 11:02:06 +00:00
|
|
|
$mode === 'w'
|
|
|
|
|| $mode === 'w+'
|
|
|
|
|| $mode === 'wb'
|
|
|
|
|| $mode === 'wb+'
|
|
|
|
) {
|
2015-04-28 15:31:03 +00:00
|
|
|
$this->isWriteOperation = true;
|
|
|
|
if (empty($this->fileKey)) {
|
|
|
|
$this->fileKey = $this->crypt->generateFileKey();
|
|
|
|
}
|
2016-02-10 10:08:03 +00:00
|
|
|
} else {
|
|
|
|
// if we read a part file we need to increase the version by 1
|
|
|
|
// because the version number was also increased by writing
|
|
|
|
// the part file
|
|
|
|
if(Scanner::isPartialFile($path)) {
|
|
|
|
$this->version = $this->version + 1;
|
|
|
|
}
|
2015-04-28 15:31:03 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 15:34:40 +00:00
|
|
|
if ($this->isWriteOperation) {
|
2015-03-26 11:23:36 +00:00
|
|
|
$this->cipher = $this->crypt->getCipher();
|
2016-01-05 15:34:40 +00:00
|
|
|
} elseif (isset($header['cipher'])) {
|
|
|
|
$this->cipher = $header['cipher'];
|
2015-04-24 11:02:06 +00:00
|
|
|
} else {
|
2015-04-28 15:31:03 +00:00
|
|
|
// if we read a file without a header we fall-back to the legacy cipher
|
|
|
|
// which was used in <=oC6
|
2015-04-24 11:02:06 +00:00
|
|
|
$this->cipher = $this->crypt->getLegacyCipher();
|
2015-03-26 11:23:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 14:29:44 +00:00
|
|
|
return array('cipher' => $this->cipher, 'signed' => 'true');
|
2015-03-26 08:32:08 +00: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
|
2016-02-03 13:32:04 +00:00
|
|
|
* @param int $position
|
2015-03-26 08:32:08 +00:00
|
|
|
* @return string remained data which should be written to the file in case
|
|
|
|
* of a write operation
|
2015-04-13 13:56:36 +00:00
|
|
|
* @throws PublicKeyMissingException
|
|
|
|
* @throws \Exception
|
|
|
|
* @throws \OCA\Encryption\Exceptions\MultiKeyEncryptException
|
2015-03-26 08:32:08 +00:00
|
|
|
*/
|
2016-02-03 13:32:04 +00:00
|
|
|
public function end($path, $position = 0) {
|
2015-03-26 11:23:36 +00:00
|
|
|
$result = '';
|
|
|
|
if ($this->isWriteOperation) {
|
2016-02-10 11:34:55 +00:00
|
|
|
$this->keyManager->setVersion($path, $this->version + 1, new View());
|
|
|
|
// in case of a part file we remember the new signature versions
|
|
|
|
// the version will be set later on update.
|
|
|
|
// This way we make sure that other apps listening to the pre-hooks
|
|
|
|
// still get the old version which should be the correct value for them
|
|
|
|
if (Scanner::isPartialFile($path)) {
|
|
|
|
self::$rememberVersion[$this->stripPartFileExtension($path)] = $this->version + 1;
|
|
|
|
}
|
2015-03-26 11:23:36 +00:00
|
|
|
if (!empty($this->writeCache)) {
|
2016-02-10 10:08:03 +00:00
|
|
|
$result = $this->crypt->symmetricEncryptFileContent($this->writeCache, $this->fileKey, $this->version + 1, $position);
|
2015-03-26 11:23:36 +00:00
|
|
|
$this->writeCache = '';
|
|
|
|
}
|
|
|
|
$publicKeys = array();
|
2015-09-07 09:38:44 +00:00
|
|
|
if ($this->useMasterPassword === true) {
|
|
|
|
$publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey();
|
|
|
|
} else {
|
|
|
|
foreach ($this->accessList['users'] as $uid) {
|
|
|
|
try {
|
|
|
|
$publicKeys[$uid] = $this->keyManager->getPublicKey($uid);
|
|
|
|
} catch (PublicKeyMissingException $e) {
|
|
|
|
$this->logger->warning(
|
|
|
|
'no public key found for user "{uid}", user will not be able to read the file',
|
|
|
|
['app' => 'encryption', 'uid' => $uid]
|
|
|
|
);
|
|
|
|
// if the public key of the owner is missing we should fail
|
|
|
|
if ($uid === $this->user) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2015-04-13 13:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-26 11:23:36 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 11:13:27 +00:00
|
|
|
$publicKeys = $this->keyManager->addSystemKeys($this->accessList, $publicKeys, $this->getOwner($path));
|
2015-03-26 12:37:14 +00:00
|
|
|
$encryptedKeyfiles = $this->crypt->multiKeyEncrypt($this->fileKey, $publicKeys);
|
2015-04-02 13:29:10 +00:00
|
|
|
$this->keyManager->setAllFileKeys($this->path, $encryptedKeyfiles);
|
2015-03-26 11:23:36 +00:00
|
|
|
}
|
|
|
|
return $result;
|
2015-03-26 08:32:08 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 09:06:48 +00:00
|
|
|
|
|
|
|
|
2015-03-26 08:32:08 +00:00
|
|
|
/**
|
|
|
|
* encrypt data
|
|
|
|
*
|
|
|
|
* @param string $data you want to encrypt
|
2016-02-03 13:32:04 +00:00
|
|
|
* @param int $position
|
2015-11-27 15:32:44 +00:00
|
|
|
* @return string encrypted data
|
2015-03-26 08:32:08 +00:00
|
|
|
*/
|
2016-02-03 13:32:04 +00:00
|
|
|
public function encrypt($data, $position = 0) {
|
2015-03-26 11:23:36 +00:00
|
|
|
// If extra data is left over from the last round, make sure it
|
2016-01-05 11:51:05 +00:00
|
|
|
// is integrated into the next block
|
2015-03-26 11:23:36 +00:00
|
|
|
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
|
2016-01-05 14:29:44 +00:00
|
|
|
if ($remainingLength < $this->unencryptedBlockSizeSigned) {
|
2015-03-26 11:23:36 +00:00
|
|
|
|
|
|
|
// 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
|
2016-01-05 14:29:44 +00:00
|
|
|
$chunk = substr($data, 0, $this->unencryptedBlockSizeSigned);
|
2015-03-26 11:23:36 +00:00
|
|
|
|
2016-02-10 10:08:03 +00:00
|
|
|
$encrypted .= $this->crypt->symmetricEncryptFileContent($chunk, $this->fileKey, $this->version + 1, $position);
|
2015-03-26 11:23:36 +00:00
|
|
|
|
|
|
|
// Remove the chunk we just processed from
|
|
|
|
// $data, leaving only unprocessed data in $data
|
|
|
|
// var, for handling on the next round
|
2016-01-05 14:29:44 +00:00
|
|
|
$data = substr($data, $this->unencryptedBlockSizeSigned);
|
2015-03-26 11:23:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $encrypted;
|
2015-03-26 08:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* decrypt data
|
|
|
|
*
|
|
|
|
* @param string $data you want to decrypt
|
2016-02-03 13:32:04 +00:00
|
|
|
* @param int $position
|
2015-11-27 15:32:44 +00:00
|
|
|
* @return string decrypted data
|
2015-05-18 11:40:23 +00:00
|
|
|
* @throws DecryptionFailedException
|
2015-03-26 08:32:08 +00:00
|
|
|
*/
|
2016-02-03 13:32:04 +00:00
|
|
|
public function decrypt($data, $position = 0) {
|
2015-05-12 16:49:25 +00:00
|
|
|
if (empty($this->fileKey)) {
|
2015-06-08 12:43:45 +00:00
|
|
|
$msg = 'Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.';
|
|
|
|
$hint = $this->l->t('Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.');
|
|
|
|
$this->logger->error($msg);
|
2015-05-12 16:49:25 +00:00
|
|
|
|
2015-06-08 12:43:45 +00:00
|
|
|
throw new DecryptionFailedException($msg, $hint);
|
2015-05-12 16:49:25 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 13:32:04 +00:00
|
|
|
return $this->crypt->symmetricDecryptFileContent($data, $this->fileKey, $this->cipher, $this->version, $position);
|
2015-03-26 08:32:08 +00: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 10:43:02 +00:00
|
|
|
* @param string $uid of the user who performs the operation
|
2015-03-26 08:32:08 +00:00
|
|
|
* @param array $accessList who has access to the file contains the key 'users' and 'public'
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-04-09 12:06:55 +00:00
|
|
|
public function update($path, $uid, array $accessList) {
|
2016-02-10 11:34:55 +00:00
|
|
|
|
|
|
|
if (empty($accessList)) {
|
|
|
|
if (isset(self::$rememberVersion[$path])) {
|
|
|
|
$this->keyManager->setVersion($path, self::$rememberVersion[$path], new View());
|
|
|
|
unset(self::$rememberVersion[$path]);
|
|
|
|
}
|
|
|
|
return;
|
2016-02-08 19:35:33 +00:00
|
|
|
}
|
2016-02-10 11:34:55 +00:00
|
|
|
|
|
|
|
$fileKey = $this->keyManager->getFileKey($path, $uid);
|
2015-05-05 20:08:16 +00:00
|
|
|
|
2015-04-28 15:29:10 +00:00
|
|
|
if (!empty($fileKey)) {
|
|
|
|
|
|
|
|
$publicKeys = array();
|
2015-09-07 09:38:44 +00:00
|
|
|
if ($this->useMasterPassword === true) {
|
|
|
|
$publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey();
|
|
|
|
} else {
|
|
|
|
foreach ($accessList['users'] as $user) {
|
2016-03-15 10:23:49 +00:00
|
|
|
try {
|
|
|
|
$publicKeys[$user] = $this->keyManager->getPublicKey($user);
|
|
|
|
} catch (PublicKeyMissingException $e) {
|
|
|
|
$this->logger->warning('Could not encrypt file for ' . $user . ': ' . $e->getMessage());
|
|
|
|
}
|
2015-09-07 09:38:44 +00:00
|
|
|
}
|
2015-04-28 15:29:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 11:13:27 +00:00
|
|
|
$publicKeys = $this->keyManager->addSystemKeys($accessList, $publicKeys, $this->getOwner($path));
|
2015-04-28 15:29:10 +00:00
|
|
|
|
|
|
|
$encryptedFileKey = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys);
|
2015-03-27 17:10:32 +00:00
|
|
|
|
2015-04-28 15:29:10 +00:00
|
|
|
$this->keyManager->deleteAllFileKeys($path);
|
2015-03-27 10:43:02 +00:00
|
|
|
|
2015-04-28 15:29:10 +00:00
|
|
|
$this->keyManager->setAllFileKeys($path, $encryptedFileKey);
|
2015-03-27 10:43:02 +00:00
|
|
|
|
2015-04-28 15:29:10 +00:00
|
|
|
} else {
|
|
|
|
$this->logger->debug('no file key found, we assume that the file "{file}" is not encrypted',
|
|
|
|
array('file' => $path, 'app' => 'encryption'));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-27 10:43:02 +00:00
|
|
|
|
|
|
|
return true;
|
2015-03-26 08:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* should the file be encrypted or not
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function shouldEncrypt($path) {
|
2015-10-13 15:54:06 +00:00
|
|
|
if ($this->util->shouldEncryptHomeStorage() === false) {
|
|
|
|
$storage = $this->util->getStorage($path);
|
|
|
|
if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 11:58:50 +00:00
|
|
|
$parts = explode('/', $path);
|
2015-05-18 10:51:47 +00:00
|
|
|
if (count($parts) < 4) {
|
2015-04-01 11:58:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-10 11:58:15 +00:00
|
|
|
if ($parts[2] === 'files') {
|
2015-04-01 11:58:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-05-10 11:58:15 +00:00
|
|
|
if ($parts[2] === 'files_versions') {
|
2015-04-01 11:58:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-05-10 11:58:15 +00:00
|
|
|
if ($parts[2] === 'files_trashbin') {
|
2015-05-18 10:51:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-04-01 11:58:50 +00:00
|
|
|
|
|
|
|
return false;
|
2015-03-26 08:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get size of the unencrypted payload per block.
|
2017-04-12 04:16:27 +00:00
|
|
|
* Nextcloud read/write files with a block size of 8192 byte
|
2015-03-26 08:32:08 +00:00
|
|
|
*
|
2016-01-05 14:29:44 +00:00
|
|
|
* @param bool $signed
|
|
|
|
* @return int
|
2015-03-26 08:32:08 +00:00
|
|
|
*/
|
2016-01-05 14:29:44 +00:00
|
|
|
public function getUnencryptedBlockSize($signed = false) {
|
|
|
|
if ($signed === false) {
|
|
|
|
return $this->unencryptedBlockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->unencryptedBlockSizeSigned;
|
2015-03-26 08:32:08 +00:00
|
|
|
}
|
2015-04-02 13:29:10 +00:00
|
|
|
|
2015-05-12 16:49:25 +00:00
|
|
|
/**
|
|
|
|
* check if the encryption module is able to read the file,
|
|
|
|
* e.g. if all encryption keys exists
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $uid user for whom we want to check if he can read the file
|
|
|
|
* @return bool
|
|
|
|
* @throws DecryptionFailedException
|
|
|
|
*/
|
|
|
|
public function isReadable($path, $uid) {
|
|
|
|
$fileKey = $this->keyManager->getFileKey($path, $uid);
|
|
|
|
if (empty($fileKey)) {
|
|
|
|
$owner = $this->util->getOwner($path);
|
|
|
|
if ($owner !== $uid) {
|
|
|
|
// if it is a shared file we throw a exception with a useful
|
|
|
|
// error message because in this case it means that the file was
|
|
|
|
// shared with the user at a point where the user didn't had a
|
|
|
|
// valid private/public key
|
|
|
|
$msg = 'Encryption module "' . $this->getDisplayName() .
|
|
|
|
'" is not able to read ' . $path;
|
|
|
|
$hint = $this->l->t('Can not read this file, probably this is a shared file. Please ask the file owner to reshare the file with you.');
|
|
|
|
$this->logger->warning($msg);
|
2015-05-27 08:37:12 +00:00
|
|
|
throw new DecryptionFailedException($msg, $hint);
|
2015-05-12 16:49:25 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:56:04 +00:00
|
|
|
/**
|
|
|
|
* Initial encryption of all files
|
|
|
|
*
|
|
|
|
* @param InputInterface $input
|
|
|
|
* @param OutputInterface $output write some status information to the terminal during encryption
|
|
|
|
*/
|
|
|
|
public function encryptAll(InputInterface $input, OutputInterface $output) {
|
2015-09-15 12:02:10 +00:00
|
|
|
$this->encryptAll->encryptAll($input, $output);
|
2015-08-24 13:56:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 10:03:53 +00:00
|
|
|
/**
|
|
|
|
* prepare module to perform decrypt all operation
|
|
|
|
*
|
|
|
|
* @param InputInterface $input
|
|
|
|
* @param OutputInterface $output
|
|
|
|
* @param string $user
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function prepareDecryptAll(InputInterface $input, OutputInterface $output, $user = '') {
|
|
|
|
return $this->decryptAll->prepare($input, $output, $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-09 08:54:53 +00:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-02 13:29:10 +00: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-05-12 16:49:25 +00:00
|
|
|
|
2016-02-10 10:08:03 +00:00
|
|
|
/**
|
|
|
|
* remove .part file extension and the ocTransferId from the file to get the
|
|
|
|
* original file name
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function stripPartFileExtension($path) {
|
|
|
|
if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
|
|
|
|
$pos = strrpos($path, '.', -6);
|
|
|
|
$path = substr($path, 0, $pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:13:27 +00:00
|
|
|
/**
|
|
|
|
* get owner of a file
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getOwner($path) {
|
|
|
|
if (!isset($this->owner[$path])) {
|
|
|
|
$this->owner[$path] = $this->util->getOwner($path);
|
|
|
|
}
|
|
|
|
return $this->owner[$path];
|
|
|
|
}
|
|
|
|
|
2016-04-14 15:32:25 +00:00
|
|
|
/**
|
|
|
|
* Check if the module is ready to be used by that specific user.
|
|
|
|
* In case a module is not ready - because e.g. key pairs have not been generated
|
|
|
|
* upon login this method can return false before any operation starts and might
|
|
|
|
* cause issues during operations.
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @return boolean
|
|
|
|
* @since 9.1.0
|
|
|
|
*/
|
|
|
|
public function isReadyForUser($user) {
|
|
|
|
return $this->keyManager->userHasKeys($user);
|
|
|
|
}
|
2017-05-30 09:59:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We only need a detailed access list if the master key is not enabled
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function needDetailedAccessList() {
|
|
|
|
return !$this->util->isMasterKeyEnabled();
|
|
|
|
}
|
2015-03-26 08:32:08 +00:00
|
|
|
}
|