/lib/private/Encryption/Keys/Storage.php

https://gitlab.com/wuhang2003/core · PHP · 326 lines · 149 code · 54 blank · 123 comment · 21 complexity · 825f3f36b2cf92b779177a6dd09df653 MD5 · raw file

  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Thomas Müller <thomas.mueller@tmit.eu>
  6. *
  7. * @copyright Copyright (c) 2016, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Encryption\Keys;
  24. use OC\Encryption\Util;
  25. use OC\Files\Filesystem;
  26. use OC\Files\View;
  27. use OCP\Encryption\Keys\IStorage;
  28. class Storage implements IStorage {
  29. // hidden file which indicate that the folder is a valid key storage
  30. const KEY_STORAGE_MARKER = '.oc_key_storage';
  31. /** @var View */
  32. private $view;
  33. /** @var Util */
  34. private $util;
  35. // base dir where all the file related keys are stored
  36. /** @var string */
  37. private $keys_base_dir;
  38. // root of the key storage default is empty which means that we use the data folder
  39. /** @var string */
  40. private $root_dir;
  41. /** @var string */
  42. private $encryption_base_dir;
  43. /** @var array */
  44. private $keyCache = [];
  45. /**
  46. * @param View $view
  47. * @param Util $util
  48. */
  49. public function __construct(View $view, Util $util) {
  50. $this->view = $view;
  51. $this->util = $util;
  52. $this->encryption_base_dir = '/files_encryption';
  53. $this->keys_base_dir = $this->encryption_base_dir .'/keys';
  54. $this->root_dir = $this->util->getKeyStorageRoot();
  55. }
  56. /**
  57. * @inheritdoc
  58. */
  59. public function getUserKey($uid, $keyId, $encryptionModuleId) {
  60. $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
  61. return $this->getKey($path);
  62. }
  63. /**
  64. * @inheritdoc
  65. */
  66. public function getFileKey($path, $keyId, $encryptionModuleId) {
  67. $realFile = $this->util->stripPartialFileExtension($path);
  68. $keyDir = $this->getFileKeyDir($encryptionModuleId, $realFile);
  69. $key = $this->getKey($keyDir . $keyId);
  70. if ($key === '' && $realFile !== $path) {
  71. // Check if the part file has keys and use them, if no normal keys
  72. // exist. This is required to fix copyBetweenStorage() when we
  73. // rename a .part file over storage borders.
  74. $keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
  75. $key = $this->getKey($keyDir . $keyId);
  76. }
  77. return $key;
  78. }
  79. /**
  80. * @inheritdoc
  81. */
  82. public function getSystemUserKey($keyId, $encryptionModuleId) {
  83. $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
  84. return $this->getKey($path);
  85. }
  86. /**
  87. * @inheritdoc
  88. */
  89. public function setUserKey($uid, $keyId, $key, $encryptionModuleId) {
  90. $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
  91. return $this->setKey($path, $key);
  92. }
  93. /**
  94. * @inheritdoc
  95. */
  96. public function setFileKey($path, $keyId, $key, $encryptionModuleId) {
  97. $keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
  98. return $this->setKey($keyDir . $keyId, $key);
  99. }
  100. /**
  101. * @inheritdoc
  102. */
  103. public function setSystemUserKey($keyId, $key, $encryptionModuleId) {
  104. $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
  105. return $this->setKey($path, $key);
  106. }
  107. /**
  108. * @inheritdoc
  109. */
  110. public function deleteUserKey($uid, $keyId, $encryptionModuleId) {
  111. $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid);
  112. return !$this->view->file_exists($path) || $this->view->unlink($path);
  113. }
  114. /**
  115. * @inheritdoc
  116. */
  117. public function deleteFileKey($path, $keyId, $encryptionModuleId) {
  118. $keyDir = $this->getFileKeyDir($encryptionModuleId, $path);
  119. return !$this->view->file_exists($keyDir . $keyId) || $this->view->unlink($keyDir . $keyId);
  120. }
  121. /**
  122. * @inheritdoc
  123. */
  124. public function deleteAllFileKeys($path) {
  125. $keyDir = $this->getFileKeyDir('', $path);
  126. return !$this->view->file_exists($keyDir) || $this->view->deleteAll($keyDir);
  127. }
  128. /**
  129. * @inheritdoc
  130. */
  131. public function deleteSystemUserKey($keyId, $encryptionModuleId) {
  132. $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, null);
  133. return !$this->view->file_exists($path) || $this->view->unlink($path);
  134. }
  135. /**
  136. * construct path to users key
  137. *
  138. * @param string $encryptionModuleId
  139. * @param string $keyId
  140. * @param string $uid
  141. * @return string
  142. */
  143. protected function constructUserKeyPath($encryptionModuleId, $keyId, $uid) {
  144. if ($uid === null) {
  145. $path = $this->root_dir . '/' . $this->encryption_base_dir . '/' . $encryptionModuleId . '/' . $keyId;
  146. } else {
  147. $path = $this->root_dir . '/' . $uid . $this->encryption_base_dir . '/'
  148. . $encryptionModuleId . '/' . $uid . '.' . $keyId;
  149. }
  150. return \OC\Files\Filesystem::normalizePath($path);
  151. }
  152. /**
  153. * read key from hard disk
  154. *
  155. * @param string $path to key
  156. * @return string
  157. */
  158. private function getKey($path) {
  159. $key = '';
  160. if ($this->view->file_exists($path)) {
  161. if (isset($this->keyCache[$path])) {
  162. $key = $this->keyCache[$path];
  163. } else {
  164. $key = $this->view->file_get_contents($path);
  165. $this->keyCache[$path] = $key;
  166. }
  167. }
  168. return $key;
  169. }
  170. /**
  171. * write key to disk
  172. *
  173. *
  174. * @param string $path path to key directory
  175. * @param string $key key
  176. * @return bool
  177. */
  178. private function setKey($path, $key) {
  179. $this->keySetPreparation(dirname($path));
  180. $result = $this->view->file_put_contents($path, $key);
  181. if (is_int($result) && $result > 0) {
  182. $this->keyCache[$path] = $key;
  183. return true;
  184. }
  185. return false;
  186. }
  187. /**
  188. * get path to key folder for a given file
  189. *
  190. * @param string $encryptionModuleId
  191. * @param string $path path to the file, relative to data/
  192. * @return string
  193. */
  194. private function getFileKeyDir($encryptionModuleId, $path) {
  195. list($owner, $filename) = $this->util->getUidAndFilename($path);
  196. // in case of system wide mount points the keys are stored directly in the data directory
  197. if ($this->util->isSystemWideMountPoint($filename, $owner)) {
  198. $keyPath = $this->root_dir . '/' . $this->keys_base_dir . $filename . '/';
  199. } else {
  200. $keyPath = $this->root_dir . '/' . $owner . $this->keys_base_dir . $filename . '/';
  201. }
  202. return Filesystem::normalizePath($keyPath . $encryptionModuleId . '/', false);
  203. }
  204. /**
  205. * move keys if a file was renamed
  206. *
  207. * @param string $source
  208. * @param string $target
  209. * @return boolean
  210. */
  211. public function renameKeys($source, $target) {
  212. $sourcePath = $this->getPathToKeys($source);
  213. $targetPath = $this->getPathToKeys($target);
  214. if ($this->view->file_exists($sourcePath)) {
  215. $this->keySetPreparation(dirname($targetPath));
  216. $this->view->rename($sourcePath, $targetPath);
  217. return true;
  218. }
  219. return false;
  220. }
  221. /**
  222. * copy keys if a file was renamed
  223. *
  224. * @param string $source
  225. * @param string $target
  226. * @return boolean
  227. */
  228. public function copyKeys($source, $target) {
  229. $sourcePath = $this->getPathToKeys($source);
  230. $targetPath = $this->getPathToKeys($target);
  231. if ($this->view->file_exists($sourcePath)) {
  232. $this->keySetPreparation(dirname($targetPath));
  233. $this->view->copy($sourcePath, $targetPath);
  234. return true;
  235. }
  236. return false;
  237. }
  238. /**
  239. * get system wide path and detect mount points
  240. *
  241. * @param string $path
  242. * @return string
  243. */
  244. protected function getPathToKeys($path) {
  245. list($owner, $relativePath) = $this->util->getUidAndFilename($path);
  246. $systemWideMountPoint = $this->util->isSystemWideMountPoint($relativePath, $owner);
  247. if ($systemWideMountPoint) {
  248. $systemPath = $this->root_dir . '/' . $this->keys_base_dir . $relativePath . '/';
  249. } else {
  250. $systemPath = $this->root_dir . '/' . $owner . $this->keys_base_dir . $relativePath . '/';
  251. }
  252. return Filesystem::normalizePath($systemPath, false);
  253. }
  254. /**
  255. * Make preparations to filesystem for saving a key file
  256. *
  257. * @param string $path relative to the views root
  258. */
  259. protected function keySetPreparation($path) {
  260. // If the file resides within a subdirectory, create it
  261. if (!$this->view->file_exists($path)) {
  262. $sub_dirs = explode('/', ltrim($path, '/'));
  263. $dir = '';
  264. foreach ($sub_dirs as $sub_dir) {
  265. $dir .= '/' . $sub_dir;
  266. if (!$this->view->is_dir($dir)) {
  267. $this->view->mkdir($dir);
  268. }
  269. }
  270. }
  271. }
  272. }