Commit 20c2f021b230112b08162237162ecd0f2e5f2aca

Authored by m-holger
1 parent fe122b0b

Refactor encryption handling to streamline key computation, centralize version-b…

…ased logic in `setEncryptionMinimumVersion`, and reduce redundancy across `QPDFWriter` methods.
include/qpdf/QPDFWriter.hh
... ... @@ -523,8 +523,7 @@ class QPDFWriter
523 523 void parseVersion(std::string const& version, int& major, int& minor) const;
524 524 int compareVersions(int major1, int minor1, int major2, int minor2) const;
525 525 void setEncryptionParameters(char const* user_password, char const* owner_password);
526   - void setEncryptionParametersInternal(
527   - std::string const& user_password, std::string const& encryption_key);
  526 + void setEncryptionMinimumVersion();
528 527 void setDataKey(int objid);
529 528 int openObject(int objid = 0);
530 529 void closeObject(int objid);
... ...
libqpdf/QPDFWriter.cc
... ... @@ -655,8 +655,8 @@ QPDFWriter::setEncryptionParameters(char const* user_password, char const* owner
655 655 {
656 656 generateID();
657 657 m->encryption->setId1(m->id1);
658   - auto encryption_key = m->encryption->compute_parameters(user_password, owner_password);
659   - setEncryptionParametersInternal(user_password, encryption_key);
  658 + m->encryption_key = m->encryption->compute_parameters(user_password, owner_password);
  659 + setEncryptionMinimumVersion();
660 660 }
661 661  
662 662 void
... ... @@ -686,11 +686,6 @@ QPDFWriter::copyEncryptionParameters(QPDF& qpdf)
686 686 }
687 687 QTC::TC("qpdf", "QPDFWriter copy encrypt metadata", encrypt_metadata ? 0 : 1);
688 688 QTC::TC("qpdf", "QPDFWriter copy use_aes", m->encrypt_use_aes ? 0 : 1);
689   - std::string encryption_key;
690   - if (V >= 5) {
691   - QTC::TC("qpdf", "QPDFWriter copy V5");
692   - encryption_key = qpdf.getEncryptionKey();
693   - }
694 689  
695 690 m->encryption = std::make_unique<QPDF::EncryptionData>(
696 691 V,
... ... @@ -704,7 +699,10 @@ QPDFWriter::copyEncryptionParameters(QPDF&amp; qpdf)
704 699 V < 5 ? "" : encrypt.getKey("/Perms").getStringValue(),
705 700 m->id1, // m->id1 == the other file's id1
706 701 encrypt_metadata);
707   - setEncryptionParametersInternal(qpdf.getPaddedUserPassword(), encryption_key);
  702 + m->encryption_key = V >= 5
  703 + ? qpdf.getEncryptionKey()
  704 + : m->encryption->compute_encryption_key(qpdf.getPaddedUserPassword());
  705 + setEncryptionMinimumVersion();
708 706 }
709 707 }
710 708  
... ... @@ -779,8 +777,7 @@ QPDFWriter::compareVersions(int major1, int minor1, int major2, int minor2) cons
779 777 }
780 778  
781 779 void
782   -QPDFWriter::setEncryptionParametersInternal(
783   - std::string const& user_password, std::string const& encryption_key)
  780 +QPDFWriter::setEncryptionMinimumVersion()
784 781 {
785 782 auto const R = m->encryption->getR();
786 783 if (R >= 6) {
... ... @@ -794,12 +791,6 @@ QPDFWriter::setEncryptionParametersInternal(
794 791 } else {
795 792 setMinimumPDFVersion("1.3");
796 793 }
797   -
798   - if (m->encryption->getV() < 5) {
799   - m->encryption_key = m->encryption->compute_encryption_key(user_password);
800   - } else {
801   - m->encryption_key = encryption_key;
802   - }
803 794 }
804 795  
805 796 void
... ...
libqpdf/QPDF_encryption.cc
... ... @@ -1201,7 +1201,7 @@ QPDF::EncryptionData::compute_parameters(char const* user_password, char const*
1201 1201 {
1202 1202 if (V < 5) {
1203 1203 compute_encryption_O_U(user_password, owner_password);
1204   - return {};
  1204 + return compute_encryption_key(user_password);
1205 1205 } else {
1206 1206 return compute_encryption_parameters_V5(user_password, owner_password);
1207 1207 }
... ...
qpdf/qpdf.testcov
... ... @@ -228,7 +228,6 @@ QPDFWriter extra header text no newline 0
228 228 QPDFWriter extra header text add newline 0
229 229 QPDF bogus 0 offset 0
230 230 QPDF global offset 0
231   -QPDFWriter copy V5 0
232 231 QPDFWriter increasing extension level 0
233 232 QPDFWriter make Extensions direct 0
234 233 QPDFWriter make ADBE direct 1
... ...