-
Notifications
You must be signed in to change notification settings - Fork 20
/
frontend.cpp
560 lines (506 loc) · 13.7 KB
/
frontend.cpp
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* Copyright (c) 2011-2013 by naehrwert
* This file is released under the GPLv2.
*/
#include <stdio.h>
#include <stdlib.h>
#include "types.h"
#include "config.h"
#include "aes.h"
#include "util.h"
#include "keys.h"
#include "sce.h"
#include "sce_inlines.h"
#include "self.h"
#include "np.h"
#include "rvk.h"
#include "spp.h"
#include "util.h"
#include "tables.h"
/*! Parameters. */
extern s8 *_template;
extern s8 *_file_type;
extern s8 *_compress_data;
extern s8 *_skip_sections;
extern s8 *_key_rev;
extern s8 *_meta_info;
extern s8 *_keyset;
extern s8 *_auth_id;
extern s8 *_vendor_id;
extern s8 *_self_type;
extern s8 *_app_version;
extern s8 *_fw_version;
extern s8 *_add_shdrs;
extern s8 *_ctrl_flags;
extern s8 *_cap_flags;
#ifdef CONFIG_CUSTOM_INDIV_SEED
extern s8 *_indiv_seed;
#endif
extern s8 *_license_type;
extern s8 *_app_type;
extern s8 *_content_id;
extern s8 *_real_fname;
extern s8 *_add_sig;
static BOOL _is_hexdigit(s8 c)
{
if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
return TRUE;
return FALSE;
}
static BOOL _is_hexnumber(const s8 *str)
{
u32 i, len = strlen(str);
for(i = 0; i < len; i++)
if(_is_hexdigit(str[i]) == FALSE)
return FALSE;
return TRUE;
}
static BOOL _fill_self_config_template(s8 *file, self_config_t *sconf)
{
u8 *buf = _read_buffer(file, NULL);
if(buf != NULL)
{
sce_buffer_ctxt_t *ctxt = sce_create_ctxt_from_buffer(buf);
if(ctxt != NULL)
{
if(sce_decrypt_header(ctxt, NULL, NULL))
{
_LOG_VERBOSE("Template header decrypted.\n");
_LOG_VERBOSE("Using:\n");
sconf->key_revision = ctxt->sceh->key_revision;
_IF_VERBOSE(printf(" Key Revision 0x%04X\n", sconf->key_revision));
sconf->auth_id = ctxt->self.ai->auth_id;
_IF_VERBOSE(printf(" Auth-ID 0x%016llX\n", sconf->auth_id));
sconf->vendor_id = ctxt->self.ai->vendor_id;
_IF_VERBOSE(printf(" Vendor-ID 0x%08X\n", sconf->vendor_id));
sconf->self_type = ctxt->self.ai->self_type;
_IF_VERBOSE(printf(" SELF-Type 0x%08X\n", sconf->self_type));
sconf->app_version = ctxt->self.ai->version;
_IF_VERBOSE(printf(" APP Version 0x%016llX\n", sconf->app_version));
control_info_t *ci = sce_get_ctrl_info(ctxt, CONTROL_INFO_TYPE_DIGEST);
ci_data_digest_40_t *cid = (ci_data_digest_40_t *)((u8 *)ci + sizeof(control_info_t));
_es_ci_data_digest_40(cid);
sconf->fw_version = cid->fw_version;
_IF_VERBOSE(printf(" FW Version 0x%016llX\n", sconf->fw_version));
ci = sce_get_ctrl_info(ctxt, CONTROL_INFO_TYPE_FLAGS);
sconf->ctrl_flags = (u8 *)_memdup(((u8 *)ci) + sizeof(control_info_t), 0x20);
_IF_VERBOSE(_hexdump(stdout, " Control Flags ", 0, sconf->ctrl_flags, 0x20, 0));
opt_header_t *oh = sce_get_opt_header(ctxt, OPT_HEADER_TYPE_CAP_FLAGS);
sconf->cap_flags = (u8 *)_memdup(((u8 *)oh) + sizeof(opt_header_t), 0x20);
_IF_VERBOSE(_hexdump(stdout, " Capability Flags", 0, sconf->cap_flags, 0x20, 0));
#ifdef CONFIG_CUSTOM_INDIV_SEED
sconf->indiv_seed = NULL;
if(ctxt->self.ai->self_type == SELF_TYPE_ISO)
{
oh = sce_get_opt_header(ctxt, OPT_HEADER_TYPE_INDIV_SEED);
sconf->indiv_seed = (u8 *)_memdup(((u8 *)oh) + sizeof(opt_header_t), oh->size - sizeof(opt_header_t));
sconf->indiv_seed_size = oh->size - sizeof(opt_header_t);
_IF_VERBOSE(_hexdump(stdout, " Individuals Seed", 0, sconf->indiv_seed, sconf->indiv_seed_size, 0));
}
#endif
sconf->add_shdrs = TRUE;
if(_add_shdrs != NULL)
if(strcmp(_add_shdrs, "FALSE") == 0)
sconf->add_shdrs = FALSE;
sconf->skip_sections = TRUE;
if(_skip_sections != NULL)
if(strcmp(_skip_sections, "FALSE") == 0)
sconf->skip_sections = FALSE;
sconf->npdrm_config = NULL;
return TRUE;
}
else
printf("[*] Warning: Could not decrypt template header.\n");
free(ctxt);
}
else
printf("[*] Error: Could not process template %s\n", file);
free(buf);
}
else
printf("[*] Error: Could not load template %s\n", file);
return FALSE;
}
static BOOL _fill_self_config(self_config_t *sconf)
{
if(_key_rev == NULL)
{
printf("[*] Error: Please specify a key revision.\n");
return FALSE;
}
if(_is_hexnumber(_key_rev) == FALSE)
{
printf("[*] Error (Key Revision): Please provide a valid hexadecimal number.\n");
return FALSE;
}
sconf->key_revision = _x_to_u64(_key_rev);
if(_auth_id == NULL)
{
printf("[*] Error: Please specify an auth ID.\n");
return FALSE;
}
sconf->auth_id = _x_to_u64(_auth_id);
if(_vendor_id == NULL)
{
printf("[*] Error: Please specify a vendor ID.\n");
return FALSE;
}
sconf->vendor_id = _x_to_u64(_vendor_id);
if(_self_type == NULL)
{
printf("[*] Error: Please specify a SELF type.\n");
return FALSE;
}
u64 type = _get_id(_self_types_params, _self_type);
if(type == (u64)(-1))
{
printf("[*] Error: Invalid SELF type.\n");
return FALSE;
}
sconf->self_type = type;
if(_app_version == NULL)
{
printf("[*] Error: Please specify an application version.\n");
return FALSE;
}
sconf->app_version = _x_to_u64(_app_version);
sconf->fw_version = 0;
if(_fw_version != NULL)
sconf->fw_version = _x_to_u64(_fw_version);
sconf->add_shdrs = TRUE;
if(_add_shdrs != NULL)
if(strcmp(_add_shdrs, "FALSE") == 0)
sconf->add_shdrs = FALSE;
sconf->skip_sections = TRUE;
if(_skip_sections != NULL)
if(strcmp(_skip_sections, "FALSE") == 0)
sconf->skip_sections = FALSE;
sconf->ctrl_flags = NULL;
if(_ctrl_flags != NULL)
{
if(strlen(_ctrl_flags) != 0x20*2)
{
printf("[*] Error: Control flags need to be 32 bytes.\n");
return FALSE;
}
sconf->ctrl_flags = _x_to_u8_buffer(_ctrl_flags);
}
sconf->cap_flags = NULL;
if(_cap_flags != NULL)
{
if(strlen(_cap_flags) != 0x20*2)
{
printf("[*] Error: Capability flags need to be 32 bytes.\n");
return FALSE;
}
sconf->cap_flags = _x_to_u8_buffer(_cap_flags);
}
#ifdef CONFIG_CUSTOM_INDIV_SEED
sconf->indiv_seed = NULL;
if(_indiv_seed != NULL)
{
u32 len = strlen(_indiv_seed);
if(len > 0x100*2)
{
printf("[*] Error: Individuals seed must be <= 0x100 bytes.\n");
return FALSE;
}
sconf->indiv_seed = _x_to_u8_buffer(_indiv_seed);
sconf->indiv_seed_size = len / 2;
}
#endif
sconf->npdrm_config = NULL;
return TRUE;
}
static BOOL _fill_npdrm_config(self_config_t *sconf)
{
if((sconf->npdrm_config = (npdrm_config_t *)malloc(sizeof(npdrm_config_t))) == NULL)
return FALSE;
if(_license_type == NULL)
{
printf("[*] Error: Please specify a license type.\n");
return FALSE;
}
//TODO!
if(strcmp(_license_type, "FREE") == 0)
sconf->npdrm_config->license_type = NP_LICENSE_FREE;
else if(strcmp(_license_type, "LOCAL") == 0)
sconf->npdrm_config->license_type = NP_LICENSE_LOCAL;
else
{
printf("[*] Error: Only supporting LOCAL and FREE license for now.\n");
return FALSE;
}
if(_app_type == NULL)
{
printf("[*] Error: Please specify an application type.\n");
return FALSE;
}
u64 type = _get_id(_np_app_types, _app_type);
if(type == (u64)(-1))
{
printf("[*] Error: Invalid application type.\n");
return FALSE;
}
sconf->npdrm_config->app_type = type;
if(_content_id == NULL)
{
printf("[*] Error: Please specify a content ID.\n");
return FALSE;
}
strncpy((s8 *)sconf->npdrm_config->content_id, _content_id, 0x30);
if(_real_fname == NULL)
{
printf("[*] Error: Please specify a real filename.\n");
return FALSE;
}
sconf->npdrm_config->real_fname = _real_fname;
return TRUE;
}
void frontend_print_infos(s8 *file)
{
u8 *buf = _read_buffer(file, NULL);
if(buf != NULL)
{
sce_buffer_ctxt_t *ctxt = sce_create_ctxt_from_buffer(buf);
if(ctxt != NULL)
{
u8 *meta_info = NULL;
if(_meta_info != NULL)
{
if(strlen(_meta_info) != 0x40*2)
{
printf("[*] Error: Metadata info needs to be 64 bytes.\n");
return;
}
meta_info = _x_to_u8_buffer(_meta_info);
}
u8 *keyset = NULL;
if(_keyset != NULL)
{
if(strlen(_keyset) != (0x20 + 0x10 + 0x15 + 0x28 + 0x01)*2)
{
printf("[*] Error: Keyset has a wrong length.\n");
return;
}
keyset = _x_to_u8_buffer(_keyset);
}
if(sce_decrypt_header(ctxt, meta_info, keyset))
{
_LOG_VERBOSE("Header decrypted.\n");
if(sce_decrypt_data(ctxt))
_LOG_VERBOSE("Data decrypted.\n");
else
printf("[*] Warning: Could not decrypt data.\n");
}
else
printf("[*] Warning: Could not decrypt header.\n");
sce_print_info(stdout, ctxt);
if(ctxt->sceh->header_type == SCE_HEADER_TYPE_SELF)
self_print_info(stdout, ctxt);
else if(ctxt->sceh->header_type == SCE_HEADER_TYPE_RVK && ctxt->mdec == TRUE)
rvk_print(stdout, ctxt);
else if(ctxt->sceh->header_type == SCE_HEADER_TYPE_SPP && ctxt->mdec == TRUE)
spp_print(stdout, ctxt);
free(ctxt);
}
else
printf("[*] Error: Could not process %s\n", file);
free(buf);
}
else
printf("[*] Error: Could not load %s\n", file);
}
void frontend_decrypt(s8 *file_in, s8 *file_out)
{
u8 *buf = _read_buffer(file_in, NULL);
if(buf != NULL)
{
sce_buffer_ctxt_t *ctxt = sce_create_ctxt_from_buffer(buf);
if(ctxt != NULL)
{
u8 *meta_info = NULL;
if(_meta_info != NULL)
{
if(strlen(_meta_info) != 0x40*2)
{
printf("[*] Error: Metadata info needs to be 64 bytes.\n");
return;
}
meta_info = _x_to_u8_buffer(_meta_info);
}
u8 *keyset = NULL;
if(_keyset != NULL)
{
if(strlen(_keyset) != (0x20 + 0x10 + 0x15 + 0x28 + 0x01)*2)
{
printf("[*] Error: Keyset has a wrong length.\n");
return;
}
keyset = _x_to_u8_buffer(_keyset);
}
if(sce_decrypt_header(ctxt, meta_info, keyset))
{
_LOG_VERBOSE("Header decrypted.\n");
if(sce_decrypt_data(ctxt))
{
_LOG_VERBOSE("Data decrypted.\n");
if(ctxt->sceh->header_type == SCE_HEADER_TYPE_SELF)
{
if(self_write_to_elf(ctxt, file_out) == TRUE)
printf("[*] ELF written to %s.\n", file_out);
else
printf("[*] Error: Could not write ELF.\n");
}
else if(ctxt->sceh->header_type == SCE_HEADER_TYPE_RVK)
{
if(_write_buffer(file_out, ctxt->scebuffer + ctxt->metash[0].data_offset,
ctxt->metash[0].data_size + ctxt->metash[1].data_size))
printf("[*] RVK written to %s.\n", file_out);
else
printf("[*] Error: Could not write RVK.\n");
}
else if(ctxt->sceh->header_type == SCE_HEADER_TYPE_PKG)
{
/*if(_write_buffer(file_out, ctxt->scebuffer + ctxt->metash[0].data_offset,
ctxt->metash[0].data_size + ctxt->metash[1].data_size + ctxt->metash[2].data_size))
printf("[*] PKG written to %s.\n", file_out);
else
printf("[*] Error: Could not write PKG.\n");*/
printf("soon...\n");
}
else if(ctxt->sceh->header_type == SCE_HEADER_TYPE_SPP)
{
if(_write_buffer(file_out, ctxt->scebuffer + ctxt->metash[0].data_offset,
ctxt->metash[0].data_size + ctxt->metash[1].data_size))
printf("[*] SPP written to %s.\n", file_out);
else
printf("[*] Error: Could not write SPP.\n");
}
}
else
printf("[*] Error: Could not decrypt data.\n");
}
else
printf("[*] Error: Could not decrypt header.\n");
free(ctxt);
}
else
printf("[*] Error: Could not process %s\n", file_in);
free(buf);
}
else
printf("[*] Error: Could not load %s\n", file_in);
}
void frontend_encrypt(s8 *file_in, s8 *file_out)
{
BOOL can_compress = FALSE;
self_config_t sconf;
sce_buffer_ctxt_t *ctxt;
u32 file_len = 0;
u8 *file;
if(_file_type == NULL)
{
printf("[*] Error: Please specify a file type.\n");
return;
}
u8 *keyset = NULL;
if(_keyset != NULL)
{
if(strlen(_keyset) != (0x20 + 0x10 + 0x15 + 0x28 + 0x01)*2)
{
printf("[*] Error: Keyset has a wrong length.\n");
return;
}
keyset = _x_to_u8_buffer(_keyset);
}
if((file = _read_buffer(file_in, &file_len)) == NULL)
{
printf("[*] Error: Could not read %s.\n", file_in);
return;
}
if(strcmp(_file_type, "SELF") == 0)
{
if(_self_type == NULL && _template == NULL)
{
printf("[*] Error: Please specify a SELF type.\n");
return;
}
if(_template != NULL)
{
//Use a template SELF to fill the config.
if(_fill_self_config_template(_template, &sconf) == FALSE)
return;
}
else
{
//Fill the config from command line arguments.
if(_fill_self_config(&sconf) == FALSE)
return;
}
if(sconf.self_type == SELF_TYPE_NPDRM)
if(_fill_npdrm_config(&sconf) == FALSE)
return;
ctxt = sce_create_ctxt_build_self(file, file_len);
if(self_build_self(ctxt, &sconf) == TRUE)
printf("[*] SELF built.\n");
else
{
printf("[*] Error: SELF not built.\n");
return;
}
//SPU SELFs may not be compressed.
if(!(sconf.self_type == SELF_TYPE_LDR || sconf.self_type == SELF_TYPE_ISO))
can_compress = TRUE;
}
else if(strcmp(_file_type, "RVK") == 0)
{
printf("soon...\n");
return;
}
else if(strcmp(_file_type, "PKG") == 0)
{
printf("soon...\n");
return;
}
else if(strcmp(_file_type, "SPP") == 0)
{
printf("soon...\n");
return;
}
//Compress data if wanted.
if(_compress_data != NULL && strcmp(_compress_data, "TRUE") == 0)
{
if(can_compress == TRUE)
{
sce_compress_data(ctxt);
printf("[*] Data compressed.\n");
}
else
printf("[*] Warning: This type of file will not be compressed.\n");
}
//Layout and encrypt context.
sce_layout_ctxt(ctxt);
if(sce_encrypt_ctxt(ctxt, keyset) == TRUE)
printf("[*] Data encrypted.\n");
else
{
printf("[*] Error: Data not encrypted.\n");
return;
}
//Write file.
if(sce_write_ctxt(ctxt, file_out) == TRUE)
{
printf("[*] %s written.\n", file_out);
//Add NPDRM footer signature.
if(sconf.self_type == SELF_TYPE_NPDRM && _add_sig != NULL && strcmp(_add_sig, "TRUE") == 0)
{
if(np_sign_file(file_out) == TRUE)
printf("[*] Added NPDRM footer signature.\n");
else
printf("[*] Error: Could not add NPDRM footer signature.\n");
}
}
else
printf("[*] Error: %s not written.\n", file_out);
}