]> source.dussan.org Git - nextcloud-server.git/commitdiff
introduce some encryption exceptions and catch additional error cases
authorBjoern Schiessle <schiessle@owncloud.com>
Wed, 16 Jul 2014 10:06:00 +0000 (12:06 +0200)
committerBjoern Schiessle <schiessle@owncloud.com>
Wed, 16 Jul 2014 10:36:30 +0000 (12:36 +0200)
apps/files_encryption/appinfo/app.php
apps/files_encryption/lib/crypt.php
apps/files_encryption/lib/exceptions.php [new file with mode: 0644]
apps/files_encryption/lib/util.php

index 104e8568caa62f022f865f36b3e6bca25d749904..a90f618e2459c7aa47c710fe5e79dd58258babd8 100644 (file)
@@ -10,6 +10,10 @@ OC::$CLASSPATH['OCA\Encryption\Session'] = 'files_encryption/lib/session.php';
 OC::$CLASSPATH['OCA\Encryption\Capabilities'] = 'files_encryption/lib/capabilities.php';
 OC::$CLASSPATH['OCA\Encryption\Helper'] = 'files_encryption/lib/helper.php';
 
+// Exceptions
+OC::$CLASSPATH['OCA\Encryption\Exceptions\MultiKeyEncryptException'] = 'files_encryption/lib/exceptions.php';
+OC::$CLASSPATH['OCA\Encryption\Exceptions\MultiKeyDecryptException'] = 'files_encryption/lib/exceptions.php';
+
 \OCP\Util::addscript('files_encryption', 'encryption');
 \OCP\Util::addscript('files_encryption', 'detect-migration');
 
