1
0

Encryption.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Files\Stream;
  9. use Icewind\Streams\Wrapper;
  10. use OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException;
  11. use OC\Encryption\File;
  12. use OC\Encryption\Util;
  13. use OC\Files\Storage\Storage;
  14. use OCP\Encryption\IEncryptionModule;
  15. use function is_array;
  16. use function stream_context_create;
  17. class Encryption extends Wrapper {
  18. protected Util $util;
  19. protected File $file;
  20. protected IEncryptionModule $encryptionModule;
  21. protected Storage $storage;
  22. protected \OC\Files\Storage\Wrapper\Encryption $encryptionStorage;
  23. protected string $internalPath;
  24. protected string $cache;
  25. protected ?int $size = null;
  26. protected int $position;
  27. protected ?int $unencryptedSize = null;
  28. protected int $headerSize;
  29. protected int $unencryptedBlockSize;
  30. protected array $header;
  31. protected string $fullPath;
  32. protected bool $signed;
  33. /**
  34. * header data returned by the encryption module, will be written to the file
  35. * in case of a write operation
  36. */
  37. protected array $newHeader;
  38. /**
  39. * user who perform the read/write operation null for public access
  40. */
  41. protected ?string $uid;
  42. protected bool $readOnly;
  43. protected bool $writeFlag;
  44. protected array $expectedContextProperties;
  45. protected bool $fileUpdated;
  46. public function __construct() {
  47. $this->expectedContextProperties = [
  48. 'source',
  49. 'storage',
  50. 'internalPath',
  51. 'fullPath',
  52. 'encryptionModule',
  53. 'header',
  54. 'uid',
  55. 'file',
  56. 'util',
  57. 'size',
  58. 'unencryptedSize',
  59. 'encryptionStorage',
  60. 'headerSize',
  61. 'signed'
  62. ];
  63. }
  64. /**
  65. * Wraps a stream with the provided callbacks
  66. *
  67. * @param resource $source
  68. * @param string $internalPath relative to mount point
  69. * @param string $fullPath relative to data/
  70. * @param array $header
  71. * @param string $uid
  72. * @param IEncryptionModule $encryptionModule
  73. * @param Storage $storage
  74. * @param \OC\Files\Storage\Wrapper\Encryption $encStorage
  75. * @param Util $util
  76. * @param File $file
  77. * @param string $mode
  78. * @param int|float $size
  79. * @param int|float $unencryptedSize
  80. * @param int $headerSize
  81. * @param bool $signed
  82. * @param string $wrapper stream wrapper class
  83. * @return resource
  84. *
  85. * @throws \BadMethodCallException
  86. */
  87. public static function wrap(
  88. $source,
  89. $internalPath,
  90. $fullPath,
  91. array $header,
  92. $uid,
  93. IEncryptionModule $encryptionModule,
  94. Storage $storage,
  95. \OC\Files\Storage\Wrapper\Encryption $encStorage,
  96. Util $util,
  97. File $file,
  98. $mode,
  99. $size,
  100. $unencryptedSize,
  101. $headerSize,
  102. $signed,
  103. $wrapper = Encryption::class,
  104. ) {
  105. $context = stream_context_create([
  106. 'ocencryption' => [
  107. 'source' => $source,
  108. 'storage' => $storage,
  109. 'internalPath' => $internalPath,
  110. 'fullPath' => $fullPath,
  111. 'encryptionModule' => $encryptionModule,
  112. 'header' => $header,
  113. 'uid' => $uid,
  114. 'util' => $util,
  115. 'file' => $file,
  116. 'size' => $size,
  117. 'unencryptedSize' => $unencryptedSize,
  118. 'encryptionStorage' => $encStorage,
  119. 'headerSize' => $headerSize,
  120. 'signed' => $signed
  121. ]
  122. ]);
  123. return self::wrapSource($source, $context, 'ocencryption', $wrapper, $mode);
  124. }
  125. /**
  126. * add stream wrapper
  127. *
  128. * @param resource|int $source
  129. * @param resource|array $context
  130. * @param string|null $protocol
  131. * @param string|null $class
  132. * @param string $mode
  133. * @return resource
  134. * @throws \BadMethodCallException
  135. */
  136. protected static function wrapSource($source, $context = [], $protocol = null, $class = null, $mode = 'r+') {
  137. try {
  138. if ($protocol === null) {
  139. $protocol = self::getProtocol($class);
  140. }
  141. stream_wrapper_register($protocol, $class);
  142. $context = self::buildContext($protocol, $context, $source);
  143. if (self::isDirectoryHandle($source)) {
  144. $wrapped = opendir($protocol . '://', $context);
  145. } else {
  146. $wrapped = fopen($protocol . '://', $mode, false, $context);
  147. }
  148. } catch (\Exception $e) {
  149. stream_wrapper_unregister($protocol);
  150. throw $e;
  151. }
  152. stream_wrapper_unregister($protocol);
  153. return $wrapped;
  154. }
  155. /**
  156. * @todo this is a copy of \Icewind\Streams\WrapperHandler::buildContext -> combine to one shared method?
  157. */
  158. private static function buildContext($protocol, $context, $source) {
  159. if (is_array($context)) {
  160. $context['source'] = $source;
  161. return stream_context_create([$protocol => $context]);
  162. }
  163. return $context;
  164. }
  165. /**
  166. * Load the source from the stream context and return the context options
  167. *
  168. * @param string|null $name
  169. * @return array
  170. * @throws \BadMethodCallException
  171. */
  172. protected function loadContext($name = null) {
  173. $context = parent::loadContext($name);
  174. foreach ($this->expectedContextProperties as $property) {
  175. if (array_key_exists($property, $context)) {
  176. $this->{$property} = $context[$property];
  177. } else {
  178. throw new \BadMethodCallException('Invalid context, "' . $property . '" options not set');
  179. }
  180. }
  181. return $context;
  182. }
  183. public function stream_open($path, $mode, $options, &$opened_path) {
  184. $this->loadContext('ocencryption');
  185. $this->position = 0;
  186. $this->cache = '';
  187. $this->writeFlag = false;
  188. $this->fileUpdated = false;
  189. if (
  190. $mode === 'w'
  191. || $mode === 'w+'
  192. || $mode === 'wb'
  193. || $mode === 'wb+'
  194. || $mode === 'r+'
  195. || $mode === 'rb+'
  196. ) {
  197. $this->readOnly = false;
  198. } else {
  199. $this->readOnly = true;
  200. }
  201. $sharePath = $this->fullPath;
  202. if (!$this->storage->file_exists($this->internalPath)) {
  203. $sharePath = dirname($sharePath);
  204. }
  205. $accessList = [];
  206. if ($this->encryptionModule->needDetailedAccessList()) {
  207. $accessList = $this->file->getAccessList($sharePath);
  208. }
  209. $this->newHeader = $this->encryptionModule->begin($this->fullPath, $this->uid, $mode, $this->header, $accessList);
  210. $this->unencryptedBlockSize = $this->encryptionModule->getUnencryptedBlockSize($this->signed);
  211. if (
  212. $mode === 'w'
  213. || $mode === 'w+'
  214. || $mode === 'wb'
  215. || $mode === 'wb+'
  216. ) {
  217. // We're writing a new file so start write counter with 0 bytes
  218. $this->unencryptedSize = 0;
  219. $this->writeHeader();
  220. $this->headerSize = $this->util->getHeaderSize();
  221. $this->size = $this->headerSize;
  222. } else {
  223. $this->skipHeader();
  224. }
  225. return true;
  226. }
  227. public function stream_eof() {
  228. return $this->position >= $this->unencryptedSize;
  229. }
  230. public function stream_read($count) {
  231. $result = '';
  232. $count = min($count, $this->unencryptedSize - $this->position);
  233. while ($count > 0) {
  234. $remainingLength = $count;
  235. // update the cache of the current block
  236. $this->readCache();
  237. // determine the relative position in the current block
  238. $blockPosition = ($this->position % $this->unencryptedBlockSize);
  239. // if entire read inside current block then only position needs to be updated
  240. if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
  241. $result .= substr($this->cache, $blockPosition, $remainingLength);
  242. $this->position += $remainingLength;
  243. $count = 0;
  244. // otherwise remainder of current block is fetched, the block is flushed and the position updated
  245. } else {
  246. $result .= substr($this->cache, $blockPosition);
  247. $this->flush();
  248. $this->position += ($this->unencryptedBlockSize - $blockPosition);
  249. $count -= ($this->unencryptedBlockSize - $blockPosition);
  250. }
  251. }
  252. return $result;
  253. }
  254. /**
  255. * stream_read_block
  256. *
  257. * This function is a wrapper for function stream_read.
  258. * It calls stream read until the requested $blockSize was received or no remaining data is present.
  259. * This is required as stream_read only returns smaller chunks of data when the stream fetches from a
  260. * remote storage over the internet and it does not care about the given $blockSize.
  261. *
  262. * @param int $blockSize Length of requested data block in bytes
  263. * @return string Data fetched from stream.
  264. */
  265. private function stream_read_block(int $blockSize): string {
  266. $remaining = $blockSize;
  267. $data = '';
  268. do {
  269. $chunk = parent::stream_read($remaining);
  270. $chunk_len = strlen($chunk);
  271. $data .= $chunk;
  272. $remaining -= $chunk_len;
  273. } while (($remaining > 0) && ($chunk_len > 0));
  274. return $data;
  275. }
  276. public function stream_write($data) {
  277. $length = 0;
  278. // loop over $data to fit it in 6126 sized unencrypted blocks
  279. while (isset($data[0])) {
  280. $remainingLength = strlen($data);
  281. // set the cache to the current 6126 block
  282. $this->readCache();
  283. // for seekable streams the pointer is moved back to the beginning of the encrypted block
  284. // flush will start writing there when the position moves to another block
  285. $positionInFile = (int)floor($this->position / $this->unencryptedBlockSize) *
  286. $this->util->getBlockSize() + $this->headerSize;
  287. $resultFseek = $this->parentStreamSeek($positionInFile);
  288. // only allow writes on seekable streams, or at the end of the encrypted stream
  289. if (!$this->readOnly && ($resultFseek || $positionInFile === $this->size)) {
  290. // switch the writeFlag so flush() will write the block
  291. $this->writeFlag = true;
  292. $this->fileUpdated = true;
  293. // determine the relative position in the current block
  294. $blockPosition = ($this->position % $this->unencryptedBlockSize);
  295. // check if $data fits in current block
  296. // if so, overwrite existing data (if any)
  297. // update position and liberate $data
  298. if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
  299. $this->cache = substr($this->cache, 0, $blockPosition)
  300. . $data . substr($this->cache, $blockPosition + $remainingLength);
  301. $this->position += $remainingLength;
  302. $length += $remainingLength;
  303. $data = '';
  304. // if $data doesn't fit the current block, the fill the current block and reiterate
  305. // after the block is filled, it is flushed and $data is updatedxxx
  306. } else {
  307. $this->cache = substr($this->cache, 0, $blockPosition) .
  308. substr($data, 0, $this->unencryptedBlockSize - $blockPosition);
  309. $this->flush();
  310. $this->position += ($this->unencryptedBlockSize - $blockPosition);
  311. $length += ($this->unencryptedBlockSize - $blockPosition);
  312. $data = substr($data, $this->unencryptedBlockSize - $blockPosition);
  313. }
  314. } else {
  315. $data = '';
  316. }
  317. $this->unencryptedSize = max($this->unencryptedSize, $this->position);
  318. }
  319. return $length;
  320. }
  321. public function stream_tell() {
  322. return $this->position;
  323. }
  324. public function stream_seek($offset, $whence = SEEK_SET) {
  325. $return = false;
  326. switch ($whence) {
  327. case SEEK_SET:
  328. $newPosition = $offset;
  329. break;
  330. case SEEK_CUR:
  331. $newPosition = $this->position + $offset;
  332. break;
  333. case SEEK_END:
  334. $newPosition = $this->unencryptedSize + $offset;
  335. break;
  336. default:
  337. return $return;
  338. }
  339. if ($newPosition > $this->unencryptedSize || $newPosition < 0) {
  340. return $return;
  341. }
  342. $newFilePosition = (int)floor($newPosition / $this->unencryptedBlockSize)
  343. * $this->util->getBlockSize() + $this->headerSize;
  344. $oldFilePosition = parent::stream_tell();
  345. if ($this->parentStreamSeek($newFilePosition)) {
  346. $this->parentStreamSeek($oldFilePosition);
  347. $this->flush();
  348. $this->parentStreamSeek($newFilePosition);
  349. $this->position = $newPosition;
  350. $return = true;
  351. }
  352. return $return;
  353. }
  354. public function stream_close() {
  355. $this->flush('end');
  356. $position = (int)floor($this->position / $this->unencryptedBlockSize);
  357. $remainingData = $this->encryptionModule->end($this->fullPath, $position . 'end');
  358. if ($this->readOnly === false) {
  359. if (!empty($remainingData)) {
  360. parent::stream_write($remainingData);
  361. }
  362. $this->encryptionStorage->updateUnencryptedSize($this->fullPath, $this->unencryptedSize);
  363. }
  364. $result = parent::stream_close();
  365. if ($this->fileUpdated) {
  366. $cache = $this->storage->getCache();
  367. $cacheEntry = $cache->get($this->internalPath);
  368. if ($cacheEntry) {
  369. $version = $cacheEntry['encryptedVersion'] + 1;
  370. $cache->update($cacheEntry->getId(), ['encrypted' => $version, 'encryptedVersion' => $version, 'unencrypted_size' => $this->unencryptedSize]);
  371. }
  372. }
  373. return $result;
  374. }
  375. /**
  376. * write block to file
  377. * @param string $positionPrefix
  378. */
  379. protected function flush($positionPrefix = '') {
  380. // write to disk only when writeFlag was set to 1
  381. if ($this->writeFlag) {
  382. // Disable the file proxies so that encryption is not
  383. // automatically attempted when the file is written to disk -
  384. // we are handling that separately here and we don't want to
  385. // get into an infinite loop
  386. $position = (int)floor($this->position / $this->unencryptedBlockSize);
  387. $encrypted = $this->encryptionModule->encrypt($this->cache, $position . $positionPrefix);
  388. $bytesWritten = parent::stream_write($encrypted);
  389. $this->writeFlag = false;
  390. // Check whether the write concerns the last block
  391. // If so then update the encrypted filesize
  392. // Note that the unencrypted pointer and filesize are NOT yet updated when flush() is called
  393. // We recalculate the encrypted filesize as we do not know the context of calling flush()
  394. $completeBlocksInFile = (int)floor($this->unencryptedSize / $this->unencryptedBlockSize);
  395. if ($completeBlocksInFile === (int)floor($this->position / $this->unencryptedBlockSize)) {
  396. $this->size = $this->util->getBlockSize() * $completeBlocksInFile;
  397. $this->size += $bytesWritten;
  398. $this->size += $this->headerSize;
  399. }
  400. }
  401. // always empty the cache (otherwise readCache() will not fill it with the new block)
  402. $this->cache = '';
  403. }
  404. /**
  405. * read block to file
  406. */
  407. protected function readCache() {
  408. // cache should always be empty string when this function is called
  409. // don't try to fill the cache when trying to write at the end of the unencrypted file when it coincides with new block
  410. if ($this->cache === '' && !($this->position === $this->unencryptedSize && ($this->position % $this->unencryptedBlockSize) === 0)) {
  411. // Get the data from the file handle
  412. $data = $this->stream_read_block($this->util->getBlockSize());
  413. $position = (int)floor($this->position / $this->unencryptedBlockSize);
  414. $numberOfChunks = (int)($this->unencryptedSize / $this->unencryptedBlockSize);
  415. if ($numberOfChunks === $position) {
  416. $position .= 'end';
  417. }
  418. $this->cache = $this->encryptionModule->decrypt($data, $position);
  419. }
  420. }
  421. /**
  422. * write header at beginning of encrypted file
  423. *
  424. * @return int|false
  425. * @throws EncryptionHeaderKeyExistsException if header key is already in use
  426. */
  427. protected function writeHeader() {
  428. $header = $this->util->createHeader($this->newHeader, $this->encryptionModule);
  429. $this->fileUpdated = true;
  430. return parent::stream_write($header);
  431. }
  432. /**
  433. * read first block to skip the header
  434. */
  435. protected function skipHeader() {
  436. $this->stream_read_block($this->headerSize);
  437. }
  438. /**
  439. * call stream_seek() from parent class
  440. *
  441. * @param integer $position
  442. * @return bool
  443. */
  444. protected function parentStreamSeek($position) {
  445. return parent::stream_seek($position);
  446. }
  447. /**
  448. * @param string $path
  449. * @param array $options
  450. * @return bool
  451. */
  452. public function dir_opendir($path, $options) {
  453. return false;
  454. }
  455. }