Manager.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  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
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Security\IdentityProof;
  27. use OC\Files\AppData\Factory;
  28. use OCP\Files\IAppData;
  29. use OCP\IConfig;
  30. use OCP\IUser;
  31. use OCP\Security\ICrypto;
  32. class Manager {
  33. /** @var IAppData */
  34. private $appData;
  35. /** @var ICrypto */
  36. private $crypto;
  37. /** @var IConfig */
  38. private $config;
  39. /**
  40. * @param Factory $appDataFactory
  41. * @param ICrypto $crypto
  42. * @param IConfig $config
  43. */
  44. public function __construct(Factory $appDataFactory,
  45. ICrypto $crypto,
  46. IConfig $config
  47. ) {
  48. $this->appData = $appDataFactory->get('identityproof');
  49. $this->crypto = $crypto;
  50. $this->config = $config;
  51. }
  52. /**
  53. * Calls the openssl functions to generate a public and private key.
  54. * In a separate function for unit testing purposes.
  55. *
  56. * @return array [$publicKey, $privateKey]
  57. */
  58. protected function generateKeyPair(): array {
  59. $config = [
  60. 'digest_alg' => 'sha512',
  61. 'private_key_bits' => 2048,
  62. ];
  63. // Generate new key
  64. $res = openssl_pkey_new($config);
  65. openssl_pkey_export($res, $privateKey);
  66. // Extract the public key from $res to $pubKey
  67. $publicKey = openssl_pkey_get_details($res);
  68. $publicKey = $publicKey['key'];
  69. return [$publicKey, $privateKey];
  70. }
  71. /**
  72. * Generate a key for a given ID
  73. * Note: If a key already exists it will be overwritten
  74. *
  75. * @param string $id key id
  76. * @return Key
  77. */
  78. protected function generateKey(string $id): Key {
  79. list($publicKey, $privateKey) = $this->generateKeyPair();
  80. // Write the private and public key to the disk
  81. try {
  82. $this->appData->newFolder($id);
  83. } catch (\Exception $e) {}
  84. $folder = $this->appData->getFolder($id);
  85. $folder->newFile('private')
  86. ->putContent($this->crypto->encrypt($privateKey));
  87. $folder->newFile('public')
  88. ->putContent($publicKey);
  89. return new Key($publicKey, $privateKey);
  90. }
  91. /**
  92. * Get key for a specific id
  93. *
  94. * @param string $id
  95. * @return Key
  96. */
  97. protected function retrieveKey(string $id): Key {
  98. try {
  99. $folder = $this->appData->getFolder($id);
  100. $privateKey = $this->crypto->decrypt(
  101. $folder->getFile('private')->getContent()
  102. );
  103. $publicKey = $folder->getFile('public')->getContent();
  104. return new Key($publicKey, $privateKey);
  105. } catch (\Exception $e) {
  106. return $this->generateKey($id);
  107. }
  108. }
  109. /**
  110. * Get public and private key for $user
  111. *
  112. * @param IUser $user
  113. * @return Key
  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. * @return Key
  123. * @throws \RuntimeException
  124. */
  125. public function getSystemKey(): Key {
  126. $instanceId = $this->config->getSystemValue('instanceid', null);
  127. if ($instanceId === null) {
  128. throw new \RuntimeException('no instance id!');
  129. }
  130. return $this->retrieveKey('system-' . $instanceId);
  131. }
  132. }