📄 sc-cryptoworks.c
字号:
/* * 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 <ctype.h>#include "system-common.h"#include "smartcard.h"#include "data.h"#include "parse.h"#include "crypto.h"#include "opts.h"#include "misc.h"#include "log-sc.h"#include "log-core.h"#define SYSTEM_NAME "SC-Cryptoworks"#define SYSTEM_PRI -5#define SC_NAME "Cryptoworks"#define SC_ID MAKE_SC_ID('C','r','W','o')#define L_SC 10#define L_SC_EXTRA LCLASS(L_SC,L_SC_LASTDEF<<1)#define L_SC_ALL LALL(L_SC_EXTRA)static const struct LogModule lm_sc = { (LMOD_ENABLE|L_SC_ALL)&LOPT_MASK, (LMOD_ENABLE|L_SC_DEFDEF)&LOPT_MASK, "sc-cryptoworks", { L_SC_DEFNAMES,"extra" } };ADD_MODULE(L_SC,lm_sc)static int disableParental=0;// -- cSystemScCryptoworks ---------------------------------------------------------------class cSystemScCryptoworks : public cSystemScCore {public: cSystemScCryptoworks(void); };cSystemScCryptoworks::cSystemScCryptoworks(void):cSystemScCore(SYSTEM_NAME,SYSTEM_PRI,SC_ID,"SC Cryptoworks"){ hasLogger=true;}// -- cSystemLinkScCryptoworks --------------------------------------------------------class cSystemLinkScCryptoworks : public cSystemLink {public: cSystemLinkScCryptoworks(void); virtual bool CanHandle(unsigned short SysId); virtual cSystem *Create(void) { return new cSystemScCryptoworks; } };static cSystemLinkScCryptoworks staticInit;cSystemLinkScCryptoworks::cSystemLinkScCryptoworks(void):cSystemLink(SYSTEM_NAME,SYSTEM_PRI){ static const char *rat[] = { trNOOP("don't touch"), trNOOP("disable") }; opts=new cOpts(SYSTEM_NAME,1); opts->Add(new cOptSel("DisableParental",trNOOP("SC-Cryptoworks: Parental rating"),&disableParental,sizeof(rat)/sizeof(char *),rat)); Feature.NeedsSmartCard();}bool cSystemLinkScCryptoworks::CanHandle(unsigned short SysId){ bool res=false; cSmartCard *card=smartcards.LockCard(SC_ID); if(card) { res=card->CanHandle(SysId); smartcards.ReleaseCard(card); } return res;}// -- cSmartCardDataCryptoworks -----------------------------------------------------enum eDataType { dtIPK, dtUCPK, dtPIN };class cSmartCardDataCryptoworks : public cSmartCardData {private: int IdLen(void) const { return type==dtIPK ? 2:5; }public: eDataType type; unsigned char id[5], pin[4]; cBN key; // cSmartCardDataCryptoworks(void); cSmartCardDataCryptoworks(eDataType Type, unsigned char *Id); virtual bool Parse(const char *line); virtual bool Matches(cSmartCardData *param); };cSmartCardDataCryptoworks::cSmartCardDataCryptoworks(void):cSmartCardData(SC_ID){}cSmartCardDataCryptoworks::cSmartCardDataCryptoworks(eDataType Type, unsigned char *Id):cSmartCardData(SC_ID){ type=Type; memset(id,0,sizeof(id)); memcpy(id,Id,IdLen());}bool cSmartCardDataCryptoworks::Matches(cSmartCardData *param){ cSmartCardDataCryptoworks *cd=(cSmartCardDataCryptoworks *)param; return cd->type==type && !memcmp(cd->id,id,IdLen());}bool cSmartCardDataCryptoworks::Parse(const char *line){ line=skipspace(line); if(!strncasecmp(line,"IPK",3)) { type=dtIPK; line+=3; } else if(!strncasecmp(line,"UCPK",4)) { type=dtUCPK; line+=4; } else if(!strncasecmp(line,"PIN",3)) { type=dtPIN; line+=3; } else { PRINTF(L_CORE_LOAD,"smartcarddatacryptoworks: format error: datatype"); return false; } line=skipspace(line); if(GetHex(line,id,IdLen())!=IdLen()) { PRINTF(L_CORE_LOAD,"smartcarddatacryptoworks: format error: caid/serial"); return false; } line=skipspace(line); if(type==dtPIN) { for(int i=0; i<4; i++) { if(!isdigit(*line)) { PRINTF(L_CORE_LOAD,"smartcarddatacryptoworks: format error: pin"); return false; } pin[i]=*line++; } } else { unsigned char buff[64]; if(GetHex(line,buff,64,true)!=64) { PRINTF(L_CORE_LOAD,"smartcarddatacryptoworks: format error: ipk/ucpk"); return false; } BN_bin2bn(buff,64,key); } return true;}// -- cSmartCardCryptoworks -----------------------------------------------------------class cSmartCardCryptoworks : public cSmartCard, public cIdSet {private: int caid; cRSA rsa; cBN ucpk, exp; bool ucpkValid; // int GetLen(void); bool EndOfData(void); bool SelectFile(int file); int ReadRecord(unsigned char *buff, int num); int ReadData(unsigned char *buff, int len);public: cSmartCardCryptoworks(void); virtual bool Init(void); virtual bool Decode(const cEcmInfo *ecm, const unsigned char *data, unsigned char *cw); virtual bool Update(int pid, int caid, const unsigned char *data); virtual bool CanHandle(unsigned short CaId); };static const struct StatusMsg msgs[] = { { { 0x90,0x00 }, "Instruction executed without errors", true }, { { 0x92,0x40 }, "Memory problem", false }, { { 0x94,0x02 }, "Out of range", false }, { { 0x94,0x04 }, "File not found", false }, { { 0x98,0x04 }, "Verification failed (wrong PIN)", false }, { { 0x98,0x05 }, "Wrong signature", false }, { { 0x98,0x40 }, "Verification failed, card blocked (deblocking by uplink required)", false }, { { 0x9F,0xFF }, "Instruction accepted, data to be read", true }, { { 0xFF,0xFF }, 0, false } };static const struct CardConfig cardCfg = { SM_8E2,1000,100 };struct chid_dat { unsigned int chid, version; unsigned char id, status; char from[16], to[16], name[16]; }; cSmartCardCryptoworks::cSmartCardCryptoworks(void):cSmartCard(&cardCfg,msgs){ static const unsigned char cwexp[] = { 0x01,0x00,0x01 }; BN_bin2bn(cwexp,sizeof(cwexp),exp); ucpkValid=false;}int cSmartCardCryptoworks::GetLen(void){ return (sb[0]==0x9F) ? sb[1] : -1;}bool cSmartCardCryptoworks::EndOfData(void){ return sb[0]==0x94 && sb[1]==0x02;}bool cSmartCardCryptoworks::SelectFile(int file){ static unsigned char insa4[] = { 0xA4,0xA4,0x00,0x00,0x02,0x00,0x00 }; insa4[5]=file>>8; insa4[6]=file&0xFF; return IsoWrite(insa4,&insa4[5]) && Status();}int cSmartCardCryptoworks::ReadRecord(unsigned char *buff, int num){ static unsigned char insa2[] = { 0xA4,0xA2,0x00,0x00,0x01,0x00 }; static unsigned char insb2[] = { 0xA4,0xB2,0x00,0x00,0x00 }; insa2[5]=num; if(IsoWrite(insa2,&insa2[5]) && Status() && (num=GetLen())>0) { insb2[4]=num; if(IsoRead(insb2,buff) && Status()) return num; } return -1;}int cSmartCardCryptoworks::ReadData(unsigned char *buff, int len){ static unsigned char insc0[] = { 0xA4,0xC0,0x00,0x00,0x00 }; insc0[4]=len; if(IsoRead(insc0,buff) && Status()) return len; return -1;}bool cSmartCardCryptoworks::Init(void){ if(atr->histLen<6 || atr->hist[1]!=0xC4 || atr->hist[4]!=0x8F || atr->hist[5]!=0xF1) { PRINTF(L_SC_INIT,"doesn't look like a Cryptoworks card"); return false; } infoStr.Begin(); infoStr.Strcat("Cryptoworks smartcard\n"); ucpkValid=false; unsigned char buff[MAX_LEN]; int mfid=0x3F20; if(ReadData(buff,0x11)>0 && buff[0]==0xDF && buff[1]>=6) mfid=buff[6]*256+buff[7]; else PRINTF(L_SC_ERROR,"reading MF-ID failed, using default 3F20"); unsigned char Caid[2], serial[5]; if(!SelectFile(0x2F01) || ReadRecord(buff,0xD1)<4) { PRINTF(L_SC_ERROR,"reading record 2f01/d1 failed");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -