📄 sc-irdeto.cpp
字号:
int ACS, caId;
cTimeMs recoverTime;
//
int DoCmd(unsigned char *cmd, int goodSB, int secGoodSB=-1);
time_t Date(int date, char *buff, int len);
public:
cSmartCardIrdeto(void);
virtual bool Init(void);
virtual bool Decode(const cEcmInfo *ecm, const unsigned char *data, unsigned char *cw);
virtual bool CanHandle(unsigned short CaId);
};
static const struct StatusMsg msgs[] = {
{ { 0x00,0x00 }, "Instruction executed without error", true },
{ { 0x55,0x00 }, "Instruction executed without error", true },
{ { 0x57,0x00 }, "CAM string rejected", false },
{ { 0x58,0x00 }, "Instruction executed without error", true },
{ { 0x9D,0x00 }, "Decoding successfull", true },
{ { 0x90,0x00 }, "ChID missing. Not subscribed?", false },
{ { 0x93,0x00 }, "ChID out of date. Subscription expired?", false },
{ { 0x9C,0x00 }, "Master key error", false },
{ { 0x9E,0x00 }, "Wrong decryption key", false },
{ { 0x9F,0x00 }, "Missing key", false },
{ { 0x70,0x00 }, "Wrong hex serial", false },
{ { 0x71,0x00 }, "Wrong provider", false },
{ { 0x72,0x00 }, "Wrong provider group", false },
{ { 0x73,0x00 }, "Wrong provider group", false },
{ { 0x7C,0x00 }, "Wrong signature", false },
{ { 0x7D,0x00 }, "Masterkey missing", false },
{ { 0x7E,0x00 }, "Wrong provider identifier", false },
{ { 0x7F,0x00 }, "Invalid nano", false },
{ { 0x54,0x00 }, "No more ChID's", true },
{ { 0xFF,0xFF }, 0, false }
};
static const struct CardConfig cardCfg = {
SM_8N2,3000,100
};
cSmartCardIrdeto::cSmartCardIrdeto(void)
:cSmartCard(&cardCfg,msgs)
{}
bool cSmartCardIrdeto::Init(void)
{
ResetIdSet();
recoverTime.Set(-RECOVER_TIME);
if(atr->histLen<6 || memcmp(atr->hist,"IRDETO",6)) {
di(printf("smartcardirdeto: doesn't looks like a Irdeto/Beta card\n"))
return false;
}
infoStr.Begin();
infoStr.Strcat("Irdeto smartcard\n");
int r;
static unsigned char getCountryCode[] = { 0x01,0x02,0x02,0x03,0x00,0x00,0xCC };
if((r=DoCmd(getCountryCode,0x0000))<=0 || !Status() || r<16) {
di(printf("smartcardirdeto: country code error\n"))
return false;
}
ACS=buff[8]*256+buff[9];
caId=buff[13]*256+buff[14];
memcpy(coco,&buff[21],3); coco[3]=0;
di(printf("smartcardirdeto: ACS Version %04x, CAID %04x, CoCo %s\n",ACS,caId,coco))
_snprintf(idStr,sizeof(idStr),"%s (ACS %x)",SC_NAME,ACS);
infoStr.Printf("ACS: %04x CAID: %04x CoCo: %s\n",ACS,caId,coco);
static unsigned char getAsciiSerial[] = { 0x01,0x02,0x00,0x03,0x00,0x00,0xCC };
if((r=DoCmd(getAsciiSerial,0x0000))<=0 || !Status() || r<10) {
di(printf("smartcardirdeto: ASCII serial error\n"))
return false;
}
strn0cpy(asciiSerial,(char*)buff+8,sizeof(asciiSerial));
di(printf("smartcardirdeto: ASCII serial %s\n",asciiSerial))
static unsigned char getHexSerial[] = { 0x01,0x02,0x01,0x03,0x00,0x00,0xCC };
if((r=DoCmd(getHexSerial,0x0000))<=0 || !Status() || r<25) {
di(printf("smartcardirdeto: hex serial error\n"))
return false;
}
int numProv=buff[18];
SetCard(new cCardIrdeto(buff[23],&buff[20]));
di(printf("smartcardirdeto: Providers: %d HEX Serial: %02X%02X%02X HEX Base: %02X\n",numProv,buff[20],buff[21],buff[22],buff[23]))
infoStr.Printf("HEX: %02X/%02X%02X%02X ASCII: %s\n",buff[23],buff[20],buff[21],buff[22],asciiSerial);
static unsigned char getCardFile[] = { 0x01,0x02,0x0E,0x02,0x00,0x00,0xCC };
unsigned char encr[128], plain[64+6+2];
PrepareCamMessage(plain+6);
getCardFile[3]=0x02;
if((r=DoCmd(getCardFile,0x0000))<=0 || !Status() || r<73) {
di(printf("smartcardirdeto: cardfile2 error\n"))
return false;
}
memcpy(encr,buff+8,64);
getCardFile[3]=0x03;
if((r=DoCmd(getCardFile,0x0000))<=0 || !Status() || r<73) {
di(printf("smartcardirdeto: cardfile3 error\n"))
return false;
}
memcpy(encr+64,buff+8,64);
bool doPlain=false;
if((ACS==0x0383 || ACS==0x0384) && atr->histLen>=12 && atr->hist[12]==0x95)
doPlain=true;
cSmartCardDataIrdeto *entry=0;
if(!doPlain) {
cSmartCardDataIrdeto cd(ACS,caId);
if(!(entry=(cSmartCardDataIrdeto *)smartcards.FindCardData(&cd))) {
di(printf("smartcardirdeto: didn't find card specific certificate, falling back to default\n"))
cSmartCardDataIrdeto cd(-1,-1);
if(!(entry=(cSmartCardDataIrdeto *)smartcards.FindCardData(&cd))) {
di(printf("smartcardirdeto: didn't find default certificate, please add one\n"))
return false;
}
}
else doPlain=entry->plain;
}
static unsigned char doCamKeyExchange[] = { 0x01,0x02,0x09,0x03,0x00,0x40 };
if(doPlain) {
// plain challenge
memcpy(plain,doCamKeyExchange,sizeof(doCamKeyExchange));
plain[4]=1; // set block counter
r=DoCmd(plain,0x5500,0x0000);
}
else {
// RSA challenge
if(!SetupCardFiles(encr,sizeof(encr),entry->exp,entry->mod)) {
di(printf("smartcardirdeto: decrypting cardfiles failed. Probably bad certificate.\n"))
return false;
}
if(!EncryptCamMessage(encr+6,plain+6)) {
di(printf("smartcardirdeto: encrypting CAM message failed. Probably bad certificate.\n"))
return false;
}
static unsigned char doRSACheck[] = { 0x01,0x02,0x11,0x00,0x00,0x40 };
memcpy(plain,doRSACheck,sizeof(doRSACheck));
if((r=DoCmd(plain,0x5800,0x0000))<=0 || !Status() || r<73) {
di(printf("smartcardirdeto: card didn't give a proper reply (buggy RSA unit?), trying to continue...\n"))
// non-fatal
}
if(r==73 && memcmp(encr+6,buff+8,64)) {
di(printf("smartcardirdeto: card failed on RSA check, trying to continue...\n"))
// non-fatal
}
memcpy(encr,doCamKeyExchange,sizeof(doCamKeyExchange));
r=DoCmd(encr,0x5500,0x0000);
}
if(r>0 && Status() && r>=9) {
memcpy(camKey,CamKey(plain+6),8);
if(r>=17) {
RevCamCrypt(camKey,buff+8);
if(memcmp(HelperKey(plain+6),buff+8,8)) {
di(printf("smartcardirdeto: camkey challenge failed"))
return false;
}
}
di(printf("smartcardirdeto: camkey"))
DUMP(camKey,sizeof(camKey));
}
else {
di(printf("smartcardirdeto: camkey error\n"))
return false;
}
static unsigned char getProvider[] = { 0x01,0x02,0x03,0x03,0x00,0x00,0xCC };
static unsigned char getChanelIds[] = { 0x01,0x02,0x04,0x00,0x00,0x01,0x00,0xCC };
for(int i=0; i<numProv; i++) {
getProvider[4]=i;
if((r=DoCmd(getProvider,0x0000))>0 && Status() && r>=33) {
AddProv(new cProviderIrdeto(buff[8]&0x0f,&buff[9]));
di(printf("smartcardirdeto: provider %d with ProvBase 0x%02x ProvId 0x%02x%02x%02x\n",i,buff[8]&0x0f,buff[9],buff[10],buff[11]))
infoStr.Printf("Provider %d Id: %02X/%02X%02X%02X\n",i,buff[8]&0x0f,buff[9],buff[10],buff[11]);
getChanelIds[4]=i;
for(int l=0; l<10; l++) {
getChanelIds[6]=l;
if((r=DoCmd(getChanelIds,0x0000,0x5400))<=0 || !Status() || r<69) break;
for(int k=0; k<buff[7]; k+=6) {
int chanId=buff[k+8+0]*256+buff[k+8+1];
int date =buff[k+8+2]*256+buff[k+8+3];
int durr =buff[k+8+4];
if(chanId!=0x0000 && chanId!=0xFFFF) {
char sdate[16], edate[16];
Date(date,sdate,sizeof(sdate));
Date(date+durr,edate,sizeof(edate));
di(printf("smartcardirdeto: ChanId 0x%04x Date 0x%04x %s Duration 0x%02x %s\n",chanId,date,sdate,durr,edate))
infoStr.Printf("ChanId: %04X Date: %s-%s\n",chanId,sdate,edate);
}
}
}
}
}
infoStr.Finish();
return true;
}
time_t cSmartCardIrdeto::Date(int date, char *buff, int len)
{
// Irdeto date starts 01.08.1997 which is
// 870393600 seconds in unix calendar time
time_t utcTime=870393600L+date*(24*3600);
if(buff) {
struct tm utcTm;
gmtime_r(&utcTime,&utcTm);
_snprintf(buff,len,"%04d/%02d/%02d",utcTm.tm_year+1900,utcTm.tm_mon+1,utcTm.tm_mday);
}
return utcTime;
}
bool cSmartCardIrdeto::CanHandle(unsigned short CaId)
{
return (CaId==caId);
}
bool cSmartCardIrdeto::Decode(const cEcmInfo *ecm, const unsigned char *data, unsigned char *cw)
{
static const unsigned char ecmCmd[] = { 0x01,0x05,0x00,0x00,0x02,0x00 };
int r=SCT_LEN(data)-6;
if(r<=255) {
unsigned char cmd[257+sizeof(ecmCmd)];
memcpy(cmd,ecmCmd,sizeof(ecmCmd));
cmd[5]=r;
memcpy(cmd+sizeof(ecmCmd),&data[6],r);
if((r=DoCmd(cmd,0x9D00))>0) {
if(Status() && r>=31) {
RevCamCrypt(camKey,&buff[14]);
RevCamCrypt(camKey,&buff[22]);
memcpy(cw,&buff[14],16);
return true;
}
}
}
return false;
}
int cSmartCardIrdeto::DoCmd(unsigned char *cmd, int goodSB, int secGoodSB)
{
int len=cmd[5]+6;
cmd[len]=XorSum(cmd,len) ^ XOR_START;
// wait until recover time is over
int r=RECOVER_TIME-recoverTime.Elapsed();
if(r>0) {
di(printf("IRDETO: recover time, waiting %d ms\n",r))
cCondWait::SleepMs(r+1);
}
r=-1;
di(printf("IRDETO: CMD ->"))
DUMP(cmd,len+1);
if(SerWrite(cmd,len+1)>0 && SerRead(buff,4,cardCfg.workTO)>0) {
len=4;
if(buff[0]==cmd[0] && buff[1]==cmd[1]) {
sb[0]=buff[2]; sb[1]=buff[3];
int SB=buff[2]*256+buff[3];
if(SB==goodSB || (secGoodSB>=0 && SB==secGoodSB)) {
if(SerRead(buff+len,5)>0) {
len+=5;
if(buff[7]) {
if(SerRead(buff+len,buff[7])<=0) return -1;
len+=buff[7];
}
if(XorSum(buff,len)==XOR_START) r=len;
else {
di(printf("IRDETO: checksum failed"))
DUMP(buff,len);
}
}
}
else r=len;
}
else {
sb[0]=buff[1]; sb[1]=buff[2];
r=3;
}
}
if(r>0) {
di(printf("IRDETO: RESP <-"))
DUMP(buff,r);
}
if(r<=4) {
recoverTime.Set();
di(printf("IRDETO: setting %d ms recover time\n",RECOVER_TIME))
}
return r;
}
// -- cSmartCardLinkIrdeto -----------------------------------------------------
class cSmartCardLinkIrdeto : public cSmartCardLink {
public:
cSmartCardLinkIrdeto(void):cSmartCardLink(SC_NAME,SC_ID) {}
virtual cSmartCard *Create(void) { return new cSmartCardIrdeto(); }
virtual cSmartCardData *CreateData(void) { return new cSmartCardDataIrdeto; }
};
static cSmartCardLinkIrdeto staticScInit;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -