forked from gfto/oscam
/
reader-viaccess.c
1073 lines (934 loc) · 31.1 KB
/
reader-viaccess.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
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "globals.h"
#ifdef READER_VIACCESS
#include "oscam-aes.h"
#include "oscam-time.h"
#include "reader-common.h"
struct via_date {
uint16_t day_s : 5;
uint16_t month_s : 4;
uint16_t year_s : 7;
uint16_t day_e : 5;
uint16_t month_e : 4;
uint16_t year_e : 7;
};
static void parse_via_date(const uchar *buf, struct via_date *vd, int32_t fend)
{
uint16_t date;
date = (buf[0]<<8) | buf[1];
vd->day_s = date & 0x1f;
vd->month_s = (date>>5) & 0x0f;
vd->year_s = (date>>9) & 0x7f;
if( fend )
{
date = (buf[2]<<8) | buf[3];
vd->day_e = date & 0x1f;
vd->month_e = (date>>5) & 0x0f;
vd->year_e = (date>>9) & 0x7f;
}
}
//static void get_via_data(const uchar *b, int32_t l, time_t *start_t, time_t *end_t, uchar *cls)
//{
// int32_t i, j;
// struct via_date vd;
// struct tm tm;
// memset(&vd, 0, sizeof(struct via_date));
//
// // b -> via date (4 bytes)
// b+=4;
// l-=4;
//
// j=l-1;
// for (; j>=0; j--)
// for (i=0; i<8; i++)
// if (b[j] & (1 << (i&7)))
// {
// parse_via_date(b-4, &vd, 1);
// *cls=(l-(j+1))*8+i;
// }
//
// memset(&tm, 0, sizeof(struct tm));
// tm.tm_year = vd.year_s + 80; //via year starts in 1980, tm_year starts in 1900
// tm.tm_mon = vd.month_s - 1; // january is 0 in tm_mon
// tm.tm_mday = vd.day_s;
// *start_t = mktime(&tm);
//
// tm.tm_year = vd.year_e + 80;
// tm.tm_mon = vd.month_e - 1;
// tm.tm_mday = vd.day_e;
// *end_t = mktime(&tm);
//
//}
static void show_class(struct s_reader *reader, const char *p, uint32_t provid, const uchar *b, int32_t l)
{
int32_t i, j;
// b -> via date (4 bytes)
b+=4;
l-=4;
j=l-1;
for (; j>=0; j--)
for (i=0; i<8; i++)
if (b[j] & (1 << (i&7)))
{
uchar cls;
struct via_date vd;
parse_via_date(b-4, &vd, 1);
cls=(l-(j+1))*8+i;
if (p)
rdr_log(reader, "%sclass: %02X, expiry date: %04d/%02d/%02d - %04d/%02d/%02d", p, cls,
vd.year_s+1980, vd.month_s, vd.day_s,
vd.year_e+1980, vd.month_e, vd.day_e);
else {
rdr_log(reader, "class: %02X, expiry date: %04d/%02d/%02d - %04d/%02d/%02d", cls,
vd.year_s+1980, vd.month_s, vd.day_s,
vd.year_e+1980, vd.month_e, vd.day_e);
time_t start_t, end_t;
struct tm tm;
//convert time:
memset(&tm, 0, sizeof(tm));
tm.tm_year = vd.year_s+80; //via year starts in 1980, tm_year starts in 1900
tm.tm_mon = vd.month_s-1; // january is 0 in tm_mon
tm.tm_mday = vd.day_s;
start_t = cs_timegm(&tm);
tm.tm_year = vd.year_e+80; //via year starts in 1980, tm_year starts in 1900
tm.tm_mon = vd.month_e-1; // january is 0 in tm_mon
tm.tm_mday = vd.day_e;
end_t = cs_timegm(&tm);
cs_add_entitlement(reader, reader->caid, provid, cls, cls, start_t, end_t, 5);
}
}
}
static void show_subs(struct s_reader * reader, const uchar *emm)
{
// emm -> A9, A6, B6
switch( emm[0] )
{
case 0xA9:
show_class(reader, "nano A9: ", 0, emm+2, emm[1]);
break;
/*
{
int32_t i, j, byts;
const uchar *oemm;
oemm = emm;
byts = emm[1]-4;
emm+=6;
j=byts-1;
for( ; j>=0; j-- )
for( i=0; i<8; i++ )
if( emm[j] & (1 << (i&7)) )
{
uchar cls;
struct via_date vd;
parse_via_date(emm-4, &vd, 1);
cls=(byts-(j+1))*8+i;
rdr_log(reader, "%sclass %02X: expiry date: %02d/%02d/%04d - %02d/%02d/%04d",
fnano?"nano A9: ":"", cls,
vd.day_s, vd.month_s, vd.year_s+1980,
vd.day_e, vd.month_e, vd.year_e+1980);
}
break;
}
*/
case 0xA6:
{
char szGeo[256];
memset(szGeo, 0, 256);
strncpy(szGeo, (char *)emm+2, emm[1]);
rdr_log(reader, "nano A6: geo %s", szGeo);
break;
}
case 0xB6:
{
uchar m; // modexp
struct via_date vd;
m=emm[emm[1]+1];
parse_via_date(emm+2, &vd, 0);
rdr_log(reader, "nano B6: modexp %d%d%d%d%d%d: %02d/%02d/%04d", (m&0x20)?1:0,
(m&0x10)?1:0,(m&0x08)?1:0,(m&0x04)?1:0,(m&0x02)?1:0,(m&0x01)?1:0,
vd.day_s, vd.month_s, vd.year_s+1980);
break;
}
}
}
static int32_t chk_prov(struct s_reader * reader, uchar *id, uchar keynr)
{
int32_t i, j, rc;
for (rc=i=0; (!rc) && (i<reader->nprov); i++)
if(!memcmp(&reader->prid[i][1], id, 3))
for (j=0; (!rc) && (j<16); j++)
if (reader->availkeys[i][j]==keynr)
rc=1;
return(rc);
}
static int32_t unlock_parental(struct s_reader * reader)
{
/* disabling parental lock. assuming pin "0000" if no pin code is provided in the config */
static const uchar inDPL[] = {0xca, 0x24, 0x02, 0x00, 0x09};
uchar cmDPL[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F};
def_resp;
if (strcmp(reader->pincode, "none")) {
rdr_log(reader, "Using PIN %s",reader->pincode);
// the pin need to be coded in bcd, so we need to convert from ascii to bcd, so '1234' -> 0x12 0x34
cmDPL[6]=((reader->pincode[0]-0x30)<<4) | ((reader->pincode[1]-0x30) & 0x0f);
cmDPL[7]=((reader->pincode[2]-0x30)<<4) | ((reader->pincode[3]-0x30) & 0x0f);
}
else {
rdr_log(reader, "Using PIN 0000!");
}
write_cmd(inDPL,cmDPL);
if( !(cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0) ) {
if (strcmp(reader->pincode, "none")) {
rdr_log(reader, "Can't disable parental lock. Wrong PIN? OSCam used %s!",reader->pincode);
}
else {
rdr_log(reader, "Can't disable parental lock. Wrong PIN? OSCam used 0000!");
}
}
else
rdr_log(reader, "Parental lock disabled");
return 0;
}
static int32_t viaccess_card_init(struct s_reader * reader, ATR *newatr)
{
get_atr;
def_resp;
int32_t i;
uchar buf[256];
uchar insac[] = { 0xca, 0xac, 0x00, 0x00, 0x00 }; // select data
uchar insb8[] = { 0xca, 0xb8, 0x00, 0x00, 0x00 }; // read selected data
uchar insa4[] = { 0xca, 0xa4, 0x00, 0x00, 0x00 }; // select issuer
uchar insc0[] = { 0xca, 0xc0, 0x00, 0x00, 0x00 }; // read data item
static const uchar insFAC[] = { 0x87, 0x02, 0x00, 0x00, 0x03 }; // init FAC
static const uchar FacDat[] = { 0x00, 0x00, 0x28 };
static unsigned char ins8702_data[] = { 0x00, 0x00, 0x11};
static unsigned char ins8704[] = { 0x87, 0x04, 0x00, 0x00, 0x07 };
static unsigned char ins8706[] = { 0x87, 0x06, 0x00, 0x00, 0x04 };
if ((atr[1]!=0x77) || ((atr[2]!=0x18) && (atr[2]!=0x11) && (atr[2]!=0x19)) || ((atr[9]!=0x68) && (atr[9]!=0x6C)))
return ERROR;
write_cmd(insFAC, FacDat);
if( !(cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0) )
return ERROR;
memset(&reader->last_geo, 0, sizeof(reader->last_geo));
write_cmd(insFAC, ins8702_data);
if ((cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0x00)) {
write_cmd(ins8704, NULL);
if ((cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0x00)) {
write_cmd(ins8706, NULL);
if ((cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0x00)) {
reader->last_geo.number_ecm =(cta_res[2]<<8) | (cta_res[3]);
rdr_log(reader, "using ecm #%x for long viaccess ecm",reader->last_geo.number_ecm);
}
}
}
// switch((atr[atrsize-4]<<8)|atr[atrsize-3])
// {
// case 0x6268: ver="2.3"; break;
// case 0x6668: ver="2.4(?)"; break;
// case 0xa268:
// default: ver="unknown"; break;
// }
reader->caid=0x500;
memset(reader->prid, 0xff, sizeof(reader->prid));
insac[2]=0xa4; write_cmd(insac, NULL); // request unique id
insb8[4]=0x07; write_cmd(insb8, NULL); // read unique id
memcpy(reader->hexserial, cta_res+2, 5);
// rdr_log(reader, "[viaccess-reader] type: Viaccess, ver: %s serial: %llu", ver, b2ll(5, cta_res+2));
rdr_log_sensitive(reader, "type: Viaccess (%sstandard atr), caid: %04X, serial: {%llu}",
atr[9]==0x68?"":"non-",reader->caid, (unsigned long long) b2ll(5, cta_res+2));
i=0;
insa4[2]=0x00; write_cmd(insa4, NULL); // select issuer 0
buf[0]=0;
while((cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0))
{
insc0[4]=0x1a; write_cmd(insc0, NULL); // show provider properties
cta_res[2]&=0xF0;
reader->prid[i][0]=0;
memcpy(&reader->prid[i][1], cta_res, 3);
memcpy(&reader->availkeys[i][0], cta_res+10, 16);
snprintf((char *)buf+strlen((char *)buf), sizeof(buf)-strlen((char *)buf), ",%06X", b2i(3, &reader->prid[i][1]));
//rdr_log(reader, "[viaccess-reader] buf: %s", buf);
insac[2]=0xa5; write_cmd(insac, NULL); // request sa
insb8[4]=0x06; write_cmd(insb8, NULL); // read sa
memcpy(&reader->sa[i][0], cta_res+2, 4);
/*
insac[2]=0xa7; write_cmd(insac, NULL); // request name
insb8[4]=0x02; write_cmd(insb8, NULL); // read name nano + len
l=cta_res[1];
insb8[4]=l; write_cmd(insb8, NULL); // read name
cta_res[l]=0;
rdr_log(reader, "[viaccess-reader] name: %s", cta_res);
*/
insa4[2]=0x02;
write_cmd(insa4, NULL); // select next issuer
i++;
}
reader->nprov=i;
rdr_log(reader, "providers: %d (%s)", reader->nprov, buf+1);
if (cfg.ulparent)
unlock_parental(reader);
rdr_log(reader, "ready for requests");
return OK;
}
bool dcw_crc(uchar *dw){
int8_t i;
for(i=0;i<16;i+=4) if(dw[i+3]!=((dw[i]+dw[i+1]+dw[i+2])& 0xFF))return 0;
return 1;
}
static int32_t viaccess_do_ecm(struct s_reader * reader, const ECM_REQUEST *er, struct s_ecm_answer *ea)
{
def_resp;
static const unsigned char insa4[] = { 0xca,0xa4,0x04,0x00,0x03 }; // set provider id
unsigned char ins88[] = { 0xca,0x88,0x00,0x00,0x00 }; // set ecm
unsigned char insf8[] = { 0xca,0xf8,0x00,0x00,0x00 }; // set geographic info
static const unsigned char insc0[] = { 0xca,0xc0,0x00,0x00,0x12 }; // read dcw
// //XXX what is the 4th byte for ??
int32_t ecm88Len = MIN(MAX_ECM_SIZE-4, SCT_LEN(er->ecm)-4);
if(ecm88Len < 1){
rdr_log(reader, "ECM: Size of ECM couldn't be correctly calculated.");
return ERROR;
}
uchar ecmData[ecm88Len];
memset(ecmData, 0, ecm88Len);
memcpy(ecmData, er->ecm+4, ecm88Len);
uchar *ecm88Data = &ecmData[0];
uint32_t provid=0;
int32_t rc=0;
int32_t hasD2 = 0;
int32_t curEcm88len=0;
int32_t nanoLen=0;
uchar *nextEcm;
uchar DE04[256];
int32_t D2KeyID=0;
int32_t curnumber_ecm=0;
//nanoD2 d2 02 0d 02 -> D2 nano, len 2
// 0d -> post AES decrypt CW
// 0b -> pre AES decrypt CW
int32_t nanoD2 = 0; // 0x0b = 1 0x0d = 2
memset(DE04, 0, sizeof(DE04)); //fix dorcel de04 bug
nextEcm=ecm88Data;
while (ecm88Len>0 && !rc) {
if(ecm88Data[0] ==0x00 && ecm88Data[1] == 0x00) {
// nano 0x00 and len 0x00 aren't valid ... something is obviously wrong with this ecm.
rdr_log(reader, "ECM: Invalid ECM structure. Rejecting");
return ERROR;
}
// 80 33 nano 80 (ecm) + len (33)
if(ecm88Data[0]==0x80) { // nano 80, give ecm len
curEcm88len=ecm88Data[1];
nextEcm=ecm88Data+curEcm88len+2;
ecm88Data += 2;
ecm88Len -= 2;
}
if(!curEcm88len) { //there was no nano 80 -> simple ecm
curEcm88len=ecm88Len;
}
// d2 02 0d 02 -> D2 nano, len 2, select the AES key to be used
if(ecm88Data[0]==0xd2) {
// test if need post or pre AES decrypt
if(ecm88Data[2]==0x0b)
{
nanoD2 = 1;
rdr_debug_mask(reader, D_READER, "ECM: nano D2 0x0b");
}
if(ecm88Data[2]==0x0d)
{
nanoD2 = 2;
rdr_debug_mask(reader, D_READER, "ECM: nano D2 0x0d");
}
// use the d2 arguments to get the key # to be used
int32_t len = ecm88Data[1] + 2;
D2KeyID=ecm88Data[3];
ecm88Data += len;
ecm88Len -= len;
curEcm88len -=len;
hasD2 = 1;
}
else
hasD2 = 0;
// 40 07 03 0b 00 -> nano 40, len =7 ident 030B00 (tntsat), key #0 <== we're pointing here
// 09 -> use key #9
// 05 67 00
if ((ecm88Data[0]==0x90 || ecm88Data[0]==0x40) && (ecm88Data[1]==0x03 || ecm88Data[1]==0x07 ) )
{
uchar ident[3], keynr;
uchar *ecmf8Data=0;
int32_t ecmf8Len=0;
nanoLen=ecm88Data[1] + 2;
keynr=ecm88Data[4]&0x0F;
// 40 07 03 0b 00 -> nano 40, len =7 ident 030B00 (tntsat), key #0 <== we're pointing here
// 09 -> use key #9
if(nanoLen>5) {
curnumber_ecm =(ecm88Data[6]<<8) | (ecm88Data[7]);
rdr_debug_mask(reader, D_READER, "checking if the ecm number (%x) match the card one (%x)",curnumber_ecm,reader->last_geo.number_ecm);
// if we have an ecm number we check it.
// we can't assume that if the nano len is 5 or more we have an ecm number
// as some card don't support this
if( reader->last_geo.number_ecm > 0 ) {
if (reader->last_geo.number_ecm == curnumber_ecm && !( ecm88Data[nanoLen-1] == 0x01 && (ecm88Data[2] == 0x03 && ecm88Data[3] == 0x0B && ecm88Data[4] == 0x00 ) )) {
keynr=ecm88Data[5];
rdr_debug_mask(reader, D_READER, "keyToUse = %02x, ECM ending with %02x",ecm88Data[5], ecm88Data[nanoLen-1]);
} else {
if( ecm88Data[nanoLen-1] == 0x01 && (ecm88Data[2] == 0x03 && ecm88Data[3] == 0x0B && ecm88Data[4] == 0x00 ) )
{
rdr_debug_mask(reader, D_READER, "Skip ECM ending with = %02x for ecm number (%x) for provider %02x%02x%02x",ecm88Data[nanoLen-1], curnumber_ecm, ecm88Data[2], ecm88Data[3], ecm88Data[4]);
}
rdr_debug_mask(reader, D_READER, "Skip ECM ending with = %02x for ecm number (%x)",ecm88Data[nanoLen-1], curnumber_ecm);
ecm88Data=nextEcm;
ecm88Len-=curEcm88len;
continue; //loop to next ecm
}
}
else { // long ecm but we don't have an ecm number so we have to try them all.
keynr=ecm88Data[5];
rdr_debug_mask(reader, D_READER, "keyToUse = %02x",ecm88Data[5]);
}
}
memcpy (ident, &ecm88Data[2], sizeof(ident));
provid = b2i(3, ident);
ident[2]&=0xF0;
if(hasD2 && reader->aes_list) {
// check that we have the AES key to decode the CW
// if not there is no need to send the ecm to the card
if(!aes_present(reader->aes_list, 0x500, (uint32_t) (provid & 0xFFFFF0) , D2KeyID))
return ERROR;
}
if (!chk_prov(reader, ident, keynr))
{
rdr_debug_mask(reader, D_READER, "ECM: provider or key not found on card");
snprintf( ea->msglog, MSGLOGSIZE, "provider(%02x%02x%02x) or key(%d) not found on card", ident[0],ident[1],ident[2], keynr );
return ERROR;
}
ecm88Data+=nanoLen;
ecm88Len-=nanoLen;
curEcm88len-=nanoLen;
// DE04
if (ecm88Data[0]==0xDE && ecm88Data[1]==0x04)
{
memcpy (DE04, &ecm88Data[0], 6);
ecm88Data+=6;
}
//
if( reader->last_geo.provid != provid )
{
reader->last_geo.provid = provid;
reader->last_geo.geo_len = 0;
reader->last_geo.geo[0] = 0;
write_cmd(insa4, ident); // set provider
}
//Nano D2 0x0b Pre AES decrypt CW
if ( hasD2 && nanoD2 == 1)
{
uchar *ecm88DataCW = ecm88Data;
int32_t cwStart = 0;
//int32_t cwStartRes = 0;
int32_t must_exit = 0;
// find CW start
while(cwStart < curEcm88len -1 && !must_exit)
{
if(ecm88Data[cwStart] == 0xEA && ecm88Data[cwStart+1] == 0x10)
{
ecm88DataCW = ecm88DataCW + cwStart + 2;
must_exit = 1;
}
cwStart++;
}
// use AES from list to decrypt CW
rdr_debug_mask(reader, D_READER, "Decoding CW : using AES key id %d for provider %06x",D2KeyID, (provid & 0xFFFFF0));
if (aes_decrypt_from_list(reader->aes_list,0x500, (uint32_t) (provid & 0xFFFFF0), D2KeyID, &ecm88DataCW[0], 16) == 0)
snprintf( ea->msglog, MSGLOGSIZE, "AES Decrypt : key id %d not found for CAID %04X , provider %06x", D2KeyID, 0x500, (provid & 0xFFFFF0) );
}
while(ecm88Len>1 && ecm88Data[0]<0xA0)
{
nanoLen=ecm88Data[1]+2;
if (!ecmf8Data)
ecmf8Data=(uchar *)ecm88Data;
ecmf8Len+=nanoLen;
ecm88Len-=nanoLen;
curEcm88len-=nanoLen;
ecm88Data+=nanoLen;
}
if(ecmf8Len)
{
if( reader->last_geo.geo_len!=ecmf8Len ||
memcmp(reader->last_geo.geo, ecmf8Data, reader->last_geo.geo_len))
{
memcpy(reader->last_geo.geo, ecmf8Data, ecmf8Len);
reader->last_geo.geo_len= ecmf8Len;
insf8[3]=keynr;
insf8[4]=ecmf8Len;
write_cmd(insf8, ecmf8Data);
}
}
ins88[2]=ecmf8Len?1:0;
ins88[3]=keynr;
ins88[4]= curEcm88len;
//
// we should check the nano to make sure the ecm is valid
// we should look for at least 1 E3 nano, 1 EA nano and the F0 signature nano
//
// DE04
if (DE04[0]==0xDE)
{
uint32_t l = curEcm88len-6;
if (l > 256 || curEcm88len <= 6) { //don't known if this is ok...
rdr_log(reader, "ecm invalid/too long! len=%d", curEcm88len);
return ERROR;
}
memcpy(DE04+6, (uchar *)ecm88Data, l);
write_cmd(ins88, DE04); // request dcw
}
else
{
write_cmd(ins88, (uchar *)ecm88Data); // request dcw
}
//
write_cmd(insc0, NULL); // read dcw
switch(cta_res[0])
{
case 0xe8: // even
if(cta_res[1]==8) { memcpy(ea->cw,cta_res+2,8); rc=1; }
break;
case 0xe9: // odd
if(cta_res[1]==8) { memcpy(ea->cw+8,cta_res+2,8); rc=1; }
break;
case 0xea: // complete
if(cta_res[1]==16) { memcpy(ea->cw,cta_res+2,16); rc=1; }
break;
default :
ecm88Data=nextEcm;
ecm88Len-=curEcm88len;
rdr_debug_mask(reader, D_READER, "ECM: key to use is not the current one, trying next ECM");
snprintf( ea->msglog, MSGLOGSIZE, "key to use is not the current one, trying next ECM" );
}
}
else {
//ecm88Data=nextEcm;
//ecm88Len-=curEcm88len;
rdr_debug_mask(reader, D_READER, "ECM: Unknown ECM type");
snprintf( ea->msglog, MSGLOGSIZE, "Unknown ECM type" );
return ERROR; /*Lets interupt the loop and exit, because we don't know this ECM type.*/
}
}
if ( hasD2 && !dcw_crc(ea->cw) && nanoD2 == 2) {
rdr_debug_mask(reader, D_READER, "Decoding CW : using AES key id %d for provider %06x",D2KeyID, (provid & 0xFFFFF0));
rc=aes_decrypt_from_list(reader->aes_list,0x500, (uint32_t) (provid & 0xFFFFF0), D2KeyID,ea->cw, 16);
if( rc == 0 )
snprintf( ea->msglog, MSGLOGSIZE, "AES Decrypt : key id %d not found for CAID %04X , provider %06x", D2KeyID, 0x500, (provid & 0xFFFFF0) );
}
return(rc?OK:ERROR);
}
static int32_t viaccess_get_emm_type(EMM_PACKET *ep, struct s_reader * rdr)
{
uint32_t provid=0;
rdr_debug_mask(rdr, D_EMM, "Entered viaccess_get_emm_type ep->emm[0]=%02x",ep->emm[0]);
if (ep->emm[3] == 0x90 && ep->emm[4] == 0x03) {
provid = ep->emm[5] << 16 | ep->emm[6] << 8 | (ep->emm[7] & 0xFE);
i2b_buf(4, provid, ep->provid);
}
switch (ep->emm[0]) {
case 0x88:
ep->type=UNIQUE;
memset(ep->hexserial, 0, 8);
memcpy(ep->hexserial, ep->emm + 4, 4);
rdr_debug_mask(rdr, D_EMM, "UNIQUE");
return(!memcmp(rdr->hexserial + 1, ep->hexserial, 4));
case 0x8A:
case 0x8B:
ep->type=GLOBAL;
rdr_debug_mask(rdr, D_EMM, "GLOBAL");
return 1;
case 0x8C:
case 0x8D:
ep->type=SHARED;
rdr_debug_mask(rdr, D_EMM, "SHARED (part)");
return 0;
case 0x8E:
ep->type=SHARED;
memset(ep->hexserial, 0, 8);
memcpy(ep->hexserial, ep->emm + 3, 3);
rdr_debug_mask(rdr, D_EMM, "SHARED");
//check for provider as serial (cccam only?)
int8_t i;
for (i=0;i<rdr->nprov;i++) {
if (!memcmp(&rdr->prid[i][1], ep->hexserial, 3))
return 1;
}
return(!memcmp(&rdr->sa[0][0], ep->hexserial, 3));
default:
ep->type = UNKNOWN;
rdr_debug_mask(rdr, D_EMM, "UNKNOWN");
return 1;
}
}
static void viaccess_get_emm_filter(struct s_reader * rdr, uchar *filter)
{
int32_t idx = 2;
filter[0]=0xFF;
filter[1]=0;
filter[idx++]=EMM_GLOBAL;
filter[idx++]=0;
filter[idx+0] = 0x8D;
filter[idx+0+16] = 0xFE;
//filter[idx+6] = 0xA0; // FIXME: dummy, flood client with EMM's
//filter[idx+6+16] = 0xF0;
filter[1]++;
idx += 32;
filter[idx++]=EMM_SHARED;
filter[idx++]=0;
filter[idx+0] = 0x8E;
filter[idx+0+16] = 0xFF;
memcpy(filter+idx+1, &rdr->sa[0][0], 3);
memset(filter+idx+1+16, 0xFF, 3);
filter[1]++;
idx += 32;
filter[idx++]=EMM_UNIQUE;
filter[idx++]=0;
filter[idx+0] = 0x88;
filter[idx+0+16] = 0xFF;
memcpy(filter+idx+1, rdr->hexserial + 1, 4);
memset(filter+idx+1+16, 0xFF, 4);
filter[1]++;
return;
}
static int32_t viaccess_do_emm(struct s_reader * reader, EMM_PACKET *ep)
{
def_resp;
static const unsigned char insa4[] = { 0xca,0xa4,0x04,0x00,0x03 }; // set provider id
unsigned char insf0[] = { 0xca,0xf0,0x00,0x01,0x22 }; // set adf
unsigned char insf4[] = { 0xca,0xf4,0x00,0x01,0x00 }; // set adf, encrypted
unsigned char ins18[] = { 0xca,0x18,0x01,0x01,0x00 }; // set subscription
unsigned char ins1c[] = { 0xca,0x1c,0x01,0x01,0x00 }; // set subscription, encrypted
static const unsigned char insc8[] = { 0xca,0xc8,0x00,0x00,0x02 }; // read extended status
// static const unsigned char insc8Data[] = { 0x00,0x00 }; // data for read extended status
int32_t emmdatastart=7;
if (ep->type == UNIQUE) emmdatastart++;
int32_t emmLen=SCT_LEN(ep->emm)-emmdatastart;
int32_t rc=0;
///cs_dump(ep->emm, emmLen+emmdatastart, "RECEIVED EMM VIACCESS");
int32_t emmUpToEnd;
uchar *emmParsed = ep->emm+emmdatastart;
int32_t provider_ok = 0;
uint32_t emm_provid;
uchar keynr = 0;
int32_t ins18Len = 0;
uchar ins18Data[512];
uchar insData[512];
uchar *nano81Data = 0;
uchar *nano91Data = 0;
uchar *nano92Data = 0;
uchar *nano9EData = 0;
uchar *nanoF0Data = 0;
for (emmUpToEnd=emmLen; (emmParsed[1] != 0) && (emmUpToEnd > 0); emmUpToEnd -= (2 + emmParsed[1]), emmParsed += (2 + emmParsed[1])) {
///cs_dump (emmParsed, emmParsed[1] + 2, "NANO");
if (emmParsed[0]==0x90 && emmParsed[1]==0x03) {
/* identification of the service operator */
uchar soid[3], ident[3], i;
for (i=0; i<3; i++) {
soid[i]=ident[i]=emmParsed[2+i];
}
ident[2]&=0xF0;
emm_provid=b2i(3, ident);
keynr=soid[2]&0x0F;
if (chk_prov(reader, ident, keynr)) {
provider_ok = 1;
} else {
rdr_log(reader, "EMM: provider or key not found on card (%x, %x)", emm_provid, keynr);
return ERROR;
}
// check if the provider changes. If yes, set the new one. If not, don't .. card will return an error if we do.
if( reader->last_geo.provid != emm_provid ) {
write_cmd(insa4, ident);
if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
cs_dump(insa4, 5, "set provider cmd:");
cs_dump(soid, 3, "set provider data:");
rdr_log(reader, "update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
return ERROR;
}
}
// as we are maybe changing the used provider, clear the cache, so the next ecm will re-select the correct one
reader->last_geo.provid = 0;
reader->last_geo.geo_len = 0;
reader->last_geo.geo[0] = 0;
}
else if (emmParsed[0]==0x9e && emmParsed[1]==0x20) {
/* adf */
if (!nano91Data) {
/* adf is not crypted, so test it */
uchar custwp;
uchar *afd;
custwp=reader->sa[0][3];
afd=(uchar*)emmParsed+2;
if( afd[31-custwp/8] & (1 << (custwp & 7)) )
rdr_debug_mask(reader, D_READER, "emm for our card %08X", b2i(4, &reader->sa[0][0]));
else
return SKIPPED;
}
// memorize
nano9EData = emmParsed;
} else if (emmParsed[0]==0x81) {
nano81Data = emmParsed;
} else if (emmParsed[0]==0x91 && emmParsed[1]==0x08) {
nano91Data = emmParsed;
} else if (emmParsed[0]==0x92 && emmParsed[1]==0x08) {
nano92Data = emmParsed;
} else if (emmParsed[0]==0xF0 && emmParsed[1]==0x08) {
nanoF0Data = emmParsed;
} else {
/* other nanos */
show_subs(reader, emmParsed);
memcpy(ins18Data+ins18Len, emmParsed, emmParsed[1] + 2);
ins18Len += emmParsed [1] + 2;
}
}
if (!provider_ok) {
rdr_debug_mask(reader, D_READER, "provider not found in emm, continue anyway");
// force key to 1...
keynr = 1;
///return ERROR;
}
if (!nanoF0Data) {
cs_dump(ep->emm, ep->emmlen, "can't find 0xf0 in emm...");
return ERROR; // error
}
if (nano9EData) {
if (!nano91Data) {
// set adf
insf0[3] = keynr; // key
insf0[4] = nano9EData[1] + 2;
write_cmd(insf0, nano9EData);
if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
cs_dump(insf0, 5, "set adf cmd:");
cs_dump(nano9EData, insf0[4] , "set adf data:");
rdr_log(reader, "update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
return ERROR;
}
} else {
// set adf crypte
insf4[3] = keynr; // key
insf4[4] = nano91Data[1] + 2 + nano9EData[1] + 2;
memcpy (insData, nano91Data, nano91Data[1] + 2);
memcpy (insData + nano91Data[1] + 2, nano9EData, nano9EData[1] + 2);
write_cmd(insf4, insData);
if(( cta_res[cta_lr-2]!=0x90 && cta_res[cta_lr-2]!=0x91) || cta_res[cta_lr-1]!=0x00 ) {
cs_dump(insf4, 5, "set adf encrypted cmd:");
cs_dump(insData, insf4[4], "set adf encrypted data:");
rdr_log(reader, "update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
return ERROR;
}
}
}
if (!nano92Data) {
// send subscription
ins18[2] = nano9EData ? 0x01: 0x00; // found 9E nano ?
ins18[3] = keynr; // key
ins18[4] = ins18Len + nanoF0Data[1] + 2;
memcpy (insData, ins18Data, ins18Len);
memcpy (insData + ins18Len, nanoF0Data, nanoF0Data[1] + 2);
write_cmd(ins18, insData);
if( (cta_res[cta_lr-2]==0x90 || cta_res[cta_lr-2]==0x91) && cta_res[cta_lr-1]==0x00 ) {
rdr_debug_mask(reader, D_READER, "update successfully written");
rc=1; // written
} else {
cs_dump(ins18, 5, "set subscription cmd:");
cs_dump(insData, ins18[4], "set subscription data:");
rdr_log(reader, "update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
}
} else {
// send subscription encrypted
if (!nano81Data) {
cs_dump(ep->emm, ep->emmlen, "0x92 found, but can't find 0x81 in emm...");
return ERROR; // error
}
ins1c[2] = nano9EData ? 0x01: 0x00; // found 9E nano ?
if (ep->type == UNIQUE) ins1c[2] = 0x02;
ins1c[3] = keynr; // key
ins1c[4] = nano92Data[1] + 2 + nano81Data[1] + 2 + nanoF0Data[1] + 2;
memcpy (insData, nano92Data, nano92Data[1] + 2);
memcpy (insData + nano92Data[1] + 2, nano81Data, nano81Data[1] + 2);
memcpy (insData + nano92Data[1] + 2 + nano81Data[1] + 2, nanoF0Data, nanoF0Data[1] + 2);
write_cmd(ins1c, insData);
if( (cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0x00) ) {
rdr_log(reader, "update successfully written");
rc=1; // written
}
else {
if( cta_res[cta_lr-2]&0x1 )
rdr_log(reader, "update not written. Data already exists or unknown address");
//if( cta_res[cta_lr-2]&0x8 ) {
write_cmd(insc8, NULL);
if( (cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0x00) ) {
rdr_log(reader, "extended status %02X %02X", cta_res[0], cta_res[1]);
}
//}
return ERROR;
}
}
/*
Sub Main()
Sc.Write("CA A4 04 00 03")
RX
Sc.Write("02 07 11")
RX
Sc.Write("CA F0 00 01 22")
RX
Sc.Write("9E 20")
Sc.Write("10 10 08 8A 80 00 04 00 10 10 26 E8 54 80 1E 80")
Sc.Write("00 01 00 00 00 00 00 50 00 00 80 02 22 00 08 50")
RX
Sc.Write("CA 18 01 01 11")
RX
Sc.Write("A9 05 34 DE 34 FF 80")
Sc.Write("F0 08 1A 3E AF B5 2B EE E3 3B")
RX
End Sub
*/
return rc;
}
static int32_t viaccess_card_info(struct s_reader * reader)
{
def_resp;
int32_t i, l;
uchar insac[] = { 0xca, 0xac, 0x00, 0x00, 0x00 }; // select data
uchar insb8[] = { 0xca, 0xb8, 0x00, 0x00, 0x00 }; // read selected data
uchar insa4[] = { 0xca, 0xa4, 0x00, 0x00, 0x00 }; // select issuer
uchar insc0[] = { 0xca, 0xc0, 0x00, 0x00, 0x00 }; // read data item
static const uchar ins24[] = { 0xca, 0x24, 0x00, 0x00, 0x09 }; // set pin
static const uchar cls[] = { 0x00, 0x21, 0xff, 0x9f};
static const uchar pin[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04};
reader->last_geo.provid = 0;
reader->last_geo.geo_len = 0;
reader->last_geo.geo[0] = 0;
rdr_log(reader, "card detected");
cs_clear_entitlement(reader); //reset the entitlements
// set pin
write_cmd(ins24, pin);
insac[2]=0xa4; write_cmd(insac, NULL); // request unique id
insb8[4]=0x07; write_cmd(insb8, NULL); // read unique id
rdr_log_sensitive(reader, "serial: {%llu}", (unsigned long long) b2ll(5, cta_res+2));
insa4[2]=0x00; write_cmd(insa4, NULL); // select issuer 0
for (i=1; (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0); i++)
{
uint32_t l_provid, l_sa;
uchar l_name[64];
insc0[4]=0x1a; write_cmd(insc0, NULL); // show provider properties
cta_res[2]&=0xF0;
l_provid=b2i(3, cta_res);
insac[2]=0xa5; write_cmd(insac, NULL); // request sa
insb8[4]=0x06; write_cmd(insb8, NULL); // read sa
l_sa=b2i(4, cta_res+2);
insac[2]=0xa7; write_cmd(insac, NULL); // request name
insb8[4]=0x02; write_cmd(insb8, NULL); // read name nano + len
l=cta_res[1];
insb8[4]=l; write_cmd(insb8, NULL); // read name
cta_res[l]=0;
trim((char *)cta_res);
if (cta_res[0])
snprintf((char *)l_name, sizeof(l_name), ", name: %s", cta_res);
else
l_name[0]=0;
// read GEO
insac[2]=0xa6; write_cmd(insac, NULL); // request GEO
insb8[4]=0x02; write_cmd(insb8, NULL); // read GEO nano + len
l=cta_res[1];
char tmp[l*3+1];
insb8[4]=l; write_cmd(insb8, NULL); // read geo
rdr_log_sensitive(reader, "provider: %d, id: {%06X%s}, sa: {%08X}, geo: %s",
i, l_provid, l_name, l_sa, (l<4) ? "empty" : cs_hexdump(1, cta_res, l, tmp, sizeof(tmp)));
// read classes subscription
insac[2]=0xa9; insac[4]=4;
write_cmd(insac, cls); // request class subs
while( (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0) )
{
insb8[4]=0x02; write_cmd(insb8, NULL); // read class subs nano + len
if( (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0) )
{
l=cta_res[1];
insb8[4]=l; write_cmd(insb8, NULL); // read class subs
if( (cta_res[cta_lr-2]==0x90) &&
(cta_res[cta_lr-1]==0x00 || cta_res[cta_lr-1]==0x08) )
{
show_class(reader, NULL, l_provid, cta_res, cta_lr-2);
}
}
}
insac[4]=0;
insa4[2]=0x02;
write_cmd(insa4, NULL); // select next provider
}
//return ERROR;
return OK;
}
#ifdef HAVE_DVBAPI
void dvbapi_sort_nanos(unsigned char *dest, const unsigned char *src, int32_t len);
int32_t viaccess_reassemble_emm(uchar *buffer, uint32_t *len) {
static uchar emm_global[512];
static int32_t emm_global_len = 0;
int32_t pos=0, i;
uint32_t k;
// Viaccess
if (*len>500) return 0;
switch(buffer[0]) {
case 0x8c:
case 0x8d:
// emm-s part 1