| 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_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/cpdf_security_handler.h" |
| 12 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h" | 13 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h" |
| 13 #include "core/fpdfapi/fpdf_parser/include/cpdf_simple_parser.h" | 14 #include "core/fpdfapi/fpdf_parser/include/cpdf_simple_parser.h" |
| 14 #include "core/fpdfapi/fpdf_parser/ipdf_security_handler.h" | |
| 15 | 15 |
| 16 IPDF_CryptoHandler::~IPDF_CryptoHandler() {} | 16 void CPDF_CryptoHandler::CryptBlock(FX_BOOL bEncrypt, |
| 17 | 17 uint32_t objnum, |
| 18 void IPDF_CryptoHandler::Decrypt(uint32_t objnum, | 18 uint32_t gennum, |
| 19 uint32_t gennum, | 19 const uint8_t* src_buf, |
| 20 CFX_ByteString& str) { | 20 uint32_t src_size, |
| 21 CFX_BinaryBuf dest_buf; | 21 uint8_t* dest_buf, |
| 22 void* context = DecryptStart(objnum, gennum); | 22 uint32_t& dest_size) { |
| 23 DecryptStream(context, str.raw_str(), str.GetLength(), dest_buf); | |
| 24 DecryptFinish(context, dest_buf); | |
| 25 str = CFX_ByteString(dest_buf.GetBuffer(), dest_buf.GetSize()); | |
| 26 } | |
| 27 | |
| 28 void CPDF_StandardCryptoHandler::CryptBlock(FX_BOOL bEncrypt, | |
| 29 uint32_t objnum, | |
| 30 uint32_t gennum, | |
| 31 const uint8_t* src_buf, | |
| 32 uint32_t src_size, | |
| 33 uint8_t* dest_buf, | |
| 34 uint32_t& dest_size) { | |
| 35 if (m_Cipher == FXCIPHER_NONE) { | 23 if (m_Cipher == FXCIPHER_NONE) { |
| 36 FXSYS_memcpy(dest_buf, src_buf, src_size); | 24 FXSYS_memcpy(dest_buf, src_buf, src_size); |
| 37 return; | 25 return; |
| 38 } | 26 } |
| 39 uint8_t realkey[16]; | 27 uint8_t realkey[16]; |
| 40 int realkeylen = 16; | 28 int realkeylen = 16; |
| 41 if (m_Cipher != FXCIPHER_AES || m_KeyLen != 32) { | 29 if (m_Cipher != FXCIPHER_AES || m_KeyLen != 32) { |
| 42 uint8_t key1[32]; | 30 uint8_t key1[32]; |
| 43 FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen); | 31 FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen); |
| 44 key1[m_KeyLen + 0] = (uint8_t)objnum; | 32 key1[m_KeyLen + 0] = (uint8_t)objnum; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 } | 80 } |
| 93 } | 81 } |
| 94 | 82 |
| 95 struct AESCryptContext { | 83 struct AESCryptContext { |
| 96 uint8_t m_Context[2048]; | 84 uint8_t m_Context[2048]; |
| 97 FX_BOOL m_bIV; | 85 FX_BOOL m_bIV; |
| 98 uint8_t m_Block[16]; | 86 uint8_t m_Block[16]; |
| 99 uint32_t m_BlockOffset; | 87 uint32_t m_BlockOffset; |
| 100 }; | 88 }; |
| 101 | 89 |
| 102 void* CPDF_StandardCryptoHandler::CryptStart(uint32_t objnum, | 90 void* CPDF_CryptoHandler::CryptStart(uint32_t objnum, |
| 103 uint32_t gennum, | 91 uint32_t gennum, |
| 104 FX_BOOL bEncrypt) { | 92 FX_BOOL bEncrypt) { |
| 105 if (m_Cipher == FXCIPHER_NONE) { | 93 if (m_Cipher == FXCIPHER_NONE) { |
| 106 return this; | 94 return this; |
| 107 } | 95 } |
| 108 if (m_Cipher == FXCIPHER_AES && m_KeyLen == 32) { | 96 if (m_Cipher == FXCIPHER_AES && m_KeyLen == 32) { |
| 109 AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); | 97 AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); |
| 110 pContext->m_bIV = TRUE; | 98 pContext->m_bIV = TRUE; |
| 111 pContext->m_BlockOffset = 0; | 99 pContext->m_BlockOffset = 0; |
| 112 CRYPT_AESSetKey(pContext->m_Context, 16, m_EncryptKey, 32, bEncrypt); | 100 CRYPT_AESSetKey(pContext->m_Context, 16, m_EncryptKey, 32, bEncrypt); |
| 113 if (bEncrypt) { | 101 if (bEncrypt) { |
| 114 for (int i = 0; i < 16; i++) { | 102 for (int i = 0; i < 16; i++) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 142 pContext->m_Block[i] = (uint8_t)rand(); | 130 pContext->m_Block[i] = (uint8_t)rand(); |
| 143 } | 131 } |
| 144 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); | 132 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); |
| 145 } | 133 } |
| 146 return pContext; | 134 return pContext; |
| 147 } | 135 } |
| 148 void* pContext = FX_Alloc(uint8_t, 1040); | 136 void* pContext = FX_Alloc(uint8_t, 1040); |
| 149 CRYPT_ArcFourSetup(pContext, realkey, realkeylen); | 137 CRYPT_ArcFourSetup(pContext, realkey, realkeylen); |
| 150 return pContext; | 138 return pContext; |
| 151 } | 139 } |
| 152 FX_BOOL CPDF_StandardCryptoHandler::CryptStream(void* context, | 140 FX_BOOL CPDF_CryptoHandler::CryptStream(void* context, |
| 153 const uint8_t* src_buf, | 141 const uint8_t* src_buf, |
| 154 uint32_t src_size, | 142 uint32_t src_size, |
| 155 CFX_BinaryBuf& dest_buf, | 143 CFX_BinaryBuf& dest_buf, |
| 156 FX_BOOL bEncrypt) { | 144 FX_BOOL bEncrypt) { |
| 157 if (!context) { | 145 if (!context) { |
| 158 return FALSE; | 146 return FALSE; |
| 159 } | 147 } |
| 160 if (m_Cipher == FXCIPHER_NONE) { | 148 if (m_Cipher == FXCIPHER_NONE) { |
| 161 dest_buf.AppendBlock(src_buf, src_size); | 149 dest_buf.AppendBlock(src_buf, src_size); |
| 162 return TRUE; | 150 return TRUE; |
| 163 } | 151 } |
| 164 if (m_Cipher == FXCIPHER_RC4) { | 152 if (m_Cipher == FXCIPHER_RC4) { |
| 165 int old_size = dest_buf.GetSize(); | 153 int old_size = dest_buf.GetSize(); |
| 166 dest_buf.AppendBlock(src_buf, src_size); | 154 dest_buf.AppendBlock(src_buf, src_size); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 dest_buf.AppendBlock(block_buf, 16); | 189 dest_buf.AppendBlock(block_buf, 16); |
| 202 pContext->m_BlockOffset = 0; | 190 pContext->m_BlockOffset = 0; |
| 203 } | 191 } |
| 204 } | 192 } |
| 205 if (!src_left) { | 193 if (!src_left) { |
| 206 break; | 194 break; |
| 207 } | 195 } |
| 208 } | 196 } |
| 209 return TRUE; | 197 return TRUE; |
| 210 } | 198 } |
| 211 FX_BOOL CPDF_StandardCryptoHandler::CryptFinish(void* context, | 199 FX_BOOL CPDF_CryptoHandler::CryptFinish(void* context, |
| 212 CFX_BinaryBuf& dest_buf, | 200 CFX_BinaryBuf& dest_buf, |
| 213 FX_BOOL bEncrypt) { | 201 FX_BOOL bEncrypt) { |
| 214 if (!context) { | 202 if (!context) { |
| 215 return FALSE; | 203 return FALSE; |
| 216 } | 204 } |
| 217 if (m_Cipher == FXCIPHER_NONE) { | 205 if (m_Cipher == FXCIPHER_NONE) { |
| 218 return TRUE; | 206 return TRUE; |
| 219 } | 207 } |
| 220 if (m_Cipher == FXCIPHER_RC4) { | 208 if (m_Cipher == FXCIPHER_RC4) { |
| 221 FX_Free(context); | 209 FX_Free(context); |
| 222 return TRUE; | 210 return TRUE; |
| 223 } | 211 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 237 } else if (pContext->m_BlockOffset == 16) { | 225 } else if (pContext->m_BlockOffset == 16) { |
| 238 uint8_t block_buf[16]; | 226 uint8_t block_buf[16]; |
| 239 CRYPT_AESDecrypt(pContext->m_Context, block_buf, pContext->m_Block, 16); | 227 CRYPT_AESDecrypt(pContext->m_Context, block_buf, pContext->m_Block, 16); |
| 240 if (block_buf[15] <= 16) { | 228 if (block_buf[15] <= 16) { |
| 241 dest_buf.AppendBlock(block_buf, 16 - block_buf[15]); | 229 dest_buf.AppendBlock(block_buf, 16 - block_buf[15]); |
| 242 } | 230 } |
| 243 } | 231 } |
| 244 FX_Free(pContext); | 232 FX_Free(pContext); |
| 245 return TRUE; | 233 return TRUE; |
| 246 } | 234 } |
| 247 void* CPDF_StandardCryptoHandler::DecryptStart(uint32_t objnum, | 235 |
| 248 uint32_t gennum) { | 236 void CPDF_CryptoHandler::Decrypt(uint32_t objnum, |
| 237 uint32_t gennum, |
| 238 CFX_ByteString& str) { |
| 239 CFX_BinaryBuf dest_buf; |
| 240 void* context = DecryptStart(objnum, gennum); |
| 241 DecryptStream(context, str.raw_str(), str.GetLength(), dest_buf); |
| 242 DecryptFinish(context, dest_buf); |
| 243 str = CFX_ByteString(dest_buf.GetBuffer(), dest_buf.GetSize()); |
| 244 } |
| 245 |
| 246 void* CPDF_CryptoHandler::DecryptStart(uint32_t objnum, uint32_t gennum) { |
| 249 return CryptStart(objnum, gennum, FALSE); | 247 return CryptStart(objnum, gennum, FALSE); |
| 250 } | 248 } |
| 251 uint32_t CPDF_StandardCryptoHandler::DecryptGetSize(uint32_t src_size) { | 249 uint32_t CPDF_CryptoHandler::DecryptGetSize(uint32_t src_size) { |
| 252 return m_Cipher == FXCIPHER_AES ? src_size - 16 : src_size; | 250 return m_Cipher == FXCIPHER_AES ? src_size - 16 : src_size; |
| 253 } | 251 } |
| 254 | 252 |
| 255 FX_BOOL CPDF_StandardCryptoHandler::Init( | 253 FX_BOOL CPDF_CryptoHandler::Init(CPDF_Dictionary* pEncryptDict, |
| 256 CPDF_Dictionary* pEncryptDict, | 254 CPDF_SecurityHandler* pSecurityHandler) { |
| 257 IPDF_SecurityHandler* pSecurityHandler) { | |
| 258 const uint8_t* key; | 255 const uint8_t* key; |
| 259 if (!pSecurityHandler->GetCryptInfo(m_Cipher, key, m_KeyLen)) { | 256 if (!pSecurityHandler->GetCryptInfo(m_Cipher, key, m_KeyLen)) { |
| 260 return FALSE; | 257 return FALSE; |
| 261 } | 258 } |
| 262 if (m_KeyLen > 32 || m_KeyLen < 0) { | 259 if (m_KeyLen > 32 || m_KeyLen < 0) { |
| 263 return FALSE; | 260 return FALSE; |
| 264 } | 261 } |
| 265 if (m_Cipher != FXCIPHER_NONE) { | 262 if (m_Cipher != FXCIPHER_NONE) { |
| 266 FXSYS_memcpy(m_EncryptKey, key, m_KeyLen); | 263 FXSYS_memcpy(m_EncryptKey, key, m_KeyLen); |
| 267 } | 264 } |
| 268 if (m_Cipher == FXCIPHER_AES) { | 265 if (m_Cipher == FXCIPHER_AES) { |
| 269 m_pAESContext = FX_Alloc(uint8_t, 2048); | 266 m_pAESContext = FX_Alloc(uint8_t, 2048); |
| 270 } | 267 } |
| 271 return TRUE; | 268 return TRUE; |
| 272 } | 269 } |
| 273 | 270 |
| 274 FX_BOOL CPDF_StandardCryptoHandler::Init(int cipher, | 271 FX_BOOL CPDF_CryptoHandler::Init(int cipher, const uint8_t* key, int keylen) { |
| 275 const uint8_t* key, | |
| 276 int keylen) { | |
| 277 if (cipher == FXCIPHER_AES) { | 272 if (cipher == FXCIPHER_AES) { |
| 278 switch (keylen) { | 273 switch (keylen) { |
| 279 case 16: | 274 case 16: |
| 280 case 24: | 275 case 24: |
| 281 case 32: | 276 case 32: |
| 282 break; | 277 break; |
| 283 default: | 278 default: |
| 284 return FALSE; | 279 return FALSE; |
| 285 } | 280 } |
| 286 } else if (cipher == FXCIPHER_AES2) { | 281 } else if (cipher == FXCIPHER_AES2) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 297 } | 292 } |
| 298 } | 293 } |
| 299 m_Cipher = cipher; | 294 m_Cipher = cipher; |
| 300 m_KeyLen = keylen; | 295 m_KeyLen = keylen; |
| 301 FXSYS_memcpy(m_EncryptKey, key, keylen); | 296 FXSYS_memcpy(m_EncryptKey, key, keylen); |
| 302 if (m_Cipher == FXCIPHER_AES) { | 297 if (m_Cipher == FXCIPHER_AES) { |
| 303 m_pAESContext = FX_Alloc(uint8_t, 2048); | 298 m_pAESContext = FX_Alloc(uint8_t, 2048); |
| 304 } | 299 } |
| 305 return TRUE; | 300 return TRUE; |
| 306 } | 301 } |
| 307 FX_BOOL CPDF_StandardCryptoHandler::DecryptStream(void* context, | 302 FX_BOOL CPDF_CryptoHandler::DecryptStream(void* context, |
| 308 const uint8_t* src_buf, | 303 const uint8_t* src_buf, |
| 309 uint32_t src_size, | 304 uint32_t src_size, |
| 310 CFX_BinaryBuf& dest_buf) { | 305 CFX_BinaryBuf& dest_buf) { |
| 311 return CryptStream(context, src_buf, src_size, dest_buf, FALSE); | 306 return CryptStream(context, src_buf, src_size, dest_buf, FALSE); |
| 312 } | 307 } |
| 313 FX_BOOL CPDF_StandardCryptoHandler::DecryptFinish(void* context, | 308 FX_BOOL CPDF_CryptoHandler::DecryptFinish(void* context, |
| 314 CFX_BinaryBuf& dest_buf) { | 309 CFX_BinaryBuf& dest_buf) { |
| 315 return CryptFinish(context, dest_buf, FALSE); | 310 return CryptFinish(context, dest_buf, FALSE); |
| 316 } | 311 } |
| 317 uint32_t CPDF_StandardCryptoHandler::EncryptGetSize(uint32_t objnum, | 312 uint32_t CPDF_CryptoHandler::EncryptGetSize(uint32_t objnum, |
| 318 uint32_t version, | 313 uint32_t version, |
| 319 const uint8_t* src_buf, | 314 const uint8_t* src_buf, |
| 320 uint32_t src_size) { | 315 uint32_t src_size) { |
| 321 if (m_Cipher == FXCIPHER_AES) { | 316 if (m_Cipher == FXCIPHER_AES) { |
| 322 return src_size + 32; | 317 return src_size + 32; |
| 323 } | 318 } |
| 324 return src_size; | 319 return src_size; |
| 325 } | 320 } |
| 326 FX_BOOL CPDF_StandardCryptoHandler::EncryptContent(uint32_t objnum, | 321 FX_BOOL CPDF_CryptoHandler::EncryptContent(uint32_t objnum, |
| 327 uint32_t gennum, | 322 uint32_t gennum, |
| 328 const uint8_t* src_buf, | 323 const uint8_t* src_buf, |
| 329 uint32_t src_size, | 324 uint32_t src_size, |
| 330 uint8_t* dest_buf, | 325 uint8_t* dest_buf, |
| 331 uint32_t& dest_size) { | 326 uint32_t& dest_size) { |
| 332 CryptBlock(TRUE, objnum, gennum, src_buf, src_size, dest_buf, dest_size); | 327 CryptBlock(TRUE, objnum, gennum, src_buf, src_size, dest_buf, dest_size); |
| 333 return TRUE; | 328 return TRUE; |
| 334 } | 329 } |
| 335 CPDF_StandardCryptoHandler::CPDF_StandardCryptoHandler() { | 330 CPDF_CryptoHandler::CPDF_CryptoHandler() { |
| 336 m_pAESContext = NULL; | 331 m_pAESContext = NULL; |
| 337 m_Cipher = FXCIPHER_NONE; | 332 m_Cipher = FXCIPHER_NONE; |
| 338 m_KeyLen = 0; | 333 m_KeyLen = 0; |
| 339 } | 334 } |
| 340 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() { | 335 CPDF_CryptoHandler::~CPDF_CryptoHandler() { |
| 341 FX_Free(m_pAESContext); | 336 FX_Free(m_pAESContext); |
| 342 } | 337 } |
| OLD | NEW |