diff options
| author | 2018-08-24 23:47:46 -0400 | |
|---|---|---|
| committer | 2018-08-24 23:47:46 -0400 | |
| commit | 6426b0f5514d6a7c5cc369368947eceb380bfc85 (patch) | |
| tree | b7acdc39a4344570a6f2c098c30ad20114bf84db /src/core/file_sys | |
| parent | Merge pull request #1065 from DarkLordZach/window-title (diff) | |
| parent | file_sys/crypto: Fix missing/unnecessary includes (diff) | |
| download | yuzu-6426b0f5514d6a7c5cc369368947eceb380bfc85.tar.gz yuzu-6426b0f5514d6a7c5cc369368947eceb380bfc85.tar.xz yuzu-6426b0f5514d6a7c5cc369368947eceb380bfc85.zip | |
Merge pull request #1094 from DarkLordZach/nax0
file_sys: Add support for NAX archives
Diffstat (limited to 'src/core/file_sys')
| -rw-r--r-- | src/core/file_sys/bis_factory.cpp | 11 | ||||
| -rw-r--r-- | src/core/file_sys/card_image.cpp | 12 | ||||
| -rw-r--r-- | src/core/file_sys/card_image.h | 2 | ||||
| -rw-r--r-- | src/core/file_sys/content_archive.cpp | 10 | ||||
| -rw-r--r-- | src/core/file_sys/content_archive.h | 3 | ||||
| -rw-r--r-- | src/core/file_sys/registered_cache.cpp | 14 | ||||
| -rw-r--r-- | src/core/file_sys/registered_cache.h | 4 | ||||
| -rw-r--r-- | src/core/file_sys/sdmc_factory.cpp | 15 | ||||
| -rw-r--r-- | src/core/file_sys/sdmc_factory.h | 7 | ||||
| -rw-r--r-- | src/core/file_sys/vfs.cpp | 7 | ||||
| -rw-r--r-- | src/core/file_sys/vfs.h | 4 | ||||
| -rw-r--r-- | src/core/file_sys/xts_archive.cpp | 169 | ||||
| -rw-r--r-- | src/core/file_sys/xts_archive.h | 69 |
13 files changed, 316 insertions, 11 deletions
diff --git a/src/core/file_sys/bis_factory.cpp b/src/core/file_sys/bis_factory.cpp index ae4e33800..08a7cea5a 100644 --- a/src/core/file_sys/bis_factory.cpp +++ b/src/core/file_sys/bis_factory.cpp | |||
| @@ -6,19 +6,12 @@ | |||
| 6 | 6 | ||
| 7 | namespace FileSys { | 7 | namespace FileSys { |
| 8 | 8 | ||
| 9 | static VirtualDir GetOrCreateDirectory(const VirtualDir& dir, std::string_view path) { | ||
| 10 | const auto res = dir->GetDirectoryRelative(path); | ||
| 11 | if (res == nullptr) | ||
| 12 | return dir->CreateDirectoryRelative(path); | ||
| 13 | return res; | ||
| 14 | } | ||
| 15 | |||
| 16 | BISFactory::BISFactory(VirtualDir nand_root_) | 9 | BISFactory::BISFactory(VirtualDir nand_root_) |
| 17 | : nand_root(std::move(nand_root_)), | 10 | : nand_root(std::move(nand_root_)), |
| 18 | sysnand_cache(std::make_shared<RegisteredCache>( | 11 | sysnand_cache(std::make_shared<RegisteredCache>( |
| 19 | GetOrCreateDirectory(nand_root, "/system/Contents/registered"))), | 12 | GetOrCreateDirectoryRelative(nand_root, "/system/Contents/registered"))), |
| 20 | usrnand_cache(std::make_shared<RegisteredCache>( | 13 | usrnand_cache(std::make_shared<RegisteredCache>( |
| 21 | GetOrCreateDirectory(nand_root, "/user/Contents/registered"))) {} | 14 | GetOrCreateDirectoryRelative(nand_root, "/user/Contents/registered"))) {} |
| 22 | 15 | ||
| 23 | std::shared_ptr<RegisteredCache> BISFactory::GetSystemNANDContents() const { | 16 | std::shared_ptr<RegisteredCache> BISFactory::GetSystemNANDContents() const { |
| 24 | return sysnand_cache; | 17 | return sysnand_cache; |
diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp index 1d7c7fb10..d61a2ebe1 100644 --- a/src/core/file_sys/card_image.cpp +++ b/src/core/file_sys/card_image.cpp | |||
| @@ -43,6 +43,8 @@ XCI::XCI(VirtualFile file_) : file(std::move(file_)), partitions(0x4) { | |||
| 43 | partitions[static_cast<size_t>(partition)] = std::make_shared<PartitionFilesystem>(raw); | 43 | partitions[static_cast<size_t>(partition)] = std::make_shared<PartitionFilesystem>(raw); |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | program_nca_status = Loader::ResultStatus::ErrorXCIMissingProgramNCA; | ||
| 47 | |||
| 46 | auto result = AddNCAFromPartition(XCIPartition::Secure); | 48 | auto result = AddNCAFromPartition(XCIPartition::Secure); |
| 47 | if (result != Loader::ResultStatus::Success) { | 49 | if (result != Loader::ResultStatus::Success) { |
| 48 | status = result; | 50 | status = result; |
| @@ -76,6 +78,10 @@ Loader::ResultStatus XCI::GetStatus() const { | |||
| 76 | return status; | 78 | return status; |
| 77 | } | 79 | } |
| 78 | 80 | ||
| 81 | Loader::ResultStatus XCI::GetProgramNCAStatus() const { | ||
| 82 | return program_nca_status; | ||
| 83 | } | ||
| 84 | |||
| 79 | VirtualDir XCI::GetPartition(XCIPartition partition) const { | 85 | VirtualDir XCI::GetPartition(XCIPartition partition) const { |
| 80 | return partitions[static_cast<size_t>(partition)]; | 86 | return partitions[static_cast<size_t>(partition)]; |
| 81 | } | 87 | } |
| @@ -143,6 +149,12 @@ Loader::ResultStatus XCI::AddNCAFromPartition(XCIPartition part) { | |||
| 143 | if (file->GetExtension() != "nca") | 149 | if (file->GetExtension() != "nca") |
| 144 | continue; | 150 | continue; |
| 145 | auto nca = std::make_shared<NCA>(file); | 151 | auto nca = std::make_shared<NCA>(file); |
| 152 | // TODO(DarkLordZach): Add proper Rev1+ Support | ||
| 153 | if (nca->IsUpdate()) | ||
| 154 | continue; | ||
| 155 | if (nca->GetType() == NCAContentType::Program) { | ||
| 156 | program_nca_status = nca->GetStatus(); | ||
| 157 | } | ||
| 146 | if (nca->GetStatus() == Loader::ResultStatus::Success) { | 158 | if (nca->GetStatus() == Loader::ResultStatus::Success) { |
| 147 | ncas.push_back(std::move(nca)); | 159 | ncas.push_back(std::move(nca)); |
| 148 | } else { | 160 | } else { |
diff --git a/src/core/file_sys/card_image.h b/src/core/file_sys/card_image.h index a03d5264e..54ab828d1 100644 --- a/src/core/file_sys/card_image.h +++ b/src/core/file_sys/card_image.h | |||
| @@ -59,6 +59,7 @@ public: | |||
| 59 | explicit XCI(VirtualFile file); | 59 | explicit XCI(VirtualFile file); |
| 60 | 60 | ||
| 61 | Loader::ResultStatus GetStatus() const; | 61 | Loader::ResultStatus GetStatus() const; |
| 62 | Loader::ResultStatus GetProgramNCAStatus() const; | ||
| 62 | 63 | ||
| 63 | u8 GetFormatVersion() const; | 64 | u8 GetFormatVersion() const; |
| 64 | 65 | ||
| @@ -90,6 +91,7 @@ private: | |||
| 90 | GamecardHeader header{}; | 91 | GamecardHeader header{}; |
| 91 | 92 | ||
| 92 | Loader::ResultStatus status; | 93 | Loader::ResultStatus status; |
| 94 | Loader::ResultStatus program_nca_status; | ||
| 93 | 95 | ||
| 94 | std::vector<VirtualDir> partitions; | 96 | std::vector<VirtualDir> partitions; |
| 95 | std::vector<std::shared_ptr<NCA>> ncas; | 97 | std::vector<std::shared_ptr<NCA>> ncas; |
diff --git a/src/core/file_sys/content_archive.cpp b/src/core/file_sys/content_archive.cpp index 47afcad9b..e8b5d6ece 100644 --- a/src/core/file_sys/content_archive.cpp +++ b/src/core/file_sys/content_archive.cpp | |||
| @@ -178,7 +178,7 @@ VirtualFile NCA::Decrypt(NCASectionHeader s_header, VirtualFile in, u64 starting | |||
| 178 | return std::static_pointer_cast<VfsFile>(out); | 178 | return std::static_pointer_cast<VfsFile>(out); |
| 179 | } | 179 | } |
| 180 | case NCASectionCryptoType::XTS: | 180 | case NCASectionCryptoType::XTS: |
| 181 | // TODO(DarkLordZach): Implement XTSEncryptionLayer. | 181 | // TODO(DarkLordZach): Find a test case for XTS-encrypted NCAs |
| 182 | default: | 182 | default: |
| 183 | LOG_ERROR(Crypto, "called with unhandled crypto type={:02X}", | 183 | LOG_ERROR(Crypto, "called with unhandled crypto type={:02X}", |
| 184 | static_cast<u8>(s_header.raw.header.crypto_type)); | 184 | static_cast<u8>(s_header.raw.header.crypto_type)); |
| @@ -258,6 +258,10 @@ NCA::NCA(VirtualFile file_) : file(std::move(file_)) { | |||
| 258 | file->ReadBytes(sections.data(), length_sections, SECTION_HEADER_OFFSET); | 258 | file->ReadBytes(sections.data(), length_sections, SECTION_HEADER_OFFSET); |
| 259 | } | 259 | } |
| 260 | 260 | ||
| 261 | is_update = std::find_if(sections.begin(), sections.end(), [](const NCASectionHeader& header) { | ||
| 262 | return header.raw.header.crypto_type == NCASectionCryptoType::BKTR; | ||
| 263 | }) != sections.end(); | ||
| 264 | |||
| 261 | for (std::ptrdiff_t i = 0; i < number_sections; ++i) { | 265 | for (std::ptrdiff_t i = 0; i < number_sections; ++i) { |
| 262 | auto section = sections[i]; | 266 | auto section = sections[i]; |
| 263 | 267 | ||
| @@ -358,6 +362,10 @@ VirtualFile NCA::GetBaseFile() const { | |||
| 358 | return file; | 362 | return file; |
| 359 | } | 363 | } |
| 360 | 364 | ||
| 365 | bool NCA::IsUpdate() const { | ||
| 366 | return is_update; | ||
| 367 | } | ||
| 368 | |||
| 361 | bool NCA::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { | 369 | bool NCA::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { |
| 362 | return false; | 370 | return false; |
| 363 | } | 371 | } |
diff --git a/src/core/file_sys/content_archive.h b/src/core/file_sys/content_archive.h index 4b74c54ec..b961cfde7 100644 --- a/src/core/file_sys/content_archive.h +++ b/src/core/file_sys/content_archive.h | |||
| @@ -93,6 +93,8 @@ public: | |||
| 93 | 93 | ||
| 94 | VirtualFile GetBaseFile() const; | 94 | VirtualFile GetBaseFile() const; |
| 95 | 95 | ||
| 96 | bool IsUpdate() const; | ||
| 97 | |||
| 96 | protected: | 98 | protected: |
| 97 | bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; | 99 | bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; |
| 98 | 100 | ||
| @@ -111,6 +113,7 @@ private: | |||
| 111 | 113 | ||
| 112 | NCAHeader header{}; | 114 | NCAHeader header{}; |
| 113 | bool has_rights_id{}; | 115 | bool has_rights_id{}; |
| 116 | bool is_update{}; | ||
| 114 | 117 | ||
| 115 | Loader::ResultStatus status{}; | 118 | Loader::ResultStatus status{}; |
| 116 | 119 | ||
diff --git a/src/core/file_sys/registered_cache.cpp b/src/core/file_sys/registered_cache.cpp index e90dc6695..a02efc71e 100644 --- a/src/core/file_sys/registered_cache.cpp +++ b/src/core/file_sys/registered_cache.cpp | |||
| @@ -254,6 +254,8 @@ RegisteredCache::RegisteredCache(VirtualDir dir_, RegisteredCacheParsingFunction | |||
| 254 | Refresh(); | 254 | Refresh(); |
| 255 | } | 255 | } |
| 256 | 256 | ||
| 257 | RegisteredCache::~RegisteredCache() = default; | ||
| 258 | |||
| 257 | bool RegisteredCache::HasEntry(u64 title_id, ContentRecordType type) const { | 259 | bool RegisteredCache::HasEntry(u64 title_id, ContentRecordType type) const { |
| 258 | return GetEntryRaw(title_id, type) != nullptr; | 260 | return GetEntryRaw(title_id, type) != nullptr; |
| 259 | } | 261 | } |
| @@ -262,6 +264,18 @@ bool RegisteredCache::HasEntry(RegisteredCacheEntry entry) const { | |||
| 262 | return GetEntryRaw(entry) != nullptr; | 264 | return GetEntryRaw(entry) != nullptr; |
| 263 | } | 265 | } |
| 264 | 266 | ||
| 267 | VirtualFile RegisteredCache::GetEntryUnparsed(u64 title_id, ContentRecordType type) const { | ||
| 268 | const auto id = GetNcaIDFromMetadata(title_id, type); | ||
| 269 | if (id == boost::none) | ||
| 270 | return nullptr; | ||
| 271 | |||
| 272 | return GetFileAtID(id.get()); | ||
| 273 | } | ||
| 274 | |||
| 275 | VirtualFile RegisteredCache::GetEntryUnparsed(RegisteredCacheEntry entry) const { | ||
| 276 | return GetEntryUnparsed(entry.title_id, entry.type); | ||
| 277 | } | ||
| 278 | |||
| 265 | VirtualFile RegisteredCache::GetEntryRaw(u64 title_id, ContentRecordType type) const { | 279 | VirtualFile RegisteredCache::GetEntryRaw(u64 title_id, ContentRecordType type) const { |
| 266 | const auto id = GetNcaIDFromMetadata(title_id, type); | 280 | const auto id = GetNcaIDFromMetadata(title_id, type); |
| 267 | if (id == boost::none) | 281 | if (id == boost::none) |
diff --git a/src/core/file_sys/registered_cache.h b/src/core/file_sys/registered_cache.h index a7c51a59c..7b8955dfa 100644 --- a/src/core/file_sys/registered_cache.h +++ b/src/core/file_sys/registered_cache.h | |||
| @@ -63,12 +63,16 @@ public: | |||
| 63 | explicit RegisteredCache(VirtualDir dir, | 63 | explicit RegisteredCache(VirtualDir dir, |
| 64 | RegisteredCacheParsingFunction parsing_function = | 64 | RegisteredCacheParsingFunction parsing_function = |
| 65 | [](const VirtualFile& file, const NcaID& id) { return file; }); | 65 | [](const VirtualFile& file, const NcaID& id) { return file; }); |
| 66 | ~RegisteredCache(); | ||
| 66 | 67 | ||
| 67 | void Refresh(); | 68 | void Refresh(); |
| 68 | 69 | ||
| 69 | bool HasEntry(u64 title_id, ContentRecordType type) const; | 70 | bool HasEntry(u64 title_id, ContentRecordType type) const; |
| 70 | bool HasEntry(RegisteredCacheEntry entry) const; | 71 | bool HasEntry(RegisteredCacheEntry entry) const; |
| 71 | 72 | ||
| 73 | VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const; | ||
| 74 | VirtualFile GetEntryUnparsed(RegisteredCacheEntry entry) const; | ||
| 75 | |||
| 72 | VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const; | 76 | VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const; |
| 73 | VirtualFile GetEntryRaw(RegisteredCacheEntry entry) const; | 77 | VirtualFile GetEntryRaw(RegisteredCacheEntry entry) const; |
| 74 | 78 | ||
diff --git a/src/core/file_sys/sdmc_factory.cpp b/src/core/file_sys/sdmc_factory.cpp index d1acf379f..d66a9c9a4 100644 --- a/src/core/file_sys/sdmc_factory.cpp +++ b/src/core/file_sys/sdmc_factory.cpp | |||
| @@ -3,14 +3,27 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <memory> | 5 | #include <memory> |
| 6 | #include "core/file_sys/registered_cache.h" | ||
| 6 | #include "core/file_sys/sdmc_factory.h" | 7 | #include "core/file_sys/sdmc_factory.h" |
| 8 | #include "core/file_sys/xts_archive.h" | ||
| 7 | 9 | ||
| 8 | namespace FileSys { | 10 | namespace FileSys { |
| 9 | 11 | ||
| 10 | SDMCFactory::SDMCFactory(VirtualDir dir) : dir(std::move(dir)) {} | 12 | SDMCFactory::SDMCFactory(VirtualDir dir_) |
| 13 | : dir(std::move(dir_)), contents(std::make_shared<RegisteredCache>( | ||
| 14 | GetOrCreateDirectoryRelative(dir, "/Nintendo/Contents/registered"), | ||
| 15 | [](const VirtualFile& file, const NcaID& id) { | ||
| 16 | return std::make_shared<NAX>(file, id)->GetDecrypted(); | ||
| 17 | })) {} | ||
| 18 | |||
| 19 | SDMCFactory::~SDMCFactory() = default; | ||
| 11 | 20 | ||
| 12 | ResultVal<VirtualDir> SDMCFactory::Open() { | 21 | ResultVal<VirtualDir> SDMCFactory::Open() { |
| 13 | return MakeResult<VirtualDir>(dir); | 22 | return MakeResult<VirtualDir>(dir); |
| 14 | } | 23 | } |
| 15 | 24 | ||
| 25 | std::shared_ptr<RegisteredCache> SDMCFactory::GetSDMCContents() const { | ||
| 26 | return contents; | ||
| 27 | } | ||
| 28 | |||
| 16 | } // namespace FileSys | 29 | } // namespace FileSys |
diff --git a/src/core/file_sys/sdmc_factory.h b/src/core/file_sys/sdmc_factory.h index 245060690..ea12149de 100644 --- a/src/core/file_sys/sdmc_factory.h +++ b/src/core/file_sys/sdmc_factory.h | |||
| @@ -4,20 +4,27 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <memory> | ||
| 7 | #include "core/file_sys/vfs.h" | 8 | #include "core/file_sys/vfs.h" |
| 8 | #include "core/hle/result.h" | 9 | #include "core/hle/result.h" |
| 9 | 10 | ||
| 10 | namespace FileSys { | 11 | namespace FileSys { |
| 11 | 12 | ||
| 13 | class RegisteredCache; | ||
| 14 | |||
| 12 | /// File system interface to the SDCard archive | 15 | /// File system interface to the SDCard archive |
| 13 | class SDMCFactory { | 16 | class SDMCFactory { |
| 14 | public: | 17 | public: |
| 15 | explicit SDMCFactory(VirtualDir dir); | 18 | explicit SDMCFactory(VirtualDir dir); |
| 19 | ~SDMCFactory(); | ||
| 16 | 20 | ||
| 17 | ResultVal<VirtualDir> Open(); | 21 | ResultVal<VirtualDir> Open(); |
| 22 | std::shared_ptr<RegisteredCache> GetSDMCContents() const; | ||
| 18 | 23 | ||
| 19 | private: | 24 | private: |
| 20 | VirtualDir dir; | 25 | VirtualDir dir; |
| 26 | |||
| 27 | std::shared_ptr<RegisteredCache> contents; | ||
| 21 | }; | 28 | }; |
| 22 | 29 | ||
| 23 | } // namespace FileSys | 30 | } // namespace FileSys |
diff --git a/src/core/file_sys/vfs.cpp b/src/core/file_sys/vfs.cpp index b915b4c11..146c839f4 100644 --- a/src/core/file_sys/vfs.cpp +++ b/src/core/file_sys/vfs.cpp | |||
| @@ -462,4 +462,11 @@ bool VfsRawCopy(VirtualFile src, VirtualFile dest) { | |||
| 462 | std::vector<u8> data = src->ReadAllBytes(); | 462 | std::vector<u8> data = src->ReadAllBytes(); |
| 463 | return dest->WriteBytes(data, 0) == data.size(); | 463 | return dest->WriteBytes(data, 0) == data.size(); |
| 464 | } | 464 | } |
| 465 | |||
| 466 | VirtualDir GetOrCreateDirectoryRelative(const VirtualDir& rel, std::string_view path) { | ||
| 467 | const auto res = rel->GetDirectoryRelative(path); | ||
| 468 | if (res == nullptr) | ||
| 469 | return rel->CreateDirectoryRelative(path); | ||
| 470 | return res; | ||
| 471 | } | ||
| 465 | } // namespace FileSys | 472 | } // namespace FileSys |
diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h index 22db08b59..5142a3e86 100644 --- a/src/core/file_sys/vfs.h +++ b/src/core/file_sys/vfs.h | |||
| @@ -318,4 +318,8 @@ bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, size_t block | |||
| 318 | // directory of src/dest. | 318 | // directory of src/dest. |
| 319 | bool VfsRawCopy(VirtualFile src, VirtualFile dest); | 319 | bool VfsRawCopy(VirtualFile src, VirtualFile dest); |
| 320 | 320 | ||
| 321 | // Checks if the directory at path relative to rel exists. If it does, returns that. If it does not | ||
| 322 | // it attempts to create it and returns the new dir or nullptr on failure. | ||
| 323 | VirtualDir GetOrCreateDirectoryRelative(const VirtualDir& rel, std::string_view path); | ||
| 324 | |||
| 321 | } // namespace FileSys | 325 | } // namespace FileSys |
diff --git a/src/core/file_sys/xts_archive.cpp b/src/core/file_sys/xts_archive.cpp new file mode 100644 index 000000000..552835738 --- /dev/null +++ b/src/core/file_sys/xts_archive.cpp | |||
| @@ -0,0 +1,169 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <array> | ||
| 7 | #include <cstring> | ||
| 8 | #include <regex> | ||
| 9 | #include <string> | ||
| 10 | #include <mbedtls/md.h> | ||
| 11 | #include <mbedtls/sha256.h> | ||
| 12 | #include "common/assert.h" | ||
| 13 | #include "common/hex_util.h" | ||
| 14 | #include "common/logging/log.h" | ||
| 15 | #include "core/crypto/aes_util.h" | ||
| 16 | #include "core/crypto/xts_encryption_layer.h" | ||
| 17 | #include "core/file_sys/partition_filesystem.h" | ||
| 18 | #include "core/file_sys/vfs_offset.h" | ||
| 19 | #include "core/file_sys/xts_archive.h" | ||
| 20 | #include "core/loader/loader.h" | ||
| 21 | |||
| 22 | namespace FileSys { | ||
| 23 | |||
| 24 | constexpr u64 NAX_HEADER_PADDING_SIZE = 0x4000; | ||
| 25 | |||
| 26 | template <typename SourceData, typename SourceKey, typename Destination> | ||
| 27 | static bool CalculateHMAC256(Destination* out, const SourceKey* key, size_t key_length, | ||
| 28 | const SourceData* data, size_t data_length) { | ||
| 29 | mbedtls_md_context_t context; | ||
| 30 | mbedtls_md_init(&context); | ||
| 31 | |||
| 32 | const auto key_f = reinterpret_cast<const u8*>(key); | ||
| 33 | const std::vector<u8> key_v(key_f, key_f + key_length); | ||
| 34 | |||
| 35 | if (mbedtls_md_setup(&context, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1) || | ||
| 36 | mbedtls_md_hmac_starts(&context, reinterpret_cast<const u8*>(key), key_length) || | ||
| 37 | mbedtls_md_hmac_update(&context, reinterpret_cast<const u8*>(data), data_length) || | ||
| 38 | mbedtls_md_hmac_finish(&context, reinterpret_cast<u8*>(out))) { | ||
| 39 | mbedtls_md_free(&context); | ||
| 40 | return false; | ||
| 41 | } | ||
| 42 | |||
| 43 | mbedtls_md_free(&context); | ||
| 44 | return true; | ||
| 45 | } | ||
| 46 | |||
| 47 | NAX::NAX(VirtualFile file_) : file(std::move(file_)), header(std::make_unique<NAXHeader>()) { | ||
| 48 | std::string path = FileUtil::SanitizePath(file->GetFullPath()); | ||
| 49 | static const std::regex nax_path_regex("/registered/(000000[0-9A-F]{2})/([0-9A-F]{32})\\.nca", | ||
| 50 | std::regex_constants::ECMAScript | | ||
| 51 | std::regex_constants::icase); | ||
| 52 | std::smatch match; | ||
| 53 | if (!std::regex_search(path, match, nax_path_regex)) { | ||
| 54 | status = Loader::ResultStatus::ErrorBadNAXFilePath; | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | |||
| 58 | std::string two_dir = match[1]; | ||
| 59 | std::string nca_id = match[2]; | ||
| 60 | std::transform(two_dir.begin(), two_dir.end(), two_dir.begin(), ::toupper); | ||
| 61 | std::transform(nca_id.begin(), nca_id.end(), nca_id.begin(), ::tolower); | ||
| 62 | |||
| 63 | status = Parse(fmt::format("/registered/{}/{}.nca", two_dir, nca_id)); | ||
| 64 | } | ||
| 65 | |||
| 66 | NAX::NAX(VirtualFile file_, std::array<u8, 0x10> nca_id) | ||
| 67 | : file(std::move(file_)), header(std::make_unique<NAXHeader>()) { | ||
| 68 | Core::Crypto::SHA256Hash hash{}; | ||
| 69 | mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0); | ||
| 70 | status = Parse(fmt::format("/registered/000000{:02X}/{}.nca", hash[0], | ||
| 71 | Common::HexArrayToString(nca_id, false))); | ||
| 72 | } | ||
| 73 | |||
| 74 | Loader::ResultStatus NAX::Parse(std::string_view path) { | ||
| 75 | if (file->ReadObject(header.get()) != sizeof(NAXHeader)) | ||
| 76 | return Loader::ResultStatus::ErrorBadNAXHeader; | ||
| 77 | |||
| 78 | if (header->magic != Common::MakeMagic('N', 'A', 'X', '0')) | ||
| 79 | return Loader::ResultStatus::ErrorBadNAXHeader; | ||
| 80 | |||
| 81 | if (file->GetSize() < NAX_HEADER_PADDING_SIZE + header->file_size) | ||
| 82 | return Loader::ResultStatus::ErrorIncorrectNAXFileSize; | ||
| 83 | |||
| 84 | keys.DeriveSDSeedLazy(); | ||
| 85 | std::array<Core::Crypto::Key256, 2> sd_keys{}; | ||
| 86 | const auto sd_keys_res = Core::Crypto::DeriveSDKeys(sd_keys, keys); | ||
| 87 | if (sd_keys_res != Loader::ResultStatus::Success) { | ||
| 88 | return sd_keys_res; | ||
| 89 | } | ||
| 90 | |||
| 91 | const auto enc_keys = header->key_area; | ||
| 92 | |||
| 93 | size_t i = 0; | ||
| 94 | for (; i < sd_keys.size(); ++i) { | ||
| 95 | std::array<Core::Crypto::Key128, 2> nax_keys{}; | ||
| 96 | if (!CalculateHMAC256(nax_keys.data(), sd_keys[i].data(), 0x10, std::string(path).c_str(), | ||
| 97 | path.size())) { | ||
| 98 | return Loader::ResultStatus::ErrorNAXKeyHMACFailed; | ||
| 99 | } | ||
| 100 | |||
| 101 | for (size_t j = 0; j < nax_keys.size(); ++j) { | ||
| 102 | Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(nax_keys[j], | ||
| 103 | Core::Crypto::Mode::ECB); | ||
| 104 | cipher.Transcode(enc_keys[j].data(), 0x10, header->key_area[j].data(), | ||
| 105 | Core::Crypto::Op::Decrypt); | ||
| 106 | } | ||
| 107 | |||
| 108 | Core::Crypto::SHA256Hash validation{}; | ||
| 109 | if (!CalculateHMAC256(validation.data(), &header->magic, 0x60, sd_keys[i].data() + 0x10, | ||
| 110 | 0x10)) { | ||
| 111 | return Loader::ResultStatus::ErrorNAXValidationHMACFailed; | ||
| 112 | } | ||
| 113 | if (header->hmac == validation) | ||
| 114 | break; | ||
| 115 | } | ||
| 116 | |||
| 117 | if (i == 2) { | ||
| 118 | return Loader::ResultStatus::ErrorNAXKeyDerivationFailed; | ||
| 119 | } | ||
| 120 | |||
| 121 | type = static_cast<NAXContentType>(i); | ||
| 122 | |||
| 123 | Core::Crypto::Key256 final_key{}; | ||
| 124 | std::memcpy(final_key.data(), &header->key_area, final_key.size()); | ||
| 125 | const auto enc_file = | ||
| 126 | std::make_shared<OffsetVfsFile>(file, header->file_size, NAX_HEADER_PADDING_SIZE); | ||
| 127 | dec_file = std::make_shared<Core::Crypto::XTSEncryptionLayer>(enc_file, final_key); | ||
| 128 | |||
| 129 | return Loader::ResultStatus::Success; | ||
| 130 | } | ||
| 131 | |||
| 132 | Loader::ResultStatus NAX::GetStatus() const { | ||
| 133 | return status; | ||
| 134 | } | ||
| 135 | |||
| 136 | VirtualFile NAX::GetDecrypted() const { | ||
| 137 | return dec_file; | ||
| 138 | } | ||
| 139 | |||
| 140 | std::shared_ptr<NCA> NAX::AsNCA() const { | ||
| 141 | if (type == NAXContentType::NCA) | ||
| 142 | return std::make_shared<NCA>(GetDecrypted()); | ||
| 143 | return nullptr; | ||
| 144 | } | ||
| 145 | |||
| 146 | NAXContentType NAX::GetContentType() const { | ||
| 147 | return type; | ||
| 148 | } | ||
| 149 | |||
| 150 | std::vector<std::shared_ptr<VfsFile>> NAX::GetFiles() const { | ||
| 151 | return {dec_file}; | ||
| 152 | } | ||
| 153 | |||
| 154 | std::vector<std::shared_ptr<VfsDirectory>> NAX::GetSubdirectories() const { | ||
| 155 | return {}; | ||
| 156 | } | ||
| 157 | |||
| 158 | std::string NAX::GetName() const { | ||
| 159 | return file->GetName(); | ||
| 160 | } | ||
| 161 | |||
| 162 | std::shared_ptr<VfsDirectory> NAX::GetParentDirectory() const { | ||
| 163 | return file->GetContainingDirectory(); | ||
| 164 | } | ||
| 165 | |||
| 166 | bool NAX::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { | ||
| 167 | return false; | ||
| 168 | } | ||
| 169 | } // namespace FileSys | ||
diff --git a/src/core/file_sys/xts_archive.h b/src/core/file_sys/xts_archive.h new file mode 100644 index 000000000..55d2154a6 --- /dev/null +++ b/src/core/file_sys/xts_archive.h | |||
| @@ -0,0 +1,69 @@ | |||
| 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 <array> | ||
| 8 | #include <vector> | ||
| 9 | #include "common/common_types.h" | ||
| 10 | #include "common/swap.h" | ||
| 11 | #include "core/crypto/key_manager.h" | ||
| 12 | #include "core/file_sys/content_archive.h" | ||
| 13 | #include "core/file_sys/vfs.h" | ||
| 14 | #include "core/loader/loader.h" | ||
| 15 | |||
| 16 | namespace FileSys { | ||
| 17 | |||
| 18 | struct NAXHeader { | ||
| 19 | std::array<u8, 0x20> hmac; | ||
| 20 | u64_le magic; | ||
| 21 | std::array<Core::Crypto::Key128, 2> key_area; | ||
| 22 | u64_le file_size; | ||
| 23 | INSERT_PADDING_BYTES(0x30); | ||
| 24 | }; | ||
| 25 | static_assert(sizeof(NAXHeader) == 0x80, "NAXHeader has incorrect size."); | ||
| 26 | |||
| 27 | enum class NAXContentType : u8 { | ||
| 28 | Save = 0, | ||
| 29 | NCA = 1, | ||
| 30 | }; | ||
| 31 | |||
| 32 | class NAX : public ReadOnlyVfsDirectory { | ||
| 33 | public: | ||
| 34 | explicit NAX(VirtualFile file); | ||
| 35 | explicit NAX(VirtualFile file, std::array<u8, 0x10> nca_id); | ||
| 36 | |||
| 37 | Loader::ResultStatus GetStatus() const; | ||
| 38 | |||
| 39 | VirtualFile GetDecrypted() const; | ||
| 40 | |||
| 41 | std::shared_ptr<NCA> AsNCA() const; | ||
| 42 | |||
| 43 | NAXContentType GetContentType() const; | ||
| 44 | |||
| 45 | std::vector<std::shared_ptr<VfsFile>> GetFiles() const override; | ||
| 46 | |||
| 47 | std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override; | ||
| 48 | |||
| 49 | std::string GetName() const override; | ||
| 50 | |||
| 51 | std::shared_ptr<VfsDirectory> GetParentDirectory() const override; | ||
| 52 | |||
| 53 | protected: | ||
| 54 | bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; | ||
| 55 | |||
| 56 | private: | ||
| 57 | Loader::ResultStatus Parse(std::string_view path); | ||
| 58 | |||
| 59 | std::unique_ptr<NAXHeader> header; | ||
| 60 | |||
| 61 | VirtualFile file; | ||
| 62 | Loader::ResultStatus status; | ||
| 63 | NAXContentType type; | ||
| 64 | |||
| 65 | VirtualFile dec_file; | ||
| 66 | |||
| 67 | Core::Crypto::KeyManager keys; | ||
| 68 | }; | ||
| 69 | } // namespace FileSys | ||