| 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 <time.h> | 7 #include <time.h> |
| 8 #include "../../../include/fpdfapi/fpdf_parser.h" | 8 #include "../../../include/fpdfapi/fpdf_parser.h" |
| 9 #include "../../../include/fdrm/fx_crypt.h" | 9 #include "../../../include/fdrm/fx_crypt.h" |
| 10 const uint8_t defpasscode[32] = { | 10 const uint8_t defpasscode[32] = { |
| 11 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, | 11 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, |
| 12 0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08, | 12 0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08, |
| 13 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80, | 13 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80, |
| 14 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a | 14 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a |
| 15 }; | 15 }; |
| 16 void CalcEncryptKey(CPDF_Dictionary* pEncrypt, const uint8_t* password, FX_DWORD
pass_size, | 16 void CalcEncryptKey(CPDF_Dictionary* pEncrypt, const uint8_t* password, FX_DWORD
pass_size, |
| 17 uint8_t* key, int keylen, FX_BOOL bIgnoreMeta, CPDF_Array* p
IdArray) | 17 uint8_t* key, int keylen, bool bIgnoreMeta, CPDF_Array* pIdA
rray) |
| 18 { | 18 { |
| 19 int revision = pEncrypt->GetInteger(FX_BSTRC("R")); | 19 int revision = pEncrypt->GetInteger(FX_BSTRC("R")); |
| 20 uint8_t passcode[32]; | 20 uint8_t passcode[32]; |
| 21 for (FX_DWORD i = 0; i < 32; i ++) { | 21 for (FX_DWORD i = 0; i < 32; i ++) { |
| 22 passcode[i] = i < pass_size ? password[i] : defpasscode[i - pass_size]; | 22 passcode[i] = i < pass_size ? password[i] : defpasscode[i - pass_size]; |
| 23 } | 23 } |
| 24 uint8_t md5[100]; | 24 uint8_t md5[100]; |
| 25 CRYPT_MD5Start(md5); | 25 CRYPT_MD5Start(md5); |
| 26 CRYPT_MD5Update(md5, passcode, 32); | 26 CRYPT_MD5Update(md5, passcode, 32); |
| 27 CFX_ByteString okey = pEncrypt->GetString(FX_BSTRC("O")); | 27 CFX_ByteString okey = pEncrypt->GetString(FX_BSTRC("O")); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 50 FXSYS_memset(key, 0, keylen); | 50 FXSYS_memset(key, 0, keylen); |
| 51 FXSYS_memcpy(key, digest, copy_len); | 51 FXSYS_memcpy(key, digest, copy_len); |
| 52 } | 52 } |
| 53 CPDF_CryptoHandler* CPDF_StandardSecurityHandler::CreateCryptoHandler() | 53 CPDF_CryptoHandler* CPDF_StandardSecurityHandler::CreateCryptoHandler() |
| 54 { | 54 { |
| 55 return new CPDF_StandardCryptoHandler; | 55 return new CPDF_StandardCryptoHandler; |
| 56 } | 56 } |
| 57 typedef struct _PDF_CRYPTOITEM { | 57 typedef struct _PDF_CRYPTOITEM { |
| 58 int32_t m_Cipher; | 58 int32_t m_Cipher; |
| 59 int32_t m_KeyLen; | 59 int32_t m_KeyLen; |
| 60 FX_BOOL» » m_bChecked; | 60 bool» » m_bChecked; |
| 61 CPDF_StandardCryptoHandler* m_pCryptoHandler; | 61 CPDF_StandardCryptoHandler* m_pCryptoHandler; |
| 62 } PDF_CRYPTOITEM; | 62 } PDF_CRYPTOITEM; |
| 63 CPDF_StandardSecurityHandler::CPDF_StandardSecurityHandler() | 63 CPDF_StandardSecurityHandler::CPDF_StandardSecurityHandler() |
| 64 { | 64 { |
| 65 m_Version = 0; | 65 m_Version = 0; |
| 66 m_Revision = 0; | 66 m_Revision = 0; |
| 67 m_pParser = NULL; | 67 m_pParser = NULL; |
| 68 m_pEncryptDict = NULL; | 68 m_pEncryptDict = NULL; |
| 69 m_bOwner = FALSE; | 69 m_bOwner = false; |
| 70 m_Permissions = 0; | 70 m_Permissions = 0; |
| 71 m_Cipher = FXCIPHER_NONE; | 71 m_Cipher = FXCIPHER_NONE; |
| 72 m_KeyLen = 0; | 72 m_KeyLen = 0; |
| 73 } | 73 } |
| 74 CPDF_StandardSecurityHandler::~CPDF_StandardSecurityHandler() | 74 CPDF_StandardSecurityHandler::~CPDF_StandardSecurityHandler() |
| 75 { | 75 { |
| 76 } | 76 } |
| 77 FX_BOOL CPDF_StandardSecurityHandler::OnInit(CPDF_Parser* pParser, CPDF_Dictiona
ry* pEncryptDict) | 77 bool CPDF_StandardSecurityHandler::OnInit(CPDF_Parser* pParser, CPDF_Dictionary*
pEncryptDict) |
| 78 { | 78 { |
| 79 m_pParser = pParser; | 79 m_pParser = pParser; |
| 80 if (!LoadDict(pEncryptDict)) { | 80 if (!LoadDict(pEncryptDict)) { |
| 81 return FALSE; | 81 return false; |
| 82 } | 82 } |
| 83 if (m_Cipher == FXCIPHER_NONE) { | 83 if (m_Cipher == FXCIPHER_NONE) { |
| 84 return TRUE; | 84 return true; |
| 85 } | 85 } |
| 86 return CheckSecurity(m_KeyLen); | 86 return CheckSecurity(m_KeyLen); |
| 87 } | 87 } |
| 88 FX_BOOL CPDF_StandardSecurityHandler::CheckSecurity(int32_t key_len) | 88 bool CPDF_StandardSecurityHandler::CheckSecurity(int32_t key_len) |
| 89 { | 89 { |
| 90 CFX_ByteString password = m_pParser->GetPassword(); | 90 CFX_ByteString password = m_pParser->GetPassword(); |
| 91 if (CheckPassword(password, password.GetLength(), TRUE, m_EncryptKey, key_le
n)) { | 91 if (CheckPassword(password, password.GetLength(), true, m_EncryptKey, key_le
n)) { |
| 92 if (password.IsEmpty()) { | 92 if (password.IsEmpty()) { |
| 93 if (!CheckPassword(password, password.GetLength(), FALSE, m_EncryptK
ey, key_len)) { | 93 if (!CheckPassword(password, password.GetLength(), false, m_EncryptK
ey, key_len)) { |
| 94 return FALSE; | 94 return false; |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 m_bOwner = TRUE; | 97 m_bOwner = true; |
| 98 return TRUE; | 98 return true; |
| 99 } | 99 } |
| 100 return CheckPassword(password, password.GetLength(), FALSE, m_EncryptKey, ke
y_len); | 100 return CheckPassword(password, password.GetLength(), false, m_EncryptKey, ke
y_len); |
| 101 } | 101 } |
| 102 FX_DWORD CPDF_StandardSecurityHandler::GetPermissions() | 102 FX_DWORD CPDF_StandardSecurityHandler::GetPermissions() |
| 103 { | 103 { |
| 104 return m_Permissions; | 104 return m_Permissions; |
| 105 } | 105 } |
| 106 static FX_BOOL _LoadCryptInfo(CPDF_Dictionary* pEncryptDict, const CFX_ByteStrin
gC& name, int& cipher, int& keylen) | 106 static bool _LoadCryptInfo(CPDF_Dictionary* pEncryptDict, const CFX_ByteStringC&
name, int& cipher, int& keylen) |
| 107 { | 107 { |
| 108 int Version = pEncryptDict->GetInteger(FX_BSTRC("V")); | 108 int Version = pEncryptDict->GetInteger(FX_BSTRC("V")); |
| 109 cipher = FXCIPHER_RC4; | 109 cipher = FXCIPHER_RC4; |
| 110 keylen = 0; | 110 keylen = 0; |
| 111 if (Version >= 4) { | 111 if (Version >= 4) { |
| 112 CPDF_Dictionary* pCryptFilters = pEncryptDict->GetDict(FX_BSTRC("CF")); | 112 CPDF_Dictionary* pCryptFilters = pEncryptDict->GetDict(FX_BSTRC("CF")); |
| 113 if (pCryptFilters == NULL) { | 113 if (pCryptFilters == NULL) { |
| 114 return FALSE; | 114 return false; |
| 115 } | 115 } |
| 116 if (name == FX_BSTRC("Identity")) { | 116 if (name == FX_BSTRC("Identity")) { |
| 117 cipher = FXCIPHER_NONE; | 117 cipher = FXCIPHER_NONE; |
| 118 } else { | 118 } else { |
| 119 CPDF_Dictionary* pDefFilter = pCryptFilters->GetDict(name); | 119 CPDF_Dictionary* pDefFilter = pCryptFilters->GetDict(name); |
| 120 if (pDefFilter == NULL) { | 120 if (pDefFilter == NULL) { |
| 121 return FALSE; | 121 return false; |
| 122 } | 122 } |
| 123 int nKeyBits = 0; | 123 int nKeyBits = 0; |
| 124 if (Version == 4) { | 124 if (Version == 4) { |
| 125 nKeyBits = pDefFilter->GetInteger(FX_BSTRC("Length"), 0); | 125 nKeyBits = pDefFilter->GetInteger(FX_BSTRC("Length"), 0); |
| 126 if (nKeyBits == 0) { | 126 if (nKeyBits == 0) { |
| 127 nKeyBits = pEncryptDict->GetInteger(FX_BSTRC("Length"), 128)
; | 127 nKeyBits = pEncryptDict->GetInteger(FX_BSTRC("Length"), 128)
; |
| 128 } | 128 } |
| 129 } else { | 129 } else { |
| 130 nKeyBits = pEncryptDict->GetInteger(FX_BSTRC("Length"), 256); | 130 nKeyBits = pEncryptDict->GetInteger(FX_BSTRC("Length"), 256); |
| 131 } | 131 } |
| 132 if (nKeyBits < 40) { | 132 if (nKeyBits < 40) { |
| 133 nKeyBits *= 8; | 133 nKeyBits *= 8; |
| 134 } | 134 } |
| 135 keylen = nKeyBits / 8; | 135 keylen = nKeyBits / 8; |
| 136 CFX_ByteString cipher_name = pDefFilter->GetString(FX_BSTRC("CFM")); | 136 CFX_ByteString cipher_name = pDefFilter->GetString(FX_BSTRC("CFM")); |
| 137 if (cipher_name == FX_BSTRC("AESV2") || cipher_name == FX_BSTRC("AES
V3")) { | 137 if (cipher_name == FX_BSTRC("AESV2") || cipher_name == FX_BSTRC("AES
V3")) { |
| 138 cipher = FXCIPHER_AES; | 138 cipher = FXCIPHER_AES; |
| 139 } | 139 } |
| 140 } | 140 } |
| 141 } else { | 141 } else { |
| 142 keylen = Version > 1 ? pEncryptDict->GetInteger(FX_BSTRC("Length"), 40)
/ 8 : 5; | 142 keylen = Version > 1 ? pEncryptDict->GetInteger(FX_BSTRC("Length"), 40)
/ 8 : 5; |
| 143 } | 143 } |
| 144 if (keylen > 32 || keylen < 0) { | 144 if (keylen > 32 || keylen < 0) { |
| 145 return FALSE; | 145 return false; |
| 146 } | 146 } |
| 147 return TRUE; | 147 return true; |
| 148 } | 148 } |
| 149 FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict) | 149 bool CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict) |
| 150 { | 150 { |
| 151 m_pEncryptDict = pEncryptDict; | 151 m_pEncryptDict = pEncryptDict; |
| 152 m_bOwner = FALSE; | 152 m_bOwner = false; |
| 153 m_Version = pEncryptDict->GetInteger(FX_BSTRC("V")); | 153 m_Version = pEncryptDict->GetInteger(FX_BSTRC("V")); |
| 154 m_Revision = pEncryptDict->GetInteger(FX_BSTRC("R")); | 154 m_Revision = pEncryptDict->GetInteger(FX_BSTRC("R")); |
| 155 m_Permissions = pEncryptDict->GetInteger(FX_BSTRC("P"), -1); | 155 m_Permissions = pEncryptDict->GetInteger(FX_BSTRC("P"), -1); |
| 156 if (m_Version < 4) { | 156 if (m_Version < 4) { |
| 157 return _LoadCryptInfo(pEncryptDict, CFX_ByteString(), m_Cipher, m_KeyLen
); | 157 return _LoadCryptInfo(pEncryptDict, CFX_ByteString(), m_Cipher, m_KeyLen
); |
| 158 } | 158 } |
| 159 CFX_ByteString stmf_name = pEncryptDict->GetString(FX_BSTRC("StmF")); | 159 CFX_ByteString stmf_name = pEncryptDict->GetString(FX_BSTRC("StmF")); |
| 160 CFX_ByteString strf_name = pEncryptDict->GetString(FX_BSTRC("StrF")); | 160 CFX_ByteString strf_name = pEncryptDict->GetString(FX_BSTRC("StrF")); |
| 161 if (stmf_name != strf_name) { | 161 if (stmf_name != strf_name) { |
| 162 return FALSE; | 162 return false; |
| 163 } | 163 } |
| 164 if (!_LoadCryptInfo(pEncryptDict, strf_name, m_Cipher, m_KeyLen)) { | 164 if (!_LoadCryptInfo(pEncryptDict, strf_name, m_Cipher, m_KeyLen)) { |
| 165 return FALSE; | 165 return false; |
| 166 } | 166 } |
| 167 return TRUE; | 167 return true; |
| 168 } | 168 } |
| 169 FX_BOOL CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict, FX
_DWORD type, int& cipher, int& key_len) | 169 bool CPDF_StandardSecurityHandler::LoadDict(CPDF_Dictionary* pEncryptDict, FX_DW
ORD type, int& cipher, int& key_len) |
| 170 { | 170 { |
| 171 m_pEncryptDict = pEncryptDict; | 171 m_pEncryptDict = pEncryptDict; |
| 172 m_bOwner = FALSE; | 172 m_bOwner = false; |
| 173 m_Version = pEncryptDict->GetInteger(FX_BSTRC("V")); | 173 m_Version = pEncryptDict->GetInteger(FX_BSTRC("V")); |
| 174 m_Revision = pEncryptDict->GetInteger(FX_BSTRC("R")); | 174 m_Revision = pEncryptDict->GetInteger(FX_BSTRC("R")); |
| 175 m_Permissions = pEncryptDict->GetInteger(FX_BSTRC("P"), -1); | 175 m_Permissions = pEncryptDict->GetInteger(FX_BSTRC("P"), -1); |
| 176 CFX_ByteString strf_name, stmf_name; | 176 CFX_ByteString strf_name, stmf_name; |
| 177 if (m_Version >= 4) { | 177 if (m_Version >= 4) { |
| 178 stmf_name = pEncryptDict->GetString(FX_BSTRC("StmF")); | 178 stmf_name = pEncryptDict->GetString(FX_BSTRC("StmF")); |
| 179 strf_name = pEncryptDict->GetString(FX_BSTRC("StrF")); | 179 strf_name = pEncryptDict->GetString(FX_BSTRC("StrF")); |
| 180 if (stmf_name != strf_name) { | 180 if (stmf_name != strf_name) { |
| 181 return FALSE; | 181 return false; |
| 182 } | 182 } |
| 183 } | 183 } |
| 184 if (!_LoadCryptInfo(pEncryptDict, strf_name, cipher, key_len)) { | 184 if (!_LoadCryptInfo(pEncryptDict, strf_name, cipher, key_len)) { |
| 185 return FALSE; | 185 return false; |
| 186 } | 186 } |
| 187 m_Cipher = cipher; | 187 m_Cipher = cipher; |
| 188 m_KeyLen = key_len; | 188 m_KeyLen = key_len; |
| 189 return TRUE; | 189 return true; |
| 190 return TRUE; | 190 return true; |
| 191 } | 191 } |
| 192 FX_BOOL CPDF_StandardSecurityHandler::GetCryptInfo(int& cipher, const uint8_t*&
buffer, int& keylen) | 192 bool CPDF_StandardSecurityHandler::GetCryptInfo(int& cipher, const uint8_t*& buf
fer, int& keylen) |
| 193 { | 193 { |
| 194 cipher = m_Cipher; | 194 cipher = m_Cipher; |
| 195 buffer = m_EncryptKey; | 195 buffer = m_EncryptKey; |
| 196 keylen = m_KeyLen; | 196 keylen = m_KeyLen; |
| 197 return TRUE; | 197 return true; |
| 198 } | 198 } |
| 199 #define FX_GET_32WORD(n,b,i)
\ | 199 #define FX_GET_32WORD(n,b,i)
\ |
| 200 {
\ | 200 {
\ |
| 201 (n) = (FX_DWORD)(( (uint64_t) (b)[(i)] << 24 ) \ | 201 (n) = (FX_DWORD)(( (uint64_t) (b)[(i)] << 24 ) \ |
| 202 | ( (uint64_t) (b)[(i) + 1] << 16 )
\ | 202 | ( (uint64_t) (b)[(i) + 1] << 16 )
\ |
| 203 | ( (uint64_t) (b)[(i) + 2] << 8 )
\ | 203 | ( (uint64_t) (b)[(i) + 2] << 8 )
\ |
| 204 | ( (uint64_t) (b)[(i) + 3] ));
\ | 204 | ( (uint64_t) (b)[(i) + 3] ));
\ |
| 205 } | 205 } |
| 206 int BigOrder64BitsMod3(uint8_t* data) | 206 int BigOrder64BitsMod3(uint8_t* data) |
| 207 { | 207 { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 buf.EstimateSize(iBufLen); | 245 buf.EstimateSize(iBufLen); |
| 246 E = buf.GetBuffer(); | 246 E = buf.GetBuffer(); |
| 247 CFX_ByteTextBuf content; | 247 CFX_ByteTextBuf content; |
| 248 for (int j = 0; j < 64; ++j) { | 248 for (int j = 0; j < 64; ++j) { |
| 249 content.AppendBlock(password, size); | 249 content.AppendBlock(password, size); |
| 250 content.AppendBlock(input, iBlockSize); | 250 content.AppendBlock(input, iBlockSize); |
| 251 if (vector) { | 251 if (vector) { |
| 252 content.AppendBlock(vector, 48); | 252 content.AppendBlock(vector, 48); |
| 253 } | 253 } |
| 254 } | 254 } |
| 255 CRYPT_AESSetKey(aes, 16, key, 16, TRUE); | 255 CRYPT_AESSetKey(aes, 16, key, 16, true); |
| 256 CRYPT_AESSetIV(aes, iv); | 256 CRYPT_AESSetIV(aes, iv); |
| 257 CRYPT_AESEncrypt(aes, E, content.GetBuffer(), iBufLen); | 257 CRYPT_AESEncrypt(aes, E, content.GetBuffer(), iBufLen); |
| 258 int iHash = 0; | 258 int iHash = 0; |
| 259 switch (BigOrder64BitsMod3(E)) { | 259 switch (BigOrder64BitsMod3(E)) { |
| 260 case 0: | 260 case 0: |
| 261 iHash = 0; | 261 iHash = 0; |
| 262 iBlockSize = 32; | 262 iBlockSize = 32; |
| 263 break; | 263 break; |
| 264 case 1: | 264 case 1: |
| 265 iHash = 1; | 265 iHash = 1; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 281 } | 281 } |
| 282 key = input; | 282 key = input; |
| 283 iv = input + 16; | 283 iv = input + 16; |
| 284 ++i; | 284 ++i; |
| 285 } | 285 } |
| 286 FX_Free(aes); | 286 FX_Free(aes); |
| 287 if (hash) { | 287 if (hash) { |
| 288 FXSYS_memcpy(hash, input, 32); | 288 FXSYS_memcpy(hash, input, 32); |
| 289 } | 289 } |
| 290 } | 290 } |
| 291 FX_BOOL CPDF_StandardSecurityHandler::AES256_CheckPassword(const uint8_t* passwo
rd, FX_DWORD size, | 291 bool CPDF_StandardSecurityHandler::AES256_CheckPassword(const uint8_t* password,
FX_DWORD size, |
| 292 FX_BOOL bOwner, uint8_t* key) | 292 bool bOwner, uint8_t* key) |
| 293 { | 293 { |
| 294 CFX_ByteString okey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("O
")) : CFX_ByteString(); | 294 CFX_ByteString okey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("O
")) : CFX_ByteString(); |
| 295 if (okey.GetLength() < 48) { | 295 if (okey.GetLength() < 48) { |
| 296 return FALSE; | 296 return false; |
| 297 } | 297 } |
| 298 CFX_ByteString ukey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("U
")) : CFX_ByteString(); | 298 CFX_ByteString ukey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("U
")) : CFX_ByteString(); |
| 299 if (ukey.GetLength() < 48) { | 299 if (ukey.GetLength() < 48) { |
| 300 return FALSE; | 300 return false; |
| 301 } | 301 } |
| 302 const uint8_t* pkey = bOwner ? (const uint8_t*)okey : (const uint8_t*)ukey; | 302 const uint8_t* pkey = bOwner ? (const uint8_t*)okey : (const uint8_t*)ukey; |
| 303 uint8_t sha[128]; | 303 uint8_t sha[128]; |
| 304 uint8_t digest[32]; | 304 uint8_t digest[32]; |
| 305 if (m_Revision >= 6) { | 305 if (m_Revision >= 6) { |
| 306 Revision6_Hash(password, size, (const uint8_t*)pkey + 32, (bOwner ? (con
st uint8_t*)ukey : NULL), digest); | 306 Revision6_Hash(password, size, (const uint8_t*)pkey + 32, (bOwner ? (con
st uint8_t*)ukey : NULL), digest); |
| 307 } else { | 307 } else { |
| 308 CRYPT_SHA256Start(sha); | 308 CRYPT_SHA256Start(sha); |
| 309 CRYPT_SHA256Update(sha, password, size); | 309 CRYPT_SHA256Update(sha, password, size); |
| 310 CRYPT_SHA256Update(sha, pkey + 32, 8); | 310 CRYPT_SHA256Update(sha, pkey + 32, 8); |
| 311 if (bOwner) { | 311 if (bOwner) { |
| 312 CRYPT_SHA256Update(sha, ukey, 48); | 312 CRYPT_SHA256Update(sha, ukey, 48); |
| 313 } | 313 } |
| 314 CRYPT_SHA256Finish(sha, digest); | 314 CRYPT_SHA256Finish(sha, digest); |
| 315 } | 315 } |
| 316 if (FXSYS_memcmp(digest, pkey, 32) != 0) { | 316 if (FXSYS_memcmp(digest, pkey, 32) != 0) { |
| 317 return FALSE; | 317 return false; |
| 318 } | 318 } |
| 319 if (key == NULL) { | 319 if (key == NULL) { |
| 320 return TRUE; | 320 return true; |
| 321 } | 321 } |
| 322 if (m_Revision >= 6) { | 322 if (m_Revision >= 6) { |
| 323 Revision6_Hash(password, size, (const uint8_t*)pkey + 40, (bOwner ? (con
st uint8_t*)ukey : NULL), digest); | 323 Revision6_Hash(password, size, (const uint8_t*)pkey + 40, (bOwner ? (con
st uint8_t*)ukey : NULL), digest); |
| 324 } else { | 324 } else { |
| 325 CRYPT_SHA256Start(sha); | 325 CRYPT_SHA256Start(sha); |
| 326 CRYPT_SHA256Update(sha, password, size); | 326 CRYPT_SHA256Update(sha, password, size); |
| 327 CRYPT_SHA256Update(sha, pkey + 40, 8); | 327 CRYPT_SHA256Update(sha, pkey + 40, 8); |
| 328 if (bOwner) { | 328 if (bOwner) { |
| 329 CRYPT_SHA256Update(sha, ukey, 48); | 329 CRYPT_SHA256Update(sha, ukey, 48); |
| 330 } | 330 } |
| 331 CRYPT_SHA256Finish(sha, digest); | 331 CRYPT_SHA256Finish(sha, digest); |
| 332 } | 332 } |
| 333 CFX_ByteString ekey = m_pEncryptDict ? m_pEncryptDict->GetString(bOwner ? FX
_BSTRC("OE") : FX_BSTRC("UE")) : CFX_ByteString(); | 333 CFX_ByteString ekey = m_pEncryptDict ? m_pEncryptDict->GetString(bOwner ? FX
_BSTRC("OE") : FX_BSTRC("UE")) : CFX_ByteString(); |
| 334 if (ekey.GetLength() < 32) { | 334 if (ekey.GetLength() < 32) { |
| 335 return FALSE; | 335 return false; |
| 336 } | 336 } |
| 337 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 337 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
| 338 CRYPT_AESSetKey(aes, 16, digest, 32, FALSE); | 338 CRYPT_AESSetKey(aes, 16, digest, 32, false); |
| 339 uint8_t iv[16]; | 339 uint8_t iv[16]; |
| 340 FXSYS_memset(iv, 0, 16); | 340 FXSYS_memset(iv, 0, 16); |
| 341 CRYPT_AESSetIV(aes, iv); | 341 CRYPT_AESSetIV(aes, iv); |
| 342 CRYPT_AESDecrypt(aes, key, ekey, 32); | 342 CRYPT_AESDecrypt(aes, key, ekey, 32); |
| 343 CRYPT_AESSetKey(aes, 16, key, 32, FALSE); | 343 CRYPT_AESSetKey(aes, 16, key, 32, false); |
| 344 CRYPT_AESSetIV(aes, iv); | 344 CRYPT_AESSetIV(aes, iv); |
| 345 CFX_ByteString perms = m_pEncryptDict->GetString(FX_BSTRC("Perms")); | 345 CFX_ByteString perms = m_pEncryptDict->GetString(FX_BSTRC("Perms")); |
| 346 if (perms.IsEmpty()) { | 346 if (perms.IsEmpty()) { |
| 347 return FALSE; | 347 return false; |
| 348 } | 348 } |
| 349 uint8_t perms_buf[16]; | 349 uint8_t perms_buf[16]; |
| 350 FXSYS_memset(perms_buf, 0, sizeof(perms_buf)); | 350 FXSYS_memset(perms_buf, 0, sizeof(perms_buf)); |
| 351 FX_DWORD copy_len = sizeof(perms_buf); | 351 FX_DWORD copy_len = sizeof(perms_buf); |
| 352 if (copy_len > (FX_DWORD)perms.GetLength()) { | 352 if (copy_len > (FX_DWORD)perms.GetLength()) { |
| 353 copy_len = perms.GetLength(); | 353 copy_len = perms.GetLength(); |
| 354 } | 354 } |
| 355 FXSYS_memcpy(perms_buf, (const uint8_t*)perms, copy_len); | 355 FXSYS_memcpy(perms_buf, (const uint8_t*)perms, copy_len); |
| 356 uint8_t buf[16]; | 356 uint8_t buf[16]; |
| 357 CRYPT_AESDecrypt(aes, buf, perms_buf, 16); | 357 CRYPT_AESDecrypt(aes, buf, perms_buf, 16); |
| 358 FX_Free(aes); | 358 FX_Free(aes); |
| 359 if (buf[9] != 'a' || buf[10] != 'd' || buf[11] != 'b') { | 359 if (buf[9] != 'a' || buf[10] != 'd' || buf[11] != 'b') { |
| 360 return FALSE; | 360 return false; |
| 361 } | 361 } |
| 362 if (FXDWORD_GET_LSBFIRST(buf) != m_Permissions) { | 362 if (FXDWORD_GET_LSBFIRST(buf) != m_Permissions) { |
| 363 return FALSE; | 363 return false; |
| 364 } | 364 } |
| 365 if ((buf[8] == 'T' && !IsMetadataEncrypted()) || (buf[8] == 'F' && IsMetadat
aEncrypted())) { | 365 if ((buf[8] == 'T' && !IsMetadataEncrypted()) || (buf[8] == 'F' && IsMetadat
aEncrypted())) { |
| 366 return FALSE; | 366 return false; |
| 367 } | 367 } |
| 368 return TRUE; | 368 return true; |
| 369 } | 369 } |
| 370 int CPDF_StandardSecurityHandler::CheckPassword(const uint8_t* password, FX_DWOR
D pass_size, FX_BOOL bOwner, uint8_t* key) | 370 int CPDF_StandardSecurityHandler::CheckPassword(const uint8_t* password, FX_DWOR
D pass_size, bool bOwner, uint8_t* key) |
| 371 { | 371 { |
| 372 return CheckPassword(password, pass_size, bOwner, key, m_KeyLen); | 372 return CheckPassword(password, pass_size, bOwner, key, m_KeyLen); |
| 373 } | 373 } |
| 374 int CPDF_StandardSecurityHandler::CheckPassword(const uint8_t* password, FX_DWOR
D size, FX_BOOL bOwner, uint8_t* key, int32_t key_len) | 374 int CPDF_StandardSecurityHandler::CheckPassword(const uint8_t* password, FX_DWOR
D size, bool bOwner, uint8_t* key, int32_t key_len) |
| 375 { | 375 { |
| 376 if (m_Revision >= 5) { | 376 if (m_Revision >= 5) { |
| 377 return AES256_CheckPassword(password, size, bOwner, key); | 377 return AES256_CheckPassword(password, size, bOwner, key); |
| 378 } | 378 } |
| 379 uint8_t keybuf[32]; | 379 uint8_t keybuf[32]; |
| 380 if (key == NULL) { | 380 if (key == NULL) { |
| 381 key = keybuf; | 381 key = keybuf; |
| 382 } | 382 } |
| 383 if (bOwner) { | 383 if (bOwner) { |
| 384 return CheckOwnerPassword(password, size, key, key_len); | 384 return CheckOwnerPassword(password, size, key, key_len); |
| 385 } | 385 } |
| 386 return CheckUserPassword(password, size, FALSE, key, key_len) || CheckUserPa
ssword(password, size, TRUE, key, key_len); | 386 return CheckUserPassword(password, size, false, key, key_len) || CheckUserPa
ssword(password, size, true, key, key_len); |
| 387 } | 387 } |
| 388 FX_BOOL CPDF_StandardSecurityHandler::CheckUserPassword(const uint8_t* password,
FX_DWORD pass_size, | 388 bool CPDF_StandardSecurityHandler::CheckUserPassword(const uint8_t* password, FX
_DWORD pass_size, |
| 389 FX_BOOL bIgnoreEncryptMeta, uint8_t* key, int32_t key_len) | 389 bool bIgnoreEncryptMeta, uint8_t* key, int32_t key_len) |
| 390 { | 390 { |
| 391 CalcEncryptKey(m_pEncryptDict, password, pass_size, key, key_len, bIgnoreEnc
ryptMeta, | 391 CalcEncryptKey(m_pEncryptDict, password, pass_size, key, key_len, bIgnoreEnc
ryptMeta, |
| 392 m_pParser->GetIDArray()); | 392 m_pParser->GetIDArray()); |
| 393 CFX_ByteString ukey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("U
")) : CFX_ByteString(); | 393 CFX_ByteString ukey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("U
")) : CFX_ByteString(); |
| 394 if (ukey.GetLength() < 16) { | 394 if (ukey.GetLength() < 16) { |
| 395 return FALSE; | 395 return false; |
| 396 } | 396 } |
| 397 uint8_t ukeybuf[32]; | 397 uint8_t ukeybuf[32]; |
| 398 if (m_Revision == 2) { | 398 if (m_Revision == 2) { |
| 399 FXSYS_memcpy(ukeybuf, defpasscode, 32); | 399 FXSYS_memcpy(ukeybuf, defpasscode, 32); |
| 400 CRYPT_ArcFourCryptBlock(ukeybuf, 32, key, key_len); | 400 CRYPT_ArcFourCryptBlock(ukeybuf, 32, key, key_len); |
| 401 } else { | 401 } else { |
| 402 uint8_t test[32], tmpkey[32]; | 402 uint8_t test[32], tmpkey[32]; |
| 403 FX_DWORD copy_len = sizeof(test); | 403 FX_DWORD copy_len = sizeof(test); |
| 404 if (copy_len > (FX_DWORD)ukey.GetLength()) { | 404 if (copy_len > (FX_DWORD)ukey.GetLength()) { |
| 405 copy_len = ukey.GetLength(); | 405 copy_len = ukey.GetLength(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 418 CRYPT_MD5Update(md5, defpasscode, 32); | 418 CRYPT_MD5Update(md5, defpasscode, 32); |
| 419 CPDF_Array* pIdArray = m_pParser->GetIDArray(); | 419 CPDF_Array* pIdArray = m_pParser->GetIDArray(); |
| 420 if (pIdArray) { | 420 if (pIdArray) { |
| 421 CFX_ByteString id = pIdArray->GetString(0); | 421 CFX_ByteString id = pIdArray->GetString(0); |
| 422 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); | 422 CRYPT_MD5Update(md5, (uint8_t*)id.c_str(), id.GetLength()); |
| 423 } | 423 } |
| 424 CRYPT_MD5Finish(md5, ukeybuf); | 424 CRYPT_MD5Finish(md5, ukeybuf); |
| 425 return FXSYS_memcmp(test, ukeybuf, 16) == 0; | 425 return FXSYS_memcmp(test, ukeybuf, 16) == 0; |
| 426 } | 426 } |
| 427 if (FXSYS_memcmp((void*)ukey.c_str(), ukeybuf, 16) == 0) { | 427 if (FXSYS_memcmp((void*)ukey.c_str(), ukeybuf, 16) == 0) { |
| 428 return TRUE; | 428 return true; |
| 429 } | 429 } |
| 430 return FALSE; | 430 return false; |
| 431 } | 431 } |
| 432 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(const uint8_t* owne
r_pass, FX_DWORD pass_size) | 432 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(const uint8_t* owne
r_pass, FX_DWORD pass_size) |
| 433 { | 433 { |
| 434 return GetUserPassword(owner_pass, pass_size, m_KeyLen); | 434 return GetUserPassword(owner_pass, pass_size, m_KeyLen); |
| 435 } | 435 } |
| 436 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(const uint8_t* owne
r_pass, FX_DWORD pass_size, int32_t key_len) | 436 CFX_ByteString CPDF_StandardSecurityHandler::GetUserPassword(const uint8_t* owne
r_pass, FX_DWORD pass_size, int32_t key_len) |
| 437 { | 437 { |
| 438 CFX_ByteString okey = m_pEncryptDict->GetString(FX_BSTRC("O")); | 438 CFX_ByteString okey = m_pEncryptDict->GetString(FX_BSTRC("O")); |
| 439 uint8_t passcode[32]; | 439 uint8_t passcode[32]; |
| 440 FX_DWORD i; | 440 FX_DWORD i; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 } | 473 } |
| 474 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, tempkey, key_len); | 474 CRYPT_ArcFourCryptBlock(okeybuf, okeylen, tempkey, key_len); |
| 475 } | 475 } |
| 476 } | 476 } |
| 477 int len = 32; | 477 int len = 32; |
| 478 while (len && defpasscode[len - 1] == okeybuf[len - 1]) { | 478 while (len && defpasscode[len - 1] == okeybuf[len - 1]) { |
| 479 len --; | 479 len --; |
| 480 } | 480 } |
| 481 return CFX_ByteString(okeybuf, len); | 481 return CFX_ByteString(okeybuf, len); |
| 482 } | 482 } |
| 483 FX_BOOL CPDF_StandardSecurityHandler::CheckOwnerPassword(const uint8_t* password
, FX_DWORD pass_size, | 483 bool CPDF_StandardSecurityHandler::CheckOwnerPassword(const uint8_t* password, F
X_DWORD pass_size, |
| 484 uint8_t* key, int32_t key_len) | 484 uint8_t* key, int32_t key_len) |
| 485 { | 485 { |
| 486 CFX_ByteString user_pass = GetUserPassword(password, pass_size, key_len); | 486 CFX_ByteString user_pass = GetUserPassword(password, pass_size, key_len); |
| 487 if (CheckUserPassword(user_pass, user_pass.GetLength(), FALSE, key, key_len)
) { | 487 if (CheckUserPassword(user_pass, user_pass.GetLength(), false, key, key_len)
) { |
| 488 return TRUE; | 488 return true; |
| 489 } | 489 } |
| 490 return CheckUserPassword(user_pass, user_pass.GetLength(), TRUE, key, key_le
n); | 490 return CheckUserPassword(user_pass, user_pass.GetLength(), true, key, key_le
n); |
| 491 } | 491 } |
| 492 FX_BOOL CPDF_StandardSecurityHandler::IsMetadataEncrypted() | 492 bool CPDF_StandardSecurityHandler::IsMetadataEncrypted() |
| 493 { | 493 { |
| 494 return m_pEncryptDict->GetBoolean(FX_BSTRC("EncryptMetadata"), TRUE); | 494 return m_pEncryptDict->GetBoolean(FX_BSTRC("EncryptMetadata"), true); |
| 495 } | 495 } |
| 496 CPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler() | 496 CPDF_SecurityHandler* FPDF_CreateStandardSecurityHandler() |
| 497 { | 497 { |
| 498 return new CPDF_StandardSecurityHandler; | 498 return new CPDF_StandardSecurityHandler; |
| 499 } | 499 } |
| 500 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_
Array* pIdArray, | 500 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_
Array* pIdArray, |
| 501 const uint8_t* user_pass, FX_DWORD user_size, | 501 const uint8_t* user_pass, FX_DWORD user_size, |
| 502 const uint8_t* owner_pass, FX_DWORD owner_size, FX_BOOL bDefault, FX_DWO
RD type) | 502 const uint8_t* owner_pass, FX_DWORD owner_size, bool bDefault, FX_DWORD
type) |
| 503 { | 503 { |
| 504 int cipher = 0, key_len = 0; | 504 int cipher = 0, key_len = 0; |
| 505 if (!LoadDict(pEncryptDict, type, cipher, key_len)) { | 505 if (!LoadDict(pEncryptDict, type, cipher, key_len)) { |
| 506 return; | 506 return; |
| 507 } | 507 } |
| 508 if (bDefault && (owner_pass == NULL || owner_size == 0)) { | 508 if (bDefault && (owner_pass == NULL || owner_size == 0)) { |
| 509 owner_pass = user_pass; | 509 owner_pass = user_pass; |
| 510 owner_size = user_size; | 510 owner_size = user_size; |
| 511 } | 511 } |
| 512 if (m_Revision >= 5) { | 512 if (m_Revision >= 5) { |
| 513 int t = (int)time(NULL); | 513 int t = (int)time(NULL); |
| 514 uint8_t sha[128]; | 514 uint8_t sha[128]; |
| 515 CRYPT_SHA256Start(sha); | 515 CRYPT_SHA256Start(sha); |
| 516 CRYPT_SHA256Update(sha, (uint8_t*)&t, sizeof t); | 516 CRYPT_SHA256Update(sha, (uint8_t*)&t, sizeof t); |
| 517 CRYPT_SHA256Update(sha, m_EncryptKey, 32); | 517 CRYPT_SHA256Update(sha, m_EncryptKey, 32); |
| 518 CRYPT_SHA256Update(sha, (uint8_t*)"there", 5); | 518 CRYPT_SHA256Update(sha, (uint8_t*)"there", 5); |
| 519 CRYPT_SHA256Finish(sha, m_EncryptKey); | 519 CRYPT_SHA256Finish(sha, m_EncryptKey); |
| 520 AES256_SetPassword(pEncryptDict, user_pass, user_size, FALSE, m_EncryptK
ey); | 520 AES256_SetPassword(pEncryptDict, user_pass, user_size, false, m_EncryptK
ey); |
| 521 if (bDefault) { | 521 if (bDefault) { |
| 522 AES256_SetPassword(pEncryptDict, owner_pass, owner_size, TRUE, m_Enc
ryptKey); | 522 AES256_SetPassword(pEncryptDict, owner_pass, owner_size, true, m_Enc
ryptKey); |
| 523 AES256_SetPerms(pEncryptDict, m_Permissions, pEncryptDict->GetBoolea
n(FX_BSTRC("EncryptMetadata"), TRUE), m_EncryptKey); | 523 AES256_SetPerms(pEncryptDict, m_Permissions, pEncryptDict->GetBoolea
n(FX_BSTRC("EncryptMetadata"), true), m_EncryptKey); |
| 524 } | 524 } |
| 525 return; | 525 return; |
| 526 } | 526 } |
| 527 if (bDefault) { | 527 if (bDefault) { |
| 528 uint8_t passcode[32]; | 528 uint8_t passcode[32]; |
| 529 FX_DWORD i; | 529 FX_DWORD i; |
| 530 for (i = 0; i < 32; i ++) { | 530 for (i = 0; i < 32; i ++) { |
| 531 passcode[i] = i < owner_size ? owner_pass[i] : defpasscode[i - owner
_size]; | 531 passcode[i] = i < owner_size ? owner_pass[i] : defpasscode[i - owner
_size]; |
| 532 } | 532 } |
| 533 uint8_t digest[16]; | 533 uint8_t digest[16]; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 547 if (m_Revision >= 3) { | 547 if (m_Revision >= 3) { |
| 548 for (i = 1; i <= 19; i ++) { | 548 for (i = 1; i <= 19; i ++) { |
| 549 for (int j = 0; j < key_len; j ++) { | 549 for (int j = 0; j < key_len; j ++) { |
| 550 tempkey[j] = enckey[j] ^ (uint8_t)i; | 550 tempkey[j] = enckey[j] ^ (uint8_t)i; |
| 551 } | 551 } |
| 552 CRYPT_ArcFourCryptBlock(passcode, 32, tempkey, key_len); | 552 CRYPT_ArcFourCryptBlock(passcode, 32, tempkey, key_len); |
| 553 } | 553 } |
| 554 } | 554 } |
| 555 pEncryptDict->SetAtString(FX_BSTRC("O"), CFX_ByteString(passcode, 32)); | 555 pEncryptDict->SetAtString(FX_BSTRC("O"), CFX_ByteString(passcode, 32)); |
| 556 } | 556 } |
| 557 CalcEncryptKey(m_pEncryptDict, (uint8_t*)user_pass, user_size, m_EncryptKey,
key_len, FALSE, pIdArray); | 557 CalcEncryptKey(m_pEncryptDict, (uint8_t*)user_pass, user_size, m_EncryptKey,
key_len, false, pIdArray); |
| 558 if (m_Revision < 3) { | 558 if (m_Revision < 3) { |
| 559 uint8_t tempbuf[32]; | 559 uint8_t tempbuf[32]; |
| 560 FXSYS_memcpy(tempbuf, defpasscode, 32); | 560 FXSYS_memcpy(tempbuf, defpasscode, 32); |
| 561 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); | 561 CRYPT_ArcFourCryptBlock(tempbuf, 32, m_EncryptKey, key_len); |
| 562 pEncryptDict->SetAtString(FX_BSTRC("U"), CFX_ByteString(tempbuf, 32)); | 562 pEncryptDict->SetAtString(FX_BSTRC("U"), CFX_ByteString(tempbuf, 32)); |
| 563 } else { | 563 } else { |
| 564 uint8_t md5[100]; | 564 uint8_t md5[100]; |
| 565 CRYPT_MD5Start(md5); | 565 CRYPT_MD5Start(md5); |
| 566 CRYPT_MD5Update(md5, defpasscode, 32); | 566 CRYPT_MD5Update(md5, defpasscode, 32); |
| 567 if (pIdArray) { | 567 if (pIdArray) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 579 CRYPT_ArcFourCryptBlock(digest, 16, tempkey, key_len); | 579 CRYPT_ArcFourCryptBlock(digest, 16, tempkey, key_len); |
| 580 } | 580 } |
| 581 CRYPT_MD5Generate(digest, 16, digest + 16); | 581 CRYPT_MD5Generate(digest, 16, digest + 16); |
| 582 pEncryptDict->SetAtString(FX_BSTRC("U"), CFX_ByteString(digest, 32)); | 582 pEncryptDict->SetAtString(FX_BSTRC("U"), CFX_ByteString(digest, 32)); |
| 583 } | 583 } |
| 584 } | 584 } |
| 585 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_
Array* pIdArray, | 585 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_
Array* pIdArray, |
| 586 const uint8_t* user_pass, FX_DWORD user_size, | 586 const uint8_t* user_pass, FX_DWORD user_size, |
| 587 const uint8_t* owner_pass, FX_DWORD owner_size, FX_DWORD type) | 587 const uint8_t* owner_pass, FX_DWORD owner_size, FX_DWORD type) |
| 588 { | 588 { |
| 589 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, owner_pass, owner_siz
e, TRUE, type); | 589 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, owner_pass, owner_siz
e, true, type); |
| 590 } | 590 } |
| 591 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_
Array* pIdArray, const uint8_t* user_pass, FX_DWORD user_size, FX_DWORD type) | 591 void CPDF_StandardSecurityHandler::OnCreate(CPDF_Dictionary* pEncryptDict, CPDF_
Array* pIdArray, const uint8_t* user_pass, FX_DWORD user_size, FX_DWORD type) |
| 592 { | 592 { |
| 593 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, NULL, 0, FALSE, type)
; | 593 OnCreate(pEncryptDict, pIdArray, user_pass, user_size, NULL, 0, false, type)
; |
| 594 } | 594 } |
| 595 void CPDF_StandardSecurityHandler::AES256_SetPassword(CPDF_Dictionary* pEncryptD
ict, const uint8_t* password, FX_DWORD size, FX_BOOL bOwner, const uint8_t* key) | 595 void CPDF_StandardSecurityHandler::AES256_SetPassword(CPDF_Dictionary* pEncryptD
ict, const uint8_t* password, FX_DWORD size, bool bOwner, const uint8_t* key) |
| 596 { | 596 { |
| 597 uint8_t sha[128]; | 597 uint8_t sha[128]; |
| 598 CRYPT_SHA1Start(sha); | 598 CRYPT_SHA1Start(sha); |
| 599 CRYPT_SHA1Update(sha, key, 32); | 599 CRYPT_SHA1Update(sha, key, 32); |
| 600 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); | 600 CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); |
| 601 uint8_t digest[20]; | 601 uint8_t digest[20]; |
| 602 CRYPT_SHA1Finish(sha, digest); | 602 CRYPT_SHA1Finish(sha, digest); |
| 603 CFX_ByteString ukey = pEncryptDict->GetString(FX_BSTRC("U")); | 603 CFX_ByteString ukey = pEncryptDict->GetString(FX_BSTRC("U")); |
| 604 uint8_t digest1[48]; | 604 uint8_t digest1[48]; |
| 605 if (m_Revision >= 6) { | 605 if (m_Revision >= 6) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 620 } else { | 620 } else { |
| 621 CRYPT_SHA256Start(sha); | 621 CRYPT_SHA256Start(sha); |
| 622 CRYPT_SHA256Update(sha, password, size); | 622 CRYPT_SHA256Update(sha, password, size); |
| 623 CRYPT_SHA256Update(sha, digest + 8, 8); | 623 CRYPT_SHA256Update(sha, digest + 8, 8); |
| 624 if (bOwner) { | 624 if (bOwner) { |
| 625 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); | 625 CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); |
| 626 } | 626 } |
| 627 CRYPT_SHA256Finish(sha, digest1); | 627 CRYPT_SHA256Finish(sha, digest1); |
| 628 } | 628 } |
| 629 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 629 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
| 630 CRYPT_AESSetKey(aes, 16, digest1, 32, TRUE); | 630 CRYPT_AESSetKey(aes, 16, digest1, 32, true); |
| 631 uint8_t iv[16]; | 631 uint8_t iv[16]; |
| 632 FXSYS_memset(iv, 0, 16); | 632 FXSYS_memset(iv, 0, 16); |
| 633 CRYPT_AESSetIV(aes, iv); | 633 CRYPT_AESSetIV(aes, iv); |
| 634 CRYPT_AESEncrypt(aes, digest1, key, 32); | 634 CRYPT_AESEncrypt(aes, digest1, key, 32); |
| 635 FX_Free(aes); | 635 FX_Free(aes); |
| 636 pEncryptDict->SetAtString(bOwner ? FX_BSTRC("OE") : FX_BSTRC("UE"), CFX_Byte
String(digest1, 32)); | 636 pEncryptDict->SetAtString(bOwner ? FX_BSTRC("OE") : FX_BSTRC("UE"), CFX_Byte
String(digest1, 32)); |
| 637 } | 637 } |
| 638 void CPDF_StandardSecurityHandler::AES256_SetPerms(CPDF_Dictionary* pEncryptDict
, FX_DWORD permissions, | 638 void CPDF_StandardSecurityHandler::AES256_SetPerms(CPDF_Dictionary* pEncryptDict
, FX_DWORD permissions, |
| 639 FX_BOOL bEncryptMetadata, const uint8_t* key) | 639 bool bEncryptMetadata, const uint8_t* key) |
| 640 { | 640 { |
| 641 uint8_t buf[16]; | 641 uint8_t buf[16]; |
| 642 buf[0] = (uint8_t)permissions; | 642 buf[0] = (uint8_t)permissions; |
| 643 buf[1] = (uint8_t)(permissions >> 8); | 643 buf[1] = (uint8_t)(permissions >> 8); |
| 644 buf[2] = (uint8_t)(permissions >> 16); | 644 buf[2] = (uint8_t)(permissions >> 16); |
| 645 buf[3] = (uint8_t)(permissions >> 24); | 645 buf[3] = (uint8_t)(permissions >> 24); |
| 646 buf[4] = 0xff; | 646 buf[4] = 0xff; |
| 647 buf[5] = 0xff; | 647 buf[5] = 0xff; |
| 648 buf[6] = 0xff; | 648 buf[6] = 0xff; |
| 649 buf[7] = 0xff; | 649 buf[7] = 0xff; |
| 650 buf[8] = bEncryptMetadata ? 'T' : 'F'; | 650 buf[8] = bEncryptMetadata ? 'T' : 'F'; |
| 651 buf[9] = 'a'; | 651 buf[9] = 'a'; |
| 652 buf[10] = 'd'; | 652 buf[10] = 'd'; |
| 653 buf[11] = 'b'; | 653 buf[11] = 'b'; |
| 654 uint8_t* aes = FX_Alloc(uint8_t, 2048); | 654 uint8_t* aes = FX_Alloc(uint8_t, 2048); |
| 655 CRYPT_AESSetKey(aes, 16, key, 32, TRUE); | 655 CRYPT_AESSetKey(aes, 16, key, 32, true); |
| 656 uint8_t iv[16], buf1[16]; | 656 uint8_t iv[16], buf1[16]; |
| 657 FXSYS_memset(iv, 0, 16); | 657 FXSYS_memset(iv, 0, 16); |
| 658 CRYPT_AESSetIV(aes, iv); | 658 CRYPT_AESSetIV(aes, iv); |
| 659 CRYPT_AESEncrypt(aes, buf1, buf, 16); | 659 CRYPT_AESEncrypt(aes, buf1, buf, 16); |
| 660 FX_Free(aes); | 660 FX_Free(aes); |
| 661 pEncryptDict->SetAtString(FX_BSTRC("Perms"), CFX_ByteString(buf1, 16)); | 661 pEncryptDict->SetAtString(FX_BSTRC("Perms"), CFX_ByteString(buf1, 16)); |
| 662 } | 662 } |
| 663 void CPDF_StandardCryptoHandler::CryptBlock(FX_BOOL bEncrypt, FX_DWORD objnum, F
X_DWORD gennum, const uint8_t* src_buf, FX_DWORD src_size, | 663 void CPDF_StandardCryptoHandler::CryptBlock(bool bEncrypt, FX_DWORD objnum, FX_D
WORD gennum, const uint8_t* src_buf, FX_DWORD src_size, |
| 664 uint8_t* dest_buf, FX_DWORD& dest_size) | 664 uint8_t* dest_buf, FX_DWORD& dest_size) |
| 665 { | 665 { |
| 666 if (m_Cipher == FXCIPHER_NONE) { | 666 if (m_Cipher == FXCIPHER_NONE) { |
| 667 FXSYS_memcpy(dest_buf, src_buf, src_size); | 667 FXSYS_memcpy(dest_buf, src_buf, src_size); |
| 668 return; | 668 return; |
| 669 } | 669 } |
| 670 uint8_t realkey[16]; | 670 uint8_t realkey[16]; |
| 671 int realkeylen = 16; | 671 int realkeylen = 16; |
| 672 if (m_Cipher != FXCIPHER_AES || m_KeyLen != 32) { | 672 if (m_Cipher != FXCIPHER_AES || m_KeyLen != 32) { |
| 673 uint8_t key1[32]; | 673 uint8_t key1[32]; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 713 } else { | 713 } else { |
| 714 ASSERT(dest_size == src_size); | 714 ASSERT(dest_size == src_size); |
| 715 if (dest_buf != src_buf) { | 715 if (dest_buf != src_buf) { |
| 716 FXSYS_memcpy(dest_buf, src_buf, src_size); | 716 FXSYS_memcpy(dest_buf, src_buf, src_size); |
| 717 } | 717 } |
| 718 CRYPT_ArcFourCryptBlock(dest_buf, dest_size, realkey, realkeylen); | 718 CRYPT_ArcFourCryptBlock(dest_buf, dest_size, realkey, realkeylen); |
| 719 } | 719 } |
| 720 } | 720 } |
| 721 typedef struct _AESCryptContext { | 721 typedef struct _AESCryptContext { |
| 722 uint8_t m_Context[2048]; | 722 uint8_t m_Context[2048]; |
| 723 FX_BOOL» » m_bIV; | 723 bool» » m_bIV; |
| 724 uint8_t m_Block[16]; | 724 uint8_t m_Block[16]; |
| 725 FX_DWORD m_BlockOffset; | 725 FX_DWORD m_BlockOffset; |
| 726 } AESCryptContext; | 726 } AESCryptContext; |
| 727 void* CPDF_StandardCryptoHandler::CryptStart(FX_DWORD objnum, FX_DWORD gennum, F
X_BOOL bEncrypt) | 727 void* CPDF_StandardCryptoHandler::CryptStart(FX_DWORD objnum, FX_DWORD gennum, b
ool bEncrypt) |
| 728 { | 728 { |
| 729 if (m_Cipher == FXCIPHER_NONE) { | 729 if (m_Cipher == FXCIPHER_NONE) { |
| 730 return this; | 730 return this; |
| 731 } | 731 } |
| 732 if (m_Cipher == FXCIPHER_AES && m_KeyLen == 32) { | 732 if (m_Cipher == FXCIPHER_AES && m_KeyLen == 32) { |
| 733 AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); | 733 AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); |
| 734 pContext->m_bIV = TRUE; | 734 pContext->m_bIV = true; |
| 735 pContext->m_BlockOffset = 0; | 735 pContext->m_BlockOffset = 0; |
| 736 CRYPT_AESSetKey(pContext->m_Context, 16, m_EncryptKey, 32, bEncrypt); | 736 CRYPT_AESSetKey(pContext->m_Context, 16, m_EncryptKey, 32, bEncrypt); |
| 737 if (bEncrypt) { | 737 if (bEncrypt) { |
| 738 for (int i = 0; i < 16; i ++) { | 738 for (int i = 0; i < 16; i ++) { |
| 739 pContext->m_Block[i] = (uint8_t)rand(); | 739 pContext->m_Block[i] = (uint8_t)rand(); |
| 740 } | 740 } |
| 741 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); | 741 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); |
| 742 } | 742 } |
| 743 return pContext; | 743 return pContext; |
| 744 } | 744 } |
| 745 uint8_t key1[48]; | 745 uint8_t key1[48]; |
| 746 FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen); | 746 FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen); |
| 747 FXSYS_memcpy(key1 + m_KeyLen, &objnum, 3); | 747 FXSYS_memcpy(key1 + m_KeyLen, &objnum, 3); |
| 748 FXSYS_memcpy(key1 + m_KeyLen + 3, &gennum, 2); | 748 FXSYS_memcpy(key1 + m_KeyLen + 3, &gennum, 2); |
| 749 if (m_Cipher == FXCIPHER_AES) { | 749 if (m_Cipher == FXCIPHER_AES) { |
| 750 FXSYS_memcpy(key1 + m_KeyLen + 5, "sAlT", 4); | 750 FXSYS_memcpy(key1 + m_KeyLen + 5, "sAlT", 4); |
| 751 } | 751 } |
| 752 uint8_t realkey[16]; | 752 uint8_t realkey[16]; |
| 753 CRYPT_MD5Generate(key1, m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen +
5, realkey); | 753 CRYPT_MD5Generate(key1, m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen +
5, realkey); |
| 754 int realkeylen = m_KeyLen + 5; | 754 int realkeylen = m_KeyLen + 5; |
| 755 if (realkeylen > 16) { | 755 if (realkeylen > 16) { |
| 756 realkeylen = 16; | 756 realkeylen = 16; |
| 757 } | 757 } |
| 758 if (m_Cipher == FXCIPHER_AES) { | 758 if (m_Cipher == FXCIPHER_AES) { |
| 759 AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); | 759 AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); |
| 760 pContext->m_bIV = TRUE; | 760 pContext->m_bIV = true; |
| 761 pContext->m_BlockOffset = 0; | 761 pContext->m_BlockOffset = 0; |
| 762 CRYPT_AESSetKey(pContext->m_Context, 16, realkey, 16, bEncrypt); | 762 CRYPT_AESSetKey(pContext->m_Context, 16, realkey, 16, bEncrypt); |
| 763 if (bEncrypt) { | 763 if (bEncrypt) { |
| 764 for (int i = 0; i < 16; i ++) { | 764 for (int i = 0; i < 16; i ++) { |
| 765 pContext->m_Block[i] = (uint8_t)rand(); | 765 pContext->m_Block[i] = (uint8_t)rand(); |
| 766 } | 766 } |
| 767 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); | 767 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); |
| 768 } | 768 } |
| 769 return pContext; | 769 return pContext; |
| 770 } | 770 } |
| 771 void* pContext = FX_Alloc(uint8_t, 1040); | 771 void* pContext = FX_Alloc(uint8_t, 1040); |
| 772 CRYPT_ArcFourSetup(pContext, realkey, realkeylen); | 772 CRYPT_ArcFourSetup(pContext, realkey, realkeylen); |
| 773 return pContext; | 773 return pContext; |
| 774 } | 774 } |
| 775 FX_BOOL CPDF_StandardCryptoHandler::CryptStream(void* context, const uint8_t* sr
c_buf, FX_DWORD src_size, CFX_BinaryBuf& dest_buf, FX_BOOL bEncrypt) | 775 bool CPDF_StandardCryptoHandler::CryptStream(void* context, const uint8_t* src_b
uf, FX_DWORD src_size, CFX_BinaryBuf& dest_buf, bool bEncrypt) |
| 776 { | 776 { |
| 777 if (!context) { | 777 if (!context) { |
| 778 return FALSE; | 778 return false; |
| 779 } | 779 } |
| 780 if (m_Cipher == FXCIPHER_NONE) { | 780 if (m_Cipher == FXCIPHER_NONE) { |
| 781 dest_buf.AppendBlock(src_buf, src_size); | 781 dest_buf.AppendBlock(src_buf, src_size); |
| 782 return TRUE; | 782 return true; |
| 783 } | 783 } |
| 784 if (m_Cipher == FXCIPHER_RC4) { | 784 if (m_Cipher == FXCIPHER_RC4) { |
| 785 int old_size = dest_buf.GetSize(); | 785 int old_size = dest_buf.GetSize(); |
| 786 dest_buf.AppendBlock(src_buf, src_size); | 786 dest_buf.AppendBlock(src_buf, src_size); |
| 787 CRYPT_ArcFourCrypt(context, dest_buf.GetBuffer() + old_size, src_size); | 787 CRYPT_ArcFourCrypt(context, dest_buf.GetBuffer() + old_size, src_size); |
| 788 return TRUE; | 788 return true; |
| 789 } | 789 } |
| 790 AESCryptContext* pContext = (AESCryptContext*)context; | 790 AESCryptContext* pContext = (AESCryptContext*)context; |
| 791 if (pContext->m_bIV && bEncrypt) { | 791 if (pContext->m_bIV && bEncrypt) { |
| 792 dest_buf.AppendBlock(pContext->m_Block, 16); | 792 dest_buf.AppendBlock(pContext->m_Block, 16); |
| 793 pContext->m_bIV = FALSE; | 793 pContext->m_bIV = false; |
| 794 } | 794 } |
| 795 FX_DWORD src_off = 0; | 795 FX_DWORD src_off = 0; |
| 796 FX_DWORD src_left = src_size; | 796 FX_DWORD src_left = src_size; |
| 797 while (1) { | 797 while (1) { |
| 798 FX_DWORD copy_size = 16 - pContext->m_BlockOffset; | 798 FX_DWORD copy_size = 16 - pContext->m_BlockOffset; |
| 799 if (copy_size > src_left) { | 799 if (copy_size > src_left) { |
| 800 copy_size = src_left; | 800 copy_size = src_left; |
| 801 } | 801 } |
| 802 FXSYS_memcpy(pContext->m_Block + pContext->m_BlockOffset, src_buf + src_
off, copy_size); | 802 FXSYS_memcpy(pContext->m_Block + pContext->m_BlockOffset, src_buf + src_
off, copy_size); |
| 803 src_off += copy_size; | 803 src_off += copy_size; |
| 804 src_left -= copy_size; | 804 src_left -= copy_size; |
| 805 pContext->m_BlockOffset += copy_size; | 805 pContext->m_BlockOffset += copy_size; |
| 806 if (pContext->m_BlockOffset == 16) { | 806 if (pContext->m_BlockOffset == 16) { |
| 807 if (!bEncrypt && pContext->m_bIV) { | 807 if (!bEncrypt && pContext->m_bIV) { |
| 808 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); | 808 CRYPT_AESSetIV(pContext->m_Context, pContext->m_Block); |
| 809 pContext->m_bIV = FALSE; | 809 pContext->m_bIV = false; |
| 810 pContext->m_BlockOffset = 0; | 810 pContext->m_BlockOffset = 0; |
| 811 } else if (src_off < src_size) { | 811 } else if (src_off < src_size) { |
| 812 uint8_t block_buf[16]; | 812 uint8_t block_buf[16]; |
| 813 if (bEncrypt) { | 813 if (bEncrypt) { |
| 814 CRYPT_AESEncrypt(pContext->m_Context, block_buf, pContext->m
_Block, 16); | 814 CRYPT_AESEncrypt(pContext->m_Context, block_buf, pContext->m
_Block, 16); |
| 815 } else { | 815 } else { |
| 816 CRYPT_AESDecrypt(pContext->m_Context, block_buf, pContext->m
_Block, 16); | 816 CRYPT_AESDecrypt(pContext->m_Context, block_buf, pContext->m
_Block, 16); |
| 817 } | 817 } |
| 818 dest_buf.AppendBlock(block_buf, 16); | 818 dest_buf.AppendBlock(block_buf, 16); |
| 819 pContext->m_BlockOffset = 0; | 819 pContext->m_BlockOffset = 0; |
| 820 } | 820 } |
| 821 } | 821 } |
| 822 if (!src_left) { | 822 if (!src_left) { |
| 823 break; | 823 break; |
| 824 } | 824 } |
| 825 } | 825 } |
| 826 return TRUE; | 826 return true; |
| 827 } | 827 } |
| 828 FX_BOOL CPDF_StandardCryptoHandler::CryptFinish(void* context, CFX_BinaryBuf& de
st_buf, FX_BOOL bEncrypt) | 828 bool CPDF_StandardCryptoHandler::CryptFinish(void* context, CFX_BinaryBuf& dest_
buf, bool bEncrypt) |
| 829 { | 829 { |
| 830 if (!context) { | 830 if (!context) { |
| 831 return FALSE; | 831 return false; |
| 832 } | 832 } |
| 833 if (m_Cipher == FXCIPHER_NONE) { | 833 if (m_Cipher == FXCIPHER_NONE) { |
| 834 return TRUE; | 834 return true; |
| 835 } | 835 } |
| 836 if (m_Cipher == FXCIPHER_RC4) { | 836 if (m_Cipher == FXCIPHER_RC4) { |
| 837 FX_Free(context); | 837 FX_Free(context); |
| 838 return TRUE; | 838 return true; |
| 839 } | 839 } |
| 840 AESCryptContext* pContext = (AESCryptContext*)context; | 840 AESCryptContext* pContext = (AESCryptContext*)context; |
| 841 if (bEncrypt) { | 841 if (bEncrypt) { |
| 842 uint8_t block_buf[16]; | 842 uint8_t block_buf[16]; |
| 843 if (pContext->m_BlockOffset == 16) { | 843 if (pContext->m_BlockOffset == 16) { |
| 844 CRYPT_AESEncrypt(pContext->m_Context, block_buf, pContext->m_Block,
16); | 844 CRYPT_AESEncrypt(pContext->m_Context, block_buf, pContext->m_Block,
16); |
| 845 dest_buf.AppendBlock(block_buf, 16); | 845 dest_buf.AppendBlock(block_buf, 16); |
| 846 pContext->m_BlockOffset = 0; | 846 pContext->m_BlockOffset = 0; |
| 847 } | 847 } |
| 848 FXSYS_memset(pContext->m_Block + pContext->m_BlockOffset, (uint8_t)(16 -
pContext->m_BlockOffset), 16 - pContext->m_BlockOffset); | 848 FXSYS_memset(pContext->m_Block + pContext->m_BlockOffset, (uint8_t)(16 -
pContext->m_BlockOffset), 16 - pContext->m_BlockOffset); |
| 849 CRYPT_AESEncrypt(pContext->m_Context, block_buf, pContext->m_Block, 16); | 849 CRYPT_AESEncrypt(pContext->m_Context, block_buf, pContext->m_Block, 16); |
| 850 dest_buf.AppendBlock(block_buf, 16); | 850 dest_buf.AppendBlock(block_buf, 16); |
| 851 } else if (pContext->m_BlockOffset == 16) { | 851 } else if (pContext->m_BlockOffset == 16) { |
| 852 uint8_t block_buf[16]; | 852 uint8_t block_buf[16]; |
| 853 CRYPT_AESDecrypt(pContext->m_Context, block_buf, pContext->m_Block, 16); | 853 CRYPT_AESDecrypt(pContext->m_Context, block_buf, pContext->m_Block, 16); |
| 854 if (block_buf[15] <= 16) { | 854 if (block_buf[15] <= 16) { |
| 855 dest_buf.AppendBlock(block_buf, 16 - block_buf[15]); | 855 dest_buf.AppendBlock(block_buf, 16 - block_buf[15]); |
| 856 } | 856 } |
| 857 } | 857 } |
| 858 FX_Free(pContext); | 858 FX_Free(pContext); |
| 859 return TRUE; | 859 return true; |
| 860 } | 860 } |
| 861 void* CPDF_StandardCryptoHandler::DecryptStart(FX_DWORD objnum, FX_DWORD gennum) | 861 void* CPDF_StandardCryptoHandler::DecryptStart(FX_DWORD objnum, FX_DWORD gennum) |
| 862 { | 862 { |
| 863 return CryptStart(objnum, gennum, FALSE); | 863 return CryptStart(objnum, gennum, false); |
| 864 } | 864 } |
| 865 FX_DWORD CPDF_StandardCryptoHandler::DecryptGetSize(FX_DWORD src_size) | 865 FX_DWORD CPDF_StandardCryptoHandler::DecryptGetSize(FX_DWORD src_size) |
| 866 { | 866 { |
| 867 return m_Cipher == FXCIPHER_AES ? src_size - 16 : src_size; | 867 return m_Cipher == FXCIPHER_AES ? src_size - 16 : src_size; |
| 868 } | 868 } |
| 869 FX_BOOL CPDF_StandardCryptoHandler::Init(CPDF_Dictionary* pEncryptDict, CPDF_Sec
urityHandler* pSecurityHandler) | 869 bool CPDF_StandardCryptoHandler::Init(CPDF_Dictionary* pEncryptDict, CPDF_Securi
tyHandler* pSecurityHandler) |
| 870 { | 870 { |
| 871 const uint8_t* key; | 871 const uint8_t* key; |
| 872 if (!pSecurityHandler->GetCryptInfo(m_Cipher, key, m_KeyLen)) { | 872 if (!pSecurityHandler->GetCryptInfo(m_Cipher, key, m_KeyLen)) { |
| 873 return FALSE; | 873 return false; |
| 874 } | 874 } |
| 875 if (m_KeyLen > 32 || m_KeyLen < 0) { | 875 if (m_KeyLen > 32 || m_KeyLen < 0) { |
| 876 return FALSE; | 876 return false; |
| 877 } | 877 } |
| 878 if (m_Cipher != FXCIPHER_NONE) { | 878 if (m_Cipher != FXCIPHER_NONE) { |
| 879 FXSYS_memcpy(m_EncryptKey, key, m_KeyLen); | 879 FXSYS_memcpy(m_EncryptKey, key, m_KeyLen); |
| 880 } | 880 } |
| 881 if (m_Cipher == FXCIPHER_AES) { | 881 if (m_Cipher == FXCIPHER_AES) { |
| 882 m_pAESContext = FX_Alloc(uint8_t, 2048); | 882 m_pAESContext = FX_Alloc(uint8_t, 2048); |
| 883 } | 883 } |
| 884 return TRUE; | 884 return true; |
| 885 } | 885 } |
| 886 FX_BOOL CPDF_StandardCryptoHandler::Init(int cipher, const uint8_t* key, int key
len) | 886 bool CPDF_StandardCryptoHandler::Init(int cipher, const uint8_t* key, int keylen
) |
| 887 { | 887 { |
| 888 if (cipher == FXCIPHER_AES) { | 888 if (cipher == FXCIPHER_AES) { |
| 889 switch(keylen) { | 889 switch(keylen) { |
| 890 case 16: | 890 case 16: |
| 891 case 24: | 891 case 24: |
| 892 case 32: | 892 case 32: |
| 893 break; | 893 break; |
| 894 default: | 894 default: |
| 895 return FALSE; | 895 return false; |
| 896 } | 896 } |
| 897 } else if (cipher == FXCIPHER_AES2) { | 897 } else if (cipher == FXCIPHER_AES2) { |
| 898 if (keylen != 32) { | 898 if (keylen != 32) { |
| 899 return FALSE; | 899 return false; |
| 900 } | 900 } |
| 901 } else if (cipher == FXCIPHER_RC4) { | 901 } else if (cipher == FXCIPHER_RC4) { |
| 902 if (keylen < 5 || keylen > 16) { | 902 if (keylen < 5 || keylen > 16) { |
| 903 return FALSE; | 903 return false; |
| 904 } | 904 } |
| 905 } else { | 905 } else { |
| 906 if (keylen > 32) { | 906 if (keylen > 32) { |
| 907 keylen = 32; | 907 keylen = 32; |
| 908 } | 908 } |
| 909 } | 909 } |
| 910 m_Cipher = cipher; | 910 m_Cipher = cipher; |
| 911 m_KeyLen = keylen; | 911 m_KeyLen = keylen; |
| 912 FXSYS_memcpy(m_EncryptKey, key, keylen); | 912 FXSYS_memcpy(m_EncryptKey, key, keylen); |
| 913 if (m_Cipher == FXCIPHER_AES) { | 913 if (m_Cipher == FXCIPHER_AES) { |
| 914 m_pAESContext = FX_Alloc(uint8_t, 2048); | 914 m_pAESContext = FX_Alloc(uint8_t, 2048); |
| 915 } | 915 } |
| 916 return TRUE; | 916 return true; |
| 917 } | 917 } |
| 918 FX_BOOL CPDF_StandardCryptoHandler::DecryptStream(void* context, const uint8_t*
src_buf, FX_DWORD src_size, | 918 bool CPDF_StandardCryptoHandler::DecryptStream(void* context, const uint8_t* src
_buf, FX_DWORD src_size, |
| 919 CFX_BinaryBuf& dest_buf) | 919 CFX_BinaryBuf& dest_buf) |
| 920 { | 920 { |
| 921 return CryptStream(context, src_buf, src_size, dest_buf, FALSE); | 921 return CryptStream(context, src_buf, src_size, dest_buf, false); |
| 922 } | 922 } |
| 923 FX_BOOL CPDF_StandardCryptoHandler::DecryptFinish(void* context, CFX_BinaryBuf&
dest_buf) | 923 bool CPDF_StandardCryptoHandler::DecryptFinish(void* context, CFX_BinaryBuf& des
t_buf) |
| 924 { | 924 { |
| 925 return CryptFinish(context, dest_buf, FALSE); | 925 return CryptFinish(context, dest_buf, false); |
| 926 } | 926 } |
| 927 FX_DWORD CPDF_StandardCryptoHandler::EncryptGetSize(FX_DWORD objnum, FX_DWORD ve
rsion, const uint8_t* src_buf, FX_DWORD src_size) | 927 FX_DWORD CPDF_StandardCryptoHandler::EncryptGetSize(FX_DWORD objnum, FX_DWORD ve
rsion, const uint8_t* src_buf, FX_DWORD src_size) |
| 928 { | 928 { |
| 929 if (m_Cipher == FXCIPHER_AES) { | 929 if (m_Cipher == FXCIPHER_AES) { |
| 930 return src_size + 32; | 930 return src_size + 32; |
| 931 } | 931 } |
| 932 return src_size; | 932 return src_size; |
| 933 } | 933 } |
| 934 FX_BOOL CPDF_StandardCryptoHandler::EncryptContent(FX_DWORD objnum, FX_DWORD gen
num, const uint8_t* src_buf, FX_DWORD src_size, | 934 bool CPDF_StandardCryptoHandler::EncryptContent(FX_DWORD objnum, FX_DWORD gennum
, const uint8_t* src_buf, FX_DWORD src_size, |
| 935 uint8_t* dest_buf, FX_DWORD& dest_size) | 935 uint8_t* dest_buf, FX_DWORD& dest_size) |
| 936 { | 936 { |
| 937 CryptBlock(TRUE, objnum, gennum, src_buf, src_size, dest_buf, dest_size); | 937 CryptBlock(true, objnum, gennum, src_buf, src_size, dest_buf, dest_size); |
| 938 return TRUE; | 938 return true; |
| 939 } | 939 } |
| 940 void CPDF_CryptoHandler::Decrypt(FX_DWORD objnum, FX_DWORD gennum, CFX_ByteStrin
g& str) | 940 void CPDF_CryptoHandler::Decrypt(FX_DWORD objnum, FX_DWORD gennum, CFX_ByteStrin
g& str) |
| 941 { | 941 { |
| 942 CFX_BinaryBuf dest_buf; | 942 CFX_BinaryBuf dest_buf; |
| 943 void* context = DecryptStart(objnum, gennum); | 943 void* context = DecryptStart(objnum, gennum); |
| 944 DecryptStream(context, (const uint8_t*)str, str.GetLength(), dest_buf); | 944 DecryptStream(context, (const uint8_t*)str, str.GetLength(), dest_buf); |
| 945 DecryptFinish(context, dest_buf); | 945 DecryptFinish(context, dest_buf); |
| 946 str = dest_buf; | 946 str = dest_buf; |
| 947 } | 947 } |
| 948 CPDF_StandardCryptoHandler::CPDF_StandardCryptoHandler() | 948 CPDF_StandardCryptoHandler::CPDF_StandardCryptoHandler() |
| 949 { | 949 { |
| 950 m_pAESContext = NULL; | 950 m_pAESContext = NULL; |
| 951 m_Cipher = FXCIPHER_NONE; | 951 m_Cipher = FXCIPHER_NONE; |
| 952 m_KeyLen = 0; | 952 m_KeyLen = 0; |
| 953 } | 953 } |
| 954 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() | 954 CPDF_StandardCryptoHandler::~CPDF_StandardCryptoHandler() |
| 955 { | 955 { |
| 956 if (m_pAESContext) { | 956 if (m_pAESContext) { |
| 957 FX_Free(m_pAESContext); | 957 FX_Free(m_pAESContext); |
| 958 } | 958 } |
| 959 } | 959 } |
| OLD | NEW |