Use more size_t in CPDF_CryptoHandler. The key length should not be negative. Change-Id: Ifbc9742d149361de003fc9a509377af0ca0a0715 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/63210 Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_crypto_handler.cpp b/core/fpdfapi/parser/cpdf_crypto_handler.cpp index 60c8a65..3b3e71c 100644 --- a/core/fpdfapi/parser/cpdf_crypto_handler.cpp +++ b/core/fpdfapi/parser/cpdf_crypto_handler.cpp
@@ -63,7 +63,7 @@ } CRYPT_MD5Generate( key1, m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen + 5, realkey); - realkeylen = std::min<size_t>(m_KeyLen + 5, sizeof(realkey)); + realkeylen = std::min(m_KeyLen + 5, sizeof(realkey)); } if (m_Cipher == FXCIPHER_AES) { CRYPT_AESSetKey(m_pAESContext.get(), @@ -130,16 +130,14 @@ uint8_t key1[48]; PopulateKey(objnum, gennum, key1); - if (m_Cipher == FXCIPHER_AES) { + if (m_Cipher == FXCIPHER_AES) memcpy(key1 + m_KeyLen + 5, "sAlT", 4); - } + uint8_t realkey[16]; CRYPT_MD5Generate( key1, m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen + 5, realkey); - int realkeylen = m_KeyLen + 5; - if (realkeylen > 16) { - realkeylen = 16; - } + size_t realkeylen = std::min(m_KeyLen + 5, sizeof(realkey)); + if (m_Cipher == FXCIPHER_AES) { AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); pContext->m_bIV = true; @@ -382,8 +380,8 @@ CPDF_CryptoHandler::CPDF_CryptoHandler(int cipher, const uint8_t* key, - int keylen) - : m_KeyLen(std::min(keylen, 32)), m_Cipher(cipher) { + size_t keylen) + : m_KeyLen(std::min<size_t>(keylen, 32)), m_Cipher(cipher) { ASSERT(cipher != FXCIPHER_AES || keylen == 16 || keylen == 24 || keylen == 32); ASSERT(cipher != FXCIPHER_AES2 || keylen == 32); @@ -396,7 +394,7 @@ m_pAESContext.reset(FX_Alloc(CRYPT_aes_context, 1)); } -CPDF_CryptoHandler::~CPDF_CryptoHandler() {} +CPDF_CryptoHandler::~CPDF_CryptoHandler() = default; void CPDF_CryptoHandler::PopulateKey(uint32_t objnum, uint32_t gennum,
diff --git a/core/fpdfapi/parser/cpdf_crypto_handler.h b/core/fpdfapi/parser/cpdf_crypto_handler.h index 74fba4d..edfba97 100644 --- a/core/fpdfapi/parser/cpdf_crypto_handler.h +++ b/core/fpdfapi/parser/cpdf_crypto_handler.h
@@ -22,7 +22,7 @@ class CPDF_CryptoHandler { public: - CPDF_CryptoHandler(int cipher, const uint8_t* key, int keylen); + CPDF_CryptoHandler(int cipher, const uint8_t* key, size_t keylen); ~CPDF_CryptoHandler(); static bool IsSignatureDictionary(const CPDF_Dictionary* dictionary); @@ -60,8 +60,8 @@ bool bEncrypt); bool CryptFinish(void* context, CFX_BinaryBuf& dest_buf, bool bEncrypt); - int m_KeyLen; - int m_Cipher; + const size_t m_KeyLen; + const int m_Cipher; std::unique_ptr<CRYPT_aes_context, FxFreeDeleter> m_pAESContext; uint8_t m_EncryptKey[32]; };