IOS: Put common key handles in an array

This commit is contained in:
JosJuice 2019-07-14 13:15:53 +02:00
parent da1fbbc5d5
commit 4ee73dbad3
5 changed files with 14 additions and 13 deletions

View File

@ -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);
}

View File

@ -452,14 +452,14 @@ std::array<u8, 16> 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<u8, 16> 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

View File

@ -198,11 +198,10 @@ static ReturnCode InitTitleImportKey(const std::vector<u8>& ticket_bytes, IOSC&
std::array<u8, 16> 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(),
return iosc.ImportSecretKey(*handle, IOSC::COMMON_KEY_HANDLES[index], iv.data(),
&ticket_bytes[offsetof(IOS::ES::Ticket, title_key)], PID_ES);
}

View File

@ -165,6 +165,9 @@ public:
HANDLE_ROOT_KEY = 0xfffffff,
};
static constexpr std::array<DefaultHandle, 2> COMMON_KEY_HANDLES = {HANDLE_COMMON_KEY,
HANDLE_NEW_COMMON_KEY};
enum ObjectType : u8
{
TYPE_SECRET_KEY = 0,

View File

@ -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.