From 20c2f021b230112b08162237162ecd0f2e5f2aca Mon Sep 17 00:00:00 2001 From: m-holger Date: Sun, 6 Jul 2025 21:15:27 +0100 Subject: [PATCH] Refactor encryption handling to streamline key computation, centralize version-based logic in `setEncryptionMinimumVersion`, and reduce redundancy across `QPDFWriter` methods. --- include/qpdf/QPDFWriter.hh | 3 +-- libqpdf/QPDFWriter.cc | 23 +++++++---------------- libqpdf/QPDF_encryption.cc | 2 +- qpdf/qpdf.testcov | 1 - 4 files changed, 9 insertions(+), 20 deletions(-) diff --git a/include/qpdf/QPDFWriter.hh b/include/qpdf/QPDFWriter.hh index ffdec53..b26be87 100644 --- a/include/qpdf/QPDFWriter.hh +++ b/include/qpdf/QPDFWriter.hh @@ -523,8 +523,7 @@ class QPDFWriter void parseVersion(std::string const& version, int& major, int& minor) const; int compareVersions(int major1, int minor1, int major2, int minor2) const; void setEncryptionParameters(char const* user_password, char const* owner_password); - void setEncryptionParametersInternal( - std::string const& user_password, std::string const& encryption_key); + void setEncryptionMinimumVersion(); void setDataKey(int objid); int openObject(int objid = 0); void closeObject(int objid); diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 7823446..68ab329 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -655,8 +655,8 @@ QPDFWriter::setEncryptionParameters(char const* user_password, char const* owner { generateID(); m->encryption->setId1(m->id1); - auto encryption_key = m->encryption->compute_parameters(user_password, owner_password); - setEncryptionParametersInternal(user_password, encryption_key); + m->encryption_key = m->encryption->compute_parameters(user_password, owner_password); + setEncryptionMinimumVersion(); } void @@ -686,11 +686,6 @@ QPDFWriter::copyEncryptionParameters(QPDF& qpdf) } QTC::TC("qpdf", "QPDFWriter copy encrypt metadata", encrypt_metadata ? 0 : 1); QTC::TC("qpdf", "QPDFWriter copy use_aes", m->encrypt_use_aes ? 0 : 1); - std::string encryption_key; - if (V >= 5) { - QTC::TC("qpdf", "QPDFWriter copy V5"); - encryption_key = qpdf.getEncryptionKey(); - } m->encryption = std::make_unique( V, @@ -704,7 +699,10 @@ QPDFWriter::copyEncryptionParameters(QPDF& qpdf) V < 5 ? "" : encrypt.getKey("/Perms").getStringValue(), m->id1, // m->id1 == the other file's id1 encrypt_metadata); - setEncryptionParametersInternal(qpdf.getPaddedUserPassword(), encryption_key); + m->encryption_key = V >= 5 + ? qpdf.getEncryptionKey() + : m->encryption->compute_encryption_key(qpdf.getPaddedUserPassword()); + setEncryptionMinimumVersion(); } } @@ -779,8 +777,7 @@ QPDFWriter::compareVersions(int major1, int minor1, int major2, int minor2) cons } void -QPDFWriter::setEncryptionParametersInternal( - std::string const& user_password, std::string const& encryption_key) +QPDFWriter::setEncryptionMinimumVersion() { auto const R = m->encryption->getR(); if (R >= 6) { @@ -794,12 +791,6 @@ QPDFWriter::setEncryptionParametersInternal( } else { setMinimumPDFVersion("1.3"); } - - if (m->encryption->getV() < 5) { - m->encryption_key = m->encryption->compute_encryption_key(user_password); - } else { - m->encryption_key = encryption_key; - } } void diff --git a/libqpdf/QPDF_encryption.cc b/libqpdf/QPDF_encryption.cc index 23c4215..dcfd5ca 100644 --- a/libqpdf/QPDF_encryption.cc +++ b/libqpdf/QPDF_encryption.cc @@ -1201,7 +1201,7 @@ QPDF::EncryptionData::compute_parameters(char const* user_password, char const* { if (V < 5) { compute_encryption_O_U(user_password, owner_password); - return {}; + return compute_encryption_key(user_password); } else { return compute_encryption_parameters_V5(user_password, owner_password); } diff --git a/qpdf/qpdf.testcov b/qpdf/qpdf.testcov index 4e90b10..c3fd781 100644 --- a/qpdf/qpdf.testcov +++ b/qpdf/qpdf.testcov @@ -228,7 +228,6 @@ QPDFWriter extra header text no newline 0 QPDFWriter extra header text add newline 0 QPDF bogus 0 offset 0 QPDF global offset 0 -QPDFWriter copy V5 0 QPDFWriter increasing extension level 0 QPDFWriter make Extensions direct 0 QPDFWriter make ADBE direct 1 -- libgit2 0.21.4