Encryption.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Storage\Wrapper;
  8. use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
  9. use OC\Encryption\Update;
  10. use OC\Encryption\Util;
  11. use OC\Files\Cache\CacheEntry;
  12. use OC\Files\Filesystem;
  13. use OC\Files\Mount\Manager;
  14. use OC\Files\ObjectStore\ObjectStoreStorage;
  15. use OC\Files\Storage\Common;
  16. use OC\Files\Storage\LocalTempFileTrait;
  17. use OC\Memcache\ArrayCache;
  18. use OCP\Cache\CappedMemoryCache;
  19. use OCP\Encryption\IFile;
  20. use OCP\Encryption\IManager;
  21. use OCP\Encryption\Keys\IStorage;
  22. use OCP\Files\Cache\ICacheEntry;
  23. use OCP\Files\Mount\IMountPoint;
  24. use OCP\Files\Storage;
  25. use Psr\Log\LoggerInterface;
  26. class Encryption extends Wrapper {
  27. use LocalTempFileTrait;
  28. private string $mountPoint;
  29. protected array $unencryptedSize = [];
  30. private IMountPoint $mount;
  31. /** for which path we execute the repair step to avoid recursions */
  32. private array $fixUnencryptedSizeOf = [];
  33. /** @var CappedMemoryCache<bool> */
  34. private CappedMemoryCache $encryptedPaths;
  35. private bool $enabled = true;
  36. /**
  37. * @param array $parameters
  38. */
  39. public function __construct(
  40. array $parameters,
  41. private IManager $encryptionManager,
  42. private Util $util,
  43. private LoggerInterface $logger,
  44. private IFile $fileHelper,
  45. private ?string $uid,
  46. private IStorage $keyStorage,
  47. private Update $update,
  48. private Manager $mountManager,
  49. private ArrayCache $arrayCache,
  50. ) {
  51. $this->mountPoint = $parameters['mountPoint'];
  52. $this->mount = $parameters['mount'];
  53. $this->encryptedPaths = new CappedMemoryCache();
  54. parent::__construct($parameters);
  55. }
  56. public function filesize(string $path): int|float|false {
  57. $fullPath = $this->getFullPath($path);
  58. $info = $this->getCache()->get($path);
  59. if ($info === false) {
  60. return false;
  61. }
  62. if (isset($this->unencryptedSize[$fullPath])) {
  63. $size = $this->unencryptedSize[$fullPath];
  64. // Update file cache (only if file is already cached).
  65. // Certain files are not cached (e.g. *.part).
  66. if (isset($info['fileid'])) {
  67. if ($info instanceof ICacheEntry) {
  68. $info['encrypted'] = $info['encryptedVersion'];
  69. } else {
  70. /**
  71. * @psalm-suppress RedundantCondition
  72. */
  73. if (!is_array($info)) {
  74. $info = [];
  75. }
  76. $info['encrypted'] = true;
  77. $info = new CacheEntry($info);
  78. }
  79. if ($size !== $info->getUnencryptedSize()) {
  80. $this->getCache()->update($info->getId(), [
  81. 'unencrypted_size' => $size
  82. ]);
  83. }
  84. }
  85. return $size;
  86. }
  87. if (isset($info['fileid']) && $info['encrypted']) {
  88. return $this->verifyUnencryptedSize($path, $info->getUnencryptedSize());
  89. }
  90. return $this->storage->filesize($path);
  91. }
  92. private function modifyMetaData(string $path, array $data): array {
  93. $fullPath = $this->getFullPath($path);
  94. $info = $this->getCache()->get($path);
  95. if (isset($this->unencryptedSize[$fullPath])) {
  96. $data['encrypted'] = true;
  97. $data['size'] = $this->unencryptedSize[$fullPath];
  98. $data['unencrypted_size'] = $data['size'];
  99. } else {
  100. if (isset($info['fileid']) && $info['encrypted']) {
  101. $data['size'] = $this->verifyUnencryptedSize($path, $info->getUnencryptedSize());
  102. $data['encrypted'] = true;
  103. $data['unencrypted_size'] = $data['size'];
  104. }
  105. }
  106. if (isset($info['encryptedVersion']) && $info['encryptedVersion'] > 1) {
  107. $data['encryptedVersion'] = $info['encryptedVersion'];
  108. }
  109. return $data;
  110. }
  111. public function getMetaData(string $path): ?array {
  112. $data = $this->storage->getMetaData($path);
  113. if (is_null($data)) {
  114. return null;
  115. }
  116. return $this->modifyMetaData($path, $data);
  117. }
  118. public function getDirectoryContent(string $directory): \Traversable {
  119. $parent = rtrim($directory, '/');
  120. foreach ($this->getWrapperStorage()->getDirectoryContent($directory) as $data) {
  121. yield $this->modifyMetaData($parent . '/' . $data['name'], $data);
  122. }
  123. }
  124. public function file_get_contents(string $path): string|false {
  125. $encryptionModule = $this->getEncryptionModule($path);
  126. if ($encryptionModule) {
  127. $handle = $this->fopen($path, 'r');
  128. if (!$handle) {
  129. return false;
  130. }
  131. $data = stream_get_contents($handle);
  132. fclose($handle);
  133. return $data;
  134. }
  135. return $this->storage->file_get_contents($path);
  136. }
  137. public function file_put_contents(string $path, mixed $data): int|float|false {
  138. // file put content will always be translated to a stream write
  139. $handle = $this->fopen($path, 'w');
  140. if (is_resource($handle)) {
  141. $written = fwrite($handle, $data);
  142. fclose($handle);
  143. return $written;
  144. }
  145. return false;
  146. }
  147. public function unlink(string $path): bool {
  148. $fullPath = $this->getFullPath($path);
  149. if ($this->util->isExcluded($fullPath)) {
  150. return $this->storage->unlink($path);
  151. }
  152. $encryptionModule = $this->getEncryptionModule($path);
  153. if ($encryptionModule) {
  154. $this->keyStorage->deleteAllFileKeys($fullPath);
  155. }
  156. return $this->storage->unlink($path);
  157. }
  158. public function rename(string $source, string $target): bool {
  159. $result = $this->storage->rename($source, $target);
  160. if ($result &&
  161. // versions always use the keys from the original file, so we can skip
  162. // this step for versions
  163. $this->isVersion($target) === false &&
  164. $this->encryptionManager->isEnabled()) {
  165. $sourcePath = $this->getFullPath($source);
  166. if (!$this->util->isExcluded($sourcePath)) {
  167. $targetPath = $this->getFullPath($target);
  168. if (isset($this->unencryptedSize[$sourcePath])) {
  169. $this->unencryptedSize[$targetPath] = $this->unencryptedSize[$sourcePath];
  170. }
  171. $this->keyStorage->renameKeys($sourcePath, $targetPath);
  172. $module = $this->getEncryptionModule($target);
  173. if ($module) {
  174. $module->update($targetPath, $this->uid, []);
  175. }
  176. }
  177. }
  178. return $result;
  179. }
  180. public function rmdir(string $path): bool {
  181. $result = $this->storage->rmdir($path);
  182. $fullPath = $this->getFullPath($path);
  183. if ($result &&
  184. $this->util->isExcluded($fullPath) === false &&
  185. $this->encryptionManager->isEnabled()
  186. ) {
  187. $this->keyStorage->deleteAllFileKeys($fullPath);
  188. }
  189. return $result;
  190. }
  191. public function isReadable(string $path): bool {
  192. $isReadable = true;
  193. $metaData = $this->getMetaData($path);
  194. if (
  195. !$this->is_dir($path) &&
  196. isset($metaData['encrypted']) &&
  197. $metaData['encrypted'] === true
  198. ) {
  199. $fullPath = $this->getFullPath($path);
  200. $module = $this->getEncryptionModule($path);
  201. $isReadable = $module->isReadable($fullPath, $this->uid);
  202. }
  203. return $this->storage->isReadable($path) && $isReadable;
  204. }
  205. public function copy(string $source, string $target): bool {
  206. $sourcePath = $this->getFullPath($source);
  207. if ($this->util->isExcluded($sourcePath)) {
  208. return $this->storage->copy($source, $target);
  209. }
  210. // need to stream copy file by file in case we copy between a encrypted
  211. // and a unencrypted storage
  212. $this->unlink($target);
  213. return $this->copyFromStorage($this, $source, $target);
  214. }
  215. public function fopen(string $path, string $mode) {
  216. // check if the file is stored in the array cache, this means that we
  217. // copy a file over to the versions folder, in this case we don't want to
  218. // decrypt it
  219. if ($this->arrayCache->hasKey('encryption_copy_version_' . $path)) {
  220. $this->arrayCache->remove('encryption_copy_version_' . $path);
  221. return $this->storage->fopen($path, $mode);
  222. }
  223. if (!$this->enabled) {
  224. return $this->storage->fopen($path, $mode);
  225. }
  226. $encryptionEnabled = $this->encryptionManager->isEnabled();
  227. $shouldEncrypt = false;
  228. $encryptionModule = null;
  229. $header = $this->getHeader($path);
  230. $signed = isset($header['signed']) && $header['signed'] === 'true';
  231. $fullPath = $this->getFullPath($path);
  232. $encryptionModuleId = $this->util->getEncryptionModuleId($header);
  233. if ($this->util->isExcluded($fullPath) === false) {
  234. $size = $unencryptedSize = 0;
  235. $realFile = $this->util->stripPartialFileExtension($path);
  236. $targetExists = $this->is_file($realFile) || $this->file_exists($path);
  237. $targetIsEncrypted = false;
  238. if ($targetExists) {
  239. // in case the file exists we require the explicit module as
  240. // specified in the file header - otherwise we need to fail hard to
  241. // prevent data loss on client side
  242. if (!empty($encryptionModuleId)) {
  243. $targetIsEncrypted = true;
  244. $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId);
  245. }
  246. if ($this->file_exists($path)) {
  247. $size = $this->storage->filesize($path);
  248. $unencryptedSize = $this->filesize($path);
  249. } else {
  250. $size = $unencryptedSize = 0;
  251. }
  252. }
  253. try {
  254. if (
  255. $mode === 'w'
  256. || $mode === 'w+'
  257. || $mode === 'wb'
  258. || $mode === 'wb+'
  259. ) {
  260. // if we update a encrypted file with a un-encrypted one we change the db flag
  261. if ($targetIsEncrypted && $encryptionEnabled === false) {
  262. $cache = $this->storage->getCache();
  263. $entry = $cache->get($path);
  264. $cache->update($entry->getId(), ['encrypted' => 0]);
  265. }
  266. if ($encryptionEnabled) {
  267. // if $encryptionModuleId is empty, the default module will be used
  268. $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId);
  269. $shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath);
  270. $signed = true;
  271. }
  272. } else {
  273. $info = $this->getCache()->get($path);
  274. // only get encryption module if we found one in the header
  275. // or if file should be encrypted according to the file cache
  276. if (!empty($encryptionModuleId)) {
  277. $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId);
  278. $shouldEncrypt = true;
  279. } elseif (empty($encryptionModuleId) && $info['encrypted'] === true) {
  280. // we come from a old installation. No header and/or no module defined
  281. // but the file is encrypted. In this case we need to use the
  282. // OC_DEFAULT_MODULE to read the file
  283. $encryptionModule = $this->encryptionManager->getEncryptionModule('OC_DEFAULT_MODULE');
  284. $shouldEncrypt = true;
  285. $targetIsEncrypted = true;
  286. }
  287. }
  288. } catch (ModuleDoesNotExistsException $e) {
  289. $this->logger->warning('Encryption module "' . $encryptionModuleId . '" not found, file will be stored unencrypted', [
  290. 'exception' => $e,
  291. 'app' => 'core',
  292. ]);
  293. }
  294. // encryption disabled on write of new file and write to existing unencrypted file -> don't encrypt
  295. if (!$encryptionEnabled || !$this->shouldEncrypt($path)) {
  296. if (!$targetExists || !$targetIsEncrypted) {
  297. $shouldEncrypt = false;
  298. }
  299. }
  300. if ($shouldEncrypt === true && $encryptionModule !== null) {
  301. $this->encryptedPaths->set($this->util->stripPartialFileExtension($path), true);
  302. $headerSize = $this->getHeaderSize($path);
  303. $source = $this->storage->fopen($path, $mode);
  304. if (!is_resource($source)) {
  305. return false;
  306. }
  307. $handle = \OC\Files\Stream\Encryption::wrap($source, $path, $fullPath, $header,
  308. $this->uid, $encryptionModule, $this->storage, $this, $this->util, $this->fileHelper, $mode,
  309. $size, $unencryptedSize, $headerSize, $signed);
  310. return $handle;
  311. }
  312. }
  313. return $this->storage->fopen($path, $mode);
  314. }
  315. /**
  316. * perform some plausibility checks if the unencrypted size is correct.
  317. * If not, we calculate the correct unencrypted size and return it
  318. *
  319. * @param string $path internal path relative to the storage root
  320. * @param int $unencryptedSize size of the unencrypted file
  321. *
  322. * @return int unencrypted size
  323. */
  324. protected function verifyUnencryptedSize(string $path, int $unencryptedSize): int {
  325. $size = $this->storage->filesize($path);
  326. $result = $unencryptedSize;
  327. if ($unencryptedSize < 0 ||
  328. ($size > 0 && $unencryptedSize === $size) ||
  329. $unencryptedSize > $size
  330. ) {
  331. // check if we already calculate the unencrypted size for the
  332. // given path to avoid recursions
  333. if (isset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]) === false) {
  334. $this->fixUnencryptedSizeOf[$this->getFullPath($path)] = true;
  335. try {
  336. $result = $this->fixUnencryptedSize($path, $size, $unencryptedSize);
  337. } catch (\Exception $e) {
  338. $this->logger->error('Couldn\'t re-calculate unencrypted size for ' . $path, ['exception' => $e]);
  339. }
  340. unset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]);
  341. }
  342. }
  343. return $result;
  344. }
  345. /**
  346. * calculate the unencrypted size
  347. *
  348. * @param string $path internal path relative to the storage root
  349. * @param int $size size of the physical file
  350. * @param int $unencryptedSize size of the unencrypted file
  351. */
  352. protected function fixUnencryptedSize(string $path, int $size, int $unencryptedSize): int|float {
  353. $headerSize = $this->getHeaderSize($path);
  354. $header = $this->getHeader($path);
  355. $encryptionModule = $this->getEncryptionModule($path);
  356. $stream = $this->storage->fopen($path, 'r');
  357. // if we couldn't open the file we return the old unencrypted size
  358. if (!is_resource($stream)) {
  359. $this->logger->error('Could not open ' . $path . '. Recalculation of unencrypted size aborted.');
  360. return $unencryptedSize;
  361. }
  362. $newUnencryptedSize = 0;
  363. $size -= $headerSize;
  364. $blockSize = $this->util->getBlockSize();
  365. // if a header exists we skip it
  366. if ($headerSize > 0) {
  367. $this->fread_block($stream, $headerSize);
  368. }
  369. // fast path, else the calculation for $lastChunkNr is bogus
  370. if ($size === 0) {
  371. return 0;
  372. }
  373. $signed = isset($header['signed']) && $header['signed'] === 'true';
  374. $unencryptedBlockSize = $encryptionModule->getUnencryptedBlockSize($signed);
  375. // calculate last chunk nr
  376. // next highest is end of chunks, one subtracted is last one
  377. // we have to read the last chunk, we can't just calculate it (because of padding etc)
  378. $lastChunkNr = ceil($size / $blockSize) - 1;
  379. // calculate last chunk position
  380. $lastChunkPos = ($lastChunkNr * $blockSize);
  381. // try to fseek to the last chunk, if it fails we have to read the whole file
  382. if (@fseek($stream, $lastChunkPos, SEEK_CUR) === 0) {
  383. $newUnencryptedSize += $lastChunkNr * $unencryptedBlockSize;
  384. }
  385. $lastChunkContentEncrypted = '';
  386. $count = $blockSize;
  387. while ($count > 0) {
  388. $data = $this->fread_block($stream, $blockSize);
  389. $count = strlen($data);
  390. $lastChunkContentEncrypted .= $data;
  391. if (strlen($lastChunkContentEncrypted) > $blockSize) {
  392. $newUnencryptedSize += $unencryptedBlockSize;
  393. $lastChunkContentEncrypted = substr($lastChunkContentEncrypted, $blockSize);
  394. }
  395. }
  396. fclose($stream);
  397. // we have to decrypt the last chunk to get it actual size
  398. $encryptionModule->begin($this->getFullPath($path), $this->uid, 'r', $header, []);
  399. $decryptedLastChunk = $encryptionModule->decrypt($lastChunkContentEncrypted, $lastChunkNr . 'end');
  400. $decryptedLastChunk .= $encryptionModule->end($this->getFullPath($path), $lastChunkNr . 'end');
  401. // calc the real file size with the size of the last chunk
  402. $newUnencryptedSize += strlen($decryptedLastChunk);
  403. $this->updateUnencryptedSize($this->getFullPath($path), $newUnencryptedSize);
  404. // write to cache if applicable
  405. $cache = $this->storage->getCache();
  406. $entry = $cache->get($path);
  407. $cache->update($entry['fileid'], [
  408. 'unencrypted_size' => $newUnencryptedSize
  409. ]);
  410. return $newUnencryptedSize;
  411. }
  412. /**
  413. * fread_block
  414. *
  415. * This function is a wrapper around the fread function. It is based on the
  416. * stream_read_block function from lib/private/Files/Streams/Encryption.php
  417. * It calls stream read until the requested $blockSize was received or no remaining data is present.
  418. * This is required as stream_read only returns smaller chunks of data when the stream fetches from a
  419. * remote storage over the internet and it does not care about the given $blockSize.
  420. *
  421. * @param resource $handle the stream to read from
  422. * @param int $blockSize Length of requested data block in bytes
  423. * @return string Data fetched from stream.
  424. */
  425. private function fread_block($handle, int $blockSize): string {
  426. $remaining = $blockSize;
  427. $data = '';
  428. do {
  429. $chunk = fread($handle, $remaining);
  430. $chunk_len = strlen($chunk);
  431. $data .= $chunk;
  432. $remaining -= $chunk_len;
  433. } while (($remaining > 0) && ($chunk_len > 0));
  434. return $data;
  435. }
  436. public function moveFromStorage(
  437. Storage\IStorage $sourceStorage,
  438. string $sourceInternalPath,
  439. string $targetInternalPath,
  440. $preserveMtime = true,
  441. ): bool {
  442. if ($sourceStorage === $this) {
  443. return $this->rename($sourceInternalPath, $targetInternalPath);
  444. }
  445. // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed:
  446. // - call $this->storage->moveFromStorage() instead of $this->copyBetweenStorage
  447. // - copy the file cache update from $this->copyBetweenStorage to this method
  448. // - copy the copyKeys() call from $this->copyBetweenStorage to this method
  449. // - remove $this->copyBetweenStorage
  450. if (!$sourceStorage->isDeletable($sourceInternalPath)) {
  451. return false;
  452. }
  453. $result = $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, true);
  454. if ($result) {
  455. if ($sourceStorage->is_dir($sourceInternalPath)) {
  456. $result = $sourceStorage->rmdir($sourceInternalPath);
  457. } else {
  458. $result = $sourceStorage->unlink($sourceInternalPath);
  459. }
  460. }
  461. return $result;
  462. }
  463. public function copyFromStorage(
  464. Storage\IStorage $sourceStorage,
  465. string $sourceInternalPath,
  466. string $targetInternalPath,
  467. $preserveMtime = false,
  468. $isRename = false,
  469. ): bool {
  470. // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed:
  471. // - call $this->storage->copyFromStorage() instead of $this->copyBetweenStorage
  472. // - copy the file cache update from $this->copyBetweenStorage to this method
  473. // - copy the copyKeys() call from $this->copyBetweenStorage to this method
  474. // - remove $this->copyBetweenStorage
  475. return $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename);
  476. }
  477. /**
  478. * Update the encrypted cache version in the database
  479. */
  480. private function updateEncryptedVersion(
  481. Storage\IStorage $sourceStorage,
  482. string $sourceInternalPath,
  483. string $targetInternalPath,
  484. bool $isRename,
  485. bool $keepEncryptionVersion,
  486. ): void {
  487. $isEncrypted = $this->encryptionManager->isEnabled() && $this->shouldEncrypt($targetInternalPath);
  488. $cacheInformation = [
  489. 'encrypted' => $isEncrypted,
  490. ];
  491. if ($isEncrypted) {
  492. $sourceCacheEntry = $sourceStorage->getCache()->get($sourceInternalPath);
  493. $targetCacheEntry = $this->getCache()->get($targetInternalPath);
  494. // Rename of the cache already happened, so we do the cleanup on the target
  495. if ($sourceCacheEntry === false && $targetCacheEntry !== false) {
  496. $encryptedVersion = $targetCacheEntry['encryptedVersion'];
  497. $isRename = false;
  498. } else {
  499. $encryptedVersion = $sourceCacheEntry['encryptedVersion'];
  500. }
  501. // In case of a move operation from an unencrypted to an encrypted
  502. // storage the old encrypted version would stay with "0" while the
  503. // correct value would be "1". Thus we manually set the value to "1"
  504. // for those cases.
  505. // See also https://github.com/owncloud/core/issues/23078
  506. if ($encryptedVersion === 0 || !$keepEncryptionVersion) {
  507. $encryptedVersion = 1;
  508. }
  509. $cacheInformation['encryptedVersion'] = $encryptedVersion;
  510. }
  511. // in case of a rename we need to manipulate the source cache because
  512. // this information will be kept for the new target
  513. if ($isRename) {
  514. $sourceStorage->getCache()->put($sourceInternalPath, $cacheInformation);
  515. } else {
  516. $this->getCache()->put($targetInternalPath, $cacheInformation);
  517. }
  518. }
  519. /**
  520. * copy file between two storages
  521. * @throws \Exception
  522. */
  523. private function copyBetweenStorage(
  524. Storage\IStorage $sourceStorage,
  525. string $sourceInternalPath,
  526. string $targetInternalPath,
  527. bool $preserveMtime,
  528. bool $isRename,
  529. ): bool {
  530. // for versions we have nothing to do, because versions should always use the
  531. // key from the original file. Just create a 1:1 copy and done
  532. if ($this->isVersion($targetInternalPath) ||
  533. $this->isVersion($sourceInternalPath)) {
  534. // remember that we try to create a version so that we can detect it during
  535. // fopen($sourceInternalPath) and by-pass the encryption in order to
  536. // create a 1:1 copy of the file
  537. $this->arrayCache->set('encryption_copy_version_' . $sourceInternalPath, true);
  538. $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  539. $this->arrayCache->remove('encryption_copy_version_' . $sourceInternalPath);
  540. if ($result) {
  541. $info = $this->getCache('', $sourceStorage)->get($sourceInternalPath);
  542. // make sure that we update the unencrypted size for the version
  543. if (isset($info['encrypted']) && $info['encrypted'] === true) {
  544. $this->updateUnencryptedSize(
  545. $this->getFullPath($targetInternalPath),
  546. $info->getUnencryptedSize()
  547. );
  548. }
  549. $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, true);
  550. }
  551. return $result;
  552. }
  553. // first copy the keys that we reuse the existing file key on the target location
  554. // and don't create a new one which would break versions for example.
  555. if ($sourceStorage->instanceOfStorage(Common::class) && $sourceStorage->getMountOption('mount_point')) {
  556. $mountPoint = $sourceStorage->getMountOption('mount_point');
  557. $source = $mountPoint . '/' . $sourceInternalPath;
  558. $target = $this->getFullPath($targetInternalPath);
  559. $this->copyKeys($source, $target);
  560. } else {
  561. $this->logger->error('Could not find mount point, can\'t keep encryption keys');
  562. }
  563. if ($sourceStorage->is_dir($sourceInternalPath)) {
  564. $dh = $sourceStorage->opendir($sourceInternalPath);
  565. if (!$this->is_dir($targetInternalPath)) {
  566. $result = $this->mkdir($targetInternalPath);
  567. } else {
  568. $result = true;
  569. }
  570. if (is_resource($dh)) {
  571. while ($result && ($file = readdir($dh)) !== false) {
  572. if (!Filesystem::isIgnoredDir($file)) {
  573. $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename);
  574. }
  575. }
  576. }
  577. } else {
  578. try {
  579. $source = $sourceStorage->fopen($sourceInternalPath, 'r');
  580. $target = $this->fopen($targetInternalPath, 'w');
  581. [, $result] = \OC_Helper::streamCopy($source, $target);
  582. } finally {
  583. if (is_resource($source)) {
  584. fclose($source);
  585. }
  586. if (is_resource($target)) {
  587. fclose($target);
  588. }
  589. }
  590. if ($result) {
  591. if ($preserveMtime) {
  592. $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath));
  593. }
  594. $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, false);
  595. } else {
  596. // delete partially written target file
  597. $this->unlink($targetInternalPath);
  598. // delete cache entry that was created by fopen
  599. $this->getCache()->remove($targetInternalPath);
  600. }
  601. }
  602. return (bool)$result;
  603. }
  604. public function getLocalFile(string $path): string|false {
  605. if ($this->encryptionManager->isEnabled()) {
  606. $cachedFile = $this->getCachedFile($path);
  607. if (is_string($cachedFile)) {
  608. return $cachedFile;
  609. }
  610. }
  611. return $this->storage->getLocalFile($path);
  612. }
  613. public function isLocal(): bool {
  614. if ($this->encryptionManager->isEnabled()) {
  615. return false;
  616. }
  617. return $this->storage->isLocal();
  618. }
  619. public function stat(string $path): array|false {
  620. $stat = $this->storage->stat($path);
  621. if (!$stat) {
  622. return false;
  623. }
  624. $fileSize = $this->filesize($path);
  625. $stat['size'] = $fileSize;
  626. $stat[7] = $fileSize;
  627. $stat['hasHeader'] = $this->getHeaderSize($path) > 0;
  628. return $stat;
  629. }
  630. public function hash(string $type, string $path, bool $raw = false): string|false {
  631. $fh = $this->fopen($path, 'rb');
  632. $ctx = hash_init($type);
  633. hash_update_stream($ctx, $fh);
  634. fclose($fh);
  635. return hash_final($ctx, $raw);
  636. }
  637. /**
  638. * return full path, including mount point
  639. *
  640. * @param string $path relative to mount point
  641. * @return string full path including mount point
  642. */
  643. protected function getFullPath(string $path): string {
  644. return Filesystem::normalizePath($this->mountPoint . '/' . $path);
  645. }
  646. /**
  647. * read first block of encrypted file, typically this will contain the
  648. * encryption header
  649. */
  650. protected function readFirstBlock(string $path): string {
  651. $firstBlock = '';
  652. if ($this->storage->is_file($path)) {
  653. $handle = $this->storage->fopen($path, 'r');
  654. $firstBlock = fread($handle, $this->util->getHeaderSize());
  655. fclose($handle);
  656. }
  657. return $firstBlock;
  658. }
  659. /**
  660. * return header size of given file
  661. */
  662. protected function getHeaderSize(string $path): int {
  663. $headerSize = 0;
  664. $realFile = $this->util->stripPartialFileExtension($path);
  665. if ($this->storage->is_file($realFile)) {
  666. $path = $realFile;
  667. }
  668. $firstBlock = $this->readFirstBlock($path);
  669. if (str_starts_with($firstBlock, Util::HEADER_START)) {
  670. $headerSize = $this->util->getHeaderSize();
  671. }
  672. return $headerSize;
  673. }
  674. /**
  675. * read header from file
  676. */
  677. protected function getHeader(string $path): array {
  678. $realFile = $this->util->stripPartialFileExtension($path);
  679. $exists = $this->storage->is_file($realFile);
  680. if ($exists) {
  681. $path = $realFile;
  682. }
  683. $result = [];
  684. $isEncrypted = $this->encryptedPaths->get($realFile);
  685. if (is_null($isEncrypted)) {
  686. $info = $this->getCache()->get($path);
  687. $isEncrypted = isset($info['encrypted']) && $info['encrypted'] === true;
  688. }
  689. if ($isEncrypted) {
  690. $firstBlock = $this->readFirstBlock($path);
  691. $result = $this->util->parseRawHeader($firstBlock);
  692. // if the header doesn't contain a encryption module we check if it is a
  693. // legacy file. If true, we add the default encryption module
  694. if (!isset($result[Util::HEADER_ENCRYPTION_MODULE_KEY]) && (!empty($result) || $exists)) {
  695. $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE';
  696. }
  697. }
  698. return $result;
  699. }
  700. /**
  701. * read encryption module needed to read/write the file located at $path
  702. *
  703. * @throws ModuleDoesNotExistsException
  704. * @throws \Exception
  705. */
  706. protected function getEncryptionModule(string $path): ?\OCP\Encryption\IEncryptionModule {
  707. $encryptionModule = null;
  708. $header = $this->getHeader($path);
  709. $encryptionModuleId = $this->util->getEncryptionModuleId($header);
  710. if (!empty($encryptionModuleId)) {
  711. try {
  712. $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId);
  713. } catch (ModuleDoesNotExistsException $e) {
  714. $this->logger->critical('Encryption module defined in "' . $path . '" not loaded!');
  715. throw $e;
  716. }
  717. }
  718. return $encryptionModule;
  719. }
  720. public function updateUnencryptedSize(string $path, int|float $unencryptedSize): void {
  721. $this->unencryptedSize[$path] = $unencryptedSize;
  722. }
  723. /**
  724. * copy keys to new location
  725. *
  726. * @param string $source path relative to data/
  727. * @param string $target path relative to data/
  728. */
  729. protected function copyKeys(string $source, string $target): bool {
  730. if (!$this->util->isExcluded($source)) {
  731. return $this->keyStorage->copyKeys($source, $target);
  732. }
  733. return false;
  734. }
  735. /**
  736. * check if path points to a files version
  737. */
  738. protected function isVersion(string $path): bool {
  739. $normalized = Filesystem::normalizePath($path);
  740. return substr($normalized, 0, strlen('/files_versions/')) === '/files_versions/';
  741. }
  742. /**
  743. * check if the given storage should be encrypted or not
  744. */
  745. protected function shouldEncrypt(string $path): bool {
  746. $fullPath = $this->getFullPath($path);
  747. $mountPointConfig = $this->mount->getOption('encrypt', true);
  748. if ($mountPointConfig === false) {
  749. return false;
  750. }
  751. try {
  752. $encryptionModule = $this->getEncryptionModule($fullPath);
  753. } catch (ModuleDoesNotExistsException $e) {
  754. return false;
  755. }
  756. if ($encryptionModule === null) {
  757. $encryptionModule = $this->encryptionManager->getEncryptionModule();
  758. }
  759. return $encryptionModule->shouldEncrypt($fullPath);
  760. }
  761. public function writeStream(string $path, $stream, ?int $size = null): int {
  762. // always fall back to fopen
  763. $target = $this->fopen($path, 'w');
  764. [$count, $result] = \OC_Helper::streamCopy($stream, $target);
  765. fclose($stream);
  766. fclose($target);
  767. // object store, stores the size after write and doesn't update this during scan
  768. // manually store the unencrypted size
  769. if ($result && $this->getWrapperStorage()->instanceOfStorage(ObjectStoreStorage::class) && $this->shouldEncrypt($path)) {
  770. $this->getCache()->put($path, ['unencrypted_size' => $count]);
  771. }
  772. return $count;
  773. }
  774. public function clearIsEncryptedCache(): void {
  775. $this->encryptedPaths->clear();
  776. }
  777. /**
  778. * Allow temporarily disabling the wrapper
  779. */
  780. public function setEnabled(bool $enabled): void {
  781. $this->enabled = $enabled;
  782. }
  783. }