signapp.php 3.1 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\FileAccessHelper;
  24. use phpseclib\Crypt\RSA;
  25. use phpseclib\File\X509;
  26. use Symfony\Component\Console\Command\Command;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Input\InputOption;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. /**
  31. * Class SignApp
  32. *
  33. * @package OC\Core\Command\Integrity
  34. */
  35. class SignApp extends Command {
  36. /** @var Checker */
  37. private $checker;
  38. /** @var FileAccessHelper */
  39. private $fileAccessHelper;
  40. /**
  41. * @param Checker $checker
  42. * @param FileAccessHelper $fileAccessHelper
  43. */
  44. public function __construct(Checker $checker,
  45. FileAccessHelper $fileAccessHelper) {
  46. parent::__construct(null);
  47. $this->checker = $checker;
  48. $this->fileAccessHelper = $fileAccessHelper;
  49. }
  50. protected function configure() {
  51. $this
  52. ->setName('integrity:sign-app')
  53. ->setDescription('Sign app using a private key.')
  54. ->addOption('appId', null, InputOption::VALUE_REQUIRED, 'Application to sign')
  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. }
  58. /**
  59. * {@inheritdoc }
  60. */
  61. protected function execute(InputInterface $input, OutputInterface $output) {
  62. $appId = $input->getOption('appId');
  63. $privateKeyPath = $input->getOption('privateKey');
  64. $keyBundlePath = $input->getOption('certificate');
  65. if(is_null($appId) || is_null($privateKeyPath) || is_null($keyBundlePath)) {
  66. $output->writeln('--appId, --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->writeAppSignature($appId, $x509, $rsa);
  85. $output->writeln('Successfully signed "'.$appId.'"');
  86. }
  87. }