2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-07-27 23:55:23 -04:00
|
|
|
|
2018-08-04 16:41:17 -04:00
|
|
|
#include <algorithm>
|
2018-07-29 19:00:09 -04:00
|
|
|
#include <array>
|
2018-09-23 21:04:13 -04:00
|
|
|
#include <bitset>
|
2018-09-29 11:48:51 -04:00
|
|
|
#include <cctype>
|
2018-07-27 23:55:23 -04:00
|
|
|
#include <fstream>
|
|
|
|
#include <locale>
|
2018-09-23 21:04:13 -04:00
|
|
|
#include <map>
|
2018-07-27 23:55:23 -04:00
|
|
|
#include <sstream>
|
2018-09-03 21:58:19 -04:00
|
|
|
#include <tuple>
|
|
|
|
#include <vector>
|
2018-09-23 21:04:13 -04:00
|
|
|
#include <mbedtls/bignum.h>
|
|
|
|
#include <mbedtls/cipher.h>
|
|
|
|
#include <mbedtls/cmac.h>
|
|
|
|
#include <mbedtls/sha256.h>
|
2021-05-25 19:32:56 -04:00
|
|
|
#include "common/fs/file.h"
|
|
|
|
#include "common/fs/fs.h"
|
|
|
|
#include "common/fs/path_util.h"
|
2018-08-09 20:45:04 -04:00
|
|
|
#include "common/hex_util.h"
|
|
|
|
#include "common/logging/log.h"
|
2021-04-14 19:07:40 -04:00
|
|
|
#include "common/settings.h"
|
2019-11-12 06:56:31 -05:00
|
|
|
#include "common/string_util.h"
|
2018-08-16 17:12:05 -04:00
|
|
|
#include "core/crypto/aes_util.h"
|
2018-07-27 23:55:23 -04:00
|
|
|
#include "core/crypto/key_manager.h"
|
2018-09-29 11:48:51 -04:00
|
|
|
#include "core/crypto/partition_data_manager.h"
|
2018-09-23 21:04:13 -04:00
|
|
|
#include "core/file_sys/content_archive.h"
|
|
|
|
#include "core/file_sys/nca_metadata.h"
|
|
|
|
#include "core/file_sys/registered_cache.h"
|
|
|
|
#include "core/hle/service/filesystem/filesystem.h"
|
2018-09-03 21:58:19 -04:00
|
|
|
#include "core/loader/loader.h"
|
2018-07-27 23:55:23 -04:00
|
|
|
|
2018-07-28 21:39:42 -04:00
|
|
|
namespace Core::Crypto {
|
2020-08-22 22:04:15 -04:00
|
|
|
namespace {
|
2018-07-27 23:55:23 -04:00
|
|
|
|
2018-09-23 20:51:44 -04:00
|
|
|
constexpr u64 CURRENT_CRYPTO_REVISION = 0x5;
|
|
|
|
|
2020-08-03 13:18:43 -04:00
|
|
|
using Common::AsArray;
|
2018-09-23 21:04:13 -04:00
|
|
|
|
2020-08-03 13:18:43 -04:00
|
|
|
// clang-format off
|
|
|
|
constexpr std::array eticket_source_hashes{
|
|
|
|
AsArray("B71DB271DC338DF380AA2C4335EF8873B1AFD408E80B3582D8719FC81C5E511C"), // eticket_rsa_kek_source
|
|
|
|
AsArray("E8965A187D30E57869F562D04383C996DE487BBA5761363D2D4D32391866A85C"), // eticket_rsa_kekek_source
|
2018-09-23 21:04:13 -04:00
|
|
|
};
|
2020-08-03 13:18:43 -04:00
|
|
|
// clang-format on
|
2018-09-23 21:04:13 -04:00
|
|
|
|
2020-08-22 22:04:15 -04:00
|
|
|
constexpr std::array<std::pair<std::string_view, KeyIndex<S128KeyType>>, 30> s128_file_id{{
|
|
|
|
{"eticket_rsa_kek", {S128KeyType::ETicketRSAKek, 0, 0}},
|
|
|
|
{"eticket_rsa_kek_source",
|
|
|
|
{S128KeyType::Source, static_cast<u64>(SourceKeyType::ETicketKek), 0}},
|
|
|
|
{"eticket_rsa_kekek_source",
|
|
|
|
{S128KeyType::Source, static_cast<u64>(SourceKeyType::ETicketKekek), 0}},
|
|
|
|
{"rsa_kek_mask_0", {S128KeyType::RSAKek, static_cast<u64>(RSAKekType::Mask0), 0}},
|
|
|
|
{"rsa_kek_seed_3", {S128KeyType::RSAKek, static_cast<u64>(RSAKekType::Seed3), 0}},
|
|
|
|
{"rsa_oaep_kek_generation_source",
|
|
|
|
{S128KeyType::Source, static_cast<u64>(SourceKeyType::RSAOaepKekGeneration), 0}},
|
|
|
|
{"sd_card_kek_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKek), 0}},
|
|
|
|
{"aes_kek_generation_source",
|
|
|
|
{S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration), 0}},
|
|
|
|
{"aes_key_generation_source",
|
|
|
|
{S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration), 0}},
|
|
|
|
{"package2_key_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::Package2), 0}},
|
|
|
|
{"master_key_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::Master), 0}},
|
|
|
|
{"header_kek_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::HeaderKek), 0}},
|
|
|
|
{"key_area_key_application_source",
|
|
|
|
{S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey),
|
|
|
|
static_cast<u64>(KeyAreaKeyType::Application)}},
|
|
|
|
{"key_area_key_ocean_source",
|
|
|
|
{S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey),
|
|
|
|
static_cast<u64>(KeyAreaKeyType::Ocean)}},
|
|
|
|
{"key_area_key_system_source",
|
|
|
|
{S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey),
|
|
|
|
static_cast<u64>(KeyAreaKeyType::System)}},
|
|
|
|
{"titlekek_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::Titlekek), 0}},
|
|
|
|
{"keyblob_mac_key_source",
|
|
|
|
{S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyblobMAC), 0}},
|
|
|
|
{"tsec_key", {S128KeyType::TSEC, 0, 0}},
|
|
|
|
{"secure_boot_key", {S128KeyType::SecureBoot, 0, 0}},
|
|
|
|
{"sd_seed", {S128KeyType::SDSeed, 0, 0}},
|
|
|
|
{"bis_key_0_crypt", {S128KeyType::BIS, 0, static_cast<u64>(BISKeyType::Crypto)}},
|
|
|
|
{"bis_key_0_tweak", {S128KeyType::BIS, 0, static_cast<u64>(BISKeyType::Tweak)}},
|
|
|
|
{"bis_key_1_crypt", {S128KeyType::BIS, 1, static_cast<u64>(BISKeyType::Crypto)}},
|
|
|
|
{"bis_key_1_tweak", {S128KeyType::BIS, 1, static_cast<u64>(BISKeyType::Tweak)}},
|
|
|
|
{"bis_key_2_crypt", {S128KeyType::BIS, 2, static_cast<u64>(BISKeyType::Crypto)}},
|
|
|
|
{"bis_key_2_tweak", {S128KeyType::BIS, 2, static_cast<u64>(BISKeyType::Tweak)}},
|
|
|
|
{"bis_key_3_crypt", {S128KeyType::BIS, 3, static_cast<u64>(BISKeyType::Crypto)}},
|
|
|
|
{"bis_key_3_tweak", {S128KeyType::BIS, 3, static_cast<u64>(BISKeyType::Tweak)}},
|
|
|
|
{"header_kek", {S128KeyType::HeaderKek, 0, 0}},
|
|
|
|
{"sd_card_kek", {S128KeyType::SDKek, 0, 0}},
|
|
|
|
}};
|
|
|
|
|
|
|
|
auto Find128ByName(std::string_view name) {
|
|
|
|
return std::find_if(s128_file_id.begin(), s128_file_id.end(),
|
|
|
|
[&name](const auto& pair) { return pair.first == name; });
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr std::array<std::pair<std::string_view, KeyIndex<S256KeyType>>, 6> s256_file_id{{
|
|
|
|
{"header_key", {S256KeyType::Header, 0, 0}},
|
|
|
|
{"sd_card_save_key_source", {S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::Save), 0}},
|
|
|
|
{"sd_card_nca_key_source", {S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::NCA), 0}},
|
|
|
|
{"header_key_source", {S256KeyType::HeaderSource, 0, 0}},
|
|
|
|
{"sd_card_save_key", {S256KeyType::SDKey, static_cast<u64>(SDKeyType::Save), 0}},
|
|
|
|
{"sd_card_nca_key", {S256KeyType::SDKey, static_cast<u64>(SDKeyType::NCA), 0}},
|
|
|
|
}};
|
|
|
|
|
|
|
|
auto Find256ByName(std::string_view name) {
|
|
|
|
return std::find_if(s256_file_id.begin(), s256_file_id.end(),
|
|
|
|
[&name](const auto& pair) { return pair.first == name; });
|
|
|
|
}
|
|
|
|
|
|
|
|
using KeyArray = std::array<std::pair<std::pair<S128KeyType, u64>, std::string_view>, 7>;
|
|
|
|
constexpr KeyArray KEYS_VARIABLE_LENGTH{{
|
2018-09-29 11:48:51 -04:00
|
|
|
{{S128KeyType::Master, 0}, "master_key_"},
|
|
|
|
{{S128KeyType::Package1, 0}, "package1_key_"},
|
|
|
|
{{S128KeyType::Package2, 0}, "package2_key_"},
|
|
|
|
{{S128KeyType::Titlekek, 0}, "titlekek_"},
|
|
|
|
{{S128KeyType::Source, static_cast<u64>(SourceKeyType::Keyblob)}, "keyblob_key_source_"},
|
|
|
|
{{S128KeyType::Keyblob, 0}, "keyblob_key_"},
|
|
|
|
{{S128KeyType::KeyblobMAC, 0}, "keyblob_mac_key_"},
|
2020-08-22 22:04:15 -04:00
|
|
|
}};
|
2018-09-29 11:48:51 -04:00
|
|
|
|
2019-05-26 13:01:42 -04:00
|
|
|
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; });
|
|
|
|
}
|
2020-08-22 22:04:15 -04:00
|
|
|
} // Anonymous namespace
|
2019-05-26 13:01:42 -04:00
|
|
|
|
2019-04-16 09:12:04 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
bool Ticket::IsValid() const {
|
|
|
|
return !std::holds_alternative<std::monostate>(data);
|
|
|
|
}
|
|
|
|
|
2019-05-26 13:01:42 -04:00
|
|
|
SignatureType Ticket::GetSignatureType() const {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (const auto* ticket = std::get_if<RSA4096Ticket>(&data)) {
|
2019-05-26 13:01:42 -04:00
|
|
|
return ticket->sig_type;
|
|
|
|
}
|
2020-08-15 08:33:16 -04:00
|
|
|
if (const auto* ticket = std::get_if<RSA2048Ticket>(&data)) {
|
2019-05-26 13:01:42 -04:00
|
|
|
return ticket->sig_type;
|
|
|
|
}
|
2020-08-15 08:33:16 -04:00
|
|
|
if (const auto* ticket = std::get_if<ECDSATicket>(&data)) {
|
2019-05-26 13:01:42 -04:00
|
|
|
return ticket->sig_type;
|
|
|
|
}
|
2021-01-05 02:18:16 -05:00
|
|
|
throw std::bad_variant_access{};
|
2019-05-26 13:01:42 -04:00
|
|
|
}
|
|
|
|
|
2019-04-16 09:12:04 -04:00
|
|
|
TicketData& Ticket::GetData() {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (auto* ticket = std::get_if<RSA4096Ticket>(&data)) {
|
2019-05-26 13:01:42 -04:00
|
|
|
return ticket->data;
|
2019-04-16 09:12:04 -04:00
|
|
|
}
|
2020-08-15 08:33:16 -04:00
|
|
|
if (auto* ticket = std::get_if<RSA2048Ticket>(&data)) {
|
2019-05-26 13:01:42 -04:00
|
|
|
return ticket->data;
|
|
|
|
}
|
2020-08-15 08:33:16 -04:00
|
|
|
if (auto* ticket = std::get_if<ECDSATicket>(&data)) {
|
2019-05-26 13:01:42 -04:00
|
|
|
return ticket->data;
|
|
|
|
}
|
2021-01-05 02:18:16 -05:00
|
|
|
throw std::bad_variant_access{};
|
2019-04-16 09:12:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const TicketData& Ticket::GetData() const {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (const auto* ticket = std::get_if<RSA4096Ticket>(&data)) {
|
2019-05-26 13:01:42 -04:00
|
|
|
return ticket->data;
|
|
|
|
}
|
2020-08-15 08:33:16 -04:00
|
|
|
if (const auto* ticket = std::get_if<RSA2048Ticket>(&data)) {
|
2019-05-26 13:01:42 -04:00
|
|
|
return ticket->data;
|
2019-04-16 09:12:04 -04:00
|
|
|
}
|
2020-08-15 08:33:16 -04:00
|
|
|
if (const auto* ticket = std::get_if<ECDSATicket>(&data)) {
|
2019-05-26 13:01:42 -04:00
|
|
|
return ticket->data;
|
|
|
|
}
|
2021-01-05 02:18:16 -05:00
|
|
|
throw std::bad_variant_access{};
|
2019-04-16 09:12:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 Ticket::GetSize() const {
|
2019-05-26 13:01:42 -04:00
|
|
|
const auto sig_type = GetSignatureType();
|
|
|
|
|
2019-04-16 09:12:04 -04:00
|
|
|
return sizeof(SignatureType) + GetSignatureTypeDataSize(sig_type) +
|
|
|
|
GetSignatureTypePaddingSize(sig_type) + sizeof(TicketData);
|
|
|
|
}
|
|
|
|
|
2019-05-26 13:01:42 -04:00
|
|
|
Ticket Ticket::SynthesizeCommon(Key128 title_key, const std::array<u8, 16>& rights_id) {
|
|
|
|
RSA2048Ticket out{};
|
2019-04-16 09:12:04 -04:00
|
|
|
out.sig_type = SignatureType::RSA_2048_SHA256;
|
2019-05-26 13:01:42 -04:00
|
|
|
out.data.rights_id = rights_id;
|
|
|
|
out.data.title_key_common = title_key;
|
|
|
|
return Ticket{out};
|
2019-04-16 09:12:04 -04:00
|
|
|
}
|
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
Ticket Ticket::Read(const FileSys::VirtualFile& file) {
|
|
|
|
// Attempt to read up to the largest ticket size, and make sure we read at least a signature
|
|
|
|
// type.
|
|
|
|
std::array<u8, sizeof(RSA4096Ticket)> raw_data{};
|
|
|
|
auto read_size = file->Read(raw_data.data(), raw_data.size(), 0);
|
|
|
|
if (read_size < sizeof(SignatureType)) {
|
|
|
|
LOG_WARNING(Crypto, "Attempted to read ticket file with invalid size {}.", read_size);
|
|
|
|
return Ticket{std::monostate()};
|
|
|
|
}
|
|
|
|
return Read(std::span{raw_data});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ticket Ticket::Read(std::span<const u8> raw_data) {
|
|
|
|
// Some tools read only 0x180 bytes of ticket data instead of 0x2C0, so
|
|
|
|
// just make sure we have at least the bare minimum of data to work with.
|
|
|
|
SignatureType sig_type;
|
|
|
|
if (raw_data.size() < sizeof(SignatureType)) {
|
|
|
|
LOG_WARNING(Crypto, "Attempted to parse ticket buffer with invalid size {}.",
|
|
|
|
raw_data.size());
|
|
|
|
return Ticket{std::monostate()};
|
|
|
|
}
|
|
|
|
std::memcpy(&sig_type, raw_data.data(), sizeof(sig_type));
|
|
|
|
|
|
|
|
switch (sig_type) {
|
|
|
|
case SignatureType::RSA_4096_SHA1:
|
|
|
|
case SignatureType::RSA_4096_SHA256: {
|
|
|
|
RSA4096Ticket ticket{};
|
|
|
|
std::memcpy(&ticket, raw_data.data(), sizeof(ticket));
|
|
|
|
return Ticket{ticket};
|
|
|
|
}
|
|
|
|
case SignatureType::RSA_2048_SHA1:
|
|
|
|
case SignatureType::RSA_2048_SHA256: {
|
|
|
|
RSA2048Ticket ticket{};
|
|
|
|
std::memcpy(&ticket, raw_data.data(), sizeof(ticket));
|
|
|
|
return Ticket{ticket};
|
|
|
|
}
|
|
|
|
case SignatureType::ECDSA_SHA1:
|
|
|
|
case SignatureType::ECDSA_SHA256: {
|
|
|
|
ECDSATicket ticket{};
|
|
|
|
std::memcpy(&ticket, raw_data.data(), sizeof(ticket));
|
|
|
|
return Ticket{ticket};
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
LOG_WARNING(Crypto, "Attempted to parse ticket buffer with invalid type {}.", sig_type);
|
|
|
|
return Ticket{std::monostate()};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 17:12:05 -04:00
|
|
|
Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed) {
|
|
|
|
Key128 out{};
|
|
|
|
|
|
|
|
AESCipher<Key128> cipher1(master, Mode::ECB);
|
|
|
|
cipher1.Transcode(kek_seed.data(), kek_seed.size(), out.data(), Op::Decrypt);
|
|
|
|
AESCipher<Key128> cipher2(out, Mode::ECB);
|
|
|
|
cipher2.Transcode(source.data(), source.size(), out.data(), Op::Decrypt);
|
|
|
|
|
|
|
|
if (key_seed != Key128{}) {
|
|
|
|
AESCipher<Key128> cipher3(out, Mode::ECB);
|
|
|
|
cipher3.Transcode(key_seed.data(), key_seed.size(), out.data(), Op::Decrypt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
Key128 DeriveKeyblobKey(const Key128& sbk, const Key128& tsec, Key128 source) {
|
2018-09-23 20:51:44 -04:00
|
|
|
AESCipher<Key128> sbk_cipher(sbk, Mode::ECB);
|
|
|
|
AESCipher<Key128> tsec_cipher(tsec, Mode::ECB);
|
|
|
|
tsec_cipher.Transcode(source.data(), source.size(), source.data(), Op::Decrypt);
|
|
|
|
sbk_cipher.Transcode(source.data(), source.size(), source.data(), Op::Decrypt);
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
Key128 DeriveMasterKey(const std::array<u8, 0x90>& keyblob, const Key128& master_source) {
|
|
|
|
Key128 master_root;
|
|
|
|
std::memcpy(master_root.data(), keyblob.data(), sizeof(Key128));
|
|
|
|
|
|
|
|
AESCipher<Key128> master_cipher(master_root, Mode::ECB);
|
|
|
|
|
|
|
|
Key128 master{};
|
|
|
|
master_cipher.Transcode(master_source.data(), master_source.size(), master.data(), Op::Decrypt);
|
|
|
|
return master;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::array<u8, 144> DecryptKeyblob(const std::array<u8, 176>& encrypted_keyblob,
|
|
|
|
const Key128& key) {
|
|
|
|
std::array<u8, 0x90> keyblob;
|
|
|
|
AESCipher<Key128> cipher(key, Mode::CTR);
|
|
|
|
cipher.SetIV(std::vector<u8>(encrypted_keyblob.data() + 0x10, encrypted_keyblob.data() + 0x20));
|
|
|
|
cipher.Transcode(encrypted_keyblob.data() + 0x20, keyblob.size(), keyblob.data(), Op::Decrypt);
|
|
|
|
return keyblob;
|
|
|
|
}
|
|
|
|
|
2018-10-13 09:13:19 -04:00
|
|
|
void KeyManager::DeriveGeneralPurposeKeys(std::size_t crypto_revision) {
|
2018-09-29 11:48:51 -04:00
|
|
|
const auto kek_generation_source =
|
|
|
|
GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration));
|
|
|
|
const auto key_generation_source =
|
|
|
|
GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration));
|
|
|
|
|
|
|
|
if (HasKey(S128KeyType::Master, crypto_revision)) {
|
|
|
|
for (auto kak_type :
|
|
|
|
{KeyAreaKeyType::Application, KeyAreaKeyType::Ocean, KeyAreaKeyType::System}) {
|
|
|
|
if (HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey),
|
|
|
|
static_cast<u64>(kak_type))) {
|
|
|
|
const auto source =
|
|
|
|
GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey),
|
|
|
|
static_cast<u64>(kak_type));
|
|
|
|
const auto kek =
|
|
|
|
GenerateKeyEncryptionKey(source, GetKey(S128KeyType::Master, crypto_revision),
|
|
|
|
kek_generation_source, key_generation_source);
|
|
|
|
SetKey(S128KeyType::KeyArea, kek, crypto_revision, static_cast<u64>(kak_type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AESCipher<Key128> master_cipher(GetKey(S128KeyType::Master, crypto_revision), Mode::ECB);
|
|
|
|
for (auto key_type : {SourceKeyType::Titlekek, SourceKeyType::Package2}) {
|
|
|
|
if (HasKey(S128KeyType::Source, static_cast<u64>(key_type))) {
|
|
|
|
Key128 key{};
|
|
|
|
master_cipher.Transcode(
|
|
|
|
GetKey(S128KeyType::Source, static_cast<u64>(key_type)).data(), key.size(),
|
|
|
|
key.data(), Op::Decrypt);
|
|
|
|
SetKey(key_type == SourceKeyType::Titlekek ? S128KeyType::Titlekek
|
|
|
|
: S128KeyType::Package2,
|
|
|
|
key, crypto_revision);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
void KeyManager::DeriveETicketRSAKey() {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (IsAllZeroArray(eticket_extended_kek) || !HasKey(S128KeyType::ETicketRSAKek)) {
|
2023-09-05 15:09:42 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2019-04-10 10:22:04 -04:00
|
|
|
|
|
|
|
const auto eticket_final = GetKey(S128KeyType::ETicketRSAKek);
|
|
|
|
|
2019-05-26 13:01:42 -04:00
|
|
|
std::vector<u8> extended_iv(eticket_extended_kek.begin(), eticket_extended_kek.begin() + 0x10);
|
2019-04-10 10:22:04 -04:00
|
|
|
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);
|
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
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());
|
2019-04-10 10:22:04 -04:00
|
|
|
}
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
Key128 DeriveKeyblobMACKey(const Key128& keyblob_key, const Key128& mac_source) {
|
|
|
|
AESCipher<Key128> mac_cipher(keyblob_key, Mode::ECB);
|
|
|
|
Key128 mac_key{};
|
|
|
|
mac_cipher.Transcode(mac_source.data(), mac_key.size(), mac_key.data(), Op::Decrypt);
|
|
|
|
return mac_key;
|
|
|
|
}
|
|
|
|
|
2018-10-30 00:03:25 -04:00
|
|
|
std::optional<Key128> DeriveSDSeed() {
|
2021-05-25 19:32:56 -04:00
|
|
|
const auto system_save_43_path =
|
|
|
|
Common::FS::GetYuzuPath(Common::FS::YuzuPath::NANDDir) / "system/save/8000000000000043";
|
|
|
|
const Common::FS::IOFile save_43{system_save_43_path, Common::FS::FileAccessMode::Read,
|
|
|
|
Common::FS::FileType::BinaryFile};
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!save_43.IsOpen()) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2018-10-13 08:12:00 -04:00
|
|
|
|
2021-05-25 19:32:56 -04:00
|
|
|
const auto sd_private_path =
|
|
|
|
Common::FS::GetYuzuPath(Common::FS::YuzuPath::SDMCDir) / "Nintendo/Contents/private";
|
|
|
|
|
|
|
|
const Common::FS::IOFile sd_private{sd_private_path, Common::FS::FileAccessMode::Read,
|
|
|
|
Common::FS::FileType::BinaryFile};
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!sd_private.IsOpen()) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2018-08-16 17:12:05 -04:00
|
|
|
|
|
|
|
std::array<u8, 0x10> private_seed{};
|
2021-05-25 19:32:56 -04:00
|
|
|
if (sd_private.Read(private_seed) != private_seed.size()) {
|
2020-08-15 08:33:16 -04:00
|
|
|
return std::nullopt;
|
2018-10-13 08:12:00 -04:00
|
|
|
}
|
2018-08-16 17:12:05 -04:00
|
|
|
|
|
|
|
std::array<u8, 0x10> buffer{};
|
2021-05-25 19:32:56 -04:00
|
|
|
s64 offset = 0;
|
|
|
|
for (; offset + 0x10 < static_cast<s64>(save_43.GetSize()); ++offset) {
|
|
|
|
if (!save_43.Seek(offset)) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (save_43.Read(buffer) != buffer.size()) {
|
2020-08-15 08:33:16 -04:00
|
|
|
return std::nullopt;
|
2018-10-13 08:12:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer == private_seed) {
|
2018-08-16 17:12:05 -04:00
|
|
|
break;
|
2018-10-13 08:12:00 -04:00
|
|
|
}
|
2018-08-16 17:12:05 -04:00
|
|
|
}
|
|
|
|
|
2021-05-25 19:32:56 -04:00
|
|
|
if (!save_43.Seek(offset + 0x10)) {
|
2020-08-15 08:33:16 -04:00
|
|
|
return std::nullopt;
|
2018-10-13 08:12:00 -04:00
|
|
|
}
|
2018-08-16 17:12:05 -04:00
|
|
|
|
|
|
|
Key128 seed{};
|
2021-05-25 19:32:56 -04:00
|
|
|
if (save_43.Read(seed) != seed.size()) {
|
2020-08-15 08:33:16 -04:00
|
|
|
return std::nullopt;
|
2018-10-13 08:12:00 -04:00
|
|
|
}
|
2021-05-25 19:32:56 -04:00
|
|
|
|
2018-08-16 17:12:05 -04:00
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
|
2018-09-23 20:31:00 -04:00
|
|
|
Loader::ResultStatus DeriveSDKeys(std::array<Key256, 2>& sd_keys, KeyManager& keys) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!keys.HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKek))) {
|
2018-08-16 17:12:05 -04:00
|
|
|
return Loader::ResultStatus::ErrorMissingSDKEKSource;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
|
|
|
if (!keys.HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration))) {
|
2018-08-16 17:12:05 -04:00
|
|
|
return Loader::ResultStatus::ErrorMissingAESKEKGenerationSource;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
|
|
|
if (!keys.HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration))) {
|
2018-08-16 17:12:05 -04:00
|
|
|
return Loader::ResultStatus::ErrorMissingAESKeyGenerationSource;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-08-16 17:12:05 -04:00
|
|
|
|
|
|
|
const auto sd_kek_source =
|
2018-09-23 20:31:00 -04:00
|
|
|
keys.GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKek));
|
2018-08-16 17:12:05 -04:00
|
|
|
const auto aes_kek_gen =
|
2018-09-23 20:31:00 -04:00
|
|
|
keys.GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration));
|
2018-08-16 17:12:05 -04:00
|
|
|
const auto aes_key_gen =
|
|
|
|
keys.GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration));
|
|
|
|
const auto master_00 = keys.GetKey(S128KeyType::Master);
|
|
|
|
const auto sd_kek =
|
|
|
|
GenerateKeyEncryptionKey(sd_kek_source, master_00, aes_kek_gen, aes_key_gen);
|
2018-09-23 20:31:00 -04:00
|
|
|
keys.SetKey(S128KeyType::SDKek, sd_kek);
|
2018-08-16 17:12:05 -04:00
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!keys.HasKey(S128KeyType::SDSeed)) {
|
2018-08-16 17:12:05 -04:00
|
|
|
return Loader::ResultStatus::ErrorMissingSDSeed;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-08-16 17:12:05 -04:00
|
|
|
const auto sd_seed = keys.GetKey(S128KeyType::SDSeed);
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!keys.HasKey(S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::Save))) {
|
2018-08-16 17:12:05 -04:00
|
|
|
return Loader::ResultStatus::ErrorMissingSDSaveKeySource;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
|
|
|
if (!keys.HasKey(S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::NCA))) {
|
2018-08-16 17:12:05 -04:00
|
|
|
return Loader::ResultStatus::ErrorMissingSDNCAKeySource;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-08-16 17:12:05 -04:00
|
|
|
|
|
|
|
std::array<Key256, 2> sd_key_sources{
|
|
|
|
keys.GetKey(S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::Save)),
|
|
|
|
keys.GetKey(S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::NCA)),
|
|
|
|
};
|
|
|
|
|
2018-08-21 21:12:53 -04:00
|
|
|
// Combine sources and seed
|
|
|
|
for (auto& source : sd_key_sources) {
|
2020-08-15 08:33:16 -04:00
|
|
|
for (std::size_t i = 0; i < source.size(); ++i) {
|
2020-10-13 08:10:50 -04:00
|
|
|
source[i] = static_cast<u8>(source[i] ^ sd_seed[i & 0xF]);
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-08-16 17:12:05 -04:00
|
|
|
}
|
|
|
|
|
2018-08-21 21:12:53 -04:00
|
|
|
AESCipher<Key128> cipher(sd_kek, Mode::ECB);
|
|
|
|
// The transform manipulates sd_keys as part of the Transcode, so the return/output is
|
|
|
|
// unnecessary. This does not alter sd_keys_sources.
|
|
|
|
std::transform(sd_key_sources.begin(), sd_key_sources.end(), sd_keys.begin(),
|
|
|
|
sd_key_sources.begin(), [&cipher](const Key256& source, Key256& out) {
|
|
|
|
cipher.Transcode(source.data(), source.size(), out.data(), Op::Decrypt);
|
|
|
|
return source; ///< Return unaltered source to satisfy output requirement.
|
|
|
|
});
|
|
|
|
|
2018-09-23 21:04:13 -04:00
|
|
|
keys.SetKey(S256KeyType::SDKey, sd_keys[0], static_cast<u64>(SDKeyType::Save));
|
|
|
|
keys.SetKey(S256KeyType::SDKey, sd_keys[1], static_cast<u64>(SDKeyType::NCA));
|
|
|
|
|
2018-08-16 17:12:05 -04:00
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
std::vector<Ticket> GetTicketblob(const Common::FS::IOFile& ticket_save) {
|
|
|
|
if (!ticket_save.IsOpen()) {
|
2018-09-23 21:04:13 -04:00
|
|
|
return {};
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
|
|
|
|
std::vector<u8> buffer(ticket_save.GetSize());
|
2021-05-25 19:32:56 -04:00
|
|
|
if (ticket_save.Read(buffer) != buffer.size()) {
|
2018-10-13 08:12:00 -04:00
|
|
|
return {};
|
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
|
2019-04-16 09:12:04 -04:00
|
|
|
std::vector<Ticket> out;
|
2018-09-23 21:04:13 -04:00
|
|
|
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) {
|
2023-09-05 15:09:42 -04:00
|
|
|
// NOTE: Assumes ticket blob will only contain RSA-2048 tickets.
|
|
|
|
auto ticket = Ticket::Read(std::span{buffer.data() + offset, sizeof(RSA2048Ticket)});
|
|
|
|
offset += sizeof(RSA2048Ticket);
|
|
|
|
if (ticket.IsValid()) {
|
|
|
|
out.push_back(ticket);
|
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t size>
|
|
|
|
static std::array<u8, size> operator^(const std::array<u8, size>& lhs,
|
|
|
|
const std::array<u8, size>& rhs) {
|
2019-11-12 04:13:17 -05:00
|
|
|
std::array<u8, size> out;
|
|
|
|
std::transform(lhs.begin(), lhs.end(), rhs.begin(), out.begin(),
|
2021-05-16 01:46:30 -04:00
|
|
|
[](u8 lhs_elem, u8 rhs_elem) { return u8(lhs_elem ^ rhs_elem); });
|
2018-09-23 21:04:13 -04:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t target_size, size_t in_size>
|
|
|
|
static std::array<u8, target_size> MGF1(const std::array<u8, in_size>& seed) {
|
2018-10-13 09:13:19 -04:00
|
|
|
// Avoids truncation overflow within the loop below.
|
|
|
|
static_assert(target_size <= 0xFF);
|
|
|
|
|
2018-09-23 21:04:13 -04:00
|
|
|
std::array<u8, in_size + 4> seed_exp{};
|
|
|
|
std::memcpy(seed_exp.data(), seed.data(), in_size);
|
|
|
|
|
|
|
|
std::vector<u8> out;
|
|
|
|
size_t i = 0;
|
|
|
|
while (out.size() < target_size) {
|
2018-09-29 11:48:51 -04:00
|
|
|
out.resize(out.size() + 0x20);
|
2018-10-13 09:13:19 -04:00
|
|
|
seed_exp[in_size + 3] = static_cast<u8>(i);
|
2019-11-12 08:37:58 -05:00
|
|
|
mbedtls_sha256_ret(seed_exp.data(), seed_exp.size(), out.data() + out.size() - 0x20, 0);
|
2018-09-23 21:04:13 -04:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
std::array<u8, target_size> target;
|
2018-09-23 21:04:13 -04:00
|
|
|
std::memcpy(target.data(), out.data(), target_size);
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
template <size_t size>
|
2018-10-30 00:03:25 -04:00
|
|
|
static std::optional<u64> FindTicketOffset(const std::array<u8, size>& data) {
|
2018-09-29 11:48:51 -04:00
|
|
|
u64 offset = 0;
|
|
|
|
for (size_t i = 0x20; i < data.size() - 0x10; ++i) {
|
|
|
|
if (data[i] == 0x1) {
|
|
|
|
offset = i + 1;
|
|
|
|
break;
|
|
|
|
} else if (data[i] != 0x0) {
|
2020-08-15 08:33:16 -04:00
|
|
|
return std::nullopt;
|
2018-09-29 11:48:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
std::optional<Key128> KeyManager::ParseTicketTitleKey(const Ticket& ticket) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-16 09:12:04 -04:00
|
|
|
const auto issuer = ticket.GetData().issuer;
|
2020-08-15 08:33:16 -04:00
|
|
|
if (IsAllZeroArray(issuer)) {
|
2023-09-05 15:09:42 -04:00
|
|
|
LOG_WARNING(Crypto, "Attempted to parse title key of ticket with invalid issuer.");
|
2020-08-15 08:33:16 -04:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
2023-09-05 15:09:42 -04:00
|
|
|
|
2019-04-16 09:12:04 -04:00
|
|
|
if (issuer[0] != 'R' || issuer[1] != 'o' || issuer[2] != 'o' || issuer[3] != 't') {
|
2023-09-05 15:09:42 -04:00
|
|
|
LOG_WARNING(Crypto, "Parsing ticket with non-standard certificate authority.");
|
2018-10-13 08:14:38 -04:00
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
if (ticket.GetData().type == TitleKeyType::Common) {
|
|
|
|
return ticket.GetData().title_key_common;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-29 11:48:51 -04:00
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
if (eticket_rsa_keypair == RSAKeyPair<2048>{}) {
|
|
|
|
LOG_WARNING(
|
|
|
|
Crypto,
|
|
|
|
"Skipping personalized ticket title key parsing due to missing ETicket RSA key-pair.");
|
|
|
|
return std::nullopt;
|
2018-09-23 21:04:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_mpi D; // RSA Private Exponent
|
|
|
|
mbedtls_mpi N; // RSA Modulus
|
|
|
|
mbedtls_mpi S; // Input
|
|
|
|
mbedtls_mpi M; // Output
|
|
|
|
|
|
|
|
mbedtls_mpi_init(&D);
|
|
|
|
mbedtls_mpi_init(&N);
|
|
|
|
mbedtls_mpi_init(&S);
|
|
|
|
mbedtls_mpi_init(&M);
|
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
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());
|
2018-09-23 21:04:13 -04:00
|
|
|
|
|
|
|
mbedtls_mpi_exp_mod(&M, &S, &D, &N, nullptr);
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
std::array<u8, 0x100> rsa_step;
|
2018-09-23 21:04:13 -04:00
|
|
|
mbedtls_mpi_write_binary(&M, rsa_step.data(), rsa_step.size());
|
|
|
|
|
|
|
|
u8 m_0 = rsa_step[0];
|
2018-09-29 11:48:51 -04:00
|
|
|
std::array<u8, 0x20> m_1;
|
2018-09-23 21:04:13 -04:00
|
|
|
std::memcpy(m_1.data(), rsa_step.data() + 0x01, m_1.size());
|
2018-09-29 11:48:51 -04:00
|
|
|
std::array<u8, 0xDF> m_2;
|
2018-09-23 21:04:13 -04:00
|
|
|
std::memcpy(m_2.data(), rsa_step.data() + 0x21, m_2.size());
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (m_0 != 0) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
|
|
|
|
m_1 = m_1 ^ MGF1<0x20>(m_2);
|
|
|
|
m_2 = m_2 ^ MGF1<0xDF>(m_1);
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
const auto offset = FindTicketOffset(m_2);
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!offset) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2018-10-30 00:03:25 -04:00
|
|
|
ASSERT(*offset > 0);
|
2018-09-23 21:04:13 -04:00
|
|
|
|
2019-04-16 09:12:04 -04:00
|
|
|
Key128 key_temp{};
|
2018-10-30 00:03:25 -04:00
|
|
|
std::memcpy(key_temp.data(), m_2.data() + *offset, key_temp.size());
|
2023-09-05 15:09:42 -04:00
|
|
|
return key_temp;
|
2018-09-23 21:04:13 -04:00
|
|
|
}
|
|
|
|
|
2018-07-28 21:39:42 -04:00
|
|
|
KeyManager::KeyManager() {
|
2023-02-04 03:44:33 -05:00
|
|
|
ReloadKeys();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyManager::ReloadKeys() {
|
2018-07-28 21:39:42 -04:00
|
|
|
// Initialize keys
|
2021-05-25 19:32:56 -04:00
|
|
|
const auto yuzu_keys_dir = Common::FS::GetYuzuPath(Common::FS::YuzuPath::KeysDir);
|
2021-02-04 06:05:50 -05:00
|
|
|
|
2021-05-25 19:32:56 -04:00
|
|
|
if (!Common::FS::CreateDir(yuzu_keys_dir)) {
|
|
|
|
LOG_ERROR(Core, "Failed to create the keys directory.");
|
2021-02-04 06:05:50 -05:00
|
|
|
}
|
|
|
|
|
2018-07-28 21:39:42 -04:00
|
|
|
if (Settings::values.use_dev_keys) {
|
|
|
|
dev_mode = true;
|
2021-05-25 19:32:56 -04:00
|
|
|
LoadFromFile(yuzu_keys_dir / "dev.keys_autogenerated", false);
|
2022-11-26 21:08:25 -05:00
|
|
|
LoadFromFile(yuzu_keys_dir / "dev.keys", false);
|
2018-07-28 21:39:42 -04:00
|
|
|
} else {
|
|
|
|
dev_mode = false;
|
2021-05-25 19:32:56 -04:00
|
|
|
LoadFromFile(yuzu_keys_dir / "prod.keys_autogenerated", false);
|
2022-11-26 21:08:25 -05:00
|
|
|
LoadFromFile(yuzu_keys_dir / "prod.keys", false);
|
2018-07-28 21:39:42 -04:00
|
|
|
}
|
2018-07-29 18:42:04 -04:00
|
|
|
|
2021-05-25 19:32:56 -04:00
|
|
|
LoadFromFile(yuzu_keys_dir / "title.keys_autogenerated", true);
|
2022-11-26 21:08:25 -05:00
|
|
|
LoadFromFile(yuzu_keys_dir / "title.keys", true);
|
2021-05-25 19:32:56 -04:00
|
|
|
LoadFromFile(yuzu_keys_dir / "console.keys_autogenerated", false);
|
2022-11-26 21:08:25 -05:00
|
|
|
LoadFromFile(yuzu_keys_dir / "console.keys", false);
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
static bool ValidCryptoRevisionString(std::string_view base, size_t begin, size_t length) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (base.size() < begin + length) {
|
2018-09-23 20:56:02 -04:00
|
|
|
return false;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-29 11:48:51 -04:00
|
|
|
return std::all_of(base.begin() + begin, base.begin() + begin + length,
|
2018-10-27 21:56:10 -04:00
|
|
|
[](u8 c) { return std::isxdigit(c); });
|
2018-09-23 20:56:02 -04:00
|
|
|
}
|
|
|
|
|
2021-05-25 19:32:56 -04:00
|
|
|
void KeyManager::LoadFromFile(const std::filesystem::path& file_path, bool is_title_keys) {
|
|
|
|
if (!Common::FS::Exists(file_path)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-20 21:24:25 -05:00
|
|
|
std::ifstream file;
|
2021-05-25 19:32:56 -04:00
|
|
|
Common::FS::OpenFileStream(file, file_path, std::ios_base::in);
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!file.is_open()) {
|
2018-07-27 23:55:23 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-07-27 23:55:23 -04:00
|
|
|
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(file, line)) {
|
|
|
|
std::vector<std::string> out;
|
|
|
|
std::stringstream stream(line);
|
|
|
|
std::string item;
|
2020-08-15 08:33:16 -04:00
|
|
|
while (std::getline(stream, item, '=')) {
|
2018-07-27 23:55:23 -04:00
|
|
|
out.push_back(std::move(item));
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-07-27 23:55:23 -04:00
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (out.size() != 2) {
|
2018-07-27 23:55:23 -04:00
|
|
|
continue;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-07-27 23:55:23 -04:00
|
|
|
|
|
|
|
out[0].erase(std::remove(out[0].begin(), out[0].end(), ' '), out[0].end());
|
|
|
|
out[1].erase(std::remove(out[1].begin(), out[1].end(), ' '), out[1].end());
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (out[0].compare(0, 1, "#") == 0) {
|
2018-09-23 20:42:37 -04:00
|
|
|
continue;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 20:42:37 -04:00
|
|
|
|
2018-07-27 23:55:23 -04:00
|
|
|
if (is_title_keys) {
|
2018-08-15 23:16:11 -04:00
|
|
|
auto rights_id_raw = Common::HexStringToArray<16>(out[0]);
|
2018-07-29 19:00:09 -04:00
|
|
|
u128 rights_id{};
|
|
|
|
std::memcpy(rights_id.data(), rights_id_raw.data(), rights_id_raw.size());
|
2018-08-15 23:16:11 -04:00
|
|
|
Key128 key = Common::HexStringToArray<16>(out[1]);
|
2018-08-16 17:12:31 -04:00
|
|
|
s128_keys[{S128KeyType::Titlekey, rights_id[1], rights_id[0]}] = key;
|
2018-07-27 23:55:23 -04:00
|
|
|
} else {
|
2019-11-12 04:13:17 -05:00
|
|
|
out[0] = Common::ToLower(out[0]);
|
2020-08-22 22:04:15 -04:00
|
|
|
if (const auto iter128 = Find128ByName(out[0]); iter128 != s128_file_id.end()) {
|
|
|
|
const auto& index = iter128->second;
|
|
|
|
const Key128 key = Common::HexStringToArray<16>(out[1]);
|
2018-08-16 17:12:31 -04:00
|
|
|
s128_keys[{index.type, index.field1, index.field2}] = key;
|
2020-08-22 22:04:15 -04:00
|
|
|
} else if (const auto iter256 = Find256ByName(out[0]); iter256 != s256_file_id.end()) {
|
|
|
|
const auto& index = iter256->second;
|
|
|
|
const Key256 key = Common::HexStringToArray<32>(out[1]);
|
2018-08-16 17:12:31 -04:00
|
|
|
s256_keys[{index.type, index.field1, index.field2}] = key;
|
2018-09-23 20:56:02 -04:00
|
|
|
} else if (out[0].compare(0, 8, "keyblob_") == 0 &&
|
|
|
|
out[0].compare(0, 9, "keyblob_k") != 0) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!ValidCryptoRevisionString(out[0], 8, 2)) {
|
2018-09-23 20:56:02 -04:00
|
|
|
continue;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 20:56:02 -04:00
|
|
|
|
2023-09-13 13:32:14 -04:00
|
|
|
const auto index = std::strtoul(out[0].substr(8, 2).c_str(), nullptr, 16);
|
2018-09-23 20:56:02 -04:00
|
|
|
keyblobs[index] = Common::HexStringToArray<0x90>(out[1]);
|
|
|
|
} else if (out[0].compare(0, 18, "encrypted_keyblob_") == 0) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!ValidCryptoRevisionString(out[0], 18, 2)) {
|
2018-09-23 20:56:02 -04:00
|
|
|
continue;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 20:56:02 -04:00
|
|
|
|
2023-09-13 13:32:14 -04:00
|
|
|
const auto index = std::strtoul(out[0].substr(18, 2).c_str(), nullptr, 16);
|
2018-09-23 20:56:02 -04:00
|
|
|
encrypted_keyblobs[index] = Common::HexStringToArray<0xB0>(out[1]);
|
2019-04-10 10:22:04 -04:00
|
|
|
} else if (out[0].compare(0, 20, "eticket_extended_kek") == 0) {
|
|
|
|
eticket_extended_kek = Common::HexStringToArray<576>(out[1]);
|
2023-09-05 15:09:42 -04:00
|
|
|
} 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());
|
2018-09-23 20:56:02 -04:00
|
|
|
} else {
|
2018-09-29 11:48:51 -04:00
|
|
|
for (const auto& kv : KEYS_VARIABLE_LENGTH) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!ValidCryptoRevisionString(out[0], kv.second.size(), 2)) {
|
2018-09-23 20:56:02 -04:00
|
|
|
continue;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 20:56:02 -04:00
|
|
|
if (out[0].compare(0, kv.second.size(), kv.second) == 0) {
|
|
|
|
const auto index =
|
2023-09-13 13:32:14 -04:00
|
|
|
std::strtoul(out[0].substr(kv.second.size(), 2).c_str(), nullptr, 16);
|
2018-09-23 20:56:02 -04:00
|
|
|
const auto sub = kv.first.second;
|
|
|
|
if (sub == 0) {
|
|
|
|
s128_keys[{kv.first.first, index, 0}] =
|
|
|
|
Common::HexStringToArray<16>(out[1]);
|
|
|
|
} else {
|
|
|
|
s128_keys[{kv.first.first, kv.first.second, index}] =
|
|
|
|
Common::HexStringToArray<16>(out[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
static constexpr std::array<const char*, 3> kak_names = {
|
2018-09-23 20:56:02 -04:00
|
|
|
"key_area_key_application_", "key_area_key_ocean_", "key_area_key_system_"};
|
2018-09-29 11:48:51 -04:00
|
|
|
for (size_t j = 0; j < kak_names.size(); ++j) {
|
2018-09-23 20:56:02 -04:00
|
|
|
const auto& match = kak_names[j];
|
|
|
|
if (out[0].compare(0, std::strlen(match), match) == 0) {
|
|
|
|
const auto index =
|
2023-09-13 13:32:14 -04:00
|
|
|
std::strtoul(out[0].substr(std::strlen(match), 2).c_str(), nullptr, 16);
|
2018-09-23 20:56:02 -04:00
|
|
|
s128_keys[{S128KeyType::KeyArea, index, j}] =
|
|
|
|
Common::HexStringToArray<16>(out[1]);
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 03:55:02 -05:00
|
|
|
bool KeyManager::AreKeysLoaded() const {
|
2023-02-04 03:44:33 -05:00
|
|
|
return !s128_keys.empty() && !s256_keys.empty();
|
|
|
|
}
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
bool KeyManager::BaseDeriveNecessary() const {
|
2018-09-23 21:03:00 -04:00
|
|
|
const auto check_key_existence = [this](auto key_type, u64 index1 = 0, u64 index2 = 0) {
|
|
|
|
return !HasKey(key_type, index1, index2);
|
|
|
|
};
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (check_key_existence(S256KeyType::Header)) {
|
2018-09-23 21:03:00 -04:00
|
|
|
return true;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
|
|
|
|
for (size_t i = 0; i < CURRENT_CRYPTO_REVISION; ++i) {
|
|
|
|
if (check_key_existence(S128KeyType::Master, i) ||
|
|
|
|
check_key_existence(S128KeyType::KeyArea, i,
|
|
|
|
static_cast<u64>(KeyAreaKeyType::Application)) ||
|
|
|
|
check_key_existence(S128KeyType::KeyArea, i, static_cast<u64>(KeyAreaKeyType::Ocean)) ||
|
|
|
|
check_key_existence(S128KeyType::KeyArea, i,
|
|
|
|
static_cast<u64>(KeyAreaKeyType::System)) ||
|
|
|
|
check_key_existence(S128KeyType::Titlekek, i))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-28 21:39:42 -04:00
|
|
|
bool KeyManager::HasKey(S128KeyType id, u64 field1, u64 field2) const {
|
2018-07-27 23:55:23 -04:00
|
|
|
return s128_keys.find({id, field1, field2}) != s128_keys.end();
|
|
|
|
}
|
|
|
|
|
2018-07-28 21:39:42 -04:00
|
|
|
bool KeyManager::HasKey(S256KeyType id, u64 field1, u64 field2) const {
|
2018-07-27 23:55:23 -04:00
|
|
|
return s256_keys.find({id, field1, field2}) != s256_keys.end();
|
|
|
|
}
|
|
|
|
|
2018-07-28 21:39:42 -04:00
|
|
|
Key128 KeyManager::GetKey(S128KeyType id, u64 field1, u64 field2) const {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!HasKey(id, field1, field2)) {
|
2018-07-27 23:55:23 -04:00
|
|
|
return {};
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-07-28 21:39:42 -04:00
|
|
|
return s128_keys.at({id, field1, field2});
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
2018-07-28 21:39:42 -04:00
|
|
|
Key256 KeyManager::GetKey(S256KeyType id, u64 field1, u64 field2) const {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!HasKey(id, field1, field2)) {
|
2018-07-27 23:55:23 -04:00
|
|
|
return {};
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-07-28 21:39:42 -04:00
|
|
|
return s256_keys.at({id, field1, field2});
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
2018-09-23 20:57:20 -04:00
|
|
|
Key256 KeyManager::GetBISKey(u8 partition_id) const {
|
|
|
|
Key256 out{};
|
|
|
|
|
|
|
|
for (const auto& bis_type : {BISKeyType::Crypto, BISKeyType::Tweak}) {
|
|
|
|
if (HasKey(S128KeyType::BIS, partition_id, static_cast<u64>(bis_type))) {
|
|
|
|
std::memcpy(
|
|
|
|
out.data() + sizeof(Key128) * static_cast<u64>(bis_type),
|
|
|
|
s128_keys.at({S128KeyType::BIS, partition_id, static_cast<u64>(bis_type)}).data(),
|
|
|
|
sizeof(Key128));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t Size>
|
|
|
|
void KeyManager::WriteKeyToFile(KeyCategory category, std::string_view keyname,
|
2018-08-18 21:16:20 -04:00
|
|
|
const std::array<u8, Size>& key) {
|
2021-05-25 19:32:56 -04:00
|
|
|
const auto yuzu_keys_dir = Common::FS::GetYuzuPath(Common::FS::YuzuPath::KeysDir);
|
|
|
|
|
2018-08-16 17:12:31 -04:00
|
|
|
std::string filename = "title.keys_autogenerated";
|
2021-05-25 19:32:56 -04:00
|
|
|
|
2019-11-12 08:06:37 -05:00
|
|
|
if (category == KeyCategory::Standard) {
|
2018-08-16 17:12:31 -04:00
|
|
|
filename = dev_mode ? "dev.keys_autogenerated" : "prod.keys_autogenerated";
|
2019-11-12 08:06:37 -05:00
|
|
|
} else if (category == KeyCategory::Console) {
|
2018-09-23 20:37:27 -04:00
|
|
|
filename = "console.keys_autogenerated";
|
2019-11-12 08:06:37 -05:00
|
|
|
}
|
|
|
|
|
2021-05-25 19:32:56 -04:00
|
|
|
const auto path = yuzu_keys_dir / filename;
|
2020-08-15 08:33:16 -04:00
|
|
|
const auto add_info_text = !Common::FS::Exists(path);
|
2021-05-25 19:32:56 -04:00
|
|
|
|
|
|
|
Common::FS::IOFile file{path, Common::FS::FileAccessMode::Append,
|
|
|
|
Common::FS::FileType::TextFile};
|
|
|
|
|
2019-11-12 08:06:37 -05:00
|
|
|
if (!file.IsOpen()) {
|
2018-08-16 17:12:31 -04:00
|
|
|
return;
|
2019-11-12 08:06:37 -05:00
|
|
|
}
|
2021-05-25 19:32:56 -04:00
|
|
|
|
2018-08-16 17:12:31 -04:00
|
|
|
if (add_info_text) {
|
2021-05-25 19:32:56 -04:00
|
|
|
void(file.WriteString(
|
2019-11-12 08:06:37 -05:00
|
|
|
"# This file is autogenerated by Yuzu\n"
|
|
|
|
"# It serves to store keys that were automatically generated from the normal keys\n"
|
2021-05-25 19:32:56 -04:00
|
|
|
"# If you are experiencing issues involving keys, it may help to delete this file\n"));
|
2018-08-16 17:12:31 -04:00
|
|
|
}
|
|
|
|
|
2021-05-25 19:32:56 -04:00
|
|
|
void(file.WriteString(fmt::format("\n{} = {}", keyname, Common::HexToString(key))));
|
|
|
|
LoadFromFile(path, category == KeyCategory::Title);
|
2018-08-16 17:12:31 -04:00
|
|
|
}
|
|
|
|
|
2018-07-27 23:55:23 -04:00
|
|
|
void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) {
|
2020-07-05 16:10:51 -04:00
|
|
|
if (s128_keys.find({id, field1, field2}) != s128_keys.end() || key == Key128{}) {
|
2018-08-25 11:43:27 -04:00
|
|
|
return;
|
2020-07-05 16:10:51 -04:00
|
|
|
}
|
2018-08-25 11:43:27 -04:00
|
|
|
if (id == S128KeyType::Titlekey) {
|
|
|
|
Key128 rights_id;
|
|
|
|
std::memcpy(rights_id.data(), &field2, sizeof(u64));
|
|
|
|
std::memcpy(rights_id.data() + sizeof(u64), &field1, sizeof(u64));
|
2019-06-12 17:27:06 -04:00
|
|
|
WriteKeyToFile(KeyCategory::Title, Common::HexToString(rights_id), key);
|
2018-08-25 11:43:27 -04:00
|
|
|
}
|
2018-09-23 20:56:02 -04:00
|
|
|
|
|
|
|
auto category = KeyCategory::Standard;
|
|
|
|
if (id == S128KeyType::Keyblob || id == S128KeyType::KeyblobMAC || id == S128KeyType::TSEC ||
|
|
|
|
id == S128KeyType::SecureBoot || id == S128KeyType::SDSeed || id == S128KeyType::BIS) {
|
|
|
|
category = KeyCategory::Console;
|
|
|
|
}
|
|
|
|
|
2018-08-25 11:43:27 -04:00
|
|
|
const auto iter2 = std::find_if(
|
2020-08-22 22:04:15 -04:00
|
|
|
s128_file_id.begin(), s128_file_id.end(), [&id, &field1, &field2](const auto& elem) {
|
2018-08-16 17:12:31 -04:00
|
|
|
return std::tie(elem.second.type, elem.second.field1, elem.second.field2) ==
|
|
|
|
std::tie(id, field1, field2);
|
|
|
|
});
|
2020-07-05 16:10:51 -04:00
|
|
|
if (iter2 != s128_file_id.end()) {
|
2018-09-23 20:44:39 -04:00
|
|
|
WriteKeyToFile(category, iter2->first, key);
|
2020-07-05 16:10:51 -04:00
|
|
|
}
|
2018-09-23 20:44:39 -04:00
|
|
|
|
|
|
|
// Variable cases
|
|
|
|
if (id == S128KeyType::KeyArea) {
|
2020-08-22 22:04:15 -04:00
|
|
|
static constexpr std::array<const char*, 3> kak_names = {
|
|
|
|
"key_area_key_application_{:02X}",
|
|
|
|
"key_area_key_ocean_{:02X}",
|
|
|
|
"key_area_key_system_{:02X}",
|
|
|
|
};
|
2021-06-23 09:59:56 -04:00
|
|
|
WriteKeyToFile(category, fmt::format(fmt::runtime(kak_names.at(field2)), field1), key);
|
2018-09-23 20:44:39 -04:00
|
|
|
} else if (id == S128KeyType::Master) {
|
|
|
|
WriteKeyToFile(category, fmt::format("master_key_{:02X}", field1), key);
|
|
|
|
} else if (id == S128KeyType::Package1) {
|
|
|
|
WriteKeyToFile(category, fmt::format("package1_key_{:02X}", field1), key);
|
|
|
|
} else if (id == S128KeyType::Package2) {
|
|
|
|
WriteKeyToFile(category, fmt::format("package2_key_{:02X}", field1), key);
|
|
|
|
} else if (id == S128KeyType::Titlekek) {
|
|
|
|
WriteKeyToFile(category, fmt::format("titlekek_{:02X}", field1), key);
|
|
|
|
} else if (id == S128KeyType::Keyblob) {
|
|
|
|
WriteKeyToFile(category, fmt::format("keyblob_key_{:02X}", field1), key);
|
|
|
|
} else if (id == S128KeyType::KeyblobMAC) {
|
|
|
|
WriteKeyToFile(category, fmt::format("keyblob_mac_key_{:02X}", field1), key);
|
|
|
|
} else if (id == S128KeyType::Source && field1 == static_cast<u64>(SourceKeyType::Keyblob)) {
|
|
|
|
WriteKeyToFile(category, fmt::format("keyblob_key_source_{:02X}", field2), key);
|
|
|
|
}
|
|
|
|
|
2018-07-27 23:55:23 -04:00
|
|
|
s128_keys[{id, field1, field2}] = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyManager::SetKey(S256KeyType id, Key256 key, u64 field1, u64 field2) {
|
2020-07-05 16:10:51 -04:00
|
|
|
if (s256_keys.find({id, field1, field2}) != s256_keys.end() || key == Key256{}) {
|
2018-08-25 11:43:27 -04:00
|
|
|
return;
|
2020-07-05 16:10:51 -04:00
|
|
|
}
|
2018-08-16 17:12:31 -04:00
|
|
|
const auto iter = std::find_if(
|
2020-08-22 22:04:15 -04:00
|
|
|
s256_file_id.begin(), s256_file_id.end(), [&id, &field1, &field2](const auto& elem) {
|
2018-08-16 17:12:31 -04:00
|
|
|
return std::tie(elem.second.type, elem.second.field1, elem.second.field2) ==
|
|
|
|
std::tie(id, field1, field2);
|
|
|
|
});
|
2020-07-05 16:10:51 -04:00
|
|
|
if (iter != s256_file_id.end()) {
|
2018-09-23 20:37:27 -04:00
|
|
|
WriteKeyToFile(KeyCategory::Standard, iter->first, key);
|
2020-07-05 16:10:51 -04:00
|
|
|
}
|
2018-07-27 23:55:23 -04:00
|
|
|
s256_keys[{id, field1, field2}] = key;
|
|
|
|
}
|
|
|
|
|
2018-07-30 12:46:23 -04:00
|
|
|
bool KeyManager::KeyFileExists(bool title) {
|
2021-05-25 19:32:56 -04:00
|
|
|
const auto yuzu_keys_dir = Common::FS::GetYuzuPath(Common::FS::YuzuPath::KeysDir);
|
|
|
|
|
2018-07-30 12:46:23 -04:00
|
|
|
if (title) {
|
2021-05-25 19:32:56 -04:00
|
|
|
return Common::FS::Exists(yuzu_keys_dir / "title.keys");
|
2018-07-30 12:46:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Settings::values.use_dev_keys) {
|
2021-05-25 19:32:56 -04:00
|
|
|
return Common::FS::Exists(yuzu_keys_dir / "dev.keys");
|
2018-07-30 12:46:23 -04:00
|
|
|
}
|
|
|
|
|
2021-05-25 19:32:56 -04:00
|
|
|
return Common::FS::Exists(yuzu_keys_dir / "prod.keys");
|
2018-07-30 12:46:23 -04:00
|
|
|
}
|
|
|
|
|
2018-08-16 17:10:01 -04:00
|
|
|
void KeyManager::DeriveSDSeedLazy() {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (HasKey(S128KeyType::SDSeed)) {
|
2018-08-18 21:16:20 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-08-18 21:16:20 -04:00
|
|
|
|
|
|
|
const auto res = DeriveSDSeed();
|
2020-08-15 08:33:16 -04:00
|
|
|
if (res) {
|
2018-10-30 00:03:25 -04:00
|
|
|
SetKey(S128KeyType::SDSeed, *res);
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-08-16 17:12:05 -04:00
|
|
|
}
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
static Key128 CalculateCMAC(const u8* source, size_t size, const Key128& key) {
|
2018-09-23 21:03:00 -04:00
|
|
|
Key128 out{};
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB), key.data(),
|
|
|
|
key.size() * 8, source, size, out.data());
|
2018-09-23 21:03:00 -04:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyManager::DeriveBase() {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!BaseDeriveNecessary()) {
|
2018-09-23 21:03:00 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!HasKey(S128KeyType::SecureBoot) || !HasKey(S128KeyType::TSEC)) {
|
2018-09-23 21:03:00 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
|
|
|
|
const auto has_bis = [this](u64 id) {
|
|
|
|
return HasKey(S128KeyType::BIS, id, static_cast<u64>(BISKeyType::Crypto)) &&
|
|
|
|
HasKey(S128KeyType::BIS, id, static_cast<u64>(BISKeyType::Tweak));
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto copy_bis = [this](u64 id_from, u64 id_to) {
|
|
|
|
SetKey(S128KeyType::BIS,
|
|
|
|
GetKey(S128KeyType::BIS, id_from, static_cast<u64>(BISKeyType::Crypto)), id_to,
|
|
|
|
static_cast<u64>(BISKeyType::Crypto));
|
|
|
|
|
|
|
|
SetKey(S128KeyType::BIS,
|
|
|
|
GetKey(S128KeyType::BIS, id_from, static_cast<u64>(BISKeyType::Tweak)), id_to,
|
|
|
|
static_cast<u64>(BISKeyType::Tweak));
|
|
|
|
};
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (has_bis(2) && !has_bis(3)) {
|
2018-09-23 21:03:00 -04:00
|
|
|
copy_bis(2, 3);
|
2020-08-15 08:33:16 -04:00
|
|
|
} else if (has_bis(3) && !has_bis(2)) {
|
2018-09-23 21:03:00 -04:00
|
|
|
copy_bis(3, 2);
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
std::bitset<32> revisions(0xFFFFFFFF);
|
|
|
|
for (size_t i = 0; i < revisions.size(); ++i) {
|
2018-09-23 21:03:00 -04:00
|
|
|
if (!HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Keyblob), i) ||
|
2018-09-29 11:48:51 -04:00
|
|
|
encrypted_keyblobs[i] == std::array<u8, 0xB0>{}) {
|
2018-09-23 21:03:00 -04:00
|
|
|
revisions.reset(i);
|
2018-09-29 11:48:51 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
}
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!revisions.any()) {
|
2018-09-23 21:03:00 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
|
|
|
|
const auto sbk = GetKey(S128KeyType::SecureBoot);
|
|
|
|
const auto tsec = GetKey(S128KeyType::TSEC);
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
for (size_t i = 0; i < revisions.size(); ++i) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!revisions[i]) {
|
2018-09-23 21:03:00 -04:00
|
|
|
continue;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
|
|
|
|
// Derive keyblob key
|
|
|
|
const auto key = DeriveKeyblobKey(
|
|
|
|
sbk, tsec, GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Keyblob), i));
|
|
|
|
|
|
|
|
SetKey(S128KeyType::Keyblob, key, i);
|
|
|
|
|
|
|
|
// Derive keyblob MAC key
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyblobMAC))) {
|
2018-09-23 21:03:00 -04:00
|
|
|
continue;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
const auto mac_key = DeriveKeyblobMACKey(
|
|
|
|
key, GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyblobMAC)));
|
2018-09-23 21:03:00 -04:00
|
|
|
SetKey(S128KeyType::KeyblobMAC, mac_key, i);
|
|
|
|
|
|
|
|
Key128 cmac = CalculateCMAC(encrypted_keyblobs[i].data() + 0x10, 0xA0, mac_key);
|
2020-08-15 08:33:16 -04:00
|
|
|
if (std::memcmp(cmac.data(), encrypted_keyblobs[i].data(), cmac.size()) != 0) {
|
2018-09-23 21:03:00 -04:00
|
|
|
continue;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
|
|
|
|
// Decrypt keyblob
|
2018-09-29 11:48:51 -04:00
|
|
|
if (keyblobs[i] == std::array<u8, 0x90>{}) {
|
|
|
|
keyblobs[i] = DecryptKeyblob(encrypted_keyblobs[i], key);
|
2018-09-23 21:03:00 -04:00
|
|
|
WriteKeyToFile<0x90>(KeyCategory::Console, fmt::format("keyblob_{:02X}", i),
|
|
|
|
keyblobs[i]);
|
|
|
|
}
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
Key128 package1;
|
2018-09-23 21:03:00 -04:00
|
|
|
std::memcpy(package1.data(), keyblobs[i].data() + 0x80, sizeof(Key128));
|
|
|
|
SetKey(S128KeyType::Package1, package1, i);
|
|
|
|
|
|
|
|
// Derive master key
|
|
|
|
if (HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Master))) {
|
2018-09-29 11:48:51 -04:00
|
|
|
SetKey(S128KeyType::Master,
|
|
|
|
DeriveMasterKey(keyblobs[i], GetKey(S128KeyType::Source,
|
|
|
|
static_cast<u64>(SourceKeyType::Master))),
|
|
|
|
i);
|
2018-09-23 21:03:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
revisions.set();
|
2018-09-29 11:48:51 -04:00
|
|
|
for (size_t i = 0; i < revisions.size(); ++i) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!HasKey(S128KeyType::Master, i)) {
|
2018-09-23 21:03:00 -04:00
|
|
|
revisions.reset(i);
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
}
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!revisions.any()) {
|
2018-09-23 21:03:00 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
for (size_t i = 0; i < revisions.size(); ++i) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!revisions[i]) {
|
2018-09-23 21:03:00 -04:00
|
|
|
continue;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
|
|
|
|
// Derive general purpose keys
|
2018-09-29 11:48:51 -04:00
|
|
|
DeriveGeneralPurposeKeys(i);
|
2018-09-23 21:03:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (HasKey(S128KeyType::Master, 0) &&
|
|
|
|
HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration)) &&
|
|
|
|
HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration)) &&
|
|
|
|
HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::HeaderKek)) &&
|
|
|
|
HasKey(S256KeyType::HeaderSource)) {
|
|
|
|
const auto header_kek = GenerateKeyEncryptionKey(
|
|
|
|
GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::HeaderKek)),
|
|
|
|
GetKey(S128KeyType::Master, 0),
|
|
|
|
GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration)),
|
|
|
|
GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration)));
|
|
|
|
SetKey(S128KeyType::HeaderKek, header_kek);
|
|
|
|
|
|
|
|
AESCipher<Key128> header_cipher(header_kek, Mode::ECB);
|
|
|
|
Key256 out = GetKey(S256KeyType::HeaderSource);
|
|
|
|
header_cipher.Transcode(out.data(), out.size(), out.data(), Op::Decrypt);
|
|
|
|
SetKey(S256KeyType::Header, out);
|
|
|
|
}
|
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
|
2020-09-14 16:47:25 -04:00
|
|
|
void KeyManager::DeriveETicket(PartitionDataManager& data,
|
|
|
|
const FileSys::ContentProvider& provider) {
|
2018-09-23 21:04:13 -04:00
|
|
|
// ETicket keys
|
2020-09-14 16:47:25 -04:00
|
|
|
const auto es = provider.GetEntry(0x0100000000000033, FileSys::ContentRecordType::Program);
|
2018-09-23 21:04:13 -04:00
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (es == nullptr) {
|
2018-09-23 21:04:13 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
|
|
|
|
const auto exefs = es->GetExeFS();
|
2020-08-15 08:33:16 -04:00
|
|
|
if (exefs == nullptr) {
|
2018-09-23 21:04:13 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
|
|
|
|
const auto main = exefs->GetFile("main");
|
2020-08-15 08:33:16 -04:00
|
|
|
if (main == nullptr) {
|
2018-09-23 21:04:13 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
|
|
|
|
const auto bytes = main->ReadAllBytes();
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
const auto eticket_kek = FindKeyFromHex16(bytes, eticket_source_hashes[0]);
|
|
|
|
const auto eticket_kekek = FindKeyFromHex16(bytes, eticket_source_hashes[1]);
|
2018-09-23 21:04:13 -04:00
|
|
|
|
|
|
|
const auto seed3 = data.GetRSAKekSeed3();
|
|
|
|
const auto mask0 = data.GetRSAKekMask0();
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (eticket_kek != Key128{}) {
|
2018-09-23 21:04:13 -04:00
|
|
|
SetKey(S128KeyType::Source, eticket_kek, static_cast<size_t>(SourceKeyType::ETicketKek));
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-29 11:48:51 -04:00
|
|
|
if (eticket_kekek != Key128{}) {
|
2018-09-23 21:04:13 -04:00
|
|
|
SetKey(S128KeyType::Source, eticket_kekek,
|
|
|
|
static_cast<size_t>(SourceKeyType::ETicketKekek));
|
2018-09-29 11:48:51 -04:00
|
|
|
}
|
2020-08-15 08:33:16 -04:00
|
|
|
if (seed3 != Key128{}) {
|
2018-09-23 21:04:13 -04:00
|
|
|
SetKey(S128KeyType::RSAKek, seed3, static_cast<size_t>(RSAKekType::Seed3));
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
|
|
|
if (mask0 != Key128{}) {
|
2018-09-23 21:04:13 -04:00
|
|
|
SetKey(S128KeyType::RSAKek, mask0, static_cast<size_t>(RSAKekType::Mask0));
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
if (eticket_kek == Key128{} || eticket_kekek == Key128{} || seed3 == Key128{} ||
|
2018-09-29 11:48:51 -04:00
|
|
|
mask0 == Key128{}) {
|
2018-09-23 21:04:13 -04:00
|
|
|
return;
|
2018-09-29 11:48:51 -04:00
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
|
2019-11-12 04:13:17 -05:00
|
|
|
const Key128 rsa_oaep_kek = seed3 ^ mask0;
|
|
|
|
if (rsa_oaep_kek == Key128{}) {
|
2018-09-23 21:04:13 -04:00
|
|
|
return;
|
2019-11-12 04:13:17 -05:00
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
|
|
|
|
SetKey(S128KeyType::Source, rsa_oaep_kek,
|
|
|
|
static_cast<u64>(SourceKeyType::RSAOaepKekGeneration));
|
|
|
|
|
|
|
|
Key128 temp_kek{};
|
|
|
|
Key128 temp_kekek{};
|
|
|
|
Key128 eticket_final{};
|
|
|
|
|
|
|
|
// Derive ETicket RSA Kek
|
|
|
|
AESCipher<Key128> es_master(GetKey(S128KeyType::Master), Mode::ECB);
|
|
|
|
es_master.Transcode(rsa_oaep_kek.data(), rsa_oaep_kek.size(), temp_kek.data(), Op::Decrypt);
|
|
|
|
AESCipher<Key128> es_kekek(temp_kek, Mode::ECB);
|
|
|
|
es_kekek.Transcode(eticket_kekek.data(), eticket_kekek.size(), temp_kekek.data(), Op::Decrypt);
|
|
|
|
AESCipher<Key128> es_kek(temp_kekek, Mode::ECB);
|
|
|
|
es_kek.Transcode(eticket_kek.data(), eticket_kek.size(), eticket_final.data(), Op::Decrypt);
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (eticket_final == Key128{}) {
|
2018-09-23 21:04:13 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
|
|
|
|
SetKey(S128KeyType::ETicketRSAKek, eticket_final);
|
|
|
|
|
|
|
|
// Titlekeys
|
2018-09-29 11:48:51 -04:00
|
|
|
data.DecryptProdInfo(GetBISKey(0));
|
2018-09-23 21:04:13 -04:00
|
|
|
|
2019-04-10 10:22:04 -04:00
|
|
|
eticket_extended_kek = data.GetETicketExtendedKek();
|
|
|
|
WriteKeyToFile(KeyCategory::Console, "eticket_extended_kek", eticket_extended_kek);
|
2023-09-05 15:09:42 -04:00
|
|
|
DeriveETicketRSAKey();
|
2019-04-10 10:22:04 -04:00
|
|
|
PopulateTickets();
|
|
|
|
}
|
2018-09-23 21:04:13 -04:00
|
|
|
|
2019-04-10 10:22:04 -04:00
|
|
|
void KeyManager::PopulateTickets() {
|
2023-09-05 15:09:42 -04:00
|
|
|
if (ticket_databases_loaded) {
|
2019-04-10 10:22:04 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2023-09-05 15:09:42 -04:00
|
|
|
ticket_databases_loaded = true;
|
2019-04-10 10:22:04 -04:00
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
std::vector<Ticket> tickets;
|
2018-09-23 21:04:13 -04:00
|
|
|
|
2021-05-25 19:32:56 -04:00
|
|
|
const auto system_save_e1_path =
|
|
|
|
Common::FS::GetYuzuPath(Common::FS::YuzuPath::NANDDir) / "system/save/80000000000000e1";
|
2023-09-05 15:09:42 -04:00
|
|
|
if (Common::FS::Exists(system_save_e1_path)) {
|
|
|
|
const Common::FS::IOFile save_e1{system_save_e1_path, Common::FS::FileAccessMode::Read,
|
|
|
|
Common::FS::FileType::BinaryFile};
|
|
|
|
const auto blob1 = GetTicketblob(save_e1);
|
|
|
|
tickets.insert(tickets.end(), blob1.begin(), blob1.end());
|
|
|
|
}
|
2021-05-25 19:32:56 -04:00
|
|
|
|
|
|
|
const auto system_save_e2_path =
|
|
|
|
Common::FS::GetYuzuPath(Common::FS::YuzuPath::NANDDir) / "system/save/80000000000000e2";
|
2023-09-05 15:09:42 -04:00
|
|
|
if (Common::FS::Exists(system_save_e2_path)) {
|
|
|
|
const Common::FS::IOFile save_e2{system_save_e2_path, Common::FS::FileAccessMode::Read,
|
|
|
|
Common::FS::FileType::BinaryFile};
|
|
|
|
const auto blob2 = GetTicketblob(save_e2);
|
|
|
|
tickets.insert(tickets.end(), blob2.begin(), blob2.end());
|
|
|
|
}
|
2021-05-25 19:32:56 -04:00
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
for (const auto& ticket : tickets) {
|
|
|
|
AddTicket(ticket);
|
2018-09-23 21:04:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:29:21 -04:00
|
|
|
void KeyManager::SynthesizeTickets() {
|
|
|
|
for (const auto& key : s128_keys) {
|
2019-05-26 13:01:42 -04:00
|
|
|
if (key.first.type != S128KeyType::Titlekey) {
|
|
|
|
continue;
|
2019-04-17 11:29:21 -04:00
|
|
|
}
|
2019-05-26 13:01:42 -04:00
|
|
|
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);
|
2019-04-17 11:29:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:03:00 -04:00
|
|
|
void KeyManager::SetKeyWrapped(S128KeyType id, Key128 key, u64 field1, u64 field2) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (key == Key128{}) {
|
2018-09-23 21:03:00 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:03:00 -04:00
|
|
|
SetKey(id, key, field1, field2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyManager::SetKeyWrapped(S256KeyType id, Key256 key, u64 field1, u64 field2) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (key == Key256{}) {
|
2018-09-23 21:03:00 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:03:00 -04:00
|
|
|
SetKey(id, key, field1, field2);
|
|
|
|
}
|
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
void KeyManager::PopulateFromPartitionData(PartitionDataManager& data) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!BaseDeriveNecessary()) {
|
2018-09-23 21:05:01 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:05:01 -04:00
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (!data.HasBoot0()) {
|
2018-09-23 21:05:01 -04:00
|
|
|
return;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:05:01 -04:00
|
|
|
|
2018-09-29 11:48:51 -04:00
|
|
|
for (size_t i = 0; i < encrypted_keyblobs.size(); ++i) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (encrypted_keyblobs[i] != std::array<u8, 0xB0>{}) {
|
2018-09-23 21:05:01 -04:00
|
|
|
continue;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:05:01 -04:00
|
|
|
encrypted_keyblobs[i] = data.GetEncryptedKeyblob(i);
|
|
|
|
WriteKeyToFile<0xB0>(KeyCategory::Console, fmt::format("encrypted_keyblob_{:02X}", i),
|
|
|
|
encrypted_keyblobs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetKeyWrapped(S128KeyType::Source, data.GetPackage2KeySource(),
|
|
|
|
static_cast<u64>(SourceKeyType::Package2));
|
|
|
|
SetKeyWrapped(S128KeyType::Source, data.GetAESKekGenerationSource(),
|
|
|
|
static_cast<u64>(SourceKeyType::AESKekGeneration));
|
|
|
|
SetKeyWrapped(S128KeyType::Source, data.GetTitlekekSource(),
|
|
|
|
static_cast<u64>(SourceKeyType::Titlekek));
|
|
|
|
SetKeyWrapped(S128KeyType::Source, data.GetMasterKeySource(),
|
|
|
|
static_cast<u64>(SourceKeyType::Master));
|
|
|
|
SetKeyWrapped(S128KeyType::Source, data.GetKeyblobMACKeySource(),
|
|
|
|
static_cast<u64>(SourceKeyType::KeyblobMAC));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < PartitionDataManager::MAX_KEYBLOB_SOURCE_HASH; ++i) {
|
|
|
|
SetKeyWrapped(S128KeyType::Source, data.GetKeyblobKeySource(i),
|
|
|
|
static_cast<u64>(SourceKeyType::Keyblob), i);
|
|
|
|
}
|
|
|
|
|
2020-08-15 08:33:16 -04:00
|
|
|
if (data.HasFuses()) {
|
2018-09-23 21:05:01 -04:00
|
|
|
SetKeyWrapped(S128KeyType::SecureBoot, data.GetSecureBootKey());
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:05:01 -04:00
|
|
|
|
|
|
|
DeriveBase();
|
|
|
|
|
|
|
|
Key128 latest_master{};
|
2018-09-29 11:48:51 -04:00
|
|
|
for (s8 i = 0x1F; i >= 0; --i) {
|
|
|
|
if (GetKey(S128KeyType::Master, static_cast<u8>(i)) != Key128{}) {
|
|
|
|
latest_master = GetKey(S128KeyType::Master, static_cast<u8>(i));
|
2018-09-23 21:05:01 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto masters = data.GetTZMasterKeys(latest_master);
|
2018-09-29 11:48:51 -04:00
|
|
|
for (size_t i = 0; i < masters.size(); ++i) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (masters[i] != Key128{} && !HasKey(S128KeyType::Master, i)) {
|
2018-09-23 21:05:01 -04:00
|
|
|
SetKey(S128KeyType::Master, masters[i], i);
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:05:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DeriveBase();
|
|
|
|
|
|
|
|
if (!data.HasPackage2())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::array<Key128, 0x20> package2_keys{};
|
2018-09-29 11:48:51 -04:00
|
|
|
for (size_t i = 0; i < package2_keys.size(); ++i) {
|
2020-08-15 08:33:16 -04:00
|
|
|
if (HasKey(S128KeyType::Package2, i)) {
|
2018-09-23 21:05:01 -04:00
|
|
|
package2_keys[i] = GetKey(S128KeyType::Package2, i);
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2018-09-23 21:05:01 -04:00
|
|
|
}
|
|
|
|
data.DecryptPackage2(package2_keys, Package2Type::NormalMain);
|
|
|
|
|
|
|
|
SetKeyWrapped(S128KeyType::Source, data.GetKeyAreaKeyApplicationSource(),
|
|
|
|
static_cast<u64>(SourceKeyType::KeyAreaKey),
|
|
|
|
static_cast<u64>(KeyAreaKeyType::Application));
|
|
|
|
SetKeyWrapped(S128KeyType::Source, data.GetKeyAreaKeyOceanSource(),
|
|
|
|
static_cast<u64>(SourceKeyType::KeyAreaKey),
|
|
|
|
static_cast<u64>(KeyAreaKeyType::Ocean));
|
|
|
|
SetKeyWrapped(S128KeyType::Source, data.GetKeyAreaKeySystemSource(),
|
|
|
|
static_cast<u64>(SourceKeyType::KeyAreaKey),
|
|
|
|
static_cast<u64>(KeyAreaKeyType::System));
|
|
|
|
SetKeyWrapped(S128KeyType::Source, data.GetSDKekSource(),
|
|
|
|
static_cast<u64>(SourceKeyType::SDKek));
|
|
|
|
SetKeyWrapped(S256KeyType::SDKeySource, data.GetSDSaveKeySource(),
|
|
|
|
static_cast<u64>(SDKeyType::Save));
|
|
|
|
SetKeyWrapped(S256KeyType::SDKeySource, data.GetSDNCAKeySource(),
|
|
|
|
static_cast<u64>(SDKeyType::NCA));
|
|
|
|
SetKeyWrapped(S128KeyType::Source, data.GetHeaderKekSource(),
|
|
|
|
static_cast<u64>(SourceKeyType::HeaderKek));
|
|
|
|
SetKeyWrapped(S256KeyType::HeaderSource, data.GetHeaderKeySource());
|
|
|
|
SetKeyWrapped(S128KeyType::Source, data.GetAESKeyGenerationSource(),
|
|
|
|
static_cast<u64>(SourceKeyType::AESKeyGeneration));
|
|
|
|
|
|
|
|
DeriveBase();
|
|
|
|
}
|
|
|
|
|
2019-04-16 09:12:04 -04:00
|
|
|
const std::map<u128, Ticket>& KeyManager::GetCommonTickets() const {
|
2019-04-10 10:22:04 -04:00
|
|
|
return common_tickets;
|
|
|
|
}
|
|
|
|
|
2019-04-16 09:12:04 -04:00
|
|
|
const std::map<u128, Ticket>& KeyManager::GetPersonalizedTickets() const {
|
2019-04-10 10:22:04 -04:00
|
|
|
return personal_tickets;
|
|
|
|
}
|
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
bool KeyManager::AddTicket(const Ticket& ticket) {
|
|
|
|
if (!ticket.IsValid()) {
|
|
|
|
LOG_WARNING(Crypto, "Attempted to add invalid ticket.");
|
2019-04-10 10:22:04 -04:00
|
|
|
return false;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
const auto& rid = ticket.GetData().rights_id;
|
2019-04-10 10:22:04 -04:00
|
|
|
u128 rights_id;
|
|
|
|
std::memcpy(rights_id.data(), rid.data(), rid.size());
|
2023-09-05 15:09:42 -04:00
|
|
|
if (ticket.GetData().type == Core::Crypto::TitleKeyType::Common) {
|
|
|
|
common_tickets[rights_id] = ticket;
|
|
|
|
} else {
|
|
|
|
personal_tickets[rights_id] = ticket;
|
|
|
|
}
|
2019-04-10 10:22:04 -04:00
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
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;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2019-04-10 10:22:04 -04:00
|
|
|
|
2023-09-05 15:09:42 -04:00
|
|
|
const auto key = ParseTicketTitleKey(ticket);
|
|
|
|
if (!key) {
|
2019-04-10 10:22:04 -04:00
|
|
|
return false;
|
2020-08-15 08:33:16 -04:00
|
|
|
}
|
2023-09-05 15:09:42 -04:00
|
|
|
SetKey(S128KeyType::Titlekey, key.value(), rights_id[1], rights_id[0]);
|
2019-04-10 10:22:04 -04:00
|
|
|
return true;
|
|
|
|
}
|
2018-07-28 21:39:42 -04:00
|
|
|
} // namespace Core::Crypto
|