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 <time.h> | 7 #include <time.h> |
8 #include "../../../include/fpdfapi/fpdf_parser.h" | 8 #include "../../../include/fpdfapi/fpdf_parser.h" |
9 #include "../../../include/fdrm/fx_crypt.h" | 9 #include "../../../include/fdrm/fx_crypt.h" |
10 const uint8_t defpasscode[32] = { | 10 const uint8_t defpasscode[32] = { |
11 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, | 11 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, |
12 0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08, | 12 0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08, |
13 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80, | 13 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80, |
14 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a | 14 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a |
15 }; | 15 }; |
16 void CalcEncryptKey(CPDF_Dictionary* pEncrypt, FX_LPCBYTE password, FX_DWORD pas
s_size, | 16 void CalcEncryptKey(CPDF_Dictionary* pEncrypt, const uint8_t* password, FX_DWORD
pass_size, |
17 FX_LPBYTE key, int keylen, FX_BOOL bIgnoreMeta, CPDF_Array*
pIdArray) | 17 uint8_t* key, int keylen, FX_BOOL bIgnoreMeta, CPDF_Array* p
IdArray) |
18 { | 18 { |
19 int revision = pEncrypt->GetInteger(FX_BSTRC("R")); | 19 int revision = pEncrypt->GetInteger(FX_BSTRC("R")); |
20 uint8_t passcode[32]; | 20 uint8_t passcode[32]; |
21 for (FX_DWORD i = 0; i < 32; i ++) { | 21 for (FX_DWORD i = 0; i < 32; i ++) { |
22 passcode[i] = i < pass_size ? password[i] : defpasscode[i - pass_size]; | 22 passcode[i] = i < pass_size ? password[i] : defpasscode[i - pass_size]; |
23 } | 23 } |
24 uint8_t md5[100]; | 24 uint8_t md5[100]; |
25 CRYPT_MD5Start(md5); | 25 CRYPT_MD5Start(md5); |
26 CRYPT_MD5Update(md5, passcode, 32); | 26 CRYPT_MD5Update(md5, passcode, 32); |
27 CFX_ByteString okey = pEncrypt->GetString(FX_BSTRC("O")); | 27 CFX_ByteString okey = pEncrypt->GetString(FX_BSTRC("O")); |
28 CRYPT_MD5Update(md5, (FX_LPBYTE)okey.c_str(), okey.GetLength()); | 28 CRYPT_MD5Update(md5, (uint8_t*)okey.c_str(), okey.GetLength()); |
29 FX_DWORD perm = pEncrypt->GetInteger(FX_BSTRC("P")); | 29 FX_DWORD perm = pEncrypt->GetInteger(FX_BSTRC("P")); |
30 CRYPT_MD5Update(md5, (FX_LPBYTE)&perm, 4); | 30 CRYPT_MD5Update(md5, (uint8_t*)&perm, 4); |
31 if (pIdArray) { | 31 if (pIdArray) { |
32 CFX_ByteString id = pIdArray->GetString(0); | 32 CFX_ByteString id = pIdArray->GetString(0); |
33 CRYPT_MD5Update(md5, (FX_LPBYTE)id.c_str(), id.GetLength()); | 33 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); |
34 } | 34 } |
35 if (!bIgnoreMeta && revision >= 3 && !pEncrypt->GetInteger(FX_BSTRC("Encrypt
Metadata"), 1)) { | 35 if (!bIgnoreMeta && revision >= 3 && !pEncrypt->GetInteger(FX_BSTRC("Encrypt
Metadata"), 1)) { |
36 FX_DWORD tag = (FX_DWORD) - 1; | 36 FX_DWORD tag = (FX_DWORD) - 1; |
37 CRYPT_MD5Update(md5, (FX_LPBYTE)&tag, 4); | 37 CRYPT_MD5Update(md5, (uint8_t*)&tag, 4); |
38 } | 38 } |
39 uint8_t digest[16]; | 39 uint8_t digest[16]; |
40 CRYPT_MD5Finish(md5, digest); | 40 CRYPT_MD5Finish(md5, digest); |
41 FX_DWORD copy_len = keylen; | 41 FX_DWORD copy_len = keylen; |
42 if (copy_len > sizeof(digest)) { | 42 if (copy_len > sizeof(digest)) { |
43 copy_len = sizeof(digest); | 43 copy_len = sizeof(digest); |
44 } | 44 } |
45 if (revision >= 3) { | 45 if (revision >= 3) { |
46 for (int i = 0; i < 50; i ++) { | 46 for (int i = 0; i < 50; i ++) { |
47 CRYPT_MD5Generate(digest, copy_len, digest); | 47 CRYPT_MD5Generate(digest, copy_len, digest); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 } | 182 } |
183 } | 183 } |
184 if (!_LoadCryptInfo(pEncryptDict, strf_name, cipher, key_len)) { | 184 if (!_LoadCryptInfo(pEncryptDict, strf_name, cipher, key_len)) { |
185 return FALSE; | 185 return FALSE; |
186 } | 186 } |
187 m_Cipher = cipher; | 187 m_Cipher = cipher; |
188 m_KeyLen = key_len; | 188 m_KeyLen = key_len; |
189 return TRUE; | 189 return TRUE; |
190 return TRUE; | 190 return TRUE; |
191 } | 191 } |
192 FX_BOOL CPDF_StandardSecurityHandler::GetCryptInfo(int& cipher, FX_LPCBYTE& buff
er, int& keylen) | 192 FX_BOOL CPDF_StandardSecurityHandler::GetCryptInfo(int& cipher, const uint8_t*&
buffer, int& keylen) |
193 { | 193 { |
194 cipher = m_Cipher; | 194 cipher = m_Cipher; |
195 buffer = m_EncryptKey; | 195 buffer = m_EncryptKey; |
196 keylen = m_KeyLen; | 196 keylen = m_KeyLen; |
197 return TRUE; | 197 return TRUE; |
198 } | 198 } |
199 #define FX_GET_32WORD(n,b,i)
\ | 199 #define FX_GET_32WORD(n,b,i)
\ |
200 {
\ | 200 {
\ |
201 (n) = (FX_DWORD)(( (uint64_t) (b)[(i)] << 24 ) \ | 201 (n) = (FX_DWORD)(( (uint64_t) (b)[(i)] << 24 ) \ |
202 | ( (uint64_t) (b)[(i) + 1] << 16 )
\ | 202 | ( (uint64_t) (b)[(i) + 1] << 16 )
\ |
203 | ( (uint64_t) (b)[(i) + 2] << 8 )
\ | 203 | ( (uint64_t) (b)[(i) + 2] << 8 )
\ |
204 | ( (uint64_t) (b)[(i) + 3] ));
\ | 204 | ( (uint64_t) (b)[(i) + 3] ));
\ |
205 } | 205 } |
206 int BigOrder64BitsMod3(FX_LPBYTE data) | 206 int BigOrder64BitsMod3(uint8_t* data) |
207 { | 207 { |
208 uint64_t ret = 0; | 208 uint64_t ret = 0; |
209 for (int i = 0; i < 4; ++i) { | 209 for (int i = 0; i < 4; ++i) { |
210 FX_DWORD value; | 210 FX_DWORD value; |
211 FX_GET_32WORD(value, data, 4 * i); | 211 FX_GET_32WORD(value, data, 4 * i); |
212 ret <<= 32; | 212 ret <<= 32; |
213 ret |= value; | 213 ret |= value; |
214 ret %= 3; | 214 ret %= 3; |
215 } | 215 } |
216 return (int)ret; | 216 return (int)ret; |
217 } | 217 } |
218 void Revision6_Hash(FX_LPCBYTE password, FX_DWORD size, FX_LPCBYTE salt, FX_LPCB
YTE vector, FX_LPBYTE hash) | 218 void Revision6_Hash(const uint8_t* password, FX_DWORD size, const uint8_t* salt,
const uint8_t* vector, uint8_t* hash) |
219 { | 219 { |
220 int iBlockSize = 32; | 220 int iBlockSize = 32; |
221 uint8_t sha[128]; | 221 uint8_t sha[128]; |
222 CRYPT_SHA256Start(sha); | 222 CRYPT_SHA256Start(sha); |
223 CRYPT_SHA256Update(sha, password, size); | 223 CRYPT_SHA256Update(sha, password, size); |
224 CRYPT_SHA256Update(sha, salt, 8); | 224 CRYPT_SHA256Update(sha, salt, 8); |
225 if (vector) { | 225 if (vector) { |
226 CRYPT_SHA256Update(sha, vector, 48); | 226 CRYPT_SHA256Update(sha, vector, 48); |
227 } | 227 } |
228 uint8_t digest[32]; | 228 uint8_t digest[32]; |
229 CRYPT_SHA256Finish(sha, digest); | 229 CRYPT_SHA256Finish(sha, digest); |
230 CFX_ByteTextBuf buf; | 230 CFX_ByteTextBuf buf; |
231 FX_LPBYTE input = digest; | 231 uint8_t* input = digest; |
232 FX_LPBYTE key = input; | 232 uint8_t* key = input; |
233 FX_LPBYTE iv = input + 16; | 233 uint8_t* iv = input + 16; |
234 FX_LPBYTE E = buf.GetBuffer(); | 234 uint8_t* E = buf.GetBuffer(); |
235 int iBufLen = buf.GetLength(); | 235 int iBufLen = buf.GetLength(); |
236 CFX_ByteTextBuf interDigest; | 236 CFX_ByteTextBuf interDigest; |
237 int i = 0; | 237 int i = 0; |
238 FX_LPBYTE aes = FX_Alloc(uint8_t, 2048); | 238 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
239 while (i < 64 || i < E[iBufLen - 1] + 32) { | 239 while (i < 64 || i < E[iBufLen - 1] + 32) { |
240 int iRoundSize = size + iBlockSize; | 240 int iRoundSize = size + iBlockSize; |
241 if (vector) { | 241 if (vector) { |
242 iRoundSize += 48; | 242 iRoundSize += 48; |
243 } | 243 } |
244 iBufLen = iRoundSize * 64; | 244 iBufLen = iRoundSize * 64; |
245 buf.EstimateSize(iBufLen); | 245 buf.EstimateSize(iBufLen); |
246 E = buf.GetBuffer(); | 246 E = buf.GetBuffer(); |
247 CFX_ByteTextBuf content; | 247 CFX_ByteTextBuf content; |
248 for (int j = 0; j < 64; ++j) { | 248 for (int j = 0; j < 64; ++j) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 } | 281 } |
282 key = input; | 282 key = input; |
283 iv = input + 16; | 283 iv = input + 16; |
284 ++i; | 284 ++i; |
285 } | 285 } |
286 FX_Free(aes); | 286 FX_Free(aes); |
287 if (hash) { | 287 if (hash) { |
288 FXSYS_memcpy32(hash, input, 32); | 288 FXSYS_memcpy32(hash, input, 32); |
289 } | 289 } |
290 } | 290 } |
291 FX_BOOL CPDF_StandardSecurityHandler::AES256_CheckPassword(FX_LPCBYTE password,
FX_DWORD size, | 291 FX_BOOL CPDF_StandardSecurityHandler::AES256_CheckPassword(const uint8_t* passwo
rd, FX_DWORD size, |
292 FX_BOOL bOwner, FX_LPBYTE key) | 292 FX_BOOL bOwner, uint8_t* key) |
293 { | 293 { |
294 CFX_ByteString okey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("O
")) : CFX_ByteString(); | 294 CFX_ByteString okey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("O
")) : CFX_ByteString(); |
295 if (okey.GetLength() < 48) { | 295 if (okey.GetLength() < 48) { |
296 return FALSE; | 296 return FALSE; |
297 } | 297 } |
298 CFX_ByteString ukey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("U
")) : CFX_ByteString(); | 298 CFX_ByteString ukey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("U
")) : CFX_ByteString(); |
299 if (ukey.GetLength() < 48) { | 299 if (ukey.GetLength() < 48) { |
300 return FALSE; | 300 return FALSE; |
301 } | 301 } |
302 FX_LPCBYTE pkey = bOwner ? (FX_LPCBYTE)okey : (FX_LPCBYTE)ukey; | 302 const uint8_t* pkey = bOwner ? (const uint8_t*)okey : (const uint8_t*)ukey; |
303 uint8_t sha[128]; | 303 uint8_t sha[128]; |
304 uint8_t digest[32]; | 304 uint8_t digest[32]; |
305 if (m_Revision >= 6) { | 305 if (m_Revision >= 6) { |
306 Revision6_Hash(password, size, (FX_LPCBYTE)pkey + 32, (bOwner ? (FX_LPCB
YTE)ukey : NULL), digest); | 306 Revision6_Hash(password, size, (const uint8_t*)pkey + 32, (bOwner ? (con
st uint8_t*)ukey : NULL), digest); |
307 } else { | 307 } else { |
308 CRYPT_SHA256Start(sha); | 308 CRYPT_SHA256Start(sha); |
309 CRYPT_SHA256Update(sha, password, size); | 309 CRYPT_SHA256Update(sha, password, size); |
310 CRYPT_SHA256Update(sha, pkey + 32, 8); | 310 CRYPT_SHA256Update(sha, pkey + 32, 8); |
311 if (bOwner) { | 311 if (bOwner) { |
312 CRYPT_SHA256Update(sha, ukey, 48); | 312 CRYPT_SHA256Update(sha, ukey, 48); |
313 } | 313 } |
314 CRYPT_SHA256Finish(sha, digest); | 314 CRYPT_SHA256Finish(sha, digest); |
315 } | 315 } |
316 if (FXSYS_memcmp32(digest, pkey, 32) != 0) { | 316 if (FXSYS_memcmp32(digest, pkey, 32) != 0) { |
317 return FALSE; | 317 return FALSE; |
318 } | 318 } |
319 if (key == NULL) { | 319 if (key == NULL) { |
320 return TRUE; | 320 return TRUE; |
321 } | 321 } |
322 if (m_Revision >= 6) { | 322 if (m_Revision >= 6) { |
323 Revision6_Hash(password, size, (FX_LPCBYTE)pkey + 40, (bOwner ? (FX_LPCB
YTE)ukey : NULL), digest); | 323 Revision6_Hash(password, size, (const uint8_t*)pkey + 40, (bOwner ? (con
st uint8_t*)ukey : NULL), digest); |
324 } else { | 324 } else { |
325 CRYPT_SHA256Start(sha); | 325 CRYPT_SHA256Start(sha); |
326 CRYPT_SHA256Update(sha, password, size); | 326 CRYPT_SHA256Update(sha, password, size); |
327 CRYPT_SHA256Update(sha, pkey + 40, 8); | 327 CRYPT_SHA256Update(sha, pkey + 40, 8); |
328 if (bOwner) { | 328 if (bOwner) { |
329 CRYPT_SHA256Update(sha, ukey, 48); | 329 CRYPT_SHA256Update(sha, ukey, 48); |
330 } | 330 } |
331 CRYPT_SHA256Finish(sha, digest); | 331 CRYPT_SHA256Finish(sha, digest); |
332 } | 332 } |
333 CFX_ByteString ekey = m_pEncryptDict ? m_pEncryptDict->GetString(bOwner ? FX
_BSTRC("OE") : FX_BSTRC("UE")) : CFX_ByteString(); | 333 CFX_ByteString ekey = m_pEncryptDict ? m_pEncryptDict->GetString(bOwner ? FX
_BSTRC("OE") : FX_BSTRC("UE")) : CFX_ByteString(); |
(...skipping 11 matching lines...) Expand all Loading... |
345 CFX_ByteString perms = m_pEncryptDict->GetString(FX_BSTRC("Perms")); | 345 CFX_ByteString perms = m_pEncryptDict->GetString(FX_BSTRC("Perms")); |
346 if (perms.IsEmpty()) { | 346 if (perms.IsEmpty()) { |
347 return FALSE; | 347 return FALSE; |
348 } | 348 } |
349 uint8_t perms_buf[16]; | 349 uint8_t perms_buf[16]; |
350 FXSYS_memset32(perms_buf, 0, sizeof(perms_buf)); | 350 FXSYS_memset32(perms_buf, 0, sizeof(perms_buf)); |
351 FX_DWORD copy_len = sizeof(perms_buf); | 351 FX_DWORD copy_len = sizeof(perms_buf); |
352 if (copy_len > (FX_DWORD)perms.GetLength()) { | 352 if (copy_len > (FX_DWORD)perms.GetLength()) { |
353 copy_len = perms.GetLength(); | 353 copy_len = perms.GetLength(); |
354 } | 354 } |
355 FXSYS_memcpy32(perms_buf, (FX_LPCBYTE)perms, copy_len); | 355 FXSYS_memcpy32(perms_buf, (const uint8_t*)perms, copy_len); |
356 uint8_t buf[16]; | 356 uint8_t buf[16]; |
357 CRYPT_AESDecrypt(aes, buf, perms_buf, 16); | 357 CRYPT_AESDecrypt(aes, buf, perms_buf, 16); |
358 FX_Free(aes); | 358 FX_Free(aes); |
359 if (buf[9] != 'a' || buf[10] != 'd' || buf[11] != 'b') { | 359 if (buf[9] != 'a' || buf[10] != 'd' || buf[11] != 'b') { |
360 return FALSE; | 360 return FALSE; |
361 } | 361 } |
362 if (FXDWORD_GET_LSBFIRST(buf) != m_Permissions) { | 362 if (FXDWORD_GET_LSBFIRST(buf) != m_Permissions) { |
363 return FALSE; | 363 return FALSE; |
364 } | 364 } |
365 if ((buf[8] == 'T' && !IsMetadataEncrypted()) || (buf[8] == 'F' && IsMetadat
aEncrypted())) { | 365 if ((buf[8] == 'T' && !IsMetadataEncrypted()) || (buf[8] == 'F' && IsMetadat
aEncrypted())) { |
366 return FALSE; | 366 return FALSE; |
367 } | 367 } |
368 return TRUE; | 368 return TRUE; |
369 } | 369 } |
370 int CPDF_StandardSecurityHandler::CheckPassword(FX_LPCBYTE password, FX_DWORD pa
ss_size, FX_BOOL bOwner, FX_LPBYTE key) | 370 int CPDF_StandardSecurityHandler::CheckPassword(const uint8_t* password, FX_DWOR
D pass_size, FX_BOOL bOwner, uint8_t* key) |
371 { | 371 { |
372 return CheckPassword(password, pass_size, bOwner, key, m_KeyLen); | 372 return CheckPassword(password, pass_size, bOwner, key, m_KeyLen); |
373 } | 373 } |
374 int CPDF_StandardSecurityHandler::CheckPassword(FX_LPCBYTE password, FX_DWORD si
ze, FX_BOOL bOwner, FX_LPBYTE key, int32_t key_len) | 374 int CPDF_StandardSecurityHandler::CheckPassword(const uint8_t* password, FX_DWOR
D size, FX_BOOL bOwner, uint8_t* key, int32_t key_len) |
375 { | 375 { |
376 if (m_Revision >= 5) { | 376 if (m_Revision >= 5) { |
377 return AES256_CheckPassword(password, size, bOwner, key); | 377 return AES256_CheckPassword(password, size, bOwner, key); |
378 } | 378 } |
379 uint8_t keybuf[32]; | 379 uint8_t keybuf[32]; |
380 if (key == NULL) { | 380 if (key == NULL) { |
381 key = keybuf; | 381 key = keybuf; |
382 } | 382 } |
383 if (bOwner) { | 383 if (bOwner) { |
384 return CheckOwnerPassword(password, size, key, key_len); | 384 return CheckOwnerPassword(password, size, key, key_len); |
385 } | 385 } |
386 return CheckUserPassword(password, size, FALSE, key, key_len) || CheckUserPa
ssword(password, size, TRUE, key, key_len); | 386 return CheckUserPassword(password, size, FALSE, key, key_len) || CheckUserPa
ssword(password, size, TRUE, key, key_len); |
387 } | 387 } |
388 FX_BOOL CPDF_StandardSecurityHandler::CheckUserPassword(FX_LPCBYTE password, FX_
DWORD pass_size, | 388 FX_BOOL CPDF_StandardSecurityHandler::CheckUserPassword(const uint8_t* password,
FX_DWORD pass_size, |
389 FX_BOOL bIgnoreEncryptMeta, FX_LPBYTE key, int32_t key_len) | 389 FX_BOOL bIgnoreEncryptMeta, uint8_t* key, int32_t key_len) |
390 { | 390 { |
391 CalcEncryptKey(m_pEncryptDict, password, pass_size, key, key_len, bIgnoreEnc
ryptMeta, | 391 CalcEncryptKey(m_pEncryptDict, password, pass_size, key, key_len, bIgnoreEnc
ryptMeta, |
392 m_pParser->GetIDArray()); | 392 m_pParser->GetIDArray()); |
393 CFX_ByteString ukey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("U
")) : CFX_ByteString(); | 393 CFX_ByteString ukey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("U
")) : CFX_ByteString(); |
394 if (ukey.GetLength() < 16) { | 394 if (ukey.GetLength() < 16) { |
395 return FALSE; | 395 return FALSE; |
396 } | 396 } |
397 uint8_t ukeybuf[32]; | 397 uint8_t ukeybuf[32]; |
398 if (m_Revision == 2) { | 398 if (m_Revision == 2) { |
399 FXSYS_memcpy32(ukeybuf, defpasscode, 32); | 399 FXSYS_memcpy32(ukeybuf, defpasscode, 32); |
(...skipping 12 matching lines...) Expand all Loading... |
412 tmpkey[j] = key[j] ^ i; | 412 tmpkey[j] = key[j] ^ i; |
413 } | 413 } |
414 CRYPT_ArcFourCryptBlock(test, 32, tmpkey, key_len); | 414 CRYPT_ArcFourCryptBlock(test, 32, tmpkey, key_len); |
415 } | 415 } |
416 uint8_t md5[100]; | 416 uint8_t md5[100]; |
417 CRYPT_MD5Start(md5); | 417 CRYPT_MD5Start(md5); |
418 CRYPT_MD5Update(md5, defpasscode, 32); | 418 CRYPT_MD5Update(md5, defpasscode, 32); |
419 CPDF_Array* pIdArray = m_pParser->GetIDArray(); | 419 CPDF_Array* pIdArray = m_pParser->GetIDArray(); |
420 if (pIdArray) { | 420 if (pIdArray) { |
421 CFX_ByteString id = pIdArray->GetString(0); | 421 CFX_ByteString id = pIdArray->GetString(0); |
422 CRYPT_MD5Update(md5, (FX_LPBYTE)id.c_str(), id.GetLength()); | 422 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); |
423 } | 423 } |
424 CRYPT_MD5Finish(md5, ukeybuf); | 424 CRYPT_MD5Finish(md5, ukeybuf); |
425 return FXSYS_memcmp32(test, ukeybuf, 16) == 0; | 425 return FXSYS_memcmp32(test, ukeybuf, 16) == 0; |
426 } | 426 } |
427 if (FXSYS_memcmp32((FX_LPVOID)ukey.c_str(), ukeybuf, 16) == 0) { | 427 if (FXSYS_memcmp32((void*)ukey.c_str(), ukeybuf, 16) == 0) { |
428 return TRUE; | 428 return TRUE; |
429 } | 429 } |
430 return FALSE; | 430 return FALSE; |
431 } | 431 } |
432 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(FX_LPCBYTE owner_pa
ss, FX_DWORD pass_size) | 432 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(const uint8_t* owne
r_pass, FX_DWORD pass_size) |
433 { | 433 { |
434 return GetUserPassword(owner_pass, pass_size, m_KeyLen); | 434 return GetUserPassword(owner_pass, pass_size, m_KeyLen); |
435 } | 435 } |
436 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(FX_LPCBYTE owner_pa
ss, FX_DWORD pass_size, int32_t key_len) | 436 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(const uint8_t* owne
r_pass, FX_DWORD pass_size, int32_t key_len) |
437 { | 437 { |
438 CFX_ByteString okey = m_pEncryptDict->GetString(FX_BSTRC("O")); | 438 CFX_ByteString okey = m_pEncryptDict->GetString(FX_BSTRC("O")); |
439 uint8_t passcode[32]; | 439 uint8_t passcode[32]; |
440 FX_DWORD i; | 440 FX_DWORD i; |
441 for (i = 0; i < 32; i ++) { | 441 for (i = 0; i < 32; i ++) { |
442 passcode[i] = i < pass_size ? owner_pass[i] : defpasscode[i - pass_size]
; | 442 passcode[i] = i < pass_size ? owner_pass[i] : defpasscode[i - pass_size]
; |
443 } | 443 } |
444 uint8_t digest[16]; | 444 uint8_t digest[16]; |
445 CRYPT_MD5Generate(passcode, 32, digest); | 445 CRYPT_MD5Generate(passcode, 32, digest); |
446 if (m_Revision >= 3) { | 446 if (m_Revision >= 3) { |
(...skipping 26 matching lines...) Expand all Loading... |
473 } | 473 } |
474 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, tempkey, key_len); | 474 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, tempkey, key_len); |
475 } | 475 } |
476 } | 476 } |
477 int len = 32; | 477 int len = 32; |
478 while (len && defpasscode[len - 1] == okeybuf[len - 1]) { | 478 while (len && defpasscode[len - 1] == okeybuf[len - 1]) { |
479 len --; | 479 len --; |
480 } | 480 } |
481 return CFX_ByteString(okeybuf, len); | 481 return CFX_ByteString(okeybuf, len); |
482 } | 482 } |
483 FX_BOOL CPDF_StandardSecurityHandler::CheckOwnerPassword(FX_LPCBYTE password, FX
_DWORD pass_size, | 483 FX_BOOL CPDF_StandardSecurityHandler::CheckOwnerPassword(const uint8_t* password
, FX_DWORD pass_size, |
484 FX_LPBYTE key, int32_t key_len) | 484 uint8_t* key, int32_t key_len) |
485 { | 485 { |
486 CFX_ByteString user_pass = GetUserPassword(password, pass_size, key_len); | 486 CFX_ByteString user_pass = GetUserPassword(password, pass_size, key_len); |
487 if (CheckUserPassword(user_pass, user_pass.GetLength(), FALSE, key, key_len)
) { | 487 if (CheckUserPassword(user_pass, user_pass.GetLength(), FALSE, key, key_len)
) { |
488 return TRUE; | 488 return TRUE; |
489 } | 489 } |
490 return CheckUserPassword(user_pass, user_pass.GetLength(), TRUE, key, key_le
n); | 490 return CheckUserPassword(user_pass, user_pass.GetLength(), TRUE, key, key_le
n); |
491 } | 491 } |
492 FX_BOOL CPDF_StandardSecurityHandler::IsMetadataEncrypted() | 492 FX_BOOL CPDF_StandardSecurityHandler::IsMetadataEncrypted() |
493 { | 493 { |
494 return m_pEncryptDict->GetBoolean(FX_BSTRC("EncryptMetadata"), TRUE); | 494 return m_pEncryptDict->GetBoolean(FX_BSTRC("EncryptMetadata"), TRUE); |
495 } | 495 } |
496 CPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler() | 496 CPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler() |
497 { | 497 { |
498 return new CPDF_StandardSecurityHandler; | 498 return new CPDF_StandardSecurityHandler; |
499 } | 499 } |
500 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_
Array* pIdArray, | 500 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_
Array* pIdArray, |
501 FX_LPCBYTE user_pass, FX_DWORD user_size, | 501 const uint8_t* user_pass, FX_DWORD user_size, |
502 FX_LPCBYTE owner_pass, FX_DWORD owner_size, FX_BOOL bDefault, FX_DWORD t
ype) | 502 const uint8_t* owner_pass, FX_DWORD owner_size, FX_BOOL bDefault, FX_DWO
RD type) |
503 { | 503 { |
504 int cipher = 0, key_len = 0; | 504 int cipher = 0, key_len = 0; |
505 if (!LoadDict(pEncryptDict, type, cipher, key_len)) { | 505 if (!LoadDict(pEncryptDict, type, cipher, key_len)) { |
506 return; | 506 return; |
507 } | 507 } |
508 if (bDefault && (owner_pass == NULL || owner_size == 0)) { | 508 if (bDefault && (owner_pass == NULL || owner_size == 0)) { |
509 owner_pass = user_pass; | 509 owner_pass = user_pass; |
510 owner_size = user_size; | 510 owner_size = user_size; |
511 } | 511 } |
512 if (m_Revision >= 5) { | 512 if (m_Revision >= 5) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 if (m_Revision >= 3) { | 547 if (m_Revision >= 3) { |
548 for (i = 1; i <= 19; i ++) { | 548 for (i = 1; i <= 19; i ++) { |
549 for (int j = 0; j < key_len; j ++) { | 549 for (int j = 0; j < key_len; j ++) { |
550 tempkey[j] = enckey[j] ^ (uint8_t)i; | 550 tempkey[j] = enckey[j] ^ (uint8_t)i; |
551 } | 551 } |
552 CRYPT_ArcFourCryptBlock(passcode, 32, tempkey, key_len); | 552 CRYPT_ArcFourCryptBlock(passcode, 32, tempkey, key_len); |
553 } | 553 } |
554 } | 554 } |
555 pEncryptDict->SetAtString(FX_BSTRC("O"), CFX_ByteString(passcode, 32)); | 555 pEncryptDict->SetAtString(FX_BSTRC("O"), CFX_ByteString(passcode, 32)); |
556 } | 556 } |
557 CalcEncryptKey(m_pEncryptDict, (FX_LPBYTE)user_pass, user_size, m_EncryptKey
, key_len, FALSE, pIdArray); | 557 CalcEncryptKey(m_pEncryptDict, (uint8_t*)user_pass, user_size, m_EncryptKey,
key_len, FALSE, pIdArray); |
558 if (m_Revision < 3) { | 558 if (m_Revision < 3) { |
559 uint8_t tempbuf[32]; | 559 uint8_t tempbuf[32]; |
560 FXSYS_memcpy32(tempbuf, defpasscode, 32); | 560 FXSYS_memcpy32(tempbuf, defpasscode, 32); |
561 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); | 561 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); |
562 pEncryptDict->SetAtString(FX_BSTRC("U"), CFX_ByteString(tempbuf, 32)); | 562 pEncryptDict->SetAtString(FX_BSTRC("U"), CFX_ByteString(tempbuf, 32)); |
563 } else { | 563 } else { |
564 uint8_t md5[100]; | 564 uint8_t md5[100]; |
565 CRYPT_MD5Start(md5); | 565 CRYPT_MD5Start(md5); |
566 CRYPT_MD5Update(md5, defpasscode, 32); | 566 CRYPT_MD5Update(md5, defpasscode, 32); |
567 if (pIdArray) { | 567 if (pIdArray) { |
568 CFX_ByteString id = pIdArray->GetString(0); | 568 CFX_ByteString id = pIdArray->GetString(0); |
569 CRYPT_MD5Update(md5, (FX_LPBYTE)id.c_str(), id.GetLength()); | 569 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); |
570 } | 570 } |
571 uint8_t digest[32]; | 571 uint8_t digest[32]; |
572 CRYPT_MD5Finish(md5, digest); | 572 CRYPT_MD5Finish(md5, digest); |
573 CRYPT_ArcFourCryptBlock(digest, 16, m_EncryptKey, key_len); | 573 CRYPT_ArcFourCryptBlock(digest, 16, m_EncryptKey, key_len); |
574 uint8_t tempkey[32]; | 574 uint8_t tempkey[32]; |
575 for (int i = 1; i <= 19; i ++) { | 575 for (int i = 1; i <= 19; i ++) { |
576 for (int j = 0; j < key_len; j ++) { | 576 for (int j = 0; j < key_len; j ++) { |
577 tempkey[j] = m_EncryptKey[j] ^ (uint8_t)i; | 577 tempkey[j] = m_EncryptKey[j] ^ (uint8_t)i; |
578 } | 578 } |
579 CRYPT_ArcFourCryptBlock(digest, 16, tempkey, key_len); | 579 CRYPT_ArcFourCryptBlock(digest, 16, tempkey, key_len); |
580 } | 580 } |
581 CRYPT_MD5Generate(digest, 16, digest + 16); | 581 CRYPT_MD5Generate(digest, 16, digest + 16); |
582 pEncryptDict->SetAtString(FX_BSTRC("U"), CFX_ByteString(digest, 32)); | 582 pEncryptDict->SetAtString(FX_BSTRC("U"), CFX_ByteString(digest, 32)); |
583 } | 583 } |
584 } | 584 } |
585 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_
Array* pIdArray, | 585 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_
Array* pIdArray, |
586 FX_LPCBYTE user_pass, FX_DWORD user_size, | 586 const uint8_t* user_pass, FX_DWORD user_size, |
587 FX_LPCBYTE owner_pass, FX_DWORD owner_size, FX_DWORD type) | 587 const uint8_t* owner_pass, FX_DWORD owner_size, FX_DWORD type) |
588 { | 588 { |
589 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, owner_pass, owner_siz
e, TRUE, type); | 589 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, owner_pass, owner_siz
e, TRUE, type); |
590 } | 590 } |
591 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_
Array* pIdArray, FX_LPCBYTE user_pass, FX_DWORD user_size, FX_DWORD type) | 591 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_
Array* pIdArray, const uint8_t* user_pass, FX_DWORD user_size, FX_DWORD type) |
592 { | 592 { |
593 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, NULL, 0, FALSE, type)
; | 593 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, NULL, 0, FALSE, type)
; |
594 } | 594 } |
595 void CPDF_StandardSecurityHandler::AES256_SetPassword(CPDF_Dictionary* pEncryptD
ict, FX_LPCBYTE password, FX_DWORD size, FX_BOOL bOwner, FX_LPCBYTE key) | 595 void CPDF_StandardSecurityHandler::AES256_SetPassword(CPDF_Dictionary* pEncryptD
ict, const uint8_t* password, FX_DWORD size, FX_BOOL bOwner, const uint8_t* key) |
596 { | 596 { |
597 uint8_t sha[128]; | 597 uint8_t sha[128]; |
598 CRYPT_SHA1Start(sha); | 598 CRYPT_SHA1Start(sha); |
599 CRYPT_SHA1Update(sha, key, 32); | 599 CRYPT_SHA1Update(sha, key, 32); |
600 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); | 600 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); |
601 uint8_t digest[20]; | 601 uint8_t digest[20]; |
602 CRYPT_SHA1Finish(sha, digest); | 602 CRYPT_SHA1Finish(sha, digest); |
603 CFX_ByteString ukey = pEncryptDict->GetString(FX_BSTRC("U")); | 603 CFX_ByteString ukey = pEncryptDict->GetString(FX_BSTRC("U")); |
604 uint8_t digest1[48]; | 604 uint8_t digest1[48]; |
605 if (m_Revision >= 6) { | 605 if (m_Revision >= 6) { |
606 Revision6_Hash(password, size, digest, (bOwner ? (FX_LPCBYTE)ukey : NULL
), digest1); | 606 Revision6_Hash(password, size, digest, (bOwner ? (const uint8_t*)ukey :
NULL), digest1); |
607 } else { | 607 } else { |
608 CRYPT_SHA256Start(sha); | 608 CRYPT_SHA256Start(sha); |
609 CRYPT_SHA256Update(sha, password, size); | 609 CRYPT_SHA256Update(sha, password, size); |
610 CRYPT_SHA256Update(sha, digest, 8); | 610 CRYPT_SHA256Update(sha, digest, 8); |
611 if (bOwner) { | 611 if (bOwner) { |
612 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); | 612 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); |
613 } | 613 } |
614 CRYPT_SHA256Finish(sha, digest1); | 614 CRYPT_SHA256Finish(sha, digest1); |
615 } | 615 } |
616 FXSYS_memcpy32(digest1 + 32, digest, 16); | 616 FXSYS_memcpy32(digest1 + 32, digest, 16); |
617 pEncryptDict->SetAtString(bOwner ? FX_BSTRC("O") : FX_BSTRC("U"), CFX_ByteSt
ring(digest1, 48)); | 617 pEncryptDict->SetAtString(bOwner ? FX_BSTRC("O") : FX_BSTRC("U"), CFX_ByteSt
ring(digest1, 48)); |
618 if (m_Revision >= 6) { | 618 if (m_Revision >= 6) { |
619 Revision6_Hash(password, size, digest + 8, (bOwner ? (FX_LPCBYTE)ukey :
NULL), digest1); | 619 Revision6_Hash(password, size, digest + 8, (bOwner ? (const uint8_t*)uke
y : NULL), digest1); |
620 } else { | 620 } else { |
621 CRYPT_SHA256Start(sha); | 621 CRYPT_SHA256Start(sha); |
622 CRYPT_SHA256Update(sha, password, size); | 622 CRYPT_SHA256Update(sha, password, size); |
623 CRYPT_SHA256Update(sha, digest + 8, 8); | 623 CRYPT_SHA256Update(sha, digest + 8, 8); |
624 if (bOwner) { | 624 if (bOwner) { |
625 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); | 625 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); |
626 } | 626 } |
627 CRYPT_SHA256Finish(sha, digest1); | 627 CRYPT_SHA256Finish(sha, digest1); |
628 } | 628 } |
629 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 629 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
630 CRYPT_AESSetKey(aes, 16, digest1, 32, TRUE); | 630 CRYPT_AESSetKey(aes, 16, digest1, 32, TRUE); |
631 uint8_t iv[16]; | 631 uint8_t iv[16]; |
632 FXSYS_memset32(iv, 0, 16); | 632 FXSYS_memset32(iv, 0, 16); |
633 CRYPT_AESSetIV(aes, iv); | 633 CRYPT_AESSetIV(aes, iv); |
634 CRYPT_AESEncrypt(aes, digest1, key, 32); | 634 CRYPT_AESEncrypt(aes, digest1, key, 32); |
635 FX_Free(aes); | 635 FX_Free(aes); |
636 pEncryptDict->SetAtString(bOwner ? FX_BSTRC("OE") : FX_BSTRC("UE"), CFX_Byte
String(digest1, 32)); | 636 pEncryptDict->SetAtString(bOwner ? FX_BSTRC("OE") : FX_BSTRC("UE"), CFX_Byte
String(digest1, 32)); |
637 } | 637 } |
638 void CPDF_StandardSecurityHandler::AES256_SetPerms(CPDF_Dictionary* pEncryptDict
, FX_DWORD permissions, | 638 void CPDF_StandardSecurityHandler::AES256_SetPerms(CPDF_Dictionary* pEncryptDict
, FX_DWORD permissions, |
639 FX_BOOL bEncryptMetadata, FX_LPCBYTE key) | 639 FX_BOOL bEncryptMetadata, const uint8_t* key) |
640 { | 640 { |
641 uint8_t buf[16]; | 641 uint8_t buf[16]; |
642 buf[0] = (uint8_t)permissions; | 642 buf[0] = (uint8_t)permissions; |
643 buf[1] = (uint8_t)(permissions >> 8); | 643 buf[1] = (uint8_t)(permissions >> 8); |
644 buf[2] = (uint8_t)(permissions >> 16); | 644 buf[2] = (uint8_t)(permissions >> 16); |
645 buf[3] = (uint8_t)(permissions >> 24); | 645 buf[3] = (uint8_t)(permissions >> 24); |
646 buf[4] = 0xff; | 646 buf[4] = 0xff; |
647 buf[5] = 0xff; | 647 buf[5] = 0xff; |
648 buf[6] = 0xff; | 648 buf[6] = 0xff; |
649 buf[7] = 0xff; | 649 buf[7] = 0xff; |
650 buf[8] = bEncryptMetadata ? 'T' : 'F'; | 650 buf[8] = bEncryptMetadata ? 'T' : 'F'; |
651 buf[9] = 'a'; | 651 buf[9] = 'a'; |
652 buf[10] = 'd'; | 652 buf[10] = 'd'; |
653 buf[11] = 'b'; | 653 buf[11] = 'b'; |
654 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 654 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
655 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); | 655 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); |
656 uint8_t iv[16], buf1[16]; | 656 uint8_t iv[16], buf1[16]; |
657 FXSYS_memset32(iv, 0, 16); | 657 FXSYS_memset32(iv, 0, 16); |
658 CRYPT_AESSetIV(aes, iv); | 658 CRYPT_AESSetIV(aes, iv); |
659 CRYPT_AESEncrypt(aes, buf1, buf, 16); | 659 CRYPT_AESEncrypt(aes, buf1, buf, 16); |
660 FX_Free(aes); | 660 FX_Free(aes); |
661 pEncryptDict->SetAtString(FX_BSTRC("Perms"), CFX_ByteString(buf1, 16)); | 661 pEncryptDict->SetAtString(FX_BSTRC("Perms"), CFX_ByteString(buf1, 16)); |
662 } | 662 } |
663 void CPDF_StandardCryptoHandler::CryptBlock(FX_BOOL bEncrypt, FX_DWORD objnum, F
X_DWORD gennum, FX_LPCBYTE src_buf, FX_DWORD src_size, | 663 void CPDF_StandardCryptoHandler::CryptBlock(FX_BOOL bEncrypt, FX_DWORD objnum, F
X_DWORD gennum, const uint8_t* src_buf, FX_DWORD src_size, |
664 FX_LPBYTE dest_buf, FX_DWORD& dest_size) | 664 uint8_t* dest_buf, FX_DWORD& dest_size) |
665 { | 665 { |
666 if (m_Cipher == FXCIPHER_NONE) { | 666 if (m_Cipher == FXCIPHER_NONE) { |
667 FXSYS_memcpy32(dest_buf, src_buf, src_size); | 667 FXSYS_memcpy32(dest_buf, src_buf, src_size); |
668 return; | 668 return; |
669 } | 669 } |
670 uint8_t realkey[16]; | 670 uint8_t realkey[16]; |
671 int realkeylen = 16; | 671 int realkeylen = 16; |
672 if (m_Cipher != FXCIPHER_AES || m_KeyLen != 32) { | 672 if (m_Cipher != FXCIPHER_AES || m_KeyLen != 32) { |
673 uint8_t key1[32]; | 673 uint8_t key1[32]; |
674 FXSYS_memcpy32(key1, m_EncryptKey, m_KeyLen); | 674 FXSYS_memcpy32(key1, m_EncryptKey, m_KeyLen); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
717 } | 717 } |
718 CRYPT_ArcFourCryptBlock(dest_buf, dest_size, realkey, realkeylen); | 718 CRYPT_ArcFourCryptBlock(dest_buf, dest_size, realkey, realkeylen); |
719 } | 719 } |
720 } | 720 } |
721 typedef struct _AESCryptContext { | 721 typedef struct _AESCryptContext { |
722 uint8_t m_Context[2048]; | 722 uint8_t m_Context[2048]; |
723 FX_BOOL m_bIV; | 723 FX_BOOL m_bIV; |
724 uint8_t m_Block[16]; | 724 uint8_t m_Block[16]; |
725 FX_DWORD m_BlockOffset; | 725 FX_DWORD m_BlockOffset; |
726 } AESCryptContext; | 726 } AESCryptContext; |
727 FX_LPVOID CPDF_StandardCryptoHandler::CryptStart(FX_DWORD objnum, FX_DWORD gennu
m, FX_BOOL bEncrypt) | 727 void* CPDF_StandardCryptoHandler::CryptStart(FX_DWORD objnum, FX_DWORD gennum, F
X_BOOL bEncrypt) |
728 { | 728 { |
729 if (m_Cipher == FXCIPHER_NONE) { | 729 if (m_Cipher == FXCIPHER_NONE) { |
730 return this; | 730 return this; |
731 } | 731 } |
732 if (m_Cipher == FXCIPHER_AES && m_KeyLen == 32) { | 732 if (m_Cipher == FXCIPHER_AES && m_KeyLen == 32) { |
733 AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); | 733 AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); |
734 pContext->m_bIV = TRUE; | 734 pContext->m_bIV = TRUE; |
735 pContext->m_BlockOffset = 0; | 735 pContext->m_BlockOffset = 0; |
736 CRYPT_AESSetKey(pContext->m_Context, 16, m_EncryptKey, 32, bEncrypt); | 736 CRYPT_AESSetKey(pContext->m_Context, 16, m_EncryptKey, 32, bEncrypt); |
737 if (bEncrypt) { | 737 if (bEncrypt) { |
(...skipping 27 matching lines...) Expand all Loading... |
765 pContext->m_Block[i] = (uint8_t)rand(); | 765 pContext->m_Block[i] = (uint8_t)rand(); |
766 } | 766 } |
767 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); | 767 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); |
768 } | 768 } |
769 return pContext; | 769 return pContext; |
770 } | 770 } |
771 void* pContext = FX_Alloc(uint8_t, 1040); | 771 void* pContext = FX_Alloc(uint8_t, 1040); |
772 CRYPT_ArcFourSetup(pContext, realkey, realkeylen); | 772 CRYPT_ArcFourSetup(pContext, realkey, realkeylen); |
773 return pContext; | 773 return pContext; |
774 } | 774 } |
775 FX_BOOL CPDF_StandardCryptoHandler::CryptStream(FX_LPVOID context, FX_LPCBYTE sr
c_buf, FX_DWORD src_size, CFX_BinaryBuf& dest_buf, FX_BOOL bEncrypt) | 775 FX_BOOL CPDF_StandardCryptoHandler::CryptStream(void* context, const uint8_t* sr
c_buf, FX_DWORD src_size, CFX_BinaryBuf& dest_buf, FX_BOOL bEncrypt) |
776 { | 776 { |
777 if (!context) { | 777 if (!context) { |
778 return FALSE; | 778 return FALSE; |
779 } | 779 } |
780 if (m_Cipher == FXCIPHER_NONE) { | 780 if (m_Cipher == FXCIPHER_NONE) { |
781 dest_buf.AppendBlock(src_buf, src_size); | 781 dest_buf.AppendBlock(src_buf, src_size); |
782 return TRUE; | 782 return TRUE; |
783 } | 783 } |
784 if (m_Cipher == FXCIPHER_RC4) { | 784 if (m_Cipher == FXCIPHER_RC4) { |
785 int old_size = dest_buf.GetSize(); | 785 int old_size = dest_buf.GetSize(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
818 dest_buf.AppendBlock(block_buf, 16); | 818 dest_buf.AppendBlock(block_buf, 16); |
819 pContext->m_BlockOffset = 0; | 819 pContext->m_BlockOffset = 0; |
820 } | 820 } |
821 } | 821 } |
822 if (!src_left) { | 822 if (!src_left) { |
823 break; | 823 break; |
824 } | 824 } |
825 } | 825 } |
826 return TRUE; | 826 return TRUE; |
827 } | 827 } |
828 FX_BOOL CPDF_StandardCryptoHandler::CryptFinish(FX_LPVOID context, CFX_BinaryBuf
& dest_buf, FX_BOOL bEncrypt) | 828 FX_BOOL CPDF_StandardCryptoHandler::CryptFinish(void* context, CFX_BinaryBuf& de
st_buf, FX_BOOL bEncrypt) |
829 { | 829 { |
830 if (!context) { | 830 if (!context) { |
831 return FALSE; | 831 return FALSE; |
832 } | 832 } |
833 if (m_Cipher == FXCIPHER_NONE) { | 833 if (m_Cipher == FXCIPHER_NONE) { |
834 return TRUE; | 834 return TRUE; |
835 } | 835 } |
836 if (m_Cipher == FXCIPHER_RC4) { | 836 if (m_Cipher == FXCIPHER_RC4) { |
837 FX_Free(context); | 837 FX_Free(context); |
838 return TRUE; | 838 return TRUE; |
(...skipping 12 matching lines...) Expand all Loading... |
851 } else if (pContext->m_BlockOffset == 16) { | 851 } else if (pContext->m_BlockOffset == 16) { |
852 uint8_t block_buf[16]; | 852 uint8_t block_buf[16]; |
853 CRYPT_AESDecrypt(pContext->m_Context, block_buf, pContext->m_Block, 16); | 853 CRYPT_AESDecrypt(pContext->m_Context, block_buf, pContext->m_Block, 16); |
854 if (block_buf[15] <= 16) { | 854 if (block_buf[15] <= 16) { |
855 dest_buf.AppendBlock(block_buf, 16 - block_buf[15]); | 855 dest_buf.AppendBlock(block_buf, 16 - block_buf[15]); |
856 } | 856 } |
857 } | 857 } |
858 FX_Free(pContext); | 858 FX_Free(pContext); |
859 return TRUE; | 859 return TRUE; |
860 } | 860 } |
861 FX_LPVOID CPDF_StandardCryptoHandler::DecryptStart(FX_DWORD objnum, FX_DWORD gen
num) | 861 void* CPDF_StandardCryptoHandler::DecryptStart(FX_DWORD objnum, FX_DWORD gennum) |
862 { | 862 { |
863 return CryptStart(objnum, gennum, FALSE); | 863 return CryptStart(objnum, gennum, FALSE); |
864 } | 864 } |
865 FX_DWORD CPDF_StandardCryptoHandler::DecryptGetSize(FX_DWORD src_size) | 865 FX_DWORD CPDF_StandardCryptoHandler::DecryptGetSize(FX_DWORD src_size) |
866 { | 866 { |
867 return m_Cipher == FXCIPHER_AES ? src_size - 16 : src_size; | 867 return m_Cipher == FXCIPHER_AES ? src_size - 16 : src_size; |
868 } | 868 } |
869 FX_BOOL CPDF_StandardCryptoHandler::Init(CPDF_Dictionary* pEncryptDict, CPDF_Sec
urityHandler* pSecurityHandler) | 869 FX_BOOL CPDF_StandardCryptoHandler::Init(CPDF_Dictionary* pEncryptDict, CPDF_Sec
urityHandler* pSecurityHandler) |
870 { | 870 { |
871 FX_LPCBYTE key; | 871 const uint8_t* key; |
872 if (!pSecurityHandler->GetCryptInfo(m_Cipher, key, m_KeyLen)) { | 872 if (!pSecurityHandler->GetCryptInfo(m_Cipher, key, m_KeyLen)) { |
873 return FALSE; | 873 return FALSE; |
874 } | 874 } |
875 if (m_KeyLen > 32 || m_KeyLen < 0) { | 875 if (m_KeyLen > 32 || m_KeyLen < 0) { |
876 return FALSE; | 876 return FALSE; |
877 } | 877 } |
878 if (m_Cipher != FXCIPHER_NONE) { | 878 if (m_Cipher != FXCIPHER_NONE) { |
879 FXSYS_memcpy32(m_EncryptKey, key, m_KeyLen); | 879 FXSYS_memcpy32(m_EncryptKey, key, m_KeyLen); |
880 } | 880 } |
881 if (m_Cipher == FXCIPHER_AES) { | 881 if (m_Cipher == FXCIPHER_AES) { |
882 m_pAESContext = FX_Alloc(uint8_t, 2048); | 882 m_pAESContext = FX_Alloc(uint8_t, 2048); |
883 } | 883 } |
884 return TRUE; | 884 return TRUE; |
885 } | 885 } |
886 FX_BOOL CPDF_StandardCryptoHandler::Init(int cipher, FX_LPCBYTE key, int keylen) | 886 FX_BOOL CPDF_StandardCryptoHandler::Init(int cipher, const uint8_t* key, int key
len) |
887 { | 887 { |
888 if (cipher == FXCIPHER_AES) { | 888 if (cipher == FXCIPHER_AES) { |
889 switch(keylen) { | 889 switch(keylen) { |
890 case 16: | 890 case 16: |
891 case 24: | 891 case 24: |
892 case 32: | 892 case 32: |
893 break; | 893 break; |
894 default: | 894 default: |
895 return FALSE; | 895 return FALSE; |
896 } | 896 } |
(...skipping 11 matching lines...) Expand all Loading... |
908 } | 908 } |
909 } | 909 } |
910 m_Cipher = cipher; | 910 m_Cipher = cipher; |
911 m_KeyLen = keylen; | 911 m_KeyLen = keylen; |
912 FXSYS_memcpy32(m_EncryptKey, key, keylen); | 912 FXSYS_memcpy32(m_EncryptKey, key, keylen); |
913 if (m_Cipher == FXCIPHER_AES) { | 913 if (m_Cipher == FXCIPHER_AES) { |
914 m_pAESContext = FX_Alloc(uint8_t, 2048); | 914 m_pAESContext = FX_Alloc(uint8_t, 2048); |
915 } | 915 } |
916 return TRUE; | 916 return TRUE; |
917 } | 917 } |
918 FX_BOOL CPDF_StandardCryptoHandler::DecryptStream(FX_LPVOID context, FX_LPCBYTE
src_buf, FX_DWORD src_size, | 918 FX_BOOL CPDF_StandardCryptoHandler::DecryptStream(void* context, const uint8_t*
src_buf, FX_DWORD src_size, |
919 CFX_BinaryBuf& dest_buf) | 919 CFX_BinaryBuf& dest_buf) |
920 { | 920 { |
921 return CryptStream(context, src_buf, src_size, dest_buf, FALSE); | 921 return CryptStream(context, src_buf, src_size, dest_buf, FALSE); |
922 } | 922 } |
923 FX_BOOL CPDF_StandardCryptoHandler::DecryptFinish(FX_LPVOID context, CFX_BinaryB
uf& dest_buf) | 923 FX_BOOL CPDF_StandardCryptoHandler::DecryptFinish(void* context, CFX_BinaryBuf&
dest_buf) |
924 { | 924 { |
925 return CryptFinish(context, dest_buf, FALSE); | 925 return CryptFinish(context, dest_buf, FALSE); |
926 } | 926 } |
927 FX_DWORD CPDF_StandardCryptoHandler::EncryptGetSize(FX_DWORD objnum, FX_DWORD ve
rsion, FX_LPCBYTE src_buf, FX_DWORD src_size) | 927 FX_DWORD CPDF_StandardCryptoHandler::EncryptGetSize(FX_DWORD objnum, FX_DWORD ve
rsion, const uint8_t* src_buf, FX_DWORD src_size) |
928 { | 928 { |
929 if (m_Cipher == FXCIPHER_AES) { | 929 if (m_Cipher == FXCIPHER_AES) { |
930 return src_size + 32; | 930 return src_size + 32; |
931 } | 931 } |
932 return src_size; | 932 return src_size; |
933 } | 933 } |
934 FX_BOOL CPDF_StandardCryptoHandler::EncryptContent(FX_DWORD objnum, FX_DWORD gen
num, FX_LPCBYTE src_buf, FX_DWORD src_size, | 934 FX_BOOL CPDF_StandardCryptoHandler::EncryptContent(FX_DWORD objnum, FX_DWORD gen
num, const uint8_t* src_buf, FX_DWORD src_size, |
935 FX_LPBYTE dest_buf, FX_DWORD& dest_size) | 935 uint8_t* dest_buf, FX_DWORD& dest_size) |
936 { | 936 { |
937 CryptBlock(TRUE, objnum, gennum, src_buf, src_size, dest_buf, dest_size); | 937 CryptBlock(TRUE, objnum, gennum, src_buf, src_size, dest_buf, dest_size); |
938 return TRUE; | 938 return TRUE; |
939 } | 939 } |
940 void CPDF_CryptoHandler::Decrypt(FX_DWORD objnum, FX_DWORD gennum, CFX_ByteStrin
g& str) | 940 void CPDF_CryptoHandler::Decrypt(FX_DWORD objnum, FX_DWORD gennum, CFX_ByteStrin
g& str) |
941 { | 941 { |
942 CFX_BinaryBuf dest_buf; | 942 CFX_BinaryBuf dest_buf; |
943 FX_LPVOID context = DecryptStart(objnum, gennum); | 943 void* context = DecryptStart(objnum, gennum); |
944 DecryptStream(context, (FX_LPCBYTE)str, str.GetLength(), dest_buf); | 944 DecryptStream(context, (const uint8_t*)str, str.GetLength(), dest_buf); |
945 DecryptFinish(context, dest_buf); | 945 DecryptFinish(context, dest_buf); |
946 str = dest_buf; | 946 str = dest_buf; |
947 } | 947 } |
948 CPDF_StandardCryptoHandler::CPDF_StandardCryptoHandler() | 948 CPDF_StandardCryptoHandler::CPDF_StandardCryptoHandler() |
949 { | 949 { |
950 m_pAESContext = NULL; | 950 m_pAESContext = NULL; |
951 m_Cipher = FXCIPHER_NONE; | 951 m_Cipher = FXCIPHER_NONE; |
952 m_KeyLen = 0; | 952 m_KeyLen = 0; |
953 } | 953 } |
954 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() | 954 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() |
955 { | 955 { |
956 if (m_pAESContext) { | 956 if (m_pAESContext) { |
957 FX_Free(m_pAESContext); | 957 FX_Free(m_pAESContext); |
958 } | 958 } |
959 } | 959 } |
OLD | NEW |