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/fpdf_parser/cpdf_standard_security_handler.h" | 7 #include "core/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h" |
8 | 8 |
9 #include <time.h> | 9 #include <time.h> |
10 | 10 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 if (!LoadDict(pEncryptDict)) { | 91 if (!LoadDict(pEncryptDict)) { |
92 return FALSE; | 92 return FALSE; |
93 } | 93 } |
94 if (m_Cipher == FXCIPHER_NONE) { | 94 if (m_Cipher == FXCIPHER_NONE) { |
95 return TRUE; | 95 return TRUE; |
96 } | 96 } |
97 return CheckSecurity(m_KeyLen); | 97 return CheckSecurity(m_KeyLen); |
98 } | 98 } |
99 FX_BOOL CPDF_StandardSecurityHandler::CheckSecurity(int32_t key_len) { | 99 FX_BOOL CPDF_StandardSecurityHandler::CheckSecurity(int32_t key_len) { |
100 CFX_ByteString password = m_pParser->GetPassword(); | 100 CFX_ByteString password = m_pParser->GetPassword(); |
101 if (CheckPassword(password, password.GetLength(), TRUE, m_EncryptKey, | 101 if (CheckPassword(password.raw_str(), password.GetLength(), TRUE, |
102 key_len)) { | 102 m_EncryptKey, key_len)) { |
103 if (password.IsEmpty()) { | 103 if (password.IsEmpty()) { |
104 if (!CheckPassword(password, password.GetLength(), FALSE, m_EncryptKey, | 104 if (!CheckPassword(password.raw_str(), password.GetLength(), FALSE, |
105 key_len)) { | 105 m_EncryptKey, key_len)) { |
106 return FALSE; | 106 return FALSE; |
107 } | 107 } |
108 } | 108 } |
109 return TRUE; | 109 return TRUE; |
110 } | 110 } |
111 return CheckPassword(password, password.GetLength(), FALSE, m_EncryptKey, | 111 return CheckPassword(password.raw_str(), password.GetLength(), FALSE, |
112 key_len); | 112 m_EncryptKey, key_len); |
113 } | 113 } |
114 uint32_t CPDF_StandardSecurityHandler::GetPermissions() { | 114 uint32_t CPDF_StandardSecurityHandler::GetPermissions() { |
115 return m_Permissions; | 115 return m_Permissions; |
116 } | 116 } |
117 static FX_BOOL _LoadCryptInfo(CPDF_Dictionary* pEncryptDict, | 117 static FX_BOOL _LoadCryptInfo(CPDF_Dictionary* pEncryptDict, |
118 const CFX_ByteStringC& name, | 118 const CFX_ByteStringC& name, |
119 int& cipher, | 119 int& cipher, |
120 int& keylen) { | 120 int& keylen) { |
121 int Version = pEncryptDict->GetIntegerBy("V"); | 121 int Version = pEncryptDict->GetIntegerBy("V"); |
122 cipher = FXCIPHER_RC4; | 122 cipher = FXCIPHER_RC4; |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 CFX_ByteString okey = | 313 CFX_ByteString okey = |
314 m_pEncryptDict ? m_pEncryptDict->GetStringBy("O") : CFX_ByteString(); | 314 m_pEncryptDict ? m_pEncryptDict->GetStringBy("O") : CFX_ByteString(); |
315 if (okey.GetLength() < 48) { | 315 if (okey.GetLength() < 48) { |
316 return FALSE; | 316 return FALSE; |
317 } | 317 } |
318 CFX_ByteString ukey = | 318 CFX_ByteString ukey = |
319 m_pEncryptDict ? m_pEncryptDict->GetStringBy("U") : CFX_ByteString(); | 319 m_pEncryptDict ? m_pEncryptDict->GetStringBy("U") : CFX_ByteString(); |
320 if (ukey.GetLength() < 48) { | 320 if (ukey.GetLength() < 48) { |
321 return FALSE; | 321 return FALSE; |
322 } | 322 } |
323 const uint8_t* pkey = bOwner ? (const uint8_t*)okey : (const uint8_t*)ukey; | 323 const uint8_t* pkey = (bOwner ? okey : ukey).raw_str(); |
324 uint8_t sha[128]; | 324 uint8_t sha[128]; |
325 uint8_t digest[32]; | 325 uint8_t digest[32]; |
326 if (m_Revision >= 6) { | 326 if (m_Revision >= 6) { |
327 Revision6_Hash(password, size, (const uint8_t*)pkey + 32, | 327 Revision6_Hash(password, size, (const uint8_t*)pkey + 32, |
328 (bOwner ? (const uint8_t*)ukey : NULL), digest); | 328 bOwner ? ukey.raw_str() : nullptr, digest); |
329 } else { | 329 } else { |
330 CRYPT_SHA256Start(sha); | 330 CRYPT_SHA256Start(sha); |
331 CRYPT_SHA256Update(sha, password, size); | 331 CRYPT_SHA256Update(sha, password, size); |
332 CRYPT_SHA256Update(sha, pkey + 32, 8); | 332 CRYPT_SHA256Update(sha, pkey + 32, 8); |
333 if (bOwner) { | 333 if (bOwner) { |
334 CRYPT_SHA256Update(sha, ukey, 48); | 334 CRYPT_SHA256Update(sha, ukey.raw_str(), 48); |
335 } | 335 } |
336 CRYPT_SHA256Finish(sha, digest); | 336 CRYPT_SHA256Finish(sha, digest); |
337 } | 337 } |
338 if (FXSYS_memcmp(digest, pkey, 32) != 0) { | 338 if (FXSYS_memcmp(digest, pkey, 32) != 0) { |
339 return FALSE; | 339 return FALSE; |
340 } | 340 } |
341 if (!key) { | 341 if (!key) { |
342 return TRUE; | 342 return TRUE; |
343 } | 343 } |
344 if (m_Revision >= 6) { | 344 if (m_Revision >= 6) { |
345 Revision6_Hash(password, size, (const uint8_t*)pkey + 40, | 345 Revision6_Hash(password, size, (const uint8_t*)pkey + 40, |
346 (bOwner ? (const uint8_t*)ukey : NULL), digest); | 346 bOwner ? ukey.raw_str() : nullptr, digest); |
347 } else { | 347 } else { |
348 CRYPT_SHA256Start(sha); | 348 CRYPT_SHA256Start(sha); |
349 CRYPT_SHA256Update(sha, password, size); | 349 CRYPT_SHA256Update(sha, password, size); |
350 CRYPT_SHA256Update(sha, pkey + 40, 8); | 350 CRYPT_SHA256Update(sha, pkey + 40, 8); |
351 if (bOwner) { | 351 if (bOwner) { |
352 CRYPT_SHA256Update(sha, ukey, 48); | 352 CRYPT_SHA256Update(sha, ukey.raw_str(), 48); |
353 } | 353 } |
354 CRYPT_SHA256Finish(sha, digest); | 354 CRYPT_SHA256Finish(sha, digest); |
355 } | 355 } |
356 CFX_ByteString ekey = m_pEncryptDict | 356 CFX_ByteString ekey = m_pEncryptDict |
357 ? m_pEncryptDict->GetStringBy(bOwner ? "OE" : "UE") | 357 ? m_pEncryptDict->GetStringBy(bOwner ? "OE" : "UE") |
358 : CFX_ByteString(); | 358 : CFX_ByteString(); |
359 if (ekey.GetLength() < 32) { | 359 if (ekey.GetLength() < 32) { |
360 return FALSE; | 360 return FALSE; |
361 } | 361 } |
362 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 362 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
363 CRYPT_AESSetKey(aes, 16, digest, 32, FALSE); | 363 CRYPT_AESSetKey(aes, 16, digest, 32, FALSE); |
364 uint8_t iv[16]; | 364 uint8_t iv[16]; |
365 FXSYS_memset(iv, 0, 16); | 365 FXSYS_memset(iv, 0, 16); |
366 CRYPT_AESSetIV(aes, iv); | 366 CRYPT_AESSetIV(aes, iv); |
367 CRYPT_AESDecrypt(aes, key, ekey, 32); | 367 CRYPT_AESDecrypt(aes, key, ekey.raw_str(), 32); |
368 CRYPT_AESSetKey(aes, 16, key, 32, FALSE); | 368 CRYPT_AESSetKey(aes, 16, key, 32, FALSE); |
369 CRYPT_AESSetIV(aes, iv); | 369 CRYPT_AESSetIV(aes, iv); |
370 CFX_ByteString perms = m_pEncryptDict->GetStringBy("Perms"); | 370 CFX_ByteString perms = m_pEncryptDict->GetStringBy("Perms"); |
371 if (perms.IsEmpty()) { | 371 if (perms.IsEmpty()) { |
372 return FALSE; | 372 return FALSE; |
373 } | 373 } |
374 uint8_t perms_buf[16]; | 374 uint8_t perms_buf[16]; |
375 FXSYS_memset(perms_buf, 0, sizeof(perms_buf)); | 375 FXSYS_memset(perms_buf, 0, sizeof(perms_buf)); |
376 uint32_t copy_len = sizeof(perms_buf); | 376 uint32_t copy_len = sizeof(perms_buf); |
377 if (copy_len > (uint32_t)perms.GetLength()) { | 377 if (copy_len > (uint32_t)perms.GetLength()) { |
378 copy_len = perms.GetLength(); | 378 copy_len = perms.GetLength(); |
379 } | 379 } |
380 FXSYS_memcpy(perms_buf, (const uint8_t*)perms, copy_len); | 380 FXSYS_memcpy(perms_buf, perms.raw_str(), copy_len); |
381 uint8_t buf[16]; | 381 uint8_t buf[16]; |
382 CRYPT_AESDecrypt(aes, buf, perms_buf, 16); | 382 CRYPT_AESDecrypt(aes, buf, perms_buf, 16); |
383 FX_Free(aes); | 383 FX_Free(aes); |
384 if (buf[9] != 'a' || buf[10] != 'd' || buf[11] != 'b') { | 384 if (buf[9] != 'a' || buf[10] != 'd' || buf[11] != 'b') { |
385 return FALSE; | 385 return FALSE; |
386 } | 386 } |
387 if (FXDWORD_GET_LSBFIRST(buf) != m_Permissions) { | 387 if (FXDWORD_GET_LSBFIRST(buf) != m_Permissions) { |
388 return FALSE; | 388 return FALSE; |
389 } | 389 } |
390 if ((buf[8] == 'T' && !IsMetadataEncrypted()) || | 390 if ((buf[8] == 'T' && !IsMetadataEncrypted()) || |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 len--; | 508 len--; |
509 } | 509 } |
510 return CFX_ByteString(okeybuf, len); | 510 return CFX_ByteString(okeybuf, len); |
511 } | 511 } |
512 FX_BOOL CPDF_StandardSecurityHandler::CheckOwnerPassword( | 512 FX_BOOL CPDF_StandardSecurityHandler::CheckOwnerPassword( |
513 const uint8_t* password, | 513 const uint8_t* password, |
514 uint32_t pass_size, | 514 uint32_t pass_size, |
515 uint8_t* key, | 515 uint8_t* key, |
516 int32_t key_len) { | 516 int32_t key_len) { |
517 CFX_ByteString user_pass = GetUserPassword(password, pass_size, key_len); | 517 CFX_ByteString user_pass = GetUserPassword(password, pass_size, key_len); |
518 if (CheckUserPassword(user_pass, user_pass.GetLength(), FALSE, key, | 518 if (CheckUserPassword(user_pass.raw_str(), user_pass.GetLength(), FALSE, key, |
519 key_len)) { | 519 key_len)) { |
520 return TRUE; | 520 return TRUE; |
521 } | 521 } |
522 return CheckUserPassword(user_pass, user_pass.GetLength(), TRUE, key, | 522 return CheckUserPassword(user_pass.raw_str(), user_pass.GetLength(), TRUE, |
523 key_len); | 523 key, key_len); |
524 } | 524 } |
525 FX_BOOL CPDF_StandardSecurityHandler::IsMetadataEncrypted() { | 525 FX_BOOL CPDF_StandardSecurityHandler::IsMetadataEncrypted() { |
526 return m_pEncryptDict->GetBooleanBy("EncryptMetadata", TRUE); | 526 return m_pEncryptDict->GetBooleanBy("EncryptMetadata", TRUE); |
527 } | 527 } |
528 | 528 |
529 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, | 529 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, |
530 CPDF_Array* pIdArray, | 530 CPDF_Array* pIdArray, |
531 const uint8_t* user_pass, | 531 const uint8_t* user_pass, |
532 uint32_t user_size, | 532 uint32_t user_size, |
533 const uint8_t* owner_pass, | 533 const uint8_t* owner_pass, |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 tempkey[j] = m_EncryptKey[j] ^ (uint8_t)i; | 615 tempkey[j] = m_EncryptKey[j] ^ (uint8_t)i; |
616 } | 616 } |
617 CRYPT_ArcFourCryptBlock(digest, 16, tempkey, key_len); | 617 CRYPT_ArcFourCryptBlock(digest, 16, tempkey, key_len); |
618 } | 618 } |
619 CRYPT_MD5Generate(digest, 16, digest + 16); | 619 CRYPT_MD5Generate(digest, 16, digest + 16); |
620 pEncryptDict->SetAtString("U", CFX_ByteString(digest, 32)); | 620 pEncryptDict->SetAtString("U", CFX_ByteString(digest, 32)); |
621 } | 621 } |
622 } | 622 } |
623 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, | 623 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, |
624 CPDF_Array* pIdArray, | 624 CPDF_Array* pIdArray, |
625 const uint8_t* user_pass, | 625 const CFX_ByteStringC& user_pass, |
626 uint32_t user_size, | |
627 const uint8_t* owner_pass, | |
628 uint32_t owner_size, | |
629 uint32_t type) { | 626 uint32_t type) { |
630 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, owner_pass, owner_size, | 627 OnCreate(pEncryptDict, pIdArray, user_pass.raw_str(), user_pass.GetLength(), |
631 TRUE, type); | 628 NULL, 0, FALSE, type); |
632 } | |
633 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, | |
634 CPDF_Array* pIdArray, | |
635 const uint8_t* user_pass, | |
636 uint32_t user_size, | |
637 uint32_t type) { | |
638 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, NULL, 0, FALSE, type); | |
639 } | 629 } |
640 void CPDF_StandardSecurityHandler::AES256_SetPassword( | 630 void CPDF_StandardSecurityHandler::AES256_SetPassword( |
641 CPDF_Dictionary* pEncryptDict, | 631 CPDF_Dictionary* pEncryptDict, |
642 const uint8_t* password, | 632 const uint8_t* password, |
643 uint32_t size, | 633 uint32_t size, |
644 FX_BOOL bOwner, | 634 FX_BOOL bOwner, |
645 const uint8_t* key) { | 635 const uint8_t* key) { |
646 uint8_t sha[128]; | 636 uint8_t sha[128]; |
647 CRYPT_SHA1Start(sha); | 637 CRYPT_SHA1Start(sha); |
648 CRYPT_SHA1Update(sha, key, 32); | 638 CRYPT_SHA1Update(sha, key, 32); |
649 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); | 639 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); |
650 uint8_t digest[20]; | 640 uint8_t digest[20]; |
651 CRYPT_SHA1Finish(sha, digest); | 641 CRYPT_SHA1Finish(sha, digest); |
652 CFX_ByteString ukey = pEncryptDict->GetStringBy("U"); | 642 CFX_ByteString ukey = pEncryptDict->GetStringBy("U"); |
653 uint8_t digest1[48]; | 643 uint8_t digest1[48]; |
654 if (m_Revision >= 6) { | 644 if (m_Revision >= 6) { |
655 Revision6_Hash(password, size, digest, | 645 Revision6_Hash(password, size, digest, bOwner ? ukey.raw_str() : nullptr, |
656 (bOwner ? (const uint8_t*)ukey : NULL), digest1); | 646 digest1); |
657 } else { | 647 } else { |
658 CRYPT_SHA256Start(sha); | 648 CRYPT_SHA256Start(sha); |
659 CRYPT_SHA256Update(sha, password, size); | 649 CRYPT_SHA256Update(sha, password, size); |
660 CRYPT_SHA256Update(sha, digest, 8); | 650 CRYPT_SHA256Update(sha, digest, 8); |
661 if (bOwner) { | 651 if (bOwner) { |
662 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); | 652 CRYPT_SHA256Update(sha, ukey.raw_str(), ukey.GetLength()); |
663 } | 653 } |
664 CRYPT_SHA256Finish(sha, digest1); | 654 CRYPT_SHA256Finish(sha, digest1); |
665 } | 655 } |
666 FXSYS_memcpy(digest1 + 32, digest, 16); | 656 FXSYS_memcpy(digest1 + 32, digest, 16); |
667 pEncryptDict->SetAtString(bOwner ? "O" : "U", CFX_ByteString(digest1, 48)); | 657 pEncryptDict->SetAtString(bOwner ? "O" : "U", CFX_ByteString(digest1, 48)); |
668 if (m_Revision >= 6) { | 658 if (m_Revision >= 6) { |
669 Revision6_Hash(password, size, digest + 8, | 659 Revision6_Hash(password, size, digest + 8, |
670 (bOwner ? (const uint8_t*)ukey : NULL), digest1); | 660 bOwner ? ukey.raw_str() : nullptr, digest1); |
671 } else { | 661 } else { |
672 CRYPT_SHA256Start(sha); | 662 CRYPT_SHA256Start(sha); |
673 CRYPT_SHA256Update(sha, password, size); | 663 CRYPT_SHA256Update(sha, password, size); |
674 CRYPT_SHA256Update(sha, digest + 8, 8); | 664 CRYPT_SHA256Update(sha, digest + 8, 8); |
675 if (bOwner) { | 665 if (bOwner) { |
676 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); | 666 CRYPT_SHA256Update(sha, ukey.raw_str(), ukey.GetLength()); |
677 } | 667 } |
678 CRYPT_SHA256Finish(sha, digest1); | 668 CRYPT_SHA256Finish(sha, digest1); |
679 } | 669 } |
680 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 670 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
681 CRYPT_AESSetKey(aes, 16, digest1, 32, TRUE); | 671 CRYPT_AESSetKey(aes, 16, digest1, 32, TRUE); |
682 uint8_t iv[16]; | 672 uint8_t iv[16]; |
683 FXSYS_memset(iv, 0, 16); | 673 FXSYS_memset(iv, 0, 16); |
684 CRYPT_AESSetIV(aes, iv); | 674 CRYPT_AESSetIV(aes, iv); |
685 CRYPT_AESEncrypt(aes, digest1, key, 32); | 675 CRYPT_AESEncrypt(aes, digest1, key, 32); |
686 FX_Free(aes); | 676 FX_Free(aes); |
(...skipping 19 matching lines...) Expand all Loading... |
706 buf[11] = 'b'; | 696 buf[11] = 'b'; |
707 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 697 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
708 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); | 698 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); |
709 uint8_t iv[16], buf1[16]; | 699 uint8_t iv[16], buf1[16]; |
710 FXSYS_memset(iv, 0, 16); | 700 FXSYS_memset(iv, 0, 16); |
711 CRYPT_AESSetIV(aes, iv); | 701 CRYPT_AESSetIV(aes, iv); |
712 CRYPT_AESEncrypt(aes, buf1, buf, 16); | 702 CRYPT_AESEncrypt(aes, buf1, buf, 16); |
713 FX_Free(aes); | 703 FX_Free(aes); |
714 pEncryptDict->SetAtString("Perms", CFX_ByteString(buf1, 16)); | 704 pEncryptDict->SetAtString("Perms", CFX_ByteString(buf1, 16)); |
715 } | 705 } |
OLD | NEW |