| 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/parser/cpdf_security_handler.h" | 7 #include "core/fpdfapi/parser/cpdf_security_handler.h" |
| 8 | 8 |
| 9 #include <time.h> | 9 #include <time.h> |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 uint32_t pass_size, | 32 uint32_t pass_size, |
| 33 uint8_t* key, | 33 uint8_t* key, |
| 34 int keylen, | 34 int keylen, |
| 35 bool bIgnoreMeta, | 35 bool bIgnoreMeta, |
| 36 CPDF_Array* pIdArray) { | 36 CPDF_Array* pIdArray) { |
| 37 int revision = pEncrypt->GetIntegerFor("R"); | 37 int revision = pEncrypt->GetIntegerFor("R"); |
| 38 uint8_t passcode[32]; | 38 uint8_t passcode[32]; |
| 39 for (uint32_t i = 0; i < 32; i++) { | 39 for (uint32_t i = 0; i < 32; i++) { |
| 40 passcode[i] = i < pass_size ? password[i] : defpasscode[i - pass_size]; | 40 passcode[i] = i < pass_size ? password[i] : defpasscode[i - pass_size]; |
| 41 } | 41 } |
| 42 uint8_t md5[100]; | 42 CRYPT_md5_context md5; |
| 43 CRYPT_MD5Start(md5); | 43 CRYPT_MD5Start(&md5); |
| 44 CRYPT_MD5Update(md5, passcode, 32); | 44 CRYPT_MD5Update(&md5, passcode, 32); |
| 45 CFX_ByteString okey = pEncrypt->GetStringFor("O"); | 45 CFX_ByteString okey = pEncrypt->GetStringFor("O"); |
| 46 CRYPT_MD5Update(md5, (uint8_t*)okey.c_str(), okey.GetLength()); | 46 CRYPT_MD5Update(&md5, (uint8_t*)okey.c_str(), okey.GetLength()); |
| 47 uint32_t perm = pEncrypt->GetIntegerFor("P"); | 47 uint32_t perm = pEncrypt->GetIntegerFor("P"); |
| 48 CRYPT_MD5Update(md5, (uint8_t*)&perm, 4); | 48 CRYPT_MD5Update(&md5, (uint8_t*)&perm, 4); |
| 49 if (pIdArray) { | 49 if (pIdArray) { |
| 50 CFX_ByteString id = pIdArray->GetStringAt(0); | 50 CFX_ByteString id = pIdArray->GetStringAt(0); |
| 51 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); | 51 CRYPT_MD5Update(&md5, (uint8_t*)id.c_str(), id.GetLength()); |
| 52 } | 52 } |
| 53 if (!bIgnoreMeta && revision >= 3 && | 53 if (!bIgnoreMeta && revision >= 3 && |
| 54 !pEncrypt->GetIntegerFor("EncryptMetadata", 1)) { | 54 !pEncrypt->GetIntegerFor("EncryptMetadata", 1)) { |
| 55 uint32_t tag = (uint32_t)-1; | 55 uint32_t tag = (uint32_t)-1; |
| 56 CRYPT_MD5Update(md5, (uint8_t*)&tag, 4); | 56 CRYPT_MD5Update(&md5, (uint8_t*)&tag, 4); |
| 57 } | 57 } |
| 58 uint8_t digest[16]; | 58 uint8_t digest[16]; |
| 59 CRYPT_MD5Finish(md5, digest); | 59 CRYPT_MD5Finish(&md5, digest); |
| 60 uint32_t copy_len = keylen; | 60 uint32_t copy_len = keylen; |
| 61 if (copy_len > sizeof(digest)) { | 61 if (copy_len > sizeof(digest)) { |
| 62 copy_len = sizeof(digest); | 62 copy_len = sizeof(digest); |
| 63 } | 63 } |
| 64 if (revision >= 3) { | 64 if (revision >= 3) { |
| 65 for (int i = 0; i < 50; i++) { | 65 for (int i = 0; i < 50; i++) { |
| 66 CRYPT_MD5Generate(digest, copy_len, digest); | 66 CRYPT_MD5Generate(digest, copy_len, digest); |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 FXSYS_memset(key, 0, keylen); | 69 FXSYS_memset(key, 0, keylen); |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 copy_len = ukey.GetLength(); | 429 copy_len = ukey.GetLength(); |
| 430 } | 430 } |
| 431 FXSYS_memset(test, 0, sizeof(test)); | 431 FXSYS_memset(test, 0, sizeof(test)); |
| 432 FXSYS_memset(tmpkey, 0, sizeof(tmpkey)); | 432 FXSYS_memset(tmpkey, 0, sizeof(tmpkey)); |
| 433 FXSYS_memcpy(test, ukey.c_str(), copy_len); | 433 FXSYS_memcpy(test, ukey.c_str(), copy_len); |
| 434 for (int32_t i = 19; i >= 0; i--) { | 434 for (int32_t i = 19; i >= 0; i--) { |
| 435 for (int j = 0; j < key_len; j++) | 435 for (int j = 0; j < key_len; j++) |
| 436 tmpkey[j] = key[j] ^ static_cast<uint8_t>(i); | 436 tmpkey[j] = key[j] ^ static_cast<uint8_t>(i); |
| 437 CRYPT_ArcFourCryptBlock(test, 32, tmpkey, key_len); | 437 CRYPT_ArcFourCryptBlock(test, 32, tmpkey, key_len); |
| 438 } | 438 } |
| 439 uint8_t md5[100]; | 439 CRYPT_md5_context md5; |
| 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) { |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 } | 580 } |
| 581 CalcEncryptKey(m_pEncryptDict, (uint8_t*)user_pass, user_size, m_EncryptKey, | 581 CalcEncryptKey(m_pEncryptDict, (uint8_t*)user_pass, user_size, m_EncryptKey, |
| 582 key_len, false, pIdArray); | 582 key_len, false, pIdArray); |
| 583 if (m_Revision < 3) { | 583 if (m_Revision < 3) { |
| 584 uint8_t tempbuf[32]; | 584 uint8_t tempbuf[32]; |
| 585 FXSYS_memcpy(tempbuf, defpasscode, 32); | 585 FXSYS_memcpy(tempbuf, defpasscode, 32); |
| 586 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); | 586 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); |
| 587 pEncryptDict->SetNewFor<CPDF_String>("U", CFX_ByteString(tempbuf, 32), | 587 pEncryptDict->SetNewFor<CPDF_String>("U", CFX_ByteString(tempbuf, 32), |
| 588 false); | 588 false); |
| 589 } else { | 589 } else { |
| 590 uint8_t md5[100]; | 590 CRYPT_md5_context md5; |
| 591 CRYPT_MD5Start(md5); | 591 CRYPT_MD5Start(&md5); |
| 592 CRYPT_MD5Update(md5, defpasscode, 32); | 592 CRYPT_MD5Update(&md5, defpasscode, 32); |
| 593 if (pIdArray) { | 593 if (pIdArray) { |
| 594 CFX_ByteString id = pIdArray->GetStringAt(0); | 594 CFX_ByteString id = pIdArray->GetStringAt(0); |
| 595 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); | 595 CRYPT_MD5Update(&md5, (uint8_t*)id.c_str(), id.GetLength()); |
| 596 } | 596 } |
| 597 uint8_t digest[32]; | 597 uint8_t digest[32]; |
| 598 CRYPT_MD5Finish(md5, digest); | 598 CRYPT_MD5Finish(&md5, digest); |
| 599 CRYPT_ArcFourCryptBlock(digest, 16, m_EncryptKey, key_len); | 599 CRYPT_ArcFourCryptBlock(digest, 16, m_EncryptKey, key_len); |
| 600 uint8_t tempkey[32]; | 600 uint8_t tempkey[32]; |
| 601 for (uint8_t i = 1; i <= 19; i++) { | 601 for (uint8_t i = 1; i <= 19; i++) { |
| 602 for (int j = 0; j < key_len; j++) { | 602 for (int j = 0; j < key_len; j++) { |
| 603 tempkey[j] = m_EncryptKey[j] ^ i; | 603 tempkey[j] = m_EncryptKey[j] ^ i; |
| 604 } | 604 } |
| 605 CRYPT_ArcFourCryptBlock(digest, 16, tempkey, key_len); | 605 CRYPT_ArcFourCryptBlock(digest, 16, tempkey, key_len); |
| 606 } | 606 } |
| 607 CRYPT_MD5Generate(digest, 16, digest + 16); | 607 CRYPT_MD5Generate(digest, 16, digest + 16); |
| 608 pEncryptDict->SetNewFor<CPDF_String>("U", CFX_ByteString(digest, 32), | 608 pEncryptDict->SetNewFor<CPDF_String>("U", CFX_ByteString(digest, 32), |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 698 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
| 699 CRYPT_AESSetKey(aes, 16, key, 32, true); | 699 CRYPT_AESSetKey(aes, 16, key, 32, true); |
| 700 uint8_t iv[16], buf1[16]; | 700 uint8_t iv[16], buf1[16]; |
| 701 FXSYS_memset(iv, 0, 16); | 701 FXSYS_memset(iv, 0, 16); |
| 702 CRYPT_AESSetIV(aes, iv); | 702 CRYPT_AESSetIV(aes, iv); |
| 703 CRYPT_AESEncrypt(aes, buf1, buf, 16); | 703 CRYPT_AESEncrypt(aes, buf1, buf, 16); |
| 704 FX_Free(aes); | 704 FX_Free(aes); |
| 705 pEncryptDict->SetNewFor<CPDF_String>("Perms", CFX_ByteString(buf1, 16), | 705 pEncryptDict->SetNewFor<CPDF_String>("Perms", CFX_ByteString(buf1, 16), |
| 706 false); | 706 false); |
| 707 } | 707 } |
| OLD | NEW |