From bf1f70db0a4b9af8c4515a91519dfb7cd89b7d58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Thu, 9 Feb 2017 14:07:36 +0100 Subject: Move the ticket code to ESFormats This moves some parsing code for tickets and ticket views to ESFormats instead of duplicating it over DiscIO and Core. --- Source/Core/DiscIO/NANDContentLoader.cpp | 100 ++++++------------------------- 1 file changed, 17 insertions(+), 83 deletions(-) (limited to 'Source/Core/DiscIO/NANDContentLoader.cpp') diff --git a/Source/Core/DiscIO/NANDContentLoader.cpp b/Source/Core/DiscIO/NANDContentLoader.cpp index fc1f600aa1..dcd875388a 100644 --- a/Source/Core/DiscIO/NANDContentLoader.cpp +++ b/Source/Core/DiscIO/NANDContentLoader.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -29,50 +28,6 @@ namespace DiscIO { -namespace -{ -// Strips the signature part of a ticket, which has variable size based on -// signature type. Returns a new vector which has only the ticket structure -// itself. -std::vector SignedTicketToTicket(const std::vector& signed_ticket) -{ - u32 signature_type = Common::swap32(signed_ticket.data()); - u32 entry_offset; - if (signature_type == 0x10000) // RSA4096 - { - entry_offset = 576; - } - else if (signature_type == 0x10001) // RSA2048 - { - entry_offset = 320; - } - else if (signature_type == 0x10002) // ECDSA - { - entry_offset = 128; - } - else - { - ERROR_LOG(DISCIO, "Invalid ticket signature type: %08x", signature_type); - return std::vector(); - } - - std::vector ticket(signed_ticket.size() - entry_offset); - std::copy(signed_ticket.begin() + entry_offset, signed_ticket.end(), ticket.begin()); - return ticket; -} - -std::vector AESDecode(const u8* key, u8* iv, const u8* src, u32 size) -{ - mbedtls_aes_context aes_ctx; - std::vector buffer(size); - - mbedtls_aes_setkey_dec(&aes_ctx, key, 128); - mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, size, iv, src, buffer.data()); - - return buffer; -} -} - CNANDContentData::~CNANDContentData() = default; CSharedContent::CSharedContent(Common::FromWhichRoot root) : m_root(root) @@ -222,13 +177,11 @@ bool CNANDContentLoader::Initialize(const std::string& name) WiiWAD wad(name); std::vector data_app; std::vector tmd; - std::vector decrypted_title_key; if (wad.IsValid()) { m_IsWAD = true; - m_Ticket = wad.GetTicket(); - decrypted_title_key = GetKeyFromTicket(m_Ticket); + m_ticket = wad.GetTicket(); tmd = wad.GetTMD(); data_app = wad.GetDataApp(); } @@ -265,12 +218,14 @@ bool CNANDContentLoader::Initialize(const std::string& name) if (m_Country == 2) // SYSMENU m_Country = GetSysMenuRegion(m_TitleVersion); - InitializeContentEntries(tmd, decrypted_title_key, data_app); + if (!m_IsWAD) + m_ticket = FindSignedTicket(m_TitleID); + + InitializeContentEntries(tmd, data_app); return true; } void CNANDContentLoader::InitializeContentEntries(const std::vector& tmd, - const std::vector& decrypted_title_key, const std::vector& data_app) { m_Content.resize(m_NumEntries); @@ -305,8 +260,8 @@ void CNANDContentLoader::InitializeContentEntries(const std::vector& tmd, iv.fill(0); std::copy(&tmd[entry_offset + 0x01E8], &tmd[entry_offset + 0x01E8 + 2], iv.begin()); - content.m_Data = std::make_unique(AESDecode( - decrypted_title_key.data(), iv.data(), &data_app[data_app_offset], rounded_size)); + content.m_Data = std::make_unique(ES::AESDecode( + m_ticket.GetTitleKey().data(), iv.data(), &data_app[data_app_offset], rounded_size)); data_app_offset += rounded_size; continue; @@ -533,14 +488,14 @@ u64 CNANDContentManager::Install_WiiWAD(const std::string& filename) return title_id; } -bool AddTicket(const std::vector& signed_ticket) +bool AddTicket(const ES::TicketReader& signed_ticket) { - std::vector ticket = SignedTicketToTicket(signed_ticket); - if (ticket.empty()) + if (!signed_ticket.IsValid()) { return false; } - u64 title_id = Common::swap64(ticket.data() + 0x9c); + + u64 title_id = signed_ticket.GetTitleId(); std::string ticket_filename = Common::GetTicketFileName(title_id, Common::FROM_CONFIGURED_ROOT); File::CreateFullPath(ticket_filename); @@ -549,46 +504,25 @@ bool AddTicket(const std::vector& signed_ticket) if (!ticket_file) return false; - return ticket_file.WriteBytes(signed_ticket.data(), signed_ticket.size()); + const std::vector& raw_ticket = signed_ticket.GetRawTicket(); + return ticket_file.WriteBytes(raw_ticket.data(), raw_ticket.size()); } -std::vector FindSignedTicket(u64 title_id) +ES::TicketReader FindSignedTicket(u64 title_id) { std::string ticket_filename = Common::GetTicketFileName(title_id, Common::FROM_CONFIGURED_ROOT); File::IOFile ticket_file(ticket_filename, "rb"); if (!ticket_file) { - return std::vector(); + return ES::TicketReader{}; } std::vector signed_ticket(ticket_file.GetSize()); if (!ticket_file.ReadBytes(signed_ticket.data(), signed_ticket.size())) { - return std::vector(); - } - - return signed_ticket; -} - -std::vector FindTicket(u64 title_id) -{ - std::vector signed_ticket = FindSignedTicket(title_id); - if (signed_ticket.empty()) - { - return std::vector(); + return ES::TicketReader{}; } - return SignedTicketToTicket(signed_ticket); -} - -std::vector GetKeyFromTicket(const std::vector& signed_ticket) -{ - const u8 common_key[16] = {0xeb, 0xe4, 0x2a, 0x22, 0x5e, 0x85, 0x93, 0xe4, - 0x48, 0xd9, 0xc5, 0x45, 0x73, 0x81, 0xaa, 0xf7}; - u8 iv[16] = {}; - - std::copy(&signed_ticket[0x01DC], &signed_ticket[0x01DC + 8], iv); - return AESDecode(common_key, iv, &signed_ticket[0x01BF], 16); + return ES::TicketReader{std::move(signed_ticket)}; } - } // namespace end -- cgit v1.2.3 From c1a139e8ace17e45e8ca6977b6f6b19a1a4195f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sat, 11 Feb 2017 08:57:47 +0100 Subject: Use ESFormats for TMDs We already have a TMDReader, so let's actually use it. And move ESFormats to IOS::ES, since it's definitely part of IOS. This adds a DiscIO dependency on Core which will be fixed in a follow-up PR. --- Source/Core/DiscIO/NANDContentLoader.cpp | 148 ++++++++++++------------------- 1 file changed, 57 insertions(+), 91 deletions(-) (limited to 'Source/Core/DiscIO/NANDContentLoader.cpp') diff --git a/Source/Core/DiscIO/NANDContentLoader.cpp b/Source/Core/DiscIO/NANDContentLoader.cpp index dcd875388a..0344fe1c46 100644 --- a/Source/Core/DiscIO/NANDContentLoader.cpp +++ b/Source/Core/DiscIO/NANDContentLoader.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -146,7 +147,6 @@ bool CNANDContentDataBuffer::GetRange(u32 start, u32 size, u8* buffer) } CNANDContentLoader::CNANDContentLoader(const std::string& content_name) - : m_Valid(false), m_IsWAD(false), m_TitleID(-1), m_IosVersion(0x09), m_BootIndex(-1) { m_Valid = Initialize(content_name); } @@ -159,7 +159,7 @@ const SNANDContent* CNANDContentLoader::GetContentByIndex(int index) const { for (auto& Content : m_Content) { - if (Content.m_Index == index) + if (Content.m_metadata.index == index) { return &Content; } @@ -176,13 +176,12 @@ bool CNANDContentLoader::Initialize(const std::string& name) WiiWAD wad(name); std::vector data_app; - std::vector tmd; if (wad.IsValid()) { m_IsWAD = true; m_ticket = wad.GetTicket(); - tmd = wad.GetTMD(); + m_tmd = wad.GetTMD(); data_app = wad.GetDataApp(); } else @@ -201,94 +200,64 @@ bool CNANDContentLoader::Initialize(const std::string& name) return false; } - tmd.resize(static_cast(File::GetSize(tmd_filename))); - tmd_file.ReadBytes(tmd.data(), tmd.size()); - } - - std::copy(&tmd[0], &tmd[TMD_HEADER_SIZE], m_TMDHeader); - std::copy(&tmd[0x180], &tmd[0x180 + TMD_VIEW_SIZE], m_TMDView); - - m_TitleVersion = Common::swap16(&tmd[0x01DC]); - m_NumEntries = Common::swap16(&tmd[0x01DE]); - m_BootIndex = Common::swap16(&tmd[0x01E0]); - m_TitleID = Common::swap64(&tmd[0x018C]); - m_IosVersion = Common::swap16(&tmd[0x018A]); - m_Country = static_cast(m_TitleID & 0xFF); + std::vector bytes(File::GetSize(tmd_filename)); + tmd_file.ReadBytes(bytes.data(), bytes.size()); + m_tmd.SetBytes(std::move(bytes)); - if (m_Country == 2) // SYSMENU - m_Country = GetSysMenuRegion(m_TitleVersion); - - if (!m_IsWAD) - m_ticket = FindSignedTicket(m_TitleID); + m_ticket = FindSignedTicket(m_tmd.GetTitleId()); + } - InitializeContentEntries(tmd, data_app); + InitializeContentEntries(data_app); return true; } -void CNANDContentLoader::InitializeContentEntries(const std::vector& tmd, - const std::vector& data_app) +void CNANDContentLoader::InitializeContentEntries(const std::vector& data_app) { - m_Content.resize(m_NumEntries); + if (!m_ticket.IsValid()) + { + ERROR_LOG(IOS_ES, "No valid ticket for title %016" PRIx64, m_tmd.GetTitleId()); + return; + } - std::array iv; - u32 data_app_offset = 0; + const std::vector contents = m_tmd.GetContents(); + m_Content.resize(contents.size()); + u32 data_app_offset = 0; + const std::vector title_key = m_ticket.GetTitleKey(); CSharedContent shared_content{Common::FromWhichRoot::FROM_SESSION_ROOT}; - for (u32 i = 0; i < m_NumEntries; i++) + for (size_t i = 0; i < contents.size(); ++i) { - const u32 entry_offset = 0x24 * i; - - SNANDContent& content = m_Content[i]; - content.m_ContentID = Common::swap32(&tmd[entry_offset + 0x01E4]); - content.m_Index = Common::swap16(&tmd[entry_offset + 0x01E8]); - content.m_Type = Common::swap16(&tmd[entry_offset + 0x01EA]); - content.m_Size = static_cast(Common::swap64(&tmd[entry_offset + 0x01EC])); - - const auto header_begin = std::next(tmd.begin(), entry_offset + 0x01E4); - const auto header_end = std::next(header_begin, ArraySize(content.m_Header)); - std::copy(header_begin, header_end, content.m_Header); - - const auto hash_begin = std::next(tmd.begin(), entry_offset + 0x01F4); - const auto hash_end = std::next(hash_begin, ArraySize(content.m_SHA1Hash)); - std::copy(hash_begin, hash_end, content.m_SHA1Hash); + const auto& content = contents.at(i); if (m_IsWAD) { - u32 rounded_size = Common::AlignUp(content.m_Size, 0x40); - - iv.fill(0); - std::copy(&tmd[entry_offset + 0x01E8], &tmd[entry_offset + 0x01E8 + 2], iv.begin()); + // The content index is used as IV (2 bytes); the remaining 14 bytes are zeroes. + std::array iv{}; + iv[0] = static_cast(content.index >> 8) & 0xFF; + iv[1] = static_cast(content.index) & 0xFF; - content.m_Data = std::make_unique(ES::AESDecode( - m_ticket.GetTitleKey().data(), iv.data(), &data_app[data_app_offset], rounded_size)); + u32 rounded_size = Common::AlignUp(static_cast(content.size), 0x40); + m_Content[i].m_Data = std::make_unique(IOS::ES::AESDecode( + title_key.data(), iv.data(), &data_app[data_app_offset], rounded_size)); data_app_offset += rounded_size; - continue; } - - std::string filename; - if (content.m_Type & 0x8000) // shared app - filename = shared_content.GetFilenameFromSHA1(content.m_SHA1Hash); else - filename = StringFromFormat("%s/%08x.app", m_Path.c_str(), content.m_ContentID); + { + std::string filename; + if (content.type & 0x8000) // shared app + filename = shared_content.GetFilenameFromSHA1(content.sha1.data()); + else + filename = StringFromFormat("%s/%08x.app", m_Path.c_str(), content.id); - content.m_Data = std::make_unique(filename); + m_Content[i].m_Data = std::make_unique(filename); + } - // Be graceful about incorrect TMDs. - if (File::Exists(filename)) - content.m_Size = static_cast(File::GetSize(filename)); + m_Content[i].m_metadata = std::move(content); } } -DiscIO::Region CNANDContentLoader::GetRegion() const -{ - if (!IsValid()) - return DiscIO::Region::UNKNOWN_REGION; - - return RegionSwitchWii(m_Country); -} - CNANDContentManager::~CNANDContentManager() { } @@ -327,18 +296,18 @@ void CNANDContentManager::ClearCache() void CNANDContentLoader::RemoveTitle() const { - INFO_LOG(DISCIO, "RemoveTitle %08x/%08x", (u32)(m_TitleID >> 32), (u32)m_TitleID); + const u64 title_id = m_tmd.GetTitleId(); + INFO_LOG(DISCIO, "RemoveTitle %08x/%08x", (u32)(title_id >> 32), (u32)title_id); if (IsValid()) { // remove TMD? - for (u32 i = 0; i < m_NumEntries; i++) + for (const auto& content : m_Content) { - if (!(m_Content[i].m_Type & 0x8000)) // skip shared apps + if (!(content.m_metadata.type & 0x8000)) // skip shared apps { - std::string filename = - StringFromFormat("%s/%08x.app", m_Path.c_str(), m_Content[i].m_ContentID); - INFO_LOG(DISCIO, "Delete %s", filename.c_str()); - File::Delete(filename); + std::string path = StringFromFormat("%s/%08x.app", m_Path.c_str(), content.m_metadata.id); + INFO_LOG(DISCIO, "Delete %s", path.c_str()); + File::Delete(path); } } CNANDContentManager::Access().ClearCache(); // deletes 'this' @@ -425,7 +394,7 @@ u64 CNANDContentManager::Install_WiiWAD(const std::string& filename) if (content_loader.IsValid() == false) return 0; - u64 title_id = content_loader.GetTitleID(); + const u64 title_id = content_loader.GetTMD().GetTitleId(); // copy WAD's TMD header and contents to content directory @@ -440,20 +409,17 @@ u64 CNANDContentManager::Install_WiiWAD(const std::string& filename) return 0; } - tmd_file.WriteBytes(content_loader.GetTMDHeader(), CNANDContentLoader::TMD_HEADER_SIZE); + const auto& raw_tmd = content_loader.GetTMD().GetRawTMD(); + tmd_file.WriteBytes(raw_tmd.data(), raw_tmd.size()); CSharedContent shared_content{Common::FromWhichRoot::FROM_CONFIGURED_ROOT}; - for (u32 i = 0; i < content_loader.GetContentSize(); i++) + for (const auto& content : content_loader.GetContent()) { - const SNANDContent& content = content_loader.GetContent()[i]; - - tmd_file.WriteBytes(content.m_Header, CNANDContentLoader::CONTENT_HEADER_SIZE); - std::string app_filename; - if (content.m_Type & 0x8000) // shared - app_filename = shared_content.AddSharedContent(content.m_SHA1Hash); + if (content.m_metadata.type & 0x8000) // shared + app_filename = shared_content.AddSharedContent(content.m_metadata.sha1.data()); else - app_filename = StringFromFormat("%s%08x.app", content_path.c_str(), content.m_ContentID); + app_filename = StringFromFormat("%s%08x.app", content_path.c_str(), content.m_metadata.id); if (!File::Exists(app_filename)) { @@ -465,7 +431,7 @@ u64 CNANDContentManager::Install_WiiWAD(const std::string& filename) return 0; } - app_file.WriteBytes(content.m_Data->Get().data(), content.m_Size); + app_file.WriteBytes(content.m_Data->Get().data(), content.m_metadata.size); } else { @@ -488,7 +454,7 @@ u64 CNANDContentManager::Install_WiiWAD(const std::string& filename) return title_id; } -bool AddTicket(const ES::TicketReader& signed_ticket) +bool AddTicket(const IOS::ES::TicketReader& signed_ticket) { if (!signed_ticket.IsValid()) { @@ -508,21 +474,21 @@ bool AddTicket(const ES::TicketReader& signed_ticket) return ticket_file.WriteBytes(raw_ticket.data(), raw_ticket.size()); } -ES::TicketReader FindSignedTicket(u64 title_id) +IOS::ES::TicketReader FindSignedTicket(u64 title_id) { std::string ticket_filename = Common::GetTicketFileName(title_id, Common::FROM_CONFIGURED_ROOT); File::IOFile ticket_file(ticket_filename, "rb"); if (!ticket_file) { - return ES::TicketReader{}; + return IOS::ES::TicketReader{}; } std::vector signed_ticket(ticket_file.GetSize()); if (!ticket_file.ReadBytes(signed_ticket.data(), signed_ticket.size())) { - return ES::TicketReader{}; + return IOS::ES::TicketReader{}; } - return ES::TicketReader{std::move(signed_ticket)}; + return IOS::ES::TicketReader{std::move(signed_ticket)}; } } // namespace end -- cgit v1.2.3 From 5104caf6a647dac47509a8e0540a2eb04c1b1ec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sun, 12 Feb 2017 11:50:35 +0100 Subject: Move AES code to Common/Crypto --- Source/Core/DiscIO/NANDContentLoader.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Source/Core/DiscIO/NANDContentLoader.cpp') diff --git a/Source/Core/DiscIO/NANDContentLoader.cpp b/Source/Core/DiscIO/NANDContentLoader.cpp index 0344fe1c46..62e2e174cb 100644 --- a/Source/Core/DiscIO/NANDContentLoader.cpp +++ b/Source/Core/DiscIO/NANDContentLoader.cpp @@ -17,6 +17,7 @@ #include "Common/Align.h" #include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" +#include "Common/Crypto/AES.h" #include "Common/FileUtil.h" #include "Common/Logging/Log.h" #include "Common/MsgHandler.h" @@ -239,7 +240,7 @@ void CNANDContentLoader::InitializeContentEntries(const std::vector& data_ap u32 rounded_size = Common::AlignUp(static_cast(content.size), 0x40); - m_Content[i].m_Data = std::make_unique(IOS::ES::AESDecode( + m_Content[i].m_Data = std::make_unique(Common::AES::Decrypt( title_key.data(), iv.data(), &data_app[data_app_offset], rounded_size)); data_app_offset += rounded_size; } -- cgit v1.2.3 From 44a3db21e4b7fe20ee2ed6253990f5981b3f12f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Tue, 14 Feb 2017 13:15:02 +0100 Subject: ES: Make sure the TMD and ticket are valid before use --- Source/Core/DiscIO/NANDContentLoader.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'Source/Core/DiscIO/NANDContentLoader.cpp') diff --git a/Source/Core/DiscIO/NANDContentLoader.cpp b/Source/Core/DiscIO/NANDContentLoader.cpp index 62e2e174cb..1204550fab 100644 --- a/Source/Core/DiscIO/NANDContentLoader.cpp +++ b/Source/Core/DiscIO/NANDContentLoader.cpp @@ -156,6 +156,11 @@ CNANDContentLoader::~CNANDContentLoader() { } +bool CNANDContentLoader::IsValid() const +{ + return m_Valid && m_tmd.IsValid(); +} + const SNANDContent* CNANDContentLoader::GetContentByIndex(int index) const { for (auto& Content : m_Content) -- cgit v1.2.3