From 4ee73dbad35aa17de48787711a2e118d04558867 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 14 Jul 2019 13:15:53 +0200 Subject: [PATCH] IOS: Put common key handles in an array --- Source/Core/Core/IOS/ES/ES.cpp | 5 ++--- Source/Core/Core/IOS/ES/Formats.cpp | 10 +++++----- Source/Core/Core/IOS/ES/TitleManagement.cpp | 7 +++---- Source/Core/Core/IOS/IOSC.h | 3 +++ Source/Core/DiscIO/VolumeVerifier.cpp | 2 +- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Source/Core/Core/IOS/ES/ES.cpp b/Source/Core/Core/IOS/ES/ES.cpp index 8d1f9a9898..1503bde01c 100644 --- a/Source/Core/Core/IOS/ES/ES.cpp +++ b/Source/Core/Core/IOS/ES/ES.cpp @@ -767,11 +767,10 @@ ReturnCode ES::SetUpStreamKey(const u32 uid, const u8* ticket_view, const IOS::E return ret; const u8 index = ticket_bytes[offsetof(IOS::ES::Ticket, common_key_index)]; - if (index > 1) + if (index >= IOSC::COMMON_KEY_HANDLES.size()) return ES_INVALID_TICKET; - auto common_key_handle = index == 0 ? IOSC::HANDLE_COMMON_KEY : IOSC::HANDLE_NEW_COMMON_KEY; - return m_ios.GetIOSC().ImportSecretKey(*handle, common_key_handle, iv.data(), + return m_ios.GetIOSC().ImportSecretKey(*handle, IOSC::COMMON_KEY_HANDLES[index], iv.data(), &ticket_bytes[offsetof(IOS::ES::Ticket, title_key)], PID_ES); } diff --git a/Source/Core/Core/IOS/ES/Formats.cpp b/Source/Core/Core/IOS/ES/Formats.cpp index d8a5af7a10..0b3431aec8 100644 --- a/Source/Core/Core/IOS/ES/Formats.cpp +++ b/Source/Core/Core/IOS/ES/Formats.cpp @@ -452,14 +452,14 @@ std::array TicketReader::GetTitleKey(const HLE::IOSC& iosc) const u8 iv[16] = {}; std::copy_n(&m_bytes[offsetof(Ticket, title_id)], sizeof(Ticket::title_id), iv); - const u8 index = m_bytes.at(offsetof(Ticket, common_key_index)); - auto common_key_handle = - index != 1 ? HLE::IOSC::HANDLE_COMMON_KEY : HLE::IOSC::HANDLE_NEW_COMMON_KEY; - if (index != 0 && index != 1) + u8 index = m_bytes.at(offsetof(Ticket, common_key_index)); + if (index >= HLE::IOSC::COMMON_KEY_HANDLES.size()) { WARN_LOG(IOS_ES, "Bad common key index for title %016" PRIx64 ": %u -- using common key 0", GetTitleId(), index); + index = 0; } + auto common_key_handle = HLE::IOSC::COMMON_KEY_HANDLES[index]; std::array key; iosc.Decrypt(common_key_handle, iv, &m_bytes[offsetof(Ticket, title_key)], 16, key.data(), @@ -537,7 +537,7 @@ void TicketReader::FixCommonKeyIndex() { u8& index = m_bytes[offsetof(Ticket, common_key_index)]; // Assume the ticket is using the normal common key if it's an invalid value. - index = index <= 1 ? index : 0; + index = index < HLE::IOSC::COMMON_KEY_HANDLES.size() ? index : 0; } struct SharedContentMap::Entry diff --git a/Source/Core/Core/IOS/ES/TitleManagement.cpp b/Source/Core/Core/IOS/ES/TitleManagement.cpp index d415e44f2a..612f04d9ab 100644 --- a/Source/Core/Core/IOS/ES/TitleManagement.cpp +++ b/Source/Core/Core/IOS/ES/TitleManagement.cpp @@ -198,12 +198,11 @@ static ReturnCode InitTitleImportKey(const std::vector& ticket_bytes, IOSC& std::array iv{}; std::copy_n(&ticket_bytes[offsetof(IOS::ES::Ticket, title_id)], sizeof(u64), iv.begin()); const u8 index = ticket_bytes[offsetof(IOS::ES::Ticket, common_key_index)]; - if (index > 1) + if (index >= IOSC::COMMON_KEY_HANDLES.size()) return ES_INVALID_TICKET; - return iosc.ImportSecretKey( - *handle, index == 0 ? IOSC::HANDLE_COMMON_KEY : IOSC::HANDLE_NEW_COMMON_KEY, iv.data(), - &ticket_bytes[offsetof(IOS::ES::Ticket, title_key)], PID_ES); + return iosc.ImportSecretKey(*handle, IOSC::COMMON_KEY_HANDLES[index], iv.data(), + &ticket_bytes[offsetof(IOS::ES::Ticket, title_key)], PID_ES); } ReturnCode ES::ImportTitleInit(Context& context, const std::vector& tmd_bytes, diff --git a/Source/Core/Core/IOS/IOSC.h b/Source/Core/Core/IOS/IOSC.h index 8155a5f0a7..e1fa3a76ba 100644 --- a/Source/Core/Core/IOS/IOSC.h +++ b/Source/Core/Core/IOS/IOSC.h @@ -165,6 +165,9 @@ public: HANDLE_ROOT_KEY = 0xfffffff, }; + static constexpr std::array COMMON_KEY_HANDLES = {HANDLE_COMMON_KEY, + HANDLE_NEW_COMMON_KEY}; + enum ObjectType : u8 { TYPE_SECRET_KEY = 0, diff --git a/Source/Core/DiscIO/VolumeVerifier.cpp b/Source/Core/DiscIO/VolumeVerifier.cpp index 55da0d27c6..22a732a8ab 100644 --- a/Source/Core/DiscIO/VolumeVerifier.cpp +++ b/Source/Core/DiscIO/VolumeVerifier.cpp @@ -609,7 +609,7 @@ void VolumeVerifier::CheckMisc() { const u8 common_key = ticket.GetCommonKeyIndex(); - if (common_key > 1) + if (common_key > IOS::HLE::IOSC::COMMON_KEY_HANDLES.size()) { // Many fakesigned WADs have the common key index set to a (random?) bogus value. // For WADs, Dolphin will detect this and use common key 0 instead, making this low severity.