From 6b7da2887fe3e185c90c307552022b4b61fb1867 Mon Sep 17 00:00:00 2001 From: Max K Date: Mon, 4 Jun 2018 23:04:44 +0200 Subject: [PATCH] package2 verification and ini1 merging fixes (#125) * fusee: Incremet offset while reading kip1s * fusee: Pass package2 section data along with the decrypted metadata --- fusee/fusee-secondary/Makefile | 2 +- fusee/fusee-secondary/src/package2.c | 9 ++++----- fusee/fusee-secondary/src/stratosphere.c | 2 ++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/fusee/fusee-secondary/Makefile b/fusee/fusee-secondary/Makefile index 56b9f74c7..b200d0ae7 100644 --- a/fusee/fusee-secondary/Makefile +++ b/fusee/fusee-secondary/Makefile @@ -108,7 +108,7 @@ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) .PHONY: $(BUILD) clean all -.PHONY: check_exosphere check_thermosphere check_exosphere +.PHONY: check_exosphere check_thermosphere check_stratosphere #--------------------------------------------------------------------------------- all: $(BUILD) diff --git a/fusee/fusee-secondary/src/package2.c b/fusee/fusee-secondary/src/package2.c index aed910593..b968818f2 100644 --- a/fusee/fusee-secondary/src/package2.c +++ b/fusee/fusee-secondary/src/package2.c @@ -98,8 +98,7 @@ static void package2_crypt_ctr(unsigned int master_key_rev, void *dst, size_t ds se_aes_ctr_crypt(KEYSLOT_SWITCH_PACKAGE2KEY, dst, dst_size, src, src_size, ctr, ctr_size); } -static bool package2_validate_metadata(package2_meta_t *metadata) { - package2_header_t *package2 = (package2_header_t *)((uint8_t *)metadata - offsetof(package2_header_t, metadata)); +static bool package2_validate_metadata(package2_meta_t *metadata, uint8_t data[]) { if (metadata->magic != MAGIC_PK21) { return false; } @@ -160,7 +159,7 @@ static bool package2_validate_metadata(package2_meta_t *metadata) { /* Validate section hashes. */ if (metadata->section_sizes[section]) { - void *section_data = package2->data + cur_section_offset; + void *section_data = data + cur_section_offset; uint8_t calculated_hash[0x20]; se_calculate_sha256(calculated_hash, section_data, metadata->section_sizes[section]); if (memcmp(calculated_hash, metadata->section_hashes[section], sizeof(metadata->section_hashes[section])) != 0) { @@ -199,7 +198,7 @@ static uint32_t package2_decrypt_and_validate_header(package2_header_t *header, /* Copy the ctr (which stores information) into the decrypted metadata. */ memcpy(metadata.ctr, header->metadata.ctr, sizeof(header->metadata.ctr)); /* See if this is the correct key. */ - if (package2_validate_metadata(&metadata)) { + if (package2_validate_metadata(&metadata, header->data)) { header->metadata = metadata; return mkey_rev; } @@ -209,7 +208,7 @@ static uint32_t package2_decrypt_and_validate_header(package2_header_t *header, if (mkey_rev > mkey_get_revision()) { fatal_error("failed to decrypt the Package2 header (master key revision %u)!\n", mkey_get_revision()); } - } else if (!package2_validate_metadata(&header->metadata)) { + } else if (!package2_validate_metadata(&header->metadata, header->data)) { fatal_error("Failed to validate the Package2 header!\n"); } return 0; diff --git a/fusee/fusee-secondary/src/stratosphere.c b/fusee/fusee-secondary/src/stratosphere.c index 63a3e4f52..e849c88bc 100644 --- a/fusee/fusee-secondary/src/stratosphere.c +++ b/fusee/fusee-secondary/src/stratosphere.c @@ -120,6 +120,8 @@ ini1_header_t *stratosphere_merge_inis(ini1_header_t **inis, size_t num_inis) { fatal_error("INI1s[%zu][%zu] appears not to be a KIP1!\n", i, p); } + offset += kip1_get_size_from_header(current_kip); + bool already_loaded = false; for (uint32_t j = 0; j < merged->num_processes; j++) { if (process_list[j] == current_kip->title_id) {