OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fpdfapi/parser/cpdf_security_handler.h" | 7 #include "core/fpdfapi/parser/cpdf_security_handler.h" |
8 | 8 |
9 #include <time.h> | 9 #include <time.h> |
10 | 10 |
11 #include "core/fdrm/crypto/fx_crypt.h" | 11 #include "core/fdrm/crypto/fx_crypt.h" |
12 #include "core/fpdfapi/parser/cpdf_array.h" | 12 #include "core/fpdfapi/parser/cpdf_array.h" |
13 #include "core/fpdfapi/parser/cpdf_crypto_handler.h" | 13 #include "core/fpdfapi/parser/cpdf_crypto_handler.h" |
14 #include "core/fpdfapi/parser/cpdf_dictionary.h" | 14 #include "core/fpdfapi/parser/cpdf_dictionary.h" |
15 #include "core/fpdfapi/parser/cpdf_object.h" | 15 #include "core/fpdfapi/parser/cpdf_object.h" |
16 #include "core/fpdfapi/parser/cpdf_parser.h" | 16 #include "core/fpdfapi/parser/cpdf_parser.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 const uint8_t defpasscode[32] = { | 20 const uint8_t defpasscode[32] = { |
21 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, 0x64, 0x00, 0x4e, | 21 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, 0x64, 0x00, 0x4e, |
22 0x56, 0xff, 0xfa, 0x01, 0x08, 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, | 22 0x56, 0xff, 0xfa, 0x01, 0x08, 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, |
23 0x3e, 0x80, 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a}; | 23 0x3e, 0x80, 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a}; |
24 | 24 |
25 void CalcEncryptKey(CPDF_Dictionary* pEncrypt, | 25 void CalcEncryptKey(CPDF_Dictionary* pEncrypt, |
26 const uint8_t* password, | 26 const uint8_t* password, |
27 uint32_t pass_size, | 27 uint32_t pass_size, |
28 uint8_t* key, | 28 uint8_t* key, |
29 int keylen, | 29 int keylen, |
30 FX_BOOL bIgnoreMeta, | 30 bool bIgnoreMeta, |
31 CPDF_Array* pIdArray) { | 31 CPDF_Array* pIdArray) { |
32 int revision = pEncrypt->GetIntegerFor("R"); | 32 int revision = pEncrypt->GetIntegerFor("R"); |
33 uint8_t passcode[32]; | 33 uint8_t passcode[32]; |
34 for (uint32_t i = 0; i < 32; i++) { | 34 for (uint32_t i = 0; i < 32; i++) { |
35 passcode[i] = i < pass_size ? password[i] : defpasscode[i - pass_size]; | 35 passcode[i] = i < pass_size ? password[i] : defpasscode[i - pass_size]; |
36 } | 36 } |
37 uint8_t md5[100]; | 37 uint8_t md5[100]; |
38 CRYPT_MD5Start(md5); | 38 CRYPT_MD5Start(md5); |
39 CRYPT_MD5Update(md5, passcode, 32); | 39 CRYPT_MD5Update(md5, passcode, 32); |
40 CFX_ByteString okey = pEncrypt->GetStringFor("O"); | 40 CFX_ByteString okey = pEncrypt->GetStringFor("O"); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 m_Cipher(FXCIPHER_NONE), | 76 m_Cipher(FXCIPHER_NONE), |
77 m_KeyLen(0), | 77 m_KeyLen(0), |
78 m_bOwnerUnlocked(false) {} | 78 m_bOwnerUnlocked(false) {} |
79 | 79 |
80 CPDF_SecurityHandler::~CPDF_SecurityHandler() {} | 80 CPDF_SecurityHandler::~CPDF_SecurityHandler() {} |
81 | 81 |
82 CPDF_CryptoHandler* CPDF_SecurityHandler::CreateCryptoHandler() { | 82 CPDF_CryptoHandler* CPDF_SecurityHandler::CreateCryptoHandler() { |
83 return new CPDF_CryptoHandler; | 83 return new CPDF_CryptoHandler; |
84 } | 84 } |
85 | 85 |
86 FX_BOOL CPDF_SecurityHandler::OnInit(CPDF_Parser* pParser, | 86 bool CPDF_SecurityHandler::OnInit(CPDF_Parser* pParser, |
87 CPDF_Dictionary* pEncryptDict) { | 87 CPDF_Dictionary* pEncryptDict) { |
88 m_pParser = pParser; | 88 m_pParser = pParser; |
89 if (!LoadDict(pEncryptDict)) { | 89 if (!LoadDict(pEncryptDict)) { |
90 return FALSE; | 90 return false; |
91 } | 91 } |
92 if (m_Cipher == FXCIPHER_NONE) { | 92 if (m_Cipher == FXCIPHER_NONE) { |
93 return TRUE; | 93 return true; |
94 } | 94 } |
95 return CheckSecurity(m_KeyLen); | 95 return CheckSecurity(m_KeyLen); |
96 } | 96 } |
97 | 97 |
98 FX_BOOL CPDF_SecurityHandler::CheckSecurity(int32_t key_len) { | 98 bool CPDF_SecurityHandler::CheckSecurity(int32_t key_len) { |
99 CFX_ByteString password = m_pParser->GetPassword(); | 99 CFX_ByteString password = m_pParser->GetPassword(); |
100 if (!password.IsEmpty() && | 100 if (!password.IsEmpty() && |
101 CheckPassword(password.raw_str(), password.GetLength(), TRUE, | 101 CheckPassword(password.raw_str(), password.GetLength(), true, |
102 m_EncryptKey, key_len)) { | 102 m_EncryptKey, key_len)) { |
103 m_bOwnerUnlocked = true; | 103 m_bOwnerUnlocked = true; |
104 return TRUE; | 104 return true; |
105 } | 105 } |
106 return CheckPassword(password.raw_str(), password.GetLength(), FALSE, | 106 return CheckPassword(password.raw_str(), password.GetLength(), false, |
107 m_EncryptKey, key_len); | 107 m_EncryptKey, key_len); |
108 } | 108 } |
109 | 109 |
110 uint32_t CPDF_SecurityHandler::GetPermissions() { | 110 uint32_t CPDF_SecurityHandler::GetPermissions() { |
111 return m_bOwnerUnlocked ? 0xFFFFFFFF : m_Permissions; | 111 return m_bOwnerUnlocked ? 0xFFFFFFFF : m_Permissions; |
112 } | 112 } |
113 | 113 |
114 static FX_BOOL LoadCryptInfo(CPDF_Dictionary* pEncryptDict, | 114 static bool LoadCryptInfo(CPDF_Dictionary* pEncryptDict, |
115 const CFX_ByteString& name, | 115 const CFX_ByteString& name, |
116 int& cipher, | 116 int& cipher, |
117 int& keylen) { | 117 int& keylen) { |
118 int Version = pEncryptDict->GetIntegerFor("V"); | 118 int Version = pEncryptDict->GetIntegerFor("V"); |
119 cipher = FXCIPHER_RC4; | 119 cipher = FXCIPHER_RC4; |
120 keylen = 0; | 120 keylen = 0; |
121 if (Version >= 4) { | 121 if (Version >= 4) { |
122 CPDF_Dictionary* pCryptFilters = pEncryptDict->GetDictFor("CF"); | 122 CPDF_Dictionary* pCryptFilters = pEncryptDict->GetDictFor("CF"); |
123 if (!pCryptFilters) { | 123 if (!pCryptFilters) { |
124 return FALSE; | 124 return false; |
125 } | 125 } |
126 if (name == "Identity") { | 126 if (name == "Identity") { |
127 cipher = FXCIPHER_NONE; | 127 cipher = FXCIPHER_NONE; |
128 } else { | 128 } else { |
129 CPDF_Dictionary* pDefFilter = pCryptFilters->GetDictFor(name); | 129 CPDF_Dictionary* pDefFilter = pCryptFilters->GetDictFor(name); |
130 if (!pDefFilter) { | 130 if (!pDefFilter) { |
131 return FALSE; | 131 return false; |
132 } | 132 } |
133 int nKeyBits = 0; | 133 int nKeyBits = 0; |
134 if (Version == 4) { | 134 if (Version == 4) { |
135 nKeyBits = pDefFilter->GetIntegerFor("Length", 0); | 135 nKeyBits = pDefFilter->GetIntegerFor("Length", 0); |
136 if (nKeyBits == 0) { | 136 if (nKeyBits == 0) { |
137 nKeyBits = pEncryptDict->GetIntegerFor("Length", 128); | 137 nKeyBits = pEncryptDict->GetIntegerFor("Length", 128); |
138 } | 138 } |
139 } else { | 139 } else { |
140 nKeyBits = pEncryptDict->GetIntegerFor("Length", 256); | 140 nKeyBits = pEncryptDict->GetIntegerFor("Length", 256); |
141 } | 141 } |
142 if (nKeyBits < 40) { | 142 if (nKeyBits < 40) { |
143 nKeyBits *= 8; | 143 nKeyBits *= 8; |
144 } | 144 } |
145 keylen = nKeyBits / 8; | 145 keylen = nKeyBits / 8; |
146 CFX_ByteString cipher_name = pDefFilter->GetStringFor("CFM"); | 146 CFX_ByteString cipher_name = pDefFilter->GetStringFor("CFM"); |
147 if (cipher_name == "AESV2" || cipher_name == "AESV3") { | 147 if (cipher_name == "AESV2" || cipher_name == "AESV3") { |
148 cipher = FXCIPHER_AES; | 148 cipher = FXCIPHER_AES; |
149 } | 149 } |
150 } | 150 } |
151 } else { | 151 } else { |
152 keylen = Version > 1 ? pEncryptDict->GetIntegerFor("Length", 40) / 8 : 5; | 152 keylen = Version > 1 ? pEncryptDict->GetIntegerFor("Length", 40) / 8 : 5; |
153 } | 153 } |
154 if (keylen > 32 || keylen < 0) { | 154 if (keylen > 32 || keylen < 0) { |
155 return FALSE; | 155 return false; |
156 } | 156 } |
157 return TRUE; | 157 return true; |
158 } | 158 } |
159 | 159 |
160 FX_BOOL CPDF_SecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict) { | 160 bool CPDF_SecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict) { |
161 m_pEncryptDict = pEncryptDict; | 161 m_pEncryptDict = pEncryptDict; |
162 m_Version = pEncryptDict->GetIntegerFor("V"); | 162 m_Version = pEncryptDict->GetIntegerFor("V"); |
163 m_Revision = pEncryptDict->GetIntegerFor("R"); | 163 m_Revision = pEncryptDict->GetIntegerFor("R"); |
164 m_Permissions = pEncryptDict->GetIntegerFor("P", -1); | 164 m_Permissions = pEncryptDict->GetIntegerFor("P", -1); |
165 if (m_Version < 4) | 165 if (m_Version < 4) |
166 return LoadCryptInfo(pEncryptDict, CFX_ByteString(), m_Cipher, m_KeyLen); | 166 return LoadCryptInfo(pEncryptDict, CFX_ByteString(), m_Cipher, m_KeyLen); |
167 | 167 |
168 CFX_ByteString stmf_name = pEncryptDict->GetStringFor("StmF"); | 168 CFX_ByteString stmf_name = pEncryptDict->GetStringFor("StmF"); |
169 CFX_ByteString strf_name = pEncryptDict->GetStringFor("StrF"); | 169 CFX_ByteString strf_name = pEncryptDict->GetStringFor("StrF"); |
170 if (stmf_name != strf_name) | 170 if (stmf_name != strf_name) |
171 return FALSE; | 171 return false; |
172 | 172 |
173 return LoadCryptInfo(pEncryptDict, strf_name, m_Cipher, m_KeyLen); | 173 return LoadCryptInfo(pEncryptDict, strf_name, m_Cipher, m_KeyLen); |
174 } | 174 } |
175 | 175 |
176 FX_BOOL CPDF_SecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict, | 176 bool CPDF_SecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict, |
177 uint32_t type, | 177 uint32_t type, |
178 int& cipher, | 178 int& cipher, |
179 int& key_len) { | 179 int& key_len) { |
180 m_pEncryptDict = pEncryptDict; | 180 m_pEncryptDict = pEncryptDict; |
181 m_Version = pEncryptDict->GetIntegerFor("V"); | 181 m_Version = pEncryptDict->GetIntegerFor("V"); |
182 m_Revision = pEncryptDict->GetIntegerFor("R"); | 182 m_Revision = pEncryptDict->GetIntegerFor("R"); |
183 m_Permissions = pEncryptDict->GetIntegerFor("P", -1); | 183 m_Permissions = pEncryptDict->GetIntegerFor("P", -1); |
184 | 184 |
185 CFX_ByteString strf_name; | 185 CFX_ByteString strf_name; |
186 CFX_ByteString stmf_name; | 186 CFX_ByteString stmf_name; |
187 if (m_Version >= 4) { | 187 if (m_Version >= 4) { |
188 stmf_name = pEncryptDict->GetStringFor("StmF"); | 188 stmf_name = pEncryptDict->GetStringFor("StmF"); |
189 strf_name = pEncryptDict->GetStringFor("StrF"); | 189 strf_name = pEncryptDict->GetStringFor("StrF"); |
190 if (stmf_name != strf_name) | 190 if (stmf_name != strf_name) |
191 return FALSE; | 191 return false; |
192 } | 192 } |
193 if (!LoadCryptInfo(pEncryptDict, strf_name, cipher, key_len)) | 193 if (!LoadCryptInfo(pEncryptDict, strf_name, cipher, key_len)) |
194 return FALSE; | 194 return false; |
195 | 195 |
196 m_Cipher = cipher; | 196 m_Cipher = cipher; |
197 m_KeyLen = key_len; | 197 m_KeyLen = key_len; |
198 return TRUE; | 198 return true; |
199 } | 199 } |
200 | 200 |
201 FX_BOOL CPDF_SecurityHandler::GetCryptInfo(int& cipher, | 201 bool CPDF_SecurityHandler::GetCryptInfo(int& cipher, |
202 const uint8_t*& buffer, | 202 const uint8_t*& buffer, |
203 int& keylen) { | 203 int& keylen) { |
204 cipher = m_Cipher; | 204 cipher = m_Cipher; |
205 buffer = m_EncryptKey; | 205 buffer = m_EncryptKey; |
206 keylen = m_KeyLen; | 206 keylen = m_KeyLen; |
207 return TRUE; | 207 return true; |
208 } | 208 } |
209 #define FX_GET_32WORD(n, b, i) \ | 209 #define FX_GET_32WORD(n, b, i) \ |
210 { \ | 210 { \ |
211 (n) = (uint32_t)( \ | 211 (n) = (uint32_t)( \ |
212 ((uint64_t)(b)[(i)] << 24) | ((uint64_t)(b)[(i) + 1] << 16) | \ | 212 ((uint64_t)(b)[(i)] << 24) | ((uint64_t)(b)[(i) + 1] << 16) | \ |
213 ((uint64_t)(b)[(i) + 2] << 8) | ((uint64_t)(b)[(i) + 3])); \ | 213 ((uint64_t)(b)[(i) + 2] << 8) | ((uint64_t)(b)[(i) + 3])); \ |
214 } | 214 } |
215 int BigOrder64BitsMod3(uint8_t* data) { | 215 int BigOrder64BitsMod3(uint8_t* data) { |
216 uint64_t ret = 0; | 216 uint64_t ret = 0; |
217 for (int i = 0; i < 4; ++i) { | 217 for (int i = 0; i < 4; ++i) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 buf.EstimateSize(iBufLen); | 256 buf.EstimateSize(iBufLen); |
257 E = buf.GetBuffer(); | 257 E = buf.GetBuffer(); |
258 CFX_ByteTextBuf content; | 258 CFX_ByteTextBuf content; |
259 for (int j = 0; j < 64; ++j) { | 259 for (int j = 0; j < 64; ++j) { |
260 content.AppendBlock(password, size); | 260 content.AppendBlock(password, size); |
261 content.AppendBlock(input, iBlockSize); | 261 content.AppendBlock(input, iBlockSize); |
262 if (vector) { | 262 if (vector) { |
263 content.AppendBlock(vector, 48); | 263 content.AppendBlock(vector, 48); |
264 } | 264 } |
265 } | 265 } |
266 CRYPT_AESSetKey(aes, 16, key, 16, TRUE); | 266 CRYPT_AESSetKey(aes, 16, key, 16, true); |
267 CRYPT_AESSetIV(aes, iv); | 267 CRYPT_AESSetIV(aes, iv); |
268 CRYPT_AESEncrypt(aes, E, content.GetBuffer(), iBufLen); | 268 CRYPT_AESEncrypt(aes, E, content.GetBuffer(), iBufLen); |
269 int iHash = 0; | 269 int iHash = 0; |
270 switch (BigOrder64BitsMod3(E)) { | 270 switch (BigOrder64BitsMod3(E)) { |
271 case 0: | 271 case 0: |
272 iHash = 0; | 272 iHash = 0; |
273 iBlockSize = 32; | 273 iBlockSize = 32; |
274 break; | 274 break; |
275 case 1: | 275 case 1: |
276 iHash = 1; | 276 iHash = 1; |
(...skipping 15 matching lines...) Expand all Loading... |
292 } | 292 } |
293 key = input; | 293 key = input; |
294 iv = input + 16; | 294 iv = input + 16; |
295 ++i; | 295 ++i; |
296 } | 296 } |
297 FX_Free(aes); | 297 FX_Free(aes); |
298 if (hash) { | 298 if (hash) { |
299 FXSYS_memcpy(hash, input, 32); | 299 FXSYS_memcpy(hash, input, 32); |
300 } | 300 } |
301 } | 301 } |
302 FX_BOOL CPDF_SecurityHandler::AES256_CheckPassword(const uint8_t* password, | 302 bool CPDF_SecurityHandler::AES256_CheckPassword(const uint8_t* password, |
303 uint32_t size, | 303 uint32_t size, |
304 FX_BOOL bOwner, | 304 bool bOwner, |
305 uint8_t* key) { | 305 uint8_t* key) { |
306 CFX_ByteString okey = | 306 CFX_ByteString okey = |
307 m_pEncryptDict ? m_pEncryptDict->GetStringFor("O") : CFX_ByteString(); | 307 m_pEncryptDict ? m_pEncryptDict->GetStringFor("O") : CFX_ByteString(); |
308 if (okey.GetLength() < 48) { | 308 if (okey.GetLength() < 48) { |
309 return FALSE; | 309 return false; |
310 } | 310 } |
311 CFX_ByteString ukey = | 311 CFX_ByteString ukey = |
312 m_pEncryptDict ? m_pEncryptDict->GetStringFor("U") : CFX_ByteString(); | 312 m_pEncryptDict ? m_pEncryptDict->GetStringFor("U") : CFX_ByteString(); |
313 if (ukey.GetLength() < 48) { | 313 if (ukey.GetLength() < 48) { |
314 return FALSE; | 314 return false; |
315 } | 315 } |
316 const uint8_t* pkey = (bOwner ? okey : ukey).raw_str(); | 316 const uint8_t* pkey = (bOwner ? okey : ukey).raw_str(); |
317 uint8_t sha[128]; | 317 uint8_t sha[128]; |
318 uint8_t digest[32]; | 318 uint8_t digest[32]; |
319 if (m_Revision >= 6) { | 319 if (m_Revision >= 6) { |
320 Revision6_Hash(password, size, (const uint8_t*)pkey + 32, | 320 Revision6_Hash(password, size, (const uint8_t*)pkey + 32, |
321 bOwner ? ukey.raw_str() : nullptr, digest); | 321 bOwner ? ukey.raw_str() : nullptr, digest); |
322 } else { | 322 } else { |
323 CRYPT_SHA256Start(sha); | 323 CRYPT_SHA256Start(sha); |
324 CRYPT_SHA256Update(sha, password, size); | 324 CRYPT_SHA256Update(sha, password, size); |
325 CRYPT_SHA256Update(sha, pkey + 32, 8); | 325 CRYPT_SHA256Update(sha, pkey + 32, 8); |
326 if (bOwner) { | 326 if (bOwner) { |
327 CRYPT_SHA256Update(sha, ukey.raw_str(), 48); | 327 CRYPT_SHA256Update(sha, ukey.raw_str(), 48); |
328 } | 328 } |
329 CRYPT_SHA256Finish(sha, digest); | 329 CRYPT_SHA256Finish(sha, digest); |
330 } | 330 } |
331 if (FXSYS_memcmp(digest, pkey, 32) != 0) { | 331 if (FXSYS_memcmp(digest, pkey, 32) != 0) { |
332 return FALSE; | 332 return false; |
333 } | 333 } |
334 if (!key) { | 334 if (!key) { |
335 return TRUE; | 335 return true; |
336 } | 336 } |
337 if (m_Revision >= 6) { | 337 if (m_Revision >= 6) { |
338 Revision6_Hash(password, size, (const uint8_t*)pkey + 40, | 338 Revision6_Hash(password, size, (const uint8_t*)pkey + 40, |
339 bOwner ? ukey.raw_str() : nullptr, digest); | 339 bOwner ? ukey.raw_str() : nullptr, digest); |
340 } else { | 340 } else { |
341 CRYPT_SHA256Start(sha); | 341 CRYPT_SHA256Start(sha); |
342 CRYPT_SHA256Update(sha, password, size); | 342 CRYPT_SHA256Update(sha, password, size); |
343 CRYPT_SHA256Update(sha, pkey + 40, 8); | 343 CRYPT_SHA256Update(sha, pkey + 40, 8); |
344 if (bOwner) { | 344 if (bOwner) { |
345 CRYPT_SHA256Update(sha, ukey.raw_str(), 48); | 345 CRYPT_SHA256Update(sha, ukey.raw_str(), 48); |
346 } | 346 } |
347 CRYPT_SHA256Finish(sha, digest); | 347 CRYPT_SHA256Finish(sha, digest); |
348 } | 348 } |
349 CFX_ByteString ekey = m_pEncryptDict | 349 CFX_ByteString ekey = m_pEncryptDict |
350 ? m_pEncryptDict->GetStringFor(bOwner ? "OE" : "UE") | 350 ? m_pEncryptDict->GetStringFor(bOwner ? "OE" : "UE") |
351 : CFX_ByteString(); | 351 : CFX_ByteString(); |
352 if (ekey.GetLength() < 32) | 352 if (ekey.GetLength() < 32) |
353 return FALSE; | 353 return false; |
354 | 354 |
355 std::vector<uint8_t> aes(2048); | 355 std::vector<uint8_t> aes(2048); |
356 CRYPT_AESSetKey(aes.data(), 16, digest, 32, FALSE); | 356 CRYPT_AESSetKey(aes.data(), 16, digest, 32, false); |
357 uint8_t iv[16]; | 357 uint8_t iv[16]; |
358 FXSYS_memset(iv, 0, 16); | 358 FXSYS_memset(iv, 0, 16); |
359 CRYPT_AESSetIV(aes.data(), iv); | 359 CRYPT_AESSetIV(aes.data(), iv); |
360 CRYPT_AESDecrypt(aes.data(), key, ekey.raw_str(), 32); | 360 CRYPT_AESDecrypt(aes.data(), key, ekey.raw_str(), 32); |
361 CRYPT_AESSetKey(aes.data(), 16, key, 32, FALSE); | 361 CRYPT_AESSetKey(aes.data(), 16, key, 32, false); |
362 CRYPT_AESSetIV(aes.data(), iv); | 362 CRYPT_AESSetIV(aes.data(), iv); |
363 CFX_ByteString perms = m_pEncryptDict->GetStringFor("Perms"); | 363 CFX_ByteString perms = m_pEncryptDict->GetStringFor("Perms"); |
364 if (perms.IsEmpty()) | 364 if (perms.IsEmpty()) |
365 return FALSE; | 365 return false; |
366 | 366 |
367 uint8_t perms_buf[16]; | 367 uint8_t perms_buf[16]; |
368 FXSYS_memset(perms_buf, 0, sizeof(perms_buf)); | 368 FXSYS_memset(perms_buf, 0, sizeof(perms_buf)); |
369 size_t copy_len = | 369 size_t copy_len = |
370 std::min(sizeof(perms_buf), static_cast<size_t>(perms.GetLength())); | 370 std::min(sizeof(perms_buf), static_cast<size_t>(perms.GetLength())); |
371 FXSYS_memcpy(perms_buf, perms.raw_str(), copy_len); | 371 FXSYS_memcpy(perms_buf, perms.raw_str(), copy_len); |
372 uint8_t buf[16]; | 372 uint8_t buf[16]; |
373 CRYPT_AESDecrypt(aes.data(), buf, perms_buf, 16); | 373 CRYPT_AESDecrypt(aes.data(), buf, perms_buf, 16); |
374 if (buf[9] != 'a' || buf[10] != 'd' || buf[11] != 'b') | 374 if (buf[9] != 'a' || buf[10] != 'd' || buf[11] != 'b') |
375 return FALSE; | 375 return false; |
376 | 376 |
377 if (FXDWORD_GET_LSBFIRST(buf) != m_Permissions) | 377 if (FXDWORD_GET_LSBFIRST(buf) != m_Permissions) |
378 return FALSE; | 378 return false; |
379 | 379 |
380 bool encrypted = IsMetadataEncrypted(); | 380 bool encrypted = IsMetadataEncrypted(); |
381 if ((buf[8] == 'T' && !encrypted) || (buf[8] == 'F' && encrypted)) | 381 if ((buf[8] == 'T' && !encrypted) || (buf[8] == 'F' && encrypted)) |
382 return FALSE; | 382 return false; |
383 return TRUE; | 383 return true; |
384 } | 384 } |
385 | 385 |
386 FX_BOOL CPDF_SecurityHandler::CheckPassword(const uint8_t* password, | 386 bool CPDF_SecurityHandler::CheckPassword(const uint8_t* password, |
387 uint32_t size, | 387 uint32_t size, |
388 FX_BOOL bOwner, | 388 bool bOwner, |
389 uint8_t* key, | 389 uint8_t* key, |
390 int32_t key_len) { | 390 int32_t key_len) { |
391 if (m_Revision >= 5) | 391 if (m_Revision >= 5) |
392 return AES256_CheckPassword(password, size, bOwner, key); | 392 return AES256_CheckPassword(password, size, bOwner, key); |
393 | 393 |
394 uint8_t keybuf[32]; | 394 uint8_t keybuf[32]; |
395 if (!key) | 395 if (!key) |
396 key = keybuf; | 396 key = keybuf; |
397 | 397 |
398 if (bOwner) | 398 if (bOwner) |
399 return CheckOwnerPassword(password, size, key, key_len); | 399 return CheckOwnerPassword(password, size, key, key_len); |
400 | 400 |
401 return CheckUserPassword(password, size, FALSE, key, key_len) || | 401 return CheckUserPassword(password, size, false, key, key_len) || |
402 CheckUserPassword(password, size, TRUE, key, key_len); | 402 CheckUserPassword(password, size, true, key, key_len); |
403 } | 403 } |
404 FX_BOOL CPDF_SecurityHandler::CheckUserPassword(const uint8_t* password, | 404 bool CPDF_SecurityHandler::CheckUserPassword(const uint8_t* password, |
405 uint32_t pass_size, | 405 uint32_t pass_size, |
406 FX_BOOL bIgnoreEncryptMeta, | 406 bool bIgnoreEncryptMeta, |
407 uint8_t* key, | 407 uint8_t* key, |
408 int32_t key_len) { | 408 int32_t key_len) { |
409 CalcEncryptKey(m_pEncryptDict, password, pass_size, key, key_len, | 409 CalcEncryptKey(m_pEncryptDict, password, pass_size, key, key_len, |
410 bIgnoreEncryptMeta, m_pParser->GetIDArray()); | 410 bIgnoreEncryptMeta, m_pParser->GetIDArray()); |
411 CFX_ByteString ukey = | 411 CFX_ByteString ukey = |
412 m_pEncryptDict ? m_pEncryptDict->GetStringFor("U") : CFX_ByteString(); | 412 m_pEncryptDict ? m_pEncryptDict->GetStringFor("U") : CFX_ByteString(); |
413 if (ukey.GetLength() < 16) { | 413 if (ukey.GetLength() < 16) { |
414 return FALSE; | 414 return false; |
415 } | 415 } |
416 uint8_t ukeybuf[32]; | 416 uint8_t ukeybuf[32]; |
417 if (m_Revision == 2) { | 417 if (m_Revision == 2) { |
418 FXSYS_memcpy(ukeybuf, defpasscode, 32); | 418 FXSYS_memcpy(ukeybuf, defpasscode, 32); |
419 CRYPT_ArcFourCryptBlock(ukeybuf, 32, key, key_len); | 419 CRYPT_ArcFourCryptBlock(ukeybuf, 32, key, key_len); |
420 } else { | 420 } else { |
421 uint8_t test[32], tmpkey[32]; | 421 uint8_t test[32], tmpkey[32]; |
422 uint32_t copy_len = sizeof(test); | 422 uint32_t copy_len = sizeof(test); |
423 if (copy_len > (uint32_t)ukey.GetLength()) { | 423 if (copy_len > (uint32_t)ukey.GetLength()) { |
424 copy_len = ukey.GetLength(); | 424 copy_len = ukey.GetLength(); |
(...skipping 11 matching lines...) Expand all Loading... |
436 CRYPT_MD5Update(md5, defpasscode, 32); | 436 CRYPT_MD5Update(md5, defpasscode, 32); |
437 CPDF_Array* pIdArray = m_pParser->GetIDArray(); | 437 CPDF_Array* pIdArray = m_pParser->GetIDArray(); |
438 if (pIdArray) { | 438 if (pIdArray) { |
439 CFX_ByteString id = pIdArray->GetStringAt(0); | 439 CFX_ByteString id = pIdArray->GetStringAt(0); |
440 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); | 440 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); |
441 } | 441 } |
442 CRYPT_MD5Finish(md5, ukeybuf); | 442 CRYPT_MD5Finish(md5, ukeybuf); |
443 return FXSYS_memcmp(test, ukeybuf, 16) == 0; | 443 return FXSYS_memcmp(test, ukeybuf, 16) == 0; |
444 } | 444 } |
445 if (FXSYS_memcmp((void*)ukey.c_str(), ukeybuf, 16) == 0) { | 445 if (FXSYS_memcmp((void*)ukey.c_str(), ukeybuf, 16) == 0) { |
446 return TRUE; | 446 return true; |
447 } | 447 } |
448 return FALSE; | 448 return false; |
449 } | 449 } |
450 CFX_ByteString CPDF_SecurityHandler::GetUserPassword(const uint8_t* owner_pass, | 450 CFX_ByteString CPDF_SecurityHandler::GetUserPassword(const uint8_t* owner_pass, |
451 uint32_t pass_size, | 451 uint32_t pass_size, |
452 int32_t key_len) { | 452 int32_t key_len) { |
453 CFX_ByteString okey = m_pEncryptDict->GetStringFor("O"); | 453 CFX_ByteString okey = m_pEncryptDict->GetStringFor("O"); |
454 uint8_t passcode[32]; | 454 uint8_t passcode[32]; |
455 for (uint32_t i = 0; i < 32; i++) { | 455 for (uint32_t i = 0; i < 32; i++) { |
456 passcode[i] = i < pass_size ? owner_pass[i] : defpasscode[i - pass_size]; | 456 passcode[i] = i < pass_size ? owner_pass[i] : defpasscode[i - pass_size]; |
457 } | 457 } |
458 uint8_t digest[16]; | 458 uint8_t digest[16]; |
(...skipping 27 matching lines...) Expand all Loading... |
486 tempkey[j] = enckey[j] ^ static_cast<uint8_t>(i); | 486 tempkey[j] = enckey[j] ^ static_cast<uint8_t>(i); |
487 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, tempkey, key_len); | 487 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, tempkey, key_len); |
488 } | 488 } |
489 } | 489 } |
490 int len = 32; | 490 int len = 32; |
491 while (len && defpasscode[len - 1] == okeybuf[len - 1]) { | 491 while (len && defpasscode[len - 1] == okeybuf[len - 1]) { |
492 len--; | 492 len--; |
493 } | 493 } |
494 return CFX_ByteString(okeybuf, len); | 494 return CFX_ByteString(okeybuf, len); |
495 } | 495 } |
496 FX_BOOL CPDF_SecurityHandler::CheckOwnerPassword(const uint8_t* password, | 496 bool CPDF_SecurityHandler::CheckOwnerPassword(const uint8_t* password, |
497 uint32_t pass_size, | 497 uint32_t pass_size, |
498 uint8_t* key, | 498 uint8_t* key, |
499 int32_t key_len) { | 499 int32_t key_len) { |
500 CFX_ByteString user_pass = GetUserPassword(password, pass_size, key_len); | 500 CFX_ByteString user_pass = GetUserPassword(password, pass_size, key_len); |
501 if (CheckUserPassword(user_pass.raw_str(), user_pass.GetLength(), FALSE, key, | 501 if (CheckUserPassword(user_pass.raw_str(), user_pass.GetLength(), false, key, |
502 key_len)) { | 502 key_len)) { |
503 return TRUE; | 503 return true; |
504 } | 504 } |
505 return CheckUserPassword(user_pass.raw_str(), user_pass.GetLength(), TRUE, | 505 return CheckUserPassword(user_pass.raw_str(), user_pass.GetLength(), true, |
506 key, key_len); | 506 key, key_len); |
507 } | 507 } |
508 | 508 |
509 bool CPDF_SecurityHandler::IsMetadataEncrypted() const { | 509 bool CPDF_SecurityHandler::IsMetadataEncrypted() const { |
510 return m_pEncryptDict->GetBooleanFor("EncryptMetadata", true); | 510 return m_pEncryptDict->GetBooleanFor("EncryptMetadata", true); |
511 } | 511 } |
512 | 512 |
513 void CPDF_SecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, | 513 void CPDF_SecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, |
514 CPDF_Array* pIdArray, | 514 CPDF_Array* pIdArray, |
515 const uint8_t* user_pass, | 515 const uint8_t* user_pass, |
516 uint32_t user_size, | 516 uint32_t user_size, |
517 const uint8_t* owner_pass, | 517 const uint8_t* owner_pass, |
518 uint32_t owner_size, | 518 uint32_t owner_size, |
519 FX_BOOL bDefault, | 519 bool bDefault, |
520 uint32_t type) { | 520 uint32_t type) { |
521 int cipher = 0, key_len = 0; | 521 int cipher = 0, key_len = 0; |
522 if (!LoadDict(pEncryptDict, type, cipher, key_len)) { | 522 if (!LoadDict(pEncryptDict, type, cipher, key_len)) { |
523 return; | 523 return; |
524 } | 524 } |
525 if (bDefault && (!owner_pass || owner_size == 0)) { | 525 if (bDefault && (!owner_pass || owner_size == 0)) { |
526 owner_pass = user_pass; | 526 owner_pass = user_pass; |
527 owner_size = user_size; | 527 owner_size = user_size; |
528 } | 528 } |
529 if (m_Revision >= 5) { | 529 if (m_Revision >= 5) { |
530 int t = (int)time(nullptr); | 530 int t = (int)time(nullptr); |
531 uint8_t sha[128]; | 531 uint8_t sha[128]; |
532 CRYPT_SHA256Start(sha); | 532 CRYPT_SHA256Start(sha); |
533 CRYPT_SHA256Update(sha, (uint8_t*)&t, sizeof t); | 533 CRYPT_SHA256Update(sha, (uint8_t*)&t, sizeof t); |
534 CRYPT_SHA256Update(sha, m_EncryptKey, 32); | 534 CRYPT_SHA256Update(sha, m_EncryptKey, 32); |
535 CRYPT_SHA256Update(sha, (uint8_t*)"there", 5); | 535 CRYPT_SHA256Update(sha, (uint8_t*)"there", 5); |
536 CRYPT_SHA256Finish(sha, m_EncryptKey); | 536 CRYPT_SHA256Finish(sha, m_EncryptKey); |
537 AES256_SetPassword(pEncryptDict, user_pass, user_size, FALSE, m_EncryptKey); | 537 AES256_SetPassword(pEncryptDict, user_pass, user_size, false, m_EncryptKey); |
538 if (bDefault) { | 538 if (bDefault) { |
539 AES256_SetPassword(pEncryptDict, owner_pass, owner_size, TRUE, | 539 AES256_SetPassword(pEncryptDict, owner_pass, owner_size, true, |
540 m_EncryptKey); | 540 m_EncryptKey); |
541 AES256_SetPerms(pEncryptDict, m_Permissions, | 541 AES256_SetPerms(pEncryptDict, m_Permissions, |
542 pEncryptDict->GetBooleanFor("EncryptMetadata", true), | 542 pEncryptDict->GetBooleanFor("EncryptMetadata", true), |
543 m_EncryptKey); | 543 m_EncryptKey); |
544 } | 544 } |
545 return; | 545 return; |
546 } | 546 } |
547 if (bDefault) { | 547 if (bDefault) { |
548 uint8_t passcode[32]; | 548 uint8_t passcode[32]; |
549 for (uint32_t i = 0; i < 32; i++) { | 549 for (uint32_t i = 0; i < 32; i++) { |
(...skipping 16 matching lines...) Expand all Loading... |
566 if (m_Revision >= 3) { | 566 if (m_Revision >= 3) { |
567 for (uint8_t i = 1; i <= 19; i++) { | 567 for (uint8_t i = 1; i <= 19; i++) { |
568 for (int j = 0; j < key_len; j++) | 568 for (int j = 0; j < key_len; j++) |
569 tempkey[j] = enckey[j] ^ i; | 569 tempkey[j] = enckey[j] ^ i; |
570 CRYPT_ArcFourCryptBlock(passcode, 32, tempkey, key_len); | 570 CRYPT_ArcFourCryptBlock(passcode, 32, tempkey, key_len); |
571 } | 571 } |
572 } | 572 } |
573 pEncryptDict->SetStringFor("O", CFX_ByteString(passcode, 32)); | 573 pEncryptDict->SetStringFor("O", CFX_ByteString(passcode, 32)); |
574 } | 574 } |
575 CalcEncryptKey(m_pEncryptDict, (uint8_t*)user_pass, user_size, m_EncryptKey, | 575 CalcEncryptKey(m_pEncryptDict, (uint8_t*)user_pass, user_size, m_EncryptKey, |
576 key_len, FALSE, pIdArray); | 576 key_len, false, pIdArray); |
577 if (m_Revision < 3) { | 577 if (m_Revision < 3) { |
578 uint8_t tempbuf[32]; | 578 uint8_t tempbuf[32]; |
579 FXSYS_memcpy(tempbuf, defpasscode, 32); | 579 FXSYS_memcpy(tempbuf, defpasscode, 32); |
580 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); | 580 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); |
581 pEncryptDict->SetStringFor("U", CFX_ByteString(tempbuf, 32)); | 581 pEncryptDict->SetStringFor("U", CFX_ByteString(tempbuf, 32)); |
582 } else { | 582 } else { |
583 uint8_t md5[100]; | 583 uint8_t md5[100]; |
584 CRYPT_MD5Start(md5); | 584 CRYPT_MD5Start(md5); |
585 CRYPT_MD5Update(md5, defpasscode, 32); | 585 CRYPT_MD5Update(md5, defpasscode, 32); |
586 if (pIdArray) { | 586 if (pIdArray) { |
(...skipping 15 matching lines...) Expand all Loading... |
602 } | 602 } |
603 } | 603 } |
604 void CPDF_SecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, | 604 void CPDF_SecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, |
605 CPDF_Array* pIdArray, | 605 CPDF_Array* pIdArray, |
606 const uint8_t* user_pass, | 606 const uint8_t* user_pass, |
607 uint32_t user_size, | 607 uint32_t user_size, |
608 const uint8_t* owner_pass, | 608 const uint8_t* owner_pass, |
609 uint32_t owner_size, | 609 uint32_t owner_size, |
610 uint32_t type) { | 610 uint32_t type) { |
611 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, owner_pass, owner_size, | 611 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, owner_pass, owner_size, |
612 TRUE, type); | 612 true, type); |
613 } | 613 } |
614 void CPDF_SecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, | 614 void CPDF_SecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, |
615 CPDF_Array* pIdArray, | 615 CPDF_Array* pIdArray, |
616 const uint8_t* user_pass, | 616 const uint8_t* user_pass, |
617 uint32_t user_size, | 617 uint32_t user_size, |
618 uint32_t type) { | 618 uint32_t type) { |
619 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, nullptr, 0, FALSE, | 619 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, nullptr, 0, false, |
620 type); | 620 type); |
621 } | 621 } |
622 void CPDF_SecurityHandler::AES256_SetPassword(CPDF_Dictionary* pEncryptDict, | 622 void CPDF_SecurityHandler::AES256_SetPassword(CPDF_Dictionary* pEncryptDict, |
623 const uint8_t* password, | 623 const uint8_t* password, |
624 uint32_t size, | 624 uint32_t size, |
625 FX_BOOL bOwner, | 625 bool bOwner, |
626 const uint8_t* key) { | 626 const uint8_t* key) { |
627 uint8_t sha[128]; | 627 uint8_t sha[128]; |
628 CRYPT_SHA1Start(sha); | 628 CRYPT_SHA1Start(sha); |
629 CRYPT_SHA1Update(sha, key, 32); | 629 CRYPT_SHA1Update(sha, key, 32); |
630 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); | 630 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); |
631 uint8_t digest[20]; | 631 uint8_t digest[20]; |
632 CRYPT_SHA1Finish(sha, digest); | 632 CRYPT_SHA1Finish(sha, digest); |
633 CFX_ByteString ukey = pEncryptDict->GetStringFor("U"); | 633 CFX_ByteString ukey = pEncryptDict->GetStringFor("U"); |
634 uint8_t digest1[48]; | 634 uint8_t digest1[48]; |
635 if (m_Revision >= 6) { | 635 if (m_Revision >= 6) { |
(...skipping 16 matching lines...) Expand all Loading... |
652 } else { | 652 } else { |
653 CRYPT_SHA256Start(sha); | 653 CRYPT_SHA256Start(sha); |
654 CRYPT_SHA256Update(sha, password, size); | 654 CRYPT_SHA256Update(sha, password, size); |
655 CRYPT_SHA256Update(sha, digest + 8, 8); | 655 CRYPT_SHA256Update(sha, digest + 8, 8); |
656 if (bOwner) { | 656 if (bOwner) { |
657 CRYPT_SHA256Update(sha, ukey.raw_str(), ukey.GetLength()); | 657 CRYPT_SHA256Update(sha, ukey.raw_str(), ukey.GetLength()); |
658 } | 658 } |
659 CRYPT_SHA256Finish(sha, digest1); | 659 CRYPT_SHA256Finish(sha, digest1); |
660 } | 660 } |
661 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 661 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
662 CRYPT_AESSetKey(aes, 16, digest1, 32, TRUE); | 662 CRYPT_AESSetKey(aes, 16, digest1, 32, true); |
663 uint8_t iv[16]; | 663 uint8_t iv[16]; |
664 FXSYS_memset(iv, 0, 16); | 664 FXSYS_memset(iv, 0, 16); |
665 CRYPT_AESSetIV(aes, iv); | 665 CRYPT_AESSetIV(aes, iv); |
666 CRYPT_AESEncrypt(aes, digest1, key, 32); | 666 CRYPT_AESEncrypt(aes, digest1, key, 32); |
667 FX_Free(aes); | 667 FX_Free(aes); |
668 pEncryptDict->SetStringFor(bOwner ? "OE" : "UE", CFX_ByteString(digest1, 32)); | 668 pEncryptDict->SetStringFor(bOwner ? "OE" : "UE", CFX_ByteString(digest1, 32)); |
669 } | 669 } |
670 void CPDF_SecurityHandler::AES256_SetPerms(CPDF_Dictionary* pEncryptDict, | 670 void CPDF_SecurityHandler::AES256_SetPerms(CPDF_Dictionary* pEncryptDict, |
671 uint32_t permissions, | 671 uint32_t permissions, |
672 FX_BOOL bEncryptMetadata, | 672 bool bEncryptMetadata, |
673 const uint8_t* key) { | 673 const uint8_t* key) { |
674 uint8_t buf[16]; | 674 uint8_t buf[16]; |
675 buf[0] = (uint8_t)permissions; | 675 buf[0] = (uint8_t)permissions; |
676 buf[1] = (uint8_t)(permissions >> 8); | 676 buf[1] = (uint8_t)(permissions >> 8); |
677 buf[2] = (uint8_t)(permissions >> 16); | 677 buf[2] = (uint8_t)(permissions >> 16); |
678 buf[3] = (uint8_t)(permissions >> 24); | 678 buf[3] = (uint8_t)(permissions >> 24); |
679 buf[4] = 0xff; | 679 buf[4] = 0xff; |
680 buf[5] = 0xff; | 680 buf[5] = 0xff; |
681 buf[6] = 0xff; | 681 buf[6] = 0xff; |
682 buf[7] = 0xff; | 682 buf[7] = 0xff; |
683 buf[8] = bEncryptMetadata ? 'T' : 'F'; | 683 buf[8] = bEncryptMetadata ? 'T' : 'F'; |
684 buf[9] = 'a'; | 684 buf[9] = 'a'; |
685 buf[10] = 'd'; | 685 buf[10] = 'd'; |
686 buf[11] = 'b'; | 686 buf[11] = 'b'; |
687 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 687 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
688 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); | 688 CRYPT_AESSetKey(aes, 16, key, 32, true); |
689 uint8_t iv[16], buf1[16]; | 689 uint8_t iv[16], buf1[16]; |
690 FXSYS_memset(iv, 0, 16); | 690 FXSYS_memset(iv, 0, 16); |
691 CRYPT_AESSetIV(aes, iv); | 691 CRYPT_AESSetIV(aes, iv); |
692 CRYPT_AESEncrypt(aes, buf1, buf, 16); | 692 CRYPT_AESEncrypt(aes, buf1, buf, 16); |
693 FX_Free(aes); | 693 FX_Free(aes); |
694 pEncryptDict->SetStringFor("Perms", CFX_ByteString(buf1, 16)); | 694 pEncryptDict->SetStringFor("Perms", CFX_ByteString(buf1, 16)); |
695 } | 695 } |
OLD | NEW |