Encryption.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Clark Tomlinson <fallen013@gmail.com>
  9. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Valdnet <47037905+Valdnet@users.noreply.github.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OCA\Encryption\Crypto;
  35. use OC\Encryption\Exceptions\DecryptionFailedException;
  36. use OC\Files\Cache\Scanner;
  37. use OC\Files\View;
  38. use OCA\Encryption\Exceptions\PublicKeyMissingException;
  39. use OCA\Encryption\KeyManager;
  40. use OCA\Encryption\Session;
  41. use OCA\Encryption\Util;
  42. use OCP\Encryption\IEncryptionModule;
  43. use OCP\IL10N;
  44. use OCP\ILogger;
  45. use Symfony\Component\Console\Input\InputInterface;
  46. use Symfony\Component\Console\Output\OutputInterface;
  47. class Encryption implements IEncryptionModule {
  48. public const ID = 'OC_DEFAULT_MODULE';
  49. public const DISPLAY_NAME = 'Default encryption module';
  50. /**
  51. * @var Crypt
  52. */
  53. private $crypt;
  54. /** @var string */
  55. private $cipher;
  56. /** @var string */
  57. private $path;
  58. /** @var string */
  59. private $user;
  60. /** @var array */
  61. private $owner;
  62. /** @var string */
  63. private $fileKey;
  64. /** @var string */
  65. private $writeCache;
  66. /** @var KeyManager */
  67. private $keyManager;
  68. /** @var array */
  69. private $accessList;
  70. /** @var boolean */
  71. private $isWriteOperation;
  72. /** @var Util */
  73. private $util;
  74. /** @var Session */
  75. private $session;
  76. /** @var ILogger */
  77. private $logger;
  78. /** @var IL10N */
  79. private $l;
  80. /** @var EncryptAll */
  81. private $encryptAll;
  82. /** @var bool */
  83. private $useMasterPassword;
  84. /** @var DecryptAll */
  85. private $decryptAll;
  86. private bool $useLegacyBase64Encoding = false;
  87. /** @var int Current version of the file */
  88. private $version = 0;
  89. private bool $useLegacyFileKey = true;
  90. /** @var array remember encryption signature version */
  91. private static $rememberVersion = [];
  92. /**
  93. *
  94. * @param Crypt $crypt
  95. * @param KeyManager $keyManager
  96. * @param Util $util
  97. * @param Session $session
  98. * @param EncryptAll $encryptAll
  99. * @param DecryptAll $decryptAll
  100. * @param ILogger $logger
  101. * @param IL10N $il10n
  102. */
  103. public function __construct(Crypt $crypt,
  104. KeyManager $keyManager,
  105. Util $util,
  106. Session $session,
  107. EncryptAll $encryptAll,
  108. DecryptAll $decryptAll,
  109. ILogger $logger,
  110. IL10N $il10n) {
  111. $this->crypt = $crypt;
  112. $this->keyManager = $keyManager;
  113. $this->util = $util;
  114. $this->session = $session;
  115. $this->encryptAll = $encryptAll;
  116. $this->decryptAll = $decryptAll;
  117. $this->logger = $logger;
  118. $this->l = $il10n;
  119. $this->owner = [];
  120. $this->useMasterPassword = $util->isMasterKeyEnabled();
  121. }
  122. /**
  123. * @return string defining the technical unique id
  124. */
  125. public function getId() {
  126. return self::ID;
  127. }
  128. /**
  129. * In comparison to getKey() this function returns a human readable (maybe translated) name
  130. *
  131. * @return string
  132. */
  133. public function getDisplayName() {
  134. return self::DISPLAY_NAME;
  135. }
  136. /**
  137. * start receiving chunks from a file. This is the place where you can
  138. * perform some initial step before starting encrypting/decrypting the
  139. * chunks
  140. *
  141. * @param string $path to the file
  142. * @param string $user who read/write the file
  143. * @param string $mode php stream open mode
  144. * @param array $header contains the header data read from the file
  145. * @param array $accessList who has access to the file contains the key 'users' and 'public'
  146. *
  147. * @return array $header contain data as key-value pairs which should be
  148. * written to the header, in case of a write operation
  149. * or if no additional data is needed return a empty array
  150. */
  151. public function begin($path, $user, $mode, array $header, array $accessList) {
  152. $this->path = $this->getPathToRealFile($path);
  153. $this->accessList = $accessList;
  154. $this->user = $user;
  155. $this->isWriteOperation = false;
  156. $this->writeCache = '';
  157. $this->useLegacyBase64Encoding = true;
  158. $this->useLegacyFileKey = ($header['useLegacyFileKey'] ?? 'true') !== 'false';
  159. if (isset($header['encoding'])) {
  160. $this->useLegacyBase64Encoding = $header['encoding'] !== Crypt::BINARY_ENCODING_FORMAT;
  161. }
  162. if ($this->session->isReady() === false) {
  163. // if the master key is enabled we can initialize encryption
  164. // with a empty password and user name
  165. if ($this->util->isMasterKeyEnabled()) {
  166. $this->keyManager->init('', '');
  167. }
  168. }
  169. if ($this->session->decryptAllModeActivated()) {
  170. $shareKey = $this->keyManager->getShareKey($this->path, $this->session->getDecryptAllUid());
  171. if ($this->useLegacyFileKey) {
  172. $encryptedFileKey = $this->keyManager->getEncryptedFileKey($this->path);
  173. $this->fileKey = $this->crypt->multiKeyDecryptLegacy($encryptedFileKey,
  174. $shareKey,
  175. $this->session->getDecryptAllKey());
  176. } else {
  177. $this->fileKey = $this->crypt->multiKeyDecrypt($shareKey, $this->session->getDecryptAllKey());
  178. }
  179. } else {
  180. $this->fileKey = $this->keyManager->getFileKey($this->path, $this->user, $this->useLegacyFileKey);
  181. }
  182. // always use the version from the original file, also part files
  183. // need to have a correct version number if they get moved over to the
  184. // final location
  185. $this->version = (int)$this->keyManager->getVersion($this->stripPartFileExtension($path), new View());
  186. if (
  187. $mode === 'w'
  188. || $mode === 'w+'
  189. || $mode === 'wb'
  190. || $mode === 'wb+'
  191. ) {
  192. $this->isWriteOperation = true;
  193. if (empty($this->fileKey)) {
  194. $this->fileKey = $this->crypt->generateFileKey();
  195. }
  196. } else {
  197. // if we read a part file we need to increase the version by 1
  198. // because the version number was also increased by writing
  199. // the part file
  200. if (Scanner::isPartialFile($path)) {
  201. $this->version = $this->version + 1;
  202. }
  203. }
  204. if ($this->isWriteOperation) {
  205. $this->cipher = $this->crypt->getCipher();
  206. $this->useLegacyBase64Encoding = $this->crypt->useLegacyBase64Encoding();
  207. } elseif (isset($header['cipher'])) {
  208. $this->cipher = $header['cipher'];
  209. } else {
  210. // if we read a file without a header we fall-back to the legacy cipher
  211. // which was used in <=oC6
  212. $this->cipher = $this->crypt->getLegacyCipher();
  213. }
  214. $result = [
  215. 'cipher' => $this->cipher,
  216. 'signed' => 'true',
  217. 'useLegacyFileKey' => 'false',
  218. ];
  219. if ($this->useLegacyBase64Encoding !== true) {
  220. $result['encoding'] = Crypt::BINARY_ENCODING_FORMAT;
  221. }
  222. return $result;
  223. }
  224. /**
  225. * last chunk received. This is the place where you can perform some final
  226. * operation and return some remaining data if something is left in your
  227. * buffer.
  228. *
  229. * @param string $path to the file
  230. * @param string $position
  231. * @return string remained data which should be written to the file in case
  232. * of a write operation
  233. * @throws PublicKeyMissingException
  234. * @throws \Exception
  235. * @throws \OCA\Encryption\Exceptions\MultiKeyEncryptException
  236. */
  237. public function end($path, $position = '0') {
  238. $result = '';
  239. if ($this->isWriteOperation) {
  240. // in case of a part file we remember the new signature versions
  241. // the version will be set later on update.
  242. // This way we make sure that other apps listening to the pre-hooks
  243. // still get the old version which should be the correct value for them
  244. if (Scanner::isPartialFile($path)) {
  245. self::$rememberVersion[$this->stripPartFileExtension($path)] = $this->version + 1;
  246. }
  247. if (!empty($this->writeCache)) {
  248. $result = $this->crypt->symmetricEncryptFileContent($this->writeCache, $this->fileKey, $this->version + 1, $position);
  249. $this->writeCache = '';
  250. }
  251. $publicKeys = [];
  252. if ($this->useMasterPassword === true) {
  253. $publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey();
  254. } else {
  255. foreach ($this->accessList['users'] as $uid) {
  256. try {
  257. $publicKeys[$uid] = $this->keyManager->getPublicKey($uid);
  258. } catch (PublicKeyMissingException $e) {
  259. $this->logger->warning(
  260. 'no public key found for user "{uid}", user will not be able to read the file',
  261. ['app' => 'encryption', 'uid' => $uid]
  262. );
  263. // if the public key of the owner is missing we should fail
  264. if ($uid === $this->user) {
  265. throw $e;
  266. }
  267. }
  268. }
  269. }
  270. $publicKeys = $this->keyManager->addSystemKeys($this->accessList, $publicKeys, $this->getOwner($path));
  271. $shareKeys = $this->crypt->multiKeyEncrypt($this->fileKey, $publicKeys);
  272. if (!$this->keyManager->deleteLegacyFileKey($this->path)) {
  273. $this->logger->warning(
  274. 'Failed to delete legacy filekey for {path}',
  275. ['app' => 'encryption', 'path' => $path]
  276. );
  277. }
  278. foreach ($shareKeys as $uid => $keyFile) {
  279. $this->keyManager->setShareKey($this->path, $uid, $keyFile);
  280. }
  281. }
  282. return $result ?: '';
  283. }
  284. /**
  285. * encrypt data
  286. *
  287. * @param string $data you want to encrypt
  288. * @param int $position
  289. * @return string encrypted data
  290. */
  291. public function encrypt($data, $position = 0) {
  292. // If extra data is left over from the last round, make sure it
  293. // is integrated into the next block
  294. if ($this->writeCache) {
  295. // Concat writeCache to start of $data
  296. $data = $this->writeCache . $data;
  297. // Clear the write cache, ready for reuse - it has been
  298. // flushed and its old contents processed
  299. $this->writeCache = '';
  300. }
  301. $encrypted = '';
  302. // While there still remains some data to be processed & written
  303. while (strlen($data) > 0) {
  304. // Remaining length for this iteration, not of the
  305. // entire file (may be greater than 8192 bytes)
  306. $remainingLength = strlen($data);
  307. // If data remaining to be written is less than the
  308. // size of 1 unencrypted block
  309. if ($remainingLength < $this->getUnencryptedBlockSize(true)) {
  310. // Set writeCache to contents of $data
  311. // The writeCache will be carried over to the
  312. // next write round, and added to the start of
  313. // $data to ensure that written blocks are
  314. // always the correct length. If there is still
  315. // data in writeCache after the writing round
  316. // has finished, then the data will be written
  317. // to disk by $this->flush().
  318. $this->writeCache = $data;
  319. // Clear $data ready for next round
  320. $data = '';
  321. } else {
  322. // Read the chunk from the start of $data
  323. $chunk = substr($data, 0, $this->getUnencryptedBlockSize(true));
  324. $encrypted .= $this->crypt->symmetricEncryptFileContent($chunk, $this->fileKey, $this->version + 1, (string)$position);
  325. // Remove the chunk we just processed from
  326. // $data, leaving only unprocessed data in $data
  327. // var, for handling on the next round
  328. $data = substr($data, $this->getUnencryptedBlockSize(true));
  329. }
  330. }
  331. return $encrypted;
  332. }
  333. /**
  334. * decrypt data
  335. *
  336. * @param string $data you want to decrypt
  337. * @param int|string $position
  338. * @return string decrypted data
  339. * @throws DecryptionFailedException
  340. */
  341. public function decrypt($data, $position = 0) {
  342. if (empty($this->fileKey)) {
  343. $msg = 'Cannot decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.';
  344. $hint = $this->l->t('Cannot decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.');
  345. $this->logger->error($msg);
  346. throw new DecryptionFailedException($msg, $hint);
  347. }
  348. return $this->crypt->symmetricDecryptFileContent($data, $this->fileKey, $this->cipher, $this->version, $position, !$this->useLegacyBase64Encoding);
  349. }
  350. /**
  351. * update encrypted file, e.g. give additional users access to the file
  352. *
  353. * @param string $path path to the file which should be updated
  354. * @param string $uid of the user who performs the operation
  355. * @param array $accessList who has access to the file contains the key 'users' and 'public'
  356. * @return bool
  357. */
  358. public function update($path, $uid, array $accessList) {
  359. if (empty($accessList)) {
  360. if (isset(self::$rememberVersion[$path])) {
  361. $this->keyManager->setVersion($path, self::$rememberVersion[$path], new View());
  362. unset(self::$rememberVersion[$path]);
  363. }
  364. return false;
  365. }
  366. $fileKey = $this->keyManager->getFileKey($path, $uid, null);
  367. if (!empty($fileKey)) {
  368. $publicKeys = [];
  369. if ($this->useMasterPassword === true) {
  370. $publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey();
  371. } else {
  372. foreach ($accessList['users'] as $user) {
  373. try {
  374. $publicKeys[$user] = $this->keyManager->getPublicKey($user);
  375. } catch (PublicKeyMissingException $e) {
  376. $this->logger->warning('Could not encrypt file for ' . $user . ': ' . $e->getMessage());
  377. }
  378. }
  379. }
  380. $publicKeys = $this->keyManager->addSystemKeys($accessList, $publicKeys, $this->getOwner($path));
  381. $shareKeys = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys);
  382. $this->keyManager->deleteAllFileKeys($path);
  383. foreach ($shareKeys as $uid => $keyFile) {
  384. $this->keyManager->setShareKey($path, $uid, $keyFile);
  385. }
  386. } else {
  387. $this->logger->debug('no file key found, we assume that the file "{file}" is not encrypted',
  388. ['file' => $path, 'app' => 'encryption']);
  389. return false;
  390. }
  391. return true;
  392. }
  393. /**
  394. * should the file be encrypted or not
  395. *
  396. * @param string $path
  397. * @return boolean
  398. */
  399. public function shouldEncrypt($path) {
  400. if ($this->util->shouldEncryptHomeStorage() === false) {
  401. $storage = $this->util->getStorage($path);
  402. if ($storage && $storage->instanceOfStorage('\OCP\Files\IHomeStorage')) {
  403. return false;
  404. }
  405. }
  406. $parts = explode('/', $path);
  407. if (count($parts) < 4) {
  408. return false;
  409. }
  410. if ($parts[2] === 'files') {
  411. return true;
  412. }
  413. if ($parts[2] === 'files_versions') {
  414. return true;
  415. }
  416. if ($parts[2] === 'files_trashbin') {
  417. return true;
  418. }
  419. return false;
  420. }
  421. /**
  422. * get size of the unencrypted payload per block.
  423. * Nextcloud read/write files with a block size of 8192 byte
  424. *
  425. * Encrypted blocks have a 22-byte IV and 2 bytes of padding, encrypted and
  426. * signed blocks have also a 71-byte signature and 1 more byte of padding,
  427. * resulting respectively in:
  428. *
  429. * 8192 - 22 - 2 = 8168 bytes in each unsigned unencrypted block
  430. * 8192 - 22 - 2 - 71 - 1 = 8096 bytes in each signed unencrypted block
  431. *
  432. * Legacy base64 encoding then reduces the available size by a 3/4 factor:
  433. *
  434. * 8168 * (3/4) = 6126 bytes in each base64-encoded unsigned unencrypted block
  435. * 8096 * (3/4) = 6072 bytes in each base64-encoded signed unencrypted block
  436. *
  437. * @param bool $signed
  438. * @return int
  439. */
  440. public function getUnencryptedBlockSize($signed = false) {
  441. if ($this->useLegacyBase64Encoding) {
  442. return $signed ? 6072 : 6126;
  443. } else {
  444. return $signed ? 8096 : 8168;
  445. }
  446. }
  447. /**
  448. * check if the encryption module is able to read the file,
  449. * e.g. if all encryption keys exists
  450. *
  451. * @param string $path
  452. * @param string $uid user for whom we want to check if he can read the file
  453. * @return bool
  454. * @throws DecryptionFailedException
  455. */
  456. public function isReadable($path, $uid) {
  457. $fileKey = $this->keyManager->getFileKey($path, $uid, null);
  458. if (empty($fileKey)) {
  459. $owner = $this->util->getOwner($path);
  460. if ($owner !== $uid) {
  461. // if it is a shared file we throw a exception with a useful
  462. // error message because in this case it means that the file was
  463. // shared with the user at a point where the user didn't had a
  464. // valid private/public key
  465. $msg = 'Encryption module "' . $this->getDisplayName() .
  466. '" is not able to read ' . $path;
  467. $hint = $this->l->t('Cannot read this file, probably this is a shared file. Please ask the file owner to reshare the file with you.');
  468. $this->logger->warning($msg);
  469. throw new DecryptionFailedException($msg, $hint);
  470. }
  471. return false;
  472. }
  473. return true;
  474. }
  475. /**
  476. * Initial encryption of all files
  477. *
  478. * @param InputInterface $input
  479. * @param OutputInterface $output write some status information to the terminal during encryption
  480. */
  481. public function encryptAll(InputInterface $input, OutputInterface $output) {
  482. $this->encryptAll->encryptAll($input, $output);
  483. }
  484. /**
  485. * prepare module to perform decrypt all operation
  486. *
  487. * @param InputInterface $input
  488. * @param OutputInterface $output
  489. * @param string $user
  490. * @return bool
  491. */
  492. public function prepareDecryptAll(InputInterface $input, OutputInterface $output, $user = '') {
  493. return $this->decryptAll->prepare($input, $output, $user);
  494. }
  495. /**
  496. * @param string $path
  497. * @return string
  498. */
  499. protected function getPathToRealFile($path) {
  500. $realPath = $path;
  501. $parts = explode('/', $path);
  502. if ($parts[2] === 'files_versions') {
  503. $realPath = '/' . $parts[1] . '/files/' . implode('/', array_slice($parts, 3));
  504. $length = strrpos($realPath, '.');
  505. $realPath = substr($realPath, 0, $length);
  506. }
  507. return $realPath;
  508. }
  509. /**
  510. * remove .part file extension and the ocTransferId from the file to get the
  511. * original file name
  512. *
  513. * @param string $path
  514. * @return string
  515. */
  516. protected function stripPartFileExtension($path) {
  517. if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
  518. $pos = strrpos($path, '.', -6);
  519. $path = substr($path, 0, $pos);
  520. }
  521. return $path;
  522. }
  523. /**
  524. * get owner of a file
  525. *
  526. * @param string $path
  527. * @return string
  528. */
  529. protected function getOwner($path) {
  530. if (!isset($this->owner[$path])) {
  531. $this->owner[$path] = $this->util->getOwner($path);
  532. }
  533. return $this->owner[$path];
  534. }
  535. /**
  536. * Check if the module is ready to be used by that specific user.
  537. * In case a module is not ready - because e.g. key pairs have not been generated
  538. * upon login this method can return false before any operation starts and might
  539. * cause issues during operations.
  540. *
  541. * @param string $user
  542. * @return boolean
  543. * @since 9.1.0
  544. */
  545. public function isReadyForUser($user) {
  546. if ($this->util->isMasterKeyEnabled()) {
  547. return true;
  548. }
  549. return $this->keyManager->userHasKeys($user);
  550. }
  551. /**
  552. * We only need a detailed access list if the master key is not enabled
  553. *
  554. * @return bool
  555. */
  556. public function needDetailedAccessList() {
  557. return !$this->util->isMasterKeyEnabled();
  558. }
  559. }