From 83a9607e8a2ff7c6a90133688149bba34b79021f Mon Sep 17 00:00:00 2001 From: pineappleEA Date: Fri, 14 Apr 2023 19:07:21 +0200 Subject: [PATCH] early-access version 3508 --- README.md | 2 +- src/core/crypto/key_manager.cpp | 73 ++++++++++++++----- src/core/crypto/key_manager.h | 6 +- src/core/file_sys/submission_package.cpp | 39 ++-------- src/core/file_sys/submission_package.h | 1 - .../renderer_vulkan/vk_pipeline_cache.cpp | 2 + 6 files changed, 68 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 16b961808..a75681b65 100755 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ yuzu emulator early access ============= -This is the source code for early-access 3507. +This is the source code for early-access 3508. ## Legal Notice diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index 1a2747c0b..eed5cefac 100755 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp @@ -210,6 +210,36 @@ Ticket Ticket::SynthesizeCommon(Key128 title_key, const std::array& righ return Ticket{out}; } +bool Ticket::Read(Ticket& ticket_out, const FileSys::VirtualFile& file) { + SignatureType sig_type; + if (file->Read(reinterpret_cast(&sig_type), sizeof(sig_type), 0) < sizeof(sig_type)) { + return false; + } + + switch (sig_type) { + case SignatureType::RSA_4096_SHA1: + case SignatureType::RSA_4096_SHA256: { + ticket_out.data.emplace(); + file->Read(reinterpret_cast(&ticket_out.data), sizeof(RSA4096Ticket), 0); + return true; + } + case SignatureType::RSA_2048_SHA1: + case SignatureType::RSA_2048_SHA256: { + ticket_out.data.emplace(); + file->Read(reinterpret_cast(&ticket_out.data), sizeof(RSA2048Ticket), 0); + return true; + } + case SignatureType::ECDSA_SHA1: + case SignatureType::ECDSA_SHA256: { + ticket_out.data.emplace(); + file->Read(reinterpret_cast(&ticket_out.data), sizeof(ECDSATicket), 0); + return true; + } + default: + return false; + } +} + Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed) { Key128 out{}; @@ -290,9 +320,9 @@ void KeyManager::DeriveGeneralPurposeKeys(std::size_t crypto_revision) { } } -RSAKeyPair<2048> KeyManager::GetETicketRSAKey() const { +void KeyManager::DeriveETicketRSAKey() { if (IsAllZeroArray(eticket_extended_kek) || !HasKey(S128KeyType::ETicketRSAKek)) { - return {}; + return; } const auto eticket_final = GetKey(S128KeyType::ETicketRSAKek); @@ -304,12 +334,12 @@ RSAKeyPair<2048> KeyManager::GetETicketRSAKey() const { rsa_1.Transcode(eticket_extended_kek.data() + 0x10, eticket_extended_kek.size() - 0x10, extended_dec.data(), Op::Decrypt); - RSAKeyPair<2048> rsa_key{}; - std::memcpy(rsa_key.decryption_key.data(), extended_dec.data(), rsa_key.decryption_key.size()); - std::memcpy(rsa_key.modulus.data(), extended_dec.data() + 0x100, rsa_key.modulus.size()); - std::memcpy(rsa_key.exponent.data(), extended_dec.data() + 0x200, rsa_key.exponent.size()); - - return rsa_key; + std::memcpy(eticket_rsa_keypair.decryption_key.data(), extended_dec.data(), + eticket_rsa_keypair.decryption_key.size()); + std::memcpy(eticket_rsa_keypair.modulus.data(), extended_dec.data() + 0x100, + eticket_rsa_keypair.modulus.size()); + std::memcpy(eticket_rsa_keypair.exponent.data(), extended_dec.data() + 0x200, + eticket_rsa_keypair.exponent.size()); } Key128 DeriveKeyblobMACKey(const Key128& keyblob_key, const Key128& mac_source) { @@ -665,6 +695,14 @@ void KeyManager::LoadFromFile(const std::filesystem::path& file_path, bool is_ti encrypted_keyblobs[index] = Common::HexStringToArray<0xB0>(out[1]); } else if (out[0].compare(0, 20, "eticket_extended_kek") == 0) { eticket_extended_kek = Common::HexStringToArray<576>(out[1]); + } else if (out[0].compare(0, 19, "eticket_rsa_keypair") == 0) { + const auto key_data = Common::HexStringToArray<528>(out[1]); + std::memcpy(eticket_rsa_keypair.decryption_key.data(), key_data.data(), + eticket_rsa_keypair.decryption_key.size()); + std::memcpy(eticket_rsa_keypair.modulus.data(), key_data.data() + 0x100, + eticket_rsa_keypair.modulus.size()); + std::memcpy(eticket_rsa_keypair.exponent.data(), key_data.data() + 0x200, + eticket_rsa_keypair.exponent.size()); } else { for (const auto& kv : KEYS_VARIABLE_LENGTH) { if (!ValidCryptoRevisionString(out[0], kv.second.size(), 2)) { @@ -1102,13 +1140,12 @@ void KeyManager::DeriveETicket(PartitionDataManager& data, eticket_extended_kek = data.GetETicketExtendedKek(); WriteKeyToFile(KeyCategory::Console, "eticket_extended_kek", eticket_extended_kek); + DeriveETicketRSAKey(); PopulateTickets(); } void KeyManager::PopulateTickets() { - const auto rsa_key = GetETicketRSAKey(); - - if (rsa_key == RSAKeyPair<2048>{}) { + if (eticket_rsa_keypair == RSAKeyPair<2048>{}) { return; } @@ -1136,7 +1173,7 @@ void KeyManager::PopulateTickets() { for (std::size_t i = 0; i < res.size(); ++i) { const auto common = i < idx; - const auto pair = ParseTicket(res[i], rsa_key); + const auto pair = ParseTicket(res[i], eticket_rsa_keypair); if (!pair) { continue; } @@ -1284,12 +1321,11 @@ const std::map& KeyManager::GetPersonalizedTickets() const { } bool KeyManager::AddTicketCommon(Ticket raw) { - const auto rsa_key = GetETicketRSAKey(); - if (rsa_key == RSAKeyPair<2048>{}) { + if (eticket_rsa_keypair == RSAKeyPair<2048>{}) { return false; } - const auto pair = ParseTicket(raw, rsa_key); + const auto pair = ParseTicket(raw, eticket_rsa_keypair); if (!pair) { return false; } @@ -1303,12 +1339,11 @@ bool KeyManager::AddTicketCommon(Ticket raw) { } bool KeyManager::AddTicketPersonalized(Ticket raw) { - const auto rsa_key = GetETicketRSAKey(); - if (rsa_key == RSAKeyPair<2048>{}) { + if (eticket_rsa_keypair == RSAKeyPair<2048>{}) { return false; } - const auto pair = ParseTicket(raw, rsa_key); + const auto pair = ParseTicket(raw, eticket_rsa_keypair); if (!pair) { return false; } @@ -1316,7 +1351,7 @@ bool KeyManager::AddTicketPersonalized(Ticket raw) { const auto& [rid, key] = *pair; u128 rights_id; std::memcpy(rights_id.data(), rid.data(), rid.size()); - common_tickets[rights_id] = raw; + personal_tickets[rights_id] = raw; SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); return true; } diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index 6a3971a11..e0d68fe9e 100755 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -29,8 +29,6 @@ enum class ResultStatus : u16; namespace Core::Crypto { -constexpr u64 TICKET_FILE_TITLEKEY_OFFSET = 0x180; - using Key128 = std::array; using Key256 = std::array; using SHA256Hash = std::array; @@ -106,6 +104,7 @@ struct Ticket { u64 GetSize() const; static Ticket SynthesizeCommon(Key128 title_key, const std::array& rights_id); + static bool Read(Ticket& ticket_out, const FileSys::VirtualFile& file); }; static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big."); @@ -280,6 +279,7 @@ private: std::array, 0x20> encrypted_keyblobs{}; std::array, 0x20> keyblobs{}; std::array eticket_extended_kek{}; + RSAKeyPair<2048> eticket_rsa_keypair{}; bool dev_mode; void LoadFromFile(const std::filesystem::path& file_path, bool is_title_keys); @@ -290,7 +290,7 @@ private: void DeriveGeneralPurposeKeys(std::size_t crypto_revision); - RSAKeyPair<2048> GetETicketRSAKey() const; + void DeriveETicketRSAKey(); void SetKeyWrapped(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); void SetKeyWrapped(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); diff --git a/src/core/file_sys/submission_package.cpp b/src/core/file_sys/submission_package.cpp index 40d88cd99..00ec01e94 100755 --- a/src/core/file_sys/submission_package.cpp +++ b/src/core/file_sys/submission_package.cpp @@ -164,24 +164,6 @@ VirtualFile NSP::GetNCAFile(u64 title_id, ContentRecordType type, TitleType titl return nullptr; } -std::vector NSP::GetTitlekey() const { - if (extracted) - LOG_WARNING(Service_FS, "called on an NSP that is of type extracted."); - std::vector out; - for (const auto& ticket_file : ticket_files) { - if (ticket_file == nullptr || - ticket_file->GetSize() < - Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) { - continue; - } - - out.emplace_back(); - ticket_file->Read(out.back().data(), out.back().size(), - Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET); - } - return out; -} - std::vector NSP::GetFiles() const { return pfs->GetFiles(); } @@ -208,22 +190,17 @@ void NSP::SetTicketKeys(const std::vector& files) { continue; } - if (ticket_file->GetSize() < - Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) { + Core::Crypto::Ticket ticket{}; + if (!Core::Crypto::Ticket::Read(ticket, ticket_file)) { + LOG_WARNING(Common_Filesystem, "Could not read NSP ticket {}", ticket_file->GetName()); continue; } - Core::Crypto::Key128 key{}; - ticket_file->Read(key.data(), key.size(), Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET); - - // We get the name without the extension in order to create the rights ID. - std::string name_only(ticket_file->GetName()); - name_only.erase(name_only.size() - 4); - - const auto rights_id_raw = Common::HexStringToArray<16>(name_only); - u128 rights_id; - std::memcpy(rights_id.data(), rights_id_raw.data(), sizeof(u128)); - keys.SetKey(Core::Crypto::S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); + if (ticket.GetData().type == Core::Crypto::TitleKeyType::Common) { + keys.AddTicketCommon(ticket); + } else { + keys.AddTicketPersonalized(ticket); + } } } diff --git a/src/core/file_sys/submission_package.h b/src/core/file_sys/submission_package.h index 19e2a968a..42a668eab 100755 --- a/src/core/file_sys/submission_package.h +++ b/src/core/file_sys/submission_package.h @@ -52,7 +52,6 @@ public: TitleType title_type = TitleType::Application) const; VirtualFile GetNCAFile(u64 title_id, ContentRecordType type, TitleType title_type = TitleType::Application) const; - std::vector GetTitlekey() const; std::vector GetFiles() const override; diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp index d56fdbac0..00dcfee79 100755 --- a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp +++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp @@ -330,6 +330,8 @@ PipelineCache::PipelineCache(RasterizerVulkan& rasterizer_, const Device& device .lower_left_origin_mode = false, .need_declared_frag_colors = false, .need_gather_subpixel_offset = driver_id == VK_DRIVER_ID_AMD_PROPRIETARY || + driver_id == VK_DRIVER_ID_AMD_OPEN_SOURCE || + driver_id == VK_DRIVER_ID_MESA_RADV || driver_id == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS || driver_id == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA,