Verify.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024, Marcel Müller <marcel.mueller@nextcloud.com>
  5. *
  6. * @author Marcel Müller <marcel.mueller@nextcloud.com>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Command\User\Keys;
  25. use OC\Security\IdentityProof\Manager;
  26. use OCP\IUser;
  27. use OCP\IUserManager;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class Verify extends Command {
  33. public function __construct(
  34. protected IUserManager $userManager,
  35. protected Manager $keyManager,
  36. ) {
  37. parent::__construct();
  38. }
  39. protected function configure(): void {
  40. $this
  41. ->setName('user:keys:verify')
  42. ->setDescription('Verify if the stored public key matches the stored private key')
  43. ->addArgument(
  44. 'user-id',
  45. InputArgument::REQUIRED,
  46. 'User ID of the user to verify'
  47. )
  48. ;
  49. }
  50. /**
  51. * @param InputInterface $input
  52. * @param OutputInterface $output
  53. * @return int
  54. */
  55. protected function execute(InputInterface $input, OutputInterface $output): int {
  56. $userId = $input->getArgument('user-id');
  57. $user = $this->userManager->get($userId);
  58. if (!$user instanceof IUser) {
  59. $output->writeln('Unknown user');
  60. return static::FAILURE;
  61. }
  62. $key = $this->keyManager->getKey($user);
  63. $publicKey = $key->getPublic();
  64. $privateKey = $key->getPrivate();
  65. $output->writeln('User public key size: ' . strlen($publicKey));
  66. $output->writeln('User private key size: ' . strlen($privateKey));
  67. // Derive the public key from the private key again to validate the stored public key
  68. $opensslPrivateKey = openssl_pkey_get_private($privateKey);
  69. $publicKeyDerived = openssl_pkey_get_details($opensslPrivateKey);
  70. $publicKeyDerived = $publicKeyDerived['key'];
  71. $output->writeln('User derived public key size: ' . strlen($publicKeyDerived));
  72. $output->writeln('');
  73. $output->writeln('Stored public key:');
  74. $output->writeln($publicKey);
  75. $output->writeln('Derived public key:');
  76. $output->writeln($publicKeyDerived);
  77. if ($publicKey != $publicKeyDerived) {
  78. $output->writeln('<error>Stored public key does not match stored private key</error>');
  79. return static::FAILURE;
  80. }
  81. $output->writeln('<info>Stored public key matches stored private key</info>');
  82. return static::SUCCESS;
  83. }
  84. }