2015-01-14 22:39:23 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2015-06-25 12:43:55 +03:00
|
|
|
* @author Jan-Christoph Borchardt <hey@jancborchardt.net>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
2015-04-07 18:02:49 +03:00
|
|
|
* @license AGPL-3.0
|
2015-01-14 22:39:23 +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-01-14 22:39:23 +03:00
|
|
|
*
|
2015-04-07 18:02:49 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2015-01-14 22:39:23 +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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Encryption;
|
|
|
|
|
|
|
|
use OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException;
|
2015-03-30 14:23:10 +03:00
|
|
|
use OC\Encryption\Exceptions\EncryptionHeaderToLargeException;
|
2015-04-07 19:05:54 +03:00
|
|
|
use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
|
2015-04-23 17:48:11 +03:00
|
|
|
use OC\Files\Filesystem;
|
2015-03-30 14:23:10 +03:00
|
|
|
use OC\Files\View;
|
2015-01-14 22:39:23 +03:00
|
|
|
use OCP\Encryption\IEncryptionModule;
|
2015-03-28 13:02:26 +03:00
|
|
|
use OCP\IConfig;
|
2018-03-25 21:42:38 +03:00
|
|
|
use OCP\IUser;
|
2015-01-14 22:39:23 +03:00
|
|
|
|
|
|
|
class Util {
|
|
|
|
|
|
|
|
const HEADER_START = 'HBEGIN';
|
|
|
|
const HEADER_END = 'HEND';
|
|
|
|
const HEADER_PADDING_CHAR = '-';
|
|
|
|
|
|
|
|
const HEADER_ENCRYPTION_MODULE_KEY = 'oc_encryption_module';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* block size will always be 8192 for a PHP stream
|
|
|
|
* @see https://bugs.php.net/bug.php?id=21641
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $headerSize = 8192;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* block size will always be 8192 for a PHP stream
|
|
|
|
* @see https://bugs.php.net/bug.php?id=21641
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $blockSize = 8192;
|
|
|
|
|
2015-03-30 14:23:10 +03:00
|
|
|
/** @var View */
|
2015-07-24 13:24:18 +03:00
|
|
|
protected $rootView;
|
2015-01-14 22:39:23 +03:00
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected $ocHeaderKeys;
|
|
|
|
|
2015-04-02 18:53:40 +03:00
|
|
|
/** @var \OC\User\Manager */
|
2015-01-14 22:39:23 +03:00
|
|
|
protected $userManager;
|
|
|
|
|
2015-03-28 13:02:26 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
/** @var array paths excluded from encryption */
|
|
|
|
protected $excludedPaths;
|
|
|
|
|
2015-04-15 14:19:17 +03:00
|
|
|
/** @var \OC\Group\Manager $manager */
|
|
|
|
protected $groupManager;
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
/**
|
2015-03-30 11:37:31 +03:00
|
|
|
*
|
2015-07-24 13:24:18 +03:00
|
|
|
* @param View $rootView
|
2015-03-30 11:37:31 +03:00
|
|
|
* @param \OC\User\Manager $userManager
|
2015-04-15 14:19:17 +03:00
|
|
|
* @param \OC\Group\Manager $groupManager
|
2015-03-30 11:37:31 +03:00
|
|
|
* @param IConfig $config
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-03-28 13:02:26 +03:00
|
|
|
public function __construct(
|
2015-07-24 13:24:18 +03:00
|
|
|
View $rootView,
|
2015-03-28 13:02:26 +03:00
|
|
|
\OC\User\Manager $userManager,
|
2015-04-15 14:19:17 +03:00
|
|
|
\OC\Group\Manager $groupManager,
|
2015-03-28 13:02:26 +03:00
|
|
|
IConfig $config) {
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
$this->ocHeaderKeys = [
|
|
|
|
self::HEADER_ENCRYPTION_MODULE_KEY
|
|
|
|
];
|
|
|
|
|
2015-07-24 13:24:18 +03:00
|
|
|
$this->rootView = $rootView;
|
2015-01-14 22:39:23 +03:00
|
|
|
$this->userManager = $userManager;
|
2015-04-15 14:19:17 +03:00
|
|
|
$this->groupManager = $groupManager;
|
2015-03-28 13:02:26 +03:00
|
|
|
$this->config = $config;
|
2015-01-14 22:39:23 +03:00
|
|
|
|
|
|
|
$this->excludedPaths[] = 'files_encryption';
|
2016-11-30 00:01:04 +03:00
|
|
|
$this->excludedPaths[] = 'appdata_' . $config->getSystemValue('instanceid', null);
|
2017-06-13 10:50:45 +03:00
|
|
|
$this->excludedPaths[] = 'files_external';
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* read encryption module ID from header
|
|
|
|
*
|
|
|
|
* @param array $header
|
|
|
|
* @return string
|
2015-04-07 19:05:54 +03:00
|
|
|
* @throws ModuleDoesNotExistsException
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-03-30 14:23:10 +03:00
|
|
|
public function getEncryptionModuleId(array $header = null) {
|
2015-01-14 22:39:23 +03:00
|
|
|
$id = '';
|
|
|
|
$encryptionModuleKey = self::HEADER_ENCRYPTION_MODULE_KEY;
|
|
|
|
|
|
|
|
if (isset($header[$encryptionModuleKey])) {
|
|
|
|
$id = $header[$encryptionModuleKey];
|
2015-04-07 19:05:54 +03:00
|
|
|
} elseif (isset($header['cipher'])) {
|
|
|
|
if (class_exists('\OCA\Encryption\Crypto\Encryption')) {
|
|
|
|
// fall back to default encryption if the user migrated from
|
|
|
|
// ownCloud <= 8.0 with the old encryption
|
|
|
|
$id = \OCA\Encryption\Crypto\Encryption::ID;
|
|
|
|
} else {
|
2015-05-05 23:08:16 +03:00
|
|
|
throw new ModuleDoesNotExistsException('Default encryption module missing');
|
2015-04-07 19:05:54 +03:00
|
|
|
}
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create header for encrypted file
|
|
|
|
*
|
|
|
|
* @param array $headerData
|
|
|
|
* @param IEncryptionModule $encryptionModule
|
|
|
|
* @return string
|
|
|
|
* @throws EncryptionHeaderToLargeException if header has to many arguments
|
|
|
|
* @throws EncryptionHeaderKeyExistsException if header key is already in use
|
|
|
|
*/
|
|
|
|
public function createHeader(array $headerData, IEncryptionModule $encryptionModule) {
|
|
|
|
$header = self::HEADER_START . ':' . self::HEADER_ENCRYPTION_MODULE_KEY . ':' . $encryptionModule->getId() . ':';
|
|
|
|
foreach ($headerData as $key => $value) {
|
|
|
|
if (in_array($key, $this->ocHeaderKeys)) {
|
2015-04-01 17:36:08 +03:00
|
|
|
throw new EncryptionHeaderKeyExistsException($key);
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
$header .= $key . ':' . $value . ':';
|
|
|
|
}
|
|
|
|
$header .= self::HEADER_END;
|
|
|
|
|
|
|
|
if (strlen($header) > $this->getHeaderSize()) {
|
2015-04-01 17:36:08 +03:00
|
|
|
throw new EncryptionHeaderToLargeException();
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$paddedHeader = str_pad($header, $this->headerSize, self::HEADER_PADDING_CHAR, STR_PAD_RIGHT);
|
|
|
|
|
|
|
|
return $paddedHeader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* go recursively through a dir and collect all files and sub files.
|
|
|
|
*
|
|
|
|
* @param string $dir relative to the users files folder
|
|
|
|
* @return array with list of files relative to the users files folder
|
|
|
|
*/
|
2015-04-23 17:48:11 +03:00
|
|
|
public function getAllFiles($dir) {
|
2015-01-14 22:39:23 +03:00
|
|
|
$result = array();
|
|
|
|
$dirList = array($dir);
|
|
|
|
|
|
|
|
while ($dirList) {
|
|
|
|
$dir = array_pop($dirList);
|
2015-07-24 13:24:18 +03:00
|
|
|
$content = $this->rootView->getDirectoryContent($dir);
|
2015-01-14 22:39:23 +03:00
|
|
|
|
|
|
|
foreach ($content as $c) {
|
2015-04-23 17:48:11 +03:00
|
|
|
if ($c->getType() === 'dir') {
|
|
|
|
$dirList[] = $c->getPath();
|
2015-01-14 22:39:23 +03:00
|
|
|
} else {
|
2015-04-23 17:48:11 +03:00
|
|
|
$result[] = $c->getPath();
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if it is a file uploaded by the user stored in data/user/files
|
|
|
|
* or a metadata file
|
|
|
|
*
|
2015-04-23 17:48:11 +03:00
|
|
|
* @param string $path relative to the data/ folder
|
2015-01-14 22:39:23 +03:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-03-31 17:23:31 +03:00
|
|
|
public function isFile($path) {
|
2015-04-23 17:48:11 +03:00
|
|
|
$parts = explode('/', Filesystem::normalizePath($path), 4);
|
|
|
|
if (isset($parts[2]) && $parts[2] === 'files') {
|
2015-01-14 22:39:23 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return size of encryption header
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getHeaderSize() {
|
|
|
|
return $this->headerSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return size of block read by a PHP stream
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getBlockSize() {
|
|
|
|
return $this->blockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-01 15:24:42 +03:00
|
|
|
* get the owner and the path for the file relative to the owners files folder
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return array
|
|
|
|
* @throws \BadMethodCallException
|
|
|
|
*/
|
|
|
|
public function getUidAndFilename($path) {
|
|
|
|
|
|
|
|
$parts = explode('/', $path);
|
|
|
|
$uid = '';
|
|
|
|
if (count($parts) > 2) {
|
|
|
|
$uid = $parts[1];
|
|
|
|
}
|
|
|
|
if (!$this->userManager->userExists($uid)) {
|
2015-04-01 15:24:42 +03:00
|
|
|
throw new \BadMethodCallException(
|
|
|
|
'path needs to be relative to the system wide data folder and point to a user specific file'
|
|
|
|
);
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
2015-04-01 15:24:42 +03:00
|
|
|
$ownerPath = implode('/', array_slice($parts, 2));
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
return array($uid, Filesystem::normalizePath($ownerPath));
|
2015-01-14 22:39:23 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove .path extension from a file path
|
|
|
|
* @param string $path Path that may identify a .part file
|
|
|
|
* @return string File path without .part extension
|
|
|
|
* @note this is needed for reusing keys
|
|
|
|
*/
|
|
|
|
public function stripPartialFileExtension($path) {
|
|
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION);
|
|
|
|
|
|
|
|
if ( $extension === 'part') {
|
|
|
|
|
|
|
|
$newLength = strlen($path) - 5; // 5 = strlen(".part")
|
|
|
|
$fPath = substr($path, 0, $newLength);
|
|
|
|
|
|
|
|
// if path also contains a transaction id, we remove it too
|
|
|
|
$extension = pathinfo($fPath, PATHINFO_EXTENSION);
|
|
|
|
if(substr($extension, 0, 12) === 'ocTransferId') { // 12 = strlen("ocTransferId")
|
|
|
|
$newLength = strlen($fPath) - strlen($extension) -1;
|
|
|
|
$fPath = substr($fPath, 0, $newLength);
|
|
|
|
}
|
|
|
|
return $fPath;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 17:23:31 +03:00
|
|
|
public function getUserWithAccessToMountPoint($users, $groups) {
|
2018-03-25 21:33:24 +03:00
|
|
|
$result = [];
|
2015-01-14 22:39:23 +03:00
|
|
|
if (in_array('all', $users)) {
|
2018-03-25 21:33:24 +03:00
|
|
|
$users = $this->userManager->search('', null, null);
|
2018-03-25 21:42:38 +03:00
|
|
|
$result = array_map(function(IUser $user) {
|
|
|
|
return $user->getUID();
|
|
|
|
}, $users);
|
2015-01-14 22:39:23 +03:00
|
|
|
} else {
|
|
|
|
$result = array_merge($result, $users);
|
2017-03-03 10:24:27 +03:00
|
|
|
|
|
|
|
$groupManager = \OC::$server->getGroupManager();
|
2015-01-14 22:39:23 +03:00
|
|
|
foreach ($groups as $group) {
|
2017-03-03 10:24:27 +03:00
|
|
|
$groupObject = $groupManager->get($group);
|
|
|
|
if ($groupObject) {
|
|
|
|
$foundUsers = $groupObject->searchUsers('', -1, 0);
|
|
|
|
$userIds = [];
|
|
|
|
foreach ($foundUsers as $user) {
|
|
|
|
$userIds[] = $user->getUID();
|
|
|
|
}
|
|
|
|
$result = array_merge($result, $userIds);
|
|
|
|
}
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if the file is stored on a system wide mount point
|
|
|
|
* @param string $path relative to /data/user with leading '/'
|
2015-04-15 14:19:17 +03:00
|
|
|
* @param string $uid
|
2015-01-14 22:39:23 +03:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-04-15 14:19:17 +03:00
|
|
|
public function isSystemWideMountPoint($path, $uid) {
|
2015-01-14 22:39:23 +03:00
|
|
|
if (\OCP\App::isEnabled("files_external")) {
|
|
|
|
$mounts = \OC_Mount_Config::getSystemMountPoints();
|
|
|
|
foreach ($mounts as $mount) {
|
2015-04-15 14:19:17 +03:00
|
|
|
if (strpos($path, '/files/' . $mount['mountpoint']) === 0) {
|
|
|
|
if ($this->isMountPointApplicableToUser($mount, $uid)) {
|
2015-01-14 22:39:23 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:19:17 +03:00
|
|
|
/**
|
|
|
|
* check if mount point is applicable to user
|
|
|
|
*
|
|
|
|
* @param array $mount contains $mount['applicable']['users'], $mount['applicable']['groups']
|
|
|
|
* @param string $uid
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function isMountPointApplicableToUser($mount, $uid) {
|
|
|
|
$acceptedUids = array('all', $uid);
|
|
|
|
// check if mount point is applicable for the user
|
|
|
|
$intersection = array_intersect($acceptedUids, $mount['applicable']['users']);
|
|
|
|
if (!empty($intersection)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// check if mount point is applicable for group where the user is a member
|
|
|
|
foreach ($mount['applicable']['groups'] as $gid) {
|
|
|
|
if ($this->groupManager->isInGroup($uid, $gid)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
/**
|
|
|
|
* check if it is a path which is excluded by ownCloud from encryption
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isExcluded($path) {
|
2015-07-24 13:24:18 +03:00
|
|
|
$normalizedPath = Filesystem::normalizePath($path);
|
2015-03-27 13:46:07 +03:00
|
|
|
$root = explode('/', $normalizedPath, 4);
|
2015-05-11 13:38:59 +03:00
|
|
|
if (count($root) > 1) {
|
2015-03-27 13:46:07 +03:00
|
|
|
|
2015-07-24 13:24:18 +03:00
|
|
|
// detect alternative key storage root
|
|
|
|
$rootDir = $this->getKeyStorageRoot();
|
|
|
|
if ($rootDir !== '' &&
|
|
|
|
0 === strpos(
|
|
|
|
Filesystem::normalizePath($path),
|
|
|
|
Filesystem::normalizePath($rootDir)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-27 13:46:07 +03:00
|
|
|
//detect system wide folders
|
|
|
|
if (in_array($root[1], $this->excludedPaths)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// detect user specific folders
|
|
|
|
if ($this->userManager->userExists($root[1])
|
|
|
|
&& in_array($root[2], $this->excludedPaths)) {
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-28 13:02:26 +03:00
|
|
|
/**
|
|
|
|
* check if recovery key is enabled for user
|
|
|
|
*
|
|
|
|
* @param string $uid
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function recoveryEnabled($uid) {
|
|
|
|
$enabled = $this->config->getUserValue($uid, 'encryption', 'recovery_enabled', '0');
|
|
|
|
|
2018-01-26 14:36:25 +03:00
|
|
|
return $enabled === '1';
|
2015-03-28 13:02:26 +03:00
|
|
|
}
|
|
|
|
|
2015-07-24 13:24:18 +03:00
|
|
|
/**
|
|
|
|
* set new key storage root
|
|
|
|
*
|
|
|
|
* @param string $root new key store root relative to the data folder
|
|
|
|
*/
|
|
|
|
public function setKeyStorageRoot($root) {
|
|
|
|
$this->config->setAppValue('core', 'encryption_key_storage_root', $root);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get key storage root
|
|
|
|
*
|
|
|
|
* @return string key storage root
|
|
|
|
*/
|
|
|
|
public function getKeyStorageRoot() {
|
|
|
|
return $this->config->getAppValue('core', 'encryption_key_storage_root', '');
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|