Fix bugs in BKTR romfs parsing, keyfile parsing

This commit is contained in:
Michael Scire 2018-07-26 12:18:30 -07:00
parent 84715a89f8
commit bdbd4f639e
2 changed files with 15 additions and 8 deletions

View file

@ -48,7 +48,7 @@ static int get_kv(FILE *f, char **key, char **value) {
for (; *p == ' ' || *p == '\t'; ++p)\ for (; *p == ' ' || *p == '\t'; ++p)\
;\ ;\
} while(0); } while(0);
static char line[512]; static char line[1024];
char *k, *v, *p, *end; char *k, *v, *p, *end;
*key = *value = NULL; *key = *value = NULL;

11
nca.c
View file

@ -611,12 +611,20 @@ int nca_decrypt_header(nca_ctx_t *ctx) {
memcpy(ctx->decrypted_keys, out_keydata, 0x20); memcpy(ctx->decrypted_keys, out_keydata, 0x20);
ctx->format_version = NCAVERSION_NCA0_BETA; ctx->format_version = NCAVERSION_NCA0_BETA;
} }
} else {
unsigned char calc_hash[0x20];
static const unsigned char expected_hash[0x20] = {0x9A, 0xBB, 0xD2, 0x11, 0x86, 0x00, 0x21, 0x9D, 0x7A, 0xDC, 0x5B, 0x43, 0x95, 0xF8, 0x4E, 0xFD, 0xFF, 0x6B, 0x25, 0xEF, 0x9F, 0x96, 0x85, 0x28, 0x18, 0x9E, 0x76, 0xB0, 0x92, 0xF0, 0x6A, 0xCB};
sha256_hash_buffer(calc_hash, dec_header.encrypted_keys, 0x20);
if (memcmp(calc_hash, expected_hash, sizeof(calc_hash)) == 0) {
ctx->format_version = NCAVERSION_NCA0;
memcpy(ctx->decrypted_keys, dec_header.encrypted_keys, 0x40);
} else { } else {
ctx->format_version = NCAVERSION_NCA0; ctx->format_version = NCAVERSION_NCA0;
aes_ctx_t *aes_ctx = new_aes_ctx(ctx->tool_ctx->settings.keyset.key_area_keys[ctx->crypto_type][dec_header.kaek_ind], 16, AES_MODE_ECB); aes_ctx_t *aes_ctx = new_aes_ctx(ctx->tool_ctx->settings.keyset.key_area_keys[ctx->crypto_type][dec_header.kaek_ind], 16, AES_MODE_ECB);
aes_decrypt(aes_ctx, ctx->decrypted_keys, dec_header.encrypted_keys, 0x20); aes_decrypt(aes_ctx, ctx->decrypted_keys, dec_header.encrypted_keys, 0x20);
free_aes_ctx(aes_ctx); free_aes_ctx(aes_ctx);
} }
}
if (ctx->format_version != NCAVERSION_UNKNOWN) { if (ctx->format_version != NCAVERSION_UNKNOWN) {
memset(dec_header.fs_headers, 0, sizeof(dec_header.fs_headers)); memset(dec_header.fs_headers, 0, sizeof(dec_header.fs_headers));
aes_ctx_t *aes_ctx = new_aes_ctx(ctx->decrypted_keys, 32, AES_MODE_XTS); aes_ctx_t *aes_ctx = new_aes_ctx(ctx->decrypted_keys, 32, AES_MODE_XTS);
@ -1191,8 +1199,7 @@ void nca_process_bktr_section(nca_section_ctx_t *ctx) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* Switch RomFS has actual entries at table offset + 4 for no good reason. */ nca_section_fseek(ctx, ctx->bktr_ctx.romfs_offset + ctx->bktr_ctx.header.dir_meta_table_offset);
nca_section_fseek(ctx, ctx->bktr_ctx.romfs_offset + ctx->bktr_ctx.header.dir_meta_table_offset + 4);
if (nca_section_fread(ctx, ctx->bktr_ctx.directories, ctx->bktr_ctx.header.dir_meta_table_size) != ctx->bktr_ctx.header.dir_meta_table_size) { if (nca_section_fread(ctx, ctx->bktr_ctx.directories, ctx->bktr_ctx.header.dir_meta_table_size) != ctx->bktr_ctx.header.dir_meta_table_size) {
fprintf(stderr, "Failed to read RomFS directory cache!\n"); fprintf(stderr, "Failed to read RomFS directory cache!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);