SignApp.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 OCP\IURLGenerator;
  26. use phpseclib\Crypt\RSA;
  27. use phpseclib\File\X509;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Input\InputOption;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. /**
  33. * Class SignApp
  34. *
  35. * @package OC\Core\Command\Integrity
  36. */
  37. class SignApp extends Command {
  38. /** @var Checker */
  39. private $checker;
  40. /** @var FileAccessHelper */
  41. private $fileAccessHelper;
  42. /** @var IURLGenerator */
  43. private $urlGenerator;
  44. /**
  45. * @param Checker $checker
  46. * @param FileAccessHelper $fileAccessHelper
  47. * @param IURLGenerator $urlGenerator
  48. */
  49. public function __construct(Checker $checker,
  50. FileAccessHelper $fileAccessHelper,
  51. IURLGenerator $urlGenerator) {
  52. parent::__construct(null);
  53. $this->checker = $checker;
  54. $this->fileAccessHelper = $fileAccessHelper;
  55. $this->urlGenerator = $urlGenerator;
  56. }
  57. protected function configure() {
  58. $this
  59. ->setName('integrity:sign-app')
  60. ->setDescription('Signs an app using a private key.')
  61. ->addOption('path', null, InputOption::VALUE_REQUIRED, 'Application to sign')
  62. ->addOption('privateKey', null, InputOption::VALUE_REQUIRED, 'Path to private key to use for signing')
  63. ->addOption('certificate', null, InputOption::VALUE_REQUIRED, 'Path to certificate to use for signing');
  64. }
  65. /**
  66. * {@inheritdoc }
  67. */
  68. protected function execute(InputInterface $input, OutputInterface $output) {
  69. $path = $input->getOption('path');
  70. $privateKeyPath = $input->getOption('privateKey');
  71. $keyBundlePath = $input->getOption('certificate');
  72. if(is_null($path) || is_null($privateKeyPath) || is_null($keyBundlePath)) {
  73. $documentationUrl = $this->urlGenerator->linkToDocs('developer-code-integrity');
  74. $output->writeln('This command requires the --path, --privateKey and --certificate.');
  75. $output->writeln('Example: ./occ integrity:sign-app --path="/Users/lukasreschke/Programming/myapp/" --privateKey="/Users/lukasreschke/private/myapp.key" --certificate="/Users/lukasreschke/public/mycert.crt"');
  76. $output->writeln('For more information please consult the documentation: '. $documentationUrl);
  77. return null;
  78. }
  79. $privateKey = $this->fileAccessHelper->file_get_contents($privateKeyPath);
  80. $keyBundle = $this->fileAccessHelper->file_get_contents($keyBundlePath);
  81. if($privateKey === false) {
  82. $output->writeln(sprintf('Private key "%s" does not exists.', $privateKeyPath));
  83. return null;
  84. }
  85. if($keyBundle === false) {
  86. $output->writeln(sprintf('Certificate "%s" does not exists.', $keyBundlePath));
  87. return null;
  88. }
  89. $rsa = new RSA();
  90. $rsa->loadKey($privateKey);
  91. $x509 = new X509();
  92. $x509->loadX509($keyBundle);
  93. $x509->setPrivateKey($rsa);
  94. $this->checker->writeAppSignature($path, $x509, $rsa);
  95. $output->writeln('Successfully signed "'.$path.'"');
  96. }
  97. }