encryption.php 29 KB

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