You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Encryption.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. /** @var array remember encryption signature version */
  90. private static $rememberVersion = [];
  91. /**
  92. *
  93. * @param Crypt $crypt
  94. * @param KeyManager $keyManager
  95. * @param Util $util
  96. * @param Session $session
  97. * @param EncryptAll $encryptAll
  98. * @param DecryptAll $decryptAll
  99. * @param ILogger $logger
  100. * @param IL10N $il10n
  101. */
  102. public function __construct(Crypt $crypt,
  103. KeyManager $keyManager,
  104. Util $util,
  105. Session $session,
  106. EncryptAll $encryptAll,
  107. DecryptAll $decryptAll,
  108. ILogger $logger,
  109. IL10N $il10n) {
  110. $this->crypt = $crypt;
  111. $this->keyManager = $keyManager;
  112. $this->util = $util;
  113. $this->session = $session;
  114. $this->encryptAll = $encryptAll;
  115. $this->decryptAll = $decryptAll;
  116. $this->logger = $logger;
  117. $this->l = $il10n;
  118. $this->owner = [];
  119. $this->useMasterPassword = $util->isMasterKeyEnabled();
  120. }
  121. /**
  122. * @return string defining the technical unique id
  123. */
  124. public function getId() {
  125. return self::ID;
  126. }
  127. /**
  128. * In comparison to getKey() this function returns a human readable (maybe translated) name
  129. *
  130. * @return string
  131. */
  132. public function getDisplayName() {
  133. return self::DISPLAY_NAME;
  134. }
  135. /**
  136. * start receiving chunks from a file. This is the place where you can
  137. * perform some initial step before starting encrypting/decrypting the
  138. * chunks
  139. *
  140. * @param string $path to the file
  141. * @param string $user who read/write the file
  142. * @param string $mode php stream open mode
  143. * @param array $header contains the header data read from the file
  144. * @param array $accessList who has access to the file contains the key 'users' and 'public'
  145. *
  146. * @return array $header contain data as key-value pairs which should be
  147. * written to the header, in case of a write operation
  148. * or if no additional data is needed return a empty array
  149. */
  150. public function begin($path, $user, $mode, array $header, array $accessList) {
  151. $this->path = $this->getPathToRealFile($path);
  152. $this->accessList = $accessList;
  153. $this->user = $user;
  154. $this->isWriteOperation = false;
  155. $this->writeCache = '';
  156. $this->useLegacyBase64Encoding = true;
  157. if (isset($header['encoding'])) {
  158. $this->useLegacyBase64Encoding = $header['encoding'] !== Crypt::BINARY_ENCODING_FORMAT;
  159. }
  160. if ($this->session->isReady() === false) {
  161. // if the master key is enabled we can initialize encryption
  162. // with a empty password and user name
  163. if ($this->util->isMasterKeyEnabled()) {
  164. $this->keyManager->init('', '');
  165. }
  166. }
  167. if ($this->session->decryptAllModeActivated()) {
  168. $encryptedFileKey = $this->keyManager->getEncryptedFileKey($this->path);
  169. $shareKey = $this->keyManager->getShareKey($this->path, $this->session->getDecryptAllUid());
  170. $this->fileKey = $this->crypt->multiKeyDecrypt($encryptedFileKey,
  171. $shareKey,
  172. $this->session->getDecryptAllKey());
  173. } else {
  174. $this->fileKey = $this->keyManager->getFileKey($this->path, $this->user);
  175. }
  176. // always use the version from the original file, also part files
  177. // need to have a correct version number if they get moved over to the
  178. // final location
  179. $this->version = (int)$this->keyManager->getVersion($this->stripPartFileExtension($path), new View());
  180. if (
  181. $mode === 'w'
  182. || $mode === 'w+'
  183. || $mode === 'wb'
  184. || $mode === 'wb+'
  185. ) {
  186. $this->isWriteOperation = true;
  187. if (empty($this->fileKey)) {
  188. $this->fileKey = $this->crypt->generateFileKey();
  189. }
  190. } else {
  191. // if we read a part file we need to increase the version by 1
  192. // because the version number was also increased by writing
  193. // the part file
  194. if (Scanner::isPartialFile($path)) {
  195. $this->version = $this->version + 1;
  196. }
  197. }
  198. if ($this->isWriteOperation) {
  199. $this->cipher = $this->crypt->getCipher();
  200. $this->useLegacyBase64Encoding = $this->crypt->useLegacyBase64Encoding();
  201. } elseif (isset($header['cipher'])) {
  202. $this->cipher = $header['cipher'];
  203. } else {
  204. // if we read a file without a header we fall-back to the legacy cipher
  205. // which was used in <=oC6
  206. $this->cipher = $this->crypt->getLegacyCipher();
  207. }
  208. $result = ['cipher' => $this->cipher, 'signed' => 'true'];
  209. if ($this->useLegacyBase64Encoding !== true) {
  210. $result['encoding'] = Crypt::BINARY_ENCODING_FORMAT;
  211. }
  212. return $result;
  213. }
  214. /**
  215. * last chunk received. This is the place where you can perform some final
  216. * operation and return some remaining data if something is left in your
  217. * buffer.
  218. *
  219. * @param string $path to the file
  220. * @param int $position
  221. * @return string remained data which should be written to the file in case
  222. * of a write operation
  223. * @throws PublicKeyMissingException
  224. * @throws \Exception
  225. * @throws \OCA\Encryption\Exceptions\MultiKeyEncryptException
  226. */
  227. public function end($path, $position = 0) {
  228. $result = '';
  229. if ($this->isWriteOperation) {
  230. // in case of a part file we remember the new signature versions
  231. // the version will be set later on update.
  232. // This way we make sure that other apps listening to the pre-hooks
  233. // still get the old version which should be the correct value for them
  234. if (Scanner::isPartialFile($path)) {
  235. self::$rememberVersion[$this->stripPartFileExtension($path)] = $this->version + 1;
  236. }
  237. if (!empty($this->writeCache)) {
  238. $result = $this->crypt->symmetricEncryptFileContent($this->writeCache, $this->fileKey, $this->version + 1, $position);
  239. $this->writeCache = '';
  240. }
  241. $publicKeys = [];
  242. if ($this->useMasterPassword === true) {
  243. $publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey();
  244. } else {
  245. foreach ($this->accessList['users'] as $uid) {
  246. try {
  247. $publicKeys[$uid] = $this->keyManager->getPublicKey($uid);
  248. } catch (PublicKeyMissingException $e) {
  249. $this->logger->warning(
  250. 'no public key found for user "{uid}", user will not be able to read the file',
  251. ['app' => 'encryption', 'uid' => $uid]
  252. );
  253. // if the public key of the owner is missing we should fail
  254. if ($uid === $this->user) {
  255. throw $e;
  256. }
  257. }
  258. }
  259. }
  260. $publicKeys = $this->keyManager->addSystemKeys($this->accessList, $publicKeys, $this->getOwner($path));
  261. $encryptedKeyfiles = $this->crypt->multiKeyEncrypt($this->fileKey, $publicKeys);
  262. $this->keyManager->setAllFileKeys($this->path, $encryptedKeyfiles);
  263. }
  264. return $result;
  265. }
  266. /**
  267. * encrypt data
  268. *
  269. * @param string $data you want to encrypt
  270. * @param int $position
  271. * @return string encrypted data
  272. */
  273. public function encrypt($data, $position = 0) {
  274. // If extra data is left over from the last round, make sure it
  275. // is integrated into the next block
  276. if ($this->writeCache) {
  277. // Concat writeCache to start of $data
  278. $data = $this->writeCache . $data;
  279. // Clear the write cache, ready for reuse - it has been
  280. // flushed and its old contents processed
  281. $this->writeCache = '';
  282. }
  283. $encrypted = '';
  284. // While there still remains some data to be processed & written
  285. while (strlen($data) > 0) {
  286. // Remaining length for this iteration, not of the
  287. // entire file (may be greater than 8192 bytes)
  288. $remainingLength = strlen($data);
  289. // If data remaining to be written is less than the
  290. // size of 1 unencrypted block
  291. if ($remainingLength < $this->getUnencryptedBlockSize(true)) {
  292. // Set writeCache to contents of $data
  293. // The writeCache will be carried over to the
  294. // next write round, and added to the start of
  295. // $data to ensure that written blocks are
  296. // always the correct length. If there is still
  297. // data in writeCache after the writing round
  298. // has finished, then the data will be written
  299. // to disk by $this->flush().
  300. $this->writeCache = $data;
  301. // Clear $data ready for next round
  302. $data = '';
  303. } else {
  304. // Read the chunk from the start of $data
  305. $chunk = substr($data, 0, $this->getUnencryptedBlockSize(true));
  306. $encrypted .= $this->crypt->symmetricEncryptFileContent($chunk, $this->fileKey, $this->version + 1, $position);
  307. // Remove the chunk we just processed from
  308. // $data, leaving only unprocessed data in $data
  309. // var, for handling on the next round
  310. $data = substr($data, $this->getUnencryptedBlockSize(true));
  311. }
  312. }
  313. return $encrypted;
  314. }
  315. /**
  316. * decrypt data
  317. *
  318. * @param string $data you want to decrypt
  319. * @param int|string $position
  320. * @return string decrypted data
  321. * @throws DecryptionFailedException
  322. */
  323. public function decrypt($data, $position = 0) {
  324. if (empty($this->fileKey)) {
  325. $msg = 'Cannot decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.';
  326. $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.');
  327. $this->logger->error($msg);
  328. throw new DecryptionFailedException($msg, $hint);
  329. }
  330. return $this->crypt->symmetricDecryptFileContent($data, $this->fileKey, $this->cipher, $this->version, $position, !$this->useLegacyBase64Encoding);
  331. }
  332. /**
  333. * update encrypted file, e.g. give additional users access to the file
  334. *
  335. * @param string $path path to the file which should be updated
  336. * @param string $uid of the user who performs the operation
  337. * @param array $accessList who has access to the file contains the key 'users' and 'public'
  338. * @return boolean
  339. */
  340. public function update($path, $uid, array $accessList) {
  341. if (empty($accessList)) {
  342. if (isset(self::$rememberVersion[$path])) {
  343. $this->keyManager->setVersion($path, self::$rememberVersion[$path], new View());
  344. unset(self::$rememberVersion[$path]);
  345. }
  346. return;
  347. }
  348. $fileKey = $this->keyManager->getFileKey($path, $uid);
  349. if (!empty($fileKey)) {
  350. $publicKeys = [];
  351. if ($this->useMasterPassword === true) {
  352. $publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey();
  353. } else {
  354. foreach ($accessList['users'] as $user) {
  355. try {
  356. $publicKeys[$user] = $this->keyManager->getPublicKey($user);
  357. } catch (PublicKeyMissingException $e) {
  358. $this->logger->warning('Could not encrypt file for ' . $user . ': ' . $e->getMessage());
  359. }
  360. }
  361. }
  362. $publicKeys = $this->keyManager->addSystemKeys($accessList, $publicKeys, $this->getOwner($path));
  363. $encryptedFileKey = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys);
  364. $this->keyManager->deleteAllFileKeys($path);
  365. $this->keyManager->setAllFileKeys($path, $encryptedFileKey);
  366. } else {
  367. $this->logger->debug('no file key found, we assume that the file "{file}" is not encrypted',
  368. ['file' => $path, 'app' => 'encryption']);
  369. return false;
  370. }
  371. return true;
  372. }
  373. /**
  374. * should the file be encrypted or not
  375. *
  376. * @param string $path
  377. * @return boolean
  378. */
  379. public function shouldEncrypt($path) {
  380. if ($this->util->shouldEncryptHomeStorage() === false) {
  381. $storage = $this->util->getStorage($path);
  382. if ($storage && $storage->instanceOfStorage('\OCP\Files\IHomeStorage')) {
  383. return false;
  384. }
  385. }
  386. $parts = explode('/', $path);
  387. if (count($parts) < 4) {
  388. return false;
  389. }
  390. if ($parts[2] === 'files') {
  391. return true;
  392. }
  393. if ($parts[2] === 'files_versions') {
  394. return true;
  395. }
  396. if ($parts[2] === 'files_trashbin') {
  397. return true;
  398. }
  399. return false;
  400. }
  401. /**
  402. * get size of the unencrypted payload per block.
  403. * Nextcloud read/write files with a block size of 8192 byte
  404. *
  405. * Encrypted blocks have a 22-byte IV and 2 bytes of padding, encrypted and
  406. * signed blocks have also a 71-byte signature and 1 more byte of padding,
  407. * resulting respectively in:
  408. *
  409. * 8192 - 22 - 2 = 8168 bytes in each unsigned unencrypted block
  410. * 8192 - 22 - 2 - 71 - 1 = 8096 bytes in each signed unencrypted block
  411. *
  412. * Legacy base64 encoding then reduces the available size by a 3/4 factor:
  413. *
  414. * 8168 * (3/4) = 6126 bytes in each base64-encoded unsigned unencrypted block
  415. * 8096 * (3/4) = 6072 bytes in each base64-encoded signed unencrypted block
  416. *
  417. * @param bool $signed
  418. * @return int
  419. */
  420. public function getUnencryptedBlockSize($signed = false) {
  421. if ($this->useLegacyBase64Encoding) {
  422. return $signed ? 6072 : 6126;
  423. } else {
  424. return $signed ? 8096 : 8168;
  425. }
  426. }
  427. /**
  428. * check if the encryption module is able to read the file,
  429. * e.g. if all encryption keys exists
  430. *
  431. * @param string $path
  432. * @param string $uid user for whom we want to check if he can read the file
  433. * @return bool
  434. * @throws DecryptionFailedException
  435. */
  436. public function isReadable($path, $uid) {
  437. $fileKey = $this->keyManager->getFileKey($path, $uid);
  438. if (empty($fileKey)) {
  439. $owner = $this->util->getOwner($path);
  440. if ($owner !== $uid) {
  441. // if it is a shared file we throw a exception with a useful
  442. // error message because in this case it means that the file was
  443. // shared with the user at a point where the user didn't had a
  444. // valid private/public key
  445. $msg = 'Encryption module "' . $this->getDisplayName() .
  446. '" is not able to read ' . $path;
  447. $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.');
  448. $this->logger->warning($msg);
  449. throw new DecryptionFailedException($msg, $hint);
  450. }
  451. return false;
  452. }
  453. return true;
  454. }
  455. /**
  456. * Initial encryption of all files
  457. *
  458. * @param InputInterface $input
  459. * @param OutputInterface $output write some status information to the terminal during encryption
  460. */
  461. public function encryptAll(InputInterface $input, OutputInterface $output) {
  462. $this->encryptAll->encryptAll($input, $output);
  463. }
  464. /**
  465. * prepare module to perform decrypt all operation
  466. *
  467. * @param InputInterface $input
  468. * @param OutputInterface $output
  469. * @param string $user
  470. * @return bool
  471. */
  472. public function prepareDecryptAll(InputInterface $input, OutputInterface $output, $user = '') {
  473. return $this->decryptAll->prepare($input, $output, $user);
  474. }
  475. /**
  476. * @param string $path
  477. * @return string
  478. */
  479. protected function getPathToRealFile($path) {
  480. $realPath = $path;
  481. $parts = explode('/', $path);
  482. if ($parts[2] === 'files_versions') {
  483. $realPath = '/' . $parts[1] . '/files/' . implode('/', array_slice($parts, 3));
  484. $length = strrpos($realPath, '.');
  485. $realPath = substr($realPath, 0, $length);
  486. }
  487. return $realPath;
  488. }
  489. /**
  490. * remove .part file extension and the ocTransferId from the file to get the
  491. * original file name
  492. *
  493. * @param string $path
  494. * @return string
  495. */
  496. protected function stripPartFileExtension($path) {
  497. if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
  498. $pos = strrpos($path, '.', -6);
  499. $path = substr($path, 0, $pos);
  500. }
  501. return $path;
  502. }
  503. /**
  504. * get owner of a file
  505. *
  506. * @param string $path
  507. * @return string
  508. */
  509. protected function getOwner($path) {
  510. if (!isset($this->owner[$path])) {
  511. $this->owner[$path] = $this->util->getOwner($path);
  512. }
  513. return $this->owner[$path];
  514. }
  515. /**
  516. * Check if the module is ready to be used by that specific user.
  517. * In case a module is not ready - because e.g. key pairs have not been generated
  518. * upon login this method can return false before any operation starts and might
  519. * cause issues during operations.
  520. *
  521. * @param string $user
  522. * @return boolean
  523. * @since 9.1.0
  524. */
  525. public function isReadyForUser($user) {
  526. if ($this->util->isMasterKeyEnabled()) {
  527. return true;
  528. }
  529. return $this->keyManager->userHasKeys($user);
  530. }
  531. /**
  532. * We only need a detailed access list if the master key is not enabled
  533. *
  534. * @return bool
  535. */
  536. public function needDetailedAccessList() {
  537. return !$this->util->isMasterKeyEnabled();
  538. }
  539. }