1 | #include "globals.h"
|
---|
2 | #include "reader-common.h"
|
---|
3 |
|
---|
4 | struct via_date {
|
---|
5 | ushort day_s : 5;
|
---|
6 | ushort month_s : 4;
|
---|
7 | ushort year_s : 7;
|
---|
8 |
|
---|
9 | ushort day_e : 5;
|
---|
10 | ushort month_e : 4;
|
---|
11 | ushort year_e : 7;
|
---|
12 | };
|
---|
13 |
|
---|
14 | static void parse_via_date(const uchar *buf, struct via_date *vd, int fend)
|
---|
15 | {
|
---|
16 | ushort date;
|
---|
17 |
|
---|
18 | date = (buf[0]<<8) | buf[1];
|
---|
19 | vd->day_s = date & 0x1f;
|
---|
20 | vd->month_s = (date>>5) & 0x0f;
|
---|
21 | vd->year_s = (date>>9) & 0x7f;
|
---|
22 |
|
---|
23 | if( fend )
|
---|
24 | {
|
---|
25 | date = (buf[2]<<8) | buf[3];
|
---|
26 | vd->day_e = date & 0x1f;
|
---|
27 | vd->month_e = (date>>5) & 0x0f;
|
---|
28 | vd->year_e = (date>>9) & 0x7f;
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | static void show_class(struct s_reader * reader, const char *p, const uchar *b, int l)
|
---|
33 | {
|
---|
34 | int i, j;
|
---|
35 |
|
---|
36 | // b -> via date (4 bytes)
|
---|
37 | b+=4;
|
---|
38 | l-=4;
|
---|
39 |
|
---|
40 | j=l-1;
|
---|
41 | for (; j>=0; j--)
|
---|
42 | for (i=0; i<8; i++)
|
---|
43 | if (b[j] & (1 << (i&7)))
|
---|
44 | {
|
---|
45 | uchar cls;
|
---|
46 | struct via_date vd;
|
---|
47 | parse_via_date(b-4, &vd, 1);
|
---|
48 | cls=(l-(j+1))*8+i;
|
---|
49 | if (p)
|
---|
50 | cs_log("%sclass: %02X, expiry date: %04d/%02d/%02d - %04d/%02d/%02d", p, cls,
|
---|
51 | vd.year_s+1980, vd.month_s, vd.day_s,
|
---|
52 | vd.year_e+1980, vd.month_e, vd.day_e);
|
---|
53 | else
|
---|
54 | cs_ri_log(reader, "class: %02X, expiry date: %04d/%02d/%02d - %04d/%02d/%02d", cls,
|
---|
55 | vd.year_s+1980, vd.month_s, vd.day_s,
|
---|
56 | vd.year_e+1980, vd.month_e, vd.day_e);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | static void show_subs(struct s_reader * reader, const uchar *emm)
|
---|
61 | {
|
---|
62 | // emm -> A9, A6, B6
|
---|
63 |
|
---|
64 | switch( emm[0] )
|
---|
65 | {
|
---|
66 | case 0xA9:
|
---|
67 | show_class(reader, "nano A9: ", emm+2, emm[1]);
|
---|
68 | break;
|
---|
69 | /*
|
---|
70 | {
|
---|
71 | int i, j, byts;
|
---|
72 | const uchar *oemm;
|
---|
73 |
|
---|
74 | oemm = emm;
|
---|
75 | byts = emm[1]-4;
|
---|
76 | emm+=6;
|
---|
77 |
|
---|
78 | j=byts-1;
|
---|
79 | for( ; j>=0; j-- )
|
---|
80 | for( i=0; i<8; i++ )
|
---|
81 | if( emm[j] & (1 << (i&7)) )
|
---|
82 | {
|
---|
83 | uchar cls;
|
---|
84 | struct via_date vd;
|
---|
85 | parse_via_date(emm-4, &vd, 1);
|
---|
86 | cls=(byts-(j+1))*8+i;
|
---|
87 | cs_log("%sclass %02X: expiry date: %02d/%02d/%04d - %02d/%02d/%04d",
|
---|
88 | fnano?"nano A9: ":"", cls,
|
---|
89 | vd.day_s, vd.month_s, vd.year_s+1980,
|
---|
90 | vd.day_e, vd.month_e, vd.year_e+1980);
|
---|
91 | }
|
---|
92 | break;
|
---|
93 | }
|
---|
94 | */
|
---|
95 | case 0xA6:
|
---|
96 | {
|
---|
97 | char szGeo[256];
|
---|
98 |
|
---|
99 | memset(szGeo, 0, 256);
|
---|
100 | strncpy(szGeo, (char *)emm+2, emm[1]);
|
---|
101 | cs_log("[viaccess-reader] nano A6: geo %s", szGeo);
|
---|
102 | break;
|
---|
103 | }
|
---|
104 | case 0xB6:
|
---|
105 | {
|
---|
106 | uchar m; // modexp
|
---|
107 | struct via_date vd;
|
---|
108 |
|
---|
109 | m=emm[emm[1]+1];
|
---|
110 | parse_via_date(emm+2, &vd, 0);
|
---|
111 | cs_log("[viaccess-reader] nano B6: modexp %d%d%d%d%d%d: %02d/%02d/%04d", (m&0x20)?1:0,
|
---|
112 | (m&0x10)?1:0,(m&0x08)?1:0,(m&0x04)?1:0,(m&0x02)?1:0,(m&0x01)?1:0,
|
---|
113 | vd.day_s, vd.month_s, vd.year_s+1980);
|
---|
114 | break;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | static int chk_prov(struct s_reader * reader, uchar *id, uchar keynr)
|
---|
120 | {
|
---|
121 | int i, j, rc;
|
---|
122 | for (rc=i=0; (!rc) && (i<reader->nprov); i++)
|
---|
123 | if(!memcmp(&reader->prid[i][1], id, 3))
|
---|
124 | for (j=0; (!rc) && (j<16); j++)
|
---|
125 | if (reader->availkeys[i][j]==keynr)
|
---|
126 | rc=1;
|
---|
127 | return(rc);
|
---|
128 | }
|
---|
129 |
|
---|
130 | static int unlock_parental(struct s_reader * reader)
|
---|
131 | {
|
---|
132 | /* disabling parental lock. assuming pin "0000" if no pin code is provided in the config */
|
---|
133 |
|
---|
134 | static const uchar inDPL[] = {0xca, 0x24, 0x02, 0x00, 0x09};
|
---|
135 | uchar cmDPL[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F};
|
---|
136 | def_resp;
|
---|
137 |
|
---|
138 | if (strcmp(reader->pincode, "none")) {
|
---|
139 | cs_log("[viaccess-reader] Using PIN %s",reader->pincode);
|
---|
140 | // the pin need to be coded in bcd, so we need to convert from ascii to bcd, so '1234' -> 0x12 0x34
|
---|
141 | cmDPL[6]=((reader->pincode[0]-0x30)<<4) | ((reader->pincode[1]-0x30) & 0x0f);
|
---|
142 | cmDPL[7]=((reader->pincode[2]-0x30)<<4) | ((reader->pincode[3]-0x30) & 0x0f);
|
---|
143 | }
|
---|
144 | else {
|
---|
145 | cs_log("[viaccess-reader] Using PIN 0000!");
|
---|
146 | }
|
---|
147 | write_cmd(inDPL,cmDPL);
|
---|
148 | if( !(cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0) ) {
|
---|
149 | if (strcmp(reader->pincode, "none")) {
|
---|
150 | cs_log("[viaccess-reader] Can't disable parental lock. Wrong PIN? OSCam used %s!",reader->pincode);
|
---|
151 | }
|
---|
152 | else {
|
---|
153 | cs_log("[viaccess-reader] Can't disable parental lock. Wrong PIN? OSCam used 0000!");
|
---|
154 | }
|
---|
155 | }
|
---|
156 | else
|
---|
157 | cs_log("[viaccess-reader] Parental lock disabled");
|
---|
158 |
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 | static int viaccess_card_init(struct s_reader * reader, ATR newatr)
|
---|
163 | {
|
---|
164 | get_atr;
|
---|
165 | def_resp;
|
---|
166 | int i;
|
---|
167 | uchar buf[256];
|
---|
168 | uchar insac[] = { 0xca, 0xac, 0x00, 0x00, 0x00 }; // select data
|
---|
169 | uchar insb8[] = { 0xca, 0xb8, 0x00, 0x00, 0x00 }; // read selected data
|
---|
170 | uchar insa4[] = { 0xca, 0xa4, 0x00, 0x00, 0x00 }; // select issuer
|
---|
171 | uchar insc0[] = { 0xca, 0xc0, 0x00, 0x00, 0x00 }; // read data item
|
---|
172 | static const uchar insFAC[] = { 0x87, 0x02, 0x00, 0x00, 0x03 }; // init FAC
|
---|
173 | static const uchar FacDat[] = { 0x00, 0x00, 0x28 };
|
---|
174 | static unsigned char ins8702_data[] = { 0x00, 0x00, 0x11};
|
---|
175 | static unsigned char ins8704[] = { 0x87, 0x04, 0x00, 0x00, 0x07 };
|
---|
176 | static unsigned char ins8706[] = { 0x87, 0x06, 0x00, 0x00, 0x04 };
|
---|
177 |
|
---|
178 |
|
---|
179 | if ((atr[0]!=0x3f) || (atr[1]!=0x77) || ((atr[2]!=0x18) && (atr[2]!=0x11) && (atr[2]!=0x19)) || (atr[9]!=0x68)) return ERROR;
|
---|
180 |
|
---|
181 | write_cmd(insFAC, FacDat);
|
---|
182 | if( !(cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0) )
|
---|
183 | return ERROR;
|
---|
184 |
|
---|
185 | // switch((atr[atrsize-4]<<8)|atr[atrsize-3])
|
---|
186 | // {
|
---|
187 | // case 0x6268: ver="2.3"; break;
|
---|
188 | // case 0x6668: ver="2.4(?)"; break;
|
---|
189 | // case 0xa268:
|
---|
190 | // default: ver="unknown"; break;
|
---|
191 | // }
|
---|
192 |
|
---|
193 | reader->caid[0]=0x500;
|
---|
194 | memset(reader->prid, 0xff, sizeof(reader->prid));
|
---|
195 | insac[2]=0xa4; write_cmd(insac, NULL); // request unique id
|
---|
196 | insb8[4]=0x07; write_cmd(insb8, NULL); // read unique id
|
---|
197 | memcpy(reader->hexserial, cta_res+2, 5);
|
---|
198 | // cs_log("[viaccess-reader] type: Viaccess, ver: %s serial: %llu", ver, b2ll(5, cta_res+2));
|
---|
199 | cs_ri_log(reader, "type: Viaccess (%sstandard atr), caid: %04X, serial: %llu",
|
---|
200 | atr[9]==0x68?"":"non-",reader->caid[0], b2ll(5, cta_res+2));
|
---|
201 |
|
---|
202 | i=0;
|
---|
203 | insa4[2]=0x00; write_cmd(insa4, NULL); // select issuer 0
|
---|
204 | buf[0]=0;
|
---|
205 | while((cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0))
|
---|
206 | {
|
---|
207 | insc0[4]=0x1a; write_cmd(insc0, NULL); // show provider properties
|
---|
208 | cta_res[2]&=0xF0;
|
---|
209 | reader->prid[i][0]=0;
|
---|
210 | memcpy(&reader->prid[i][1], cta_res, 3);
|
---|
211 | memcpy(&reader->availkeys[i][0], cta_res+10, 16);
|
---|
212 | sprintf((char *)buf+strlen((char *)buf), ",%06lX", b2i(3, &reader->prid[i][1]));
|
---|
213 | //cs_log("[viaccess-reader] buf: %s", buf);
|
---|
214 |
|
---|
215 | insac[2]=0xa5; write_cmd(insac, NULL); // request sa
|
---|
216 | insb8[4]=0x06; write_cmd(insb8, NULL); // read sa
|
---|
217 | memcpy(&reader->sa[i][0], cta_res+2, 4);
|
---|
218 |
|
---|
219 | /*
|
---|
220 | insac[2]=0xa7; write_cmd(insac, NULL); // request name
|
---|
221 | insb8[4]=0x02; write_cmd(insb8, NULL); // read name nano + len
|
---|
222 | l=cta_res[1];
|
---|
223 | insb8[4]=l; write_cmd(insb8, NULL); // read name
|
---|
224 | cta_res[l]=0;
|
---|
225 | cs_log("[viaccess-reader] name: %s", cta_res);
|
---|
226 | */
|
---|
227 |
|
---|
228 | insa4[2]=0x02;
|
---|
229 | write_cmd(insa4, NULL); // select next issuer
|
---|
230 | i++;
|
---|
231 | }
|
---|
232 | reader->nprov=i;
|
---|
233 | cs_ri_log(reader, "providers: %d (%s)", reader->nprov, buf+1);
|
---|
234 |
|
---|
235 | /* init the maybe existing aes key */
|
---|
236 | aes_set_key((char *)reader->aes_key);
|
---|
237 |
|
---|
238 | if (cfg->ulparent)
|
---|
239 | unlock_parental(reader);
|
---|
240 |
|
---|
241 | if(card_write(reader, insFAC, ins8702_data, cta_res, &cta_lr) &&
|
---|
242 | card_write(reader, ins8704, NULL, cta_res, &cta_lr) &&
|
---|
243 | card_write(reader, ins8706, NULL, cta_res, &cta_lr) &&
|
---|
244 | (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0x00)) {
|
---|
245 | reader->last_geo.number_ecm =(cta_res[2]<<8) | (cta_res[3]);
|
---|
246 | cs_log("using ecm #%04x for long viaccess ecm",reader->last_geo.number_ecm);
|
---|
247 | }
|
---|
248 | else
|
---|
249 | reader->last_geo.number_ecm = 0;
|
---|
250 |
|
---|
251 | cs_log("[viaccess-reader] ready for requests");
|
---|
252 | memset(&reader->last_geo, 0, sizeof(reader->last_geo));
|
---|
253 | return OK;
|
---|
254 | }
|
---|
255 |
|
---|
256 | bool check_crc( uchar *data )
|
---|
257 | {
|
---|
258 | int valid = 0;
|
---|
259 |
|
---|
260 | uchar sum1=data[0] + data[1] + data[2] ;
|
---|
261 | uchar sum2=data[4] + data[5] + data[6] ;
|
---|
262 | uchar sum3=data[8] + data[9] + data[10] ;
|
---|
263 | uchar sum4=data[12] + data[13] + data[14] ;
|
---|
264 |
|
---|
265 | if ( ( sum1 == data[3] ) && ( sum2 == data[7] ) && ( sum3 == data[11] )&& ( sum4 == data[15] ) )
|
---|
266 | valid = 1;
|
---|
267 |
|
---|
268 | return valid;
|
---|
269 | }
|
---|
270 |
|
---|
271 | static int viaccess_do_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
272 | {
|
---|
273 | def_resp;
|
---|
274 | static const unsigned char insa4[] = { 0xca,0xa4,0x04,0x00,0x03 }; // set provider id
|
---|
275 | unsigned char ins88[] = { 0xca,0x88,0x00,0x00,0x00 }; // set ecm
|
---|
276 | unsigned char insf8[] = { 0xca,0xf8,0x00,0x00,0x00 }; // set geographic info
|
---|
277 | static const unsigned char insc0[] = { 0xca,0xc0,0x00,0x00,0x12 }; // read dcw
|
---|
278 |
|
---|
279 | const uchar *ecm88Data=er->ecm+4; //XXX what is the 4th byte for ??
|
---|
280 | int ecm88Len=SCT_LEN(er->ecm)-4;
|
---|
281 | ulong provid=0;
|
---|
282 | int rc=0;
|
---|
283 | int hasD2 = 0;
|
---|
284 | int curEcm88len=0;
|
---|
285 | int nanoLen=0;
|
---|
286 | const uchar *nextEcm;
|
---|
287 | uchar keyToUse=0;
|
---|
288 | uchar DE04[256];
|
---|
289 | int D2KeyID=0;
|
---|
290 | int curnumber_ecm=0;
|
---|
291 |
|
---|
292 | memset(DE04, 0, sizeof(DE04)); //fix dorcel de04 bug
|
---|
293 |
|
---|
294 | nextEcm=ecm88Data;
|
---|
295 |
|
---|
296 | while (ecm88Len && !rc) {
|
---|
297 |
|
---|
298 | if(ecm88Data[0] ==0x00 && ecm88Data[1] == 0x00) {
|
---|
299 | // nano 0x00 and len 0x00 aren't valid ... something is obviously wrong with this ecm.
|
---|
300 | cs_log("[viaccess-reader] ECM: Invalid ECM structure. Rejecting");
|
---|
301 | return ERROR;
|
---|
302 | }
|
---|
303 |
|
---|
304 | // 80 33 nano 80 (ecm) + len (33)
|
---|
305 | if(ecm88Data[0]==0x80) { // nano 80, give ecm len
|
---|
306 | curEcm88len=ecm88Data[1];
|
---|
307 | nextEcm=ecm88Data+curEcm88len+2;
|
---|
308 | ecm88Data += 2;
|
---|
309 | ecm88Len -= 2;
|
---|
310 | }
|
---|
311 |
|
---|
312 | if(!curEcm88len) { //there was no nano 80 -> simple ecm
|
---|
313 | curEcm88len=ecm88Len;
|
---|
314 | }
|
---|
315 |
|
---|
316 | // d2 02 0d 02 -> D2 nano, len 2, select the AES key to be used
|
---|
317 | if(ecm88Data[0]==0xd2) {
|
---|
318 | // use the d2 arguments to get the key # to be used
|
---|
319 | int len = ecm88Data[1] + 2;
|
---|
320 | D2KeyID=ecm88Data[3];
|
---|
321 | ecm88Data += len;
|
---|
322 | ecm88Len -= len;
|
---|
323 | curEcm88len -=len;
|
---|
324 | hasD2 = 1;
|
---|
325 | }
|
---|
326 | else
|
---|
327 | hasD2 = 0;
|
---|
328 |
|
---|
329 |
|
---|
330 | // 40 07 03 0b 00 -> nano 40, len =7 ident 030B00 (tntsat), key #0 <== we're pointing here
|
---|
331 | // 09 -> use key #9
|
---|
332 | // 05 67 00
|
---|
333 | if ((ecm88Data[0]==0x90 || ecm88Data[0]==0x40) && (ecm88Data[1]==0x03 || ecm88Data[1]==0x07 ) )
|
---|
334 | {
|
---|
335 | uchar ident[3], keynr;
|
---|
336 | //uchar buff[256]; // MAX_LEN
|
---|
337 | uchar *ecmf8Data=0;
|
---|
338 | int ecmf8Len=0;
|
---|
339 |
|
---|
340 | nanoLen=ecm88Data[1] + 2;
|
---|
341 | curnumber_ecm =(ecm88Data[6]<<8) | (ecm88Data[7]);
|
---|
342 | keynr=ecm88Data[4]&0x0F;
|
---|
343 |
|
---|
344 | // 40 07 03 0b 00 -> nano 40, len =7 ident 030B00 (tntsat), key #0 <== we're pointing here
|
---|
345 | // 09 -> use key #9
|
---|
346 | if(nanoLen>5) {
|
---|
347 | if( reader->last_geo.number_ecm > 0 && reader->last_geo.number_ecm ==curnumber_ecm ) {
|
---|
348 |
|
---|
349 | keyToUse=ecm88Data[5];
|
---|
350 | keynr=keyToUse;
|
---|
351 | cs_debug("keyToUse = %d",keyToUse);
|
---|
352 | }
|
---|
353 | else
|
---|
354 | {
|
---|
355 | ecm88Data=nextEcm;
|
---|
356 | ecm88Len-=curEcm88len;
|
---|
357 | continue; //loop to next ecm
|
---|
358 | }
|
---|
359 |
|
---|
360 | }
|
---|
361 |
|
---|
362 | memcpy (ident, &ecm88Data[2], sizeof(ident));
|
---|
363 | provid = b2i(3, ident);
|
---|
364 | ident[2]&=0xF0;
|
---|
365 |
|
---|
366 | if(hasD2 && reader->aes_list) {
|
---|
367 | // check that we have the AES key to decode the CW
|
---|
368 | // if not there is no need to send the ecm to the card
|
---|
369 | if(!aes_present(reader->aes_list, 0x500, (uint32) provid, D2KeyID))
|
---|
370 | return ERROR;
|
---|
371 | }
|
---|
372 |
|
---|
373 |
|
---|
374 | if (!chk_prov(reader, ident, keynr))
|
---|
375 | {
|
---|
376 | cs_debug("[viaccess-reader] ECM: provider or key not found on card");
|
---|
377 | snprintf( er->msglog, MSGLOGSIZE, "provider(%02x%02x%02x) or key(%d) not found on card", ident[0],ident[1],ident[2], keynr );
|
---|
378 | return ERROR;
|
---|
379 | }
|
---|
380 |
|
---|
381 | ecm88Data+=nanoLen;
|
---|
382 | ecm88Len-=nanoLen;
|
---|
383 | curEcm88len-=nanoLen;
|
---|
384 |
|
---|
385 | // DE04
|
---|
386 | if (ecm88Data[0]==0xDE && ecm88Data[1]==0x04)
|
---|
387 | {
|
---|
388 | memcpy (DE04, &ecm88Data[0], 6);
|
---|
389 | ecm88Data+=6;
|
---|
390 | }
|
---|
391 | //
|
---|
392 |
|
---|
393 | if( reader->last_geo.provid != provid )
|
---|
394 | {
|
---|
395 | reader->last_geo.provid = provid;
|
---|
396 | reader->last_geo.geo_len = 0;
|
---|
397 | reader->last_geo.geo[0] = 0;
|
---|
398 | write_cmd(insa4, ident); // set provider
|
---|
399 | }
|
---|
400 |
|
---|
401 | while(ecm88Len>0 && ecm88Data[0]<0xA0)
|
---|
402 | {
|
---|
403 | int nanoLen=ecm88Data[1]+2;
|
---|
404 | if (!ecmf8Data)
|
---|
405 | ecmf8Data=(uchar *)ecm88Data;
|
---|
406 | ecmf8Len+=nanoLen;
|
---|
407 | ecm88Len-=nanoLen;
|
---|
408 | curEcm88len-=nanoLen;
|
---|
409 | ecm88Data+=nanoLen;
|
---|
410 | }
|
---|
411 | if(ecmf8Len)
|
---|
412 | {
|
---|
413 | if( reader->last_geo.geo_len!=ecmf8Len ||
|
---|
414 | memcmp(reader->last_geo.geo, ecmf8Data, reader->last_geo.geo_len))
|
---|
415 | {
|
---|
416 | memcpy(reader->last_geo.geo, ecmf8Data, ecmf8Len);
|
---|
417 | reader->last_geo.geo_len= ecmf8Len;
|
---|
418 | insf8[3]=keynr;
|
---|
419 | insf8[4]=ecmf8Len;
|
---|
420 | write_cmd(insf8, ecmf8Data);
|
---|
421 | }
|
---|
422 | }
|
---|
423 | ins88[2]=ecmf8Len?1:0;
|
---|
424 | ins88[3]=keynr;
|
---|
425 | ins88[4]= curEcm88len;
|
---|
426 | //
|
---|
427 | // we should check the nano to make sure the ecm is valid
|
---|
428 | // we should look for at least 1 E3 nano, 1 EA nano and the F0 signature nano
|
---|
429 | //
|
---|
430 | // DE04
|
---|
431 | if (DE04[0]==0xDE)
|
---|
432 | {
|
---|
433 | memcpy(DE04+6, (uchar *)ecm88Data, curEcm88len-6);
|
---|
434 | write_cmd(ins88, DE04); // request dcw
|
---|
435 | }
|
---|
436 | else
|
---|
437 | {
|
---|
438 | write_cmd(ins88, (uchar *)ecm88Data); // request dcw
|
---|
439 | }
|
---|
440 | //
|
---|
441 | write_cmd(insc0, NULL); // read dcw
|
---|
442 | switch(cta_res[0])
|
---|
443 | {
|
---|
444 | case 0xe8: // even
|
---|
445 | if(cta_res[1]==8) { memcpy(er->cw,cta_res+2,8); rc=1; }
|
---|
446 | break;
|
---|
447 | case 0xe9: // odd
|
---|
448 | if(cta_res[1]==8) { memcpy(er->cw+8,cta_res+2,8); rc=1; }
|
---|
449 | break;
|
---|
450 | case 0xea: // complete
|
---|
451 | if(cta_res[1]==16) { memcpy(er->cw,cta_res+2,16); rc=1; }
|
---|
452 | break;
|
---|
453 | default :
|
---|
454 | ecm88Data=nextEcm;
|
---|
455 | ecm88Len-=curEcm88len;
|
---|
456 | cs_debug("[viaccess-reader] ECM: key to use is not the current one, trying next ECM");
|
---|
457 | snprintf( er->msglog, MSGLOGSIZE, "key to use is not the current one, trying next ECM" );
|
---|
458 | }
|
---|
459 | }
|
---|
460 | else {
|
---|
461 | ecm88Data=nextEcm;
|
---|
462 | ecm88Len-=curEcm88len;
|
---|
463 | cs_debug("[viaccess-reader] ECM: Unknown ECM type");
|
---|
464 | snprintf( er->msglog, MSGLOGSIZE, "Unknown ECM type" );
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | if ( hasD2 && !check_crc(er->cw)) {
|
---|
469 | if(reader->aes_list) {
|
---|
470 | cs_debug("Decoding CW : using AES key id %d for provider %06x",D2KeyID,provid);
|
---|
471 | rc=aes_decrypt_from_list(reader->aes_list,0x500, (uint32) provid, D2KeyID,er->cw, 16);
|
---|
472 | if( rc == 0 )
|
---|
473 | snprintf( er->msglog, MSGLOGSIZE, "AES Decrypt : key id %d not found for CAID %04X , provider %06lx", D2KeyID, 0x500, provid & 0xFFFFF0 );
|
---|
474 | }
|
---|
475 | else
|
---|
476 | aes_decrypt(er->cw, 16);
|
---|
477 | }
|
---|
478 |
|
---|
479 | return(rc?OK:ERROR);
|
---|
480 | }
|
---|
481 |
|
---|
482 | static int viaccess_get_emm_type(EMM_PACKET *ep, struct s_reader * rdr)
|
---|
483 | {
|
---|
484 | ulong provid=0;
|
---|
485 | cs_debug_mask(D_EMM, "Entered viaccess_get_emm_type ep->emm[0]=%02x",ep->emm[0]);
|
---|
486 |
|
---|
487 | if (ep->emm[3] == 0x90 && ep->emm[4] == 0x03) {
|
---|
488 | provid = ep->emm[5] << 16 | ep->emm[6] << 8 | (ep->emm[7] & 0xFE);
|
---|
489 | memcpy(ep->provid, i2b(4, provid), 4);
|
---|
490 | }
|
---|
491 |
|
---|
492 | switch (ep->emm[0]) {
|
---|
493 | case 0x88:
|
---|
494 | ep->type=UNIQUE;
|
---|
495 | memset(ep->hexserial, 0, 8);
|
---|
496 | memcpy(ep->hexserial, ep->emm + 4, 4);
|
---|
497 | cs_debug_mask(D_EMM, "VIACCESS EMM: UNIQUE");
|
---|
498 | return(!memcmp(rdr->hexserial + 1, ep->hexserial, 4));
|
---|
499 |
|
---|
500 | case 0x8A:
|
---|
501 | case 0x8B:
|
---|
502 | ep->type=GLOBAL;
|
---|
503 | cs_debug_mask(D_EMM, "VIACCESS EMM: GLOBAL");
|
---|
504 | return TRUE;
|
---|
505 |
|
---|
506 | case 0x8C:
|
---|
507 | case 0x8D:
|
---|
508 | ep->type=SHARED;
|
---|
509 | cs_debug_mask(D_EMM, "VIACCESS EMM: SHARED (part)");
|
---|
510 | return FALSE;
|
---|
511 |
|
---|
512 | case 0x8E:
|
---|
513 | ep->type=SHARED;
|
---|
514 | memset(ep->hexserial, 0, 8);
|
---|
515 | memcpy(ep->hexserial, ep->emm + 3, 3);
|
---|
516 | cs_debug_mask(D_EMM, "VIACCESS EMM: SHARED");
|
---|
517 | return(!memcmp(&rdr->sa[0][0], ep->hexserial, 3));
|
---|
518 |
|
---|
519 | default:
|
---|
520 | ep->type = UNKNOWN;
|
---|
521 | cs_debug_mask(D_EMM, "VIACCESS EMM: UNKNOWN");
|
---|
522 | return TRUE;
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | static void viaccess_get_emm_filter(struct s_reader * rdr, uchar *filter)
|
---|
527 | {
|
---|
528 | filter[0]=0xFF;
|
---|
529 | filter[1]=3;
|
---|
530 |
|
---|
531 | filter[2]=GLOBAL;
|
---|
532 | filter[3]=0;
|
---|
533 |
|
---|
534 | filter[4+0] = 0x8D;
|
---|
535 | filter[4+0+16] = 0xFE;
|
---|
536 | //filter[4+6] = 0xA0; // FIXME: dummy, flood client with EMM's
|
---|
537 | //filter[4+6+16] = 0xF0;
|
---|
538 |
|
---|
539 |
|
---|
540 | filter[36]=SHARED;
|
---|
541 | filter[37]=0;
|
---|
542 |
|
---|
543 | filter[38+0] = 0x8E;
|
---|
544 | filter[38+0+16] = 0xFF;
|
---|
545 | memcpy(filter+38+1, &rdr->sa[0][0], 3);
|
---|
546 | memset(filter+38+1+16, 0xFF, 3);
|
---|
547 |
|
---|
548 |
|
---|
549 | filter[70]=UNIQUE;
|
---|
550 | filter[71]=0;
|
---|
551 |
|
---|
552 | filter[72+0] = 0x88;
|
---|
553 | filter[72+0+16] = 0xFF;
|
---|
554 | memcpy(filter+72+1, rdr->hexserial + 1, 4);
|
---|
555 | memset(filter+72+1+16, 0xFF, 4);
|
---|
556 |
|
---|
557 | return;
|
---|
558 | }
|
---|
559 |
|
---|
560 | static int viaccess_do_emm(struct s_reader * reader, EMM_PACKET *ep)
|
---|
561 | {
|
---|
562 | def_resp;
|
---|
563 | static const unsigned char insa4[] = { 0xca,0xa4,0x04,0x00,0x03 }; // set provider id
|
---|
564 | unsigned char insf0[] = { 0xca,0xf0,0x00,0x01,0x22 }; // set adf
|
---|
565 | unsigned char insf4[] = { 0xca,0xf4,0x00,0x01,0x00 }; // set adf, encrypted
|
---|
566 | unsigned char ins18[] = { 0xca,0x18,0x01,0x01,0x00 }; // set subscription
|
---|
567 | unsigned char ins1c[] = { 0xca,0x1c,0x01,0x01,0x00 }; // set subscription, encrypted
|
---|
568 | static const unsigned char insc8[] = { 0xca,0xc8,0x00,0x00,0x02 }; // read extended status
|
---|
569 | // static const unsigned char insc8Data[] = { 0x00,0x00 }; // data for read extended status
|
---|
570 |
|
---|
571 | int emmdatastart=7;
|
---|
572 | if (ep->type == UNIQUE) emmdatastart++;
|
---|
573 | int emmLen=SCT_LEN(ep->emm)-emmdatastart;
|
---|
574 | int rc=0;
|
---|
575 |
|
---|
576 | ///cs_dump(ep->emm, emmLen+emmdatastart, "RECEIVED EMM VIACCESS");
|
---|
577 |
|
---|
578 | int emmUpToEnd;
|
---|
579 | uchar *emmParsed = ep->emm+emmdatastart;
|
---|
580 | int provider_ok = 0;
|
---|
581 | unsigned int emm_provid;
|
---|
582 | uchar keynr = 0;
|
---|
583 | int ins18Len = 0;
|
---|
584 | uchar ins18Data[512];
|
---|
585 | uchar insData[512];
|
---|
586 | uchar *nano81Data = 0;
|
---|
587 | uchar *nano91Data = 0;
|
---|
588 | uchar *nano92Data = 0;
|
---|
589 | uchar *nano9EData = 0;
|
---|
590 | uchar *nanoF0Data = 0;
|
---|
591 |
|
---|
592 | for (emmUpToEnd=emmLen; (emmParsed[1] != 0) && (emmUpToEnd > 0); emmUpToEnd -= (2 + emmParsed[1]), emmParsed += (2 + emmParsed[1])) {
|
---|
593 | ///cs_dump (emmParsed, emmParsed[1] + 2, "NANO");
|
---|
594 |
|
---|
595 | if (emmParsed[0]==0x90 && emmParsed[1]==0x03) {
|
---|
596 | /* identification of the service operator */
|
---|
597 |
|
---|
598 | uchar soid[3], ident[3], i;
|
---|
599 |
|
---|
600 | for (i=0; i<3; i++) {
|
---|
601 | soid[i]=ident[i]=emmParsed[2+i];
|
---|
602 | }
|
---|
603 | ident[2]&=0xF0;
|
---|
604 | emm_provid=b2i(3, ident);
|
---|
605 | keynr=soid[2]&0x0F;
|
---|
606 | if (chk_prov(reader, ident, keynr)) {
|
---|
607 | provider_ok = 1;
|
---|
608 | } else {
|
---|
609 | cs_debug("[viaccess-reader] EMM: provider or key not found on card (%x, %x)", ident, keynr);
|
---|
610 | cs_log("[viaccess-reader] EMM: provider or key not found on card (%x, %x)", ident, keynr);
|
---|
611 | return ERROR;
|
---|
612 | }
|
---|
613 |
|
---|
614 | // check if the provider changes. If yes, set the new one. If not, don't .. card will return an error if we do.
|
---|
615 | if( reader->last_geo.provid != emm_provid ) {
|
---|
616 | write_cmd(insa4, ident);
|
---|
617 | if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
618 | cs_dump(insa4, 5, "set provider cmd:");
|
---|
619 | cs_dump(soid, 3, "set provider data:");
|
---|
620 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
621 | return ERROR;
|
---|
622 | }
|
---|
623 | }
|
---|
624 | // as we are maybe changing the used provider, clear the cache, so the next ecm will re-select the correct one
|
---|
625 | memset(&reader->last_geo, 0, sizeof(reader->last_geo));
|
---|
626 |
|
---|
627 | }
|
---|
628 | else if (emmParsed[0]==0x9e && emmParsed[1]==0x20) {
|
---|
629 | /* adf */
|
---|
630 |
|
---|
631 | if (!nano91Data) {
|
---|
632 | /* adf is not crypted, so test it */
|
---|
633 |
|
---|
634 | uchar custwp;
|
---|
635 | uchar *afd;
|
---|
636 |
|
---|
637 | custwp=reader->sa[0][3];
|
---|
638 | afd=(uchar*)emmParsed+2;
|
---|
639 |
|
---|
640 | if( afd[31-custwp/8] & (1 << (custwp & 7)) )
|
---|
641 | cs_debug("[viaccess-reader] emm for our card %08X", b2i(4, &reader->sa[0][0]));
|
---|
642 | else
|
---|
643 | return SKIPPED;
|
---|
644 | }
|
---|
645 |
|
---|
646 | // memorize
|
---|
647 | nano9EData = emmParsed;
|
---|
648 |
|
---|
649 | } else if (emmParsed[0]==0x81) {
|
---|
650 | nano81Data = emmParsed;
|
---|
651 | } else if (emmParsed[0]==0x91 && emmParsed[1]==0x08) {
|
---|
652 | nano91Data = emmParsed;
|
---|
653 | } else if (emmParsed[0]==0x92 && emmParsed[1]==0x08) {
|
---|
654 | nano92Data = emmParsed;
|
---|
655 | } else if (emmParsed[0]==0xF0 && emmParsed[1]==0x08) {
|
---|
656 | nanoF0Data = emmParsed;
|
---|
657 | } else if (emmParsed[0]==0x1D && emmParsed[0]==0x01 && emmParsed[0]==0x01) {
|
---|
658 | /* from cccam... skip it... */
|
---|
659 | } else {
|
---|
660 | /* other nanos */
|
---|
661 | show_subs(reader, emmParsed);
|
---|
662 |
|
---|
663 | memcpy(ins18Data+ins18Len, emmParsed, emmParsed[1] + 2);
|
---|
664 | ins18Len += emmParsed [1] + 2;
|
---|
665 | }
|
---|
666 | }
|
---|
667 |
|
---|
668 | if (!provider_ok) {
|
---|
669 | cs_debug("[viaccess-reader] provider not found in emm, continue anyway");
|
---|
670 | // force key to 1...
|
---|
671 | keynr = 1;
|
---|
672 | ///return ERROR;
|
---|
673 | }
|
---|
674 |
|
---|
675 | if (!nanoF0Data) {
|
---|
676 | cs_dump(ep->emm, ep->l, "can't find 0xf0 in emm...");
|
---|
677 | return ERROR; // error
|
---|
678 | }
|
---|
679 |
|
---|
680 | if (nano9EData) {
|
---|
681 | if (!nano91Data) {
|
---|
682 | // set adf
|
---|
683 | insf0[3] = keynr; // key
|
---|
684 | insf0[4] = nano9EData[1] + 2;
|
---|
685 | write_cmd(insf0, nano9EData);
|
---|
686 | if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
687 | cs_dump(insf0, 5, "set adf cmd:");
|
---|
688 | cs_dump(nano9EData, insf0[4] , "set adf data:");
|
---|
689 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
690 | return ERROR;
|
---|
691 | }
|
---|
692 | } else {
|
---|
693 | // set adf crypte
|
---|
694 | insf4[3] = keynr; // key
|
---|
695 | insf4[4] = nano91Data[1] + 2 + nano9EData[1] + 2;
|
---|
696 | memcpy (insData, nano91Data, nano91Data[1] + 2);
|
---|
697 | memcpy (insData + nano91Data[1] + 2, nano9EData, nano9EData[1] + 2);
|
---|
698 | write_cmd(insf4, insData);
|
---|
699 | if(( cta_res[cta_lr-2]!=0x90 && cta_res[cta_lr-2]!=0x91) || cta_res[cta_lr-1]!=0x00 ) {
|
---|
700 | cs_dump(insf4, 5, "set adf encrypted cmd:");
|
---|
701 | cs_dump(insData, insf4[4], "set adf encrypted data:");
|
---|
702 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
703 | return ERROR;
|
---|
704 | }
|
---|
705 | }
|
---|
706 | }
|
---|
707 |
|
---|
708 | if (!nano92Data) {
|
---|
709 | // send subscription
|
---|
710 | ins18[2] = nano9EData ? 0x01: 0x00; // found 9E nano ?
|
---|
711 | ins18[3] = keynr; // key
|
---|
712 | ins18[4] = ins18Len + nanoF0Data[1] + 2;
|
---|
713 | memcpy (insData, ins18Data, ins18Len);
|
---|
714 | memcpy (insData + ins18Len, nanoF0Data, nanoF0Data[1] + 2);
|
---|
715 | write_cmd(ins18, insData);
|
---|
716 | if( (cta_res[cta_lr-2]==0x90 || cta_res[cta_lr-2]==0x91) && cta_res[cta_lr-1]==0x00 ) {
|
---|
717 | cs_debug("[viaccess-reader] update successfully written");
|
---|
718 | rc=1; // written
|
---|
719 | } else {
|
---|
720 | cs_dump(ins18, 5, "set subscription cmd:");
|
---|
721 | cs_dump(insData, ins18[4], "set subscription data:");
|
---|
722 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
723 | }
|
---|
724 |
|
---|
725 | } else {
|
---|
726 | // send subscription encrypted
|
---|
727 |
|
---|
728 | if (!nano81Data) {
|
---|
729 | cs_dump(ep->emm, ep->l, "0x92 found, but can't find 0x81 in emm...");
|
---|
730 | return ERROR; // error
|
---|
731 | }
|
---|
732 |
|
---|
733 | ins1c[2] = nano9EData ? 0x01: 0x00; // found 9E nano ?
|
---|
734 | if (ep->type == UNIQUE) ins1c[2] = 0x02;
|
---|
735 | ins1c[3] = keynr; // key
|
---|
736 | ins1c[4] = nano92Data[1] + 2 + nano81Data[1] + 2 + nanoF0Data[1] + 2;
|
---|
737 | memcpy (insData, nano92Data, nano92Data[1] + 2);
|
---|
738 | memcpy (insData + nano92Data[1] + 2, nano81Data, nano81Data[1] + 2);
|
---|
739 | memcpy (insData + nano92Data[1] + 2 + nano81Data[1] + 2, nanoF0Data, nanoF0Data[1] + 2);
|
---|
740 | write_cmd(ins1c, insData);
|
---|
741 |
|
---|
742 | if( (cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0x00) ) {
|
---|
743 | cs_log("[viaccess-reader] update successfully written");
|
---|
744 | rc=1; // written
|
---|
745 | }
|
---|
746 | else {
|
---|
747 | if( cta_res[cta_lr-2]&0x1 )
|
---|
748 | cs_log("[viaccess-reader] update not written. Data already exists or unknown address");
|
---|
749 |
|
---|
750 | //if( cta_res[cta_lr-2]&0x8 ) {
|
---|
751 | write_cmd(insc8, NULL);
|
---|
752 | if( (cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0x00) ) {
|
---|
753 | cs_log("[viaccess-reader] extended status %02X %02X", cta_res[0], cta_res[1]);
|
---|
754 | }
|
---|
755 | //}
|
---|
756 | return ERROR;
|
---|
757 | }
|
---|
758 |
|
---|
759 | }
|
---|
760 |
|
---|
761 | /*
|
---|
762 | Sub Main()
|
---|
763 | Sc.Write("CA A4 04 00 03")
|
---|
764 | RX
|
---|
765 | Sc.Write("02 07 11")
|
---|
766 | RX
|
---|
767 | Sc.Write("CA F0 00 01 22")
|
---|
768 | RX
|
---|
769 | Sc.Write("9E 20")
|
---|
770 | Sc.Write("10 10 08 8A 80 00 04 00 10 10 26 E8 54 80 1E 80")
|
---|
771 | Sc.Write("00 01 00 00 00 00 00 50 00 00 80 02 22 00 08 50")
|
---|
772 | RX
|
---|
773 | Sc.Write("CA 18 01 01 11")
|
---|
774 | RX
|
---|
775 | Sc.Write("A9 05 34 DE 34 FF 80")
|
---|
776 | Sc.Write("F0 08 1A 3E AF B5 2B EE E3 3B")
|
---|
777 | RX
|
---|
778 |
|
---|
779 | End Sub
|
---|
780 | */
|
---|
781 | return rc;
|
---|
782 | }
|
---|
783 |
|
---|
784 | static int viaccess_card_info(struct s_reader * reader)
|
---|
785 | {
|
---|
786 | def_resp;
|
---|
787 | int i, l, scls, show_cls;
|
---|
788 | uchar insac[] = { 0xca, 0xac, 0x00, 0x00, 0x00 }; // select data
|
---|
789 | uchar insb8[] = { 0xca, 0xb8, 0x00, 0x00, 0x00 }; // read selected data
|
---|
790 | uchar insa4[] = { 0xca, 0xa4, 0x00, 0x00, 0x00 }; // select issuer
|
---|
791 | uchar insc0[] = { 0xca, 0xc0, 0x00, 0x00, 0x00 }; // read data item
|
---|
792 | static const uchar ins24[] = { 0xca, 0x24, 0x00, 0x00, 0x09 }; // set pin
|
---|
793 |
|
---|
794 | static const uchar cls[] = { 0x00, 0x21, 0xff, 0x9f};
|
---|
795 | static const uchar pin[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04};
|
---|
796 |
|
---|
797 | show_cls=reader->show_cls;
|
---|
798 | memset(&reader->last_geo, 0, sizeof(reader->last_geo));
|
---|
799 |
|
---|
800 | cs_log("[viaccess-reader] card detected");
|
---|
801 |
|
---|
802 | // set pin
|
---|
803 | write_cmd(ins24, pin);
|
---|
804 |
|
---|
805 | insac[2]=0xa4; write_cmd(insac, NULL); // request unique id
|
---|
806 | insb8[4]=0x07; write_cmd(insb8, NULL); // read unique id
|
---|
807 | cs_log("[viaccess-reader] serial: %llu", b2ll(5, cta_res+2));
|
---|
808 |
|
---|
809 | scls=0;
|
---|
810 | insa4[2]=0x00; write_cmd(insa4, NULL); // select issuer 0
|
---|
811 | for (i=1; (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0); i++)
|
---|
812 | {
|
---|
813 | ulong l_provid, l_sa;
|
---|
814 | uchar l_name[64];
|
---|
815 | insc0[4]=0x1a; write_cmd(insc0, NULL); // show provider properties
|
---|
816 | cta_res[2]&=0xF0;
|
---|
817 | l_provid=b2i(3, cta_res);
|
---|
818 |
|
---|
819 | insac[2]=0xa5; write_cmd(insac, NULL); // request sa
|
---|
820 | insb8[4]=0x06; write_cmd(insb8, NULL); // read sa
|
---|
821 | l_sa=b2i(4, cta_res+2);
|
---|
822 |
|
---|
823 | insac[2]=0xa7; write_cmd(insac, NULL); // request name
|
---|
824 | insb8[4]=0x02; write_cmd(insb8, NULL); // read name nano + len
|
---|
825 | l=cta_res[1];
|
---|
826 | insb8[4]=l; write_cmd(insb8, NULL); // read name
|
---|
827 | cta_res[l]=0;
|
---|
828 | trim((char *)cta_res);
|
---|
829 | if (cta_res[0])
|
---|
830 | snprintf((char *)l_name, sizeof(l_name), ", name: %s", cta_res);
|
---|
831 | else
|
---|
832 | l_name[0]=0;
|
---|
833 |
|
---|
834 | // read GEO
|
---|
835 | insac[2]=0xa6; write_cmd(insac, NULL); // request GEO
|
---|
836 | insb8[4]=0x02; write_cmd(insb8, NULL); // read GEO nano + len
|
---|
837 | l=cta_res[1];
|
---|
838 | insb8[4]=l; write_cmd(insb8, NULL); // read geo
|
---|
839 | cs_ri_log(reader, "provider: %d, id: %06X%s, sa: %08X, geo: %s",
|
---|
840 | i, l_provid, l_name, l_sa, (l<4) ? "empty" : cs_hexdump(1, cta_res, l));
|
---|
841 |
|
---|
842 | // read classes subscription
|
---|
843 | insac[2]=0xa9; insac[4]=4;
|
---|
844 | write_cmd(insac, cls); // request class subs
|
---|
845 | scls=0;
|
---|
846 | while( (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0) )
|
---|
847 | {
|
---|
848 | insb8[4]=0x02; write_cmd(insb8, NULL); // read class subs nano + len
|
---|
849 | if( (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0) )
|
---|
850 | {
|
---|
851 | int fshow;
|
---|
852 | l=cta_res[1];
|
---|
853 | //fshow=(client[cs_idx].dbglvl==D_DUMP)?1:(scls < show_cls)?1:0;
|
---|
854 | fshow=(scls<show_cls);
|
---|
855 | insb8[4]=l; write_cmd(insb8, NULL); // read class subs
|
---|
856 | if( (cta_res[cta_lr-2]==0x90) && (fshow) &&
|
---|
857 | (cta_res[cta_lr-1]==0x00 || cta_res[cta_lr-1]==0x08) )
|
---|
858 | {
|
---|
859 | show_class(reader, NULL, cta_res, cta_lr-2);
|
---|
860 | scls++;
|
---|
861 | }
|
---|
862 | }
|
---|
863 | }
|
---|
864 |
|
---|
865 | insac[4]=0;
|
---|
866 | insa4[2]=0x02;
|
---|
867 | write_cmd(insa4, NULL); // select next provider
|
---|
868 | }
|
---|
869 | //return ERROR;
|
---|
870 | return OK;
|
---|
871 | }
|
---|
872 |
|
---|
873 | #ifdef HAVE_DVBAPI
|
---|
874 | int viaccess_reassemble_emm(uchar *buffer, uint *len) {
|
---|
875 | static uchar emm_global[512];
|
---|
876 | static int emm_global_len = 0;
|
---|
877 |
|
---|
878 | int pos, emm_len = 0, k;
|
---|
879 | uchar emmbuf[512];
|
---|
880 |
|
---|
881 | // Viaccess
|
---|
882 | if (*len>500) return 0;
|
---|
883 |
|
---|
884 | switch(buffer[0]) {
|
---|
885 | case 0x8c:
|
---|
886 | case 0x8d:
|
---|
887 | // emm-s part 1
|
---|
888 | if (!memcmp(emm_global, buffer, *len))
|
---|
889 | return 0;
|
---|
890 |
|
---|
891 | //cs_log("viaccess global emm_provid: %06X provid: %06X", emm_provid, provider);
|
---|
892 |
|
---|
893 | // copy first part of the emm-s
|
---|
894 | memcpy(emm_global, buffer, *len);
|
---|
895 | emm_global_len=*len;
|
---|
896 | //cs_ddump(buffer, len, "viaccess global emm:");
|
---|
897 | return 0;
|
---|
898 |
|
---|
899 | case 0x8e:
|
---|
900 | // emm-s part 2
|
---|
901 | if (!emm_global_len) return 0;
|
---|
902 |
|
---|
903 | if (buffer[6]!=0x00) return 0;
|
---|
904 |
|
---|
905 | memcpy(emmbuf, buffer, 7);
|
---|
906 | pos=7;
|
---|
907 |
|
---|
908 | for (k=3; k<emm_global[2]+2 && k<emm_global_len; k += emm_global[k+1]+2) {
|
---|
909 | if (emm_global[k]!=0x90) continue;
|
---|
910 | memcpy(emmbuf+pos, emm_global+k, emm_global[k+1]+2);
|
---|
911 | pos += emm_global[k+1]+2;
|
---|
912 | }
|
---|
913 |
|
---|
914 | memcpy(emmbuf+pos, "\x9E\x20", 2);
|
---|
915 | memcpy(emmbuf+pos+2, buffer+7, 32);
|
---|
916 | pos+=34;
|
---|
917 |
|
---|
918 | int found=0;
|
---|
919 | for (k=8; k<emm_global[2]+2 && k<emm_global_len; k += emm_global[k+1]+2) {
|
---|
920 | if (emm_global[k] == 0xA1 || emm_global[k] == 0xA8 || emm_global[k] == 0xA9 || emm_global[k] == 0xB6) {
|
---|
921 | memcpy(emmbuf+pos, emm_global+k, emm_global[k+1]+2);
|
---|
922 | pos += emm_global[k+1]+2;
|
---|
923 | found=1;
|
---|
924 | }
|
---|
925 | }
|
---|
926 | if (found==0) return 0;
|
---|
927 |
|
---|
928 | memcpy(emmbuf+pos, "\xF0\x08", 2);
|
---|
929 | memcpy(emmbuf+pos+2, buffer+39, 8);
|
---|
930 | pos+=10;
|
---|
931 |
|
---|
932 | emm_len=pos;
|
---|
933 | emmbuf[2]=emm_len-3;
|
---|
934 | cs_ddump(buffer, *len, "original emm:");
|
---|
935 | memcpy(buffer, emmbuf, emm_len);
|
---|
936 | *len=emm_len;
|
---|
937 | break;
|
---|
938 | }
|
---|
939 | return 1;
|
---|
940 | }
|
---|
941 | #endif
|
---|
942 |
|
---|
943 | void reader_viaccess(struct s_cardsystem *ph)
|
---|
944 | {
|
---|
945 | ph->do_emm=viaccess_do_emm;
|
---|
946 | ph->do_ecm=viaccess_do_ecm;
|
---|
947 | ph->card_info=viaccess_card_info;
|
---|
948 | ph->card_init=viaccess_card_init;
|
---|
949 | ph->get_emm_type=viaccess_get_emm_type;
|
---|
950 | ph->get_emm_filter=viaccess_get_emm_filter;
|
---|
951 | ph->caids[0]=0x05;
|
---|
952 | }
|
---|