From 094213e23171c274e0c88fab02ad2c23d37217ce Mon Sep 17 00:00:00 2001 From: Sam Tuke Date: Thu, 24 Jan 2013 18:37:34 +0000 Subject: Fixed many coding guidelines issues Continued work on upgrade path via login hook listener New spec file with notes --- apps/files_encryption/lib/session.php | 47 ++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'apps/files_encryption/lib/session.php') diff --git a/apps/files_encryption/lib/session.php b/apps/files_encryption/lib/session.php index 85d533fde7a..4abc8be689f 100644 --- a/apps/files_encryption/lib/session.php +++ b/apps/files_encryption/lib/session.php @@ -29,11 +29,11 @@ namespace OCA\Encryption; class Session { /** - * @brief Sets user id for session and triggers emit + * @brief Sets user private key to session * @return bool * */ - public function setPrivateKey( $privateKey, $userId ) { + public function setPrivateKey( $privateKey ) { $_SESSION['privateKey'] = $privateKey; @@ -42,15 +42,15 @@ class Session { } /** - * @brief Gets user id for session and triggers emit + * @brief Gets user private key from session * @returns string $privateKey The user's plaintext private key * */ - public function getPrivateKey( $userId ) { + public function getPrivateKey() { if ( - isset( $_SESSION['privateKey'] ) - && !empty( $_SESSION['privateKey'] ) + isset( $_SESSION['privateKey'] ) + && !empty( $_SESSION['privateKey'] ) ) { return $_SESSION['privateKey']; @@ -62,5 +62,40 @@ class Session { } } + + /** + * @brief Sets user legacy key to session + * @return bool + * + */ + public function setLegacyKey( $legacyKey ) { + + $_SESSION['legacyKey'] = $LegacyKey; + + return true; + + } + + /** + * @brief Gets user legacy key from session + * @returns string $legacyKey The user's plaintext legacy key + * + */ + public function getLegacyKey() { + + if ( + isset( $_SESSION['legacyKey'] ) + && !empty( $_SESSION['legacyKey'] ) + ) { + + return $_SESSION['legacyKey']; + + } else { + + return false; + + } + + } } \ No newline at end of file -- cgit v1.2.3 From 06847f609b09f118b552d70e6f837a92008db570 Mon Sep 17 00:00:00 2001 From: Sam Tuke Date: Thu, 31 Jan 2013 19:40:51 +0000 Subject: Improved support for detecting and recrypting legacy files. Bugs remain. --- apps/files_encryption/hooks/hooks.php | 4 ++-- apps/files_encryption/lib/crypt.php | 13 ++++++------- apps/files_encryption/lib/session.php | 2 +- apps/files_encryption/lib/util.php | 35 ++++++++++++++++++++++++++++------- 4 files changed, 37 insertions(+), 17 deletions(-) (limited to 'apps/files_encryption/lib/session.php') diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php index dafa14fc000..cb9993b2389 100644 --- a/apps/files_encryption/hooks/hooks.php +++ b/apps/files_encryption/hooks/hooks.php @@ -70,7 +70,7 @@ class Hooks { $view1->file_exists( 'encryption.key' ) && $encLegacyKey = $view1->file_get_contents( 'encryption.key' ) ) { - + $plainLegacyKey = Crypt::legacyDecrypt( $encLegacyKey, $params['password'] ); $session->setLegacyKey( $plainLegacyKey ); @@ -87,7 +87,7 @@ class Hooks { ) { \OC_Log::write( - 'Encryption library', 'Encryption of file belonging to "' . $params['uid'] . '" was started at login' + 'Encryption library', 'Encryption of existing files belonging to "' . $params['uid'] . '" started at login' , \OC_Log::INFO ); diff --git a/apps/files_encryption/lib/crypt.php b/apps/files_encryption/lib/crypt.php index 106b757307d..6fbbd412b89 100755 --- a/apps/files_encryption/lib/crypt.php +++ b/apps/files_encryption/lib/crypt.php @@ -184,19 +184,18 @@ class Crypt { * @brief Check if a file is encrypted via legacy system * @return true / false */ - public static function isLegacyEncryptedContent( $content ) { + public static function isLegacyEncryptedContent( $data, $path ) { // Fetch all file metadata from DB - $metadata = \OC\Files\Filesystem::getFileInfo( $content, '' ); - + $metadata = \OC\Files\Filesystem::getFileInfo( $path, '' ); + // If a file is flagged with encryption in DB, but isn't a // valid content + IV combination, it's probably using the // legacy encryption system if ( - $content - and isset( $metadata['encrypted'] ) - and $metadata['encrypted'] === true - and ! self::isCatfile( $content ) + isset( $metadata['encrypted'] ) + and $metadata['encrypted'] === true + and ! self::isCatfile( $data ) ) { return true; diff --git a/apps/files_encryption/lib/session.php b/apps/files_encryption/lib/session.php index 4abc8be689f..bda22ee3a03 100644 --- a/apps/files_encryption/lib/session.php +++ b/apps/files_encryption/lib/session.php @@ -70,7 +70,7 @@ class Session { */ public function setLegacyKey( $legacyKey ) { - $_SESSION['legacyKey'] = $LegacyKey; + $_SESSION['legacyKey'] = $legacyKey; return true; diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index 2a69bba43c9..b1c128cf8c4 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -37,8 +37,9 @@ namespace OCA\Encryption; /** * @brief Class for utilities relating to encrypted file storage system - * @param $view OC_FilesystemView object, expected to have OC '/' as root path - * @param $client flag indicating status of client side encryption. Currently + * @param OC_FilesystemView $view expected to have OC '/' as root path + * @param string $userId ID of the logged in user + * @param int $client indicating status of client side encryption. Currently * unused, likely to become obsolete shortly */ @@ -262,17 +263,25 @@ class Util { } elseif ( $this->view->is_file( $filePath ) ) { // Disable proxies again, some- - // how they get re-enabled :/ + // where they got re-enabled :/ \OC_FileProxy::$enabled = false; + $data = $this->view->file_get_contents( $filePath ); + // If the file is encrypted - if ( Keymanager::getFileKey( $this->view, $this->userId, $file ) ) { + // NOTE: If the userId is + // empty or not set, file will + // detected as plain + if ( + Keymanager::getFileKey( $this->view, $this->userId, $file ) + && Crypt::isCatfile( $filePath ) + ) { $found['encrypted'][] = array( 'name' => $file, 'path' => $filePath ); // If the file uses old // encryption system - } elseif ( Crypt::isLegacyEncryptedContent( $this->view->file_get_contents( $filePath ) ) ) { + } elseif ( Crypt::isLegacyEncryptedContent( $this->view->file_get_contents( $filePath ), $filePath ) ) { $found['legacy'][] = array( 'name' => $file, 'path' => $filePath ); @@ -355,11 +364,16 @@ class Util { $sliced = array_slice( $split, 2 ); $relPath = implode( '/', $sliced ); - // Save catfile + // Save keyfile Keymanager::setFileKey( $this->view, $relPath, $this->userId, $encrypted['key'] ); // Overwrite the existing file with the encrypted one $this->view->file_put_contents( $plainFile['path'], $encrypted['data'] ); + + $size = strlen( $encrypted['data'] ); + + // Add the file to the cache + \OC\Files\Filesystem::putFileInfo( $plainFile['path'], array( 'encrypted'=>true, 'size' => $size ), '' ); } @@ -370,6 +384,8 @@ class Util { && ! empty( $newPassphrase ) ) { + trigger_error("LEGACY FOUND"); + foreach ( $found['legacy'] as $legacyFilePath ) { // Fetch data from file @@ -378,11 +394,16 @@ class Util { // Recrypt data, generate catfile $recrypted = Crypt::legacyKeyRecryptKeyfile( $legacyData, $legacyPassphrase, $publicKey, $newPassphrase ); - // Save catfile + // Save keyfile Keymanager::setFileKey( $this->view, $plainFile['path'], $this->userId, $recrypted['key'] ); // Overwrite the existing file with the encrypted one $this->view->file_put_contents( $plainFile['path'], $recrypted['data'] ); + + $size = strlen( $recrypted['data'] ); + + // Add the file to the cache + \OC\Files\Filesystem::putFileInfo( $plainFile['path'], array( 'encrypted'=>true, 'size' => $size ), '' ); } -- cgit v1.2.3 From 20b1d12cbfc65b604acdaac84272f6af8b0d7be4 Mon Sep 17 00:00:00 2001 From: Sam Tuke Date: Tue, 5 Feb 2013 17:28:26 +0000 Subject: Fixed comment typo, wrapped return value conditional on var assignment in session{} --- apps/files_encryption/lib/session.php | 6 ++++-- apps/files_encryption/lib/stream.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'apps/files_encryption/lib/session.php') diff --git a/apps/files_encryption/lib/session.php b/apps/files_encryption/lib/session.php index bda22ee3a03..769a40b359f 100644 --- a/apps/files_encryption/lib/session.php +++ b/apps/files_encryption/lib/session.php @@ -70,9 +70,11 @@ class Session { */ public function setLegacyKey( $legacyKey ) { - $_SESSION['legacyKey'] = $legacyKey; + if ( $_SESSION['legacyKey'] = $legacyKey ) { - return true; + return true; + + } } diff --git a/apps/files_encryption/lib/stream.php b/apps/files_encryption/lib/stream.php index 4102a681d7f..d4b993b4c06 100644 --- a/apps/files_encryption/lib/stream.php +++ b/apps/files_encryption/lib/stream.php @@ -347,7 +347,7 @@ class Stream { // // // Make sure we always start on a block start if ( 0 != ( $pointer % 8192 ) ) { - // if the current positoin of + // if the current position of // file indicator is not aligned to a 8192 byte block, fix it // so that it is -- cgit v1.2.3