Commit b26ce88ea172a5a3b66d0b8dd94b98043831eb27
1 parent
db95960a
Minor fixes to copyEncryptionParameters
This fixes were to code added yesterday; the problems would not have impacted any previously released code. These are all changes related to the possibility that copyEncryptionParameters may be called on behalf a different QPDF than the one being written.
Showing
1 changed file
with
14 additions
and
5 deletions
libqpdf/QPDFWriter.cc
| @@ -251,7 +251,6 @@ QPDFWriter::setR2EncryptionParameters( | @@ -251,7 +251,6 @@ QPDFWriter::setR2EncryptionParameters( | ||
| 251 | clear.insert(6); | 251 | clear.insert(6); |
| 252 | } | 252 | } |
| 253 | 253 | ||
| 254 | - setMinimumPDFVersion("1.3"); | ||
| 255 | setEncryptionParameters(user_password, owner_password, 1, 2, 5, clear); | 254 | setEncryptionParameters(user_password, owner_password, 1, 2, 5, clear); |
| 256 | } | 255 | } |
| 257 | 256 | ||
| @@ -265,7 +264,6 @@ QPDFWriter::setR3EncryptionParameters( | @@ -265,7 +264,6 @@ QPDFWriter::setR3EncryptionParameters( | ||
| 265 | interpretR3EncryptionParameters( | 264 | interpretR3EncryptionParameters( |
| 266 | clear, user_password, owner_password, | 265 | clear, user_password, owner_password, |
| 267 | allow_accessibility, allow_extract, print, modify); | 266 | allow_accessibility, allow_extract, print, modify); |
| 268 | - setMinimumPDFVersion("1.4"); | ||
| 269 | setEncryptionParameters(user_password, owner_password, 2, 3, 16, clear); | 267 | setEncryptionParameters(user_password, owner_password, 2, 3, 16, clear); |
| 270 | } | 268 | } |
| 271 | 269 | ||
| @@ -282,7 +280,6 @@ QPDFWriter::setR4EncryptionParameters( | @@ -282,7 +280,6 @@ QPDFWriter::setR4EncryptionParameters( | ||
| 282 | allow_accessibility, allow_extract, print, modify); | 280 | allow_accessibility, allow_extract, print, modify); |
| 283 | this->encrypt_use_aes = use_aes; | 281 | this->encrypt_use_aes = use_aes; |
| 284 | this->encrypt_metadata = encrypt_metadata; | 282 | this->encrypt_metadata = encrypt_metadata; |
| 285 | - setMinimumPDFVersion(use_aes ? "1.6" : "1.5"); | ||
| 286 | setEncryptionParameters(user_password, owner_password, 4, 4, 16, clear); | 283 | setEncryptionParameters(user_password, owner_password, 4, 4, 16, clear); |
| 287 | } | 284 | } |
| 288 | 285 | ||
| @@ -396,6 +393,8 @@ QPDFWriter::copyEncryptionParameters(QPDF& qpdf) | @@ -396,6 +393,8 @@ QPDFWriter::copyEncryptionParameters(QPDF& qpdf) | ||
| 396 | QPDFObjectHandle trailer = qpdf.getTrailer(); | 393 | QPDFObjectHandle trailer = qpdf.getTrailer(); |
| 397 | if (trailer.hasKey("/Encrypt")) | 394 | if (trailer.hasKey("/Encrypt")) |
| 398 | { | 395 | { |
| 396 | + this->id1 = | ||
| 397 | + trailer.getKey("/ID").getArrayItem(0).getStringValue(); | ||
| 399 | QPDFObjectHandle encrypt = trailer.getKey("/Encrypt"); | 398 | QPDFObjectHandle encrypt = trailer.getKey("/Encrypt"); |
| 400 | int V = encrypt.getKey("/V").getIntValue(); | 399 | int V = encrypt.getKey("/V").getIntValue(); |
| 401 | int key_len = 5; | 400 | int key_len = 5; |
| @@ -445,7 +444,7 @@ QPDFWriter::copyEncryptionParameters(QPDF& qpdf) | @@ -445,7 +444,7 @@ QPDFWriter::copyEncryptionParameters(QPDF& qpdf) | ||
| 445 | encrypt.getKey("/O").getStringValue(), | 444 | encrypt.getKey("/O").getStringValue(), |
| 446 | encrypt.getKey("/U").getStringValue(), | 445 | encrypt.getKey("/U").getStringValue(), |
| 447 | this->id1, // this->id1 == the other file's id1 | 446 | this->id1, // this->id1 == the other file's id1 |
| 448 | - pdf.getPaddedUserPassword()); | 447 | + qpdf.getPaddedUserPassword()); |
| 449 | } | 448 | } |
| 450 | } | 449 | } |
| 451 | 450 | ||
| @@ -555,6 +554,16 @@ QPDFWriter::setEncryptionParametersInternal( | @@ -555,6 +554,16 @@ QPDFWriter::setEncryptionParametersInternal( | ||
| 555 | encryption_dictionary["/P"] = QUtil::int_to_string(P); | 554 | encryption_dictionary["/P"] = QUtil::int_to_string(P); |
| 556 | encryption_dictionary["/O"] = QPDF_String(O).unparse(true); | 555 | encryption_dictionary["/O"] = QPDF_String(O).unparse(true); |
| 557 | encryption_dictionary["/U"] = QPDF_String(U).unparse(true); | 556 | encryption_dictionary["/U"] = QPDF_String(U).unparse(true); |
| 557 | + setMinimumPDFVersion("1.3"); | ||
| 558 | + if (R == 3) | ||
| 559 | + { | ||
| 560 | + setMinimumPDFVersion("1.4"); | ||
| 561 | + } | ||
| 562 | + else if (R >= 4) | ||
| 563 | + { | ||
| 564 | + setMinimumPDFVersion(this->encrypt_use_aes ? "1.6" : "1.5"); | ||
| 565 | + } | ||
| 566 | + | ||
| 558 | if ((R >= 4) && (! encrypt_metadata)) | 567 | if ((R >= 4) && (! encrypt_metadata)) |
| 559 | { | 568 | { |
| 560 | encryption_dictionary["/EncryptMetadata"] = "false"; | 569 | encryption_dictionary["/EncryptMetadata"] = "false"; |
| @@ -573,7 +582,7 @@ QPDFWriter::setEncryptionParametersInternal( | @@ -573,7 +582,7 @@ QPDFWriter::setEncryptionParametersInternal( | ||
| 573 | 582 | ||
| 574 | this->encrypted = true; | 583 | this->encrypted = true; |
| 575 | QPDF::EncryptionData encryption_data( | 584 | QPDF::EncryptionData encryption_data( |
| 576 | - V, R, key_len, P, O, U, this->id1, this->encrypt_metadata); | 585 | + V, R, key_len, P, O, U, id1, this->encrypt_metadata); |
| 577 | this->encryption_key = QPDF::compute_encryption_key( | 586 | this->encryption_key = QPDF::compute_encryption_key( |
| 578 | user_password, encryption_data); | 587 | user_password, encryption_data); |
| 579 | } | 588 | } |