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