-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
confirm authenticity of id-proof keypair #32827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
lib/private/Security/IdentityProof/Exception/IdentityProofKeySumException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright Copyright (c) 2022, Maxence Lange <[email protected]> | ||
| * | ||
| * @author Maxence Lange <[email protected]> | ||
| * | ||
| * @license GNU AGPL version 3 or any later version | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as | ||
| * published by the Free Software Foundation, either version 3 of the | ||
| * License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
| namespace OC\Security\IdentityProof\Exception; | ||
|
|
||
| class IdentityProofKeySumException extends \Exception { | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| * | ||
| * @author Lukas Reschke <[email protected]> | ||
| * @author Roeland Jago Douma <[email protected]> | ||
| * @author Maxence Lange <[email protected]> | ||
| * | ||
| * @license GNU AGPL version 3 or any later version | ||
| * | ||
|
|
@@ -48,4 +49,8 @@ public function getPrivate(): string { | |
| public function getPublic(): string { | ||
| return $this->publicKey; | ||
| } | ||
|
|
||
| public function getSum(): string { | ||
| return hash('sha512', $this->getPublic() . '.' . $this->getPrivate()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| * @author Joas Schilling <[email protected]> | ||
| * @author Lukas Reschke <[email protected]> | ||
| * @author Roeland Jago Douma <[email protected]> | ||
| * @author Maxence Lange <[email protected]> | ||
| * | ||
| * @license GNU AGPL version 3 or any later version | ||
| * | ||
|
|
@@ -27,16 +28,24 @@ | |
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
|
|
||
| namespace OC\Security\IdentityProof; | ||
|
|
||
| use OC\Files\AppData\Factory; | ||
| use OC\Security\IdentityProof\Exception\IdentityProofKeySumException; | ||
| use OCP\Files\IAppData; | ||
| use OCP\Files\NotFoundException; | ||
| use OCP\Files\NotPermittedException; | ||
| use OCP\IConfig; | ||
| use OCP\IUser; | ||
| use OCP\PreConditionNotMetException; | ||
| use OCP\Security\ICrypto; | ||
| use Psr\Log\LoggerInterface; | ||
| use RuntimeException; | ||
|
|
||
| class Manager { | ||
| public const SUM_PREFERENCES_KEY = 'identity_proof_key_sum'; | ||
|
|
||
| /** @var IAppData */ | ||
| private $appData; | ||
| /** @var ICrypto */ | ||
|
|
@@ -61,7 +70,7 @@ public function __construct(Factory $appDataFactory, | |
| * In a separate function for unit testing purposes. | ||
| * | ||
| * @return array [$publicKey, $privateKey] | ||
| * @throws \RuntimeException | ||
| * @throws RuntimeException | ||
| */ | ||
| protected function generateKeyPair(): array { | ||
| $config = [ | ||
|
|
@@ -74,12 +83,12 @@ protected function generateKeyPair(): array { | |
|
|
||
| if ($res === false) { | ||
| $this->logOpensslError(); | ||
| throw new \RuntimeException('OpenSSL reported a problem'); | ||
| throw new RuntimeException('OpenSSL reported a problem'); | ||
| } | ||
|
|
||
| if (openssl_pkey_export($res, $privateKey, null, $config) === false) { | ||
| $this->logOpensslError(); | ||
| throw new \RuntimeException('OpenSSL reported a problem'); | ||
| throw new RuntimeException('OpenSSL reported a problem'); | ||
| } | ||
|
|
||
| // Extract the public key from $res to $pubKey | ||
|
|
@@ -94,8 +103,10 @@ protected function generateKeyPair(): array { | |
| * Note: If a key already exists it will be overwritten | ||
| * | ||
| * @param string $id key id | ||
| * | ||
| * @return Key | ||
| * @throws \RuntimeException | ||
| * @throws NotFoundException | ||
| * @throws NotPermittedException | ||
| */ | ||
| protected function generateKey(string $id): Key { | ||
| [$publicKey, $privateKey] = $this->generateKeyPair(); | ||
|
|
@@ -109,54 +120,112 @@ protected function generateKey(string $id): Key { | |
| $folder->newFile('private') | ||
| ->putContent($this->crypto->encrypt($privateKey)); | ||
| $folder->newFile('public') | ||
| ->putContent($publicKey); | ||
| ->putContent($publicKey); | ||
|
|
||
| return new Key($publicKey, $privateKey); | ||
| } | ||
|
|
||
| /** | ||
| * Get key for a specific id | ||
| * if $userId is set, a checksum of the key will be stored for future comparison | ||
| * | ||
| * @param string $id | ||
| * @param string $userId | ||
| * | ||
| * @return Key | ||
| * @throws \RuntimeException | ||
| * @throws NotFoundException | ||
| * @throws NotPermittedException | ||
| * @throws PreConditionNotMetException | ||
| */ | ||
| protected function retrieveKey(string $id): Key { | ||
| protected function retrieveKey(string $id, string $userId = ''): Key { | ||
| try { | ||
| $folder = $this->appData->getFolder($id); | ||
| $privateKey = $this->crypto->decrypt( | ||
| $folder->getFile('private')->getContent() | ||
| ); | ||
| $publicKey = $folder->getFile('public')->getContent(); | ||
| return new Key($publicKey, $privateKey); | ||
| $key = new Key($publicKey, $privateKey); | ||
|
|
||
| $this->confirmSum($key, $userId); | ||
| } catch (\Exception $e) { | ||
| return $this->generateKey($id); | ||
| $key = $this->generateKey($id); | ||
| $this->generateKeySum($key, $userId); | ||
| } | ||
|
|
||
| return $key; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * @param Key $key | ||
| * @param string $userId | ||
| * | ||
| * @throws IdentityProofKeySumException | ||
| * @throws PreConditionNotMetException | ||
| */ | ||
| protected function confirmSum(Key $key, string $userId): void { | ||
| if ($userId === '') { | ||
| $knownSum = $this->config->getAppValue('core', self::SUM_PREFERENCES_KEY, ''); | ||
| } else { | ||
| $knownSum = $this->config->getUserValue($userId, 'core', self::SUM_PREFERENCES_KEY, ''); | ||
| } | ||
|
|
||
| if ($knownSum === '') { // sum is not known, generate a new one | ||
| $this->generateKeySum($key, $userId); | ||
| } | ||
|
|
||
| if ($knownSum !== $key->getSum()) { | ||
| throw new IdentityProofKeySumException(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * @param Key $key | ||
| * @param string $userId | ||
| * | ||
| * @return void | ||
| * @throws PreConditionNotMetException | ||
| */ | ||
| public function generateKeySum(Key $key, string $userId): void { | ||
| if ($userId === '') { | ||
| $this->config->setAppValue('core', self::SUM_PREFERENCES_KEY, $key->getSum()); | ||
| } else { | ||
| $this->config->setUserValue($userId, 'core', self::SUM_PREFERENCES_KEY, $key->getSum()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Get public and private key for $user | ||
| * | ||
| * @param IUser $user | ||
| * | ||
| * @return Key | ||
| * @throws \RuntimeException | ||
| * @throws NotFoundException | ||
| * @throws NotPermittedException | ||
| * @throws PreConditionNotMetException | ||
| */ | ||
| public function getKey(IUser $user): Key { | ||
| $uid = $user->getUID(); | ||
| return $this->retrieveKey('user-' . $uid); | ||
|
|
||
| return $this->retrieveKey('user-' . $uid, $uid); | ||
| } | ||
|
|
||
| /** | ||
| * Get instance wide public and private key | ||
| * | ||
| * @return Key | ||
| * @throws \RuntimeException | ||
| * @throws NotFoundException | ||
| * @throws NotPermittedException | ||
| * @throws PreConditionNotMetException | ||
| */ | ||
| public function getSystemKey(): Key { | ||
| $instanceId = $this->config->getSystemValue('instanceid', null); | ||
| if ($instanceId === null) { | ||
| throw new \RuntimeException('no instance id!'); | ||
| throw new RuntimeException('no instance id!'); | ||
| } | ||
|
|
||
| return $this->retrieveKey('system-' . $instanceId); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be in the public namespace so it can be catched?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait this all has no public api?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only did some monkeying on this one. There is no public interface on
Security\IdentityProof. Should I keep the files as it is, or do you have an example of the structure we should expect ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep as is for now and after this PR we can add a public interface for 25+