diff --git a/Source/Core/Core/IOS/DI/DI.cpp b/Source/Core/Core/IOS/DI/DI.cpp index 0df9861054..4e3f258603 100644 --- a/Source/Core/Core/IOS/DI/DI.cpp +++ b/Source/Core/Core/IOS/DI/DI.cpp @@ -110,7 +110,7 @@ IPCCommandResult DI::IOCtlV(const IOCtlVRequest& request) // Read TMD to the buffer const IOS::ES::TMDReader tmd = DVDThread::GetTMD(partition); - const std::vector raw_tmd = tmd.GetRawTMD(); + const std::vector& raw_tmd = tmd.GetBytes(); Memory::CopyToEmu(request.io_vectors[0].address, raw_tmd.data(), raw_tmd.size()); ES::DIVerify(tmd, DVDThread::GetTicket(partition)); diff --git a/Source/Core/Core/IOS/ES/ES.cpp b/Source/Core/Core/IOS/ES/ES.cpp index 8dbe7337ab..629f093f23 100644 --- a/Source/Core/Core/IOS/ES/ES.cpp +++ b/Source/Core/Core/IOS/ES/ES.cpp @@ -610,7 +610,7 @@ s32 ES::DIVerify(const IOS::ES::TMDReader& tmd, const IOS::ES::TicketReader& tic if (!File::Exists(tmd_path)) { File::IOFile tmd_file(tmd_path, "wb"); - const std::vector& tmd_bytes = tmd.GetRawTMD(); + const std::vector& tmd_bytes = tmd.GetBytes(); if (!tmd_file.WriteBytes(tmd_bytes.data(), tmd_bytes.size())) ERROR_LOG(IOS_ES, "DIVerify failed to write disc TMD to NAND."); } diff --git a/Source/Core/Core/IOS/ES/Formats.cpp b/Source/Core/Core/IOS/ES/Formats.cpp index e68d28109f..0fcfa033d3 100644 --- a/Source/Core/Core/IOS/ES/Formats.cpp +++ b/Source/Core/Core/IOS/ES/Formats.cpp @@ -164,26 +164,19 @@ bool IsValidTMDSize(size_t size) return size <= 0x49e4; } -TMDReader::TMDReader(const std::vector& bytes) : m_bytes(bytes) +TMDReader::TMDReader(const std::vector& bytes) : SignedBlobReader(bytes) { } -TMDReader::TMDReader(std::vector&& bytes) : m_bytes(std::move(bytes)) +TMDReader::TMDReader(std::vector&& bytes) : SignedBlobReader(std::move(bytes)) { } -void TMDReader::SetBytes(const std::vector& bytes) -{ - m_bytes = bytes; -} - -void TMDReader::SetBytes(std::vector&& bytes) -{ - m_bytes = std::move(bytes); -} - bool TMDReader::IsValid() const { + if (!IsSignatureValid()) + return false; + if (m_bytes.size() < sizeof(TMDHeader)) { // TMD is too small to contain its base fields. @@ -199,11 +192,6 @@ bool TMDReader::IsValid() const return true; } -const std::vector& TMDReader::GetRawTMD() const -{ - return m_bytes; -} - std::vector TMDReader::GetRawHeader() const { return std::vector(m_bytes.begin(), m_bytes.begin() + sizeof(TMDHeader)); @@ -331,37 +319,17 @@ bool TMDReader::FindContentById(u32 id, Content* content) const return false; } -void TMDReader::DoState(PointerWrap& p) -{ - p.Do(m_bytes); -} - -TicketReader::TicketReader(const std::vector& bytes) : m_bytes(bytes) +TicketReader::TicketReader(const std::vector& bytes) : SignedBlobReader(bytes) { } -TicketReader::TicketReader(std::vector&& bytes) : m_bytes(std::move(bytes)) +TicketReader::TicketReader(std::vector&& bytes) : SignedBlobReader(std::move(bytes)) { } -void TicketReader::SetBytes(const std::vector& bytes) -{ - m_bytes = bytes; -} - -void TicketReader::SetBytes(std::vector&& bytes) -{ - m_bytes = std::move(bytes); -} - bool TicketReader::IsValid() const { - return !m_bytes.empty() && m_bytes.size() % sizeof(Ticket) == 0; -} - -void TicketReader::DoState(PointerWrap& p) -{ - p.Do(m_bytes); + return IsSignatureValid() && !m_bytes.empty() && m_bytes.size() % sizeof(Ticket) == 0; } size_t TicketReader::GetNumberOfTickets() const @@ -369,11 +337,6 @@ size_t TicketReader::GetNumberOfTickets() const return m_bytes.size() / sizeof(Ticket); } -const std::vector& TicketReader::GetRawTicket() const -{ - return m_bytes; -} - std::vector TicketReader::GetRawTicket(u64 ticket_id_to_find) const { for (size_t i = 0; i < GetNumberOfTickets(); ++i) @@ -404,13 +367,6 @@ std::vector TicketReader::GetRawTicketView(u32 ticket_num) const return view; } -std::string TicketReader::GetIssuer() const -{ - const char* bytes = - reinterpret_cast(m_bytes.data() + offsetof(Ticket, signature.issuer)); - return std::string(bytes, strnlen(bytes, sizeof(Ticket::signature.issuer))); -} - u32 TicketReader::GetDeviceId() const { return Common::swap32(m_bytes.data() + offsetof(Ticket, device_id)); diff --git a/Source/Core/Core/IOS/ES/Formats.h b/Source/Core/Core/IOS/ES/Formats.h index 83666f726c..dd3c0aad9c 100644 --- a/Source/Core/Core/IOS/ES/Formats.h +++ b/Source/Core/Core/IOS/ES/Formats.h @@ -169,20 +169,16 @@ protected: bool IsValidTMDSize(size_t size); -class TMDReader final +class TMDReader final : public SignedBlobReader { public: TMDReader() = default; explicit TMDReader(const std::vector& bytes); explicit TMDReader(std::vector&& bytes); - void SetBytes(const std::vector& bytes); - void SetBytes(std::vector&& bytes); - bool IsValid() const; - // Returns the TMD or parts of it without any kind of parsing. Intended for use by ES. - const std::vector& GetRawTMD() const; + // Returns parts of the TMD without any kind of parsing. Intended for use by ES. std::vector GetRawHeader() const; std::vector GetRawView() const; @@ -203,27 +199,17 @@ public: bool GetContent(u16 index, Content* content) const; std::vector GetContents() const; bool FindContentById(u32 id, Content* content) const; - - void DoState(PointerWrap& p); - -private: - std::vector m_bytes; }; -class TicketReader final +class TicketReader final : public SignedBlobReader { public: TicketReader() = default; explicit TicketReader(const std::vector& bytes); explicit TicketReader(std::vector&& bytes); - void SetBytes(const std::vector& bytes); - void SetBytes(std::vector&& bytes); - bool IsValid() const; - void DoState(PointerWrap& p); - const std::vector& GetRawTicket() const; std::vector GetRawTicket(u64 ticket_id) const; size_t GetNumberOfTickets() const; @@ -233,7 +219,6 @@ public: // more than just one ticket and generate ticket views for them, so we implement it too. std::vector GetRawTicketView(u32 ticket_num) const; - std::string GetIssuer() const; u32 GetDeviceId() const; u64 GetTitleId() const; std::vector GetTitleKey() const; @@ -244,9 +229,6 @@ public: // Decrypts the title key field for a "personalised" ticket -- one that is device-specific // and has a title key that must be decrypted first. s32 Unpersonalise(); - -private: - std::vector m_bytes; }; class SharedContentMap final diff --git a/Source/Core/Core/IOS/ES/NandUtils.cpp b/Source/Core/Core/IOS/ES/NandUtils.cpp index d0fb59745d..7f5b3a026d 100644 --- a/Source/Core/Core/IOS/ES/NandUtils.cpp +++ b/Source/Core/Core/IOS/ES/NandUtils.cpp @@ -254,7 +254,7 @@ bool ES::WriteImportTMD(const IOS::ES::TMDReader& tmd) { File::IOFile file(tmd_path, "wb"); - if (!file.WriteBytes(tmd.GetRawTMD().data(), tmd.GetRawTMD().size())) + if (!file.WriteBytes(tmd.GetBytes().data(), tmd.GetBytes().size())) return false; } diff --git a/Source/Core/Core/IOS/ES/TitleInformation.cpp b/Source/Core/Core/IOS/ES/TitleInformation.cpp index 51c2dabc18..583c9d5aca 100644 --- a/Source/Core/Core/IOS/ES/TitleInformation.cpp +++ b/Source/Core/Core/IOS/ES/TitleInformation.cpp @@ -150,7 +150,7 @@ IPCCommandResult ES::GetStoredTMDSize(const IOCtlVRequest& request) if (!tmd.IsValid()) return GetDefaultReply(FS_ENOENT); - const u32 tmd_size = static_cast(tmd.GetRawTMD().size()); + const u32 tmd_size = static_cast(tmd.GetBytes().size()); Memory::Write_U32(tmd_size, request.io_vectors[0].address); INFO_LOG(IOS_ES, "GetStoredTMDSize: %u bytes for %016" PRIx64, tmd_size, title_id); @@ -171,7 +171,7 @@ IPCCommandResult ES::GetStoredTMD(const IOCtlVRequest& request) // TODO: actually use this param in when writing to the outbuffer :/ const u32 MaxCount = Memory::Read_U32(request.in_vectors[1].address); - const std::vector raw_tmd = tmd.GetRawTMD(); + const std::vector& raw_tmd = tmd.GetBytes(); if (raw_tmd.size() != request.io_vectors[0].size) return GetDefaultReply(ES_EINVAL); diff --git a/Source/Core/Core/IOS/ES/TitleManagement.cpp b/Source/Core/Core/IOS/ES/TitleManagement.cpp index 01d0627a89..ca81b76333 100644 --- a/Source/Core/Core/IOS/ES/TitleManagement.cpp +++ b/Source/Core/Core/IOS/ES/TitleManagement.cpp @@ -40,7 +40,7 @@ static ReturnCode WriteTicket(const IOS::ES::TicketReader& ticket) if (!ticket_file) return ES_EIO; - const std::vector& raw_ticket = ticket.GetRawTicket(); + const std::vector& raw_ticket = ticket.GetBytes(); return ticket_file.WriteBytes(raw_ticket.data(), raw_ticket.size()) ? IPC_SUCCESS : ES_EIO; } @@ -394,7 +394,7 @@ ReturnCode ES::DeleteTicket(const u8* ticket_view) const u64 ticket_id = Common::swap64(ticket_view + offsetof(IOS::ES::TicketView, ticket_id)); ticket.DeleteTicket(ticket_id); - const std::vector& new_ticket = ticket.GetRawTicket(); + const std::vector& new_ticket = ticket.GetBytes(); const std::string ticket_path = Common::GetTicketFileName(title_id, Common::FROM_SESSION_ROOT); { File::IOFile ticket_file(ticket_path, "wb"); @@ -505,7 +505,7 @@ ReturnCode ES::ExportTitleInit(Context& context, u64 title_id, u8* tmd_bytes, u3 context.title_export.title_key = ticket.GetTitleKey(); - const auto& raw_tmd = context.title_export.tmd.GetRawTMD(); + const std::vector& raw_tmd = context.title_export.tmd.GetBytes(); if (tmd_size != raw_tmd.size()) return ES_EINVAL; diff --git a/Source/Core/Core/IOS/ES/Views.cpp b/Source/Core/Core/IOS/ES/Views.cpp index ead7a5b7e4..5e16aeac34 100644 --- a/Source/Core/Core/IOS/ES/Views.cpp +++ b/Source/Core/Core/IOS/ES/Views.cpp @@ -376,7 +376,7 @@ IPCCommandResult ES::DIGetTMDSize(const IOCtlVRequest& request) if (!GetTitleContext().active) return GetDefaultReply(ES_EINVAL); - Memory::Write_U32(static_cast(GetTitleContext().tmd.GetRawTMD().size()), + Memory::Write_U32(static_cast(GetTitleContext().tmd.GetBytes().size()), request.io_vectors[0].address); return GetDefaultReply(IPC_SUCCESS); } @@ -393,7 +393,7 @@ IPCCommandResult ES::DIGetTMD(const IOCtlVRequest& request) if (!GetTitleContext().active) return GetDefaultReply(ES_EINVAL); - const std::vector& tmd_bytes = GetTitleContext().tmd.GetRawTMD(); + const std::vector& tmd_bytes = GetTitleContext().tmd.GetBytes(); if (static_cast(tmd_bytes.size()) > tmd_size) return GetDefaultReply(ES_EINVAL); diff --git a/Source/Core/UICommon/WiiUtils.cpp b/Source/Core/UICommon/WiiUtils.cpp index e7b3834900..acd4713a33 100644 --- a/Source/Core/UICommon/WiiUtils.cpp +++ b/Source/Core/UICommon/WiiUtils.cpp @@ -27,8 +27,8 @@ bool InstallWAD(const std::string& wad_path) const auto es = ios.GetES(); IOS::HLE::Device::ES::Context context; - if (es->ImportTicket(wad.GetTicket().GetRawTicket()) < 0 || - es->ImportTitleInit(context, tmd.GetRawTMD()) < 0) + if (es->ImportTicket(wad.GetTicket().GetBytes()) < 0 || + es->ImportTitleInit(context, tmd.GetBytes()) < 0) { PanicAlertT("WAD installation failed: Could not initialise title import."); return false; diff --git a/Source/UnitTests/Core/IOS/ES/FormatsTest.cpp b/Source/UnitTests/Core/IOS/ES/FormatsTest.cpp index b249fe8419..4f76bbd51e 100644 --- a/Source/UnitTests/Core/IOS/ES/FormatsTest.cpp +++ b/Source/UnitTests/Core/IOS/ES/FormatsTest.cpp @@ -76,7 +76,7 @@ void TMDReaderTest::TestGeneralInfo() void TMDReaderTest::TestRawTMDAndView() { - const std::vector& dolphin_tmd_bytes = m_tmd.GetRawTMD(); + const std::vector& dolphin_tmd_bytes = m_tmd.GetBytes(); // Separate check because gtest prints neither the size nor the full buffer. EXPECT_EQ(m_raw_tmd.size(), dolphin_tmd_bytes.size()); EXPECT_EQ(m_raw_tmd, dolphin_tmd_bytes);