• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
11 
12 #include "core/fxcrt/fx_string.h"
13 #include "core/fxcrt/fx_system.h"
14 #include "core/fxcrt/retain_ptr.h"
15 
16 #define FXCIPHER_NONE 0
17 #define FXCIPHER_RC4 1
18 #define FXCIPHER_AES 2
19 #define FXCIPHER_AES2 3
20 
21 class CPDF_Array;
22 class CPDF_CryptoHandler;
23 class CPDF_Dictionary;
24 class CPDF_Parser;
25 
26 class CPDF_SecurityHandler : public Retainable {
27  public:
28   template <typename T, typename... Args>
29   friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
30 
31   bool OnInit(const CPDF_Dictionary* pEncryptDict,
32               const CPDF_Array* pIdArray,
33               const ByteString& password);
34   void OnCreate(CPDF_Dictionary* pEncryptDict,
35                 const CPDF_Array* pIdArray,
36                 const ByteString& user_password,
37                 const ByteString& owner_password);
38   void OnCreate(CPDF_Dictionary* pEncryptDict,
39                 const CPDF_Array* pIdArray,
40                 const ByteString& user_password);
41 
42   uint32_t GetPermissions() const;
43   bool IsMetadataEncrypted() const;
44 
GetCryptoHandler()45   CPDF_CryptoHandler* GetCryptoHandler() const {
46     return m_pCryptoHandler.get();
47   }
48 
49   // Take |password| and encode it, if necessary, based on the password encoding
50   // conversion.
51   ByteString GetEncodedPassword(ByteStringView password) const;
52 
53  private:
54   enum PasswordEncodingConversion {
55     kUnknown,
56     kNone,
57     kLatin1ToUtf8,
58     kUtf8toLatin1,
59   };
60 
61   CPDF_SecurityHandler();
62   ~CPDF_SecurityHandler() override;
63 
64   bool LoadDict(const CPDF_Dictionary* pEncryptDict);
65   bool LoadDict(const CPDF_Dictionary* pEncryptDict,
66                 int* cipher,
67                 size_t* key_len);
68 
69   ByteString GetUserPassword(const ByteString& owner_password) const;
70   bool CheckPassword(const ByteString& user_password, bool bOwner);
71   bool CheckPasswordImpl(const ByteString& password, bool bOwner);
72   bool CheckUserPassword(const ByteString& password, bool bIgnoreEncryptMeta);
73   bool CheckOwnerPassword(const ByteString& password);
74   bool AES256_CheckPassword(const ByteString& password, bool bOwner);
75   void AES256_SetPassword(CPDF_Dictionary* pEncryptDict,
76                           const ByteString& password,
77                           bool bOwner);
78   void AES256_SetPerms(CPDF_Dictionary* pEncryptDict);
79   void OnCreateInternal(CPDF_Dictionary* pEncryptDict,
80                         const CPDF_Array* pIdArray,
81                         const ByteString& user_password,
82                         const ByteString& owner_password,
83                         bool bDefault);
84   bool CheckSecurity(const ByteString& password);
85 
86   void InitCryptoHandler();
87 
88   bool m_bOwnerUnlocked = false;
89   int m_Version = 0;
90   int m_Revision = 0;
91   uint32_t m_Permissions = 0;
92   int m_Cipher = FXCIPHER_NONE;
93   size_t m_KeyLen = 0;
94   PasswordEncodingConversion m_PasswordEncodingConversion = kUnknown;
95   ByteString m_FileId;
96   RetainPtr<const CPDF_Dictionary> m_pEncryptDict;
97   std::unique_ptr<CPDF_CryptoHandler> m_pCryptoHandler;
98   uint8_t m_EncryptKey[32];
99 };
100 
101 #endif  // CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_
102