aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_encryption/lib
diff options
context:
space:
mode:
authorBart Visscher <bartv@thisnet.nl>2012-05-04 17:04:11 +0200
committerBart Visscher <bartv@thisnet.nl>2012-05-04 17:04:11 +0200
commit1e471562268ff62c59708b724c22930bc1d01d95 (patch)
tree5b5125ec41e865fa009d51b4293042f6572067f4 /apps/files_encryption/lib
parent71f9b1968e3d4decc4395db2a1555a872cbb2820 (diff)
parent07ff1e723ae4fa3a0297b168ef2262e01a0a5e50 (diff)
downloadnextcloud-server-1e471562268ff62c59708b724c22930bc1d01d95.tar.gz
nextcloud-server-1e471562268ff62c59708b724c22930bc1d01d95.zip
Merge branch 'master' into tasks
Diffstat (limited to 'apps/files_encryption/lib')
-rwxr-xr-x[-rw-r--r--]apps/files_encryption/lib/crypt.php59
-rwxr-xr-x[-rw-r--r--]apps/files_encryption/lib/cryptstream.php51
-rwxr-xr-x[-rw-r--r--]apps/files_encryption/lib/proxy.php34
3 files changed, 63 insertions, 81 deletions
diff --git a/apps/files_encryption/lib/crypt.php b/apps/files_encryption/lib/crypt.php
index 246d4f672db..37eaedc3fc9 100644..100755
--- a/apps/files_encryption/lib/crypt.php
+++ b/apps/files_encryption/lib/crypt.php
@@ -91,8 +91,8 @@ class OC_Crypt {
}
public static function changekeypasscode($oldPassword, $newPassword) {
- if(OC_User::isLoggedIn()){
- $username=OC_USER::getUser();
+ if(OCP\User::isLoggedIn()){
+ $username=OCP\USER::getUser();
$view=new OC_FilesystemView('/'.$username);
// read old key
@@ -119,7 +119,7 @@ class OC_Crypt {
*/
public static function encrypt( $content, $key='') {
$bf = self::getBlowfish($key);
- return($bf->encrypt($content));
+ return $bf->encrypt($content);
}
/**
@@ -132,61 +132,62 @@ class OC_Crypt {
*/
public static function decrypt( $content, $key='') {
$bf = self::getBlowfish($key);
- return($bf->decrypt($content));
+ $data=$bf->decrypt($content);
+ return rtrim($data, "\0");
}
/**
* @brief encryption of a file
- * @param $filename
- * @param $key the encryption key
+ * @param string $source
+ * @param string $target
+ * @param string $key the decryption key
*
* This function encrypts a file
*/
- public static function encryptfile( $filename, $key) {
- $handleread = fopen($filename, "rb");
- if($handleread<>FALSE) {
- $handlewrite = fopen($filename.OC_Crypt::$encription_extension, "wb");
+ public static function encryptFile( $source, $target, $key='') {
+ $handleread = fopen($source, "rb");
+ if($handleread!=FALSE) {
+ $handlewrite = fopen($target, "wb");
while (!feof($handleread)) {
$content = fread($handleread, 8192);
$enccontent=OC_CRYPT::encrypt( $content, $key);
fwrite($handlewrite, $enccontent);
}
fclose($handlewrite);
- unlink($filename);
+ fclose($handleread);
}
- fclose($handleread);
}
- /**
- * @brief decryption of a file
- * @param $filename
- * @param $key the decryption key
- *
- * This function decrypts a file
- */
- public static function decryptfile( $filename, $key) {
- $handleread = fopen($filename.OC_Crypt::$encription_extension, "rb");
- if($handleread<>FALSE) {
- $handlewrite = fopen($filename, "wb");
+ /**
+ * @brief decryption of a file
+ * @param string $source
+ * @param string $target
+ * @param string $key the decryption key
+ *
+ * This function decrypts a file
+ */
+ public static function decryptFile( $source, $target, $key='') {
+ $handleread = fopen($source, "rb");
+ if($handleread!=FALSE) {
+ $handlewrite = fopen($target, "wb");
while (!feof($handleread)) {
$content = fread($handleread, 8192);
$enccontent=OC_CRYPT::decrypt( $content, $key);
fwrite($handlewrite, $enccontent);
}
fclose($handlewrite);
- unlink($filename.OC_Crypt::$encription_extension);
+ fclose($handleread);
}
- fclose($handleread);
}
/**
* encrypt data in 8192b sized blocks
*/
- public static function blockEncrypt($data){
+ public static function blockEncrypt($data, $key=''){
$result='';
while(strlen($data)){
- $result=self::encrypt(substr($data,0,8192));
+ $result.=self::encrypt(substr($data,0,8192),$key);
$data=substr($data,8192);
}
return $result;
@@ -195,10 +196,10 @@ class OC_Crypt {
/**
* decrypt data in 8192b sized blocks
*/
- public static function blockDecrypt($data){
+ public static function blockDecrypt($data, $key=''){
$result='';
while(strlen($data)){
- $result=self::decrypt(substr($data,0,8192));
+ $result.=self::decrypt(substr($data,0,8192),$key);
$data=substr($data,8192);
}
return $result;
diff --git a/apps/files_encryption/lib/cryptstream.php b/apps/files_encryption/lib/cryptstream.php
index 86583096f1d..1a7c595cb83 100644..100755
--- a/apps/files_encryption/lib/cryptstream.php
+++ b/apps/files_encryption/lib/cryptstream.php
@@ -33,6 +33,7 @@ class OC_CryptStream{
private $path;
private $readBuffer;//for streams that dont support seeking
private $meta=array();//header/meta for source stream
+ private $count;
public function stream_open($path, $mode, $options, &$opened_path){
$path=str_replace('crypt://','',$path);
@@ -41,12 +42,12 @@ class OC_CryptStream{
$this->path=self::$sourceStreams[basename($path)]['path'];
}else{
$this->path=$path;
- OC_Log::write('files_encryption','open encrypted '.$path. ' in '.$mode,OC_Log::DEBUG);
+ 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=OC_FileSystem::fopen($path,$mode);
OC_FileProxy::$enabled=true;
if(!is_resource($this->source)){
- OC_Log::write('files_encryption','failed to open '.$path,OC_Log::ERROR);
+ OCP\Util::writeLog('files_encryption','failed to open '.$path,OCP\Util::ERROR);
}
}
if(is_resource($this->source)){
@@ -64,29 +65,19 @@ class OC_CryptStream{
}
public function stream_read($count){
- $pos=0;
- $currentPos=ftell($this->source);
- $offset=$currentPos%8192;
- $result='';
- if($offset>0){
- if($this->meta['seekable']){
- fseek($this->source,-$offset,SEEK_CUR);//if seeking isnt supported the internal read buffer will be used
- }else{
- $pos=strlen($this->readBuffer);
- $result=$this->readBuffer;
- }
- }
- while($count>$pos){
- $data=fread($this->source,8192);
- $pos+=8192;
- if(strlen($data)){
- $result.=OC_Crypt::decrypt($data);
- }
+ //$count will always be 8192 https://bugs.php.net/bug.php?id=21641
+ //This makes this function a lot simpler but will breake everything the moment it's fixed
+ if($count!=8192){
+ OCP\Util::writeLog('files_encryption','php bug 21641 no longer holds, decryption will not work',OCP\Util::FATAL);
+ die();
}
- if(!$this->meta['seekable']){
- $this->readBuffer=substr($result,$count);
+ $data=fread($this->source,8192);
+ if(strlen($data)){
+ $result=OC_Crypt::decrypt($data);
+ }else{
+ $result='';
}
- return substr($result,0,$count);
+ return $result;
}
public function stream_write($data){
@@ -102,14 +93,6 @@ class OC_CryptStream{
$data=substr($block,0,$currentPos%8192).$data;
}
while(strlen($data)>0){
- if(strlen($data)<8192){
- //fetch the current data in that block and append it to the input so we always write entire blocks
- $oldPos=ftell($this->source);
- $encryptedBlock=fread($this->source,8192);
- fseek($this->source,$oldPos);
- $block=OC_Crypt::decrypt($encryptedBlock);
- $data.=substr($block,strlen($data));
- }
$encrypted=OC_Crypt::encrypt(substr($data,0,8192));
fwrite($this->source,$encrypted);
$data=substr($data,8192);
@@ -147,7 +130,9 @@ class OC_CryptStream{
}
public function stream_close(){
- OC_FileCache::put($this->path,array('encrypted'=>true));
+ if($this->meta['mode']!='r' and $this->meta['mode']!='rb'){
+ OC_FileCache::put($this->path,array('encrypted'=>true));
+ }
return fclose($this->source);
}
-} \ No newline at end of file
+}
diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php
index c1c26d7754f..06f963fc981 100644..100755
--- a/apps/files_encryption/lib/proxy.php
+++ b/apps/files_encryption/lib/proxy.php
@@ -27,7 +27,7 @@
class OC_FileProxy_Encryption extends OC_FileProxy{
private static $blackList=null; //mimetypes blacklisted from encryption
- private static $metaData=array(); //metadata cache
+ private static $enableEncryption=null;
/**
* check if a file should be encrypted during write
@@ -35,14 +35,20 @@ class OC_FileProxy_Encryption extends OC_FileProxy{
* @return bool
*/
private static function shouldEncrypt($path){
+ if(is_null(self::$enableEncryption)){
+ self::$enableEncryption=(OCP\Config::getAppValue('files_encryption','enable_encryption','true')=='true');
+ }
+ if(!self::$enableEncryption){
+ return false;
+ }
if(is_null(self::$blackList)){
- self::$blackList=explode(',',OC_Appconfig::getValue('files_encryption','type_blacklist','jpg,png,jpeg,avi,mpg,mpeg,mkv,mp3,oga,ogv,ogg'));
+ self::$blackList=explode(',',OCP\Config::getAppValue('files_encryption','type_blacklist','jpg,png,jpeg,avi,mpg,mpeg,mkv,mp3,oga,ogv,ogg'));
}
if(self::isEncrypted($path)){
return true;
}
- $extention=substr($path,strrpos($path,'.')+1);
- if(array_search($extention,self::$blackList)===false){
+ $extension=substr($path,strrpos($path,'.')+1);
+ if(array_search($extension,self::$blackList)===false){
return true;
}
}
@@ -53,13 +59,8 @@ class OC_FileProxy_Encryption extends OC_FileProxy{
* @return bool
*/
private static function isEncrypted($path){
- if(isset(self::$metaData[$path])){
- $metadata=self::$metaData[$path];
- }else{
- $metadata=OC_FileCache::getCached($path);
- self::$metaData[$path]=$metadata;
- }
- return (bool)$metadata['encrypted'];
+ $metadata=OC_FileCache::getCached($path);
+ return isset($metadata['encrypted']) and (bool)$metadata['encrypted'];
}
public function preFile_put_contents($path,&$data){
@@ -89,14 +90,9 @@ class OC_FileProxy_Encryption extends OC_FileProxy{
}elseif(self::shouldEncrypt($path) and $meta['mode']!='r' and $meta['mode']!='rb'){
if(OC_Filesystem::file_exists($path) and OC_Filesystem::filesize($path)>0){
//first encrypt the target file so we don't end up with a half encrypted file
- OC_Log::write('files_encryption','Decrypting '.$path.' before writing',OC_Log::DEBUG);
+ OCP\Util::writeLog('files_encryption','Decrypting '.$path.' before writing',OCP\Util::DEBUG);
$tmp=fopen('php://temp');
- while(!feof($result)){
- $chunk=fread($result,8192);
- if($chunk){
- fwrite($tmp,$chunk);
- }
- }
+ OCP\Files::streamCopy($result,$tmp);
fclose($result);
OC_Filesystem::file_put_contents($path,$tmp);
fclose($tmp);
@@ -108,7 +104,7 @@ class OC_FileProxy_Encryption extends OC_FileProxy{
public function postGetMimeType($path,$mime){
if(self::isEncrypted($path)){
- $mime=OC_Helper::getMimeType('crypt://'.$path,'w');
+ $mime=OCP\Files::getMimeType('crypt://'.$path,'w');
}
return $mime;
}