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

Side by Side Diff: core/fpdfapi/fpdf_parser/cpdf_security_handler.cpp

Issue 2005653002: Distinguish between user and owner passwords. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: fix bug Created 4 years, 7 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/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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 for (int i = 0; i < 50; i++) { 60 for (int i = 0; i < 50; i++) {
61 CRYPT_MD5Generate(digest, copy_len, digest); 61 CRYPT_MD5Generate(digest, copy_len, digest);
62 } 62 }
63 } 63 }
64 FXSYS_memset(key, 0, keylen); 64 FXSYS_memset(key, 0, keylen);
65 FXSYS_memcpy(key, digest, copy_len); 65 FXSYS_memcpy(key, digest, copy_len);
66 } 66 }
67 67
68 } // namespace 68 } // namespace
69 69
70 CPDF_SecurityHandler::CPDF_SecurityHandler() { 70 CPDF_SecurityHandler::CPDF_SecurityHandler()
71 m_Version = 0; 71 : m_Version(0),
72 m_Revision = 0; 72 m_Revision(0),
73 m_pParser = NULL; 73 m_pParser(nullptr),
74 m_pEncryptDict = NULL; 74 m_pEncryptDict(nullptr),
75 m_Permissions = 0; 75 m_Permissions(0),
76 m_Cipher = FXCIPHER_NONE; 76 m_Cipher(FXCIPHER_NONE),
77 m_KeyLen = 0; 77 m_KeyLen(0),
78 } 78 m_bOwnerUnlocked(false) {}
79 79
80 CPDF_SecurityHandler::~CPDF_SecurityHandler() {} 80 CPDF_SecurityHandler::~CPDF_SecurityHandler() {}
81 81
82 CPDF_CryptoHandler* CPDF_SecurityHandler::CreateCryptoHandler() { 82 CPDF_CryptoHandler* CPDF_SecurityHandler::CreateCryptoHandler() {
83 return new CPDF_CryptoHandler; 83 return new CPDF_CryptoHandler;
84 } 84 }
85 85
86 FX_BOOL CPDF_SecurityHandler::OnInit(CPDF_Parser* pParser, 86 FX_BOOL CPDF_SecurityHandler::OnInit(CPDF_Parser* pParser,
87 CPDF_Dictionary* pEncryptDict) { 87 CPDF_Dictionary* pEncryptDict) {
88 m_pParser = pParser; 88 m_pParser = pParser;
89 if (!LoadDict(pEncryptDict)) { 89 if (!LoadDict(pEncryptDict)) {
90 return FALSE; 90 return FALSE;
91 } 91 }
92 if (m_Cipher == FXCIPHER_NONE) { 92 if (m_Cipher == FXCIPHER_NONE) {
93 return TRUE; 93 return TRUE;
94 } 94 }
95 return CheckSecurity(m_KeyLen); 95 return CheckSecurity(m_KeyLen);
96 } 96 }
97
97 FX_BOOL CPDF_SecurityHandler::CheckSecurity(int32_t key_len) { 98 FX_BOOL CPDF_SecurityHandler::CheckSecurity(int32_t key_len) {
98 CFX_ByteString password = m_pParser->GetPassword(); 99 CFX_ByteString password = m_pParser->GetPassword();
99 if (CheckPassword(password.raw_str(), password.GetLength(), TRUE, 100 if (!password.IsEmpty() &&
101 CheckPassword(password.raw_str(), password.GetLength(), TRUE,
Tom Sepez 2016/05/23 16:30:26 Nit: candiadate for taking a const CFX_ByteStringC
Lei Zhang 2016/05/23 18:41:24 Yep. I will take care of this in a follow up CL.
100 m_EncryptKey, key_len)) { 102 m_EncryptKey, key_len)) {
101 if (password.IsEmpty()) { 103 m_bOwnerUnlocked = true;
102 if (!CheckPassword(password.raw_str(), password.GetLength(), FALSE,
103 m_EncryptKey, key_len)) {
104 return FALSE;
105 }
106 }
107 return TRUE; 104 return TRUE;
108 } 105 }
109 return CheckPassword(password.raw_str(), password.GetLength(), FALSE, 106 return CheckPassword(password.raw_str(), password.GetLength(), FALSE,
110 m_EncryptKey, key_len); 107 m_EncryptKey, key_len);
111 } 108 }
109
112 uint32_t CPDF_SecurityHandler::GetPermissions() { 110 uint32_t CPDF_SecurityHandler::GetPermissions() {
113 return m_Permissions; 111 return m_bOwnerUnlocked ? 0xFFFFFFFF : m_Permissions;
114 } 112 }
115 113
116 static FX_BOOL LoadCryptInfo(CPDF_Dictionary* pEncryptDict, 114 static FX_BOOL LoadCryptInfo(CPDF_Dictionary* pEncryptDict,
117 const CFX_ByteString& name, 115 const CFX_ByteString& name,
118 int& cipher, 116 int& cipher,
119 int& keylen) { 117 int& keylen) {
120 int Version = pEncryptDict->GetIntegerBy("V"); 118 int Version = pEncryptDict->GetIntegerBy("V");
121 cipher = FXCIPHER_RC4; 119 cipher = FXCIPHER_RC4;
122 keylen = 0; 120 keylen = 0;
123 if (Version >= 4) { 121 if (Version >= 4) {
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 buf[11] = 'b'; 694 buf[11] = 'b';
697 uint8_t* aes = FX_Alloc(uint8_t, 2048); 695 uint8_t* aes = FX_Alloc(uint8_t, 2048);
698 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); 696 CRYPT_AESSetKey(aes, 16, key, 32, TRUE);
699 uint8_t iv[16], buf1[16]; 697 uint8_t iv[16], buf1[16];
700 FXSYS_memset(iv, 0, 16); 698 FXSYS_memset(iv, 0, 16);
701 CRYPT_AESSetIV(aes, iv); 699 CRYPT_AESSetIV(aes, iv);
702 CRYPT_AESEncrypt(aes, buf1, buf, 16); 700 CRYPT_AESEncrypt(aes, buf1, buf, 16);
703 FX_Free(aes); 701 FX_Free(aes);
704 pEncryptDict->SetAtString("Perms", CFX_ByteString(buf1, 16)); 702 pEncryptDict->SetAtString("Perms", CFX_ByteString(buf1, 16));
705 } 703 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698