|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Jose\Component\Console\Command; |
|
15
|
|
|
|
|
16
|
|
|
use Jose\Component\Core\JWKSet; |
|
17
|
|
|
use Jose\Component\KeyManagement\JWKFactory; |
|
18
|
|
|
use Symfony\Component\Console\Command\Command; |
|
19
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
23
|
|
|
|
|
24
|
|
|
final class RsaKeysetGeneratorCommand extends AbstractGeneratorCommand |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function configure() |
|
30
|
|
|
{ |
|
31
|
|
|
$this |
|
32
|
|
|
->setName('keyset:generate:rsa') |
|
33
|
|
|
->setDescription('Generate a key set with RSA keys (JWK format)') |
|
34
|
|
|
->setDefinition( |
|
35
|
|
|
new InputDefinition([ |
|
36
|
|
|
new InputOption('quantity', null, InputOption::VALUE_REQUIRED, 'Quantity of keys in the key set.'), |
|
37
|
|
|
new InputOption('size', 's', InputOption::VALUE_OPTIONAL, 'Key size.', 2048), |
|
38
|
|
|
new InputOption('use', 'u', InputOption::VALUE_OPTIONAL, 'Usage of the key. Must be either "sig" or "enc".'), |
|
39
|
|
|
new InputOption('alg', 'a', InputOption::VALUE_OPTIONAL, 'Algorithm for the key.'), |
|
40
|
|
|
new InputOption('out', 'o', InputOption::VALUE_OPTIONAL, 'File where to save the key. Must be a valid and writable file name.'), |
|
41
|
|
|
]) |
|
42
|
|
|
) |
|
43
|
|
|
; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
50
|
|
|
{ |
|
51
|
|
|
$quantity = (int) $input->getOption('quantity'); |
|
52
|
|
|
$size = (int) $input->getOption('size'); |
|
53
|
|
|
$args = []; |
|
54
|
|
|
foreach (['use', 'alg'] as $key) { |
|
55
|
|
|
$value = $input->getOption($key); |
|
56
|
|
|
if (null !== $value) { |
|
57
|
|
|
$args[$key] = $value; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$keyset = JWKSet::createFromKeys([]); |
|
62
|
|
|
for ($i = 0; $i < $quantity; ++$i) { |
|
63
|
|
|
$keyset = $keyset->withKey(JWKFactory::createRSAKey($size, $args)); |
|
64
|
|
|
} |
|
65
|
|
|
$json = json_encode($keyset); |
|
66
|
|
|
|
|
67
|
|
|
$file = $input->getOption('out'); |
|
68
|
|
|
if (null !== $file) { |
|
69
|
|
|
file_put_contents($file, $json, LOCK_EX); |
|
70
|
|
|
} else { |
|
71
|
|
|
$output->write($json); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|