xref: /aosp_15_r20/external/pdfium/core/fpdfapi/parser/cpdf_crypto_handler.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 <memory>
14 
15 #include "core/fdrm/fx_crypt.h"
16 #include "core/fxcrt/binary_buffer.h"
17 #include "core/fxcrt/bytestring.h"
18 #include "core/fxcrt/fx_memory_wrappers.h"
19 #include "core/fxcrt/retain_ptr.h"
20 #include "third_party/base/containers/span.h"
21 
22 class CPDF_Dictionary;
23 class CPDF_Object;
24 
25 class CPDF_CryptoHandler {
26  public:
27   enum class Cipher {
28     kNone = 0,
29     kRC4 = 1,
30     kAES = 2,
31     kAES2 = 3,
32   };
33 
34   static bool IsSignatureDictionary(const CPDF_Dictionary* dictionary);
35 
36   CPDF_CryptoHandler(Cipher cipher, const uint8_t* key, size_t keylen);
37   ~CPDF_CryptoHandler();
38 
39   bool DecryptObjectTree(RetainPtr<CPDF_Object> object);
40   size_t EncryptGetSize(pdfium::span<const uint8_t> source) const;
41   void EncryptContent(uint32_t objnum,
42                       uint32_t gennum,
43                       pdfium::span<const uint8_t> source,
44                       uint8_t* dest_buf,
45                       size_t& dest_size) 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   uint8_t m_EncryptKey[32] = {};
63 };
64 
65 #endif  // CORE_FPDFAPI_PARSER_CPDF_CRYPTO_HANDLER_H_
66