1 // Copyright 2016 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef CORE_FPDFAPI_PARSER_CPDF_CRYPTO_HANDLER_H_ 8 #define CORE_FPDFAPI_PARSER_CPDF_CRYPTO_HANDLER_H_ 9 10 #include <memory> 11 12 #include "core/fdrm/fx_crypt.h" 13 #include "core/fxcrt/cfx_binarybuf.h" 14 #include "core/fxcrt/fx_memory_wrappers.h" 15 #include "core/fxcrt/fx_string.h" 16 #include "core/fxcrt/fx_system.h" 17 #include "third_party/base/span.h" 18 19 class CPDF_Dictionary; 20 class CPDF_Object; 21 class CPDF_SecurityHandler; 22 23 class CPDF_CryptoHandler { 24 public: 25 CPDF_CryptoHandler(int cipher, const uint8_t* key, size_t keylen); 26 ~CPDF_CryptoHandler(); 27 28 static bool IsSignatureDictionary(const CPDF_Dictionary* dictionary); 29 30 bool DecryptObjectTree(RetainPtr<CPDF_Object> object); 31 size_t EncryptGetSize(pdfium::span<const uint8_t> source) const; 32 bool EncryptContent(uint32_t objnum, 33 uint32_t gennum, 34 pdfium::span<const uint8_t> source, 35 uint8_t* dest_buf, 36 uint32_t& dest_size); 37 38 bool IsCipherAES() const; 39 40 private: 41 uint32_t DecryptGetSize(uint32_t src_size); 42 void* DecryptStart(uint32_t objnum, uint32_t gennum); 43 ByteString Decrypt(uint32_t objnum, uint32_t gennum, const ByteString& str); 44 bool DecryptStream(void* context, 45 pdfium::span<const uint8_t> source, 46 CFX_BinaryBuf& dest_buf); 47 bool DecryptFinish(void* context, CFX_BinaryBuf& dest_buf); 48 49 void PopulateKey(uint32_t objnum, uint32_t gennum, uint8_t* key); 50 void CryptBlock(bool bEncrypt, 51 uint32_t objnum, 52 uint32_t gennum, 53 pdfium::span<const uint8_t> source, 54 uint8_t* dest_buf, 55 uint32_t& dest_size); 56 void* CryptStart(uint32_t objnum, uint32_t gennum, bool bEncrypt); 57 bool CryptStream(void* context, 58 pdfium::span<const uint8_t> source, 59 CFX_BinaryBuf& dest_buf, 60 bool bEncrypt); 61 bool CryptFinish(void* context, CFX_BinaryBuf& dest_buf, bool bEncrypt); 62 63 const size_t m_KeyLen; 64 const int m_Cipher; 65 std::unique_ptr<CRYPT_aes_context, FxFreeDeleter> m_pAESContext; 66 uint8_t m_EncryptKey[32]; 67 }; 68 69 #endif // CORE_FPDFAPI_PARSER_CPDF_CRYPTO_HANDLER_H_ 70