| 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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 const uint8_t* key) { | 645 const uint8_t* key) { |
| 646 uint8_t sha[128]; | 646 uint8_t sha[128]; |
| 647 CRYPT_SHA1Start(sha); | 647 CRYPT_SHA1Start(sha); |
| 648 CRYPT_SHA1Update(sha, key, 32); | 648 CRYPT_SHA1Update(sha, key, 32); |
| 649 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); | 649 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); |
| 650 uint8_t digest[20]; | 650 uint8_t digest[20]; |
| 651 CRYPT_SHA1Finish(sha, digest); | 651 CRYPT_SHA1Finish(sha, digest); |
| 652 CFX_ByteString ukey = pEncryptDict->GetStringBy("U"); | 652 CFX_ByteString ukey = pEncryptDict->GetStringBy("U"); |
| 653 uint8_t digest1[48]; | 653 uint8_t digest1[48]; |
| 654 if (m_Revision >= 6) { | 654 if (m_Revision >= 6) { |
| 655 Revision6_Hash(password, size, digest, | 655 Revision6_Hash(password, size, digest, bOwner ? ukey.raw_str() : nullptr, |
| 656 (bOwner ? (const uint8_t*)ukey : NULL), digest1); | 656 digest1); |
| 657 } else { | 657 } else { |
| 658 CRYPT_SHA256Start(sha); | 658 CRYPT_SHA256Start(sha); |
| 659 CRYPT_SHA256Update(sha, password, size); | 659 CRYPT_SHA256Update(sha, password, size); |
| 660 CRYPT_SHA256Update(sha, digest, 8); | 660 CRYPT_SHA256Update(sha, digest, 8); |
| 661 if (bOwner) { | 661 if (bOwner) { |
| 662 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); | 662 CRYPT_SHA256Update(sha, ukey.raw_str(), ukey.GetLength()); |
| 663 } | 663 } |
| 664 CRYPT_SHA256Finish(sha, digest1); | 664 CRYPT_SHA256Finish(sha, digest1); |
| 665 } | 665 } |
| 666 FXSYS_memcpy(digest1 + 32, digest, 16); | 666 FXSYS_memcpy(digest1 + 32, digest, 16); |
| 667 pEncryptDict->SetAtString(bOwner ? "O" : "U", CFX_ByteString(digest1, 48)); | 667 pEncryptDict->SetAtString(bOwner ? "O" : "U", CFX_ByteString(digest1, 48)); |
| 668 if (m_Revision >= 6) { | 668 if (m_Revision >= 6) { |
| 669 Revision6_Hash(password, size, digest + 8, | 669 Revision6_Hash(password, size, digest + 8, |
| 670 (bOwner ? (const uint8_t*)ukey : NULL), digest1); | 670 bOwner ? ukey.raw_str() : nullptr, digest1); |
| 671 } else { | 671 } else { |
| 672 CRYPT_SHA256Start(sha); | 672 CRYPT_SHA256Start(sha); |
| 673 CRYPT_SHA256Update(sha, password, size); | 673 CRYPT_SHA256Update(sha, password, size); |
| 674 CRYPT_SHA256Update(sha, digest + 8, 8); | 674 CRYPT_SHA256Update(sha, digest + 8, 8); |
| 675 if (bOwner) { | 675 if (bOwner) { |
| 676 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); | 676 CRYPT_SHA256Update(sha, ukey.raw_str(), ukey.GetLength()); |
| 677 } | 677 } |
| 678 CRYPT_SHA256Finish(sha, digest1); | 678 CRYPT_SHA256Finish(sha, digest1); |
| 679 } | 679 } |
| 680 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 680 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
| 681 CRYPT_AESSetKey(aes, 16, digest1, 32, TRUE); | 681 CRYPT_AESSetKey(aes, 16, digest1, 32, TRUE); |
| 682 uint8_t iv[16]; | 682 uint8_t iv[16]; |
| 683 FXSYS_memset(iv, 0, 16); | 683 FXSYS_memset(iv, 0, 16); |
| 684 CRYPT_AESSetIV(aes, iv); | 684 CRYPT_AESSetIV(aes, iv); |
| 685 CRYPT_AESEncrypt(aes, digest1, key, 32); | 685 CRYPT_AESEncrypt(aes, digest1, key, 32); |
| 686 FX_Free(aes); | 686 FX_Free(aes); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 706 buf[11] = 'b'; | 706 buf[11] = 'b'; |
| 707 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 707 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
| 708 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); | 708 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); |
| 709 uint8_t iv[16], buf1[16]; | 709 uint8_t iv[16], buf1[16]; |
| 710 FXSYS_memset(iv, 0, 16); | 710 FXSYS_memset(iv, 0, 16); |
| 711 CRYPT_AESSetIV(aes, iv); | 711 CRYPT_AESSetIV(aes, iv); |
| 712 CRYPT_AESEncrypt(aes, buf1, buf, 16); | 712 CRYPT_AESEncrypt(aes, buf1, buf, 16); |
| 713 FX_Free(aes); | 713 FX_Free(aes); |
| 714 pEncryptDict->SetAtString("Perms", CFX_ByteString(buf1, 16)); | 714 pEncryptDict->SetAtString("Perms", CFX_ByteString(buf1, 16)); |
| 715 } | 715 } |
| OLD | NEW |