1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JWX\JWS; |
4
|
|
|
|
5
|
|
|
use JWX\JWK\JWK; |
6
|
|
|
use JWX\JWT\Header\Header; |
7
|
|
|
use JWX\JWT\Header\JOSE; |
8
|
|
|
use JWX\JWT\Parameter\CriticalParameter; |
9
|
|
|
use JWX\JWT\Parameter\RegisteredJWTParameter; |
10
|
|
|
use JWX\Util\Base64; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class to represent JWS structure. |
15
|
|
|
* |
16
|
|
|
* @link https://tools.ietf.org/html/rfc7515#section-3 |
17
|
|
|
*/ |
18
|
|
|
class JWS |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Protected header. |
22
|
|
|
* |
23
|
|
|
* @var Header $_protectedHeader |
24
|
|
|
*/ |
25
|
|
|
protected $_protectedHeader; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Payload. |
29
|
|
|
* |
30
|
|
|
* @var string $_payload |
31
|
|
|
*/ |
32
|
|
|
protected $_payload; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Input value for the signature computation. |
36
|
|
|
* |
37
|
|
|
* @var string $_signatureInput |
38
|
|
|
*/ |
39
|
|
|
protected $_signatureInput; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Signature. |
43
|
|
|
* |
44
|
|
|
* @var string $_signature |
45
|
|
|
*/ |
46
|
|
|
protected $_signature; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor |
50
|
|
|
* |
51
|
|
|
* @param Header $protected_header JWS Protected Header |
52
|
|
|
* @param string $payload JWS Payload |
53
|
|
|
* @param string $signature_input Input value for the signature computation |
54
|
|
|
* @param string $signature JWS Signature |
55
|
|
|
*/ |
56
|
23 |
|
protected function __construct(Header $protected_header, $payload, |
57
|
|
|
$signature_input, $signature) { |
58
|
23 |
|
$this->_protectedHeader = $protected_header; |
59
|
23 |
|
$this->_payload = $payload; |
60
|
23 |
|
$this->_signatureInput = $signature_input; |
61
|
23 |
|
$this->_signature = $signature; |
62
|
23 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Initialize from a compact serialization. |
66
|
|
|
* |
67
|
|
|
* @param string $data |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
6 |
|
public static function fromCompact($data) { |
71
|
6 |
|
return self::fromParts(explode(".", $data)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Initialize from the parts of a compact serialization. |
76
|
|
|
* |
77
|
|
|
* @param array $parts |
78
|
|
|
* @throws \UnexpectedValueException |
79
|
|
|
* @return self |
80
|
|
|
*/ |
81
|
11 |
|
public static function fromParts(array $parts) { |
82
|
11 |
|
if (count($parts) != 3) { |
83
|
1 |
|
throw new \UnexpectedValueException( |
84
|
1 |
|
"Invalid JWS compact serialization."); |
85
|
|
|
} |
86
|
10 |
|
$header = Header::fromJSON(Base64::urlDecode($parts[0])); |
87
|
10 |
|
$b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
88
|
10 |
|
$payload = $b64 ? Base64::urlDecode($parts[1]) : $parts[1]; |
89
|
10 |
|
$signature_input = $parts[0] . "." . $parts[1]; |
90
|
10 |
|
$signature = Base64::urlDecode($parts[2]); |
91
|
10 |
|
return new self($header, $payload, $signature_input, $signature); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Initialize by signing the payload with given algorithm. |
96
|
|
|
* |
97
|
|
|
* @param string $payload JWS Payload |
98
|
|
|
* @param SignatureAlgorithm $algo Signature algorithm |
99
|
|
|
* @param Header|null $header Desired header. Algorithm specific |
100
|
|
|
* parameters are added automatically. |
101
|
|
|
* @throws \RuntimeException If signature computation fails |
102
|
|
|
* @return self |
103
|
|
|
*/ |
104
|
14 |
|
public static function sign($payload, SignatureAlgorithm $algo, |
105
|
|
|
Header $header = null) { |
106
|
14 |
|
if (!isset($header)) { |
107
|
7 |
|
$header = new Header(); |
108
|
7 |
|
} |
109
|
14 |
|
$header = $header->withParameters(...$algo->headerParameters()); |
110
|
|
|
// ensure that if b64 parameter is used, it's marked critical |
111
|
14 |
|
if ($header->hasB64Payload()) { |
112
|
2 |
|
if (!$header->hasCritical()) { |
113
|
1 |
|
$crit = new CriticalParameter(RegisteredJWTParameter::P_B64); |
114
|
1 |
|
} else { |
115
|
1 |
|
$crit = $header->critical()->withParamName( |
116
|
1 |
|
RegisteredJWTParameter::P_B64); |
117
|
|
|
} |
118
|
2 |
|
$header = $header->withParameters($crit); |
119
|
2 |
|
} |
120
|
14 |
|
$signature_input = self::_generateSignatureInput($payload, $header); |
121
|
14 |
|
$signature = $algo->computeSignature($signature_input); |
122
|
14 |
|
return new self($header, $payload, $signature_input, $signature); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get JOSE header. |
127
|
|
|
* |
128
|
|
|
* @return JOSE |
129
|
|
|
*/ |
130
|
13 |
|
public function header() { |
131
|
13 |
|
return new JOSE($this->_protectedHeader); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the signature algorithm name. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
11 |
|
public function algorithmName() { |
140
|
11 |
|
return $this->header() |
141
|
11 |
|
->algorithm() |
142
|
11 |
|
->value(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the payload. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
4 |
|
public function payload() { |
151
|
4 |
|
return $this->_payload; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get the signature. |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
5 |
|
public function signature() { |
160
|
5 |
|
return $this->_signature; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the payload encoded for serialization. |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
12 |
|
protected function _encodedPayload() { |
169
|
12 |
|
$b64 = true; |
170
|
12 |
|
if ($this->_protectedHeader->hasB64Payload()) { |
171
|
1 |
|
$b64 = $this->_protectedHeader->B64Payload()->value(); |
172
|
1 |
|
} |
173
|
12 |
|
return $b64 ? Base64::urlEncode($this->_payload) : $this->_payload; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Validate signature. |
178
|
|
|
* |
179
|
|
|
* @param SignatureAlgorithm $algo |
180
|
|
|
* @throws \UnexpectedValueException If using different signature algorithm |
181
|
|
|
* then specified by the header |
182
|
|
|
* @throws \RuntimeException If signature computation fails |
183
|
|
|
* @return bool True if signature is valid |
184
|
|
|
*/ |
185
|
10 |
|
public function validate(SignatureAlgorithm $algo) { |
186
|
10 |
|
if ($algo->algorithmParamValue() != $this->algorithmName()) { |
187
|
1 |
|
throw new \UnexpectedValueException("Invalid signature algorithm."); |
188
|
|
|
} |
189
|
9 |
|
return $algo->validateSignature($this->_signatureInput, |
190
|
9 |
|
$this->_signature); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Validate signature using given JWK. |
195
|
|
|
* |
196
|
|
|
* Signature algorithm is determined from the header. |
197
|
|
|
* |
198
|
|
|
* @param JWK $jwk JSON Web Key |
199
|
|
|
* @return bool True if signature is valid |
200
|
|
|
*/ |
201
|
1 |
|
public function validateWithJWK(JWK $jwk) { |
202
|
1 |
|
$algo = SignatureAlgorithm::fromJWK($jwk, $this->header()); |
203
|
1 |
|
return $this->validate($algo); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Convert to compact serialization. |
208
|
|
|
* |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
12 |
|
public function toCompact() { |
212
|
12 |
|
return Base64::urlEncode($this->_protectedHeader->toJSON()) . "." . |
213
|
12 |
|
$this->_encodedPayload() . "." . |
214
|
12 |
|
Base64::urlEncode($this->_signature); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Convert to compact serialization with payload detached. |
219
|
|
|
* |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
2 |
|
public function toCompactDetached() { |
223
|
2 |
|
return Base64::urlEncode($this->_protectedHeader->toJSON()) . ".." . |
224
|
2 |
|
Base64::urlEncode($this->_signature); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Generate input for the signature computation. |
229
|
|
|
* |
230
|
|
|
* @param string $payload Payload |
231
|
|
|
* @param Header $header Protected header |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
14 |
|
protected static function _generateSignatureInput($payload, Header $header) { |
235
|
14 |
|
$b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
236
|
14 |
|
$data = Base64::urlEncode($header->toJSON()) . "."; |
237
|
14 |
|
$data .= $b64 ? Base64::urlEncode($payload) : $payload; |
238
|
14 |
|
return $data; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Convert JWS to string. |
243
|
|
|
* |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
1 |
|
public function __toString() { |
247
|
1 |
|
return $this->toCompact(); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|