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
15#define FXCIPHER_NONE 0
16#define FXCIPHER_RC4 1
17#define FXCIPHER_AES 2
18#define FXCIPHER_AES2 3
19
20class CPDF_Array;
21class CPDF_CryptoHandler;
22class CPDF_Dictionary;
23class CPDF_Parser;
24
25class CPDF_SecurityHandler {
26 public:
27  CPDF_SecurityHandler();
28  ~CPDF_SecurityHandler();
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
44  ByteString GetUserPassword(const ByteString& owner_password,
45                             int32_t key_len) const;
46  bool CheckPassword(const ByteString& user_password,
47                     bool bOwner,
48                     uint8_t* key,
49                     int key_len);
50
51  CPDF_CryptoHandler* GetCryptoHandler() const {
52    return m_pCryptoHandler.get();
53  }
54
55 private:
56  bool LoadDict(const CPDF_Dictionary* pEncryptDict);
57  bool LoadDict(const CPDF_Dictionary* pEncryptDict,
58                int& cipher,
59                int& key_len);
60
61  bool CheckUserPassword(const ByteString& password,
62                         bool bIgnoreEncryptMeta,
63                         uint8_t* key,
64                         int32_t key_len);
65
66  bool CheckOwnerPassword(const ByteString& password,
67                          uint8_t* key,
68                          int32_t key_len);
69  bool AES256_CheckPassword(const ByteString& password,
70                            bool bOwner,
71                            uint8_t* key);
72  void AES256_SetPassword(CPDF_Dictionary* pEncryptDict,
73                          const ByteString& password,
74                          bool bOwner,
75                          const uint8_t* key);
76  void AES256_SetPerms(CPDF_Dictionary* pEncryptDict,
77                       uint32_t permission,
78                       bool bEncryptMetadata,
79                       const uint8_t* key);
80  void OnCreateInternal(CPDF_Dictionary* pEncryptDict,
81                        const CPDF_Array* pIdArray,
82                        const ByteString& user_password,
83                        const ByteString& owner_password,
84                        bool bDefault);
85  bool CheckSecurity(const ByteString& password);
86
87  void InitCryptoHandler();
88
89  int m_Version;
90  int m_Revision;
91  UnownedPtr<const CPDF_Dictionary> m_pEncryptDict;
92  ByteString m_FileId;
93  uint32_t m_Permissions;
94  int m_Cipher;
95  uint8_t m_EncryptKey[32];
96  int m_KeyLen;
97  bool m_bOwnerUnlocked;
98  std::unique_ptr<CPDF_CryptoHandler> m_pCryptoHandler;
99};
100
101#endif  // CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_
102