crypto: Streamline ticket loading.
Do not parse title key when already known, and re-use code where possible. Offers a significant speed-up to game list population.
This commit is contained in:
parent
64f3ad2654
commit
09956e883a
4 changed files with 53 additions and 84 deletions
|
@ -556,28 +556,35 @@ static std::optional<u64> FindTicketOffset(const std::array<u8, size>& data) {
|
|||
return offset;
|
||||
}
|
||||
|
||||
std::optional<std::pair<Key128, Key128>> ParseTicket(const Ticket& ticket,
|
||||
const RSAKeyPair<2048>& key) {
|
||||
std::optional<Key128> 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<std::pair<Key128, Key128>> 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<std::pair<Key128, Key128>> 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<u128, Ticket>& 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
|
||||
|
|
|
@ -290,8 +290,7 @@ public:
|
|||
const std::map<u128, Ticket>& GetCommonTickets() const;
|
||||
const std::map<u128, Ticket>& 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<Key128> 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<Key256, 2>& sd_keys, KeyManager& ke
|
|||
|
||||
std::vector<Ticket> 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<std::pair<Key128, Key128>> ParseTicket(const Ticket& ticket,
|
||||
const RSAKeyPair<2048>& eticket_extended_key);
|
||||
|
||||
} // namespace Core::Crypto
|
||||
|
|
|
@ -191,16 +191,10 @@ void NSP::SetTicketKeys(const std::vector<VirtualFile>& 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue