1 | #include "globals.h"
|
---|
2 | #include "reader-common.h"
|
---|
3 | #include "reader-videoguard-common.h"
|
---|
4 |
|
---|
5 |
|
---|
6 | static void vg2_read_tiers(struct s_reader * reader)
|
---|
7 | {
|
---|
8 | def_resp;
|
---|
9 | int l;
|
---|
10 |
|
---|
11 | static const unsigned char ins2a[5] = { 0xd0,0x2a,0x00,0x00,0x00 };
|
---|
12 | if(cmd_exists(ins2a)) { //ins2a is not needed and causes an error on some cards eg Sky Italy 09CD
|
---|
13 | l=do_cmd(reader, ins2a,NULL,NULL,cta_res);
|
---|
14 | if(l<0 || !status_ok(cta_res+l)){
|
---|
15 | cs_log ("[videoguard2-reader] cmd ins2a failed");
|
---|
16 | return;
|
---|
17 | }
|
---|
18 | }
|
---|
19 |
|
---|
20 | static unsigned char ins76[5] = { 0xd0,0x76,0x00,0x00,0x00 };
|
---|
21 | ins76[3]=0x7f; ins76[4]=2;
|
---|
22 | if(!write_cmd_vg(ins76,NULL) || !status_ok(cta_res+2)) return;
|
---|
23 | ins76[3]=0; ins76[4]=0;
|
---|
24 | int num=cta_res[1];
|
---|
25 | int i;
|
---|
26 | #ifdef CS_RDR_INIT_HIST
|
---|
27 | reader->init_history_pos = 0; //reset for re-read
|
---|
28 | memset(reader->init_history, 0, sizeof(reader->init_history));
|
---|
29 | #endif
|
---|
30 | for(i=0; i<num; i++) {
|
---|
31 | ins76[2]=i;
|
---|
32 | l=do_cmd(reader, ins76,NULL,NULL,cta_res);
|
---|
33 | if(l<0 || !status_ok(cta_res+l)) return;
|
---|
34 | if(cta_res[2]==0 && cta_res[3]==0) break;
|
---|
35 | int y,m,d,H,M,S;
|
---|
36 | rev_date_calc(&cta_res[4],&y,&m,&d,&H,&M,&S,VG_BASEYEAR);
|
---|
37 | unsigned short tier_id = (cta_res[2] << 8) | cta_res[3];
|
---|
38 | char *tier_name = get_tiername(tier_id, reader->caid[0]);
|
---|
39 | cs_ri_log(reader, "[videoguard2-reader] tier: %04x, expiry date: %04d/%02d/%02d-%02d:%02d:%02d %s",tier_id,y,m,d,H,M,S,tier_name);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | int videoguard2_card_init(struct s_reader * reader, ATR newatr)
|
---|
44 | {
|
---|
45 |
|
---|
46 | get_hist;
|
---|
47 | if ((hist_size < 7) || (hist[1] != 0xB0) || (hist[4] != 0xFF) || (hist[5] != 0x4A) || (hist[6] != 0x50))
|
---|
48 | return ERROR;
|
---|
49 |
|
---|
50 | get_atr;
|
---|
51 | def_resp;
|
---|
52 |
|
---|
53 | /* get information on the card from reader-videoguard-common.c */
|
---|
54 | NDS_ATR_ENTRY nds_atr_entry = {{0},0,0,0,0};
|
---|
55 | memcpy(nds_atr_entry.atr,atr,atr_size);
|
---|
56 | nds_atr_entry.atr_len = atr_size;
|
---|
57 |
|
---|
58 | getNdsAtrEntry(&nds_atr_entry);
|
---|
59 |
|
---|
60 | if((reader->ndsversion != NDS2) &&
|
---|
61 | (((nds_atr_entry.nds_version != NDS2) && (nds_atr_entry.nds_version != NDSUNKNOWN)) ||
|
---|
62 | (reader->ndsversion != NDSAUTO))) {
|
---|
63 | /* known ATR and not NDS2
|
---|
64 | or known NDS2 ATR and forced to another NDS version */
|
---|
65 | return ERROR;
|
---|
66 | }
|
---|
67 |
|
---|
68 | if(reader->ndsversion == NDS2){
|
---|
69 | cs_ri_log(reader, "[videoguard2-reader] Forced to NDS2");
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (nds_atr_entry.desc){
|
---|
73 | VG_BASEYEAR=nds_atr_entry.base_year;
|
---|
74 | cs_ri_log(reader, "[videoguard2-reader] type: %s", nds_atr_entry.desc);
|
---|
75 | }
|
---|
76 |
|
---|
77 | //a non videoguard2/NDS2 card will fail on read_cmd_len(ins7401)
|
---|
78 | //this way unknown videoguard2/NDS2 cards will also pass this check
|
---|
79 |
|
---|
80 | unsigned char ins7401[5] = { 0xD0,0x74,0x01,0x00,0x00 };
|
---|
81 | int l;
|
---|
82 | ins7401[3]=0x80; // from newcs log
|
---|
83 | ins7401[4]=0x01;
|
---|
84 | if((l=read_cmd_len(reader, ins7401))<0) return ERROR; //not a videoguard2/NDS card or communication error
|
---|
85 | ins7401[3]=0x00;
|
---|
86 | ins7401[4]=l;
|
---|
87 | if(!write_cmd_vg(ins7401,NULL) || !status_ok(cta_res+l)) {
|
---|
88 | cs_log ("[videoguard2-reader] failed to read cmd list");
|
---|
89 | return ERROR;
|
---|
90 | }
|
---|
91 |
|
---|
92 | memorize_cmd_table (cta_res,l);
|
---|
93 |
|
---|
94 | unsigned char buff[256];
|
---|
95 |
|
---|
96 | unsigned char ins7416[5] = { 0xD0,0x74,0x16,0x00,0x00 };
|
---|
97 | if(do_cmd(reader, ins7416, NULL, NULL,cta_res)<0) {
|
---|
98 | cs_log ("[videoguard2-reader] cmd 7416 failed");
|
---|
99 | return ERROR;
|
---|
100 | }
|
---|
101 |
|
---|
102 | unsigned char ins36[5] = { 0xD0,0x36,0x00,0x00,0x00 };
|
---|
103 | unsigned char boxID [4];
|
---|
104 |
|
---|
105 | if (reader->boxid > 0) {
|
---|
106 | /* the boxid is specified in the config */
|
---|
107 | int i;
|
---|
108 | for (i=0; i < 4; i++) {
|
---|
109 | boxID[i] = (reader->boxid >> (8 * (3 - i))) % 0x100;
|
---|
110 | }
|
---|
111 | } else {
|
---|
112 | /* we can try to get the boxid from the card */
|
---|
113 | int boxidOK=0;
|
---|
114 | l=do_cmd(reader, ins36, NULL, buff,cta_res);
|
---|
115 | if(l<13)
|
---|
116 | cs_log("[videoguard2-reader] ins36: too short answer");
|
---|
117 | else if (buff[7] > 0x0F)
|
---|
118 | cs_log("[videoguard2-reader] ins36: encrypted - can't parse");
|
---|
119 | else {
|
---|
120 | /* skipping the initial fixed fields: cmdecho (4) + length (1) + encr/rev++ (4) */
|
---|
121 | int i=9;
|
---|
122 | int gotUA=0;
|
---|
123 | while (i<l) {
|
---|
124 | if (!gotUA && buff[i]<0xF0) { /* then we guess that the next 4 bytes is the UA */
|
---|
125 | gotUA=1;
|
---|
126 | i+=4;
|
---|
127 | } else switch (buff[i]) { /* object length vary depending on type */
|
---|
128 | case 0x00: /* padding */
|
---|
129 | i+=1;
|
---|
130 | break;
|
---|
131 | case 0xEF: /* card status */
|
---|
132 | i+=3;
|
---|
133 | break;
|
---|
134 | case 0xD1:
|
---|
135 | i+=4;
|
---|
136 | break;
|
---|
137 | case 0xDF: /* next server contact */
|
---|
138 | i+=5;
|
---|
139 | break;
|
---|
140 | case 0xF3: /* boxID */
|
---|
141 | memcpy(&boxID,&buff[i+1],sizeof(boxID));
|
---|
142 | boxidOK=1;
|
---|
143 | i+=5;
|
---|
144 | break;
|
---|
145 | case 0xF6:
|
---|
146 | i+=6;
|
---|
147 | break;
|
---|
148 | case 0x01: /* date & time */
|
---|
149 | i+=7;
|
---|
150 | break;
|
---|
151 | case 0xFA:
|
---|
152 | i+=9;
|
---|
153 | break;
|
---|
154 | case 0x5E:
|
---|
155 | case 0x67: /* signature */
|
---|
156 | case 0xDE:
|
---|
157 | case 0xE2:
|
---|
158 | case 0xE9: /* tier dates */
|
---|
159 | case 0xF8: /* Old PPV Event Record */
|
---|
160 | case 0xFD:
|
---|
161 | i+=buff[i+1]+2; /* skip length + 2 bytes (type and length) */
|
---|
162 | break;
|
---|
163 | default: /* default to assume a length byte */
|
---|
164 | cs_log("[videoguard2-reader] ins36 returned unknown type=0x%02X - parsing may fail", buff[i]);
|
---|
165 | i+=buff[i+1]+2;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | if(!boxidOK) {
|
---|
171 | cs_log ("[videoguard2-reader] no boxID available");
|
---|
172 | return ERROR;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | unsigned char ins4C[5] = { 0xD0,0x4C,0x00,0x00,0x09 };
|
---|
177 | unsigned char payload4C[9] = { 0,0,0,0, 3,0,0,0,4 };
|
---|
178 | memcpy(payload4C,boxID,4);
|
---|
179 | if(!write_cmd_vg(ins4C,payload4C) || !status_ok(cta_res+l)) {
|
---|
180 | cs_log("[videoguard2-reader] sending boxid failed");
|
---|
181 | return ERROR;
|
---|
182 | }
|
---|
183 | // Start of suggested fix for 09ac cards
|
---|
184 | unsigned char Dimeno_Magic[0x10]={0xF9,0xFB,0xCD,0x5A,0x76,0xB5,0xC4,0x5C,0xC8,0x2E,0x1D,0xE1,0xCC,0x5B,0x6B,0x02};
|
---|
185 | int a;
|
---|
186 | for(a=0; a<4; a++)
|
---|
187 | Dimeno_Magic[a]=Dimeno_Magic[a]^boxID[a];
|
---|
188 | //I supposed to declare a AES_KEY Astro_Key somewhere before...
|
---|
189 | AES_set_decrypt_key(Dimeno_Magic,128,&Astro_Key);
|
---|
190 | Astro_Key.rounds=10;
|
---|
191 | //Important for ecm decryption...
|
---|
192 | // End of suggested fix
|
---|
193 |
|
---|
194 | //short int SWIRDstatus = cta_res[1];
|
---|
195 | unsigned char ins58[5] = { 0xD0,0x58,0x00,0x00,0x00 };
|
---|
196 | l=do_cmd(reader, ins58, NULL, buff,cta_res);
|
---|
197 | if(l<0) {
|
---|
198 | cs_log("[videoguard2-reader] cmd ins58 failed");
|
---|
199 | return ERROR;
|
---|
200 | }
|
---|
201 | memset(reader->hexserial, 0, 8);
|
---|
202 | memcpy(reader->hexserial+2, cta_res+3, 4);
|
---|
203 | memcpy(reader->sa, cta_res+3, 3);
|
---|
204 | reader->caid[0] = cta_res[24]*0x100+cta_res[25];
|
---|
205 |
|
---|
206 | /* we have one provider, 0x0000 */
|
---|
207 | reader->nprov = 1;
|
---|
208 | memset(reader->prid, 0x00, sizeof(reader->prid));
|
---|
209 |
|
---|
210 | /*
|
---|
211 | cs_log ("[videoguard2-reader] INS58 : Fuse byte=0x%02X, IRDStatus=0x%02X", cta_res[2],SWIRDstatus);
|
---|
212 | if (SWIRDstatus==4) {
|
---|
213 | // If swMarriage=4, not married then exchange for BC Key
|
---|
214 | cs_log ("[videoguard2-reader] Card not married, exchange for BC Keys");
|
---|
215 | */
|
---|
216 |
|
---|
217 | static unsigned char seed1[] = {
|
---|
218 | 0xb9, 0xd5, 0xef, 0xd5, 0xf5, 0xd5, 0xfb, 0xd5, 0x31, 0xd6, 0x43, 0xd6, 0x55, 0xd6, 0x61, 0xd6,
|
---|
219 | 0x85, 0xd6, 0x9d, 0xd6, 0xaf, 0xd6, 0xc7, 0xd6, 0xd9, 0xd6, 0x09, 0xd7, 0x15, 0xd7, 0x21, 0xd7,
|
---|
220 | 0x27, 0xd7, 0x3f, 0xd7, 0x45, 0xd7, 0xb1, 0xd7, 0xbd, 0xd7, 0xdb, 0xd7, 0x11, 0xd8, 0x23, 0xd8,
|
---|
221 | 0x29, 0xd8, 0x2f, 0xd8, 0x4d, 0xd8, 0x8f, 0xd8, 0xa1, 0xd8, 0xad, 0xd8, 0xbf, 0xd8, 0xd7, 0xd8
|
---|
222 | };
|
---|
223 | static unsigned char seed2[] = {
|
---|
224 | 0x01, 0x00, 0xcf, 0x13, 0xe0, 0x60, 0x54, 0xac, 0xab, 0x99, 0xe6, 0x0c, 0x9f, 0x5b, 0x91, 0xb9,
|
---|
225 | 0x72, 0x72, 0x4d, 0x5b, 0x5f, 0xd3, 0xb7, 0x5b, 0x01, 0x4d, 0xef, 0x9e, 0x6b, 0x8a, 0xb9, 0xd1,
|
---|
226 | 0xc9, 0x9f, 0xa1, 0x2a, 0x8d, 0x86, 0xb6, 0xd6, 0x39, 0xb4, 0x64, 0x65, 0x13, 0x77, 0xa1, 0x0a,
|
---|
227 | 0x0c, 0xcf, 0xb4, 0x2b, 0x3a, 0x2f, 0xd2, 0x09, 0x92, 0x15, 0x40, 0x47, 0x66, 0x5c, 0xda, 0xc9
|
---|
228 | };
|
---|
229 | cCamCryptVG_SetSeed(seed1,seed2);
|
---|
230 |
|
---|
231 | unsigned char insB4[5] = { 0xD0,0xB4,0x00,0x00,0x40 };
|
---|
232 | unsigned char tbuff[64];
|
---|
233 | cCamCryptVG_GetCamKey(tbuff);
|
---|
234 | l=do_cmd(reader, insB4, tbuff, NULL,cta_res);
|
---|
235 | if(l<0 || !status_ok(cta_res)) {
|
---|
236 | cs_log ("[videoguard2-reader] cmd D0B4 failed (%02X%02X)", cta_res[0], cta_res[1]);
|
---|
237 | return ERROR;
|
---|
238 | }
|
---|
239 |
|
---|
240 | unsigned char insBC[5] = { 0xD0,0xBC,0x00,0x00,0x00 };
|
---|
241 | l=do_cmd(reader, insBC, NULL, NULL,cta_res);
|
---|
242 | if(l<0) {
|
---|
243 | cs_log("[videoguard2-reader] cmd D0BC failed");
|
---|
244 | return ERROR;
|
---|
245 | }
|
---|
246 |
|
---|
247 | unsigned char insBE[5] = { 0xD3,0xBE,0x00,0x00,0x00 };
|
---|
248 | l=do_cmd(reader, insBE, NULL, NULL,cta_res);
|
---|
249 | if(l<0) {
|
---|
250 | cs_log("[videoguard2-reader] cmd D3BE failed");
|
---|
251 | return ERROR;
|
---|
252 | }
|
---|
253 |
|
---|
254 | unsigned char ins58a[5] = { 0xD1,0x58,0x00,0x00,0x00 };
|
---|
255 | l=do_cmd(reader, ins58a, NULL, NULL,cta_res);
|
---|
256 | if(l<0) {
|
---|
257 | cs_log("[videoguard2-reader] cmd D158 failed");
|
---|
258 | return ERROR;
|
---|
259 | }
|
---|
260 |
|
---|
261 | unsigned char ins4Ca[5] = { 0xD1,0x4C,0x00,0x00,0x00 };
|
---|
262 | l=do_cmd(reader, ins4Ca,payload4C, NULL,cta_res);
|
---|
263 | if(l<0 || !status_ok(cta_res)) {
|
---|
264 | cs_log("[videoguard2-reader] cmd D14Ca failed");
|
---|
265 | return ERROR;
|
---|
266 | }
|
---|
267 |
|
---|
268 | cs_ri_log(reader, "[videoguard2-reader] type: VideoGuard, caid: %04X, serial: %02X%02X%02X%02X, BoxID: %02X%02X%02X%02X",
|
---|
269 | reader->caid[0],
|
---|
270 | reader->hexserial[2],reader->hexserial[3],reader->hexserial[4],reader->hexserial[5],
|
---|
271 | boxID[0],boxID[1],boxID[2],boxID[3]);
|
---|
272 |
|
---|
273 | cs_log("[videoguard2-reader] ready for requests");
|
---|
274 |
|
---|
275 | return OK;
|
---|
276 | }
|
---|
277 |
|
---|
278 | int videoguard2_do_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
279 | {
|
---|
280 | unsigned char cta_res[CTA_RES_LEN];
|
---|
281 | static unsigned char ins40[5] = { 0xD1,0x40,0x00,0x80,0xFF };
|
---|
282 | static const unsigned char ins54[5] = { 0xD3,0x54,0x00,0x00,0x00};
|
---|
283 | int posECMpart2=er->ecm[6]+7;
|
---|
284 | int lenECMpart2=er->ecm[posECMpart2]+1;
|
---|
285 | unsigned char tbuff[264];
|
---|
286 | tbuff[0]=0;
|
---|
287 | memcpy(&tbuff[1],&(er->ecm[posECMpart2+1]),lenECMpart2-1);
|
---|
288 | ins40[4]=lenECMpart2;
|
---|
289 | int l;
|
---|
290 | l = do_cmd(reader, ins40,tbuff,NULL,cta_res);
|
---|
291 | if(l>0 && status_ok(cta_res)) {
|
---|
292 | l = do_cmd(reader, ins54,NULL,NULL,cta_res);
|
---|
293 | if(l>0 && status_ok(cta_res+l)) {
|
---|
294 | if (!cw_is_valid(CW1)) //sky cards report 90 00 = ok but send cw = 00 when channel not subscribed
|
---|
295 | {
|
---|
296 | snprintf( er->msglog, MSGLOGSIZE, "9000 but cw=00 -> channel not subscribed " );
|
---|
297 | return ERROR;
|
---|
298 | }
|
---|
299 |
|
---|
300 | if(er->ecm[0]&1) {
|
---|
301 | memcpy(er->cw+8,CW1,8);
|
---|
302 | memcpy(er->cw+0,CW2,8);
|
---|
303 | }
|
---|
304 | else {
|
---|
305 | memcpy(er->cw+0,CW1,8);
|
---|
306 | memcpy(er->cw+8,CW2,8);
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | //test for postprocessing marker
|
---|
311 | int posB0 = -1;
|
---|
312 | int i;
|
---|
313 | for (i = 6; i < posECMpart2; i++)
|
---|
314 | {
|
---|
315 | if (er->ecm[i-3] == 0x80 && er->ecm[i] == 0xB0 && ((er->ecm[i+1] == 0x01) ||(er->ecm[i+1] == 0x02)||(er->ecm[i+1] == 0x03) ) ) {
|
---|
316 | posB0 = i;
|
---|
317 | break;
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | if (posB0 != -1) {
|
---|
322 | do_post_dw_hash( er->cw+0, &er->ecm[posB0-2]);
|
---|
323 | do_post_dw_hash( er->cw+8, &er->ecm[posB0-2]);
|
---|
324 | }
|
---|
325 |
|
---|
326 | return OK;
|
---|
327 | }
|
---|
328 | }
|
---|
329 | snprintf( er->msglog, MSGLOGSIZE, "(%d) status not ok %02x %02x",l, cta_res[0],cta_res[1] );
|
---|
330 | return ERROR;
|
---|
331 | }
|
---|
332 |
|
---|
333 | static int num_addr(const unsigned char *data)
|
---|
334 | {
|
---|
335 | return ((data[3]&0x30)>>4)+1;
|
---|
336 | }
|
---|
337 | /*
|
---|
338 | Example of GLOBAL EMM's
|
---|
339 | This one has IRD-EMM + Card-EMM
|
---|
340 | 82 70 20 00 02 06 02 7D 0E 89 53 71 16 90 14 40
|
---|
341 | 01 ED 17 7D 9E 1F 28 CF 09 97 54 F1 8E 72 06 E7
|
---|
342 | 51 AF F5
|
---|
343 | This one has only IRD-EMM
|
---|
344 | 82 70 6D 00 07 69 01 30 07 14 5E 0F FF FF 00 06
|
---|
345 | 00 0D 01 00 03 01 00 00 00 0F 00 00 00 5E 01 00
|
---|
346 | 01 0C 2E 70 E4 55 B6 D2 34 F7 44 86 9E 5C 91 14
|
---|
347 | 81 FC DF CB D0 86 65 77 DF A9 E1 6B A8 9F 9B DE
|
---|
348 | 90 92 B9 AA 6C B3 4E 87 D2 EC 92 DA FC 71 EF 27
|
---|
349 | B3 C3 D0 17 CF 0B D6 5E 8C DB EB B3 37 55 6E 09
|
---|
350 | 7F 27 3C F1 85 29 C9 4E 0B EE DF 68 BE 00 C9 00
|
---|
351 | */
|
---|
352 | static const unsigned char *payload_addr(uchar emmtype, const unsigned char *data, const unsigned char *a)
|
---|
353 | {
|
---|
354 | int s;
|
---|
355 | int l;
|
---|
356 | const unsigned char *ptr = NULL;
|
---|
357 | int position=-1;
|
---|
358 | int numAddrs=0;
|
---|
359 |
|
---|
360 | switch(emmtype) {
|
---|
361 | case SHARED: s=3; break;
|
---|
362 | case UNIQUE: s=4; break;
|
---|
363 | default: s=0;
|
---|
364 | }
|
---|
365 |
|
---|
366 | numAddrs=num_addr(data);
|
---|
367 |
|
---|
368 | if(s>0) {
|
---|
369 | for(l=0;l<numAddrs;l++) {
|
---|
370 | if(!memcmp(&data[l*4+4],a+2,s)) {
|
---|
371 | position=l;
|
---|
372 | break;
|
---|
373 | }
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | int num_filter = (position == -1) ? 0 : numAddrs;
|
---|
378 |
|
---|
379 | /* skip header and the filter list */
|
---|
380 | ptr = data+4+4*num_filter;
|
---|
381 |
|
---|
382 | if (*ptr != 0x02 && *ptr != 0x07) // some clients omit 00 00 separator */
|
---|
383 | {
|
---|
384 | ptr += 2; // skip 00 00 separator
|
---|
385 | if (*ptr == 0x00) ptr++; // skip optional 00
|
---|
386 | ptr++; // skip the 1st bitmap len
|
---|
387 | }
|
---|
388 |
|
---|
389 | /* check for IRD-EMM */
|
---|
390 | if (*ptr != 0x02 && *ptr != 0x07) return NULL;
|
---|
391 |
|
---|
392 | /* skip IRD-EMM part, 02 00 or 02 06 xx aabbccdd yy */
|
---|
393 | ptr += 2 + ptr[1];
|
---|
394 |
|
---|
395 | /* check for EMM boundaries - ptr should not exceed EMM length */
|
---|
396 | if ((int)(ptr - (data + 3)) >= data[2]) return NULL;
|
---|
397 |
|
---|
398 | for(l=0;l<position;l++) {
|
---|
399 | /* skip the payload of the previous sub-EMM */
|
---|
400 | ptr += 1 + ptr [0];
|
---|
401 |
|
---|
402 | /* check for EMM boundaries - ptr should not exceed EMM length */
|
---|
403 | if ((int)(ptr - (data + 3)) >= data[2]) return NULL;
|
---|
404 |
|
---|
405 | /* skip optional 00 */
|
---|
406 | if (*ptr == 0x00) ptr++;
|
---|
407 |
|
---|
408 | /* skip the bitmap len */
|
---|
409 | ptr++;
|
---|
410 |
|
---|
411 | /* check for IRD-EMM */
|
---|
412 | if (*ptr != 0x02 && *ptr != 0x07) return NULL;
|
---|
413 |
|
---|
414 | /* skip IRD-EMM part, 02 00 or 02 06 xx aabbccdd yy */
|
---|
415 | ptr += 2 + ptr[1];
|
---|
416 | }
|
---|
417 |
|
---|
418 | return ptr;
|
---|
419 | }
|
---|
420 |
|
---|
421 | int videoguard2_get_emm_type(EMM_PACKET *ep, struct s_reader * rdr)
|
---|
422 | {
|
---|
423 |
|
---|
424 | /*
|
---|
425 | 82 30 ad 70 00 XX XX XX 00 XX XX XX 00 XX XX XX 00 XX XX XX 00 00
|
---|
426 | d3 02 00 22 90 20 44 02 4a 50 1d 88 ab 02 ac 79 16 6c df a1 b1 b7 77 00 ba eb 63 b5 c9 a9 30 2b 43 e9 16 a9 d5 14 00
|
---|
427 | d3 02 00 22 90 20 44 02 13 e3 40 bd 29 e4 90 97 c3 aa 93 db 8d f5 6b e4 92 dd 00 9b 51 03 c9 3d d0 e2 37 44 d3 bf 00
|
---|
428 | d3 02 00 22 90 20 44 02 97 79 5d 18 96 5f 3a 67 70 55 bb b9 d2 49 31 bd 18 17 2a e9 6f eb d8 76 ec c3 c9 cc 53 39 00
|
---|
429 | d2 02 00 21 90 1f 44 02 99 6d df 36 54 9c 7c 78 1b 21 54 d9 d4 9f c1 80 3c 46 10 76 aa 75 ef d6 82 27 2e 44 7b 00
|
---|
430 | */
|
---|
431 |
|
---|
432 | int i, pos;
|
---|
433 | int serial_count = ((ep->emm[3] >> 4) & 3) + 1;
|
---|
434 | int serial_len = (ep->emm[3] & 0x80) ? 3 : 4;
|
---|
435 | uchar emmtype = (ep->emm[3] & VG_EMMTYPE_MASK) >> 6;
|
---|
436 |
|
---|
437 | pos = 4 + (serial_len * serial_count) + 2;
|
---|
438 |
|
---|
439 | switch(emmtype) {
|
---|
440 | case VG_EMMTYPE_G:
|
---|
441 | ep->type=GLOBAL;
|
---|
442 | cs_debug_mask(D_EMM, "VIDEOGUARD2 EMM: GLOBAL");
|
---|
443 | return TRUE;
|
---|
444 |
|
---|
445 | case VG_EMMTYPE_U:
|
---|
446 | cs_debug_mask(D_EMM, "VIDEOGUARD2 EMM: UNIQUE");
|
---|
447 | ep->type=UNIQUE;
|
---|
448 | if (ep->emm[1] == 0) // detected UNIQUE EMM from cccam (there is no serial)
|
---|
449 | return TRUE;
|
---|
450 |
|
---|
451 | for (i = 1;i <= serial_count;i++) {
|
---|
452 | if (!memcmp (rdr->hexserial + 2, ep->emm + (serial_len * i), serial_len)) {
|
---|
453 | memcpy(ep->hexserial, ep->emm + (serial_len * i), serial_len);
|
---|
454 | return TRUE;
|
---|
455 | }
|
---|
456 |
|
---|
457 | pos = pos + ep->emm[pos+5] + 5;
|
---|
458 | }
|
---|
459 | return FALSE; // if UNIQUE but no serial match return FALSE
|
---|
460 |
|
---|
461 | case VG_EMMTYPE_S:
|
---|
462 | ep->type=SHARED;
|
---|
463 | cs_debug_mask(D_EMM, "VIDEOGUARD2 EMM: SHARED");
|
---|
464 | return TRUE; // FIXME: no check for SA
|
---|
465 |
|
---|
466 | default:
|
---|
467 | if (ep->emm[pos-2] != 0x00 && ep->emm[pos-1] != 0x00 && ep->emm[pos-1] != 0x01) {
|
---|
468 | //remote emm without serial
|
---|
469 | ep->type=UNKNOWN;
|
---|
470 | return TRUE;
|
---|
471 | }
|
---|
472 | return FALSE;
|
---|
473 | }
|
---|
474 | }
|
---|
475 |
|
---|
476 | void videoguard2_get_emm_filter(struct s_reader * rdr, uchar *filter)
|
---|
477 | {
|
---|
478 | filter[0]=0xFF;
|
---|
479 | filter[1]=3;
|
---|
480 |
|
---|
481 | //ToDo videoguard2_get_emm_filter basic construction
|
---|
482 |
|
---|
483 | filter[2]=UNIQUE;
|
---|
484 | filter[3]=0;
|
---|
485 |
|
---|
486 | filter[4+0] = 0x82;
|
---|
487 | filter[4+0+16] = 0xFF;
|
---|
488 |
|
---|
489 | memcpy(filter+4+2, rdr->hexserial+2, 4);
|
---|
490 | memset(filter+4+2+16, 0xFF, 4);
|
---|
491 |
|
---|
492 | filter[36]=UNIQUE;
|
---|
493 | filter[37]=0;
|
---|
494 |
|
---|
495 | filter[38+0] = 0x82;
|
---|
496 | filter[38+0+16] = 0xFF;
|
---|
497 |
|
---|
498 | memcpy(filter+38+6, rdr->hexserial+2, 4);
|
---|
499 | memset(filter+38+6+16, 0xFF, 4);
|
---|
500 |
|
---|
501 | filter[70]=UNIQUE;
|
---|
502 | filter[71]=0;
|
---|
503 |
|
---|
504 | filter[72+0] = 0x82;
|
---|
505 | filter[72+0+16] = 0xFF;
|
---|
506 |
|
---|
507 | memcpy(filter+72+10, rdr->hexserial+2, 4);
|
---|
508 | memset(filter+72+10+16, 0xFF, 4);
|
---|
509 |
|
---|
510 | /* filter[104]=UNIQUE;
|
---|
511 | filter[105]=0;
|
---|
512 |
|
---|
513 | filter[106+0] = 0x82;
|
---|
514 | filter[106+0+16] = 0xFF;
|
---|
515 |
|
---|
516 | memcpy(filter+106+14, rdr->hexserial+2, 2);
|
---|
517 | memset(filter+106+14+16, 0xFF, 2); */
|
---|
518 |
|
---|
519 | return;
|
---|
520 | }
|
---|
521 |
|
---|
522 | int videoguard2_do_emm(struct s_reader * reader, EMM_PACKET *ep)
|
---|
523 | {
|
---|
524 | unsigned char cta_res[CTA_RES_LEN];
|
---|
525 | unsigned char ins42[5] = { 0xD1,0x42,0x00,0x00,0xFF };
|
---|
526 | int rc=ERROR;
|
---|
527 |
|
---|
528 | const unsigned char *payload = payload_addr(ep->type, ep->emm, reader->hexserial);
|
---|
529 | while (payload) {
|
---|
530 | ins42[4]=*payload;
|
---|
531 | int l = do_cmd(reader, ins42,payload+1,NULL,cta_res);
|
---|
532 | if(l>0 && status_ok(cta_res)) {
|
---|
533 | rc=OK;
|
---|
534 | }
|
---|
535 |
|
---|
536 | cs_debug_mask(D_EMM, "[videoguard2-reader] EMM request return code : %02X%02X", cta_res[0], cta_res[1]);
|
---|
537 | //cs_dump(ep->emm, 64, "EMM:");
|
---|
538 | if (status_ok (cta_res) && (cta_res[1] & 0x01)) {
|
---|
539 | vg2_read_tiers(reader);
|
---|
540 | }
|
---|
541 |
|
---|
542 | if (num_addr(ep->emm) == 1 && (int)(&payload[1] - &ep->emm[0]) + *payload + 1 < ep->l) {
|
---|
543 | payload += *payload + 1;
|
---|
544 | if (*payload == 0x00) ++payload;
|
---|
545 | ++payload;
|
---|
546 | if (*payload != 0x02) break;
|
---|
547 | payload += 2 + payload[1];
|
---|
548 | }
|
---|
549 | else
|
---|
550 | payload = 0;
|
---|
551 |
|
---|
552 | }
|
---|
553 |
|
---|
554 | return(rc);
|
---|
555 | }
|
---|
556 |
|
---|
557 | int videoguard2_card_info(struct s_reader * reader)
|
---|
558 | {
|
---|
559 | /* info is displayed in init, or when processing info */
|
---|
560 | cs_log("[videoguard2-reader] card detected");
|
---|
561 | cs_log("[videoguard2-reader] type: VideoGuard" );
|
---|
562 | vg2_read_tiers (reader);
|
---|
563 | return OK;
|
---|
564 | }
|
---|