1 // Copyright 2016 The PDFium Authors 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 <stddef.h> 11 #include <stdint.h> 12 13 #include <array> 14 #include <memory> 15 16 #include "core/fdrm/fx_crypt.h" 17 #include "core/fxcrt/binary_buffer.h" 18 #include "core/fxcrt/bytestring.h" 19 #include "core/fxcrt/data_vector.h" 20 #include "core/fxcrt/fx_memory_wrappers.h" 21 #include "core/fxcrt/retain_ptr.h" 22 #include "core/fxcrt/span.h" 23 24 class CPDF_Dictionary; 25 class CPDF_Object; 26 27 class CPDF_CryptoHandler { 28 public: 29 enum class Cipher { 30 kNone = 0, 31 kRC4 = 1, 32 kAES = 2, 33 kAES2 = 3, 34 }; 35 36 static bool IsSignatureDictionary(const CPDF_Dictionary* dictionary); 37 38 CPDF_CryptoHandler(Cipher cipher, pdfium::span<const uint8_t> key); 39 ~CPDF_CryptoHandler(); 40 41 bool DecryptObjectTree(RetainPtr<CPDF_Object> object); 42 43 DataVector<uint8_t> EncryptContent(uint32_t objnum, 44 uint32_t gennum, 45 pdfium::span<const uint8_t> source) const; 46 47 bool IsCipherAES() const; 48 49 private: 50 size_t DecryptGetSize(size_t src_size); 51 void* DecryptStart(uint32_t objnum, uint32_t gennum); 52 ByteString Decrypt(uint32_t objnum, uint32_t gennum, const ByteString& str); 53 bool DecryptStream(void* context, 54 pdfium::span<const uint8_t> source, 55 BinaryBuffer& dest_buf); 56 bool DecryptFinish(void* context, BinaryBuffer& dest_buf); 57 void PopulateKey(uint32_t objnum, uint32_t gennum, uint8_t* key) const; 58 59 const size_t m_KeyLen; 60 const Cipher m_Cipher; 61 std::unique_ptr<CRYPT_aes_context, FxFreeDeleter> m_pAESContext; 62 std::array<uint8_t, 32> m_EncryptKey = {}; 63 }; 64 65 #endif // CORE_FPDFAPI_PARSER_CPDF_CRYPTO_HANDLER_H_ 66