index 5632a2bc298d587bb081c7881993beb3635a06a3..18f0224391dbd92e58544cbed0db2433cd0f3d73 100755 (executable)
@@ -358,6 +358,7 @@ class Crypt {
         * @param string $plainContent content to be encrypted\r
         * @param array $publicKeys array keys must be the userId of corresponding user\r
         * @return array keys: keys (array, key = userId), data\r
+        * @throws \OCA\Encryption\Exceptions\\MultiKeyEncryptException if encryption failed\r
         * @note symmetricDecryptFileContent() can decrypt files created using this method\r
         */\r
        public static function multiKeyEncrypt($plainContent, array $publicKeys) {\r
@@ -365,9 +366,7 @@ class Crypt {
                // openssl_seal returns false without errors if $plainContent\r
                // is empty, so trigger our own error\r
                if (empty($plainContent)) {\r
-\r
-                       throw new \Exception('Cannot mutliKeyEncrypt empty plain content');\r
-\r
+                       throw new Exceptions\MultiKeyEncryptException('Cannot mutliKeyEncrypt empty plain content', 10);\r
                }\r
 \r
                // Set empty vars to be set by openssl by reference\r
@@ -394,9 +393,7 @@ class Crypt {
                        );\r
 \r
                } else {\r
-\r
-                       return false;\r
-\r
+                       throw new Exceptions\MultiKeyEncryptException('multi key encryption failed: ' . openssl_error_string(), 20);\r
                }\r
 \r
        }\r
@@ -406,8 +403,8 @@ class Crypt {
         * @param string $encryptedContent\r
         * @param string $shareKey\r
         * @param mixed $privateKey\r
-        * @return false|string\r
-        * @internal param string $plainContent content to be encrypted\r
+        * @throws \OCA\Encryption\Exceptions\\MultiKeyDecryptException if decryption failed\r
+        * @internal param string $plainContent contains decrypted content\r
         * @return string $plainContent decrypted string\r
         * @note symmetricDecryptFileContent() can be used to decrypt files created using this method\r
         *\r
@@ -416,9 +413,7 @@ class Crypt {
        public static function multiKeyDecrypt($encryptedContent, $shareKey, $privateKey) {\r
 \r
                if (!$encryptedContent) {\r
-\r
-                       return false;\r
-\r
+                       throw new Exceptions\MultiKeyDecryptException('Cannot mutliKeyDecrypt empty plain content', 10);\r
                }\r
 \r
                if (openssl_open($encryptedContent, $plainContent, $shareKey, $privateKey)) {\r
@@ -426,11 +421,7 @@ class Crypt {
                        return $plainContent;\r
 \r
                } else {\r
-\r
-                       \OCP\Util::writeLog('Encryption library', 'Decryption (asymmetric) of sealed content with share-key "'.$shareKey.'" failed', \OCP\Util::ERROR);\r
-\r
-                       return false;\r
-\r
+                       throw new Exceptions\MultiKeyDecryptException('multiKeyDecrypt with share-key' . $shareKey . 'failed: ' . openssl_error_string(), 20);\r
                }\r
 \r
        }\r
diff --git a/apps/files_encryption/lib/exceptions.php b/apps/files_encryption/lib/exceptions.php
new file mode 100644 (file)
index 0000000..a409b0f
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+/**
+ * ownCloud
+ *
+ * @author Bjoern Schiessle
+ * @copyright 2014 Bjoern Schiessle <schiessle@owncloud.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library 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 library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Encryption\Exceptions;
+
+class EncryptionException extends \Exception {
+}
+
+/**
+ * Throw this exception if multi key encrytion fails
+ *
+ * Possible error codes:
+ * 10 - empty plain content was given
+ * 20 - openssl_seal failed
+ */
+class MultiKeyEncryptException extends EncryptionException {
+}
+
+/**
+ * Throw this encryption if multi key decryption failed
+ *
+ * Possible error codes:
+ * 10 - empty encrypted content was given
+ * 20 - openssl_open failed
+ */
+class MultiKeyDecryptException extends EncryptionException {
+}
index ee9939318c6aeaed6bccbba5d2a39dd10a7dbd24..e44a8bd3dda78daf7171429c74746e809d1965b7 100644 (file)
@@ -908,19 +908,26 @@ class Util {
                // Get the current users's private key for decrypting existing keyfile
                $privateKey = $session->getPrivateKey();
 
-               $fileOwner = \OC\Files\Filesystem::getOwner($filePath);
-
-               // Decrypt keyfile
-               $plainKeyfile = $this->decryptKeyfile($filePath, $privateKey);
-
-               // Re-enc keyfile to (additional) sharekeys
-               $multiEncKey = Crypt::multiKeyEncrypt($plainKeyfile, $userPubKeys);
+               try {
+                       // Decrypt keyfile
+                       $plainKeyfile = $this->decryptKeyfile($filePath, $privateKey);
+                       // Re-enc keyfile to (additional) sharekeys
+                       $multiEncKey = Crypt::multiKeyEncrypt($plainKeyfile, $userPubKeys);
+               } catch (Exceptions\EncryptionException $e) {
+                       $msg = 'set shareFileKeyFailed (code: ' . $e->getCode() . '): ' . $e->getMessage();
+                       \OCP\Util::writeLog('files_encryption', $msg, \OCP\Util::FATAL);
+                       return false;
+               } catch (\Exception $e) {
+                       $msg = 'set shareFileKeyFailed (unknown error): ' . $e->getMessage();
+                       \OCP\Util::writeLog('files_encryption', $msg, \OCP\Util::FATAL);
+                       return false;
+               }
 
                // Save the recrypted key to it's owner's keyfiles directory
                // Save new sharekeys to all necessary user directory
                if (
-                       !Keymanager::setFileKey($this->view, $this, $filePath, $multiEncKey['data'])
-                       || !Keymanager::setShareKeys($this->view, $this, $filePath, $multiEncKey['keys'])
+                               !Keymanager::setFileKey($this->view, $this, $filePath, $multiEncKey['data'])
+                               || !Keymanager::setShareKeys($this->view, $this, $filePath, $multiEncKey['keys'])
                ) {
 
                        \OCP\Util::writeLog('Encryption library',