recovery.php 7.8 KB

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