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 OkpKeysetGeneratorCommand extends Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
protected function configure() |
30
|
|
|
{ |
31
|
|
|
$this |
32
|
|
|
->setName('keyset:generate:okp') |
33
|
|
|
->setDescription('Generate a key set with Octet Key Pairs keys (JWKSet format)') |
34
|
|
|
->setDefinition( |
35
|
|
|
new InputDefinition([ |
36
|
|
|
new InputOption('quantity', null, InputOption::VALUE_REQUIRED, 'Quantity of keys in the key set.'), |
37
|
|
|
new InputOption('curve', 'c', InputOption::VALUE_REQUIRED, 'Curve of the key.'), |
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
|
|
|
$curve = $input->getOption('curve'); |
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::createOKPKey($curve, $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
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function isEnabled() |
79
|
|
|
{ |
80
|
|
|
return class_exists('\Jose\Component\KeyManagement\JWKFactory'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|