1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWK\RSA; |
6
|
|
|
|
7
|
|
|
use Sop\CryptoEncoding\PEM; |
8
|
|
|
use Sop\CryptoTypes\Asymmetric\PrivateKeyInfo; |
9
|
|
|
use Sop\CryptoTypes\Asymmetric\RSA\RSAPrivateKey; |
10
|
|
|
use Sop\JWX\JWK\Asymmetric\PrivateKeyJWK; |
11
|
|
|
use Sop\JWX\JWK\Asymmetric\PublicKeyJWK; |
12
|
|
|
use Sop\JWX\JWK\Parameter\ExponentParameter; |
13
|
|
|
use Sop\JWX\JWK\Parameter\FirstCRTCoefficientParameter; |
14
|
|
|
use Sop\JWX\JWK\Parameter\FirstFactorCRTExponentParameter; |
15
|
|
|
use Sop\JWX\JWK\Parameter\FirstPrimeFactorParameter; |
16
|
|
|
use Sop\JWX\JWK\Parameter\JWKParameter; |
17
|
|
|
use Sop\JWX\JWK\Parameter\KeyTypeParameter; |
18
|
|
|
use Sop\JWX\JWK\Parameter\ModulusParameter; |
19
|
|
|
use Sop\JWX\JWK\Parameter\PrivateExponentParameter; |
20
|
|
|
use Sop\JWX\JWK\Parameter\SecondFactorCRTExponentParameter; |
21
|
|
|
use Sop\JWX\JWK\Parameter\SecondPrimeFactorParameter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class representing RSA private key as a JWK. |
25
|
|
|
* |
26
|
|
|
* @see https://tools.ietf.org/html/rfc7517#section-4 |
27
|
|
|
* @see https://tools.ietf.org/html/rfc7518#section-6.3 |
28
|
|
|
* @see https://tools.ietf.org/html/rfc7518#section-6.3.2 |
29
|
|
|
*/ |
30
|
|
|
class RSAPrivateKeyJWK extends PrivateKeyJWK |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Parameter names managed by this class. |
34
|
|
|
* |
35
|
|
|
* @internal |
36
|
|
|
* |
37
|
|
|
* @var string[] |
38
|
|
|
*/ |
39
|
|
|
public const MANAGED_PARAMS = [ |
40
|
|
|
JWKParameter::PARAM_KEY_TYPE, |
41
|
|
|
JWKParameter::PARAM_MODULUS, |
42
|
|
|
JWKParameter::PARAM_EXPONENT, |
43
|
|
|
JWKParameter::PARAM_PRIVATE_EXPONENT, |
44
|
|
|
JWKParameter::PARAM_FIRST_PRIME_FACTOR, |
45
|
|
|
JWKParameter::PARAM_SECOND_PRIME_FACTOR, |
46
|
|
|
JWKParameter::PARAM_FIRST_FACTOR_CRT_EXPONENT, |
47
|
|
|
JWKParameter::PARAM_SECOND_FACTOR_CRT_EXPONENT, |
48
|
|
|
JWKParameter::PARAM_FIRST_CRT_COEFFICIENT, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Constructor. |
53
|
|
|
* |
54
|
|
|
* @param JWKParameter ...$params |
55
|
|
|
* |
56
|
|
|
* @throws \UnexpectedValueException If missing required parameter |
57
|
|
|
*/ |
58
|
13 |
|
public function __construct(JWKParameter ...$params) |
59
|
|
|
{ |
60
|
13 |
|
parent::__construct(...$params); |
61
|
13 |
|
foreach (self::MANAGED_PARAMS as $name) { |
62
|
13 |
|
if (!$this->has($name)) { |
63
|
1 |
|
throw new \UnexpectedValueException( |
64
|
13 |
|
"Missing '{$name}' parameter."); |
65
|
|
|
} |
66
|
|
|
} |
67
|
12 |
|
if (KeyTypeParameter::TYPE_RSA !== $this->keyTypeParameter()->value()) { |
68
|
1 |
|
throw new \UnexpectedValueException('Invalid key type.'); |
69
|
|
|
} |
70
|
|
|
// cast private exponent to correct class |
71
|
11 |
|
$key = JWKParameter::PARAM_PRIVATE_EXPONENT; |
72
|
11 |
|
$this->_parameters[$key] = new PrivateExponentParameter( |
73
|
11 |
|
$this->_parameters[$key]->value()); |
74
|
11 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Initialize from RSAPrivateKey. |
78
|
|
|
*/ |
79
|
3 |
|
public static function fromRSAPrivateKey(RSAPrivateKey $pk): self |
80
|
|
|
{ |
81
|
3 |
|
$n = ModulusParameter::fromNumber($pk->modulus()); |
82
|
3 |
|
$e = ExponentParameter::fromNumber($pk->publicExponent()); |
83
|
3 |
|
$d = PrivateExponentParameter::fromNumber($pk->privateExponent()); |
84
|
3 |
|
$p = FirstPrimeFactorParameter::fromNumber($pk->prime1()); |
85
|
3 |
|
$q = SecondPrimeFactorParameter::fromNumber($pk->prime2()); |
86
|
3 |
|
$dp = FirstFactorCRTExponentParameter::fromNumber($pk->exponent1()); |
87
|
3 |
|
$dq = SecondFactorCRTExponentParameter::fromNumber($pk->exponent2()); |
88
|
3 |
|
$qi = FirstCRTCoefficientParameter::fromNumber($pk->coefficient()); |
89
|
3 |
|
$key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_RSA); |
90
|
3 |
|
return new self($key_type, $n, $e, $d, $p, $q, $dp, $dq, $qi); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Initialize from PEM. |
95
|
|
|
*/ |
96
|
1 |
|
public static function fromPEM(PEM $pem): self |
97
|
|
|
{ |
98
|
1 |
|
return self::fromRSAPrivateKey(RSAPrivateKey::fromPEM($pem)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get public key component. |
103
|
|
|
* |
104
|
|
|
* @return RSAPublicKeyJWK |
105
|
|
|
*/ |
106
|
26 |
|
public function publicKey(): PublicKeyJWK |
107
|
|
|
{ |
108
|
26 |
|
$kty = $this->keyTypeParameter(); |
109
|
26 |
|
$n = $this->modulusParameter(); |
110
|
26 |
|
$e = $this->exponentParameter(); |
111
|
26 |
|
return new RSAPublicKeyJWK($kty, $n, $e); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Convert JWK to PEM. |
116
|
|
|
*/ |
117
|
16 |
|
public function toPEM(): PEM |
118
|
|
|
{ |
119
|
16 |
|
$n = $this->modulusParameter()->number()->base10(); |
120
|
16 |
|
$e = $this->exponentParameter()->number()->base10(); |
121
|
16 |
|
$d = $this->privateExponentParameter()->number()->base10(); |
122
|
16 |
|
$p = $this->firstPrimeFactorParameter()->number()->base10(); |
123
|
16 |
|
$q = $this->secondPrimeFactorParameter()->number()->base10(); |
124
|
16 |
|
$dp = $this->firstFactorCRTExponentParameter()->number()->base10(); |
125
|
16 |
|
$dq = $this->secondFactorCRTExponentParameter()->number()->base10(); |
126
|
16 |
|
$qi = $this->firstCRTCoefficientParameter()->number()->base10(); |
127
|
16 |
|
$pk = new RSAPrivateKey($n, $e, $d, $p, $q, $dp, $dq, $qi); |
128
|
16 |
|
return PrivateKeyInfo::fromPrivateKey($pk)->toPEM(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|