[3181] | 1 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
[8] | 2 | #include "globals.h"
|
---|
| 3 | #include "reader-common.h"
|
---|
| 4 |
|
---|
| 5 | static const uchar CryptTable[256] =
|
---|
| 6 | {
|
---|
| 7 | 0xDA, 0x26, 0xE8, 0x72, 0x11, 0x52, 0x3E, 0x46,
|
---|
| 8 | 0x32, 0xFF, 0x8C, 0x1E, 0xA7, 0xBE, 0x2C, 0x29,
|
---|
| 9 | 0x5F, 0x86, 0x7E, 0x75, 0x0A, 0x08, 0xA5, 0x21,
|
---|
| 10 | 0x61, 0xFB, 0x7A, 0x58, 0x60, 0xF7, 0x81, 0x4F,
|
---|
| 11 | 0xE4, 0xFC, 0xDF, 0xB1, 0xBB, 0x6A, 0x02, 0xB3,
|
---|
| 12 | 0x0B, 0x6E, 0x5D, 0x5C, 0xD5, 0xCF, 0xCA, 0x2A,
|
---|
| 13 | 0x14, 0xB7, 0x90, 0xF3, 0xD9, 0x37, 0x3A, 0x59,
|
---|
| 14 | 0x44, 0x69, 0xC9, 0x78, 0x30, 0x16, 0x39, 0x9A,
|
---|
| 15 | 0x0D, 0x05, 0x1F, 0x8B, 0x5E, 0xEE, 0x1B, 0xC4,
|
---|
| 16 | 0x76, 0x43, 0xBD, 0xEB, 0x42, 0xEF, 0xF9, 0xD0,
|
---|
| 17 | 0x4D, 0xE3, 0xF4, 0x57, 0x56, 0xA3, 0x0F, 0xA6,
|
---|
| 18 | 0x50, 0xFD, 0xDE, 0xD2, 0x80, 0x4C, 0xD3, 0xCB,
|
---|
| 19 | 0xF8, 0x49, 0x8F, 0x22, 0x71, 0x84, 0x33, 0xE0,
|
---|
| 20 | 0x47, 0xC2, 0x93, 0xBC, 0x7C, 0x3B, 0x9C, 0x7D,
|
---|
| 21 | 0xEC, 0xC3, 0xF1, 0x89, 0xCE, 0x98, 0xA2, 0xE1,
|
---|
| 22 | 0xC1, 0xF2, 0x27, 0x12, 0x01, 0xEA, 0xE5, 0x9B,
|
---|
| 23 | 0x25, 0x87, 0x96, 0x7B, 0x34, 0x45, 0xAD, 0xD1,
|
---|
| 24 | 0xB5, 0xDB, 0x83, 0x55, 0xB0, 0x9E, 0x19, 0xD7,
|
---|
| 25 | 0x17, 0xC6, 0x35, 0xD8, 0xF0, 0xAE, 0xD4, 0x2B,
|
---|
| 26 | 0x1D, 0xA0, 0x99, 0x8A, 0x15, 0x00, 0xAF, 0x2D,
|
---|
| 27 | 0x09, 0xA8, 0xF5, 0x6C, 0xA1, 0x63, 0x67, 0x51,
|
---|
| 28 | 0x3C, 0xB2, 0xC0, 0xED, 0x94, 0x03, 0x6F, 0xBA,
|
---|
| 29 | 0x3F, 0x4E, 0x62, 0x92, 0x85, 0xDD, 0xAB, 0xFE,
|
---|
| 30 | 0x10, 0x2E, 0x68, 0x65, 0xE7, 0x04, 0xF6, 0x0C,
|
---|
| 31 | 0x20, 0x1C, 0xA9, 0x53, 0x40, 0x77, 0x2F, 0xA4,
|
---|
| 32 | 0xFA, 0x6D, 0x73, 0x28, 0xE2, 0xCD, 0x79, 0xC8,
|
---|
| 33 | 0x97, 0x66, 0x8E, 0x82, 0x74, 0x06, 0xC7, 0x88,
|
---|
| 34 | 0x1A, 0x4A, 0x6B, 0xCC, 0x41, 0xE9, 0x9D, 0xB8,
|
---|
| 35 | 0x23, 0x9F, 0x3D, 0xBF, 0x8D, 0x95, 0xC5, 0x13,
|
---|
| 36 | 0xB9, 0x24, 0x5A, 0xDC, 0x64, 0x18, 0x38, 0x91,
|
---|
| 37 | 0x7F, 0x5B, 0x70, 0x54, 0x07, 0xB6, 0x4B, 0x0E,
|
---|
| 38 | 0x36, 0xAC, 0x31, 0xE6, 0xD6, 0x48, 0xAA, 0xB4
|
---|
| 39 | };
|
---|
| 40 |
|
---|
[3120] | 41 | uchar
|
---|
[8] | 42 | sc_GetCountryCode[] = { 0x02, 0x02, 0x03, 0x00, 0x00 },
|
---|
| 43 | sc_GetASCIISerial[] = { 0x02, 0x00, 0x03, 0x00, 0x00 },
|
---|
| 44 | sc_GetHEXSerial[] = { 0x02, 0x01, 0x00, 0x00, 0x00 },
|
---|
[3120] | 45 | sc_GetCardFile[] = { 0x02, 0x0E, 0x02, 0x00, 0x00 },
|
---|
| 46 | sc_GetChanelIds[] = { 0x02, 0x04, 0x00, 0x00, 0x01, 0x00 },
|
---|
| 47 | sc_CamKey[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 },
|
---|
[8] | 48 | sc_GetProvider[] = { 0x02, 0x03, 0x03, 0x00, 0x00 },
|
---|
[3120] | 49 | sc_GetCamKey383C[] = { 0x02, 0x09, 0x03, 0x00, 0x40,
|
---|
| 50 | 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
|
---|
| 51 | 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
|
---|
| 52 | 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
|
---|
| 53 | 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF,
|
---|
| 54 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
---|
| 55 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
---|
| 56 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
---|
| 57 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
---|
| 58 |
|
---|
| 59 | static const uchar
|
---|
[8] | 60 | sc_GetCountryCode2[]= { 0x02, 0x0B, 0x00, 0x00, 0x00 },
|
---|
| 61 | sc_GetCamKey384CZ[] = { 0x02, 0x09, 0x03, 0x00, 0x40,
|
---|
| 62 | 0x18, 0xD7, 0x55, 0x14, 0xC0, 0x83, 0xF1, 0x38,
|
---|
| 63 | 0x39, 0x6F, 0xF2, 0xEC, 0x4F, 0xE3, 0xF1, 0x85,
|
---|
| 64 | 0x01, 0x46, 0x06, 0xCE, 0x7D, 0x08, 0x2C, 0x74,
|
---|
| 65 | 0x46, 0x8F, 0x72, 0xC4, 0xEA, 0xD7, 0x9C, 0xE0,
|
---|
| 66 | 0xE1, 0xFF, 0x58, 0xE7, 0x70, 0x0C, 0x92, 0x45,
|
---|
| 67 | 0x26, 0x18, 0x4F, 0xA0, 0xE2, 0xF5, 0x9E, 0x46,
|
---|
| 68 | 0x6F, 0xAE, 0x95, 0x35, 0xB0, 0x49, 0xB2, 0x0E,
|
---|
| 69 | 0xA4, 0x1F, 0x8E, 0x47, 0xD0, 0x24, 0x11, 0xD0 },
|
---|
| 70 | sc_GetCamKey384DZ[] = { 0x02, 0x09, 0x03, 0x00, 0x40,
|
---|
| 71 | 0x27, 0xF2, 0xD6, 0xCD, 0xE6, 0x88, 0x62, 0x46,
|
---|
| 72 | 0x81, 0xB0, 0xF5, 0x3E, 0x6F, 0x13, 0x4D, 0xCC,
|
---|
| 73 | 0xFE, 0xD0, 0x67, 0xB1, 0x93, 0xDD, 0xF4, 0xDE,
|
---|
| 74 | 0xEF, 0xF5, 0x3B, 0x04, 0x1D, 0xE5, 0xC3, 0xB2,
|
---|
| 75 | 0x54, 0x38, 0x57, 0x7E, 0xC8, 0x39, 0x07, 0x2E,
|
---|
| 76 | 0xD2, 0xF4, 0x05, 0xAA, 0x15, 0xB5, 0x55, 0x24,
|
---|
| 77 | 0x90, 0xBB, 0x9B, 0x00, 0x96, 0xF0, 0xCB, 0xF1,
|
---|
| 78 | 0x8A, 0x08, 0x7F, 0x0B, 0xB8, 0x79, 0xC3, 0x5D },
|
---|
| 79 | sc_GetCamKey384FZ[] = { 0x02, 0x09, 0x03, 0x00, 0x40,
|
---|
| 80 | 0x62, 0xFE, 0xD8, 0x4F, 0x44, 0x86, 0x2C, 0x21,
|
---|
| 81 | 0x50, 0x9A, 0xBE, 0x27, 0x15, 0x9E, 0xC4, 0x48,
|
---|
| 82 | 0xF3, 0x73, 0x5C, 0xBD, 0x08, 0x64, 0x6D, 0x13,
|
---|
| 83 | 0x64, 0x90, 0x14, 0xDB, 0xFF, 0xC3, 0xFE, 0x03,
|
---|
| 84 | 0x97, 0xFA, 0x75, 0x08, 0x12, 0xF9, 0x8F, 0x84,
|
---|
| 85 | 0x83, 0x17, 0xAA, 0x6F, 0xEF, 0x2C, 0x10, 0x1B,
|
---|
| 86 | 0xBF, 0x31, 0x41, 0xC3, 0x54, 0x2F, 0x65, 0x50,
|
---|
[3120] | 87 | 0x95, 0xA9, 0x64, 0x22, 0x5E, 0xA4, 0xAF, 0xA9 };
|
---|
[8] | 88 |
|
---|
[2855] | 89 | typedef struct chid_base_date {
|
---|
| 90 | ushort caid;
|
---|
| 91 | ushort acs;
|
---|
| 92 | char c_code[4];
|
---|
| 93 | long base;
|
---|
| 94 | } CHID_BASE_DATE;
|
---|
| 95 |
|
---|
[8] | 96 | static void XRotateLeft8Byte(uchar *buf)
|
---|
| 97 | {
|
---|
| 98 | int k;
|
---|
| 99 | uchar t1=buf[7];
|
---|
| 100 | uchar t2=0;
|
---|
| 101 | for(k=0; k<=7; k++)
|
---|
| 102 | {
|
---|
| 103 | t2=t1;
|
---|
| 104 | t1=buf[k];
|
---|
| 105 | buf[k]=(buf[k]<<1)|(t2>>7);
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | static void ReverseSessionKeyCrypt(const uchar *camkey, uchar *key)
|
---|
| 110 | {
|
---|
| 111 | uchar localkey[8], tmp1, tmp2;
|
---|
| 112 | int idx1,idx2;
|
---|
| 113 |
|
---|
| 114 | memcpy(localkey, camkey, 8) ;
|
---|
| 115 | for(idx1=0; idx1<8; idx1++)
|
---|
| 116 | {
|
---|
| 117 | for(idx2=0; idx2<8; idx2++)
|
---|
| 118 | {
|
---|
| 119 | tmp1 = CryptTable[key[7] ^ localkey[idx2] ^ idx1] ;
|
---|
| 120 | tmp2 = key[0] ;
|
---|
| 121 | key[0] = key[1] ;
|
---|
| 122 | key[1] = key[2] ;
|
---|
| 123 | key[2] = key[3] ;
|
---|
| 124 | key[3] = key[4] ;
|
---|
| 125 | key[4] = key[5] ;
|
---|
| 126 | key[5] = key[6] ^ tmp1 ;
|
---|
| 127 | key[6] = key[7] ;
|
---|
| 128 | key[7] = tmp1 ^ tmp2 ;
|
---|
| 129 | }
|
---|
| 130 | XRotateLeft8Byte(localkey);
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[2814] | 134 | static time_t chid_date(struct s_reader * reader, ulong date, char *buf, int l)
|
---|
[8] | 135 | {
|
---|
[2855] | 136 |
|
---|
[2814] | 137 | // Irdeto date starts 01.08.1997 which is
|
---|
| 138 | // 870393600 seconds in unix calendar time
|
---|
| 139 | //
|
---|
| 140 | // The above might not be true for all Irdeto card
|
---|
| 141 | // we need to find a way to identify cards to set the base date
|
---|
| 142 | // like we did for NDS
|
---|
| 143 | //
|
---|
| 144 | // this is the known default value.
|
---|
[2815] | 145 | long date_base=870393600L; // this is actually 31.07.1997, 17:00
|
---|
[3012] | 146 | // CAID, ACS, Country, base date D . M. Y, h : m
|
---|
[2855] | 147 | CHID_BASE_DATE table[] = { {0x0604, 0x1541, "GRC", 977817600L}, // 26.12.2000, 00:00
|
---|
| 148 | {0x0604, 0x1542, "GRC", 977817600L}, // 26.12.2000, 00:00
|
---|
| 149 | {0x0604, 0x1543, "GRC", 977817600L}, // 26.12.2000, 00:00
|
---|
| 150 | {0x0604, 0x1544, "GRC", 977817600L}, // 26.12.2000, 17:00
|
---|
| 151 | {0x0628, 0x0606, "MCR", 1159574400L}, // 29.09.2006, 00:00
|
---|
[3012] | 152 | {0x0604, 0x0608, "EGY", 999993600L}, // 08.09.2001, 17:00
|
---|
| 153 | {0x0604, 0x0606, "EGY", 1003276800L}, // 16.10.2001, 17:00
|
---|
| 154 | {0x0627, 0x0608, "EGY", 946598400L}, // 30.12.1999, 16:00
|
---|
| 155 | {0x0664, 0x0608, "TUR", 946598400L}, // 31.12.1999, 00:00
|
---|
[3014] | 156 | // {0x1702, 0x0384, "GER", 888883200L}, // 02.03.1998, 16:00 -> this fixes some card but break others (S02).
|
---|
[2855] | 157 | {0x0, 0x0, "", 0L}
|
---|
| 158 | };
|
---|
| 159 |
|
---|
[2814] | 160 | // now check for specific providers base date
|
---|
[2855] | 161 | int i=0;
|
---|
| 162 | while(table[i].caid) {
|
---|
| 163 | if(reader->caid[0]==table[i].caid && reader->acs==table[i].acs && !memcmp(reader->country_code,table[i].c_code,3) ) {
|
---|
| 164 | date_base = table[i].base;
|
---|
| 165 | break;
|
---|
[2814] | 166 | }
|
---|
[2855] | 167 | i++;
|
---|
[2814] | 168 | }
|
---|
[2855] | 169 |
|
---|
[2814] | 170 | time_t ut=date_base+date*(24*3600);
|
---|
| 171 | if (buf) {
|
---|
| 172 | struct tm *t;
|
---|
| 173 | t=gmtime(&ut);
|
---|
| 174 | snprintf(buf, l, "%04d/%02d/%02d", t->tm_year+1900, t->tm_mon+1, t->tm_mday);
|
---|
| 175 | }
|
---|
| 176 | return(ut);
|
---|
[8] | 177 | }
|
---|
| 178 |
|
---|
[1951] | 179 | static int irdeto_do_cmd(struct s_reader * reader, uchar *buf, ushort good, uchar * cta_res, ushort * p_cta_lr)
|
---|
[8] | 180 | {
|
---|
[3011] | 181 | int rc;
|
---|
| 182 | if( (rc = reader_cmd2icc(reader, buf, buf[4] + 5, cta_res, p_cta_lr)) )
|
---|
| 183 | return(rc); // result may be 0 (success) or negative
|
---|
| 184 | if (*p_cta_lr < 2)
|
---|
| 185 | return(0x7F7F); // this should never happen
|
---|
| 186 | return(good != b2i(2, cta_res+*p_cta_lr-2));
|
---|
[8] | 187 | }
|
---|
| 188 |
|
---|
| 189 | #define reader_chk_cmd(cmd, l) \
|
---|
| 190 | { \
|
---|
[1951] | 191 | if (reader_cmd2icc(reader, cmd, sizeof(cmd), cta_res, &cta_lr)) return ERROR; \
|
---|
[1389] | 192 | if (l && (cta_lr!=l)) return ERROR; }
|
---|
[8] | 193 |
|
---|
[1926] | 194 | static int irdeto_card_init_provider(struct s_reader * reader)
|
---|
[1507] | 195 | {
|
---|
[3011] | 196 | def_resp;
|
---|
| 197 | int i, p;
|
---|
| 198 | uchar buf[256] = {0};
|
---|
[1507] | 199 |
|
---|
[3011] | 200 | /*
|
---|
| 201 | * Provider
|
---|
| 202 | */
|
---|
| 203 | memset(reader->prid, 0xff, sizeof(reader->prid));
|
---|
| 204 | for (buf[0] = i = p = 0; i<reader->nprov; i++)
|
---|
| 205 | {
|
---|
| 206 | sc_GetProvider[3] = i;
|
---|
| 207 | reader_chk_cmd(sc_GetProvider, 0);
|
---|
| 208 | //if ((cta_lr==26) && (cta_res[0]!=0xf))
|
---|
| 209 | if ((cta_lr == 26) && ((!(i&1)) || (cta_res[0] != 0xf)))
|
---|
| 210 | {
|
---|
| 211 | reader->prid[i][4] = p++;
|
---|
[1827] | 212 |
|
---|
[3011] | 213 | // maps the provider id for Betacrypt from FFFFFF to 000000,
|
---|
| 214 | // fixes problems with cascading CCcam and OSCam
|
---|
| 215 | if ((reader->caid[0] >= 0x1700) && (reader->caid[0] <= 0x1799))
|
---|
| 216 | memset(&reader->prid[i][0], 0, 4);
|
---|
| 217 | else
|
---|
| 218 | memcpy(&reader->prid[i][0], cta_res, 4);
|
---|
[1827] | 219 |
|
---|
[3011] | 220 | sprintf((char *) buf+strlen((char *)buf), ",%06lx", b2i(3, &reader->prid[i][1]));
|
---|
| 221 | }
|
---|
| 222 | else
|
---|
| 223 | reader->prid[i][0] = 0xf;
|
---|
| 224 | }
|
---|
| 225 | if (p)
|
---|
| 226 | cs_ri_log(reader, "active providers: %d (%s)", p, buf + 1);
|
---|
[1507] | 227 |
|
---|
[3011] | 228 | return OK;
|
---|
[1507] | 229 | }
|
---|
| 230 |
|
---|
[1926] | 231 | int irdeto_card_init(struct s_reader * reader, ATR newatr)
|
---|
[8] | 232 | {
|
---|
[3011] | 233 | def_resp;
|
---|
| 234 | get_atr;
|
---|
| 235 | int camkey = 0;
|
---|
| 236 | uchar buf[256] = {0};
|
---|
[8] | 237 |
|
---|
[3011] | 238 | if (memcmp(atr+4, "IRDETO", 6))
|
---|
| 239 | return ERROR;
|
---|
| 240 | cs_ri_log(reader, "detect irdeto card");
|
---|
[8] | 241 |
|
---|
[3011] | 242 | if(reader->has_rsa && !reader->force_irdeto) // we use rsa from config as camkey
|
---|
| 243 | {
|
---|
| 244 | cs_debug("[irdeto-reader] using camkey data from config");
|
---|
| 245 | memcpy(&sc_GetCamKey383C[5], reader->rsa_mod, 0x40);
|
---|
| 246 | memcpy(sc_CamKey, reader->nagra_boxkey, 8);
|
---|
| 247 | cs_debug("[irdeto-reader] camkey: %s", cs_hexdump (0, sc_CamKey, 8));
|
---|
| 248 | cs_debug("[irdeto-reader] camkey-data: %s", cs_hexdump (0, &sc_GetCamKey383C[5], 32));
|
---|
| 249 | cs_debug("[irdeto-reader] camkey-data: %s", cs_hexdump (0, &sc_GetCamKey383C[37], 32));
|
---|
| 250 | }
|
---|
[8] | 251 |
|
---|
[3011] | 252 | /*
|
---|
| 253 | * ContryCode
|
---|
| 254 | */
|
---|
| 255 | reader_chk_cmd(sc_GetCountryCode, 18);
|
---|
| 256 | reader->acs = (cta_res[0] << 8) | cta_res[1];
|
---|
| 257 | reader->caid[0] = (cta_res[5] << 8) | cta_res[6];
|
---|
| 258 | memcpy(reader->country_code,cta_res + 13, 3);
|
---|
| 259 | cs_ri_log(reader, "caid: %04X, acs: %x.%02x, country code: %c%c%c",
|
---|
| 260 | reader->caid[0], cta_res[0], cta_res[1], cta_res[13], cta_res[14], cta_res[15]);
|
---|
[8] | 261 |
|
---|
[3011] | 262 | /*
|
---|
| 263 | * Ascii/Hex-Serial
|
---|
| 264 | */
|
---|
| 265 | reader_chk_cmd(sc_GetASCIISerial, 22);
|
---|
| 266 | memcpy(buf, cta_res, 10);
|
---|
| 267 | buf[10] = 0;
|
---|
| 268 | reader_chk_cmd(sc_GetHEXSerial, 18);
|
---|
| 269 | memcpy(reader->hexserial, cta_res+12, 8);
|
---|
| 270 | reader->nprov = cta_res[10];
|
---|
| 271 | cs_ri_log(reader, "providers: %d, ascii serial: %s, hex serial: %02X%02X%02X, hex base: %02X",
|
---|
| 272 | reader->nprov, buf, cta_res[12], cta_res[13], cta_res[14], cta_res[15]);
|
---|
[8] | 273 |
|
---|
[3011] | 274 | /*
|
---|
| 275 | * CardFile
|
---|
| 276 | */
|
---|
| 277 | for (sc_GetCardFile[2] = 2; sc_GetCardFile[2] < 4; sc_GetCardFile[2]++)
|
---|
| 278 | reader_chk_cmd(sc_GetCardFile, 0);
|
---|
[8] | 279 |
|
---|
[3011] | 280 | /*
|
---|
| 281 | * CamKey
|
---|
| 282 | */
|
---|
| 283 | if ((atr[14] == 0x03) && (atr[15] == 0x84) && (atr[16] == 0x55))
|
---|
| 284 | {
|
---|
| 285 | switch (reader->caid[0])
|
---|
| 286 | {
|
---|
| 287 | case 0x1702: camkey = 1; break;
|
---|
| 288 | case 0x1722: camkey = 2; break;
|
---|
| 289 | case 0x1762: camkey = 3; break;
|
---|
| 290 | default : camkey = 4; break;
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
[8] | 293 |
|
---|
[3011] | 294 | cs_debug("[irdeto-reader] set camkey for type=%d", camkey);
|
---|
| 295 |
|
---|
| 296 | switch (camkey)
|
---|
| 297 | {
|
---|
| 298 | case 1:
|
---|
| 299 | reader_chk_cmd(sc_GetCamKey384CZ, 10);
|
---|
| 300 | break;
|
---|
| 301 | case 2:
|
---|
| 302 | reader_chk_cmd(sc_GetCamKey384DZ, 10);
|
---|
| 303 | break;
|
---|
| 304 | case 3:
|
---|
| 305 | reader_chk_cmd(sc_GetCamKey384FZ, 10);
|
---|
| 306 | break;
|
---|
| 307 | default:
|
---|
| 308 | reader_chk_cmd(sc_GetCamKey383C, 0);
|
---|
| 309 | break;
|
---|
| 310 | }
|
---|
[1926] | 311 | if (reader->cardmhz != 600)
|
---|
[1399] | 312 | cs_log("WARNING: For Irdeto cards you will have to set 'cardmhz = 600' in oscam.server");
|
---|
[1824] | 313 |
|
---|
[3011] | 314 | return irdeto_card_init_provider(reader);
|
---|
[8] | 315 | }
|
---|
| 316 |
|
---|
[1926] | 317 | int irdeto_do_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
[8] | 318 | {
|
---|
[3011] | 319 | def_resp; cta_lr = 0; //suppress compiler error
|
---|
| 320 | static const uchar sc_EcmCmd[] = { 0x05, 0x00, 0x00, 0x02, 0x00 };
|
---|
| 321 | uchar cta_cmd[272];
|
---|
[8] | 322 |
|
---|
[3011] | 323 | memcpy(cta_cmd, sc_EcmCmd, sizeof(sc_EcmCmd));
|
---|
| 324 | cta_cmd[4] = (er->ecm[2]) - 3;
|
---|
| 325 | memcpy(cta_cmd + sizeof(sc_EcmCmd), &er->ecm[6], cta_cmd[4]);
|
---|
| 326 |
|
---|
| 327 | if (irdeto_do_cmd(reader, cta_cmd, 0x9D00, cta_res, &cta_lr)) {
|
---|
| 328 | if(cta_lr >= 2)
|
---|
| 329 | snprintf( er->msglog, MSGLOGSIZE, "irdeto_do_cmd [%d] %02x %02x", cta_lr, cta_res[cta_lr - 2], cta_res[cta_lr - 1] );
|
---|
| 330 | else
|
---|
| 331 | snprintf( er->msglog, MSGLOGSIZE, "irdeto_do_cmd [%d]<2", cta_lr);
|
---|
| 332 |
|
---|
| 333 | return ERROR;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | if (cta_lr < 24) {
|
---|
| 337 | snprintf( er->msglog, MSGLOGSIZE, "cta_lr (%d) < 24",cta_lr );
|
---|
| 338 | return ERROR;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | ReverseSessionKeyCrypt(sc_CamKey, cta_res+6);
|
---|
| 342 | ReverseSessionKeyCrypt(sc_CamKey, cta_res+14);
|
---|
| 343 | memcpy(er->cw, cta_res + 6, 16);
|
---|
| 344 | return OK;
|
---|
[8] | 345 | }
|
---|
| 346 |
|
---|
[1782] | 347 | int irdeto_get_emm_type(EMM_PACKET *ep, struct s_reader * rdr) {
|
---|
| 348 |
|
---|
[2091] | 349 | int i, l = (ep->emm[3]&0x07);
|
---|
[2000] | 350 | int base = (ep->emm[3]>>3);
|
---|
[1807] | 351 | char dumprdrserial[l*3];
|
---|
[1782] | 352 |
|
---|
| 353 | cs_debug_mask(D_EMM, "Entered irdeto_get_emm_type ep->emm[3]=%02x",ep->emm[3]);
|
---|
| 354 |
|
---|
[2000] | 355 | switch (l) {
|
---|
[1782] | 356 |
|
---|
[2000] | 357 | case 0:
|
---|
| 358 | // global emm, 0 bytes addressed
|
---|
[1782] | 359 | ep->type = GLOBAL;
|
---|
| 360 | cs_debug_mask(D_EMM, "IRDETO EMM: GLOBAL");
|
---|
| 361 | return TRUE;
|
---|
[2163] | 362 |
|
---|
[2000] | 363 | case 2:
|
---|
| 364 | // shared emm, 2 bytes addressed
|
---|
[1782] | 365 | ep->type = SHARED;
|
---|
| 366 | memset(ep->hexserial, 0, 8);
|
---|
| 367 | memcpy(ep->hexserial, ep->emm + 4, l);
|
---|
[1807] | 368 | strcpy(dumprdrserial, cs_hexdump(1, rdr->hexserial, l));
|
---|
[2091] | 369 | cs_debug_mask(D_EMM, "IRDETO EMM: SHARED l = %d ep = %s rdr = %s base = %02x", l,
|
---|
[3011] | 370 | cs_hexdump(1, ep->hexserial, l), dumprdrserial, base);
|
---|
[2091] | 371 |
|
---|
| 372 | if (base & 0x10) {
|
---|
| 373 | // hex addressed
|
---|
[2092] | 374 | return (base == rdr->hexserial[3] && !memcmp(ep->emm + 4, rdr->hexserial, l));
|
---|
[2091] | 375 | }
|
---|
| 376 | else {
|
---|
| 377 | // provider addressed
|
---|
| 378 | for(i = 0; i < rdr->nprov; i++)
|
---|
[2092] | 379 | if (base == rdr->prid[i][0] && !memcmp(ep->emm + 4, &rdr->prid[i][1], l))
|
---|
[2091] | 380 | return TRUE;
|
---|
| 381 | }
|
---|
[2163] | 382 | cs_debug_mask(D_EMM, "IRDETO EMM: neither hex nor provider addressed or unknown provider id");
|
---|
| 383 | return FALSE;
|
---|
[3011] | 384 |
|
---|
[2000] | 385 | case 3:
|
---|
| 386 | // unique emm, 3 bytes addressed
|
---|
[1782] | 387 | ep->type = UNIQUE;
|
---|
| 388 | memset(ep->hexserial, 0, 8);
|
---|
| 389 | memcpy(ep->hexserial, ep->emm + 4, l);
|
---|
[1807] | 390 | strcpy(dumprdrserial, cs_hexdump(1, rdr->hexserial, l));
|
---|
[2091] | 391 | cs_debug_mask(D_EMM, "IRDETO EMM: UNIQUE l = %d ep = %s rdr = %s", l,
|
---|
[3011] | 392 | cs_hexdump(1, ep->hexserial, l), dumprdrserial);
|
---|
[2091] | 393 |
|
---|
[2092] | 394 | return (base == rdr->hexserial[3] && !memcmp(ep->emm + 4, rdr->hexserial, l));
|
---|
[2000] | 395 |
|
---|
[1782] | 396 | default:
|
---|
| 397 | ep->type = UNKNOWN;
|
---|
| 398 | cs_debug_mask(D_EMM, "IRDETO EMM: UNKNOWN");
|
---|
| 399 | return TRUE;
|
---|
[1775] | 400 | }
|
---|
[1782] | 401 |
|
---|
[1766] | 402 | }
|
---|
| 403 |
|
---|
[2074] | 404 | void irdeto_get_emm_filter(struct s_reader * rdr, uchar *filter)
|
---|
[1976] | 405 | {
|
---|
[2074] | 406 | filter[0]=0xFF;
|
---|
| 407 | filter[1]=3;
|
---|
[1976] | 408 |
|
---|
[2000] | 409 | int base = rdr->hexserial[3];
|
---|
| 410 | int emm_g = base * 8;
|
---|
| 411 | int emm_s = emm_g + 2;
|
---|
| 412 | int emm_u = emm_g + 3;
|
---|
| 413 |
|
---|
[2074] | 414 | filter[2]=GLOBAL;
|
---|
| 415 | filter[3]=0;
|
---|
| 416 |
|
---|
| 417 | filter[4+0] = 0x82;
|
---|
| 418 | filter[4+0+16] = 0xFF;
|
---|
| 419 | filter[4+1] = emm_g;
|
---|
| 420 | filter[4+1+16] = 0x0F;
|
---|
[2000] | 421 |
|
---|
[2075] | 422 | filter[36]=SHARED;
|
---|
[2074] | 423 | filter[37]=0;
|
---|
| 424 | filter[38+0] = 0x82;
|
---|
| 425 | filter[38+0+16] = 0xFF;
|
---|
| 426 | filter[38+1] = emm_s;
|
---|
| 427 | filter[38+1+16] = 0xFF;
|
---|
| 428 | memcpy(filter+38+2, rdr->hexserial, 2);
|
---|
| 429 | memset(filter+38+2+16, 0xFF, 2);
|
---|
[1976] | 430 |
|
---|
[2075] | 431 | filter[70]=UNIQUE;
|
---|
[2074] | 432 | filter[71]=0;
|
---|
| 433 | filter[72+0] = 0x82;
|
---|
| 434 | filter[72+0+16] = 0xFF;
|
---|
| 435 | filter[72+1] = emm_u;
|
---|
| 436 | filter[72+1+16] = 0xFF;
|
---|
[2114] | 437 | memcpy(filter+72+2, rdr->hexserial, 3);
|
---|
| 438 | memset(filter+72+2+16, 0xFF, 3);
|
---|
[2074] | 439 |
|
---|
| 440 | return;
|
---|
[1976] | 441 | }
|
---|
| 442 |
|
---|
[1926] | 443 | int irdeto_do_emm(struct s_reader * reader, EMM_PACKET *ep)
|
---|
[8] | 444 | {
|
---|
[3011] | 445 | def_resp;
|
---|
| 446 | static const uchar sc_EmmCmd[] = { 0x01,0x00,0x00,0x00,0x00 };
|
---|
| 447 | uchar cta_cmd[272];
|
---|
[8] | 448 |
|
---|
[3011] | 449 | int i, l = (ep->emm[3] & 0x07), ok = 0;
|
---|
| 450 | int mode = (ep->emm[3] >> 3);
|
---|
[1090] | 451 |
|
---|
[3011] | 452 | uchar *emm = ep->emm;
|
---|
| 453 | if (mode & 0x10) // Hex addressed
|
---|
| 454 | {
|
---|
| 455 | ok=(mode == reader->hexserial[3] &&
|
---|
| 456 | (!l || !memcmp(&emm[4], reader->hexserial, l)));
|
---|
| 457 | }
|
---|
| 458 | else // Provider addressed
|
---|
| 459 | {
|
---|
| 460 | for(i = 0; i < reader->nprov; i++)
|
---|
| 461 | {
|
---|
| 462 | ok=(mode == reader->prid[i][0] &&
|
---|
| 463 | (!l || !memcmp(&emm[4], &reader->prid[i][1], l)));
|
---|
| 464 | if (ok) break;
|
---|
| 465 | }
|
---|
| 466 | }
|
---|
[1090] | 467 |
|
---|
[3011] | 468 | if (ok)
|
---|
| 469 | {
|
---|
| 470 | l++;
|
---|
| 471 | if (l <= ADDRLEN)
|
---|
| 472 | {
|
---|
| 473 | const int dataLen = SCT_LEN(emm) - 5 - l; // sizeof of emm bytes (nanos)
|
---|
| 474 | uchar *ptr = cta_cmd;
|
---|
| 475 | memcpy(ptr, sc_EmmCmd, sizeof(sc_EmmCmd)); // copy card command
|
---|
| 476 | ptr[4] = dataLen + ADDRLEN; // set card command emm size
|
---|
| 477 | ptr += sizeof(sc_EmmCmd); emm += 3;
|
---|
| 478 | memset(ptr, 0, ADDRLEN); // clear addr range
|
---|
| 479 | memcpy(ptr, emm, l); // copy addr bytes
|
---|
| 480 | ptr += ADDRLEN; emm += l;
|
---|
| 481 | memcpy(ptr, &emm[2], dataLen); // copy emm bytes
|
---|
| 482 | return(irdeto_do_cmd(reader, cta_cmd, 0, cta_res, &cta_lr) ? 0 : 1);
|
---|
| 483 | }
|
---|
| 484 | else
|
---|
| 485 | cs_debug("[irdeto-reader] addrlen %d > %d", l, ADDRLEN);
|
---|
| 486 | }
|
---|
| 487 | return ERROR;
|
---|
[8] | 488 | }
|
---|
| 489 |
|
---|
[1926] | 490 | int irdeto_card_info(struct s_reader * reader)
|
---|
[8] | 491 | {
|
---|
[1951] | 492 | def_resp;
|
---|
[1824] | 493 | int i, p;
|
---|
| 494 |
|
---|
[1828] | 495 | /*
|
---|
| 496 | * ContryCode2
|
---|
| 497 | */
|
---|
| 498 | reader_chk_cmd(sc_GetCountryCode2, 0);
|
---|
| 499 |
|
---|
[1824] | 500 | if ((cta_lr>9) && !(cta_res[cta_lr-2]|cta_res[cta_lr-1]))
|
---|
| 501 | {
|
---|
| 502 | cs_debug("[irdeto-reader] max chids: %d, %d, %d, %d", cta_res[6], cta_res[7], cta_res[8], cta_res[9]);
|
---|
| 503 |
|
---|
| 504 | /*
|
---|
| 505 | * Provider 2
|
---|
| 506 | */
|
---|
[1926] | 507 | for (i=p=0; i<reader->nprov; i++)
|
---|
[1824] | 508 | {
|
---|
| 509 | int j, k, chid, first=1;
|
---|
| 510 | char t[32];
|
---|
[1926] | 511 | if (reader->prid[i][4]!=0xff)
|
---|
[1824] | 512 | {
|
---|
| 513 | p++;
|
---|
[2398] | 514 | sc_GetChanelIds[3]=i; // provider at index i
|
---|
| 515 | j=0;
|
---|
| 516 | // for (j=0; j<10; j++) => why 10 .. do we know for sure the there are only 10 chids !!!
|
---|
| 517 | // shouldn't it me the max chid value we read above ?!
|
---|
| 518 | while(1) // will exit if cta_lr < 61 .. which is the correct break condition.
|
---|
[1824] | 519 | {
|
---|
[2398] | 520 | sc_GetChanelIds[5]=j; // chid at index j for provider at index i
|
---|
[1824] | 521 | reader_chk_cmd(sc_GetChanelIds, 0);
|
---|
[2414] | 522 | // if (cta_lr<61) break; // why 61 (0 to 60 in steps of 6 .. is it 10*6 from the 10 in the for loop ?
|
---|
| 523 | // what happen if the card only send back.. 9 chids (or less)... we don't see them
|
---|
[2418] | 524 | // so we should check whether or not we have at least 6 bytes (1 chid).
|
---|
[2414] | 525 | if (cta_lr<6) break;
|
---|
| 526 |
|
---|
[1824] | 527 | for(k=0; k<cta_lr; k+=6)
|
---|
| 528 | {
|
---|
| 529 | chid=b2i(2, cta_res+k);
|
---|
| 530 | if (chid && chid!=0xFFFF)
|
---|
| 531 | {
|
---|
| 532 | time_t date;
|
---|
[2814] | 533 | chid_date(reader,date=b2i(2, cta_res+k+2), t, 16);
|
---|
| 534 | chid_date(reader,date+cta_res[k+4], t+16, 16);
|
---|
[1824] | 535 | if (first)
|
---|
| 536 | {
|
---|
[2440] | 537 | cs_ri_log(reader, "entitlements for provider: %d, id: %06X", p, b2i(3, &reader->prid[i][1]));
|
---|
[1824] | 538 | first=0;
|
---|
| 539 | }
|
---|
[1926] | 540 | cs_ri_log(reader, "chid: %04X, date: %s - %s", chid, t, t+16);
|
---|
[1824] | 541 | }
|
---|
| 542 | }
|
---|
[2398] | 543 | j++;
|
---|
[1824] | 544 | }
|
---|
| 545 | }
|
---|
| 546 | }
|
---|
| 547 | }
|
---|
[1940] | 548 | cs_log("[irdeto-reader] ready for requests");
|
---|
[1824] | 549 | return OK;
|
---|
[1507] | 550 | }
|
---|
[3168] | 551 |
|
---|
| 552 | void reader_irdeto(struct s_cardsystem *ph)
|
---|
| 553 | {
|
---|
| 554 | ph->do_emm=irdeto_do_emm;
|
---|
| 555 | ph->do_ecm=irdeto_do_ecm;
|
---|
| 556 | ph->card_info=irdeto_card_info;
|
---|
| 557 | ph->card_init=irdeto_card_init;
|
---|
| 558 | ph->get_emm_type=irdeto_get_emm_type;
|
---|
| 559 | ph->get_emm_filter=irdeto_get_emm_filter;
|
---|
| 560 | ph->caids[0]=0x06;
|
---|
| 561 | ph->caids[1]=0x17;
|
---|
| 562 | }
|
---|