Manager.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Security\IdentityProof;
  8. use OC\Files\AppData\Factory;
  9. use OCP\Files\IAppData;
  10. use OCP\IConfig;
  11. use OCP\IUser;
  12. use OCP\Security\ICrypto;
  13. use Psr\Log\LoggerInterface;
  14. class Manager {
  15. private IAppData $appData;
  16. public function __construct(
  17. Factory $appDataFactory,
  18. private ICrypto $crypto,
  19. private IConfig $config,
  20. private LoggerInterface $logger,
  21. ) {
  22. $this->appData = $appDataFactory->get('identityproof');
  23. }
  24. /**
  25. * Calls the openssl functions to generate a public and private key.
  26. * In a separate function for unit testing purposes.
  27. *
  28. * @return array [$publicKey, $privateKey]
  29. * @throws \RuntimeException
  30. */
  31. protected function generateKeyPair(): array {
  32. $config = [
  33. 'digest_alg' => 'sha512',
  34. 'private_key_bits' => 2048,
  35. ];
  36. // Generate new key
  37. $res = openssl_pkey_new($config);
  38. if ($res === false) {
  39. $this->logOpensslError();
  40. throw new \RuntimeException('OpenSSL reported a problem');
  41. }
  42. if (openssl_pkey_export($res, $privateKey, null, $config) === false) {
  43. $this->logOpensslError();
  44. throw new \RuntimeException('OpenSSL reported a problem');
  45. }
  46. // Extract the public key from $res to $pubKey
  47. $publicKey = openssl_pkey_get_details($res);
  48. $publicKey = $publicKey['key'];
  49. return [$publicKey, $privateKey];
  50. }
  51. /**
  52. * Generate a key for a given ID
  53. * Note: If a key already exists it will be overwritten
  54. *
  55. * @param string $id key id
  56. * @throws \RuntimeException
  57. */
  58. protected function generateKey(string $id): Key {
  59. [$publicKey, $privateKey] = $this->generateKeyPair();
  60. // Write the private and public key to the disk
  61. try {
  62. $this->appData->newFolder($id);
  63. } catch (\Exception $e) {
  64. }
  65. $folder = $this->appData->getFolder($id);
  66. $folder->newFile('private')
  67. ->putContent($this->crypto->encrypt($privateKey));
  68. $folder->newFile('public')
  69. ->putContent($publicKey);
  70. return new Key($publicKey, $privateKey);
  71. }
  72. /**
  73. * Get key for a specific id
  74. *
  75. * @throws \RuntimeException
  76. */
  77. protected function retrieveKey(string $id): Key {
  78. try {
  79. $folder = $this->appData->getFolder($id);
  80. $privateKey = $this->crypto->decrypt(
  81. $folder->getFile('private')->getContent()
  82. );
  83. $publicKey = $folder->getFile('public')->getContent();
  84. return new Key($publicKey, $privateKey);
  85. } catch (\Exception $e) {
  86. return $this->generateKey($id);
  87. }
  88. }
  89. /**
  90. * Get public and private key for $user
  91. *
  92. * @throws \RuntimeException
  93. */
  94. public function getKey(IUser $user): Key {
  95. $uid = $user->getUID();
  96. return $this->retrieveKey('user-' . $uid);
  97. }
  98. /**
  99. * Get instance wide public and private key
  100. *
  101. * @throws \RuntimeException
  102. */
  103. public function getSystemKey(): Key {
  104. $instanceId = $this->config->getSystemValue('instanceid', null);
  105. if ($instanceId === null) {
  106. throw new \RuntimeException('no instance id!');
  107. }
  108. return $this->retrieveKey('system-' . $instanceId);
  109. }
  110. private function logOpensslError(): void {
  111. $errors = [];
  112. while ($error = openssl_error_string()) {
  113. $errors[] = $error;
  114. }
  115. $this->logger->critical('Something is wrong with your openssl setup: ' . implode(', ', $errors));
  116. }
  117. }