diff options
author | Tom Sepez <tsepez@chromium.org> | 2016-03-09 17:42:18 -0800 |
---|---|---|
committer | Tom Sepez <tsepez@chromium.org> | 2016-03-09 17:42:18 -0800 |
commit | a1193aedb8797fcb26f54d7b99e4662ae72e3de9 (patch) | |
tree | 97f3a1c85e6227e4910981d735cd5ada62ac2b5c | |
parent | 99406198ac4921a53425edc73cac67e7b4760895 (diff) | |
download | pdfium-a1193aedb8797fcb26f54d7b99e4662ae72e3de9.tar.xz |
Split off IPDF_CryptoHandler into its own .cpp/.h files.
CPDF_CryptoHandler was always a pure-virtual class, now it
is named as IPDF_ to indicate this.
R=dsinclair@chromium.org
Review URL: https://codereview.chromium.org/1782673002 .
-rw-r--r-- | BUILD.gn | 6 | ||||
-rw-r--r-- | core/include/fpdfapi/cpdf_parser.h | 4 | ||||
-rw-r--r-- | core/include/fpdfapi/fpdf_parser.h | 86 | ||||
-rw-r--r-- | core/include/fpdfapi/fpdf_serial.h | 4 | ||||
-rw-r--r-- | core/include/fpdfapi/ipdf_crypto_handler.h | 45 | ||||
-rw-r--r-- | core/include/fpdfapi/ipdf_security_handler.h | 4 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 14 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/cpdf_parser.cpp | 5 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.cpp (renamed from core/src/fpdfapi/fpdf_parser/fpdf_parser_encrypt.cpp) | 31 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h | 64 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.cpp | 3 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h | 2 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.cpp | 5 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.h | 8 | ||||
-rw-r--r-- | pdfium.gyp | 4 |
15 files changed, 159 insertions, 126 deletions
@@ -248,6 +248,7 @@ static_library("fpdfapi") { "core/include/fpdfapi/fpdf_render.h", "core/include/fpdfapi/fpdf_resource.h", "core/include/fpdfapi/fpdf_serial.h", + "core/include/fpdfapi/ipdf_crypto_handler.h", "core/include/fpdfapi/ipdf_security_handler.h", "core/src/fpdfapi/fpdf_basic_module.cpp", "core/src/fpdfapi/fpdf_cmaps/CNS1/Adobe-CNS1-UCS2_5.cpp", @@ -338,12 +339,13 @@ static_library("fpdfapi") { "core/src/fpdfapi/fpdf_parser/cpdf_document.cpp", "core/src/fpdfapi/fpdf_parser/cpdf_parser.cpp", "core/src/fpdfapi/fpdf_parser/cpdf_simple_parser.cpp", + "core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.cpp", + "core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h", "core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.cpp", "core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h", "core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.cpp", "core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.h", "core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp", - "core/src/fpdfapi/fpdf_parser/fpdf_parser_encrypt.cpp", "core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp", "core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp", "core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp", @@ -860,7 +862,6 @@ if (pdf_enable_xfa) { "xfa/src/fde/fde_brush.h", "xfa/src/fde/fde_devbasic.cpp", "xfa/src/fde/fde_devbasic.h", - "xfa/src/fde/fde_renderdevice.h", "xfa/src/fde/fde_gedevice.cpp", "xfa/src/fde/fde_gedevice.h", "xfa/src/fde/fde_geobject.cpp", @@ -874,6 +875,7 @@ if (pdf_enable_xfa) { "xfa/src/fde/fde_pen.h", "xfa/src/fde/fde_render.cpp", "xfa/src/fde/fde_render.h", + "xfa/src/fde/fde_renderdevice.h", "xfa/src/fde/tto/fde_textout.cpp", "xfa/src/fde/tto/fde_textout.h", "xfa/src/fde/xml/fde_xml.h", diff --git a/core/include/fpdfapi/cpdf_parser.h b/core/include/fpdfapi/cpdf_parser.h index 80a1107743..4ec8d988e0 100644 --- a/core/include/fpdfapi/cpdf_parser.h +++ b/core/include/fpdfapi/cpdf_parser.h @@ -14,7 +14,6 @@ #include "core/include/fxcrt/fx_basic.h" class CPDF_Array; -class CPDF_CryptoHandler; class CPDF_Dictionary; class CPDF_Document; class CPDF_IndirectObjectHolder; @@ -22,6 +21,7 @@ class CPDF_Object; class CPDF_StreamAcc; class CPDF_SyntaxParser; class IFX_FileRead; +class IPDF_CryptoHandler; class IPDF_SecurityHandler; class CPDF_Parser { @@ -63,7 +63,7 @@ class CPDF_Parser { bool IsVersionUpdated() const { return m_bVersionUpdated; } bool IsObjectFreeOrNull(FX_DWORD objnum) const; FX_BOOL IsFormStream(FX_DWORD objnum, FX_BOOL& bForm); - CPDF_CryptoHandler* GetCryptoHandler(); + IPDF_CryptoHandler* GetCryptoHandler(); IFX_FileRead* GetFileAccess() const; FX_FILESIZE GetObjectOffset(FX_DWORD objnum) const; diff --git a/core/include/fpdfapi/fpdf_parser.h b/core/include/fpdfapi/fpdf_parser.h index 705587c513..9504061fcf 100644 --- a/core/include/fpdfapi/fpdf_parser.h +++ b/core/include/fpdfapi/fpdf_parser.h @@ -17,7 +17,7 @@ class CFX_Font; class CFX_Matrix; class CPDF_ColorSpace; -class CPDF_CryptoHandler; +class IPDF_CryptoHandler; class CPDF_Document; class CPDF_DocPageData; class CPDF_DocRenderData; @@ -45,90 +45,6 @@ class IPDF_SecurityHandler; // Indexed by 8-bit char code, contains unicode code points. extern const FX_WORD PDFDocEncoding[256]; -class CPDF_CryptoHandler { - public: - virtual ~CPDF_CryptoHandler() {} - - virtual FX_BOOL Init(CPDF_Dictionary* pEncryptDict, - IPDF_SecurityHandler* pSecurityHandler) = 0; - - virtual FX_DWORD DecryptGetSize(FX_DWORD src_size) = 0; - - virtual void* DecryptStart(FX_DWORD objnum, FX_DWORD gennum) = 0; - - virtual FX_BOOL DecryptStream(void* context, - const uint8_t* src_buf, - FX_DWORD src_size, - CFX_BinaryBuf& dest_buf) = 0; - - virtual FX_BOOL DecryptFinish(void* context, CFX_BinaryBuf& dest_buf) = 0; - - virtual FX_DWORD EncryptGetSize(FX_DWORD objnum, - FX_DWORD version, - const uint8_t* src_buf, - FX_DWORD src_size) = 0; - - virtual FX_BOOL EncryptContent(FX_DWORD objnum, - FX_DWORD version, - const uint8_t* src_buf, - FX_DWORD src_size, - uint8_t* dest_buf, - FX_DWORD& dest_size) = 0; - - void Decrypt(FX_DWORD objnum, FX_DWORD version, CFX_ByteString& str); -}; - -class CPDF_StandardCryptoHandler : public CPDF_CryptoHandler { - public: - CPDF_StandardCryptoHandler(); - ~CPDF_StandardCryptoHandler() override; - - // CPDF_CryptoHandler - FX_BOOL Init(CPDF_Dictionary* pEncryptDict, - IPDF_SecurityHandler* pSecurityHandler) override; - FX_DWORD DecryptGetSize(FX_DWORD src_size) override; - void* DecryptStart(FX_DWORD objnum, FX_DWORD gennum) override; - FX_BOOL DecryptStream(void* context, - const uint8_t* src_buf, - FX_DWORD src_size, - CFX_BinaryBuf& dest_buf) override; - FX_BOOL DecryptFinish(void* context, CFX_BinaryBuf& dest_buf) override; - FX_DWORD EncryptGetSize(FX_DWORD objnum, - FX_DWORD version, - const uint8_t* src_buf, - FX_DWORD src_size) override; - FX_BOOL EncryptContent(FX_DWORD objnum, - FX_DWORD version, - const uint8_t* src_buf, - FX_DWORD src_size, - uint8_t* dest_buf, - FX_DWORD& dest_size) override; - - FX_BOOL Init(int cipher, const uint8_t* key, int keylen); - - protected: - virtual void CryptBlock(FX_BOOL bEncrypt, - FX_DWORD objnum, - FX_DWORD gennum, - const uint8_t* src_buf, - FX_DWORD src_size, - uint8_t* dest_buf, - FX_DWORD& dest_size); - virtual void* CryptStart(FX_DWORD objnum, FX_DWORD gennum, FX_BOOL bEncrypt); - virtual FX_BOOL CryptStream(void* context, - const uint8_t* src_buf, - FX_DWORD src_size, - CFX_BinaryBuf& dest_buf, - FX_BOOL bEncrypt); - virtual FX_BOOL CryptFinish(void* context, - CFX_BinaryBuf& dest_buf, - FX_BOOL bEncrypt); - - uint8_t m_EncryptKey[32]; - int m_KeyLen; - int m_Cipher; - uint8_t* m_pAESContext; -}; CFX_ByteString PDF_NameDecode(const CFX_ByteStringC& orig); CFX_ByteString PDF_NameDecode(const CFX_ByteString& orig); diff --git a/core/include/fpdfapi/fpdf_serial.h b/core/include/fpdfapi/fpdf_serial.h index ccbea36404..6ed3756803 100644 --- a/core/include/fpdfapi/fpdf_serial.h +++ b/core/include/fpdfapi/fpdf_serial.h @@ -44,7 +44,7 @@ class CPDF_Creator { FX_BOOL m_bStandardSecurity; - CPDF_CryptoHandler* m_pCryptoHandler; + IPDF_CryptoHandler* m_pCryptoHandler; FX_BOOL m_bNewCrypto; FX_BOOL m_bEncryptMetadata; @@ -86,7 +86,7 @@ class CPDF_Creator { void InitID(FX_BOOL bDefault = TRUE); int32_t WriteStream(const CPDF_Object* pStream, FX_DWORD objnum, - CPDF_CryptoHandler* pCrypto); + IPDF_CryptoHandler* pCrypto); int32_t m_iStage; FX_DWORD m_dwFlags; diff --git a/core/include/fpdfapi/ipdf_crypto_handler.h b/core/include/fpdfapi/ipdf_crypto_handler.h new file mode 100644 index 0000000000..6a09a02278 --- /dev/null +++ b/core/include/fpdfapi/ipdf_crypto_handler.h @@ -0,0 +1,45 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_INCLUDE_FPDFAPI_IPDF_CRYPTO_HANDLER_H_ +#define CORE_INCLUDE_FPDFAPI_IPDF_CRYPTO_HANDLER_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CPDF_Dictionary; +class IPDF_SecurityHandler; + +class IPDF_CryptoHandler { + public: + virtual ~IPDF_CryptoHandler(); + + virtual FX_BOOL Init(CPDF_Dictionary* pEncryptDict, + IPDF_SecurityHandler* pSecurityHandler) = 0; + + virtual FX_DWORD DecryptGetSize(FX_DWORD src_size) = 0; + virtual void* DecryptStart(FX_DWORD objnum, FX_DWORD gennum) = 0; + virtual FX_BOOL DecryptStream(void* context, + const uint8_t* src_buf, + FX_DWORD src_size, + CFX_BinaryBuf& dest_buf) = 0; + + virtual FX_BOOL DecryptFinish(void* context, CFX_BinaryBuf& dest_buf) = 0; + virtual FX_DWORD EncryptGetSize(FX_DWORD objnum, + FX_DWORD version, + const uint8_t* src_buf, + FX_DWORD src_size) = 0; + + virtual FX_BOOL EncryptContent(FX_DWORD objnum, + FX_DWORD version, + const uint8_t* src_buf, + FX_DWORD src_size, + uint8_t* dest_buf, + FX_DWORD& dest_size) = 0; + + void Decrypt(FX_DWORD objnum, FX_DWORD version, CFX_ByteString& str); +}; + +#endif // CORE_INCLUDE_FPDFAPI_IPDF_CRYPTO_HANDLER_H_ diff --git a/core/include/fpdfapi/ipdf_security_handler.h b/core/include/fpdfapi/ipdf_security_handler.h index 699cf38c3a..4dce7c6963 100644 --- a/core/include/fpdfapi/ipdf_security_handler.h +++ b/core/include/fpdfapi/ipdf_security_handler.h @@ -11,7 +11,7 @@ class CPDF_Parser; class CPDF_Dictionary; -class CPDF_CryptoHandler; +class IPDF_CryptoHandler; #define FXCIPHER_NONE 0 #define FXCIPHER_RC4 1 @@ -30,7 +30,7 @@ class IPDF_SecurityHandler { int& keylen) = 0; virtual FX_BOOL IsMetadataEncrypted() = 0; - virtual CPDF_CryptoHandler* CreateCryptoHandler() = 0; + virtual IPDF_CryptoHandler* CreateCryptoHandler() = 0; }; #endif // CORE_INCLUDE_FPDFAPI_IPDF_SECURITY_HANDLER_H_ diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index 4cfafc51b6..848b12fbdd 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -12,7 +12,9 @@ #include "core/include/fpdfapi/cpdf_parser.h" #include "core/include/fpdfapi/fpdf_parser.h" #include "core/include/fpdfapi/fpdf_serial.h" +#include "core/include/fpdfapi/ipdf_crypto_handler.h" #include "core/include/fxcrt/fx_ext.h" +#include "core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h" #include "core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h" #include "third_party/base/stl_util.h" @@ -470,7 +472,7 @@ class CPDF_Encryptor { public: CPDF_Encryptor(); ~CPDF_Encryptor(); - FX_BOOL Initialize(CPDF_CryptoHandler* pHandler, + FX_BOOL Initialize(IPDF_CryptoHandler* pHandler, int objnum, uint8_t* src_data, FX_DWORD src_size); @@ -483,7 +485,7 @@ CPDF_Encryptor::CPDF_Encryptor() { m_dwSize = 0; m_bNewBuf = FALSE; } -FX_BOOL CPDF_Encryptor::Initialize(CPDF_CryptoHandler* pHandler, +FX_BOOL CPDF_Encryptor::Initialize(IPDF_CryptoHandler* pHandler, int objnum, uint8_t* src_data, FX_DWORD src_size) { @@ -540,7 +542,7 @@ FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { return 0; } CFX_FileBufferArchive* pFile = &pCreator->m_File; - CPDF_CryptoHandler* pHandler = pCreator->m_pCryptoHandler; + IPDF_CryptoHandler* pHandler = pCreator->m_pCryptoHandler; FX_FILESIZE ObjOffset = pCreator->m_Offset; if (!m_dwObjNum) { m_dwObjNum = ++pCreator->m_dwLastObjNum; @@ -1016,7 +1018,7 @@ int32_t CPDF_Creator::AppendObjectNumberToXRef(FX_DWORD objnum) { } int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, FX_DWORD objnum, - CPDF_CryptoHandler* pCrypto) { + IPDF_CryptoHandler* pCrypto) { CPDF_FlateEncoder encoder; encoder.Initialize(const_cast<CPDF_Stream*>(pStream->AsStream()), pStream == m_pMetadata ? FALSE : m_bCompress); @@ -1059,7 +1061,7 @@ int32_t CPDF_Creator::WriteIndirectObj(FX_DWORD objnum, m_Offset += len; if (pObj->IsStream()) { - CPDF_CryptoHandler* pHandler = nullptr; + IPDF_CryptoHandler* pHandler = nullptr; pHandler = (pObj == m_pMetadata && !m_bEncryptMetadata) ? NULL : m_pCryptoHandler; if (WriteStream(pObj, objnum, pHandler) < 0) @@ -1139,7 +1141,7 @@ int32_t CPDF_Creator::WriteDirectObj(FX_DWORD objnum, encoder.Initialize(const_cast<CPDF_Stream*>(pObj->AsStream()), m_bCompress); CPDF_Encryptor encryptor; - CPDF_CryptoHandler* pHandler = m_pCryptoHandler; + IPDF_CryptoHandler* pHandler = m_pCryptoHandler; encryptor.Initialize(pHandler, objnum, encoder.m_pData, encoder.m_dwSize); if ((FX_DWORD)encoder.m_pDict->GetIntegerBy("Length") != encryptor.m_dwSize) { diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_parser.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_parser.cpp index 45d29f9351..b016797d7d 100644 --- a/core/src/fpdfapi/fpdf_parser/cpdf_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/cpdf_parser.cpp @@ -10,6 +10,7 @@ #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/fpdf_parser.h" +#include "core/include/fpdfapi/ipdf_crypto_handler.h" #include "core/include/fxcrt/fx_ext.h" #include "core/include/fxcrt/fx_safe_types.h" #include "core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h" @@ -94,7 +95,7 @@ void CPDF_Parser::SetEncryptDictionary(CPDF_Dictionary* pDict) { m_pEncryptDict = pDict; } -CPDF_CryptoHandler* CPDF_Parser::GetCryptoHandler() { +IPDF_CryptoHandler* CPDF_Parser::GetCryptoHandler() { return m_pSyntax->m_pCryptoHandler.get(); } @@ -281,7 +282,7 @@ CPDF_Parser::Error CPDF_Parser::SetEncryptHandler() { return err; m_pSecurityHandler = std::move(pSecurityHandler); - std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler( + std::unique_ptr<IPDF_CryptoHandler> pCryptoHandler( m_pSecurityHandler->CreateCryptoHandler()); if (!pCryptoHandler->Init(m_pEncryptDict, m_pSecurityHandler.get())) return HANDLER_ERROR; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_encrypt.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.cpp index 5d710657be..d7a6fd59fe 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_encrypt.cpp +++ b/core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.cpp @@ -4,19 +4,27 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#include "core/include/fpdfapi/fpdf_parser.h" +#include "core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h" + +#include <time.h> +#include "core/include/fpdfapi/fpdf_parser.h" #include "core/include/fpdfapi/cpdf_parser.h" #include "core/include/fpdfapi/cpdf_simple_parser.h" #include "core/include/fpdfapi/ipdf_security_handler.h" #include "core/include/fdrm/fx_crypt.h" -struct PDF_CRYPTOITEM { - int32_t m_Cipher; - int32_t m_KeyLen; - FX_BOOL m_bChecked; - CPDF_StandardCryptoHandler* m_pCryptoHandler; -}; +IPDF_CryptoHandler::~IPDF_CryptoHandler() {} + +void IPDF_CryptoHandler::Decrypt(FX_DWORD objnum, + FX_DWORD gennum, + CFX_ByteString& str) { + CFX_BinaryBuf dest_buf; + void* context = DecryptStart(objnum, gennum); + DecryptStream(context, (const uint8_t*)str, str.GetLength(), dest_buf); + DecryptFinish(context, dest_buf); + str = dest_buf; +} void CPDF_StandardCryptoHandler::CryptBlock(FX_BOOL bEncrypt, FX_DWORD objnum, @@ -325,15 +333,6 @@ FX_BOOL CPDF_StandardCryptoHandler::EncryptContent(FX_DWORD objnum, CryptBlock(TRUE, objnum, gennum, src_buf, src_size, dest_buf, dest_size); return TRUE; } -void CPDF_CryptoHandler::Decrypt(FX_DWORD objnum, - FX_DWORD gennum, - CFX_ByteString& str) { - CFX_BinaryBuf dest_buf; - void* context = DecryptStart(objnum, gennum); - DecryptStream(context, (const uint8_t*)str, str.GetLength(), dest_buf); - DecryptFinish(context, dest_buf); - str = dest_buf; -} CPDF_StandardCryptoHandler::CPDF_StandardCryptoHandler() { m_pAESContext = NULL; m_Cipher = FXCIPHER_NONE; diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h b/core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h new file mode 100644 index 0000000000..3ef1f7143f --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h @@ -0,0 +1,64 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_SRC_FPDFAPI_FPDF_PARSER_CPDF_STANDARD_CRYPTO_HANDLER_H_ +#define CORE_SRC_FPDFAPI_FPDF_PARSER_CPDF_STANDARD_CRYPTO_HANDLER_H_ + +#include "core/include/fpdfapi/ipdf_crypto_handler.h" + +class CPDF_StandardCryptoHandler : public IPDF_CryptoHandler { + public: + CPDF_StandardCryptoHandler(); + ~CPDF_StandardCryptoHandler() override; + + // IPDF_CryptoHandler + FX_BOOL Init(CPDF_Dictionary* pEncryptDict, + IPDF_SecurityHandler* pSecurityHandler) override; + FX_DWORD DecryptGetSize(FX_DWORD src_size) override; + void* DecryptStart(FX_DWORD objnum, FX_DWORD gennum) override; + FX_BOOL DecryptStream(void* context, + const uint8_t* src_buf, + FX_DWORD src_size, + CFX_BinaryBuf& dest_buf) override; + FX_BOOL DecryptFinish(void* context, CFX_BinaryBuf& dest_buf) override; + FX_DWORD EncryptGetSize(FX_DWORD objnum, + FX_DWORD version, + const uint8_t* src_buf, + FX_DWORD src_size) override; + FX_BOOL EncryptContent(FX_DWORD objnum, + FX_DWORD version, + const uint8_t* src_buf, + FX_DWORD src_size, + uint8_t* dest_buf, + FX_DWORD& dest_size) override; + + FX_BOOL Init(int cipher, const uint8_t* key, int keylen); + + protected: + virtual void CryptBlock(FX_BOOL bEncrypt, + FX_DWORD objnum, + FX_DWORD gennum, + const uint8_t* src_buf, + FX_DWORD src_size, + uint8_t* dest_buf, + FX_DWORD& dest_size); + virtual void* CryptStart(FX_DWORD objnum, FX_DWORD gennum, FX_BOOL bEncrypt); + virtual FX_BOOL CryptStream(void* context, + const uint8_t* src_buf, + FX_DWORD src_size, + CFX_BinaryBuf& dest_buf, + FX_BOOL bEncrypt); + virtual FX_BOOL CryptFinish(void* context, + CFX_BinaryBuf& dest_buf, + FX_BOOL bEncrypt); + + uint8_t m_EncryptKey[32]; + int m_KeyLen; + int m_Cipher; + uint8_t* m_pAESContext; +}; + +#endif // CORE_SRC_FPDFAPI_FPDF_PARSER_CPDF_STANDARD_CRYPTO_HANDLER_H_ diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.cpp index dab9b1f89e..60d6ba47dd 100644 --- a/core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.cpp +++ b/core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.cpp @@ -11,6 +11,7 @@ #include "core/include/fdrm/fx_crypt.h" #include "core/include/fpdfapi/cpdf_parser.h" #include "core/include/fpdfapi/fpdf_parser.h" +#include "core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h" namespace { @@ -78,7 +79,7 @@ CPDF_StandardSecurityHandler::CPDF_StandardSecurityHandler() { CPDF_StandardSecurityHandler::~CPDF_StandardSecurityHandler() {} -CPDF_CryptoHandler* CPDF_StandardSecurityHandler::CreateCryptoHandler() { +IPDF_CryptoHandler* CPDF_StandardSecurityHandler::CreateCryptoHandler() { return new CPDF_StandardCryptoHandler; } diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h b/core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h index 27aeaa8bc0..fa761a1846 100644 --- a/core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h +++ b/core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h @@ -27,7 +27,7 @@ class CPDF_StandardSecurityHandler : public IPDF_SecurityHandler { const uint8_t*& buffer, int& keylen) override; FX_BOOL IsMetadataEncrypted() override; - CPDF_CryptoHandler* CreateCryptoHandler() override; + IPDF_CryptoHandler* CreateCryptoHandler() override; void OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_Array* pIdArray, diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.cpp index 151139e4a4..f635e02e3a 100644 --- a/core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.cpp @@ -10,6 +10,7 @@ #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_parser.h" +#include "core/include/fpdfapi/ipdf_crypto_handler.h" #include "core/include/fxcrt/fx_ext.h" #include "core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.h" #include "third_party/base/numerics/safe_math.h" @@ -636,7 +637,7 @@ CPDF_Stream* CPDF_SyntaxParser::ReadStream(CPDF_Dictionary* pDict, const CFX_ByteStringC kEndStreamStr("endstream"); const CFX_ByteStringC kEndObjStr("endobj"); - CPDF_CryptoHandler* pCryptoHandler = + IPDF_CryptoHandler* pCryptoHandler = objnum == (FX_DWORD)m_MetadataObjnum ? nullptr : m_pCryptoHandler.get(); if (!pCryptoHandler) { FX_BOOL bSearchForKeyword = TRUE; @@ -974,6 +975,6 @@ FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag, } void CPDF_SyntaxParser::SetEncrypt( - std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler) { + std::unique_ptr<IPDF_CryptoHandler> pCryptoHandler) { m_pCryptoHandler = std::move(pCryptoHandler); } diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.h b/core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.h index 44c98eafd8..c259efb1ee 100644 --- a/core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.h +++ b/core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.h @@ -11,12 +11,12 @@ #include "core/include/fxcrt/fx_basic.h" -class CPDF_CryptoHandler; +class CPDF_Dictionary; class CPDF_IndirectObjectHolder; class CPDF_Object; -class CPDF_Dictionary; class CPDF_Stream; class IFX_FileRead; +class IPDF_CryptoHandler; class CPDF_SyntaxParser { public: @@ -49,7 +49,7 @@ class CPDF_SyntaxParser { FX_FILESIZE limit); FX_FILESIZE FindTag(const CFX_ByteStringC& tag, FX_FILESIZE limit); - void SetEncrypt(std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler); + void SetEncrypt(std::unique_ptr<IPDF_CryptoHandler> pCryptoHandler); FX_BOOL ReadBlock(uint8_t* pBuf, FX_DWORD size); FX_BOOL GetCharAt(FX_FILESIZE pos, uint8_t& ch); @@ -88,7 +88,7 @@ class CPDF_SyntaxParser { uint8_t* m_pFileBuf; FX_DWORD m_BufSize; FX_FILESIZE m_BufOffset; - std::unique_ptr<CPDF_CryptoHandler> m_pCryptoHandler; + std::unique_ptr<IPDF_CryptoHandler> m_pCryptoHandler; uint8_t m_WordBuffer[257]; FX_DWORD m_WordSize; }; diff --git a/pdfium.gyp b/pdfium.gyp index 1193066b52..7478a18fcf 100644 --- a/pdfium.gyp +++ b/pdfium.gyp @@ -262,6 +262,7 @@ 'core/include/fpdfapi/fpdf_render.h', 'core/include/fpdfapi/fpdf_resource.h', 'core/include/fpdfapi/fpdf_serial.h', + 'core/include/fpdfapi/ipdf_crypto_handler.h', 'core/include/fpdfapi/ipdf_security_handler.h', 'core/src/fpdfapi/fpdf_basic_module.cpp', 'core/src/fpdfapi/fpdf_cmaps/cmap_int.h', @@ -352,12 +353,13 @@ 'core/src/fpdfapi/fpdf_parser/cpdf_document.cpp', 'core/src/fpdfapi/fpdf_parser/cpdf_parser.cpp', 'core/src/fpdfapi/fpdf_parser/cpdf_simple_parser.cpp', + 'core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.cpp', + 'core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h', 'core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.cpp', 'core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h', 'core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.cpp', 'core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.h', 'core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp', - 'core/src/fpdfapi/fpdf_parser/fpdf_parser_encrypt.cpp', 'core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp', 'core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp', 'core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp', |