|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Lukas Reschke <[email protected]> |
|
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
|
|
|
|
|
23
|
|
|
namespace OC\Core\Command\Integrity; |
|
24
|
|
|
|
|
25
|
|
|
use OC\IntegrityCheck\Checker; |
|
26
|
|
|
use OC\IntegrityCheck\Helpers\FileAccessHelper; |
|
27
|
|
|
use OCP\IURLGenerator; |
|
28
|
|
|
use phpseclib\Crypt\RSA; |
|
29
|
|
|
use phpseclib\File\X509; |
|
30
|
|
|
use Symfony\Component\Console\Command\Command; |
|
31
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
32
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
33
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Class SignApp |
|
37
|
|
|
* |
|
38
|
|
|
* @package OC\Core\Command\Integrity |
|
39
|
|
|
*/ |
|
40
|
|
|
class SignApp extends Command { |
|
41
|
|
|
/** @var Checker */ |
|
42
|
|
|
private $checker; |
|
43
|
|
|
/** @var FileAccessHelper */ |
|
44
|
|
|
private $fileAccessHelper; |
|
45
|
|
|
/** @var IURLGenerator */ |
|
46
|
|
|
private $urlGenerator; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Checker $checker |
|
50
|
|
|
* @param FileAccessHelper $fileAccessHelper |
|
51
|
|
|
* @param IURLGenerator $urlGenerator |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(Checker $checker, |
|
54
|
|
|
FileAccessHelper $fileAccessHelper, |
|
55
|
|
|
IURLGenerator $urlGenerator) { |
|
56
|
|
|
parent::__construct(null); |
|
57
|
|
|
$this->checker = $checker; |
|
58
|
|
|
$this->fileAccessHelper = $fileAccessHelper; |
|
59
|
|
|
$this->urlGenerator = $urlGenerator; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
View Code Duplication |
protected function configure() { |
|
|
|
|
|
|
63
|
|
|
$this |
|
64
|
|
|
->setName('integrity:sign-app') |
|
65
|
|
|
->setDescription('Signs an app using a private key.') |
|
66
|
|
|
->addOption('path', null, InputOption::VALUE_REQUIRED, 'Application to sign') |
|
67
|
|
|
->addOption('privateKey', null, InputOption::VALUE_REQUIRED, 'Path to private key to use for signing') |
|
68
|
|
|
->addOption('certificate', null, InputOption::VALUE_REQUIRED, 'Path to certificate to use for signing'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc } |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
75
|
|
|
$path = $input->getOption('path'); |
|
76
|
|
|
$privateKeyPath = $input->getOption('privateKey'); |
|
77
|
|
|
$keyBundlePath = $input->getOption('certificate'); |
|
78
|
|
|
if(is_null($path) || is_null($privateKeyPath) || is_null($keyBundlePath)) { |
|
79
|
|
|
$documentationUrl = $this->urlGenerator->linkToDocs('developer-code-integrity'); |
|
80
|
|
|
$output->writeln('This command requires the --path, --privateKey and --certificate.'); |
|
81
|
|
|
$output->writeln('Example: ./occ integrity:sign-app --path="/Users/lukasreschke/Programming/myapp/" --privateKey="/Users/lukasreschke/private/myapp.key" --certificate="/Users/lukasreschke/public/mycert.crt"'); |
|
82
|
|
|
$output->writeln('For more information please consult the documentation: '. $documentationUrl); |
|
83
|
|
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$privateKey = $this->fileAccessHelper->file_get_contents($privateKeyPath); |
|
87
|
|
|
$keyBundle = $this->fileAccessHelper->file_get_contents($keyBundlePath); |
|
88
|
|
|
|
|
89
|
|
|
if($privateKey === false) { |
|
90
|
|
|
$output->writeln(sprintf('Private key "%s" does not exists.', $privateKeyPath)); |
|
91
|
|
|
return null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if($keyBundle === false) { |
|
95
|
|
|
$output->writeln(sprintf('Certificate "%s" does not exists.', $keyBundlePath)); |
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$rsa = new RSA(); |
|
100
|
|
|
$rsa->loadKey($privateKey); |
|
101
|
|
|
$x509 = new X509(); |
|
102
|
|
|
$x509->loadX509($keyBundle); |
|
103
|
|
|
$x509->setPrivateKey($rsa); |
|
104
|
|
|
try { |
|
105
|
|
|
$this->checker->writeAppSignature($path, $x509, $rsa); |
|
106
|
|
|
$output->writeln('Successfully signed "'.$path.'"'); |
|
107
|
|
|
} catch (\Exception $e){ |
|
108
|
|
|
$output->writeln('Error: ' . $e->getMessage()); |
|
109
|
|
|
return 1; |
|
110
|
|
|
} |
|
111
|
|
|
return 0; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.