diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/core/crypto/key_manager.cpp | 799 | ||||
| -rw-r--r-- | src/core/crypto/key_manager.h | 104 | ||||
| -rw-r--r-- | src/core/crypto/partition_data_manager.cpp | 601 | ||||
| -rw-r--r-- | src/core/crypto/partition_data_manager.h | 105 | ||||
| -rw-r--r-- | src/core/file_sys/vfs.h | 10 | ||||
| -rw-r--r-- | src/core/file_sys/vfs_types.h | 21 | ||||
| -rw-r--r-- | src/yuzu/main.cpp | 82 | ||||
| -rw-r--r-- | src/yuzu/main.h | 6 | ||||
| -rw-r--r-- | src/yuzu/main.ui | 6 |
10 files changed, 1663 insertions, 74 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index e4a676e91..4fddaafd1 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -18,6 +18,8 @@ add_library(core STATIC | |||
| 18 | crypto/encryption_layer.h | 18 | crypto/encryption_layer.h |
| 19 | crypto/key_manager.cpp | 19 | crypto/key_manager.cpp |
| 20 | crypto/key_manager.h | 20 | crypto/key_manager.h |
| 21 | crypto/partition_data_manager.cpp | ||
| 22 | crypto/partition_data_manager.h | ||
| 21 | crypto/ctr_encryption_layer.cpp | 23 | crypto/ctr_encryption_layer.cpp |
| 22 | crypto/ctr_encryption_layer.h | 24 | crypto/ctr_encryption_layer.h |
| 23 | crypto/xts_encryption_layer.cpp | 25 | crypto/xts_encryption_layer.cpp |
| @@ -70,6 +72,7 @@ add_library(core STATIC | |||
| 70 | file_sys/vfs_real.cpp | 72 | file_sys/vfs_real.cpp |
| 71 | file_sys/vfs_real.h | 73 | file_sys/vfs_real.h |
| 72 | file_sys/vfs_static.h | 74 | file_sys/vfs_static.h |
| 75 | file_sys/vfs_types.h | ||
| 73 | file_sys/vfs_vector.cpp | 76 | file_sys/vfs_vector.cpp |
| 74 | file_sys/vfs_vector.h | 77 | file_sys/vfs_vector.h |
| 75 | file_sys/xts_archive.cpp | 78 | file_sys/xts_archive.cpp |
diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index bf3a70944..a59a7e1f5 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp | |||
| @@ -4,23 +4,56 @@ | |||
| 4 | 4 | ||
| 5 | #include <algorithm> | 5 | #include <algorithm> |
| 6 | #include <array> | 6 | #include <array> |
| 7 | #include <bitset> | ||
| 8 | #include <cctype> | ||
| 7 | #include <fstream> | 9 | #include <fstream> |
| 8 | #include <locale> | 10 | #include <locale> |
| 11 | #include <map> | ||
| 9 | #include <sstream> | 12 | #include <sstream> |
| 10 | #include <string_view> | 13 | #include <string_view> |
| 11 | #include <tuple> | 14 | #include <tuple> |
| 12 | #include <vector> | 15 | #include <vector> |
| 16 | #include <mbedtls/bignum.h> | ||
| 17 | #include <mbedtls/cipher.h> | ||
| 18 | #include <mbedtls/cmac.h> | ||
| 19 | #include <mbedtls/sha256.h> | ||
| 20 | #include "common/common_funcs.h" | ||
| 13 | #include "common/common_paths.h" | 21 | #include "common/common_paths.h" |
| 14 | #include "common/file_util.h" | 22 | #include "common/file_util.h" |
| 15 | #include "common/hex_util.h" | 23 | #include "common/hex_util.h" |
| 16 | #include "common/logging/log.h" | 24 | #include "common/logging/log.h" |
| 17 | #include "core/crypto/aes_util.h" | 25 | #include "core/crypto/aes_util.h" |
| 18 | #include "core/crypto/key_manager.h" | 26 | #include "core/crypto/key_manager.h" |
| 27 | #include "core/crypto/partition_data_manager.h" | ||
| 28 | #include "core/file_sys/content_archive.h" | ||
| 29 | #include "core/file_sys/nca_metadata.h" | ||
| 30 | #include "core/file_sys/partition_filesystem.h" | ||
| 31 | #include "core/file_sys/registered_cache.h" | ||
| 32 | #include "core/hle/service/filesystem/filesystem.h" | ||
| 19 | #include "core/loader/loader.h" | 33 | #include "core/loader/loader.h" |
| 20 | #include "core/settings.h" | 34 | #include "core/settings.h" |
| 21 | 35 | ||
| 22 | namespace Core::Crypto { | 36 | namespace Core::Crypto { |
| 23 | 37 | ||
| 38 | constexpr u64 CURRENT_CRYPTO_REVISION = 0x5; | ||
| 39 | |||
| 40 | using namespace Common; | ||
| 41 | |||
| 42 | const std::array<SHA256Hash, 2> eticket_source_hashes{ | ||
| 43 | "B71DB271DC338DF380AA2C4335EF8873B1AFD408E80B3582D8719FC81C5E511C"_array32, // eticket_rsa_kek_source | ||
| 44 | "E8965A187D30E57869F562D04383C996DE487BBA5761363D2D4D32391866A85C"_array32, // eticket_rsa_kekek_source | ||
| 45 | }; | ||
| 46 | |||
| 47 | const std::map<std::pair<S128KeyType, u64>, std::string> KEYS_VARIABLE_LENGTH{ | ||
| 48 | {{S128KeyType::Master, 0}, "master_key_"}, | ||
| 49 | {{S128KeyType::Package1, 0}, "package1_key_"}, | ||
| 50 | {{S128KeyType::Package2, 0}, "package2_key_"}, | ||
| 51 | {{S128KeyType::Titlekek, 0}, "titlekek_"}, | ||
| 52 | {{S128KeyType::Source, static_cast<u64>(SourceKeyType::Keyblob)}, "keyblob_key_source_"}, | ||
| 53 | {{S128KeyType::Keyblob, 0}, "keyblob_key_"}, | ||
| 54 | {{S128KeyType::KeyblobMAC, 0}, "keyblob_mac_key_"}, | ||
| 55 | }; | ||
| 56 | |||
| 24 | Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed) { | 57 | Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed) { |
| 25 | Key128 out{}; | 58 | Key128 out{}; |
| 26 | 59 | ||
| @@ -37,6 +70,77 @@ Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, K | |||
| 37 | return out; | 70 | return out; |
| 38 | } | 71 | } |
| 39 | 72 | ||
| 73 | Key128 DeriveKeyblobKey(const Key128& sbk, const Key128& tsec, Key128 source) { | ||
| 74 | AESCipher<Key128> sbk_cipher(sbk, Mode::ECB); | ||
| 75 | AESCipher<Key128> tsec_cipher(tsec, Mode::ECB); | ||
| 76 | tsec_cipher.Transcode(source.data(), source.size(), source.data(), Op::Decrypt); | ||
| 77 | sbk_cipher.Transcode(source.data(), source.size(), source.data(), Op::Decrypt); | ||
| 78 | return source; | ||
| 79 | } | ||
| 80 | |||
| 81 | Key128 DeriveMasterKey(const std::array<u8, 0x90>& keyblob, const Key128& master_source) { | ||
| 82 | Key128 master_root; | ||
| 83 | std::memcpy(master_root.data(), keyblob.data(), sizeof(Key128)); | ||
| 84 | |||
| 85 | AESCipher<Key128> master_cipher(master_root, Mode::ECB); | ||
| 86 | |||
| 87 | Key128 master{}; | ||
| 88 | master_cipher.Transcode(master_source.data(), master_source.size(), master.data(), Op::Decrypt); | ||
| 89 | return master; | ||
| 90 | } | ||
| 91 | |||
| 92 | std::array<u8, 144> DecryptKeyblob(const std::array<u8, 176>& encrypted_keyblob, | ||
| 93 | const Key128& key) { | ||
| 94 | std::array<u8, 0x90> keyblob; | ||
| 95 | AESCipher<Key128> cipher(key, Mode::CTR); | ||
| 96 | cipher.SetIV(std::vector<u8>(encrypted_keyblob.data() + 0x10, encrypted_keyblob.data() + 0x20)); | ||
| 97 | cipher.Transcode(encrypted_keyblob.data() + 0x20, keyblob.size(), keyblob.data(), Op::Decrypt); | ||
| 98 | return keyblob; | ||
| 99 | } | ||
| 100 | |||
| 101 | void KeyManager::DeriveGeneralPurposeKeys(u8 crypto_revision) { | ||
| 102 | const auto kek_generation_source = | ||
| 103 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration)); | ||
| 104 | const auto key_generation_source = | ||
| 105 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration)); | ||
| 106 | |||
| 107 | if (HasKey(S128KeyType::Master, crypto_revision)) { | ||
| 108 | for (auto kak_type : | ||
| 109 | {KeyAreaKeyType::Application, KeyAreaKeyType::Ocean, KeyAreaKeyType::System}) { | ||
| 110 | if (HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey), | ||
| 111 | static_cast<u64>(kak_type))) { | ||
| 112 | const auto source = | ||
| 113 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey), | ||
| 114 | static_cast<u64>(kak_type)); | ||
| 115 | const auto kek = | ||
| 116 | GenerateKeyEncryptionKey(source, GetKey(S128KeyType::Master, crypto_revision), | ||
| 117 | kek_generation_source, key_generation_source); | ||
| 118 | SetKey(S128KeyType::KeyArea, kek, crypto_revision, static_cast<u64>(kak_type)); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | AESCipher<Key128> master_cipher(GetKey(S128KeyType::Master, crypto_revision), Mode::ECB); | ||
| 123 | for (auto key_type : {SourceKeyType::Titlekek, SourceKeyType::Package2}) { | ||
| 124 | if (HasKey(S128KeyType::Source, static_cast<u64>(key_type))) { | ||
| 125 | Key128 key{}; | ||
| 126 | master_cipher.Transcode( | ||
| 127 | GetKey(S128KeyType::Source, static_cast<u64>(key_type)).data(), key.size(), | ||
| 128 | key.data(), Op::Decrypt); | ||
| 129 | SetKey(key_type == SourceKeyType::Titlekek ? S128KeyType::Titlekek | ||
| 130 | : S128KeyType::Package2, | ||
| 131 | key, crypto_revision); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | Key128 DeriveKeyblobMACKey(const Key128& keyblob_key, const Key128& mac_source) { | ||
| 138 | AESCipher<Key128> mac_cipher(keyblob_key, Mode::ECB); | ||
| 139 | Key128 mac_key{}; | ||
| 140 | mac_cipher.Transcode(mac_source.data(), mac_key.size(), mac_key.data(), Op::Decrypt); | ||
| 141 | return mac_key; | ||
| 142 | } | ||
| 143 | |||
| 40 | boost::optional<Key128> DeriveSDSeed() { | 144 | boost::optional<Key128> DeriveSDSeed() { |
| 41 | const FileUtil::IOFile save_43(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + | 145 | const FileUtil::IOFile save_43(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + |
| 42 | "/system/save/8000000000000043", | 146 | "/system/save/8000000000000043", |
| @@ -71,23 +175,24 @@ boost::optional<Key128> DeriveSDSeed() { | |||
| 71 | return seed; | 175 | return seed; |
| 72 | } | 176 | } |
| 73 | 177 | ||
| 74 | Loader::ResultStatus DeriveSDKeys(std::array<Key256, 2>& sd_keys, const KeyManager& keys) { | 178 | Loader::ResultStatus DeriveSDKeys(std::array<Key256, 2>& sd_keys, KeyManager& keys) { |
| 75 | if (!keys.HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKEK))) | 179 | if (!keys.HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKek))) |
| 76 | return Loader::ResultStatus::ErrorMissingSDKEKSource; | 180 | return Loader::ResultStatus::ErrorMissingSDKEKSource; |
| 77 | if (!keys.HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKEKGeneration))) | 181 | if (!keys.HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration))) |
| 78 | return Loader::ResultStatus::ErrorMissingAESKEKGenerationSource; | 182 | return Loader::ResultStatus::ErrorMissingAESKEKGenerationSource; |
| 79 | if (!keys.HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration))) | 183 | if (!keys.HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration))) |
| 80 | return Loader::ResultStatus::ErrorMissingAESKeyGenerationSource; | 184 | return Loader::ResultStatus::ErrorMissingAESKeyGenerationSource; |
| 81 | 185 | ||
| 82 | const auto sd_kek_source = | 186 | const auto sd_kek_source = |
| 83 | keys.GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKEK)); | 187 | keys.GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKek)); |
| 84 | const auto aes_kek_gen = | 188 | const auto aes_kek_gen = |
| 85 | keys.GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKEKGeneration)); | 189 | keys.GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration)); |
| 86 | const auto aes_key_gen = | 190 | const auto aes_key_gen = |
| 87 | keys.GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration)); | 191 | keys.GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration)); |
| 88 | const auto master_00 = keys.GetKey(S128KeyType::Master); | 192 | const auto master_00 = keys.GetKey(S128KeyType::Master); |
| 89 | const auto sd_kek = | 193 | const auto sd_kek = |
| 90 | GenerateKeyEncryptionKey(sd_kek_source, master_00, aes_kek_gen, aes_key_gen); | 194 | GenerateKeyEncryptionKey(sd_kek_source, master_00, aes_kek_gen, aes_key_gen); |
| 195 | keys.SetKey(S128KeyType::SDKek, sd_kek); | ||
| 91 | 196 | ||
| 92 | if (!keys.HasKey(S128KeyType::SDSeed)) | 197 | if (!keys.HasKey(S128KeyType::SDSeed)) |
| 93 | return Loader::ResultStatus::ErrorMissingSDSeed; | 198 | return Loader::ResultStatus::ErrorMissingSDSeed; |
| @@ -118,9 +223,141 @@ Loader::ResultStatus DeriveSDKeys(std::array<Key256, 2>& sd_keys, const KeyManag | |||
| 118 | return source; ///< Return unaltered source to satisfy output requirement. | 223 | return source; ///< Return unaltered source to satisfy output requirement. |
| 119 | }); | 224 | }); |
| 120 | 225 | ||
| 226 | keys.SetKey(S256KeyType::SDKey, sd_keys[0], static_cast<u64>(SDKeyType::Save)); | ||
| 227 | keys.SetKey(S256KeyType::SDKey, sd_keys[1], static_cast<u64>(SDKeyType::NCA)); | ||
| 228 | |||
| 121 | return Loader::ResultStatus::Success; | 229 | return Loader::ResultStatus::Success; |
| 122 | } | 230 | } |
| 123 | 231 | ||
| 232 | std::vector<TicketRaw> GetTicketblob(const FileUtil::IOFile& ticket_save) { | ||
| 233 | if (!ticket_save.IsOpen()) | ||
| 234 | return {}; | ||
| 235 | |||
| 236 | std::vector<u8> buffer(ticket_save.GetSize()); | ||
| 237 | ticket_save.ReadBytes(buffer.data(), buffer.size()); | ||
| 238 | |||
| 239 | std::vector<TicketRaw> out; | ||
| 240 | u32 magic{}; | ||
| 241 | for (std::size_t offset = 0; offset + 0x4 < buffer.size(); ++offset) { | ||
| 242 | if (buffer[offset] == 0x4 && buffer[offset + 1] == 0x0 && buffer[offset + 2] == 0x1 && | ||
| 243 | buffer[offset + 3] == 0x0) { | ||
| 244 | out.emplace_back(); | ||
| 245 | auto& next = out.back(); | ||
| 246 | std::memcpy(&next, buffer.data() + offset, sizeof(TicketRaw)); | ||
| 247 | offset += next.size(); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | return out; | ||
| 252 | } | ||
| 253 | |||
| 254 | template <size_t size> | ||
| 255 | static std::array<u8, size> operator^(const std::array<u8, size>& lhs, | ||
| 256 | const std::array<u8, size>& rhs) { | ||
| 257 | std::array<u8, size> out{}; | ||
| 258 | std::transform(lhs.begin(), lhs.end(), rhs.begin(), out.begin(), std::bit_xor<>()); | ||
| 259 | return out; | ||
| 260 | } | ||
| 261 | |||
| 262 | template <size_t target_size, size_t in_size> | ||
| 263 | static std::array<u8, target_size> MGF1(const std::array<u8, in_size>& seed) { | ||
| 264 | std::array<u8, in_size + 4> seed_exp{}; | ||
| 265 | std::memcpy(seed_exp.data(), seed.data(), in_size); | ||
| 266 | |||
| 267 | std::vector<u8> out; | ||
| 268 | size_t i = 0; | ||
| 269 | while (out.size() < target_size) { | ||
| 270 | out.resize(out.size() + 0x20); | ||
| 271 | seed_exp[in_size + 3] = i; | ||
| 272 | mbedtls_sha256(seed_exp.data(), seed_exp.size(), out.data() + out.size() - 0x20, 0); | ||
| 273 | ++i; | ||
| 274 | } | ||
| 275 | |||
| 276 | std::array<u8, target_size> target; | ||
| 277 | std::memcpy(target.data(), out.data(), target_size); | ||
| 278 | return target; | ||
| 279 | } | ||
| 280 | |||
| 281 | template <size_t size> | ||
| 282 | static boost::optional<u64> FindTicketOffset(const std::array<u8, size>& data) { | ||
| 283 | u64 offset = 0; | ||
| 284 | for (size_t i = 0x20; i < data.size() - 0x10; ++i) { | ||
| 285 | if (data[i] == 0x1) { | ||
| 286 | offset = i + 1; | ||
| 287 | break; | ||
| 288 | } else if (data[i] != 0x0) { | ||
| 289 | return boost::none; | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | return offset; | ||
| 294 | } | ||
| 295 | |||
| 296 | boost::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket, | ||
| 297 | const RSAKeyPair<2048>& key) { | ||
| 298 | u32 cert_authority; | ||
| 299 | std::memcpy(&cert_authority, ticket.data() + 0x140, sizeof(cert_authority)); | ||
| 300 | if (cert_authority == 0) | ||
| 301 | return boost::none; | ||
| 302 | if (cert_authority != Common::MakeMagic('R', 'o', 'o', 't')) | ||
| 303 | LOG_INFO(Crypto, | ||
| 304 | "Attempting to parse ticket with non-standard certificate authority {:08X}.", | ||
| 305 | cert_authority); | ||
| 306 | |||
| 307 | Key128 rights_id; | ||
| 308 | std::memcpy(rights_id.data(), ticket.data() + 0x2A0, sizeof(Key128)); | ||
| 309 | |||
| 310 | if (rights_id == Key128{}) | ||
| 311 | return boost::none; | ||
| 312 | |||
| 313 | Key128 key_temp{}; | ||
| 314 | |||
| 315 | if (!std::any_of(ticket.begin() + 0x190, ticket.begin() + 0x280, [](u8 b) { return b != 0; })) { | ||
| 316 | std::memcpy(key_temp.data(), ticket.data() + 0x180, key_temp.size()); | ||
| 317 | return std::make_pair(rights_id, key_temp); | ||
| 318 | } | ||
| 319 | |||
| 320 | mbedtls_mpi D; // RSA Private Exponent | ||
| 321 | mbedtls_mpi N; // RSA Modulus | ||
| 322 | mbedtls_mpi S; // Input | ||
| 323 | mbedtls_mpi M; // Output | ||
| 324 | |||
| 325 | mbedtls_mpi_init(&D); | ||
| 326 | mbedtls_mpi_init(&N); | ||
| 327 | mbedtls_mpi_init(&S); | ||
| 328 | mbedtls_mpi_init(&M); | ||
| 329 | |||
| 330 | mbedtls_mpi_read_binary(&D, key.decryption_key.data(), key.decryption_key.size()); | ||
| 331 | mbedtls_mpi_read_binary(&N, key.modulus.data(), key.modulus.size()); | ||
| 332 | mbedtls_mpi_read_binary(&S, ticket.data() + 0x180, 0x100); | ||
| 333 | |||
| 334 | mbedtls_mpi_exp_mod(&M, &S, &D, &N, nullptr); | ||
| 335 | |||
| 336 | std::array<u8, 0x100> rsa_step; | ||
| 337 | mbedtls_mpi_write_binary(&M, rsa_step.data(), rsa_step.size()); | ||
| 338 | |||
| 339 | u8 m_0 = rsa_step[0]; | ||
| 340 | std::array<u8, 0x20> m_1; | ||
| 341 | std::memcpy(m_1.data(), rsa_step.data() + 0x01, m_1.size()); | ||
| 342 | std::array<u8, 0xDF> m_2; | ||
| 343 | std::memcpy(m_2.data(), rsa_step.data() + 0x21, m_2.size()); | ||
| 344 | |||
| 345 | if (m_0 != 0) | ||
| 346 | return boost::none; | ||
| 347 | |||
| 348 | m_1 = m_1 ^ MGF1<0x20>(m_2); | ||
| 349 | m_2 = m_2 ^ MGF1<0xDF>(m_1); | ||
| 350 | |||
| 351 | const auto offset = FindTicketOffset(m_2); | ||
| 352 | if (offset == boost::none) | ||
| 353 | return boost::none; | ||
| 354 | ASSERT(offset.get() > 0); | ||
| 355 | |||
| 356 | std::memcpy(key_temp.data(), m_2.data() + offset.get(), key_temp.size()); | ||
| 357 | |||
| 358 | return std::make_pair(rights_id, key_temp); | ||
| 359 | } | ||
| 360 | |||
| 124 | KeyManager::KeyManager() { | 361 | KeyManager::KeyManager() { |
| 125 | // Initialize keys | 362 | // Initialize keys |
| 126 | const std::string hactool_keys_dir = FileUtil::GetHactoolConfigurationPath(); | 363 | const std::string hactool_keys_dir = FileUtil::GetHactoolConfigurationPath(); |
| @@ -137,6 +374,15 @@ KeyManager::KeyManager() { | |||
| 137 | 374 | ||
| 138 | AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "title.keys", true); | 375 | AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "title.keys", true); |
| 139 | AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, "title.keys_autogenerated", true); | 376 | AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, "title.keys_autogenerated", true); |
| 377 | AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "console.keys", false); | ||
| 378 | AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, "console.keys_autogenerated", false); | ||
| 379 | } | ||
| 380 | |||
| 381 | static bool ValidCryptoRevisionString(std::string_view base, size_t begin, size_t length) { | ||
| 382 | if (base.size() < begin + length) | ||
| 383 | return false; | ||
| 384 | return std::all_of(base.begin() + begin, base.begin() + begin + length, | ||
| 385 | [](u8 c) { return std::isdigit(c); }); | ||
| 140 | } | 386 | } |
| 141 | 387 | ||
| 142 | void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { | 388 | void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { |
| @@ -158,6 +404,9 @@ void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { | |||
| 158 | out[0].erase(std::remove(out[0].begin(), out[0].end(), ' '), out[0].end()); | 404 | out[0].erase(std::remove(out[0].begin(), out[0].end(), ' '), out[0].end()); |
| 159 | out[1].erase(std::remove(out[1].begin(), out[1].end(), ' '), out[1].end()); | 405 | out[1].erase(std::remove(out[1].begin(), out[1].end(), ' '), out[1].end()); |
| 160 | 406 | ||
| 407 | if (out[0].compare(0, 1, "#") == 0) | ||
| 408 | continue; | ||
| 409 | |||
| 161 | if (is_title_keys) { | 410 | if (is_title_keys) { |
| 162 | auto rights_id_raw = Common::HexStringToArray<16>(out[0]); | 411 | auto rights_id_raw = Common::HexStringToArray<16>(out[0]); |
| 163 | u128 rights_id{}; | 412 | u128 rights_id{}; |
| @@ -174,6 +423,50 @@ void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { | |||
| 174 | const auto index = s256_file_id.at(out[0]); | 423 | const auto index = s256_file_id.at(out[0]); |
| 175 | Key256 key = Common::HexStringToArray<32>(out[1]); | 424 | Key256 key = Common::HexStringToArray<32>(out[1]); |
| 176 | s256_keys[{index.type, index.field1, index.field2}] = key; | 425 | s256_keys[{index.type, index.field1, index.field2}] = key; |
| 426 | } else if (out[0].compare(0, 8, "keyblob_") == 0 && | ||
| 427 | out[0].compare(0, 9, "keyblob_k") != 0) { | ||
| 428 | if (!ValidCryptoRevisionString(out[0], 8, 2)) | ||
| 429 | continue; | ||
| 430 | |||
| 431 | const auto index = std::stoul(out[0].substr(8, 2), nullptr, 16); | ||
| 432 | keyblobs[index] = Common::HexStringToArray<0x90>(out[1]); | ||
| 433 | } else if (out[0].compare(0, 18, "encrypted_keyblob_") == 0) { | ||
| 434 | if (!ValidCryptoRevisionString(out[0], 18, 2)) | ||
| 435 | continue; | ||
| 436 | |||
| 437 | const auto index = std::stoul(out[0].substr(18, 2), nullptr, 16); | ||
| 438 | encrypted_keyblobs[index] = Common::HexStringToArray<0xB0>(out[1]); | ||
| 439 | } else { | ||
| 440 | for (const auto& kv : KEYS_VARIABLE_LENGTH) { | ||
| 441 | if (!ValidCryptoRevisionString(out[0], kv.second.size(), 2)) | ||
| 442 | continue; | ||
| 443 | if (out[0].compare(0, kv.second.size(), kv.second) == 0) { | ||
| 444 | const auto index = | ||
| 445 | std::stoul(out[0].substr(kv.second.size(), 2), nullptr, 16); | ||
| 446 | const auto sub = kv.first.second; | ||
| 447 | if (sub == 0) { | ||
| 448 | s128_keys[{kv.first.first, index, 0}] = | ||
| 449 | Common::HexStringToArray<16>(out[1]); | ||
| 450 | } else { | ||
| 451 | s128_keys[{kv.first.first, kv.first.second, index}] = | ||
| 452 | Common::HexStringToArray<16>(out[1]); | ||
| 453 | } | ||
| 454 | |||
| 455 | break; | ||
| 456 | } | ||
| 457 | } | ||
| 458 | |||
| 459 | static constexpr std::array<const char*, 3> kak_names = { | ||
| 460 | "key_area_key_application_", "key_area_key_ocean_", "key_area_key_system_"}; | ||
| 461 | for (size_t j = 0; j < kak_names.size(); ++j) { | ||
| 462 | const auto& match = kak_names[j]; | ||
| 463 | if (out[0].compare(0, std::strlen(match), match) == 0) { | ||
| 464 | const auto index = | ||
| 465 | std::stoul(out[0].substr(std::strlen(match), 2), nullptr, 16); | ||
| 466 | s128_keys[{S128KeyType::KeyArea, index, j}] = | ||
| 467 | Common::HexStringToArray<16>(out[1]); | ||
| 468 | } | ||
| 469 | } | ||
| 177 | } | 470 | } |
| 178 | } | 471 | } |
| 179 | } | 472 | } |
| @@ -187,6 +480,28 @@ void KeyManager::AttemptLoadKeyFile(const std::string& dir1, const std::string& | |||
| 187 | LoadFromFile(dir2 + DIR_SEP + filename, title); | 480 | LoadFromFile(dir2 + DIR_SEP + filename, title); |
| 188 | } | 481 | } |
| 189 | 482 | ||
| 483 | bool KeyManager::BaseDeriveNecessary() const { | ||
| 484 | const auto check_key_existence = [this](auto key_type, u64 index1 = 0, u64 index2 = 0) { | ||
| 485 | return !HasKey(key_type, index1, index2); | ||
| 486 | }; | ||
| 487 | |||
| 488 | if (check_key_existence(S256KeyType::Header)) | ||
| 489 | return true; | ||
| 490 | |||
| 491 | for (size_t i = 0; i < CURRENT_CRYPTO_REVISION; ++i) { | ||
| 492 | if (check_key_existence(S128KeyType::Master, i) || | ||
| 493 | check_key_existence(S128KeyType::KeyArea, i, | ||
| 494 | static_cast<u64>(KeyAreaKeyType::Application)) || | ||
| 495 | check_key_existence(S128KeyType::KeyArea, i, static_cast<u64>(KeyAreaKeyType::Ocean)) || | ||
| 496 | check_key_existence(S128KeyType::KeyArea, i, | ||
| 497 | static_cast<u64>(KeyAreaKeyType::System)) || | ||
| 498 | check_key_existence(S128KeyType::Titlekek, i)) | ||
| 499 | return true; | ||
| 500 | } | ||
| 501 | |||
| 502 | return false; | ||
| 503 | } | ||
| 504 | |||
| 190 | bool KeyManager::HasKey(S128KeyType id, u64 field1, u64 field2) const { | 505 | bool KeyManager::HasKey(S128KeyType id, u64 field1, u64 field2) const { |
| 191 | return s128_keys.find({id, field1, field2}) != s128_keys.end(); | 506 | return s128_keys.find({id, field1, field2}) != s128_keys.end(); |
| 192 | } | 507 | } |
| @@ -207,13 +522,30 @@ Key256 KeyManager::GetKey(S256KeyType id, u64 field1, u64 field2) const { | |||
| 207 | return s256_keys.at({id, field1, field2}); | 522 | return s256_keys.at({id, field1, field2}); |
| 208 | } | 523 | } |
| 209 | 524 | ||
| 210 | template <std::size_t Size> | 525 | Key256 KeyManager::GetBISKey(u8 partition_id) const { |
| 211 | void KeyManager::WriteKeyToFile(bool title_key, std::string_view keyname, | 526 | Key256 out{}; |
| 527 | |||
| 528 | for (const auto& bis_type : {BISKeyType::Crypto, BISKeyType::Tweak}) { | ||
| 529 | if (HasKey(S128KeyType::BIS, partition_id, static_cast<u64>(bis_type))) { | ||
| 530 | std::memcpy( | ||
| 531 | out.data() + sizeof(Key128) * static_cast<u64>(bis_type), | ||
| 532 | s128_keys.at({S128KeyType::BIS, partition_id, static_cast<u64>(bis_type)}).data(), | ||
| 533 | sizeof(Key128)); | ||
| 534 | } | ||
| 535 | } | ||
| 536 | |||
| 537 | return out; | ||
| 538 | } | ||
| 539 | |||
| 540 | template <size_t Size> | ||
| 541 | void KeyManager::WriteKeyToFile(KeyCategory category, std::string_view keyname, | ||
| 212 | const std::array<u8, Size>& key) { | 542 | const std::array<u8, Size>& key) { |
| 213 | const std::string yuzu_keys_dir = FileUtil::GetUserPath(FileUtil::UserPath::KeysDir); | 543 | const std::string yuzu_keys_dir = FileUtil::GetUserPath(FileUtil::UserPath::KeysDir); |
| 214 | std::string filename = "title.keys_autogenerated"; | 544 | std::string filename = "title.keys_autogenerated"; |
| 215 | if (!title_key) | 545 | if (category == KeyCategory::Standard) |
| 216 | filename = dev_mode ? "dev.keys_autogenerated" : "prod.keys_autogenerated"; | 546 | filename = dev_mode ? "dev.keys_autogenerated" : "prod.keys_autogenerated"; |
| 547 | else if (category == KeyCategory::Console) | ||
| 548 | filename = "console.keys_autogenerated"; | ||
| 217 | const auto add_info_text = !FileUtil::Exists(yuzu_keys_dir + DIR_SEP + filename); | 549 | const auto add_info_text = !FileUtil::Exists(yuzu_keys_dir + DIR_SEP + filename); |
| 218 | FileUtil::CreateFullPath(yuzu_keys_dir + DIR_SEP + filename); | 550 | FileUtil::CreateFullPath(yuzu_keys_dir + DIR_SEP + filename); |
| 219 | std::ofstream file(yuzu_keys_dir + DIR_SEP + filename, std::ios::app); | 551 | std::ofstream file(yuzu_keys_dir + DIR_SEP + filename, std::ios::app); |
| @@ -227,7 +559,7 @@ void KeyManager::WriteKeyToFile(bool title_key, std::string_view keyname, | |||
| 227 | } | 559 | } |
| 228 | 560 | ||
| 229 | file << fmt::format("\n{} = {}", keyname, Common::HexArrayToString(key)); | 561 | file << fmt::format("\n{} = {}", keyname, Common::HexArrayToString(key)); |
| 230 | AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, filename, title_key); | 562 | AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, filename, category == KeyCategory::Title); |
| 231 | } | 563 | } |
| 232 | 564 | ||
| 233 | void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) { | 565 | void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) { |
| @@ -237,8 +569,15 @@ void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) { | |||
| 237 | Key128 rights_id; | 569 | Key128 rights_id; |
| 238 | std::memcpy(rights_id.data(), &field2, sizeof(u64)); | 570 | std::memcpy(rights_id.data(), &field2, sizeof(u64)); |
| 239 | std::memcpy(rights_id.data() + sizeof(u64), &field1, sizeof(u64)); | 571 | std::memcpy(rights_id.data() + sizeof(u64), &field1, sizeof(u64)); |
| 240 | WriteKeyToFile(true, Common::HexArrayToString(rights_id), key); | 572 | WriteKeyToFile(KeyCategory::Title, Common::HexArrayToString(rights_id), key); |
| 241 | } | 573 | } |
| 574 | |||
| 575 | auto category = KeyCategory::Standard; | ||
| 576 | if (id == S128KeyType::Keyblob || id == S128KeyType::KeyblobMAC || id == S128KeyType::TSEC || | ||
| 577 | id == S128KeyType::SecureBoot || id == S128KeyType::SDSeed || id == S128KeyType::BIS) { | ||
| 578 | category = KeyCategory::Console; | ||
| 579 | } | ||
| 580 | |||
| 242 | const auto iter2 = std::find_if( | 581 | const auto iter2 = std::find_if( |
| 243 | s128_file_id.begin(), s128_file_id.end(), | 582 | s128_file_id.begin(), s128_file_id.end(), |
| 244 | [&id, &field1, &field2](const std::pair<std::string, KeyIndex<S128KeyType>> elem) { | 583 | [&id, &field1, &field2](const std::pair<std::string, KeyIndex<S128KeyType>> elem) { |
| @@ -246,7 +585,30 @@ void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) { | |||
| 246 | std::tie(id, field1, field2); | 585 | std::tie(id, field1, field2); |
| 247 | }); | 586 | }); |
| 248 | if (iter2 != s128_file_id.end()) | 587 | if (iter2 != s128_file_id.end()) |
| 249 | WriteKeyToFile(false, iter2->first, key); | 588 | WriteKeyToFile(category, iter2->first, key); |
| 589 | |||
| 590 | // Variable cases | ||
| 591 | if (id == S128KeyType::KeyArea) { | ||
| 592 | static constexpr std::array<const char*, 3> kak_names = {"key_area_key_application_{:02X}", | ||
| 593 | "key_area_key_ocean_{:02X}", | ||
| 594 | "key_area_key_system_{:02X}"}; | ||
| 595 | WriteKeyToFile(category, fmt::format(kak_names.at(field2), field1), key); | ||
| 596 | } else if (id == S128KeyType::Master) { | ||
| 597 | WriteKeyToFile(category, fmt::format("master_key_{:02X}", field1), key); | ||
| 598 | } else if (id == S128KeyType::Package1) { | ||
| 599 | WriteKeyToFile(category, fmt::format("package1_key_{:02X}", field1), key); | ||
| 600 | } else if (id == S128KeyType::Package2) { | ||
| 601 | WriteKeyToFile(category, fmt::format("package2_key_{:02X}", field1), key); | ||
| 602 | } else if (id == S128KeyType::Titlekek) { | ||
| 603 | WriteKeyToFile(category, fmt::format("titlekek_{:02X}", field1), key); | ||
| 604 | } else if (id == S128KeyType::Keyblob) { | ||
| 605 | WriteKeyToFile(category, fmt::format("keyblob_key_{:02X}", field1), key); | ||
| 606 | } else if (id == S128KeyType::KeyblobMAC) { | ||
| 607 | WriteKeyToFile(category, fmt::format("keyblob_mac_key_{:02X}", field1), key); | ||
| 608 | } else if (id == S128KeyType::Source && field1 == static_cast<u64>(SourceKeyType::Keyblob)) { | ||
| 609 | WriteKeyToFile(category, fmt::format("keyblob_key_source_{:02X}", field2), key); | ||
| 610 | } | ||
| 611 | |||
| 250 | s128_keys[{id, field1, field2}] = key; | 612 | s128_keys[{id, field1, field2}] = key; |
| 251 | } | 613 | } |
| 252 | 614 | ||
| @@ -260,7 +622,7 @@ void KeyManager::SetKey(S256KeyType id, Key256 key, u64 field1, u64 field2) { | |||
| 260 | std::tie(id, field1, field2); | 622 | std::tie(id, field1, field2); |
| 261 | }); | 623 | }); |
| 262 | if (iter != s256_file_id.end()) | 624 | if (iter != s256_file_id.end()) |
| 263 | WriteKeyToFile(false, iter->first, key); | 625 | WriteKeyToFile(KeyCategory::Standard, iter->first, key); |
| 264 | s256_keys[{id, field1, field2}] = key; | 626 | s256_keys[{id, field1, field2}] = key; |
| 265 | } | 627 | } |
| 266 | 628 | ||
| @@ -290,59 +652,388 @@ void KeyManager::DeriveSDSeedLazy() { | |||
| 290 | SetKey(S128KeyType::SDSeed, res.get()); | 652 | SetKey(S128KeyType::SDSeed, res.get()); |
| 291 | } | 653 | } |
| 292 | 654 | ||
| 655 | static Key128 CalculateCMAC(const u8* source, size_t size, const Key128& key) { | ||
| 656 | Key128 out{}; | ||
| 657 | |||
| 658 | mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB), key.data(), | ||
| 659 | key.size() * 8, source, size, out.data()); | ||
| 660 | return out; | ||
| 661 | } | ||
| 662 | |||
| 663 | void KeyManager::DeriveBase() { | ||
| 664 | if (!BaseDeriveNecessary()) | ||
| 665 | return; | ||
| 666 | |||
| 667 | if (!HasKey(S128KeyType::SecureBoot) || !HasKey(S128KeyType::TSEC)) | ||
| 668 | return; | ||
| 669 | |||
| 670 | const auto has_bis = [this](u64 id) { | ||
| 671 | return HasKey(S128KeyType::BIS, id, static_cast<u64>(BISKeyType::Crypto)) && | ||
| 672 | HasKey(S128KeyType::BIS, id, static_cast<u64>(BISKeyType::Tweak)); | ||
| 673 | }; | ||
| 674 | |||
| 675 | const auto copy_bis = [this](u64 id_from, u64 id_to) { | ||
| 676 | SetKey(S128KeyType::BIS, | ||
| 677 | GetKey(S128KeyType::BIS, id_from, static_cast<u64>(BISKeyType::Crypto)), id_to, | ||
| 678 | static_cast<u64>(BISKeyType::Crypto)); | ||
| 679 | |||
| 680 | SetKey(S128KeyType::BIS, | ||
| 681 | GetKey(S128KeyType::BIS, id_from, static_cast<u64>(BISKeyType::Tweak)), id_to, | ||
| 682 | static_cast<u64>(BISKeyType::Tweak)); | ||
| 683 | }; | ||
| 684 | |||
| 685 | if (has_bis(2) && !has_bis(3)) | ||
| 686 | copy_bis(2, 3); | ||
| 687 | else if (has_bis(3) && !has_bis(2)) | ||
| 688 | copy_bis(3, 2); | ||
| 689 | |||
| 690 | std::bitset<32> revisions(0xFFFFFFFF); | ||
| 691 | for (size_t i = 0; i < revisions.size(); ++i) { | ||
| 692 | if (!HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Keyblob), i) || | ||
| 693 | encrypted_keyblobs[i] == std::array<u8, 0xB0>{}) { | ||
| 694 | revisions.reset(i); | ||
| 695 | } | ||
| 696 | } | ||
| 697 | |||
| 698 | if (!revisions.any()) | ||
| 699 | return; | ||
| 700 | |||
| 701 | const auto sbk = GetKey(S128KeyType::SecureBoot); | ||
| 702 | const auto tsec = GetKey(S128KeyType::TSEC); | ||
| 703 | const auto master_source = GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Master)); | ||
| 704 | |||
| 705 | for (size_t i = 0; i < revisions.size(); ++i) { | ||
| 706 | if (!revisions[i]) | ||
| 707 | continue; | ||
| 708 | |||
| 709 | // Derive keyblob key | ||
| 710 | const auto key = DeriveKeyblobKey( | ||
| 711 | sbk, tsec, GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Keyblob), i)); | ||
| 712 | |||
| 713 | SetKey(S128KeyType::Keyblob, key, i); | ||
| 714 | |||
| 715 | // Derive keyblob MAC key | ||
| 716 | if (!HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyblobMAC))) | ||
| 717 | continue; | ||
| 718 | |||
| 719 | const auto mac_key = DeriveKeyblobMACKey( | ||
| 720 | key, GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyblobMAC))); | ||
| 721 | SetKey(S128KeyType::KeyblobMAC, mac_key, i); | ||
| 722 | |||
| 723 | Key128 cmac = CalculateCMAC(encrypted_keyblobs[i].data() + 0x10, 0xA0, mac_key); | ||
| 724 | if (std::memcmp(cmac.data(), encrypted_keyblobs[i].data(), cmac.size()) != 0) | ||
| 725 | continue; | ||
| 726 | |||
| 727 | // Decrypt keyblob | ||
| 728 | if (keyblobs[i] == std::array<u8, 0x90>{}) { | ||
| 729 | keyblobs[i] = DecryptKeyblob(encrypted_keyblobs[i], key); | ||
| 730 | WriteKeyToFile<0x90>(KeyCategory::Console, fmt::format("keyblob_{:02X}", i), | ||
| 731 | keyblobs[i]); | ||
| 732 | } | ||
| 733 | |||
| 734 | Key128 package1; | ||
| 735 | std::memcpy(package1.data(), keyblobs[i].data() + 0x80, sizeof(Key128)); | ||
| 736 | SetKey(S128KeyType::Package1, package1, i); | ||
| 737 | |||
| 738 | // Derive master key | ||
| 739 | if (HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Master))) { | ||
| 740 | SetKey(S128KeyType::Master, | ||
| 741 | DeriveMasterKey(keyblobs[i], GetKey(S128KeyType::Source, | ||
| 742 | static_cast<u64>(SourceKeyType::Master))), | ||
| 743 | i); | ||
| 744 | } | ||
| 745 | } | ||
| 746 | |||
| 747 | revisions.set(); | ||
| 748 | for (size_t i = 0; i < revisions.size(); ++i) { | ||
| 749 | if (!HasKey(S128KeyType::Master, i)) | ||
| 750 | revisions.reset(i); | ||
| 751 | } | ||
| 752 | |||
| 753 | if (!revisions.any()) | ||
| 754 | return; | ||
| 755 | |||
| 756 | for (size_t i = 0; i < revisions.size(); ++i) { | ||
| 757 | if (!revisions[i]) | ||
| 758 | continue; | ||
| 759 | |||
| 760 | // Derive general purpose keys | ||
| 761 | DeriveGeneralPurposeKeys(i); | ||
| 762 | } | ||
| 763 | |||
| 764 | if (HasKey(S128KeyType::Master, 0) && | ||
| 765 | HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration)) && | ||
| 766 | HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration)) && | ||
| 767 | HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::HeaderKek)) && | ||
| 768 | HasKey(S256KeyType::HeaderSource)) { | ||
| 769 | const auto header_kek = GenerateKeyEncryptionKey( | ||
| 770 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::HeaderKek)), | ||
| 771 | GetKey(S128KeyType::Master, 0), | ||
| 772 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration)), | ||
| 773 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration))); | ||
| 774 | SetKey(S128KeyType::HeaderKek, header_kek); | ||
| 775 | |||
| 776 | AESCipher<Key128> header_cipher(header_kek, Mode::ECB); | ||
| 777 | Key256 out = GetKey(S256KeyType::HeaderSource); | ||
| 778 | header_cipher.Transcode(out.data(), out.size(), out.data(), Op::Decrypt); | ||
| 779 | SetKey(S256KeyType::Header, out); | ||
| 780 | } | ||
| 781 | } | ||
| 782 | |||
| 783 | void KeyManager::DeriveETicket(PartitionDataManager& data) { | ||
| 784 | // ETicket keys | ||
| 785 | const auto es = Service::FileSystem::GetUnionContents()->GetEntry( | ||
| 786 | 0x0100000000000033, FileSys::ContentRecordType::Program); | ||
| 787 | |||
| 788 | if (es == nullptr) | ||
| 789 | return; | ||
| 790 | |||
| 791 | const auto exefs = es->GetExeFS(); | ||
| 792 | if (exefs == nullptr) | ||
| 793 | return; | ||
| 794 | |||
| 795 | const auto main = exefs->GetFile("main"); | ||
| 796 | if (main == nullptr) | ||
| 797 | return; | ||
| 798 | |||
| 799 | const auto bytes = main->ReadAllBytes(); | ||
| 800 | |||
| 801 | const auto eticket_kek = FindKeyFromHex16(bytes, eticket_source_hashes[0]); | ||
| 802 | const auto eticket_kekek = FindKeyFromHex16(bytes, eticket_source_hashes[1]); | ||
| 803 | |||
| 804 | const auto seed3 = data.GetRSAKekSeed3(); | ||
| 805 | const auto mask0 = data.GetRSAKekMask0(); | ||
| 806 | |||
| 807 | if (eticket_kek != Key128{}) | ||
| 808 | SetKey(S128KeyType::Source, eticket_kek, static_cast<size_t>(SourceKeyType::ETicketKek)); | ||
| 809 | if (eticket_kekek != Key128{}) { | ||
| 810 | SetKey(S128KeyType::Source, eticket_kekek, | ||
| 811 | static_cast<size_t>(SourceKeyType::ETicketKekek)); | ||
| 812 | } | ||
| 813 | if (seed3 != Key128{}) | ||
| 814 | SetKey(S128KeyType::RSAKek, seed3, static_cast<size_t>(RSAKekType::Seed3)); | ||
| 815 | if (mask0 != Key128{}) | ||
| 816 | SetKey(S128KeyType::RSAKek, mask0, static_cast<size_t>(RSAKekType::Mask0)); | ||
| 817 | if (eticket_kek == Key128{} || eticket_kekek == Key128{} || seed3 == Key128{} || | ||
| 818 | mask0 == Key128{}) { | ||
| 819 | return; | ||
| 820 | } | ||
| 821 | |||
| 822 | Key128 rsa_oaep_kek{}; | ||
| 823 | std::transform(seed3.begin(), seed3.end(), mask0.begin(), rsa_oaep_kek.begin(), | ||
| 824 | std::bit_xor<>()); | ||
| 825 | |||
| 826 | if (rsa_oaep_kek == Key128{}) | ||
| 827 | return; | ||
| 828 | |||
| 829 | SetKey(S128KeyType::Source, rsa_oaep_kek, | ||
| 830 | static_cast<u64>(SourceKeyType::RSAOaepKekGeneration)); | ||
| 831 | |||
| 832 | Key128 temp_kek{}; | ||
| 833 | Key128 temp_kekek{}; | ||
| 834 | Key128 eticket_final{}; | ||
| 835 | |||
| 836 | // Derive ETicket RSA Kek | ||
| 837 | AESCipher<Key128> es_master(GetKey(S128KeyType::Master), Mode::ECB); | ||
| 838 | es_master.Transcode(rsa_oaep_kek.data(), rsa_oaep_kek.size(), temp_kek.data(), Op::Decrypt); | ||
| 839 | AESCipher<Key128> es_kekek(temp_kek, Mode::ECB); | ||
| 840 | es_kekek.Transcode(eticket_kekek.data(), eticket_kekek.size(), temp_kekek.data(), Op::Decrypt); | ||
| 841 | AESCipher<Key128> es_kek(temp_kekek, Mode::ECB); | ||
| 842 | es_kek.Transcode(eticket_kek.data(), eticket_kek.size(), eticket_final.data(), Op::Decrypt); | ||
| 843 | |||
| 844 | if (eticket_final == Key128{}) | ||
| 845 | return; | ||
| 846 | |||
| 847 | SetKey(S128KeyType::ETicketRSAKek, eticket_final); | ||
| 848 | |||
| 849 | // Titlekeys | ||
| 850 | data.DecryptProdInfo(GetBISKey(0)); | ||
| 851 | |||
| 852 | const auto eticket_extended_kek = data.GetETicketExtendedKek(); | ||
| 853 | |||
| 854 | std::vector<u8> extended_iv(0x10); | ||
| 855 | std::memcpy(extended_iv.data(), eticket_extended_kek.data(), extended_iv.size()); | ||
| 856 | std::array<u8, 0x230> extended_dec{}; | ||
| 857 | AESCipher<Key128> rsa_1(eticket_final, Mode::CTR); | ||
| 858 | rsa_1.SetIV(extended_iv); | ||
| 859 | rsa_1.Transcode(eticket_extended_kek.data() + 0x10, eticket_extended_kek.size() - 0x10, | ||
| 860 | extended_dec.data(), Op::Decrypt); | ||
| 861 | |||
| 862 | RSAKeyPair<2048> rsa_key{}; | ||
| 863 | std::memcpy(rsa_key.decryption_key.data(), extended_dec.data(), rsa_key.decryption_key.size()); | ||
| 864 | std::memcpy(rsa_key.modulus.data(), extended_dec.data() + 0x100, rsa_key.modulus.size()); | ||
| 865 | std::memcpy(rsa_key.exponent.data(), extended_dec.data() + 0x200, rsa_key.exponent.size()); | ||
| 866 | |||
| 867 | const FileUtil::IOFile save1(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + | ||
| 868 | "/system/save/80000000000000e1", | ||
| 869 | "rb+"); | ||
| 870 | const FileUtil::IOFile save2(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + | ||
| 871 | "/system/save/80000000000000e2", | ||
| 872 | "rb+"); | ||
| 873 | |||
| 874 | auto res = GetTicketblob(save1); | ||
| 875 | const auto res2 = GetTicketblob(save2); | ||
| 876 | std::copy(res2.begin(), res2.end(), std::back_inserter(res)); | ||
| 877 | |||
| 878 | for (const auto& raw : res) { | ||
| 879 | const auto pair = ParseTicket(raw, rsa_key); | ||
| 880 | if (pair == boost::none) | ||
| 881 | continue; | ||
| 882 | const auto& [rid, key] = pair.value(); | ||
| 883 | u128 rights_id; | ||
| 884 | std::memcpy(rights_id.data(), rid.data(), rid.size()); | ||
| 885 | SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); | ||
| 886 | } | ||
| 887 | } | ||
| 888 | |||
| 889 | void KeyManager::SetKeyWrapped(S128KeyType id, Key128 key, u64 field1, u64 field2) { | ||
| 890 | if (key == Key128{}) | ||
| 891 | return; | ||
| 892 | SetKey(id, key, field1, field2); | ||
| 893 | } | ||
| 894 | |||
| 895 | void KeyManager::SetKeyWrapped(S256KeyType id, Key256 key, u64 field1, u64 field2) { | ||
| 896 | if (key == Key256{}) | ||
| 897 | return; | ||
| 898 | SetKey(id, key, field1, field2); | ||
| 899 | } | ||
| 900 | |||
| 901 | void KeyManager::PopulateFromPartitionData(PartitionDataManager& data) { | ||
| 902 | if (!BaseDeriveNecessary()) | ||
| 903 | return; | ||
| 904 | |||
| 905 | if (!data.HasBoot0()) | ||
| 906 | return; | ||
| 907 | |||
| 908 | for (size_t i = 0; i < encrypted_keyblobs.size(); ++i) { | ||
| 909 | if (encrypted_keyblobs[i] != std::array<u8, 0xB0>{}) | ||
| 910 | continue; | ||
| 911 | encrypted_keyblobs[i] = data.GetEncryptedKeyblob(i); | ||
| 912 | WriteKeyToFile<0xB0>(KeyCategory::Console, fmt::format("encrypted_keyblob_{:02X}", i), | ||
| 913 | encrypted_keyblobs[i]); | ||
| 914 | } | ||
| 915 | |||
| 916 | SetKeyWrapped(S128KeyType::Source, data.GetPackage2KeySource(), | ||
| 917 | static_cast<u64>(SourceKeyType::Package2)); | ||
| 918 | SetKeyWrapped(S128KeyType::Source, data.GetAESKekGenerationSource(), | ||
| 919 | static_cast<u64>(SourceKeyType::AESKekGeneration)); | ||
| 920 | SetKeyWrapped(S128KeyType::Source, data.GetTitlekekSource(), | ||
| 921 | static_cast<u64>(SourceKeyType::Titlekek)); | ||
| 922 | SetKeyWrapped(S128KeyType::Source, data.GetMasterKeySource(), | ||
| 923 | static_cast<u64>(SourceKeyType::Master)); | ||
| 924 | SetKeyWrapped(S128KeyType::Source, data.GetKeyblobMACKeySource(), | ||
| 925 | static_cast<u64>(SourceKeyType::KeyblobMAC)); | ||
| 926 | |||
| 927 | for (size_t i = 0; i < PartitionDataManager::MAX_KEYBLOB_SOURCE_HASH; ++i) { | ||
| 928 | SetKeyWrapped(S128KeyType::Source, data.GetKeyblobKeySource(i), | ||
| 929 | static_cast<u64>(SourceKeyType::Keyblob), i); | ||
| 930 | } | ||
| 931 | |||
| 932 | if (data.HasFuses()) | ||
| 933 | SetKeyWrapped(S128KeyType::SecureBoot, data.GetSecureBootKey()); | ||
| 934 | |||
| 935 | DeriveBase(); | ||
| 936 | |||
| 937 | Key128 latest_master{}; | ||
| 938 | for (s8 i = 0x1F; i >= 0; --i) { | ||
| 939 | if (GetKey(S128KeyType::Master, static_cast<u8>(i)) != Key128{}) { | ||
| 940 | latest_master = GetKey(S128KeyType::Master, static_cast<u8>(i)); | ||
| 941 | break; | ||
| 942 | } | ||
| 943 | } | ||
| 944 | |||
| 945 | const auto masters = data.GetTZMasterKeys(latest_master); | ||
| 946 | for (size_t i = 0; i < masters.size(); ++i) { | ||
| 947 | if (masters[i] != Key128{} && !HasKey(S128KeyType::Master, i)) | ||
| 948 | SetKey(S128KeyType::Master, masters[i], i); | ||
| 949 | } | ||
| 950 | |||
| 951 | DeriveBase(); | ||
| 952 | |||
| 953 | if (!data.HasPackage2()) | ||
| 954 | return; | ||
| 955 | |||
| 956 | std::array<Key128, 0x20> package2_keys{}; | ||
| 957 | for (size_t i = 0; i < package2_keys.size(); ++i) { | ||
| 958 | if (HasKey(S128KeyType::Package2, i)) | ||
| 959 | package2_keys[i] = GetKey(S128KeyType::Package2, i); | ||
| 960 | } | ||
| 961 | data.DecryptPackage2(package2_keys, Package2Type::NormalMain); | ||
| 962 | |||
| 963 | SetKeyWrapped(S128KeyType::Source, data.GetKeyAreaKeyApplicationSource(), | ||
| 964 | static_cast<u64>(SourceKeyType::KeyAreaKey), | ||
| 965 | static_cast<u64>(KeyAreaKeyType::Application)); | ||
| 966 | SetKeyWrapped(S128KeyType::Source, data.GetKeyAreaKeyOceanSource(), | ||
| 967 | static_cast<u64>(SourceKeyType::KeyAreaKey), | ||
| 968 | static_cast<u64>(KeyAreaKeyType::Ocean)); | ||
| 969 | SetKeyWrapped(S128KeyType::Source, data.GetKeyAreaKeySystemSource(), | ||
| 970 | static_cast<u64>(SourceKeyType::KeyAreaKey), | ||
| 971 | static_cast<u64>(KeyAreaKeyType::System)); | ||
| 972 | SetKeyWrapped(S128KeyType::Source, data.GetSDKekSource(), | ||
| 973 | static_cast<u64>(SourceKeyType::SDKek)); | ||
| 974 | SetKeyWrapped(S256KeyType::SDKeySource, data.GetSDSaveKeySource(), | ||
| 975 | static_cast<u64>(SDKeyType::Save)); | ||
| 976 | SetKeyWrapped(S256KeyType::SDKeySource, data.GetSDNCAKeySource(), | ||
| 977 | static_cast<u64>(SDKeyType::NCA)); | ||
| 978 | SetKeyWrapped(S128KeyType::Source, data.GetHeaderKekSource(), | ||
| 979 | static_cast<u64>(SourceKeyType::HeaderKek)); | ||
| 980 | SetKeyWrapped(S256KeyType::HeaderSource, data.GetHeaderKeySource()); | ||
| 981 | SetKeyWrapped(S128KeyType::Source, data.GetAESKeyGenerationSource(), | ||
| 982 | static_cast<u64>(SourceKeyType::AESKeyGeneration)); | ||
| 983 | |||
| 984 | DeriveBase(); | ||
| 985 | } | ||
| 986 | |||
| 293 | const boost::container::flat_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = { | 987 | const boost::container::flat_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = { |
| 294 | {"master_key_00", {S128KeyType::Master, 0, 0}}, | ||
| 295 | {"master_key_01", {S128KeyType::Master, 1, 0}}, | ||
| 296 | {"master_key_02", {S128KeyType::Master, 2, 0}}, | ||
| 297 | {"master_key_03", {S128KeyType::Master, 3, 0}}, | ||
| 298 | {"master_key_04", {S128KeyType::Master, 4, 0}}, | ||
| 299 | {"package1_key_00", {S128KeyType::Package1, 0, 0}}, | ||
| 300 | {"package1_key_01", {S128KeyType::Package1, 1, 0}}, | ||
| 301 | {"package1_key_02", {S128KeyType::Package1, 2, 0}}, | ||
| 302 | {"package1_key_03", {S128KeyType::Package1, 3, 0}}, | ||
| 303 | {"package1_key_04", {S128KeyType::Package1, 4, 0}}, | ||
| 304 | {"package2_key_00", {S128KeyType::Package2, 0, 0}}, | ||
| 305 | {"package2_key_01", {S128KeyType::Package2, 1, 0}}, | ||
| 306 | {"package2_key_02", {S128KeyType::Package2, 2, 0}}, | ||
| 307 | {"package2_key_03", {S128KeyType::Package2, 3, 0}}, | ||
| 308 | {"package2_key_04", {S128KeyType::Package2, 4, 0}}, | ||
| 309 | {"titlekek_00", {S128KeyType::Titlekek, 0, 0}}, | ||
| 310 | {"titlekek_01", {S128KeyType::Titlekek, 1, 0}}, | ||
| 311 | {"titlekek_02", {S128KeyType::Titlekek, 2, 0}}, | ||
| 312 | {"titlekek_03", {S128KeyType::Titlekek, 3, 0}}, | ||
| 313 | {"titlekek_04", {S128KeyType::Titlekek, 4, 0}}, | ||
| 314 | {"eticket_rsa_kek", {S128KeyType::ETicketRSAKek, 0, 0}}, | 988 | {"eticket_rsa_kek", {S128KeyType::ETicketRSAKek, 0, 0}}, |
| 315 | {"key_area_key_application_00", | 989 | {"eticket_rsa_kek_source", |
| 316 | {S128KeyType::KeyArea, 0, static_cast<u64>(KeyAreaKeyType::Application)}}, | 990 | {S128KeyType::Source, static_cast<u64>(SourceKeyType::ETicketKek), 0}}, |
| 317 | {"key_area_key_application_01", | 991 | {"eticket_rsa_kekek_source", |
| 318 | {S128KeyType::KeyArea, 1, static_cast<u64>(KeyAreaKeyType::Application)}}, | 992 | {S128KeyType::Source, static_cast<u64>(SourceKeyType::ETicketKekek), 0}}, |
| 319 | {"key_area_key_application_02", | 993 | {"rsa_kek_mask_0", {S128KeyType::RSAKek, static_cast<u64>(RSAKekType::Mask0), 0}}, |
| 320 | {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::Application)}}, | 994 | {"rsa_kek_seed_3", {S128KeyType::RSAKek, static_cast<u64>(RSAKekType::Seed3), 0}}, |
| 321 | {"key_area_key_application_03", | 995 | {"rsa_oaep_kek_generation_source", |
| 322 | {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::Application)}}, | 996 | {S128KeyType::Source, static_cast<u64>(SourceKeyType::RSAOaepKekGeneration), 0}}, |
| 323 | {"key_area_key_application_04", | 997 | {"sd_card_kek_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKek), 0}}, |
| 324 | {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::Application)}}, | ||
| 325 | {"key_area_key_ocean_00", {S128KeyType::KeyArea, 0, static_cast<u64>(KeyAreaKeyType::Ocean)}}, | ||
| 326 | {"key_area_key_ocean_01", {S128KeyType::KeyArea, 1, static_cast<u64>(KeyAreaKeyType::Ocean)}}, | ||
| 327 | {"key_area_key_ocean_02", {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::Ocean)}}, | ||
| 328 | {"key_area_key_ocean_03", {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::Ocean)}}, | ||
| 329 | {"key_area_key_ocean_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::Ocean)}}, | ||
| 330 | {"key_area_key_system_00", {S128KeyType::KeyArea, 0, static_cast<u64>(KeyAreaKeyType::System)}}, | ||
| 331 | {"key_area_key_system_01", {S128KeyType::KeyArea, 1, static_cast<u64>(KeyAreaKeyType::System)}}, | ||
| 332 | {"key_area_key_system_02", {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::System)}}, | ||
| 333 | {"key_area_key_system_03", {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::System)}}, | ||
| 334 | {"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::System)}}, | ||
| 335 | {"sd_card_kek_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKEK), 0}}, | ||
| 336 | {"aes_kek_generation_source", | 998 | {"aes_kek_generation_source", |
| 337 | {S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKEKGeneration), 0}}, | 999 | {S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration), 0}}, |
| 338 | {"aes_key_generation_source", | 1000 | {"aes_key_generation_source", |
| 339 | {S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration), 0}}, | 1001 | {S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration), 0}}, |
| 1002 | {"package2_key_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::Package2), 0}}, | ||
| 1003 | {"master_key_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::Master), 0}}, | ||
| 1004 | {"header_kek_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::HeaderKek), 0}}, | ||
| 1005 | {"key_area_key_application_source", | ||
| 1006 | {S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey), | ||
| 1007 | static_cast<u64>(KeyAreaKeyType::Application)}}, | ||
| 1008 | {"key_area_key_ocean_source", | ||
| 1009 | {S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey), | ||
| 1010 | static_cast<u64>(KeyAreaKeyType::Ocean)}}, | ||
| 1011 | {"key_area_key_system_source", | ||
| 1012 | {S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey), | ||
| 1013 | static_cast<u64>(KeyAreaKeyType::System)}}, | ||
| 1014 | {"titlekek_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::Titlekek), 0}}, | ||
| 1015 | {"keyblob_mac_key_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyblobMAC)}}, | ||
| 1016 | {"tsec_key", {S128KeyType::TSEC, 0, 0}}, | ||
| 1017 | {"secure_boot_key", {S128KeyType::SecureBoot, 0, 0}}, | ||
| 340 | {"sd_seed", {S128KeyType::SDSeed, 0, 0}}, | 1018 | {"sd_seed", {S128KeyType::SDSeed, 0, 0}}, |
| 1019 | {"bis_key_0_crypt", {S128KeyType::BIS, 0, static_cast<u64>(BISKeyType::Crypto)}}, | ||
| 1020 | {"bis_key_0_tweak", {S128KeyType::BIS, 0, static_cast<u64>(BISKeyType::Tweak)}}, | ||
| 1021 | {"bis_key_1_crypt", {S128KeyType::BIS, 1, static_cast<u64>(BISKeyType::Crypto)}}, | ||
| 1022 | {"bis_key_1_tweak", {S128KeyType::BIS, 1, static_cast<u64>(BISKeyType::Tweak)}}, | ||
| 1023 | {"bis_key_2_crypt", {S128KeyType::BIS, 2, static_cast<u64>(BISKeyType::Crypto)}}, | ||
| 1024 | {"bis_key_2_tweak", {S128KeyType::BIS, 2, static_cast<u64>(BISKeyType::Tweak)}}, | ||
| 1025 | {"bis_key_3_crypt", {S128KeyType::BIS, 3, static_cast<u64>(BISKeyType::Crypto)}}, | ||
| 1026 | {"bis_key_3_tweak", {S128KeyType::BIS, 3, static_cast<u64>(BISKeyType::Tweak)}}, | ||
| 1027 | {"header_kek", {S128KeyType::HeaderKek, 0, 0}}, | ||
| 1028 | {"sd_card_kek", {S128KeyType::SDKek, 0, 0}}, | ||
| 341 | }; | 1029 | }; |
| 342 | 1030 | ||
| 343 | const boost::container::flat_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = { | 1031 | const boost::container::flat_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = { |
| 344 | {"header_key", {S256KeyType::Header, 0, 0}}, | 1032 | {"header_key", {S256KeyType::Header, 0, 0}}, |
| 345 | {"sd_card_save_key_source", {S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::Save), 0}}, | 1033 | {"sd_card_save_key_source", {S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::Save), 0}}, |
| 346 | {"sd_card_nca_key_source", {S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::NCA), 0}}, | 1034 | {"sd_card_nca_key_source", {S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::NCA), 0}}, |
| 1035 | {"header_key_source", {S256KeyType::HeaderSource, 0, 0}}, | ||
| 1036 | {"sd_card_save_key", {S256KeyType::SDKey, static_cast<u64>(SDKeyType::Save), 0}}, | ||
| 1037 | {"sd_card_nca_key", {S256KeyType::SDKey, static_cast<u64>(SDKeyType::NCA), 0}}, | ||
| 347 | }; | 1038 | }; |
| 348 | } // namespace Core::Crypto | 1039 | } // namespace Core::Crypto |
diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index 978eec8dc..a41abbdfc 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h | |||
| @@ -5,11 +5,18 @@ | |||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <map> | ||
| 8 | #include <string> | 9 | #include <string> |
| 9 | #include <boost/container/flat_map.hpp> | 10 | #include <boost/container/flat_map.hpp> |
| 10 | #include <boost/optional.hpp> | 11 | #include <boost/optional.hpp> |
| 11 | #include <fmt/format.h> | 12 | #include <fmt/format.h> |
| 12 | #include "common/common_types.h" | 13 | #include "common/common_types.h" |
| 14 | #include "core/crypto/partition_data_manager.h" | ||
| 15 | #include "core/file_sys/vfs_types.h" | ||
| 16 | |||
| 17 | namespace FileUtil { | ||
| 18 | class IOFile; | ||
| 19 | } | ||
| 13 | 20 | ||
| 14 | namespace Loader { | 21 | namespace Loader { |
| 15 | enum class ResultStatus : u16; | 22 | enum class ResultStatus : u16; |
| @@ -22,13 +29,30 @@ constexpr u64 TICKET_FILE_TITLEKEY_OFFSET = 0x180; | |||
| 22 | using Key128 = std::array<u8, 0x10>; | 29 | using Key128 = std::array<u8, 0x10>; |
| 23 | using Key256 = std::array<u8, 0x20>; | 30 | using Key256 = std::array<u8, 0x20>; |
| 24 | using SHA256Hash = std::array<u8, 0x20>; | 31 | using SHA256Hash = std::array<u8, 0x20>; |
| 32 | using TicketRaw = std::array<u8, 0x400>; | ||
| 25 | 33 | ||
| 26 | static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big."); | 34 | static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big."); |
| 27 | static_assert(sizeof(Key256) == 32, "Key128 must be 128 bytes big."); | 35 | static_assert(sizeof(Key256) == 32, "Key256 must be 256 bytes big."); |
| 36 | |||
| 37 | template <size_t bit_size, size_t byte_size = (bit_size >> 3)> | ||
| 38 | struct RSAKeyPair { | ||
| 39 | std::array<u8, byte_size> encryption_key; | ||
| 40 | std::array<u8, byte_size> decryption_key; | ||
| 41 | std::array<u8, byte_size> modulus; | ||
| 42 | std::array<u8, 4> exponent; | ||
| 43 | }; | ||
| 44 | |||
| 45 | enum class KeyCategory : u8 { | ||
| 46 | Standard, | ||
| 47 | Title, | ||
| 48 | Console, | ||
| 49 | }; | ||
| 28 | 50 | ||
| 29 | enum class S256KeyType : u64 { | 51 | enum class S256KeyType : u64 { |
| 30 | Header, // | 52 | SDKey, // f1=SDKeyType |
| 31 | SDKeySource, // f1=SDKeyType | 53 | Header, // |
| 54 | SDKeySource, // f1=SDKeyType | ||
| 55 | HeaderSource, // | ||
| 32 | }; | 56 | }; |
| 33 | 57 | ||
| 34 | enum class S128KeyType : u64 { | 58 | enum class S128KeyType : u64 { |
| @@ -41,6 +65,14 @@ enum class S128KeyType : u64 { | |||
| 41 | SDSeed, // | 65 | SDSeed, // |
| 42 | Titlekey, // f1=rights id LSB f2=rights id MSB | 66 | Titlekey, // f1=rights id LSB f2=rights id MSB |
| 43 | Source, // f1=source type, f2= sub id | 67 | Source, // f1=source type, f2= sub id |
| 68 | Keyblob, // f1=crypto revision | ||
| 69 | KeyblobMAC, // f1=crypto revision | ||
| 70 | TSEC, // | ||
| 71 | SecureBoot, // | ||
| 72 | BIS, // f1=partition (0-3), f2=type {crypt, tweak} | ||
| 73 | HeaderKek, // | ||
| 74 | SDKek, // | ||
| 75 | RSAKek, // | ||
| 44 | }; | 76 | }; |
| 45 | 77 | ||
| 46 | enum class KeyAreaKeyType : u8 { | 78 | enum class KeyAreaKeyType : u8 { |
| @@ -50,9 +82,19 @@ enum class KeyAreaKeyType : u8 { | |||
| 50 | }; | 82 | }; |
| 51 | 83 | ||
| 52 | enum class SourceKeyType : u8 { | 84 | enum class SourceKeyType : u8 { |
| 53 | SDKEK, | 85 | SDKek, // |
| 54 | AESKEKGeneration, | 86 | AESKekGeneration, // |
| 55 | AESKeyGeneration, | 87 | AESKeyGeneration, // |
| 88 | RSAOaepKekGeneration, // | ||
| 89 | Master, // | ||
| 90 | Keyblob, // f2=crypto revision | ||
| 91 | KeyAreaKey, // f2=KeyAreaKeyType | ||
| 92 | Titlekek, // | ||
| 93 | Package2, // | ||
| 94 | HeaderKek, // | ||
| 95 | KeyblobMAC, // | ||
| 96 | ETicketKek, // | ||
| 97 | ETicketKekek, // | ||
| 56 | }; | 98 | }; |
| 57 | 99 | ||
| 58 | enum class SDKeyType : u8 { | 100 | enum class SDKeyType : u8 { |
| @@ -60,6 +102,16 @@ enum class SDKeyType : u8 { | |||
| 60 | NCA, | 102 | NCA, |
| 61 | }; | 103 | }; |
| 62 | 104 | ||
| 105 | enum class BISKeyType : u8 { | ||
| 106 | Crypto, | ||
| 107 | Tweak, | ||
| 108 | }; | ||
| 109 | |||
| 110 | enum class RSAKekType : u8 { | ||
| 111 | Mask0, | ||
| 112 | Seed3, | ||
| 113 | }; | ||
| 114 | |||
| 63 | template <typename KeyType> | 115 | template <typename KeyType> |
| 64 | struct KeyIndex { | 116 | struct KeyIndex { |
| 65 | KeyType type; | 117 | KeyType type; |
| @@ -91,6 +143,8 @@ public: | |||
| 91 | Key128 GetKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0) const; | 143 | Key128 GetKey(S128KeyType id, u64 field1 = 0, u64 field2 = 0) const; |
| 92 | Key256 GetKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0) const; | 144 | Key256 GetKey(S256KeyType id, u64 field1 = 0, u64 field2 = 0) const; |
| 93 | 145 | ||
| 146 | Key256 GetBISKey(u8 partition_id) const; | ||
| 147 | |||
| 94 | void SetKey(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); | 148 | void SetKey(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); |
| 95 | void SetKey(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); | 149 | void SetKey(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); |
| 96 | 150 | ||
| @@ -100,23 +154,51 @@ public: | |||
| 100 | // 8*43 and the private file to exist. | 154 | // 8*43 and the private file to exist. |
| 101 | void DeriveSDSeedLazy(); | 155 | void DeriveSDSeedLazy(); |
| 102 | 156 | ||
| 157 | bool BaseDeriveNecessary() const; | ||
| 158 | void DeriveBase(); | ||
| 159 | void DeriveETicket(PartitionDataManager& data); | ||
| 160 | |||
| 161 | void PopulateFromPartitionData(PartitionDataManager& data); | ||
| 162 | |||
| 103 | private: | 163 | private: |
| 104 | boost::container::flat_map<KeyIndex<S128KeyType>, Key128> s128_keys; | 164 | std::map<KeyIndex<S128KeyType>, Key128> s128_keys; |
| 105 | boost::container::flat_map<KeyIndex<S256KeyType>, Key256> s256_keys; | 165 | std::map<KeyIndex<S256KeyType>, Key256> s256_keys; |
| 166 | |||
| 167 | std::array<std::array<u8, 0xB0>, 0x20> encrypted_keyblobs{}; | ||
| 168 | std::array<std::array<u8, 0x90>, 0x20> keyblobs{}; | ||
| 106 | 169 | ||
| 107 | bool dev_mode; | 170 | bool dev_mode; |
| 108 | void LoadFromFile(const std::string& filename, bool is_title_keys); | 171 | void LoadFromFile(const std::string& filename, bool is_title_keys); |
| 109 | void AttemptLoadKeyFile(const std::string& dir1, const std::string& dir2, | 172 | void AttemptLoadKeyFile(const std::string& dir1, const std::string& dir2, |
| 110 | const std::string& filename, bool title); | 173 | const std::string& filename, bool title); |
| 111 | template <std::size_t Size> | 174 | template <size_t Size> |
| 112 | void WriteKeyToFile(bool title_key, std::string_view keyname, const std::array<u8, Size>& key); | 175 | void WriteKeyToFile(KeyCategory category, std::string_view keyname, |
| 176 | const std::array<u8, Size>& key); | ||
| 177 | |||
| 178 | void DeriveGeneralPurposeKeys(u8 crypto_revision); | ||
| 179 | |||
| 180 | void SetKeyWrapped(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); | ||
| 181 | void SetKeyWrapped(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); | ||
| 113 | 182 | ||
| 114 | static const boost::container::flat_map<std::string, KeyIndex<S128KeyType>> s128_file_id; | 183 | static const boost::container::flat_map<std::string, KeyIndex<S128KeyType>> s128_file_id; |
| 115 | static const boost::container::flat_map<std::string, KeyIndex<S256KeyType>> s256_file_id; | 184 | static const boost::container::flat_map<std::string, KeyIndex<S256KeyType>> s256_file_id; |
| 116 | }; | 185 | }; |
| 117 | 186 | ||
| 118 | Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed); | 187 | Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed); |
| 188 | Key128 DeriveKeyblobKey(const Key128& sbk, const Key128& tsec, Key128 source); | ||
| 189 | Key128 DeriveKeyblobMACKey(const Key128& keyblob_key, const Key128& mac_source); | ||
| 190 | Key128 DeriveMasterKey(const std::array<u8, 0x90>& keyblob, const Key128& master_source); | ||
| 191 | std::array<u8, 0x90> DecryptKeyblob(const std::array<u8, 0xB0>& encrypted_keyblob, | ||
| 192 | const Key128& key); | ||
| 193 | |||
| 119 | boost::optional<Key128> DeriveSDSeed(); | 194 | boost::optional<Key128> DeriveSDSeed(); |
| 120 | Loader::ResultStatus DeriveSDKeys(std::array<Key256, 2>& sd_keys, const KeyManager& keys); | 195 | Loader::ResultStatus DeriveSDKeys(std::array<Key256, 2>& sd_keys, KeyManager& keys); |
| 196 | |||
| 197 | std::vector<TicketRaw> GetTicketblob(const FileUtil::IOFile& ticket_save); | ||
| 198 | |||
| 199 | // Returns a pair of {rights_id, titlekey}. Fails if the ticket has no certificate authority (offset | ||
| 200 | // 0x140-0x144 is zero) | ||
| 201 | boost::optional<std::pair<Key128, Key128>> ParseTicket( | ||
| 202 | const TicketRaw& ticket, const RSAKeyPair<2048>& eticket_extended_key); | ||
| 121 | 203 | ||
| 122 | } // namespace Core::Crypto | 204 | } // namespace Core::Crypto |
diff --git a/src/core/crypto/partition_data_manager.cpp b/src/core/crypto/partition_data_manager.cpp new file mode 100644 index 000000000..d1c04e98d --- /dev/null +++ b/src/core/crypto/partition_data_manager.cpp | |||
| @@ -0,0 +1,601 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | // NOTE TO FUTURE MAINTAINERS: | ||
| 6 | // When a new version of switch cryptography is released, | ||
| 7 | // hash the new keyblob source and master key and add the hashes to | ||
| 8 | // the arrays below. | ||
| 9 | |||
| 10 | #include <algorithm> | ||
| 11 | #include <array> | ||
| 12 | #include <cctype> | ||
| 13 | #include <cstring> | ||
| 14 | #include <boost/optional/optional.hpp> | ||
| 15 | #include <mbedtls/sha256.h> | ||
| 16 | #include "common/assert.h" | ||
| 17 | #include "common/common_funcs.h" | ||
| 18 | #include "common/common_types.h" | ||
| 19 | #include "common/hex_util.h" | ||
| 20 | #include "common/logging/log.h" | ||
| 21 | #include "common/string_util.h" | ||
| 22 | #include "core/crypto/ctr_encryption_layer.h" | ||
| 23 | #include "core/crypto/key_manager.h" | ||
| 24 | #include "core/crypto/partition_data_manager.h" | ||
| 25 | #include "core/crypto/xts_encryption_layer.h" | ||
| 26 | #include "core/file_sys/vfs.h" | ||
| 27 | #include "core/file_sys/vfs_offset.h" | ||
| 28 | |||
| 29 | using namespace Common; | ||
| 30 | |||
| 31 | namespace Core::Crypto { | ||
| 32 | |||
| 33 | struct Package2Header { | ||
| 34 | std::array<u8, 0x100> signature; | ||
| 35 | Key128 header_ctr; | ||
| 36 | std::array<Key128, 4> section_ctr; | ||
| 37 | u32_le magic; | ||
| 38 | u32_le base_offset; | ||
| 39 | INSERT_PADDING_BYTES(4); | ||
| 40 | u8 version_max; | ||
| 41 | u8 version_min; | ||
| 42 | INSERT_PADDING_BYTES(2); | ||
| 43 | std::array<u32_le, 4> section_size; | ||
| 44 | std::array<u32_le, 4> section_offset; | ||
| 45 | std::array<SHA256Hash, 4> section_hash; | ||
| 46 | }; | ||
| 47 | static_assert(sizeof(Package2Header) == 0x200, "Package2Header has incorrect size."); | ||
| 48 | |||
| 49 | struct INIHeader { | ||
| 50 | u32_le magic; | ||
| 51 | u32_le size; | ||
| 52 | u32_le process_count; | ||
| 53 | INSERT_PADDING_BYTES(4); | ||
| 54 | }; | ||
| 55 | static_assert(sizeof(INIHeader) == 0x10, "INIHeader has incorrect size."); | ||
| 56 | |||
| 57 | struct SectionHeader { | ||
| 58 | u32_le offset; | ||
| 59 | u32_le size_decompressed; | ||
| 60 | u32_le size_compressed; | ||
| 61 | u32_le attribute; | ||
| 62 | }; | ||
| 63 | static_assert(sizeof(SectionHeader) == 0x10, "SectionHeader has incorrect size."); | ||
| 64 | |||
| 65 | struct KIPHeader { | ||
| 66 | u32_le magic; | ||
| 67 | std::array<char, 12> name; | ||
| 68 | u64_le title_id; | ||
| 69 | u32_le category; | ||
| 70 | u8 priority; | ||
| 71 | u8 core; | ||
| 72 | INSERT_PADDING_BYTES(1); | ||
| 73 | u8 flags; | ||
| 74 | std::array<SectionHeader, 6> sections; | ||
| 75 | std::array<u32, 0x20> capabilities; | ||
| 76 | }; | ||
| 77 | static_assert(sizeof(KIPHeader) == 0x100, "KIPHeader has incorrect size."); | ||
| 78 | |||
| 79 | const std::array<SHA256Hash, 0x10> source_hashes{ | ||
| 80 | "B24BD293259DBC7AC5D63F88E60C59792498E6FC5443402C7FFE87EE8B61A3F0"_array32, // keyblob_mac_key_source | ||
| 81 | "7944862A3A5C31C6720595EFD302245ABD1B54CCDCF33000557681E65C5664A4"_array32, // master_key_source | ||
| 82 | "21E2DF100FC9E094DB51B47B9B1D6E94ED379DB8B547955BEF8FE08D8DD35603"_array32, // package2_key_source | ||
| 83 | "FC02B9D37B42D7A1452E71444F1F700311D1132E301A83B16062E72A78175085"_array32, // aes_kek_generation_source | ||
| 84 | "FBD10056999EDC7ACDB96098E47E2C3606230270D23281E671F0F389FC5BC585"_array32, // aes_key_generation_source | ||
| 85 | "C48B619827986C7F4E3081D59DB2B460C84312650E9A8E6B458E53E8CBCA4E87"_array32, // titlekek_source | ||
| 86 | "04AD66143C726B2A139FB6B21128B46F56C553B2B3887110304298D8D0092D9E"_array32, // key_area_key_application_source | ||
| 87 | "FD434000C8FF2B26F8E9A9D2D2C12F6BE5773CBB9DC86300E1BD99F8EA33A417"_array32, // key_area_key_ocean_source | ||
| 88 | "1F17B1FD51AD1C2379B58F152CA4912EC2106441E51722F38700D5937A1162F7"_array32, // key_area_key_system_source | ||
| 89 | "6B2ED877C2C52334AC51E59ABFA7EC457F4A7D01E46291E9F2EAA45F011D24B7"_array32, // sd_card_kek_source | ||
| 90 | "D482743563D3EA5DCDC3B74E97C9AC8A342164FA041A1DC80F17F6D31E4BC01C"_array32, // sd_card_save_key_source | ||
| 91 | "2E751CECF7D93A2B957BD5FFCB082FD038CC2853219DD3092C6DAB9838F5A7CC"_array32, // sd_card_nca_key_source | ||
| 92 | "1888CAED5551B3EDE01499E87CE0D86827F80820EFB275921055AA4E2ABDFFC2"_array32, // header_kek_source | ||
| 93 | "8F783E46852DF6BE0BA4E19273C4ADBAEE16380043E1B8C418C4089A8BD64AA6"_array32, // header_key_source | ||
| 94 | "D1757E52F1AE55FA882EC690BC6F954AC46A83DC22F277F8806BD55577C6EED7"_array32, // rsa_kek_seed3 | ||
| 95 | "FC02B9D37B42D7A1452E71444F1F700311D1132E301A83B16062E72A78175085"_array32, // rsa_kek_mask0 | ||
| 96 | }; | ||
| 97 | |||
| 98 | const std::array<SHA256Hash, 0x20> keyblob_source_hashes{ | ||
| 99 | "8A06FE274AC491436791FDB388BCDD3AB9943BD4DEF8094418CDAC150FD73786"_array32, // keyblob_key_source_00 | ||
| 100 | "2D5CAEB2521FEF70B47E17D6D0F11F8CE2C1E442A979AD8035832C4E9FBCCC4B"_array32, // keyblob_key_source_01 | ||
| 101 | "61C5005E713BAE780641683AF43E5F5C0E03671117F702F401282847D2FC6064"_array32, // keyblob_key_source_02 | ||
| 102 | "8E9795928E1C4428E1B78F0BE724D7294D6934689C11B190943923B9D5B85903"_array32, // keyblob_key_source_03 | ||
| 103 | "95FA33AF95AFF9D9B61D164655B32710ED8D615D46C7D6CC3CC70481B686B402"_array32, // keyblob_key_source_04 | ||
| 104 | "3F5BE7B3C8B1ABD8C10B4B703D44766BA08730562C172A4FE0D6B866B3E2DB3E"_array32, // keyblob_key_source_05 | ||
| 105 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_06 | ||
| 106 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_07 | ||
| 107 | |||
| 108 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_08 | ||
| 109 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_09 | ||
| 110 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_0A | ||
| 111 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_0B | ||
| 112 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_0C | ||
| 113 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_0D | ||
| 114 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_0E | ||
| 115 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_0F | ||
| 116 | |||
| 117 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_10 | ||
| 118 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_11 | ||
| 119 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_12 | ||
| 120 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_13 | ||
| 121 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_14 | ||
| 122 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_15 | ||
| 123 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_16 | ||
| 124 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_17 | ||
| 125 | |||
| 126 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_18 | ||
| 127 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_19 | ||
| 128 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_1A | ||
| 129 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_1B | ||
| 130 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_1C | ||
| 131 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_1D | ||
| 132 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_1E | ||
| 133 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_1F | ||
| 134 | }; | ||
| 135 | |||
| 136 | const std::array<SHA256Hash, 0x20> master_key_hashes{ | ||
| 137 | "0EE359BE3C864BB0782E1D70A718A0342C551EED28C369754F9C4F691BECF7CA"_array32, // master_key_00 | ||
| 138 | "4FE707B7E4ABDAF727C894AAF13B1351BFE2AC90D875F73B2E20FA94B9CC661E"_array32, // master_key_01 | ||
| 139 | "79277C0237A2252EC3DFAC1F7C359C2B3D121E9DB15BB9AB4C2B4408D2F3AE09"_array32, // master_key_02 | ||
| 140 | "4F36C565D13325F65EE134073C6A578FFCB0008E02D69400836844EAB7432754"_array32, // master_key_03 | ||
| 141 | "75FF1D95D26113550EE6FCC20ACB58E97EDEB3A2FF52543ED5AEC63BDCC3DA50"_array32, // master_key_04 | ||
| 142 | "EBE2BCD6704673EC0F88A187BB2AD9F1CC82B718C389425941BDC194DC46B0DD"_array32, // master_key_05 | ||
| 143 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_06 | ||
| 144 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_07 | ||
| 145 | |||
| 146 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_08 | ||
| 147 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_09 | ||
| 148 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_0A | ||
| 149 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_0B | ||
| 150 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_0C | ||
| 151 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_0D | ||
| 152 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_0E | ||
| 153 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_0F | ||
| 154 | |||
| 155 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_10 | ||
| 156 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_11 | ||
| 157 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_12 | ||
| 158 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_13 | ||
| 159 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_14 | ||
| 160 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_15 | ||
| 161 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_16 | ||
| 162 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_17 | ||
| 163 | |||
| 164 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_18 | ||
| 165 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_19 | ||
| 166 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_1A | ||
| 167 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_1B | ||
| 168 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_1C | ||
| 169 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_1D | ||
| 170 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_1E | ||
| 171 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_1F | ||
| 172 | }; | ||
| 173 | |||
| 174 | static std::vector<u8> DecompressBLZ(const std::vector<u8>& in) { | ||
| 175 | const auto data_size = in.size() - 0xC; | ||
| 176 | |||
| 177 | u32 compressed_size{}; | ||
| 178 | u32 init_index{}; | ||
| 179 | u32 additional_size{}; | ||
| 180 | std::memcpy(&compressed_size, in.data() + data_size, sizeof(u32)); | ||
| 181 | std::memcpy(&init_index, in.data() + data_size + 0x4, sizeof(u32)); | ||
| 182 | std::memcpy(&additional_size, in.data() + data_size + 0x8, sizeof(u32)); | ||
| 183 | |||
| 184 | std::vector<u8> out(in.size() + additional_size); | ||
| 185 | |||
| 186 | if (compressed_size == in.size()) | ||
| 187 | std::memcpy(out.data(), in.data() + in.size() - compressed_size, compressed_size); | ||
| 188 | else | ||
| 189 | std::memcpy(out.data(), in.data(), compressed_size); | ||
| 190 | |||
| 191 | auto index = in.size() - init_index; | ||
| 192 | auto out_index = out.size(); | ||
| 193 | |||
| 194 | while (out_index > 0) { | ||
| 195 | --index; | ||
| 196 | auto control = in[index]; | ||
| 197 | for (size_t i = 0; i < 8; ++i) { | ||
| 198 | if ((control & 0x80) > 0) { | ||
| 199 | ASSERT(index >= 2); | ||
| 200 | index -= 2; | ||
| 201 | u64 segment_offset = in[index] | in[index + 1] << 8; | ||
| 202 | u64 segment_size = ((segment_offset >> 12) & 0xF) + 3; | ||
| 203 | segment_offset &= 0xFFF; | ||
| 204 | segment_offset += 3; | ||
| 205 | |||
| 206 | if (out_index < segment_size) | ||
| 207 | segment_size = out_index; | ||
| 208 | |||
| 209 | ASSERT(out_index >= segment_size); | ||
| 210 | |||
| 211 | out_index -= segment_size; | ||
| 212 | |||
| 213 | for (size_t j = 0; j < segment_size; ++j) { | ||
| 214 | ASSERT(out_index + j + segment_offset < out.size()); | ||
| 215 | out[out_index + j] = out[out_index + j + segment_offset]; | ||
| 216 | } | ||
| 217 | } else { | ||
| 218 | ASSERT(out_index >= 1); | ||
| 219 | --out_index; | ||
| 220 | --index; | ||
| 221 | out[out_index] = in[index]; | ||
| 222 | } | ||
| 223 | |||
| 224 | control <<= 1; | ||
| 225 | if (out_index == 0) | ||
| 226 | return out; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | return out; | ||
| 231 | } | ||
| 232 | |||
| 233 | static u8 CalculateMaxKeyblobSourceHash() { | ||
| 234 | for (s8 i = 0x1F; i >= 0; --i) { | ||
| 235 | if (keyblob_source_hashes[i] != SHA256Hash{}) | ||
| 236 | return static_cast<u8>(i + 1); | ||
| 237 | } | ||
| 238 | |||
| 239 | return 0; | ||
| 240 | } | ||
| 241 | |||
| 242 | const u8 PartitionDataManager::MAX_KEYBLOB_SOURCE_HASH = CalculateMaxKeyblobSourceHash(); | ||
| 243 | |||
| 244 | template <size_t key_size = 0x10> | ||
| 245 | std::array<u8, key_size> FindKeyFromHex(const std::vector<u8>& binary, | ||
| 246 | const std::array<u8, 0x20>& hash) { | ||
| 247 | if (binary.size() < key_size) | ||
| 248 | return {}; | ||
| 249 | |||
| 250 | std::array<u8, 0x20> temp{}; | ||
| 251 | for (size_t i = 0; i < binary.size() - key_size; ++i) { | ||
| 252 | mbedtls_sha256(binary.data() + i, key_size, temp.data(), 0); | ||
| 253 | |||
| 254 | if (temp != hash) | ||
| 255 | continue; | ||
| 256 | |||
| 257 | std::array<u8, key_size> out{}; | ||
| 258 | std::memcpy(out.data(), binary.data() + i, key_size); | ||
| 259 | return out; | ||
| 260 | } | ||
| 261 | |||
| 262 | return {}; | ||
| 263 | } | ||
| 264 | |||
| 265 | std::array<u8, 16> FindKeyFromHex16(const std::vector<u8>& binary, std::array<u8, 32> hash) { | ||
| 266 | return FindKeyFromHex<0x10>(binary, hash); | ||
| 267 | } | ||
| 268 | |||
| 269 | static std::array<Key128, 0x20> FindEncryptedMasterKeyFromHex(const std::vector<u8>& binary, | ||
| 270 | const Key128& key) { | ||
| 271 | if (binary.size() < 0x10) | ||
| 272 | return {}; | ||
| 273 | |||
| 274 | SHA256Hash temp{}; | ||
| 275 | Key128 dec_temp{}; | ||
| 276 | std::array<Key128, 0x20> out{}; | ||
| 277 | AESCipher<Key128> cipher(key, Mode::ECB); | ||
| 278 | for (size_t i = 0; i < binary.size() - 0x10; ++i) { | ||
| 279 | cipher.Transcode(binary.data() + i, dec_temp.size(), dec_temp.data(), Op::Decrypt); | ||
| 280 | mbedtls_sha256(dec_temp.data(), dec_temp.size(), temp.data(), 0); | ||
| 281 | |||
| 282 | for (size_t k = 0; k < out.size(); ++k) { | ||
| 283 | if (temp == master_key_hashes[k]) { | ||
| 284 | out[k] = dec_temp; | ||
| 285 | break; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | return out; | ||
| 291 | } | ||
| 292 | |||
| 293 | FileSys::VirtualFile FindFileInDirWithNames(const FileSys::VirtualDir& dir, | ||
| 294 | const std::string& name) { | ||
| 295 | auto upper = name; | ||
| 296 | std::transform(upper.begin(), upper.end(), upper.begin(), [](u8 c) { return std::toupper(c); }); | ||
| 297 | for (const auto& fname : {name, name + ".bin", upper, upper + ".BIN"}) { | ||
| 298 | if (dir->GetFile(fname) != nullptr) | ||
| 299 | return dir->GetFile(fname); | ||
| 300 | } | ||
| 301 | |||
| 302 | return nullptr; | ||
| 303 | } | ||
| 304 | |||
| 305 | PartitionDataManager::PartitionDataManager(FileSys::VirtualDir sysdata_dir) | ||
| 306 | : boot0(FindFileInDirWithNames(sysdata_dir, "BOOT0")), | ||
| 307 | fuses(FindFileInDirWithNames(sysdata_dir, "fuse")), | ||
| 308 | kfuses(FindFileInDirWithNames(sysdata_dir, "kfuse")), | ||
| 309 | package2({ | ||
| 310 | FindFileInDirWithNames(sysdata_dir, "BCPKG2-1-Normal-Main"), | ||
| 311 | FindFileInDirWithNames(sysdata_dir, "BCPKG2-2-Normal-Sub"), | ||
| 312 | FindFileInDirWithNames(sysdata_dir, "BCPKG2-3-SafeMode-Main"), | ||
| 313 | FindFileInDirWithNames(sysdata_dir, "BCPKG2-4-SafeMode-Sub"), | ||
| 314 | FindFileInDirWithNames(sysdata_dir, "BCPKG2-5-Repair-Main"), | ||
| 315 | FindFileInDirWithNames(sysdata_dir, "BCPKG2-6-Repair-Sub"), | ||
| 316 | }), | ||
| 317 | secure_monitor(FindFileInDirWithNames(sysdata_dir, "secmon")), | ||
| 318 | package1_decrypted(FindFileInDirWithNames(sysdata_dir, "pkg1_decr")), | ||
| 319 | secure_monitor_bytes(secure_monitor == nullptr ? std::vector<u8>{} | ||
| 320 | : secure_monitor->ReadAllBytes()), | ||
| 321 | package1_decrypted_bytes(package1_decrypted == nullptr ? std::vector<u8>{} | ||
| 322 | : package1_decrypted->ReadAllBytes()), | ||
| 323 | prodinfo(FindFileInDirWithNames(sysdata_dir, "PRODINFO")) {} | ||
| 324 | |||
| 325 | PartitionDataManager::~PartitionDataManager() = default; | ||
| 326 | |||
| 327 | bool PartitionDataManager::HasBoot0() const { | ||
| 328 | return boot0 != nullptr; | ||
| 329 | } | ||
| 330 | |||
| 331 | FileSys::VirtualFile PartitionDataManager::GetBoot0Raw() const { | ||
| 332 | return boot0; | ||
| 333 | } | ||
| 334 | |||
| 335 | std::array<u8, 176> PartitionDataManager::GetEncryptedKeyblob(u8 index) const { | ||
| 336 | if (HasBoot0() && index < 32) | ||
| 337 | return GetEncryptedKeyblobs()[index]; | ||
| 338 | return {}; | ||
| 339 | } | ||
| 340 | |||
| 341 | std::array<std::array<u8, 176>, 32> PartitionDataManager::GetEncryptedKeyblobs() const { | ||
| 342 | if (!HasBoot0()) | ||
| 343 | return {}; | ||
| 344 | |||
| 345 | std::array<std::array<u8, 176>, 32> out{}; | ||
| 346 | for (size_t i = 0; i < 0x20; ++i) | ||
| 347 | boot0->Read(out[i].data(), out[i].size(), 0x180000 + i * 0x200); | ||
| 348 | return out; | ||
| 349 | } | ||
| 350 | |||
| 351 | std::vector<u8> PartitionDataManager::GetSecureMonitor() const { | ||
| 352 | return secure_monitor_bytes; | ||
| 353 | } | ||
| 354 | |||
| 355 | std::array<u8, 16> PartitionDataManager::GetPackage2KeySource() const { | ||
| 356 | return FindKeyFromHex(secure_monitor_bytes, source_hashes[2]); | ||
| 357 | } | ||
| 358 | |||
| 359 | std::array<u8, 16> PartitionDataManager::GetAESKekGenerationSource() const { | ||
| 360 | return FindKeyFromHex(secure_monitor_bytes, source_hashes[3]); | ||
| 361 | } | ||
| 362 | |||
| 363 | std::array<u8, 16> PartitionDataManager::GetTitlekekSource() const { | ||
| 364 | return FindKeyFromHex(secure_monitor_bytes, source_hashes[5]); | ||
| 365 | } | ||
| 366 | |||
| 367 | std::array<std::array<u8, 16>, 32> PartitionDataManager::GetTZMasterKeys( | ||
| 368 | std::array<u8, 0x10> master_key) const { | ||
| 369 | return FindEncryptedMasterKeyFromHex(secure_monitor_bytes, master_key); | ||
| 370 | } | ||
| 371 | |||
| 372 | std::array<u8, 16> PartitionDataManager::GetRSAKekSeed3() const { | ||
| 373 | return FindKeyFromHex(secure_monitor_bytes, source_hashes[14]); | ||
| 374 | } | ||
| 375 | |||
| 376 | std::array<u8, 16> PartitionDataManager::GetRSAKekMask0() const { | ||
| 377 | return FindKeyFromHex(secure_monitor_bytes, source_hashes[15]); | ||
| 378 | } | ||
| 379 | |||
| 380 | std::vector<u8> PartitionDataManager::GetPackage1Decrypted() const { | ||
| 381 | return package1_decrypted_bytes; | ||
| 382 | } | ||
| 383 | |||
| 384 | std::array<u8, 16> PartitionDataManager::GetMasterKeySource() const { | ||
| 385 | return FindKeyFromHex(package1_decrypted_bytes, source_hashes[1]); | ||
| 386 | } | ||
| 387 | |||
| 388 | std::array<u8, 16> PartitionDataManager::GetKeyblobMACKeySource() const { | ||
| 389 | return FindKeyFromHex(package1_decrypted_bytes, source_hashes[0]); | ||
| 390 | } | ||
| 391 | |||
| 392 | std::array<u8, 16> PartitionDataManager::GetKeyblobKeySource(u8 revision) const { | ||
| 393 | if (keyblob_source_hashes[revision] == SHA256Hash{}) { | ||
| 394 | LOG_WARNING(Crypto, | ||
| 395 | "No keyblob source hash for crypto revision {:02X}! Cannot derive keys...", | ||
| 396 | revision); | ||
| 397 | } | ||
| 398 | return FindKeyFromHex(package1_decrypted_bytes, keyblob_source_hashes[revision]); | ||
| 399 | } | ||
| 400 | |||
| 401 | bool PartitionDataManager::HasFuses() const { | ||
| 402 | return fuses != nullptr; | ||
| 403 | } | ||
| 404 | |||
| 405 | FileSys::VirtualFile PartitionDataManager::GetFusesRaw() const { | ||
| 406 | return fuses; | ||
| 407 | } | ||
| 408 | |||
| 409 | std::array<u8, 16> PartitionDataManager::GetSecureBootKey() const { | ||
| 410 | if (!HasFuses()) | ||
| 411 | return {}; | ||
| 412 | Key128 out{}; | ||
| 413 | fuses->Read(out.data(), out.size(), 0xA4); | ||
| 414 | return out; | ||
| 415 | } | ||
| 416 | |||
| 417 | bool PartitionDataManager::HasKFuses() const { | ||
| 418 | return kfuses != nullptr; | ||
| 419 | } | ||
| 420 | |||
| 421 | FileSys::VirtualFile PartitionDataManager::GetKFusesRaw() const { | ||
| 422 | return kfuses; | ||
| 423 | } | ||
| 424 | |||
| 425 | bool PartitionDataManager::HasPackage2(Package2Type type) const { | ||
| 426 | return package2.at(static_cast<size_t>(type)) != nullptr; | ||
| 427 | } | ||
| 428 | |||
| 429 | FileSys::VirtualFile PartitionDataManager::GetPackage2Raw(Package2Type type) const { | ||
| 430 | return package2.at(static_cast<size_t>(type)); | ||
| 431 | } | ||
| 432 | |||
| 433 | bool AttemptDecrypt(const std::array<u8, 16>& key, Package2Header& header) { | ||
| 434 | |||
| 435 | const std::vector<u8> iv(header.header_ctr.begin(), header.header_ctr.end()); | ||
| 436 | Package2Header temp = header; | ||
| 437 | AESCipher<Key128> cipher(key, Mode::CTR); | ||
| 438 | cipher.SetIV(iv); | ||
| 439 | cipher.Transcode(&temp.header_ctr, sizeof(Package2Header) - 0x100, &temp.header_ctr, | ||
| 440 | Op::Decrypt); | ||
| 441 | if (temp.magic == Common::MakeMagic('P', 'K', '2', '1')) { | ||
| 442 | header = temp; | ||
| 443 | return true; | ||
| 444 | } | ||
| 445 | |||
| 446 | return false; | ||
| 447 | } | ||
| 448 | |||
| 449 | void PartitionDataManager::DecryptPackage2(std::array<std::array<u8, 16>, 0x20> package2_keys, | ||
| 450 | Package2Type type) { | ||
| 451 | FileSys::VirtualFile file = std::make_shared<FileSys::OffsetVfsFile>( | ||
| 452 | package2[static_cast<size_t>(type)], | ||
| 453 | package2[static_cast<size_t>(type)]->GetSize() - 0x4000, 0x4000); | ||
| 454 | |||
| 455 | Package2Header header{}; | ||
| 456 | if (file->ReadObject(&header) != sizeof(Package2Header)) | ||
| 457 | return; | ||
| 458 | |||
| 459 | u8 revision = 0xFF; | ||
| 460 | if (header.magic != Common::MakeMagic('P', 'K', '2', '1')) { | ||
| 461 | for (size_t i = 0; i < package2_keys.size(); ++i) { | ||
| 462 | if (AttemptDecrypt(package2_keys[i], header)) | ||
| 463 | revision = i; | ||
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | if (header.magic != Common::MakeMagic('P', 'K', '2', '1')) | ||
| 468 | return; | ||
| 469 | |||
| 470 | const std::vector<u8> s1_iv(header.section_ctr[1].begin(), header.section_ctr[1].end()); | ||
| 471 | |||
| 472 | const auto a = std::make_shared<FileSys::OffsetVfsFile>( | ||
| 473 | file, header.section_size[1], header.section_size[0] + sizeof(Package2Header)); | ||
| 474 | |||
| 475 | auto c = a->ReadAllBytes(); | ||
| 476 | |||
| 477 | AESCipher<Key128> cipher(package2_keys[revision], Mode::CTR); | ||
| 478 | cipher.SetIV(s1_iv); | ||
| 479 | cipher.Transcode(c.data(), c.size(), c.data(), Op::Decrypt); | ||
| 480 | |||
| 481 | // package2_decrypted[static_cast<size_t>(type)] = s1; | ||
| 482 | |||
| 483 | INIHeader ini; | ||
| 484 | std::memcpy(&ini, c.data(), sizeof(INIHeader)); | ||
| 485 | if (ini.magic != Common::MakeMagic('I', 'N', 'I', '1')) | ||
| 486 | return; | ||
| 487 | |||
| 488 | std::map<u64, KIPHeader> kips{}; | ||
| 489 | u64 offset = sizeof(INIHeader); | ||
| 490 | for (size_t i = 0; i < ini.process_count; ++i) { | ||
| 491 | KIPHeader kip; | ||
| 492 | std::memcpy(&kip, c.data() + offset, sizeof(KIPHeader)); | ||
| 493 | if (kip.magic != Common::MakeMagic('K', 'I', 'P', '1')) | ||
| 494 | return; | ||
| 495 | kips.emplace(offset, kip); | ||
| 496 | |||
| 497 | const auto name = | ||
| 498 | Common::StringFromFixedZeroTerminatedBuffer(kip.name.data(), kip.name.size()); | ||
| 499 | |||
| 500 | if (name != "FS" && name != "spl") { | ||
| 501 | offset += sizeof(KIPHeader) + kip.sections[0].size_compressed + | ||
| 502 | kip.sections[1].size_compressed + kip.sections[2].size_compressed; | ||
| 503 | continue; | ||
| 504 | } | ||
| 505 | |||
| 506 | std::vector<u8> text(kip.sections[0].size_compressed); | ||
| 507 | std::vector<u8> rodata(kip.sections[1].size_compressed); | ||
| 508 | std::vector<u8> data(kip.sections[2].size_compressed); | ||
| 509 | |||
| 510 | u64 offset_sec = sizeof(KIPHeader) + offset; | ||
| 511 | std::memcpy(text.data(), c.data() + offset_sec, text.size()); | ||
| 512 | offset_sec += text.size(); | ||
| 513 | std::memcpy(rodata.data(), c.data() + offset_sec, rodata.size()); | ||
| 514 | offset_sec += rodata.size(); | ||
| 515 | std::memcpy(data.data(), c.data() + offset_sec, data.size()); | ||
| 516 | |||
| 517 | offset += sizeof(KIPHeader) + kip.sections[0].size_compressed + | ||
| 518 | kip.sections[1].size_compressed + kip.sections[2].size_compressed; | ||
| 519 | |||
| 520 | text = DecompressBLZ(text); | ||
| 521 | rodata = DecompressBLZ(rodata); | ||
| 522 | data = DecompressBLZ(data); | ||
| 523 | |||
| 524 | std::vector<u8> out(text.size() + rodata.size() + data.size()); | ||
| 525 | std::memcpy(out.data(), text.data(), text.size()); | ||
| 526 | std::memcpy(out.data() + text.size(), rodata.data(), rodata.size()); | ||
| 527 | std::memcpy(out.data() + text.size() + rodata.size(), data.data(), data.size()); | ||
| 528 | |||
| 529 | if (name == "FS") | ||
| 530 | package2_fs[static_cast<size_t>(type)] = out; | ||
| 531 | else if (name == "spl") | ||
| 532 | package2_spl[static_cast<size_t>(type)] = out; | ||
| 533 | } | ||
| 534 | } | ||
| 535 | |||
| 536 | const std::vector<u8>& PartitionDataManager::GetPackage2FSDecompressed(Package2Type type) const { | ||
| 537 | return package2_fs.at(static_cast<size_t>(type)); | ||
| 538 | } | ||
| 539 | |||
| 540 | std::array<u8, 16> PartitionDataManager::GetKeyAreaKeyApplicationSource(Package2Type type) const { | ||
| 541 | return FindKeyFromHex(package2_fs.at(static_cast<size_t>(type)), source_hashes[6]); | ||
| 542 | } | ||
| 543 | |||
| 544 | std::array<u8, 16> PartitionDataManager::GetKeyAreaKeyOceanSource(Package2Type type) const { | ||
| 545 | return FindKeyFromHex(package2_fs.at(static_cast<size_t>(type)), source_hashes[7]); | ||
| 546 | } | ||
| 547 | |||
| 548 | std::array<u8, 16> PartitionDataManager::GetKeyAreaKeySystemSource(Package2Type type) const { | ||
| 549 | return FindKeyFromHex(package2_fs.at(static_cast<size_t>(type)), source_hashes[8]); | ||
| 550 | } | ||
| 551 | |||
| 552 | std::array<u8, 16> PartitionDataManager::GetSDKekSource(Package2Type type) const { | ||
| 553 | return FindKeyFromHex(package2_fs.at(static_cast<size_t>(type)), source_hashes[9]); | ||
| 554 | } | ||
| 555 | |||
| 556 | std::array<u8, 32> PartitionDataManager::GetSDSaveKeySource(Package2Type type) const { | ||
| 557 | return FindKeyFromHex<0x20>(package2_fs.at(static_cast<size_t>(type)), source_hashes[10]); | ||
| 558 | } | ||
| 559 | |||
| 560 | std::array<u8, 32> PartitionDataManager::GetSDNCAKeySource(Package2Type type) const { | ||
| 561 | return FindKeyFromHex<0x20>(package2_fs.at(static_cast<size_t>(type)), source_hashes[11]); | ||
| 562 | } | ||
| 563 | |||
| 564 | std::array<u8, 16> PartitionDataManager::GetHeaderKekSource(Package2Type type) const { | ||
| 565 | return FindKeyFromHex(package2_fs.at(static_cast<size_t>(type)), source_hashes[12]); | ||
| 566 | } | ||
| 567 | |||
| 568 | std::array<u8, 32> PartitionDataManager::GetHeaderKeySource(Package2Type type) const { | ||
| 569 | return FindKeyFromHex<0x20>(package2_fs.at(static_cast<size_t>(type)), source_hashes[13]); | ||
| 570 | } | ||
| 571 | |||
| 572 | const std::vector<u8>& PartitionDataManager::GetPackage2SPLDecompressed(Package2Type type) const { | ||
| 573 | return package2_spl.at(static_cast<size_t>(type)); | ||
| 574 | } | ||
| 575 | |||
| 576 | std::array<u8, 16> PartitionDataManager::GetAESKeyGenerationSource(Package2Type type) const { | ||
| 577 | return FindKeyFromHex(package2_spl.at(static_cast<size_t>(type)), source_hashes[4]); | ||
| 578 | } | ||
| 579 | |||
| 580 | bool PartitionDataManager::HasProdInfo() const { | ||
| 581 | return prodinfo != nullptr; | ||
| 582 | } | ||
| 583 | |||
| 584 | FileSys::VirtualFile PartitionDataManager::GetProdInfoRaw() const { | ||
| 585 | return prodinfo; | ||
| 586 | } | ||
| 587 | |||
| 588 | void PartitionDataManager::DecryptProdInfo(std::array<u8, 0x20> bis_key) { | ||
| 589 | if (prodinfo == nullptr) | ||
| 590 | return; | ||
| 591 | |||
| 592 | prodinfo_decrypted = std::make_shared<XTSEncryptionLayer>(prodinfo, bis_key); | ||
| 593 | } | ||
| 594 | |||
| 595 | std::array<u8, 576> PartitionDataManager::GetETicketExtendedKek() const { | ||
| 596 | std::array<u8, 0x240> out{}; | ||
| 597 | if (prodinfo_decrypted != nullptr) | ||
| 598 | prodinfo_decrypted->Read(out.data(), out.size(), 0x3890); | ||
| 599 | return out; | ||
| 600 | } | ||
| 601 | } // namespace Core::Crypto | ||
diff --git a/src/core/crypto/partition_data_manager.h b/src/core/crypto/partition_data_manager.h new file mode 100644 index 000000000..45c7fecfa --- /dev/null +++ b/src/core/crypto/partition_data_manager.h | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <vector> | ||
| 8 | #include "common/common_funcs.h" | ||
| 9 | #include "common/common_types.h" | ||
| 10 | #include "common/swap.h" | ||
| 11 | #include "core/file_sys/vfs_types.h" | ||
| 12 | |||
| 13 | namespace Core::Crypto { | ||
| 14 | |||
| 15 | enum class Package2Type { | ||
| 16 | NormalMain, | ||
| 17 | NormalSub, | ||
| 18 | SafeModeMain, | ||
| 19 | SafeModeSub, | ||
| 20 | RepairMain, | ||
| 21 | RepairSub, | ||
| 22 | }; | ||
| 23 | |||
| 24 | class PartitionDataManager { | ||
| 25 | public: | ||
| 26 | static const u8 MAX_KEYBLOB_SOURCE_HASH; | ||
| 27 | |||
| 28 | explicit PartitionDataManager(FileSys::VirtualDir sysdata_dir); | ||
| 29 | ~PartitionDataManager(); | ||
| 30 | |||
| 31 | // BOOT0 | ||
| 32 | bool HasBoot0() const; | ||
| 33 | FileSys::VirtualFile GetBoot0Raw() const; | ||
| 34 | std::array<u8, 0xB0> GetEncryptedKeyblob(u8 index) const; | ||
| 35 | std::array<std::array<u8, 0xB0>, 0x20> GetEncryptedKeyblobs() const; | ||
| 36 | std::vector<u8> GetSecureMonitor() const; | ||
| 37 | std::array<u8, 0x10> GetPackage2KeySource() const; | ||
| 38 | std::array<u8, 0x10> GetAESKekGenerationSource() const; | ||
| 39 | std::array<u8, 0x10> GetTitlekekSource() const; | ||
| 40 | std::array<std::array<u8, 0x10>, 0x20> GetTZMasterKeys(std::array<u8, 0x10> master_key) const; | ||
| 41 | std::array<u8, 0x10> GetRSAKekSeed3() const; | ||
| 42 | std::array<u8, 0x10> GetRSAKekMask0() const; | ||
| 43 | std::vector<u8> GetPackage1Decrypted() const; | ||
| 44 | std::array<u8, 0x10> GetMasterKeySource() const; | ||
| 45 | std::array<u8, 0x10> GetKeyblobMACKeySource() const; | ||
| 46 | std::array<u8, 0x10> GetKeyblobKeySource(u8 revision) const; | ||
| 47 | |||
| 48 | // Fuses | ||
| 49 | bool HasFuses() const; | ||
| 50 | FileSys::VirtualFile GetFusesRaw() const; | ||
| 51 | std::array<u8, 0x10> GetSecureBootKey() const; | ||
| 52 | |||
| 53 | // K-Fuses | ||
| 54 | bool HasKFuses() const; | ||
| 55 | FileSys::VirtualFile GetKFusesRaw() const; | ||
| 56 | |||
| 57 | // Package2 | ||
| 58 | bool HasPackage2(Package2Type type = Package2Type::NormalMain) const; | ||
| 59 | FileSys::VirtualFile GetPackage2Raw(Package2Type type = Package2Type::NormalMain) const; | ||
| 60 | void DecryptPackage2(std::array<std::array<u8, 16>, 0x20> package2, Package2Type type); | ||
| 61 | const std::vector<u8>& GetPackage2FSDecompressed( | ||
| 62 | Package2Type type = Package2Type::NormalMain) const; | ||
| 63 | std::array<u8, 0x10> GetKeyAreaKeyApplicationSource( | ||
| 64 | Package2Type type = Package2Type::NormalMain) const; | ||
| 65 | std::array<u8, 0x10> GetKeyAreaKeyOceanSource( | ||
| 66 | Package2Type type = Package2Type::NormalMain) const; | ||
| 67 | std::array<u8, 0x10> GetKeyAreaKeySystemSource( | ||
| 68 | Package2Type type = Package2Type::NormalMain) const; | ||
| 69 | std::array<u8, 0x10> GetSDKekSource(Package2Type type = Package2Type::NormalMain) const; | ||
| 70 | std::array<u8, 0x20> GetSDSaveKeySource(Package2Type type = Package2Type::NormalMain) const; | ||
| 71 | std::array<u8, 0x20> GetSDNCAKeySource(Package2Type type = Package2Type::NormalMain) const; | ||
| 72 | std::array<u8, 0x10> GetHeaderKekSource(Package2Type type = Package2Type::NormalMain) const; | ||
| 73 | std::array<u8, 0x20> GetHeaderKeySource(Package2Type type = Package2Type::NormalMain) const; | ||
| 74 | const std::vector<u8>& GetPackage2SPLDecompressed( | ||
| 75 | Package2Type type = Package2Type::NormalMain) const; | ||
| 76 | std::array<u8, 0x10> GetAESKeyGenerationSource( | ||
| 77 | Package2Type type = Package2Type::NormalMain) const; | ||
| 78 | |||
| 79 | // PRODINFO | ||
| 80 | bool HasProdInfo() const; | ||
| 81 | FileSys::VirtualFile GetProdInfoRaw() const; | ||
| 82 | void DecryptProdInfo(std::array<u8, 0x20> bis_key); | ||
| 83 | std::array<u8, 0x240> GetETicketExtendedKek() const; | ||
| 84 | |||
| 85 | private: | ||
| 86 | FileSys::VirtualFile boot0; | ||
| 87 | FileSys::VirtualFile fuses; | ||
| 88 | FileSys::VirtualFile kfuses; | ||
| 89 | std::array<FileSys::VirtualFile, 6> package2; | ||
| 90 | FileSys::VirtualFile prodinfo; | ||
| 91 | FileSys::VirtualFile secure_monitor; | ||
| 92 | FileSys::VirtualFile package1_decrypted; | ||
| 93 | |||
| 94 | // Processed | ||
| 95 | std::array<FileSys::VirtualFile, 6> package2_decrypted; | ||
| 96 | FileSys::VirtualFile prodinfo_decrypted; | ||
| 97 | std::vector<u8> secure_monitor_bytes; | ||
| 98 | std::vector<u8> package1_decrypted_bytes; | ||
| 99 | std::array<std::vector<u8>, 6> package2_fs; | ||
| 100 | std::array<std::vector<u8>, 6> package2_spl; | ||
| 101 | }; | ||
| 102 | |||
| 103 | std::array<u8, 0x10> FindKeyFromHex16(const std::vector<u8>& binary, std::array<u8, 0x20> hash); | ||
| 104 | |||
| 105 | } // namespace Core::Crypto | ||
diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h index 270291631..7f0d520ca 100644 --- a/src/core/file_sys/vfs.h +++ b/src/core/file_sys/vfs.h | |||
| @@ -12,20 +12,12 @@ | |||
| 12 | #include <vector> | 12 | #include <vector> |
| 13 | #include <boost/optional.hpp> | 13 | #include <boost/optional.hpp> |
| 14 | #include "common/common_types.h" | 14 | #include "common/common_types.h" |
| 15 | #include "core/file_sys/vfs_types.h" | ||
| 15 | 16 | ||
| 16 | namespace FileSys { | 17 | namespace FileSys { |
| 17 | 18 | ||
| 18 | class VfsDirectory; | ||
| 19 | class VfsFile; | ||
| 20 | class VfsFilesystem; | ||
| 21 | |||
| 22 | enum class Mode : u32; | 19 | enum class Mode : u32; |
| 23 | 20 | ||
| 24 | // Convenience typedefs to use Vfs* interfaces | ||
| 25 | using VirtualFilesystem = std::shared_ptr<VfsFilesystem>; | ||
| 26 | using VirtualDir = std::shared_ptr<VfsDirectory>; | ||
| 27 | using VirtualFile = std::shared_ptr<VfsFile>; | ||
| 28 | |||
| 29 | // An enumeration representing what can be at the end of a path in a VfsFilesystem | 21 | // An enumeration representing what can be at the end of a path in a VfsFilesystem |
| 30 | enum class VfsEntryType { | 22 | enum class VfsEntryType { |
| 31 | None, | 23 | None, |
diff --git a/src/core/file_sys/vfs_types.h b/src/core/file_sys/vfs_types.h new file mode 100644 index 000000000..6215ed7af --- /dev/null +++ b/src/core/file_sys/vfs_types.h | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <memory> | ||
| 8 | |||
| 9 | namespace FileSys { | ||
| 10 | |||
| 11 | class VfsDirectory; | ||
| 12 | class VfsFile; | ||
| 13 | class VfsFilesystem; | ||
| 14 | |||
| 15 | // Declarations for Vfs* pointer types | ||
| 16 | |||
| 17 | using VirtualDir = std::shared_ptr<VfsDirectory>; | ||
| 18 | using VirtualFile = std::shared_ptr<VfsFile>; | ||
| 19 | using VirtualFilesystem = std::shared_ptr<VfsFilesystem>; | ||
| 20 | |||
| 21 | } // namespace FileSys | ||
diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index e11833c5a..fc186dc2d 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp | |||
| @@ -31,6 +31,7 @@ static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::Virtual | |||
| 31 | #include <QDialogButtonBox> | 31 | #include <QDialogButtonBox> |
| 32 | #include <QFileDialog> | 32 | #include <QFileDialog> |
| 33 | #include <QMessageBox> | 33 | #include <QMessageBox> |
| 34 | #include <QtConcurrent/QtConcurrent> | ||
| 34 | #include <QtGui> | 35 | #include <QtGui> |
| 35 | #include <QtWidgets> | 36 | #include <QtWidgets> |
| 36 | #include <fmt/format.h> | 37 | #include <fmt/format.h> |
| @@ -171,6 +172,9 @@ GMainWindow::GMainWindow() | |||
| 171 | .arg(Common::g_build_fullname, Common::g_scm_branch, Common::g_scm_desc)); | 172 | .arg(Common::g_build_fullname, Common::g_scm_branch, Common::g_scm_desc)); |
| 172 | show(); | 173 | show(); |
| 173 | 174 | ||
| 175 | // Gen keys if necessary | ||
| 176 | OnReinitializeKeys(ReinitializeKeyBehavior::NoWarning); | ||
| 177 | |||
| 174 | // Necessary to load titles from nand in gamelist. | 178 | // Necessary to load titles from nand in gamelist. |
| 175 | Service::FileSystem::CreateFactories(vfs); | 179 | Service::FileSystem::CreateFactories(vfs); |
| 176 | game_list->LoadCompatibilityList(); | 180 | game_list->LoadCompatibilityList(); |
| @@ -443,6 +447,8 @@ void GMainWindow::ConnectMenuEvents() { | |||
| 443 | connect(ui.action_Fullscreen, &QAction::triggered, this, &GMainWindow::ToggleFullscreen); | 447 | connect(ui.action_Fullscreen, &QAction::triggered, this, &GMainWindow::ToggleFullscreen); |
| 444 | 448 | ||
| 445 | // Help | 449 | // Help |
| 450 | connect(ui.action_Rederive, &QAction::triggered, this, | ||
| 451 | std::bind(&GMainWindow::OnReinitializeKeys, this, ReinitializeKeyBehavior::Warning)); | ||
| 446 | connect(ui.action_About, &QAction::triggered, this, &GMainWindow::OnAbout); | 452 | connect(ui.action_About, &QAction::triggered, this, &GMainWindow::OnAbout); |
| 447 | } | 453 | } |
| 448 | 454 | ||
| @@ -1375,6 +1381,82 @@ void GMainWindow::OnCoreError(Core::System::ResultStatus result, std::string det | |||
| 1375 | } | 1381 | } |
| 1376 | } | 1382 | } |
| 1377 | 1383 | ||
| 1384 | void GMainWindow::OnReinitializeKeys(ReinitializeKeyBehavior behavior) { | ||
| 1385 | if (behavior == ReinitializeKeyBehavior::Warning) { | ||
| 1386 | const auto res = QMessageBox::information( | ||
| 1387 | this, tr("Confirm Key Rederivation"), | ||
| 1388 | tr("You are about to force rederive all of your keys. \nIf you do not know what this " | ||
| 1389 | "means or what you are doing, \nthis is a potentially destructive action. \nPlease " | ||
| 1390 | "make " | ||
| 1391 | "sure this is what you want \nand optionally make backups.\n\nThis will delete your " | ||
| 1392 | "autogenerated key files and re-run the key derivation module."), | ||
| 1393 | QMessageBox::StandardButtons{QMessageBox::Ok, QMessageBox::Cancel}); | ||
| 1394 | |||
| 1395 | if (res == QMessageBox::Cancel) | ||
| 1396 | return; | ||
| 1397 | |||
| 1398 | FileUtil::Delete(FileUtil::GetUserPath(FileUtil::UserPath::KeysDir) + | ||
| 1399 | "prod.keys_autogenerated"); | ||
| 1400 | FileUtil::Delete(FileUtil::GetUserPath(FileUtil::UserPath::KeysDir) + | ||
| 1401 | "console.keys_autogenerated"); | ||
| 1402 | FileUtil::Delete(FileUtil::GetUserPath(FileUtil::UserPath::KeysDir) + | ||
| 1403 | "title.keys_autogenerated"); | ||
| 1404 | } | ||
| 1405 | |||
| 1406 | Core::Crypto::KeyManager keys{}; | ||
| 1407 | if (keys.BaseDeriveNecessary()) { | ||
| 1408 | Core::Crypto::PartitionDataManager pdm{vfs->OpenDirectory( | ||
| 1409 | FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir), FileSys::Mode::Read)}; | ||
| 1410 | |||
| 1411 | const auto function = [this, &keys, &pdm] { | ||
| 1412 | keys.PopulateFromPartitionData(pdm); | ||
| 1413 | Service::FileSystem::CreateFactories(vfs); | ||
| 1414 | keys.DeriveETicket(pdm); | ||
| 1415 | }; | ||
| 1416 | |||
| 1417 | QString errors; | ||
| 1418 | |||
| 1419 | if (!pdm.HasFuses()) | ||
| 1420 | errors += tr("- Missing fuses - Cannot derive SBK\n"); | ||
| 1421 | if (!pdm.HasBoot0()) | ||
| 1422 | errors += tr("- Missing BOOT0 - Cannot derive master keys\n"); | ||
| 1423 | if (!pdm.HasPackage2()) | ||
| 1424 | errors += tr("- Missing BCPKG2-1-Normal-Main - Cannot derive general keys\n"); | ||
| 1425 | if (!pdm.HasProdInfo()) | ||
| 1426 | errors += tr("- Missing PRODINFO - Cannot derive title keys\n"); | ||
| 1427 | |||
| 1428 | if (!errors.isEmpty()) { | ||
| 1429 | |||
| 1430 | QMessageBox::warning( | ||
| 1431 | this, tr("Warning Missing Derivation Components"), | ||
| 1432 | tr("The following are missing from your configuration that may hinder key " | ||
| 1433 | "derivation. It will be attempted but may not complete.\n\n") + | ||
| 1434 | errors); | ||
| 1435 | } | ||
| 1436 | |||
| 1437 | QProgressDialog prog; | ||
| 1438 | prog.setRange(0, 0); | ||
| 1439 | prog.setLabelText(tr("Deriving keys...\nThis may take up to a minute depending \non your " | ||
| 1440 | "system's performance.")); | ||
| 1441 | prog.setWindowTitle(tr("Deriving Keys")); | ||
| 1442 | |||
| 1443 | prog.show(); | ||
| 1444 | |||
| 1445 | auto future = QtConcurrent::run(function); | ||
| 1446 | while (!future.isFinished()) { | ||
| 1447 | QCoreApplication::processEvents(); | ||
| 1448 | } | ||
| 1449 | |||
| 1450 | prog.close(); | ||
| 1451 | } | ||
| 1452 | |||
| 1453 | Service::FileSystem::CreateFactories(vfs); | ||
| 1454 | |||
| 1455 | if (behavior == ReinitializeKeyBehavior::Warning) { | ||
| 1456 | game_list->PopulateAsync(UISettings::values.gamedir, UISettings::values.gamedir_deepscan); | ||
| 1457 | } | ||
| 1458 | } | ||
| 1459 | |||
| 1378 | bool GMainWindow::ConfirmClose() { | 1460 | bool GMainWindow::ConfirmClose() { |
| 1379 | if (emu_thread == nullptr || !UISettings::values.confirm_before_closing) | 1461 | if (emu_thread == nullptr || !UISettings::values.confirm_before_closing) |
| 1380 | return true; | 1462 | return true; |
diff --git a/src/yuzu/main.h b/src/yuzu/main.h index fe0e9a50a..3663d6aed 100644 --- a/src/yuzu/main.h +++ b/src/yuzu/main.h | |||
| @@ -41,6 +41,11 @@ enum class EmulatedDirectoryTarget { | |||
| 41 | SDMC, | 41 | SDMC, |
| 42 | }; | 42 | }; |
| 43 | 43 | ||
| 44 | enum class ReinitializeKeyBehavior { | ||
| 45 | NoWarning, | ||
| 46 | Warning, | ||
| 47 | }; | ||
| 48 | |||
| 44 | namespace DiscordRPC { | 49 | namespace DiscordRPC { |
| 45 | class DiscordInterface; | 50 | class DiscordInterface; |
| 46 | } | 51 | } |
| @@ -167,6 +172,7 @@ private slots: | |||
| 167 | void HideFullscreen(); | 172 | void HideFullscreen(); |
| 168 | void ToggleWindowMode(); | 173 | void ToggleWindowMode(); |
| 169 | void OnCoreError(Core::System::ResultStatus, std::string); | 174 | void OnCoreError(Core::System::ResultStatus, std::string); |
| 175 | void OnReinitializeKeys(ReinitializeKeyBehavior behavior); | ||
| 170 | 176 | ||
| 171 | private: | 177 | private: |
| 172 | void UpdateStatusBar(); | 178 | void UpdateStatusBar(); |
diff --git a/src/yuzu/main.ui b/src/yuzu/main.ui index cb1664b21..9851f507d 100644 --- a/src/yuzu/main.ui +++ b/src/yuzu/main.ui | |||
| @@ -103,6 +103,7 @@ | |||
| 103 | </property> | 103 | </property> |
| 104 | <addaction name="action_Report_Compatibility"/> | 104 | <addaction name="action_Report_Compatibility"/> |
| 105 | <addaction name="separator"/> | 105 | <addaction name="separator"/> |
| 106 | <addaction name="action_Rederive"/> | ||
| 106 | <addaction name="action_About"/> | 107 | <addaction name="action_About"/> |
| 107 | </widget> | 108 | </widget> |
| 108 | <addaction name="menu_File"/> | 109 | <addaction name="menu_File"/> |
| @@ -159,6 +160,11 @@ | |||
| 159 | <string>&Stop</string> | 160 | <string>&Stop</string> |
| 160 | </property> | 161 | </property> |
| 161 | </action> | 162 | </action> |
| 163 | <action name="action_Rederive"> | ||
| 164 | <property name="text"> | ||
| 165 | <string>Reinitialize keys...</string> | ||
| 166 | </property> | ||
| 167 | </action> | ||
| 162 | <action name="action_About"> | 168 | <action name="action_About"> |
| 163 | <property name="text"> | 169 | <property name="text"> |
| 164 | <string>About yuzu</string> | 170 | <string>About yuzu</string> |