]> source.dussan.org Git - nextcloud-server.git/commitdiff
Adding sharing support: added new method in Keymanager setShareKey()
authorSam Tuke <samtuke@owncloud.com>
Mon, 14 Jan 2013 19:07:28 +0000 (19:07 +0000)
committerSam Tuke <samtuke@owncloud.com>
Mon, 14 Jan 2013 19:07:28 +0000 (19:07 +0000)
Added notes in proxy{} and stream{} pointing to share support

apps/files_encryption/appinfo/app.php
apps/files_encryption/hooks/hooks.php
apps/files_encryption/lib/keymanager.php
apps/files_encryption/lib/proxy.php
apps/files_encryption/lib/stream.php
apps/files_encryption/test/keymanager.php

index 31b430d37a9fbc5c177abff55432689ba445d581..cc78402d1d052cdf9e368fda85f4a41a96c1fd79 100644 (file)
@@ -10,10 +10,18 @@ OC::$CLASSPATH['OCA\Encryption\Session'] = 'apps/files_encryption/lib/session.ph
 
 OC_FileProxy::register( new OCA\Encryption\Proxy() );
 
+// User-related hooks
 OCP\Util::connectHook( 'OC_User','post_login', 'OCA\Encryption\Hooks', 'login' );
-OCP\Util::connectHook( 'OC_Webdav_Properties', 'update', 'OCA\Encryption\Hooks', 'updateKeyfile' );
 OCP\Util::connectHook( 'OC_User','post_setPassword','OCA\Encryption\Hooks' ,'setPassphrase' );
 
+// Sharing-related hooks
+OCP\Util::connectHook( 'OCP\Share','post_shared','OCA\Encryption\Hooks' ,'postShared' );
+OCP\Util::connectHook( 'OCP\Share','pre_unshare','OCA\Encryption\Hooks' ,'preUnshare' );
+OCP\Util::connectHook( 'OCP\Share','pre_unshareAll','OCA\Encryption\Hooks' ,'preUnshareAll' );
+
+// Webdav-related hooks
+OCP\Util::connectHook( 'OC_Webdav_Properties', 'update', 'OCA\Encryption\Hooks', 'updateKeyfile' );
+
 stream_wrapper_register( 'crypt', 'OCA\Encryption\Stream' );
 
 $session = new OCA\Encryption\Session();
