diff options
author | Bjoern Schiessle <schiessle@owncloud.com> | 2013-07-29 17:06:05 +0200 |
---|---|---|
committer | Bjoern Schiessle <schiessle@owncloud.com> | 2013-07-29 17:06:05 +0200 |
commit | b6fa0e4eefb332dc1fb9b45df50de4621ed8e6bd (patch) | |
tree | 2ca892180568bd5c76facbfd9e6762621485a5dd /apps | |
parent | 830f5d24c77b863bd49eda0cbc4ba812add8065d (diff) | |
download | nextcloud-server-b6fa0e4eefb332dc1fb9b45df50de4621ed8e6bd.tar.gz nextcloud-server-b6fa0e4eefb332dc1fb9b45df50de4621ed8e6bd.zip |
working decrypt files method
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files_encryption/hooks/hooks.php | 13 | ||||
-rw-r--r-- | apps/files_encryption/lib/util.php | 102 |
2 files changed, 97 insertions, 18 deletions
diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php index b2a17f6bca5..d9c3f49b736 100644 --- a/apps/files_encryption/hooks/hooks.php +++ b/apps/files_encryption/hooks/hooks.php @@ -62,18 +62,7 @@ class Hooks { return false;
}
- $encryptedKey = Keymanager::getPrivateKey($view, $params['uid']);
-
- $privateKey = Crypt::decryptPrivateKey($encryptedKey, $params['password']);
-
- if ($privateKey === false) {
- \OCP\Util::writeLog('Encryption library', 'Private key for user "' . $params['uid']
- . '" is not valid! Maybe the user password was changed from outside if so please change it back to gain access', \OCP\Util::ERROR);
- }
-
- $session = new \OCA\Encryption\Session($view);
-
- $session->setPrivateKey($privateKey);
+ $session = $util->initEncryption($params);
// Check if first-run file migration has already been performed
$ready = false;
diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index 50e823585d7..03e2fae4c65 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -661,6 +661,69 @@ class Util { } } + + /** + * @brief Decrypt all files + * @return bool + */ + public function decryptAll() { + + $found = $this->findEncFiles($this->userId . '/files'); + + if ($found) { + + // Disable proxy to prevent file being encrypted twice + \OC_FileProxy::$enabled = false; + + // Encrypt unencrypted files + foreach ($found['encrypted'] as $encryptedFile) { + + //relative to data/<user>/file + $relPath = Helper::stripUserFilesPath($encryptedFile['path']); + + //relative to /data + $rawPath = $encryptedFile['path']; + + // Open enc file handle for binary reading + $encHandle = fopen('crypt://' . $rawPath, 'rb'); + + // Open plain file handle for binary writing, with same filename as original plain file + $plainHandle = $this->view->fopen($rawPath . '.part', 'wb'); + + // Move plain file to a temporary location + $size = stream_copy_to_stream($encHandle, $plainHandle); + + fclose($encHandle); + fclose($plainHandle); + + $fakeRoot = $this->view->getRoot(); + $this->view->chroot('/' . $this->userId . '/files'); + + $this->view->rename($relPath . '.part', $relPath); + + $this->view->chroot($fakeRoot); + + // Add the file to the cache + \OC\Files\Filesystem::putFileInfo($relPath, array( + 'encrypted' => false, + 'size' => $size, + 'unencrypted_size' => $size + )); + } + + $this->view->deleteAll($this->keyfilesPath); + $this->view->deleteAll($this->shareKeysPath); + + \OC_FileProxy::$enabled = true; + + // If files were found, return true + return true; + } else { + + // If no files were found, return false + return false; + } + } /** * @brief Encrypt all files in a directory @@ -672,7 +735,9 @@ class Util { */ public function encryptAll($dirPath, $legacyPassphrase = null, $newPassphrase = null) { - if ($found = $this->findEncFiles($dirPath)) { + $found = $this->findEncFiles($dirPath); + + if ($found) { // Disable proxy to prevent file being encrypted twice \OC_FileProxy::$enabled = false; @@ -690,12 +755,13 @@ class Util { $plainHandle = $this->view->fopen($rawPath, 'rb'); // Open enc file handle for binary writing, with same filename as original plain file - $encHandle = fopen('crypt://' . $relPath . '.part', 'wb'); + $encHandle = fopen('crypt://' . $rawPath . '.part', 'wb'); // Move plain file to a temporary location $size = stream_copy_to_stream($plainHandle, $encHandle); fclose($encHandle); + fclose($plainHandle); $fakeRoot = $this->view->getRoot(); $this->view->chroot('/' . $this->userId . '/files'); @@ -706,10 +772,10 @@ class Util { // Add the file to the cache \OC\Files\Filesystem::putFileInfo($relPath, array( - 'encrypted' => true, - 'size' => $size, - 'unencrypted_size' => $size - )); + 'encrypted' => true, + 'size' => $size, + 'unencrypted_size' => $size + )); } // Encrypt legacy encrypted files @@ -1579,4 +1645,28 @@ class Util { return false; } + /** + * @brief decrypt private key and add it to the current session + * @param array $params with 'uid' and 'password' + * @return mixed session or false + */ + public function initEncryption($params) { + + $encryptedKey = Keymanager::getPrivateKey($this->view, $params['uid']); + + $privateKey = Crypt::decryptPrivateKey($encryptedKey, $params['password']); + + if ($privateKey === false) { + \OCP\Util::writeLog('Encryption library', 'Private key for user "' . $params['uid'] + . '" is not valid! Maybe the user password was changed from outside if so please change it back to gain access', \OCP\Util::ERROR); + return false; + } + + $session = new \OCA\Encryption\Session($this->view); + + $session->setPrivateKey($privateKey); + + return $session; + } + } |