mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2024-12-22 12:21:18 +00:00
ro: implement NRR signature checks
This commit is contained in:
parent
f3629f863d
commit
5fa534fb71
3 changed files with 185 additions and 17 deletions
|
@ -31,18 +31,27 @@ namespace ams::ro {
|
|||
};
|
||||
static_assert(sizeof(ModuleId) == sizeof(LoaderModuleInfo::build_id), "ModuleId definition!");
|
||||
|
||||
struct NrrCertification {
|
||||
static constexpr size_t RsaKeySize = 0x100;
|
||||
static constexpr size_t SignedSize = 0x120;
|
||||
|
||||
u64 program_id_mask;
|
||||
u64 program_id_pattern;
|
||||
u8 reserved_10[0x10];
|
||||
u8 modulus[RsaKeySize];
|
||||
u8 signature[RsaKeySize];
|
||||
};
|
||||
static_assert(sizeof(NrrCertification) == NrrCertification::RsaKeySize + NrrCertification::SignedSize);
|
||||
|
||||
class NrrHeader {
|
||||
public:
|
||||
static constexpr u32 Magic = util::FourCC<'N','R','R','0'>::Code;
|
||||
private:
|
||||
u32 magic;
|
||||
u8 reserved_04[0xC];
|
||||
u64 program_id_mask;
|
||||
u64 program_id_pattern;
|
||||
u8 reserved_20[0x10];
|
||||
u8 modulus[0x100];
|
||||
u8 fixed_key_signature[0x100];
|
||||
u8 nrr_signature[0x100];
|
||||
u32 key_generation;
|
||||
u8 reserved_08[0x08];
|
||||
NrrCertification certification;
|
||||
u8 signature[0x100];
|
||||
ncm::ProgramId program_id;
|
||||
u32 size;
|
||||
u8 type; /* 7.0.0+ */
|
||||
|
@ -56,7 +65,7 @@ namespace ams::ro {
|
|||
}
|
||||
|
||||
bool IsProgramIdValid() const {
|
||||
return (static_cast<u64>(this->program_id) & this->program_id_mask) == this->program_id_pattern;
|
||||
return (static_cast<u64>(this->program_id) & this->certification.program_id_mask) == this->certification.program_id_pattern;
|
||||
}
|
||||
|
||||
ModuleType GetType() const {
|
||||
|
@ -80,9 +89,39 @@ namespace ams::ro {
|
|||
uintptr_t GetHashes() const {
|
||||
return reinterpret_cast<uintptr_t>(this) + this->hashes_offset;
|
||||
}
|
||||
|
||||
u32 GetKeyGeneration() const {
|
||||
return this->key_generation;
|
||||
}
|
||||
|
||||
const u8 *GetCertificationSignature() const {
|
||||
return this->certification.signature;
|
||||
}
|
||||
|
||||
const u8 *GetCertificationSignedArea() const {
|
||||
return reinterpret_cast<const u8 *>(std::addressof(this->certification));
|
||||
}
|
||||
|
||||
const u8 *GetCertificationModulus() const {
|
||||
return this->certification.modulus;
|
||||
}
|
||||
|
||||
const u8 *GetSignature() const {
|
||||
return this->signature;
|
||||
}
|
||||
|
||||
const u8 *GetSignedArea() const {
|
||||
return reinterpret_cast<const u8 *>(std::addressof(this->program_id));
|
||||
}
|
||||
|
||||
size_t GetSignedAreaSize() const;
|
||||
};
|
||||
static_assert(sizeof(NrrHeader) == 0x350, "NrrHeader definition!");
|
||||
|
||||
inline size_t NrrHeader::GetSignedAreaSize() const {
|
||||
return this->size - OFFSETOF(NrrHeader, program_id);
|
||||
}
|
||||
|
||||
class NroHeader {
|
||||
public:
|
||||
static constexpr u32 Magic = util::FourCC<'N','R','O','0'>::Code;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
"title_id": "0x0100000000000037",
|
||||
"title_id_range_min": "0x0100000000000037",
|
||||
"title_id_range_max": "0x0100000000000037",
|
||||
"main_thread_stack_size": "0x00005000",
|
||||
"main_thread_stack_size": "0x00008000",
|
||||
"main_thread_priority": 49,
|
||||
"default_cpu_id": 3,
|
||||
"process_category": 0,
|
||||
|
|
|
@ -20,18 +20,138 @@ namespace ams::ro::impl {
|
|||
|
||||
namespace {
|
||||
|
||||
/* Helper functions. */
|
||||
Result ValidateNrrSignature(const NrrHeader *header) {
|
||||
/* TODO: Implement RSA-2048 PSS..... */
|
||||
constexpr size_t KeyGenerationMax100 = 0;
|
||||
constexpr size_t KeyGenerationMax910 = 1;
|
||||
constexpr size_t KeyGenerationMax = std::max(KeyGenerationMax100, KeyGenerationMax910);
|
||||
constexpr size_t KeyGenerationCount = KeyGenerationMax + 1;
|
||||
|
||||
/* TODO: Check PSS fixed-key signature. */
|
||||
R_UNLESS(true, ResultNotAuthorized());
|
||||
constexpr size_t RsaKeySize = 0x100;
|
||||
|
||||
constexpr const u8 Exponent[3] = { 0x01, 0x00, 0x01 };
|
||||
|
||||
constexpr const u8 DevModuli[KeyGenerationCount][RsaKeySize] = {
|
||||
{
|
||||
0xC1, 0x15, 0x7C, 0x02, 0x26, 0xE5, 0x35, 0x6F, 0x99, 0xDB, 0xBE, 0xBD, 0xD7, 0x01, 0x07, 0x1C,
|
||||
0xC2, 0x3D, 0x1E, 0x6B, 0x7E, 0x08, 0x07, 0xBC, 0xE2, 0x6D, 0x49, 0xEC, 0x0B, 0xFF, 0xE4, 0x91,
|
||||
0x8C, 0x62, 0xB9, 0xFC, 0x69, 0xBF, 0x3A, 0xB6, 0x6C, 0x3A, 0x5D, 0x0E, 0x31, 0x5E, 0x6C, 0x1D,
|
||||
0x9A, 0x21, 0xD9, 0x9D, 0xD3, 0xB8, 0x50, 0x5F, 0x27, 0x7C, 0x4A, 0xD2, 0xFE, 0xE8, 0xDA, 0x1C,
|
||||
0xB9, 0x9E, 0x7E, 0x1E, 0x2F, 0x7D, 0xF9, 0x70, 0xA2, 0x98, 0x19, 0x6A, 0x53, 0x40, 0x64, 0xE7,
|
||||
0xC0, 0x92, 0xAE, 0x64, 0xD2, 0x01, 0xB2, 0x49, 0x30, 0x19, 0x7F, 0xF8, 0x6E, 0x0D, 0x49, 0x35,
|
||||
0xE9, 0x95, 0x77, 0x00, 0x65, 0xC5, 0x1E, 0xF5, 0x2A, 0xF9, 0xA1, 0x52, 0xA0, 0xA4, 0xFA, 0x87,
|
||||
0x3D, 0x8F, 0x51, 0xEC, 0x02, 0x80, 0xA4, 0xC7, 0x22, 0x74, 0xEF, 0x56, 0x61, 0x71, 0x39, 0xE2,
|
||||
0x2F, 0x03, 0x82, 0xDB, 0x50, 0xE9, 0xCC, 0x60, 0x48, 0x46, 0x71, 0xE2, 0xC6, 0x71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 0x4A, 0x3F, 0xAE, 0x4F, 0xF3, 0x02, 0x71, 0x97, 0xD6, 0x78, 0xC6,
|
||||
0x8E, 0x4B, 0x41, 0x78, 0x5B, 0x56, 0x6F, 0x6C, 0xA0, 0x0D, 0x83, 0x28, 0x25, 0x07, 0x75, 0x15,
|
||||
}
|
||||
};
|
||||
|
||||
constexpr const u8 ProdModuli[KeyGenerationCount][RsaKeySize] = {
|
||||
{
|
||||
0xCF, 0x2F, 0x54, 0xF1, 0x7D, 0xA3, 0x11, 0x47, 0xC3, 0xBD, 0xED, 0x10, 0x3D, 0x64, 0xF8, 0x6B,
|
||||
0x51, 0x62, 0x05, 0x6F, 0x10, 0x5F, 0x50, 0x76, 0xF2, 0x40, 0x09, 0xA3, 0x16, 0xFC, 0xC3, 0xA8,
|
||||
0x75, 0xFD, 0x7C, 0xB4, 0x38, 0x9D, 0x30, 0x5C, 0x02, 0x6E, 0x2C, 0xCF, 0x03, 0x84, 0x23, 0x64,
|
||||
0x4A, 0xF9, 0x90, 0x44, 0x58, 0xAF, 0x43, 0x19, 0xD2, 0x27, 0xF1, 0x60, 0x28, 0xB8, 0x7B, 0x26,
|
||||
0xD3, 0x21, 0xE8, 0xF0, 0xED, 0x65, 0x01, 0x31, 0x51, 0x45, 0xD3, 0x93, 0xCF, 0x07, 0x65, 0xF1,
|
||||
0x8A, 0x72, 0x61, 0x55, 0x63, 0x91, 0x84, 0x63, 0x2C, 0x0B, 0x3E, 0xF6, 0x7E, 0x9B, 0x19, 0x4C,
|
||||
0x42, 0xE3, 0xB9, 0x83, 0xF3, 0xA1, 0x2B, 0x7B, 0xA1, 0x5F, 0x00, 0xDC, 0x80, 0x1A, 0x6D, 0x6C,
|
||||
0x22, 0x20, 0x2F, 0x19, 0x61, 0x96, 0x5E, 0xD9, 0x93, 0x45, 0x62, 0x48, 0xC1, 0xB1, 0x48, 0x8A,
|
||||
0x07, 0x6C, 0xBB, 0x3D, 0xCC, 0xE0, 0xEF, 0x43, 0x56, 0x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6, 0xFA, 0x33, 0xDB, 0x1C, 0xBC, 0x28, 0xE1, 0x85, 0x1E, 0x2F, 0xF9, 0xFD, 0xEA, 0x83, 0x52,
|
||||
0x2F, 0xEE, 0x07, 0x7C, 0x1E, 0x2F, 0xCD, 0x76, 0x91, 0xEA, 0x27, 0xE6, 0x50, 0x9A, 0x86, 0x23,
|
||||
}
|
||||
};
|
||||
|
||||
/* Helper functions. */
|
||||
|
||||
Result GetCertificationModulus(const u8 **out, u32 key_generation) {
|
||||
if (hos::GetVersion() >= hos::Version_910) {
|
||||
R_UNLESS(key_generation <= KeyGenerationMax910, ro::ResultNotAuthorized());
|
||||
} else {
|
||||
R_UNLESS(key_generation <= KeyGenerationMax100, ro::ResultNotAuthorized());
|
||||
}
|
||||
|
||||
*out = IsDevelopmentHardware() ? DevModuli[key_generation] : ProdModuli[key_generation];
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result ValidateNrrCertification(const NrrHeader *header, const u8 *mod) {
|
||||
static_assert(RsaKeySize == NrrCertification::RsaKeySize);
|
||||
|
||||
/* Verify the signature. */
|
||||
const u8 *sig = header->GetCertificationSignature();
|
||||
const size_t sig_size = RsaKeySize;
|
||||
const size_t mod_size = RsaKeySize;
|
||||
const u8 *exp = Exponent;
|
||||
const size_t exp_size = util::size(Exponent);
|
||||
const u8 *msg = header->GetCertificationSignedArea();
|
||||
const size_t msg_size = NrrCertification::SignedSize;
|
||||
R_UNLESS(crypto::VerifyRsa2048PssSha256(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size), ro::ResultNotAuthorized());
|
||||
|
||||
/* Check ProgramId pattern is valid. */
|
||||
R_UNLESS(header->IsProgramIdValid(), ResultNotAuthorized());
|
||||
|
||||
/* TODO: Check PSS signature over hashes. */
|
||||
R_UNLESS(true, ResultNotAuthorized());
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result ValidateNrrSignature(const NrrHeader *header) {
|
||||
static_assert(RsaKeySize == NrrCertification::RsaKeySize);
|
||||
|
||||
/* Verify the signature. */
|
||||
const u8 *sig = header->GetSignature();
|
||||
const size_t sig_size = RsaKeySize;
|
||||
const u8 *mod = header->GetCertificationModulus();
|
||||
const size_t mod_size = RsaKeySize;
|
||||
const u8 *exp = Exponent;
|
||||
const size_t exp_size = util::size(Exponent);
|
||||
const u8 *msg = header->GetSignedArea();
|
||||
const size_t msg_size = header->GetSignedAreaSize();
|
||||
R_UNLESS(crypto::VerifyRsa2048PssSha256(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size), ro::ResultNotAuthorized());
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
@ -45,8 +165,17 @@ namespace ams::ro::impl {
|
|||
|
||||
/* Only perform checks if we must. */
|
||||
const bool ease_nro_restriction = ShouldEaseNroRestriction();
|
||||
|
||||
/* Get certification modulus. */
|
||||
/* NOTE: This must succeed even when ease_nro_restriction is true. */
|
||||
const u8 *modulus;
|
||||
R_TRY(GetCertificationModulus(std::addressof(modulus), header->GetKeyGeneration()));
|
||||
|
||||
if (!ease_nro_restriction) {
|
||||
/* Check signature. */
|
||||
/* Check certification signature. */
|
||||
R_TRY(ValidateNrrCertification(header, modulus));
|
||||
|
||||
/* Check NRR signature. */
|
||||
R_TRY(ValidateNrrSignature(header));
|
||||
|
||||
/* Check program id. */
|
||||
|
|
Loading…
Reference in a new issue