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   CONSTRUCT_VIA_MAKE_RETAIN;
29 
30   bool OnInit(const CPDF_Dictionary* pEncryptDict,
31               const CPDF_Array* pIdArray,
32               const ByteString& password);
33   void OnCreate(CPDF_Dictionary* pEncryptDict,
34                 const CPDF_Array* pIdArray,
35                 const ByteString& user_password,
36                 const ByteString& owner_password);
37   void OnCreate(CPDF_Dictionary* pEncryptDict,
38                 const CPDF_Array* pIdArray,
39                 const ByteString& user_password);
40 
41   uint32_t GetPermissions() const;
42   bool IsMetadataEncrypted() const;
43 
GetCryptoHandler()44   CPDF_CryptoHandler* GetCryptoHandler() const {
45     return m_pCryptoHandler.get();
46   }
47 
48   // Take |password| and encode it, if necessary, based on the password encoding
49   // conversion.
50   ByteString GetEncodedPassword(ByteStringView password) const;
51 
52  private:
53   enum PasswordEncodingConversion {
54     kUnknown,
55     kNone,
56     kLatin1ToUtf8,
57     kUtf8toLatin1,
58   };
59 
60   CPDF_SecurityHandler();
61   ~CPDF_SecurityHandler() override;
62 
63   bool LoadDict(const CPDF_Dictionary* pEncryptDict);
64   bool LoadDict(const CPDF_Dictionary* pEncryptDict,
65                 int* cipher,
66                 size_t* key_len);
67 
68   ByteString GetUserPassword(const ByteString& owner_password) const;
69   bool CheckPassword(const ByteString& user_password, bool bOwner);
70   bool CheckPasswordImpl(const ByteString& password, bool bOwner);
71   bool CheckUserPassword(const ByteString& password, bool bIgnoreEncryptMeta);
72   bool CheckOwnerPassword(const ByteString& password);
73   bool AES256_CheckPassword(const ByteString& password, bool bOwner);
74   void AES256_SetPassword(CPDF_Dictionary* pEncryptDict,
75                           const ByteString& password,
76                           bool bOwner);
77   void AES256_SetPerms(CPDF_Dictionary* pEncryptDict);
78   void OnCreateInternal(CPDF_Dictionary* pEncryptDict,
79                         const CPDF_Array* pIdArray,
80                         const ByteString& user_password,
81                         const ByteString& owner_password,
82                         bool bDefault);
83   bool CheckSecurity(const ByteString& password);
84 
85   void InitCryptoHandler();
86 
87   bool m_bOwnerUnlocked = false;
88   int m_Version = 0;
89   int m_Revision = 0;
90   uint32_t m_Permissions = 0;
91   int m_Cipher = FXCIPHER_NONE;
92   size_t m_KeyLen = 0;
93   PasswordEncodingConversion m_PasswordEncodingConversion = kUnknown;
94   ByteString m_FileId;
95   RetainPtr<const CPDF_Dictionary> m_pEncryptDict;
96   std::unique_ptr<CPDF_CryptoHandler> m_pCryptoHandler;
97   uint8_t m_EncryptKey[32];
98 };
99 
100 #endif  // CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_
101