SignCore.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\Integrity;
  8. use OC\IntegrityCheck\Checker;
  9. use OC\IntegrityCheck\Helpers\FileAccessHelper;
  10. use phpseclib\Crypt\RSA;
  11. use phpseclib\File\X509;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. /**
  17. * Class SignCore
  18. *
  19. * @package OC\Core\Command\Integrity
  20. */
  21. class SignCore extends Command {
  22. public function __construct(
  23. private Checker $checker,
  24. private FileAccessHelper $fileAccessHelper,
  25. ) {
  26. parent::__construct(null);
  27. }
  28. protected function configure() {
  29. $this
  30. ->setName('integrity:sign-core')
  31. ->setDescription('Sign core using a private key.')
  32. ->addOption('privateKey', null, InputOption::VALUE_REQUIRED, 'Path to private key to use for signing')
  33. ->addOption('certificate', null, InputOption::VALUE_REQUIRED, 'Path to certificate to use for signing')
  34. ->addOption('path', null, InputOption::VALUE_REQUIRED, 'Path of core to sign');
  35. }
  36. /**
  37. * {@inheritdoc }
  38. */
  39. protected function execute(InputInterface $input, OutputInterface $output): int {
  40. $privateKeyPath = $input->getOption('privateKey');
  41. $keyBundlePath = $input->getOption('certificate');
  42. $path = $input->getOption('path');
  43. if (is_null($privateKeyPath) || is_null($keyBundlePath) || is_null($path)) {
  44. $output->writeln('--privateKey, --certificate and --path are required.');
  45. return 1;
  46. }
  47. $privateKey = $this->fileAccessHelper->file_get_contents($privateKeyPath);
  48. $keyBundle = $this->fileAccessHelper->file_get_contents($keyBundlePath);
  49. if ($privateKey === false) {
  50. $output->writeln(sprintf('Private key "%s" does not exists.', $privateKeyPath));
  51. return 1;
  52. }
  53. if ($keyBundle === false) {
  54. $output->writeln(sprintf('Certificate "%s" does not exists.', $keyBundlePath));
  55. return 1;
  56. }
  57. $rsa = new RSA();
  58. $rsa->loadKey($privateKey);
  59. $x509 = new X509();
  60. $x509->loadX509($keyBundle);
  61. $x509->setPrivateKey($rsa);
  62. try {
  63. $this->checker->writeCoreSignature($x509, $rsa, $path);
  64. $output->writeln('Successfully signed "core"');
  65. } catch (\Exception $e) {
  66. $output->writeln('Error: ' . $e->getMessage());
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. }