1 | <?php |
||
16 | class JWE |
||
17 | { |
||
18 | /** |
||
19 | * Protected header. |
||
20 | * |
||
21 | * @var Header $_protectedHeader |
||
22 | */ |
||
23 | protected $_protectedHeader; |
||
24 | |||
25 | /** |
||
26 | * Encrypted key. |
||
27 | * |
||
28 | * @var string $_encryptedKey |
||
29 | */ |
||
30 | protected $_encryptedKey; |
||
31 | |||
32 | /** |
||
33 | * Initialization vector. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $_iv; |
||
38 | |||
39 | /** |
||
40 | * Additional authenticated data. |
||
41 | * |
||
42 | * @var string $_aad |
||
43 | */ |
||
44 | protected $_aad; |
||
45 | |||
46 | /** |
||
47 | * Ciphertext. |
||
48 | * |
||
49 | * @var string $_ciphertext |
||
50 | */ |
||
51 | protected $_ciphertext; |
||
52 | |||
53 | /** |
||
54 | * Authentication tag. |
||
55 | * |
||
56 | * @var string $_authenticationTag |
||
57 | */ |
||
58 | protected $_authenticationTag; |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | * |
||
63 | * @param Header $protected_header JWE Protected Header |
||
64 | * @param string $encrypted_key Encrypted key |
||
65 | * @param string $iv Initialization vector |
||
66 | * @param string $ciphertext Ciphertext |
||
67 | * @param string $auth_tag Authentication tag |
||
68 | * @param string|null $aad Additional authenticated data |
||
69 | */ |
||
70 | 24 | public function __construct(Header $protected_header, $encrypted_key, $iv, |
|
79 | |||
80 | /** |
||
81 | * Initialize from compact serialization. |
||
82 | * |
||
83 | * @param string $data |
||
84 | * @return self |
||
85 | */ |
||
86 | 6 | public static function fromCompact($data) { |
|
89 | |||
90 | /** |
||
91 | * Initialize from parts of compact serialization. |
||
92 | * |
||
93 | * @param array $parts |
||
94 | * @throws \UnexpectedValueException |
||
95 | * @return self |
||
96 | */ |
||
97 | 12 | public static function fromParts(array $parts) { |
|
109 | |||
110 | /** |
||
111 | * Initialize by encrypting the given payload. |
||
112 | * |
||
113 | * @param string $payload Payload |
||
114 | * @param KeyManagementAlgorithm $key_algo Key management algorithm |
||
115 | * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
||
116 | * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
||
117 | * @param Header|null $header Optional desired header. Algorithm specific |
||
118 | * parameters are automatically added. |
||
119 | * @param string|null $cek Optional content encryption key. Randomly |
||
120 | * generated if not set. |
||
121 | * @param string|null $iv Optional initialization vector. Randomly generated |
||
122 | * if not set. |
||
123 | * @throws \RuntimeException If encrypt fails |
||
124 | * @return self |
||
125 | */ |
||
126 | 15 | public static function encrypt($payload, KeyManagementAlgorithm $key_algo, |
|
150 | |||
151 | /** |
||
152 | * Encrypt content with explicit parameters. |
||
153 | * |
||
154 | * @param string $plaintext Plaintext content to encrypt |
||
155 | * @param string $cek Content encryption key |
||
156 | * @param string $iv Initialization vector |
||
157 | * @param KeyManagementAlgorithm $key_algo Key management algorithm |
||
158 | * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
||
159 | * @param Header $header Header |
||
160 | * @throws \UnexpectedValueException |
||
161 | * @return self |
||
162 | */ |
||
163 | 15 | private static function _encryptContent($plaintext, $cek, $iv, |
|
186 | |||
187 | /** |
||
188 | * Decrypt content. |
||
189 | * |
||
190 | * @param KeyManagementAlgorithm $key_algo |
||
191 | * @param ContentEncryptionAlgorithm $enc_algo |
||
192 | * @throws \RuntimeException If decrypt fails |
||
193 | * @return string Plaintext payload |
||
194 | */ |
||
195 | 11 | public function decrypt(KeyManagementAlgorithm $key_algo, |
|
209 | |||
210 | /** |
||
211 | * Get JOSE header. |
||
212 | * |
||
213 | * @return JOSE |
||
214 | */ |
||
215 | 2 | public function header() { |
|
218 | |||
219 | /** |
||
220 | * Get encrypted CEK. |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | 1 | public function encryptedKey() { |
|
227 | |||
228 | /** |
||
229 | * Get initialization vector. |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | 1 | public function initializationVector() { |
|
236 | |||
237 | /** |
||
238 | * Get ciphertext. |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | 1 | public function ciphertext() { |
|
245 | |||
246 | /** |
||
247 | * Get authentication tag. |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | 1 | public function authenticationTag() { |
|
254 | |||
255 | /** |
||
256 | * Convert to compact serialization. |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | 11 | public function toCompact() { |
|
267 | |||
268 | /** |
||
269 | * Convert JWE to string. |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | 1 | public function __toString() { |
|
276 | } |
||
277 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: