-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathDeCSA.cpp
1430 lines (1288 loc) · 38.9 KB
/
DeCSA.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
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
/*
* vdr-plugin-dvbapi - softcam dvbapi plugin for VDR
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* ----------------- FASTECM description:
The encrypted control word is broadcast in an ECM approximately once every two seconds
The Control Word used to encrypt the transport stream packets are changed regularly,
usually every 10 seconds.If the Control Words change stops for whatever reason the STBs can use the same Control Word
to decrypt the incoming signal until the problem is fixed.This is a serious security issue.
In each PID header there are 2 bits telling the decoder if the Odd or Even Control Word should be used.The ECM
normally contains two Control Words.This mechanism allows the ECM to carry both the Control Word currently used
and the Control Word which will be used for scrambling the next time the Control Word changes.This ensures that the
STB always has the Control Word needed to descramble the content.
Sky Germany only has one Control Word.
We can see the CW around 620ms before it shoul be used.
*/
#include "DeCSA.h"
#include "Log.h"
#include "cscrypt/des.h"
DeCSA *decsa = NULL;
#define lldcast long long int
bool IsFastECMCAID(int caCaid)
{
if (caCaid == 0x09C4 || caCaid == 0x098C || caCaid == 0x098D || caCaid == 0x09AF || //SKY DE 09AF is OBSOLETE
caCaid == 0x09CD || //Sky IT
caCaid == 0x0963) //Sky UK
{
return true;
}
return false;
}
class cMutexLockHelper {
private:
cMutexLock *pmutexLock;
cMutex *pmutex;
public:
cMutexLockHelper(cMutex *Mutex = NULL, bool block = true)
{
pmutexLock = NULL;
pmutex = Mutex;
if (block)
pmutexLock = new cMutexLock(pmutex);
};
~cMutexLockHelper()
{
if (pmutexLock) delete pmutexLock;
pmutexLock = NULL;
pmutex = NULL;
}
void UnLock()
{
if (pmutexLock != NULL) delete pmutexLock;
pmutexLock = NULL;
}
void ReLock()
{
if (pmutexLock == NULL)
pmutexLock = new cMutexLock(pmutex);
}
};
bool CheckNull(const unsigned char *data, int len)
{
while (--len >= 0)
if (data[len])
return false;
return true;
}
#ifdef LIBSSL
struct aes_keys_t
{
AES_KEY even;
AES_KEY odd;
};
void aes_set_control_words(void *aeskeys, const unsigned char *ev, const unsigned char *od)
{
AES_set_decrypt_key(ev, 128, &((struct aes_keys_t *) aeskeys)->even);
AES_set_decrypt_key(od, 128, &((struct aes_keys_t *) aeskeys)->odd);
}
void * aes_get_key_struct(void)
{
struct aes_keys_t *aeskeys = (struct aes_keys_t *) malloc(sizeof(struct aes_keys_t));
if (aeskeys)
{
static const unsigned char packet[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
aes_set_control_words(aeskeys, packet, packet);
}
return aeskeys;
}
void aes_free_key_struct(void *aeskeys)
{
if (aeskeys)
free(aeskeys);
}
#endif
DeCSAKey::DeCSAKey()
{
Aes = false;
index = -1;
#ifndef LIBDVBCSA
key = NULL;
#else
cs_key_even = NULL;
cs_key_odd = NULL;
#endif
#ifdef LIBSSL
csa_aes_key = NULL;
#endif
lastcwlog = 0;
cwSeen = 0;
}
DeCSAKey::~DeCSAKey()
{
#ifndef LIBDVBCSA
if (key)
{
cMutexLock lock(&mutexKEY);
free_key_struct(key);
}
key = NULL;
#else
{
cMutexLock lock(&mutexKEY);
if (cs_key_even)
dvbcsa_bs_key_free(cs_key_even);
cs_key_even = NULL;
if (cs_key_odd)
dvbcsa_bs_key_free(cs_key_odd);
cs_key_odd = NULL;
}
#endif
#ifdef LIBSSL
if (csa_aes_key)
{
cMutexLock lock(&mutexKEY);
aes_free_key_struct(csa_aes_key);
}
csa_aes_key = NULL;
#endif
}
void DeCSAKey::SetAes(uint32_t usedAes)
{
cMutexLock lock(&mutexKEY);
Aes = usedAes;
}
uint32_t DeCSAKey::GetAes()
{
cMutexLock lock(&mutexKEY);
return Aes;
}
void DeCSAKey::SetAlgo(uint32_t usedAlgo)
{
cMutexLock lock(&mutexKEY);
algo = usedAlgo;
}
uint32_t DeCSAKey::GetAlgo()
{
cMutexLock lock(&mutexKEY);
return algo;
}
#ifdef LIBSSL
bool DeCSAKey::GetorCreateAesKeyStruct()
{
cMutexLock lock(&mutexKEY);
if (!csa_aes_key)
{
DEBUGLOG("GetorCreateAesKeyStruct - keyindex:%d", index);
csa_aes_key = aes_get_key_struct();
}
return csa_aes_key != 0;
}
#endif
bool DeCSAKey::GetorCreateKeyStruct()
{
cMutexLock lock(&mutexKEY);
#ifndef LIBDVBCSA
if (!key)
{
DEBUGLOG("GetorCreateKeyStruct - keyindex:%d", index);
key = get_key_struct();
}
return key != 0;
#else
if (!cs_key_even)
{
DEBUGLOG("GetorCreateKeyStruct - even, keyindex:%d", index);
cs_key_even = dvbcsa_bs_key_alloc();
}
if (!cs_key_odd)
{
DEBUGLOG("GetorCreateKeyStruct - odd, keyindex:%d", index);
cs_key_odd = dvbcsa_bs_key_alloc();
}
return (cs_key_even != 0) && (cs_key_odd != 0);
#endif
}
bool DeCSAKey::CWExpired()
{
cMutexLock lock(&mutexKEY);
if (CheckExpiredCW)
{
time_t tnow = time(NULL);
if (CheckExpiredCW && cwSeen > 0 && (tnow - cwSeen) > MAX_KEY_WAIT)
{
if ((tnow - lastcwlog) > 10)
{
lastcwlog = tnow;
DEBUGLOG("%s: CheckExpiredCW key is expired", __FUNCTION__);
}
return true;
}
else
{
lastcwlog = tnow;
}
}
return false;
}
#ifndef LIBDVBCSA
bool DeCSAKey::SetFastECMCaidSid(int caid, int sid)
{
cMutexLock lock(&mutexKEY);
if (key)
{
setFastECMCaidSid(key, caid,sid);
return true;
}
return false;
}
int DeCSAKey::Set_FastECM_CW_Parity(int pid, int parity, bool bforce, int& oldparity, bool& bfirsttimecheck, bool& bnextparityset, bool& bactivparitypatched)
{
cMutexLock lock(&mutexKEY);
if (key)
{
return set_FastECM_CW_Parity(key, pid, parity, bforce, oldparity, bfirsttimecheck, bnextparityset, bactivparitypatched);
}
return 1;
}
void DeCSAKey::SetFastECMPid(int pid)
{
cMutexLock lock(&mutexKEY);
if (key)
{
setFastECMPid(key, pid);
}
}
void DeCSAKey::Get_FastECM_CAID(int* caid)
{
cMutexLock lock(&mutexKEY);
*caid = 0;
if (key)
get_FastECM_CAID(key,caid);
}
void DeCSAKey::Get_FastECM_SID(int* caSid)
{
cMutexLock lock(&mutexKEY);
*caSid = 0;
if (key)
{
get_FastECM_SID(key, caSid);
}
}
void DeCSAKey::Get_FastECM_PID(int* caPid)
{
cMutexLock lock(&mutexKEY);
*caPid = 0;
if (key)
{
get_FastECM_PID(key, caPid);
}
}
bool DeCSAKey::Get_FastECM_struct(FAST_ECM& fecm)
{
cMutexLock lock(&mutexKEY);
if (key)
{
FAST_ECM* fe=get_FastECM_struct(key);
fecm = *fe;
return true;
}
return false;
}
bool DeCSAKey::GetActiveParity(int pid, int& aparity, int& aparity2)
{
cMutexLock lock(&mutexKEY);
if (key)
{
getActiveParity(key,pid, aparity, aparity2);
return true;
}
return false;
}
void DeCSAKey::InitFastEcmOnCaid(int Caid)
{
cMutexLock lock(&mutexKEY);
if (key)
{
struct FAST_ECM* sf = get_FastECM_struct(key);
if (sf && Caid == sf->csaCaid)
{
sf->oddparityTime = 0;
sf->evenparityTime = 0;
sf->nextparity = 0;
sf->activparity.clear();
sf->activparity2.clear();
}
}
}
void DeCSAKey::SetActiveParity2(int pid,int parity2)
{
cMutexLock lock(&mutexKEY);
if (key)
{
FAST_ECM* fe = get_FastECM_struct(key);
fe->activparity2[pid] = parity2;
}
}
int DeCSAKey::Decrypt_packets(unsigned char **cluster)
{
cMutexLock lock(&mutexKEY);
if (key)
{
return decrypt_packets(key, cluster);
}
else
{
DEBUGLOG("%s: ind:%d Decrypt_packets key is null", __FUNCTION__, index);
}
return 0;
}
bool DeCSAKey::Get_control_words(unsigned char *even, unsigned char *odd)
{
cMutexLock lock(&mutexKEY);
if (key)
{
get_control_words(key, even, odd);
return true;
}
return false;
}
#endif
void DeCSAKey::Des(uint8_t* data, unsigned char parity)
{
cMutexLock lock(&mutexKEY);
des(data, des_key_schedule[parity], 0);
}
void DeCSAKey::Des_set_key(const unsigned char *cw, unsigned char parity)
{
cMutexLock lock(&mutexKEY);
cwSeen = time(NULL);
des_set_key(cw, des_key_schedule[parity]);
}
bool DeCSAKey::Set_even_control_word(const unsigned char *even, const unsigned char ecm)
{
cMutexLock lock(&mutexKEY);
#ifndef LIBDVBCSA
if (key)
{
set_even_control_word(key, even, ecm);
return true;
}
#else
if (cs_key_even)
{
#ifdef LIBDVBCSA_NEW
dvbcsa_bs_key_set_ecm(ecm, even, cs_key_even); //libdvbcsa must be upgraded to support this.
#else
dvbcsa_bs_key_set(even, cs_key_even);
#endif
return true;
}
#endif
return false;
}
bool DeCSAKey::Set_odd_control_word(const unsigned char *odd, const unsigned char ecm)
{
cMutexLock lock(&mutexKEY);
#ifndef LIBDVBCSA
if (key)
{
set_odd_control_word(key, odd, ecm);
return true;
}
#else
if (cs_key_odd)
{
#ifdef LIBDVBCSA_NEW
dvbcsa_bs_key_set_ecm(ecm, odd, cs_key_odd); //libdvbcsa must be upgraded to support this.
#else
dvbcsa_bs_key_set(odd, cs_key_odd);
#endif
return true;
}
#endif
return false;
}
#ifndef LIBDVBCSA
void DeCSAKey::Init_Parity2(bool binitcsa)
{
cMutexLock lock(&mutexKEY);
if (key)
{
if (binitcsa)
DEBUGLOG("Init_Parity keyindex:%d", index);
else
DEBUGLOG("Init_Parity from Timeout keyindex:%d", index);
Init_FastECM(key, binitcsa);
}
}
#endif
DeCSAAdapter::DeCSAAdapter()
{
cardindex = -1;
bCW_Waiting = false;
bAbort = false;
#ifndef LIBDVBCSA
csnew = get_suggested_cluster_size();
rangenew = MALLOC(unsigned char *, (csnew * 2 + 5));
#else
cs = dvbcsa_bs_batch_size();
cs_tsbbatch_even = reinterpret_cast<dvbcsa_bs_batch_s *>(malloc((cs + 1) * sizeof(struct dvbcsa_bs_batch_s)));
cs_tsbbatch_odd = reinterpret_cast<dvbcsa_bs_batch_s *>(malloc((cs + 1) * sizeof(struct dvbcsa_bs_batch_s)));
#endif
}
DeCSAAdapter::~DeCSAAdapter()
{
cMutexLockHelper lockDecrypt(&mutexDecrypt);
#ifndef LIBDVBCSA
free(rangenew);
#else
free(cs_tsbbatch_even);
free(cs_tsbbatch_odd);
#endif
}
void DeCSAAdapter::CancelWait()
{
if (bCW_Waiting)
{
DEBUGLOG("%s: decsa CW Waiting", __FUNCTION__);
bAbort = true;
cMutexLock lock(&mutexStopDecrypt);
bAbort = false;
DEBUGLOG("%s: decsa CW Waiting Aborted", __FUNCTION__);
}
}
#ifndef LIBDVBCSA
void DeCSAAdapter::Init_Parity(DeCSAKey *keys, int sid, int slot,bool bdelete)
{
cMutexLock lock(&mutexAdapter);
if (sid < 0 && slot < 0) return;
if (sid >= 0)
DEBUGLOG("Init_Parity cardindex:%d SID %d (0x%04X)", cardindex, sid, sid);
else
DEBUGLOG("Init_Parity cardindex:%d Slot %d", cardindex, slot);
bool bEND = false;
do
{
bEND = true;
map<int, unsigned char>::iterator it;
for (it = AdapterPidMap.begin(); it != AdapterPidMap.end(); ++it)
{
int ipid = it->first;
int iidx = it->second;
int caCaid = -1;
int caSid = -1;
int caPid = -1;
keys[iidx].Get_FastECM_CAID(&caCaid);
keys[iidx].Get_FastECM_SID(&caSid);
keys[iidx].Get_FastECM_PID(&caPid);
DEBUGLOG("Init_Parity cardindex:%d iidx:%d sid:%d caSid:%d pid:%d caCaid:0x%04X caPid:%d", cardindex,iidx,sid, caSid, ipid,caCaid,caPid);
if (sid >= 0 && caSid == sid)
{
keys[iidx].Init_Parity2();
if (bdelete)
{
DEBUGLOG("Init_Parity delete AdapterPidMap1 cardindex:%d keyindex:%d pid:%d", cardindex, iidx, ipid);
AdapterPidMap.erase(ipid);
bEND = false;
break;
}
}
else if (slot >= 0 && slot == iidx)
{
keys[iidx].Init_Parity2();
DEBUGLOG("Init_Parity delete AdapterPidMap2 cardindex:%d keyindex:%d pid:%d", cardindex, iidx, ipid);
AdapterPidMap.erase(ipid);
bEND = false;
break;
}
}
}
while (bEND == false);
//DebugLogPidmap();
}
#endif
int DeCSAAdapter::SearchPIDinMAP(int pid)
{
cMutexLock lock(&mutexAdapter);
//we must search for pid, otherwise on tune start we use always idx 0
//int idx = -1;
map<int, unsigned char>::iterator it;
for (it = AdapterPidMap.begin(); it != AdapterPidMap.end(); ++it)
{
if (it->first == pid)
{
return it->second;
}
}
//DEBUGLOG("%s: pid not found in m_pidmap cardindex:%d pid:%d(0x%04X) l:%d len:%d", __FUNCTION__, adapter_index,pid,pid, l,len);
return -1;
}
void DeCSAAdapter::SetCaPid(int pid, int index)
{
cMutexLock lock(&mutexAdapter);
DEBUGLOG("%s: SetCaPid cardindex:%d pid:%d index:%d", __FUNCTION__, cardindex, pid, index);
AdapterPidMap[pid] = index == -1 ? 0 : index;
}
#ifndef LIBDVBCSA
void DeCSAAdapter::SetDVBAPIPid(DeCSA* parent,int slot, int dvbapiPID)
{
if (dvbapiPID >= 0 && slot >= 0 && slot<MAX_CSA_IDX)
{
cMutexLock lock(&mutexAdapter);
int idxOK = -1;
map<int, unsigned char>::iterator it;
for (it = AdapterPidMap.begin(); it != AdapterPidMap.end(); ++it)
{
//int ipid = it->first;
int iidx = it->second;
if (iidx == slot)
{
idxOK = iidx;
break;
}
}
//slot not found - create it later in DeCSA::SetCaPid
if (idxOK < 0)
{
idxOK = slot;
}
parent->SetFastECMPid(cardindex, idxOK, slot, dvbapiPID);
}
else
{
}
}
#endif
DeCSA::DeCSA()
{
for (int i = 0;i < MAXADAPTER;i++)
DeCSAArray[i].cardindex = i;
for (int i = 0;i < MAX_CSA_IDX;i++)
DeCSAKeyArray[i].index = i;
ResetState();
}
DeCSA::~DeCSA()
{
}
void DeCSA::ResetState(void)
{
DEBUGLOG("%s", __FUNCTION__);
}
bool DeCSA::GetKeyStruct(int idx)
{
if (idx >= 0 && idx<MAX_CSA_IDX)
{
return DeCSAKeyArray[idx].GetorCreateKeyStruct();
}
return false;
}
#ifdef LIBSSL
bool DeCSA::GetKeyStructAes(int idx)
{
if (idx >= 0)
{
return DeCSAKeyArray[idx].GetorCreateAesKeyStruct();
}
return false;
}
#endif
bool DeCSA::SetDescr(ca_descr_t *ca_descr, bool initial, int adapter_index)
{
DEBUGLOG("%s addapter:%d", __FUNCTION__, adapter_index);
cMutexLock lock(&mutex);
int idx = ca_descr->index;
if (idx < MAX_CSA_IDX && GetKeyStruct(idx))
{
#ifndef LIBDVBCSA
FAST_ECM fecm;
DeCSAKeyArray[idx].Get_FastECM_struct(fecm);
uint64_t now = GetTick();
uint64_t evendelta = -1;
if (fecm.evenparityTime > 0)
evendelta = now - fecm.evenparityTime;
uint64_t odddelta = -1;
if (fecm.oddparityTime > 0)
odddelta = now - fecm.oddparityTime;
unsigned char cweven[8];
unsigned char cwodd[8];
DeCSAKeyArray[idx].Get_control_words(cweven, cwodd);
DEBUGLOG("keyindex:%d adapter:%d EVENKEYOLD: CW: %02x %02x %02x %02x %02x %02x %02x %02x deltams:%lld nextparity:%d csaSid:%04x csaCaid:%04x csaPid:%04x",
idx, adapter_index,
cweven[0], cweven[1], cweven[2], cweven[3], cweven[4], cweven[5], cweven[6], cweven[7],
(lldcast)evendelta, fecm.nextparity, fecm.csaSid, fecm.csaCaid, fecm.csaPid);
DEBUGLOG("keyindex:%d adapter:%d ODDKEYOLD: CW: %02x %02x %02x %02x %02x %02x %02x %02x deltams:%lld nextparity:%d csaSid:%04x csaCaid:%04x csaPid:%04x",
idx, adapter_index,
cwodd[0], cwodd[1], cwodd[2], cwodd[3], cwodd[4], cwodd[5], cwodd[6], cwodd[7],
(lldcast)odddelta, fecm.nextparity, fecm.csaSid, fecm.csaCaid, fecm.csaPid);
#endif
DEBUGLOG("keyindex:%d adapter:%d %4s CW key set index:%d CW: %02x %02x %02x %02x %02x %02x %02x %02x initial:%d",
idx, adapter_index,
ca_descr->parity ? "odd" : "even", ca_descr->index,
ca_descr->cw[0], ca_descr->cw[1], ca_descr->cw[2], ca_descr->cw[3], ca_descr->cw[4], ca_descr->cw[5], ca_descr->cw[6], ca_descr->cw[7],
initial);
DeCSAKeyArray[idx].Des_set_key(ca_descr->cw, ca_descr->parity);
unsigned char ecm = filter->GetECM(adapter_index, ca_descr);
if (ca_descr->parity == 0)
DeCSAKeyArray[idx].Set_even_control_word(ca_descr->cw,ecm);
else
DeCSAKeyArray[idx].Set_odd_control_word(ca_descr->cw,ecm);
}
return true;
}
bool DeCSA::SetDescrAes(ca_descr_aes_t *ca_descr_aes, bool initial)
{
DEBUGLOG("%s", __FUNCTION__);
#ifdef LIBSSL
cMutexLock lock(&mutex);
int idx = ca_descr_aes->index;
if (idx < MAX_CSA_IDX && GetKeyStructAes(idx))
{
DEBUGLOG("%d: %4s aes key set", idx, ca_descr_aes->parity ? "odd" : "even");
if (ca_descr_aes->parity == 0)
{
if (DeCSAKeyArray[idx].csa_aes_key)
AES_set_decrypt_key(ca_descr_aes->cw, 128, &((struct aes_keys_t *) DeCSAKeyArray[idx].csa_aes_key)->even);
}
else
{
if (DeCSAKeyArray[idx].csa_aes_key)
AES_set_decrypt_key(ca_descr_aes->cw, 128, &((struct aes_keys_t *) DeCSAKeyArray[idx].csa_aes_key)->odd);
}
}
#endif
return true;
}
bool DeCSA::SetData(ca_descr_data_t *ca_descr_data, bool initial)
{
DEBUGLOG("%s", __FUNCTION__);
#ifdef LIBSSL
cMutexLock lock(&mutex);
int idx = ca_descr_data->index;
if (ca_descr_data->data_type == CA_DATA_IV)
{
if (idx < MAX_CSA_IDX)
{
DEBUGLOG("%d: ivec set", idx);
ivec[idx] = ca_descr_data->data;
}
}
else if (ca_descr_data->data_type == CA_DATA_KEY)
{
if (idx < MAX_CSA_IDX && GetKeyStructAes(idx))
{
DEBUGLOG("%d: %4s aes key set", idx, ca_descr_data->parity ? "odd" : "even");
if (ca_descr_data->parity == CA_PARITY_EVEN)
AES_set_decrypt_key(ca_descr_data->data, 8 * ca_descr_data->length, &((struct aes_keys_t*) DeCSAKeyArray[idx].csa_aes_key)->even);
else
AES_set_decrypt_key(ca_descr_data->data, 8 * ca_descr_data->length, &((struct aes_keys_t*) DeCSAKeyArray[idx].csa_aes_key)->odd);
}
}
#endif
return true;
}
bool DeCSA::SetCaPid(uint8_t adapter_index, ca_pid_t *ca_pid)
{
cMutexLock lock(&mutex);
if (ca_pid->index < MAX_CSA_IDX && ca_pid->pid < MAX_CSA_PID)
{
if (ca_pid->index >= 0 && adapter_index>=0 && adapter_index<MAXADAPTER)
DeCSAArray[adapter_index].SetCaPid(ca_pid->pid,ca_pid->index);
DEBUGLOG("%d.%d: set pid 0x%04x", adapter_index, ca_pid->index, ca_pid->pid);
}
else
ERRORLOG("%s: Parameter(s) out of range: adapter_index=%d, pid=0x%04x, index=0x%x", __FUNCTION__, adapter_index, ca_pid->pid, ca_pid->index);
return true;
}
void DeCSA::SetAlgo(uint32_t index, uint32_t usedAlgo)
{
if (index >= 0 && index < MAX_CSA_IDX)
DeCSAKeyArray[index].SetAlgo(usedAlgo);
}
void DeCSA::SetAes(uint32_t index, bool usedAes)
{
if (index >= 0 && index < MAX_CSA_IDX)
DeCSAKeyArray[index].SetAes(usedAes);
}
void DeCSA::SetCipherMode(uint32_t index, uint32_t usedCipherMode)
{
if (index < MAX_CSA_IDX)
cipher_mode[index] = usedCipherMode;
}
unsigned char ts_packet_get_payload_offset(unsigned char *ts_packet)
{
if (ts_packet[0] != TS_SYNC_BYTE)
return 0;
unsigned char adapt_field = (ts_packet[3] &~ 0xDF) >> 5; // 11x11111
unsigned char payload_field = (ts_packet[3] &~ 0xEF) >> 4; // 111x1111
if (!adapt_field && !payload_field) // Not allowed
return 0;
if (adapt_field)
{
unsigned char adapt_len = ts_packet[4];
if (payload_field && adapt_len > 182) // Validity checks
return 0;
if (!payload_field && adapt_len > 183)
return 0;
if (adapt_len + 4 > TS_SIZE) // adaptation field takes the whole packet
return 0;
return 4 + 1 + adapt_len; // ts header + adapt_field_len_byte + adapt_field_len
}
else
{
return 4; // No adaptation, data starts directly after TS header
}
}
bool DeCSA::Decrypt(uint8_t adapter_index, unsigned char *data, int len, bool force)
{
if (adapter_index < 0 || adapter_index >= MAXADAPTER)
return false;
return DeCSAArray[adapter_index].Decrypt(this, data, len, force);
}
#ifndef LIBDVBCSA
int DeCSA::GetCaid(uint8_t adapter_index, int pid)
{
if (adapter_index < 0 || adapter_index >= MAXADAPTER)
return 0;
return DeCSAArray[adapter_index].GetCaid(this, pid);
}
#endif
void DeCSA::CancelWait()
{
for (int i = 0;i < MAXADAPTER;i++)
DeCSAArray[i].CancelWait();
}
#ifndef LIBDVBCSA
void DeCSA::DebugLogPidmap()
{
if (LogLevel < 3) return;
for (int iadapter = 0;iadapter < MAXADAPTER;iadapter++)
{
if (DeCSAArray[iadapter].AdapterPidMap.size() > 0)
{
map<int, unsigned char>::iterator it;
for (it = DeCSAArray[iadapter].AdapterPidMap.begin(); it != DeCSAArray[iadapter].AdapterPidMap.end(); ++it)
{
int ipid = it->first;
int iidx = it->second;
FAST_ECM fecm;
if (DeCSAKeyArray[iidx].Get_FastECM_struct(fecm))
{
uint64_t now = GetTick();
uint64_t evendelta = -1;
if (fecm.evenparityTime > 0)
evendelta = now - fecm.evenparityTime;
uint64_t odddelta = -1;
if (fecm.oddparityTime > 0)
odddelta = now - fecm.oddparityTime;
int aparity = 0;
int aparity2 = 0;
DeCSAKeyArray[iidx].GetActiveParity(ipid, aparity, aparity2);
DEBUGLOG("DebugLogPidmap cardindex:%d pid:%d(0x%04X) keyindex:%d SID:%d(0x%04X) caid:%d(0x%04X) DvbApiPid:%d(0x%04X) activparity:%d activparity2:%d nextparity:%d evendelta:%lld odddelta:%lld",
iadapter, ipid, ipid, iidx, fecm.csaSid, fecm.csaSid, fecm.csaCaid, fecm.csaCaid, fecm.csaPid, fecm.csaPid,
aparity, aparity2, fecm.nextparity, (lldcast)evendelta, (lldcast)odddelta);
}
}
}
}
}
void DeCSA::Init_Parity(int cardindex, int sid, int slot,bool bdelete)
{
if (cardindex < 0)
return;
if (sid < 0 && slot < 0)
return;
DeCSAArray[cardindex].Init_Parity(DeCSAKeyArray, sid,slot,bdelete);
}
void DeCSA::SetDVBAPIPid(int adapter, int slot, int dvbapiPID)
{
if (adapter>=0 && dvbapiPID >= 0 && slot >= 0 && slot<MAX_CSA_IDX)
DeCSAArray[adapter].SetDVBAPIPid(this,slot, dvbapiPID);
}
void DeCSA::InitFastEcmOnCaid(int Caid)
{
//called when timeout occurs
//reset all stream with Caid
//initialize disable check (wait for odd, even and so on..)
for (int i = 0;i < MAX_CSA_IDX;i++)
DeCSAKeyArray[i].InitFastEcmOnCaid(Caid);
}
void DeCSA::SetFastECMPid(int cardindex, int idx,int slot,int dvbapiPID)
{
if (idx>=0 && GetKeyStruct(idx))
{
DEBUGLOG("SetDVBAPIPid %d.%d (PID %d (0x%04X)) keyindex:%d",cardindex, slot, dvbapiPID, dvbapiPID, idx);
//Init_FastECM(keys[idxOK]);
DeCSAKeyArray[idx].SetFastECMPid(dvbapiPID);
}
}
#endif
uint32_t DeCSA::GetAlgo(int idx)
{
if (idx < 0 || idx >= MAX_CSA_IDX)
return -1;
return DeCSAKeyArray[idx].GetAlgo();
}
uint32_t DeCSA::GetAes(int idx)
{
if (idx < 0 || idx >= MAX_CSA_IDX)
return -1;
return DeCSAKeyArray[idx].GetAes();
}
#ifndef LIBDVBCSA
int DeCSAAdapter::GetCaid(DeCSA* parent, int pid)
{
DEBUGLOG("%s: DeCSAAdapter::GetCaid %d", __FUNCTION__, pid);
int ret = 0;
if (capmt)
{
if (LogLevel >= 3)
{
map<int, unsigned char>::iterator it;
for (it = AdapterPidMap.begin(); it != AdapterPidMap.end(); ++it)
{
int idx = it->second;
if (idx >= 0)
{
int caCaid = 0;
parent->DeCSAKeyArray[idx].Get_FastECM_CAID(&caCaid);
DEBUGLOG("%s: cardindex:%d pid:%d pid:%d keyindex:%d caid:0x%04X", __FUNCTION__, cardindex,pid,it->first,idx,caCaid);
}
}
}
int caCaid = 0;
int idx = SearchPIDinMAP(pid);
if (idx >= 0 && (pid < MAX_CSA_PID))
parent->DeCSAKeyArray[idx].Get_FastECM_CAID(&caCaid);
DEBUGLOG("%s: DeCSAAdapter::GetCaid %d 0x%04X", __FUNCTION__, pid,caCaid);
ret = caCaid;
}
return ret;
}
#endif
bool DeCSAAdapter::Decrypt(DeCSA* parent, unsigned char *data, int len, bool force)
{
cTimeMs starttime(cTimeMs::Now());
cMutexLockHelper lockDecrypt(&mutexDecrypt);
cMutexLockHelper lockPIDMAPnew(&mutexAdapter);
#ifndef LIBDVBCSA
bool blogfull = false;
uint8_t adapter_index = cardindex;
uint64_t sleeptime = 0;
int itimeout = 2500; //FASTECM maximum wait time for CW
int iSleep = 50;
int imaxSleep = itimeout / iSleep;
cTimeMs TimerTimeout(itimeout);
if (!rangenew)
#else
if (!cs_tsbbatch_even || !cs_tsbbatch_odd)
#endif
{
ERRORLOG("%s: Error allocating memory for DeCSA", __FUNCTION__);
return false;
}
int offset, currIdx = -1;