summaryrefslogtreecommitdiffstats
path: root/apps/encryption/lib
diff options
context:
space:
mode:
authorClark Tomlinson <fallen013@gmail.com>2015-03-30 17:01:50 -0400
committerThomas Müller <thomas.mueller@tmit.eu>2015-04-07 13:30:28 +0200
commite6dc6944c2cd92617818a2fd029ecdb1de5ab663 (patch)
tree3f370c438fd6d175c8ec988ad6596f697c2e1b84 /apps/encryption/lib
parent1b42b492dce562596b8b57a11546728f697c4f38 (diff)
downloadnextcloud-server-e6dc6944c2cd92617818a2fd029ecdb1de5ab663.tar.gz
nextcloud-server-e6dc6944c2cd92617818a2fd029ecdb1de5ab663.zip
moving methods to their final places
and updating test some.
Diffstat (limited to 'apps/encryption/lib')
-rw-r--r--apps/encryption/lib/keymanager.php24
-rw-r--r--apps/encryption/lib/recovery.php85
-rw-r--r--apps/encryption/lib/util.php97
3 files changed, 103 insertions, 103 deletions
diff --git a/apps/encryption/lib/keymanager.php b/apps/encryption/lib/keymanager.php
index d9b670c3f5a..cd983be17f9 100644
--- a/apps/encryption/lib/keymanager.php
+++ b/apps/encryption/lib/keymanager.php
@@ -27,8 +27,6 @@ use OC\Encryption\Exceptions\PrivateKeyMissingException;
use OC\Encryption\Exceptions\PublicKeyMissingException;
use OCA\Encryption\Crypto\Crypt;
use OCP\Encryption\Keys\IStorage;
-use OCP\ICache;
-use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IUserSession;
@@ -86,6 +84,10 @@ class KeyManager {
* @var ILogger
*/
private $log;
+ /**
+ * @var Recovery
+ */
+ private $recovery;
/**
* @param IStorage $keyStorage
@@ -94,6 +96,7 @@ class KeyManager {
* @param IUserSession $userSession
* @param \OCP\ISession $session
* @param ILogger $log
+ * @param Recovery $recovery
*/
public function __construct(
IStorage $keyStorage,
@@ -101,7 +104,9 @@ class KeyManager {
IConfig $config,
IUserSession $userSession,
ISession $session,
- ILogger $log) {
+ ILogger $log,
+ Recovery $recovery
+ ) {
self::$session = $session;
$this->keyStorage = $keyStorage;
@@ -115,7 +120,9 @@ class KeyManager {
if (empty($this->publicShareKeyId)) {
$this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8);
- $this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId);
+ $this->config->setAppValue('encryption',
+ 'publicShareKeyId',
+ $this->publicShareKeyId);
$keyPair = $this->crypt->createKeyPair();
@@ -125,9 +132,11 @@ class KeyManager {
$keyPair['publicKey']);
// Encrypt private key empty passphrase
- $encryptedKey = $this->crypt->symmetricEncryptFileContent($keyPair['privateKey'], '');
+ $encryptedKey = $this->crypt->symmetricEncryptFileContent($keyPair['privateKey'],
+ '');
if ($encryptedKey) {
- $this->keyStorage->setSystemUserKey($this->publicShareKeyId . '.privateKey', $encryptedKey);
+ $this->keyStorage->setSystemUserKey($this->publicShareKeyId . '.privateKey',
+ $encryptedKey);
} else {
$this->log->error('Could not create public share keys');
}
@@ -136,6 +145,7 @@ class KeyManager {
$this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false;
$this->log = $log;
+ $this->recovery = $recovery;
}
/**
@@ -386,7 +396,7 @@ class KeyManager {
$this->setPrivateKey($user, $encryptedKey);
if ($recoveryPassword) { // if recovery key is set we can re-encrypt the key files
- $util->recoverUsersFiles($recoveryPassword);
+ $this->recovery->recoverUsersFiles($recoveryPassword);
}
} else {
$this->log->error('Encryption Could not update users encryption password');
diff --git a/apps/encryption/lib/recovery.php b/apps/encryption/lib/recovery.php
index 457184b4b96..376d3ef83ba 100644
--- a/apps/encryption/lib/recovery.php
+++ b/apps/encryption/lib/recovery.php
@@ -29,6 +29,7 @@ use OCP\IUser;
use OCP\IUserSession;
use OCP\PreConditionNotMetException;
use OCP\Security\ISecureRandom;
+use OCP\Share;
class Recovery {
@@ -54,10 +55,12 @@ class Recovery {
*/
private $config;
/**
- * @var IEncryptionKeyStorage
+ * @var IStorage
*/
private $keyStorage;
+ private $recoveryKeyId;
+
/**
* @param IUserSession $user
* @param Crypt $crypt
@@ -90,7 +93,9 @@ class Recovery {
if ($recoveryKeyId === null) {
$recoveryKeyId = $this->random->getLowStrengthGenerator();
- $appConfig->setAppValue('encryption', 'recoveryKeyId', $recoveryKeyId);
+ $appConfig->setAppValue('encryption',
+ 'recoveryKeyId',
+ $recoveryKeyId);
}
$keyManager = $this->keyManager;
@@ -98,7 +103,9 @@ class Recovery {
if (!$keyManager->recoveryKeyExists()) {
$keyPair = $this->crypt->createKeyPair();
- return $this->keyManager->storeKeyPair($this->user->getUID(), $password, $keyPair);
+ return $this->keyManager->storeKeyPair($this->user->getUID(),
+ $password,
+ $keyPair);
}
if ($keyManager->checkRecoveryPassword($password)) {
@@ -143,6 +150,7 @@ class Recovery {
return ($recoveryMode === '1');
}
+
/**
* @param $enabled
* @return bool
@@ -165,12 +173,79 @@ class Recovery {
* @param $recoveryPassword
*/
public function recoverUsersFiles($recoveryPassword) {
- // todo: get system private key here
-// $this->keyManager->get
+ $encryptedKey = $this->keyManager->getSystemPrivateKey();
+
$privateKey = $this->crypt->decryptPrivateKey($encryptedKey,
$recoveryPassword);
$this->recoverAllFiles('/', $privateKey);
}
+ /**
+ * @param $path
+ * @param $privateKey
+ */
+ private function recoverAllFiles($path, $privateKey) {
+ $dirContent = $this->files->getDirectoryContent($path);
+
+ foreach ($dirContent as $item) {
+ // Get relative path from encryption/keyfiles
+ $filePath = substr($item['path'], strlen('encryption/keys'));
+ if ($this->files->is_dir($this->user->getUID() . '/files' . '/' . $filePath)) {
+ $this->recoverAllFiles($filePath . '/', $privateKey);
+ } else {
+ $this->recoverFile($filePath, $privateKey);
+ }
+ }
+
+ }
+
+ /**
+ * @param $filePath
+ * @param $privateKey
+ */
+ private function recoverFile($filePath, $privateKey) {
+ $sharingEnabled = Share::isEnabled();
+ $uid = $this->user->getUID();
+
+ // Find out who, if anyone, is sharing the file
+ if ($sharingEnabled) {
+ $result = Share::getUsersSharingFile($filePath,
+ $uid,
+ true);
+ $userIds = $result['users'];
+ $userIds[] = 'public';
+ } else {
+ $userIds = [
+ $uid,
+ $this->recoveryKeyId
+ ];
+ }
+ $filteredUids = $this->filterShareReadyUsers($userIds);
+
+ // Decrypt file key
+ $encKeyFile = $this->keyManager->getFileKey($filePath,
+ $uid);
+
+ $shareKey = $this->keyManager->getShareKey($filePath,
+ $uid);
+
+ $plainKeyFile = $this->crypt->multiKeyDecrypt($encKeyFile,
+ $shareKey,
+ $privateKey);
+
+ // Encrypt the file key again to all users, this time with the new publick keyt for the recovered user
+ $userPublicKeys = $this->keyManager->getPublicKeys($filteredUids['ready']);
+ $multiEncryptionKey = $this->crypt->multiKeyEncrypt($plainKeyFile,
+ $userPublicKeys);
+
+ $this->keyManager->setFileKey($multiEncryptionKey['data'],
+ $uid);
+
+ $this->keyManager->setShareKey($filePath,
+ $uid,
+ $multiEncryptionKey['keys']);
+ }
+
+
}
diff --git a/apps/encryption/lib/util.php b/apps/encryption/lib/util.php
index 5fc08c6cc7d..45891be5dad 100644
--- a/apps/encryption/lib/util.php
+++ b/apps/encryption/lib/util.php
@@ -26,7 +26,6 @@ namespace OCA\Encryption;
use OC\Files\Filesystem;
use OC\Files\View;
use OCA\Encryption\Crypto\Crypt;
-use OCA\Files_Versions\Storage;
use OCP\App;
use OCP\IConfig;
use OCP\ILogger;
@@ -41,10 +40,6 @@ class Util {
*/
private $files;
/**
- * @var Filesystem
- */
- private $filesystem;
- /**
* @var Crypt
*/
private $crypt;
@@ -69,24 +64,20 @@ class Util {
* Util constructor.
*
* @param View $files
- * @param Filesystem $filesystem
* @param Crypt $crypt
* @param KeyManager $keyManager
* @param ILogger $logger
* @param IUserSession $userSession
* @param IConfig $config
*/
- public function __construct(
- View $files,
- Filesystem $filesystem,
- Crypt $crypt,
- KeyManager $keyManager,
- ILogger $logger,
- IUserSession $userSession,
- IConfig $config
+ public function __construct(View $files,
+ Crypt $crypt,
+ KeyManager $keyManager,
+ ILogger $logger,
+ IUserSession $userSession,
+ IConfig $config
) {
$this->files = $files;
- $this->filesystem = $filesystem;
$this->crypt = $crypt;
$this->keyManager = $keyManager;
$this->logger = $logger;
@@ -95,16 +86,6 @@ class Util {
}
/**
- * @param $filePath
- * @return array
- */
- private function splitPath($filePath) {
- $normalized = $this->filesystem->normalizePath($filePath);
-
- return explode('/', $normalized);
- }
-
- /**
* @return bool
*/
public function recoveryEnabledForUser() {
@@ -154,71 +135,5 @@ class Util {
return $this->files->file_exists($uid . '/files');
}
- /**
- * @param $path
- * @param $privateKey
- */
- private function recoverAllFiles($path, $privateKey) {
- // Todo relocate to storage
- $dirContent = $this->files->getDirectoryContent($path);
-
- foreach ($dirContent as $item) {
- // Get relative path from encryption/keyfiles
- $filePath = substr($item['path'], strlen('encryption/keys'));
- if ($this->files->is_dir($this->user->getUID() . '/files' . '/' . $filePath)) {
- $this->recoverAllFiles($filePath . '/', $privateKey);
- } else {
- $this->recoverFile($filePath, $privateKey);
- }
- }
-
- }
-
- /**
- * @param $filePath
- * @param $privateKey
- */
- private function recoverFile($filePath, $privateKey) {
- $sharingEnabled = Share::isEnabled();
- $uid = $this->user->getUID();
-
- // Find out who, if anyone, is sharing the file
- if ($sharingEnabled) {
- $result = Share::getUsersSharingFile($filePath,
- $uid,
- true);
- $userIds = $result['users'];
- $userIds[] = 'public';
- } else {
- $userIds = [
- $uid,
- $this->recoveryKeyId
- ];
- }
- $filteredUids = $this->filterShareReadyUsers($userIds);
-
- // Decrypt file key
- $encKeyFile = $this->keyManager->getFileKey($filePath,
- $uid);
-
- $shareKey = $this->keyManager->getShareKey($filePath,
- $uid);
-
- $plainKeyFile = $this->crypt->multiKeyDecrypt($encKeyFile,
- $shareKey,
- $privateKey);
-
- // Encrypt the file key again to all users, this time with the new publick keyt for the recovered user
- $userPublicKeys = $this->keyManager->getPublicKeys($filteredUids['ready']);
- $multiEncryptionKey = $this->crypt->multiKeyEncrypt($plainKeyFile,
- $userPublicKeys);
-
- $this->keyManager->setFileKey($multiEncryptionKey['data'],
- $uid);
-
- $this->keyManager->setShareKey($filePath,
- $uid,
- $multiEncryptionKey['keys']);
- }
}