SignApp.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Core\Command\Integrity;
  24. use OC\IntegrityCheck\Checker;
  25. use OC\IntegrityCheck\Helpers\FileAccessHelper;
  26. use OCP\IURLGenerator;
  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 SignApp
  35. *
  36. * @package OC\Core\Command\Integrity
  37. */
  38. class SignApp extends Command {
  39. /** @var Checker */
  40. private $checker;
  41. /** @var FileAccessHelper */
  42. private $fileAccessHelper;
  43. /** @var IURLGenerator */
  44. private $urlGenerator;
  45. /**
  46. * @param Checker $checker
  47. * @param FileAccessHelper $fileAccessHelper
  48. * @param IURLGenerator $urlGenerator
  49. */
  50. public function __construct(Checker $checker,
  51. FileAccessHelper $fileAccessHelper,
  52. IURLGenerator $urlGenerator) {
  53. parent::__construct(null);
  54. $this->checker = $checker;
  55. $this->fileAccessHelper = $fileAccessHelper;
  56. $this->urlGenerator = $urlGenerator;
  57. }
  58. protected function configure() {
  59. $this
  60. ->setName('integrity:sign-app')
  61. ->setDescription('Signs an app using a private key.')
  62. ->addOption('path', null, InputOption::VALUE_REQUIRED, 'Application to sign')
  63. ->addOption('privateKey', null, InputOption::VALUE_REQUIRED, 'Path to private key to use for signing')
  64. ->addOption('certificate', null, InputOption::VALUE_REQUIRED, 'Path to certificate to use for signing');
  65. }
  66. /**
  67. * {@inheritdoc }
  68. */
  69. protected function execute(InputInterface $input, OutputInterface $output) {
  70. $path = $input->getOption('path');
  71. $privateKeyPath = $input->getOption('privateKey');
  72. $keyBundlePath = $input->getOption('certificate');
  73. if(is_null($path) || is_null($privateKeyPath) || is_null($keyBundlePath)) {
  74. $documentationUrl = $this->urlGenerator->linkToDocs('developer-code-integrity');
  75. $output->writeln('This command requires the --path, --privateKey and --certificate.');
  76. $output->writeln('Example: ./occ integrity:sign-app --path="/Users/lukasreschke/Programming/myapp/" --privateKey="/Users/lukasreschke/private/myapp.key" --certificate="/Users/lukasreschke/public/mycert.crt"');
  77. $output->writeln('For more information please consult the documentation: '. $documentationUrl);
  78. return null;
  79. }
  80. $privateKey = $this->fileAccessHelper->file_get_contents($privateKeyPath);
  81. $keyBundle = $this->fileAccessHelper->file_get_contents($keyBundlePath);
  82. if($privateKey === false) {
  83. $output->writeln(sprintf('Private key "%s" does not exists.', $privateKeyPath));
  84. return null;
  85. }
  86. if($keyBundle === false) {
  87. $output->writeln(sprintf('Certificate "%s" does not exists.', $keyBundlePath));
  88. return null;
  89. }
  90. $rsa = new RSA();
  91. $rsa->loadKey($privateKey);
  92. $x509 = new X509();
  93. $x509->loadX509($keyBundle);
  94. $x509->setPrivateKey($rsa);
  95. try {
  96. $this->checker->writeAppSignature($path, $x509, $rsa);
  97. $output->writeln('Successfully signed "'.$path.'"');
  98. } catch (\Exception $e){
  99. $output->writeln('Error: ' . $e->getMessage());
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. }