SignCore.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Core\Command\Integrity;
  26. use OC\IntegrityCheck\Checker;
  27. use OC\IntegrityCheck\Helpers\FileAccessHelper;
  28. use phpseclib\Crypt\RSA;
  29. use phpseclib\File\X509;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Input\InputOption;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. /**
  35. * Class SignCore
  36. *
  37. * @package OC\Core\Command\Integrity
  38. */
  39. class SignCore extends Command {
  40. public function __construct(
  41. private Checker $checker,
  42. private FileAccessHelper $fileAccessHelper,
  43. ) {
  44. parent::__construct(null);
  45. }
  46. protected function configure() {
  47. $this
  48. ->setName('integrity:sign-core')
  49. ->setDescription('Sign core using a private key.')
  50. ->addOption('privateKey', null, InputOption::VALUE_REQUIRED, 'Path to private key to use for signing')
  51. ->addOption('certificate', null, InputOption::VALUE_REQUIRED, 'Path to certificate to use for signing')
  52. ->addOption('path', null, InputOption::VALUE_REQUIRED, 'Path of core to sign');
  53. }
  54. /**
  55. * {@inheritdoc }
  56. */
  57. protected function execute(InputInterface $input, OutputInterface $output): int {
  58. $privateKeyPath = $input->getOption('privateKey');
  59. $keyBundlePath = $input->getOption('certificate');
  60. $path = $input->getOption('path');
  61. if (is_null($privateKeyPath) || is_null($keyBundlePath) || is_null($path)) {
  62. $output->writeln('--privateKey, --certificate and --path are required.');
  63. return 1;
  64. }
  65. $privateKey = $this->fileAccessHelper->file_get_contents($privateKeyPath);
  66. $keyBundle = $this->fileAccessHelper->file_get_contents($keyBundlePath);
  67. if ($privateKey === false) {
  68. $output->writeln(sprintf('Private key "%s" does not exists.', $privateKeyPath));
  69. return 1;
  70. }
  71. if ($keyBundle === false) {
  72. $output->writeln(sprintf('Certificate "%s" does not exists.', $keyBundlePath));
  73. return 1;
  74. }
  75. $rsa = new RSA();
  76. $rsa->loadKey($privateKey);
  77. $x509 = new X509();
  78. $x509->loadX509($keyBundle);
  79. $x509->setPrivateKey($rsa);
  80. try {
  81. $this->checker->writeCoreSignature($x509, $rsa, $path);
  82. $output->writeln('Successfully signed "core"');
  83. } catch (\Exception $e) {
  84. $output->writeln('Error: ' . $e->getMessage());
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. }