1 | <?php |
||
19 | class JWE |
||
20 | { |
||
21 | /** |
||
22 | * Protected header. |
||
23 | * |
||
24 | * @var Header $_protectedHeader |
||
25 | */ |
||
26 | protected $_protectedHeader; |
||
27 | |||
28 | /** |
||
29 | * Encrypted key. |
||
30 | * |
||
31 | * @var string $_encryptedKey |
||
32 | */ |
||
33 | protected $_encryptedKey; |
||
34 | |||
35 | /** |
||
36 | * Initialization vector. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $_iv; |
||
41 | |||
42 | /** |
||
43 | * Additional authenticated data. |
||
44 | * |
||
45 | * @var string $_aad |
||
46 | */ |
||
47 | protected $_aad; |
||
48 | |||
49 | /** |
||
50 | * Ciphertext. |
||
51 | * |
||
52 | * @var string $_ciphertext |
||
53 | */ |
||
54 | protected $_ciphertext; |
||
55 | |||
56 | /** |
||
57 | * Authentication tag. |
||
58 | * |
||
59 | * @var string $_authenticationTag |
||
60 | */ |
||
61 | protected $_authenticationTag; |
||
62 | |||
63 | /** |
||
64 | * Constructor |
||
65 | * |
||
66 | * @param Header $protected_header JWE Protected Header |
||
67 | * @param string $encrypted_key Encrypted key |
||
68 | * @param string $iv Initialization vector |
||
69 | * @param string $ciphertext Ciphertext |
||
70 | * @param string $auth_tag Authentication tag |
||
71 | * @param string|null $aad Additional authenticated data |
||
72 | */ |
||
73 | 24 | public function __construct(Header $protected_header, $encrypted_key, $iv, |
|
82 | |||
83 | /** |
||
84 | * Initialize from compact serialization. |
||
85 | * |
||
86 | * @param string $data |
||
87 | * @return self |
||
88 | */ |
||
89 | 6 | public static function fromCompact($data) { |
|
92 | |||
93 | /** |
||
94 | * Initialize from parts of compact serialization. |
||
95 | * |
||
96 | * @param array $parts |
||
97 | * @throws \UnexpectedValueException |
||
98 | * @return self |
||
99 | */ |
||
100 | 12 | public static function fromParts(array $parts) { |
|
112 | |||
113 | /** |
||
114 | * Initialize by encrypting the given payload. |
||
115 | * |
||
116 | * @param string $payload Payload |
||
117 | * @param KeyManagementAlgorithm $key_algo Key management algorithm |
||
118 | * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
||
119 | * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
||
120 | * @param Header|null $header Optional desired header. Algorithm specific |
||
121 | * parameters are automatically added. |
||
122 | * @param string|null $cek Optional content encryption key. Randomly |
||
123 | * generated if not set. |
||
124 | * @param string|null $iv Optional initialization vector. Randomly generated |
||
125 | * if not set. |
||
126 | * @throws \RuntimeException If encrypt fails |
||
127 | * @return self |
||
128 | */ |
||
129 | 16 | public static function encrypt($payload, KeyManagementAlgorithm $key_algo, |
|
153 | |||
154 | /** |
||
155 | * Encrypt content with explicit parameters. |
||
156 | * |
||
157 | * @param string $plaintext Plaintext content to encrypt |
||
158 | * @param string $cek Content encryption key |
||
159 | * @param string $iv Initialization vector |
||
160 | * @param KeyManagementAlgorithm $key_algo Key management algorithm |
||
161 | * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
||
162 | * @param Header $header Header |
||
163 | * @throws \UnexpectedValueException |
||
164 | * @return self |
||
165 | */ |
||
166 | 16 | private static function _encryptContent($plaintext, $cek, $iv, |
|
193 | |||
194 | /** |
||
195 | * Decrypt content. |
||
196 | * |
||
197 | * @param KeyManagementAlgorithm $key_algo |
||
198 | * @param ContentEncryptionAlgorithm $enc_algo |
||
199 | * @throws \RuntimeException If decrypt fails |
||
200 | * @return string Plaintext payload |
||
201 | */ |
||
202 | 12 | public function decrypt(KeyManagementAlgorithm $key_algo, |
|
216 | |||
217 | /** |
||
218 | * Decrypt content using given JWK. |
||
219 | * |
||
220 | * Key management and content encryption algorithms are determined from the |
||
221 | * header. |
||
222 | * |
||
223 | * @param JWK $jwk JSON Web Key |
||
224 | * @return string Plaintext payload |
||
225 | */ |
||
226 | 1 | public function decryptWithJWK(JWK $jwk) { |
|
233 | |||
234 | /** |
||
235 | * Get JOSE header. |
||
236 | * |
||
237 | * @return JOSE |
||
238 | */ |
||
239 | 3 | public function header() { |
|
242 | |||
243 | /** |
||
244 | * Get encrypted CEK. |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | 1 | public function encryptedKey() { |
|
251 | |||
252 | /** |
||
253 | * Get initialization vector. |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | 1 | public function initializationVector() { |
|
260 | |||
261 | /** |
||
262 | * Get ciphertext. |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | 1 | public function ciphertext() { |
|
269 | |||
270 | /** |
||
271 | * Get authentication tag. |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | 1 | public function authenticationTag() { |
|
278 | |||
279 | /** |
||
280 | * Convert to compact serialization. |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | 11 | public function toCompact() { |
|
291 | |||
292 | /** |
||
293 | * Convert JWE to string. |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | 1 | public function __toString() { |
|
300 | } |
||
301 |