forked from tiliarou/4NXCI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnca.c
398 lines (356 loc) · 14.2 KB
/
nca.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include <inttypes.h>
#include "nca.h"
#include "aes.h"
#include "pki.h"
#include "sha.h"
#include "rsa.h"
#include "utils.h"
#include "extkeys.h"
#include "filepath.h"
#include "nsp.h"
/* Initialize the context. */
void nca_init(nca_ctx_t *ctx) {
memset(ctx, 0, sizeof(*ctx));
}
void nca_free_section_contexts(nca_ctx_t *ctx) {
for (unsigned int i = 0; i < 4; i++) {
if (ctx->section_contexts[i].is_present) {
if (ctx->section_contexts[i].aes) {
free_aes_ctx(ctx->section_contexts[i].aes);
}
if (ctx->section_contexts[i].type == PFS0 && ctx->section_contexts[i].pfs0_ctx.is_exefs) {
free(ctx->section_contexts[i].pfs0_ctx.npdm);
} else if (ctx->section_contexts[i].type == ROMFS) {
if (ctx->section_contexts[i].romfs_ctx.directories) {
free(ctx->section_contexts[i].romfs_ctx.directories);
}
if (ctx->section_contexts[i].romfs_ctx.files) {
free(ctx->section_contexts[i].romfs_ctx.files);
}
} else if (ctx->section_contexts[i].type == NCA0_ROMFS) {
if (ctx->section_contexts[i].nca0_romfs_ctx.directories) {
free(ctx->section_contexts[i].nca0_romfs_ctx.directories);
}
if (ctx->section_contexts[i].nca0_romfs_ctx.files) {
free(ctx->section_contexts[i].nca0_romfs_ctx.files);
}
} else if (ctx->section_contexts[i].type == BKTR) {
if (ctx->section_contexts[i].bktr_ctx.subsection_block) {
free(ctx->section_contexts[i].bktr_ctx.subsection_block);
}
if (ctx->section_contexts[i].bktr_ctx.relocation_block) {
free(ctx->section_contexts[i].bktr_ctx.relocation_block);
}
if (ctx->section_contexts[i].bktr_ctx.directories) {
free(ctx->section_contexts[i].bktr_ctx.directories);
}
if (ctx->section_contexts[i].bktr_ctx.files) {
free(ctx->section_contexts[i].bktr_ctx.files);
}
}
}
}
}
static void nca_save(nca_ctx_t *ctx) {
/* Rewrite header */
fseeko64(ctx->file, 0, SEEK_SET);
if (!fwrite(&ctx->header, 1, 0xC00, ctx->file)) {
fprintf(stderr,"Unable to patch NCA header");
exit(EXIT_FAILURE);
}
fclose(ctx->file);
}
char *nca_get_content_type(nca_ctx_t *ctx) {
switch (ctx->header.content_type) {
case 0:
return "Program";
break;
case 1:
return "Meta";
break;
case 2:
return "Control";
break;
case 3:
return "LegalInformation";
break;
case 4:
return "Data";
break;
default:
fprintf(stderr, "Unknown NCA content type");
exit(EXIT_FAILURE);
}
}
// For writing xml tags in proper order
int nca_type_to_index(uint8_t nca_type)
{
switch (nca_type) {
case 0:
return 0;
break;
case 1:
return 3;
break;
case 2:
return 1;
break;
case 3:
return 2;
break;
default:
fprintf(stderr, "Unknown NCA content type");
exit(EXIT_FAILURE);
}
}
int nca_type_to_cnmt_type(uint8_t nca_type)
{
switch (nca_type) {
case 0:
return 1;
break;
case 1:
return 3;
break;
case 2:
return 5;
break;
default:
fprintf(stderr, "Unknown NCA content type");
exit(EXIT_FAILURE);
}
}
// Heavily modify meta file
void meta_process(nca_ctx_t *ctx)
{
// Set header and pfs0 superblock values for cnmt.nca
ctx->header.nca_size = 0x1000;
ctx->header.section_entries[0].media_start_offset = 0x06;
ctx->header.section_entries[0].media_end_offset = 0x08;
ctx->header.fs_headers[0].crypt_type = 0x01;
ctx->header.fs_headers[0].pfs0_superblock.block_size = 0x1000;
ctx->header.fs_headers[0].pfs0_superblock.hash_table_size = 0x20;
ctx->header.fs_headers[0].pfs0_superblock.pfs0_offset = 0x200;
ctx->header.fs_headers[0].pfs0_superblock.pfs0_size = 0x158;
pfs0_t pfs0 = {
.hashtable.padding = {0},
.header.magic = 0x30534650, // PFS0
.header.num_files = 0x01, // Application_tid.cnmt
.header.string_table_size = 0x38,
.header.reserved = 0,
.file_entry.offset = 0,
.file_entry.size = 0xF8,
.file_entry.string_table_offset = 0,
.file_entry.reserved = 0,
.string_table = {'\0'},
.application_cnmt_header.version = 0,
.application_cnmt_header.type = 0x80, // Regular application
.application_cnmt_header.offset = 0x10,
.application_cnmt_header.content_count = 0x03,
.application_cnmt_header.meta_count = 0,
.application_cnmt_header.unknown2 = {0},
.application_cnmt_header.unknown1 = 0,
.application_cnmt_header.tid = ctx->header.title_id,
.application_cnmt_header.patchid = ctx->header.title_id + 0x800,
.application_cnmt_header.sysversion = 0,
.digest = {0},
.padding = {0}
};
// String table = Application_tid.cnmt
strcat(pfs0.string_table,"Application_");
strncat(pfs0.string_table,cnmt_xml.tid,16);
strcat(pfs0.string_table,".cnmt");
memcpy(pfs0.application_cnmt_contents,application_cnmt_contents,sizeof(application_cnmt_contents));
// Calculate PFS0 hash
sha_ctx_t *pfs0_sha_ctx = new_sha_ctx(HASH_TYPE_SHA256,0);
unsigned char *pfs0_hash_result = (unsigned char*)calloc(1,33);
sha_update(pfs0_sha_ctx,&pfs0.header,sizeof(pfs0.header));
sha_update(pfs0_sha_ctx,&pfs0.file_entry,sizeof(pfs0.file_entry));
sha_update(pfs0_sha_ctx,&pfs0.string_table,sizeof(pfs0.string_table));
sha_update(pfs0_sha_ctx,&pfs0.application_cnmt_header,sizeof(pfs0.application_cnmt_header));
sha_update(pfs0_sha_ctx,&pfs0.application_cnmt_contents,sizeof(pfs0.application_cnmt_contents));
sha_update(pfs0_sha_ctx,&pfs0.digest,sizeof(pfs0.digest));
sha_get_hash(pfs0_sha_ctx,pfs0_hash_result);
memcpy(pfs0.hashtable.hash,pfs0_hash_result,32);
meta_save(ctx, &pfs0);
// Calculate PFS0 superblock hash
sha_ctx_t *pfs0_superblock_sha_ctx = new_sha_ctx(HASH_TYPE_SHA256,0);
unsigned char *pfs0_superblock_hash_result = (unsigned char*)calloc(1,33);
sha_update(pfs0_superblock_sha_ctx,pfs0_hash_result,32);
sha_get_hash(pfs0_superblock_sha_ctx,pfs0_superblock_hash_result);
memcpy(ctx->header.fs_headers[0].pfs0_superblock.master_hash,pfs0_superblock_hash_result,32);
free(pfs0_superblock_hash_result);
free(pfs0_hash_result);
// Calculate section hash
sha_ctx_t *pfs0_section_sha_ctx = new_sha_ctx(HASH_TYPE_SHA256,0);
unsigned char *pfs0_section_hash_result = (unsigned char*)calloc(1,33);
uint8_t pfs0_section_zeros[0x1B0] = { 0 };
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0]._0x0,sizeof(ctx->header.fs_headers[0]._0x0));
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0]._0x1,sizeof(ctx->header.fs_headers[0]._0x1));
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0].partition_type,sizeof(ctx->header.fs_headers[0].partition_type));
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0].fs_type,sizeof(ctx->header.fs_headers[0].fs_type));
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0].crypt_type,sizeof(ctx->header.fs_headers[0].crypt_type));
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0]._0x5,sizeof(ctx->header.fs_headers[0]._0x5));
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0].pfs0_superblock.master_hash,sizeof(ctx->header.fs_headers[0].pfs0_superblock.master_hash));
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0].pfs0_superblock.block_size,sizeof(ctx->header.fs_headers[0].pfs0_superblock.block_size));
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0].pfs0_superblock.always_2,sizeof(ctx->header.fs_headers[0].pfs0_superblock.always_2));
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0].pfs0_superblock.hash_table_offset,sizeof(ctx->header.fs_headers[0].pfs0_superblock.hash_table_offset));
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0].pfs0_superblock.hash_table_size,sizeof(ctx->header.fs_headers[0].pfs0_superblock.hash_table_size));
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0].pfs0_superblock.pfs0_offset,sizeof(ctx->header.fs_headers[0].pfs0_superblock.pfs0_offset));
sha_update(pfs0_section_sha_ctx,&ctx->header.fs_headers[0].pfs0_superblock.pfs0_size,sizeof(ctx->header.fs_headers[0].pfs0_superblock.pfs0_size));
sha_update(pfs0_section_sha_ctx,&pfs0_section_zeros,sizeof(pfs0_section_zeros));
sha_get_hash(pfs0_section_sha_ctx,pfs0_section_hash_result);
memcpy(ctx->header.section_hashes[0],pfs0_section_hash_result,32);
free(pfs0_section_hash_result);
}
void meta_save(nca_ctx_t *ctx, pfs0_t *pfs0)
{
fseeko64(ctx->file, 0xC00, SEEK_SET);
if (!fwrite(pfs0, sizeof(*pfs0) , 1, ctx->file)) {
fprintf(stderr,"Unable to write meta");
exit(EXIT_FAILURE);
}
}
void nca_process(nca_ctx_t *ctx, char *filepath) {
/* Decrypt header */
if (!nca_decrypt_header(ctx)) {
fprintf(stderr, "Invalid NCA header! Are keys correct?\n");
return;
}
/* Sort out crypto type. */
ctx->crypto_type = ctx->header.crypto_type;
if (ctx->header.crypto_type2 > ctx->header.crypto_type)
ctx->crypto_type = ctx->header.crypto_type2;
if (ctx->crypto_type)
ctx->crypto_type--; /* 0, 1 are both master key 0. */
// Set distribution type to "System"
ctx->header.distribution = 0;
ctx->format_version = NCAVERSION_NCA3;
// Set required values for creating .cnmt.xml
int index = nca_type_to_index(ctx->header.content_type);
cnmt_xml.contents[index].type = nca_get_content_type(ctx);
cnmt_xml.contents[index].keygeneration = ctx->crypto_type;
if (index == 3) {
char *tid = (char*)calloc(1,17);
//Convert tile id to hex
sprintf(tid, "%016" PRIx64, ctx->header.title_id);
cnmt_xml.tid = tid;
cnmt_xml.filepath = (char*)calloc(1,strlen(filepath) + 1);
strcpy(cnmt_xml.filepath,filepath);
//Remove .nca and replace it with .xml
strip_ext(cnmt_xml.filepath);
strcat(cnmt_xml.filepath,".xml");
meta_process(ctx);
}
/* Re-encrypt header */
nca_encrypt_header(ctx);
printf("Patching %s\n",filepath);
nca_save(ctx);
// Calculate SHA-256 hash for .cnmt.xml
sha_ctx_t *sha_ctx = new_sha_ctx(HASH_TYPE_SHA256,0);
// Get file size
FILE *file = fopen(filepath, "rb");
if (file == NULL) {
fprintf(stderr, "Failed to open %s!\n", filepath);
exit(EXIT_FAILURE);
}
fseeko64(file,0,SEEK_END);
uint64_t filesize = (uint64_t)ftello64(file);
fseeko64(file,0,SEEK_SET);
uint64_t read_size = 0x4000000; // 4 MB buffer.
unsigned char *buf = malloc(read_size);
if (buf == NULL) {
fprintf(stderr, "Failed to allocate file-read buffer!\n");
exit(EXIT_FAILURE);
}
uint64_t ofs = 0;
while (ofs < filesize) {
if (ofs + read_size >= filesize) read_size = filesize - ofs;
if (fread(buf, 1, read_size, file) != read_size) {
fprintf(stderr, "Failed to read file!\n");
exit(EXIT_FAILURE);
}
sha_update(sha_ctx,buf,read_size);
ofs += read_size;
}
unsigned char *hash_result = (unsigned char*)calloc(1,33);
sha_get_hash(sha_ctx,hash_result);
// Set file size for creating .cnmt.xml
cnmt_xml.contents[index].size = filesize;
// Set file size for creating nsp
nsp_create_info[index].filesize = filesize;
// Set filepath for creating nsp
nsp_create_info[index].filepath = (char*)calloc(1,strlen(filepath) + 1);
strcpy(nsp_create_info[index].filepath,filepath);
// Convert hash to hex string
char *hash_hex = (char*)calloc(1,65);
hexBinaryString(hash_result,32,hash_hex,65);
cnmt_xml.contents[index].hash = hash_hex;
// Get id for creating .cnmt.xml, id = first 16 bytes of hash
strncpy(cnmt_xml.contents[index].id,hash_hex,32);
// Set new filename for creating nsp
if (index == 3) {
nsp_create_info[index].nsp_filename = (char*)calloc(1,42);
strcpy(nsp_create_info[index].nsp_filename,cnmt_xml.contents[index].id);
strcat(nsp_create_info[index].nsp_filename,".cnmt.nca");
}
else {
nsp_create_info[index].nsp_filename = (char*)calloc(1,37);
strcpy(nsp_create_info[index].nsp_filename,cnmt_xml.contents[index].id);
strcat(nsp_create_info[index].nsp_filename,".nca");
}
// Set required values for creating application.cnmt
if (index != 3)
{
uint8_t cnmt_type = nca_type_to_cnmt_type(index);
uint8_t padding = 0;
memcpy(&application_cnmt_contents[index].hash,hash_result,32);
memcpy(&application_cnmt_contents[index].ncaid,hash_result,16);
memcpy(&application_cnmt_contents[index].size,&filesize,6);
memcpy(&application_cnmt_contents[index].type,&cnmt_type,1);
memcpy(&application_cnmt_contents[index].padding, &padding ,1);
}
fclose(file);
free(buf);
free_sha_ctx(sha_ctx);
free(hash_result);
}
void nca_decrypt_key_area(nca_ctx_t *ctx) {
if (ctx->format_version == NCAVERSION_NCA0_BETA || ctx->format_version == NCAVERSION_NCA0) return;
aes_ctx_t *aes_ctx = new_aes_ctx(ctx->tool_ctx->settings.keyset.key_area_keys[ctx->crypto_type][ctx->header.kaek_ind], 16, AES_MODE_ECB);
aes_decrypt(aes_ctx, ctx->decrypted_keys, ctx->header.encrypted_keys, 0x40);
free_aes_ctx(aes_ctx);
}
/* Decrypt NCA header. */
int nca_decrypt_header(nca_ctx_t *ctx) {
fseeko64(ctx->file, 0, SEEK_SET);
if (fread(&ctx->header, 1, 0xC00, ctx->file) != 0xC00) {
fprintf(stderr, "Failed to read NCA header!\n");
return 0;
}
ctx->is_decrypted = 0;
nca_header_t dec_header;
aes_ctx_t *hdr_aes_ctx = new_aes_ctx(ctx->tool_ctx->settings.keyset.header_key, 32, AES_MODE_XTS);
aes_xts_decrypt(hdr_aes_ctx, &dec_header, &ctx->header, 0x400, 0, 0x200);
if (dec_header.magic == MAGIC_NCA3) {
ctx->format_version = NCAVERSION_NCA3;
aes_xts_decrypt(hdr_aes_ctx, &dec_header, &ctx->header, 0xC00, 0, 0x200);
ctx->header = dec_header;
} else {
fprintf(stderr, "Invalid NCA magic!\n");
return 0;
}
free_aes_ctx(hdr_aes_ctx);
return ctx->format_version != NCAVERSION_UNKNOWN;
}
// Encrypt NCA header
void nca_encrypt_header(nca_ctx_t *ctx) {
nca_header_t enc_header;
aes_ctx_t *hdr_aes_ctx = new_aes_ctx(ctx->tool_ctx->settings.keyset.header_key, 32, AES_MODE_XTS);
aes_xts_encrypt(hdr_aes_ctx, &enc_header, &ctx->header, 0xC00, 0, 0x200);
ctx->header = enc_header;
free_aes_ctx(hdr_aes_ctx);
}