1
0

Encryption.php 18 KB

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