|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWS\Algorithm; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\JWX\JWA\JWA; |
|
8
|
|
|
use Sop\JWX\JWK\JWK; |
|
9
|
|
|
use Sop\JWX\JWK\RSA\RSAPrivateKeyJWK; |
|
10
|
|
|
use Sop\JWX\JWK\RSA\RSAPublicKeyJWK; |
|
11
|
|
|
use Sop\JWX\JWS\SignatureAlgorithm; |
|
12
|
|
|
use Sop\JWX\JWT\Header\Header; |
|
13
|
|
|
use Sop\JWX\JWT\Parameter\AlgorithmParameter; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Base class for algorithms implementing signature with PKCS #1. |
|
17
|
|
|
* |
|
18
|
|
|
* @see https://tools.ietf.org/html/rfc7518#section-3.3 |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class RSASSAPKCS1Algorithm extends OpenSSLSignatureAlgorithm |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Mapping from algorithm name to class name. |
|
24
|
|
|
* |
|
25
|
|
|
* @internal |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
const MAP_ALGO_TO_CLASS = [ |
|
30
|
|
|
JWA::ALGO_RS256 => RS256Algorithm::class, |
|
31
|
|
|
JWA::ALGO_RS384 => RS384Algorithm::class, |
|
32
|
|
|
JWA::ALGO_RS512 => RS512Algorithm::class, |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param RSAPublicKeyJWK $pub_key |
|
39
|
|
|
* @param RSAPrivateKeyJWK $priv_key |
|
40
|
|
|
*/ |
|
41
|
10 |
|
protected function __construct(RSAPublicKeyJWK $pub_key, |
|
42
|
|
|
RSAPrivateKeyJWK $priv_key = null) |
|
43
|
|
|
{ |
|
44
|
10 |
|
$this->_publicKey = $pub_key; |
|
45
|
10 |
|
$this->_privateKey = $priv_key; |
|
46
|
10 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Initialize from a public key. |
|
50
|
|
|
* |
|
51
|
|
|
* @param RSAPublicKeyJWK $jwk |
|
52
|
|
|
* |
|
53
|
|
|
* @return self |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public static function fromPublicKey(RSAPublicKeyJWK $jwk): self |
|
56
|
|
|
{ |
|
57
|
2 |
|
return new static($jwk); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Initialize from a private key. |
|
62
|
|
|
* |
|
63
|
|
|
* @param RSAPrivateKeyJWK $jwk |
|
64
|
|
|
* |
|
65
|
|
|
* @return self |
|
66
|
|
|
*/ |
|
67
|
8 |
|
public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self |
|
68
|
|
|
{ |
|
69
|
8 |
|
return new static($jwk->publicKey(), $jwk); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
* |
|
75
|
|
|
* @return self |
|
76
|
|
|
*/ |
|
77
|
5 |
|
public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
78
|
|
|
{ |
|
79
|
5 |
|
$alg = JWA::deriveAlgorithmName($header, $jwk); |
|
80
|
5 |
|
if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
81
|
1 |
|
throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
82
|
|
|
} |
|
83
|
4 |
|
$cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
84
|
4 |
|
if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
85
|
2 |
|
return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
86
|
|
|
} |
|
87
|
2 |
|
return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
2 |
|
public function headerParameters(): array |
|
94
|
|
|
{ |
|
95
|
2 |
|
return array_merge(parent::headerParameters(), |
|
96
|
2 |
|
[AlgorithmParameter::fromAlgorithm($this)]); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|