Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: core/src/fpdfapi/fpdf_parser/fpdf_parser_encrypt.cpp

Issue 1644633003: Member function name refactoring (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/include/fpdfapi/fpdf_parser.h" 7 #include "core/include/fpdfapi/fpdf_parser.h"
8 8
9 #include <time.h> 9 #include <time.h>
10 10
11 #include "core/include/fdrm/fx_crypt.h" 11 #include "core/include/fdrm/fx_crypt.h"
12 12
13 const uint8_t defpasscode[32] = { 13 const uint8_t defpasscode[32] = {
14 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, 0x64, 0x00, 0x4e, 14 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, 0x64, 0x00, 0x4e,
15 0x56, 0xff, 0xfa, 0x01, 0x08, 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 15 0x56, 0xff, 0xfa, 0x01, 0x08, 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68,
16 0x3e, 0x80, 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a}; 16 0x3e, 0x80, 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a};
17 void CalcEncryptKey(CPDF_Dictionary* pEncrypt, 17 void CalcEncryptKey(CPDF_Dictionary* pEncrypt,
18 const uint8_t* password, 18 const uint8_t* password,
19 FX_DWORD pass_size, 19 FX_DWORD pass_size,
20 uint8_t* key, 20 uint8_t* key,
21 int keylen, 21 int keylen,
22 FX_BOOL bIgnoreMeta, 22 FX_BOOL bIgnoreMeta,
23 CPDF_Array* pIdArray) { 23 CPDF_Array* pIdArray) {
24 int revision = pEncrypt->GetInteger("R"); 24 int revision = pEncrypt->GetIntegerBy("R");
25 uint8_t passcode[32]; 25 uint8_t passcode[32];
26 for (FX_DWORD i = 0; i < 32; i++) { 26 for (FX_DWORD i = 0; i < 32; i++) {
27 passcode[i] = i < pass_size ? password[i] : defpasscode[i - pass_size]; 27 passcode[i] = i < pass_size ? password[i] : defpasscode[i - pass_size];
28 } 28 }
29 uint8_t md5[100]; 29 uint8_t md5[100];
30 CRYPT_MD5Start(md5); 30 CRYPT_MD5Start(md5);
31 CRYPT_MD5Update(md5, passcode, 32); 31 CRYPT_MD5Update(md5, passcode, 32);
32 CFX_ByteString okey = pEncrypt->GetString("O"); 32 CFX_ByteString okey = pEncrypt->GetStringBy("O");
33 CRYPT_MD5Update(md5, (uint8_t*)okey.c_str(), okey.GetLength()); 33 CRYPT_MD5Update(md5, (uint8_t*)okey.c_str(), okey.GetLength());
34 FX_DWORD perm = pEncrypt->GetInteger("P"); 34 FX_DWORD perm = pEncrypt->GetIntegerBy("P");
35 CRYPT_MD5Update(md5, (uint8_t*)&perm, 4); 35 CRYPT_MD5Update(md5, (uint8_t*)&perm, 4);
36 if (pIdArray) { 36 if (pIdArray) {
37 CFX_ByteString id = pIdArray->GetString(0); 37 CFX_ByteString id = pIdArray->GetStringAt(0);
38 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); 38 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength());
39 } 39 }
40 if (!bIgnoreMeta && revision >= 3 && 40 if (!bIgnoreMeta && revision >= 3 &&
41 !pEncrypt->GetInteger("EncryptMetadata", 1)) { 41 !pEncrypt->GetIntegerBy("EncryptMetadata", 1)) {
42 FX_DWORD tag = (FX_DWORD)-1; 42 FX_DWORD tag = (FX_DWORD)-1;
43 CRYPT_MD5Update(md5, (uint8_t*)&tag, 4); 43 CRYPT_MD5Update(md5, (uint8_t*)&tag, 4);
44 } 44 }
45 uint8_t digest[16]; 45 uint8_t digest[16];
46 CRYPT_MD5Finish(md5, digest); 46 CRYPT_MD5Finish(md5, digest);
47 FX_DWORD copy_len = keylen; 47 FX_DWORD copy_len = keylen;
48 if (copy_len > sizeof(digest)) { 48 if (copy_len > sizeof(digest)) {
49 copy_len = sizeof(digest); 49 copy_len = sizeof(digest);
50 } 50 }
51 if (revision >= 3) { 51 if (revision >= 3) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 return CheckPassword(password, password.GetLength(), FALSE, m_EncryptKey, 103 return CheckPassword(password, password.GetLength(), FALSE, m_EncryptKey,
104 key_len); 104 key_len);
105 } 105 }
106 FX_DWORD CPDF_StandardSecurityHandler::GetPermissions() { 106 FX_DWORD CPDF_StandardSecurityHandler::GetPermissions() {
107 return m_Permissions; 107 return m_Permissions;
108 } 108 }
109 static FX_BOOL _LoadCryptInfo(CPDF_Dictionary* pEncryptDict, 109 static FX_BOOL _LoadCryptInfo(CPDF_Dictionary* pEncryptDict,
110 const CFX_ByteStringC& name, 110 const CFX_ByteStringC& name,
111 int& cipher, 111 int& cipher,
112 int& keylen) { 112 int& keylen) {
113 int Version = pEncryptDict->GetInteger("V"); 113 int Version = pEncryptDict->GetIntegerBy("V");
114 cipher = FXCIPHER_RC4; 114 cipher = FXCIPHER_RC4;
115 keylen = 0; 115 keylen = 0;
116 if (Version >= 4) { 116 if (Version >= 4) {
117 CPDF_Dictionary* pCryptFilters = pEncryptDict->GetDict("CF"); 117 CPDF_Dictionary* pCryptFilters = pEncryptDict->GetDictBy("CF");
118 if (!pCryptFilters) { 118 if (!pCryptFilters) {
119 return FALSE; 119 return FALSE;
120 } 120 }
121 if (name == "Identity") { 121 if (name == "Identity") {
122 cipher = FXCIPHER_NONE; 122 cipher = FXCIPHER_NONE;
123 } else { 123 } else {
124 CPDF_Dictionary* pDefFilter = pCryptFilters->GetDict(name); 124 CPDF_Dictionary* pDefFilter = pCryptFilters->GetDictBy(name);
125 if (!pDefFilter) { 125 if (!pDefFilter) {
126 return FALSE; 126 return FALSE;
127 } 127 }
128 int nKeyBits = 0; 128 int nKeyBits = 0;
129 if (Version == 4) { 129 if (Version == 4) {
130 nKeyBits = pDefFilter->GetInteger("Length", 0); 130 nKeyBits = pDefFilter->GetIntegerBy("Length", 0);
131 if (nKeyBits == 0) { 131 if (nKeyBits == 0) {
132 nKeyBits = pEncryptDict->GetInteger("Length", 128); 132 nKeyBits = pEncryptDict->GetIntegerBy("Length", 128);
133 } 133 }
134 } else { 134 } else {
135 nKeyBits = pEncryptDict->GetInteger("Length", 256); 135 nKeyBits = pEncryptDict->GetIntegerBy("Length", 256);
136 } 136 }
137 if (nKeyBits < 40) { 137 if (nKeyBits < 40) {
138 nKeyBits *= 8; 138 nKeyBits *= 8;
139 } 139 }
140 keylen = nKeyBits / 8; 140 keylen = nKeyBits / 8;
141 CFX_ByteString cipher_name = pDefFilter->GetString("CFM"); 141 CFX_ByteString cipher_name = pDefFilter->GetStringBy("CFM");
142 if (cipher_name == "AESV2" || cipher_name == "AESV3") { 142 if (cipher_name == "AESV2" || cipher_name == "AESV3") {
143 cipher = FXCIPHER_AES; 143 cipher = FXCIPHER_AES;
144 } 144 }
145 } 145 }
146 } else { 146 } else {
147 keylen = Version > 1 ? pEncryptDict->GetInteger("Length", 40) / 8 : 5; 147 keylen = Version > 1 ? pEncryptDict->GetIntegerBy("Length", 40) / 8 : 5;
148 } 148 }
149 if (keylen > 32 || keylen < 0) { 149 if (keylen > 32 || keylen < 0) {
150 return FALSE; 150 return FALSE;
151 } 151 }
152 return TRUE; 152 return TRUE;
153 } 153 }
154 FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict) { 154 FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict) {
155 m_pEncryptDict = pEncryptDict; 155 m_pEncryptDict = pEncryptDict;
156 m_bOwner = FALSE; 156 m_bOwner = FALSE;
157 m_Version = pEncryptDict->GetInteger("V"); 157 m_Version = pEncryptDict->GetIntegerBy("V");
158 m_Revision = pEncryptDict->GetInteger("R"); 158 m_Revision = pEncryptDict->GetIntegerBy("R");
159 m_Permissions = pEncryptDict->GetInteger("P", -1); 159 m_Permissions = pEncryptDict->GetIntegerBy("P", -1);
160 if (m_Version < 4) { 160 if (m_Version < 4) {
161 return _LoadCryptInfo(pEncryptDict, CFX_ByteString(), m_Cipher, m_KeyLen); 161 return _LoadCryptInfo(pEncryptDict, CFX_ByteString(), m_Cipher, m_KeyLen);
162 } 162 }
163 CFX_ByteString stmf_name = pEncryptDict->GetString("StmF"); 163 CFX_ByteString stmf_name = pEncryptDict->GetStringBy("StmF");
164 CFX_ByteString strf_name = pEncryptDict->GetString("StrF"); 164 CFX_ByteString strf_name = pEncryptDict->GetStringBy("StrF");
165 if (stmf_name != strf_name) { 165 if (stmf_name != strf_name) {
166 return FALSE; 166 return FALSE;
167 } 167 }
168 if (!_LoadCryptInfo(pEncryptDict, strf_name, m_Cipher, m_KeyLen)) { 168 if (!_LoadCryptInfo(pEncryptDict, strf_name, m_Cipher, m_KeyLen)) {
169 return FALSE; 169 return FALSE;
170 } 170 }
171 return TRUE; 171 return TRUE;
172 } 172 }
173 FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict, 173 FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict,
174 FX_DWORD type, 174 FX_DWORD type,
175 int& cipher, 175 int& cipher,
176 int& key_len) { 176 int& key_len) {
177 m_pEncryptDict = pEncryptDict; 177 m_pEncryptDict = pEncryptDict;
178 m_bOwner = FALSE; 178 m_bOwner = FALSE;
179 m_Version = pEncryptDict->GetInteger("V"); 179 m_Version = pEncryptDict->GetIntegerBy("V");
180 m_Revision = pEncryptDict->GetInteger("R"); 180 m_Revision = pEncryptDict->GetIntegerBy("R");
181 m_Permissions = pEncryptDict->GetInteger("P", -1); 181 m_Permissions = pEncryptDict->GetIntegerBy("P", -1);
182 CFX_ByteString strf_name, stmf_name; 182 CFX_ByteString strf_name, stmf_name;
183 if (m_Version >= 4) { 183 if (m_Version >= 4) {
184 stmf_name = pEncryptDict->GetString("StmF"); 184 stmf_name = pEncryptDict->GetStringBy("StmF");
185 strf_name = pEncryptDict->GetString("StrF"); 185 strf_name = pEncryptDict->GetStringBy("StrF");
186 if (stmf_name != strf_name) { 186 if (stmf_name != strf_name) {
187 return FALSE; 187 return FALSE;
188 } 188 }
189 } 189 }
190 if (!_LoadCryptInfo(pEncryptDict, strf_name, cipher, key_len)) { 190 if (!_LoadCryptInfo(pEncryptDict, strf_name, cipher, key_len)) {
191 return FALSE; 191 return FALSE;
192 } 192 }
193 m_Cipher = cipher; 193 m_Cipher = cipher;
194 m_KeyLen = key_len; 194 m_KeyLen = key_len;
195 return TRUE; 195 return TRUE;
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 if (hash) { 295 if (hash) {
296 FXSYS_memcpy(hash, input, 32); 296 FXSYS_memcpy(hash, input, 32);
297 } 297 }
298 } 298 }
299 FX_BOOL CPDF_StandardSecurityHandler::AES256_CheckPassword( 299 FX_BOOL CPDF_StandardSecurityHandler::AES256_CheckPassword(
300 const uint8_t* password, 300 const uint8_t* password,
301 FX_DWORD size, 301 FX_DWORD size,
302 FX_BOOL bOwner, 302 FX_BOOL bOwner,
303 uint8_t* key) { 303 uint8_t* key) {
304 CFX_ByteString okey = 304 CFX_ByteString okey =
305 m_pEncryptDict ? m_pEncryptDict->GetString("O") : CFX_ByteString(); 305 m_pEncryptDict ? m_pEncryptDict->GetStringBy("O") : CFX_ByteString();
306 if (okey.GetLength() < 48) { 306 if (okey.GetLength() < 48) {
307 return FALSE; 307 return FALSE;
308 } 308 }
309 CFX_ByteString ukey = 309 CFX_ByteString ukey =
310 m_pEncryptDict ? m_pEncryptDict->GetString("U") : CFX_ByteString(); 310 m_pEncryptDict ? m_pEncryptDict->GetStringBy("U") : CFX_ByteString();
311 if (ukey.GetLength() < 48) { 311 if (ukey.GetLength() < 48) {
312 return FALSE; 312 return FALSE;
313 } 313 }
314 const uint8_t* pkey = bOwner ? (const uint8_t*)okey : (const uint8_t*)ukey; 314 const uint8_t* pkey = bOwner ? (const uint8_t*)okey : (const uint8_t*)ukey;
315 uint8_t sha[128]; 315 uint8_t sha[128];
316 uint8_t digest[32]; 316 uint8_t digest[32];
317 if (m_Revision >= 6) { 317 if (m_Revision >= 6) {
318 Revision6_Hash(password, size, (const uint8_t*)pkey + 32, 318 Revision6_Hash(password, size, (const uint8_t*)pkey + 32,
319 (bOwner ? (const uint8_t*)ukey : NULL), digest); 319 (bOwner ? (const uint8_t*)ukey : NULL), digest);
320 } else { 320 } else {
(...skipping 17 matching lines...) Expand all
338 } else { 338 } else {
339 CRYPT_SHA256Start(sha); 339 CRYPT_SHA256Start(sha);
340 CRYPT_SHA256Update(sha, password, size); 340 CRYPT_SHA256Update(sha, password, size);
341 CRYPT_SHA256Update(sha, pkey + 40, 8); 341 CRYPT_SHA256Update(sha, pkey + 40, 8);
342 if (bOwner) { 342 if (bOwner) {
343 CRYPT_SHA256Update(sha, ukey, 48); 343 CRYPT_SHA256Update(sha, ukey, 48);
344 } 344 }
345 CRYPT_SHA256Finish(sha, digest); 345 CRYPT_SHA256Finish(sha, digest);
346 } 346 }
347 CFX_ByteString ekey = m_pEncryptDict 347 CFX_ByteString ekey = m_pEncryptDict
348 ? m_pEncryptDict->GetString(bOwner ? "OE" : "UE") 348 ? m_pEncryptDict->GetStringBy(bOwner ? "OE" : "UE")
349 : CFX_ByteString(); 349 : CFX_ByteString();
350 if (ekey.GetLength() < 32) { 350 if (ekey.GetLength() < 32) {
351 return FALSE; 351 return FALSE;
352 } 352 }
353 uint8_t* aes = FX_Alloc(uint8_t, 2048); 353 uint8_t* aes = FX_Alloc(uint8_t, 2048);
354 CRYPT_AESSetKey(aes, 16, digest, 32, FALSE); 354 CRYPT_AESSetKey(aes, 16, digest, 32, FALSE);
355 uint8_t iv[16]; 355 uint8_t iv[16];
356 FXSYS_memset(iv, 0, 16); 356 FXSYS_memset(iv, 0, 16);
357 CRYPT_AESSetIV(aes, iv); 357 CRYPT_AESSetIV(aes, iv);
358 CRYPT_AESDecrypt(aes, key, ekey, 32); 358 CRYPT_AESDecrypt(aes, key, ekey, 32);
359 CRYPT_AESSetKey(aes, 16, key, 32, FALSE); 359 CRYPT_AESSetKey(aes, 16, key, 32, FALSE);
360 CRYPT_AESSetIV(aes, iv); 360 CRYPT_AESSetIV(aes, iv);
361 CFX_ByteString perms = m_pEncryptDict->GetString("Perms"); 361 CFX_ByteString perms = m_pEncryptDict->GetStringBy("Perms");
362 if (perms.IsEmpty()) { 362 if (perms.IsEmpty()) {
363 return FALSE; 363 return FALSE;
364 } 364 }
365 uint8_t perms_buf[16]; 365 uint8_t perms_buf[16];
366 FXSYS_memset(perms_buf, 0, sizeof(perms_buf)); 366 FXSYS_memset(perms_buf, 0, sizeof(perms_buf));
367 FX_DWORD copy_len = sizeof(perms_buf); 367 FX_DWORD copy_len = sizeof(perms_buf);
368 if (copy_len > (FX_DWORD)perms.GetLength()) { 368 if (copy_len > (FX_DWORD)perms.GetLength()) {
369 copy_len = perms.GetLength(); 369 copy_len = perms.GetLength();
370 } 370 }
371 FXSYS_memcpy(perms_buf, (const uint8_t*)perms, copy_len); 371 FXSYS_memcpy(perms_buf, (const uint8_t*)perms, copy_len);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 } 410 }
411 FX_BOOL CPDF_StandardSecurityHandler::CheckUserPassword( 411 FX_BOOL CPDF_StandardSecurityHandler::CheckUserPassword(
412 const uint8_t* password, 412 const uint8_t* password,
413 FX_DWORD pass_size, 413 FX_DWORD pass_size,
414 FX_BOOL bIgnoreEncryptMeta, 414 FX_BOOL bIgnoreEncryptMeta,
415 uint8_t* key, 415 uint8_t* key,
416 int32_t key_len) { 416 int32_t key_len) {
417 CalcEncryptKey(m_pEncryptDict, password, pass_size, key, key_len, 417 CalcEncryptKey(m_pEncryptDict, password, pass_size, key, key_len,
418 bIgnoreEncryptMeta, m_pParser->GetIDArray()); 418 bIgnoreEncryptMeta, m_pParser->GetIDArray());
419 CFX_ByteString ukey = 419 CFX_ByteString ukey =
420 m_pEncryptDict ? m_pEncryptDict->GetString("U") : CFX_ByteString(); 420 m_pEncryptDict ? m_pEncryptDict->GetStringBy("U") : CFX_ByteString();
421 if (ukey.GetLength() < 16) { 421 if (ukey.GetLength() < 16) {
422 return FALSE; 422 return FALSE;
423 } 423 }
424 uint8_t ukeybuf[32]; 424 uint8_t ukeybuf[32];
425 if (m_Revision == 2) { 425 if (m_Revision == 2) {
426 FXSYS_memcpy(ukeybuf, defpasscode, 32); 426 FXSYS_memcpy(ukeybuf, defpasscode, 32);
427 CRYPT_ArcFourCryptBlock(ukeybuf, 32, key, key_len); 427 CRYPT_ArcFourCryptBlock(ukeybuf, 32, key, key_len);
428 } else { 428 } else {
429 uint8_t test[32], tmpkey[32]; 429 uint8_t test[32], tmpkey[32];
430 FX_DWORD copy_len = sizeof(test); 430 FX_DWORD copy_len = sizeof(test);
431 if (copy_len > (FX_DWORD)ukey.GetLength()) { 431 if (copy_len > (FX_DWORD)ukey.GetLength()) {
432 copy_len = ukey.GetLength(); 432 copy_len = ukey.GetLength();
433 } 433 }
434 FXSYS_memset(test, 0, sizeof(test)); 434 FXSYS_memset(test, 0, sizeof(test));
435 FXSYS_memset(tmpkey, 0, sizeof(tmpkey)); 435 FXSYS_memset(tmpkey, 0, sizeof(tmpkey));
436 FXSYS_memcpy(test, ukey.c_str(), copy_len); 436 FXSYS_memcpy(test, ukey.c_str(), copy_len);
437 for (int i = 19; i >= 0; i--) { 437 for (int i = 19; i >= 0; i--) {
438 for (int j = 0; j < key_len; j++) { 438 for (int j = 0; j < key_len; j++) {
439 tmpkey[j] = key[j] ^ i; 439 tmpkey[j] = key[j] ^ i;
440 } 440 }
441 CRYPT_ArcFourCryptBlock(test, 32, tmpkey, key_len); 441 CRYPT_ArcFourCryptBlock(test, 32, tmpkey, key_len);
442 } 442 }
443 uint8_t md5[100]; 443 uint8_t md5[100];
444 CRYPT_MD5Start(md5); 444 CRYPT_MD5Start(md5);
445 CRYPT_MD5Update(md5, defpasscode, 32); 445 CRYPT_MD5Update(md5, defpasscode, 32);
446 CPDF_Array* pIdArray = m_pParser->GetIDArray(); 446 CPDF_Array* pIdArray = m_pParser->GetIDArray();
447 if (pIdArray) { 447 if (pIdArray) {
448 CFX_ByteString id = pIdArray->GetString(0); 448 CFX_ByteString id = pIdArray->GetStringAt(0);
449 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); 449 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength());
450 } 450 }
451 CRYPT_MD5Finish(md5, ukeybuf); 451 CRYPT_MD5Finish(md5, ukeybuf);
452 return FXSYS_memcmp(test, ukeybuf, 16) == 0; 452 return FXSYS_memcmp(test, ukeybuf, 16) == 0;
453 } 453 }
454 if (FXSYS_memcmp((void*)ukey.c_str(), ukeybuf, 16) == 0) { 454 if (FXSYS_memcmp((void*)ukey.c_str(), ukeybuf, 16) == 0) {
455 return TRUE; 455 return TRUE;
456 } 456 }
457 return FALSE; 457 return FALSE;
458 } 458 }
459 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword( 459 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(
460 const uint8_t* owner_pass, 460 const uint8_t* owner_pass,
461 FX_DWORD pass_size) { 461 FX_DWORD pass_size) {
462 return GetUserPassword(owner_pass, pass_size, m_KeyLen); 462 return GetUserPassword(owner_pass, pass_size, m_KeyLen);
463 } 463 }
464 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword( 464 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(
465 const uint8_t* owner_pass, 465 const uint8_t* owner_pass,
466 FX_DWORD pass_size, 466 FX_DWORD pass_size,
467 int32_t key_len) { 467 int32_t key_len) {
468 CFX_ByteString okey = m_pEncryptDict->GetString("O"); 468 CFX_ByteString okey = m_pEncryptDict->GetStringBy("O");
469 uint8_t passcode[32]; 469 uint8_t passcode[32];
470 FX_DWORD i; 470 FX_DWORD i;
471 for (i = 0; i < 32; i++) { 471 for (i = 0; i < 32; i++) {
472 passcode[i] = i < pass_size ? owner_pass[i] : defpasscode[i - pass_size]; 472 passcode[i] = i < pass_size ? owner_pass[i] : defpasscode[i - pass_size];
473 } 473 }
474 uint8_t digest[16]; 474 uint8_t digest[16];
475 CRYPT_MD5Generate(passcode, 32, digest); 475 CRYPT_MD5Generate(passcode, 32, digest);
476 if (m_Revision >= 3) { 476 if (m_Revision >= 3) {
477 for (int i = 0; i < 50; i++) { 477 for (int i = 0; i < 50; i++) {
478 CRYPT_MD5Generate(digest, 16, digest); 478 CRYPT_MD5Generate(digest, 16, digest);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 int32_t key_len) { 517 int32_t key_len) {
518 CFX_ByteString user_pass = GetUserPassword(password, pass_size, key_len); 518 CFX_ByteString user_pass = GetUserPassword(password, pass_size, key_len);
519 if (CheckUserPassword(user_pass, user_pass.GetLength(), FALSE, key, 519 if (CheckUserPassword(user_pass, user_pass.GetLength(), FALSE, key,
520 key_len)) { 520 key_len)) {
521 return TRUE; 521 return TRUE;
522 } 522 }
523 return CheckUserPassword(user_pass, user_pass.GetLength(), TRUE, key, 523 return CheckUserPassword(user_pass, user_pass.GetLength(), TRUE, key,
524 key_len); 524 key_len);
525 } 525 }
526 FX_BOOL CPDF_StandardSecurityHandler::IsMetadataEncrypted() { 526 FX_BOOL CPDF_StandardSecurityHandler::IsMetadataEncrypted() {
527 return m_pEncryptDict->GetBoolean("EncryptMetadata", TRUE); 527 return m_pEncryptDict->GetBooleanBy("EncryptMetadata", TRUE);
528 } 528 }
529 CPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler() { 529 CPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler() {
530 return new CPDF_StandardSecurityHandler; 530 return new CPDF_StandardSecurityHandler;
531 } 531 }
532 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, 532 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict,
533 CPDF_Array* pIdArray, 533 CPDF_Array* pIdArray,
534 const uint8_t* user_pass, 534 const uint8_t* user_pass,
535 FX_DWORD user_size, 535 FX_DWORD user_size,
536 const uint8_t* owner_pass, 536 const uint8_t* owner_pass,
537 FX_DWORD owner_size, 537 FX_DWORD owner_size,
(...skipping 13 matching lines...) Expand all
551 CRYPT_SHA256Start(sha); 551 CRYPT_SHA256Start(sha);
552 CRYPT_SHA256Update(sha, (uint8_t*)&t, sizeof t); 552 CRYPT_SHA256Update(sha, (uint8_t*)&t, sizeof t);
553 CRYPT_SHA256Update(sha, m_EncryptKey, 32); 553 CRYPT_SHA256Update(sha, m_EncryptKey, 32);
554 CRYPT_SHA256Update(sha, (uint8_t*)"there", 5); 554 CRYPT_SHA256Update(sha, (uint8_t*)"there", 5);
555 CRYPT_SHA256Finish(sha, m_EncryptKey); 555 CRYPT_SHA256Finish(sha, m_EncryptKey);
556 AES256_SetPassword(pEncryptDict, user_pass, user_size, FALSE, m_EncryptKey); 556 AES256_SetPassword(pEncryptDict, user_pass, user_size, FALSE, m_EncryptKey);
557 if (bDefault) { 557 if (bDefault) {
558 AES256_SetPassword(pEncryptDict, owner_pass, owner_size, TRUE, 558 AES256_SetPassword(pEncryptDict, owner_pass, owner_size, TRUE,
559 m_EncryptKey); 559 m_EncryptKey);
560 AES256_SetPerms(pEncryptDict, m_Permissions, 560 AES256_SetPerms(pEncryptDict, m_Permissions,
561 pEncryptDict->GetBoolean("EncryptMetadata", TRUE), 561 pEncryptDict->GetBooleanBy("EncryptMetadata", TRUE),
562 m_EncryptKey); 562 m_EncryptKey);
563 } 563 }
564 return; 564 return;
565 } 565 }
566 if (bDefault) { 566 if (bDefault) {
567 uint8_t passcode[32]; 567 uint8_t passcode[32];
568 FX_DWORD i; 568 FX_DWORD i;
569 for (i = 0; i < 32; i++) { 569 for (i = 0; i < 32; i++) {
570 passcode[i] = 570 passcode[i] =
571 i < owner_size ? owner_pass[i] : defpasscode[i - owner_size]; 571 i < owner_size ? owner_pass[i] : defpasscode[i - owner_size];
(...skipping 27 matching lines...) Expand all
599 if (m_Revision < 3) { 599 if (m_Revision < 3) {
600 uint8_t tempbuf[32]; 600 uint8_t tempbuf[32];
601 FXSYS_memcpy(tempbuf, defpasscode, 32); 601 FXSYS_memcpy(tempbuf, defpasscode, 32);
602 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); 602 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len);
603 pEncryptDict->SetAtString("U", CFX_ByteString(tempbuf, 32)); 603 pEncryptDict->SetAtString("U", CFX_ByteString(tempbuf, 32));
604 } else { 604 } else {
605 uint8_t md5[100]; 605 uint8_t md5[100];
606 CRYPT_MD5Start(md5); 606 CRYPT_MD5Start(md5);
607 CRYPT_MD5Update(md5, defpasscode, 32); 607 CRYPT_MD5Update(md5, defpasscode, 32);
608 if (pIdArray) { 608 if (pIdArray) {
609 CFX_ByteString id = pIdArray->GetString(0); 609 CFX_ByteString id = pIdArray->GetStringAt(0);
610 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); 610 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength());
611 } 611 }
612 uint8_t digest[32]; 612 uint8_t digest[32];
613 CRYPT_MD5Finish(md5, digest); 613 CRYPT_MD5Finish(md5, digest);
614 CRYPT_ArcFourCryptBlock(digest, 16, m_EncryptKey, key_len); 614 CRYPT_ArcFourCryptBlock(digest, 16, m_EncryptKey, key_len);
615 uint8_t tempkey[32]; 615 uint8_t tempkey[32];
616 for (int i = 1; i <= 19; i++) { 616 for (int i = 1; i <= 19; i++) {
617 for (int j = 0; j < key_len; j++) { 617 for (int j = 0; j < key_len; j++) {
618 tempkey[j] = m_EncryptKey[j] ^ (uint8_t)i; 618 tempkey[j] = m_EncryptKey[j] ^ (uint8_t)i;
619 } 619 }
(...skipping 25 matching lines...) Expand all
645 const uint8_t* password, 645 const uint8_t* password,
646 FX_DWORD size, 646 FX_DWORD size,
647 FX_BOOL bOwner, 647 FX_BOOL bOwner,
648 const uint8_t* key) { 648 const uint8_t* key) {
649 uint8_t sha[128]; 649 uint8_t sha[128];
650 CRYPT_SHA1Start(sha); 650 CRYPT_SHA1Start(sha);
651 CRYPT_SHA1Update(sha, key, 32); 651 CRYPT_SHA1Update(sha, key, 32);
652 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); 652 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5);
653 uint8_t digest[20]; 653 uint8_t digest[20];
654 CRYPT_SHA1Finish(sha, digest); 654 CRYPT_SHA1Finish(sha, digest);
655 CFX_ByteString ukey = pEncryptDict->GetString("U"); 655 CFX_ByteString ukey = pEncryptDict->GetStringBy("U");
656 uint8_t digest1[48]; 656 uint8_t digest1[48];
657 if (m_Revision >= 6) { 657 if (m_Revision >= 6) {
658 Revision6_Hash(password, size, digest, 658 Revision6_Hash(password, size, digest,
659 (bOwner ? (const uint8_t*)ukey : NULL), digest1); 659 (bOwner ? (const uint8_t*)ukey : NULL), digest1);
660 } else { 660 } else {
661 CRYPT_SHA256Start(sha); 661 CRYPT_SHA256Start(sha);
662 CRYPT_SHA256Update(sha, password, size); 662 CRYPT_SHA256Update(sha, password, size);
663 CRYPT_SHA256Update(sha, digest, 8); 663 CRYPT_SHA256Update(sha, digest, 8);
664 if (bOwner) { 664 if (bOwner) {
665 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); 665 CRYPT_SHA256Update(sha, ukey, ukey.GetLength());
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
1029 str = dest_buf; 1029 str = dest_buf;
1030 } 1030 }
1031 CPDF_StandardCryptoHandler::CPDF_StandardCryptoHandler() { 1031 CPDF_StandardCryptoHandler::CPDF_StandardCryptoHandler() {
1032 m_pAESContext = NULL; 1032 m_pAESContext = NULL;
1033 m_Cipher = FXCIPHER_NONE; 1033 m_Cipher = FXCIPHER_NONE;
1034 m_KeyLen = 0; 1034 m_KeyLen = 0;
1035 } 1035 }
1036 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() { 1036 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() {
1037 FX_Free(m_pAESContext); 1037 FX_Free(m_pAESContext);
1038 } 1038 }
OLDNEW
« no previous file with comments | « core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp ('k') | core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698