cryptstream.php 4.7 KB

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