meta.c
11.4 KB
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
#include <PR/bcp.h>
#include <PR/R4300.h>
#include <PR/bbsim.h>
#include <PR/bbskapi.h>
#include <PR/os.h>
#include <PR/os_bbfs.h>
#include <PR/os_bbatb.h>
#include "common.h"
#include <PR/os_bbsa.h>
#include <bb_nn.h>
#include <sha1.h>
int getTicketStatus( BbTicket *pTick, u16 window, u16 *cc);
int updateLPWindow( OSBbSaTickets *tickets, u16 *window, u16 *cc);
int _osBbSaVerifyTickets(OSBbSaTickets *ticketset)
{
BbTicketBundle ticketBundle;
BbAppLaunchCrls appRls;
SHA1Context sha;
u32 compareStart = 0;
BbShaHash hash_data;
BbRsaPublicKey2048 authpublickey;
BbRsaExponent authexponent;
unsigned char computedSig[sizeof(BbRsaPublicKey4096)];
int i;
PRINTF("_osBbSaVerifyTickets(), %d\n",ticketset->numTickets);
/* zero public key and exponent until an authentic one is found */
bzero((u8 *)authpublickey, sizeof(BbRsaPublicKey2048));
bzero((u8 *)&authexponent, sizeof(BbRsaExponent));
for(i=0; i< ticketset->numTickets; i++){
PRINTF(" i = %d\n",i);
if(BB_SYSAPP_PASS != osBbSaBundleTicket(&(ticketset->ticket[i]),
&ticketBundle,
&appRls)){
return BB_SYSAPP_FAIL;
}
PRINTF("ticket bundled\n");
/* compute hash */
SHA1Reset(&sha);
SHA1Input(&sha, (u8 *) ticketBundle.ticket,
(sizeof(BbTicket) - sizeof(BbRsaSig2048)));
SHA1Result(&sha, (u8 *) hash_data);
/* verify ticket */
if(i==0){
/* call sk function first time */
if(skVerifyHash(hash_data,
(BbGenericSig *)&(ticketBundle.ticket->head).ticketSign,
ticketBundle.ticketChain,
&appRls) == SK_API_SUCCESS){
/* if success, get authenticated public key and exponent */
memcpy(authpublickey,
((BbRsaCert *)ticketBundle.ticketChain[0])->publicKey,
sizeof(BbRsaPublicKey2048));
memcpy(&authexponent,
&(((BbRsaCert *)ticketBundle.ticketChain[0])->exponent),
sizeof(BbRsaExponent));
}
else{
return BB_SYSAPP_FAIL;
}
}
/* other tickets */
else{
/* verify directly from libcrypto to avoid cert checks again*/
bsl_rsa_verify(computedSig,
(u32 *)(BbGenericSig *)&(ticketBundle.ticket->head).ticketSign,
authpublickey,
&authexponent,
2048);
compareStart = sizeof(BbRsaPublicKey2048) - sizeof(BbShaHash);
if (memcmp(hash_data, computedSig+compareStart,
sizeof(BbShaHash))!=0){
return BB_SYSAPP_FAIL;
}
}
}
return BB_SYSAPP_PASS;
}
int osBbSaMetaGetTickets(OSBbSaTickets *tickets, int verify)
{
OSBbStatBuf fsStat;
s32 fd;
u32 ret = BB_SYSAPP_PASS;
int rv;
fd = osBbFOpen(gSaTicketFname,"r");
if(fd<0){
PRINTF("osBbSaMetaGetTickets (%d): Can't open ticket file %s\n", fd, gSaTicketFname);
return SAMETA_ERR_FS;
}
if( (rv=osBbFStat(fd, &fsStat, NULL, 0)) < 0){
PRINTF("osBbSaMetaGetTickets: Can't stat ticket file\n");
ret = SAMETA_ERR_FS;
goto exit;
}
if(fsStat.size > sizeof(OSBbSaTickets)){
PRINTF("osBbSaMetaGetTickets: ticket file size to big\n");
ret = SAMETA_ERR_MEM;
goto exit;
}
if(osBbFRead(fd, 0, (u8 *)tickets, fsStat.size)<0){
PRINTF("osBbSaMetaGetTickets: Can't read ticket file\n");
ret = SAMETA_ERR_FS;
goto exit;
}
/* sanity-check number of tickets */
if(((tickets->numTickets)*sizeof(BbTicket)+4) > fsStat.size){
PRINTF("osBbSaMetaGetTickets: Bad numTickets\n");
ret = SAMETA_ERR_FS;
goto exit;
}
if (verify) {
if(_osBbSaVerifyTickets(tickets) != BB_SYSAPP_PASS){
ret = SAMETA_ERR_FS;
goto exit;
}
}
exit:
osBbFClose(fd);
return ret;
}
int writeTicketFile(OSBbSaTickets *tickets)
{
const char newFname[]="temp.tmp";
int size, block_size;
s32 fd;
size = 4 + sizeof(BbTicket) * (tickets->numTickets);
block_size = (((size)+(BB_FL_BLOCK_SIZE-1)) & ~(BB_FL_BLOCK_SIZE-1));
osBbFDelete(newFname);
if ((fd = osBbFCreate(newFname, 1, block_size))<0) {
PRINTF("temp.tmp creation failed %d\n", fd);
return SAMETA_ERR_FS;
} else {
if( osBbFWrite(fd, 0, tickets, block_size)<0 ){
PRINTF("failed to write temp.tmp\n");
osBbFClose(fd);
osBbFDelete(newFname);
return SAMETA_ERR_FS;
}
osBbFClose(fd);
}
osBbFRename(newFname, gSaTicketFname);
return BB_SYSAPP_PASS;
}
#define SWAP_TYPE(t, a, b) { t tmp; tmp = a; a = b; b = tmp; }
#define SWAP(a, b) \
SWAP_TYPE(int, valid[(a)], valid[(b)]); \
SWAP_TYPE(int, status[(a)], status[(b)]); \
SWAP_TYPE(BbTicket, tickets->ticket[(a)], tickets->ticket[(b)])
#define GET_TITLE(a) \
tickets->ticket[(a)].cmd.contentDesc + sizeof(OSBbLaunchMetaData) + 4 \
+ *((u16*)(tickets->ticket[(a)].cmd.contentDesc + sizeof(OSBbLaunchMetaData))) \
+ *((u16*)(tickets->ticket[(a)].cmd.contentDesc + sizeof(OSBbLaunchMetaData) + 2))
int osBbSaMetaCleanTickets(OSBbSaTickets *tickets)
{
int i, j, k;
int valid[SA_META_MAX_TICKETS], status[SA_META_MAX_TICKETS];
int gameTickets;
u8 *ititle, *jtitle;
u16 window, cc[BB_MAX_CC];
int ret = BB_SYSAPP_PASS;
// get raw tickets
ret = osBbSaMetaGetTickets(tickets, 1);
if (ret != BB_SYSAPP_PASS) return ret;
/* get consumption window */
skGetConsumption(&window, cc);
/* get ticket status */
for (i = 0; i < tickets->numTickets; ++i) {
valid[i] = 1;
status[i] = getTicketStatus( &(tickets->ticket[i]), window, cc);
}
/* move club to the end */
gameTickets = tickets->numTickets;
for (i = 0; i < tickets->numTickets; ++i) {
ititle = GET_TITLE(i);
if (!strcmp(ititle, gSaClubTitle)) {
--gameTickets;
SWAP(i, gameTickets);
break;
}
}
/* sort by title */
for (i = 0; i < gameTickets - 1; ++i) {
for (j = i + 1; j < gameTickets; ++j) {
if (strcmp(GET_TITLE(j), GET_TITLE(i)) < 0) {
SWAP(i, j);
}
}
}
/* process same title tickets */
i = 0;
while (i < gameTickets - 1) {
int perm = -1, limited = -1, expired = -1;
/* search for consecutive tickets with the same title */
ititle = GET_TITLE(i);
for (j = i + 1; j < gameTickets; ++j) {
jtitle = GET_TITLE(j);
if (strcmp(ititle, jtitle)) {
break;
}
}
if (j == (i + 1))
goto skip;
/* Find highest "priority" ticket in the consecutive block [i, j) */
for (k = i; k < j; ++k) {
switch (status[k]) {
case SAMETA_TICKET_PERM:
if (perm == -1)
perm = k;
break;
case SAMETA_TICKET_LP_VALID:
if (limited == -1)
limited = k;
break;
case SAMETA_TICKET_LP_EXPIRED:
if (tickets->ticket[k].head.tid > expired)
expired = tickets->ticket[k].head.tid;
break;
}
}
/* mark unnecessary tickets as invalid */
if (perm != -1) {
for (k = i; k < j; ++k) {
if (k != perm) {
valid[k] = 0;
}
}
} else if (limited != -1) {
for (k = i; k < j; ++k) {
if (status[k] == SAMETA_TICKET_LP_EXPIRED) {
valid[k] = 0;
}
}
} else {
for (k = i; k < j; ++k) {
if (tickets->ticket[k].head.tid != expired) {
valid[k] = 0;
}
}
}
skip:
i = j;
}
/* remove invalid tickets */
for (i = 0, j = 0; i < tickets->numTickets; ++i) {
if (valid[i]) {
if (i != j) {
bcopy(&tickets->ticket[i], &tickets->ticket[j], sizeof(BbTicket));
}
++j;
}
}
tickets->numTickets = j;
return writeTicketFile(tickets);
}
int osBbSaMetaGetData(OSBbSaTickets *tickets, OSBbSaGameMetaData *metaData, int trial)
{
int i;
s32 fd;
char appName[16];
BbTicketId tid;
u16 window, cc[BB_MAX_CC];
if (trial)
skGetConsumption(&window, cc);
for(i=0; i<tickets->numTickets; i++){
u8 *p = tickets->ticket[i].cmd.contentDesc;
metaData[i].launch = *((OSBbLaunchMetaData*)p);
p += sizeof(OSBbLaunchMetaData);
metaData[i].thumb_len = *((u16*)p);
p += sizeof(u16);
metaData[i].title_img_len = *((u16*)p);
p += sizeof(u16);
metaData[i].thumb_data = p;
p += metaData[i].thumb_len;
metaData[i].title_img = p;
p += metaData[i].title_img_len;
metaData[i].title = p; while (*p!='\0') p++; p++;
metaData[i].isbn = p; while (*p!='\0') p++; p++;
metaData[i].crid = p; while (*p!='\0') p++; p++;
metaData[i].oadid = p;
metaData[i].size = tickets->ticket[i].cmd.head.size;
metaData[i].type = 0;
/* is present on card? */
osBbSaCidToAppName(tickets->ticket[i].cmd.head.id,gSaAppExt,appName);
if( (fd = osBbFOpen(appName,"r")) >= 0){
metaData[i].type |= SA_META_TYPE_ONCARD;
osBbFClose(fd);
}
else{
osBbSaCidToAppName(tickets->ticket[i].cmd.head.id,gSaRecExt,
appName);
if( (fd = osBbFOpen(appName,"r")) >= 0){
metaData[i].type |= SA_META_TYPE_ONCARD;
osBbFClose(fd);
}
}
metaData[i].limit = tickets->ticket[i].head.limit;
/* trial games? */
tid = tickets->ticket[i].head.tid;
if (tid >= BB_TICKET_ID_LIMITED && trial) {
metaData[i].type |= SA_META_TYPE_TRIAL;
metaData[i].code = tickets->ticket[i].head.code;
tid = tid & (BB_TICKET_ID_LIMITED-1);
if( tid < window ) // kicked out of window, expired
metaData[i].cc = tickets->ticket[i].head.limit;
else if( (tid - window) >= BB_MAX_CC ) // not yet played
metaData[i].cc = 0;
else
metaData[i].cc = cc[tid-window];
}
}
return BB_SYSAPP_PASS;
}
int getTicketStatus(BbTicket* pTick, u16 window, u16* cc)
{
u16 tid;
int status = SAMETA_TICKET_PERM;
if ( pTick->head.tid >= BB_TICKET_ID_LIMITED ) {
tid = (pTick->head.tid) & (BB_TICKET_ID_LIMITED-1);
if( tid < window ) // kicked out of window, expired
status = SAMETA_TICKET_LP_EXPIRED;
else if( (tid - window) >= BB_MAX_CC ) // not yet played
status = SAMETA_TICKET_LP_VALID;
else {
if ( pTick->head.limit <= cc[tid - window] )
status = SAMETA_TICKET_LP_EXPIRED;
else
status = SAMETA_TICKET_LP_VALID;
}
}
return status;
}