1 | <?php |
||
20 | class JWE |
||
21 | { |
||
22 | /** |
||
23 | * Protected header. |
||
24 | * |
||
25 | * @var Header $_protectedHeader |
||
26 | */ |
||
27 | protected $_protectedHeader; |
||
28 | |||
29 | /** |
||
30 | * Encrypted key. |
||
31 | * |
||
32 | * @var string $_encryptedKey |
||
33 | */ |
||
34 | protected $_encryptedKey; |
||
35 | |||
36 | /** |
||
37 | * Initialization vector. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $_iv; |
||
42 | |||
43 | /** |
||
44 | * Additional authenticated data. |
||
45 | * |
||
46 | * @var string $_aad |
||
47 | */ |
||
48 | protected $_aad; |
||
49 | |||
50 | /** |
||
51 | * Ciphertext. |
||
52 | * |
||
53 | * @var string $_ciphertext |
||
54 | */ |
||
55 | protected $_ciphertext; |
||
56 | |||
57 | /** |
||
58 | * Authentication tag. |
||
59 | * |
||
60 | * @var string $_authenticationTag |
||
61 | */ |
||
62 | protected $_authenticationTag; |
||
63 | |||
64 | /** |
||
65 | * Constructor |
||
66 | * |
||
67 | * @param Header $protected_header JWE Protected Header |
||
68 | * @param string $encrypted_key Encrypted key |
||
69 | * @param string $iv Initialization vector |
||
70 | * @param string $ciphertext Ciphertext |
||
71 | * @param string $auth_tag Authentication tag |
||
72 | * @param string|null $aad Additional authenticated data |
||
73 | */ |
||
74 | 25 | public function __construct(Header $protected_header, $encrypted_key, $iv, |
|
83 | |||
84 | /** |
||
85 | * Initialize from compact serialization. |
||
86 | * |
||
87 | * @param string $data |
||
88 | * @return self |
||
89 | */ |
||
90 | 6 | public static function fromCompact($data) { |
|
93 | |||
94 | /** |
||
95 | * Initialize from parts of compact serialization. |
||
96 | * |
||
97 | * @param array $parts |
||
98 | * @throws \UnexpectedValueException |
||
99 | * @return self |
||
100 | */ |
||
101 | 13 | public static function fromParts(array $parts) { |
|
113 | |||
114 | /** |
||
115 | * Initialize by encrypting the given payload. |
||
116 | * |
||
117 | * @param string $payload Payload |
||
118 | * @param KeyManagementAlgorithm $key_algo Key management algorithm |
||
119 | * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
||
120 | * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
||
121 | * @param Header|null $header Optional desired header. Algorithm specific |
||
122 | * parameters are automatically added. |
||
123 | * @param string|null $cek Optional content encryption key. Randomly |
||
124 | * generated if not set. |
||
125 | * @param string|null $iv Optional initialization vector. Randomly generated |
||
126 | * if not set. |
||
127 | * @throws \RuntimeException If encrypt fails |
||
128 | * @return self |
||
129 | */ |
||
130 | 16 | public static function encrypt($payload, KeyManagementAlgorithm $key_algo, |
|
154 | |||
155 | /** |
||
156 | * Encrypt content with explicit parameters. |
||
157 | * |
||
158 | * @param string $plaintext Plaintext content to encrypt |
||
159 | * @param string $cek Content encryption key |
||
160 | * @param string $iv Initialization vector |
||
161 | * @param KeyManagementAlgorithm $key_algo Key management algorithm |
||
162 | * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
||
163 | * @param Header $header Header |
||
164 | * @throws \UnexpectedValueException |
||
165 | * @return self |
||
166 | */ |
||
167 | 16 | private static function _encryptContent($plaintext, $cek, $iv, |
|
194 | |||
195 | /** |
||
196 | * Decrypt the content using explicit algorithms. |
||
197 | * |
||
198 | * @param KeyManagementAlgorithm $key_algo |
||
199 | * @param ContentEncryptionAlgorithm $enc_algo |
||
200 | * @throws \RuntimeException If decrypt fails |
||
201 | * @return string Plaintext payload |
||
202 | */ |
||
203 | 15 | public function decrypt(KeyManagementAlgorithm $key_algo, |
|
229 | |||
230 | /** |
||
231 | * Decrypt content using given JWK. |
||
232 | * |
||
233 | * Key management and content encryption algorithms are determined from the |
||
234 | * header. |
||
235 | * |
||
236 | * @param JWK $jwk JSON Web Key |
||
237 | * @return string Plaintext payload |
||
238 | */ |
||
239 | 2 | public function decryptWithJWK(JWK $jwk) { |
|
245 | |||
246 | /** |
||
247 | * Decrypt content using a key from the given JWK set. |
||
248 | * |
||
249 | * Correct key shall be sought by the key ID indicated by the header. |
||
250 | * |
||
251 | * @param JWKSet $set Set of JSON Web Keys |
||
252 | * @return string Plaintext payload |
||
253 | */ |
||
254 | 4 | public function decryptWithJWKSet(JWKSet $set) { |
|
261 | |||
262 | /** |
||
263 | * Get JOSE header. |
||
264 | * |
||
265 | * @return JOSE |
||
266 | */ |
||
267 | 18 | public function header() { |
|
270 | |||
271 | /** |
||
272 | * Get the name of the key management algorithm. |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | 15 | public function algorithmName() { |
|
281 | |||
282 | /** |
||
283 | * Get the name of the encryption algorithm. |
||
284 | * |
||
285 | * @return string |
||
286 | */ |
||
287 | 14 | public function encryptionAlgorithmName() { |
|
292 | |||
293 | /** |
||
294 | * Get encrypted CEK. |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | 1 | public function encryptedKey() { |
|
301 | |||
302 | /** |
||
303 | * Get initialization vector. |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | 1 | public function initializationVector() { |
|
310 | |||
311 | /** |
||
312 | * Get ciphertext. |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | 1 | public function ciphertext() { |
|
319 | |||
320 | /** |
||
321 | * Get authentication tag. |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | 1 | public function authenticationTag() { |
|
328 | |||
329 | /** |
||
330 | * Convert to compact serialization. |
||
331 | * |
||
332 | * @return string |
||
333 | */ |
||
334 | 11 | public function toCompact() { |
|
341 | |||
342 | /** |
||
343 | * Convert JWE to string. |
||
344 | * |
||
345 | * @return string |
||
346 | */ |
||
347 | 1 | public function __toString() { |
|
350 | } |
||
351 |