From a081931e2b00e3aadff2050f7d253e17c124c69f Mon Sep 17 00:00:00 2001 From: Artem Strygin Date: Tue, 3 Oct 2017 21:51:18 +0300 Subject: Move initialization of CPDF_CryptoHandler into CPDF_SytnaxParser initialization. Change-Id: I70d04f38767f2c17c41407fc9b4fc25519ba32f4 Reviewed-on: https://pdfium-review.googlesource.com/15330 Commit-Queue: Art Snake Reviewed-by: dsinclair --- core/fpdfapi/parser/cpdf_crypto_handler.cpp | 68 +++++++---------------------- 1 file changed, 16 insertions(+), 52 deletions(-) (limited to 'core/fpdfapi/parser/cpdf_crypto_handler.cpp') diff --git a/core/fpdfapi/parser/cpdf_crypto_handler.cpp b/core/fpdfapi/parser/cpdf_crypto_handler.cpp index 441ca8000d..e8f0265ae4 100644 --- a/core/fpdfapi/parser/cpdf_crypto_handler.cpp +++ b/core/fpdfapi/parser/cpdf_crypto_handler.cpp @@ -8,6 +8,7 @@ #include +#include #include #include @@ -278,56 +279,6 @@ uint32_t CPDF_CryptoHandler::DecryptGetSize(uint32_t src_size) { return m_Cipher == FXCIPHER_AES ? src_size - 16 : src_size; } -bool CPDF_CryptoHandler::Init(CPDF_Dictionary* pEncryptDict, - CPDF_SecurityHandler* pSecurityHandler) { - const uint8_t* key; - if (!pSecurityHandler->GetCryptInfo(m_Cipher, key, m_KeyLen)) - return false; - - if (m_KeyLen > 32 || m_KeyLen < 0) - return false; - - if (m_Cipher != FXCIPHER_NONE) - memcpy(m_EncryptKey, key, m_KeyLen); - - if (m_Cipher == FXCIPHER_AES) - m_pAESContext.reset(FX_Alloc(CRYPT_aes_context, 1)); - - return true; -} - -bool CPDF_CryptoHandler::Init(int cipher, const uint8_t* key, int keylen) { - if (cipher == FXCIPHER_AES) { - switch (keylen) { - case 16: - case 24: - case 32: - break; - default: - return false; - } - } else if (cipher == FXCIPHER_AES2) { - if (keylen != 32) { - return false; - } - } else if (cipher == FXCIPHER_RC4) { - if (keylen < 5 || keylen > 16) { - return false; - } - } else { - if (keylen > 32) { - keylen = 32; - } - } - m_Cipher = cipher; - m_KeyLen = keylen; - memcpy(m_EncryptKey, key, keylen); - if (m_Cipher == FXCIPHER_AES) - m_pAESContext.reset(FX_Alloc(CRYPT_aes_context, 1)); - - return true; -} - bool CPDF_CryptoHandler::IsCipherAES() const { return m_Cipher == FXCIPHER_AES; } @@ -444,8 +395,21 @@ bool CPDF_CryptoHandler::EncryptContent(uint32_t objnum, return true; } -CPDF_CryptoHandler::CPDF_CryptoHandler() - : m_KeyLen(0), m_Cipher(FXCIPHER_NONE) {} +CPDF_CryptoHandler::CPDF_CryptoHandler(int cipher, + const uint8_t* key, + int keylen) + : m_KeyLen(std::min(keylen, 32)), m_Cipher(cipher) { + ASSERT(cipher != FXCIPHER_AES || keylen == 16 || keylen == 24 || + keylen == 32); + ASSERT(cipher != FXCIPHER_AES2 || keylen == 32); + ASSERT(cipher != FXCIPHER_RC4 || (keylen >= 5 && keylen <= 16)); + + if (m_Cipher != FXCIPHER_NONE) + memcpy(m_EncryptKey, key, m_KeyLen); + + if (m_Cipher == FXCIPHER_AES) + m_pAESContext.reset(FX_Alloc(CRYPT_aes_context, 1)); +} CPDF_CryptoHandler::~CPDF_CryptoHandler() {} -- cgit v1.2.3