forked from aiolos/sasc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.c
567 lines (508 loc) · 14.5 KB
/
system.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
/*
* Softcam plugin to VDR (C++)
*
* This code 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 code 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* Or, point your browser to http://www.gnu.org/copyleft/gpl.html
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vdr/tools.h>
#include <openssl/md5.h>
#include "sc.h"
#include "system.h"
#include "data.h"
#include "override.h"
#include "opts.h"
#include "log-core.h"
#include "i18n.h"
// --- cFeature ----------------------------------------------------------------
cFeature Feature;
bool cFeature::keyfile=false;
bool cFeature::smartcard=false;
void cFeature::NeedsKeyFile(void)
{
if(!keyfile) PRINTF(L_CORE_DYN,"feature: using feature KEYFILE");
keyfile=true;
}
void cFeature::NeedsSmartCard(void)
{
if(!smartcard) PRINTF(L_CORE_DYN,"feature: using feature SMARTCARD");
smartcard=true;
}
// -- cKeySnoop ----------------------------------------------------------------
cKeySnoop::cKeySnoop(cSystem *Sys, int Type, int Id, int Keynr)
{
ok=false;
sys=Sys; type=Type; id=Id; keynr=Keynr;
}
cKeySnoop::~cKeySnoop()
{
if(!ok) sys->KeyFail(type,id,keynr);
}
void cKeySnoop::OK(cPlainKey *pk)
{
sys->KeyOK(pk);
ok=true;
}
// -- cLogHook -----------------------------------------------------------------
cLogHook::cLogHook(int Id, const char *Name)
{
id=Id; name=Name;
bailOut=false;
}
// -- cSystem ------------------------------------------------------------------
#define MAX_CHID 10
struct EcmCheck {
union {
struct {
bool startFlag;
int current, chids[MAX_CHID];
} sys06;
} caid;
};
int cSystem::foundKeys=0;
int cSystem::newKeys=0;
cSystem::cSystem(const char *Name, int Pri)
{
name=Name; pri=Pri;
currentKeyStr[0]=0; doLog=true; cardNum=-1; logecm=0;
check=new struct EcmCheck;
memset(check,0,sizeof(struct EcmCheck));
memset(cw,0,sizeof(cw));
// default config
maxEcmTry=2; // try to get a key X times from the same ECM pid (default)
hasLogger=false;
needsLogger=false;
local=true;
needsDescrData=false;
constant=false;
}
cSystem::~cSystem()
{
delete check;
if(logecm) cSoftCAM::SetLogStatus(cardNum,logecm,false);
delete logecm;
}
void cSystem::StartLog(const cEcmInfo *ecm, int caid)
{
if(!logecm || logecm->caId!=caid) {
if(logecm) cSoftCAM::SetLogStatus(cardNum,logecm,false);
delete logecm;
logecm=new cEcmInfo(ecm);
logecm->caId=caid; logecm->emmCaId=0;
cSoftCAM::SetLogStatus(cardNum,logecm,true);
}
}
void cSystem::ParseCADescriptor(cSimpleList<cEcmInfo> *ecms, unsigned short sysId, int source, const unsigned char *data, int len)
{
const int pid=WORD(data,2,0x1FFF);
switch(sysId>>8) {
case 0x01: // Seca style
for(int p=2; p<len; p+=15) {
cEcmInfo *n=new cEcmInfo(name,WORD(data,p,0x1FFF),sysId,WORD(data,p+2,0xFFFF));
if(data[p+4]==0xFF) n->SetDataIdx(p+5);
ecms->Add(n);
}
break;
case 0x05: // Viaccess style
for(int p=4; p<len; p+=2+data[p+1])
if(data[p]==0x14)
ecms->Add(new cEcmInfo(name,pid,sysId,(data[p+2]<<16)|(data[p+3]<<8)|(data[p+4]&0xF0)));
break;
default: // default style
ecms->Add(new cEcmInfo(name,pid,sysId,0));
break;
}
}
void cSystem::ParseCAT(cPids *pids, const unsigned char *buffer, int source, int transponder)
{
if(buffer[0]==0x09) {
int caid=WORD(buffer,2,0xFFFF);
int pid=WORD(buffer,4,0x1FFF);
if(overrides.AddEmmPids(caid,source,transponder,pids,pid)) return;
switch(caid>>8) {
case 0x01: // Seca style (82/84)
if(buffer[1]>4) {
pids->AddPid(pid,0x82,0xFF); // Unique updates
for(int i=7, nn=buffer[6] ; nn ; nn--,i+=4)
pids->AddPid(WORD(buffer,i,0x1FFF),0x84,0xFF); // Shared updates
}
break;
case 0x05: // Viaccess style (88/8c/8d/8e)
pids->AddPid(pid,0x8B,0xFE,0x07); // mismatching 89/8f
break;
case 0x0d: // Cryptoworks style (82/84/86/88/89)
pids->AddPid(pid,0x80,0xFF,0x06);
pids->AddPid(pid,0x88,0xFE);
break;
case 0x18: // Nagra style (82/83)
pids->AddPid(pid,0x82,0xFE);
break;
default: // default style (82)
pids->AddPid(pid,0x82,0xFF);
break;
}
}
}
int cSystem::CheckECM(const cEcmInfo *ecm, const unsigned char *data, bool sync)
{
switch(ecm->caId>>8) {
case 0x06: // Irdeto
{
const int chid=WORD(data,6,0xFFFF);
// if multiple channel id's, use current one only
if(sync && check->caid.sys06.current>0 && chid!=check->caid.sys06.current) {
PRINTF(L_CORE_ECMPROC,"ecmcheck(%s): chid %04x != current %04x",name,chid,check->caid.sys06.current);
return 3;
}
/*
//XXX is that still needed? Seems to conflict with some Irdeto2 provider
// search for fake channel id's on Stream
const int cur=data[4];
const int max=data[5];
if(!sync && max>0 && max<MAX_CHID) {
if(cur==0) check->caid.sys06.startFlag=true;
if(check->caid.sys06.startFlag) {
if(cur<=max) check->caid.sys06.chids[cur]=chid;
if(cur==max) {
for(int i=0 ; i<max ; i++) {
if(check->caid.sys06.chids[i]==0) {
check->caid.sys06.startFlag=false;
PRINTF(L_CORE_ECMPROC,"ecmcheck(%s): zero chid",name);
return 1;
}
for(int j=i+1 ; j<=max ; j++) {
if(check->caid.sys06.chids[i]==check->caid.sys06.chids[j]) {
check->caid.sys06.startFlag=false;
PRINTF(L_CORE_ECMPROC,"ecmcheck(%s): duplicate chid %04x",name,check->caid.sys06.chids[i]);
return 1;
}
}
}
}
}
}
*/
break;
}
}
return 0;
}
void cSystem::CheckECMResult(const cEcmInfo *ecm, const unsigned char *data, bool result)
{
switch(ecm->caId>>8) {
case 0x06: // Irdeto
check->caid.sys06.current=result ? WORD(data,6,0xFFFF) : 0;
break;
}
}
void cSystem::KeyOK(cPlainKey *pk)
{
if(lastkey.NotLast(pk->type,pk->id,pk->keynr)) {
strn0cpy(currentKeyStr,pk->ToString(false),sizeof(currentKeyStr));
PRINTF(L_CORE_ECM,"system: using key %s",*pk->ToString(true));
doLog=true;
}
}
void cSystem::KeyOK(const char *txt)
{
snprintf(currentKeyStr,sizeof(currentKeyStr),"%s (%s)\n",txt?txt:name,tr("undisclosed key"));
doLog=true;
}
void cSystem::KeyFail(int type, int id, int keynr)
{
keys.Trigger(type,id,keynr);
if(lastkey.NotLast(type,id,keynr) && doLog)
PRINTF(L_CORE_ECM,"system: no key found for %s",*keys.KeyString(type,id,keynr));
}
// -- cSystemLink --------------------------------------------------------------
cSystemLink::cSystemLink(const char *Name, int Pri)
{
name=Name; pri=Pri;
opts=0; noFF=false;
cSystems::Register(this);
}
cSystemLink::~cSystemLink()
{
delete opts;
}
// -- cSystems -----------------------------------------------------------------
cSystemLink *cSystems::first=0;
int cSystems::nextSysIdent=0x1000;
void cSystems::Register(cSystemLink *sysLink)
{
PRINTF(L_CORE_DYN,"systems: registering CA system %s, pri %d, ident %04X",sysLink->name,sysLink->pri,nextSysIdent);
sysLink->next=first;
sysLink->sysIdent=nextSysIdent++;
first=sysLink;
}
int cSystems::CanHandle(int SysId, bool ff)
{
for(cSystemLink *sl=first; sl; sl=sl->next)
if(sl->CanHandle(SysId))
return (sl->noFF && ff) ? -1:1;
return 0;
}
cSystemLink *cSystems::FindByName(const char *Name)
{
cSystemLink *sl=first;
while(sl) {
if(!strcasecmp(sl->name,Name)) return sl;
sl=sl->next;
}
return 0;
}
cSystemLink *cSystems::FindById(unsigned short SysId, bool ff, int oldPri)
{
// all pri's are negative!
// oldPri = 0 -> get highest pri system
// oldPri < 0 -> get highest pri system with pri<oldPri
// oldPri > 0 -> get lowest pri system
cSystemLink *sl=first, *csl=0;
while(sl) {
if((!ff || !sl->noFF) && sl->CanHandle(SysId)
&& sl->pri<oldPri
&& (!csl
|| (oldPri<=0 && sl->pri>csl->pri)
|| (oldPri>0 && sl->pri<csl->pri)
)
) csl=sl;
sl=sl->next;
}
return csl;
}
cSystemLink *cSystems::FindByIdent(int ident)
{
cSystemLink *sl=first;
while(sl) {
if(sl->sysIdent==ident) return sl;
sl=sl->next;
}
return 0;
}
cSystem *cSystems::FindBySysId(unsigned short SysId, bool ff, int oldPri)
{
cSystemLink *csl=FindById(SysId,ff,oldPri);
return csl ? csl->Create() : 0;
}
int cSystems::FindIdentBySysId(unsigned short SysId, bool ff, int &Pri)
{
cSystemLink *csl=FindById(SysId,ff,Pri);
if(csl) {
Pri=csl->pri;
return csl->sysIdent;
}
return 0;
}
int cSystems::FindIdentBySysName(unsigned short SysId, bool ff, const char *Name, int &Pri)
{
cSystemLink *csl=FindByName(Name);
if(csl && (!ff || !csl->noFF) && csl->CanHandle(SysId)) {
Pri=csl->pri;
return csl->sysIdent;
}
return 0;
}
cSystem *cSystems::FindBySysIdent(int ident)
{
cSystemLink *csl=FindByIdent(ident);
return csl ? csl->Create() : 0;
}
bool cSystems::Init(const char *cfgdir)
{
int num=0;
PRINTF(L_CORE_LOAD,"** registered systems:");
for(cSystemLink *sl=first; sl; sl=sl->next, num++)
PRINTF(L_CORE_LOAD,"** %-16s (pri %3d)",sl->name,sl->pri);
if(num<1)
PRINTF(L_GEN_WARN,"No registered systems! Won't decode anything! Where are your libs?");
for(cSystemLink *sl=first; sl; sl=sl->next)
if(!sl->Init(cfgdir)) return false;
return true;
}
void cSystems::Clean(void)
{
for(cSystemLink *sl=first; sl; sl=sl->next) sl->Clean();
}
bool cSystems::ConfigParse(const char *Name, const char *Value)
{
char sysName[32];
unsigned int i;
for(i=0; Name[i] && Name[i]!='.' && i<sizeof(sysName); i++)
sysName[i]=Name[i];
if(Name[i]) {
sysName[i]=0; i++;
cSystemLink *sl=FindByName(sysName);
if(sl && sl->opts) return sl->opts->Parse(&Name[i],Value);
}
return false;
}
void cSystems::ConfigStore(bool AsIs)
{
for(cSystemLink *sl=first; sl; sl=sl->next)
if(sl->opts && sl->opts->Store(AsIs)) sl->NewConfig();
}
cOpts *cSystems::GetSystemOpts(bool start)
{
static cSystemLink *sl=0;
sl=(start || !sl) ? first : sl->next;
while(sl) {
if(sl->opts) return sl->opts;
sl=sl->next;
}
return 0;
}
// -- cMsgCache ----------------------------------------------------------------
#define FREE 0x00 // modes
#define FAIL1 0x01
#define FAIL2 0x3D
#define FAILN 0x3E
#define GOOD 0x3F
#define MASK 0x3F
#define QUEUED 0x40
#define WAIT 0x80
struct Cache {
int crc;
int mode;
};
cMsgCache::cMsgCache(int NumCache, int StoreSize)
{
numCache=NumCache;
storeSize=StoreSize;
ptr=0; stores=0; maxFail=2;
caches=MALLOC(struct Cache,numCache);
if(caches) {
Clear();
if(storeSize>0) {
stores=MALLOC(unsigned char,numCache*storeSize);
if(!stores) PRINTF(L_GEN_ERROR,"msgcache: no memory for store area");
}
}
else PRINTF(L_GEN_ERROR,"msgcache: no memory for cache");
}
cMsgCache::~cMsgCache()
{
free(caches);
free(stores);
}
void cMsgCache::SetMaxFail(int maxfail)
{
maxFail=min(maxfail,FAILN);
PRINTF(L_CORE_MSGCACHE,"%d/%p: maxFail set to %d",getpid(),this,maxFail);
}
void cMsgCache::Clear(void)
{
cMutexLock lock(&mutex);
memset(caches,0,sizeof(struct Cache)*numCache);
ptr=0;
PRINTF(L_CORE_MSGCACHE,"%d/%p: clear",getpid(),this);
}
struct Cache *cMsgCache::FindMsg(int crc)
{
int i=ptr;
while(1) {
if(--i<0) i=numCache-1;
struct Cache * const s=&caches[i];
if(!s->mode) break;
if(s->crc==crc) return s;
if(i==ptr) break;
}
return 0;
}
// returns:
// -1 - msg cached as failed (max failed)
// 0 - msg cached as good, result stored
// >0 - msg not cached, queue id
int cMsgCache::Get(const unsigned char *msg, int len, unsigned char *store)
{
unsigned char md[16];
int crc=crc32_le(0,MD5(msg,len,md),16);
cMutexLock lock(&mutex);
if(!caches || (storeSize>0 && !stores)) return -1; // sanity
struct Cache *s;
while((s=FindMsg(crc))) {
if(!(s->mode&QUEUED)) break;
s->mode|=WAIT;
PRINTF(L_CORE_MSGCACHE,"%d/%p: msg already queued. waiting to complete",getpid(),this);
wait.Wait(mutex);
}
int id;
if(!s) {
while(1) {
s=&caches[ptr];
if(!(s->mode&QUEUED)) break;
s->mode|=WAIT;
PRINTF(L_CORE_MSGCACHE,"%d/%p: queue overwrite protection id=%d",getpid(),this,ptr+1);
wait.Wait(mutex); // don't overwrite queued msg's
}
id=ptr+1;
s->crc=crc;
s->mode=QUEUED;
PRINTF(L_CORE_MSGCACHE,"%d/%p: queued msg with id=%d",getpid(),this,id);
ptr++; if(ptr>=numCache) { ptr=0; PRINTF(L_CORE_MSGCACHE,"msgcache: roll-over (%d)",numCache); }
return id;
}
else {
id=(s-&caches[0])+1;
if(s->mode==GOOD) {
if(store && storeSize>0)
memcpy(store,&stores[(id-1)*storeSize],storeSize);
PRINTF(L_CORE_MSGCACHE,"%d/%p: msg is cached as GOOD (%d)",getpid(),this,id);
return 0;
}
else if(s->mode>=FAIL1 && s->mode<=FAIL2) {
PRINTF(L_CORE_MSGCACHE,"%d/%p: msg is cached as FAIL%d (%d)",getpid(),this,s->mode,id);
s->mode|=QUEUED;
return id;
}
else {
PRINTF(L_CORE_MSGCACHE,"%d/%p: msg is cached as FAILN (%d)",getpid(),this,id);
return -1;
}
}
}
int cMsgCache::Cache(int id, bool result, const unsigned char *store)
{
cMutexLock lock(&mutex);
if(id<1 || !caches || (storeSize>0 && !stores)) return 0; // sanity
struct Cache *s=&caches[id-1];
LBSTARTF(L_CORE_MSGCACHE);
LBPUT("%d/%p: de-queued msg with id=%d ",getpid(),this,id);
if(s->mode&WAIT) wait.Broadcast();
if(result) {
if(store && storeSize>0)
memcpy(&stores[(id-1)*storeSize],store,storeSize);
s->mode=GOOD;
LBPUT("(GOOD)");
return 0;
}
else {
int m=s->mode&MASK;
if(m==GOOD)
m=s->mode=FREE;
if(m==FREE || (m>=FAIL1 && m<=FAIL2)) {
s->mode=(s->mode&MASK)+1;
if(s->mode<maxFail)
LBPUT("(FAIL%d)",s->mode);
else
m=s->mode=FAILN;
}
if(m==FAILN)
LBPUT("(FAILN)");
return s->mode;
}
LBEND();
}