diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index 5132d344c9..58faf9013b 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp @@ -556,28 +556,35 @@ static std::optional FindTicketOffset(const std::array& data) { return offset; } -std::optional> ParseTicket(const Ticket& ticket, - const RSAKeyPair<2048>& key) { +std::optional KeyManager::ParseTicketTitleKey(const Ticket& ticket) { + if (eticket_rsa_keypair == RSAKeyPair<2048>{}) { + LOG_WARNING(Crypto, + "Skipping ticket title key parsing due to missing ETicket RSA key-pair."); + return std::nullopt; + } + if (!ticket.IsValid()) { + LOG_WARNING(Crypto, "Attempted to parse title key of invalid ticket."); + return std::nullopt; + } + + if (ticket.GetData().rights_id == Key128{}) { + LOG_WARNING(Crypto, "Attempted to parse title key of ticket with no rights ID."); return std::nullopt; } const auto issuer = ticket.GetData().issuer; if (IsAllZeroArray(issuer)) { + LOG_WARNING(Crypto, "Attempted to parse title key of ticket with invalid issuer."); return std::nullopt; } + if (issuer[0] != 'R' || issuer[1] != 'o' || issuer[2] != 'o' || issuer[3] != 't') { - LOG_INFO(Crypto, "Attempting to parse ticket with non-standard certificate authority."); - } - - Key128 rights_id = ticket.GetData().rights_id; - - if (rights_id == Key128{}) { - return std::nullopt; + LOG_WARNING(Crypto, "Parsing ticket with non-standard certificate authority."); } if (ticket.GetData().type == TitleKeyType::Common) { - return std::make_pair(rights_id, ticket.GetData().title_key_common); + return ticket.GetData().title_key_common; } mbedtls_mpi D; // RSA Private Exponent @@ -590,9 +597,12 @@ std::optional> ParseTicket(const Ticket& ticket, mbedtls_mpi_init(&S); mbedtls_mpi_init(&M); - mbedtls_mpi_read_binary(&D, key.decryption_key.data(), key.decryption_key.size()); - mbedtls_mpi_read_binary(&N, key.modulus.data(), key.modulus.size()); - mbedtls_mpi_read_binary(&S, ticket.GetData().title_key_block.data(), 0x100); + const auto& title_key_block = ticket.GetData().title_key_block; + mbedtls_mpi_read_binary(&D, eticket_rsa_keypair.decryption_key.data(), + eticket_rsa_keypair.decryption_key.size()); + mbedtls_mpi_read_binary(&N, eticket_rsa_keypair.modulus.data(), + eticket_rsa_keypair.modulus.size()); + mbedtls_mpi_read_binary(&S, title_key_block.data(), title_key_block.size()); mbedtls_mpi_exp_mod(&M, &S, &D, &N, nullptr); @@ -620,8 +630,7 @@ std::optional> ParseTicket(const Ticket& ticket, Key128 key_temp{}; std::memcpy(key_temp.data(), m_2.data() + *offset, key_temp.size()); - - return std::make_pair(rights_id, key_temp); + return key_temp; } KeyManager::KeyManager() { @@ -1199,30 +1208,12 @@ void KeyManager::PopulateTickets() { const Common::FS::IOFile save_e2{system_save_e2_path, Common::FS::FileAccessMode::Read, Common::FS::FileType::BinaryFile}; + auto tickets = GetTicketblob(save_e1); const auto blob2 = GetTicketblob(save_e2); - auto res = GetTicketblob(save_e1); + tickets.insert(tickets.end(), blob2.begin(), blob2.end()); - const auto idx = res.size(); - res.insert(res.end(), blob2.begin(), blob2.end()); - - for (std::size_t i = 0; i < res.size(); ++i) { - const auto common = i < idx; - const auto pair = ParseTicket(res[i], eticket_rsa_keypair); - if (!pair) { - continue; - } - - const auto& [rid, key] = *pair; - u128 rights_id; - std::memcpy(rights_id.data(), rid.data(), rid.size()); - - if (common) { - common_tickets[rights_id] = res[i]; - } else { - personal_tickets[rights_id] = res[i]; - } - - SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); + for (const auto& ticket : tickets) { + AddTicket(ticket); } } @@ -1354,39 +1345,33 @@ const std::map& KeyManager::GetPersonalizedTickets() const { return personal_tickets; } -bool KeyManager::AddTicketCommon(Ticket raw) { - if (eticket_rsa_keypair == RSAKeyPair<2048>{}) { +bool KeyManager::AddTicket(const Ticket& ticket) { + if (!ticket.IsValid()) { + LOG_WARNING(Crypto, "Attempted to add invalid ticket."); return false; } - const auto pair = ParseTicket(raw, eticket_rsa_keypair); - if (!pair) { - return false; - } - - const auto& [rid, key] = *pair; + const auto& rid = ticket.GetData().rights_id; u128 rights_id; std::memcpy(rights_id.data(), rid.data(), rid.size()); - common_tickets[rights_id] = raw; - SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); - return true; -} - -bool KeyManager::AddTicketPersonalized(Ticket raw) { - if (eticket_rsa_keypair == RSAKeyPair<2048>{}) { - return false; + if (ticket.GetData().type == Core::Crypto::TitleKeyType::Common) { + common_tickets[rights_id] = ticket; + } else { + personal_tickets[rights_id] = ticket; } - const auto pair = ParseTicket(raw, eticket_rsa_keypair); - if (!pair) { - return false; + if (HasKey(S128KeyType::Titlekey, rights_id[1], rights_id[0])) { + LOG_DEBUG(Crypto, + "Skipping parsing title key from ticket for known rights ID {:016X}{:016X}.", + rights_id[1], rights_id[0]); + return true; } - const auto& [rid, key] = *pair; - u128 rights_id; - std::memcpy(rights_id.data(), rid.data(), rid.size()); - personal_tickets[rights_id] = raw; - SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); + const auto key = ParseTicketTitleKey(ticket); + if (!key) { + return false; + } + SetKey(S128KeyType::Titlekey, key.value(), rights_id[1], rights_id[0]); return true; } } // namespace Core::Crypto diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index 52edc8b05a..650a297319 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -290,8 +290,7 @@ public: const std::map& GetCommonTickets() const; const std::map& GetPersonalizedTickets() const; - bool AddTicketCommon(Ticket raw); - bool AddTicketPersonalized(Ticket raw); + bool AddTicket(const Ticket& ticket); void ReloadKeys(); bool AreKeysLoaded() const; @@ -324,6 +323,9 @@ private: void SetKeyWrapped(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); void SetKeyWrapped(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); + + /// Parses the title key section of a ticket. + std::optional ParseTicketTitleKey(const Ticket& ticket); }; Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed); @@ -338,9 +340,4 @@ Loader::ResultStatus DeriveSDKeys(std::array& sd_keys, KeyManager& ke std::vector GetTicketblob(const Common::FS::IOFile& ticket_save); -// Returns a pair of {rights_id, titlekey}. Fails if the ticket has no certificate authority -// (offset 0x140-0x144 is zero) -std::optional> ParseTicket(const Ticket& ticket, - const RSAKeyPair<2048>& eticket_extended_key); - } // namespace Core::Crypto diff --git a/src/core/file_sys/submission_package.cpp b/src/core/file_sys/submission_package.cpp index b0f55df708..68e8ec22fc 100644 --- a/src/core/file_sys/submission_package.cpp +++ b/src/core/file_sys/submission_package.cpp @@ -191,16 +191,10 @@ void NSP::SetTicketKeys(const std::vector& files) { } auto ticket = Core::Crypto::Ticket::Read(ticket_file); - if (!ticket.IsValid()) { - LOG_WARNING(Common_Filesystem, "Could not read NSP ticket {}", ticket_file->GetName()); + if (!keys.AddTicket(ticket)) { + LOG_WARNING(Common_Filesystem, "Could not load NSP ticket {}", ticket_file->GetName()); continue; } - - if (ticket.GetData().type == Core::Crypto::TitleKeyType::Common) { - keys.AddTicketCommon(ticket); - } else { - keys.AddTicketPersonalized(ticket); - } } } diff --git a/src/core/hle/service/es/es.cpp b/src/core/hle/service/es/es.cpp index 896f870b5e..9eaae4c4bd 100644 --- a/src/core/hle/service/es/es.cpp +++ b/src/core/hle/service/es/es.cpp @@ -133,14 +133,7 @@ private: } Core::Crypto::Ticket ticket = Core::Crypto::Ticket::Read(raw_ticket); - if (!ticket.IsValid()) { - LOG_ERROR(Service_ETicket, "The ticket is invalid!"); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ERROR_INVALID_ARGUMENT); - return; - } - - if (!keys.AddTicketPersonalized(ticket)) { + if (!keys.AddTicket(ticket)) { LOG_ERROR(Service_ETicket, "The ticket could not be imported!"); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ERROR_INVALID_ARGUMENT);