From b7fe3c6e8c9d332fc6d3ca55ec9f852ce162c5a1 Mon Sep 17 00:00:00 2001 From: Artem Strygin Date: Tue, 3 Oct 2017 19:10:04 +0300 Subject: Move the CryptoHandler into the SecurityHandler Change-Id: Idb5928e65833641d0443d955e4f2866d0f94cf5f Reviewed-on: https://pdfium-review.googlesource.com/15291 Commit-Queue: Art Snake Reviewed-by: dsinclair --- core/fpdfapi/edit/cpdf_creator.cpp | 30 +++++++++++++++------------ core/fpdfapi/edit/cpdf_creator.h | 5 +++-- core/fpdfapi/parser/cpdf_parser.cpp | 19 +++++++---------- core/fpdfapi/parser/cpdf_parser.h | 5 +++-- core/fpdfapi/parser/cpdf_security_handler.cpp | 5 +++++ core/fpdfapi/parser/cpdf_security_handler.h | 10 ++++++++- 6 files changed, 45 insertions(+), 29 deletions(-) (limited to 'core') diff --git a/core/fpdfapi/edit/cpdf_creator.cpp b/core/fpdfapi/edit/cpdf_creator.cpp index e176d85044..3fa6817957 100644 --- a/core/fpdfapi/edit/cpdf_creator.cpp +++ b/core/fpdfapi/edit/cpdf_creator.cpp @@ -145,7 +145,7 @@ CPDF_Creator::CPDF_Creator(CPDF_Document* pDoc, m_bSecurityChanged(false), m_pEncryptDict(m_pParser ? m_pParser->GetEncryptDict() : nullptr), m_dwEncryptObjNum(0), - m_pCryptoHandler(m_pParser ? m_pParser->GetCryptoHandler() : nullptr), + m_pSecurityHandler(m_pParser ? m_pParser->GetSecurityHandler() : nullptr), m_pMetadata(nullptr), m_dwLastObjNum(m_pDocument->GetLastObjNum()), m_Archive(pdfium::MakeUnique(archive)), @@ -196,7 +196,7 @@ bool CPDF_Creator::WriteIndirectObj(uint32_t objnum, const CPDF_Object* pObj) { if (pObj->IsStream()) { CPDF_CryptoHandler* pHandler = - pObj != m_pMetadata ? m_pCryptoHandler.Get() : nullptr; + pObj != m_pMetadata ? GetCryptoHandler() : nullptr; if (!WriteStream(pObj, objnum, pHandler)) return false; } else if (!WriteDirectObj(objnum, pObj, true)) { @@ -222,12 +222,12 @@ bool CPDF_Creator::WriteDirectObj(uint32_t objnum, case CPDF_Object::STRING: { ByteString str = pObj->GetString(); bool bHex = pObj->AsString()->IsHex(); - if (!m_pCryptoHandler || !bEncrypt) { + if (!GetCryptoHandler() || !bEncrypt) { if (!pObj->WriteTo(m_Archive.get())) return false; break; } - CPDF_Encryptor encryptor(m_pCryptoHandler.Get(), objnum, + CPDF_Encryptor encryptor(GetCryptoHandler(), objnum, (uint8_t*)str.c_str(), str.GetLength()); ByteString content = PDF_EncodeString( ByteString(encryptor.GetData(), encryptor.GetSize()), bHex); @@ -238,8 +238,8 @@ bool CPDF_Creator::WriteDirectObj(uint32_t objnum, case CPDF_Object::STREAM: { CPDF_FlateEncoder encoder(const_cast(pObj->AsStream()), true); - CPDF_Encryptor encryptor(m_pCryptoHandler.Get(), objnum, - encoder.GetData(), encoder.GetSize()); + CPDF_Encryptor encryptor(GetCryptoHandler(), objnum, encoder.GetData(), + encoder.GetSize()); if (static_cast(encoder.GetDict()->GetIntegerFor("Length")) != encryptor.GetSize()) { encoder.CloneDict(); @@ -277,7 +277,7 @@ bool CPDF_Creator::WriteDirectObj(uint32_t objnum, break; } case CPDF_Object::DICTIONARY: { - if (!m_pCryptoHandler || pObj == m_pEncryptDict) { + if (!GetCryptoHandler() || pObj == m_pEncryptDict) { if (!pObj->WriteTo(m_Archive.get())) return false; break; @@ -804,11 +804,11 @@ void CPDF_Creator::InitID() { if (m_pEncryptDict->GetStringFor("Filter") == "Standard") { ByteString user_pass = m_pParser->GetPassword(); uint32_t flag = PDF_ENCRYPT_CONTENT; - CPDF_SecurityHandler handler; - handler.OnCreate(m_pEncryptDict.Get(), m_pIDArray.get(), - user_pass.raw_str(), user_pass.GetLength(), flag); - m_pCryptoHandler = pdfium::MakeUnique(); - m_pCryptoHandler->Init(m_pEncryptDict.Get(), &handler); + m_pSecurityHandler = pdfium::MakeUnique(); + m_pSecurityHandler->OnCreate(m_pEncryptDict.Get(), m_pIDArray.get(), + user_pass.raw_str(), user_pass.GetLength(), + flag); + m_pSecurityHandler->InitCryptoHandler(); m_bSecurityChanged = true; } } @@ -848,7 +848,11 @@ bool CPDF_Creator::SetFileVersion(int32_t fileVersion) { } void CPDF_Creator::RemoveSecurity() { - m_pCryptoHandler.Reset(); + m_pSecurityHandler.Reset(); m_bSecurityChanged = true; m_pEncryptDict = nullptr; } + +CPDF_CryptoHandler* CPDF_Creator::GetCryptoHandler() { + return m_pSecurityHandler ? m_pSecurityHandler->GetCryptoHandler() : nullptr; +} diff --git a/core/fpdfapi/edit/cpdf_creator.h b/core/fpdfapi/edit/cpdf_creator.h index 4e8f9e8dbe..4c1d0f1800 100644 --- a/core/fpdfapi/edit/cpdf_creator.h +++ b/core/fpdfapi/edit/cpdf_creator.h @@ -18,6 +18,7 @@ class CPDF_Array; class CPDF_CryptoHandler; +class CPDF_SecurityHandler; class CPDF_Dictionary; class CPDF_Document; class CPDF_Object; @@ -41,7 +42,7 @@ class CPDF_Creator { uint32_t GetNextObjectNumber() { return ++m_dwLastObjNum; } uint32_t GetLastObjectNumber() const { return m_dwLastObjNum; } - CPDF_CryptoHandler* GetCryptoHandler() { return m_pCryptoHandler.Get(); } + CPDF_CryptoHandler* GetCryptoHandler(); CPDF_Document* GetDocument() const { return m_pDocument.Get(); } CPDF_Array* GetIDArray() const { return m_pIDArray.get(); } CPDF_Dictionary* GetEncryptDict() const { return m_pEncryptDict.Get(); } @@ -86,7 +87,7 @@ class CPDF_Creator { bool m_bSecurityChanged; UnownedPtr m_pEncryptDict; uint32_t m_dwEncryptObjNum; - fxcrt::MaybeOwned m_pCryptoHandler; + fxcrt::MaybeOwned m_pSecurityHandler; UnownedPtr m_pMetadata; uint32_t m_dwLastObjNum; std::unique_ptr m_Archive; diff --git a/core/fpdfapi/parser/cpdf_parser.cpp b/core/fpdfapi/parser/cpdf_parser.cpp index 17f49c6324..a1b8a2deba 100644 --- a/core/fpdfapi/parser/cpdf_parser.cpp +++ b/core/fpdfapi/parser/cpdf_parser.cpp @@ -155,10 +155,6 @@ void CPDF_Parser::SetEncryptDictionary(CPDF_Dictionary* pDict) { m_pEncryptDict = pDict; } -CPDF_CryptoHandler* CPDF_Parser::GetCryptoHandler() const { - return m_pCryptoHandler.get(); -} - RetainPtr CPDF_Parser::GetFileAccess() const { return m_pSyntax->GetFileAccess(); } @@ -315,17 +311,15 @@ CPDF_Parser::Error CPDF_Parser::SetEncryptHandler() { m_Password)) return PASSWORD_ERROR; - m_pSecurityHandler = std::move(pSecurityHandler); - auto pCryptoHandler = pdfium::MakeUnique(); - if (!pCryptoHandler->Init(m_pEncryptDict.Get(), m_pSecurityHandler.get())) + if (!pSecurityHandler->InitCryptoHandler()) return HANDLER_ERROR; - m_pCryptoHandler = std::move(pCryptoHandler); + + m_pSecurityHandler = std::move(pSecurityHandler); } return SUCCESS; } void CPDF_Parser::ReleaseEncryptHandler() { - m_pCryptoHandler.reset(); m_pSecurityHandler.reset(); SetEncryptDictionary(nullptr); } @@ -1276,9 +1270,12 @@ std::unique_ptr CPDF_Parser::ParseIndirectObjectAtInternal( if (result && objnum && result->GetObjNum() != objnum) return nullptr; - const bool should_decrypt = m_pCryptoHandler && objnum != m_MetadataObjnum; + const bool should_decrypt = m_pSecurityHandler && + m_pSecurityHandler->GetCryptoHandler() && + objnum != m_MetadataObjnum; if (should_decrypt) - result = m_pCryptoHandler->DecryptObjectTree(std::move(result)); + result = m_pSecurityHandler->GetCryptoHandler()->DecryptObjectTree( + std::move(result)); return result; } diff --git a/core/fpdfapi/parser/cpdf_parser.h b/core/fpdfapi/parser/cpdf_parser.h index 0c7d29f728..2dee1ab9e1 100644 --- a/core/fpdfapi/parser/cpdf_parser.h +++ b/core/fpdfapi/parser/cpdf_parser.h @@ -76,7 +76,9 @@ class CPDF_Parser { FX_FILESIZE GetObjectPositionOrZero(uint32_t objnum) const; uint16_t GetObjectGenNum(uint32_t objnum) const; bool IsObjectFreeOrNull(uint32_t objnum) const; - CPDF_CryptoHandler* GetCryptoHandler() const; + CPDF_SecurityHandler* GetSecurityHandler() const { + return m_pSecurityHandler.get(); + } RetainPtr GetFileAccess() const; bool IsObjectFree(uint32_t objnum) const; @@ -221,7 +223,6 @@ class CPDF_Parser { // All indirect object numbers that are being parsed. std::set m_ParsingObjNums; - std::unique_ptr m_pCryptoHandler; uint32_t m_MetadataObjnum = 0; }; diff --git a/core/fpdfapi/parser/cpdf_security_handler.cpp b/core/fpdfapi/parser/cpdf_security_handler.cpp index dce2ac4fdc..03c15458a9 100644 --- a/core/fpdfapi/parser/cpdf_security_handler.cpp +++ b/core/fpdfapi/parser/cpdf_security_handler.cpp @@ -706,3 +706,8 @@ void CPDF_SecurityHandler::AES256_SetPerms(CPDF_Dictionary* pEncryptDict, CRYPT_AESEncrypt(&aes, buf1, buf, 16); pEncryptDict->SetNewFor("Perms", ByteString(buf1, 16), false); } + +bool CPDF_SecurityHandler::InitCryptoHandler() { + m_pCryptoHandler = pdfium::MakeUnique(); + return m_pCryptoHandler->Init(m_pEncryptDict.Get(), this); +} diff --git a/core/fpdfapi/parser/cpdf_security_handler.h b/core/fpdfapi/parser/cpdf_security_handler.h index 8b55fa21a7..c03d97c411 100644 --- a/core/fpdfapi/parser/cpdf_security_handler.h +++ b/core/fpdfapi/parser/cpdf_security_handler.h @@ -7,7 +7,8 @@ #ifndef CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_ #define CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_ -#include "core/fpdfapi/parser/cpdf_crypto_handler.h" +#include + #include "core/fxcrt/fx_string.h" #include "core/fxcrt/fx_system.h" @@ -19,6 +20,7 @@ #define PDF_ENCRYPT_CONTENT 0 class CPDF_Array; +class CPDF_CryptoHandler; class CPDF_Dictionary; class CPDF_Parser; @@ -57,6 +59,11 @@ class CPDF_SecurityHandler { uint8_t* key, int key_len); + bool InitCryptoHandler(); + CPDF_CryptoHandler* GetCryptoHandler() const { + return m_pCryptoHandler.get(); + } + private: bool LoadDict(CPDF_Dictionary* pEncryptDict); bool LoadDict(CPDF_Dictionary* pEncryptDict, @@ -107,6 +114,7 @@ class CPDF_SecurityHandler { uint8_t m_EncryptKey[32]; int m_KeyLen; bool m_bOwnerUnlocked; + std::unique_ptr m_pCryptoHandler; }; #endif // CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_ -- cgit v1.2.3