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_standard_crypto_handler.h" | 7 #include "core/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h" |
8 | 8 |
9 #include <time.h> | 9 #include <time.h> |
10 | 10 |
11 #include "core/fdrm/crypto/include/fx_crypt.h" | 11 #include "core/fdrm/crypto/include/fx_crypt.h" |
12 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h" | 12 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h" |
13 #include "core/fpdfapi/fpdf_parser/include/cpdf_simple_parser.h" | 13 #include "core/fpdfapi/fpdf_parser/include/cpdf_simple_parser.h" |
14 #include "core/fpdfapi/fpdf_parser/ipdf_security_handler.h" | 14 #include "core/fpdfapi/fpdf_parser/ipdf_security_handler.h" |
15 | 15 |
16 IPDF_CryptoHandler::~IPDF_CryptoHandler() {} | 16 IPDF_CryptoHandler::~IPDF_CryptoHandler() {} |
17 | 17 |
18 void IPDF_CryptoHandler::Decrypt(FX_DWORD objnum, | 18 void IPDF_CryptoHandler::Decrypt(uint32_t objnum, |
19 FX_DWORD gennum, | 19 uint32_t gennum, |
20 CFX_ByteString& str) { | 20 CFX_ByteString& str) { |
21 CFX_BinaryBuf dest_buf; | 21 CFX_BinaryBuf dest_buf; |
22 void* context = DecryptStart(objnum, gennum); | 22 void* context = DecryptStart(objnum, gennum); |
23 DecryptStream(context, (const uint8_t*)str, str.GetLength(), dest_buf); | 23 DecryptStream(context, (const uint8_t*)str, str.GetLength(), dest_buf); |
24 DecryptFinish(context, dest_buf); | 24 DecryptFinish(context, dest_buf); |
25 str = dest_buf; | 25 str = dest_buf; |
26 } | 26 } |
27 | 27 |
28 void CPDF_StandardCryptoHandler::CryptBlock(FX_BOOL bEncrypt, | 28 void CPDF_StandardCryptoHandler::CryptBlock(FX_BOOL bEncrypt, |
29 FX_DWORD objnum, | 29 uint32_t objnum, |
30 FX_DWORD gennum, | 30 uint32_t gennum, |
31 const uint8_t* src_buf, | 31 const uint8_t* src_buf, |
32 FX_DWORD src_size, | 32 uint32_t src_size, |
33 uint8_t* dest_buf, | 33 uint8_t* dest_buf, |
34 FX_DWORD& dest_size) { | 34 uint32_t& dest_size) { |
35 if (m_Cipher == FXCIPHER_NONE) { | 35 if (m_Cipher == FXCIPHER_NONE) { |
36 FXSYS_memcpy(dest_buf, src_buf, src_size); | 36 FXSYS_memcpy(dest_buf, src_buf, src_size); |
37 return; | 37 return; |
38 } | 38 } |
39 uint8_t realkey[16]; | 39 uint8_t realkey[16]; |
40 int realkeylen = 16; | 40 int realkeylen = 16; |
41 if (m_Cipher != FXCIPHER_AES || m_KeyLen != 32) { | 41 if (m_Cipher != FXCIPHER_AES || m_KeyLen != 32) { |
42 uint8_t key1[32]; | 42 uint8_t key1[32]; |
43 FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen); | 43 FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen); |
44 key1[m_KeyLen + 0] = (uint8_t)objnum; | 44 key1[m_KeyLen + 0] = (uint8_t)objnum; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 FXSYS_memcpy(dest_buf, src_buf, src_size); | 89 FXSYS_memcpy(dest_buf, src_buf, src_size); |
90 } | 90 } |
91 CRYPT_ArcFourCryptBlock(dest_buf, dest_size, realkey, realkeylen); | 91 CRYPT_ArcFourCryptBlock(dest_buf, dest_size, realkey, realkeylen); |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
95 struct AESCryptContext { | 95 struct AESCryptContext { |
96 uint8_t m_Context[2048]; | 96 uint8_t m_Context[2048]; |
97 FX_BOOL m_bIV; | 97 FX_BOOL m_bIV; |
98 uint8_t m_Block[16]; | 98 uint8_t m_Block[16]; |
99 FX_DWORD m_BlockOffset; | 99 uint32_t m_BlockOffset; |
100 }; | 100 }; |
101 | 101 |
102 void* CPDF_StandardCryptoHandler::CryptStart(FX_DWORD objnum, | 102 void* CPDF_StandardCryptoHandler::CryptStart(uint32_t objnum, |
103 FX_DWORD gennum, | 103 uint32_t gennum, |
104 FX_BOOL bEncrypt) { | 104 FX_BOOL bEncrypt) { |
105 if (m_Cipher == FXCIPHER_NONE) { | 105 if (m_Cipher == FXCIPHER_NONE) { |
106 return this; | 106 return this; |
107 } | 107 } |
108 if (m_Cipher == FXCIPHER_AES && m_KeyLen == 32) { | 108 if (m_Cipher == FXCIPHER_AES && m_KeyLen == 32) { |
109 AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); | 109 AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); |
110 pContext->m_bIV = TRUE; | 110 pContext->m_bIV = TRUE; |
111 pContext->m_BlockOffset = 0; | 111 pContext->m_BlockOffset = 0; |
112 CRYPT_AESSetKey(pContext->m_Context, 16, m_EncryptKey, 32, bEncrypt); | 112 CRYPT_AESSetKey(pContext->m_Context, 16, m_EncryptKey, 32, bEncrypt); |
113 if (bEncrypt) { | 113 if (bEncrypt) { |
(...skipping 30 matching lines...) Expand all Loading... |
144 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); | 144 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); |
145 } | 145 } |
146 return pContext; | 146 return pContext; |
147 } | 147 } |
148 void* pContext = FX_Alloc(uint8_t, 1040); | 148 void* pContext = FX_Alloc(uint8_t, 1040); |
149 CRYPT_ArcFourSetup(pContext, realkey, realkeylen); | 149 CRYPT_ArcFourSetup(pContext, realkey, realkeylen); |
150 return pContext; | 150 return pContext; |
151 } | 151 } |
152 FX_BOOL CPDF_StandardCryptoHandler::CryptStream(void* context, | 152 FX_BOOL CPDF_StandardCryptoHandler::CryptStream(void* context, |
153 const uint8_t* src_buf, | 153 const uint8_t* src_buf, |
154 FX_DWORD src_size, | 154 uint32_t src_size, |
155 CFX_BinaryBuf& dest_buf, | 155 CFX_BinaryBuf& dest_buf, |
156 FX_BOOL bEncrypt) { | 156 FX_BOOL bEncrypt) { |
157 if (!context) { | 157 if (!context) { |
158 return FALSE; | 158 return FALSE; |
159 } | 159 } |
160 if (m_Cipher == FXCIPHER_NONE) { | 160 if (m_Cipher == FXCIPHER_NONE) { |
161 dest_buf.AppendBlock(src_buf, src_size); | 161 dest_buf.AppendBlock(src_buf, src_size); |
162 return TRUE; | 162 return TRUE; |
163 } | 163 } |
164 if (m_Cipher == FXCIPHER_RC4) { | 164 if (m_Cipher == FXCIPHER_RC4) { |
165 int old_size = dest_buf.GetSize(); | 165 int old_size = dest_buf.GetSize(); |
166 dest_buf.AppendBlock(src_buf, src_size); | 166 dest_buf.AppendBlock(src_buf, src_size); |
167 CRYPT_ArcFourCrypt(context, dest_buf.GetBuffer() + old_size, src_size); | 167 CRYPT_ArcFourCrypt(context, dest_buf.GetBuffer() + old_size, src_size); |
168 return TRUE; | 168 return TRUE; |
169 } | 169 } |
170 AESCryptContext* pContext = (AESCryptContext*)context; | 170 AESCryptContext* pContext = (AESCryptContext*)context; |
171 if (pContext->m_bIV && bEncrypt) { | 171 if (pContext->m_bIV && bEncrypt) { |
172 dest_buf.AppendBlock(pContext->m_Block, 16); | 172 dest_buf.AppendBlock(pContext->m_Block, 16); |
173 pContext->m_bIV = FALSE; | 173 pContext->m_bIV = FALSE; |
174 } | 174 } |
175 FX_DWORD src_off = 0; | 175 uint32_t src_off = 0; |
176 FX_DWORD src_left = src_size; | 176 uint32_t src_left = src_size; |
177 while (1) { | 177 while (1) { |
178 FX_DWORD copy_size = 16 - pContext->m_BlockOffset; | 178 uint32_t copy_size = 16 - pContext->m_BlockOffset; |
179 if (copy_size > src_left) { | 179 if (copy_size > src_left) { |
180 copy_size = src_left; | 180 copy_size = src_left; |
181 } | 181 } |
182 FXSYS_memcpy(pContext->m_Block + pContext->m_BlockOffset, src_buf + src_off, | 182 FXSYS_memcpy(pContext->m_Block + pContext->m_BlockOffset, src_buf + src_off, |
183 copy_size); | 183 copy_size); |
184 src_off += copy_size; | 184 src_off += copy_size; |
185 src_left -= copy_size; | 185 src_left -= copy_size; |
186 pContext->m_BlockOffset += copy_size; | 186 pContext->m_BlockOffset += copy_size; |
187 if (pContext->m_BlockOffset == 16) { | 187 if (pContext->m_BlockOffset == 16) { |
188 if (!bEncrypt && pContext->m_bIV) { | 188 if (!bEncrypt && pContext->m_bIV) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 } else if (pContext->m_BlockOffset == 16) { | 237 } else if (pContext->m_BlockOffset == 16) { |
238 uint8_t block_buf[16]; | 238 uint8_t block_buf[16]; |
239 CRYPT_AESDecrypt(pContext->m_Context, block_buf, pContext->m_Block, 16); | 239 CRYPT_AESDecrypt(pContext->m_Context, block_buf, pContext->m_Block, 16); |
240 if (block_buf[15] <= 16) { | 240 if (block_buf[15] <= 16) { |
241 dest_buf.AppendBlock(block_buf, 16 - block_buf[15]); | 241 dest_buf.AppendBlock(block_buf, 16 - block_buf[15]); |
242 } | 242 } |
243 } | 243 } |
244 FX_Free(pContext); | 244 FX_Free(pContext); |
245 return TRUE; | 245 return TRUE; |
246 } | 246 } |
247 void* CPDF_StandardCryptoHandler::DecryptStart(FX_DWORD objnum, | 247 void* CPDF_StandardCryptoHandler::DecryptStart(uint32_t objnum, |
248 FX_DWORD gennum) { | 248 uint32_t gennum) { |
249 return CryptStart(objnum, gennum, FALSE); | 249 return CryptStart(objnum, gennum, FALSE); |
250 } | 250 } |
251 FX_DWORD CPDF_StandardCryptoHandler::DecryptGetSize(FX_DWORD src_size) { | 251 uint32_t CPDF_StandardCryptoHandler::DecryptGetSize(uint32_t src_size) { |
252 return m_Cipher == FXCIPHER_AES ? src_size - 16 : src_size; | 252 return m_Cipher == FXCIPHER_AES ? src_size - 16 : src_size; |
253 } | 253 } |
254 | 254 |
255 FX_BOOL CPDF_StandardCryptoHandler::Init( | 255 FX_BOOL CPDF_StandardCryptoHandler::Init( |
256 CPDF_Dictionary* pEncryptDict, | 256 CPDF_Dictionary* pEncryptDict, |
257 IPDF_SecurityHandler* pSecurityHandler) { | 257 IPDF_SecurityHandler* pSecurityHandler) { |
258 const uint8_t* key; | 258 const uint8_t* key; |
259 if (!pSecurityHandler->GetCryptInfo(m_Cipher, key, m_KeyLen)) { | 259 if (!pSecurityHandler->GetCryptInfo(m_Cipher, key, m_KeyLen)) { |
260 return FALSE; | 260 return FALSE; |
261 } | 261 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 m_Cipher = cipher; | 299 m_Cipher = cipher; |
300 m_KeyLen = keylen; | 300 m_KeyLen = keylen; |
301 FXSYS_memcpy(m_EncryptKey, key, keylen); | 301 FXSYS_memcpy(m_EncryptKey, key, keylen); |
302 if (m_Cipher == FXCIPHER_AES) { | 302 if (m_Cipher == FXCIPHER_AES) { |
303 m_pAESContext = FX_Alloc(uint8_t, 2048); | 303 m_pAESContext = FX_Alloc(uint8_t, 2048); |
304 } | 304 } |
305 return TRUE; | 305 return TRUE; |
306 } | 306 } |
307 FX_BOOL CPDF_StandardCryptoHandler::DecryptStream(void* context, | 307 FX_BOOL CPDF_StandardCryptoHandler::DecryptStream(void* context, |
308 const uint8_t* src_buf, | 308 const uint8_t* src_buf, |
309 FX_DWORD src_size, | 309 uint32_t src_size, |
310 CFX_BinaryBuf& dest_buf) { | 310 CFX_BinaryBuf& dest_buf) { |
311 return CryptStream(context, src_buf, src_size, dest_buf, FALSE); | 311 return CryptStream(context, src_buf, src_size, dest_buf, FALSE); |
312 } | 312 } |
313 FX_BOOL CPDF_StandardCryptoHandler::DecryptFinish(void* context, | 313 FX_BOOL CPDF_StandardCryptoHandler::DecryptFinish(void* context, |
314 CFX_BinaryBuf& dest_buf) { | 314 CFX_BinaryBuf& dest_buf) { |
315 return CryptFinish(context, dest_buf, FALSE); | 315 return CryptFinish(context, dest_buf, FALSE); |
316 } | 316 } |
317 FX_DWORD CPDF_StandardCryptoHandler::EncryptGetSize(FX_DWORD objnum, | 317 uint32_t CPDF_StandardCryptoHandler::EncryptGetSize(uint32_t objnum, |
318 FX_DWORD version, | 318 uint32_t version, |
319 const uint8_t* src_buf, | 319 const uint8_t* src_buf, |
320 FX_DWORD src_size) { | 320 uint32_t src_size) { |
321 if (m_Cipher == FXCIPHER_AES) { | 321 if (m_Cipher == FXCIPHER_AES) { |
322 return src_size + 32; | 322 return src_size + 32; |
323 } | 323 } |
324 return src_size; | 324 return src_size; |
325 } | 325 } |
326 FX_BOOL CPDF_StandardCryptoHandler::EncryptContent(FX_DWORD objnum, | 326 FX_BOOL CPDF_StandardCryptoHandler::EncryptContent(uint32_t objnum, |
327 FX_DWORD gennum, | 327 uint32_t gennum, |
328 const uint8_t* src_buf, | 328 const uint8_t* src_buf, |
329 FX_DWORD src_size, | 329 uint32_t src_size, |
330 uint8_t* dest_buf, | 330 uint8_t* dest_buf, |
331 FX_DWORD& dest_size) { | 331 uint32_t& dest_size) { |
332 CryptBlock(TRUE, objnum, gennum, src_buf, src_size, dest_buf, dest_size); | 332 CryptBlock(TRUE, objnum, gennum, src_buf, src_size, dest_buf, dest_size); |
333 return TRUE; | 333 return TRUE; |
334 } | 334 } |
335 CPDF_StandardCryptoHandler::CPDF_StandardCryptoHandler() { | 335 CPDF_StandardCryptoHandler::CPDF_StandardCryptoHandler() { |
336 m_pAESContext = NULL; | 336 m_pAESContext = NULL; |
337 m_Cipher = FXCIPHER_NONE; | 337 m_Cipher = FXCIPHER_NONE; |
338 m_KeyLen = 0; | 338 m_KeyLen = 0; |
339 } | 339 } |
340 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() { | 340 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() { |
341 FX_Free(m_pAESContext); | 341 FX_Free(m_pAESContext); |
342 } | 342 } |
OLD | NEW |