Home | History | Annotate | Download | only in parser
      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/crypto/fx_crypt.h"
     13 #include "core/fxcrt/cfx_binarybuf.h"
     14 #include "core/fxcrt/fx_memory.h"
     15 #include "core/fxcrt/fx_string.h"
     16 #include "core/fxcrt/fx_system.h"
     17 #include "core/fxcrt/retain_ptr.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, int keylen);
     26   ~CPDF_CryptoHandler();
     27 
     28   static bool IsSignatureDictionary(const CPDF_Dictionary* dictionary);
     29 
     30   std::unique_ptr<CPDF_Object> DecryptObjectTree(
     31       std::unique_ptr<CPDF_Object> object);
     32 
     33   uint32_t EncryptGetSize(uint32_t objnum,
     34                           uint32_t version,
     35                           const uint8_t* src_buf,
     36                           uint32_t src_size);
     37   bool EncryptContent(uint32_t objnum,
     38                       uint32_t version,
     39                       const uint8_t* src_buf,
     40                       uint32_t src_size,
     41                       uint8_t* dest_buf,
     42                       uint32_t& dest_size);
     43 
     44   bool IsCipherAES() const;
     45 
     46  private:
     47   uint32_t DecryptGetSize(uint32_t src_size);
     48   void* DecryptStart(uint32_t objnum, uint32_t gennum);
     49   ByteString Decrypt(uint32_t objnum, uint32_t gennum, const ByteString& str);
     50   bool DecryptStream(void* context,
     51                      const uint8_t* src_buf,
     52                      uint32_t src_size,
     53                      CFX_BinaryBuf& dest_buf);
     54   bool DecryptFinish(void* context, CFX_BinaryBuf& dest_buf);
     55 
     56   void PopulateKey(uint32_t objnum, uint32_t gennum, uint8_t* key);
     57   void CryptBlock(bool bEncrypt,
     58                   uint32_t objnum,
     59                   uint32_t gennum,
     60                   const uint8_t* src_buf,
     61                   uint32_t src_size,
     62                   uint8_t* dest_buf,
     63                   uint32_t& dest_size);
     64   void* CryptStart(uint32_t objnum, uint32_t gennum, bool bEncrypt);
     65   bool CryptStream(void* context,
     66                    const uint8_t* src_buf,
     67                    uint32_t src_size,
     68                    CFX_BinaryBuf& dest_buf,
     69                    bool bEncrypt);
     70   bool CryptFinish(void* context, CFX_BinaryBuf& dest_buf, bool bEncrypt);
     71 
     72   uint8_t m_EncryptKey[32];
     73   int m_KeyLen;
     74   int m_Cipher;
     75   std::unique_ptr<CRYPT_aes_context, FxFreeDeleter> m_pAESContext;
     76 };
     77 
     78 #endif  // CORE_FPDFAPI_PARSER_CPDF_CRYPTO_HANDLER_H_
     79