cryptstream.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2011 Robin Appelman icewind1991@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * transparently encrypted filestream
  24. *
  25. * you can use it as wrapper around an existing stream by setting OC_CryptStream::$sourceStreams['foo']=array('path'=>$path,'stream'=>$stream)
  26. * and then fopen('crypt://streams/foo');
  27. */
  28. class OC_CryptStream{
  29. public static $sourceStreams=array();
  30. private $source;
  31. private $path;
  32. private $readBuffer;//for streams that dont support seeking
  33. private $meta=array();//header/meta for source stream
  34. private $count;
  35. private $writeCache;
  36. private $size;
  37. private static $rootView;
  38. public function stream_open($path, $mode, $options, &$opened_path){
  39. if(!self::$rootView){
  40. self::$rootView=new OC_FilesystemView('');
  41. }
  42. $path=str_replace('crypt://','',$path);
  43. if(dirname($path)=='streams' and isset(self::$sourceStreams[basename($path)])){
  44. $this->source=self::$sourceStreams[basename($path)]['stream'];
  45. $this->path=self::$sourceStreams[basename($path)]['path'];
  46. $this->size=self::$sourceStreams[basename($path)]['size'];
  47. }else{
  48. $this->path=$path;
  49. if($mode=='w' or $mode=='w+' or $mode=='wb' or $mode=='wb+'){
  50. $this->size=0;
  51. }else{
  52. $this->size=self::$rootView->filesize($path,$mode);
  53. }
  54. OC_FileProxy::$enabled=false;//disable fileproxies so we can open the source file
  55. $this->source=self::$rootView->fopen($path,$mode);
  56. OC_FileProxy::$enabled=true;
  57. if(!is_resource($this->source)){
  58. OCP\Util::writeLog('files_encryption','failed to open '.$path,OCP\Util::ERROR);
  59. }
  60. }
  61. if(is_resource($this->source)){
  62. $this->meta=stream_get_meta_data($this->source);
  63. }
  64. return is_resource($this->source);
  65. }
  66. public function stream_seek($offset, $whence=SEEK_SET){
  67. $this->flush();
  68. fseek($this->source,$offset,$whence);
  69. }
  70. public function stream_tell(){
  71. return ftell($this->source);
  72. }
  73. public function stream_read($count){
  74. //$count will always be 8192 https://bugs.php.net/bug.php?id=21641
  75. //This makes this function a lot simpler but will breake everything the moment it's fixed
  76. $this->writeCache='';
  77. if($count!=8192){
  78. OCP\Util::writeLog('files_encryption','php bug 21641 no longer holds, decryption will not work',OCP\Util::FATAL);
  79. die();
  80. }
  81. $pos=ftell($this->source);
  82. $data=fread($this->source,8192);
  83. if(strlen($data)){
  84. $result=OC_Crypt::decrypt($data);
  85. }else{
  86. $result='';
  87. }
  88. $length=$this->size-$pos;
  89. if($length<8192){
  90. $result=substr($result,0,$length);
  91. }
  92. return $result;
  93. }
  94. public function stream_write($data){
  95. $length=strlen($data);
  96. $written=0;
  97. $currentPos=ftell($this->source);
  98. if($this->writeCache){
  99. $data=$this->writeCache.$data;
  100. $this->writeCache='';
  101. }
  102. if($currentPos%8192!=0){
  103. //make sure we always start on a block start
  104. fseek($this->source,-($currentPos%8192),SEEK_CUR);
  105. $encryptedBlock=fread($this->source,8192);
  106. fseek($this->source,-($currentPos%8192),SEEK_CUR);
  107. $block=OC_Crypt::decrypt($encryptedBlock);
  108. $data=substr($block,0,$currentPos%8192).$data;
  109. fseek($this->source,-($currentPos%8192),SEEK_CUR);
  110. }
  111. $currentPos=ftell($this->source);
  112. while($remainingLength=strlen($data)>0){
  113. if($remainingLength<8192){
  114. $this->writeCache=$data;
  115. $data='';
  116. }else{
  117. $encrypted=OC_Crypt::encrypt(substr($data,0,8192));
  118. fwrite($this->source,$encrypted);
  119. $data=substr($data,8192);
  120. }
  121. }
  122. $this->size=max($this->size,$currentPos+$length);
  123. return $length;
  124. }
  125. public function stream_set_option($option,$arg1,$arg2){
  126. switch($option){
  127. case STREAM_OPTION_BLOCKING:
  128. stream_set_blocking($this->source,$arg1);
  129. break;
  130. case STREAM_OPTION_READ_TIMEOUT:
  131. stream_set_timeout($this->source,$arg1,$arg2);
  132. break;
  133. case STREAM_OPTION_WRITE_BUFFER:
  134. stream_set_write_buffer($this->source,$arg1,$arg2);
  135. }
  136. }
  137. public function stream_stat(){
  138. return fstat($this->source);
  139. }
  140. public function stream_lock($mode){
  141. flock($this->source,$mode);
  142. }
  143. public function stream_flush(){
  144. return fflush($this->source);
  145. }
  146. public function stream_eof(){
  147. return feof($this->source);
  148. }
  149. private function flush(){
  150. if($this->writeCache){
  151. $encrypted=OC_Crypt::encrypt($this->writeCache);
  152. fwrite($this->source,$encrypted);
  153. $this->writeCache='';
  154. }
  155. }
  156. public function stream_close(){
  157. $this->flush();
  158. if($this->meta['mode']!='r' and $this->meta['mode']!='rb'){
  159. OC_FileCache::put($this->path,array('encrypted'=>true,'size'=>$this->size),'/');
  160. }
  161. return fclose($this->source);
  162. }
  163. }