early-access version 3515

This commit is contained in:
pineappleEA 2023-04-21 07:09:52 +02:00
parent 8b51ae9c54
commit 3a85013cbd
5 changed files with 105 additions and 130 deletions

View file

@ -1,7 +1,7 @@
yuzu emulator early access yuzu emulator early access
============= =============
This is the source code for early-access 3514. This is the source code for early-access 3515.
## Legal Notice ## Legal Notice

View file

@ -156,10 +156,6 @@ u64 GetSignatureTypePaddingSize(SignatureType type) {
UNREACHABLE(); UNREACHABLE();
} }
bool Ticket::IsValid() const {
return !std::holds_alternative<std::monostate>(data);
}
SignatureType Ticket::GetSignatureType() const { SignatureType Ticket::GetSignatureType() const {
if (const auto* ticket = std::get_if<RSA4096Ticket>(&data)) { if (const auto* ticket = std::get_if<RSA4096Ticket>(&data)) {
return ticket->sig_type; return ticket->sig_type;
@ -214,37 +210,6 @@ Ticket Ticket::SynthesizeCommon(Key128 title_key, const std::array<u8, 16>& righ
return Ticket{out}; return Ticket{out};
} }
bool Ticket::Read(Ticket& ticket_out, const FileSys::VirtualFile& file) {
SignatureType sig_type;
if (file->Read(reinterpret_cast<u8*>(&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<RSA4096Ticket>();
file->Read(reinterpret_cast<u8*>(&ticket_out.data), sizeof(RSA4096Ticket), 0);
return true;
}
case SignatureType::RSA_2048_SHA1:
case SignatureType::RSA_2048_SHA256: {
ticket_out.data.emplace<RSA2048Ticket>();
file->Read(reinterpret_cast<u8*>(&ticket_out.data), sizeof(RSA2048Ticket), 0);
return true;
}
case SignatureType::ECDSA_SHA1:
case SignatureType::ECDSA_SHA256: {
ticket_out.data.emplace<ECDSATicket>();
file->Read(reinterpret_cast<u8*>(&ticket_out.data), sizeof(ECDSATicket), 0);
return true;
}
default:
ticket_out.data.emplace<std::monostate>();
return false;
}
}
Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed) { Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed) {
Key128 out{}; Key128 out{};
@ -325,9 +290,9 @@ void KeyManager::DeriveGeneralPurposeKeys(std::size_t crypto_revision) {
} }
} }
void KeyManager::DeriveETicketRSAKey() { RSAKeyPair<2048> KeyManager::GetETicketRSAKey() const {
if (IsAllZeroArray(eticket_extended_kek) || !HasKey(S128KeyType::ETicketRSAKek)) { if (IsAllZeroArray(eticket_extended_kek) || !HasKey(S128KeyType::ETicketRSAKek)) {
return; return {};
} }
const auto eticket_final = GetKey(S128KeyType::ETicketRSAKek); const auto eticket_final = GetKey(S128KeyType::ETicketRSAKek);
@ -339,12 +304,12 @@ void KeyManager::DeriveETicketRSAKey() {
rsa_1.Transcode(eticket_extended_kek.data() + 0x10, eticket_extended_kek.size() - 0x10, rsa_1.Transcode(eticket_extended_kek.data() + 0x10, eticket_extended_kek.size() - 0x10,
extended_dec.data(), Op::Decrypt); extended_dec.data(), Op::Decrypt);
std::memcpy(eticket_rsa_keypair.decryption_key.data(), extended_dec.data(), RSAKeyPair<2048> rsa_key{};
eticket_rsa_keypair.decryption_key.size()); std::memcpy(rsa_key.decryption_key.data(), extended_dec.data(), rsa_key.decryption_key.size());
std::memcpy(eticket_rsa_keypair.modulus.data(), extended_dec.data() + 0x100, std::memcpy(rsa_key.modulus.data(), extended_dec.data() + 0x100, rsa_key.modulus.size());
eticket_rsa_keypair.modulus.size()); std::memcpy(rsa_key.exponent.data(), extended_dec.data() + 0x200, rsa_key.exponent.size());
std::memcpy(eticket_rsa_keypair.exponent.data(), extended_dec.data() + 0x200,
eticket_rsa_keypair.exponent.size()); return rsa_key;
} }
Key128 DeriveKeyblobMACKey(const Key128& keyblob_key, const Key128& mac_source) { Key128 DeriveKeyblobMACKey(const Key128& keyblob_key, const Key128& mac_source) {
@ -540,12 +505,6 @@ static std::optional<u64> FindTicketOffset(const std::array<u8, size>& data) {
std::optional<std::pair<Key128, Key128>> ParseTicket(const Ticket& ticket, std::optional<std::pair<Key128, Key128>> ParseTicket(const Ticket& ticket,
const RSAKeyPair<2048>& key) { const RSAKeyPair<2048>& key) {
if (!ticket.IsValid()) {
return std::nullopt;
}
// Dirty hack, figure out why ticket.data variant is invalid
try {
const auto issuer = ticket.GetData().issuer; const auto issuer = ticket.GetData().issuer;
if (IsAllZeroArray(issuer)) { if (IsAllZeroArray(issuer)) {
return std::nullopt; return std::nullopt;
@ -560,7 +519,8 @@ std::optional<std::pair<Key128, Key128>> ParseTicket(const Ticket& ticket,
return std::nullopt; return std::nullopt;
} }
if (ticket.GetData().type == TitleKeyType::Common) { if (!std::any_of(ticket.GetData().title_key_common_pad.begin(),
ticket.GetData().title_key_common_pad.end(), [](u8 b) { return b != 0; })) {
return std::make_pair(rights_id, ticket.GetData().title_key_common); return std::make_pair(rights_id, ticket.GetData().title_key_common);
} }
@ -606,9 +566,6 @@ std::optional<std::pair<Key128, Key128>> ParseTicket(const Ticket& ticket,
std::memcpy(key_temp.data(), m_2.data() + *offset, key_temp.size()); std::memcpy(key_temp.data(), m_2.data() + *offset, key_temp.size());
return std::make_pair(rights_id, key_temp); return std::make_pair(rights_id, key_temp);
} catch (const std::bad_variant_access&) {
return std::nullopt;
}
} }
KeyManager::KeyManager() { KeyManager::KeyManager() {
@ -708,14 +665,6 @@ void KeyManager::LoadFromFile(const std::filesystem::path& file_path, bool is_ti
encrypted_keyblobs[index] = Common::HexStringToArray<0xB0>(out[1]); encrypted_keyblobs[index] = Common::HexStringToArray<0xB0>(out[1]);
} else if (out[0].compare(0, 20, "eticket_extended_kek") == 0) { } else if (out[0].compare(0, 20, "eticket_extended_kek") == 0) {
eticket_extended_kek = Common::HexStringToArray<576>(out[1]); 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 { } else {
for (const auto& kv : KEYS_VARIABLE_LENGTH) { for (const auto& kv : KEYS_VARIABLE_LENGTH) {
if (!ValidCryptoRevisionString(out[0], kv.second.size(), 2)) { if (!ValidCryptoRevisionString(out[0], kv.second.size(), 2)) {
@ -1153,12 +1102,13 @@ void KeyManager::DeriveETicket(PartitionDataManager& data,
eticket_extended_kek = data.GetETicketExtendedKek(); eticket_extended_kek = data.GetETicketExtendedKek();
WriteKeyToFile(KeyCategory::Console, "eticket_extended_kek", eticket_extended_kek); WriteKeyToFile(KeyCategory::Console, "eticket_extended_kek", eticket_extended_kek);
DeriveETicketRSAKey();
PopulateTickets(); PopulateTickets();
} }
void KeyManager::PopulateTickets() { void KeyManager::PopulateTickets() {
if (eticket_rsa_keypair == RSAKeyPair<2048>{}) { const auto rsa_key = GetETicketRSAKey();
if (rsa_key == RSAKeyPair<2048>{}) {
return; return;
} }
@ -1186,7 +1136,7 @@ void KeyManager::PopulateTickets() {
for (std::size_t i = 0; i < res.size(); ++i) { for (std::size_t i = 0; i < res.size(); ++i) {
const auto common = i < idx; const auto common = i < idx;
const auto pair = ParseTicket(res[i], eticket_rsa_keypair); const auto pair = ParseTicket(res[i], rsa_key);
if (!pair) { if (!pair) {
continue; continue;
} }
@ -1334,11 +1284,12 @@ const std::map<u128, Ticket>& KeyManager::GetPersonalizedTickets() const {
} }
bool KeyManager::AddTicketCommon(Ticket raw) { bool KeyManager::AddTicketCommon(Ticket raw) {
if (eticket_rsa_keypair == RSAKeyPair<2048>{}) { const auto rsa_key = GetETicketRSAKey();
if (rsa_key == RSAKeyPair<2048>{}) {
return false; return false;
} }
const auto pair = ParseTicket(raw, eticket_rsa_keypair); const auto pair = ParseTicket(raw, rsa_key);
if (!pair) { if (!pair) {
return false; return false;
} }
@ -1352,11 +1303,12 @@ bool KeyManager::AddTicketCommon(Ticket raw) {
} }
bool KeyManager::AddTicketPersonalized(Ticket raw) { bool KeyManager::AddTicketPersonalized(Ticket raw) {
if (eticket_rsa_keypair == RSAKeyPair<2048>{}) { const auto rsa_key = GetETicketRSAKey();
if (rsa_key == RSAKeyPair<2048>{}) {
return false; return false;
} }
const auto pair = ParseTicket(raw, eticket_rsa_keypair); const auto pair = ParseTicket(raw, rsa_key);
if (!pair) { if (!pair) {
return false; return false;
} }
@ -1364,7 +1316,7 @@ bool KeyManager::AddTicketPersonalized(Ticket raw) {
const auto& [rid, key] = *pair; const auto& [rid, key] = *pair;
u128 rights_id; u128 rights_id;
std::memcpy(rights_id.data(), rid.data(), rid.size()); std::memcpy(rights_id.data(), rid.data(), rid.size());
personal_tickets[rights_id] = raw; common_tickets[rights_id] = raw;
SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]);
return true; return true;
} }

View file

@ -29,6 +29,8 @@ enum class ResultStatus : u16;
namespace Core::Crypto { namespace Core::Crypto {
constexpr u64 TICKET_FILE_TITLEKEY_OFFSET = 0x180;
using Key128 = std::array<u8, 0x10>; using Key128 = std::array<u8, 0x10>;
using Key256 = std::array<u8, 0x20>; using Key256 = std::array<u8, 0x20>;
using SHA256Hash = std::array<u8, 0x20>; using SHA256Hash = std::array<u8, 0x20>;
@ -96,16 +98,14 @@ struct ECDSATicket {
}; };
struct Ticket { struct Ticket {
std::variant<std::monostate, RSA4096Ticket, RSA2048Ticket, ECDSATicket> data; std::variant<RSA4096Ticket, RSA2048Ticket, ECDSATicket> data;
bool IsValid() const;
SignatureType GetSignatureType() const; SignatureType GetSignatureType() const;
TicketData& GetData(); TicketData& GetData();
const TicketData& GetData() const; const TicketData& GetData() const;
u64 GetSize() const; u64 GetSize() const;
static Ticket SynthesizeCommon(Key128 title_key, const std::array<u8, 0x10>& rights_id); static Ticket SynthesizeCommon(Key128 title_key, const std::array<u8, 0x10>& rights_id);
static bool Read(Ticket& ticket_out, const FileSys::VirtualFile& file);
}; };
static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big."); static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big.");
@ -280,7 +280,6 @@ private:
std::array<std::array<u8, 0xB0>, 0x20> encrypted_keyblobs{}; std::array<std::array<u8, 0xB0>, 0x20> encrypted_keyblobs{};
std::array<std::array<u8, 0x90>, 0x20> keyblobs{}; std::array<std::array<u8, 0x90>, 0x20> keyblobs{};
std::array<u8, 576> eticket_extended_kek{}; std::array<u8, 576> eticket_extended_kek{};
RSAKeyPair<2048> eticket_rsa_keypair{};
bool dev_mode; bool dev_mode;
void LoadFromFile(const std::filesystem::path& file_path, bool is_title_keys); void LoadFromFile(const std::filesystem::path& file_path, bool is_title_keys);
@ -291,7 +290,7 @@ private:
void DeriveGeneralPurposeKeys(std::size_t crypto_revision); void DeriveGeneralPurposeKeys(std::size_t crypto_revision);
void DeriveETicketRSAKey(); RSAKeyPair<2048> GetETicketRSAKey() const;
void SetKeyWrapped(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); void SetKeyWrapped(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0);
void SetKeyWrapped(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); void SetKeyWrapped(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0);

View file

@ -164,6 +164,24 @@ VirtualFile NSP::GetNCAFile(u64 title_id, ContentRecordType type, TitleType titl
return nullptr; return nullptr;
} }
std::vector<Core::Crypto::Key128> NSP::GetTitlekey() const {
if (extracted)
LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
std::vector<Core::Crypto::Key128> 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<VirtualFile> NSP::GetFiles() const { std::vector<VirtualFile> NSP::GetFiles() const {
return pfs->GetFiles(); return pfs->GetFiles();
} }
@ -190,17 +208,22 @@ void NSP::SetTicketKeys(const std::vector<VirtualFile>& files) {
continue; continue;
} }
Core::Crypto::Ticket ticket{}; if (ticket_file->GetSize() <
if (!Core::Crypto::Ticket::Read(ticket, ticket_file)) { Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) {
LOG_WARNING(Common_Filesystem, "Could not read NSP ticket {}", ticket_file->GetName());
continue; continue;
} }
if (ticket.GetData().type == Core::Crypto::TitleKeyType::Common) { Core::Crypto::Key128 key{};
keys.AddTicketCommon(ticket); ticket_file->Read(key.data(), key.size(), Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET);
} else {
keys.AddTicketPersonalized(ticket); // 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]);
} }
} }

View file

@ -52,6 +52,7 @@ public:
TitleType title_type = TitleType::Application) const; TitleType title_type = TitleType::Application) const;
VirtualFile GetNCAFile(u64 title_id, ContentRecordType type, VirtualFile GetNCAFile(u64 title_id, ContentRecordType type,
TitleType title_type = TitleType::Application) const; TitleType title_type = TitleType::Application) const;
std::vector<Core::Crypto::Key128> GetTitlekey() const;
std::vector<VirtualFile> GetFiles() const override; std::vector<VirtualFile> GetFiles() const override;