Manager.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OC\Security\IdentityProof;
  29. use OC\Files\AppData\Factory;
  30. use OCP\Files\IAppData;
  31. use OCP\IConfig;
  32. use OCP\IUser;
  33. use OCP\Security\ICrypto;
  34. use Psr\Log\LoggerInterface;
  35. class Manager {
  36. private IAppData $appData;
  37. public function __construct(
  38. Factory $appDataFactory,
  39. private ICrypto $crypto,
  40. private IConfig $config,
  41. private LoggerInterface $logger,
  42. ) {
  43. $this->appData = $appDataFactory->get('identityproof');
  44. }
  45. /**
  46. * Calls the openssl functions to generate a public and private key.
  47. * In a separate function for unit testing purposes.
  48. *
  49. * @return array [$publicKey, $privateKey]
  50. * @throws \RuntimeException
  51. */
  52. protected function generateKeyPair(): array {
  53. $config = [
  54. 'digest_alg' => 'sha512',
  55. 'private_key_bits' => 2048,
  56. ];
  57. // Generate new key
  58. $res = openssl_pkey_new($config);
  59. if ($res === false) {
  60. $this->logOpensslError();
  61. throw new \RuntimeException('OpenSSL reported a problem');
  62. }
  63. if (openssl_pkey_export($res, $privateKey, null, $config) === false) {
  64. $this->logOpensslError();
  65. throw new \RuntimeException('OpenSSL reported a problem');
  66. }
  67. // Extract the public key from $res to $pubKey
  68. $publicKey = openssl_pkey_get_details($res);
  69. $publicKey = $publicKey['key'];
  70. return [$publicKey, $privateKey];
  71. }
  72. /**
  73. * Generate a key for a given ID
  74. * Note: If a key already exists it will be overwritten
  75. *
  76. * @param string $id key id
  77. * @throws \RuntimeException
  78. */
  79. protected function generateKey(string $id): Key {
  80. [$publicKey, $privateKey] = $this->generateKeyPair();
  81. // Write the private and public key to the disk
  82. try {
  83. $this->appData->newFolder($id);
  84. } catch (\Exception $e) {
  85. }
  86. $folder = $this->appData->getFolder($id);
  87. $folder->newFile('private')
  88. ->putContent($this->crypto->encrypt($privateKey));
  89. $folder->newFile('public')
  90. ->putContent($publicKey);
  91. return new Key($publicKey, $privateKey);
  92. }
  93. /**
  94. * Get key for a specific id
  95. *
  96. * @throws \RuntimeException
  97. */
  98. protected function retrieveKey(string $id): Key {
  99. try {
  100. $folder = $this->appData->getFolder($id);
  101. $privateKey = $this->crypto->decrypt(
  102. $folder->getFile('private')->getContent()
  103. );
  104. $publicKey = $folder->getFile('public')->getContent();
  105. return new Key($publicKey, $privateKey);
  106. } catch (\Exception $e) {
  107. return $this->generateKey($id);
  108. }
  109. }
  110. /**
  111. * Get public and private key for $user
  112. *
  113. * @throws \RuntimeException
  114. */
  115. public function getKey(IUser $user): Key {
  116. $uid = $user->getUID();
  117. return $this->retrieveKey('user-' . $uid);
  118. }
  119. /**
  120. * Get instance wide public and private key
  121. *
  122. * @throws \RuntimeException
  123. */
  124. public function getSystemKey(): Key {
  125. $instanceId = $this->config->getSystemValue('instanceid', null);
  126. if ($instanceId === null) {
  127. throw new \RuntimeException('no instance id!');
  128. }
  129. return $this->retrieveKey('system-' . $instanceId);
  130. }
  131. private function logOpensslError(): void {
  132. $errors = [];
  133. while ($error = openssl_error_string()) {
  134. $errors[] = $error;
  135. }
  136. $this->logger->critical('Something is wrong with your openssl setup: ' . implode(', ', $errors));
  137. }
  138. }