SignCore.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Core\Command\Integrity;
  25. use OC\IntegrityCheck\Checker;
  26. use OC\IntegrityCheck\Helpers\FileAccessHelper;
  27. use phpseclib\Crypt\RSA;
  28. use phpseclib\File\X509;
  29. use Symfony\Component\Console\Command\Command;
  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. try {
  88. $this->checker->writeCoreSignature($x509, $rsa, $path);
  89. $output->writeln('Successfully signed "core"');
  90. } catch (\Exception $e) {
  91. $output->writeln('Error: ' . $e->getMessage());
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. }