summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2020-08-20 17:41:18 +0200
committerGitHub <noreply@github.com>2020-08-20 17:41:18 +0200
commit65b5e651851ce0270de2b5087dc42fd3aa7cae31 (patch)
tree452b41ec7501f48ac5bff52593cf686be214c027 /lib
parent0b39e7dca60c9303ef069021e031a58071bbd267 (diff)
parent5340ab3a75d58651e3cc65688d94444b38570cfc (diff)
downloadnextcloud-server-65b5e651851ce0270de2b5087dc42fd3aa7cae31.tar.gz
nextcloud-server-65b5e651851ce0270de2b5087dc42fd3aa7cae31.zip
Merge pull request #21529 from nextcloud/enh/encryption/improve_key_format
New SSE key format
Diffstat (limited to 'lib')
-rw-r--r--lib/composer/composer/autoload_classmap.php2
-rw-r--r--lib/composer/composer/autoload_static.php2
-rw-r--r--lib/private/Encryption/Keys/Storage.php146
-rw-r--r--lib/private/Repair.php2
-rw-r--r--lib/private/Repair/NC20/EncryptionMigration.php62
-rw-r--r--lib/private/Server.php2
6 files changed, 200 insertions, 16 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index d3fb9181c3b..c0e35fa4604 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -808,6 +808,7 @@ return array(
'OC\\Core\\Command\\Encryption\\Enable' => $baseDir . '/core/Command/Encryption/Enable.php',
'OC\\Core\\Command\\Encryption\\EncryptAll' => $baseDir . '/core/Command/Encryption/EncryptAll.php',
'OC\\Core\\Command\\Encryption\\ListModules' => $baseDir . '/core/Command/Encryption/ListModules.php',
+ 'OC\\Core\\Command\\Encryption\\MigrateKeyStorage' => $baseDir . '/core/Command/Encryption/MigrateKeyStorage.php',
'OC\\Core\\Command\\Encryption\\SetDefaultModule' => $baseDir . '/core/Command/Encryption/SetDefaultModule.php',
'OC\\Core\\Command\\Encryption\\ShowKeyStorageRoot' => $baseDir . '/core/Command/Encryption/ShowKeyStorageRoot.php',
'OC\\Core\\Command\\Encryption\\Status' => $baseDir . '/core/Command/Encryption/Status.php',
@@ -1248,6 +1249,7 @@ return array(
'OC\\Repair\\NC16\\ClearCollectionsAccessCache' => $baseDir . '/lib/private/Repair/NC16/ClearCollectionsAccessCache.php',
'OC\\Repair\\NC18\\ResetGeneratedAvatarFlag' => $baseDir . '/lib/private/Repair/NC18/ResetGeneratedAvatarFlag.php',
'OC\\Repair\\NC20\\EncryptionLegacyCipher' => $baseDir . '/lib/private/Repair/NC20/EncryptionLegacyCipher.php',
+ 'OC\\Repair\\NC20\\EncryptionMigration' => $baseDir . '/lib/private/Repair/NC20/EncryptionMigration.php',
'OC\\Repair\\OldGroupMembershipShares' => $baseDir . '/lib/private/Repair/OldGroupMembershipShares.php',
'OC\\Repair\\Owncloud\\DropAccountTermsTable' => $baseDir . '/lib/private/Repair/Owncloud/DropAccountTermsTable.php',
'OC\\Repair\\Owncloud\\SaveAccountsTableData' => $baseDir . '/lib/private/Repair/Owncloud/SaveAccountsTableData.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index e0ac8a31147..a1786bc1de0 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -837,6 +837,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Core\\Command\\Encryption\\Enable' => __DIR__ . '/../../..' . '/core/Command/Encryption/Enable.php',
'OC\\Core\\Command\\Encryption\\EncryptAll' => __DIR__ . '/../../..' . '/core/Command/Encryption/EncryptAll.php',
'OC\\Core\\Command\\Encryption\\ListModules' => __DIR__ . '/../../..' . '/core/Command/Encryption/ListModules.php',
+ 'OC\\Core\\Command\\Encryption\\MigrateKeyStorage' => __DIR__ . '/../../..' . '/core/Command/Encryption/MigrateKeyStorage.php',
'OC\\Core\\Command\\Encryption\\SetDefaultModule' => __DIR__ . '/../../..' . '/core/Command/Encryption/SetDefaultModule.php',
'OC\\Core\\Command\\Encryption\\ShowKeyStorageRoot' => __DIR__ . '/../../..' . '/core/Command/Encryption/ShowKeyStorageRoot.php',
'OC\\Core\\Command\\Encryption\\Status' => __DIR__ . '/../../..' . '/core/Command/Encryption/Status.php',
@@ -1277,6 +1278,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Repair\\NC16\\ClearCollectionsAccessCache' => __DIR__ . '/../../..' . '/lib/private/Repair/NC16/ClearCollectionsAccessCache.php',
'OC\\Repair\\NC18\\ResetGeneratedAvatarFlag' => __DIR__ . '/../../..' . '/lib/private/Repair/NC18/ResetGeneratedAvatarFlag.php',
'OC\\Repair\\NC20\\EncryptionLegacyCipher' => __DIR__ . '/../../..' . '/lib/private/Repair/NC20/EncryptionLegacyCipher.php',
+ 'OC\\Repair\\NC20\\EncryptionMigration' => __DIR__ . '/../../..' . '/lib/private/Repair/NC20/EncryptionMigration.php',
'OC\\Repair\\OldGroupMembershipShares' => __DIR__ . '/../../..' . '/lib/private/Repair/OldGroupMembershipShares.php',
'OC\\Repair\\Owncloud\\DropAccountTermsTable' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/DropAccountTermsTable.php',
'OC\\Repair\\Owncloud\\SaveAccountsTableData' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/SaveAccountsTableData.php',
diff --git a/lib/private/Encryption/Keys/Storage.php b/lib/private/Encryption/Keys/Storage.php
index cee32691261..43a291b886c 100644
--- a/lib/private/Encryption/Keys/Storage.php
+++ b/lib/private/Encryption/Keys/Storage.php
@@ -31,8 +31,11 @@ namespace OC\Encryption\Keys;
use OC\Encryption\Util;
use OC\Files\Filesystem;
use OC\Files\View;
+use OC\ServerNotAvailableException;
use OC\User\NoUserException;
use OCP\Encryption\Keys\IStorage;
+use OCP\IConfig;
+use OCP\Security\ICrypto;
class Storage implements IStorage {
@@ -62,11 +65,17 @@ class Storage implements IStorage {
/** @var array */
private $keyCache = [];
+ /** @var ICrypto */
+ private $crypto;
+
+ /** @var IConfig */
+ private $config;
+
/**
* @param View $view
* @param Util $util
*/
- public function __construct(View $view, Util $util) {
+ public function __construct(View $view, Util $util, ICrypto $crypto, IConfig $config) {
$this->view = $view;
$this->util = $util;
@@ -74,6 +83,8 @@ class Storage implements IStorage {
$this->keys_base_dir = $this->encryption_base_dir .'/keys';
$this->backup_base_dir = $this->encryption_base_dir .'/backup';
$this->root_dir = $this->util->getKeyStorageRoot();
+ $this->crypto = $crypto;
+ $this->config = $config;
}
/**
@@ -81,7 +92,7 @@ class Storage implements IStorage {
*/
public function getUserKey($uid, $keyId, $encryptionModuleId) {
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
- return $this->getKey($path);
+ return base64_decode($this->getKeyWithUid($path, $uid));
}
/**
@@ -90,17 +101,17 @@ class Storage implements IStorage {
public function getFileKey($path, $keyId, $encryptionModuleId) {
$realFile = $this->util->stripPartialFileExtension($path);
$keyDir = $this->getFileKeyDir($encryptionModuleId, $realFile);
- $key = $this->getKey($keyDir . $keyId);
+ $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);
- $key = $this->getKey($keyDir . $keyId);
+ $key = $this->getKey($keyDir . $keyId)['key'];
}
- return $key;
+ return base64_decode($key);
}
/**
@@ -108,7 +119,7 @@ class Storage implements IStorage {
*/
public function getSystemUserKey($keyId, $encryptionModuleId) {
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
- return $this->getKey($path);
+ return base64_decode($this->getKeyWithUid($path, null));
}
/**
@@ -116,7 +127,10 @@ class Storage implements IStorage {
*/
public function setUserKey($uid, $keyId, $key, $encryptionModuleId) {
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
- return $this->setKey($path, $key);
+ return $this->setKey($path, [
+ 'key' => base64_encode($key),
+ 'uid' => $uid,
+ ]);
}
/**
@@ -124,7 +138,9 @@ class Storage implements IStorage {
*/
public function setFileKey($path, $keyId, $key, $encryptionModuleId) {
$keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
- return $this->setKey($keyDir . $keyId, $key);
+ return $this->setKey($keyDir . $keyId, [
+ 'key' => base64_encode($key),
+ ]);
}
/**
@@ -132,7 +148,10 @@ class Storage implements IStorage {
*/
public function setSystemUserKey($keyId, $key, $encryptionModuleId) {
$path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
- return $this->setKey($path, $key);
+ return $this->setKey($path, [
+ 'key' => base64_encode($key),
+ 'uid' => null,
+ ]);
}
/**
@@ -200,19 +219,106 @@ class Storage implements IStorage {
}
/**
+ * @param string $path
+ * @param string|null $uid
+ * @return string
+ * @throws ServerNotAvailableException
+ *
+ * Small helper function to fetch the key and verify the value for user and system keys
+ */
+ private function getKeyWithUid(string $path, ?string $uid): string {
+ $data = $this->getKey($path);
+
+ if (!isset($data['key'])) {
+ throw new ServerNotAvailableException('Key is invalid');
+ }
+
+ if ($data['key'] === '') {
+ return '';
+ }
+
+ if (!array_key_exists('uid', $data) || $data['uid'] !== $uid) {
+ // If the migration is done we error out
+ $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
+ if (version_compare($versionFromBeforeUpdate, '20.0.0.1', '<=')) {
+ return $data['key'];
+ }
+
+ if ($this->config->getSystemValueBool('encryption.key_storage_migrated', true)) {
+ throw new ServerNotAvailableException('Key has been modified');
+ } else {
+ //Otherwise we migrate
+ $data['uid'] = $uid;
+ $this->setKey($path, $data);
+ }
+ }
+
+ return $data['key'];
+ }
+
+ /**
* read key from hard disk
*
* @param string $path to key
- * @return string
+ * @return array containing key as base64encoded key, and possible the uid
*/
- private function getKey($path) {
- $key = '';
+ private function getKey($path): array {
+ $key = [
+ 'key' => '',
+ ];
if ($this->view->file_exists($path)) {
if (isset($this->keyCache[$path])) {
$key = $this->keyCache[$path];
} else {
- $key = $this->view->file_get_contents($path);
+ $data = $this->view->file_get_contents($path);
+
+ // Version <20.0.0.1 doesn't have this
+ $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
+ if (version_compare($versionFromBeforeUpdate, '20.0.0.1', '<=')) {
+ $key = [
+ 'key' => base64_encode($data),
+ ];
+ } else {
+ if ($this->config->getSystemValueBool('encryption.key_storage_migrated', true)) {
+ try {
+ $clearData = $this->crypto->decrypt($data);
+ } catch (\Exception $e) {
+ throw new ServerNotAvailableException('Could not decrypt key', 0, $e);
+ }
+
+ $dataArray = json_decode($clearData, true);
+ if ($dataArray === null) {
+ throw new ServerNotAvailableException('Invalid encryption key');
+ }
+
+ $key = $dataArray;
+ } else {
+ /*
+ * Even if not all keys are migrated we should still try to decrypt it (in case some have moved).
+ * However it is only a failure now if it is an array and decryption fails
+ */
+ $fallback = false;
+ try {
+ $clearData = $this->crypto->decrypt($data);
+ } catch (\Exception $e) {
+ $fallback = true;
+ }
+
+ if (!$fallback) {
+ $dataArray = json_decode($clearData, true);
+ if ($dataArray === null) {
+ throw new ServerNotAvailableException('Invalid encryption key');
+ }
+ $key = $dataArray;
+ } else {
+ $key = [
+ 'key' => base64_encode($data),
+ ];
+ }
+ }
+ }
+
$this->keyCache[$path] = $key;
}
}
@@ -225,13 +331,23 @@ class Storage implements IStorage {
*
*
* @param string $path path to key directory
- * @param string $key key
+ * @param array $key key
* @return bool
*/
private function setKey($path, $key) {
$this->keySetPreparation(dirname($path));
- $result = $this->view->file_put_contents($path, $key);
+ $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
+ if (version_compare($versionFromBeforeUpdate, '20.0.0.1', '<=')) {
+ // Only store old format if this happens during the migration.
+ // TODO: Remove for 21
+ $data = base64_decode($key['key']);
+ } else {
+ // Wrap the data
+ $data = $this->crypto->encrypt(json_encode($key));
+ }
+
+ $result = $this->view->file_put_contents($path, $data);
if (is_int($result) && $result > 0) {
$this->keyCache[$path] = $key;
diff --git a/lib/private/Repair.php b/lib/private/Repair.php
index 6151812d316..4ad21e74238 100644
--- a/lib/private/Repair.php
+++ b/lib/private/Repair.php
@@ -49,6 +49,7 @@ use OC\Repair\NC16\CleanupCardDAVPhotoCache;
use OC\Repair\NC16\ClearCollectionsAccessCache;
use OC\Repair\NC18\ResetGeneratedAvatarFlag;
use OC\Repair\NC20\EncryptionLegacyCipher;
+use OC\Repair\NC20\EncryptionMigration;
use OC\Repair\OldGroupMembershipShares;
use OC\Repair\Owncloud\DropAccountTermsTable;
use OC\Repair\Owncloud\SaveAccountsTableData;
@@ -158,6 +159,7 @@ class Repair implements IOutput {
new ClearCollectionsAccessCache(\OC::$server->getConfig(), \OC::$server->query(IManager::class)),
\OC::$server->query(ResetGeneratedAvatarFlag::class),
\OC::$server->query(EncryptionLegacyCipher::class),
+ \OC::$server->query(EncryptionMigration::class),
];
}
diff --git a/lib/private/Repair/NC20/EncryptionMigration.php b/lib/private/Repair/NC20/EncryptionMigration.php
new file mode 100644
index 00000000000..6d5c2dc0c58
--- /dev/null
+++ b/lib/private/Repair/NC20/EncryptionMigration.php
@@ -0,0 +1,62 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OC\Repair\NC20;
+
+use OCP\Encryption\IManager;
+use OCP\IConfig;
+use OCP\Migration\IOutput;
+use OCP\Migration\IRepairStep;
+
+class EncryptionMigration implements IRepairStep {
+
+ /** @var IConfig */
+ private $config;
+ /** @var IManager */
+ private $manager;
+
+ public function __construct(IConfig $config,
+ IManager $manager) {
+ $this->config = $config;
+ $this->manager = $manager;
+ }
+
+ public function getName(): string {
+ return 'Check encryption key format';
+ }
+
+ private function shouldRun(): bool {
+ $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
+ return version_compare($versionFromBeforeUpdate, '20.0.0.1', '<=');
+ }
+
+ public function run(IOutput $output): void {
+ if ($this->manager->isEnabled()) {
+ if ($this->config->getSystemValue('encryption.key_storage_migrated', '') === '') {
+ $this->config->setSystemValue('encryption.key_storage_migrated', false);
+ }
+ }
+ }
+}
diff --git a/lib/private/Server.php b/lib/private/Server.php
index 9b452f21ce1..947ba2434b0 100644
--- a/lib/private/Server.php
+++ b/lib/private/Server.php
@@ -348,7 +348,7 @@ class Server extends ServerContainer implements IServerContainer {
$c->getConfig()
);
- return new Encryption\Keys\Storage($view, $util);
+ return new Encryption\Keys\Storage($view, $util, $c->getCrypto(), $c->getConfig());
});
/** @deprecated 20.0.0 */
$this->registerDeprecatedAlias('TagMapper', TagMapper::class);