You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.0 KiB

  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 $meta=array();//header/meta for source stream
  33. private $writeCache;
  34. private $size;
  35. private static $rootView;
  36. public function stream_open($path, $mode, $options, &$opened_path) {
  37. if(!self::$rootView) {
  38. self::$rootView=new OC_FilesystemView('');
  39. }
  40. $path=str_replace('crypt://','',$path);
  41. if(dirname($path)=='streams' and isset(self::$sourceStreams[basename($path)])) {
  42. $this->source=self::$sourceStreams[basename($path)]['stream'];
  43. $this->path=self::$sourceStreams[basename($path)]['path'];
  44. $this->size=self::$sourceStreams[basename($path)]['size'];
  45. }else{
  46. $this->path=$path;
  47. if($mode=='w' or $mode=='w+' or $mode=='wb' or $mode=='wb+') {
  48. $this->size=0;
  49. }else{
  50. $this->size=self::$rootView->filesize($path,$mode);
  51. }
  52. OC_FileProxy::$enabled=false;//disable fileproxies so we can open the source file
  53. $this->source=self::$rootView->fopen($path,$mode);
  54. OC_FileProxy::$enabled=true;
  55. if(!is_resource($this->source)) {
  56. OCP\Util::writeLog('files_encryption','failed to open '.$path,OCP\Util::ERROR);
  57. }
  58. }
  59. if(is_resource($this->source)) {
  60. $this->meta=stream_get_meta_data($this->source);
  61. }
  62. return is_resource($this->source);
  63. }
  64. public function stream_seek($offset, $whence=SEEK_SET) {
  65. $this->flush();
  66. fseek($this->source,$offset,$whence);
  67. }
  68. public function stream_tell() {
  69. return ftell($this->source);
  70. }
  71. public function stream_read($count) {
  72. //$count will always be 8192 https://bugs.php.net/bug.php?id=21641
  73. //This makes this function a lot simpler but will breake everything the moment it's fixed
  74. $this->writeCache='';
  75. if($count!=8192) {
  76. OCP\Util::writeLog('files_encryption','php bug 21641 no longer holds, decryption will not work',OCP\Util::FATAL);
  77. die();
  78. }
  79. $pos=ftell($this->source);
  80. $data=fread($this->source,8192);
  81. if(strlen($data)) {
  82. $result=OC_Crypt::decrypt($data);
  83. }else{
  84. $result='';
  85. }
  86. $length=$this->size-$pos;
  87. if($length<8192) {
  88. $result=substr($result,0,$length);
  89. }
  90. return $result;
  91. }
  92. public function stream_write($data) {
  93. $length=strlen($data);
  94. $currentPos=ftell($this->source);
  95. if($this->writeCache) {
  96. $data=$this->writeCache.$data;
  97. $this->writeCache='';
  98. }
  99. if($currentPos%8192!=0) {
  100. //make sure we always start on a block start
  101. fseek($this->source,-($currentPos%8192),SEEK_CUR);
  102. $encryptedBlock=fread($this->source,8192);
  103. fseek($this->source,-($currentPos%8192),SEEK_CUR);
  104. $block=OC_Crypt::decrypt($encryptedBlock);
  105. $data=substr($block,0,$currentPos%8192).$data;
  106. fseek($this->source,-($currentPos%8192),SEEK_CUR);
  107. }
  108. $currentPos=ftell($this->source);
  109. while($remainingLength=strlen($data)>0) {
  110. if($remainingLength<8192) {
  111. $this->writeCache=$data;
  112. $data='';
  113. }else{
  114. $encrypted=OC_Crypt::encrypt(substr($data,0,8192));
  115. fwrite($this->source,$encrypted);
  116. $data=substr($data,8192);
  117. }
  118. }
  119. $this->size=max($this->size,$currentPos+$length);
  120. return $length;
  121. }
  122. public function stream_set_option($option,$arg1,$arg2) {
  123. switch($option) {
  124. case STREAM_OPTION_BLOCKING:
  125. stream_set_blocking($this->source,$arg1);
  126. break;
  127. case STREAM_OPTION_READ_TIMEOUT:
  128. stream_set_timeout($this->source,$arg1,$arg2);
  129. break;
  130. case STREAM_OPTION_WRITE_BUFFER:
  131. stream_set_write_buffer($this->source,$arg1,$arg2);
  132. }
  133. }
  134. public function stream_stat() {
  135. return fstat($this->source);
  136. }
  137. public function stream_lock($mode) {
  138. flock($this->source,$mode);
  139. }
  140. public function stream_flush() {
  141. return fflush($this->source);
  142. }
  143. public function stream_eof() {
  144. return feof($this->source);
  145. }
  146. private function flush() {
  147. if($this->writeCache) {
  148. $encrypted=OC_Crypt::encrypt($this->writeCache);
  149. fwrite($this->source,$encrypted);
  150. $this->writeCache='';
  151. }
  152. }
  153. public function stream_close() {
  154. $this->flush();
  155. if($this->meta['mode']!='r' and $this->meta['mode']!='rb') {
  156. OC_FileCache::put($this->path, array('encrypted'=>true,'size'=>$this->size),'');
  157. }
  158. return fclose($this->source);
  159. }
  160. }