]> source.dussan.org Git - nextcloud-server.git/commitdiff
introduce decryptPrivateKey() method which also checks if the result is a valid priva...
authorBjörn Schießle <schiessle@owncloud.com>
Mon, 3 Jun 2013 12:19:31 +0000 (14:19 +0200)
committerBjörn Schießle <schiessle@owncloud.com>
Thu, 13 Jun 2013 11:25:50 +0000 (13:25 +0200)
Conflicts:

apps/files_encryption/lib/session.php

apps/files_encryption/ajax/changeRecoveryPassword.php
apps/files_encryption/hooks/hooks.php
apps/files_encryption/lib/crypt.php
apps/files_encryption/lib/helper.php
apps/files_encryption/lib/session.php
apps/files_encryption/lib/util.php

index b0594f967ba30c88ff067ef122ba8f4b445eca50..366f634a51cc0504887abd1c599e320b404b4a5d 100644 (file)
@@ -22,28 +22,28 @@ $return = false;
 $oldPassword = $_POST['oldPassword'];
 $newPassword = $_POST['newPassword'];
 
+$view = new \OC\Files\View('/');
 $util = new \OCA\Encryption\Util(new \OC_FilesystemView('/'), \OCP\User::getUser());
 
-$result = $util->checkRecoveryPassword($oldPassword);
+$proxyStatus = \OC_FileProxy::$enabled;
+\OC_FileProxy::$enabled = false;
 
