nextcloud/lib/private/encryption/keys/storage.php

314 lines
8.0 KiB
PHP
Raw Normal View History

<?php
/**
* @author Björn Schießle <schiessle@owncloud.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 OC\Encryption\Keys;
use OC\Encryption\Util;
use OC\Files\Filesystem;
use OC\Files\View;
2015-04-01 17:36:08 +03:00
use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\Encryption\Keys\IStorage;
class Storage implements IStorage {
/** @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 */
private $keys_base_dir;
2015-04-22 14:12:52 +03:00
/** @var string */
private $encryption_base_dir;
2015-04-22 14:12:52 +03:00
/** @var array */
private $keyCache = [];
/**
* @param View $view
* @param Util $util
*/
public function __construct(View $view, Util $util) {
$this->view = $view;
$this->util = $util;
$this->encryption_base_dir = '/files_encryption';
$this->keys_base_dir = $this->encryption_base_dir .'/keys';
}
/**
* @inheritdoc
*/
public function getUserKey($uid, $keyId, $encryptionModuleId) {
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
return $this->getKey($path);
}
/**
* @inheritdoc
*/
public function getFileKey($path, $keyId, $encryptionModuleId) {
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
return $this->getKey($keyDir . $keyId);
}
/**
* @inheritdoc
*/
public function getSystemUserKey($keyId, $encryptionModuleId) {
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
return $this->getKey($path);
}
/**
* @inheritdoc
*/
public function setUserKey($uid, $keyId, $key, $encryptionModuleId) {
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
return $this->setKey($path, $key);
}
/**
* @inheritdoc
*/
public function setFileKey($path, $keyId, $key, $encryptionModuleId) {
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
return $this->setKey($keyDir . $keyId, $key);
}
/**
* @inheritdoc
*/
public function setSystemUserKey($keyId, $key, $encryptionModuleId) {
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
return $this->setKey($path, $key);
}
2015-03-20 18:24:44 +03:00
/**
* @inheritdoc
2015-03-20 18:24:44 +03:00
*/
public function deleteUserKey($uid, $keyId, $encryptionModuleId) {
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
return !$this->view->file_exists($path) || $this->view->unlink($path);
2015-03-20 18:24:44 +03:00
}
/**
* @inheritdoc
2015-03-20 18:24:44 +03:00
*/
public function deleteFileKey($path, $keyId, $encryptionModuleId) {
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
return !$this->view->file_exists($keyDir . $keyId) || $this->view->unlink($keyDir . $keyId);
2015-03-20 18:24:44 +03:00
}
/**
* @inheritdoc
*/
public function deleteAllFileKeys($path, $encryptionModuleId) {
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
$path = dirname($keyDir);
return !$this->view->file_exists($path) || $this->view->deleteAll($path);
}
2015-03-20 18:24:44 +03:00
/**
* @inheritdoc
2015-03-20 18:24:44 +03:00
*/
public function deleteSystemUserKey($keyId, $encryptionModuleId) {
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
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
*/
protected function constructUserKeyPath($encryptionModuleId, $keyId, $uid) {
2015-03-20 18:24:44 +03:00
if ($uid === null) {
$path = $this->encryption_base_dir . '/' . $encryptionModuleId . '/' . $keyId;
2015-03-20 18:24:44 +03:00
} else {
$path = '/' . $uid . $this->encryption_base_dir . '/'
. $encryptionModuleId . '/' . $uid . '.' . $keyId;
2015-03-20 18:24:44 +03:00
}
return $path;
}
/**
* 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
* @param string $path path to the file, relative to data/
* @return string
2015-03-30 23:36:48 +03:00
* @throws GenericEncryptionException
* @internal param string $keyId
*/
private function getFileKeyDir($encryptionModuleId, $path) {
if ($this->view->is_dir($path)) {
2015-04-01 17:36:08 +03:00
throw new GenericEncryptionException("file was expected but directory was given: $path");
}
list($owner, $filename) = $this->util->getUidAndFilename($path);
$filename = $this->util->stripPartialFileExtension($filename);
// in case of system wide mount points the keys are stored directly in the data directory
if ($this->util->isSystemWideMountPoint($filename, $owner)) {
$keyPath = $this->keys_base_dir . $filename . '/';
} else {
$keyPath = '/' . $owner . $this->keys_base_dir . $filename . '/';
}
return Filesystem::normalizePath($keyPath . $encryptionModuleId . '/', false);
}
/**
* move keys if a file was renamed
*
* @param string $source
* @param string $target
* @return boolean
*/
2015-04-02 17:25:01 +03:00
public function renameKeys($source, $target) {
list($owner, $source) = $this->util->getUidAndFilename($source);
list(, $target) = $this->util->getUidAndFilename($target);
$systemWide = $this->util->isSystemWideMountPoint($target, $owner);
2015-04-02 17:25:01 +03:00
if ($systemWide) {
$sourcePath = $this->keys_base_dir . $source . '/';
$targetPath = $this->keys_base_dir . $target . '/';
} else {
$sourcePath = '/' . $owner . $this->keys_base_dir . $source . '/';
$targetPath = '/' . $owner . $this->keys_base_dir . $target . '/';
}
if ($this->view->file_exists($sourcePath)) {
$this->keySetPreparation(dirname($targetPath));
$this->view->rename($sourcePath, $targetPath);
return true;
}
return false;
}
2015-04-02 17:25:01 +03:00
/**
* copy keys if a file was renamed
*
* @param string $source
* @param string $target
* @return boolean
2015-04-02 17:25:01 +03:00
*/
public function copyKeys($source, $target) {
list($owner, $source) = $this->util->getUidAndFilename($source);
list(, $target) = $this->util->getUidAndFilename($target);
$systemWide = $this->util->isSystemWideMountPoint($target, $owner);
2015-04-02 17:25:01 +03:00
if ($systemWide) {
$sourcePath = $this->keys_base_dir . $source . '/';
$targetPath = $this->keys_base_dir . $target . '/';
} else {
$sourcePath = '/' . $owner . $this->keys_base_dir . $source . '/';
$targetPath = '/' . $owner . $this->keys_base_dir . $target . '/';
}
if ($this->view->file_exists($sourcePath)) {
$this->keySetPreparation(dirname($targetPath));
$this->view->copy($sourcePath, $targetPath);
return true;
2015-04-02 17:25:01 +03:00
}
return false;
2015-04-02 17:25:01 +03:00
}
/**
2015-04-22 14:12:52 +03:00
* Make preparations to filesystem for saving a key file
*
* @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, '/'));
$dir = '';
foreach ($sub_dirs as $sub_dir) {
$dir .= '/' . $sub_dir;
if (!$this->view->is_dir($dir)) {
$this->view->mkdir($dir);
}
}
}
}
}