|
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\JWK; |
|
17
|
|
|
use Jose\Component\Core\Util\ECKey; |
|
18
|
|
|
use Jose\Component\Core\Util\RSAKey; |
|
19
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class PemConverterCommand extends AbstractObjectOutputCommand |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function configure() |
|
29
|
|
|
{ |
|
30
|
|
|
parent::configure(); |
|
31
|
|
|
$this |
|
32
|
|
|
->setName('key:convert:pkcs1') |
|
33
|
|
|
->setDescription('Converts a RSA or EC key into PKCS#1 key.') |
|
34
|
|
|
->addArgument('jwk', InputArgument::REQUIRED, 'The key') |
|
35
|
|
|
; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
42
|
|
|
{ |
|
43
|
|
|
$jwk = $input->getArgument('jwk'); |
|
44
|
|
|
$json = $this->jsonConverter->decode($jwk); |
|
45
|
|
|
if (!is_array($json)) { |
|
46
|
|
|
throw new \InvalidArgumentException('Invalid key.'); |
|
47
|
|
|
} |
|
48
|
|
|
$key = JWK::create($json); |
|
49
|
|
|
switch ($key->get('kty')) { |
|
50
|
|
|
case 'RSA': |
|
51
|
|
|
$pem = RSAKey::createFromJWK($key)->toPem(); |
|
52
|
|
|
|
|
53
|
|
|
break; |
|
54
|
|
|
case 'EC': |
|
55
|
|
|
$pem = ECKey::createFromJWK($key)->toPem(); |
|
56
|
|
|
|
|
57
|
|
|
break; |
|
58
|
|
|
default: |
|
59
|
|
|
throw new \InvalidArgumentException('Not a RSA or EC key.'); |
|
60
|
|
|
} |
|
61
|
|
|
$this->prepareOutput($input, $output, $pem); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|