Encryption.php 31 KB

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