/src/qpdf/libqpdf/QPDF_encryption.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // This file implements methods from the QPDF class that involve |
2 | | // encryption. |
3 | | |
4 | | #include <qpdf/assert_debug.h> |
5 | | |
6 | | #include <qpdf/QPDF_private.hh> |
7 | | |
8 | | #include <qpdf/QPDFExc.hh> |
9 | | |
10 | | #include <qpdf/MD5.hh> |
11 | | #include <qpdf/Pl_AES_PDF.hh> |
12 | | #include <qpdf/Pl_Buffer.hh> |
13 | | #include <qpdf/Pl_RC4.hh> |
14 | | #include <qpdf/Pl_SHA2.hh> |
15 | | #include <qpdf/QPDFObjectHandle_private.hh> |
16 | | #include <qpdf/QTC.hh> |
17 | | #include <qpdf/QUtil.hh> |
18 | | #include <qpdf/RC4.hh> |
19 | | #include <qpdf/Util.hh> |
20 | | |
21 | | #include <algorithm> |
22 | | #include <cstring> |
23 | | |
24 | | using namespace qpdf; |
25 | | using namespace std::literals; |
26 | | |
27 | | static std::string padding_string = |
28 | | "\x28\xbf\x4e\x5e\x4e\x75\x8a\x41\x64\x00\x4e\x56\xff\xfa\x01\x08" |
29 | | "\x2e\x2e\x00\xb6\xd0\x68\x3e\x80\x2f\x0c\xa9\xfe\x64\x53\x69\x7a"s; |
30 | | |
31 | | static unsigned int const key_bytes = 32; |
32 | | |
33 | | static unsigned int const OU_key_bytes_V5 = 48; |
34 | | static unsigned int const OUE_key_bytes_V5 = 32; |
35 | | static unsigned int const Perms_key_bytes_V5 = 16; |
36 | | |
37 | | int |
38 | | QPDF::EncryptionData::getV() const |
39 | 1.37k | { |
40 | 1.37k | return this->V; |
41 | 1.37k | } |
42 | | |
43 | | int |
44 | | QPDF::EncryptionData::getR() const |
45 | 2.38k | { |
46 | 2.38k | return this->R; |
47 | 2.38k | } |
48 | | |
49 | | int |
50 | | QPDF::EncryptionData::getLengthBytes() const |
51 | 819 | { |
52 | 819 | return this->Length_bytes; |
53 | 819 | } |
54 | | |
55 | | int |
56 | | QPDF::EncryptionData::getP() const |
57 | 439 | { |
58 | 439 | return static_cast<int>(P.to_ulong()); |
59 | 439 | } |
60 | | |
61 | | bool |
62 | | QPDF::EncryptionData::getP(size_t bit) const |
63 | 0 | { |
64 | 0 | qpdf_assert_debug(bit); |
65 | 0 | return P.test(bit - 1); |
66 | 0 | } |
67 | | |
68 | | bool |
69 | | QPDF::EncryptionParameters::P(size_t bit) const |
70 | 0 | { |
71 | 0 | qpdf_assert_debug(bit); |
72 | 0 | return P_.test(bit - 1); |
73 | 0 | } |
74 | | |
75 | | std::string const& |
76 | | QPDF::EncryptionData::getO() const |
77 | 1.17k | { |
78 | 1.17k | return this->O; |
79 | 1.17k | } |
80 | | |
81 | | std::string const& |
82 | | QPDF::EncryptionData::getU() const |
83 | 1.71k | { |
84 | 1.71k | return this->U; |
85 | 1.71k | } |
86 | | |
87 | | std::string const& |
88 | | QPDF::EncryptionData::getOE() const |
89 | 17 | { |
90 | 17 | return this->OE; |
91 | 17 | } |
92 | | |
93 | | std::string const& |
94 | | QPDF::EncryptionData::getUE() const |
95 | 142 | { |
96 | 142 | return this->UE; |
97 | 142 | } |
98 | | |
99 | | std::string const& |
100 | | QPDF::EncryptionData::getPerms() const |
101 | 159 | { |
102 | 159 | return this->Perms; |
103 | 159 | } |
104 | | |
105 | | std::string const& |
106 | | QPDF::EncryptionData::getId1() const |
107 | 535 | { |
108 | 535 | return this->id1; |
109 | 535 | } |
110 | | |
111 | | bool |
112 | | QPDF::EncryptionData::getEncryptMetadata() const |
113 | 416 | { |
114 | 416 | return this->encrypt_metadata; |
115 | 416 | } |
116 | | |
117 | | void |
118 | | QPDF::EncryptionData::setO(std::string const& O) |
119 | 0 | { |
120 | 0 | this->O = O; |
121 | 0 | } |
122 | | |
123 | | void |
124 | | QPDF::EncryptionData::setU(std::string const& U) |
125 | 0 | { |
126 | 0 | this->U = U; |
127 | 0 | } |
128 | | |
129 | | void |
130 | | QPDF::EncryptionData::setP(size_t bit, bool val) |
131 | 0 | { |
132 | 0 | qpdf_assert_debug(bit); |
133 | 0 | P.set(bit - 1, val); |
134 | 0 | } |
135 | | |
136 | | void |
137 | | QPDF::EncryptionData::setP(unsigned long val) |
138 | 0 | { |
139 | 0 | P = std::bitset<32>(val); |
140 | 0 | } |
141 | | |
142 | | void |
143 | | QPDF::EncryptionData::setId1(std::string const& val) |
144 | 0 | { |
145 | 0 | id1 = val; |
146 | 0 | } |
147 | | |
148 | | void |
149 | | QPDF::EncryptionData::setV5EncryptionParameters( |
150 | | std::string const& O, |
151 | | std::string const& OE, |
152 | | std::string const& U, |
153 | | std::string const& UE, |
154 | | std::string const& Perms) |
155 | 0 | { |
156 | 0 | this->O = O; |
157 | 0 | this->OE = OE; |
158 | 0 | this->U = U; |
159 | 0 | this->UE = UE; |
160 | 0 | this->Perms = Perms; |
161 | 0 | } |
162 | | |
163 | | void |
164 | | QPDF::trim_user_password(std::string& user_password) |
165 | 1 | { |
166 | | // Although unnecessary, this routine trims the padding string from the end of a user password. |
167 | | // Its only purpose is for recovery of user passwords which is done in the test suite. |
168 | 1 | if (user_password.size() < key_bytes) { |
169 | 0 | return; |
170 | 0 | } |
171 | | |
172 | 1 | auto idx = user_password.find('\x28'); |
173 | | |
174 | 1 | while (idx != user_password.npos) { |
175 | 0 | if (padding_string.starts_with(user_password.substr(idx))) { |
176 | 0 | user_password.resize(idx); |
177 | 0 | return; |
178 | 0 | } |
179 | 0 | QTC::TC("qpdf", "QPDF_encryption skip 0x28"); |
180 | 0 | idx = user_password.find('\x28', ++idx); |
181 | 0 | } |
182 | 1 | } |
183 | | |
184 | | static std::string |
185 | | pad_or_truncate_password_V4(std::string password) |
186 | 415 | { |
187 | 415 | if (password.size() < key_bytes) { |
188 | 279 | password.append(padding_string); |
189 | 279 | } |
190 | 415 | password.resize(key_bytes); |
191 | 415 | return password; |
192 | 415 | } |
193 | | |
194 | | static std::string |
195 | | iterate_md5_digest(MD5& md5, int iterations, int key_len) |
196 | 415 | { |
197 | 415 | MD5::Digest digest; |
198 | 415 | md5.digest(digest); |
199 | 415 | auto len = std::min(QIntC::to_size(key_len), sizeof(digest)); |
200 | 20.0k | for (int i = 0; i < iterations; ++i) { |
201 | 19.6k | MD5 m; |
202 | 19.6k | m.encodeDataIncrementally(reinterpret_cast<char*>(digest), len); |
203 | 19.6k | m.digest(digest); |
204 | 19.6k | } |
205 | 415 | return {reinterpret_cast<char*>(digest), len}; |
206 | 415 | } |
207 | | |
208 | | static void |
209 | | iterate_rc4(std::string& data, std::string_view okey, int iterations, bool reverse) |
210 | 404 | { |
211 | 404 | auto len = okey.size(); |
212 | 404 | std::string key(len, '\0'); |
213 | 8.08k | for (int i = 0; i < iterations; ++i) { |
214 | 7.68k | int const xor_value = (reverse ? iterations - 1 - i : i); |
215 | 130k | for (size_t j = 0; j < len; ++j) { |
216 | 122k | key[j] = static_cast<char>(okey[j] ^ xor_value); |
217 | 122k | } |
218 | 7.68k | RC4::process(key, data); |
219 | 7.68k | } |
220 | 404 | } |
221 | | |
222 | | static std::string |
223 | | process_with_aes( |
224 | | std::string const& key, |
225 | | bool encrypt, |
226 | | std::string const& data, |
227 | | size_t outlength = 0, |
228 | | unsigned int repetitions = 1, |
229 | | unsigned char const* iv = nullptr, |
230 | | size_t iv_length = 0) |
231 | 69.2k | { |
232 | 69.2k | Pl_Buffer buffer("buffer"); |
233 | 69.2k | Pl_AES_PDF aes( |
234 | 69.2k | "aes", &buffer, encrypt, QUtil::unsigned_char_pointer(key), QIntC::to_uint(key.length())); |
235 | 69.2k | if (iv) { |
236 | 68.9k | aes.setIV(iv, iv_length); |
237 | 68.9k | } else { |
238 | 318 | aes.useZeroIV(); |
239 | 318 | } |
240 | 69.2k | aes.disablePadding(); |
241 | 4.47M | for (unsigned int i = 0; i < repetitions; ++i) { |
242 | 4.41M | aes.writeString(data); |
243 | 4.41M | } |
244 | 69.2k | aes.finish(); |
245 | 69.2k | if (outlength == 0) { |
246 | 69.2k | return buffer.getString(); |
247 | 69.2k | } else { |
248 | 0 | return buffer.getString().substr(0, outlength); |
249 | 0 | } |
250 | 69.2k | } |
251 | | |
252 | | std::string |
253 | | QPDF::EncryptionData::hash_V5( |
254 | | std::string const& password, std::string const& salt, std::string const& udata) const |
255 | 1.02k | { |
256 | 1.02k | Pl_SHA2 hash(256); |
257 | 1.02k | hash.writeString(password); |
258 | 1.02k | hash.writeString(salt); |
259 | 1.02k | hash.writeString(udata); |
260 | 1.02k | hash.finish(); |
261 | 1.02k | std::string K = hash.getRawDigest(); |
262 | | |
263 | 1.02k | std::string result; |
264 | 1.02k | if (getR() < 6) { |
265 | 39 | result = K; |
266 | 981 | } else { |
267 | | // Algorithm 2.B from ISO 32000-1 chapter 7: Computing a hash |
268 | | |
269 | 981 | int round_number = 0; |
270 | 981 | bool done = false; |
271 | 69.8k | while (!done) { |
272 | | // The hash algorithm has us setting K initially to the R5 value and then repeating a |
273 | | // series of steps 64 times before starting with the termination case testing. The |
274 | | // wording of the specification is very unclear as to the exact number of times it |
275 | | // should be run since the wording about whether the initial setup counts as round 0 or |
276 | | // not is ambiguous. This code counts the initial setup (R5) value as round 0, which |
277 | | // appears to be correct. This was determined to be correct by increasing or decreasing |
278 | | // the number of rounds by 1 or 2 from this value and generating 20 test files. In this |
279 | | // interpretation, all the test files worked with Adobe Reader X. In the other |
280 | | // configurations, many of the files did not work, and we were accurately able to |
281 | | // predict which files didn't work by looking at the conditions under which we |
282 | | // terminated repetition. |
283 | | |
284 | 68.9k | ++round_number; |
285 | 68.9k | std::string K1 = password + K + udata; |
286 | 68.9k | qpdf_assert_debug(K.length() >= 32); |
287 | 68.9k | std::string E = process_with_aes( |
288 | 68.9k | K.substr(0, 16), |
289 | 68.9k | true, |
290 | 68.9k | K1, |
291 | 68.9k | 0, |
292 | 68.9k | 64, |
293 | 68.9k | QUtil::unsigned_char_pointer(K.substr(16, 16)), |
294 | 68.9k | 16); |
295 | | |
296 | | // E_mod_3 is supposed to be mod 3 of the first 16 bytes of E taken as as a (128-bit) |
297 | | // big-endian number. Since (xy mod n) is equal to ((x mod n) + (y mod n)) mod n and |
298 | | // since 256 mod n is 1, we can just take the sums of the the mod 3s of each byte to get |
299 | | // the same result. |
300 | 68.9k | int E_mod_3 = 0; |
301 | 1.17M | for (unsigned int i = 0; i < 16; ++i) { |
302 | 1.10M | E_mod_3 += static_cast<unsigned char>(E.at(i)); |
303 | 1.10M | } |
304 | 68.9k | E_mod_3 %= 3; |
305 | 68.9k | int next_hash = ((E_mod_3 == 0) ? 256 : (E_mod_3 == 1) ? 384 : 512); |
306 | 68.9k | Pl_SHA2 sha2(next_hash); |
307 | 68.9k | sha2.writeString(E); |
308 | 68.9k | sha2.finish(); |
309 | 68.9k | K = sha2.getRawDigest(); |
310 | | |
311 | 68.9k | if (round_number >= 64) { |
312 | 7.10k | unsigned int ch = static_cast<unsigned char>(*(E.rbegin())); |
313 | | |
314 | 7.10k | if (ch <= QIntC::to_uint(round_number - 32)) { |
315 | 981 | done = true; |
316 | 981 | } |
317 | 7.10k | } |
318 | 68.9k | } |
319 | 981 | result = K.substr(0, 32); |
320 | 981 | } |
321 | | |
322 | 1.02k | return result; |
323 | 1.02k | } |
324 | | |
325 | | static void |
326 | | pad_short_parameter(std::string& param, size_t max_len) |
327 | 2.08k | { |
328 | 2.08k | if (param.length() < max_len) { |
329 | 202 | QTC::TC("qpdf", "QPDF_encryption pad short parameter"); |
330 | 202 | param.append(max_len - param.length(), '\0'); |
331 | 202 | } |
332 | 2.08k | } |
333 | | |
334 | | std::string |
335 | | QPDF::compute_data_key( |
336 | | std::string const& encryption_key, |
337 | | int objid, |
338 | | int generation, |
339 | | bool use_aes, |
340 | | int encryption_V, |
341 | | int encryption_R) |
342 | 186 | { |
343 | | // Algorithm 3.1 from the PDF 1.7 Reference Manual |
344 | | |
345 | 186 | std::string result = encryption_key; |
346 | | |
347 | 186 | if (encryption_V >= 5) { |
348 | | // Algorithm 3.1a (PDF 1.7 extension level 3): just use encryption key straight. |
349 | 83 | return result; |
350 | 83 | } |
351 | | |
352 | | // Append low three bytes of object ID and low two bytes of generation |
353 | 103 | result.append(1, static_cast<char>(objid & 0xff)); |
354 | 103 | result.append(1, static_cast<char>((objid >> 8) & 0xff)); |
355 | 103 | result.append(1, static_cast<char>((objid >> 16) & 0xff)); |
356 | 103 | result.append(1, static_cast<char>(generation & 0xff)); |
357 | 103 | result.append(1, static_cast<char>((generation >> 8) & 0xff)); |
358 | 103 | if (use_aes) { |
359 | 7 | result += "sAlT"; |
360 | 7 | } |
361 | 103 | return MD5::digest(result).substr(0, result.size()); |
362 | 186 | } |
363 | | |
364 | | std::string |
365 | | QPDF::compute_encryption_key(std::string const& password, EncryptionData const& data) |
366 | 0 | { |
367 | 0 | return data.compute_encryption_key(password); |
368 | 0 | } |
369 | | |
370 | | std::string |
371 | | QPDF::EncryptionData::compute_encryption_key(std::string const& password) const |
372 | 280 | { |
373 | 280 | if (getV() >= 5) { |
374 | | // For V >= 5, the encryption key is generated and stored in the file, encrypted separately |
375 | | // with both user and owner passwords. |
376 | 0 | return recover_encryption_key_with_password(password); |
377 | 280 | } else { |
378 | | // For V < 5, the encryption key is derived from the user |
379 | | // password. |
380 | 280 | return compute_encryption_key_from_password(password); |
381 | 280 | } |
382 | 280 | } |
383 | | |
384 | | std::string |
385 | | QPDF::EncryptionData::compute_encryption_key_from_password(std::string const& password) const |
386 | 280 | { |
387 | | // Algorithm 3.2 from the PDF 1.7 Reference Manual |
388 | | |
389 | | // This code does not properly handle Unicode passwords. Passwords are supposed to be converted |
390 | | // from OS codepage characters to PDFDocEncoding. Unicode passwords are supposed to be |
391 | | // converted to OS codepage before converting to PDFDocEncoding. We instead require the |
392 | | // password to be presented in its final form. |
393 | | |
394 | 280 | MD5 md5; |
395 | 280 | md5.encodeDataIncrementally(pad_or_truncate_password_V4(password)); |
396 | 280 | md5.encodeDataIncrementally(getO()); |
397 | 280 | char pbytes[4]; |
398 | 280 | int p = getP(); |
399 | 280 | pbytes[0] = static_cast<char>(p & 0xff); |
400 | 280 | pbytes[1] = static_cast<char>((p >> 8) & 0xff); |
401 | 280 | pbytes[2] = static_cast<char>((p >> 16) & 0xff); |
402 | 280 | pbytes[3] = static_cast<char>((p >> 24) & 0xff); |
403 | 280 | md5.encodeDataIncrementally(pbytes, 4); |
404 | 280 | md5.encodeDataIncrementally(getId1()); |
405 | 280 | if (getR() >= 4 && !getEncryptMetadata()) { |
406 | 2 | md5.encodeDataIncrementally("\xff\xff\xff\xff"); |
407 | 2 | } |
408 | 280 | return iterate_md5_digest(md5, (getR() >= 3 ? 50 : 0), getLengthBytes()); |
409 | 280 | } |
410 | | |
411 | | std::string |
412 | | QPDF::EncryptionData::compute_O_rc4_key( |
413 | | std::string const& user_password, std::string const& owner_password) const |
414 | 135 | { |
415 | 135 | if (getV() >= 5) { |
416 | 0 | throw std::logic_error("compute_O_rc4_key called for file with V >= 5"); |
417 | 0 | } |
418 | 135 | std::string password = owner_password.empty() ? user_password : owner_password; |
419 | 135 | MD5 md5; |
420 | 135 | md5.encodeDataIncrementally(pad_or_truncate_password_V4(password)); |
421 | 135 | return iterate_md5_digest(md5, (getR() >= 3 ? 50 : 0), getLengthBytes()); |
422 | 135 | } |
423 | | |
424 | | std::string |
425 | | QPDF::EncryptionData::compute_O_value( |
426 | | std::string const& user_password, std::string const& owner_password) const |
427 | 0 | { |
428 | | // Algorithm 3.3 from the PDF 1.7 Reference Manual |
429 | |
|
430 | 0 | auto upass = pad_or_truncate_password_V4(user_password); |
431 | 0 | std::string O_key = compute_O_rc4_key(user_password, owner_password); |
432 | 0 | pad_short_parameter(O_key, QIntC::to_size(getLengthBytes())); |
433 | 0 | iterate_rc4(upass, O_key, getR() >= 3 ? 20 : 1, false); |
434 | 0 | return upass; |
435 | 0 | } |
436 | | |
437 | | std::string |
438 | | QPDF::EncryptionData::compute_U_value_R2(std::string const& user_password) const |
439 | 14 | { |
440 | | // Algorithm 3.4 from the PDF 1.7 Reference Manual |
441 | | |
442 | 14 | std::string k1 = compute_encryption_key(user_password); |
443 | 14 | auto udata = padding_string; |
444 | 14 | pad_short_parameter(k1, QIntC::to_size(getLengthBytes())); |
445 | 14 | iterate_rc4(udata, k1, 1, false); |
446 | 14 | return udata; |
447 | 14 | } |
448 | | |
449 | | std::string |
450 | | QPDF::EncryptionData::compute_U_value_R3(std::string const& user_password) const |
451 | 255 | { |
452 | | // Algorithm 3.5 from the PDF 1.7 Reference Manual |
453 | | |
454 | 255 | std::string k1 = compute_encryption_key(user_password); |
455 | 255 | MD5 md5; |
456 | 255 | md5.encodeDataIncrementally(padding_string); |
457 | 255 | md5.encodeDataIncrementally(getId1()); |
458 | 255 | auto result = md5.digest(); |
459 | 255 | pad_short_parameter(k1, QIntC::to_size(getLengthBytes())); |
460 | 255 | iterate_rc4(result, k1, 20, false); |
461 | | // pad with arbitrary data -- make it consistent for the sake of testing |
462 | 255 | result += "\x0\x21\x44\x69\x90\xb9\xe4\x11\x40\x71\xa4\xd9\x10\x49\x84\xc1"s; |
463 | 255 | return result; |
464 | 255 | } |
465 | | |
466 | | std::string |
467 | | QPDF::EncryptionData::compute_U_value(std::string const& user_password) const |
468 | 269 | { |
469 | 269 | if (getR() >= 3) { |
470 | 255 | return compute_U_value_R3(user_password); |
471 | 255 | } |
472 | | |
473 | 14 | return compute_U_value_R2(user_password); |
474 | 269 | } |
475 | | |
476 | | bool |
477 | | QPDF::EncryptionData::check_user_password_V4(std::string const& user_password) const |
478 | 269 | { |
479 | | // Algorithm 3.6 from the PDF 1.7 Reference Manual |
480 | | |
481 | 269 | std::string u_value = compute_U_value(user_password); |
482 | 269 | size_t to_compare = (getR() >= 3 ? sizeof(MD5::Digest) : key_bytes); |
483 | 269 | return memcmp(getU().c_str(), u_value.c_str(), to_compare) == 0; |
484 | 269 | } |
485 | | |
486 | | bool |
487 | | QPDF::EncryptionData::check_user_password_V5(std::string const& user_password) const |
488 | 422 | { |
489 | | // Algorithm 3.11 from the PDF 1.7 extension level 3 |
490 | | |
491 | 422 | std::string user_data = getU().substr(0, 32); |
492 | 422 | std::string validation_salt = getU().substr(32, 8); |
493 | 422 | std::string password = user_password.substr(0, 127); |
494 | 422 | return hash_V5(user_password.substr(0, 127), validation_salt, "") == user_data; |
495 | 422 | } |
496 | | |
497 | | bool |
498 | | QPDF::EncryptionData::check_user_password(std::string const& user_password) const |
499 | 549 | { |
500 | 549 | if (getV() < 5) { |
501 | 269 | return check_user_password_V4(user_password); |
502 | 280 | } else { |
503 | 280 | return check_user_password_V5(user_password); |
504 | 280 | } |
505 | 549 | } |
506 | | |
507 | | bool |
508 | | QPDF::EncryptionData::check_owner_password_V4( |
509 | | std::string& user_password, std::string const& owner_password) const |
510 | 135 | { |
511 | | // Algorithm 3.7 from the PDF 1.7 Reference Manual |
512 | | |
513 | 135 | auto key = compute_O_rc4_key(user_password, owner_password); |
514 | 135 | pad_short_parameter(key, QIntC::to_size(getLengthBytes())); |
515 | 135 | auto new_user_password = O.substr(0, key_bytes); |
516 | 135 | iterate_rc4(new_user_password, key, (getR() >= 3) ? 20 : 1, true); |
517 | 135 | if (check_user_password(new_user_password)) { |
518 | 1 | user_password = new_user_password; |
519 | 1 | return true; |
520 | 1 | } |
521 | 134 | return false; |
522 | 135 | } |
523 | | |
524 | | bool |
525 | | QPDF::EncryptionData::check_owner_password_V5(std::string const& owner_password) const |
526 | 439 | { |
527 | | // Algorithm 3.12 from the PDF 1.7 extension level 3 |
528 | | |
529 | 439 | std::string user_data = getU().substr(0, 48); |
530 | 439 | std::string owner_data = getO().substr(0, 32); |
531 | 439 | std::string validation_salt = getO().substr(32, 8); |
532 | 439 | return hash_V5(owner_password.substr(0, 127), validation_salt, user_data) == owner_data; |
533 | 439 | } |
534 | | |
535 | | bool |
536 | | QPDF::EncryptionData::check_owner_password( |
537 | | std::string& user_password, std::string const& owner_password) const |
538 | 415 | { |
539 | 415 | if (getV() < 5) { |
540 | 135 | return check_owner_password_V4(user_password, owner_password); |
541 | 280 | } else { |
542 | 280 | return check_owner_password_V5(owner_password); |
543 | 280 | } |
544 | 415 | } |
545 | | |
546 | | std::string |
547 | | QPDF::EncryptionData::recover_encryption_key_with_password(std::string const& password) const |
548 | 0 | { |
549 | | // Disregard whether Perms is valid. |
550 | 0 | bool disregard; |
551 | 0 | return recover_encryption_key_with_password(password, disregard); |
552 | 0 | } |
553 | | |
554 | | std::string |
555 | | QPDF::EncryptionData::compute_Perms_value_V5_clear() const |
556 | 159 | { |
557 | | // From algorithm 3.10 from the PDF 1.7 extension level 3 |
558 | 159 | std::string k = " \xff\xff\xff\xffTadb "; |
559 | 159 | int perms = getP(); |
560 | 795 | for (size_t i = 0; i < 4; ++i) { |
561 | 636 | k[i] = static_cast<char>(perms & 0xff); |
562 | 636 | perms >>= 8; |
563 | 636 | } |
564 | 159 | if (!getEncryptMetadata()) { |
565 | 0 | k[8] = 'F'; |
566 | 0 | } |
567 | 159 | QUtil::initializeWithRandomBytes(reinterpret_cast<unsigned char*>(&k[12]), 4); |
568 | 159 | return k; |
569 | 159 | } |
570 | | |
571 | | std::string |
572 | | QPDF::EncryptionData::recover_encryption_key_with_password( |
573 | | std::string const& password, bool& perms_valid) const |
574 | 159 | { |
575 | | // Algorithm 3.2a from the PDF 1.7 extension level 3 |
576 | | |
577 | | // This code does not handle Unicode passwords correctly. Empirical evidence suggests that most |
578 | | // viewers don't. We are supposed to process the input string with the SASLprep (RFC 4013) |
579 | | // profile of stringprep (RFC 3454) and then convert the result to UTF-8. |
580 | | |
581 | 159 | perms_valid = false; |
582 | 159 | std::string key_password = password.substr(0, 127); |
583 | 159 | std::string key_salt; |
584 | 159 | std::string user_data; |
585 | 159 | std::string encrypted_file_key; |
586 | 159 | if (check_owner_password_V5(key_password)) { |
587 | 17 | key_salt = getO().substr(40, 8); |
588 | 17 | user_data = getU().substr(0, 48); |
589 | 17 | encrypted_file_key = getOE().substr(0, 32); |
590 | 142 | } else if (check_user_password_V5(key_password)) { |
591 | 142 | key_salt = getU().substr(40, 8); |
592 | 142 | encrypted_file_key = getUE().substr(0, 32); |
593 | 142 | } |
594 | 159 | std::string intermediate_key = hash_V5(key_password, key_salt, user_data); |
595 | 159 | std::string file_key = process_with_aes(intermediate_key, false, encrypted_file_key); |
596 | | |
597 | | // Decrypt Perms and check against expected value |
598 | 159 | auto perms_check = process_with_aes(file_key, false, getPerms()).substr(0, 12); |
599 | 159 | perms_valid = compute_Perms_value_V5_clear().substr(0, 12) == perms_check; |
600 | 159 | return file_key; |
601 | 159 | } |
602 | | |
603 | | QPDF::encryption_method_e |
604 | | QPDF::EncryptionParameters::interpretCF(QPDFObjectHandle const& cf) const |
605 | 818 | { |
606 | 818 | if (!cf.isName()) { |
607 | | // Default: /Identity |
608 | 111 | return e_none; |
609 | 111 | } |
610 | 707 | std::string filter = cf.getName(); |
611 | 707 | auto it = crypt_filters.find(filter); |
612 | 707 | if (it != crypt_filters.end()) { |
613 | 129 | return it->second; |
614 | 129 | } |
615 | 578 | if (filter == "/Identity") { |
616 | 5 | return e_none; |
617 | 5 | } |
618 | 573 | return e_unknown; |
619 | 578 | } |
620 | | |
621 | | void |
622 | | QPDF::initializeEncryption() |
623 | 9.00k | { |
624 | 9.00k | m->encp->initialize(*this); |
625 | 9.00k | } |
626 | | |
627 | | void |
628 | | QPDF::EncryptionParameters::initialize(QPDF& qpdf) |
629 | 9.00k | { |
630 | 9.00k | if (encryption_initialized) { |
631 | 6 | return; |
632 | 6 | } |
633 | 9.00k | encryption_initialized = true; |
634 | | |
635 | 9.00k | auto& qm = *qpdf.m; |
636 | 9.00k | auto& trailer = qm.trailer; |
637 | 9.00k | auto& file = qm.file; |
638 | | |
639 | 9.00k | auto warn_damaged_pdf = [&qpdf](std::string const& msg) { |
640 | 133 | qpdf.warn(qpdf.damagedPDF("encryption dictionary", msg)); |
641 | 133 | }; |
642 | 9.00k | auto throw_damaged_pdf = [&qpdf](std::string const& msg) { |
643 | 36 | throw qpdf.damagedPDF("encryption dictionary", msg); |
644 | 36 | }; |
645 | 9.00k | auto unsupported = [&file](std::string const& msg) -> QPDFExc { |
646 | 80 | return { |
647 | 80 | qpdf_e_unsupported, |
648 | 80 | file->getName(), |
649 | 80 | "encryption dictionary", |
650 | 80 | file->getLastOffset(), |
651 | 80 | msg}; |
652 | 80 | }; |
653 | | |
654 | | // After we initialize encryption parameters, we must use stored key information and never look |
655 | | // at /Encrypt again. Otherwise, things could go wrong if someone mutates the encryption |
656 | | // dictionary. |
657 | | |
658 | 9.00k | if (!trailer.hasKey("/Encrypt")) { |
659 | 8.46k | return; |
660 | 8.46k | } |
661 | | |
662 | | // Go ahead and set m->encrypted here. That way, isEncrypted will return true even if there |
663 | | // were errors reading the encryption dictionary. |
664 | 539 | encrypted = true; |
665 | | |
666 | 539 | std::string id1; |
667 | 539 | auto id_obj = trailer.getKey("/ID"); |
668 | 539 | if (!id_obj.isArray() || id_obj.getArrayNItems() != 2 || !id_obj.getArrayItem(0).isString()) { |
669 | | // Treating a missing ID as the empty string enables qpdf to decrypt some invalid encrypted |
670 | | // files with no /ID that poppler can read but Adobe Reader can't. |
671 | 268 | qpdf.warn(qpdf.damagedPDF("trailer", "invalid /ID in trailer dictionary")); |
672 | 271 | } else { |
673 | 271 | id1 = id_obj.getArrayItem(0).getStringValue(); |
674 | 271 | } |
675 | | |
676 | 539 | auto encryption_dict = trailer.getKey("/Encrypt"); |
677 | 539 | if (!encryption_dict.isDictionary()) { |
678 | 1 | throw qpdf.damagedPDF("/Encrypt in trailer dictionary is not a dictionary"); |
679 | 1 | } |
680 | | |
681 | 538 | if (!(encryption_dict.getKey("/Filter").isName() && |
682 | 538 | (encryption_dict.getKey("/Filter").getName() == "/Standard"))) { |
683 | 72 | throw unsupported("unsupported encryption filter"); |
684 | 72 | } |
685 | 466 | if (!encryption_dict.getKey("/SubFilter").isNull()) { |
686 | 1 | qpdf.warn(unsupported("file uses encryption SubFilters, which qpdf does not support")); |
687 | 1 | } |
688 | | |
689 | 466 | if (!(encryption_dict.getKey("/V").isInteger() && encryption_dict.getKey("/R").isInteger() && |
690 | 466 | encryption_dict.getKey("/O").isString() && encryption_dict.getKey("/U").isString() && |
691 | 466 | encryption_dict.getKey("/P").isInteger())) { |
692 | 29 | throw_damaged_pdf("some encryption dictionary parameters are missing or the wrong type"); |
693 | 29 | } |
694 | | |
695 | 466 | int V = encryption_dict.getKey("/V").getIntValueAsInt(); |
696 | 466 | int R = encryption_dict.getKey("/R").getIntValueAsInt(); |
697 | 466 | std::string O = encryption_dict.getKey("/O").getStringValue(); |
698 | 466 | std::string U = encryption_dict.getKey("/U").getStringValue(); |
699 | 466 | int p = static_cast<int>(encryption_dict.getKey("/P").getIntValue()); |
700 | | |
701 | | // If supporting new encryption R/V values, remember to update error message inside this if |
702 | | // statement. |
703 | 466 | if (!(2 <= R && R <= 6 && (V == 1 || V == 2 || V == 4 || V == 5))) { |
704 | 7 | throw unsupported( |
705 | 7 | "Unsupported /R or /V in encryption dictionary; R = " + std::to_string(R) + |
706 | 7 | " (max 6), V = " + std::to_string(V) + " (max 5)"); |
707 | 7 | } |
708 | | |
709 | 459 | P_ = std::bitset<32>(static_cast<unsigned long long>(p)); |
710 | 459 | encryption_V = V; |
711 | 459 | R_ = R; |
712 | | |
713 | | // OE, UE, and Perms are only present if V >= 5. |
714 | 459 | std::string OE; |
715 | 459 | std::string UE; |
716 | 459 | std::string Perms; |
717 | | |
718 | 459 | if (V < 5) { |
719 | | // These must be exactly the right number of bytes. |
720 | 139 | pad_short_parameter(O, key_bytes); |
721 | 139 | pad_short_parameter(U, key_bytes); |
722 | 139 | if (!(O.length() == key_bytes && U.length() == key_bytes)) { |
723 | 3 | throw_damaged_pdf("incorrect length for /O and/or /U in encryption dictionary"); |
724 | 3 | } |
725 | 320 | } else { |
726 | 320 | if (!(encryption_dict.getKey("/OE").isString() && |
727 | 320 | encryption_dict.getKey("/UE").isString() && |
728 | 320 | encryption_dict.getKey("/Perms").isString())) { |
729 | 4 | throw_damaged_pdf( |
730 | 4 | "some V=5 encryption dictionary parameters are missing or the wrong type"); |
731 | 4 | } |
732 | 320 | OE = encryption_dict.getKey("/OE").getStringValue(); |
733 | 320 | UE = encryption_dict.getKey("/UE").getStringValue(); |
734 | 320 | Perms = encryption_dict.getKey("/Perms").getStringValue(); |
735 | | |
736 | | // These may be longer than the minimum number of bytes. |
737 | 320 | pad_short_parameter(O, OU_key_bytes_V5); |
738 | 320 | pad_short_parameter(U, OU_key_bytes_V5); |
739 | 320 | pad_short_parameter(OE, OUE_key_bytes_V5); |
740 | 320 | pad_short_parameter(UE, OUE_key_bytes_V5); |
741 | 320 | pad_short_parameter(Perms, Perms_key_bytes_V5); |
742 | 320 | } |
743 | | |
744 | 459 | int Length = 128; // Just take a guess. |
745 | 459 | if (V <= 1) { |
746 | 2 | Length = 40; |
747 | 457 | } else if (V == 4) { |
748 | 128 | Length = 128; |
749 | 329 | } else if (V == 5) { |
750 | 281 | Length = 256; |
751 | 281 | } else { |
752 | 48 | if (encryption_dict.getKey("/Length").isInteger()) { |
753 | 4 | Length = encryption_dict.getKey("/Length").getIntValueAsInt(); |
754 | 4 | if (Length % 8 || Length < 40 || Length > 128) { |
755 | 2 | Length = 128; // Just take a guess. |
756 | 2 | } |
757 | 4 | } |
758 | 48 | } |
759 | | |
760 | 459 | encrypt_metadata = true; |
761 | 459 | if (V >= 4 && encryption_dict.getKey("/EncryptMetadata").isBool()) { |
762 | 75 | encrypt_metadata = encryption_dict.getKey("/EncryptMetadata").getBoolValue(); |
763 | 75 | } |
764 | | |
765 | 459 | if (V == 4 || V == 5) { |
766 | 409 | auto CF = encryption_dict.getKey("/CF"); |
767 | 3.63k | for (auto const& [filter, cdict]: CF.as_dictionary()) { |
768 | 3.63k | if (cdict.isDictionary()) { |
769 | 1.53k | encryption_method_e method = e_none; |
770 | 1.53k | if (cdict.getKey("/CFM").isName()) { |
771 | 303 | std::string method_name = cdict.getKey("/CFM").getName(); |
772 | 303 | if (method_name == "/V2") { |
773 | 3 | QTC::TC("qpdf", "QPDF_encryption CFM V2"); |
774 | 3 | method = e_rc4; |
775 | 300 | } else if (method_name == "/AESV2") { |
776 | 19 | QTC::TC("qpdf", "QPDF_encryption CFM AESV2"); |
777 | 19 | method = e_aes; |
778 | 281 | } else if (method_name == "/AESV3") { |
779 | 103 | QTC::TC("qpdf", "QPDF_encryption CFM AESV3"); |
780 | 103 | method = e_aesv3; |
781 | 178 | } else { |
782 | | // Don't complain now -- maybe we won't need to reference this type. |
783 | 178 | method = e_unknown; |
784 | 178 | } |
785 | 303 | } |
786 | 1.53k | crypt_filters[filter] = method; |
787 | 1.53k | } |
788 | 3.63k | } |
789 | | |
790 | 409 | cf_stream = interpretCF(encryption_dict.getKey("/StmF")); |
791 | 409 | cf_string = interpretCF(encryption_dict.getKey("/StrF")); |
792 | 409 | if (auto EFF = encryption_dict.getKey("/EFF"); EFF.isName()) { |
793 | | // qpdf does not use this for anything other than informational purposes. This is |
794 | | // intended to instruct conforming writers on which crypt filter should be used when new |
795 | | // file attachments are added to a PDF file, but qpdf never generates encrypted files |
796 | | // with non-default crypt filters. Prior to 10.2, I was under the mistaken impression |
797 | | // that this was supposed to be used for decrypting attachments, but the code was wrong |
798 | | // in a way that turns out not to have mattered because no writers were generating files |
799 | | // the way I was imagining. Still, providing this information could be useful when |
800 | | // looking at a file generated by something else, such as Acrobat when specifying that |
801 | | // only attachments should be encrypted. |
802 | 2 | cf_file = interpretCF(EFF); |
803 | 407 | } else { |
804 | 407 | cf_file = cf_stream; |
805 | 407 | } |
806 | 409 | } |
807 | | |
808 | 459 | EncryptionData data(V, R, Length / 8, p, O, U, OE, UE, Perms, id1, encrypt_metadata); |
809 | 459 | if (qm.provided_password_is_hex_key) { |
810 | | // ignore passwords in file |
811 | 0 | encryption_key = QUtil::hex_decode(provided_password); |
812 | 0 | return; |
813 | 0 | } |
814 | | |
815 | 459 | owner_password_matched = data.check_owner_password(user_password, provided_password); |
816 | 459 | if (owner_password_matched && V < 5) { |
817 | | // password supplied was owner password; user_password has been initialized for V < 5 |
818 | 1 | if (qpdf.getTrimmedUserPassword() == provided_password) { |
819 | 0 | user_password_matched = true; |
820 | 0 | QTC::TC("qpdf", "QPDF_encryption user matches owner V < 5"); |
821 | 0 | } |
822 | 458 | } else { |
823 | 458 | user_password_matched = data.check_user_password(provided_password); |
824 | 458 | if (user_password_matched) { |
825 | 153 | user_password = provided_password; |
826 | 153 | } |
827 | 458 | } |
828 | 459 | if (user_password_matched && owner_password_matched) { |
829 | 1 | QTC::TC("qpdf", "QPDF_encryption same password", (V < 5) ? 0 : 1); |
830 | 1 | } |
831 | 459 | if (!(owner_password_matched || user_password_matched)) { |
832 | 245 | throw QPDFExc(qpdf_e_password, file->getName(), "", 0, "invalid password"); |
833 | 245 | } |
834 | | |
835 | 214 | if (V < 5) { |
836 | | // For V < 5, the user password is encrypted with the owner password, and the user password |
837 | | // is always used for computing the encryption key. |
838 | 11 | encryption_key = data.compute_encryption_key(user_password); |
839 | 203 | } else { |
840 | | // For V >= 5, either password can be used independently to compute the encryption key, and |
841 | | // neither password can be used to recover the other. |
842 | 203 | bool perms_valid; |
843 | 203 | encryption_key = data.recover_encryption_key_with_password(provided_password, perms_valid); |
844 | 203 | if (!perms_valid) { |
845 | 133 | warn_damaged_pdf("/Perms field in encryption dictionary doesn't match expected value"); |
846 | 133 | } |
847 | 203 | } |
848 | 214 | } |
849 | | |
850 | | std::string |
851 | | QPDF::getKeyForObject(std::shared_ptr<EncryptionParameters> encp, QPDFObjGen og, bool use_aes) |
852 | 2.25k | { |
853 | 2.25k | if (!encp->encrypted) { |
854 | 0 | throw std::logic_error("request for encryption key in non-encrypted PDF"); |
855 | 0 | } |
856 | | |
857 | 2.25k | if (og != encp->cached_key_og) { |
858 | 186 | encp->cached_object_encryption_key = compute_data_key( |
859 | 186 | encp->encryption_key, og.getObj(), og.getGen(), use_aes, encp->encryption_V, encp->R()); |
860 | 186 | encp->cached_key_og = og; |
861 | 186 | } |
862 | | |
863 | 2.25k | return encp->cached_object_encryption_key; |
864 | 2.25k | } |
865 | | |
866 | | void |
867 | | QPDF::decryptString(std::string& str, QPDFObjGen og) |
868 | 2.65k | { |
869 | 2.65k | if (!og.isIndirect()) { |
870 | 0 | return; |
871 | 0 | } |
872 | 2.65k | bool use_aes = false; |
873 | 2.65k | if (m->encp->encryption_V >= 4) { |
874 | 852 | switch (m->encp->cf_string) { |
875 | 442 | case e_none: |
876 | 442 | return; |
877 | | |
878 | 323 | case e_aes: |
879 | 323 | use_aes = true; |
880 | 323 | break; |
881 | | |
882 | 31 | case e_aesv3: |
883 | 31 | use_aes = true; |
884 | 31 | break; |
885 | | |
886 | 0 | case e_rc4: |
887 | 0 | break; |
888 | | |
889 | 56 | default: |
890 | 56 | warn(damagedPDF( |
891 | 56 | "unknown encryption filter for strings (check /StrF in " |
892 | 56 | "/Encrypt dictionary); strings may be decrypted improperly")); |
893 | | // To avoid repeated warnings, reset cf_string. Assume we'd want to use AES if V == 4. |
894 | 56 | m->encp->cf_string = e_aes; |
895 | 56 | use_aes = true; |
896 | 56 | break; |
897 | 852 | } |
898 | 852 | } |
899 | | |
900 | 2.21k | std::string key = getKeyForObject(m->encp, og, use_aes); |
901 | 2.21k | try { |
902 | 2.21k | if (use_aes) { |
903 | 410 | QTC::TC("qpdf", "QPDF_encryption aes decode string"); |
904 | 410 | Pl_Buffer bufpl("decrypted string"); |
905 | 410 | Pl_AES_PDF pl( |
906 | 410 | "aes decrypt string", |
907 | 410 | &bufpl, |
908 | 410 | false, |
909 | 410 | QUtil::unsigned_char_pointer(key), |
910 | 410 | key.length()); |
911 | 410 | pl.writeString(str); |
912 | 410 | pl.finish(); |
913 | 410 | str = bufpl.getString(); |
914 | 1.80k | } else { |
915 | 1.80k | QTC::TC("qpdf", "QPDF_encryption rc4 decode string"); |
916 | 1.80k | size_t vlen = str.length(); |
917 | | // Using std::shared_ptr guarantees that tmp will be freed even if rc4.process throws an |
918 | | // exception. |
919 | 1.80k | auto tmp = QUtil::make_unique_cstr(str); |
920 | 1.80k | RC4 rc4(QUtil::unsigned_char_pointer(key), toI(key.length())); |
921 | 1.80k | auto data = QUtil::unsigned_char_pointer(tmp.get()); |
922 | 1.80k | rc4.process(data, vlen, data); |
923 | 1.80k | str = std::string(tmp.get(), vlen); |
924 | 1.80k | } |
925 | 2.21k | } catch (QPDFExc&) { |
926 | 0 | throw; |
927 | 5 | } catch (std::runtime_error& e) { |
928 | 5 | throw damagedPDF("error decrypting string for object " + og.unparse() + ": " + e.what()); |
929 | 5 | } |
930 | 2.21k | } |
931 | | |
932 | | // Prepend a decryption pipeline to 'pipeline'. The decryption pipeline (returned as |
933 | | // 'decrypt_pipeline' must be owned by the caller to ensure that it stays alive while the pipeline |
934 | | // is in use. |
935 | | void |
936 | | QPDF::decryptStream( |
937 | | std::shared_ptr<EncryptionParameters> encp, |
938 | | std::shared_ptr<InputSource> file, |
939 | | QPDF& qpdf_for_warning, |
940 | | Pipeline*& pipeline, |
941 | | QPDFObjGen og, |
942 | | QPDFObjectHandle& stream_dict, |
943 | | bool is_root_metadata, |
944 | | std::unique_ptr<Pipeline>& decrypt_pipeline) |
945 | 46 | { |
946 | 46 | std::string type; |
947 | 46 | if (stream_dict.getKey("/Type").isName()) { |
948 | 35 | type = stream_dict.getKey("/Type").getName(); |
949 | 35 | } |
950 | 46 | if (type == "/XRef") { |
951 | 1 | QTC::TC("qpdf", "QPDF_encryption xref stream from encrypted file"); |
952 | 1 | return; |
953 | 1 | } |
954 | 45 | bool use_aes = false; |
955 | 45 | if (encp->encryption_V >= 4) { |
956 | 23 | encryption_method_e method = e_unknown; |
957 | 23 | std::string method_source = "/StmF from /Encrypt dictionary"; |
958 | | |
959 | 23 | if (stream_dict.getKey("/Filter").isOrHasName("/Crypt")) { |
960 | 0 | if (stream_dict.getKey("/DecodeParms").isDictionary()) { |
961 | 0 | QPDFObjectHandle decode_parms = stream_dict.getKey("/DecodeParms"); |
962 | 0 | if (decode_parms.isDictionaryOfType("/CryptFilterDecodeParms")) { |
963 | 0 | QTC::TC("qpdf", "QPDF_encryption stream crypt filter"); |
964 | 0 | method = encp->interpretCF(decode_parms.getKey("/Name")); |
965 | 0 | method_source = "stream's Crypt decode parameters"; |
966 | 0 | } |
967 | 0 | } else if ( |
968 | 0 | stream_dict.getKey("/DecodeParms").isArray() && |
969 | 0 | stream_dict.getKey("/Filter").isArray()) { |
970 | 0 | QPDFObjectHandle filter = stream_dict.getKey("/Filter"); |
971 | 0 | QPDFObjectHandle decode = stream_dict.getKey("/DecodeParms"); |
972 | 0 | if (filter.getArrayNItems() == decode.getArrayNItems()) { |
973 | 0 | for (int i = 0; i < filter.getArrayNItems(); ++i) { |
974 | 0 | if (filter.getArrayItem(i).isNameAndEquals("/Crypt")) { |
975 | 0 | QPDFObjectHandle crypt_params = decode.getArrayItem(i); |
976 | 0 | if (crypt_params.isDictionary() && |
977 | 0 | crypt_params.getKey("/Name").isName()) { |
978 | 0 | QTC::TC("qpdf", "QPDF_encrypt crypt array"); |
979 | 0 | method = encp->interpretCF(crypt_params.getKey("/Name")); |
980 | 0 | method_source = "stream's Crypt decode parameters (array)"; |
981 | 0 | } |
982 | 0 | } |
983 | 0 | } |
984 | 0 | } |
985 | 0 | } |
986 | 0 | } |
987 | | |
988 | 23 | if (method == e_unknown) { |
989 | 23 | if ((!encp->encrypt_metadata) && is_root_metadata) { |
990 | 0 | QTC::TC("qpdf", "QPDF_encryption cleartext metadata"); |
991 | 0 | method = e_none; |
992 | 23 | } else { |
993 | 23 | method = encp->cf_stream; |
994 | 23 | } |
995 | 23 | } |
996 | 23 | use_aes = false; |
997 | 23 | switch (method) { |
998 | 1 | case e_none: |
999 | 1 | return; |
1000 | 0 | break; |
1001 | | |
1002 | 3 | case e_aes: |
1003 | 3 | use_aes = true; |
1004 | 3 | break; |
1005 | | |
1006 | 1 | case e_aesv3: |
1007 | 1 | use_aes = true; |
1008 | 1 | break; |
1009 | | |
1010 | 0 | case e_rc4: |
1011 | 0 | break; |
1012 | | |
1013 | 18 | default: |
1014 | | // filter local to this stream. |
1015 | 18 | qpdf_for_warning.warn(QPDFExc( |
1016 | 18 | qpdf_e_damaged_pdf, |
1017 | 18 | file->getName(), |
1018 | 18 | "", |
1019 | 18 | file->getLastOffset(), |
1020 | 18 | "unknown encryption filter for streams (check " + method_source + |
1021 | 18 | "); streams may be decrypted improperly")); |
1022 | | // To avoid repeated warnings, reset cf_stream. Assume we'd want to use AES if V == 4. |
1023 | 18 | encp->cf_stream = e_aes; |
1024 | 18 | use_aes = true; |
1025 | 18 | break; |
1026 | 23 | } |
1027 | 23 | } |
1028 | 44 | std::string key = getKeyForObject(encp, og, use_aes); |
1029 | 44 | if (use_aes) { |
1030 | 22 | QTC::TC("qpdf", "QPDF_encryption aes decode stream"); |
1031 | 22 | decrypt_pipeline = std::make_unique<Pl_AES_PDF>( |
1032 | 22 | "AES stream decryption", |
1033 | 22 | pipeline, |
1034 | 22 | false, |
1035 | 22 | QUtil::unsigned_char_pointer(key), |
1036 | 22 | key.length()); |
1037 | 22 | } else { |
1038 | 22 | QTC::TC("qpdf", "QPDF_encryption rc4 decode stream"); |
1039 | 22 | decrypt_pipeline = std::make_unique<Pl_RC4>( |
1040 | 22 | "RC4 stream decryption", |
1041 | 22 | pipeline, |
1042 | 22 | QUtil::unsigned_char_pointer(key), |
1043 | 22 | toI(key.length())); |
1044 | 22 | } |
1045 | 44 | pipeline = decrypt_pipeline.get(); |
1046 | 44 | } |
1047 | | |
1048 | | void |
1049 | | QPDF::compute_encryption_O_U( |
1050 | | char const* user_password, |
1051 | | char const* owner_password, |
1052 | | int V, |
1053 | | int R, |
1054 | | int key_len, |
1055 | | int P, |
1056 | | bool encrypt_metadata, |
1057 | | std::string const& id1, |
1058 | | std::string& out_O, |
1059 | | std::string& out_U) |
1060 | 0 | { |
1061 | 0 | EncryptionData data(V, R, key_len, P, "", "", "", "", "", id1, encrypt_metadata); |
1062 | 0 | data.compute_encryption_O_U(user_password, owner_password); |
1063 | 0 | out_O = data.getO(); |
1064 | 0 | out_U = data.getU(); |
1065 | 0 | } |
1066 | | |
1067 | | void |
1068 | | QPDF::EncryptionData::compute_encryption_O_U(char const* user_password, char const* owner_password) |
1069 | 0 | { |
1070 | 0 | if (V >= 5) { |
1071 | 0 | throw std::logic_error("compute_encryption_O_U called for file with V >= 5"); |
1072 | 0 | } |
1073 | 0 | O = compute_O_value(user_password, owner_password); |
1074 | 0 | U = compute_U_value(user_password); |
1075 | 0 | } |
1076 | | |
1077 | | void |
1078 | | QPDF::compute_encryption_parameters_V5( |
1079 | | char const* user_password, |
1080 | | char const* owner_password, |
1081 | | int V, |
1082 | | int R, |
1083 | | int key_len, |
1084 | | int P, |
1085 | | bool encrypt_metadata, |
1086 | | std::string const& id1, |
1087 | | std::string& encryption_key, |
1088 | | std::string& out_O, |
1089 | | std::string& out_U, |
1090 | | std::string& out_OE, |
1091 | | std::string& out_UE, |
1092 | | std::string& out_Perms) |
1093 | 0 | { |
1094 | 0 | EncryptionData data(V, R, key_len, P, "", "", "", "", "", id1, encrypt_metadata); |
1095 | 0 | encryption_key = data.compute_encryption_parameters_V5(user_password, owner_password); |
1096 | |
|
1097 | 0 | out_O = data.getO(); |
1098 | 0 | out_U = data.getU(); |
1099 | 0 | out_OE = data.getOE(); |
1100 | 0 | out_UE = data.getUE(); |
1101 | 0 | out_Perms = data.getPerms(); |
1102 | 0 | } |
1103 | | |
1104 | | std::string |
1105 | | QPDF::EncryptionData::compute_encryption_parameters_V5( |
1106 | | char const* user_password, char const* owner_password) |
1107 | 0 | { |
1108 | 0 | auto out_encryption_key = util::random_string(key_bytes); |
1109 | | // Algorithm 8 from the PDF 2.0 |
1110 | 0 | auto validation_salt = util::random_string(8); |
1111 | 0 | auto key_salt = util::random_string(8); |
1112 | 0 | U = hash_V5(user_password, validation_salt, "").append(validation_salt).append(key_salt); |
1113 | 0 | auto intermediate_key = hash_V5(user_password, key_salt, ""); |
1114 | 0 | UE = process_with_aes(intermediate_key, true, out_encryption_key); |
1115 | | // Algorithm 9 from the PDF 2.0 |
1116 | 0 | validation_salt = util::random_string(8); |
1117 | 0 | key_salt = util::random_string(8); |
1118 | 0 | O = hash_V5(owner_password, validation_salt, U) + validation_salt + key_salt; |
1119 | 0 | intermediate_key = hash_V5(owner_password, key_salt, U); |
1120 | 0 | OE = process_with_aes(intermediate_key, true, out_encryption_key); |
1121 | | // Algorithm 10 from the PDF 2.0 |
1122 | 0 | Perms = process_with_aes(out_encryption_key, true, compute_Perms_value_V5_clear()); |
1123 | 0 | return out_encryption_key; |
1124 | 0 | } |
1125 | | |
1126 | | std::string |
1127 | | QPDF::EncryptionData::compute_parameters(char const* user_password, char const* owner_password) |
1128 | 0 | { |
1129 | 0 | if (V < 5) { |
1130 | 0 | compute_encryption_O_U(user_password, owner_password); |
1131 | 0 | return compute_encryption_key(user_password); |
1132 | 0 | } else { |
1133 | 0 | return compute_encryption_parameters_V5(user_password, owner_password); |
1134 | 0 | } |
1135 | 0 | } |
1136 | | |
1137 | | std::string const& |
1138 | | QPDF::getPaddedUserPassword() const |
1139 | 0 | { |
1140 | 0 | return m->encp->user_password; |
1141 | 0 | } |
1142 | | |
1143 | | std::string |
1144 | | QPDF::getTrimmedUserPassword() const |
1145 | 1 | { |
1146 | 1 | std::string result = m->encp->user_password; |
1147 | 1 | trim_user_password(result); |
1148 | 1 | return result; |
1149 | 1 | } |
1150 | | |
1151 | | std::string |
1152 | | QPDF::getEncryptionKey() const |
1153 | 0 | { |
1154 | 0 | return m->encp->encryption_key; |
1155 | 0 | } |
1156 | | |
1157 | | bool |
1158 | | QPDF::isEncrypted() const |
1159 | 0 | { |
1160 | 0 | return m->encp->encrypted; |
1161 | 0 | } |
1162 | | |
1163 | | bool |
1164 | | QPDF::isEncrypted(int& R, int& P) |
1165 | 0 | { |
1166 | 0 | if (!m->encp->encrypted) { |
1167 | 0 | return false; |
1168 | 0 | } |
1169 | 0 | P = m->encp->P(); |
1170 | 0 | R = m->encp->R(); |
1171 | 0 | return true; |
1172 | 0 | } |
1173 | | |
1174 | | bool |
1175 | | QPDF::isEncrypted( |
1176 | | int& R, |
1177 | | int& P, |
1178 | | int& V, |
1179 | | encryption_method_e& stream_method, |
1180 | | encryption_method_e& string_method, |
1181 | | encryption_method_e& file_method) |
1182 | 0 | { |
1183 | 0 | if (!m->encp->encrypted) { |
1184 | 0 | return false; |
1185 | 0 | } |
1186 | 0 | P = m->encp->P(); |
1187 | 0 | R = m->encp->R(); |
1188 | 0 | V = m->encp->encryption_V; |
1189 | 0 | stream_method = m->encp->cf_stream; |
1190 | 0 | string_method = m->encp->cf_string; |
1191 | 0 | file_method = m->encp->cf_file; |
1192 | 0 | return true; |
1193 | 0 | } |
1194 | | |
1195 | | bool |
1196 | | QPDF::ownerPasswordMatched() const |
1197 | 0 | { |
1198 | 0 | return m->encp->owner_password_matched; |
1199 | 0 | } |
1200 | | |
1201 | | bool |
1202 | | QPDF::userPasswordMatched() const |
1203 | 0 | { |
1204 | 0 | return m->encp->user_password_matched; |
1205 | 0 | } |
1206 | | |
1207 | | bool |
1208 | | QPDF::allowAccessibility() |
1209 | 0 | { |
1210 | 0 | return m->encp->R() < 3 ? m->encp->P(5) : m->encp->P(10); |
1211 | 0 | } |
1212 | | |
1213 | | bool |
1214 | | QPDF::allowExtractAll() |
1215 | 0 | { |
1216 | 0 | return m->encp->P(5); |
1217 | 0 | } |
1218 | | |
1219 | | bool |
1220 | | QPDF::allowPrintLowRes() |
1221 | 0 | { |
1222 | 0 | return m->encp->P(3); |
1223 | 0 | } |
1224 | | |
1225 | | bool |
1226 | | QPDF::allowPrintHighRes() |
1227 | 0 | { |
1228 | 0 | return allowPrintLowRes() && (m->encp->R() < 3 ? true : m->encp->P(12)); |
1229 | 0 | } |
1230 | | |
1231 | | bool |
1232 | | QPDF::allowModifyAssembly() |
1233 | 0 | { |
1234 | 0 | return m->encp->R() < 3 ? m->encp->P(4) : m->encp->P(11); |
1235 | 0 | } |
1236 | | |
1237 | | bool |
1238 | | QPDF::allowModifyForm() |
1239 | 0 | { |
1240 | 0 | return m->encp->R() < 3 ? m->encp->P(6) : m->encp->P(9); |
1241 | 0 | } |
1242 | | |
1243 | | bool |
1244 | | QPDF::allowModifyAnnotation() |
1245 | 0 | { |
1246 | 0 | return m->encp->P(6); |
1247 | 0 | } |
1248 | | |
1249 | | bool |
1250 | | QPDF::allowModifyOther() |
1251 | 0 | { |
1252 | 0 | return m->encp->P(4); |
1253 | 0 | } |
1254 | | |
1255 | | bool |
1256 | | QPDF::allowModifyAll() |
1257 | 0 | { |
1258 | 0 | return allowModifyAnnotation() && allowModifyOther() && |
1259 | 0 | (m->encp->R() < 3 ? true : allowModifyForm() && allowModifyAssembly()); |
1260 | 0 | } |