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/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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 } | 105 } |
106 } | 106 } |
107 return TRUE; | 107 return TRUE; |
108 } | 108 } |
109 return CheckPassword(password.raw_str(), password.GetLength(), FALSE, | 109 return CheckPassword(password.raw_str(), password.GetLength(), FALSE, |
110 m_EncryptKey, key_len); | 110 m_EncryptKey, key_len); |
111 } | 111 } |
112 uint32_t CPDF_SecurityHandler::GetPermissions() { | 112 uint32_t CPDF_SecurityHandler::GetPermissions() { |
113 return m_Permissions; | 113 return m_Permissions; |
114 } | 114 } |
115 static FX_BOOL _LoadCryptInfo(CPDF_Dictionary* pEncryptDict, | 115 |
116 const CFX_ByteStringC& name, | 116 static FX_BOOL LoadCryptInfo(CPDF_Dictionary* pEncryptDict, |
117 int& cipher, | 117 const CFX_ByteString& name, |
118 int& keylen) { | 118 int& cipher, |
| 119 int& keylen) { |
119 int Version = pEncryptDict->GetIntegerBy("V"); | 120 int Version = pEncryptDict->GetIntegerBy("V"); |
120 cipher = FXCIPHER_RC4; | 121 cipher = FXCIPHER_RC4; |
121 keylen = 0; | 122 keylen = 0; |
122 if (Version >= 4) { | 123 if (Version >= 4) { |
123 CPDF_Dictionary* pCryptFilters = pEncryptDict->GetDictBy("CF"); | 124 CPDF_Dictionary* pCryptFilters = pEncryptDict->GetDictBy("CF"); |
124 if (!pCryptFilters) { | 125 if (!pCryptFilters) { |
125 return FALSE; | 126 return FALSE; |
126 } | 127 } |
127 if (name == "Identity") { | 128 if (name == "Identity") { |
128 cipher = FXCIPHER_NONE; | 129 cipher = FXCIPHER_NONE; |
(...skipping 27 matching lines...) Expand all Loading... |
156 return FALSE; | 157 return FALSE; |
157 } | 158 } |
158 return TRUE; | 159 return TRUE; |
159 } | 160 } |
160 | 161 |
161 FX_BOOL CPDF_SecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict) { | 162 FX_BOOL CPDF_SecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict) { |
162 m_pEncryptDict = pEncryptDict; | 163 m_pEncryptDict = pEncryptDict; |
163 m_Version = pEncryptDict->GetIntegerBy("V"); | 164 m_Version = pEncryptDict->GetIntegerBy("V"); |
164 m_Revision = pEncryptDict->GetIntegerBy("R"); | 165 m_Revision = pEncryptDict->GetIntegerBy("R"); |
165 m_Permissions = pEncryptDict->GetIntegerBy("P", -1); | 166 m_Permissions = pEncryptDict->GetIntegerBy("P", -1); |
166 if (m_Version < 4) { | 167 if (m_Version < 4) |
167 return _LoadCryptInfo(pEncryptDict, CFX_ByteStringC(), m_Cipher, m_KeyLen); | 168 return LoadCryptInfo(pEncryptDict, CFX_ByteString(), m_Cipher, m_KeyLen); |
168 } | 169 |
169 CFX_ByteString stmf_name = pEncryptDict->GetStringBy("StmF"); | 170 CFX_ByteString stmf_name = pEncryptDict->GetStringBy("StmF"); |
170 CFX_ByteString strf_name = pEncryptDict->GetStringBy("StrF"); | 171 CFX_ByteString strf_name = pEncryptDict->GetStringBy("StrF"); |
171 if (stmf_name != strf_name) { | 172 if (stmf_name != strf_name) |
172 return FALSE; | 173 return FALSE; |
173 } | 174 |
174 if (!_LoadCryptInfo(pEncryptDict, strf_name.AsStringC(), m_Cipher, | 175 return LoadCryptInfo(pEncryptDict, strf_name, m_Cipher, m_KeyLen); |
175 m_KeyLen)) { | |
176 return FALSE; | |
177 } | |
178 return TRUE; | |
179 } | 176 } |
180 | 177 |
181 FX_BOOL CPDF_SecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict, | 178 FX_BOOL CPDF_SecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict, |
182 uint32_t type, | 179 uint32_t type, |
183 int& cipher, | 180 int& cipher, |
184 int& key_len) { | 181 int& key_len) { |
185 m_pEncryptDict = pEncryptDict; | 182 m_pEncryptDict = pEncryptDict; |
186 m_Version = pEncryptDict->GetIntegerBy("V"); | 183 m_Version = pEncryptDict->GetIntegerBy("V"); |
187 m_Revision = pEncryptDict->GetIntegerBy("R"); | 184 m_Revision = pEncryptDict->GetIntegerBy("R"); |
188 m_Permissions = pEncryptDict->GetIntegerBy("P", -1); | 185 m_Permissions = pEncryptDict->GetIntegerBy("P", -1); |
189 CFX_ByteString strf_name, stmf_name; | 186 |
| 187 CFX_ByteString strf_name; |
| 188 CFX_ByteString stmf_name; |
190 if (m_Version >= 4) { | 189 if (m_Version >= 4) { |
191 stmf_name = pEncryptDict->GetStringBy("StmF"); | 190 stmf_name = pEncryptDict->GetStringBy("StmF"); |
192 strf_name = pEncryptDict->GetStringBy("StrF"); | 191 strf_name = pEncryptDict->GetStringBy("StrF"); |
193 if (stmf_name != strf_name) { | 192 if (stmf_name != strf_name) |
194 return FALSE; | 193 return FALSE; |
195 } | |
196 } | 194 } |
197 if (!_LoadCryptInfo(pEncryptDict, strf_name.AsStringC(), cipher, key_len)) { | 195 if (!LoadCryptInfo(pEncryptDict, strf_name, cipher, key_len)) |
198 return FALSE; | 196 return FALSE; |
199 } | 197 |
200 m_Cipher = cipher; | 198 m_Cipher = cipher; |
201 m_KeyLen = key_len; | 199 m_KeyLen = key_len; |
202 return TRUE; | 200 return TRUE; |
203 } | 201 } |
204 | 202 |
205 FX_BOOL CPDF_SecurityHandler::GetCryptInfo(int& cipher, | 203 FX_BOOL CPDF_SecurityHandler::GetCryptInfo(int& cipher, |
206 const uint8_t*& buffer, | 204 const uint8_t*& buffer, |
207 int& keylen) { | 205 int& keylen) { |
208 cipher = m_Cipher; | 206 cipher = m_Cipher; |
209 buffer = m_EncryptKey; | 207 buffer = m_EncryptKey; |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 buf[11] = 'b'; | 696 buf[11] = 'b'; |
699 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 697 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
700 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); | 698 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); |
701 uint8_t iv[16], buf1[16]; | 699 uint8_t iv[16], buf1[16]; |
702 FXSYS_memset(iv, 0, 16); | 700 FXSYS_memset(iv, 0, 16); |
703 CRYPT_AESSetIV(aes, iv); | 701 CRYPT_AESSetIV(aes, iv); |
704 CRYPT_AESEncrypt(aes, buf1, buf, 16); | 702 CRYPT_AESEncrypt(aes, buf1, buf, 16); |
705 FX_Free(aes); | 703 FX_Free(aes); |
706 pEncryptDict->SetAtString("Perms", CFX_ByteString(buf1, 16)); | 704 pEncryptDict->SetAtString("Perms", CFX_ByteString(buf1, 16)); |
707 } | 705 } |
OLD | NEW |