certificatemanager.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Security;
  27. use OC\Files\Filesystem;
  28. use OCP\ICertificateManager;
  29. use OCP\IConfig;
  30. /**
  31. * Manage trusted certificates for users
  32. */
  33. class CertificateManager implements ICertificateManager {
  34. /**
  35. * @var string
  36. */
  37. protected $uid;
  38. /**
  39. * @var \OC\Files\View
  40. */
  41. protected $view;
  42. /**
  43. * @var IConfig
  44. */
  45. protected $config;
  46. /**
  47. * @param string $uid
  48. * @param \OC\Files\View $view relative to data/
  49. * @param IConfig $config
  50. */
  51. public function __construct($uid, \OC\Files\View $view, IConfig $config) {
  52. $this->uid = $uid;
  53. $this->view = $view;
  54. $this->config = $config;
  55. }
  56. /**
  57. * Returns all certificates trusted by the user
  58. *
  59. * @return \OCP\ICertificate[]
  60. */
  61. public function listCertificates() {
  62. if (!$this->config->getSystemValue('installed', false)) {
  63. return array();
  64. }
  65. $path = $this->getPathToCertificates() . 'uploads/';
  66. if (!$this->view->is_dir($path)) {
  67. return array();
  68. }
  69. $result = array();
  70. $handle = $this->view->opendir($path);
  71. if (!is_resource($handle)) {
  72. return array();
  73. }
  74. while (false !== ($file = readdir($handle))) {
  75. if ($file != '.' && $file != '..') {
  76. try {
  77. $result[] = new Certificate($this->view->file_get_contents($path . $file), $file);
  78. } catch (\Exception $e) {
  79. }
  80. }
  81. }
  82. closedir($handle);
  83. return $result;
  84. }
  85. /**
  86. * create the certificate bundle of all trusted certificated
  87. */
  88. public function createCertificateBundle() {
  89. $path = $this->getPathToCertificates();
  90. $certs = $this->listCertificates();
  91. if (!$this->view->file_exists($path)) {
  92. $this->view->mkdir($path);
  93. }
  94. $fhCerts = $this->view->fopen($path . '/rootcerts.crt', 'w');
  95. // Write user certificates
  96. foreach ($certs as $cert) {
  97. $file = $path . '/uploads/' . $cert->getName();
  98. $data = $this->view->file_get_contents($file);
  99. if (strpos($data, 'BEGIN CERTIFICATE')) {
  100. fwrite($fhCerts, $data);
  101. fwrite($fhCerts, "\r\n");
  102. }
  103. }
  104. // Append the default certificates
  105. $defaultCertificates = file_get_contents(\OC::$SERVERROOT . '/resources/config/ca-bundle.crt');
  106. fwrite($fhCerts, $defaultCertificates);
  107. // Append the system certificate bundle
  108. $systemBundle = $this->getCertificateBundle(null);
  109. if ($this->view->file_exists($systemBundle)) {
  110. $systemCertificates = $this->view->file_get_contents($systemBundle);
  111. fwrite($fhCerts, $systemCertificates);
  112. }
  113. fclose($fhCerts);
  114. }
  115. /**
  116. * Save the certificate and re-generate the certificate bundle
  117. *
  118. * @param string $certificate the certificate data
  119. * @param string $name the filename for the certificate
  120. * @return \OCP\ICertificate
  121. * @throws \Exception If the certificate could not get added
  122. */
  123. public function addCertificate($certificate, $name) {
  124. if (!Filesystem::isValidPath($name) or Filesystem::isFileBlacklisted($name)) {
  125. throw new \Exception('Filename is not valid');
  126. }
  127. $dir = $this->getPathToCertificates() . 'uploads/';
  128. if (!$this->view->file_exists($dir)) {
  129. $this->view->mkdir($dir);
  130. }
  131. try {
  132. $file = $dir . $name;
  133. $certificateObject = new Certificate($certificate, $name);
  134. $this->view->file_put_contents($file, $certificate);
  135. $this->createCertificateBundle();
  136. return $certificateObject;
  137. } catch (\Exception $e) {
  138. throw $e;
  139. }
  140. }
  141. /**
  142. * Remove the certificate and re-generate the certificate bundle
  143. *
  144. * @param string $name
  145. * @return bool
  146. */
  147. public function removeCertificate($name) {
  148. if (!Filesystem::isValidPath($name)) {
  149. return false;
  150. }
  151. $path = $this->getPathToCertificates() . 'uploads/';
  152. if ($this->view->file_exists($path . $name)) {
  153. $this->view->unlink($path . $name);
  154. $this->createCertificateBundle();
  155. }
  156. return true;
  157. }
  158. /**
  159. * Get the path to the certificate bundle for this user
  160. *
  161. * @param string $uid (optional) user to get the certificate bundle for, use `null` to get the system bundle
  162. * @return string
  163. */
  164. public function getCertificateBundle($uid = '') {
  165. if ($uid === '') {
  166. $uid = $this->uid;
  167. }
  168. return $this->getPathToCertificates($uid) . 'rootcerts.crt';
  169. }
  170. /**
  171. * Get the full local path to the certificate bundle for this user
  172. *
  173. * @param string $uid (optional) user to get the certificate bundle for, use `null` to get the system bundle
  174. * @return string
  175. */
  176. public function getAbsoluteBundlePath($uid = '') {
  177. if ($uid === '') {
  178. $uid = $this->uid;
  179. }
  180. if ($this->needsRebundling($uid)) {
  181. if (is_null($uid)) {
  182. $manager = new CertificateManager(null, $this->view, $this->config);
  183. $manager->createCertificateBundle();
  184. } else {
  185. $this->createCertificateBundle();
  186. }
  187. }
  188. return $this->view->getLocalFile($this->getCertificateBundle($uid));
  189. }
  190. /**
  191. * @param string $uid (optional) user to get the certificate path for, use `null` to get the system path
  192. * @return string
  193. */
  194. private function getPathToCertificates($uid = '') {
  195. if ($uid === '') {
  196. $uid = $this->uid;
  197. }
  198. $path = is_null($uid) ? '/files_external/' : '/' . $uid . '/files_external/';
  199. return $path;
  200. }
  201. /**
  202. * Check if we need to re-bundle the certificates because one of the sources has updated
  203. *
  204. * @param string $uid (optional) user to get the certificate path for, use `null` to get the system path
  205. * @return bool
  206. */
  207. private function needsRebundling($uid = '') {
  208. if ($uid === '') {
  209. $uid = $this->uid;
  210. }
  211. $sourceMTimes = [filemtime(\OC::$SERVERROOT . '/resources/config/ca-bundle.crt')];
  212. $targetBundle = $this->getCertificateBundle($uid);
  213. if (!$this->view->file_exists($targetBundle)) {
  214. return true;
  215. }
  216. if (!is_null($uid)) { // also depend on the system bundle
  217. $sourceBundles[] = $this->view->filemtime($this->getCertificateBundle(null));
  218. }
  219. $sourceMTime = array_reduce($sourceMTimes, function ($max, $mtime) {
  220. return max($max, $mtime);
  221. }, 0);
  222. return $sourceMTime > $this->view->filemtime($targetBundle);
  223. }
  224. }