1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JWX\JWS\Algorithm; |
4
|
|
|
|
5
|
|
|
use JWX\JWA\JWA; |
6
|
|
|
use JWX\JWK\JWK; |
7
|
|
|
use JWX\JWK\RSA\RSAPrivateKeyJWK; |
8
|
|
|
use JWX\JWK\RSA\RSAPublicKeyJWK; |
9
|
|
|
use JWX\JWT\Header\Header; |
10
|
|
|
use JWX\JWT\Parameter\AlgorithmParameter; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Base class for algorithms implementing signature with PKCS #1. |
15
|
|
|
* |
16
|
|
|
* @link https://tools.ietf.org/html/rfc7518#section-3.3 |
17
|
|
|
*/ |
18
|
|
|
abstract class RSASSAPKCS1Algorithm extends OpenSSLSignatureAlgorithm |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Mapping from algorithm name to class name. |
22
|
|
|
* |
23
|
|
|
* @internal |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
const MAP_ALGO_TO_CLASS = array( |
28
|
|
|
/* @formatter:off */ |
29
|
|
|
JWA::ALGO_RS256 => RS256Algorithm::class, |
30
|
|
|
JWA::ALGO_RS384 => RS384Algorithm::class, |
31
|
|
|
JWA::ALGO_RS512 => RS512Algorithm::class |
32
|
|
|
/* @formatter:on */ |
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
|
10 |
|
$this->_publicKey = $pub_key; |
44
|
10 |
|
$this->_privateKey = $priv_key; |
45
|
10 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initialize from a public key. |
49
|
|
|
* |
50
|
|
|
* @param RSAPublicKeyJWK $jwk |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
2 |
|
public static function fromPublicKey(RSAPublicKeyJWK $jwk) { |
54
|
2 |
|
return new static($jwk); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Initialize from a private key. |
59
|
|
|
* |
60
|
|
|
* @param RSAPrivateKeyJWK $jwk |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
8 |
|
public static function fromPrivateKey(RSAPrivateKeyJWK $jwk) { |
64
|
8 |
|
return new static($jwk->publicKey(), $jwk); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
* @param JWK $jwk |
70
|
|
|
* @param Header $header |
71
|
|
|
* @throws \UnexpectedValueException |
72
|
|
|
* @return RSASSAPKCS1Algorithm |
73
|
|
|
*/ |
74
|
5 |
View Code Duplication |
public static function fromJWK(JWK $jwk, Header $header) { |
|
|
|
|
75
|
5 |
|
$alg = JWA::deriveAlgorithmName($header, $jwk); |
76
|
5 |
|
if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
77
|
1 |
|
throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
78
|
|
|
} |
79
|
4 |
|
$cls = self::MAP_ALGO_TO_CLASS[$alg]; |
80
|
4 |
|
if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
81
|
2 |
|
return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
82
|
|
|
} |
83
|
2 |
|
return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function headerParameters() { |
87
|
1 |
|
return array(AlgorithmParameter::fromAlgorithm($this)); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.