Move EncryptionType to NCCHContainer

This commit is contained in:
Pengfei
2021-08-09 11:16:18 +08:00
parent f22ecacb7c
commit b331f50b91
3 changed files with 951 additions and 950 deletions
File diff suppressed because it is too large Load Diff
+330 -320
View File
@@ -1,320 +1,330 @@
// Copyright 2017 Citra Emulator Project / 2019 threeSD Project // Copyright 2017 Citra Emulator Project / 2019 threeSD Project
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#pragma once #pragma once
#include <cstddef> #include <cstddef>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "common/bit_field.h" #include "common/bit_field.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/file_util.h" #include "common/file_util.h"
#include "common/progress_callback.h" #include "common/progress_callback.h"
#include "common/swap.h" #include "common/swap.h"
#include "core/sdmc_decryptor.h" #include "core/sdmc_decryptor.h"
namespace Core { namespace Core {
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
/// NCCH (Nintendo Content Container Header) header /// NCCH (Nintendo Content Container Header) header
struct NCCH_Header { struct NCCH_Header {
u8 signature[0x100]; u8 signature[0x100];
u32_le magic; u32_le magic;
u32_le content_size; u32_le content_size;
u8 partition_id[8]; u8 partition_id[8];
u16_le maker_code; u16_le maker_code;
u16_le version; u16_le version;
u8 reserved_0[4]; u8 reserved_0[4];
u64_le program_id; u64_le program_id;
u8 reserved_1[0x10]; u8 reserved_1[0x10];
u8 logo_region_hash[0x20]; u8 logo_region_hash[0x20];
u8 product_code[0x10]; u8 product_code[0x10];
u8 extended_header_hash[0x20]; u8 extended_header_hash[0x20];
u32_le extended_header_size; u32_le extended_header_size;
u8 reserved_2[4]; u8 reserved_2[4];
u8 reserved_flag[3]; u8 reserved_flag[3];
u8 secondary_key_slot; u8 secondary_key_slot;
u8 platform; u8 platform;
enum class ContentType : u8 { enum class ContentType : u8 {
Application = 0, Application = 0,
SystemUpdate = 1, SystemUpdate = 1,
Manual = 2, Manual = 2,
Child = 3, Child = 3,
Trial = 4, Trial = 4,
}; };
union { union {
BitField<0, 1, u8> is_data; BitField<0, 1, u8> is_data;
BitField<1, 1, u8> is_executable; BitField<1, 1, u8> is_executable;
BitField<2, 3, ContentType> content_type; BitField<2, 3, ContentType> content_type;
}; };
u8 content_unit_size; u8 content_unit_size;
union { union {
BitField<0, 1, u8> fixed_key; BitField<0, 1, u8> fixed_key;
BitField<1, 1, u8> no_romfs; BitField<1, 1, u8> no_romfs;
BitField<2, 1, u8> no_crypto; BitField<2, 1, u8> no_crypto;
BitField<5, 1, u8> seed_crypto; BitField<5, 1, u8> seed_crypto;
u8 raw_crypto_flags; u8 raw_crypto_flags;
}; };
u32_le plain_region_offset; u32_le plain_region_offset;
u32_le plain_region_size; u32_le plain_region_size;
u32_le logo_region_offset; u32_le logo_region_offset;
u32_le logo_region_size; u32_le logo_region_size;
u32_le exefs_offset; u32_le exefs_offset;
u32_le exefs_size; u32_le exefs_size;
u32_le exefs_hash_region_size; u32_le exefs_hash_region_size;
u8 reserved_3[4]; u8 reserved_3[4];
u32_le romfs_offset; u32_le romfs_offset;
u32_le romfs_size; u32_le romfs_size;
u32_le romfs_hash_region_size; u32_le romfs_hash_region_size;
u8 reserved_4[4]; u8 reserved_4[4];
u8 exefs_super_block_hash[0x20]; u8 exefs_super_block_hash[0x20];
u8 romfs_super_block_hash[0x20]; u8 romfs_super_block_hash[0x20];
}; };
static_assert(sizeof(NCCH_Header) == 0x200, "NCCH header structure size is wrong"); static_assert(sizeof(NCCH_Header) == 0x200, "NCCH header structure size is wrong");
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// ExeFS (executable file system) headers // ExeFS (executable file system) headers
struct ExeFs_SectionHeader { struct ExeFs_SectionHeader {
char name[8]; char name[8];
u32 offset; u32 offset;
u32 size; u32 size;
}; };
struct ExeFs_Header { struct ExeFs_Header {
ExeFs_SectionHeader section[8]; ExeFs_SectionHeader section[8];
u8 reserved[0x80]; u8 reserved[0x80];
u8 hashes[8][0x20]; u8 hashes[8][0x20];
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// ExHeader (executable file system header) headers // ExHeader (executable file system header) headers
struct ExHeader_SystemInfoFlags { struct ExHeader_SystemInfoFlags {
u8 reserved[5]; u8 reserved[5];
u8 flag; u8 flag;
u8 remaster_version[2]; u8 remaster_version[2];
}; };
struct ExHeader_CodeSegmentInfo { struct ExHeader_CodeSegmentInfo {
u32 address; u32 address;
u32 num_max_pages; u32 num_max_pages;
u32 code_size; u32 code_size;
}; };
struct ExHeader_CodeSetInfo { struct ExHeader_CodeSetInfo {
u8 name[8]; u8 name[8];
ExHeader_SystemInfoFlags flags; ExHeader_SystemInfoFlags flags;
ExHeader_CodeSegmentInfo text; ExHeader_CodeSegmentInfo text;
u32 stack_size; u32 stack_size;
ExHeader_CodeSegmentInfo ro; ExHeader_CodeSegmentInfo ro;
u8 reserved[4]; u8 reserved[4];
ExHeader_CodeSegmentInfo data; ExHeader_CodeSegmentInfo data;
u32 bss_size; u32 bss_size;
}; };
struct ExHeader_DependencyList { struct ExHeader_DependencyList {
u8 program_id[0x30][8]; u8 program_id[0x30][8];
}; };
struct ExHeader_SystemInfo { struct ExHeader_SystemInfo {
u64 save_data_size; u64 save_data_size;
u64_le jump_id; u64_le jump_id;
u8 reserved_2[0x30]; u8 reserved_2[0x30];
}; };
struct ExHeader_StorageInfo { struct ExHeader_StorageInfo {
union { union {
u64_le ext_save_data_id; u64_le ext_save_data_id;
// When using extended savedata access // When using extended savedata access
// Prefer the ID specified in the most significant bits // Prefer the ID specified in the most significant bits
BitField<40, 20, u64> extdata_id3; BitField<40, 20, u64> extdata_id3;
BitField<20, 20, u64> extdata_id4; BitField<20, 20, u64> extdata_id4;
BitField<0, 20, u64> extdata_id5; BitField<0, 20, u64> extdata_id5;
}; };
u8 system_save_data_id[8]; u8 system_save_data_id[8];
union { union {
u64_le storage_accessible_unique_ids; u64_le storage_accessible_unique_ids;
// When using extended savedata access // When using extended savedata access
// Prefer the ID specified in the most significant bits // Prefer the ID specified in the most significant bits
BitField<40, 20, u64> extdata_id0; BitField<40, 20, u64> extdata_id0;
BitField<20, 20, u64> extdata_id1; BitField<20, 20, u64> extdata_id1;
BitField<0, 20, u64> extdata_id2; BitField<0, 20, u64> extdata_id2;
}; };
u8 access_info[7]; u8 access_info[7];
u8 other_attributes; u8 other_attributes;
}; };
struct ExHeader_ARM11_SystemLocalCaps { struct ExHeader_ARM11_SystemLocalCaps {
u64_le program_id; u64_le program_id;
u32_le core_version; u32_le core_version;
u8 reserved_flags[2]; u8 reserved_flags[2];
union { union {
u8 flags0; u8 flags0;
BitField<0, 2, u8> ideal_processor; BitField<0, 2, u8> ideal_processor;
BitField<2, 2, u8> affinity_mask; BitField<2, 2, u8> affinity_mask;
BitField<4, 4, u8> system_mode; BitField<4, 4, u8> system_mode;
}; };
u8 priority; u8 priority;
u8 resource_limit_descriptor[0x10][2]; u8 resource_limit_descriptor[0x10][2];
ExHeader_StorageInfo storage_info; ExHeader_StorageInfo storage_info;
u8 service_access_control[0x20][8]; u8 service_access_control[0x20][8];
u8 ex_service_access_control[0x2][8]; u8 ex_service_access_control[0x2][8];
u8 reserved[0xf]; u8 reserved[0xf];
u8 resource_limit_category; u8 resource_limit_category;
}; };
struct ExHeader_ARM11_KernelCaps { struct ExHeader_ARM11_KernelCaps {
u32_le descriptors[28]; u32_le descriptors[28];
u8 reserved[0x10]; u8 reserved[0x10];
}; };
struct ExHeader_ARM9_AccessControl { struct ExHeader_ARM9_AccessControl {
u8 descriptors[15]; u8 descriptors[15];
u8 descversion; u8 descversion;
}; };
struct ExHeader_Header { struct ExHeader_Header {
ExHeader_CodeSetInfo codeset_info; ExHeader_CodeSetInfo codeset_info;
ExHeader_DependencyList dependency_list; ExHeader_DependencyList dependency_list;
ExHeader_SystemInfo system_info; ExHeader_SystemInfo system_info;
ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps; ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps;
ExHeader_ARM11_KernelCaps arm11_kernel_caps; ExHeader_ARM11_KernelCaps arm11_kernel_caps;
ExHeader_ARM9_AccessControl arm9_access_control; ExHeader_ARM9_AccessControl arm9_access_control;
struct { struct {
u8 signature[0x100]; u8 signature[0x100];
u8 ncch_public_key_modulus[0x100]; u8 ncch_public_key_modulus[0x100];
ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps; ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps;
ExHeader_ARM11_KernelCaps arm11_kernel_caps; ExHeader_ARM11_KernelCaps arm11_kernel_caps;
ExHeader_ARM9_AccessControl arm9_access_control; ExHeader_ARM9_AccessControl arm9_access_control;
} access_desc; } access_desc;
}; };
static_assert(sizeof(ExHeader_Header) == 0x800, "ExHeader structure size is wrong"); static_assert(sizeof(ExHeader_Header) == 0x800, "ExHeader structure size is wrong");
enum class EncryptionType; /**
* Encryption type of an importable content.
/** */
* Helper which implements an interface to deal with NCCH containers which can enum class EncryptionType {
* contain ExeFS archives or RomFS archives for games or other applications. None,
* FixedKey,
* Note that this is heavily stripped down and can only read (primary-key NCCHSecure1,
* encrypted non-code sections of) ExeFS and ExHeader by design. NCCHSecure2,
*/ NCCHSecure3,
class NCCHContainer { NCCHSecure4,
public: };
NCCHContainer(std::shared_ptr<FileUtil::IOFile> file);
NCCHContainer() {} /**
* Helper which implements an interface to deal with NCCH containers which can
bool OpenFile(std::shared_ptr<FileUtil::IOFile> file); * contain ExeFS archives or RomFS archives for games or other applications.
*
/** * Note that this is heavily stripped down and can only read (primary-key
* Ensure ExeFS and exheader is loaded and ready for reading sections * encrypted non-code sections of) ExeFS and ExHeader by design.
*/ */
bool Load(); class NCCHContainer {
public:
/** NCCHContainer(std::shared_ptr<FileUtil::IOFile> file);
* Reads an application ExeFS section of an NCCH file (non-compressed, primary key only) NCCHContainer() {}
* @param name Name of section to read out of NCCH file
* @param buffer Vector to read data into bool OpenFile(std::shared_ptr<FileUtil::IOFile> file);
*/
bool LoadSectionExeFS(const char* name, std::vector<u8>& buffer); /**
* Ensure ExeFS and exheader is loaded and ready for reading sections
/** */
* Get the Program ID of the NCCH container bool Load();
*/
bool ReadProgramId(u64_le& program_id); /**
* Reads an application ExeFS section of an NCCH file (non-compressed, primary key only)
/** * @param name Name of section to read out of NCCH file
* Get the Extdata ID of the NCCH container * @param buffer Vector to read data into
*/ */
bool ReadExtdataId(u64& extdata_id); bool LoadSectionExeFS(const char* name, std::vector<u8>& buffer);
/** /**
* Checks whether the NCCH container contains an ExeFS * Get the Program ID of the NCCH container
* @return bool check result */
*/ bool ReadProgramId(u64_le& program_id);
bool HasExeFS();
/**
/** * Get the Extdata ID of the NCCH container
* Checks whether the NCCH container contains an ExHeader */
* @return bool check result bool ReadExtdataId(u64& extdata_id);
*/
bool HasExHeader(); /**
* Checks whether the NCCH container contains an ExeFS
/** * @return bool check result
* Reads the name of the codeset. */
*/ bool HasExeFS();
bool ReadCodesetName(std::string& name);
/**
/** * Checks whether the NCCH container contains an ExHeader
* Reads the product code. * @return bool check result
*/ */
bool ReadProductCode(std::string& name); bool HasExHeader();
/** /**
* Gets encryption type (which key is used). * Reads the name of the codeset.
*/ */
bool ReadEncryptionType(EncryptionType& encryption); bool ReadCodesetName(std::string& name);
/** /**
* Gets whether seed crypto is used. * Reads the product code.
*/ */
bool ReadSeedCrypto(bool& used); bool ReadProductCode(std::string& name);
/** /**
* Decrypts this NCCH and write to the destination file. * Gets encryption type (which key is used).
*/ */
bool DecryptToFile( bool ReadEncryptionType(EncryptionType& encryption);
std::shared_ptr<FileUtil::IOFile> dest_file,
const Common::ProgressCallback& callback = [](u64, u64) {}); /**
* Gets whether seed crypto is used.
/** */
* Aborts DecryptToFile. Simply aborts the decryptor. bool ReadSeedCrypto(bool& used);
*/
void AbortDecryptToFile(); /**
* Decrypts this NCCH and write to the destination file.
NCCH_Header ncch_header; */
ExHeader_Header exheader_header; bool DecryptToFile(
ExeFs_Header exefs_header; std::shared_ptr<FileUtil::IOFile> dest_file,
const Common::ProgressCallback& callback = [](u64, u64) {});
private:
bool has_exheader = false; /**
bool has_exefs = false; * Aborts DecryptToFile. Simply aborts the decryptor.
bool has_romfs = false; */
void AbortDecryptToFile();
bool is_loaded = false;
NCCH_Header ncch_header;
bool is_encrypted = false; ExHeader_Header exheader_header;
// for decrypting exheader, exefs header and icon/banner section ExeFs_Header exefs_header;
std::array<u8, 16> primary_key{};
std::array<u8, 16> secondary_key{}; // for decrypting romfs and .code section private:
std::array<u8, 16> exheader_ctr{}; bool has_exheader = false;
std::array<u8, 16> exefs_ctr{}; bool has_exefs = false;
std::array<u8, 16> romfs_ctr{}; bool has_romfs = false;
u32 exefs_offset = 0; bool is_loaded = false;
std::string root_folder; bool is_encrypted = false;
std::string filepath; // for decrypting exheader, exefs header and icon/banner section
std::shared_ptr<FileUtil::IOFile> file; std::array<u8, 16> primary_key{};
std::shared_ptr<FileUtil::IOFile> exefs_file; std::array<u8, 16> secondary_key{}; // for decrypting romfs and .code section
std::array<u8, 16> exheader_ctr{};
// Used for DecryptToFile std::array<u8, 16> exefs_ctr{};
FileDecryptor decryptor; std::array<u8, 16> romfs_ctr{};
std::atomic_bool aborted{false};
u32 exefs_offset = 0;
friend class CIABuilder;
}; std::string root_folder;
std::string filepath;
/** std::shared_ptr<FileUtil::IOFile> file;
* Extracts the shared RomFS from a NCCH image. std::shared_ptr<FileUtil::IOFile> exefs_file;
* Used for handling system archives.
*/ // Used for DecryptToFile
std::vector<u8> LoadSharedRomFS(const std::vector<u8>& data); FileDecryptor decryptor;
std::atomic_bool aborted{false};
} // namespace Core
friend class CIABuilder;
};
/**
* Extracts the shared RomFS from a NCCH image.
* Used for handling system archives.
*/
std::vector<u8> LoadSharedRomFS(const std::vector<u8>& data);
} // namespace Core
+4 -12
View File
@@ -46,18 +46,6 @@ constexpr bool IsNandTitle(ContentType type) {
return type == ContentType::SystemTitle || type == ContentType::SystemApplet; return type == ContentType::SystemTitle || type == ContentType::SystemApplet;
} }
/**
* Encryption type of an importable content.
*/
enum class EncryptionType {
None,
FixedKey,
NCCHSecure1,
NCCHSecure2,
NCCHSecure3,
NCCHSecure4,
};
/** /**
* Struct that specifies an importable content. * Struct that specifies an importable content.
*/ */
@@ -187,6 +175,10 @@ public:
bool LoadTMD(ContentType type, u64 id, TitleMetadata& out) const; bool LoadTMD(ContentType type, u64 id, TitleMetadata& out) const;
bool LoadTMD(const ContentSpecifier& specifier, TitleMetadata& out) const; bool LoadTMD(const ContentSpecifier& specifier, TitleMetadata& out) const;
std::string GetTitleContentsPath(const ContentSpecifier& specifier) const;
std::shared_ptr<FileUtil::IOFile> OpenBootContent(const ContentSpecifier& specifier,
const TitleMetadata& tmd) const;
std::shared_ptr<TicketDB>& GetTicketDB() { std::shared_ptr<TicketDB>& GetTicketDB() {
return ticket_db; return ticket_db;
} }