]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix exception handling
authorThomas Müller <thomas.mueller@tmit.eu>
Wed, 1 Apr 2015 11:58:23 +0000 (13:58 +0200)
committerThomas Müller <thomas.mueller@tmit.eu>
Tue, 7 Apr 2015 11:30:30 +0000 (13:30 +0200)
apps/encryption/lib/crypto/crypt.php
apps/encryption/lib/exceptions/filekeymissingexception.php [new file with mode: 0644]
apps/encryption/lib/exceptions/multikeydecryptexception.php [new file with mode: 0644]
apps/encryption/lib/exceptions/multikeyencryptexception.php [new file with mode: 0644]

index ea7f9e1cdf56c929bf75a601b8f7ece76ed1245e..6b79057fe7ee5bde8194cc76748ec1420b1e4a2a 100644 (file)
@@ -25,6 +25,8 @@ namespace OCA\Encryption\Crypto;
 use OC\Encryption\Exceptions\DecryptionFailedException;
 use OC\Encryption\Exceptions\EncryptionFailedException;
 use OC\Encryption\Exceptions\GenericEncryptionException;
+use OCA\Encryption\Exceptions\MultiKeyDecryptException;
+use OCA\Encryption\Exceptions\MultiKeyEncryptException;
 use OCP\IConfig;
 use OCP\ILogger;
 use OCP\IUser;
@@ -120,11 +122,11 @@ class Crypt {
 
        /**
         * @param $plainContent
-        * @param $passphrase
+        * @param $passPhrase
         * @return bool|string
         * @throws GenericEncryptionException
         */
-       public function symmetricEncryptFileContent($plainContent, $passphrase) {
+       public function symmetricEncryptFileContent($plainContent, $passPhrase) {
 
                if (!$plainContent) {
                        $this->logger->error('Encryption Library, symmetrical encryption failed no content given',
@@ -134,37 +136,29 @@ class Crypt {
 
                $iv = $this->generateIv();
 
-               try {
-                       $encryptedContent = $this->encrypt($plainContent,
-                               $iv,
-                               $passphrase,
-                               $this->getCipher());
-                       // combine content to encrypt the IV identifier and actual IV
-                       $catFile = $this->concatIV($encryptedContent, $iv);
-                       $padded = $this->addPadding($catFile);
-
-                       return $padded;
-               } catch (EncryptionFailedException $e) {
-                       $message = 'Could not encrypt file content (code: ' . $e->getCode() . '): ';
-                       $this->logger->error('files_encryption' . $message . $e->getMessage(),
-                               ['app' => 'encryption']);
-                       return false;
-               }
+               $encryptedContent = $this->encrypt($plainContent,
+                       $iv,
+                       $passPhrase,
+                       $this->getCipher());
+               // combine content to encrypt the IV identifier and actual IV
+               $catFile = $this->concatIV($encryptedContent, $iv);
+               $padded = $this->addPadding($catFile);
 
+               return $padded;
        }
 
        /**
         * @param $plainContent
         * @param $iv
-        * @param string $passphrase
+        * @param string $passPhrase
         * @param string $cipher
         * @return string
         * @throws EncryptionFailedException
         */
-       private function encrypt($plainContent, $iv, $passphrase = '', $cipher = self::DEFAULT_CIPHER) {
+       private function encrypt($plainContent, $iv, $passPhrase = '', $cipher = self::DEFAULT_CIPHER) {
                $encryptedContent = openssl_encrypt($plainContent,
                        $cipher,
-                       $passphrase,
+                       $passPhrase,
                        false,
                        $iv);
 
@@ -246,27 +240,21 @@ class Crypt {
 
        /**
         * @param $keyFileContents
-        * @param string $passphrase
+        * @param string $passPhrase
         * @param string $cipher
-        * @return bool|string
+        * @return string
         * @throws DecryptionFailedException
         */
-       public function symmetricDecryptFileContent($keyFileContents, $passphrase = '', $cipher = self::DEFAULT_CIPHER) {
+       public function symmetricDecryptFileContent($keyFileContents, $passPhrase = '', $cipher = self::DEFAULT_CIPHER) {
                // Remove Padding
                $noPadding = $this->removePadding($keyFileContents);
 
                $catFile = $this->splitIv($noPadding);
 
-               $plainContent = $this->decrypt($catFile['encrypted'],
+               return $this->decrypt($catFile['encrypted'],
                        $catFile['iv'],
-                       $passphrase,
+                       $passPhrase,
                        $cipher);
-
-               if ($plainContent) {
-                       return $plainContent;
-               }
-
-               return false;
        }
 
        /**
@@ -304,22 +292,22 @@ class Crypt {
        /**
         * @param $encryptedContent
         * @param $iv
-        * @param string $passphrase
+        * @param string $passPhrase
         * @param string $cipher
         * @return string
         * @throws DecryptionFailedException
         */
-       private function decrypt($encryptedContent, $iv, $passphrase = '', $cipher = self::DEFAULT_CIPHER) {
+       private function decrypt($encryptedContent, $iv, $passPhrase = '', $cipher = self::DEFAULT_CIPHER) {
                $plainContent = openssl_decrypt($encryptedContent,
                        $cipher,
-                       $passphrase,
+                       $passPhrase,
                        false,
                        $iv);
 
                if ($plainContent) {
                        return $plainContent;
                } else {
-                       throw new DecryptionFailedException('Encryption library: Decryption (symmetric) of content failed');
+                       throw new DecryptionFailedException('Encryption library: Decryption (symmetric) of content failed: ' . openssl_error_string());
                }
        }
 
@@ -427,7 +415,7 @@ class Crypt {
                if (openssl_open($encKeyFile, $plainContent, $shareKey, $privateKey)) {
                        return $plainContent;
                } else {
-                       throw new MultiKeyDecryptException('multikeydecrypt with share key failed');
+                       throw new MultiKeyDecryptException('multikeydecrypt with share key failed:' . openssl_error_string());
                }
        }
 
@@ -452,7 +440,7 @@ class Crypt {
                if (openssl_seal($plainContent, $sealed, $shareKeys, $keyFiles)) {
                        $i = 0;
 
-                       // Ensure each shareKey is labelled with its coreesponding keyid
+                       // Ensure each shareKey is labelled with its corresponding key id
                        foreach ($keyFiles as $userId => $publicKey) {
                                $mappedShareKeys[$userId] = $shareKeys[$i];
                                $i++;
diff --git a/apps/encryption/lib/exceptions/filekeymissingexception.php b/apps/encryption/lib/exceptions/filekeymissingexception.php
new file mode 100644 (file)
index 0000000..9eb2d4c
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+
+
+namespace OCA\Encryption\Exceptions;
+
+class FileKeyMissingException extends \Exception {
+
+}
diff --git a/apps/encryption/lib/exceptions/multikeydecryptexception.php b/apps/encryption/lib/exceptions/multikeydecryptexception.php
new file mode 100644 (file)
index 0000000..36a9554
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+
+namespace OCA\Encryption\Exceptions;
+
+class MultiKeyDecryptException extends \Exception {
+
+}
diff --git a/apps/encryption/lib/exceptions/multikeyencryptexception.php b/apps/encryption/lib/exceptions/multikeyencryptexception.php
new file mode 100644 (file)
index 0000000..e518a09
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+
+namespace OCA\Encryption\Exceptions;
+
+class MultiKeyEncryptException extends \Exception {
+
+}