signcore.php 3.0 KB

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