@@ -20,138 +20,138 @@ |
||
20 | 20 | */ |
21 | 21 | abstract class ECDSAAlgorithm extends OpenSSLSignatureAlgorithm |
22 | 22 | { |
23 | - /** |
|
24 | - * Mapping from algorithm name to class name. |
|
25 | - * |
|
26 | - * @internal |
|
27 | - * |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - const MAP_ALGO_TO_CLASS = array( |
|
31 | - /* @formatter:off */ |
|
32 | - JWA::ALGO_ES256 => ES256Algorithm::class, |
|
33 | - JWA::ALGO_ES384 => ES384Algorithm::class, |
|
34 | - JWA::ALGO_ES512 => ES512Algorithm::class |
|
35 | - /* @formatter:on */ |
|
36 | - ); |
|
23 | + /** |
|
24 | + * Mapping from algorithm name to class name. |
|
25 | + * |
|
26 | + * @internal |
|
27 | + * |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + const MAP_ALGO_TO_CLASS = array( |
|
31 | + /* @formatter:off */ |
|
32 | + JWA::ALGO_ES256 => ES256Algorithm::class, |
|
33 | + JWA::ALGO_ES384 => ES384Algorithm::class, |
|
34 | + JWA::ALGO_ES512 => ES512Algorithm::class |
|
35 | + /* @formatter:on */ |
|
36 | + ); |
|
37 | 37 | |
38 | - /** |
|
39 | - * Signature size in bytes. |
|
40 | - * |
|
41 | - * @var int |
|
42 | - */ |
|
43 | - private $_signatureSize; |
|
38 | + /** |
|
39 | + * Signature size in bytes. |
|
40 | + * |
|
41 | + * @var int |
|
42 | + */ |
|
43 | + private $_signatureSize; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Get the name of the curve used by this algorithm. |
|
47 | - * |
|
48 | - * @return string |
|
49 | - */ |
|
50 | - abstract protected function _curveName(): string; |
|
45 | + /** |
|
46 | + * Get the name of the curve used by this algorithm. |
|
47 | + * |
|
48 | + * @return string |
|
49 | + */ |
|
50 | + abstract protected function _curveName(): string; |
|
51 | 51 | |
52 | - /** |
|
53 | - * Constructor. |
|
54 | - * |
|
55 | - * @param ECPublicKeyJWK $pub_key |
|
56 | - * @param ECPrivateKeyJWK $priv_key |
|
57 | - */ |
|
58 | - protected function __construct(ECPublicKeyJWK $pub_key, |
|
59 | - ECPrivateKeyJWK $priv_key = null) |
|
60 | - { |
|
61 | - $curve = $pub_key->curveParameter()->value(); |
|
62 | - if ($this->_curveName() != $curve) { |
|
63 | - throw new \InvalidArgumentException( |
|
64 | - "Key with " . $this->_curveName() . |
|
65 | - " curve expected, got $curve."); |
|
66 | - } |
|
67 | - $this->_publicKey = $pub_key; |
|
68 | - $this->_privateKey = $priv_key; |
|
69 | - $key_size = $pub_key->curveParameter()->keySizeBits(); |
|
70 | - $this->_signatureSize = intval(ceil($key_size / 8) * 2); |
|
71 | - } |
|
52 | + /** |
|
53 | + * Constructor. |
|
54 | + * |
|
55 | + * @param ECPublicKeyJWK $pub_key |
|
56 | + * @param ECPrivateKeyJWK $priv_key |
|
57 | + */ |
|
58 | + protected function __construct(ECPublicKeyJWK $pub_key, |
|
59 | + ECPrivateKeyJWK $priv_key = null) |
|
60 | + { |
|
61 | + $curve = $pub_key->curveParameter()->value(); |
|
62 | + if ($this->_curveName() != $curve) { |
|
63 | + throw new \InvalidArgumentException( |
|
64 | + "Key with " . $this->_curveName() . |
|
65 | + " curve expected, got $curve."); |
|
66 | + } |
|
67 | + $this->_publicKey = $pub_key; |
|
68 | + $this->_privateKey = $priv_key; |
|
69 | + $key_size = $pub_key->curveParameter()->keySizeBits(); |
|
70 | + $this->_signatureSize = intval(ceil($key_size / 8) * 2); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Initialize from a public key. |
|
75 | - * |
|
76 | - * @param ECPublicKeyJWK $jwk |
|
77 | - * @return self |
|
78 | - */ |
|
79 | - public static function fromPublicKey(ECPublicKeyJWK $jwk): self |
|
80 | - { |
|
81 | - return new static($jwk); |
|
82 | - } |
|
73 | + /** |
|
74 | + * Initialize from a public key. |
|
75 | + * |
|
76 | + * @param ECPublicKeyJWK $jwk |
|
77 | + * @return self |
|
78 | + */ |
|
79 | + public static function fromPublicKey(ECPublicKeyJWK $jwk): self |
|
80 | + { |
|
81 | + return new static($jwk); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Initialize from a private key. |
|
86 | - * |
|
87 | - * @param ECPrivateKeyJWK $jwk |
|
88 | - * @return self |
|
89 | - */ |
|
90 | - public static function fromPrivateKey(ECPrivateKeyJWK $jwk): self |
|
91 | - { |
|
92 | - return new static($jwk->publicKey(), $jwk); |
|
93 | - } |
|
84 | + /** |
|
85 | + * Initialize from a private key. |
|
86 | + * |
|
87 | + * @param ECPrivateKeyJWK $jwk |
|
88 | + * @return self |
|
89 | + */ |
|
90 | + public static function fromPrivateKey(ECPrivateKeyJWK $jwk): self |
|
91 | + { |
|
92 | + return new static($jwk->publicKey(), $jwk); |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * |
|
97 | - * {@inheritdoc} |
|
98 | - */ |
|
99 | - public static function fromJWK(JWK $jwk, Header $header): self |
|
100 | - { |
|
101 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
102 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
103 | - throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
104 | - } |
|
105 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
106 | - if ($jwk->has(...ECPrivateKeyJWK::MANAGED_PARAMS)) { |
|
107 | - return $cls::fromPrivateKey(ECPrivateKeyJWK::fromJWK($jwk)); |
|
108 | - } |
|
109 | - return $cls::fromPublicKey(ECPublicKeyJWK::fromJWK($jwk)); |
|
110 | - } |
|
95 | + /** |
|
96 | + * |
|
97 | + * {@inheritdoc} |
|
98 | + */ |
|
99 | + public static function fromJWK(JWK $jwk, Header $header): self |
|
100 | + { |
|
101 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
102 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
103 | + throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
104 | + } |
|
105 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
106 | + if ($jwk->has(...ECPrivateKeyJWK::MANAGED_PARAMS)) { |
|
107 | + return $cls::fromPrivateKey(ECPrivateKeyJWK::fromJWK($jwk)); |
|
108 | + } |
|
109 | + return $cls::fromPublicKey(ECPublicKeyJWK::fromJWK($jwk)); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * |
|
114 | - * @see \JWX\JWS\Algorithm\OpenSSLSignatureAlgorithm::computeSignature() |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public function computeSignature(string $data): string |
|
118 | - { |
|
119 | - // OpenSSL returns ECDSA signature as a DER encoded ECDSA-Sig-Value |
|
120 | - $der = parent::computeSignature($data); |
|
121 | - $sig = ECSignature::fromDER($der); |
|
122 | - $mlen = intval(floor($this->_signatureSize / 2)); |
|
123 | - $signature = ECConversion::numberToOctets($sig->r(), $mlen) . |
|
124 | - ECConversion::numberToOctets($sig->s(), $mlen); |
|
125 | - return $signature; |
|
126 | - } |
|
112 | + /** |
|
113 | + * |
|
114 | + * @see \JWX\JWS\Algorithm\OpenSSLSignatureAlgorithm::computeSignature() |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public function computeSignature(string $data): string |
|
118 | + { |
|
119 | + // OpenSSL returns ECDSA signature as a DER encoded ECDSA-Sig-Value |
|
120 | + $der = parent::computeSignature($data); |
|
121 | + $sig = ECSignature::fromDER($der); |
|
122 | + $mlen = intval(floor($this->_signatureSize / 2)); |
|
123 | + $signature = ECConversion::numberToOctets($sig->r(), $mlen) . |
|
124 | + ECConversion::numberToOctets($sig->s(), $mlen); |
|
125 | + return $signature; |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * |
|
130 | - * @see \JWX\JWS\Algorithm\OpenSSLSignatureAlgorithm::validateSignature() |
|
131 | - * @return bool |
|
132 | - */ |
|
133 | - public function validateSignature(string $data, string $signature): bool |
|
134 | - { |
|
135 | - if (strlen($signature) != $this->_signatureSize) { |
|
136 | - throw new \UnexpectedValueException("Invalid signature length."); |
|
137 | - } |
|
138 | - list($r_octets, $s_octets) = str_split($signature, |
|
139 | - intval(floor($this->_signatureSize / 2))); |
|
140 | - // convert signature to DER sequence for OpenSSL |
|
141 | - $r = ECConversion::octetsToNumber($r_octets); |
|
142 | - $s = ECConversion::octetsToNumber($s_octets); |
|
143 | - $sig = new ECSignature($r, $s); |
|
144 | - return parent::validateSignature($data, $sig->toDER()); |
|
145 | - } |
|
128 | + /** |
|
129 | + * |
|
130 | + * @see \JWX\JWS\Algorithm\OpenSSLSignatureAlgorithm::validateSignature() |
|
131 | + * @return bool |
|
132 | + */ |
|
133 | + public function validateSignature(string $data, string $signature): bool |
|
134 | + { |
|
135 | + if (strlen($signature) != $this->_signatureSize) { |
|
136 | + throw new \UnexpectedValueException("Invalid signature length."); |
|
137 | + } |
|
138 | + list($r_octets, $s_octets) = str_split($signature, |
|
139 | + intval(floor($this->_signatureSize / 2))); |
|
140 | + // convert signature to DER sequence for OpenSSL |
|
141 | + $r = ECConversion::octetsToNumber($r_octets); |
|
142 | + $s = ECConversion::octetsToNumber($s_octets); |
|
143 | + $sig = new ECSignature($r, $s); |
|
144 | + return parent::validateSignature($data, $sig->toDER()); |
|
145 | + } |
|
146 | 146 | |
147 | - /** |
|
148 | - * |
|
149 | - * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
150 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
151 | - */ |
|
152 | - public function headerParameters(): array |
|
153 | - { |
|
154 | - return array_merge(parent::headerParameters(), |
|
155 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
156 | - } |
|
147 | + /** |
|
148 | + * |
|
149 | + * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
150 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
151 | + */ |
|
152 | + public function headerParameters(): array |
|
153 | + { |
|
154 | + return array_merge(parent::headerParameters(), |
|
155 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
156 | + } |
|
157 | 157 | } |
@@ -13,21 +13,21 @@ |
||
13 | 13 | */ |
14 | 14 | class RS256Algorithm extends RSASSAPKCS1Algorithm |
15 | 15 | { |
16 | - /** |
|
17 | - * |
|
18 | - * {@inheritdoc} |
|
19 | - */ |
|
20 | - protected function _mdMethod(): string |
|
21 | - { |
|
22 | - return "sha256WithRSAEncryption"; |
|
23 | - } |
|
16 | + /** |
|
17 | + * |
|
18 | + * {@inheritdoc} |
|
19 | + */ |
|
20 | + protected function _mdMethod(): string |
|
21 | + { |
|
22 | + return "sha256WithRSAEncryption"; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * |
|
27 | - * {@inheritdoc} |
|
28 | - */ |
|
29 | - public function algorithmParamValue(): string |
|
30 | - { |
|
31 | - return JWA::ALGO_RS256; |
|
32 | - } |
|
25 | + /** |
|
26 | + * |
|
27 | + * {@inheritdoc} |
|
28 | + */ |
|
29 | + public function algorithmParamValue(): string |
|
30 | + { |
|
31 | + return JWA::ALGO_RS256; |
|
32 | + } |
|
33 | 33 | } |
@@ -14,30 +14,30 @@ |
||
14 | 14 | */ |
15 | 15 | class ES256Algorithm extends ECDSAAlgorithm |
16 | 16 | { |
17 | - /** |
|
18 | - * |
|
19 | - * {@inheritdoc} |
|
20 | - */ |
|
21 | - protected function _curveName(): string |
|
22 | - { |
|
23 | - return CurveParameter::CURVE_P256; |
|
24 | - } |
|
17 | + /** |
|
18 | + * |
|
19 | + * {@inheritdoc} |
|
20 | + */ |
|
21 | + protected function _curveName(): string |
|
22 | + { |
|
23 | + return CurveParameter::CURVE_P256; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * |
|
28 | - * {@inheritdoc} |
|
29 | - */ |
|
30 | - protected function _mdMethod(): string |
|
31 | - { |
|
32 | - return "sha256"; |
|
33 | - } |
|
26 | + /** |
|
27 | + * |
|
28 | + * {@inheritdoc} |
|
29 | + */ |
|
30 | + protected function _mdMethod(): string |
|
31 | + { |
|
32 | + return "sha256"; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * |
|
37 | - * {@inheritdoc} |
|
38 | - */ |
|
39 | - public function algorithmParamValue(): string |
|
40 | - { |
|
41 | - return JWA::ALGO_ES256; |
|
42 | - } |
|
35 | + /** |
|
36 | + * |
|
37 | + * {@inheritdoc} |
|
38 | + */ |
|
39 | + public function algorithmParamValue(): string |
|
40 | + { |
|
41 | + return JWA::ALGO_ES256; |
|
42 | + } |
|
43 | 43 | } |
@@ -14,30 +14,30 @@ |
||
14 | 14 | */ |
15 | 15 | class ES384Algorithm extends ECDSAAlgorithm |
16 | 16 | { |
17 | - /** |
|
18 | - * |
|
19 | - * {@inheritdoc} |
|
20 | - */ |
|
21 | - protected function _curveName(): string |
|
22 | - { |
|
23 | - return CurveParameter::CURVE_P384; |
|
24 | - } |
|
17 | + /** |
|
18 | + * |
|
19 | + * {@inheritdoc} |
|
20 | + */ |
|
21 | + protected function _curveName(): string |
|
22 | + { |
|
23 | + return CurveParameter::CURVE_P384; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * |
|
28 | - * {@inheritdoc} |
|
29 | - */ |
|
30 | - protected function _mdMethod(): string |
|
31 | - { |
|
32 | - return "sha384"; |
|
33 | - } |
|
26 | + /** |
|
27 | + * |
|
28 | + * {@inheritdoc} |
|
29 | + */ |
|
30 | + protected function _mdMethod(): string |
|
31 | + { |
|
32 | + return "sha384"; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * |
|
37 | - * {@inheritdoc} |
|
38 | - */ |
|
39 | - public function algorithmParamValue(): string |
|
40 | - { |
|
41 | - return JWA::ALGO_ES384; |
|
42 | - } |
|
35 | + /** |
|
36 | + * |
|
37 | + * {@inheritdoc} |
|
38 | + */ |
|
39 | + public function algorithmParamValue(): string |
|
40 | + { |
|
41 | + return JWA::ALGO_ES384; |
|
42 | + } |
|
43 | 43 | } |
@@ -13,21 +13,21 @@ |
||
13 | 13 | */ |
14 | 14 | class HS512Algorithm extends HMACAlgorithm |
15 | 15 | { |
16 | - /** |
|
17 | - * |
|
18 | - * {@inheritdoc} |
|
19 | - */ |
|
20 | - protected function _hashAlgo(): string |
|
21 | - { |
|
22 | - return "sha512"; |
|
23 | - } |
|
16 | + /** |
|
17 | + * |
|
18 | + * {@inheritdoc} |
|
19 | + */ |
|
20 | + protected function _hashAlgo(): string |
|
21 | + { |
|
22 | + return "sha512"; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * |
|
27 | - * {@inheritdoc} |
|
28 | - */ |
|
29 | - public function algorithmParamValue(): string |
|
30 | - { |
|
31 | - return JWA::ALGO_HS512; |
|
32 | - } |
|
25 | + /** |
|
26 | + * |
|
27 | + * {@inheritdoc} |
|
28 | + */ |
|
29 | + public function algorithmParamValue(): string |
|
30 | + { |
|
31 | + return JWA::ALGO_HS512; |
|
32 | + } |
|
33 | 33 | } |
@@ -15,89 +15,89 @@ |
||
15 | 15 | */ |
16 | 16 | class SignatureAlgorithmFactory |
17 | 17 | { |
18 | - /** |
|
19 | - * Header. |
|
20 | - * |
|
21 | - * @var Header $_header |
|
22 | - */ |
|
23 | - protected $_header; |
|
18 | + /** |
|
19 | + * Header. |
|
20 | + * |
|
21 | + * @var Header $_header |
|
22 | + */ |
|
23 | + protected $_header; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Mapping from algorithm name to class name. |
|
27 | - * |
|
28 | - * @internal |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - const MAP_ALGO_TO_CLASS = array( |
|
33 | - /* @formatter:off */ |
|
34 | - JWA::ALGO_HS256 => HS256Algorithm::class, |
|
35 | - JWA::ALGO_HS384 => HS384Algorithm::class, |
|
36 | - JWA::ALGO_HS512 => HS512Algorithm::class, |
|
37 | - JWA::ALGO_RS256 => RS256Algorithm::class, |
|
38 | - JWA::ALGO_RS384 => RS384Algorithm::class, |
|
39 | - JWA::ALGO_RS512 => RS512Algorithm::class, |
|
40 | - JWA::ALGO_ES256 => ES256Algorithm::class, |
|
41 | - JWA::ALGO_ES384 => ES384Algorithm::class, |
|
42 | - JWA::ALGO_ES512 => ES512Algorithm::class |
|
43 | - /* @formatter:on */ |
|
44 | - ); |
|
25 | + /** |
|
26 | + * Mapping from algorithm name to class name. |
|
27 | + * |
|
28 | + * @internal |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + const MAP_ALGO_TO_CLASS = array( |
|
33 | + /* @formatter:off */ |
|
34 | + JWA::ALGO_HS256 => HS256Algorithm::class, |
|
35 | + JWA::ALGO_HS384 => HS384Algorithm::class, |
|
36 | + JWA::ALGO_HS512 => HS512Algorithm::class, |
|
37 | + JWA::ALGO_RS256 => RS256Algorithm::class, |
|
38 | + JWA::ALGO_RS384 => RS384Algorithm::class, |
|
39 | + JWA::ALGO_RS512 => RS512Algorithm::class, |
|
40 | + JWA::ALGO_ES256 => ES256Algorithm::class, |
|
41 | + JWA::ALGO_ES384 => ES384Algorithm::class, |
|
42 | + JWA::ALGO_ES512 => ES512Algorithm::class |
|
43 | + /* @formatter:on */ |
|
44 | + ); |
|
45 | 45 | |
46 | - /** |
|
47 | - * Constructor. |
|
48 | - * |
|
49 | - * @param Header $header |
|
50 | - */ |
|
51 | - public function __construct(Header $header) |
|
52 | - { |
|
53 | - $this->_header = $header; |
|
54 | - } |
|
46 | + /** |
|
47 | + * Constructor. |
|
48 | + * |
|
49 | + * @param Header $header |
|
50 | + */ |
|
51 | + public function __construct(Header $header) |
|
52 | + { |
|
53 | + $this->_header = $header; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get signature algorithm by given JWK. |
|
58 | - * |
|
59 | - * @param JWK $jwk |
|
60 | - * @return SignatureAlgorithm |
|
61 | - */ |
|
62 | - public function algoByKey(JWK $jwk): SignatureAlgorithm |
|
63 | - { |
|
64 | - $alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
|
65 | - $cls = self::_algoClassByName($alg); |
|
66 | - return $cls::fromJWK($jwk, $this->_header); |
|
67 | - } |
|
56 | + /** |
|
57 | + * Get signature algorithm by given JWK. |
|
58 | + * |
|
59 | + * @param JWK $jwk |
|
60 | + * @return SignatureAlgorithm |
|
61 | + */ |
|
62 | + public function algoByKey(JWK $jwk): SignatureAlgorithm |
|
63 | + { |
|
64 | + $alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
|
65 | + $cls = self::_algoClassByName($alg); |
|
66 | + return $cls::fromJWK($jwk, $this->_header); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Get signature algorithm using a matching key from given JWK set. |
|
71 | - * |
|
72 | - * @param JWKSet $set |
|
73 | - * @throws \UnexpectedValueException If a key cannot be found |
|
74 | - * @return SignatureAlgorithm |
|
75 | - */ |
|
76 | - public function algoByKeys(JWKSet $set): SignatureAlgorithm |
|
77 | - { |
|
78 | - if (!$this->_header->hasKeyID()) { |
|
79 | - throw new \UnexpectedValueException("No key ID paremeter."); |
|
80 | - } |
|
81 | - $id = $this->_header->keyID()->value(); |
|
82 | - if (!$set->hasKeyID($id)) { |
|
83 | - throw new \UnexpectedValueException("No key for ID '$id'."); |
|
84 | - } |
|
85 | - return $this->algoByKey($set->keyByID($id)); |
|
86 | - } |
|
69 | + /** |
|
70 | + * Get signature algorithm using a matching key from given JWK set. |
|
71 | + * |
|
72 | + * @param JWKSet $set |
|
73 | + * @throws \UnexpectedValueException If a key cannot be found |
|
74 | + * @return SignatureAlgorithm |
|
75 | + */ |
|
76 | + public function algoByKeys(JWKSet $set): SignatureAlgorithm |
|
77 | + { |
|
78 | + if (!$this->_header->hasKeyID()) { |
|
79 | + throw new \UnexpectedValueException("No key ID paremeter."); |
|
80 | + } |
|
81 | + $id = $this->_header->keyID()->value(); |
|
82 | + if (!$set->hasKeyID($id)) { |
|
83 | + throw new \UnexpectedValueException("No key for ID '$id'."); |
|
84 | + } |
|
85 | + return $this->algoByKey($set->keyByID($id)); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Get the algorithm implementation class name by an algorithm name. |
|
90 | - * |
|
91 | - * @param string $alg Algorithm name |
|
92 | - * @throws \UnexpectedValueException |
|
93 | - * @return string Class name |
|
94 | - */ |
|
95 | - private static function _algoClassByName(string $alg): string |
|
96 | - { |
|
97 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
98 | - throw new \UnexpectedValueException( |
|
99 | - "Algorithm '$alg' not supported."); |
|
100 | - } |
|
101 | - return self::MAP_ALGO_TO_CLASS[$alg]; |
|
102 | - } |
|
88 | + /** |
|
89 | + * Get the algorithm implementation class name by an algorithm name. |
|
90 | + * |
|
91 | + * @param string $alg Algorithm name |
|
92 | + * @throws \UnexpectedValueException |
|
93 | + * @return string Class name |
|
94 | + */ |
|
95 | + private static function _algoClassByName(string $alg): string |
|
96 | + { |
|
97 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
98 | + throw new \UnexpectedValueException( |
|
99 | + "Algorithm '$alg' not supported."); |
|
100 | + } |
|
101 | + return self::MAP_ALGO_TO_CLASS[$alg]; |
|
102 | + } |
|
103 | 103 | } |
@@ -13,21 +13,21 @@ |
||
13 | 13 | */ |
14 | 14 | class RS512Algorithm extends RSASSAPKCS1Algorithm |
15 | 15 | { |
16 | - /** |
|
17 | - * |
|
18 | - * {@inheritdoc} |
|
19 | - */ |
|
20 | - protected function _mdMethod(): string |
|
21 | - { |
|
22 | - return "sha512WithRSAEncryption"; |
|
23 | - } |
|
16 | + /** |
|
17 | + * |
|
18 | + * {@inheritdoc} |
|
19 | + */ |
|
20 | + protected function _mdMethod(): string |
|
21 | + { |
|
22 | + return "sha512WithRSAEncryption"; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * |
|
27 | - * {@inheritdoc} |
|
28 | - */ |
|
29 | - public function algorithmParamValue(): string |
|
30 | - { |
|
31 | - return JWA::ALGO_RS512; |
|
32 | - } |
|
25 | + /** |
|
26 | + * |
|
27 | + * {@inheritdoc} |
|
28 | + */ |
|
29 | + public function algorithmParamValue(): string |
|
30 | + { |
|
31 | + return JWA::ALGO_RS512; |
|
32 | + } |
|
33 | 33 | } |
@@ -13,21 +13,21 @@ |
||
13 | 13 | */ |
14 | 14 | class RS384Algorithm extends RSASSAPKCS1Algorithm |
15 | 15 | { |
16 | - /** |
|
17 | - * |
|
18 | - * {@inheritdoc} |
|
19 | - */ |
|
20 | - protected function _mdMethod(): string |
|
21 | - { |
|
22 | - return "sha384WithRSAEncryption"; |
|
23 | - } |
|
16 | + /** |
|
17 | + * |
|
18 | + * {@inheritdoc} |
|
19 | + */ |
|
20 | + protected function _mdMethod(): string |
|
21 | + { |
|
22 | + return "sha384WithRSAEncryption"; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * |
|
27 | - * {@inheritdoc} |
|
28 | - */ |
|
29 | - public function algorithmParamValue(): string |
|
30 | - { |
|
31 | - return JWA::ALGO_RS384; |
|
32 | - } |
|
25 | + /** |
|
26 | + * |
|
27 | + * {@inheritdoc} |
|
28 | + */ |
|
29 | + public function algorithmParamValue(): string |
|
30 | + { |
|
31 | + return JWA::ALGO_RS384; |
|
32 | + } |
|
33 | 33 | } |
@@ -18,84 +18,84 @@ |
||
18 | 18 | */ |
19 | 19 | abstract class RSASSAPKCS1Algorithm extends OpenSSLSignatureAlgorithm |
20 | 20 | { |
21 | - /** |
|
22 | - * Mapping from algorithm name to class name. |
|
23 | - * |
|
24 | - * @internal |
|
25 | - * |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - const MAP_ALGO_TO_CLASS = array( |
|
29 | - /* @formatter:off */ |
|
30 | - JWA::ALGO_RS256 => RS256Algorithm::class, |
|
31 | - JWA::ALGO_RS384 => RS384Algorithm::class, |
|
32 | - JWA::ALGO_RS512 => RS512Algorithm::class |
|
33 | - /* @formatter:on */ |
|
34 | - ); |
|
21 | + /** |
|
22 | + * Mapping from algorithm name to class name. |
|
23 | + * |
|
24 | + * @internal |
|
25 | + * |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + const MAP_ALGO_TO_CLASS = array( |
|
29 | + /* @formatter:off */ |
|
30 | + JWA::ALGO_RS256 => RS256Algorithm::class, |
|
31 | + JWA::ALGO_RS384 => RS384Algorithm::class, |
|
32 | + JWA::ALGO_RS512 => RS512Algorithm::class |
|
33 | + /* @formatter:on */ |
|
34 | + ); |
|
35 | 35 | |
36 | - /** |
|
37 | - * Constructor. |
|
38 | - * |
|
39 | - * @param RSAPublicKeyJWK $pub_key |
|
40 | - * @param RSAPrivateKeyJWK $priv_key |
|
41 | - */ |
|
42 | - protected function __construct(RSAPublicKeyJWK $pub_key, |
|
43 | - RSAPrivateKeyJWK $priv_key = null) |
|
44 | - { |
|
45 | - $this->_publicKey = $pub_key; |
|
46 | - $this->_privateKey = $priv_key; |
|
47 | - } |
|
36 | + /** |
|
37 | + * Constructor. |
|
38 | + * |
|
39 | + * @param RSAPublicKeyJWK $pub_key |
|
40 | + * @param RSAPrivateKeyJWK $priv_key |
|
41 | + */ |
|
42 | + protected function __construct(RSAPublicKeyJWK $pub_key, |
|
43 | + RSAPrivateKeyJWK $priv_key = null) |
|
44 | + { |
|
45 | + $this->_publicKey = $pub_key; |
|
46 | + $this->_privateKey = $priv_key; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Initialize from a public key. |
|
51 | - * |
|
52 | - * @param RSAPublicKeyJWK $jwk |
|
53 | - * @return self |
|
54 | - */ |
|
55 | - public static function fromPublicKey(RSAPublicKeyJWK $jwk): self |
|
56 | - { |
|
57 | - return new static($jwk); |
|
58 | - } |
|
49 | + /** |
|
50 | + * Initialize from a public key. |
|
51 | + * |
|
52 | + * @param RSAPublicKeyJWK $jwk |
|
53 | + * @return self |
|
54 | + */ |
|
55 | + public static function fromPublicKey(RSAPublicKeyJWK $jwk): self |
|
56 | + { |
|
57 | + return new static($jwk); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Initialize from a private key. |
|
62 | - * |
|
63 | - * @param RSAPrivateKeyJWK $jwk |
|
64 | - * @return self |
|
65 | - */ |
|
66 | - public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self |
|
67 | - { |
|
68 | - return new static($jwk->publicKey(), $jwk); |
|
69 | - } |
|
60 | + /** |
|
61 | + * Initialize from a private key. |
|
62 | + * |
|
63 | + * @param RSAPrivateKeyJWK $jwk |
|
64 | + * @return self |
|
65 | + */ |
|
66 | + public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self |
|
67 | + { |
|
68 | + return new static($jwk->publicKey(), $jwk); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * |
|
73 | - * @param JWK $jwk |
|
74 | - * @param Header $header |
|
75 | - * @throws \UnexpectedValueException |
|
76 | - * @return RSASSAPKCS1Algorithm |
|
77 | - */ |
|
78 | - public static function fromJWK(JWK $jwk, Header $header): self |
|
79 | - { |
|
80 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
81 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
82 | - throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
83 | - } |
|
84 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
85 | - if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
86 | - return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
87 | - } |
|
88 | - return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
89 | - } |
|
71 | + /** |
|
72 | + * |
|
73 | + * @param JWK $jwk |
|
74 | + * @param Header $header |
|
75 | + * @throws \UnexpectedValueException |
|
76 | + * @return RSASSAPKCS1Algorithm |
|
77 | + */ |
|
78 | + public static function fromJWK(JWK $jwk, Header $header): self |
|
79 | + { |
|
80 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
81 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
82 | + throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
83 | + } |
|
84 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
85 | + if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
86 | + return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
87 | + } |
|
88 | + return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * |
|
93 | - * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
94 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
95 | - */ |
|
96 | - public function headerParameters(): array |
|
97 | - { |
|
98 | - return array_merge(parent::headerParameters(), |
|
99 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
100 | - } |
|
91 | + /** |
|
92 | + * |
|
93 | + * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
94 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
95 | + */ |
|
96 | + public function headerParameters(): array |
|
97 | + { |
|
98 | + return array_merge(parent::headerParameters(), |
|
99 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
100 | + } |
|
101 | 101 | } |