From eaa61b8539bcd1f428d8fad1d67894e8cb4f271a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Schie=C3=9Fle?= <schiessle@owncloud.com>
Date: Fri, 17 May 2013 17:29:32 +0200
Subject: [PATCH] fix migration to new encryption

---
 apps/files_encryption/ajax/encryptall.php | 40 -----------------------
 apps/files_encryption/hooks/hooks.php     |  2 +-
 apps/files_encryption/lib/crypt.php       | 21 +++++++++---
 apps/files_encryption/lib/util.php        |  3 +-
 4 files changed, 18 insertions(+), 48 deletions(-)
 delete mode 100644 apps/files_encryption/ajax/encryptall.php

diff --git a/apps/files_encryption/ajax/encryptall.php b/apps/files_encryption/ajax/encryptall.php
deleted file mode 100644
index ce613ca443..0000000000
--- a/apps/files_encryption/ajax/encryptall.php
+++ /dev/null
@@ -1,40 +0,0 @@
-<?php
-/**
- * Copyright (c) 2013, Sam Tuke <samtuke@owncloud.com>
- * This file is licensed under the Affero General Public License version 3 or later.
- * See the COPYING-README file.
- *
- * @brief Script to handle manual trigger of \OCA\Encryption\Util{}->encryptAll()
- */
-
-use OCA\Encryption;
-
-\OCP\JSON::checkAppEnabled( 'files_encryption' );
-\OCP\JSON::callCheck();
-
-$return = false;
-
-if ( 
-	isset( $_POST['encryptAll'] )
-	&& ! empty( $_POST['userPassword'] )
-) {
-
-	$view = new \OC_FilesystemView( '' );
-	$userId = \OCP\User::getUser();
-	$util = new \OCA\Encryption\Util( $view, $userId );
-	$session = new \OCA\Encryption\Session( $view );
-	$publicKey = \OCA\Encryption\Keymanager::getPublicKey( $view, $userId );
-	$path = '/' . $userId . '/' . 'files';
-	
-	$util->encryptAll( $publicKey, $path, $session->getLegacyKey(), $_POST['userPassword'] );
-	
-	$return = true;
-
-} else {
-
-	$return = false;
-	
-}
-
-// Return success or failure
-( $return ) ? \OCP\JSON::success() : \OCP\JSON::error();
\ No newline at end of file
diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php
index 76a19ff968..72334559b8 100644
--- a/apps/files_encryption/hooks/hooks.php
+++ b/apps/files_encryption/hooks/hooks.php
@@ -88,7 +88,7 @@ class Hooks {
 			// This serves to upgrade old versions of the encryption
 			// app (see appinfo/spec.txt)
 			if (
-				$util->encryptAll( $publicKey,  '/' . $params['uid'] . '/' . 'files', $session->getLegacyKey(), $params['password'] )
+				$util->encryptAll( '/' . $params['uid'] . '/' . 'files', $session->getLegacyKey(), $params['password'] )
 			) {
 				
 				\OC_Log::write( 
diff --git a/apps/files_encryption/lib/crypt.php b/apps/files_encryption/lib/crypt.php
index 708d1719d7..56dacc94b0 100755
--- a/apps/files_encryption/lib/crypt.php
+++ b/apps/files_encryption/lib/crypt.php
@@ -169,7 +169,7 @@ class Crypt {
          * @return true / false
          */
 	public static function isLegacyEncryptedContent( $data, $relPath ) {
-	
+
 		// Fetch all file metadata from DB
 		$metadata = \OC\Files\Filesystem::getFileInfo( $relPath, '' );
 		
@@ -683,15 +683,26 @@ class Crypt {
 		
 		$decrypted = $bf->decrypt( $content );
 		
-		$trimmed = rtrim( $decrypted, "\0" );
-		
-		return $trimmed;
+		return $decrypted;
 		
 	}
+
+	private static function legacyBlockDecrypt($data, $key='',$maxLength=0) {
+		$result = '';
+		while (strlen($data)) {
+			$result.=self::legacyDecrypt(substr($data, 0, 8192), $key);
+			$data = substr($data, 8192);
+		}
+		if ($maxLength > 0) {
+			return substr($result, 0, $maxLength);
+		} else {
+			return rtrim($result, "\0");
+		}
+	}
 	
 	public static function legacyKeyRecryptKeyfile( $legacyEncryptedContent, $legacyPassphrase, $publicKeys, $newPassphrase, $path ) {
 	
-		$decrypted = self::legacyDecrypt( $legacyEncryptedContent, $legacyPassphrase );
+		$decrypted = self::legacyBlockDecrypt( $legacyEncryptedContent, $legacyPassphrase );
 
 		// Encrypt plain data, generate keyfile & encrypted file
 		$cryptedData = self::symmetricEncryptFileContentKeyfile( $decrypted );
diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php
index f1042ed759..9588db8d64 100644
--- a/apps/files_encryption/lib/util.php
+++ b/apps/files_encryption/lib/util.php
@@ -652,11 +652,10 @@ class Util {
 	
 	/**
 	 * @brief Encrypt all files in a directory
-	 * @param string $publicKey the public key to encrypt files with
 	 * @param string $dirPath the directory whose files will be encrypted
 	 * @note Encryption is recursive
 	 */
-	public function encryptAll($publicKey, $dirPath, $legacyPassphrase = null, $newPassphrase = null) {
+	public function encryptAll($dirPath, $legacyPassphrase = null, $newPassphrase = null) {
 
 		if ($found = $this->findEncFiles($dirPath)) {
 
-- 
GitLab