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_SECURITY_HANDLER_H_
      8 #define CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_
      9 
     10 #include "core/fxcrt/fx_string.h"
     11 #include "core/fxcrt/fx_system.h"
     12 
     13 #define FXCIPHER_NONE 0
     14 #define FXCIPHER_RC4 1
     15 #define FXCIPHER_AES 2
     16 #define FXCIPHER_AES2 3
     17 
     18 #define PDF_ENCRYPT_CONTENT 0
     19 
     20 class CPDF_Array;
     21 class CPDF_CryptoHandler;
     22 class CPDF_Dictionary;
     23 class CPDF_Parser;
     24 
     25 class CPDF_SecurityHandler {
     26  public:
     27   CPDF_SecurityHandler();
     28   ~CPDF_SecurityHandler();
     29 
     30   bool OnInit(CPDF_Parser* pParser, CPDF_Dictionary* pEncryptDict);
     31   uint32_t GetPermissions();
     32   bool GetCryptInfo(int& cipher, const uint8_t*& buffer, int& keylen);
     33   bool IsMetadataEncrypted() const;
     34   CPDF_CryptoHandler* CreateCryptoHandler();
     35 
     36   void OnCreate(CPDF_Dictionary* pEncryptDict,
     37                 CPDF_Array* pIdArray,
     38                 const uint8_t* user_pass,
     39                 uint32_t user_size,
     40                 const uint8_t* owner_pass,
     41                 uint32_t owner_size,
     42                 uint32_t type = PDF_ENCRYPT_CONTENT);
     43 
     44   void OnCreate(CPDF_Dictionary* pEncryptDict,
     45                 CPDF_Array* pIdArray,
     46                 const uint8_t* user_pass,
     47                 uint32_t user_size,
     48                 uint32_t type = PDF_ENCRYPT_CONTENT);
     49 
     50   CFX_ByteString GetUserPassword(const uint8_t* owner_pass,
     51                                  uint32_t pass_size,
     52                                  int32_t key_len);
     53   bool CheckPassword(const uint8_t* password,
     54                      uint32_t pass_size,
     55                      bool bOwner,
     56                      uint8_t* key,
     57                      int key_len);
     58 
     59  private:
     60   bool LoadDict(CPDF_Dictionary* pEncryptDict);
     61   bool LoadDict(CPDF_Dictionary* pEncryptDict,
     62                 uint32_t type,
     63                 int& cipher,
     64                 int& key_len);
     65 
     66   bool CheckUserPassword(const uint8_t* password,
     67                          uint32_t pass_size,
     68                          bool bIgnoreEncryptMeta,
     69                          uint8_t* key,
     70                          int32_t key_len);
     71 
     72   bool CheckOwnerPassword(const uint8_t* password,
     73                           uint32_t pass_size,
     74                           uint8_t* key,
     75                           int32_t key_len);
     76   bool AES256_CheckPassword(const uint8_t* password,
     77                             uint32_t size,
     78                             bool bOwner,
     79                             uint8_t* key);
     80   void AES256_SetPassword(CPDF_Dictionary* pEncryptDict,
     81                           const uint8_t* password,
     82                           uint32_t size,
     83                           bool bOwner,
     84                           const uint8_t* key);
     85   void AES256_SetPerms(CPDF_Dictionary* pEncryptDict,
     86                        uint32_t permission,
     87                        bool bEncryptMetadata,
     88                        const uint8_t* key);
     89   void OnCreate(CPDF_Dictionary* pEncryptDict,
     90                 CPDF_Array* pIdArray,
     91                 const uint8_t* user_pass,
     92                 uint32_t user_size,
     93                 const uint8_t* owner_pass,
     94                 uint32_t owner_size,
     95                 bool bDefault,
     96                 uint32_t type);
     97   bool CheckSecurity(int32_t key_len);
     98 
     99   int m_Version;
    100   int m_Revision;
    101   CPDF_Parser* m_pParser;
    102   CPDF_Dictionary* m_pEncryptDict;
    103   uint32_t m_Permissions;
    104   int m_Cipher;
    105   uint8_t m_EncryptKey[32];
    106   int m_KeyLen;
    107   bool m_bOwnerUnlocked;
    108 };
    109 
    110 #endif  // CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_
    111