| 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 uint8_t defpasscode[32] = { | 10 const uint8_t 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, const uint8_t* password, FX_DWORD
pass_size, | 16 void CalcEncryptKey(CPDF_Dictionary* pEncrypt, const uint8_t* password, FX_DWORD
pass_size, |
| 17 uint8_t* key, int keylen, FX_BOOL bIgnoreMeta, CPDF_Array* p
IdArray) | 17 uint8_t* key, int keylen, FX_BOOL bIgnoreMeta, CPDF_Array* p
IdArray) |
| 18 { | 18 { |
| 19 int revision = pEncrypt->GetInteger(FX_BSTRC("R")); | 19 int revision = pEncrypt->GetInteger(FX_BSTRC("R")); |
| 20 uint8_t passcode[32]; | 20 uint8_t 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 uint8_t md5[100]; | 24 uint8_t 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->GetStringAt("O"); |
| 28 CRYPT_MD5Update(md5, (uint8_t*)okey.c_str(), okey.GetLength()); | 28 CRYPT_MD5Update(md5, (uint8_t*)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, (uint8_t*)&perm, 4); | 30 CRYPT_MD5Update(md5, (uint8_t*)&perm, 4); |
| 31 if (pIdArray) { | 31 if (pIdArray) { |
| 32 CFX_ByteString id = pIdArray->GetString(0); | 32 CFX_ByteString id = pIdArray->GetStringAt(0); |
| 33 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); | 33 CRYPT_MD5Update(md5, (uint8_t*)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, (uint8_t*)&tag, 4); | 37 CRYPT_MD5Update(md5, (uint8_t*)&tag, 4); |
| 38 } | 38 } |
| 39 uint8_t digest[16]; | 39 uint8_t 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)) { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 if (nKeyBits == 0) { | 126 if (nKeyBits == 0) { |
| 127 nKeyBits = pEncryptDict->GetInteger(FX_BSTRC("Length"), 128)
; | 127 nKeyBits = pEncryptDict->GetInteger(FX_BSTRC("Length"), 128)
; |
| 128 } | 128 } |
| 129 } else { | 129 } else { |
| 130 nKeyBits = pEncryptDict->GetInteger(FX_BSTRC("Length"), 256); | 130 nKeyBits = pEncryptDict->GetInteger(FX_BSTRC("Length"), 256); |
| 131 } | 131 } |
| 132 if (nKeyBits < 40) { | 132 if (nKeyBits < 40) { |
| 133 nKeyBits *= 8; | 133 nKeyBits *= 8; |
| 134 } | 134 } |
| 135 keylen = nKeyBits / 8; | 135 keylen = nKeyBits / 8; |
| 136 CFX_ByteString cipher_name = pDefFilter->GetString(FX_BSTRC("CFM")); | 136 CFX_ByteString cipher_name = pDefFilter->GetStringAt("CFM"); |
| 137 if (cipher_name == FX_BSTRC("AESV2") || cipher_name == FX_BSTRC("AES
V3")) { | 137 if (cipher_name == FX_BSTRC("AESV2") || cipher_name == FX_BSTRC("AES
V3")) { |
| 138 cipher = FXCIPHER_AES; | 138 cipher = FXCIPHER_AES; |
| 139 } | 139 } |
| 140 } | 140 } |
| 141 } else { | 141 } else { |
| 142 keylen = Version > 1 ? pEncryptDict->GetInteger(FX_BSTRC("Length"), 40)
/ 8 : 5; | 142 keylen = Version > 1 ? pEncryptDict->GetInteger(FX_BSTRC("Length"), 40)
/ 8 : 5; |
| 143 } | 143 } |
| 144 if (keylen > 32 || keylen < 0) { | 144 if (keylen > 32 || keylen < 0) { |
| 145 return FALSE; | 145 return FALSE; |
| 146 } | 146 } |
| 147 return TRUE; | 147 return TRUE; |
| 148 } | 148 } |
| 149 FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict) | 149 FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict) |
| 150 { | 150 { |
| 151 m_pEncryptDict = pEncryptDict; | 151 m_pEncryptDict = pEncryptDict; |
| 152 m_bOwner = FALSE; | 152 m_bOwner = FALSE; |
| 153 m_Version = pEncryptDict->GetInteger(FX_BSTRC("V")); | 153 m_Version = pEncryptDict->GetInteger(FX_BSTRC("V")); |
| 154 m_Revision = pEncryptDict->GetInteger(FX_BSTRC("R")); | 154 m_Revision = pEncryptDict->GetInteger(FX_BSTRC("R")); |
| 155 m_Permissions = pEncryptDict->GetInteger(FX_BSTRC("P"), -1); | 155 m_Permissions = pEncryptDict->GetInteger(FX_BSTRC("P"), -1); |
| 156 if (m_Version < 4) { | 156 if (m_Version < 4) { |
| 157 return _LoadCryptInfo(pEncryptDict, CFX_ByteString(), m_Cipher, m_KeyLen
); | 157 return _LoadCryptInfo(pEncryptDict, CFX_ByteString(), m_Cipher, m_KeyLen
); |
| 158 } | 158 } |
| 159 CFX_ByteString stmf_name = pEncryptDict->GetString(FX_BSTRC("StmF")); | 159 CFX_ByteString stmf_name = pEncryptDict->GetStringAt("StmF"); |
| 160 CFX_ByteString strf_name = pEncryptDict->GetString(FX_BSTRC("StrF")); | 160 CFX_ByteString strf_name = pEncryptDict->GetStringAt("StrF"); |
| 161 if (stmf_name != strf_name) { | 161 if (stmf_name != strf_name) { |
| 162 return FALSE; | 162 return FALSE; |
| 163 } | 163 } |
| 164 if (!_LoadCryptInfo(pEncryptDict, strf_name, m_Cipher, m_KeyLen)) { | 164 if (!_LoadCryptInfo(pEncryptDict, strf_name, m_Cipher, m_KeyLen)) { |
| 165 return FALSE; | 165 return FALSE; |
| 166 } | 166 } |
| 167 return TRUE; | 167 return TRUE; |
| 168 } | 168 } |
| 169 FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict, FX
_DWORD type, int& cipher, int& key_len) | 169 FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict, FX
_DWORD type, int& cipher, int& key_len) |
| 170 { | 170 { |
| 171 m_pEncryptDict = pEncryptDict; | 171 m_pEncryptDict = pEncryptDict; |
| 172 m_bOwner = FALSE; | 172 m_bOwner = FALSE; |
| 173 m_Version = pEncryptDict->GetInteger(FX_BSTRC("V")); | 173 m_Version = pEncryptDict->GetInteger(FX_BSTRC("V")); |
| 174 m_Revision = pEncryptDict->GetInteger(FX_BSTRC("R")); | 174 m_Revision = pEncryptDict->GetInteger(FX_BSTRC("R")); |
| 175 m_Permissions = pEncryptDict->GetInteger(FX_BSTRC("P"), -1); | 175 m_Permissions = pEncryptDict->GetInteger(FX_BSTRC("P"), -1); |
| 176 CFX_ByteString strf_name, stmf_name; | 176 CFX_ByteString strf_name, stmf_name; |
| 177 if (m_Version >= 4) { | 177 if (m_Version >= 4) { |
| 178 stmf_name = pEncryptDict->GetString(FX_BSTRC("StmF")); | 178 stmf_name = pEncryptDict->GetStringAt("StmF"); |
| 179 strf_name = pEncryptDict->GetString(FX_BSTRC("StrF")); | 179 strf_name = pEncryptDict->GetStringAt("StrF"); |
| 180 if (stmf_name != strf_name) { | 180 if (stmf_name != strf_name) { |
| 181 return FALSE; | 181 return FALSE; |
| 182 } | 182 } |
| 183 } | 183 } |
| 184 if (!_LoadCryptInfo(pEncryptDict, strf_name, cipher, key_len)) { | 184 if (!_LoadCryptInfo(pEncryptDict, strf_name, cipher, key_len)) { |
| 185 return FALSE; | 185 return FALSE; |
| 186 } | 186 } |
| 187 m_Cipher = cipher; | 187 m_Cipher = cipher; |
| 188 m_KeyLen = key_len; | 188 m_KeyLen = key_len; |
| 189 return TRUE; | 189 return TRUE; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 ++i; | 284 ++i; |
| 285 } | 285 } |
| 286 FX_Free(aes); | 286 FX_Free(aes); |
| 287 if (hash) { | 287 if (hash) { |
| 288 FXSYS_memcpy(hash, input, 32); | 288 FXSYS_memcpy(hash, input, 32); |
| 289 } | 289 } |
| 290 } | 290 } |
| 291 FX_BOOL CPDF_StandardSecurityHandler::AES256_CheckPassword(const uint8_t* passwo
rd, FX_DWORD size, | 291 FX_BOOL CPDF_StandardSecurityHandler::AES256_CheckPassword(const uint8_t* passwo
rd, FX_DWORD size, |
| 292 FX_BOOL bOwner, uint8_t* key) | 292 FX_BOOL bOwner, uint8_t* key) |
| 293 { | 293 { |
| 294 CFX_ByteString okey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("O
")) : CFX_ByteString(); | 294 CFX_ByteString okey = m_pEncryptDict ? |
| 295 m_pEncryptDict->GetStringAt("O") : CFX_ByteString(); |
| 295 if (okey.GetLength() < 48) { | 296 if (okey.GetLength() < 48) { |
| 296 return FALSE; | 297 return FALSE; |
| 297 } | 298 } |
| 298 CFX_ByteString ukey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("U
")) : CFX_ByteString(); | 299 CFX_ByteString ukey = m_pEncryptDict ? |
| 300 m_pEncryptDict->GetStringAt("U") : CFX_ByteString(); |
| 299 if (ukey.GetLength() < 48) { | 301 if (ukey.GetLength() < 48) { |
| 300 return FALSE; | 302 return FALSE; |
| 301 } | 303 } |
| 302 const uint8_t* pkey = bOwner ? (const uint8_t*)okey : (const uint8_t*)ukey; | 304 const uint8_t* pkey = bOwner ? (const uint8_t*)okey : (const uint8_t*)ukey; |
| 303 uint8_t sha[128]; | 305 uint8_t sha[128]; |
| 304 uint8_t digest[32]; | 306 uint8_t digest[32]; |
| 305 if (m_Revision >= 6) { | 307 if (m_Revision >= 6) { |
| 306 Revision6_Hash(password, size, (const uint8_t*)pkey + 32, (bOwner ? (con
st uint8_t*)ukey : NULL), digest); | 308 Revision6_Hash(password, size, (const uint8_t*)pkey + 32, (bOwner ? (con
st uint8_t*)ukey : NULL), digest); |
| 307 } else { | 309 } else { |
| 308 CRYPT_SHA256Start(sha); | 310 CRYPT_SHA256Start(sha); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 323 Revision6_Hash(password, size, (const uint8_t*)pkey + 40, (bOwner ? (con
st uint8_t*)ukey : NULL), digest); | 325 Revision6_Hash(password, size, (const uint8_t*)pkey + 40, (bOwner ? (con
st uint8_t*)ukey : NULL), digest); |
| 324 } else { | 326 } else { |
| 325 CRYPT_SHA256Start(sha); | 327 CRYPT_SHA256Start(sha); |
| 326 CRYPT_SHA256Update(sha, password, size); | 328 CRYPT_SHA256Update(sha, password, size); |
| 327 CRYPT_SHA256Update(sha, pkey + 40, 8); | 329 CRYPT_SHA256Update(sha, pkey + 40, 8); |
| 328 if (bOwner) { | 330 if (bOwner) { |
| 329 CRYPT_SHA256Update(sha, ukey, 48); | 331 CRYPT_SHA256Update(sha, ukey, 48); |
| 330 } | 332 } |
| 331 CRYPT_SHA256Finish(sha, digest); | 333 CRYPT_SHA256Finish(sha, digest); |
| 332 } | 334 } |
| 333 CFX_ByteString ekey = m_pEncryptDict ? m_pEncryptDict->GetString(bOwner ? FX
_BSTRC("OE") : FX_BSTRC("UE")) : CFX_ByteString(); | 335 CFX_ByteString ekey = m_pEncryptDict ? |
| 336 m_pEncryptDict->GetStringAt(bOwner ? "OE" : "UE") : CFX_ByteString(); |
| 334 if (ekey.GetLength() < 32) { | 337 if (ekey.GetLength() < 32) { |
| 335 return FALSE; | 338 return FALSE; |
| 336 } | 339 } |
| 337 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 340 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
| 338 CRYPT_AESSetKey(aes, 16, digest, 32, FALSE); | 341 CRYPT_AESSetKey(aes, 16, digest, 32, FALSE); |
| 339 uint8_t iv[16]; | 342 uint8_t iv[16]; |
| 340 FXSYS_memset(iv, 0, 16); | 343 FXSYS_memset(iv, 0, 16); |
| 341 CRYPT_AESSetIV(aes, iv); | 344 CRYPT_AESSetIV(aes, iv); |
| 342 CRYPT_AESDecrypt(aes, key, ekey, 32); | 345 CRYPT_AESDecrypt(aes, key, ekey, 32); |
| 343 CRYPT_AESSetKey(aes, 16, key, 32, FALSE); | 346 CRYPT_AESSetKey(aes, 16, key, 32, FALSE); |
| 344 CRYPT_AESSetIV(aes, iv); | 347 CRYPT_AESSetIV(aes, iv); |
| 345 CFX_ByteString perms = m_pEncryptDict->GetString(FX_BSTRC("Perms")); | 348 CFX_ByteString perms = m_pEncryptDict->GetStringAt("Perms"); |
| 346 if (perms.IsEmpty()) { | 349 if (perms.IsEmpty()) { |
| 347 return FALSE; | 350 return FALSE; |
| 348 } | 351 } |
| 349 uint8_t perms_buf[16]; | 352 uint8_t perms_buf[16]; |
| 350 FXSYS_memset(perms_buf, 0, sizeof(perms_buf)); | 353 FXSYS_memset(perms_buf, 0, sizeof(perms_buf)); |
| 351 FX_DWORD copy_len = sizeof(perms_buf); | 354 FX_DWORD copy_len = sizeof(perms_buf); |
| 352 if (copy_len > (FX_DWORD)perms.GetLength()) { | 355 if (copy_len > (FX_DWORD)perms.GetLength()) { |
| 353 copy_len = perms.GetLength(); | 356 copy_len = perms.GetLength(); |
| 354 } | 357 } |
| 355 FXSYS_memcpy(perms_buf, (const uint8_t*)perms, copy_len); | 358 FXSYS_memcpy(perms_buf, (const uint8_t*)perms, copy_len); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 383 if (bOwner) { | 386 if (bOwner) { |
| 384 return CheckOwnerPassword(password, size, key, key_len); | 387 return CheckOwnerPassword(password, size, key, key_len); |
| 385 } | 388 } |
| 386 return CheckUserPassword(password, size, FALSE, key, key_len) || CheckUserPa
ssword(password, size, TRUE, key, key_len); | 389 return CheckUserPassword(password, size, FALSE, key, key_len) || CheckUserPa
ssword(password, size, TRUE, key, key_len); |
| 387 } | 390 } |
| 388 FX_BOOL CPDF_StandardSecurityHandler::CheckUserPassword(const uint8_t* password,
FX_DWORD pass_size, | 391 FX_BOOL CPDF_StandardSecurityHandler::CheckUserPassword(const uint8_t* password,
FX_DWORD pass_size, |
| 389 FX_BOOL bIgnoreEncryptMeta, uint8_t* key, int32_t key_len) | 392 FX_BOOL bIgnoreEncryptMeta, uint8_t* key, int32_t key_len) |
| 390 { | 393 { |
| 391 CalcEncryptKey(m_pEncryptDict, password, pass_size, key, key_len, bIgnoreEnc
ryptMeta, | 394 CalcEncryptKey(m_pEncryptDict, password, pass_size, key, key_len, bIgnoreEnc
ryptMeta, |
| 392 m_pParser->GetIDArray()); | 395 m_pParser->GetIDArray()); |
| 393 CFX_ByteString ukey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("U
")) : CFX_ByteString(); | 396 CFX_ByteString ukey = m_pEncryptDict ? |
| 397 m_pEncryptDict->GetStringAt("U") : CFX_ByteString(); |
| 394 if (ukey.GetLength() < 16) { | 398 if (ukey.GetLength() < 16) { |
| 395 return FALSE; | 399 return FALSE; |
| 396 } | 400 } |
| 397 uint8_t ukeybuf[32]; | 401 uint8_t ukeybuf[32]; |
| 398 if (m_Revision == 2) { | 402 if (m_Revision == 2) { |
| 399 FXSYS_memcpy(ukeybuf, defpasscode, 32); | 403 FXSYS_memcpy(ukeybuf, defpasscode, 32); |
| 400 CRYPT_ArcFourCryptBlock(ukeybuf, 32, key, key_len); | 404 CRYPT_ArcFourCryptBlock(ukeybuf, 32, key, key_len); |
| 401 } else { | 405 } else { |
| 402 uint8_t test[32], tmpkey[32]; | 406 uint8_t test[32], tmpkey[32]; |
| 403 FX_DWORD copy_len = sizeof(test); | 407 FX_DWORD copy_len = sizeof(test); |
| 404 if (copy_len > (FX_DWORD)ukey.GetLength()) { | 408 if (copy_len > (FX_DWORD)ukey.GetLength()) { |
| 405 copy_len = ukey.GetLength(); | 409 copy_len = ukey.GetLength(); |
| 406 } | 410 } |
| 407 FXSYS_memset(test, 0, sizeof(test)); | 411 FXSYS_memset(test, 0, sizeof(test)); |
| 408 FXSYS_memset(tmpkey, 0, sizeof(tmpkey)); | 412 FXSYS_memset(tmpkey, 0, sizeof(tmpkey)); |
| 409 FXSYS_memcpy(test, ukey.c_str(), copy_len); | 413 FXSYS_memcpy(test, ukey.c_str(), copy_len); |
| 410 for (int i = 19; i >= 0; i --) { | 414 for (int i = 19; i >= 0; i --) { |
| 411 for (int j = 0; j < key_len; j ++) { | 415 for (int j = 0; j < key_len; j ++) { |
| 412 tmpkey[j] = key[j] ^ i; | 416 tmpkey[j] = key[j] ^ i; |
| 413 } | 417 } |
| 414 CRYPT_ArcFourCryptBlock(test, 32, tmpkey, key_len); | 418 CRYPT_ArcFourCryptBlock(test, 32, tmpkey, key_len); |
| 415 } | 419 } |
| 416 uint8_t md5[100]; | 420 uint8_t md5[100]; |
| 417 CRYPT_MD5Start(md5); | 421 CRYPT_MD5Start(md5); |
| 418 CRYPT_MD5Update(md5, defpasscode, 32); | 422 CRYPT_MD5Update(md5, defpasscode, 32); |
| 419 CPDF_Array* pIdArray = m_pParser->GetIDArray(); | 423 CPDF_Array* pIdArray = m_pParser->GetIDArray(); |
| 420 if (pIdArray) { | 424 if (pIdArray) { |
| 421 CFX_ByteString id = pIdArray->GetString(0); | 425 CFX_ByteString id = pIdArray->GetStringAt(0); |
| 422 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); | 426 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); |
| 423 } | 427 } |
| 424 CRYPT_MD5Finish(md5, ukeybuf); | 428 CRYPT_MD5Finish(md5, ukeybuf); |
| 425 return FXSYS_memcmp(test, ukeybuf, 16) == 0; | 429 return FXSYS_memcmp(test, ukeybuf, 16) == 0; |
| 426 } | 430 } |
| 427 if (FXSYS_memcmp((void*)ukey.c_str(), ukeybuf, 16) == 0) { | 431 if (FXSYS_memcmp((void*)ukey.c_str(), ukeybuf, 16) == 0) { |
| 428 return TRUE; | 432 return TRUE; |
| 429 } | 433 } |
| 430 return FALSE; | 434 return FALSE; |
| 431 } | 435 } |
| 432 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(const uint8_t* owne
r_pass, FX_DWORD pass_size) | 436 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(const uint8_t* owne
r_pass, FX_DWORD pass_size) |
| 433 { | 437 { |
| 434 return GetUserPassword(owner_pass, pass_size, m_KeyLen); | 438 return GetUserPassword(owner_pass, pass_size, m_KeyLen); |
| 435 } | 439 } |
| 436 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(const uint8_t* owne
r_pass, FX_DWORD pass_size, int32_t key_len) | 440 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(const uint8_t* owne
r_pass, FX_DWORD pass_size, int32_t key_len) |
| 437 { | 441 { |
| 438 CFX_ByteString okey = m_pEncryptDict->GetString(FX_BSTRC("O")); | 442 CFX_ByteString okey = m_pEncryptDict->GetStringAt("O"); |
| 439 uint8_t passcode[32]; | 443 uint8_t passcode[32]; |
| 440 FX_DWORD i; | 444 FX_DWORD i; |
| 441 for (i = 0; i < 32; i ++) { | 445 for (i = 0; i < 32; i ++) { |
| 442 passcode[i] = i < pass_size ? owner_pass[i] : defpasscode[i - pass_size]
; | 446 passcode[i] = i < pass_size ? owner_pass[i] : defpasscode[i - pass_size]
; |
| 443 } | 447 } |
| 444 uint8_t digest[16]; | 448 uint8_t digest[16]; |
| 445 CRYPT_MD5Generate(passcode, 32, digest); | 449 CRYPT_MD5Generate(passcode, 32, digest); |
| 446 if (m_Revision >= 3) { | 450 if (m_Revision >= 3) { |
| 447 for (int i = 0; i < 50; i ++) { | 451 for (int i = 0; i < 50; i ++) { |
| 448 CRYPT_MD5Generate(digest, 16, digest); | 452 CRYPT_MD5Generate(digest, 16, digest); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 if (m_Revision < 3) { | 562 if (m_Revision < 3) { |
| 559 uint8_t tempbuf[32]; | 563 uint8_t tempbuf[32]; |
| 560 FXSYS_memcpy(tempbuf, defpasscode, 32); | 564 FXSYS_memcpy(tempbuf, defpasscode, 32); |
| 561 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); | 565 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); |
| 562 pEncryptDict->SetAtString(FX_BSTRC("U"), CFX_ByteString(tempbuf, 32)); | 566 pEncryptDict->SetAtString(FX_BSTRC("U"), CFX_ByteString(tempbuf, 32)); |
| 563 } else { | 567 } else { |
| 564 uint8_t md5[100]; | 568 uint8_t md5[100]; |
| 565 CRYPT_MD5Start(md5); | 569 CRYPT_MD5Start(md5); |
| 566 CRYPT_MD5Update(md5, defpasscode, 32); | 570 CRYPT_MD5Update(md5, defpasscode, 32); |
| 567 if (pIdArray) { | 571 if (pIdArray) { |
| 568 CFX_ByteString id = pIdArray->GetString(0); | 572 CFX_ByteString id = pIdArray->GetStringAt(0); |
| 569 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); | 573 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); |
| 570 } | 574 } |
| 571 uint8_t digest[32]; | 575 uint8_t digest[32]; |
| 572 CRYPT_MD5Finish(md5, digest); | 576 CRYPT_MD5Finish(md5, digest); |
| 573 CRYPT_ArcFourCryptBlock(digest, 16, m_EncryptKey, key_len); | 577 CRYPT_ArcFourCryptBlock(digest, 16, m_EncryptKey, key_len); |
| 574 uint8_t tempkey[32]; | 578 uint8_t tempkey[32]; |
| 575 for (int i = 1; i <= 19; i ++) { | 579 for (int i = 1; i <= 19; i ++) { |
| 576 for (int j = 0; j < key_len; j ++) { | 580 for (int j = 0; j < key_len; j ++) { |
| 577 tempkey[j] = m_EncryptKey[j] ^ (uint8_t)i; | 581 tempkey[j] = m_EncryptKey[j] ^ (uint8_t)i; |
| 578 } | 582 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 593 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, NULL, 0, FALSE, type)
; | 597 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, NULL, 0, FALSE, type)
; |
| 594 } | 598 } |
| 595 void CPDF_StandardSecurityHandler::AES256_SetPassword(CPDF_Dictionary* pEncryptD
ict, const uint8_t* password, FX_DWORD size, FX_BOOL bOwner, const uint8_t* key) | 599 void CPDF_StandardSecurityHandler::AES256_SetPassword(CPDF_Dictionary* pEncryptD
ict, const uint8_t* password, FX_DWORD size, FX_BOOL bOwner, const uint8_t* key) |
| 596 { | 600 { |
| 597 uint8_t sha[128]; | 601 uint8_t sha[128]; |
| 598 CRYPT_SHA1Start(sha); | 602 CRYPT_SHA1Start(sha); |
| 599 CRYPT_SHA1Update(sha, key, 32); | 603 CRYPT_SHA1Update(sha, key, 32); |
| 600 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); | 604 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); |
| 601 uint8_t digest[20]; | 605 uint8_t digest[20]; |
| 602 CRYPT_SHA1Finish(sha, digest); | 606 CRYPT_SHA1Finish(sha, digest); |
| 603 CFX_ByteString ukey = pEncryptDict->GetString(FX_BSTRC("U")); | 607 CFX_ByteString ukey = pEncryptDict->GetStringAt("U"); |
| 604 uint8_t digest1[48]; | 608 uint8_t digest1[48]; |
| 605 if (m_Revision >= 6) { | 609 if (m_Revision >= 6) { |
| 606 Revision6_Hash(password, size, digest, (bOwner ? (const uint8_t*)ukey :
NULL), digest1); | 610 Revision6_Hash(password, size, digest, (bOwner ? (const uint8_t*)ukey :
NULL), digest1); |
| 607 } else { | 611 } else { |
| 608 CRYPT_SHA256Start(sha); | 612 CRYPT_SHA256Start(sha); |
| 609 CRYPT_SHA256Update(sha, password, size); | 613 CRYPT_SHA256Update(sha, password, size); |
| 610 CRYPT_SHA256Update(sha, digest, 8); | 614 CRYPT_SHA256Update(sha, digest, 8); |
| 611 if (bOwner) { | 615 if (bOwner) { |
| 612 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); | 616 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); |
| 613 } | 617 } |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 950 m_pAESContext = NULL; | 954 m_pAESContext = NULL; |
| 951 m_Cipher = FXCIPHER_NONE; | 955 m_Cipher = FXCIPHER_NONE; |
| 952 m_KeyLen = 0; | 956 m_KeyLen = 0; |
| 953 } | 957 } |
| 954 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() | 958 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() |
| 955 { | 959 { |
| 956 if (m_pAESContext) { | 960 if (m_pAESContext) { |
| 957 FX_Free(m_pAESContext); | 961 FX_Free(m_pAESContext); |
| 958 } | 962 } |
| 959 } | 963 } |
| OLD | NEW |