You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CertificateController.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @author Björn Schießle <bjoern@schiessle.org>
  4. * @author Lukas Reschke <lukas@statuscode.ch>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Vincent Petry <pvince81@owncloud.com>
  7. *
  8. * @copyright Copyright (c) 2016, ownCloud, Inc.
  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 OC\Settings\Controller;
  25. use OCP\App\IAppManager;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\ICertificateManager;
  30. use OCP\IL10N;
  31. use OCP\IRequest;
  32. /**
  33. * @package OC\Settings\Controller
  34. */
  35. class CertificateController extends Controller {
  36. /** @var ICertificateManager */
  37. private $userCertificateManager;
  38. /** @var ICertificateManager */
  39. private $systemCertificateManager;
  40. /** @var IL10N */
  41. private $l10n;
  42. /** @var IAppManager */
  43. private $appManager;
  44. /**
  45. * @param string $appName
  46. * @param IRequest $request
  47. * @param ICertificateManager $userCertificateManager
  48. * @param ICertificateManager $systemCertificateManager
  49. * @param IL10N $l10n
  50. * @param IAppManager $appManager
  51. */
  52. public function __construct($appName,
  53. IRequest $request,
  54. ICertificateManager $userCertificateManager,
  55. ICertificateManager $systemCertificateManager,
  56. IL10N $l10n,
  57. IAppManager $appManager) {
  58. parent::__construct($appName, $request);
  59. $this->userCertificateManager = $userCertificateManager;
  60. $this->systemCertificateManager = $systemCertificateManager;
  61. $this->l10n = $l10n;
  62. $this->appManager = $appManager;
  63. }
  64. /**
  65. * Add a new personal root certificate to the users' trust store
  66. *
  67. * @NoAdminRequired
  68. * @NoSubadminRequired
  69. * @return array
  70. */
  71. public function addPersonalRootCertificate() {
  72. return $this->addCertificate($this->userCertificateManager);
  73. }
  74. /**
  75. * Add a new root certificate to a trust store
  76. *
  77. * @param ICertificateManager $certificateManager
  78. * @return array
  79. */
  80. private function addCertificate(ICertificateManager $certificateManager) {
  81. $headers = [];
  82. if ($this->request->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE_8])) {
  83. // due to upload iframe workaround, need to set content-type to text/plain
  84. $headers['Content-Type'] = 'text/plain';
  85. }
  86. if ($this->isCertificateImportAllowed() === false) {
  87. return new DataResponse(['message' => 'Individual certificate management disabled'], Http::STATUS_FORBIDDEN, $headers);
  88. }
  89. $file = $this->request->getUploadedFile('rootcert_import');
  90. if (empty($file)) {
  91. return new DataResponse(['message' => 'No file uploaded'], Http::STATUS_UNPROCESSABLE_ENTITY, $headers);
  92. }
  93. try {
  94. $certificate = $certificateManager->addCertificate(file_get_contents($file['tmp_name']), $file['name']);
  95. return new DataResponse(
  96. [
  97. 'name' => $certificate->getName(),
  98. 'commonName' => $certificate->getCommonName(),
  99. 'organization' => $certificate->getOrganization(),
  100. 'validFrom' => $certificate->getIssueDate()->getTimestamp(),
  101. 'validTill' => $certificate->getExpireDate()->getTimestamp(),
  102. 'validFromString' => $this->l10n->l('date', $certificate->getIssueDate()),
  103. 'validTillString' => $this->l10n->l('date', $certificate->getExpireDate()),
  104. 'issuer' => $certificate->getIssuerName(),
  105. 'issuerOrganization' => $certificate->getIssuerOrganization(),
  106. ],
  107. Http::STATUS_OK,
  108. $headers
  109. );
  110. } catch (\Exception $e) {
  111. return new DataResponse('An error occurred.', Http::STATUS_UNPROCESSABLE_ENTITY, $headers);
  112. }
  113. }
  114. /**
  115. * Removes a personal root certificate from the users' trust store
  116. *
  117. * @NoAdminRequired
  118. * @NoSubadminRequired
  119. * @param string $certificateIdentifier
  120. * @return DataResponse
  121. */
  122. public function removePersonalRootCertificate($certificateIdentifier) {
  123. if ($this->isCertificateImportAllowed() === false) {
  124. return new DataResponse('Individual certificate management disabled', Http::STATUS_FORBIDDEN);
  125. }
  126. $this->userCertificateManager->removeCertificate($certificateIdentifier);
  127. return new DataResponse();
  128. }
  129. /**
  130. * check if certificate import is allowed
  131. *
  132. * @return bool
  133. */
  134. protected function isCertificateImportAllowed() {
  135. $externalStorageEnabled = $this->appManager->isEnabledForUser('files_external');
  136. if ($externalStorageEnabled) {
  137. /** @var \OCA\Files_External\Service\BackendService $backendService */
  138. $backendService = \OC_Mount_Config::$app->getContainer()->query('\OCA\Files_External\Service\BackendService');
  139. if ($backendService->isUserMountingAllowed()) {
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. /**
  146. * Add a new personal root certificate to the system's trust store
  147. *
  148. * @return array
  149. */
  150. public function addSystemRootCertificate() {
  151. return $this->addCertificate($this->systemCertificateManager);
  152. }
  153. /**
  154. * Removes a personal root certificate from the users' trust store
  155. *
  156. * @param string $certificateIdentifier
  157. * @return DataResponse
  158. */
  159. public function removeSystemRootCertificate($certificateIdentifier) {
  160. if ($this->isCertificateImportAllowed() === false) {
  161. return new DataResponse('Individual certificate management disabled', Http::STATUS_FORBIDDEN);
  162. }
  163. $this->systemCertificateManager->removeCertificate($certificateIdentifier);
  164. return new DataResponse();
  165. }
  166. }