SignCore.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\FileAccessHelper;
  25. use phpseclib\Crypt\RSA;
  26. use phpseclib\File\X509;
  27. use Symfony\Component\Console\Command\Command;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Input\InputOption;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. /**
  32. * Class SignCore
  33. *
  34. * @package OC\Core\Command\Integrity
  35. */
  36. class SignCore extends Command {
  37. /** @var Checker */
  38. private $checker;
  39. /** @var FileAccessHelper */
  40. private $fileAccessHelper;
  41. /**
  42. * @param Checker $checker
  43. * @param FileAccessHelper $fileAccessHelper
  44. */
  45. public function __construct(Checker $checker,
  46. FileAccessHelper $fileAccessHelper) {
  47. parent::__construct(null);
  48. $this->checker = $checker;
  49. $this->fileAccessHelper = $fileAccessHelper;
  50. }
  51. protected function configure() {
  52. $this
  53. ->setName('integrity:sign-core')
  54. ->setDescription('Sign core using a private key.')
  55. ->addOption('privateKey', null, InputOption::VALUE_REQUIRED, 'Path to private key to use for signing')
  56. ->addOption('certificate', null, InputOption::VALUE_REQUIRED, 'Path to certificate to use for signing')
  57. ->addOption('path', null, InputOption::VALUE_REQUIRED, 'Path of core to sign');
  58. }
  59. /**
  60. * {@inheritdoc }
  61. */
  62. protected function execute(InputInterface $input, OutputInterface $output) {
  63. $privateKeyPath = $input->getOption('privateKey');
  64. $keyBundlePath = $input->getOption('certificate');
  65. $path = $input->getOption('path');
  66. if(is_null($privateKeyPath) || is_null($keyBundlePath) || is_null($path)) {
  67. $output->writeln('--privateKey, --certificate and --path are required.');
  68. return null;
  69. }
  70. $privateKey = $this->fileAccessHelper->file_get_contents($privateKeyPath);
  71. $keyBundle = $this->fileAccessHelper->file_get_contents($keyBundlePath);
  72. if($privateKey === false) {
  73. $output->writeln(sprintf('Private key "%s" does not exists.', $privateKeyPath));
  74. return null;
  75. }
  76. if($keyBundle === false) {
  77. $output->writeln(sprintf('Certificate "%s" does not exists.', $keyBundlePath));
  78. return null;
  79. }
  80. $rsa = new RSA();
  81. $rsa->loadKey($privateKey);
  82. $x509 = new X509();
  83. $x509->loadX509($keyBundle);
  84. $x509->setPrivateKey($rsa);
  85. try {
  86. $this->checker->writeCoreSignature($x509, $rsa, $path);
  87. $output->writeln('Successfully signed "core"');
  88. } catch (\Exception $e){
  89. $output->writeln('Error: ' . $e->getMessage());
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. }