From 3efd9aebc9eaff6f3eefce18b3e517d7544a9be0 Mon Sep 17 00:00:00 2001 From: Svetlana Kulikova Date: Mon, 29 Jan 2024 18:00:04 +0300 Subject: [PATCH] Fix bug 66223 --- PdfFile/PdfFile.cpp | 24 +++++++++++++++++++----- PdfFile/SrcWriter/Encrypt.cpp | 6 ++++++ PdfFile/SrcWriter/EncryptDictionary.cpp | 4 ++-- PdfFile/SrcWriter/EncryptDictionary.h | 2 +- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/PdfFile/PdfFile.cpp b/PdfFile/PdfFile.cpp index 2f924c963e..e4b4643859 100644 --- a/PdfFile/PdfFile.cpp +++ b/PdfFile/PdfFile.cpp @@ -593,7 +593,7 @@ bool CPdfFile::EditPdf(const std::wstring& wsDstFile) // Нужно получить словарь Encrypt БЕЗ дешифровки, поэтому времено отключаем encrypted в xref xref->offEncrypted(); - Object encrypt; + Object encrypt, ID, ID1; if (pTrailerDict->dictLookup("Encrypt", &encrypt) && encrypt.isDict()) { for (int nIndex = 0; nIndex < encrypt.dictGetLength(); ++nIndex) @@ -604,16 +604,30 @@ bool CPdfFile::EditPdf(const std::wstring& wsDstFile) DictToCDictObject(&oTemp, pEncryptDict, true, chKey); oTemp.free(); } - - pEncryptDict->SetRef(0, 0); - pEncryptDict->Fix(); } + + if (!pEncryptDict->Get("Length")) + pEncryptDict->Add("Length", 40); + encrypt.free(); + if (pTrailerDict->dictLookup("ID", &ID) && ID.isArray() && ID.arrayGet(0, &ID1) && ID1.isString()) + DictToCDictObject(&ID1, pEncryptDict, true, "ID"); + ID.free(); ID1.free(); + xref->onEncrypted(); + pEncryptDict->SetRef(0, 0); + pEncryptDict->Fix(); + pEncryptDict->SetPasswords(m_pInternal->wsPassword, m_pInternal->wsPassword); - pEncryptDict->UpdateKey(nCryptAlgorithm); + if (!pEncryptDict->UpdateKey(nCryptAlgorithm)) + { + pagesRefObj.free(); + RELEASEOBJECT(pXref); + RELEASEOBJECT(pDRXref); + return false; + } } } diff --git a/PdfFile/SrcWriter/Encrypt.cpp b/PdfFile/SrcWriter/Encrypt.cpp index 78d3ebc98a..f47451e399 100644 --- a/PdfFile/SrcWriter/Encrypt.cpp +++ b/PdfFile/SrcWriter/Encrypt.cpp @@ -203,6 +203,12 @@ namespace PdfWriter m_unPermission = ENABLE_PRINT | ENABLE_EDIT_ALL | ENABLE_COPY | ENABLE_EDIT | PERMISSION_PAD; MemSet(m_anEncryptID, 0, ID_LEN); + + MemSet(m_anOwnerKey, 0, 48); + MemSet(m_anUserKey, 0, 48); + MemSet(m_anOwnerEncryptKey, 0, 32); + MemSet(m_anUserEncryptKey, 0, 32); + MemSet(m_anPermEncrypt, 0, 16); } CEncrypt::~CEncrypt() { diff --git a/PdfFile/SrcWriter/EncryptDictionary.cpp b/PdfFile/SrcWriter/EncryptDictionary.cpp index c5622cbf8d..5842adc7c8 100644 --- a/PdfFile/SrcWriter/EncryptDictionary.cpp +++ b/PdfFile/SrcWriter/EncryptDictionary.cpp @@ -205,9 +205,9 @@ namespace PdfWriter CBinaryObject* pEncryptPerm = new CBinaryObject(m_pEncrypt->m_anPermEncrypt, 16); Add("Perms", pEncryptPerm); } - void CEncryptDict::UpdateKey(int nCryptAlgorithm) + bool CEncryptDict::UpdateKey(int nCryptAlgorithm) { - m_pEncrypt->MakeFileKey(nCryptAlgorithm); + return m_pEncrypt->MakeFileKey(nCryptAlgorithm); } //---------------------------------------------------------------------------------------- // CSignatureDict diff --git a/PdfFile/SrcWriter/EncryptDictionary.h b/PdfFile/SrcWriter/EncryptDictionary.h index 05922501d8..d296223e08 100644 --- a/PdfFile/SrcWriter/EncryptDictionary.h +++ b/PdfFile/SrcWriter/EncryptDictionary.h @@ -61,7 +61,7 @@ namespace PdfWriter { return m_pEncrypt; } - void UpdateKey(int nCryptAlgorithm); + bool UpdateKey(int nCryptAlgorithm); private: CEncrypt* m_pEncrypt; std::string PadOrTrancatePassword(const std::wstring & wsPassword);