| 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_security_handler.h" | 7 #include "core/fpdfapi/fpdf_parser/cpdf_security_handler.h" |
| 8 | 8 |
| 9 #include <time.h> | 9 #include <time.h> |
| 10 | 10 |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 CRYPT_ArcFourCryptBlock(ukeybuf, 32, key, key_len); | 423 CRYPT_ArcFourCryptBlock(ukeybuf, 32, key, key_len); |
| 424 } else { | 424 } else { |
| 425 uint8_t test[32], tmpkey[32]; | 425 uint8_t test[32], tmpkey[32]; |
| 426 uint32_t copy_len = sizeof(test); | 426 uint32_t copy_len = sizeof(test); |
| 427 if (copy_len > (uint32_t)ukey.GetLength()) { | 427 if (copy_len > (uint32_t)ukey.GetLength()) { |
| 428 copy_len = ukey.GetLength(); | 428 copy_len = ukey.GetLength(); |
| 429 } | 429 } |
| 430 FXSYS_memset(test, 0, sizeof(test)); | 430 FXSYS_memset(test, 0, sizeof(test)); |
| 431 FXSYS_memset(tmpkey, 0, sizeof(tmpkey)); | 431 FXSYS_memset(tmpkey, 0, sizeof(tmpkey)); |
| 432 FXSYS_memcpy(test, ukey.c_str(), copy_len); | 432 FXSYS_memcpy(test, ukey.c_str(), copy_len); |
| 433 for (int i = 19; i >= 0; i--) { | 433 for (int32_t i = 19; i >= 0; i--) { |
| 434 for (int j = 0; j < key_len; j++) { | 434 for (int j = 0; j < key_len; j++) |
| 435 tmpkey[j] = key[j] ^ i; | 435 tmpkey[j] = key[j] ^ static_cast<uint8_t>(i); |
| 436 } | |
| 437 CRYPT_ArcFourCryptBlock(test, 32, tmpkey, key_len); | 436 CRYPT_ArcFourCryptBlock(test, 32, tmpkey, key_len); |
| 438 } | 437 } |
| 439 uint8_t md5[100]; | 438 uint8_t md5[100]; |
| 440 CRYPT_MD5Start(md5); | 439 CRYPT_MD5Start(md5); |
| 441 CRYPT_MD5Update(md5, defpasscode, 32); | 440 CRYPT_MD5Update(md5, defpasscode, 32); |
| 442 CPDF_Array* pIdArray = m_pParser->GetIDArray(); | 441 CPDF_Array* pIdArray = m_pParser->GetIDArray(); |
| 443 if (pIdArray) { | 442 if (pIdArray) { |
| 444 CFX_ByteString id = pIdArray->GetStringAt(0); | 443 CFX_ByteString id = pIdArray->GetStringAt(0); |
| 445 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); | 444 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); |
| 446 } | 445 } |
| 447 CRYPT_MD5Finish(md5, ukeybuf); | 446 CRYPT_MD5Finish(md5, ukeybuf); |
| 448 return FXSYS_memcmp(test, ukeybuf, 16) == 0; | 447 return FXSYS_memcmp(test, ukeybuf, 16) == 0; |
| 449 } | 448 } |
| 450 if (FXSYS_memcmp((void*)ukey.c_str(), ukeybuf, 16) == 0) { | 449 if (FXSYS_memcmp((void*)ukey.c_str(), ukeybuf, 16) == 0) { |
| 451 return TRUE; | 450 return TRUE; |
| 452 } | 451 } |
| 453 return FALSE; | 452 return FALSE; |
| 454 } | 453 } |
| 455 CFX_ByteString CPDF_SecurityHandler::GetUserPassword(const uint8_t* owner_pass, | 454 CFX_ByteString CPDF_SecurityHandler::GetUserPassword(const uint8_t* owner_pass, |
| 456 uint32_t pass_size, | 455 uint32_t pass_size, |
| 457 int32_t key_len) { | 456 int32_t key_len) { |
| 458 CFX_ByteString okey = m_pEncryptDict->GetStringBy("O"); | 457 CFX_ByteString okey = m_pEncryptDict->GetStringBy("O"); |
| 459 uint8_t passcode[32]; | 458 uint8_t passcode[32]; |
| 460 uint32_t i; | 459 for (uint32_t i = 0; i < 32; i++) { |
| 461 for (i = 0; i < 32; i++) { | |
| 462 passcode[i] = i < pass_size ? owner_pass[i] : defpasscode[i - pass_size]; | 460 passcode[i] = i < pass_size ? owner_pass[i] : defpasscode[i - pass_size]; |
| 463 } | 461 } |
| 464 uint8_t digest[16]; | 462 uint8_t digest[16]; |
| 465 CRYPT_MD5Generate(passcode, 32, digest); | 463 CRYPT_MD5Generate(passcode, 32, digest); |
| 466 if (m_Revision >= 3) { | 464 if (m_Revision >= 3) { |
| 467 for (int i = 0; i < 50; i++) { | 465 for (uint32_t i = 0; i < 50; i++) { |
| 468 CRYPT_MD5Generate(digest, 16, digest); | 466 CRYPT_MD5Generate(digest, 16, digest); |
| 469 } | 467 } |
| 470 } | 468 } |
| 471 uint8_t enckey[32]; | 469 uint8_t enckey[32]; |
| 472 FXSYS_memset(enckey, 0, sizeof(enckey)); | 470 FXSYS_memset(enckey, 0, sizeof(enckey)); |
| 473 uint32_t copy_len = key_len; | 471 uint32_t copy_len = key_len; |
| 474 if (copy_len > sizeof(digest)) { | 472 if (copy_len > sizeof(digest)) { |
| 475 copy_len = sizeof(digest); | 473 copy_len = sizeof(digest); |
| 476 } | 474 } |
| 477 FXSYS_memcpy(enckey, digest, copy_len); | 475 FXSYS_memcpy(enckey, digest, copy_len); |
| 478 int okeylen = okey.GetLength(); | 476 int okeylen = okey.GetLength(); |
| 479 if (okeylen > 32) { | 477 if (okeylen > 32) { |
| 480 okeylen = 32; | 478 okeylen = 32; |
| 481 } | 479 } |
| 482 uint8_t okeybuf[64]; | 480 uint8_t okeybuf[64]; |
| 483 FXSYS_memset(okeybuf, 0, sizeof(okeybuf)); | 481 FXSYS_memset(okeybuf, 0, sizeof(okeybuf)); |
| 484 FXSYS_memcpy(okeybuf, okey.c_str(), okeylen); | 482 FXSYS_memcpy(okeybuf, okey.c_str(), okeylen); |
| 485 if (m_Revision == 2) { | 483 if (m_Revision == 2) { |
| 486 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, enckey, key_len); | 484 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, enckey, key_len); |
| 487 } else { | 485 } else { |
| 488 for (int i = 19; i >= 0; i--) { | 486 for (int32_t i = 19; i >= 0; i--) { |
| 489 uint8_t tempkey[32]; | 487 uint8_t tempkey[32]; |
| 490 FXSYS_memset(tempkey, 0, sizeof(tempkey)); | 488 FXSYS_memset(tempkey, 0, sizeof(tempkey)); |
| 491 for (int j = 0; j < m_KeyLen; j++) { | 489 for (int j = 0; j < m_KeyLen; j++) |
| 492 tempkey[j] = enckey[j] ^ i; | 490 tempkey[j] = enckey[j] ^ static_cast<uint8_t>(i); |
| 493 } | |
| 494 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, tempkey, key_len); | 491 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, tempkey, key_len); |
| 495 } | 492 } |
| 496 } | 493 } |
| 497 int len = 32; | 494 int len = 32; |
| 498 while (len && defpasscode[len - 1] == okeybuf[len - 1]) { | 495 while (len && defpasscode[len - 1] == okeybuf[len - 1]) { |
| 499 len--; | 496 len--; |
| 500 } | 497 } |
| 501 return CFX_ByteString(okeybuf, len); | 498 return CFX_ByteString(okeybuf, len); |
| 502 } | 499 } |
| 503 FX_BOOL CPDF_SecurityHandler::CheckOwnerPassword(const uint8_t* password, | 500 FX_BOOL CPDF_SecurityHandler::CheckOwnerPassword(const uint8_t* password, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 AES256_SetPassword(pEncryptDict, owner_pass, owner_size, TRUE, | 543 AES256_SetPassword(pEncryptDict, owner_pass, owner_size, TRUE, |
| 547 m_EncryptKey); | 544 m_EncryptKey); |
| 548 AES256_SetPerms(pEncryptDict, m_Permissions, | 545 AES256_SetPerms(pEncryptDict, m_Permissions, |
| 549 pEncryptDict->GetBooleanBy("EncryptMetadata", true), | 546 pEncryptDict->GetBooleanBy("EncryptMetadata", true), |
| 550 m_EncryptKey); | 547 m_EncryptKey); |
| 551 } | 548 } |
| 552 return; | 549 return; |
| 553 } | 550 } |
| 554 if (bDefault) { | 551 if (bDefault) { |
| 555 uint8_t passcode[32]; | 552 uint8_t passcode[32]; |
| 556 uint32_t i; | 553 for (uint32_t i = 0; i < 32; i++) { |
| 557 for (i = 0; i < 32; i++) { | |
| 558 passcode[i] = | 554 passcode[i] = |
| 559 i < owner_size ? owner_pass[i] : defpasscode[i - owner_size]; | 555 i < owner_size ? owner_pass[i] : defpasscode[i - owner_size]; |
| 560 } | 556 } |
| 561 uint8_t digest[16]; | 557 uint8_t digest[16]; |
| 562 CRYPT_MD5Generate(passcode, 32, digest); | 558 CRYPT_MD5Generate(passcode, 32, digest); |
| 563 if (m_Revision >= 3) { | 559 if (m_Revision >= 3) { |
| 564 for (int i = 0; i < 50; i++) { | 560 for (uint32_t i = 0; i < 50; i++) |
| 565 CRYPT_MD5Generate(digest, 16, digest); | 561 CRYPT_MD5Generate(digest, 16, digest); |
| 566 } | |
| 567 } | 562 } |
| 568 uint8_t enckey[32]; | 563 uint8_t enckey[32]; |
| 569 FXSYS_memcpy(enckey, digest, key_len); | 564 FXSYS_memcpy(enckey, digest, key_len); |
| 570 for (i = 0; i < 32; i++) { | 565 for (uint32_t i = 0; i < 32; i++) { |
| 571 passcode[i] = i < user_size ? user_pass[i] : defpasscode[i - user_size]; | 566 passcode[i] = i < user_size ? user_pass[i] : defpasscode[i - user_size]; |
| 572 } | 567 } |
| 573 CRYPT_ArcFourCryptBlock(passcode, 32, enckey, key_len); | 568 CRYPT_ArcFourCryptBlock(passcode, 32, enckey, key_len); |
| 574 uint8_t tempkey[32]; | 569 uint8_t tempkey[32]; |
| 575 if (m_Revision >= 3) { | 570 if (m_Revision >= 3) { |
| 576 for (i = 1; i <= 19; i++) { | 571 for (uint8_t i = 1; i <= 19; i++) { |
| 577 for (int j = 0; j < key_len; j++) { | 572 for (int j = 0; j < key_len; j++) |
| 578 tempkey[j] = enckey[j] ^ (uint8_t)i; | 573 tempkey[j] = enckey[j] ^ i; |
| 579 } | |
| 580 CRYPT_ArcFourCryptBlock(passcode, 32, tempkey, key_len); | 574 CRYPT_ArcFourCryptBlock(passcode, 32, tempkey, key_len); |
| 581 } | 575 } |
| 582 } | 576 } |
| 583 pEncryptDict->SetAtString("O", CFX_ByteString(passcode, 32)); | 577 pEncryptDict->SetAtString("O", CFX_ByteString(passcode, 32)); |
| 584 } | 578 } |
| 585 CalcEncryptKey(m_pEncryptDict, (uint8_t*)user_pass, user_size, m_EncryptKey, | 579 CalcEncryptKey(m_pEncryptDict, (uint8_t*)user_pass, user_size, m_EncryptKey, |
| 586 key_len, FALSE, pIdArray); | 580 key_len, FALSE, pIdArray); |
| 587 if (m_Revision < 3) { | 581 if (m_Revision < 3) { |
| 588 uint8_t tempbuf[32]; | 582 uint8_t tempbuf[32]; |
| 589 FXSYS_memcpy(tempbuf, defpasscode, 32); | 583 FXSYS_memcpy(tempbuf, defpasscode, 32); |
| 590 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); | 584 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); |
| 591 pEncryptDict->SetAtString("U", CFX_ByteString(tempbuf, 32)); | 585 pEncryptDict->SetAtString("U", CFX_ByteString(tempbuf, 32)); |
| 592 } else { | 586 } else { |
| 593 uint8_t md5[100]; | 587 uint8_t md5[100]; |
| 594 CRYPT_MD5Start(md5); | 588 CRYPT_MD5Start(md5); |
| 595 CRYPT_MD5Update(md5, defpasscode, 32); | 589 CRYPT_MD5Update(md5, defpasscode, 32); |
| 596 if (pIdArray) { | 590 if (pIdArray) { |
| 597 CFX_ByteString id = pIdArray->GetStringAt(0); | 591 CFX_ByteString id = pIdArray->GetStringAt(0); |
| 598 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); | 592 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); |
| 599 } | 593 } |
| 600 uint8_t digest[32]; | 594 uint8_t digest[32]; |
| 601 CRYPT_MD5Finish(md5, digest); | 595 CRYPT_MD5Finish(md5, digest); |
| 602 CRYPT_ArcFourCryptBlock(digest, 16, m_EncryptKey, key_len); | 596 CRYPT_ArcFourCryptBlock(digest, 16, m_EncryptKey, key_len); |
| 603 uint8_t tempkey[32]; | 597 uint8_t tempkey[32]; |
| 604 for (int i = 1; i <= 19; i++) { | 598 for (uint8_t i = 1; i <= 19; i++) { |
| 605 for (int j = 0; j < key_len; j++) { | 599 for (int j = 0; j < key_len; j++) { |
| 606 tempkey[j] = m_EncryptKey[j] ^ (uint8_t)i; | 600 tempkey[j] = m_EncryptKey[j] ^ i; |
| 607 } | 601 } |
| 608 CRYPT_ArcFourCryptBlock(digest, 16, tempkey, key_len); | 602 CRYPT_ArcFourCryptBlock(digest, 16, tempkey, key_len); |
| 609 } | 603 } |
| 610 CRYPT_MD5Generate(digest, 16, digest + 16); | 604 CRYPT_MD5Generate(digest, 16, digest + 16); |
| 611 pEncryptDict->SetAtString("U", CFX_ByteString(digest, 32)); | 605 pEncryptDict->SetAtString("U", CFX_ByteString(digest, 32)); |
| 612 } | 606 } |
| 613 } | 607 } |
| 614 void CPDF_SecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, | 608 void CPDF_SecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, |
| 615 CPDF_Array* pIdArray, | 609 CPDF_Array* pIdArray, |
| 616 const uint8_t* user_pass, | 610 const uint8_t* user_pass, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 buf[11] = 'b'; | 689 buf[11] = 'b'; |
| 696 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 690 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
| 697 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); | 691 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); |
| 698 uint8_t iv[16], buf1[16]; | 692 uint8_t iv[16], buf1[16]; |
| 699 FXSYS_memset(iv, 0, 16); | 693 FXSYS_memset(iv, 0, 16); |
| 700 CRYPT_AESSetIV(aes, iv); | 694 CRYPT_AESSetIV(aes, iv); |
| 701 CRYPT_AESEncrypt(aes, buf1, buf, 16); | 695 CRYPT_AESEncrypt(aes, buf1, buf, 16); |
| 702 FX_Free(aes); | 696 FX_Free(aes); |
| 703 pEncryptDict->SetAtString("Perms", CFX_ByteString(buf1, 16)); | 697 pEncryptDict->SetAtString("Perms", CFX_ByteString(buf1, 16)); |
| 704 } | 698 } |
| OLD | NEW |