@@ -24,7 +32,9 @@ if (
 && OCA\Encryption\Crypt::mode() == 'server' 
 ) {
 
-       // Force the user to re-log in if the encryption key isn't unlocked (happens when a user is logged in before the encryption app is enabled)
+       // Force the user to re-log in if the encryption key isn't unlocked 
+       // (happens when a user is logged in before the encryption app is 
+       // enabled)
        OCP\User::logout();
        
        header( "Location: " . OC::$WEBROOT.'/' );
@@ -33,5 +43,5 @@ if (
 
 }
 
-OCP\App::registerAdmin( 'files_encryption', 'settings');
+OCP\App::registerAdmin( 'files_encryption', 'settings' );
 OCP\App::registerPersonal( 'files_encryption', 'settings-personal' );
\ No newline at end of file
index c2f97247835331d63f189acb864f43616a147d3c..ecceae352bc24b7902edbdc7ed039c435b20fd3e 100644 (file)
@@ -121,8 +121,11 @@ class Hooks {
                        \r
                        if ( isset( $params['properties']['key'] ) ) {\r
                                \r
-                               Keymanager::setFileKey( $params['path'], $params['properties']['key'] );\r
-                       \r
+                               $view = new \OC_FilesystemView( '/' );\r
+                               $userId = \OCP\User::getUser();\r
+                               \r
+                               Keymanager::setFileKey( $view, $params['path'], $userId, $params['properties']['key'] );\r
+                               \r
                        } else {\r
                                \r
                                \OC_Log::write( \r
@@ -138,6 +141,43 @@ class Hooks {
                \r
        }\r
        \r
+       /**\r
+        * @brief \r
+        */\r
+       public static function postShared( $params ) {\r
+               \r
+               // Delete existing catfile\r
+               Keymanager::deleteFileKey(  );\r
+               \r
+               // Generate new catfile and env keys\r
+               Crypt::multiKeyEncrypt( $plainContent, $publicKeys );\r
+               \r
+               // Save env keys to user folders\r
+               \r
+               \r
+       }\r
+       \r
+       /**\r
+        * @brief \r
+        */\r
+       public static function preUnshare( $params ) {\r
+               \r
+               // Delete existing catfile\r
+               \r
+               // Generate new catfile and env keys\r
+               \r
+               // Save env keys to user folders\r
+       }\r
+       \r
+       /**\r
+        * @brief \r
+        */\r
+       public static function preUnshareAll( $params ) {\r
+               \r
+               trigger_error( "preUnshareAll" );\r
+               \r
+       }\r
+       \r
 }\r
 \r
 ?>
\ No newline at end of file
index 706e1c2661e6a263cac03875bd948fb93c67a544..61bc50721ef094b5ea4ac4473134674546f85c92 100755 (executable)
@@ -238,7 +238,7 @@ class Keymanager {
         */\r
        public static function setUserKeys($privatekey, $publickey) {\r
        \r
-               return (self::setPrivateKey($privatekey) && self::setPublicKey($publickey));\r
+               return ( self::setPrivateKey( $privatekey ) && self::setPublicKey( $publickey ) );\r
        \r
        }\r
        \r
@@ -262,6 +262,42 @@ class Keymanager {
                \r
        }\r
        \r
+       /**\r
+        * @note 'shareKey' is a more user-friendly name for env_key\r
+        */\r
+       public static function setShareKey( \OC_FilesystemView $view, $path, $userId, $shareKey ) {\r
+               \r
+               $basePath = '/' . $userId . '/files_encryption/share-keys';\r
+               \r
+               $shareKeyPath = self::keySetPreparation( $view, $path, $basePath, $userId );\r
+               \r
+               return $view->file_put_contents( $basePath . '/' . $shareKeyPath . '.shareKey', $shareKey );\r
+               \r
+       }\r
+       \r
+       /**\r
+        * @brief Make preparations to vars and filesystem for saving a keyfile\r
+        */\r
+       public static function keySetPreparation( \OC_FilesystemView $view, $path, $basePath, $userId ) {\r
+       \r
+               $targetPath = ltrim( $path, '/' );\r
+               \r
+               $path_parts = pathinfo( $targetPath );\r
+               \r
+               // If the file resides within a subdirectory, create it\r
+               if ( \r
+               isset( $path_parts['dirname'] )\r
+               && ! $view->file_exists( $basePath . $path_parts['dirname'] ) \r
+               ) {\r
+               \r
+                       $view->mkdir( $basePath . $path_parts['dirname'] );\r
+                       \r
+               }\r
+               \r
+               return $targetPath;\r
+       \r
+       }\r
+       \r
        /**\r
         * @brief store file encryption key\r
         *\r
@@ -271,15 +307,16 @@ class Keymanager {
         * @note The keyfile is not encrypted here. Client code must \r
         * asymmetrically encrypt the keyfile before passing it to this method\r
         */\r
-       public static function setFileKey( $path, $key, $view = Null, $dbClassName = '\OC_DB') {\r
-\r
-               $targetPath = ltrim(  $path, '/'  );\r
-               $user = \OCP\User::getUser();\r
+       public static function setFileKey( \OC_FilesystemView $view, $path, $userId, $catfile ) {\r
+               \r
+               $basePath = '/' . $userId . '/files_encryption/keyfiles';\r
                \r
-//             // update $keytarget and $user if key belongs to a file shared by someone else\r
+               $targetPath = self::keySetPreparation( $view, $path, $basePath, $userId );\r
+               \r
+//             // update $keytarget and $userId if key belongs to a file shared by someone else\r
 //             $query = $dbClassName::prepare( "SELECT uid_owner, source, target FROM `*PREFIX*sharing` WHERE target = ? AND uid_shared_with = ?" );\r
 //             \r
-//             $result = $query->execute(  array ( '/'.$user.'/files/'.$targetPath, $user ) );\r
+//             $result = $query->execute(  array ( '/'.$userId.'/files/'.$targetPath, $userId ) );\r
 //             \r
 //             if ( $row = $result->fetchRow(  ) ) {\r
 //             \r
@@ -287,7 +324,7 @@ class Keymanager {
 //                     \r
 //                     $targetPath_parts = explode( '/', $targetPath );\r
 //                     \r
-//                     $user = $targetPath_parts[1];\r
+//                     $userId = $targetPath_parts[1];\r
 // \r
 //                     $rootview = new \OC_FilesystemView( '/' );\r
 //                     \r
@@ -299,34 +336,14 @@ class Keymanager {
 //                             \r
 //                     }\r
 //                     \r
-//                     $targetPath = str_replace( '/'.$user.'/files/', '', $targetPath );\r
+//                     $targetPath = str_replace( '/'.$userId.'/files/', '', $targetPath );\r
 //                     \r
 //                     //TODO: check for write permission on shared file once the new sharing API is in place\r
 //                     \r
 //             }\r
                \r
-               $path_parts = pathinfo( $targetPath );\r
-               \r
-               if ( !$view ) {\r
-               \r
-                       $view = new \OC_FilesystemView( '/' );\r
-                       \r
-               }\r
-               \r
-               $view->chroot( '/' . $user . '/files_encryption/keyfiles' );\r
-               \r
-               // If the file resides within a subdirectory, create it\r
-               if ( \r
-               isset( $path_parts['dirname'] )\r
-               && ! $view->file_exists( $path_parts['dirname'] ) \r
-               ) {\r
-               \r
-                       $view->mkdir( $path_parts['dirname'] );\r
-                       \r
-               }\r
-               \r
                // Save the keyfile in parallel directory\r
-               return $view->file_put_contents( '/' . $targetPath . '.key', $key );\r
+               return $view->file_put_contents( $basePath . '/' . $targetPath . '.key', $catfile );\r
                \r
        }\r
        \r
index 52f47dba2940faacfa6625c744695434a9282377..83c5e21c4bd3bc069a4945d0abcf57cc73d051bd 100644 (file)
@@ -101,6 +101,8 @@ class Proxy extends \OC_FileProxy {
                                // Disable encryption proxy to prevent recursive calls
                                \OC_FileProxy::$enabled = false;
                                
+                               # TODO: Check if file is shared, if so, use multiKeyEncrypt
+                               
                                // Encrypt plain data and fetch key
                                $encrypted = Crypt::keyEncryptKeyfile( $data, Keymanager::getPublicKey( $rootView, $userId ) );
                                
@@ -114,10 +116,11 @@ class Proxy extends \OC_FileProxy {
                                $filePath = '/' . implode( '/', $filePath );
                                
                                # TODO: make keyfile dir dynamic from app config
-                               $view = new \OC_FilesystemView( '/' . $userId . '/files_encryption/keyfiles' );
+                               
+                               $view = new \OC_FilesystemView( '/' );
                                
                                // Save keyfile for newly encrypted file in parallel directory tree
-                               Keymanager::setFileKey( $filePath, $encrypted['key'], $view, '\OC_DB' );
+                               Keymanager::setFileKey( $view, $filePath, $userId, $encrypted['key'] );
                                
                                // Update the file cache with file info
                                \OC_FileCache::put( $path, array( 'encrypted'=>true, 'size' => $size ), '' );
@@ -159,6 +162,8 @@ class Proxy extends \OC_FileProxy {
                        
                        $userId = \OCP\USER::getUser();
                        
+                       # TODO: Check if file is shared, if so, use multiKeyDecrypt
+                       
                        $encryptedKeyfile = Keymanager::getFileKey( $view, $userId, $filePath );
 
                        $session = new Session();
index f482e2d75ac13480f293564291bbc8b548aa4ca6..a17a4514faaad46a6910812ef93e8bd0e56bd90c 100644 (file)
@@ -302,8 +302,12 @@ class Stream {
                // Make sure the userId is set
                $this->getuser();
                
+               # TODO: Check if file is shared, if so, use multiKeyEncrypt and
+               # save shareKeys in necessary user directories
+               
                // Get / generate the keyfile for the file we're handling
-               // If we're writing a new file (not overwriting an existing one), save the newly generated keyfile
+               // If we're writing a new file (not overwriting an existing 
+               // one), save the newly generated keyfile
                if ( ! $this->getKey() ) {
                
                        $this->keyfile = Crypt::generateKey();
@@ -312,10 +316,11 @@ class Stream {
                        
                        $this->encKeyfile = Crypt::keyEncrypt( $this->keyfile, $this->publicKey );
                        
-                       // Save the new encrypted file key
-                       Keymanager::setFileKey( $this->rawPath, $this->encKeyfile, new \OC_FilesystemView( '/' ) );
+                       $view = new \OC_FilesystemView( '/' );
+                       $userId = \OCP\User::getUser();
                        
-                       # TODO: move this new OCFSV out of here some how, use DI
+                       // Save the new encrypted file key
+                       Keymanager::setFileKey( $view, $this->rawPath, $userId, $this->encKeyfile );
                        
                }
 
index f02d6eb5f7a873428bae80b3b011b10d24072ed9..bf453fe3163b8455136fc5b501beb451ba54c251 100644 (file)
@@ -79,15 +79,13 @@ class Test_Keymanager extends \PHPUnit_Framework_TestCase {
                # NOTE: This cannot be tested until we are able to break out 
                # of the FileSystemView data directory root
        
-//             $key = Crypt::symmetricEncryptFileContentKeyfile( $this->data, 'hat' );
-//             
-//             $tmpPath = sys_get_temp_dir(). '/' . 'testSetFileKey';
-//             
-//             $view = new \OC_FilesystemView( '/tmp/' );
-//             
-//             //$view = new \OC_FilesystemView( '/' . $this->userId . '/files_encryption/keyfiles' );
-//             
-//             Encryption\Keymanager::setFileKey( $tmpPath, $key['key'], $view );
+               $key = Encryption\Crypt::symmetricEncryptFileContentKeyfile( $this->randomKey, 'hat' );
+               
+               $path = 'unittest-'.time().'txt';
+               
+               //$view = new \OC_FilesystemView( '/' . $this->userId . '/files_encryption/keyfiles' );
+               
+               Encryption\Keymanager::setFileKey( $this->view, $path, $this->userId, $key['key'] );
        
        }