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