diff --git a/src/Common/Game/IW3/IW3.h b/src/Common/Game/IW3/IW3.h index 1a3bda98..a39ad3db 100644 --- a/src/Common/Game/IW3/IW3.h +++ b/src/Common/Game/IW3/IW3.h @@ -11,6 +11,33 @@ namespace IW3 { + + struct DB_AuthHash + { + char bytes[32]; + }; + + struct DB_AuthSignature + { + char bytes[256]; + }; + + struct DB_AuthSubHeader + { + char fastfileName[32]; + unsigned int reserved; + DB_AuthHash masterBlockHashes[244]; + }; + + struct DB_AuthHeader + { + char magic[8]; + unsigned int reserved; + DB_AuthHash subheaderHash; + DB_AuthSignature signedSubheaderHash; + DB_AuthSubHeader subheader; + }; + struct ScriptStringList { int count; diff --git a/src/ZoneCommon/Game/IW3/ZoneConstantsIW3.h b/src/ZoneCommon/Game/IW3/ZoneConstantsIW3.h index f6e5bdb8..c8749356 100644 --- a/src/ZoneCommon/Game/IW3/ZoneConstantsIW3.h +++ b/src/ZoneCommon/Game/IW3/ZoneConstantsIW3.h @@ -12,12 +12,29 @@ namespace IW3 ZoneConstants() = default; public: + static constexpr const char* MAGIC_SIGNED = "IWff0100"; static constexpr const char* MAGIC_UNSIGNED = "IWffu100"; static constexpr int ZONE_VERSION_PC = 5; static constexpr int ZONE_VERSION_XENON = 1; static_assert(std::char_traits::length(MAGIC_UNSIGNED) == sizeof(ZoneHeader::m_magic)); + static constexpr const char* MAGIC_AUTH_HEADER = "IWffs100"; + inline static const uint8_t RSA_PUBLIC_KEY_INFINITY_WARD[270] = { + 0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xCD, 0x96, 0x50, 0xC8, 0xB2, 0x4E, 0x10, 0xE4, 0x79, 0x05, 0x41, 0x6E, 0x9B, 0xEF, + 0xCE, 0x51, 0xC4, 0x2D, 0x9E, 0xC9, 0xD1, 0x84, 0x23, 0xF0, 0xAC, 0x22, 0x24, 0x18, 0xE9, 0x9D, 0x28, 0xCB, 0xAF, 0xB4, 0x7F, 0x60, 0xB3, + 0x67, 0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0x89, + 0x89, 0x91, 0x8F, 0x06, 0xBA, 0x3A, 0x37, 0x16, 0xC5, 0x7D, 0x89, 0xEF, 0xEF, 0x63, 0x4E, 0xF3, 0x89, 0x19, 0x06, 0x89, 0xF1, 0x7B, 0xDC, + 0xF5, 0x6E, 0x36, 0x0A, 0x8B, 0xC8, 0xC4, 0x5D, 0xC2, 0x7D, 0x13, 0x9D, 0x02, 0x03, 0x01, 0x00, 0x01, + }; + static constexpr size_t AUTHED_CHUNK_SIZE = 0x2000; static constexpr unsigned AUTHED_CHUNK_COUNT_PER_GROUP = 256; diff --git a/src/ZoneLoading/Game/IW3/ZoneLoaderFactoryIW3.cpp b/src/ZoneLoading/Game/IW3/ZoneLoaderFactoryIW3.cpp index 7400ac11..7f06f3f3 100644 --- a/src/ZoneLoading/Game/IW3/ZoneLoaderFactoryIW3.cpp +++ b/src/ZoneLoading/Game/IW3/ZoneLoaderFactoryIW3.cpp @@ -6,12 +6,22 @@ #include "Game/IW3/GameIW3.h" #include "Game/IW3/IW3.h" #include "Game/IW3/ZoneConstantsIW3.h" +#include "Loading/Processor/ProcessorAuthedBlocks.h" +#include "Loading/Processor/ProcessorCaptureData.h" #include "Loading/Processor/ProcessorInflate.h" #include "Loading/Steps/StepAddProcessor.h" #include "Loading/Steps/StepAllocXBlocks.h" #include "Loading/Steps/StepDumpData.h" +#include "Loading/Steps/StepLoadHash.h" +#include "Loading/Steps/StepLoadSignature.h" #include "Loading/Steps/StepLoadZoneContent.h" #include "Loading/Steps/StepLoadZoneSizes.h" +#include "Loading/Steps/StepRemoveProcessor.h" +#include "Loading/Steps/StepSkipBytes.h" +#include "Loading/Steps/StepVerifyFileName.h" +#include "Loading/Steps/StepVerifyHash.h" +#include "Loading/Steps/StepVerifyMagic.h" +#include "Loading/Steps/StepVerifySignature.h" #include "Utils/ClassUtils.h" #include "Utils/Endianness.h" #include "Utils/Logging/Log.h" @@ -42,6 +52,77 @@ namespace #undef XBLOCK_DEF } + + std::unique_ptr SetupRsa(const bool isOfficial) + { + if (isOfficial) + { + auto rsa = cryptography::CreateRsa(cryptography::HashingAlgorithm::RSA_HASH_SHA256, cryptography::RsaPaddingMode::RSA_PADDING_PSS); + + if (!rsa->SetKey(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD, sizeof(ZoneConstants::RSA_PUBLIC_KEY_INFINITY_WARD))) + { + con::error("Invalid public key for signature checking"); + return nullptr; + } + + return rsa; + } + else + { + assert(false); + + // TODO: Load custom RSA key here + return nullptr; + } + } + + void AddAuthHeaderSteps(const ZoneLoaderInspectionResult& inspectResult, ZoneLoader& zoneLoader, const std::string& fileName) + { + // Unsigned zones do not have an auth header + if (!inspectResult.m_is_signed) + return; + + // If file is signed setup a RSA instance. + auto rsa = SetupRsa(inspectResult.m_is_official); + + zoneLoader.AddLoadingStep(step::CreateStepVerifyMagic(ZoneConstants::MAGIC_AUTH_HEADER)); + zoneLoader.AddLoadingStep(step::CreateStepSkipBytes(4)); // Skip reserved + + auto subHeaderHash = step::CreateStepLoadHash(sizeof(DB_AuthHash::bytes), 1); + auto* subHeaderHashPtr = subHeaderHash.get(); + zoneLoader.AddLoadingStep(std::move(subHeaderHash)); + + auto subHeaderHashSignature = step::CreateStepLoadSignature(sizeof(DB_AuthSignature::bytes)); + auto* subHeaderHashSignaturePtr = subHeaderHashSignature.get(); + zoneLoader.AddLoadingStep(std::move(subHeaderHashSignature)); + + zoneLoader.AddLoadingStep(step::CreateStepVerifySignature(std::move(rsa), subHeaderHashSignaturePtr, subHeaderHashPtr)); + + auto subHeaderCapture = processor::CreateProcessorCaptureData(sizeof(DB_AuthSubHeader)); + auto* subHeaderCapturePtr = subHeaderCapture.get(); + zoneLoader.AddLoadingStep(step::CreateStepAddProcessor(std::move(subHeaderCapture))); + + zoneLoader.AddLoadingStep(step::CreateStepVerifyFileName(fileName, sizeof(DB_AuthSubHeader::fastfileName))); + zoneLoader.AddLoadingStep(step::CreateStepSkipBytes(4)); // Skip reserved + + auto masterBlockHashes = + step::CreateStepLoadHash(sizeof(DB_AuthHash::bytes), static_cast(std::extent_v)); + auto* masterBlockHashesPtr = masterBlockHashes.get(); + zoneLoader.AddLoadingStep(std::move(masterBlockHashes)); + + zoneLoader.AddLoadingStep(step::CreateStepVerifyHash(cryptography::CreateSha256(), 0, subHeaderHashPtr, subHeaderCapturePtr)); + zoneLoader.AddLoadingStep(step::CreateStepRemoveProcessor(subHeaderCapturePtr)); + + // Skip the rest of the first chunk + zoneLoader.AddLoadingStep(step::CreateStepSkipBytes(ZoneConstants::AUTHED_CHUNK_SIZE - sizeof(DB_AuthHeader))); + + zoneLoader.AddLoadingStep(step::CreateStepAddProcessor( + processor::CreateProcessorAuthedBlocks(ZoneConstants::AUTHED_CHUNK_COUNT_PER_GROUP, + ZoneConstants::AUTHED_CHUNK_SIZE, + static_cast(std::extent_v), + cryptography::CreateSha256(), + masterBlockHashesPtr))); + } } // namespace std::optional ZoneLoaderFactory::InspectZoneHeader(const ZoneHeader& header) const @@ -70,11 +151,23 @@ std::optional ZoneLoaderFactory::InspectZoneHeader(c .m_endianness = GameEndianness::BE, .m_word_size = GameWordSize::ARCH_32, .m_platform = GamePlatform::XBOX, - .m_is_official = false, + .m_is_official = true, .m_is_signed = false, .m_is_encrypted = false, }; } + if (!memcmp(header.m_magic, ZoneConstants::MAGIC_SIGNED, std::char_traits::length(ZoneConstants::MAGIC_SIGNED))) + { + return ZoneLoaderInspectionResult{ + .m_game_id = GameId::IW3, + .m_endianness = GameEndianness::BE, + .m_word_size = GameWordSize::ARCH_32, + .m_platform = GamePlatform::XBOX, + .m_is_official = true, + .m_is_signed = true, + .m_is_encrypted = false, + }; + } } return std::nullopt; @@ -99,6 +192,9 @@ std::unique_ptr ZoneLoaderFactory::CreateLoaderForHeader(const ZoneH SetupBlock(*zoneLoader); + // Add steps for loading the auth header which also contain the signature of the zone if it is signed. + AddAuthHeaderSteps(*inspectResult, *zoneLoader, fileName); + zoneLoader->AddLoadingStep(step::CreateStepAddProcessor(processor::CreateProcessorInflate(ZoneConstants::AUTHED_CHUNK_SIZE))); if (inspectResult->m_endianness == GameEndianness::LE) diff --git a/src/ZoneLoading/Loading/Processor/ProcessorInflate.cpp b/src/ZoneLoading/Loading/Processor/ProcessorInflate.cpp index 3cca74f6..8137e5aa 100644 --- a/src/ZoneLoading/Loading/Processor/ProcessorInflate.cpp +++ b/src/ZoneLoading/Loading/Processor/ProcessorInflate.cpp @@ -61,6 +61,8 @@ namespace if (ret < 0) throw InvalidCompressionException(); + if (ret == Z_STREAM_END) + break; } return length - m_stream.avail_out;