nstool/src/NcaProcess.h

139 lines
3.3 KiB
C
Raw Normal View History

2019-01-31 09:10:19 +00:00
#pragma once
#include "types.h"
#include "KeyBag.h"
2021-10-15 09:29:29 +00:00
#include "FsProcess.h"
2019-01-31 09:10:19 +00:00
2022-06-29 13:19:36 +00:00
#include <pietendo/hac/ContentArchiveHeader.h>
#include <pietendo/hac/HierarchicalIntegrityHeader.h>
#include <pietendo/hac/HierarchicalSha256Header.h>
2019-01-31 09:10:19 +00:00
namespace nstool {
2019-01-31 09:10:19 +00:00
class NcaProcess
{
public:
NcaProcess();
void process();
// generic
void setInputFile(const std::shared_ptr<tc::io::IStream>& file);
void setKeyCfg(const KeyBag& keycfg);
2019-01-31 09:10:19 +00:00
void setCliOutputMode(CliOutputMode type);
void setVerifyMode(bool verify);
2023-01-21 12:03:21 +00:00
void setBaseNcaPath(const tc::Optional<tc::io::Path>& nca_path);
2023-01-12 22:33:08 +00:00
2019-01-31 09:10:19 +00:00
2021-10-15 09:29:29 +00:00
// fs specific
void setShowFsTree(bool show_fs_tree);
void setFsRootLabel(const std::string& root_label);
void setExtractJobs(const std::vector<nstool::ExtractJob>& extract_jobs);
2019-01-31 09:10:19 +00:00
2021-10-15 09:29:29 +00:00
// post process() get FS out
2022-06-29 13:19:36 +00:00
const std::shared_ptr<tc::io::IFileSystem>& getFileSystem() const;
2019-01-31 09:10:19 +00:00
private:
2021-10-15 09:29:29 +00:00
const std::string kNpdmExefsPath = "/main.npdm";
std::string mModuleName;
2019-01-31 09:10:19 +00:00
// user options
std::shared_ptr<tc::io::IStream> mFile;
KeyBag mKeyCfg;
2019-01-31 09:10:19 +00:00
CliOutputMode mCliOutputMode;
bool mVerify;
2023-01-21 12:03:21 +00:00
tc::Optional<tc::io::Path> mBaseNcaPath;
2019-01-31 09:10:19 +00:00
2021-10-15 09:29:29 +00:00
// fs processing
2022-06-29 13:19:36 +00:00
std::shared_ptr<tc::io::IFileSystem> mFileSystem;
2021-10-15 09:29:29 +00:00
FsProcess mFsProcess;
2019-01-31 09:10:19 +00:00
2021-10-15 09:29:29 +00:00
// nca data
2022-06-29 13:19:36 +00:00
pie::hac::sContentArchiveHeaderBlock mHdrBlock;
pie::hac::detail::sha256_hash_t mHdrHash;
pie::hac::ContentArchiveHeader mHdr;
2019-01-31 09:10:19 +00:00
// crypto
struct sKeys
{
struct sKeyAreaKey
{
byte_t index;
bool decrypted;
KeyBag::aes128_key_t enc;
KeyBag::aes128_key_t dec;
2019-01-31 09:10:19 +00:00
void operator=(const sKeyAreaKey& other)
{
index = other.index;
decrypted = other.decrypted;
enc = other.enc;
dec = other.dec;
}
bool operator==(const sKeyAreaKey& other) const
{
return (index == other.index) \
&& (decrypted == other.decrypted) \
&& (enc == other.enc) \
&& (dec == other.dec);
}
bool operator!=(const sKeyAreaKey& other) const
{
return !(*this == other);
}
};
std::vector<sKeyAreaKey> kak_list;
2019-01-31 09:10:19 +00:00
2022-06-29 13:19:36 +00:00
tc::Optional<pie::hac::detail::aes128_key_t> aes_ctr;
2019-01-31 09:10:19 +00:00
} mContentKey;
2021-10-16 08:40:14 +00:00
struct SparseInfo
{
};
2021-10-15 09:29:29 +00:00
// raw partition data
2019-01-31 09:10:19 +00:00
struct sPartitionInfo
{
std::shared_ptr<tc::io::IStream> raw_reader; // raw unprocessed partition stream
std::shared_ptr<tc::io::IStream> decrypt_reader; // partition stream with transparent decryption
std::shared_ptr<tc::io::IStream> reader; // partition stream with transparent decryption & hash layer processing
2022-06-29 13:19:36 +00:00
tc::io::VirtualFileSystem::FileSystemSnapshot fs_snapshot;
std::shared_ptr<tc::io::IFileSystem> fs_reader;
2019-01-31 09:10:19 +00:00
std::string fail_reason;
int64_t offset;
int64_t size;
2019-01-31 09:10:19 +00:00
// meta data
2022-06-29 13:19:36 +00:00
pie::hac::nca::FormatType format_type;
pie::hac::nca::HashType hash_type;
pie::hac::nca::EncryptionType enc_type;
pie::hac::nca::MetaDataHashType metadata_hash_type;
2021-10-16 06:13:11 +00:00
// hash meta data
2022-06-29 13:19:36 +00:00
pie::hac::HierarchicalIntegrityHeader hierarchicalintegrity_hdr;
pie::hac::HierarchicalSha256Header hierarchicalsha256_hdr;
2021-10-16 06:13:11 +00:00
// crypto metadata
2022-06-29 13:19:36 +00:00
pie::hac::detail::aes_iv_t aes_ctr;
2021-10-16 08:40:14 +00:00
// sparse metadata
SparseInfo sparse_info;
2021-10-15 09:29:29 +00:00
};
2022-06-29 13:19:36 +00:00
std::array<sPartitionInfo, pie::hac::nca::kPartitionNum> mPartitions;
2019-01-31 09:10:19 +00:00
void importHeader();
void generateNcaBodyEncryptionKeys();
void generatePartitionConfiguration();
void validateNcaSignatures();
void displayHeader();
void processPartitions();
2023-01-12 22:33:08 +00:00
NcaProcess readBaseNCA();
2022-06-29 13:19:36 +00:00
std::string getContentTypeForMountStr(pie::hac::nca::ContentType cont_type) const;
};
}