SASC-NG
DVBSoftCAM

system-common.c

Go to the documentation of this file.
00001 /*
00002  * Softcam plugin to VDR (C++)
00003  *
00004  * This code is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This code is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
00018  */
00019 
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <malloc.h>
00023 #include <ctype.h>
00024 
00025 #include <vdr/tools.h>
00026 
00027 #include "system-common.h"
00028 #include "smartcard.h"
00029 #include "data.h"
00030 #include "crypto-bn.h"
00031 
00032 // -- cHexKey ------------------------------------------------------------------
00033 
00034 cHexKey::cHexKey(bool Super)
00035 :cPlainKey(Super)
00036 {
00037   key=0;
00038 }
00039 
00040 cHexKey::~cHexKey()
00041 {
00042   free(key);
00043 }
00044 
00045 bool cHexKey::SetKey(void *Key, int Keylen)
00046 {
00047   keylen=Keylen;
00048   free(key); key=MALLOC(unsigned char,keylen);
00049   if(key) memcpy(key,Key,keylen);
00050   return key!=0;
00051 }
00052 
00053 bool cHexKey::SetBinKey(unsigned char *Mem, int Keylen)
00054 {
00055   return SetKey(Mem,Keylen);
00056 }
00057 
00058 bool cHexKey::Cmp(cPlainKey *k)
00059 {
00060   cHexKey *hk=dynamic_cast<cHexKey *>(k); // downcast
00061   return hk && keylen==hk->keylen && memcmp(key,hk->key,keylen)==0;
00062 }
00063 
00064 void cHexKey::Get(void *mem)
00065 {
00066   memcpy(mem,key,keylen);
00067 }
00068 
00069 cString cHexKey::Print(void)
00070 {
00071   char *str=(char *)malloc(keylen*2+2);
00072   if(str) HexStr(str,key,keylen);
00073   return cString(str,true);
00074 }
00075 
00076 // -- cBNKey -------------------------------------------------------------------
00077 
00078 cBNKey::cBNKey(bool Super, bool Rotate)
00079 :cPlainKey(Super)
00080 {
00081   key=new cBN; rotate=Rotate;
00082 }
00083 
00084 cBNKey::~cBNKey()
00085 {
00086   delete key;
00087 }
00088 
00089 bool cBNKey::SetKey(void *Key ,int Keylen)
00090 {
00091   keylen=Keylen;
00092   return BN_copy(*key,(BIGNUM *)Key)!=0;
00093 }
00094 
00095 bool cBNKey::SetBinKey(unsigned char *Mem, int Keylen)
00096 {
00097   keylen=Keylen;
00098   return rotate ? key->GetLE(Mem,Keylen) : key->Get(Mem,Keylen);
00099 }
00100 
00101 bool cBNKey::Cmp(cPlainKey *k)
00102 {
00103   cBNKey *bk=dynamic_cast<cBNKey *>(k); // downcast
00104   return bk && BN_cmp(*key,*bk->key)==0;
00105 }
00106 
00107 void cBNKey::Get(void *mem)
00108 {
00109   BN_copy((BIGNUM *)mem,*key);
00110 }
00111 
00112 cString cBNKey::Print(void)
00113 {
00114   unsigned char *mem=AUTOMEM(keylen);
00115   if(rotate) key->PutLE(mem,keylen); else key->Put(mem,keylen);
00116   char *str=(char *)malloc(keylen*2+2);
00117   if(str) HexStr(str,mem,keylen);
00118   return cString(str,true);
00119 }
00120 
00121 // -- cDualKey -----------------------------------------------------------------
00122 
00123 cDualKey::cDualKey(bool Super, bool Rotate)
00124 :cMutableKey(Super)
00125 {
00126   rotate=Rotate;
00127 }
00128 
00129 cPlainKey *cDualKey::Alloc(void) const
00130 {
00131   if(IsBNKey()) return new cBNKey(CanSupersede(),rotate);
00132   else          return new cHexKey(CanSupersede());
00133 }
00134 
00135 // -- cPlainKeyStd ---------------------------------------------------------------
00136 
00137 #define PLAINLEN_STD 8
00138 
00139 cPlainKeyStd::cPlainKeyStd(bool Super)
00140 :cHexKey(Super)
00141 {}
00142 
00143 bool cPlainKeyStd::Parse(const char *line)
00144 {
00145   unsigned char sid[3], skeynr, skey[PLAINLEN_STD];
00146   int len;
00147   if(GetChar(line,&type,1) && (len=GetHex(line,sid,3,false)) &&
00148      GetHex(line,&skeynr,1) && GetHex(line,skey,PLAINLEN_STD)) {
00149     type=toupper(type); id=Bin2Int(sid,len); keynr=skeynr;
00150     SetBinKey(skey,PLAINLEN_STD);
00151     return true;
00152     }
00153   return false;
00154 }
00155 
00156 // -- cSystemScCore ------------------------------------------------------------
00157 
00158 cSystemScCore::cSystemScCore(const char *Name, int Pri, int ScId, const char *ScName)
00159 :cSystem(Name,Pri)
00160 {
00161   scId=ScId; scName=ScName;
00162 }
00163 
00164 bool cSystemScCore::ProcessECM(const cEcmInfo *ecm, unsigned char *source)
00165 {
00166   bool res=false;
00167   cSmartCard *card=smartcards.LockCard(scId);
00168   if(card) {
00169     res=card->Decode(ecm,source,cw);
00170     smartcards.ReleaseCard(card);
00171     if(res) KeyOK(scName);
00172     }
00173   return res;
00174 }
00175 
00176 void cSystemScCore::ProcessEMM(int pid, int caid, const unsigned char *buffer)
00177 {
00178   cSmartCard *card=smartcards.LockCard(scId);
00179   if(card) {
00180     card->Update(pid,caid,buffer);
00181     smartcards.ReleaseCard(card);
00182     }
00183 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines