Recovery.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Clark Tomlinson <fallen013@gmail.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Encryption;
  25. use OCA\Encryption\Crypto\Crypt;
  26. use OCP\Encryption\Keys\IStorage;
  27. use OCP\IConfig;
  28. use OCP\IUser;
  29. use OCP\IUserSession;
  30. use OCP\PreConditionNotMetException;
  31. use OCP\Security\ISecureRandom;
  32. use OC\Files\View;
  33. use OCP\Encryption\IFile;
  34. class Recovery {
  35. /**
  36. * @var null|IUser
  37. */
  38. protected $user;
  39. /**
  40. * @var Crypt
  41. */
  42. protected $crypt;
  43. /**
  44. * @var ISecureRandom
  45. */
  46. private $random;
  47. /**
  48. * @var KeyManager
  49. */
  50. private $keyManager;
  51. /**
  52. * @var IConfig
  53. */
  54. private $config;
  55. /**
  56. * @var IStorage
  57. */
  58. private $keyStorage;
  59. /**
  60. * @var View
  61. */
  62. private $view;
  63. /**
  64. * @var IFile
  65. */
  66. private $file;
  67. /**
  68. * @param IUserSession $user
  69. * @param Crypt $crypt
  70. * @param ISecureRandom $random
  71. * @param KeyManager $keyManager
  72. * @param IConfig $config
  73. * @param IStorage $keyStorage
  74. * @param IFile $file
  75. * @param View $view
  76. */
  77. public function __construct(IUserSession $user,
  78. Crypt $crypt,
  79. ISecureRandom $random,
  80. KeyManager $keyManager,
  81. IConfig $config,
  82. IStorage $keyStorage,
  83. IFile $file,
  84. View $view) {
  85. $this->user = ($user && $user->isLoggedIn()) ? $user->getUser() : false;
  86. $this->crypt = $crypt;
  87. $this->random = $random;
  88. $this->keyManager = $keyManager;
  89. $this->config = $config;
  90. $this->keyStorage = $keyStorage;
  91. $this->view = $view;
  92. $this->file = $file;
  93. }
  94. /**
  95. * @param string $password
  96. * @return bool
  97. */
  98. public function enableAdminRecovery($password) {
  99. $appConfig = $this->config;
  100. $keyManager = $this->keyManager;
  101. if (!$keyManager->recoveryKeyExists()) {
  102. $keyPair = $this->crypt->createKeyPair();
  103. if(!is_array($keyPair)) {
  104. return false;
  105. }
  106. $this->keyManager->setRecoveryKey($password, $keyPair);
  107. }
  108. if ($keyManager->checkRecoveryPassword($password)) {
  109. $appConfig->setAppValue('encryption', 'recoveryAdminEnabled', 1);
  110. return true;
  111. }
  112. return false;
  113. }
  114. /**
  115. * change recovery key id
  116. *
  117. * @param string $newPassword
  118. * @param string $oldPassword
  119. * @return bool
  120. */
  121. public function changeRecoveryKeyPassword($newPassword, $oldPassword) {
  122. $recoveryKey = $this->keyManager->getSystemPrivateKey($this->keyManager->getRecoveryKeyId());
  123. $decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $oldPassword);
  124. if($decryptedRecoveryKey === false) {
  125. return false;
  126. }
  127. $encryptedRecoveryKey = $this->crypt->encryptPrivateKey($decryptedRecoveryKey, $newPassword);
  128. $header = $this->crypt->generateHeader();
  129. if ($encryptedRecoveryKey) {
  130. $this->keyManager->setSystemPrivateKey($this->keyManager->getRecoveryKeyId(), $header . $encryptedRecoveryKey);
  131. return true;
  132. }
  133. return false;
  134. }
  135. /**
  136. * @param string $recoveryPassword
  137. * @return bool
  138. */
  139. public function disableAdminRecovery($recoveryPassword) {
  140. $keyManager = $this->keyManager;
  141. if ($keyManager->checkRecoveryPassword($recoveryPassword)) {
  142. // Set recoveryAdmin as disabled
  143. $this->config->setAppValue('encryption', 'recoveryAdminEnabled', 0);
  144. return true;
  145. }
  146. return false;
  147. }
  148. /**
  149. * check if recovery is enabled for user
  150. *
  151. * @param string $user if no user is given we check the current logged-in user
  152. *
  153. * @return bool
  154. */
  155. public function isRecoveryEnabledForUser($user = '') {
  156. $uid = empty($user) ? $this->user->getUID() : $user;
  157. $recoveryMode = $this->config->getUserValue($uid,
  158. 'encryption',
  159. 'recoveryEnabled',
  160. 0);
  161. return ($recoveryMode === '1');
  162. }
  163. /**
  164. * check if recovery is key is enabled by the administrator
  165. *
  166. * @return bool
  167. */
  168. public function isRecoveryKeyEnabled() {
  169. $enabled = $this->config->getAppValue('encryption', 'recoveryAdminEnabled', 0);
  170. return ($enabled === '1');
  171. }
  172. /**
  173. * @param string $value
  174. * @return bool
  175. */
  176. public function setRecoveryForUser($value) {
  177. try {
  178. $this->config->setUserValue($this->user->getUID(),
  179. 'encryption',
  180. 'recoveryEnabled',
  181. $value);
  182. if ($value === '1') {
  183. $this->addRecoveryKeys('/' . $this->user->getUID() . '/files/');
  184. } else {
  185. $this->removeRecoveryKeys('/' . $this->user->getUID() . '/files/');
  186. }
  187. return true;
  188. } catch (PreConditionNotMetException $e) {
  189. return false;
  190. }
  191. }
  192. /**
  193. * add recovery key to all encrypted files
  194. * @param string $path
  195. */
  196. private function addRecoveryKeys($path) {
  197. $dirContent = $this->view->getDirectoryContent($path);
  198. foreach ($dirContent as $item) {
  199. $filePath = $item->getPath();
  200. if ($item['type'] === 'dir') {
  201. $this->addRecoveryKeys($filePath . '/');
  202. } else {
  203. $fileKey = $this->keyManager->getFileKey($filePath, $this->user->getUID());
  204. if (!empty($fileKey)) {
  205. $accessList = $this->file->getAccessList($filePath);
  206. $publicKeys = array();
  207. foreach ($accessList['users'] as $uid) {
  208. $publicKeys[$uid] = $this->keyManager->getPublicKey($uid);
  209. }
  210. $publicKeys = $this->keyManager->addSystemKeys($accessList, $publicKeys, $this->user->getUID());
  211. $encryptedKeyfiles = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys);
  212. $this->keyManager->setAllFileKeys($filePath, $encryptedKeyfiles);
  213. }
  214. }
  215. }
  216. }
  217. /**
  218. * remove recovery key to all encrypted files
  219. * @param string $path
  220. */
  221. private function removeRecoveryKeys($path) {
  222. $dirContent = $this->view->getDirectoryContent($path);
  223. foreach ($dirContent as $item) {
  224. $filePath = $item->getPath();
  225. if ($item['type'] === 'dir') {
  226. $this->removeRecoveryKeys($filePath . '/');
  227. } else {
  228. $this->keyManager->deleteShareKey($filePath, $this->keyManager->getRecoveryKeyId());
  229. }
  230. }
  231. }
  232. /**
  233. * recover users files with the recovery key
  234. *
  235. * @param string $recoveryPassword
  236. * @param string $user
  237. */
  238. public function recoverUsersFiles($recoveryPassword, $user) {
  239. $encryptedKey = $this->keyManager->getSystemPrivateKey($this->keyManager->getRecoveryKeyId());
  240. $privateKey = $this->crypt->decryptPrivateKey($encryptedKey, $recoveryPassword);
  241. if($privateKey !== false) {
  242. $this->recoverAllFiles('/' . $user . '/files/', $privateKey, $user);
  243. }
  244. }
  245. /**
  246. * recover users files
  247. *
  248. * @param string $path
  249. * @param string $privateKey
  250. * @param string $uid
  251. */
  252. private function recoverAllFiles($path, $privateKey, $uid) {
  253. $dirContent = $this->view->getDirectoryContent($path);
  254. foreach ($dirContent as $item) {
  255. // Get relative path from encryption/keyfiles
  256. $filePath = $item->getPath();
  257. if ($this->view->is_dir($filePath)) {
  258. $this->recoverAllFiles($filePath . '/', $privateKey, $uid);
  259. } else {
  260. $this->recoverFile($filePath, $privateKey, $uid);
  261. }
  262. }
  263. }
  264. /**
  265. * recover file
  266. *
  267. * @param string $path
  268. * @param string $privateKey
  269. * @param string $uid
  270. */
  271. private function recoverFile($path, $privateKey, $uid) {
  272. $encryptedFileKey = $this->keyManager->getEncryptedFileKey($path);
  273. $shareKey = $this->keyManager->getShareKey($path, $this->keyManager->getRecoveryKeyId());
  274. if ($encryptedFileKey && $shareKey && $privateKey) {
  275. $fileKey = $this->crypt->multiKeyDecrypt($encryptedFileKey,
  276. $shareKey,
  277. $privateKey);
  278. }
  279. if (!empty($fileKey)) {
  280. $accessList = $this->file->getAccessList($path);
  281. $publicKeys = array();
  282. foreach ($accessList['users'] as $user) {
  283. $publicKeys[$user] = $this->keyManager->getPublicKey($user);
  284. }
  285. $publicKeys = $this->keyManager->addSystemKeys($accessList, $publicKeys, $uid);
  286. $encryptedKeyfiles = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys);
  287. $this->keyManager->setAllFileKeys($path, $encryptedKeyfiles);
  288. }
  289. }
  290. }