1 | <?php |
||
18 | class JWE implements JWT |
||
19 | { |
||
20 | /** |
||
21 | * @var Recipient[] |
||
22 | */ |
||
23 | private $recipients = []; |
||
24 | |||
25 | /** |
||
26 | * @var string|null |
||
27 | */ |
||
28 | private $ciphertext = null; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $iv; |
||
34 | |||
35 | /** |
||
36 | * @var string|null |
||
37 | */ |
||
38 | private $aad = null; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $tag; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private $sharedHeader = []; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | private $sharedProtectedHeader = []; |
||
54 | |||
55 | /** |
||
56 | * @var string|null |
||
57 | */ |
||
58 | private $encodedSharedProtectedHeader = null; |
||
59 | |||
60 | /** |
||
61 | * @var string|null |
||
62 | */ |
||
63 | private $payload = null; |
||
64 | |||
65 | /** |
||
66 | * JWE constructor. |
||
67 | * |
||
68 | * @param string $ciphertext |
||
69 | * @param string $iv |
||
70 | * @param string $tag |
||
71 | * @param null|string $aad |
||
72 | * @param array $sharedHeader |
||
73 | * @param array $sharedProtectedHeader |
||
74 | * @param null|string $encodedSharedProtectedHeader |
||
75 | * @param array $recipients |
||
76 | */ |
||
77 | private function __construct(string $ciphertext, string $iv, string $tag, ?string $aad = null, array $sharedHeader = [], array $sharedProtectedHeader = [], ?string $encodedSharedProtectedHeader = null, array $recipients = []) |
||
88 | |||
89 | /** |
||
90 | * Creates a new JWE object. |
||
91 | * |
||
92 | * @param string $ciphertext |
||
93 | * @param string $iv |
||
94 | * @param string $tag |
||
95 | * @param null|string $aad |
||
96 | * @param array $sharedHeader |
||
97 | * @param array $sharedProtectedHeader |
||
98 | * @param null|string $encodedSharedProtectedHeader |
||
99 | * @param array $recipients |
||
100 | * |
||
101 | * @return JWE |
||
102 | */ |
||
103 | public static function create(string $ciphertext, string $iv, string $tag, ?string $aad = null, array $sharedHeader = [], array $sharedProtectedHeader = [], ?string $encodedSharedProtectedHeader = null, array $recipients = []): self |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function getPayload(): ?string |
||
115 | |||
116 | /** |
||
117 | * Set the payload. |
||
118 | * This method is immutable and a new object will be returned. |
||
119 | * |
||
120 | * @param string $payload |
||
121 | * |
||
122 | * @return JWE |
||
123 | */ |
||
124 | public function withPayload(string $payload): self |
||
131 | |||
132 | /** |
||
133 | * Returns the number of recipients associated with the JWS. |
||
134 | * |
||
135 | * @return int |
||
136 | */ |
||
137 | public function countRecipients(): int |
||
141 | |||
142 | /** |
||
143 | * Returns true is the JWE has already been encrypted. |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function isEncrypted(): bool |
||
151 | |||
152 | /** |
||
153 | * Returns the recipients associated with the JWS. |
||
154 | * |
||
155 | * @return Recipient[] |
||
156 | */ |
||
157 | public function getRecipients(): array |
||
161 | |||
162 | /** |
||
163 | * Returns the recipient object at the given index. |
||
164 | * |
||
165 | * @param int $id |
||
166 | * |
||
167 | * @return Recipient |
||
168 | */ |
||
169 | public function getRecipient(int $id): Recipient |
||
177 | |||
178 | /** |
||
179 | * Returns the ciphertext. This method will return null is the JWE has not yet been encrypted. |
||
180 | * |
||
181 | * @return string|null The cyphertext |
||
182 | */ |
||
183 | public function getCiphertext(): ?string |
||
187 | |||
188 | /** |
||
189 | * Returns the Additional Authentication Data if available. |
||
190 | * |
||
191 | * @return string|null |
||
192 | */ |
||
193 | public function getAAD(): ?string |
||
197 | |||
198 | /** |
||
199 | * Returns the Initialization Vector if available. |
||
200 | * |
||
201 | * @return string|null |
||
202 | */ |
||
203 | public function getIV(): ?string |
||
207 | |||
208 | /** |
||
209 | * Returns the tag if available. |
||
210 | * |
||
211 | * @return string|null |
||
212 | */ |
||
213 | public function getTag(): ?string |
||
217 | |||
218 | /** |
||
219 | * Returns the encoded shared protected header. |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getEncodedSharedProtectedHeader(): string |
||
227 | |||
228 | /** |
||
229 | * Returns the shared protected header. |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | public function getSharedProtectedHeader(): array |
||
237 | |||
238 | /** |
||
239 | * Returns the shared protected header parameter identified by the given key. |
||
240 | * Throws an exception is the the parameter is not available. |
||
241 | * |
||
242 | * @param string $key The key |
||
243 | * |
||
244 | * @return mixed|null |
||
245 | */ |
||
246 | public function getSharedProtectedHeaderParameter(string $key) |
||
254 | |||
255 | /** |
||
256 | * Returns true if the shared protected header has the parameter identified by the given key. |
||
257 | * |
||
258 | * @param string $key The key |
||
259 | * |
||
260 | * @return bool |
||
261 | */ |
||
262 | public function hasSharedProtectedHeaderParameter(string $key): bool |
||
266 | |||
267 | /** |
||
268 | * Returns the shared header. |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | public function getSharedHeader(): array |
||
276 | |||
277 | /** |
||
278 | * Returns the shared header parameter identified by the given key. |
||
279 | * Throws an exception is the the parameter is not available. |
||
280 | * |
||
281 | * @param string $key The key |
||
282 | * |
||
283 | * @return mixed|null |
||
284 | */ |
||
285 | public function getSharedHeaderParameter(string $key) |
||
293 | |||
294 | /** |
||
295 | * Returns true if the shared header has the parameter identified by the given key. |
||
296 | * |
||
297 | * @param string $key The key |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function hasSharedHeaderParameter(string $key): bool |
||
305 | |||
306 | /** |
||
307 | * This method splits the JWE into a list of JWEs. |
||
308 | * It is only useful when the JWE contains more than one recipient (JSON General Serialization). |
||
309 | * |
||
310 | * @return JWE[] |
||
311 | */ |
||
312 | public function split(): array |
||
330 | } |
||
331 |