From be0f5c29e01ed787a47019acd5bb27dcacaed7c8 Mon Sep 17 00:00:00 2001 From: jakcron Date: Fri, 22 Oct 2021 04:12:03 +0800 Subject: [PATCH] Better detect, handle and warn when a custom ticket is malformed. --- src/KeyBag.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/KeyBag.cpp b/src/KeyBag.cpp index 87152cf..0f97ed9 100644 --- a/src/KeyBag.cpp +++ b/src/KeyBag.cpp @@ -568,10 +568,12 @@ void nstool::KeyBagInitializer::importTicket(const tc::io::Path& tik_path) byte_t common_key_index = tik.getBody().getCommonKeyId(); // work around for bad scene tickets where they don't set the commonkey id field (detect scene ticket with ffff.... signature) - auto signature_bytes = tik.getSignature().getSignature(); - if (common_key_index != rights_id[15] && *((uint64_t*)signature_bytes.data()) == (uint64_t)0xffffffffffffffff) + if (common_key_index == 0 && *((uint64_t*)tik.getSignature().getSignature().data()) == (uint64_t)0xffffffffffffffff) { - common_key_index = rights_id[15]; + fmt::print("[WARNING] Ticket \"{:s}\" is fake-signed, and NCA decryption may fail if ticket was incorrectly generated.", tc::cli::FormatUtil::formatBytesAsString(rights_id.data(), rights_id.size(), true, "")); + // the keygeneration was included in the rights_id from keygeneration 0x03 and onwards, so in those cases we can copy from there + if (rights_id[15] >= 0x03) + common_key_index = rights_id[15]; } // convert key_generation