From 3a1ef48b64cf55b24ce475779d529ebae7ab4518 Mon Sep 17 00:00:00 2001 From: jakcron Date: Sat, 30 Jun 2018 00:14:19 +0800 Subject: [PATCH] [nstool] Add support for detecting and specifying ticket and cert files. --- programs/nstool/makefile | 2 +- programs/nstool/source/UserSettings.cpp | 57 ++++++++++++++++++++++++- programs/nstool/source/UserSettings.h | 2 + programs/nstool/source/nstool.h | 2 + 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/programs/nstool/makefile b/programs/nstool/makefile index 85aa304..517a561 100644 --- a/programs/nstool/makefile +++ b/programs/nstool/makefile @@ -3,7 +3,7 @@ SRC_DIR = source OBJS = $(foreach dir,$(SRC_DIR),$(subst .cpp,.o,$(wildcard $(dir)/*.cpp))) $(foreach dir,$(SRC_DIR),$(subst .c,.o,$(wildcard $(dir)/*.c))) # External dependencies -DEPENDS = nx-hb nx crypto compress fnd +DEPENDS = nx-hb nx es crypto compress fnd LIB_DIR = ../../lib LIBS = $(foreach dep,$(DEPENDS), -L"$(LIB_DIR)/lib$(dep)" -l$(dep)) INCS = $(foreach dep,$(DEPENDS), -I"$(LIB_DIR)/lib$(dep)/include") diff --git a/programs/nstool/source/UserSettings.cpp b/programs/nstool/source/UserSettings.cpp index ce6975a..ca5a65a 100644 --- a/programs/nstool/source/UserSettings.cpp +++ b/programs/nstool/source/UserSettings.cpp @@ -22,6 +22,7 @@ #include #include #include +#include UserSettings::UserSettings() {} @@ -43,7 +44,7 @@ void UserSettings::showHelp() printf("\n General Options:\n"); printf(" -d, --dev Use devkit keyset\n"); printf(" -k, --keyset Specify keyset file\n"); - printf(" -t, --type Specify input file type [xci, pfs, romfs, nca, npdm, cnmt, nso, nro, nacp, aset]\n"); + printf(" -t, --type Specify input file type [xci, pfs, romfs, nca, npdm, cnmt, nso, nro, nacp, aset, cert, tik]\n"); printf(" -y, --verify Verify file\n"); printf("\n Output Options:\n"); printf(" --showkeys Show keys generated\n"); @@ -695,6 +696,10 @@ FileType UserSettings::getFileTypeFromString(const std::string& type_str) type = FILE_NRO; else if (str == "nacp") type = FILE_NACP; + else if (str == "cert") + type = FILE_ES_CERT; + else if (str == "tik") + type = FILE_ES_TIK; else if (str == "aset" || str == "asset") type = FILE_HB_ASSET; else @@ -753,6 +758,12 @@ FileType UserSettings::determineFileTypeFromFile(const std::string& path) // test nso else if (_ASSERT_SIZE(sizeof(nx::sNroHeader)) && _TYPE_PTR(nx::sNroHeader)->st_magic.get() == nx::nro::kNroStructMagic) file_type = FILE_NRO; + // test es certificate + else if (determineValidEsCertFromSample(scratch)) + file_type = FILE_ES_CERT; + // test es ticket + else if (determineValidEsTikFromSample(scratch)) + file_type = FILE_ES_TIK; // test hb asset else if (_ASSERT_SIZE(sizeof(nx::sAssetHeader)) && _TYPE_PTR(nx::sAssetHeader)->st_magic.get() == nx::aset::kAssetStructMagic) file_type = FILE_HB_ASSET; @@ -855,6 +866,50 @@ bool UserSettings::determineValidNacpFromSample(const fnd::Vec& sample) return true; } +bool UserSettings::determineValidEsCertFromSample(const fnd::Vec& sample) const +{ + es::SignatureBlock sign; + + try + { + sign.fromBytes(sample.data(), sample.size()); + } + catch (...) + { + return false; + } + + if (sign.isLittleEndian() == true) + return false; + + if (sign.getSignType() != es::sign::SIGN_RSA4096_SHA256 && sign.getSignType() != es::sign::SIGN_RSA2048_SHA256 && sign.getSignType() != es::sign::SIGN_ECDSA240_SHA256) + return false; + + return true; +} + +bool UserSettings::determineValidEsTikFromSample(const fnd::Vec& sample) const +{ + es::SignatureBlock sign; + + try + { + sign.fromBytes(sample.data(), sample.size()); + } + catch (...) + { + return false; + } + + if (sign.isLittleEndian() == false) + return false; + + if (sign.getSignType() != es::sign::SIGN_RSA2048_SHA256) + return false; + + return true; +} + nx::npdm::InstructionType UserSettings::getInstructionTypeFromString(const std::string & type_str) { std::string str = type_str; diff --git a/programs/nstool/source/UserSettings.h b/programs/nstool/source/UserSettings.h index d7c27b9..c851647 100644 --- a/programs/nstool/source/UserSettings.h +++ b/programs/nstool/source/UserSettings.h @@ -106,5 +106,7 @@ private: bool determineValidNcaFromSample(const fnd::Vec& sample) const; bool determineValidCnmtFromSample(const fnd::Vec& sample) const; bool determineValidNacpFromSample(const fnd::Vec& sample) const; + bool determineValidEsCertFromSample(const fnd::Vec& sample) const; + bool determineValidEsTikFromSample(const fnd::Vec& sample) const; nx::npdm::InstructionType getInstructionTypeFromString(const std::string& type_str); }; \ No newline at end of file diff --git a/programs/nstool/source/nstool.h b/programs/nstool/source/nstool.h index cf6505c..7752ffa 100644 --- a/programs/nstool/source/nstool.h +++ b/programs/nstool/source/nstool.h @@ -27,6 +27,8 @@ enum FileType FILE_NSO, FILE_NRO, FILE_NACP, + FILE_ES_CERT, + FILE_ES_TIK, FILE_HB_ASSET, FILE_INVALID = -1, };