summaryrefslogtreecommitdiffstats
path: root/src/core/crypto
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/core/crypto/key_manager.cpp245
-rw-r--r--src/core/crypto/key_manager.h116
-rw-r--r--src/core/crypto/partition_data_manager.cpp135
-rw-r--r--src/core/crypto/partition_data_manager.h1
4 files changed, 336 insertions, 161 deletions
diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp
index dc006e2bb..46aceec3d 100644
--- a/src/core/crypto/key_manager.cpp
+++ b/src/core/crypto/key_manager.cpp
@@ -37,6 +37,7 @@
namespace Core::Crypto {
constexpr u64 CURRENT_CRYPTO_REVISION = 0x5;
+constexpr u64 FULL_TICKET_SIZE = 0x400;
using namespace Common;
@@ -55,6 +56,99 @@ const std::map<std::pair<S128KeyType, u64>, std::string> KEYS_VARIABLE_LENGTH{
{{S128KeyType::KeyblobMAC, 0}, "keyblob_mac_key_"},
};
+namespace {
+template <std::size_t Size>
+bool IsAllZeroArray(const std::array<u8, Size>& array) {
+ return std::all_of(array.begin(), array.end(), [](const auto& elem) { return elem == 0; });
+}
+} // namespace
+
+u64 GetSignatureTypeDataSize(SignatureType type) {
+ switch (type) {
+ case SignatureType::RSA_4096_SHA1:
+ case SignatureType::RSA_4096_SHA256:
+ return 0x200;
+ case SignatureType::RSA_2048_SHA1:
+ case SignatureType::RSA_2048_SHA256:
+ return 0x100;
+ case SignatureType::ECDSA_SHA1:
+ case SignatureType::ECDSA_SHA256:
+ return 0x3C;
+ }
+ UNREACHABLE();
+}
+
+u64 GetSignatureTypePaddingSize(SignatureType type) {
+ switch (type) {
+ case SignatureType::RSA_4096_SHA1:
+ case SignatureType::RSA_4096_SHA256:
+ case SignatureType::RSA_2048_SHA1:
+ case SignatureType::RSA_2048_SHA256:
+ return 0x3C;
+ case SignatureType::ECDSA_SHA1:
+ case SignatureType::ECDSA_SHA256:
+ return 0x40;
+ }
+ UNREACHABLE();
+}
+
+SignatureType Ticket::GetSignatureType() const {
+ if (auto ticket = std::get_if<RSA4096Ticket>(&data)) {
+ return ticket->sig_type;
+ }
+ if (auto ticket = std::get_if<RSA2048Ticket>(&data)) {
+ return ticket->sig_type;
+ }
+ if (auto ticket = std::get_if<ECDSATicket>(&data)) {
+ return ticket->sig_type;
+ }
+
+ UNREACHABLE();
+}
+
+TicketData& Ticket::GetData() {
+ if (auto ticket = std::get_if<RSA4096Ticket>(&data)) {
+ return ticket->data;
+ }
+ if (auto ticket = std::get_if<RSA2048Ticket>(&data)) {
+ return ticket->data;
+ }
+ if (auto ticket = std::get_if<ECDSATicket>(&data)) {
+ return ticket->data;
+ }
+
+ UNREACHABLE();
+}
+
+const TicketData& Ticket::GetData() const {
+ if (auto ticket = std::get_if<RSA4096Ticket>(&data)) {
+ return ticket->data;
+ }
+ if (auto ticket = std::get_if<RSA2048Ticket>(&data)) {
+ return ticket->data;
+ }
+ if (auto ticket = std::get_if<ECDSATicket>(&data)) {
+ return ticket->data;
+ }
+
+ UNREACHABLE();
+}
+
+u64 Ticket::GetSize() const {
+ const auto sig_type = GetSignatureType();
+
+ return sizeof(SignatureType) + GetSignatureTypeDataSize(sig_type) +
+ GetSignatureTypePaddingSize(sig_type) + sizeof(TicketData);
+}
+
+Ticket Ticket::SynthesizeCommon(Key128 title_key, const std::array<u8, 16>& rights_id) {
+ RSA2048Ticket out{};
+ out.sig_type = SignatureType::RSA_2048_SHA256;
+ out.data.rights_id = rights_id;
+ out.data.title_key_common = title_key;
+ return Ticket{out};
+}
+
Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed) {
Key128 out{};
@@ -135,6 +229,27 @@ void KeyManager::DeriveGeneralPurposeKeys(std::size_t crypto_revision) {
}
}
+RSAKeyPair<2048> KeyManager::GetETicketRSAKey() const {
+ if (IsAllZeroArray(eticket_extended_kek) || !HasKey(S128KeyType::ETicketRSAKek))
+ return {};
+
+ const auto eticket_final = GetKey(S128KeyType::ETicketRSAKek);
+
+ std::vector<u8> extended_iv(eticket_extended_kek.begin(), eticket_extended_kek.begin() + 0x10);
+ std::array<u8, 0x230> extended_dec{};
+ AESCipher<Key128> rsa_1(eticket_final, Mode::CTR);
+ rsa_1.SetIV(extended_iv);
+ 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;
+}
+
Key128 DeriveKeyblobMACKey(const Key128& keyblob_key, const Key128& mac_source) {
AESCipher<Key128> mac_cipher(keyblob_key, Mode::ECB);
Key128 mac_key{};
@@ -237,7 +352,7 @@ Loader::ResultStatus DeriveSDKeys(std::array<Key256, 2>& sd_keys, KeyManager& ke
return Loader::ResultStatus::Success;
}
-std::vector<TicketRaw> GetTicketblob(const FileUtil::IOFile& ticket_save) {
+std::vector<Ticket> GetTicketblob(const FileUtil::IOFile& ticket_save) {
if (!ticket_save.IsOpen())
return {};
@@ -246,14 +361,14 @@ std::vector<TicketRaw> GetTicketblob(const FileUtil::IOFile& ticket_save) {
return {};
}
- std::vector<TicketRaw> out;
+ std::vector<Ticket> out;
for (std::size_t offset = 0; offset + 0x4 < buffer.size(); ++offset) {
if (buffer[offset] == 0x4 && buffer[offset + 1] == 0x0 && buffer[offset + 2] == 0x1 &&
buffer[offset + 3] == 0x0) {
out.emplace_back();
auto& next = out.back();
- std::memcpy(&next, buffer.data() + offset, sizeof(TicketRaw));
- offset += next.size();
+ std::memcpy(&next, buffer.data() + offset, sizeof(Ticket));
+ offset += FULL_TICKET_SIZE;
}
}
@@ -305,29 +420,23 @@ static std::optional<u64> FindTicketOffset(const std::array<u8, size>& data) {
return offset;
}
-std::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket,
+std::optional<std::pair<Key128, Key128>> ParseTicket(const Ticket& ticket,
const RSAKeyPair<2048>& key) {
- u32 cert_authority;
- std::memcpy(&cert_authority, ticket.data() + 0x140, sizeof(cert_authority));
- if (cert_authority == 0)
+ const auto issuer = ticket.GetData().issuer;
+ if (issuer == std::array<u8, 0x40>{})
return {};
- if (cert_authority != Common::MakeMagic('R', 'o', 'o', 't')) {
- LOG_INFO(Crypto,
- "Attempting to parse ticket with non-standard certificate authority {:08X}.",
- cert_authority);
+ 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;
- std::memcpy(rights_id.data(), ticket.data() + 0x2A0, sizeof(Key128));
+ Key128 rights_id = ticket.GetData().rights_id;
if (rights_id == Key128{})
return {};
- Key128 key_temp{};
-
- if (!std::any_of(ticket.begin() + 0x190, ticket.begin() + 0x280, [](u8 b) { return b != 0; })) {
- std::memcpy(key_temp.data(), ticket.data() + 0x180, key_temp.size());
- return std::make_pair(rights_id, key_temp);
+ 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);
}
mbedtls_mpi D; // RSA Private Exponent
@@ -342,7 +451,7 @@ std::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket,
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.data() + 0x180, 0x100);
+ mbedtls_mpi_read_binary(&S, ticket.GetData().title_key_block.data(), 0x100);
mbedtls_mpi_exp_mod(&M, &S, &D, &N, nullptr);
@@ -366,6 +475,7 @@ std::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket,
return {};
ASSERT(*offset > 0);
+ Key128 key_temp{};
std::memcpy(key_temp.data(), m_2.data() + *offset, key_temp.size());
return std::make_pair(rights_id, key_temp);
@@ -450,6 +560,8 @@ void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) {
const auto index = std::stoul(out[0].substr(18, 2), nullptr, 16);
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 {
for (const auto& kv : KEYS_VARIABLE_LENGTH) {
if (!ValidCryptoRevisionString(out[0], kv.second.size(), 2))
@@ -572,7 +684,7 @@ void KeyManager::WriteKeyToFile(KeyCategory category, std::string_view keyname,
<< "# If you are experiencing issues involving keys, it may help to delete this file\n";
}
- file << fmt::format("\n{} = {}", keyname, Common::HexArrayToString(key));
+ file << fmt::format("\n{} = {}", keyname, Common::HexToString(key));
AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, filename, category == KeyCategory::Title);
}
@@ -583,7 +695,7 @@ void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) {
Key128 rights_id;
std::memcpy(rights_id.data(), &field2, sizeof(u64));
std::memcpy(rights_id.data() + sizeof(u64), &field1, sizeof(u64));
- WriteKeyToFile(KeyCategory::Title, Common::HexArrayToString(rights_id), key);
+ WriteKeyToFile(KeyCategory::Title, Common::HexToString(rights_id), key);
}
auto category = KeyCategory::Standard;
@@ -862,20 +974,19 @@ void KeyManager::DeriveETicket(PartitionDataManager& data) {
// Titlekeys
data.DecryptProdInfo(GetBISKey(0));
- const auto eticket_extended_kek = data.GetETicketExtendedKek();
+ eticket_extended_kek = data.GetETicketExtendedKek();
+ WriteKeyToFile(KeyCategory::Console, "eticket_extended_kek", eticket_extended_kek);
+ PopulateTickets();
+}
- std::vector<u8> extended_iv(0x10);
- std::memcpy(extended_iv.data(), eticket_extended_kek.data(), extended_iv.size());
- std::array<u8, 0x230> extended_dec{};
- AESCipher<Key128> rsa_1(eticket_final, Mode::CTR);
- rsa_1.SetIV(extended_iv);
- rsa_1.Transcode(eticket_extended_kek.data() + 0x10, eticket_extended_kek.size() - 0x10,
- extended_dec.data(), Op::Decrypt);
+void KeyManager::PopulateTickets() {
+ const auto rsa_key = GetETicketRSAKey();
- 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());
+ if (rsa_key == RSAKeyPair<2048>{})
+ return;
+
+ if (!common_tickets.empty() && !personal_tickets.empty())
+ return;
const FileUtil::IOFile save1(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
"/system/save/80000000000000e1",
@@ -886,19 +997,41 @@ void KeyManager::DeriveETicket(PartitionDataManager& data) {
const auto blob2 = GetTicketblob(save2);
auto res = GetTicketblob(save1);
+ const auto idx = res.size();
res.insert(res.end(), blob2.begin(), blob2.end());
- for (const auto& raw : res) {
- const auto pair = ParseTicket(raw, rsa_key);
+ for (std::size_t i = 0; i < res.size(); ++i) {
+ const auto common = i < idx;
+ const auto pair = ParseTicket(res[i], rsa_key);
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]);
}
}
+void KeyManager::SynthesizeTickets() {
+ for (const auto& key : s128_keys) {
+ if (key.first.type != S128KeyType::Titlekey) {
+ continue;
+ }
+ u128 rights_id{key.first.field1, key.first.field2};
+ Key128 rights_id_2;
+ std::memcpy(rights_id_2.data(), rights_id.data(), rights_id_2.size());
+ const auto ticket = Ticket::SynthesizeCommon(key.second, rights_id_2);
+ common_tickets.insert_or_assign(rights_id, ticket);
+ }
+}
+
void KeyManager::SetKeyWrapped(S128KeyType id, Key128 key, u64 field1, u64 field2) {
if (key == Key128{})
return;
@@ -997,6 +1130,46 @@ void KeyManager::PopulateFromPartitionData(PartitionDataManager& data) {
DeriveBase();
}
+const std::map<u128, Ticket>& KeyManager::GetCommonTickets() const {
+ return common_tickets;
+}
+
+const std::map<u128, Ticket>& KeyManager::GetPersonalizedTickets() const {
+ return personal_tickets;
+}
+
+bool KeyManager::AddTicketCommon(Ticket raw) {
+ const auto rsa_key = GetETicketRSAKey();
+ if (rsa_key == RSAKeyPair<2048>{})
+ return false;
+
+ const auto pair = ParseTicket(raw, rsa_key);
+ if (!pair)
+ return false;
+ const auto& [rid, key] = *pair;
+ 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) {
+ const auto rsa_key = GetETicketRSAKey();
+ if (rsa_key == RSAKeyPair<2048>{})
+ return false;
+
+ const auto pair = ParseTicket(raw, rsa_key);
+ if (!pair)
+ return false;
+ const auto& [rid, key] = *pair;
+ 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;
+}
+
const boost::container::flat_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = {
{"eticket_rsa_kek", {S128KeyType::ETicketRSAKek, 0, 0}},
{"eticket_rsa_kek_source",
diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h
index 22f268c65..7265c4171 100644
--- a/src/core/crypto/key_manager.h
+++ b/src/core/crypto/key_manager.h
@@ -9,8 +9,10 @@
#include <optional>
#include <string>
+#include <variant>
#include <boost/container/flat_map.hpp>
#include <fmt/format.h>
+#include "common/common_funcs.h"
#include "common/common_types.h"
#include "core/crypto/partition_data_manager.h"
#include "core/file_sys/vfs_types.h"
@@ -30,7 +32,79 @@ constexpr u64 TICKET_FILE_TITLEKEY_OFFSET = 0x180;
using Key128 = std::array<u8, 0x10>;
using Key256 = std::array<u8, 0x20>;
using SHA256Hash = std::array<u8, 0x20>;
-using TicketRaw = std::array<u8, 0x400>;
+
+enum class SignatureType {
+ RSA_4096_SHA1 = 0x10000,
+ RSA_2048_SHA1 = 0x10001,
+ ECDSA_SHA1 = 0x10002,
+ RSA_4096_SHA256 = 0x10003,
+ RSA_2048_SHA256 = 0x10004,
+ ECDSA_SHA256 = 0x10005,
+};
+
+u64 GetSignatureTypeDataSize(SignatureType type);
+u64 GetSignatureTypePaddingSize(SignatureType type);
+
+enum class TitleKeyType : u8 {
+ Common = 0,
+ Personalized = 1,
+};
+
+struct TicketData {
+ std::array<u8, 0x40> issuer;
+ union {
+ std::array<u8, 0x100> title_key_block;
+
+ struct {
+ Key128 title_key_common;
+ std::array<u8, 0xF0> title_key_common_pad;
+ };
+ };
+
+ INSERT_PADDING_BYTES(0x1);
+ TitleKeyType type;
+ INSERT_PADDING_BYTES(0x3);
+ u8 revision;
+ INSERT_PADDING_BYTES(0xA);
+ u64 ticket_id;
+ u64 device_id;
+ std::array<u8, 0x10> rights_id;
+ u32 account_id;
+ INSERT_PADDING_BYTES(0x14C);
+};
+static_assert(sizeof(TicketData) == 0x2C0, "TicketData has incorrect size.");
+
+struct RSA4096Ticket {
+ SignatureType sig_type;
+ std::array<u8, 0x200> sig_data;
+ INSERT_PADDING_BYTES(0x3C);
+ TicketData data;
+};
+
+struct RSA2048Ticket {
+ SignatureType sig_type;
+ std::array<u8, 0x100> sig_data;
+ INSERT_PADDING_BYTES(0x3C);
+ TicketData data;
+};
+
+struct ECDSATicket {
+ SignatureType sig_type;
+ std::array<u8, 0x3C> sig_data;
+ INSERT_PADDING_BYTES(0x40);
+ TicketData data;
+};
+
+struct Ticket {
+ std::variant<RSA4096Ticket, RSA2048Ticket, ECDSATicket> data;
+
+ SignatureType GetSignatureType() const;
+ TicketData& GetData();
+ const TicketData& GetData() const;
+ u64 GetSize() const;
+
+ static Ticket SynthesizeCommon(Key128 title_key, const std::array<u8, 0x10>& rights_id);
+};
static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big.");
static_assert(sizeof(Key256) == 32, "Key256 must be 256 bytes big.");
@@ -43,6 +117,19 @@ struct RSAKeyPair {
std::array<u8, 4> exponent;
};
+template <size_t bit_size, size_t byte_size>
+bool operator==(const RSAKeyPair<bit_size, byte_size>& lhs,
+ const RSAKeyPair<bit_size, byte_size>& rhs) {
+ return std::tie(lhs.encryption_key, lhs.decryption_key, lhs.modulus, lhs.exponent) ==
+ std::tie(rhs.encryption_key, rhs.decryption_key, rhs.modulus, rhs.exponent);
+}
+
+template <size_t bit_size, size_t byte_size>
+bool operator!=(const RSAKeyPair<bit_size, byte_size>& lhs,
+ const RSAKeyPair<bit_size, byte_size>& rhs) {
+ return !(lhs == rhs);
+}
+
enum class KeyCategory : u8 {
Standard,
Title,
@@ -151,22 +238,35 @@ public:
static bool KeyFileExists(bool title);
- // Call before using the sd seed to attempt to derive it if it dosen't exist. Needs system save
- // 8*43 and the private file to exist.
+ // Call before using the sd seed to attempt to derive it if it dosen't exist. Needs system
+ // save 8*43 and the private file to exist.
void DeriveSDSeedLazy();
bool BaseDeriveNecessary() const;
void DeriveBase();
void DeriveETicket(PartitionDataManager& data);
+ void PopulateTickets();
+ void SynthesizeTickets();
void PopulateFromPartitionData(PartitionDataManager& data);
+ const std::map<u128, Ticket>& GetCommonTickets() const;
+ const std::map<u128, Ticket>& GetPersonalizedTickets() const;
+
+ bool AddTicketCommon(Ticket raw);
+ bool AddTicketPersonalized(Ticket raw);
+
private:
std::map<KeyIndex<S128KeyType>, Key128> s128_keys;
std::map<KeyIndex<S256KeyType>, Key256> s256_keys;
+ // Map from rights ID to ticket
+ std::map<u128, Ticket> common_tickets;
+ std::map<u128, Ticket> personal_tickets;
+
std::array<std::array<u8, 0xB0>, 0x20> encrypted_keyblobs{};
std::array<std::array<u8, 0x90>, 0x20> keyblobs{};
+ std::array<u8, 576> eticket_extended_kek{};
bool dev_mode;
void LoadFromFile(const std::string& filename, bool is_title_keys);
@@ -178,6 +278,8 @@ private:
void DeriveGeneralPurposeKeys(std::size_t crypto_revision);
+ RSAKeyPair<2048> GetETicketRSAKey() const;
+
void SetKeyWrapped(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0);
void SetKeyWrapped(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0);
@@ -195,11 +297,11 @@ std::array<u8, 0x90> DecryptKeyblob(const std::array<u8, 0xB0>& encrypted_keyblo
std::optional<Key128> DeriveSDSeed();
Loader::ResultStatus DeriveSDKeys(std::array<Key256, 2>& sd_keys, KeyManager& keys);
-std::vector<TicketRaw> GetTicketblob(const FileUtil::IOFile& ticket_save);
+std::vector<Ticket> GetTicketblob(const FileUtil::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 TicketRaw& ticket,
+// 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
diff --git a/src/core/crypto/partition_data_manager.cpp b/src/core/crypto/partition_data_manager.cpp
index ed0775444..594cd82c5 100644
--- a/src/core/crypto/partition_data_manager.cpp
+++ b/src/core/crypto/partition_data_manager.cpp
@@ -22,8 +22,10 @@
#include "core/crypto/key_manager.h"
#include "core/crypto/partition_data_manager.h"
#include "core/crypto/xts_encryption_layer.h"
+#include "core/file_sys/kernel_executable.h"
#include "core/file_sys/vfs.h"
#include "core/file_sys/vfs_offset.h"
+#include "core/file_sys/vfs_vector.h"
using namespace Common;
@@ -45,36 +47,6 @@ struct Package2Header {
};
static_assert(sizeof(Package2Header) == 0x200, "Package2Header has incorrect size.");
-struct INIHeader {
- u32_le magic;
- u32_le size;
- u32_le process_count;
- INSERT_PADDING_BYTES(4);
-};
-static_assert(sizeof(INIHeader) == 0x10, "INIHeader has incorrect size.");
-
-struct SectionHeader {
- u32_le offset;
- u32_le size_decompressed;
- u32_le size_compressed;
- u32_le attribute;
-};
-static_assert(sizeof(SectionHeader) == 0x10, "SectionHeader has incorrect size.");
-
-struct KIPHeader {
- u32_le magic;
- std::array<char, 12> name;
- u64_le title_id;
- u32_le category;
- u8 priority;
- u8 core;
- INSERT_PADDING_BYTES(1);
- u8 flags;
- std::array<SectionHeader, 6> sections;
- std::array<u32, 0x20> capabilities;
-};
-static_assert(sizeof(KIPHeader) == 0x100, "KIPHeader has incorrect size.");
-
const std::array<SHA256Hash, 0x10> source_hashes{
"B24BD293259DBC7AC5D63F88E60C59792498E6FC5443402C7FFE87EE8B61A3F0"_array32, // keyblob_mac_key_source
"7944862A3A5C31C6720595EFD302245ABD1B54CCDCF33000557681E65C5664A4"_array32, // master_key_source
@@ -170,65 +142,6 @@ const std::array<SHA256Hash, 0x20> master_key_hashes{
"0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_1F
};
-static std::vector<u8> DecompressBLZ(const std::vector<u8>& in) {
- const auto data_size = in.size() - 0xC;
-
- u32 compressed_size{};
- u32 init_index{};
- u32 additional_size{};
- std::memcpy(&compressed_size, in.data() + data_size, sizeof(u32));
- std::memcpy(&init_index, in.data() + data_size + 0x4, sizeof(u32));
- std::memcpy(&additional_size, in.data() + data_size + 0x8, sizeof(u32));
-
- std::vector<u8> out(in.size() + additional_size);
-
- if (compressed_size == in.size())
- std::memcpy(out.data(), in.data() + in.size() - compressed_size, compressed_size);
- else
- std::memcpy(out.data(), in.data(), compressed_size);
-
- auto index = in.size() - init_index;
- auto out_index = out.size();
-
- while (out_index > 0) {
- --index;
- auto control = in[index];
- for (size_t i = 0; i < 8; ++i) {
- if ((control & 0x80) > 0) {
- ASSERT(index >= 2);
- index -= 2;
- u64 segment_offset = in[index] | in[index + 1] << 8;
- u64 segment_size = ((segment_offset >> 12) & 0xF) + 3;
- segment_offset &= 0xFFF;
- segment_offset += 3;
-
- if (out_index < segment_size)
- segment_size = out_index;
-
- ASSERT(out_index >= segment_size);
-
- out_index -= segment_size;
-
- for (size_t j = 0; j < segment_size; ++j) {
- ASSERT(out_index + j + segment_offset < out.size());
- out[out_index + j] = out[out_index + j + segment_offset];
- }
- } else {
- ASSERT(out_index >= 1);
- --out_index;
- --index;
- out[out_index] = in[index];
- }
-
- control <<= 1;
- if (out_index == 0)
- return out;
- }
- }
-
- return out;
-}
-
static u8 CalculateMaxKeyblobSourceHash() {
for (s8 i = 0x1F; i >= 0; --i) {
if (keyblob_source_hashes[i] != SHA256Hash{})
@@ -478,37 +391,22 @@ void PartitionDataManager::DecryptPackage2(const std::array<Key128, 0x20>& packa
cipher.SetIV({header.section_ctr[1].begin(), header.section_ctr[1].end()});
cipher.Transcode(c.data(), c.size(), c.data(), Op::Decrypt);
- INIHeader ini;
- std::memcpy(&ini, c.data(), sizeof(INIHeader));
- if (ini.magic != Common::MakeMagic('I', 'N', 'I', '1'))
+ const auto ini_file = std::make_shared<FileSys::VectorVfsFile>(c);
+ const FileSys::INI ini{ini_file};
+ if (ini.GetStatus() != Loader::ResultStatus::Success)
return;
- u64 offset = sizeof(INIHeader);
- for (size_t i = 0; i < ini.process_count; ++i) {
- KIPHeader kip;
- std::memcpy(&kip, c.data() + offset, sizeof(KIPHeader));
- if (kip.magic != Common::MakeMagic('K', 'I', 'P', '1'))
+ for (const auto& kip : ini.GetKIPs()) {
+ if (kip.GetStatus() != Loader::ResultStatus::Success)
return;
- const auto name =
- Common::StringFromFixedZeroTerminatedBuffer(kip.name.data(), kip.name.size());
-
- if (name != "FS" && name != "spl") {
- offset += sizeof(KIPHeader) + kip.sections[0].size_compressed +
- kip.sections[1].size_compressed + kip.sections[2].size_compressed;
+ if (kip.GetName() != "FS" && kip.GetName() != "spl") {
continue;
}
- const u64 initial_offset = sizeof(KIPHeader) + offset;
- const auto text_begin = c.cbegin() + initial_offset;
- const auto text_end = text_begin + kip.sections[0].size_compressed;
- const std::vector<u8> text = DecompressBLZ({text_begin, text_end});
-
- const auto rodata_end = text_end + kip.sections[1].size_compressed;
- const std::vector<u8> rodata = DecompressBLZ({text_end, rodata_end});
-
- const auto data_end = rodata_end + kip.sections[2].size_compressed;
- const std::vector<u8> data = DecompressBLZ({rodata_end, data_end});
+ const auto& text = kip.GetTextSection();
+ const auto& rodata = kip.GetRODataSection();
+ const auto& data = kip.GetDataSection();
std::vector<u8> out;
out.reserve(text.size() + rodata.size() + data.size());
@@ -516,12 +414,9 @@ void PartitionDataManager::DecryptPackage2(const std::array<Key128, 0x20>& packa
out.insert(out.end(), rodata.begin(), rodata.end());
out.insert(out.end(), data.begin(), data.end());
- offset += sizeof(KIPHeader) + kip.sections[0].size_compressed +
- kip.sections[1].size_compressed + kip.sections[2].size_compressed;
-
- if (name == "FS")
+ if (kip.GetName() == "FS")
package2_fs[static_cast<size_t>(type)] = std::move(out);
- else if (name == "spl")
+ else if (kip.GetName() == "spl")
package2_spl[static_cast<size_t>(type)] = std::move(out);
}
}
@@ -585,6 +480,10 @@ void PartitionDataManager::DecryptProdInfo(std::array<u8, 0x20> bis_key) {
prodinfo_decrypted = std::make_shared<XTSEncryptionLayer>(prodinfo, bis_key);
}
+FileSys::VirtualFile PartitionDataManager::GetDecryptedProdInfo() const {
+ return prodinfo_decrypted;
+}
+
std::array<u8, 576> PartitionDataManager::GetETicketExtendedKek() const {
std::array<u8, 0x240> out{};
if (prodinfo_decrypted != nullptr)
diff --git a/src/core/crypto/partition_data_manager.h b/src/core/crypto/partition_data_manager.h
index 0ad007c72..7a7b5d038 100644
--- a/src/core/crypto/partition_data_manager.h
+++ b/src/core/crypto/partition_data_manager.h
@@ -84,6 +84,7 @@ public:
bool HasProdInfo() const;
FileSys::VirtualFile GetProdInfoRaw() const;
void DecryptProdInfo(std::array<u8, 0x20> bis_key);
+ FileSys::VirtualFile GetDecryptedProdInfo() const;
std::array<u8, 0x240> GetETicketExtendedKek() const;
private: