• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SECURITY_HANDLER_H_
8 #define CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include <array>
14 #include <memory>
15 
16 #include "core/fpdfapi/parser/cpdf_crypto_handler.h"
17 #include "core/fxcrt/bytestring.h"
18 #include "core/fxcrt/retain_ptr.h"
19 
20 class CPDF_Array;
21 class CPDF_Dictionary;
22 
23 class CPDF_SecurityHandler final : public Retainable {
24  public:
25   CONSTRUCT_VIA_MAKE_RETAIN;
26 
27   bool OnInit(const CPDF_Dictionary* pEncryptDict,
28               RetainPtr<const CPDF_Array> pIdArray,
29               const ByteString& password);
30   void OnCreate(CPDF_Dictionary* pEncryptDict,
31                 const CPDF_Array* pIdArray,
32                 const ByteString& password);
33 
34   // When `get_owner_perms` is true, returns full permissions if unlocked by
35   // owner.
36   uint32_t GetPermissions(bool get_owner_perms) const;
37   bool IsMetadataEncrypted() const;
38 
GetCryptoHandler()39   CPDF_CryptoHandler* GetCryptoHandler() const {
40     return m_pCryptoHandler.get();
41   }
42 
43   // Take |password| and encode it, if necessary, based on the password encoding
44   // conversion.
45   ByteString GetEncodedPassword(ByteStringView password) const;
46 
47  private:
48   enum PasswordEncodingConversion {
49     kUnknown,
50     kNone,
51     kLatin1ToUtf8,
52     kUtf8toLatin1,
53   };
54 
55   CPDF_SecurityHandler();
56   ~CPDF_SecurityHandler() override;
57 
58   bool LoadDict(const CPDF_Dictionary* pEncryptDict);
59   bool LoadDict(const CPDF_Dictionary* pEncryptDict,
60                 CPDF_CryptoHandler::Cipher* cipher,
61                 size_t* key_len);
62 
63   ByteString GetUserPassword(const ByteString& owner_password) const;
64   bool CheckPassword(const ByteString& user_password, bool bOwner);
65   bool CheckPasswordImpl(const ByteString& password, bool bOwner);
66   bool CheckUserPassword(const ByteString& password, bool bIgnoreEncryptMeta);
67   bool CheckOwnerPassword(const ByteString& password);
68   bool AES256_CheckPassword(const ByteString& password, bool bOwner);
69   void AES256_SetPassword(CPDF_Dictionary* pEncryptDict,
70                           const ByteString& password);
71   void AES256_SetPerms(CPDF_Dictionary* pEncryptDict);
72   bool CheckSecurity(const ByteString& password);
73 
74   void InitCryptoHandler();
75 
76   bool m_bOwnerUnlocked = false;
77   int m_Version = 0;
78   int m_Revision = 0;
79   uint32_t m_Permissions = 0;
80   size_t m_KeyLen = 0;
81   CPDF_CryptoHandler::Cipher m_Cipher = CPDF_CryptoHandler::Cipher::kNone;
82   PasswordEncodingConversion m_PasswordEncodingConversion = kUnknown;
83   ByteString m_FileId;
84   RetainPtr<const CPDF_Dictionary> m_pEncryptDict;
85   std::unique_ptr<CPDF_CryptoHandler> m_pCryptoHandler;
86   std::array<uint8_t, 32> m_EncryptKey = {};
87 };
88 
89 #endif  // CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_
90