You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SignCore.php 3.1KB

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