skapi.c
21.9 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
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
#include <PR/bcp.h>
#include <PR/bbboot.h>
#include <PR/bbnand.h>
#include <PR/bbticket.h>
#include <PR/bbvirage.h>
#include <PR/bbfs.h>
#include <PR/bbcert.h>
#include <PR/bbskapi.h>
#include <aes_api.h>
#include <sha1.h>
#include <aes.h>
#include <algorithms.h>
#include "util.h"
#include "skerror.h"
#include "cert.h"
#include "nvram.h"
#include "skreencrypt.h"
#include "rl.h"
/* NOTE: this table must be ordered to match the SK_API_CALL_*
* #defines in bbskapi.h.
*/
const int skapi_call_table[]={
(int)skGetId,
(int)skLaunchSetup,
(int)skLaunch,
(int)skRecryptListValid,
(int)skRecryptBegin,
(int)skRecryptData,
(int)skRecryptComputeState,
(int)skRecryptEnd,
(int)skSignHash,
(int)skVerifyHash,
(int)skGetConsumption,
(int)skAdvanceTicketWindow,
(int)skSetLimit,
(int)skExit,
(int)skKeepAlive,
#ifdef SK_DEVELOP
(int)skGetRandomKeyData,
(int)skDumpVirage,
(int)skTest2,
(int)skTest3,
(int)skResetWindow,
(int)skValidateRls
#endif
};
const u32 gNumApiCalls = sizeof(skapi_call_table)/sizeof(void *);
BbTicketId gSavedTid = BB_TICKET_ID_INVAL; /* force into data segment not bss */
u16 gSavedCc = 0;
u16 gSavedCode = BB_LIMIT_CODE_INVAL;
u32 gExtraBits = 0; /* used to pass information back to sysapp */
extern BbRsaPublicKey4096 gRootKey;
extern BbRsaExponent gRootExp;
/* Globals to store state during app launch and recryption */
static BbContentMetaDataHead gCmdh;
static BbTicketHead gTikh;
static SHA1Context gSha;
static u32 gBytesProcessed;
static AesKeyInstance gRecryptKey;
static AesCipherInstance gRecryptCipher;
static u32 gRecryptState;
static u32 gRecryptHwAesChain;
static BbAesIv gRecryptRecoverIv;
#define RECRYPT_DATA 0
#define RECRYPT_RECOVERY 1
#define AES_BLOCK_SIZE 128
int ticketBundlePtrsValid(BbTicketBundle *ticketBundle)
{
if(!ptrValid(ticketBundle, sizeof *ticketBundle, 4))
return 0;
if(!ptrValid(ticketBundle->ticket, sizeof *ticketBundle->ticket, 4))
return 0;
if(!certChainPtrsValid(ticketBundle->ticketChain))
return 0;
if(!certChainPtrsValid(ticketBundle->cmdChain))
return 0;
return 1;
}
int verifyTicket(BbTicketBundle *ticketBundle)
{
BbTicketHead *ticketHead = &(ticketBundle->ticket->head);
BbContentMetaDataHead *cmdh = &(ticketBundle->ticket->cmd.head);
BbAesKey sharedkey;
SkDataChain dataChain[2];
u8 decryptChunk[BB_CMD_ENCRYPTED_CHUNK_BYTES];
int ret;
/*
* insure ticket cert chains valid
*/
ret = verifyCertChain(ticketBundle->cmdChain, CHAIN_TYPE_CONTENT_PUB);
if(ret != SK_SUCCESS){
message("metadata cert chain did not verify\n");
return SK_API_FAIL;
}
ret = verifyCertChain(ticketBundle->ticketChain, CHAIN_TYPE_TICKET_PUB);
if(ret != SK_SUCCESS){
message("ticket cert chain did not verify\n");
return SK_API_FAIL;
}
/* first check bbid */
if(ticketHead->bbId != v2->bbId){
message("ticket id \n");
output_int32_hex(*((u32 *)(&(ticketHead->bbId))));
message("virage id \n");
output_int32_hex(*((u32 *)(&(v2->bbId))));
return SK_FAIL;
}
/* verify play limits */
if (ticketHead->tid >= BB_TICKET_ID_LIMITED) {
u16* cc = getCcSlot(ticketHead->tid);
/* only check time and count limits here */
if (!cc || (ticketHead->code < BB_LIMIT_CODE_EXTERN &&
*cc >= ticketHead->limit)) {
message("exceeded ticket limit\n");
return SK_FAIL;
}
}
/* verify ticket sig */
dataChain[0].data = (u8 *)ticketBundle->ticket;
dataChain[0].size = sizeof(BbTicket) - sizeof(BbRsaSig2048);
if(verifyRsaSigDataChain(dataChain, 1,
((BbRsaCert *)ticketBundle->ticketChain[0])->publicKey,
((BbRsaCert *)ticketBundle->ticketChain[0])->exponent,
BB_SIG_TYPE_RSA2048,
(BbGenericSig *)(ticketBundle->ticket->head).ticketSign)
<0 ){
message("Ticket sig verify failed.\n");
return SK_FAIL;
}
/*
* Verify ticket contents
*/
/* decrypt appropriate ticket cmdh sections and store globally */
memcpy((u8 *)&gCmdh, (u8 *)cmdh, sizeof(BbContentMetaDataHead));
eccGenAesKey(ticketHead->serverKey, v2->privateKey, sharedkey);
aes_SwDecrypt((u8 *)sharedkey,
(u8 *)ticketHead->cmdIv,
(u8 *)&(cmdh->key),
BB_CMD_ENCRYPTED_CHUNK_BYTES,
(u8 *)&(gCmdh.key));
message("decryption done\n");
/* verify cmd sig from cp server */
dataChain[0].data = (u8 *)&(ticketBundle->ticket->cmd);
dataChain[0].size = BB_CMD_DESC_SIZE;
dataChain[1].data = (u8 *)&gCmdh;
dataChain[1].size = BB_CMD_HEAD_SIGNED_BYTES;
/* hard-coding cert type here */
if( verifyRsaSigDataChain(dataChain, 2,
((BbRsaCert *)ticketBundle->cmdChain[0])->publicKey,
((BbRsaCert *)ticketBundle->cmdChain[0])->exponent,
BB_SIG_TYPE_RSA2048,
(BbGenericSig *)ticketBundle->ticket->cmd.head.contentMetaDataSign)
<0 )
return SK_FAIL;
/* decrypt encrypted portions (by content publisher) of cmd */
aes_SwDecrypt((u8 *)v2->bootAppKey,
(u8 *)cmdh->commonCmdIv,
(u8 *)&(gCmdh.key),
BB_CMD_ENCRYPTED_CHUNK_BYTES,
decryptChunk);
memcpy((u8 *)&(gCmdh.key),decryptChunk,BB_CMD_ENCRYPTED_CHUNK_BYTES);
return SK_API_SUCCESS;
}
int skGetId(BbId *id)
{
if (!ptrValid(id, sizeof *id, 4))
return SK_API_FAIL;
*id = v2->bbId;
return SK_API_SUCCESS;
}
int verifyLaunchBundle(BbTicketBundle *ticketBundle,
BbAppLaunchCrls *crls,
void *pKeyList,
int forRecrypt)
{
int ret;
if (!ticketBundlePtrsValid(ticketBundle))
return SK_API_FAIL;
if( forRecrypt &&
!(ticketBundle->ticket->cmd.head.execFlags&BB_CMD_EXEC_RECRYPT) ){
message("no reencryption required\n");
return SK_API_RECRYPT_NOT_REQUIRED;
}
/* internal crls ptrs checked later */
if (!ptrValid(crls, sizeof *crls, 4))
return SK_API_FAIL;
if (!ptrValid(pKeyList, SK_RECRYPT_KEYLIST_SIZE, 4))
return SK_API_FAIL;
if((ret = ticketBundleRevoked(ticketBundle, crls)))
return ret;
if((ret = verifyTicket(ticketBundle)))
return ret;
return SK_SUCCESS;
}
int skLaunchSetup(BbTicketBundle *ticketBundle,
BbAppLaunchCrls *crls,
void *pKeyList)
{
int ret;
BbTicketHead *ticketHead;
BbAesKey key;
PROFILE_OUT(Enter skLaunchSetup());
if((ret = verifyLaunchBundle(ticketBundle,crls,pKeyList,0))!=SK_SUCCESS)
return ret;
ticketHead = &(ticketBundle->ticket->head);
PROFILE_OUT(ticket verified);
if( gCmdh.execFlags&BB_CMD_EXEC_RECRYPT ){
message("needs reencryption\n");
if(pKeyList == 0){
return SK_API_FAIL;
}
ret = getRecryptKey(pKeyList, key, gCmdh.id);
PRINT_LONG(ret);
if(ret == SK_API_RECRYPT_COMPLETE){
/* set up for decryption */
aesHwInit(key, gCmdh.iv);
}
else{
/* needed but not complete (SK_API_RECRYPT_INCOMPLETE,
* SK_API_RECRYPT_NEW), or list error (SK_API_FAIL)
*/
message("recrypt not previously completed.\n");
return ret;
}
}
else{
/* setup hw aes for decryption */
message("no recryption needed.\n");
aesHwInit(gCmdh.key,gCmdh.iv);
}
/* store ticket head locally as persistent state */
memcpy((u8 *)&gTikh, (u8 *)ticketHead , sizeof(BbTicketHead));
/* setup AES for ensuing compatible-mode dma by sysapp and app */
IO_WRITE(PI_AES_CTRL_REG,0);
PRINT_LONG(gCmdh.size);
return SK_API_SUCCESS;
}
int skLaunch(u32 addr)
{
message("Enter skLaunch\n");
if (!ptrValid((void *)addr, 0, 4)) {
return SK_API_FAIL;
}
if(!(gCmdh.execFlags&BB_CMD_EXEC_RECRYPT)){
BbShaHash appHash;
if (!ptrValid(((u8 *)addr)-4096, gCmdh.size, 4)) {
return SK_API_FAIL;
}
SHA1Reset(&gSha);
SHA1Input(&gSha,((u8 *)addr)-4096,gCmdh.size);
SHA1Result(&gSha,(u8 *)appHash);
if(memcmp(appHash, gCmdh.hash, sizeof appHash)){
message("app hash mismatch\n");
return SK_API_FAIL;
}
message("app hash matches\n");
}
gSavedCode = BB_LIMIT_CODE_INVAL;
//#define LIMIT_EVERYTHING
#ifdef LIMIT_EVERYTHING
{
u32 r[4];
getRandoms(r, 4);
gLimit = 2;
gTime = SK_TIMER_SEC_TO_TIC(110+(r[0]%10));
//output_int32_hex(r[0]%10);
gSavedCode = BB_LIMIT_CODE_TIME2;
}
#else
/* install play limits */
gLimit = gTikh.limit; /* set unconditionally for parental control */
if (gTikh.tid >= BB_TICKET_ID_LIMITED) {
u16* cc = getCcSlot(gTikh.tid);
if (!cc || *cc >= gTikh.limit) {
message("exceeded ticket limit\n");
return SK_FAIL;
}
gSavedCode = gTikh.code;
if (gTikh.code == BB_LIMIT_CODE_TIME || gTikh.code == BB_LIMIT_CODE_TIME2) {
int tmp = (gLimit - *cc + 3)/4;
gTime = SK_TIMER_SEC_TO_TIC(*cc*60);
gSavedCc = *cc;
if (tmp > 0) *cc += tmp;
else *cc = gLimit;
gSavedTid = gTikh.tid;
} else { /* treat all others like code == BB_LIMIT_CODE_COUNT */
(*cc)++;
}
v01Update(&v01);
} else if (gLimit) /* parental control */
gSavedCode = gTikh.code;
#endif
setAccessRights(&gCmdh);
/* check for rev 1.1 version of BCP */
if (IO_READ(PI_GPIO_REG) & PI_ID_BOARD_REV_MASK){
/* enable power button trap */
IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_CLR_BUT);
IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_SET_BUT);
IO_WRITE(MI_SEC_MODE_REG, IO_READ(MI_SEC_MODE_REG) |
MI_SEC_MODE_BUT_TRAP_EN);
}
/* set up timer for 1 second interrupts @ 96Mhz */
/*XXXblythe handle other clock speeds?*/
/* do this last to minimize chance of timer expiring before we exit */
if (gSavedCode == BB_LIMIT_CODE_TIME || gSavedCode == BB_LIMIT_CODE_TIME2)
IO_WRITE(MI_SEC_TIMER_REG, SK_TIMER_PRESCALE| SK_TIMER_TICS);
/* never actually return via $ra, so setup return to the
* boot entrance to entry.s to handle transfer of cpu to app.
* register v0 ($2) must hold app's entry address.
*/
asm(".set noreorder; move $2, %0;la $8, __sk_exit_app_launch; "
"jr $8; nop; .set reorder"
: /* no outputs */
: "r" (addr)
: "$2");
/* never get here!!! */
return SK_API_FAIL;
}
int skRecryptListValid(void *pKeyList)
{
if (!ptrValid(pKeyList, SK_RECRYPT_KEYLIST_SIZE, 4)) {
return SK_API_FAIL;
}
return isValidList(pKeyList);
}
int skRecryptBegin(BbTicketBundle *ticketBundle,
BbAppLaunchCrls *crls,
void *pKeyList)
{
BbTicketHead *ticketHead = &(ticketBundle->ticket->head);
BbAesKey key;
int ret;
PROFILE_OUT(Enter skRecryptBegin);
if((ret = verifyLaunchBundle(ticketBundle,crls,pKeyList,1))!=SK_SUCCESS)
return ret;
PROFILE_OUT(ticket verified);
ret = getRecryptKey(pKeyList, key, gCmdh.id);
if( ret == SK_API_RECRYPT_INCOMPLETE){
gRecryptState = RECRYPT_RECOVERY;
aesHwInit(key,gCmdh.iv);
}
else{
/* SK_API_RECRYPT_COMPLETE || SK_API_RECRYPT_NEW */
gRecryptState = RECRYPT_DATA;
/* setup hw aes for decryption */
aesHwInit(gCmdh.key,gCmdh.iv);
PROFILE_OUT(Generated new key);
/* init sw encryption cipher */
aesCipherInit(&gRecryptCipher, AES_MODE_CBC,(u8 *)gCmdh.iv);
updateRecryptEntry(pKeyList, gCmdh.id, SK_API_RECRYPT_INCOMPLETE);
}
/* set gRecryptKey */
aesMakeKey(&gRecryptKey, AES_DIR_ENCRYPT, AES_BLOCK_SIZE, (u8 *)key);
SHA1Reset(&gSha);
/* store ticket head locally as persistent state */
memcpy((u8 *)&gTikh, (u8 *)ticketHead , sizeof(BbTicketHead));
gBytesProcessed = 0;
gRecryptHwAesChain = 0;
PROFILE_OUT(Leaving skRecryptBegin);
#ifdef SK_STACK_CHECK
message("stack usage = ");
output_int32_hex(stackCheckUsage());
message("\n");
#endif
return ret;
}
int processRecryptBlock(u8 *data, u32 size,u32 recrypt)
{
int i;
int bufSize = PI_BUFFER_DATA_SIZE/2;
for(i=0; i<size; i+= bufSize){
if (piBufDma(0,KSEG_TO_PHYS(data),bufSize,
SK_DMA_DRAM_TO_BUF)<0){
//message("BUF DMA FAIL\n");
/* TODO: need marker that this recryption will not work? */
return SK_API_FAIL;
}
/* decrypt, using hardware iv chaining */
aesStart(0, gRecryptHwAesChain);
gRecryptHwAesChain=1;
POLL_AES_BUSY;
if(bufSize<=(gCmdh.size-gBytesProcessed)){
SHA1Input(&gSha,(u8 *)PHYS_TO_K1(PI_BUFFER_0_START),bufSize);
gBytesProcessed += bufSize;
}
else{
SHA1Input(&gSha,(u8 *)PHYS_TO_K1(PI_BUFFER_0_START),
gCmdh.size-gBytesProcessed);
gBytesProcessed = gCmdh.size;
}
if(recrypt){
/* encrypt with gRecryptKey, output back to buf */
/* XXX: use kseg0 iram instead of kseg1 pibuf ??? */
aesBlockEncrypt(&gRecryptCipher,
&gRecryptKey,
(u8 *)PHYS_TO_K1(PI_BUFFER_0_START),
(bufSize)*8,
data);
}
data+=bufSize;
}
return SK_API_SUCCESS;
}
/* input data chunk by chunk */
int skRecryptData(u8 *data, u32 size)
{
if(data!=NULL && !ptrValid(data, size, 1))
return SK_API_FAIL;
PROFILE_OUT(Enter skRecryptData);
if(gRecryptState == RECRYPT_RECOVERY){
/* must reprime aes engine (hw and sw) */
u8 *swiv = (u8 *)gRecryptRecoverIv;
u32 *hwiv = (u32 *)(data + size - sizeof(BbAesIv));
/* init sw encryption cipher.
* iv comes from last 16B of data in unless data=NULL. this
* is case where previous failure before any data actually
* processed, but keylist shows incomplete. */
if(data==NULL){
swiv = (u8 *)gCmdh.iv;
hwiv = gCmdh.iv;
}
aesCipherInit(&gRecryptCipher, AES_MODE_CBC, swiv);
aesHwInit(gCmdh.key, hwiv);
gRecryptHwAesChain = 0;
gRecryptState = RECRYPT_DATA;
return SK_API_SUCCESS;
}
processRecryptBlock(data, size, 1);
return SK_API_SUCCESS;
}
/* when recovering from aborted recryption attempt, this call must
* be used to feed the already recrypted data before calling
* skRecryptData to finish.
*/
int skRecryptComputeState(u8 *data, u32 size)
{
u32 *src = (u32 *)(data + size - sizeof(BbAesIv));
if (!ptrValid(data, size, 1))
return SK_API_FAIL;
PROFILE_OUT(Enter skRecryptComputeState);
/* store last 16B for iv when change over to skRecryptData calls */
memcpy(gRecryptRecoverIv, src, sizeof(BbAesIv));
/* process block to compute hash */
processRecryptBlock(data, size, 0);
return SK_API_SUCCESS;
}
/* end recyrption and determine success */
int skRecryptEnd(void *pKeyList)
{
int ret;
BbShaHash appHash;
if (!ptrValid(pKeyList, SK_RECRYPT_KEYLIST_SIZE, 4))
return SK_API_FAIL;
/* compare hash to value from ticket */
SHA1Result(&gSha,(u8 *)appHash);
#ifdef DEBUG
output_int32_array(appHash, 5);
#endif
if (memcmp(appHash, gCmdh.hash, sizeof appHash)) {
message("recrypt hash mismatch\n");
return SK_API_FAIL;
}
if((ret=updateRecryptEntry(pKeyList, gCmdh.id,
SK_API_RECRYPT_COMPLETE) != SK_API_SUCCESS)){
message("failed to mark list.\n");
return SK_API_FAIL;
}
return SK_API_SUCCESS;
}
int skSignHash(BbShaHash hash, BbEccSig sign)
{
if (!ptrValid(hash, sizeof(BbShaHash), 4)) return SK_API_FAIL;
if (!ptrValid(sign, sizeof(BbEccSig), 4)) return SK_API_FAIL;
/* only ECC type supported */
computeEccSig((u8 *) hash, sizeof(BbShaHash), v2->privateKey, sign,
API_IDENTITY);
return SK_API_SUCCESS;
}
int skVerifyHash(BbShaHash hash, BbGenericSig *sign, BbCertBase *certChain[],
BbAppLaunchCrls *crls)
{
int i;
int res;
BbEccPublicKey pubkey;
if (!ptrValid(hash, sizeof(BbShaHash), 4))
return SK_API_FAIL;
if(certChain){
/* verify cert chain first since common for both types of
* signing cert.
*/
if(!certChainPtrsValid(certChain)){
return SK_API_FAIL;
}
if(verifyCertChain(certChain, CHAIN_TYPE_DONT_CARE) < 0) {
return SK_API_FAIL;
}
/* insure rls valid */
if(!rlPtrsValid(crls))
return SK_API_FAIL;
if(SK_SUCCESS != validateRls(&crls->carl, v01.caCrlVersion,
&crls->cprl, v01.cpCrlVersion,
&crls->tsrl, v01.tsCrlVersion))
return SK_API_FAIL;
/* insure cert chain ca is on revoked list */
if(RL_EXISTS(crls->carl)){
for(i=0; i<crls->carl.head->numberRevoked; i++){
if(strstr(certChain[1]->name.server,
crls->carl.list[i])!=NULL)
return SK_API_REVOKED_SERVER;
}
}
if(((BbCertBase *)certChain[0])->certType == BB_CERT_TYPE_SERVER){
/* must check cprl and tsrl for server case */
if(RL_EXISTS(crls->cprl)){
for(i=0; i<crls->cprl.head->numberRevoked; i++){
if(strstr(certChain[0]->name.server,
crls->cprl.list[i])!=NULL)
return SK_API_REVOKED_SERVER;
}
}
if(RL_EXISTS(crls->tsrl)){
for(i=0; i<crls->tsrl.head->numberRevoked; i++){
if(strstr(certChain[0]->name.server,
crls->tsrl.list[i])!=NULL)
return SK_API_REVOKED_SERVER;
}
}
return verifyRsaSigFromHash(hash,
((BbRsaCert *)certChain[0])->publicKey,
((BbRsaCert *)certChain[0])->exponent,
BB_SIG_TYPE_RSA2048,
sign);
} else {
/* its of ECC type (we have no manufacturing server rl) */
res = verifyEccSig((u8 *)hash, sizeof(BbShaHash),
((BbEccCert *)certChain[0])->publicKey,
sign->ecc, API_IDENTITY);
if(res != SK_API_SUCCESS) {
return SK_API_FAIL;
}
}
} else {
/* no cert chain : verify using this BB's public key */
getBbPublicKey(pubkey);
res = verifyEccSig((u8 *)hash, sizeof(BbShaHash), pubkey, sign->ecc,
API_IDENTITY);
if (res != SK_API_SUCCESS) {
return SK_API_FAIL;
}
}
return SK_API_SUCCESS;
}
/*
* limited play support
*/
int skGetConsumption(u16* tidWindow, u16 cc[BB_MAX_CC])
{
if (!ptrValid(tidWindow, sizeof *tidWindow, 2) ||
!ptrValid(cc, sizeof v01.cc, 2))
return SK_API_FAIL;
*tidWindow = v01.tidWindow;
memcpy(cc, v01.cc, sizeof v01.cc);
return SK_API_SUCCESS;
}
int skAdvanceTicketWindow(void)
{
int i;
/* copy counters down one slot */
/*XXXblythe overlapped memcpy?*/
for(i = 0; i < BB_MAX_CC-1; i++) {
v01.cc[i] = v01.cc[i+1];
}
v01.cc[i] = 0;
v01.tidWindow++;
return v01Update(&v01);
}
int skSetLimit(u16 limit, u16 code)
{
/* use issuer name to determine whether we have completed launch setup */
if (!gTikh.issuer[0]) return SK_API_FAIL;
if (gTikh.tid >= BB_TICKET_ID_LIMITED) {
/* allow code and limit override for extern */
if (gTikh.code >= BB_LIMIT_CODE_EXTERN) {
gTikh.code = code;
gTikh.limit = limit;
} else if (limit < gTikh.limit) {
/* allow limit override for parental control */
gTikh.limit = limit;
}
} else {
/* permanent game, allow a limit to be set */
gTikh.code = code;
gTikh.limit = limit;
}
return SK_API_SUCCESS;
}
int skExit(void)
{
skReboot();
return 0; /* not reached */
}
int skKeepAlive(void)
{
u32 diff = SK_TIMER_TICS - (IO_READ(MI_SEC_TIMER_REG)>>16);
u32 sec;
static u32 utime;
if (gSavedCode != BB_LIMIT_CODE_TIME2) return 0;
utime += diff;
if (utime > SK_TIMER_TICS) {
gTime++;
utime -= SK_TIMER_TICS;
}
sec = SK_TIMER_TIC_TO_SEC(gTime);
gSavedCc = (sec+30)/60;
if (sec >= gLimit*60) {
message("die\n");
gExtraBits = MI_SEC_MODE_TIMER;
skReboot();
}
IO_WRITE(MI_SEC_TIMER_REG, SK_TIMER_PRESCALE|SK_TIMER_TICS);
return 0;
}
#ifdef SK_DEVELOP
/* some simple functions for testing */
int skDumpVirage(u8 *v)
{
if (!ptrValid(v, sizeof(BbVirage2)+2*sizeof(BbVirage01), 1))
return SK_API_FAIL;
/* copy to dram */
wmemcpy(v, v2, sizeof(BbVirage2)/4);
v += sizeof(BbVirage2);
wmemcpy(v, (void*)PHYS_TO_K1(VIRAGE1_RAM_START), sizeof(BbVirage01)/4);
v += sizeof(BbVirage01);
wmemcpy(v, (void*)PHYS_TO_K1(VIRAGE0_RAM_START), sizeof(BbVirage01)/4);
return SK_API_SUCCESS;
}
int skTest2(int a,int b)
{
#ifdef DEBUG
IO_WRITE(MI_SEC_TIMER_REG,((40000<<16)|1));
#endif
return a-b;
}
int skTest3(int a,int b)
{
return a*b;
}
/* test api call to check RLs
*/
int skValidateRls(BbCrlBundle *carl,
BbCrlBundle *cprl,
BbCrlBundle *tsrl,
u32 *versions){
int ret;
if(versions==NULL)
return SK_API_FAIL;
ret = validateRls(carl, versions[0],
cprl, versions[1],
tsrl, versions[2]);
versions[0] = v01.caCrlVersion;
versions[1] = v01.cpCrlVersion;
versions[2] = v01.tsCrlVersion;
return ret;
}
/* test api call that returns random numbers: can get only 16 byte multiples
* size is in bytes
*/
int skGetRandomKeyData(u8 * keydata, int size)
{
return getRandoms( (u32 *) keydata, size/4);
}
int skResetWindow(void)
{
#if 1
int seq = v01.seq;
memset(&v01, 0, sizeof v01);
v01.seq = seq;
#else
memset(v01.cc, 0, sizeof v01.cc);
v01.tidWindow = 0;
#endif
return v01Update(&v01);
}
#endif