summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2012-06-15 23:48:39 +0200
committerRobin Appelman <icewind@owncloud.com>2012-06-20 20:18:42 +0200
commit9ba467d62980e4e397d152e9c38b08da5cf31a99 (patch)
treea0766dd2d5ddea5013aa825b287b01b8e6a249dc /apps
parent0f2600e9ea950c76777aecff86be8e8136e2aa4e (diff)
downloadnextcloud-server-9ba467d62980e4e397d152e9c38b08da5cf31a99.tar.gz
nextcloud-server-9ba467d62980e4e397d152e9c38b08da5cf31a99.zip
fix encryption for binary files
Diffstat (limited to 'apps')
-rw-r--r--apps/files_encryption/lib/crypt.php13
-rw-r--r--apps/files_encryption/lib/cryptstream.php4
-rw-r--r--apps/files_encryption/tests/encryption.php19
-rw-r--r--apps/files_encryption/tests/stream.php17
4 files changed, 49 insertions, 4 deletions
diff --git a/apps/files_encryption/lib/crypt.php b/apps/files_encryption/lib/crypt.php
index 37eaedc3fc9..62f6a8c2365 100644
--- a/apps/files_encryption/lib/crypt.php
+++ b/apps/files_encryption/lib/crypt.php
@@ -44,7 +44,13 @@ class OC_Crypt {
}
public static function init($login,$password) {
+ $view1=new OC_FilesystemView('/');
+ if(!$view1->file_exists('/'.$login)){
+ $view1->mkdir('/'.$login);
+ }
+
$view=new OC_FilesystemView('/'.$login);
+
OC_FileProxy::$enabled=false;
if(!$view->file_exists('/encryption.key')){// does key exist?
OC_Crypt::createkey($login,$password);
@@ -133,7 +139,7 @@ class OC_Crypt {
public static function decrypt( $content, $key='') {
$bf = self::getBlowfish($key);
$data=$bf->decrypt($content);
- return rtrim($data, "\0");
+ return $data;
}
/**
@@ -174,6 +180,9 @@ class OC_Crypt {
while (!feof($handleread)) {
$content = fread($handleread, 8192);
$enccontent=OC_CRYPT::decrypt( $content, $key);
+ if(feof($handleread)){
+ $enccontent=rtrim($enccontent, "\0");
+ }
fwrite($handlewrite, $enccontent);
}
fclose($handlewrite);
@@ -202,6 +211,6 @@ class OC_Crypt {
$result.=self::decrypt(substr($data,0,8192),$key);
$data=substr($data,8192);
}
- return $result;
+ return rtrim($result, "\0");
}
}
diff --git a/apps/files_encryption/lib/cryptstream.php b/apps/files_encryption/lib/cryptstream.php
index a698ee00335..56331cbf601 100644
--- a/apps/files_encryption/lib/cryptstream.php
+++ b/apps/files_encryption/lib/cryptstream.php
@@ -47,7 +47,6 @@ class OC_CryptStream{
$this->path=self::$sourceStreams[basename($path)]['path'];
}else{
$this->path=$path;
- OCP\Util::writeLog('files_encryption','open encrypted '.$path. ' in '.$mode,OCP\Util::DEBUG);
OC_FileProxy::$enabled=false;//disable fileproxies so we can open the source file
$this->source=self::$rootView->fopen($path,$mode);
OC_FileProxy::$enabled=true;
@@ -84,6 +83,9 @@ class OC_CryptStream{
}else{
$result='';
}
+ if($this->stream_eof()){
+ $result=rtrim($result, "\0");
+ }
return $result;
}
diff --git a/apps/files_encryption/tests/encryption.php b/apps/files_encryption/tests/encryption.php
index cf24a225d28..70aa1daf4c3 100644
--- a/apps/files_encryption/tests/encryption.php
+++ b/apps/files_encryption/tests/encryption.php
@@ -13,6 +13,7 @@ class Test_Encryption extends UnitTestCase {
$source=file_get_contents($file); //nice large text file
$encrypted=OC_Crypt::encrypt($source,$key);
$decrypted=OC_Crypt::decrypt($encrypted,$key);
+ $decrypted=rtrim($decrypted, "\0");
$this->assertNotEqual($encrypted,$source);
$this->assertEqual($decrypted,$source);
@@ -20,6 +21,7 @@ class Test_Encryption extends UnitTestCase {
$encrypted=OC_Crypt::encrypt($chunk,$key);
$this->assertEqual(strlen($chunk),strlen($encrypted));
$decrypted=OC_Crypt::decrypt($encrypted,$key);
+ $decrypted=rtrim($decrypted, "\0");
$this->assertEqual($decrypted,$chunk);
$encrypted=OC_Crypt::blockEncrypt($source,$key);
@@ -43,6 +45,7 @@ class Test_Encryption extends UnitTestCase {
$source=file_get_contents($file); //binary file
$encrypted=OC_Crypt::encrypt($source,$key);
$decrypted=OC_Crypt::decrypt($encrypted,$key);
+ $decrypted=rtrim($decrypted, "\0");
$this->assertEqual($decrypted,$source);
$encrypted=OC_Crypt::blockEncrypt($source,$key);
@@ -50,4 +53,20 @@ class Test_Encryption extends UnitTestCase {
$this->assertEqual($decrypted,$source);
}
+
+ function testBinary(){
+ $key=uniqid();
+
+ $file=__DIR__.'/binary';
+ $source=file_get_contents($file); //binary file
+ $encrypted=OC_Crypt::encrypt($source,$key);
+ $decrypted=OC_Crypt::decrypt($encrypted,$key);
+
+ $decrypted=rtrim($decrypted, "\0");
+ $this->assertEqual($decrypted,$source);
+
+ $encrypted=OC_Crypt::blockEncrypt($source,$key);
+ $decrypted=OC_Crypt::blockDecrypt($encrypted,$key);
+ $this->assertEqual($decrypted,$source);
+ }
}
diff --git a/apps/files_encryption/tests/stream.php b/apps/files_encryption/tests/stream.php
index b23805d60b0..4ffeb6210a9 100644
--- a/apps/files_encryption/tests/stream.php
+++ b/apps/files_encryption/tests/stream.php
@@ -50,7 +50,22 @@ class Test_CryptStream extends UnitTestCase {
$file=$this->tmpFiles[$id];
}
$stream=fopen($file,$mode);
- OC_CryptStream::$sourceStreams[$id]=array('path'=>'dummy','stream'=>$stream);
+ OC_CryptStream::$sourceStreams[$id]=array('path'=>'dummy'.$id,'stream'=>$stream);
return fopen('crypt://streams/'.$id,$mode);
}
+
+ function testBinary(){
+ $file=__DIR__.'/binary';
+ $source=file_get_contents($file);
+
+ $stream=$this->getStream('test','w');
+ fwrite($stream,$source);
+ fclose($stream);
+
+ $stream=$this->getStream('test','r');
+ $data=stream_get_contents($stream);
+ fclose($stream);
+ $this->assertEqual(strlen($data),strlen($source));
+ $this->assertEqual($source,$data);
+ }
}