summaryrefslogtreecommitdiff
path: root/core/fpdfapi/parser/cpdf_security_handler.h
blob: 27f7f7c6100bca88053a0350cfbbb83019e13bd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2016 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#ifndef CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_
#define CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_

#include "core/fpdfapi/parser/cpdf_crypto_handler.h"
#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/fx_system.h"

#define FXCIPHER_NONE 0
#define FXCIPHER_RC4 1
#define FXCIPHER_AES 2
#define FXCIPHER_AES2 3

#define PDF_ENCRYPT_CONTENT 0

class CPDF_Array;
class CPDF_Dictionary;
class CPDF_Parser;

class CPDF_SecurityHandler {
 public:
  CPDF_SecurityHandler();
  ~CPDF_SecurityHandler();

  bool OnInit(CPDF_Parser* pParser, CPDF_Dictionary* pEncryptDict);
  uint32_t GetPermissions();
  bool GetCryptInfo(int& cipher, const uint8_t*& buffer, int& keylen);
  bool IsMetadataEncrypted() const;

  void OnCreate(CPDF_Dictionary* pEncryptDict,
                CPDF_Array* pIdArray,
                const uint8_t* user_pass,
                uint32_t user_size,
                const uint8_t* owner_pass,
                uint32_t owner_size,
                uint32_t type = PDF_ENCRYPT_CONTENT);

  void OnCreate(CPDF_Dictionary* pEncryptDict,
                CPDF_Array* pIdArray,
                const uint8_t* user_pass,
                uint32_t user_size,
                uint32_t type = PDF_ENCRYPT_CONTENT);

  CFX_ByteString GetUserPassword(const uint8_t* owner_pass,
                                 uint32_t pass_size,
                                 int32_t key_len);
  bool CheckPassword(const uint8_t* password,
                     uint32_t pass_size,
                     bool bOwner,
                     uint8_t* key,
                     int key_len);

 private:
  bool LoadDict(CPDF_Dictionary* pEncryptDict);
  bool LoadDict(CPDF_Dictionary* pEncryptDict,
                uint32_t type,
                int& cipher,
                int& key_len);

  bool CheckUserPassword(const uint8_t* password,
                         uint32_t pass_size,
                         bool bIgnoreEncryptMeta,
                         uint8_t* key,
                         int32_t key_len);

  bool CheckOwnerPassword(const uint8_t* password,
                          uint32_t pass_size,
                          uint8_t* key,
                          int32_t key_len);
  bool AES256_CheckPassword(const uint8_t* password,
                            uint32_t size,
                            bool bOwner,
                            uint8_t* key);
  void AES256_SetPassword(CPDF_Dictionary* pEncryptDict,
                          const uint8_t* password,
                          uint32_t size,
                          bool bOwner,
                          const uint8_t* key);
  void AES256_SetPerms(CPDF_Dictionary* pEncryptDict,
                       uint32_t permission,
                       bool bEncryptMetadata,
                       const uint8_t* key);
  void OnCreate(CPDF_Dictionary* pEncryptDict,
                CPDF_Array* pIdArray,
                const uint8_t* user_pass,
                uint32_t user_size,
                const uint8_t* owner_pass,
                uint32_t owner_size,
                bool bDefault,
                uint32_t type);
  bool CheckSecurity(int32_t key_len);

  int m_Version;
  int m_Revision;
  CFX_UnownedPtr<CPDF_Parser> m_pParser;
  CFX_UnownedPtr<CPDF_Dictionary> m_pEncryptDict;
  uint32_t m_Permissions;
  int m_Cipher;
  uint8_t m_EncryptKey[32];
  int m_KeyLen;
  bool m_bOwnerUnlocked;
};

#endif  // CORE_FPDFAPI_PARSER_CPDF_SECURITY_HANDLER_H_