fix for encryption binary files

remotes/origin/stable45
Robin Appelman 13 years ago
parent 8484e16516
commit 195c37f88a
  1. 35
      apps/files_encryption/lib/crypt.php
  2. 4
      apps/files_encryption/lib/cryptstream.php
  3. 19
      apps/files_encryption/tests/encryption.php
  4. 17
      apps/files_encryption/tests/stream.php

@ -43,22 +43,22 @@ class OC_Crypt {
self::init($params['uid'],$params['password']); self::init($params['uid'],$params['password']);
} }
public static function init($login,$password) { public static function init($login,$password) {
$view1=new OC_FilesystemView('/'); $view1=new OC_FilesystemView('/');
if(!$view1->file_exists('/'.$login)){ if(!$view1->file_exists('/'.$login)){
$view1->mkdir('/'.$login); $view1->mkdir('/'.$login);
} }
$view=new OC_FilesystemView('/'.$login); $view=new OC_FilesystemView('/'.$login);
OC_FileProxy::$enabled=false; OC_FileProxy::$enabled=false;
if(!$view->file_exists('/encryption.key')){// does key exist? if(!$view->file_exists('/encryption.key')){// does key exist?
OC_Crypt::createkey($login,$password); OC_Crypt::createkey($login,$password);
} }
$key=$view->file_get_contents('/encryption.key'); $key=$view->file_get_contents('/encryption.key');
OC_FileProxy::$enabled=true; OC_FileProxy::$enabled=true;
$_SESSION['enckey']=OC_Crypt::decrypt($key, $password); $_SESSION['enckey']=OC_Crypt::decrypt($key, $password);
} }
/** /**
@ -140,7 +140,7 @@ class OC_Crypt {
public static function decrypt( $content, $key='') { public static function decrypt( $content, $key='') {
$bf = self::getBlowfish($key); $bf = self::getBlowfish($key);
$data=$bf->decrypt($content); $data=$bf->decrypt($content);
return rtrim($data, "\0"); return $data;
} }
/** /**
@ -181,6 +181,9 @@ class OC_Crypt {
while (!feof($handleread)) { while (!feof($handleread)) {
$content = fread($handleread, 8192); $content = fread($handleread, 8192);
$enccontent=OC_CRYPT::decrypt( $content, $key); $enccontent=OC_CRYPT::decrypt( $content, $key);
if(feof($handleread)){
$enccontent=rtrim($enccontent, "\0");
}
fwrite($handlewrite, $enccontent); fwrite($handlewrite, $enccontent);
} }
fclose($handlewrite); fclose($handlewrite);
@ -209,6 +212,6 @@ class OC_Crypt {
$result.=self::decrypt(substr($data,0,8192),$key); $result.=self::decrypt(substr($data,0,8192),$key);
$data=substr($data,8192); $data=substr($data,8192);
} }
return $result; return rtrim($result, "\0");
} }
} }

@ -47,7 +47,6 @@ class OC_CryptStream{
$this->path=self::$sourceStreams[basename($path)]['path']; $this->path=self::$sourceStreams[basename($path)]['path'];
}else{ }else{
$this->path=$path; $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 OC_FileProxy::$enabled=false;//disable fileproxies so we can open the source file
$this->source=self::$rootView->fopen($path,$mode); $this->source=self::$rootView->fopen($path,$mode);
OC_FileProxy::$enabled=true; OC_FileProxy::$enabled=true;
@ -84,6 +83,9 @@ class OC_CryptStream{
}else{ }else{
$result=''; $result='';
} }
if($this->stream_eof()){
$result=rtrim($result, "\0");
}
return $result; return $result;
} }

@ -13,6 +13,7 @@ class Test_Encryption extends UnitTestCase {
$source=file_get_contents($file); //nice large text file $source=file_get_contents($file); //nice large text file
$encrypted=OC_Crypt::encrypt($source,$key); $encrypted=OC_Crypt::encrypt($source,$key);
$decrypted=OC_Crypt::decrypt($encrypted,$key); $decrypted=OC_Crypt::decrypt($encrypted,$key);
$decrypted=rtrim($decrypted, "\0");
$this->assertNotEqual($encrypted,$source); $this->assertNotEqual($encrypted,$source);
$this->assertEqual($decrypted,$source); $this->assertEqual($decrypted,$source);
@ -20,6 +21,7 @@ class Test_Encryption extends UnitTestCase {
$encrypted=OC_Crypt::encrypt($chunk,$key); $encrypted=OC_Crypt::encrypt($chunk,$key);
$this->assertEqual(strlen($chunk),strlen($encrypted)); $this->assertEqual(strlen($chunk),strlen($encrypted));
$decrypted=OC_Crypt::decrypt($encrypted,$key); $decrypted=OC_Crypt::decrypt($encrypted,$key);
$decrypted=rtrim($decrypted, "\0");
$this->assertEqual($decrypted,$chunk); $this->assertEqual($decrypted,$chunk);
$encrypted=OC_Crypt::blockEncrypt($source,$key); $encrypted=OC_Crypt::blockEncrypt($source,$key);
@ -43,6 +45,7 @@ class Test_Encryption extends UnitTestCase {
$source=file_get_contents($file); //binary file $source=file_get_contents($file); //binary file
$encrypted=OC_Crypt::encrypt($source,$key); $encrypted=OC_Crypt::encrypt($source,$key);
$decrypted=OC_Crypt::decrypt($encrypted,$key); $decrypted=OC_Crypt::decrypt($encrypted,$key);
$decrypted=rtrim($decrypted, "\0");
$this->assertEqual($decrypted,$source); $this->assertEqual($decrypted,$source);
$encrypted=OC_Crypt::blockEncrypt($source,$key); $encrypted=OC_Crypt::blockEncrypt($source,$key);
@ -50,4 +53,20 @@ class Test_Encryption extends UnitTestCase {
$this->assertEqual($decrypted,$source); $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);
}
} }

@ -50,7 +50,22 @@ class Test_CryptStream extends UnitTestCase {
$file=$this->tmpFiles[$id]; $file=$this->tmpFiles[$id];
} }
$stream=fopen($file,$mode); $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); 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);
}
} }

Loading…
Cancel
Save