1 | <?php |
||
18 | abstract class AbstractEncryption implements EncryptionInterface |
||
19 | { |
||
20 | // @codingStandardsIgnoreStart |
||
21 | const ENCRYPTION_PADDING = "\x28\xbf\x4e\x5e\x4e\x75\x8a\x41\x64\x00\x4e\x56\xff\xfa\x01\x08\x2e\x2e\x00\xb6\xd0\x68\x3e\x80\x2f\x0c\xa9\xfe\x64\x53\x69\x7a"; |
||
22 | // @codingStandardsIgnoreEnd |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $encryptionKey; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $userEntry; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $ownerEntry; |
||
38 | |||
39 | /** |
||
40 | * @var Permissions |
||
41 | */ |
||
42 | private $userPermissions; |
||
43 | |||
44 | /** |
||
45 | * @param string $permanentFileIdentifier |
||
46 | * @param string $userPassword |
||
47 | * @param string $ownerPassword |
||
48 | * @param Permissions $userPermissions |
||
49 | * @throws UnexpectedValueException |
||
50 | */ |
||
51 | public function __construct( |
||
100 | |||
101 | /** |
||
102 | * Returns an encryption fitting for a specific PDF version. |
||
103 | * |
||
104 | * @param string $pdfVersion |
||
105 | * @param string $permanentFileIdentifier |
||
106 | * @param EncryptionOptions $options |
||
107 | * @return EncryptionInterface |
||
108 | */ |
||
109 | public static function forPdfVersion($pdfVersion, $permanentFileIdentifier, EncryptionOptions $options) |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function writeEncryptDictionary(ObjectWriter $objectWriter) |
||
157 | |||
158 | /** |
||
159 | * Adds additional entries to the encrypt dictionary if required. |
||
160 | * |
||
161 | * @param ObjectWriter $objectWriter |
||
162 | */ |
||
163 | protected function writeAdditionalEncryptDictionaryEntries(ObjectWriter $objectWriter) |
||
166 | |||
167 | /** |
||
168 | * Returns the revision number of the encryption. |
||
169 | * |
||
170 | * @return int |
||
171 | */ |
||
172 | abstract protected function getRevision(); |
||
173 | |||
174 | /** |
||
175 | * Returns the algorithm number of the encryption. |
||
176 | * |
||
177 | * @return int |
||
178 | */ |
||
179 | abstract protected function getAlgorithm(); |
||
180 | |||
181 | /** |
||
182 | * Returns the key length to be used. |
||
183 | * |
||
184 | * The returned value must be either 40 or 128. |
||
185 | * |
||
186 | * @return int |
||
187 | */ |
||
188 | abstract protected function getKeyLength(); |
||
189 | |||
190 | /** |
||
191 | * Computes an individual ecryption key for an object. |
||
192 | * |
||
193 | * @param string $objectNumber |
||
194 | * @param string $generationNumber |
||
195 | * @return string |
||
196 | */ |
||
197 | protected function computeIndividualEncryptionKey($objectNumber, $generationNumber) |
||
206 | |||
207 | /** |
||
208 | * Encodes a given password into latin-1 and performs length check. |
||
209 | * |
||
210 | * @param string $password |
||
211 | * @return string |
||
212 | * @throws UnsupportedPasswordException |
||
213 | */ |
||
214 | private function encodePassword($password) |
||
231 | |||
232 | /** |
||
233 | * Computes the encryption key as defined by algorithm 3.2 in 3.5.2. |
||
234 | * |
||
235 | * @param string $password |
||
236 | * @param int $revision |
||
237 | * @param int $keyLength |
||
238 | * @param string $ownerEntry |
||
239 | * @param int $permissions |
||
240 | * @param string $permanentFileIdentifier |
||
241 | * @param bool $encryptMetadata |
||
242 | * @return string |
||
243 | */ |
||
244 | private function computeEncryptionKey( |
||
274 | |||
275 | /** |
||
276 | * Computes the owner entry as defined by algorithm 3.3 in 3.5.2. |
||
277 | * |
||
278 | * @param string $ownerPassword |
||
279 | * @param string $userPassword |
||
280 | * @param int $revision |
||
281 | * @param int $keyLength |
||
282 | * @return string |
||
283 | */ |
||
284 | private function computeOwnerEntry($ownerPassword, $userPassword, $revision, $keyLength) |
||
306 | |||
307 | /** |
||
308 | * Computes the user entry (rev 2) as defined by algorithm 3.4 in 3.5.2. |
||
309 | * |
||
310 | * @param string $userPassword |
||
311 | * @param string $ownerEntry |
||
312 | * @param int $userPermissionFlags |
||
313 | * @param string $permanentFileIdentifier |
||
314 | * @return string[] |
||
315 | */ |
||
316 | private function computeUserEntryRev2($userPassword, $ownerEntry, $userPermissionFlags, $permanentFileIdentifier) |
||
332 | |||
333 | /** |
||
334 | * Computes the user entry (rev 3 or greater) as defined by algorithm 3.5 in 3.5.2. |
||
335 | * |
||
336 | * @param string $userPassword |
||
337 | * @param int $revision |
||
338 | * @param int $keyLength |
||
339 | * @param string $ownerEntry |
||
340 | * @param int $permissions |
||
341 | * @param string $permanentFileIdentifier |
||
342 | * @return string[] |
||
343 | */ |
||
344 | private function computeUserEntryRev3OrGreater( |
||
370 | |||
371 | /** |
||
372 | * Applies loop RC4 encryption. |
||
373 | * |
||
374 | * @param string $value |
||
375 | * @param string $key |
||
376 | * @param int $keyLength |
||
377 | * @return string |
||
378 | */ |
||
379 | private function applyRc4Loop($value, $key, $keyLength) |
||
393 | } |
||
394 |