| 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 <time.h> | 7 #include <time.h> |
| 8 #include "../../../include/fpdfapi/fpdf_parser.h" | 8 #include "../../../include/fpdfapi/fpdf_parser.h" |
| 9 #include "../../../include/fdrm/fx_crypt.h" | 9 #include "../../../include/fdrm/fx_crypt.h" |
| 10 const FX_BYTE defpasscode[32] = { | 10 const FX_BYTE defpasscode[32] = { |
| 11 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, | 11 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, |
| 12 0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08, | 12 0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08, |
| 13 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80, | 13 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80, |
| 14 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a | 14 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a |
| 15 }; | 15 }; |
| 16 void CalcEncryptKey(CPDF_Dictionary* pEncrypt, FX_LPCBYTE password, FX_DWORD pas
s_size, | 16 void CalcEncryptKey(CPDF_Dictionary* pEncrypt, FX_LPCBYTE password, FX_DWORD pas
s_size, |
| 17 FX_LPBYTE key, int keylen, FX_BOOL bIgnoreMeta, CPDF_Array*
pIdArray) | 17 FX_LPBYTE key, int keylen, FX_BOOL bIgnoreMeta, CPDF_Array*
pIdArray) |
| 18 { | 18 { |
| 19 int revision = pEncrypt->GetInteger(FX_BSTRC("R")); | 19 int revision = pEncrypt->GetInteger(FX_BSTRC("R")); |
| 20 FX_BYTE passcode[32]; | 20 FX_BYTE passcode[32]; |
| 21 for (FX_DWORD i = 0; i < 32; i ++) { | 21 for (FX_DWORD i = 0; i < 32; i ++) { |
| 22 passcode[i] = i < pass_size ? password[i] : defpasscode[i - pass_size]; | 22 passcode[i] = i < pass_size ? password[i] : defpasscode[i - pass_size]; |
| 23 } | 23 } |
| 24 FX_BYTE md5[100]; | 24 FX_BYTE md5[100]; |
| 25 CRYPT_MD5Start(md5); | 25 CRYPT_MD5Start(md5); |
| 26 CRYPT_MD5Update(md5, passcode, 32); | 26 CRYPT_MD5Update(md5, passcode, 32); |
| 27 CFX_ByteString okey = pEncrypt->GetString(FX_BSTRC("O")); | 27 CFX_ByteString okey = pEncrypt->GetString(FX_BSTRC("O")); |
| 28 CRYPT_MD5Update(md5, (FX_LPBYTE)(FX_LPCSTR)okey, okey.GetLength()); | 28 CRYPT_MD5Update(md5, (FX_LPBYTE)okey.c_str(), okey.GetLength()); |
| 29 FX_DWORD perm = pEncrypt->GetInteger(FX_BSTRC("P")); | 29 FX_DWORD perm = pEncrypt->GetInteger(FX_BSTRC("P")); |
| 30 CRYPT_MD5Update(md5, (FX_LPBYTE)&perm, 4); | 30 CRYPT_MD5Update(md5, (FX_LPBYTE)&perm, 4); |
| 31 if (pIdArray) { | 31 if (pIdArray) { |
| 32 CFX_ByteString id = pIdArray->GetString(0); | 32 CFX_ByteString id = pIdArray->GetString(0); |
| 33 CRYPT_MD5Update(md5, (FX_LPBYTE)(FX_LPCSTR)id, id.GetLength()); | 33 CRYPT_MD5Update(md5, (FX_LPBYTE)id.c_str(), id.GetLength()); |
| 34 } | 34 } |
| 35 if (!bIgnoreMeta && revision >= 3 && !pEncrypt->GetInteger(FX_BSTRC("Encrypt
Metadata"), 1)) { | 35 if (!bIgnoreMeta && revision >= 3 && !pEncrypt->GetInteger(FX_BSTRC("Encrypt
Metadata"), 1)) { |
| 36 FX_DWORD tag = (FX_DWORD) - 1; | 36 FX_DWORD tag = (FX_DWORD) - 1; |
| 37 CRYPT_MD5Update(md5, (FX_LPBYTE)&tag, 4); | 37 CRYPT_MD5Update(md5, (FX_LPBYTE)&tag, 4); |
| 38 } | 38 } |
| 39 FX_BYTE digest[16]; | 39 FX_BYTE digest[16]; |
| 40 CRYPT_MD5Finish(md5, digest); | 40 CRYPT_MD5Finish(md5, digest); |
| 41 FX_DWORD copy_len = keylen; | 41 FX_DWORD copy_len = keylen; |
| 42 if (copy_len > sizeof(digest)) { | 42 if (copy_len > sizeof(digest)) { |
| 43 copy_len = sizeof(digest); | 43 copy_len = sizeof(digest); |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 FXSYS_memcpy32(ukeybuf, defpasscode, 32); | 399 FXSYS_memcpy32(ukeybuf, defpasscode, 32); |
| 400 CRYPT_ArcFourCryptBlock(ukeybuf, 32, key, key_len); | 400 CRYPT_ArcFourCryptBlock(ukeybuf, 32, key, key_len); |
| 401 } else { | 401 } else { |
| 402 FX_BYTE test[32], tmpkey[32]; | 402 FX_BYTE test[32], tmpkey[32]; |
| 403 FX_DWORD copy_len = sizeof(test); | 403 FX_DWORD copy_len = sizeof(test); |
| 404 if (copy_len > (FX_DWORD)ukey.GetLength()) { | 404 if (copy_len > (FX_DWORD)ukey.GetLength()) { |
| 405 copy_len = ukey.GetLength(); | 405 copy_len = ukey.GetLength(); |
| 406 } | 406 } |
| 407 FXSYS_memset32(test, 0, sizeof(test)); | 407 FXSYS_memset32(test, 0, sizeof(test)); |
| 408 FXSYS_memset32(tmpkey, 0, sizeof(tmpkey)); | 408 FXSYS_memset32(tmpkey, 0, sizeof(tmpkey)); |
| 409 FXSYS_memcpy32(test, (FX_LPCSTR)ukey, copy_len); | 409 FXSYS_memcpy32(test, ukey.c_str(), copy_len); |
| 410 for (int i = 19; i >= 0; i --) { | 410 for (int i = 19; i >= 0; i --) { |
| 411 for (int j = 0; j < key_len; j ++) { | 411 for (int j = 0; j < key_len; j ++) { |
| 412 tmpkey[j] = key[j] ^ i; | 412 tmpkey[j] = key[j] ^ i; |
| 413 } | 413 } |
| 414 CRYPT_ArcFourCryptBlock(test, 32, tmpkey, key_len); | 414 CRYPT_ArcFourCryptBlock(test, 32, tmpkey, key_len); |
| 415 } | 415 } |
| 416 FX_BYTE md5[100]; | 416 FX_BYTE md5[100]; |
| 417 CRYPT_MD5Start(md5); | 417 CRYPT_MD5Start(md5); |
| 418 CRYPT_MD5Update(md5, defpasscode, 32); | 418 CRYPT_MD5Update(md5, defpasscode, 32); |
| 419 CPDF_Array* pIdArray = m_pParser->GetIDArray(); | 419 CPDF_Array* pIdArray = m_pParser->GetIDArray(); |
| 420 if (pIdArray) { | 420 if (pIdArray) { |
| 421 CFX_ByteString id = pIdArray->GetString(0); | 421 CFX_ByteString id = pIdArray->GetString(0); |
| 422 CRYPT_MD5Update(md5, (FX_LPBYTE)(FX_LPCSTR)id, id.GetLength()); | 422 CRYPT_MD5Update(md5, (FX_LPBYTE)id.c_str(), id.GetLength()); |
| 423 } | 423 } |
| 424 CRYPT_MD5Finish(md5, ukeybuf); | 424 CRYPT_MD5Finish(md5, ukeybuf); |
| 425 return FXSYS_memcmp32(test, ukeybuf, 16) == 0; | 425 return FXSYS_memcmp32(test, ukeybuf, 16) == 0; |
| 426 } | 426 } |
| 427 if (FXSYS_memcmp32((FX_LPVOID)(FX_LPCSTR)ukey, ukeybuf, 16) == 0) { | 427 if (FXSYS_memcmp32((FX_LPVOID)ukey.c_str(), ukeybuf, 16) == 0) { |
| 428 return TRUE; | 428 return TRUE; |
| 429 } | 429 } |
| 430 return FALSE; | 430 return FALSE; |
| 431 } | 431 } |
| 432 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(FX_LPCBYTE owner_pa
ss, FX_DWORD pass_size) | 432 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(FX_LPCBYTE owner_pa
ss, FX_DWORD pass_size) |
| 433 { | 433 { |
| 434 return GetUserPassword(owner_pass, pass_size, m_KeyLen); | 434 return GetUserPassword(owner_pass, pass_size, m_KeyLen); |
| 435 } | 435 } |
| 436 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(FX_LPCBYTE owner_pa
ss, FX_DWORD pass_size, FX_INT32 key_len) | 436 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(FX_LPCBYTE owner_pa
ss, FX_DWORD pass_size, FX_INT32 key_len) |
| 437 { | 437 { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 454 if (copy_len > sizeof(digest)) { | 454 if (copy_len > sizeof(digest)) { |
| 455 copy_len = sizeof(digest); | 455 copy_len = sizeof(digest); |
| 456 } | 456 } |
| 457 FXSYS_memcpy32(enckey, digest, copy_len); | 457 FXSYS_memcpy32(enckey, digest, copy_len); |
| 458 int okeylen = okey.GetLength(); | 458 int okeylen = okey.GetLength(); |
| 459 if (okeylen > 32) { | 459 if (okeylen > 32) { |
| 460 okeylen = 32; | 460 okeylen = 32; |
| 461 } | 461 } |
| 462 FX_BYTE okeybuf[64]; | 462 FX_BYTE okeybuf[64]; |
| 463 FXSYS_memset32(okeybuf, 0, sizeof(okeybuf)); | 463 FXSYS_memset32(okeybuf, 0, sizeof(okeybuf)); |
| 464 FXSYS_memcpy32(okeybuf, (FX_LPCSTR)okey, okeylen); | 464 FXSYS_memcpy32(okeybuf, okey.c_str(), okeylen); |
| 465 if (m_Revision == 2) { | 465 if (m_Revision == 2) { |
| 466 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, enckey, key_len); | 466 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, enckey, key_len); |
| 467 } else { | 467 } else { |
| 468 for (int i = 19; i >= 0; i --) { | 468 for (int i = 19; i >= 0; i --) { |
| 469 FX_BYTE tempkey[32]; | 469 FX_BYTE tempkey[32]; |
| 470 FXSYS_memset32(tempkey, 0, sizeof(tempkey)); | 470 FXSYS_memset32(tempkey, 0, sizeof(tempkey)); |
| 471 for (int j = 0; j < m_KeyLen; j ++) { | 471 for (int j = 0; j < m_KeyLen; j ++) { |
| 472 tempkey[j] = enckey[j] ^ i; | 472 tempkey[j] = enckey[j] ^ i; |
| 473 } | 473 } |
| 474 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, tempkey, key_len); | 474 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, tempkey, key_len); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 FX_BYTE tempbuf[32]; | 559 FX_BYTE tempbuf[32]; |
| 560 FXSYS_memcpy32(tempbuf, defpasscode, 32); | 560 FXSYS_memcpy32(tempbuf, defpasscode, 32); |
| 561 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); | 561 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); |
| 562 pEncryptDict->SetAtString(FX_BSTRC("U"), CFX_ByteString(tempbuf, 32)); | 562 pEncryptDict->SetAtString(FX_BSTRC("U"), CFX_ByteString(tempbuf, 32)); |
| 563 } else { | 563 } else { |
| 564 FX_BYTE md5[100]; | 564 FX_BYTE md5[100]; |
| 565 CRYPT_MD5Start(md5); | 565 CRYPT_MD5Start(md5); |
| 566 CRYPT_MD5Update(md5, defpasscode, 32); | 566 CRYPT_MD5Update(md5, defpasscode, 32); |
| 567 if (pIdArray) { | 567 if (pIdArray) { |
| 568 CFX_ByteString id = pIdArray->GetString(0); | 568 CFX_ByteString id = pIdArray->GetString(0); |
| 569 CRYPT_MD5Update(md5, (FX_LPBYTE)(FX_LPCSTR)id, id.GetLength()); | 569 CRYPT_MD5Update(md5, (FX_LPBYTE)id.c_str(), id.GetLength()); |
| 570 } | 570 } |
| 571 FX_BYTE digest[32]; | 571 FX_BYTE digest[32]; |
| 572 CRYPT_MD5Finish(md5, digest); | 572 CRYPT_MD5Finish(md5, digest); |
| 573 CRYPT_ArcFourCryptBlock(digest, 16, m_EncryptKey, key_len); | 573 CRYPT_ArcFourCryptBlock(digest, 16, m_EncryptKey, key_len); |
| 574 FX_BYTE tempkey[32]; | 574 FX_BYTE tempkey[32]; |
| 575 for (int i = 1; i <= 19; i ++) { | 575 for (int i = 1; i <= 19; i ++) { |
| 576 for (int j = 0; j < key_len; j ++) { | 576 for (int j = 0; j < key_len; j ++) { |
| 577 tempkey[j] = m_EncryptKey[j] ^ (FX_BYTE)i; | 577 tempkey[j] = m_EncryptKey[j] ^ (FX_BYTE)i; |
| 578 } | 578 } |
| 579 CRYPT_ArcFourCryptBlock(digest, 16, tempkey, key_len); | 579 CRYPT_ArcFourCryptBlock(digest, 16, tempkey, key_len); |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 950 m_pAESContext = NULL; | 950 m_pAESContext = NULL; |
| 951 m_Cipher = FXCIPHER_NONE; | 951 m_Cipher = FXCIPHER_NONE; |
| 952 m_KeyLen = 0; | 952 m_KeyLen = 0; |
| 953 } | 953 } |
| 954 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() | 954 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() |
| 955 { | 955 { |
| 956 if (m_pAESContext) { | 956 if (m_pAESContext) { |
| 957 FX_Free(m_pAESContext); | 957 FX_Free(m_pAESContext); |
| 958 } | 958 } |
| 959 } | 959 } |
| OLD | NEW |