-if ($result) {
-       $keyId = $util->getRecoveryKeyId();
-       $keyPath = '/owncloud_private_key/' . $keyId . '.private.key';
-       $view = new \OC\Files\View('/');
+$keyId = $util->getRecoveryKeyId();
+$keyPath = '/owncloud_private_key/' . $keyId . '.private.key';
 
-       $proxyStatus = \OC_FileProxy::$enabled;
-       \OC_FileProxy::$enabled = false;
+$encryptedRecoveryKey = $view->file_get_contents($keyPath);
+$decryptedRecoveryKey = \OCA\Encryption\Crypt::decryptPrivateKey($encryptedRecoveryKey, $oldPassword);
+
+if ($decryptedRecoveryKey) {
 
-       $encryptedRecoveryKey = $view->file_get_contents($keyPath);
-       $decryptedRecoveryKey = \OCA\Encryption\Crypt::symmetricDecryptFileContent($encryptedRecoveryKey, $oldPassword);
        $encryptedRecoveryKey = \OCA\Encryption\Crypt::symmetricEncryptFileContent($decryptedRecoveryKey, $newPassword);
        $view->file_put_contents($keyPath, $encryptedRecoveryKey);
 
-       \OC_FileProxy::$enabled = $proxyStatus;
-
        $return = true;
 }
 
+\OC_FileProxy::$enabled = $proxyStatus;
+
 // success or failure
 if ($return) {
        \OCP\JSON::success(array('data' => array('message' => $l->t('Password successfully changed.'))));
index 5048f316efe69f2b249cd8bfba2861addf717b20..cd48e5ecd9b17667ec73b6e2b78d23de0f663981 100644 (file)
@@ -60,18 +60,7 @@ class Hooks {
 \r
                $encryptedKey = Keymanager::getPrivateKey($view, $params['uid']);\r
 \r
-               $privateKey = Crypt::symmetricDecryptFileContent($encryptedKey, $params['password']);\r
-\r
-               // check if this a valid private key\r
-               $res = openssl_pkey_get_private($privateKey);\r
-               if(is_resource($res)) {\r
-                       $sslInfo = openssl_pkey_get_details($res);\r
-                       if(!isset($sslInfo['key'])) {\r
-                               $privateKey = false;\r
-                       }\r
-               } else {\r
-                       $privateKey = false;\r
-               }\r
+               $privateKey = Crypt::decryptPrivateKey($encryptedKey, $params['password']);\r
 \r
                if($privateKey === false) {\r
                        \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);\r
index ced9ab7c67644b88cbb527d791ce0da3b22fb917..1f1dac3238b7fc97be9147ba6f9527510e26cad8 100755 (executable)
@@ -351,6 +351,34 @@ class Crypt {
 \r
        }\r
 \r
+       /**\r
+        * @brief Decrypt private key and check if the result is a valid keyfile\r
+        * @param string $encryptedKey encrypted keyfile\r
+        * @param string $passphrase to decrypt keyfile\r
+        * @returns encrypted private key or false\r
+        *\r
+        * This function decrypts a file\r
+        */\r
+       public static function decryptPrivateKey($encryptedKey, $passphrase) {\r
+\r
+               $plainKey = self::symmetricDecryptFileContent($encryptedKey, $passphrase);\r
+\r
+               // check if this a valid private key\r
+               $res = openssl_pkey_get_private($plainKey);\r
+               if(is_resource($res)) {\r
+                       $sslInfo = openssl_pkey_get_details($res);\r
+                       if(!isset($sslInfo['key'])) {\r
+                               $plainKey = false;\r
+                       }\r
+               } else {\r
+                       $plainKey = false;\r
+               }\r
+\r
+               return $plainKey;\r
+\r
+       }\r
+\r
+\r
        /**\r
         * @brief Creates symmetric keyfile content using a generated key\r
         * @param string $plainContent content to be encrypted\r
index 184e1782494912753cef197f2cbf998f6f401036..3fd18c7d26486a8f0e3ff78957f6b1d0571f6a5b 100755 (executable)
@@ -94,6 +94,7 @@ class Helper {
         * @return bool
         */
        public static function adminEnableRecovery($recoveryKeyId, $recoveryPassword) {
+
                $view = new \OC\Files\View('/');
 
                if ($recoveryKeyId === null) {
@@ -128,13 +129,6 @@ class Helper {
                        // Save private key
                        $view->file_put_contents('/owncloud_private_key/' . $recoveryKeyId . '.private.key', $encryptedPrivateKey);
 
-                       // create control file which let us check later on if the entered password was correct.
-                       $encryptedControlData = \OCA\Encryption\Crypt::keyEncrypt("ownCloud", $keypair['publicKey']);
-                       if (!$view->is_dir('/control-file')) {
-                               $view->mkdir('/control-file');
-                       }
-                       $view->file_put_contents('/control-file/controlfile.enc', $encryptedControlData);
-
                        \OC_FileProxy::$enabled = true;
 
                        // Set recoveryAdmin as enabled
index 4067fad00aff61171c3e264d7d72ef2c01e7a68a..6b118582f7a827a38b1b92aae61f890f1922fca0 100644 (file)
@@ -194,4 +194,4 @@ class Session {
 
        }
 
-}
\ No newline at end of file
+}
index 0e799aa0ca7c9185c424cbbd106a58a0e09c6cab..ec239d2deb6757e3a36d79fc6989565513c86617 100644 (file)
@@ -1380,26 +1380,24 @@ class Util {
         */
        public function checkRecoveryPassword($password) {
 
+               $result = false;
                $pathKey = '/owncloud_private_key/' . $this->recoveryKeyId . ".private.key";
-               $pathControlData = '/control-file/controlfile.enc';
 
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;
 
                $recoveryKey = $this->view->file_get_contents($pathKey);
 
-               $decryptedRecoveryKey = Crypt::symmetricDecryptFileContent($recoveryKey, $password);
+               $decryptedRecoveryKey = Crypt::decryptPrivateKey($recoveryKey, $password);
 
-               $controlData = $this->view->file_get_contents($pathControlData);
-               $decryptedControlData = Crypt::keyDecrypt($controlData, $decryptedRecoveryKey);
+               if ($decryptedRecoveryKey) {
+                       $result = true;
+               }
 
                \OC_FileProxy::$enabled = $proxyStatus;
 
-               if ($decryptedControlData === 'ownCloud') {
-                       return true;
-               }
 
-               return false;
+               return $result;
        }
 
        /**
@@ -1528,7 +1526,7 @@ class Util {
 
                $encryptedKey = $this->view->file_get_contents(
                        '/owncloud_private_key/' . $this->recoveryKeyId . '.private.key');
-               $privateKey = Crypt::symmetricDecryptFileContent($encryptedKey, $recoveryPassword);
+               $privateKey = Crypt::decryptPrivateKey($encryptedKey, $recoveryPassword);
 
                \OC_FileProxy::$enabled = $proxyStatus;