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