2015-01-14 22:39:23 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
2015-04-07 18:02:49 +03:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @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,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Encryption\Keys;
|
|
|
|
|
|
|
|
use OC\Encryption\Util;
|
2015-04-22 12:18:18 +03:00
|
|
|
use OC\Files\Filesystem;
|
2015-01-14 22:39:23 +03:00
|
|
|
use OC\Files\View;
|
2015-04-01 17:36:08 +03:00
|
|
|
use OCP\Encryption\Exceptions\GenericEncryptionException;
|
2015-04-22 12:18:18 +03:00
|
|
|
use OCP\Encryption\Keys\IStorage;
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2015-04-22 12:18:18 +03:00
|
|
|
class Storage implements IStorage {
|
2015-01-14 22:39:23 +03:00
|
|
|
|
|
|
|
/** @var View */
|
|
|
|
private $view;
|
|
|
|
|
|
|
|
/** @var Util */
|
|
|
|
private $util;
|
|
|
|
|
|
|
|
// base dir where all the file related keys are stored
|
2015-04-22 14:12:52 +03:00
|
|
|
/** @var string */
|
2015-01-14 22:39:23 +03:00
|
|
|
private $keys_base_dir;
|
2015-04-22 14:12:52 +03:00
|
|
|
|
|
|
|
/** @var string */
|
2015-01-14 22:39:23 +03:00
|
|
|
private $encryption_base_dir;
|
|
|
|
|
2015-04-22 14:12:52 +03:00
|
|
|
/** @var array */
|
|
|
|
private $keyCache = [];
|
2015-01-14 22:39:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param View $view
|
|
|
|
* @param Util $util
|
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function __construct(View $view, Util $util) {
|
2015-01-14 22:39:23 +03:00
|
|
|
$this->view = $view;
|
|
|
|
$this->util = $util;
|
|
|
|
|
|
|
|
$this->encryption_base_dir = '/files_encryption';
|
|
|
|
$this->keys_base_dir = $this->encryption_base_dir .'/keys';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function getUserKey($uid, $keyId, $encryptionModuleId) {
|
|
|
|
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
|
2015-01-14 22:39:23 +03:00
|
|
|
return $this->getKey($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function getFileKey($path, $keyId, $encryptionModuleId) {
|
2015-05-22 13:12:31 +03:00
|
|
|
$realFile = $this->util->stripPartialFileExtension($path);
|
|
|
|
$keyDir = $this->getFileKeyDir($encryptionModuleId, $realFile);
|
2015-06-08 16:36:54 +03:00
|
|
|
$key = $this->getKey($keyDir . $keyId);
|
|
|
|
|
|
|
|
if ($key === '' && $realFile !== $path) {
|
|
|
|
// Check if the part file has keys and use them, if no normal keys
|
|
|
|
// exist. This is required to fix copyBetweenStorage() when we
|
|
|
|
// rename a .part file over storage borders.
|
|
|
|
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
|
|
|
|
$key = $this->getKey($keyDir . $keyId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $key;
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function getSystemUserKey($keyId, $encryptionModuleId) {
|
|
|
|
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
|
2015-01-14 22:39:23 +03:00
|
|
|
return $this->getKey($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function setUserKey($uid, $keyId, $key, $encryptionModuleId) {
|
|
|
|
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
|
2015-01-14 22:39:23 +03:00
|
|
|
return $this->setKey($path, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function setFileKey($path, $keyId, $key, $encryptionModuleId) {
|
|
|
|
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
|
2015-01-14 22:39:23 +03:00
|
|
|
return $this->setKey($keyDir . $keyId, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function setSystemUserKey($keyId, $key, $encryptionModuleId) {
|
|
|
|
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
|
2015-01-14 22:39:23 +03:00
|
|
|
return $this->setKey($path, $key);
|
|
|
|
}
|
|
|
|
|
2015-03-20 18:24:44 +03:00
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-03-20 18:24:44 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function deleteUserKey($uid, $keyId, $encryptionModuleId) {
|
|
|
|
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
|
2015-04-09 11:28:02 +03:00
|
|
|
return !$this->view->file_exists($path) || $this->view->unlink($path);
|
2015-03-20 18:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-03-20 18:24:44 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function deleteFileKey($path, $keyId, $encryptionModuleId) {
|
|
|
|
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
|
2015-04-09 11:28:02 +03:00
|
|
|
return !$this->view->file_exists($keyDir . $keyId) || $this->view->unlink($keyDir . $keyId);
|
2015-03-20 18:24:44 +03:00
|
|
|
}
|
|
|
|
|
2015-03-26 11:24:28 +03:00
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-03-26 11:24:28 +03:00
|
|
|
*/
|
2015-05-13 15:39:27 +03:00
|
|
|
public function deleteAllFileKeys($path) {
|
|
|
|
$keyDir = $this->getFileKeyDir('', $path);
|
|
|
|
return !$this->view->file_exists($keyDir) || $this->view->deleteAll($keyDir);
|
2015-03-26 11:24:28 +03:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:24:44 +03:00
|
|
|
/**
|
2015-04-22 12:18:18 +03:00
|
|
|
* @inheritdoc
|
2015-03-20 18:24:44 +03:00
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
public function deleteSystemUserKey($keyId, $encryptionModuleId) {
|
|
|
|
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
|
2015-04-09 11:28:02 +03:00
|
|
|
return !$this->view->file_exists($path) || $this->view->unlink($path);
|
2015-03-20 18:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* construct path to users key
|
|
|
|
*
|
2015-04-22 14:12:52 +03:00
|
|
|
* @param string $encryptionModuleId
|
2015-03-20 18:24:44 +03:00
|
|
|
* @param string $keyId
|
|
|
|
* @param string $uid
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
protected function constructUserKeyPath($encryptionModuleId, $keyId, $uid) {
|
2015-03-20 18:24:44 +03:00
|
|
|
|
|
|
|
if ($uid === null) {
|
2015-04-22 12:18:18 +03:00
|
|
|
$path = $this->encryption_base_dir . '/' . $encryptionModuleId . '/' . $keyId;
|
2015-03-20 18:24:44 +03:00
|
|
|
} else {
|
|
|
|
$path = '/' . $uid . $this->encryption_base_dir . '/'
|
2015-04-22 12:18:18 +03:00
|
|
|
. $encryptionModuleId . '/' . $uid . '.' . $keyId;
|
2015-03-20 18:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
2015-01-14 22:39:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* read key from hard disk
|
|
|
|
*
|
|
|
|
* @param string $path to key
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getKey($path) {
|
|
|
|
|
|
|
|
$key = '';
|
|
|
|
|
|
|
|
if ($this->view->file_exists($path)) {
|
|
|
|
if (isset($this->keyCache[$path])) {
|
|
|
|
$key = $this->keyCache[$path];
|
|
|
|
} else {
|
|
|
|
$key = $this->view->file_get_contents($path);
|
|
|
|
$this->keyCache[$path] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* write key to disk
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param string $path path to key directory
|
|
|
|
* @param string $key key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function setKey($path, $key) {
|
|
|
|
$this->keySetPreparation(dirname($path));
|
|
|
|
|
|
|
|
$result = $this->view->file_put_contents($path, $key);
|
|
|
|
|
|
|
|
if (is_int($result) && $result > 0) {
|
|
|
|
$this->keyCache[$path] = $key;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get path to key folder for a given file
|
|
|
|
*
|
2015-04-22 14:12:52 +03:00
|
|
|
* @param string $encryptionModuleId
|
2015-01-14 22:39:23 +03:00
|
|
|
* @param string $path path to the file, relative to data/
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-22 12:18:18 +03:00
|
|
|
private function getFileKeyDir($encryptionModuleId, $path) {
|
2015-01-14 22:39:23 +03:00
|
|
|
|
|
|
|
list($owner, $filename) = $this->util->getUidAndFilename($path);
|
|
|
|
|
|
|
|
// in case of system wide mount points the keys are stored directly in the data directory
|
2015-04-15 14:19:17 +03:00
|
|
|
if ($this->util->isSystemWideMountPoint($filename, $owner)) {
|
2015-01-14 22:39:23 +03:00
|
|
|
$keyPath = $this->keys_base_dir . $filename . '/';
|
|
|
|
} else {
|
|
|
|
$keyPath = '/' . $owner . $this->keys_base_dir . $filename . '/';
|
|
|
|
}
|
|
|
|
|
2015-04-22 12:18:18 +03:00
|
|
|
return Filesystem::normalizePath($keyPath . $encryptionModuleId . '/', false);
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
2015-03-26 11:24:28 +03:00
|
|
|
/**
|
|
|
|
* move keys if a file was renamed
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @param string $target
|
2015-04-24 11:16:06 +03:00
|
|
|
* @return boolean
|
2015-03-26 11:24:28 +03:00
|
|
|
*/
|
2015-04-02 17:25:01 +03:00
|
|
|
public function renameKeys($source, $target) {
|
|
|
|
|
2015-06-03 12:12:44 +03:00
|
|
|
$sourcePath = $this->getPathToKeys($source);
|
|
|
|
$targetPath = $this->getPathToKeys($target);
|
2015-03-26 11:24:28 +03:00
|
|
|
|
|
|
|
if ($this->view->file_exists($sourcePath)) {
|
|
|
|
$this->keySetPreparation(dirname($targetPath));
|
|
|
|
$this->view->rename($sourcePath, $targetPath);
|
2015-04-24 11:16:06 +03:00
|
|
|
|
|
|
|
return true;
|
2015-03-26 11:24:28 +03:00
|
|
|
}
|
2015-04-24 11:16:06 +03:00
|
|
|
|
|
|
|
return false;
|
2015-03-26 11:24:28 +03:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:12:44 +03:00
|
|
|
|
2015-04-02 17:25:01 +03:00
|
|
|
/**
|
|
|
|
* copy keys if a file was renamed
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @param string $target
|
2015-04-24 11:16:06 +03:00
|
|
|
* @return boolean
|
2015-04-02 17:25:01 +03:00
|
|
|
*/
|
|
|
|
public function copyKeys($source, $target) {
|
|
|
|
|
2015-06-03 12:12:44 +03:00
|
|
|
$sourcePath = $this->getPathToKeys($source);
|
|
|
|
$targetPath = $this->getPathToKeys($target);
|
2015-04-02 17:25:01 +03:00
|
|
|
|
|
|
|
if ($this->view->file_exists($sourcePath)) {
|
|
|
|
$this->keySetPreparation(dirname($targetPath));
|
|
|
|
$this->view->copy($sourcePath, $targetPath);
|
2015-04-24 11:16:06 +03:00
|
|
|
return true;
|
2015-04-02 17:25:01 +03:00
|
|
|
}
|
2015-04-24 11:16:06 +03:00
|
|
|
|
|
|
|
return false;
|
2015-04-02 17:25:01 +03:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:12:44 +03:00
|
|
|
/**
|
|
|
|
* get system wide path and detect mount points
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getPathToKeys($path) {
|
|
|
|
list($owner, $relativePath) = $this->util->getUidAndFilename($path);
|
|
|
|
$systemWideMountPoint = $this->util->isSystemWideMountPoint($relativePath, $owner);
|
|
|
|
|
|
|
|
if ($systemWideMountPoint) {
|
|
|
|
$systemPath = $this->keys_base_dir . $relativePath . '/';
|
|
|
|
} else {
|
|
|
|
$systemPath = '/' . $owner . $this->keys_base_dir . $relativePath . '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $systemPath;
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
/**
|
2015-04-22 14:12:52 +03:00
|
|
|
* Make preparations to filesystem for saving a key file
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
|
|
|
* @param string $path relative to the views root
|
|
|
|
*/
|
|
|
|
protected function keySetPreparation($path) {
|
|
|
|
// If the file resides within a subdirectory, create it
|
|
|
|
if (!$this->view->file_exists($path)) {
|
2015-04-02 17:42:28 +03:00
|
|
|
$sub_dirs = explode('/', ltrim($path, '/'));
|
2015-01-14 22:39:23 +03:00
|
|
|
$dir = '';
|
|
|
|
foreach ($sub_dirs as $sub_dir) {
|
|
|
|
$dir .= '/' . $sub_dir;
|
|
|
|
if (!$this->view->is_dir($dir)) {
|
|
|
|
$this->view->mkdir($dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|