Skip to content
Permalink
Browse files

IOS: Put common key handles in an array

  • Loading branch information...
JosJuice committed Jul 14, 2019
1 parent da1fbbc commit 4ee73dbad35aa17de48787711a2e118d04558867
@@ -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);
}
@@ -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
@@ -198,12 +198,11 @@ 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(),
&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<u8>& tmd_bytes,
@@ -165,6 +165,9 @@ class IOSC final
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,
@@ -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.

0 comments on commit 4ee73db

Please sign in to comment.
You can’t perform that action at this time.