Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion core/Command/Info/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OC\Core\Command\Info;

use OC\Files\ObjectStore\ObjectStoreStorage;
use OC\Files\View;
use OCA\Files_External\Config\ExternalMountPoint;
use OCA\GroupFolders\Mount\GroupMountPoint;
use OCP\Files\Folder;
Expand All @@ -24,11 +25,19 @@
class File extends Command {
private IL10N $l10n;
private FileUtils $fileUtils;
private View $rootView;
private \OC\Encryption\Util $encryptionUtil;

public function __construct(IFactory $l10nFactory, FileUtils $fileUtils) {
public function __construct(
IFactory $l10nFactory,
FileUtils $fileUtils,
\OC\Encryption\Util $encryptionUtil
) {
$this->l10n = $l10nFactory->get("core");
$this->fileUtils = $fileUtils;
$this->encryptionUtil = $encryptionUtil;
parent::__construct();
$this->rootView = new View();
}

protected function configure(): void {
Expand All @@ -53,6 +62,14 @@ public function execute(InputInterface $input, OutputInterface $output): int {
$output->writeln(" mimetype: " . $node->getMimetype());
$output->writeln(" modified: " . (string)$this->l10n->l("datetime", $node->getMTime()));
$output->writeln(" " . ($node->isEncrypted() ? "encrypted" : "not encrypted"));
if ($node->isEncrypted()) {
$keyPath = $this->encryptionUtil->getFileKeyDir('', $node->getPath());
if ($this->rootView->file_exists($keyPath)) {
$output->writeln(" encryption key at: " . $keyPath);
} else {
$output->writeln(" <error>encryption key not found</error> should be location at: " . $keyPath);
}
}
$output->writeln(" size: " . Util::humanFileSize($node->getSize()));
$output->writeln(" etag: " . $node->getEtag());
if ($node instanceof Folder) {
Expand Down
30 changes: 5 additions & 25 deletions lib/private/Encryption/Keys/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ public function getUserKey($uid, $keyId, $encryptionModuleId) {
*/
public function getFileKey($path, $keyId, $encryptionModuleId) {
$realFile = $this->util->stripPartialFileExtension($path);
$keyDir = $this->getFileKeyDir($encryptionModuleId, $realFile);
$keyDir = $this->util->getFileKeyDir($encryptionModuleId, $realFile);
$key = $this->getKey($keyDir . $keyId)['key'];

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);
$keyDir = $this->util->getFileKeyDir($encryptionModuleId, $path);
$key = $this->getKey($keyDir . $keyId)['key'];
}

Expand Down Expand Up @@ -135,7 +135,7 @@ public function setUserKey($uid, $keyId, $key, $encryptionModuleId) {
* @inheritdoc
*/
public function setFileKey($path, $keyId, $key, $encryptionModuleId) {
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
$keyDir = $this->util->getFileKeyDir($encryptionModuleId, $path);
return $this->setKey($keyDir . $keyId, [
'key' => base64_encode($key),
]);
Expand Down Expand Up @@ -177,15 +177,15 @@ public function deleteUserKey($uid, $keyId, $encryptionModuleId) {
* @inheritdoc
*/
public function deleteFileKey($path, $keyId, $encryptionModuleId) {
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
$keyDir = $this->util->getFileKeyDir($encryptionModuleId, $path);
return !$this->view->file_exists($keyDir . $keyId) || $this->view->unlink($keyDir . $keyId);
}

/**
* @inheritdoc
*/
public function deleteAllFileKeys($path) {
$keyDir = $this->getFileKeyDir('', $path);
$keyDir = $this->util->getFileKeyDir('', $path);
return !$this->view->file_exists($keyDir) || $this->view->deleteAll($keyDir);
}

Expand Down Expand Up @@ -355,26 +355,6 @@ private function setKey($path, $key) {
return false;
}

/**
* get path to key folder for a given file
*
* @param string $encryptionModuleId
* @param string $path path to the file, relative to data/
* @return string
*/
private function getFileKeyDir($encryptionModuleId, $path) {
[$owner, $filename] = $this->util->getUidAndFilename($path);

// in case of system wide mount points the keys are stored directly in the data directory
if ($this->util->isSystemWideMountPoint($filename, $owner)) {
$keyPath = $this->root_dir . '/' . $this->keys_base_dir . $filename . '/';
} else {
$keyPath = $this->root_dir . '/' . $owner . $this->keys_base_dir . $filename . '/';
}

return Filesystem::normalizePath($keyPath . $encryptionModuleId . '/', false);
}

/**
* move keys if a file was renamed
*
Expand Down
21 changes: 21 additions & 0 deletions lib/private/Encryption/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,25 @@ public function setKeyStorageRoot(string $root): void {
public function getKeyStorageRoot(): string {
return $this->config->getAppValue('core', 'encryption_key_storage_root', '');
}

/**
* get path to key folder for a given file
*
* @param string $encryptionModuleId
* @param string $path path to the file, relative to data/
* @return string
*/
public function getFileKeyDir(string $encryptionModuleId, string $path): string {
[$owner, $filename] = $this->getUidAndFilename($path);
$root = $this->getKeyStorageRoot();

// in case of system-wide mount points the keys are stored directly in the data directory
if ($this->isSystemWideMountPoint($filename, $owner)) {
$keyPath = $root . '/' . '/files_encryption/keys' . $filename . '/';
} else {
$keyPath = $root . '/' . $owner . '/files_encryption/keys' . $filename . '/';
}

return Filesystem::normalizePath($keyPath . $encryptionModuleId . '/', false);
}
}