@@ -19,184 +19,184 @@ |
||
| 19 | 19 | use Psr\Log\LoggerInterface; |
| 20 | 20 | |
| 21 | 21 | class Manager { |
| 22 | - private IAppData $appData; |
|
| 23 | - |
|
| 24 | - protected ICache $cache; |
|
| 25 | - |
|
| 26 | - public function __construct( |
|
| 27 | - Factory $appDataFactory, |
|
| 28 | - private ICrypto $crypto, |
|
| 29 | - private IConfig $config, |
|
| 30 | - private LoggerInterface $logger, |
|
| 31 | - private ICacheFactory $cacheFactory, |
|
| 32 | - ) { |
|
| 33 | - $this->appData = $appDataFactory->get('identityproof'); |
|
| 34 | - $this->cache = $this->cacheFactory->createDistributed('identityproof::'); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Calls the openssl functions to generate a public and private key. |
|
| 39 | - * In a separate function for unit testing purposes. |
|
| 40 | - * |
|
| 41 | - * @param array $options config options to generate key {@see openssl_csr_new} |
|
| 42 | - * |
|
| 43 | - * @return array [$publicKey, $privateKey] |
|
| 44 | - * @throws \RuntimeException |
|
| 45 | - */ |
|
| 46 | - protected function generateKeyPair(array $options = []): array { |
|
| 47 | - $config = [ |
|
| 48 | - 'digest_alg' => $options['algorithm'] ?? 'sha512', |
|
| 49 | - 'private_key_bits' => $options['bits'] ?? 2048, |
|
| 50 | - 'private_key_type' => $options['type'] ?? OPENSSL_KEYTYPE_RSA, |
|
| 51 | - ]; |
|
| 52 | - |
|
| 53 | - // Generate new key |
|
| 54 | - $res = openssl_pkey_new($config); |
|
| 55 | - if ($res === false) { |
|
| 56 | - $this->logOpensslError(); |
|
| 57 | - throw new \RuntimeException('OpenSSL reported a problem'); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - if (openssl_pkey_export($res, $privateKey, null, $config) === false) { |
|
| 61 | - $this->logOpensslError(); |
|
| 62 | - throw new \RuntimeException('OpenSSL reported a problem'); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - // Extract the public key from $res to $pubKey |
|
| 66 | - $publicKey = openssl_pkey_get_details($res); |
|
| 67 | - $publicKey = $publicKey['key']; |
|
| 68 | - |
|
| 69 | - return [$publicKey, $privateKey]; |
|
| 70 | - } |
|
| 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 | - * @param array $options config options to generate key {@see openssl_csr_new} |
|
| 78 | - * |
|
| 79 | - * @throws \RuntimeException |
|
| 80 | - */ |
|
| 81 | - protected function generateKey(string $id, array $options = []): Key { |
|
| 82 | - [$publicKey, $privateKey] = $this->generateKeyPair($options); |
|
| 83 | - |
|
| 84 | - // Write the private and public key to the disk |
|
| 85 | - try { |
|
| 86 | - $this->appData->newFolder($id); |
|
| 87 | - } catch (\Exception) { |
|
| 88 | - } |
|
| 89 | - $folder = $this->appData->getFolder($id); |
|
| 90 | - $folder->newFile('private') |
|
| 91 | - ->putContent($this->crypto->encrypt($privateKey)); |
|
| 92 | - $folder->newFile('public') |
|
| 93 | - ->putContent($publicKey); |
|
| 94 | - |
|
| 95 | - return new Key($publicKey, $privateKey); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get key for a specific id |
|
| 100 | - * |
|
| 101 | - * @throws \RuntimeException |
|
| 102 | - */ |
|
| 103 | - protected function retrieveKey(string $id): Key { |
|
| 104 | - try { |
|
| 105 | - $cachedPublicKey = $this->cache->get($id . '-public'); |
|
| 106 | - $cachedPrivateKey = $this->cache->get($id . '-private'); |
|
| 107 | - |
|
| 108 | - if ($cachedPublicKey !== null && $cachedPrivateKey !== null) { |
|
| 109 | - $decryptedPrivateKey = $this->crypto->decrypt($cachedPrivateKey); |
|
| 110 | - |
|
| 111 | - return new Key($cachedPublicKey, $decryptedPrivateKey); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $folder = $this->appData->getFolder($id); |
|
| 115 | - $privateKey = $folder->getFile('private')->getContent(); |
|
| 116 | - $publicKey = $folder->getFile('public')->getContent(); |
|
| 117 | - |
|
| 118 | - $this->cache->set($id . '-public', $publicKey); |
|
| 119 | - $this->cache->set($id . '-private', $privateKey); |
|
| 120 | - |
|
| 121 | - $decryptedPrivateKey = $this->crypto->decrypt($privateKey); |
|
| 122 | - return new Key($publicKey, $decryptedPrivateKey); |
|
| 123 | - } catch (\Exception $e) { |
|
| 124 | - return $this->generateKey($id); |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Get public and private key for $user |
|
| 130 | - * |
|
| 131 | - * @throws \RuntimeException |
|
| 132 | - */ |
|
| 133 | - public function getKey(IUser $user): Key { |
|
| 134 | - $uid = $user->getUID(); |
|
| 135 | - return $this->retrieveKey('user-' . $uid); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Set public key for $user |
|
| 140 | - */ |
|
| 141 | - public function setPublicKey(IUser $user, string $publicKey): void { |
|
| 142 | - $id = 'user-' . $user->getUID(); |
|
| 143 | - |
|
| 144 | - $folder = $this->appData->getFolder($id); |
|
| 145 | - $folder->newFile('public', $publicKey); |
|
| 146 | - |
|
| 147 | - $this->cache->set($id . '-public', $publicKey); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Get instance wide public and private key |
|
| 152 | - * |
|
| 153 | - * @throws \RuntimeException |
|
| 154 | - */ |
|
| 155 | - public function getSystemKey(): Key { |
|
| 156 | - $instanceId = $this->config->getSystemValue('instanceid', null); |
|
| 157 | - if ($instanceId === null) { |
|
| 158 | - throw new \RuntimeException('no instance id!'); |
|
| 159 | - } |
|
| 160 | - return $this->retrieveKey('system-' . $instanceId); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - public function hasAppKey(string $app, string $name): bool { |
|
| 164 | - $id = $this->generateAppKeyId($app, $name); |
|
| 165 | - try { |
|
| 166 | - $folder = $this->appData->getFolder($id); |
|
| 167 | - return ($folder->fileExists('public') && $folder->fileExists('private')); |
|
| 168 | - } catch (NotFoundException) { |
|
| 169 | - return false; |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - public function getAppKey(string $app, string $name): Key { |
|
| 174 | - return $this->retrieveKey($this->generateAppKeyId($app, $name)); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - public function generateAppKey(string $app, string $name, array $options = []): Key { |
|
| 178 | - return $this->generateKey($this->generateAppKeyId($app, $name), $options); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - public function deleteAppKey(string $app, string $name): bool { |
|
| 182 | - try { |
|
| 183 | - $folder = $this->appData->getFolder($this->generateAppKeyId($app, $name)); |
|
| 184 | - $folder->delete(); |
|
| 185 | - return true; |
|
| 186 | - } catch (NotFoundException) { |
|
| 187 | - return false; |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - private function generateAppKeyId(string $app, string $name): string { |
|
| 192 | - return 'app-' . $app . '-' . $name; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - private function logOpensslError(): void { |
|
| 196 | - $errors = []; |
|
| 197 | - while ($error = openssl_error_string()) { |
|
| 198 | - $errors[] = $error; |
|
| 199 | - } |
|
| 200 | - $this->logger->critical('Something is wrong with your openssl setup: ' . implode(', ', $errors)); |
|
| 201 | - } |
|
| 22 | + private IAppData $appData; |
|
| 23 | + |
|
| 24 | + protected ICache $cache; |
|
| 25 | + |
|
| 26 | + public function __construct( |
|
| 27 | + Factory $appDataFactory, |
|
| 28 | + private ICrypto $crypto, |
|
| 29 | + private IConfig $config, |
|
| 30 | + private LoggerInterface $logger, |
|
| 31 | + private ICacheFactory $cacheFactory, |
|
| 32 | + ) { |
|
| 33 | + $this->appData = $appDataFactory->get('identityproof'); |
|
| 34 | + $this->cache = $this->cacheFactory->createDistributed('identityproof::'); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Calls the openssl functions to generate a public and private key. |
|
| 39 | + * In a separate function for unit testing purposes. |
|
| 40 | + * |
|
| 41 | + * @param array $options config options to generate key {@see openssl_csr_new} |
|
| 42 | + * |
|
| 43 | + * @return array [$publicKey, $privateKey] |
|
| 44 | + * @throws \RuntimeException |
|
| 45 | + */ |
|
| 46 | + protected function generateKeyPair(array $options = []): array { |
|
| 47 | + $config = [ |
|
| 48 | + 'digest_alg' => $options['algorithm'] ?? 'sha512', |
|
| 49 | + 'private_key_bits' => $options['bits'] ?? 2048, |
|
| 50 | + 'private_key_type' => $options['type'] ?? OPENSSL_KEYTYPE_RSA, |
|
| 51 | + ]; |
|
| 52 | + |
|
| 53 | + // Generate new key |
|
| 54 | + $res = openssl_pkey_new($config); |
|
| 55 | + if ($res === false) { |
|
| 56 | + $this->logOpensslError(); |
|
| 57 | + throw new \RuntimeException('OpenSSL reported a problem'); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + if (openssl_pkey_export($res, $privateKey, null, $config) === false) { |
|
| 61 | + $this->logOpensslError(); |
|
| 62 | + throw new \RuntimeException('OpenSSL reported a problem'); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + // Extract the public key from $res to $pubKey |
|
| 66 | + $publicKey = openssl_pkey_get_details($res); |
|
| 67 | + $publicKey = $publicKey['key']; |
|
| 68 | + |
|
| 69 | + return [$publicKey, $privateKey]; |
|
| 70 | + } |
|
| 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 | + * @param array $options config options to generate key {@see openssl_csr_new} |
|
| 78 | + * |
|
| 79 | + * @throws \RuntimeException |
|
| 80 | + */ |
|
| 81 | + protected function generateKey(string $id, array $options = []): Key { |
|
| 82 | + [$publicKey, $privateKey] = $this->generateKeyPair($options); |
|
| 83 | + |
|
| 84 | + // Write the private and public key to the disk |
|
| 85 | + try { |
|
| 86 | + $this->appData->newFolder($id); |
|
| 87 | + } catch (\Exception) { |
|
| 88 | + } |
|
| 89 | + $folder = $this->appData->getFolder($id); |
|
| 90 | + $folder->newFile('private') |
|
| 91 | + ->putContent($this->crypto->encrypt($privateKey)); |
|
| 92 | + $folder->newFile('public') |
|
| 93 | + ->putContent($publicKey); |
|
| 94 | + |
|
| 95 | + return new Key($publicKey, $privateKey); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get key for a specific id |
|
| 100 | + * |
|
| 101 | + * @throws \RuntimeException |
|
| 102 | + */ |
|
| 103 | + protected function retrieveKey(string $id): Key { |
|
| 104 | + try { |
|
| 105 | + $cachedPublicKey = $this->cache->get($id . '-public'); |
|
| 106 | + $cachedPrivateKey = $this->cache->get($id . '-private'); |
|
| 107 | + |
|
| 108 | + if ($cachedPublicKey !== null && $cachedPrivateKey !== null) { |
|
| 109 | + $decryptedPrivateKey = $this->crypto->decrypt($cachedPrivateKey); |
|
| 110 | + |
|
| 111 | + return new Key($cachedPublicKey, $decryptedPrivateKey); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $folder = $this->appData->getFolder($id); |
|
| 115 | + $privateKey = $folder->getFile('private')->getContent(); |
|
| 116 | + $publicKey = $folder->getFile('public')->getContent(); |
|
| 117 | + |
|
| 118 | + $this->cache->set($id . '-public', $publicKey); |
|
| 119 | + $this->cache->set($id . '-private', $privateKey); |
|
| 120 | + |
|
| 121 | + $decryptedPrivateKey = $this->crypto->decrypt($privateKey); |
|
| 122 | + return new Key($publicKey, $decryptedPrivateKey); |
|
| 123 | + } catch (\Exception $e) { |
|
| 124 | + return $this->generateKey($id); |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Get public and private key for $user |
|
| 130 | + * |
|
| 131 | + * @throws \RuntimeException |
|
| 132 | + */ |
|
| 133 | + public function getKey(IUser $user): Key { |
|
| 134 | + $uid = $user->getUID(); |
|
| 135 | + return $this->retrieveKey('user-' . $uid); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Set public key for $user |
|
| 140 | + */ |
|
| 141 | + public function setPublicKey(IUser $user, string $publicKey): void { |
|
| 142 | + $id = 'user-' . $user->getUID(); |
|
| 143 | + |
|
| 144 | + $folder = $this->appData->getFolder($id); |
|
| 145 | + $folder->newFile('public', $publicKey); |
|
| 146 | + |
|
| 147 | + $this->cache->set($id . '-public', $publicKey); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Get instance wide public and private key |
|
| 152 | + * |
|
| 153 | + * @throws \RuntimeException |
|
| 154 | + */ |
|
| 155 | + public function getSystemKey(): Key { |
|
| 156 | + $instanceId = $this->config->getSystemValue('instanceid', null); |
|
| 157 | + if ($instanceId === null) { |
|
| 158 | + throw new \RuntimeException('no instance id!'); |
|
| 159 | + } |
|
| 160 | + return $this->retrieveKey('system-' . $instanceId); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + public function hasAppKey(string $app, string $name): bool { |
|
| 164 | + $id = $this->generateAppKeyId($app, $name); |
|
| 165 | + try { |
|
| 166 | + $folder = $this->appData->getFolder($id); |
|
| 167 | + return ($folder->fileExists('public') && $folder->fileExists('private')); |
|
| 168 | + } catch (NotFoundException) { |
|
| 169 | + return false; |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + public function getAppKey(string $app, string $name): Key { |
|
| 174 | + return $this->retrieveKey($this->generateAppKeyId($app, $name)); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + public function generateAppKey(string $app, string $name, array $options = []): Key { |
|
| 178 | + return $this->generateKey($this->generateAppKeyId($app, $name), $options); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + public function deleteAppKey(string $app, string $name): bool { |
|
| 182 | + try { |
|
| 183 | + $folder = $this->appData->getFolder($this->generateAppKeyId($app, $name)); |
|
| 184 | + $folder->delete(); |
|
| 185 | + return true; |
|
| 186 | + } catch (NotFoundException) { |
|
| 187 | + return false; |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + private function generateAppKeyId(string $app, string $name): string { |
|
| 192 | + return 'app-' . $app . '-' . $name; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + private function logOpensslError(): void { |
|
| 196 | + $errors = []; |
|
| 197 | + while ($error = openssl_error_string()) { |
|
| 198 | + $errors[] = $error; |
|
| 199 | + } |
|
| 200 | + $this->logger->critical('Something is wrong with your openssl setup: ' . implode(', ', $errors)); |
|
| 201 | + } |
|
| 202 | 202 | } |
@@ -102,8 +102,8 @@ discard block |
||
| 102 | 102 | */ |
| 103 | 103 | protected function retrieveKey(string $id): Key { |
| 104 | 104 | try { |
| 105 | - $cachedPublicKey = $this->cache->get($id . '-public'); |
|
| 106 | - $cachedPrivateKey = $this->cache->get($id . '-private'); |
|
| 105 | + $cachedPublicKey = $this->cache->get($id.'-public'); |
|
| 106 | + $cachedPrivateKey = $this->cache->get($id.'-private'); |
|
| 107 | 107 | |
| 108 | 108 | if ($cachedPublicKey !== null && $cachedPrivateKey !== null) { |
| 109 | 109 | $decryptedPrivateKey = $this->crypto->decrypt($cachedPrivateKey); |
@@ -115,8 +115,8 @@ discard block |
||
| 115 | 115 | $privateKey = $folder->getFile('private')->getContent(); |
| 116 | 116 | $publicKey = $folder->getFile('public')->getContent(); |
| 117 | 117 | |
| 118 | - $this->cache->set($id . '-public', $publicKey); |
|
| 119 | - $this->cache->set($id . '-private', $privateKey); |
|
| 118 | + $this->cache->set($id.'-public', $publicKey); |
|
| 119 | + $this->cache->set($id.'-private', $privateKey); |
|
| 120 | 120 | |
| 121 | 121 | $decryptedPrivateKey = $this->crypto->decrypt($privateKey); |
| 122 | 122 | return new Key($publicKey, $decryptedPrivateKey); |
@@ -132,19 +132,19 @@ discard block |
||
| 132 | 132 | */ |
| 133 | 133 | public function getKey(IUser $user): Key { |
| 134 | 134 | $uid = $user->getUID(); |
| 135 | - return $this->retrieveKey('user-' . $uid); |
|
| 135 | + return $this->retrieveKey('user-'.$uid); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** |
| 139 | 139 | * Set public key for $user |
| 140 | 140 | */ |
| 141 | 141 | public function setPublicKey(IUser $user, string $publicKey): void { |
| 142 | - $id = 'user-' . $user->getUID(); |
|
| 142 | + $id = 'user-'.$user->getUID(); |
|
| 143 | 143 | |
| 144 | 144 | $folder = $this->appData->getFolder($id); |
| 145 | 145 | $folder->newFile('public', $publicKey); |
| 146 | 146 | |
| 147 | - $this->cache->set($id . '-public', $publicKey); |
|
| 147 | + $this->cache->set($id.'-public', $publicKey); |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | /** |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | if ($instanceId === null) { |
| 158 | 158 | throw new \RuntimeException('no instance id!'); |
| 159 | 159 | } |
| 160 | - return $this->retrieveKey('system-' . $instanceId); |
|
| 160 | + return $this->retrieveKey('system-'.$instanceId); |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | public function hasAppKey(string $app, string $name): bool { |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | private function generateAppKeyId(string $app, string $name): string { |
| 192 | - return 'app-' . $app . '-' . $name; |
|
| 192 | + return 'app-'.$app.'-'.$name; |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | private function logOpensslError(): void { |
@@ -197,6 +197,6 @@ discard block |
||
| 197 | 197 | while ($error = openssl_error_string()) { |
| 198 | 198 | $errors[] = $error; |
| 199 | 199 | } |
| 200 | - $this->logger->critical('Something is wrong with your openssl setup: ' . implode(', ', $errors)); |
|
| 200 | + $this->logger->critical('Something is wrong with your openssl setup: '.implode(', ', $errors)); |
|
| 201 | 201 | } |
| 202 | 202 | } |
@@ -19,77 +19,77 @@ |
||
| 19 | 19 | use Symfony\Component\Console\Output\OutputInterface; |
| 20 | 20 | |
| 21 | 21 | class Verify extends Command { |
| 22 | - public function __construct( |
|
| 23 | - protected IUserManager $userManager, |
|
| 24 | - protected Manager $keyManager, |
|
| 25 | - ) { |
|
| 26 | - parent::__construct(); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - protected function configure(): void { |
|
| 30 | - $this |
|
| 31 | - ->setName('user:keys:verify') |
|
| 32 | - ->setDescription('Verify if the stored public key matches the stored private key') |
|
| 33 | - ->addArgument( |
|
| 34 | - 'user-id', |
|
| 35 | - InputArgument::REQUIRED, |
|
| 36 | - 'User ID of the user to verify' |
|
| 37 | - ) |
|
| 38 | - ->addOption( |
|
| 39 | - 'update', |
|
| 40 | - null, |
|
| 41 | - InputOption::VALUE_NONE, |
|
| 42 | - 'Save the derived public key to match the private key' |
|
| 43 | - ) |
|
| 44 | - ; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @param InputInterface $input |
|
| 49 | - * @param OutputInterface $output |
|
| 50 | - * @return int |
|
| 51 | - */ |
|
| 52 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 53 | - $userId = $input->getArgument('user-id'); |
|
| 54 | - |
|
| 55 | - $user = $this->userManager->get($userId); |
|
| 56 | - if (!$user instanceof IUser) { |
|
| 57 | - $output->writeln('Unknown user'); |
|
| 58 | - return static::FAILURE; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $key = $this->keyManager->getKey($user); |
|
| 62 | - $publicKey = $key->getPublic(); |
|
| 63 | - $privateKey = $key->getPrivate(); |
|
| 64 | - |
|
| 65 | - $output->writeln('User public key size: ' . strlen($publicKey)); |
|
| 66 | - $output->writeln('User private key size: ' . strlen($privateKey)); |
|
| 67 | - |
|
| 68 | - // Derive the public key from the private key again to validate the stored public key |
|
| 69 | - $opensslPrivateKey = openssl_pkey_get_private($privateKey); |
|
| 70 | - $publicKeyDerived = openssl_pkey_get_details($opensslPrivateKey); |
|
| 71 | - $publicKeyDerived = $publicKeyDerived['key']; |
|
| 72 | - $output->writeln('User derived public key size: ' . strlen($publicKeyDerived)); |
|
| 73 | - |
|
| 74 | - $output->writeln(''); |
|
| 75 | - |
|
| 76 | - $output->writeln('Stored public key:'); |
|
| 77 | - $output->writeln($publicKey); |
|
| 78 | - $output->writeln('Derived public key:'); |
|
| 79 | - $output->writeln($publicKeyDerived); |
|
| 80 | - |
|
| 81 | - if ($publicKey != $publicKeyDerived) { |
|
| 82 | - if (!$input->getOption('update')) { |
|
| 83 | - $output->writeln('<error>Stored public key does not match stored private key</error>'); |
|
| 84 | - return static::FAILURE; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $this->keyManager->setPublicKey($user, $publicKeyDerived); |
|
| 88 | - $output->writeln('<info>Derived public key did not match, successfully updated</info>'); |
|
| 89 | - return static::SUCCESS; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $output->writeln('<info>Stored public key matches stored private key</info>'); |
|
| 93 | - return static::SUCCESS; |
|
| 94 | - } |
|
| 22 | + public function __construct( |
|
| 23 | + protected IUserManager $userManager, |
|
| 24 | + protected Manager $keyManager, |
|
| 25 | + ) { |
|
| 26 | + parent::__construct(); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + protected function configure(): void { |
|
| 30 | + $this |
|
| 31 | + ->setName('user:keys:verify') |
|
| 32 | + ->setDescription('Verify if the stored public key matches the stored private key') |
|
| 33 | + ->addArgument( |
|
| 34 | + 'user-id', |
|
| 35 | + InputArgument::REQUIRED, |
|
| 36 | + 'User ID of the user to verify' |
|
| 37 | + ) |
|
| 38 | + ->addOption( |
|
| 39 | + 'update', |
|
| 40 | + null, |
|
| 41 | + InputOption::VALUE_NONE, |
|
| 42 | + 'Save the derived public key to match the private key' |
|
| 43 | + ) |
|
| 44 | + ; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @param InputInterface $input |
|
| 49 | + * @param OutputInterface $output |
|
| 50 | + * @return int |
|
| 51 | + */ |
|
| 52 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 53 | + $userId = $input->getArgument('user-id'); |
|
| 54 | + |
|
| 55 | + $user = $this->userManager->get($userId); |
|
| 56 | + if (!$user instanceof IUser) { |
|
| 57 | + $output->writeln('Unknown user'); |
|
| 58 | + return static::FAILURE; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $key = $this->keyManager->getKey($user); |
|
| 62 | + $publicKey = $key->getPublic(); |
|
| 63 | + $privateKey = $key->getPrivate(); |
|
| 64 | + |
|
| 65 | + $output->writeln('User public key size: ' . strlen($publicKey)); |
|
| 66 | + $output->writeln('User private key size: ' . strlen($privateKey)); |
|
| 67 | + |
|
| 68 | + // Derive the public key from the private key again to validate the stored public key |
|
| 69 | + $opensslPrivateKey = openssl_pkey_get_private($privateKey); |
|
| 70 | + $publicKeyDerived = openssl_pkey_get_details($opensslPrivateKey); |
|
| 71 | + $publicKeyDerived = $publicKeyDerived['key']; |
|
| 72 | + $output->writeln('User derived public key size: ' . strlen($publicKeyDerived)); |
|
| 73 | + |
|
| 74 | + $output->writeln(''); |
|
| 75 | + |
|
| 76 | + $output->writeln('Stored public key:'); |
|
| 77 | + $output->writeln($publicKey); |
|
| 78 | + $output->writeln('Derived public key:'); |
|
| 79 | + $output->writeln($publicKeyDerived); |
|
| 80 | + |
|
| 81 | + if ($publicKey != $publicKeyDerived) { |
|
| 82 | + if (!$input->getOption('update')) { |
|
| 83 | + $output->writeln('<error>Stored public key does not match stored private key</error>'); |
|
| 84 | + return static::FAILURE; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $this->keyManager->setPublicKey($user, $publicKeyDerived); |
|
| 88 | + $output->writeln('<info>Derived public key did not match, successfully updated</info>'); |
|
| 89 | + return static::SUCCESS; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $output->writeln('<info>Stored public key matches stored private key</info>'); |
|
| 93 | + return static::SUCCESS; |
|
| 94 | + } |
|
| 95 | 95 | } |
@@ -26,251 +26,251 @@ |
||
| 26 | 26 | use Test\TestCase; |
| 27 | 27 | |
| 28 | 28 | class ManagerTest extends TestCase { |
| 29 | - private Factory&MockObject $factory; |
|
| 30 | - private IAppData&MockObject $appData; |
|
| 31 | - private ICrypto&MockObject $crypto; |
|
| 32 | - private Manager&MockObject $manager; |
|
| 33 | - private IConfig&MockObject $config; |
|
| 34 | - private LoggerInterface&MockObject $logger; |
|
| 35 | - private ICacheFactory&MockObject $cacheFactory; |
|
| 36 | - private ICache&MockObject $cache; |
|
| 37 | - |
|
| 38 | - protected function setUp(): void { |
|
| 39 | - parent::setUp(); |
|
| 40 | - |
|
| 41 | - /** @var Factory|\PHPUnit\Framework\MockObject\MockObject $factory */ |
|
| 42 | - $this->factory = $this->createMock(Factory::class); |
|
| 43 | - $this->appData = $this->createMock(AppData::class); |
|
| 44 | - $this->config = $this->createMock(IConfig::class); |
|
| 45 | - $this->factory->expects($this->any()) |
|
| 46 | - ->method('get') |
|
| 47 | - ->with('identityproof') |
|
| 48 | - ->willReturn($this->appData); |
|
| 49 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
| 50 | - $this->cacheFactory = $this->createMock(ICacheFactory::class); |
|
| 51 | - $this->cache = $this->createMock(ICache::class); |
|
| 52 | - |
|
| 53 | - $this->cacheFactory->expects($this->any()) |
|
| 54 | - ->method('createDistributed') |
|
| 55 | - ->willReturn($this->cache); |
|
| 56 | - |
|
| 57 | - $this->crypto = $this->createMock(ICrypto::class); |
|
| 58 | - $this->manager = $this->getManager(['generateKeyPair']); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * create manager object |
|
| 63 | - * |
|
| 64 | - * @param array $setMethods |
|
| 65 | - * @return Manager|\PHPUnit\Framework\MockObject\MockObject |
|
| 66 | - */ |
|
| 67 | - protected function getManager($setMethods = []) { |
|
| 68 | - if (empty($setMethods)) { |
|
| 69 | - return new Manager( |
|
| 70 | - $this->factory, |
|
| 71 | - $this->crypto, |
|
| 72 | - $this->config, |
|
| 73 | - $this->logger, |
|
| 74 | - $this->cacheFactory, |
|
| 75 | - ); |
|
| 76 | - } else { |
|
| 77 | - return $this->getMockBuilder(Manager::class) |
|
| 78 | - ->setConstructorArgs([ |
|
| 79 | - $this->factory, |
|
| 80 | - $this->crypto, |
|
| 81 | - $this->config, |
|
| 82 | - $this->logger, |
|
| 83 | - $this->cacheFactory, |
|
| 84 | - ]) |
|
| 85 | - ->onlyMethods($setMethods) |
|
| 86 | - ->getMock(); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - public function testGetKeyWithExistingKey(): void { |
|
| 91 | - $user = $this->createMock(IUser::class); |
|
| 92 | - $user |
|
| 93 | - ->expects($this->once()) |
|
| 94 | - ->method('getUID') |
|
| 95 | - ->willReturn('MyUid'); |
|
| 96 | - $folder = $this->createMock(ISimpleFolder::class); |
|
| 97 | - $privateFile = $this->createMock(ISimpleFile::class); |
|
| 98 | - $privateFile |
|
| 99 | - ->expects($this->once()) |
|
| 100 | - ->method('getContent') |
|
| 101 | - ->willReturn('EncryptedPrivateKey'); |
|
| 102 | - $publicFile = $this->createMock(ISimpleFile::class); |
|
| 103 | - $publicFile |
|
| 104 | - ->expects($this->once()) |
|
| 105 | - ->method('getContent') |
|
| 106 | - ->willReturn('MyPublicKey'); |
|
| 107 | - $this->crypto |
|
| 108 | - ->expects($this->once()) |
|
| 109 | - ->method('decrypt') |
|
| 110 | - ->with('EncryptedPrivateKey') |
|
| 111 | - ->willReturn('MyPrivateKey'); |
|
| 112 | - $folder |
|
| 113 | - ->expects($this->exactly(2)) |
|
| 114 | - ->method('getFile') |
|
| 115 | - ->willReturnMap([ |
|
| 116 | - ['private', $privateFile], |
|
| 117 | - ['public', $publicFile], |
|
| 118 | - ]); |
|
| 119 | - $this->appData |
|
| 120 | - ->expects($this->once()) |
|
| 121 | - ->method('getFolder') |
|
| 122 | - ->with('user-MyUid') |
|
| 123 | - ->willReturn($folder); |
|
| 124 | - $this->cache |
|
| 125 | - ->expects($this->exactly(2)) |
|
| 126 | - ->method('get') |
|
| 127 | - ->willReturn(null); |
|
| 128 | - |
|
| 129 | - $expected = new Key('MyPublicKey', 'MyPrivateKey'); |
|
| 130 | - $this->assertEquals($expected, $this->manager->getKey($user)); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - public function testGetKeyWithExistingKeyCached(): void { |
|
| 134 | - $user = $this->createMock(IUser::class); |
|
| 135 | - $user |
|
| 136 | - ->expects($this->once()) |
|
| 137 | - ->method('getUID') |
|
| 138 | - ->willReturn('MyUid'); |
|
| 139 | - $this->crypto |
|
| 140 | - ->expects($this->once()) |
|
| 141 | - ->method('decrypt') |
|
| 142 | - ->with('EncryptedPrivateKey') |
|
| 143 | - ->willReturn('MyPrivateKey'); |
|
| 144 | - $this->cache |
|
| 145 | - ->expects($this->exactly(2)) |
|
| 146 | - ->method('get') |
|
| 147 | - ->willReturnMap([ |
|
| 148 | - ['user-MyUid-public', 'MyPublicKey'], |
|
| 149 | - ['user-MyUid-private', 'EncryptedPrivateKey'], |
|
| 150 | - ]); |
|
| 151 | - |
|
| 152 | - $expected = new Key('MyPublicKey', 'MyPrivateKey'); |
|
| 153 | - $this->assertEquals($expected, $this->manager->getKey($user)); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - public function testSetPublicKey(): void { |
|
| 157 | - $user = $this->createMock(IUser::class); |
|
| 158 | - $user |
|
| 159 | - ->expects($this->exactly(1)) |
|
| 160 | - ->method('getUID') |
|
| 161 | - ->willReturn('MyUid'); |
|
| 162 | - $publicFile = $this->createMock(ISimpleFile::class); |
|
| 163 | - $folder = $this->createMock(ISimpleFolder::class); |
|
| 164 | - $folder |
|
| 165 | - ->expects($this->once()) |
|
| 166 | - ->method('newFile') |
|
| 167 | - ->willReturnMap([ |
|
| 168 | - ['public', 'MyNewPublicKey', $publicFile], |
|
| 169 | - ]); |
|
| 170 | - $this->appData |
|
| 171 | - ->expects($this->once()) |
|
| 172 | - ->method('getFolder') |
|
| 173 | - ->with('user-MyUid') |
|
| 174 | - ->willReturn($folder); |
|
| 175 | - $this->cache |
|
| 176 | - ->expects($this->once()) |
|
| 177 | - ->method('set') |
|
| 178 | - ->with('user-MyUid-public', 'MyNewPublicKey'); |
|
| 179 | - |
|
| 180 | - $this->manager->setPublicKey($user, 'MyNewPublicKey'); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - public function testGetKeyWithNotExistingKey(): void { |
|
| 184 | - $user = $this->createMock(IUser::class); |
|
| 185 | - $user |
|
| 186 | - ->expects($this->once()) |
|
| 187 | - ->method('getUID') |
|
| 188 | - ->willReturn('MyUid'); |
|
| 189 | - $this->manager |
|
| 190 | - ->expects($this->once()) |
|
| 191 | - ->method('generateKeyPair') |
|
| 192 | - ->willReturn(['MyNewPublicKey', 'MyNewPrivateKey']); |
|
| 193 | - $this->appData |
|
| 194 | - ->expects($this->once()) |
|
| 195 | - ->method('newFolder') |
|
| 196 | - ->with('user-MyUid'); |
|
| 197 | - $folder = $this->createMock(ISimpleFolder::class); |
|
| 198 | - $this->crypto |
|
| 199 | - ->expects($this->once()) |
|
| 200 | - ->method('encrypt') |
|
| 201 | - ->with('MyNewPrivateKey') |
|
| 202 | - ->willReturn('MyNewEncryptedPrivateKey'); |
|
| 203 | - $privateFile = $this->createMock(ISimpleFile::class); |
|
| 204 | - $privateFile |
|
| 205 | - ->expects($this->once()) |
|
| 206 | - ->method('putContent') |
|
| 207 | - ->with('MyNewEncryptedPrivateKey'); |
|
| 208 | - $publicFile = $this->createMock(ISimpleFile::class); |
|
| 209 | - $publicFile |
|
| 210 | - ->expects($this->once()) |
|
| 211 | - ->method('putContent') |
|
| 212 | - ->with('MyNewPublicKey'); |
|
| 213 | - $folder |
|
| 214 | - ->expects($this->exactly(2)) |
|
| 215 | - ->method('newFile') |
|
| 216 | - ->willReturnMap([ |
|
| 217 | - ['private', null, $privateFile], |
|
| 218 | - ['public', null, $publicFile], |
|
| 219 | - ]); |
|
| 220 | - $this->appData |
|
| 221 | - ->expects($this->exactly(2)) |
|
| 222 | - ->method('getFolder') |
|
| 223 | - ->with('user-MyUid') |
|
| 224 | - ->willReturnOnConsecutiveCalls( |
|
| 225 | - $this->throwException(new \Exception()), |
|
| 226 | - $folder |
|
| 227 | - ); |
|
| 228 | - |
|
| 229 | - |
|
| 230 | - $expected = new Key('MyNewPublicKey', 'MyNewPrivateKey'); |
|
| 231 | - $this->assertEquals($expected, $this->manager->getKey($user)); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - public function testGenerateKeyPair(): void { |
|
| 235 | - $manager = $this->getManager(); |
|
| 236 | - $data = 'MyTestData'; |
|
| 237 | - |
|
| 238 | - [$resultPublicKey, $resultPrivateKey] = self::invokePrivate($manager, 'generateKeyPair'); |
|
| 239 | - openssl_sign($data, $signature, $resultPrivateKey); |
|
| 240 | - $details = openssl_pkey_get_details(openssl_pkey_get_public($resultPublicKey)); |
|
| 241 | - |
|
| 242 | - $this->assertSame(1, openssl_verify($data, $signature, $resultPublicKey)); |
|
| 243 | - $this->assertSame(2048, $details['bits']); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - public function testGetSystemKey(): void { |
|
| 247 | - $manager = $this->getManager(['retrieveKey']); |
|
| 248 | - |
|
| 249 | - /** @var Key|\PHPUnit\Framework\MockObject\MockObject $key */ |
|
| 250 | - $key = $this->createMock(Key::class); |
|
| 251 | - |
|
| 252 | - $this->config->expects($this->once())->method('getSystemValue') |
|
| 253 | - ->with('instanceid', null)->willReturn('instanceId'); |
|
| 254 | - |
|
| 255 | - $manager->expects($this->once())->method('retrieveKey')->with('system-instanceId') |
|
| 256 | - ->willReturn($key); |
|
| 257 | - |
|
| 258 | - $this->assertSame($key, $manager->getSystemKey()); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - |
|
| 262 | - |
|
| 263 | - public function testGetSystemKeyFailure(): void { |
|
| 264 | - $this->expectException(\RuntimeException::class); |
|
| 265 | - |
|
| 266 | - $manager = $this->getManager(['retrieveKey']); |
|
| 267 | - |
|
| 268 | - /** @var Key|\PHPUnit\Framework\MockObject\MockObject $key */ |
|
| 269 | - $key = $this->createMock(Key::class); |
|
| 270 | - |
|
| 271 | - $this->config->expects($this->once())->method('getSystemValue') |
|
| 272 | - ->with('instanceid', null)->willReturn(null); |
|
| 273 | - |
|
| 274 | - $manager->getSystemKey(); |
|
| 275 | - } |
|
| 29 | + private Factory&MockObject $factory; |
|
| 30 | + private IAppData&MockObject $appData; |
|
| 31 | + private ICrypto&MockObject $crypto; |
|
| 32 | + private Manager&MockObject $manager; |
|
| 33 | + private IConfig&MockObject $config; |
|
| 34 | + private LoggerInterface&MockObject $logger; |
|
| 35 | + private ICacheFactory&MockObject $cacheFactory; |
|
| 36 | + private ICache&MockObject $cache; |
|
| 37 | + |
|
| 38 | + protected function setUp(): void { |
|
| 39 | + parent::setUp(); |
|
| 40 | + |
|
| 41 | + /** @var Factory|\PHPUnit\Framework\MockObject\MockObject $factory */ |
|
| 42 | + $this->factory = $this->createMock(Factory::class); |
|
| 43 | + $this->appData = $this->createMock(AppData::class); |
|
| 44 | + $this->config = $this->createMock(IConfig::class); |
|
| 45 | + $this->factory->expects($this->any()) |
|
| 46 | + ->method('get') |
|
| 47 | + ->with('identityproof') |
|
| 48 | + ->willReturn($this->appData); |
|
| 49 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
| 50 | + $this->cacheFactory = $this->createMock(ICacheFactory::class); |
|
| 51 | + $this->cache = $this->createMock(ICache::class); |
|
| 52 | + |
|
| 53 | + $this->cacheFactory->expects($this->any()) |
|
| 54 | + ->method('createDistributed') |
|
| 55 | + ->willReturn($this->cache); |
|
| 56 | + |
|
| 57 | + $this->crypto = $this->createMock(ICrypto::class); |
|
| 58 | + $this->manager = $this->getManager(['generateKeyPair']); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * create manager object |
|
| 63 | + * |
|
| 64 | + * @param array $setMethods |
|
| 65 | + * @return Manager|\PHPUnit\Framework\MockObject\MockObject |
|
| 66 | + */ |
|
| 67 | + protected function getManager($setMethods = []) { |
|
| 68 | + if (empty($setMethods)) { |
|
| 69 | + return new Manager( |
|
| 70 | + $this->factory, |
|
| 71 | + $this->crypto, |
|
| 72 | + $this->config, |
|
| 73 | + $this->logger, |
|
| 74 | + $this->cacheFactory, |
|
| 75 | + ); |
|
| 76 | + } else { |
|
| 77 | + return $this->getMockBuilder(Manager::class) |
|
| 78 | + ->setConstructorArgs([ |
|
| 79 | + $this->factory, |
|
| 80 | + $this->crypto, |
|
| 81 | + $this->config, |
|
| 82 | + $this->logger, |
|
| 83 | + $this->cacheFactory, |
|
| 84 | + ]) |
|
| 85 | + ->onlyMethods($setMethods) |
|
| 86 | + ->getMock(); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + public function testGetKeyWithExistingKey(): void { |
|
| 91 | + $user = $this->createMock(IUser::class); |
|
| 92 | + $user |
|
| 93 | + ->expects($this->once()) |
|
| 94 | + ->method('getUID') |
|
| 95 | + ->willReturn('MyUid'); |
|
| 96 | + $folder = $this->createMock(ISimpleFolder::class); |
|
| 97 | + $privateFile = $this->createMock(ISimpleFile::class); |
|
| 98 | + $privateFile |
|
| 99 | + ->expects($this->once()) |
|
| 100 | + ->method('getContent') |
|
| 101 | + ->willReturn('EncryptedPrivateKey'); |
|
| 102 | + $publicFile = $this->createMock(ISimpleFile::class); |
|
| 103 | + $publicFile |
|
| 104 | + ->expects($this->once()) |
|
| 105 | + ->method('getContent') |
|
| 106 | + ->willReturn('MyPublicKey'); |
|
| 107 | + $this->crypto |
|
| 108 | + ->expects($this->once()) |
|
| 109 | + ->method('decrypt') |
|
| 110 | + ->with('EncryptedPrivateKey') |
|
| 111 | + ->willReturn('MyPrivateKey'); |
|
| 112 | + $folder |
|
| 113 | + ->expects($this->exactly(2)) |
|
| 114 | + ->method('getFile') |
|
| 115 | + ->willReturnMap([ |
|
| 116 | + ['private', $privateFile], |
|
| 117 | + ['public', $publicFile], |
|
| 118 | + ]); |
|
| 119 | + $this->appData |
|
| 120 | + ->expects($this->once()) |
|
| 121 | + ->method('getFolder') |
|
| 122 | + ->with('user-MyUid') |
|
| 123 | + ->willReturn($folder); |
|
| 124 | + $this->cache |
|
| 125 | + ->expects($this->exactly(2)) |
|
| 126 | + ->method('get') |
|
| 127 | + ->willReturn(null); |
|
| 128 | + |
|
| 129 | + $expected = new Key('MyPublicKey', 'MyPrivateKey'); |
|
| 130 | + $this->assertEquals($expected, $this->manager->getKey($user)); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + public function testGetKeyWithExistingKeyCached(): void { |
|
| 134 | + $user = $this->createMock(IUser::class); |
|
| 135 | + $user |
|
| 136 | + ->expects($this->once()) |
|
| 137 | + ->method('getUID') |
|
| 138 | + ->willReturn('MyUid'); |
|
| 139 | + $this->crypto |
|
| 140 | + ->expects($this->once()) |
|
| 141 | + ->method('decrypt') |
|
| 142 | + ->with('EncryptedPrivateKey') |
|
| 143 | + ->willReturn('MyPrivateKey'); |
|
| 144 | + $this->cache |
|
| 145 | + ->expects($this->exactly(2)) |
|
| 146 | + ->method('get') |
|
| 147 | + ->willReturnMap([ |
|
| 148 | + ['user-MyUid-public', 'MyPublicKey'], |
|
| 149 | + ['user-MyUid-private', 'EncryptedPrivateKey'], |
|
| 150 | + ]); |
|
| 151 | + |
|
| 152 | + $expected = new Key('MyPublicKey', 'MyPrivateKey'); |
|
| 153 | + $this->assertEquals($expected, $this->manager->getKey($user)); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + public function testSetPublicKey(): void { |
|
| 157 | + $user = $this->createMock(IUser::class); |
|
| 158 | + $user |
|
| 159 | + ->expects($this->exactly(1)) |
|
| 160 | + ->method('getUID') |
|
| 161 | + ->willReturn('MyUid'); |
|
| 162 | + $publicFile = $this->createMock(ISimpleFile::class); |
|
| 163 | + $folder = $this->createMock(ISimpleFolder::class); |
|
| 164 | + $folder |
|
| 165 | + ->expects($this->once()) |
|
| 166 | + ->method('newFile') |
|
| 167 | + ->willReturnMap([ |
|
| 168 | + ['public', 'MyNewPublicKey', $publicFile], |
|
| 169 | + ]); |
|
| 170 | + $this->appData |
|
| 171 | + ->expects($this->once()) |
|
| 172 | + ->method('getFolder') |
|
| 173 | + ->with('user-MyUid') |
|
| 174 | + ->willReturn($folder); |
|
| 175 | + $this->cache |
|
| 176 | + ->expects($this->once()) |
|
| 177 | + ->method('set') |
|
| 178 | + ->with('user-MyUid-public', 'MyNewPublicKey'); |
|
| 179 | + |
|
| 180 | + $this->manager->setPublicKey($user, 'MyNewPublicKey'); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + public function testGetKeyWithNotExistingKey(): void { |
|
| 184 | + $user = $this->createMock(IUser::class); |
|
| 185 | + $user |
|
| 186 | + ->expects($this->once()) |
|
| 187 | + ->method('getUID') |
|
| 188 | + ->willReturn('MyUid'); |
|
| 189 | + $this->manager |
|
| 190 | + ->expects($this->once()) |
|
| 191 | + ->method('generateKeyPair') |
|
| 192 | + ->willReturn(['MyNewPublicKey', 'MyNewPrivateKey']); |
|
| 193 | + $this->appData |
|
| 194 | + ->expects($this->once()) |
|
| 195 | + ->method('newFolder') |
|
| 196 | + ->with('user-MyUid'); |
|
| 197 | + $folder = $this->createMock(ISimpleFolder::class); |
|
| 198 | + $this->crypto |
|
| 199 | + ->expects($this->once()) |
|
| 200 | + ->method('encrypt') |
|
| 201 | + ->with('MyNewPrivateKey') |
|
| 202 | + ->willReturn('MyNewEncryptedPrivateKey'); |
|
| 203 | + $privateFile = $this->createMock(ISimpleFile::class); |
|
| 204 | + $privateFile |
|
| 205 | + ->expects($this->once()) |
|
| 206 | + ->method('putContent') |
|
| 207 | + ->with('MyNewEncryptedPrivateKey'); |
|
| 208 | + $publicFile = $this->createMock(ISimpleFile::class); |
|
| 209 | + $publicFile |
|
| 210 | + ->expects($this->once()) |
|
| 211 | + ->method('putContent') |
|
| 212 | + ->with('MyNewPublicKey'); |
|
| 213 | + $folder |
|
| 214 | + ->expects($this->exactly(2)) |
|
| 215 | + ->method('newFile') |
|
| 216 | + ->willReturnMap([ |
|
| 217 | + ['private', null, $privateFile], |
|
| 218 | + ['public', null, $publicFile], |
|
| 219 | + ]); |
|
| 220 | + $this->appData |
|
| 221 | + ->expects($this->exactly(2)) |
|
| 222 | + ->method('getFolder') |
|
| 223 | + ->with('user-MyUid') |
|
| 224 | + ->willReturnOnConsecutiveCalls( |
|
| 225 | + $this->throwException(new \Exception()), |
|
| 226 | + $folder |
|
| 227 | + ); |
|
| 228 | + |
|
| 229 | + |
|
| 230 | + $expected = new Key('MyNewPublicKey', 'MyNewPrivateKey'); |
|
| 231 | + $this->assertEquals($expected, $this->manager->getKey($user)); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + public function testGenerateKeyPair(): void { |
|
| 235 | + $manager = $this->getManager(); |
|
| 236 | + $data = 'MyTestData'; |
|
| 237 | + |
|
| 238 | + [$resultPublicKey, $resultPrivateKey] = self::invokePrivate($manager, 'generateKeyPair'); |
|
| 239 | + openssl_sign($data, $signature, $resultPrivateKey); |
|
| 240 | + $details = openssl_pkey_get_details(openssl_pkey_get_public($resultPublicKey)); |
|
| 241 | + |
|
| 242 | + $this->assertSame(1, openssl_verify($data, $signature, $resultPublicKey)); |
|
| 243 | + $this->assertSame(2048, $details['bits']); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + public function testGetSystemKey(): void { |
|
| 247 | + $manager = $this->getManager(['retrieveKey']); |
|
| 248 | + |
|
| 249 | + /** @var Key|\PHPUnit\Framework\MockObject\MockObject $key */ |
|
| 250 | + $key = $this->createMock(Key::class); |
|
| 251 | + |
|
| 252 | + $this->config->expects($this->once())->method('getSystemValue') |
|
| 253 | + ->with('instanceid', null)->willReturn('instanceId'); |
|
| 254 | + |
|
| 255 | + $manager->expects($this->once())->method('retrieveKey')->with('system-instanceId') |
|
| 256 | + ->willReturn($key); |
|
| 257 | + |
|
| 258 | + $this->assertSame($key, $manager->getSystemKey()); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + |
|
| 262 | + |
|
| 263 | + public function testGetSystemKeyFailure(): void { |
|
| 264 | + $this->expectException(\RuntimeException::class); |
|
| 265 | + |
|
| 266 | + $manager = $this->getManager(['retrieveKey']); |
|
| 267 | + |
|
| 268 | + /** @var Key|\PHPUnit\Framework\MockObject\MockObject $key */ |
|
| 269 | + $key = $this->createMock(Key::class); |
|
| 270 | + |
|
| 271 | + $this->config->expects($this->once())->method('getSystemValue') |
|
| 272 | + ->with('instanceid', null)->willReturn(null); |
|
| 273 | + |
|
| 274 | + $manager->getSystemKey(); |
|
| 275 | + } |
|
| 276 | 276 | } |