diff options
| author | 2018-08-16 17:08:13 -0400 | |
|---|---|---|
| committer | 2018-08-23 11:53:30 -0400 | |
| commit | ab44192ab01a27115e14a52d4823722cbd357c0d (patch) | |
| tree | b61ce1e6b007d8497c17066ab4c44e2f658d5af0 /src/core | |
| parent | registration: Add GetEntryUnparsed methods (diff) | |
| download | yuzu-ab44192ab01a27115e14a52d4823722cbd357c0d.tar.gz yuzu-ab44192ab01a27115e14a52d4823722cbd357c0d.tar.xz yuzu-ab44192ab01a27115e14a52d4823722cbd357c0d.zip | |
file_sys: Implement NAX containers
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | src/core/file_sys/xts_archive.cpp | 164 | ||||
| -rw-r--r-- | src/core/file_sys/xts_archive.h | 68 |
3 files changed, 238 insertions, 0 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 31a7bf6fd..a74270a0f 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -20,6 +20,8 @@ add_library(core STATIC | |||
| 20 | crypto/key_manager.h | 20 | crypto/key_manager.h |
| 21 | crypto/ctr_encryption_layer.cpp | 21 | crypto/ctr_encryption_layer.cpp |
| 22 | crypto/ctr_encryption_layer.h | 22 | crypto/ctr_encryption_layer.h |
| 23 | crypto/xts_encryption_layer.cpp | ||
| 24 | crypto/xts_encryption_layer.h | ||
| 23 | file_sys/bis_factory.cpp | 25 | file_sys/bis_factory.cpp |
| 24 | file_sys/bis_factory.h | 26 | file_sys/bis_factory.h |
| 25 | file_sys/card_image.cpp | 27 | file_sys/card_image.cpp |
| @@ -57,6 +59,8 @@ add_library(core STATIC | |||
| 57 | file_sys/vfs_real.h | 59 | file_sys/vfs_real.h |
| 58 | file_sys/vfs_vector.cpp | 60 | file_sys/vfs_vector.cpp |
| 59 | file_sys/vfs_vector.h | 61 | file_sys/vfs_vector.h |
| 62 | file_sys/xts_archive.cpp | ||
| 63 | file_sys/xts_archive.h | ||
| 60 | frontend/emu_window.cpp | 64 | frontend/emu_window.cpp |
| 61 | frontend/emu_window.h | 65 | frontend/emu_window.h |
| 62 | frontend/framebuffer_layout.cpp | 66 | frontend/framebuffer_layout.cpp |
| @@ -347,6 +351,8 @@ add_library(core STATIC | |||
| 347 | loader/linker.h | 351 | loader/linker.h |
| 348 | loader/loader.cpp | 352 | loader/loader.cpp |
| 349 | loader/loader.h | 353 | loader/loader.h |
| 354 | loader/nax.cpp | ||
| 355 | loader/nax.h | ||
| 350 | loader/nca.cpp | 356 | loader/nca.cpp |
| 351 | loader/nca.h | 357 | loader/nca.h |
| 352 | loader/nro.cpp | 358 | loader/nro.cpp |
diff --git a/src/core/file_sys/xts_archive.cpp b/src/core/file_sys/xts_archive.cpp new file mode 100644 index 000000000..54be31916 --- /dev/null +++ b/src/core/file_sys/xts_archive.cpp | |||
| @@ -0,0 +1,164 @@ | |||
| 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 <array> | ||
| 6 | #include <regex> | ||
| 7 | #include <string> | ||
| 8 | #include <mbedtls/md.h> | ||
| 9 | #include <mbedtls/sha256.h> | ||
| 10 | #include "common/assert.h" | ||
| 11 | #include "common/hex_util.h" | ||
| 12 | #include "common/logging/log.h" | ||
| 13 | #include "core/crypto/aes_util.h" | ||
| 14 | #include "core/crypto/xts_encryption_layer.h" | ||
| 15 | #include "core/file_sys/partition_filesystem.h" | ||
| 16 | #include "core/file_sys/vfs_offset.h" | ||
| 17 | #include "core/file_sys/xts_archive.h" | ||
| 18 | #include "core/loader/loader.h" | ||
| 19 | |||
| 20 | namespace FileSys { | ||
| 21 | |||
| 22 | template <typename SourceData, typename SourceKey, typename Destination> | ||
| 23 | static bool CalculateHMAC256(Destination* out, const SourceKey* key, size_t key_length, | ||
| 24 | const SourceData* data, size_t data_length) { | ||
| 25 | mbedtls_md_context_t context; | ||
| 26 | mbedtls_md_init(&context); | ||
| 27 | |||
| 28 | const auto key_f = reinterpret_cast<const u8*>(key); | ||
| 29 | const std::vector<u8> key_v(key_f, key_f + key_length); | ||
| 30 | |||
| 31 | if (mbedtls_md_setup(&context, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1) || | ||
| 32 | mbedtls_md_hmac_starts(&context, reinterpret_cast<const u8*>(key), key_length) || | ||
| 33 | mbedtls_md_hmac_update(&context, reinterpret_cast<const u8*>(data), data_length) || | ||
| 34 | mbedtls_md_hmac_finish(&context, reinterpret_cast<u8*>(out))) { | ||
| 35 | mbedtls_md_free(&context); | ||
| 36 | return false; | ||
| 37 | } | ||
| 38 | |||
| 39 | mbedtls_md_free(&context); | ||
| 40 | return true; | ||
| 41 | } | ||
| 42 | |||
| 43 | NAX::NAX(VirtualFile file_) : file(std::move(file_)), header(std::make_unique<NAXHeader>()) { | ||
| 44 | std::string path = FileUtil::SanitizePath(file->GetFullPath()); | ||
| 45 | static const std::regex nax_path_regex("/registered/(000000[0-9A-F]{2})/([0-9A-F]{32})\\.nca", | ||
| 46 | std::regex_constants::ECMAScript | | ||
| 47 | std::regex_constants::icase); | ||
| 48 | std::smatch match; | ||
| 49 | if (!std::regex_search(path, match, nax_path_regex)) { | ||
| 50 | status = Loader::ResultStatus::ErrorBadNAXFilePath; | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | std::string two_dir = match[1]; | ||
| 55 | std::string nca_id = match[2]; | ||
| 56 | std::transform(two_dir.begin(), two_dir.end(), two_dir.begin(), ::toupper); | ||
| 57 | std::transform(nca_id.begin(), nca_id.end(), nca_id.begin(), ::tolower); | ||
| 58 | |||
| 59 | status = Parse(fmt::format("/registered/{}/{}.nca", two_dir, nca_id)); | ||
| 60 | } | ||
| 61 | |||
| 62 | NAX::NAX(VirtualFile file_, std::array<u8, 0x10> nca_id) | ||
| 63 | : file(std::move(file_)), header(std::make_unique<NAXHeader>()) { | ||
| 64 | Core::Crypto::SHA256Hash hash{}; | ||
| 65 | mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0); | ||
| 66 | status = Parse(fmt::format("/registered/000000{:02X}/{}.nca", hash[0], | ||
| 67 | Common::HexArrayToString(nca_id, false))); | ||
| 68 | } | ||
| 69 | |||
| 70 | Loader::ResultStatus NAX::Parse(std::string path) { | ||
| 71 | if (file->ReadObject(header.get()) != sizeof(NAXHeader)) | ||
| 72 | return Loader::ResultStatus::ErrorBadNAXHeader; | ||
| 73 | |||
| 74 | if (header->magic != Common::MakeMagic('N', 'A', 'X', '0')) | ||
| 75 | return Loader::ResultStatus::ErrorBadNAXHeader; | ||
| 76 | |||
| 77 | if (file->GetSize() < 0x4000 + header->file_size) | ||
| 78 | return Loader::ResultStatus::ErrorIncorrectNAXFileSize; | ||
| 79 | |||
| 80 | keys.DeriveSDSeedLazy(); | ||
| 81 | std::array<Core::Crypto::Key256, 2> sd_keys{}; | ||
| 82 | const auto sd_keys_res = Core::Crypto::DeriveSDKeys(sd_keys, keys); | ||
| 83 | if (sd_keys_res != Loader::ResultStatus::Success) { | ||
| 84 | return sd_keys_res; | ||
| 85 | } | ||
| 86 | |||
| 87 | const auto enc_keys = header->key_area; | ||
| 88 | |||
| 89 | size_t i = 0; | ||
| 90 | for (; i < 2; ++i) { | ||
| 91 | std::array<Core::Crypto::Key128, 2> nax_keys{}; | ||
| 92 | if (!CalculateHMAC256(nax_keys.data(), sd_keys[i].data(), 0x10, path.c_str(), | ||
| 93 | path.size())) { | ||
| 94 | return Loader::ResultStatus::ErrorNAXKeyHMACFailed; | ||
| 95 | } | ||
| 96 | |||
| 97 | for (size_t j = 0; j < 2; ++j) { | ||
| 98 | Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(nax_keys[j], | ||
| 99 | Core::Crypto::Mode::ECB); | ||
| 100 | cipher.Transcode(enc_keys[j].data(), 0x10, header->key_area[j].data(), | ||
| 101 | Core::Crypto::Op::Decrypt); | ||
| 102 | } | ||
| 103 | |||
| 104 | Core::Crypto::SHA256Hash validation{}; | ||
| 105 | if (!CalculateHMAC256(validation.data(), &header->magic, 0x60, sd_keys[i].data() + 0x10, | ||
| 106 | 0x10)) { | ||
| 107 | return Loader::ResultStatus::ErrorNAXValidationHMACFailed; | ||
| 108 | } | ||
| 109 | if (header->hmac == validation) | ||
| 110 | break; | ||
| 111 | } | ||
| 112 | |||
| 113 | if (i == 2) { | ||
| 114 | return Loader::ResultStatus::ErrorNAXKeyDerivationFailed; | ||
| 115 | } | ||
| 116 | |||
| 117 | type = static_cast<NAXContentType>(i); | ||
| 118 | |||
| 119 | Core::Crypto::Key256 final_key{}; | ||
| 120 | memcpy(final_key.data(), &header->key_area, 0x20); | ||
| 121 | const auto enc_file = std::make_shared<OffsetVfsFile>(file, header->file_size, 0x4000); | ||
| 122 | dec_file = std::make_shared<Core::Crypto::XTSEncryptionLayer>(enc_file, final_key); | ||
| 123 | |||
| 124 | return Loader::ResultStatus::Success; | ||
| 125 | } | ||
| 126 | |||
| 127 | Loader::ResultStatus NAX::GetStatus() { | ||
| 128 | return status; | ||
| 129 | } | ||
| 130 | |||
| 131 | VirtualFile NAX::GetDecrypted() { | ||
| 132 | return dec_file; | ||
| 133 | } | ||
| 134 | |||
| 135 | std::shared_ptr<NCA> NAX::AsNCA() { | ||
| 136 | if (type == NAXContentType::NCA) | ||
| 137 | return std::make_shared<NCA>(GetDecrypted()); | ||
| 138 | return nullptr; | ||
| 139 | } | ||
| 140 | |||
| 141 | NAXContentType NAX::GetContentType() { | ||
| 142 | return type; | ||
| 143 | } | ||
| 144 | |||
| 145 | std::vector<std::shared_ptr<VfsFile>> NAX::GetFiles() const { | ||
| 146 | return {dec_file}; | ||
| 147 | } | ||
| 148 | |||
| 149 | std::vector<std::shared_ptr<VfsDirectory>> NAX::GetSubdirectories() const { | ||
| 150 | return {}; | ||
| 151 | } | ||
| 152 | |||
| 153 | std::string NAX::GetName() const { | ||
| 154 | return file->GetName(); | ||
| 155 | } | ||
| 156 | |||
| 157 | std::shared_ptr<VfsDirectory> NAX::GetParentDirectory() const { | ||
| 158 | return file->GetContainingDirectory(); | ||
| 159 | } | ||
| 160 | |||
| 161 | bool NAX::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { | ||
| 162 | return false; | ||
| 163 | } | ||
| 164 | } // 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..4e44f634a --- /dev/null +++ b/src/core/file_sys/xts_archive.h | |||
| @@ -0,0 +1,68 @@ | |||
| 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/file_sys/content_archive.h" | ||
| 12 | #include "core/file_sys/vfs.h" | ||
| 13 | #include "core/loader/loader.h" | ||
| 14 | |||
| 15 | namespace FileSys { | ||
| 16 | |||
| 17 | struct NAXHeader { | ||
| 18 | std::array<u8, 0x20> hmac; | ||
| 19 | u64_le magic; | ||
| 20 | std::array<Core::Crypto::Key128, 2> key_area; | ||
| 21 | u64_le file_size; | ||
| 22 | INSERT_PADDING_BYTES(0x30); | ||
| 23 | }; | ||
| 24 | static_assert(sizeof(NAXHeader) == 0x80, "NAXHeader has incorrect size."); | ||
| 25 | |||
| 26 | enum class NAXContentType : u8 { | ||
| 27 | Save = 0, | ||
| 28 | NCA = 1, | ||
| 29 | }; | ||
| 30 | |||
| 31 | class NAX : public ReadOnlyVfsDirectory { | ||
| 32 | public: | ||
| 33 | explicit NAX(VirtualFile file); | ||
| 34 | explicit NAX(VirtualFile file, std::array<u8, 0x10> nca_id); | ||
| 35 | |||
| 36 | Loader::ResultStatus GetStatus(); | ||
| 37 | |||
| 38 | VirtualFile GetDecrypted(); | ||
| 39 | |||
| 40 | std::shared_ptr<NCA> AsNCA(); | ||
| 41 | |||
| 42 | NAXContentType GetContentType(); | ||
| 43 | |||
| 44 | std::vector<std::shared_ptr<VfsFile>> GetFiles() const override; | ||
| 45 | |||
| 46 | std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override; | ||
| 47 | |||
| 48 | std::string GetName() const override; | ||
| 49 | |||
| 50 | std::shared_ptr<VfsDirectory> GetParentDirectory() const override; | ||
| 51 | |||
| 52 | protected: | ||
| 53 | bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; | ||
| 54 | |||
| 55 | private: | ||
| 56 | Loader::ResultStatus Parse(std::string path); | ||
| 57 | |||
| 58 | std::unique_ptr<NAXHeader> header; | ||
| 59 | |||
| 60 | VirtualFile file; | ||
| 61 | Loader::ResultStatus status; | ||
| 62 | NAXContentType type; | ||
| 63 | |||
| 64 | VirtualFile dec_file; | ||
| 65 | |||
| 66 | Core::Crypto::KeyManager keys; | ||
| 67 | }; | ||
| 68 | } // namespace FileSys | ||