Manager.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. /** @var IAppData */
  37. private $appData;
  38. /** @var ICrypto */
  39. private $crypto;
  40. /** @var IConfig */
  41. private $config;
  42. private LoggerInterface $logger;
  43. public function __construct(Factory $appDataFactory,
  44. ICrypto $crypto,
  45. IConfig $config,
  46. LoggerInterface $logger
  47. ) {
  48. $this->appData = $appDataFactory->get('identityproof');
  49. $this->crypto = $crypto;
  50. $this->config = $config;
  51. $this->logger = $logger;
  52. }
  53. /**
  54. * Calls the openssl functions to generate a public and private key.
  55. * In a separate function for unit testing purposes.
  56. *
  57. * @return array [$publicKey, $privateKey]
  58. * @throws \RuntimeException
  59. */
  60. protected function generateKeyPair(): array {
  61. $config = [
  62. 'digest_alg' => 'sha512',
  63. 'private_key_bits' => 2048,
  64. ];
  65. // Generate new key
  66. $res = openssl_pkey_new($config);
  67. if ($res === false) {
  68. $this->logOpensslError();
  69. throw new \RuntimeException('OpenSSL reported a problem');
  70. }
  71. if (openssl_pkey_export($res, $privateKey, null, $config) === false) {
  72. $this->logOpensslError();
  73. throw new \RuntimeException('OpenSSL reported a problem');
  74. }
  75. // Extract the public key from $res to $pubKey
  76. $publicKey = openssl_pkey_get_details($res);
  77. $publicKey = $publicKey['key'];
  78. return [$publicKey, $privateKey];
  79. }
  80. /**
  81. * Generate a key for a given ID
  82. * Note: If a key already exists it will be overwritten
  83. *
  84. * @param string $id key id
  85. * @return Key
  86. * @throws \RuntimeException
  87. */
  88. protected function generateKey(string $id): Key {
  89. [$publicKey, $privateKey] = $this->generateKeyPair();
  90. // Write the private and public key to the disk
  91. try {
  92. $this->appData->newFolder($id);
  93. } catch (\Exception $e) {
  94. }
  95. $folder = $this->appData->getFolder($id);
  96. $folder->newFile('private')
  97. ->putContent($this->crypto->encrypt($privateKey));
  98. $folder->newFile('public')
  99. ->putContent($publicKey);
  100. return new Key($publicKey, $privateKey);
  101. }
  102. /**
  103. * Get key for a specific id
  104. *
  105. * @param string $id
  106. * @return Key
  107. * @throws \RuntimeException
  108. */
  109. protected function retrieveKey(string $id): Key {
  110. try {
  111. $folder = $this->appData->getFolder($id);
  112. $privateKey = $this->crypto->decrypt(
  113. $folder->getFile('private')->getContent()
  114. );
  115. $publicKey = $folder->getFile('public')->getContent();
  116. return new Key($publicKey, $privateKey);
  117. } catch (\Exception $e) {
  118. return $this->generateKey($id);
  119. }
  120. }
  121. /**
  122. * Get public and private key for $user
  123. *
  124. * @param IUser $user
  125. * @return Key
  126. * @throws \RuntimeException
  127. */
  128. public function getKey(IUser $user): Key {
  129. $uid = $user->getUID();
  130. return $this->retrieveKey('user-' . $uid);
  131. }
  132. /**
  133. * Get instance wide public and private key
  134. *
  135. * @return Key
  136. * @throws \RuntimeException
  137. */
  138. public function getSystemKey(): Key {
  139. $instanceId = $this->config->getSystemValue('instanceid', null);
  140. if ($instanceId === null) {
  141. throw new \RuntimeException('no instance id!');
  142. }
  143. return $this->retrieveKey('system-' . $instanceId);
  144. }
  145. private function logOpensslError(): void {
  146. $errors = [];
  147. while ($error = openssl_error_string()) {
  148. $errors[] = $error;
  149. }
  150. $this->logger->critical('Something is wrong with your openssl setup: ' . implode(', ', $errors));
  151. }
  152. }