suyu/src/core/file_sys/xts_archive.cpp

172 lines
5.6 KiB
C++
Raw Normal View History

2018-08-16 23:08:13 +02:00
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <algorithm>
2018-08-16 23:08:13 +02:00
#include <array>
#include <cstring>
2018-08-16 23:08:13 +02:00
#include <regex>
#include <string>
2018-08-16 23:08:13 +02:00
#include <mbedtls/md.h>
#include <mbedtls/sha256.h>
common: fs: Rework the Common Filesystem interface to make use of std::filesystem (#6270) * common: fs: fs_types: Create filesystem types Contains various filesystem types used by the Common::FS library * common: fs: fs_util: Add std::string to std::u8string conversion utility * common: fs: path_util: Add utlity functions for paths Contains various utility functions for getting or manipulating filesystem paths used by the Common::FS library * common: fs: file: Rewrite the IOFile implementation * common: fs: Reimplement Common::FS library using std::filesystem * common: fs: fs_paths: Add fs_paths to replace common_paths * common: fs: path_util: Add the rest of the path functions * common: Remove the previous Common::FS implementation * general: Remove unused fs includes * string_util: Remove unused function and include * nvidia_flags: Migrate to the new Common::FS library * settings: Migrate to the new Common::FS library * logging: backend: Migrate to the new Common::FS library * core: Migrate to the new Common::FS library * perf_stats: Migrate to the new Common::FS library * reporter: Migrate to the new Common::FS library * telemetry_session: Migrate to the new Common::FS library * key_manager: Migrate to the new Common::FS library * bis_factory: Migrate to the new Common::FS library * registered_cache: Migrate to the new Common::FS library * xts_archive: Migrate to the new Common::FS library * service: acc: Migrate to the new Common::FS library * applets/profile: Migrate to the new Common::FS library * applets/web: Migrate to the new Common::FS library * service: filesystem: Migrate to the new Common::FS library * loader: Migrate to the new Common::FS library * gl_shader_disk_cache: Migrate to the new Common::FS library * nsight_aftermath_tracker: Migrate to the new Common::FS library * vulkan_library: Migrate to the new Common::FS library * configure_debug: Migrate to the new Common::FS library * game_list_worker: Migrate to the new Common::FS library * config: Migrate to the new Common::FS library * configure_filesystem: Migrate to the new Common::FS library * configure_per_game_addons: Migrate to the new Common::FS library * configure_profile_manager: Migrate to the new Common::FS library * configure_ui: Migrate to the new Common::FS library * input_profiles: Migrate to the new Common::FS library * yuzu_cmd: config: Migrate to the new Common::FS library * yuzu_cmd: Migrate to the new Common::FS library * vfs_real: Migrate to the new Common::FS library * vfs: Migrate to the new Common::FS library * vfs_libzip: Migrate to the new Common::FS library * service: bcat: Migrate to the new Common::FS library * yuzu: main: Migrate to the new Common::FS library * vfs_real: Delete the contents of an existing file in CreateFile Current usages of CreateFile expect to delete the contents of an existing file, retain this behavior for now. * input_profiles: Don't iterate the input profile dir if it does not exist Silences an error produced in the log if the directory does not exist. * game_list_worker: Skip parsing file if the returned VfsFile is nullptr Prevents crashes in GetLoader when the virtual file is nullptr * common: fs: Validate paths for path length * service: filesystem: Open the mod load directory as read only
2021-05-26 01:32:56 +02:00
#include "common/fs/path_util.h"
2018-08-16 23:08:13 +02:00
#include "common/hex_util.h"
#include "common/string_util.h"
2018-08-16 23:08:13 +02:00
#include "core/crypto/aes_util.h"
#include "core/crypto/key_manager.h"
2018-08-16 23:08:13 +02:00
#include "core/crypto/xts_encryption_layer.h"
#include "core/file_sys/content_archive.h"
2018-08-16 23:08:13 +02:00
#include "core/file_sys/vfs_offset.h"
#include "core/file_sys/xts_archive.h"
#include "core/loader/loader.h"
namespace FileSys {
2018-08-19 03:14:57 +02:00
constexpr u64 NAX_HEADER_PADDING_SIZE = 0x4000;
2018-08-16 23:08:13 +02:00
template <typename SourceData, typename SourceKey, typename Destination>
static bool CalculateHMAC256(Destination* out, const SourceKey* key, std::size_t key_length,
const SourceData* data, std::size_t data_length) {
2018-08-16 23:08:13 +02:00
mbedtls_md_context_t context;
mbedtls_md_init(&context);
if (mbedtls_md_setup(&context, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1) ||
mbedtls_md_hmac_starts(&context, reinterpret_cast<const u8*>(key), key_length) ||
mbedtls_md_hmac_update(&context, reinterpret_cast<const u8*>(data), data_length) ||
mbedtls_md_hmac_finish(&context, reinterpret_cast<u8*>(out))) {
mbedtls_md_free(&context);
return false;
}
mbedtls_md_free(&context);
return true;
}
NAX::NAX(VirtualFile file_)
: header(std::make_unique<NAXHeader>()),
file(std::move(file_)), keys{Core::Crypto::KeyManager::Instance()} {
std::string path = Common::FS::SanitizePath(file->GetFullPath());
2018-08-16 23:08:13 +02:00
static const std::regex nax_path_regex("/registered/(000000[0-9A-F]{2})/([0-9A-F]{32})\\.nca",
std::regex_constants::ECMAScript |
std::regex_constants::icase);
std::smatch match;
if (!std::regex_search(path, match, nax_path_regex)) {
status = Loader::ResultStatus::ErrorBadNAXFilePath;
return;
}
const std::string two_dir = Common::ToUpper(match[1]);
const std::string nca_id = Common::ToLower(match[2]);
2018-08-16 23:08:13 +02:00
status = Parse(fmt::format("/registered/{}/{}.nca", two_dir, nca_id));
}
NAX::NAX(VirtualFile file_, std::array<u8, 0x10> nca_id)
: header(std::make_unique<NAXHeader>()),
file(std::move(file_)), keys{Core::Crypto::KeyManager::Instance()} {
2018-08-16 23:08:13 +02:00
Core::Crypto::SHA256Hash hash{};
mbedtls_sha256_ret(nca_id.data(), nca_id.size(), hash.data(), 0);
2018-08-16 23:08:13 +02:00
status = Parse(fmt::format("/registered/000000{:02X}/{}.nca", hash[0],
Common::HexToString(nca_id, false)));
2018-08-16 23:08:13 +02:00
}
NAX::~NAX() = default;
2018-08-19 03:14:57 +02:00
Loader::ResultStatus NAX::Parse(std::string_view path) {
if (file == nullptr) {
return Loader::ResultStatus::ErrorNullFile;
}
if (file->ReadObject(header.get()) != sizeof(NAXHeader)) {
2018-08-16 23:08:13 +02:00
return Loader::ResultStatus::ErrorBadNAXHeader;
}
if (header->magic != Common::MakeMagic('N', 'A', 'X', '0')) {
2018-08-16 23:08:13 +02:00
return Loader::ResultStatus::ErrorBadNAXHeader;
}
if (file->GetSize() < NAX_HEADER_PADDING_SIZE + header->file_size) {
2018-08-16 23:08:13 +02:00
return Loader::ResultStatus::ErrorIncorrectNAXFileSize;
}
2018-08-16 23:08:13 +02:00
keys.DeriveSDSeedLazy();
std::array<Core::Crypto::Key256, 2> sd_keys{};
const auto sd_keys_res = Core::Crypto::DeriveSDKeys(sd_keys, keys);
if (sd_keys_res != Loader::ResultStatus::Success) {
return sd_keys_res;
}
const auto enc_keys = header->key_area;
std::size_t i = 0;
2018-08-19 03:14:57 +02:00
for (; i < sd_keys.size(); ++i) {
2018-08-16 23:08:13 +02:00
std::array<Core::Crypto::Key128, 2> nax_keys{};
if (!CalculateHMAC256(nax_keys.data(), sd_keys[i].data(), 0x10, path.data(), path.size())) {
2018-08-16 23:08:13 +02:00
return Loader::ResultStatus::ErrorNAXKeyHMACFailed;
}
for (std::size_t j = 0; j < nax_keys.size(); ++j) {
2018-08-16 23:08:13 +02:00
Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(nax_keys[j],
Core::Crypto::Mode::ECB);
cipher.Transcode(enc_keys[j].data(), 0x10, header->key_area[j].data(),
Core::Crypto::Op::Decrypt);
}
Core::Crypto::SHA256Hash validation{};
if (!CalculateHMAC256(validation.data(), &header->magic, 0x60, sd_keys[i].data() + 0x10,
0x10)) {
return Loader::ResultStatus::ErrorNAXValidationHMACFailed;
}
if (header->hmac == validation)
break;
}
if (i == 2) {
return Loader::ResultStatus::ErrorNAXKeyDerivationFailed;
}
type = static_cast<NAXContentType>(i);
Core::Crypto::Key256 final_key{};
2018-08-19 03:14:57 +02:00
std::memcpy(final_key.data(), &header->key_area, final_key.size());
const auto enc_file =
std::make_shared<OffsetVfsFile>(file, header->file_size, NAX_HEADER_PADDING_SIZE);
2018-08-16 23:08:13 +02:00
dec_file = std::make_shared<Core::Crypto::XTSEncryptionLayer>(enc_file, final_key);
return Loader::ResultStatus::Success;
}
2018-08-19 03:14:57 +02:00
Loader::ResultStatus NAX::GetStatus() const {
2018-08-16 23:08:13 +02:00
return status;
}
2018-08-19 03:14:57 +02:00
VirtualFile NAX::GetDecrypted() const {
2018-08-16 23:08:13 +02:00
return dec_file;
}
std::unique_ptr<NCA> NAX::AsNCA() const {
2018-08-16 23:08:13 +02:00
if (type == NAXContentType::NCA)
return std::make_unique<NCA>(GetDecrypted());
2018-08-16 23:08:13 +02:00
return nullptr;
}
2018-08-19 03:14:57 +02:00
NAXContentType NAX::GetContentType() const {
2018-08-16 23:08:13 +02:00
return type;
}
std::vector<VirtualFile> NAX::GetFiles() const {
2018-08-16 23:08:13 +02:00
return {dec_file};
}
std::vector<VirtualDir> NAX::GetSubdirectories() const {
2018-08-16 23:08:13 +02:00
return {};
}
std::string NAX::GetName() const {
return file->GetName();
}
VirtualDir NAX::GetParentDirectory() const {
2018-08-16 23:08:13 +02:00
return file->GetContainingDirectory();
}
} // namespace FileSys