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))
|
---|
180 | return ERROR;
|
---|
181 |
|
---|
182 | write_cmd(insFAC, FacDat);
|
---|
183 | if( !(cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0) )
|
---|
184 | return ERROR;
|
---|
185 |
|
---|
186 | memset(&reader->last_geo, 0, sizeof(reader->last_geo));
|
---|
187 | write_cmd(insFAC, ins8702_data);
|
---|
188 | if ((cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0x00)) {
|
---|
189 | write_cmd(ins8704, NULL);
|
---|
190 | if ((cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0x00)) {
|
---|
191 | write_cmd(ins8706, NULL);
|
---|
192 | if ((cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0x00)) {
|
---|
193 | reader->last_geo.number_ecm =(cta_res[2]<<8) | (cta_res[3]);
|
---|
194 | cs_log("[viaccess-reader] using ecm #%x for long viaccess ecm",reader->last_geo.number_ecm);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | // switch((atr[atrsize-4]<<8)|atr[atrsize-3])
|
---|
201 | // {
|
---|
202 | // case 0x6268: ver="2.3"; break;
|
---|
203 | // case 0x6668: ver="2.4(?)"; break;
|
---|
204 | // case 0xa268:
|
---|
205 | // default: ver="unknown"; break;
|
---|
206 | // }
|
---|
207 |
|
---|
208 | reader->caid[0]=0x500;
|
---|
209 | memset(reader->prid, 0xff, sizeof(reader->prid));
|
---|
210 | insac[2]=0xa4; write_cmd(insac, NULL); // request unique id
|
---|
211 | insb8[4]=0x07; write_cmd(insb8, NULL); // read unique id
|
---|
212 | memcpy(reader->hexserial, cta_res+2, 5);
|
---|
213 | // cs_log("[viaccess-reader] type: Viaccess, ver: %s serial: %llu", ver, b2ll(5, cta_res+2));
|
---|
214 | cs_ri_log(reader, "type: Viaccess (%sstandard atr), caid: %04X, serial: %llu",
|
---|
215 | atr[9]==0x68?"":"non-",reader->caid[0], b2ll(5, cta_res+2));
|
---|
216 |
|
---|
217 | i=0;
|
---|
218 | insa4[2]=0x00; write_cmd(insa4, NULL); // select issuer 0
|
---|
219 | buf[0]=0;
|
---|
220 | while((cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0))
|
---|
221 | {
|
---|
222 | insc0[4]=0x1a; write_cmd(insc0, NULL); // show provider properties
|
---|
223 | cta_res[2]&=0xF0;
|
---|
224 | reader->prid[i][0]=0;
|
---|
225 | memcpy(&reader->prid[i][1], cta_res, 3);
|
---|
226 | memcpy(&reader->availkeys[i][0], cta_res+10, 16);
|
---|
227 | sprintf((char *)buf+strlen((char *)buf), ",%06lX", b2i(3, &reader->prid[i][1]));
|
---|
228 | //cs_log("[viaccess-reader] buf: %s", buf);
|
---|
229 |
|
---|
230 | insac[2]=0xa5; write_cmd(insac, NULL); // request sa
|
---|
231 | insb8[4]=0x06; write_cmd(insb8, NULL); // read sa
|
---|
232 | memcpy(&reader->sa[i][0], cta_res+2, 4);
|
---|
233 |
|
---|
234 | /*
|
---|
235 | insac[2]=0xa7; write_cmd(insac, NULL); // request name
|
---|
236 | insb8[4]=0x02; write_cmd(insb8, NULL); // read name nano + len
|
---|
237 | l=cta_res[1];
|
---|
238 | insb8[4]=l; write_cmd(insb8, NULL); // read name
|
---|
239 | cta_res[l]=0;
|
---|
240 | cs_log("[viaccess-reader] name: %s", cta_res);
|
---|
241 | */
|
---|
242 |
|
---|
243 | insa4[2]=0x02;
|
---|
244 | write_cmd(insa4, NULL); // select next issuer
|
---|
245 | i++;
|
---|
246 | }
|
---|
247 | reader->nprov=i;
|
---|
248 | cs_ri_log(reader, "providers: %d (%s)", reader->nprov, buf+1);
|
---|
249 |
|
---|
250 | if (cfg->ulparent)
|
---|
251 | unlock_parental(reader);
|
---|
252 |
|
---|
253 | cs_log("[viaccess-reader] ready for requests");
|
---|
254 | return OK;
|
---|
255 | }
|
---|
256 |
|
---|
257 | bool check_crc( uchar *data )
|
---|
258 | {
|
---|
259 | int valid = 0;
|
---|
260 |
|
---|
261 | uchar sum1=data[0] + data[1] + data[2] ;
|
---|
262 | uchar sum2=data[4] + data[5] + data[6] ;
|
---|
263 | uchar sum3=data[8] + data[9] + data[10] ;
|
---|
264 | uchar sum4=data[12] + data[13] + data[14] ;
|
---|
265 |
|
---|
266 | if ( ( sum1 == data[3] ) && ( sum2 == data[7] ) && ( sum3 == data[11] )&& ( sum4 == data[15] ) )
|
---|
267 | valid = 1;
|
---|
268 |
|
---|
269 | return valid;
|
---|
270 | }
|
---|
271 |
|
---|
272 | static int viaccess_do_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
273 | {
|
---|
274 | def_resp;
|
---|
275 | static const unsigned char insa4[] = { 0xca,0xa4,0x04,0x00,0x03 }; // set provider id
|
---|
276 | unsigned char ins88[] = { 0xca,0x88,0x00,0x00,0x00 }; // set ecm
|
---|
277 | unsigned char insf8[] = { 0xca,0xf8,0x00,0x00,0x00 }; // set geographic info
|
---|
278 | static const unsigned char insc0[] = { 0xca,0xc0,0x00,0x00,0x12 }; // read dcw
|
---|
279 |
|
---|
280 | uchar *ecm88Data=er->ecm+4; //XXX what is the 4th byte for ??
|
---|
281 | int ecm88Len=SCT_LEN(er->ecm)-4;
|
---|
282 | ulong provid=0;
|
---|
283 | int rc=0;
|
---|
284 | int hasD2 = 0;
|
---|
285 | int curEcm88len=0;
|
---|
286 | int nanoLen=0;
|
---|
287 | uchar *nextEcm;
|
---|
288 | uchar DE04[256];
|
---|
289 | int D2KeyID=0;
|
---|
290 | int curnumber_ecm=0;
|
---|
291 | //nanoD2 d2 02 0d 02 -> D2 nano, len 2
|
---|
292 | // 0d -> post AES decrypt CW
|
---|
293 | // 0b -> pre AES decrypt CW
|
---|
294 | int nanoD2 = 0; // 0x0b = 1 0x0d = 2
|
---|
295 |
|
---|
296 | memset(DE04, 0, sizeof(DE04)); //fix dorcel de04 bug
|
---|
297 |
|
---|
298 | nextEcm=ecm88Data;
|
---|
299 |
|
---|
300 | while (ecm88Len && !rc) {
|
---|
301 |
|
---|
302 | if(ecm88Data[0] ==0x00 && ecm88Data[1] == 0x00) {
|
---|
303 | // nano 0x00 and len 0x00 aren't valid ... something is obviously wrong with this ecm.
|
---|
304 | cs_log("[viaccess-reader] ECM: Invalid ECM structure. Rejecting");
|
---|
305 | return ERROR;
|
---|
306 | }
|
---|
307 |
|
---|
308 | // 80 33 nano 80 (ecm) + len (33)
|
---|
309 | if(ecm88Data[0]==0x80) { // nano 80, give ecm len
|
---|
310 | curEcm88len=ecm88Data[1];
|
---|
311 | nextEcm=ecm88Data+curEcm88len+2;
|
---|
312 | ecm88Data += 2;
|
---|
313 | ecm88Len -= 2;
|
---|
314 | }
|
---|
315 |
|
---|
316 | if(!curEcm88len) { //there was no nano 80 -> simple ecm
|
---|
317 | curEcm88len=ecm88Len;
|
---|
318 | }
|
---|
319 |
|
---|
320 | // d2 02 0d 02 -> D2 nano, len 2, select the AES key to be used
|
---|
321 | if(ecm88Data[0]==0xd2) {
|
---|
322 | // test if need post or pre AES decrypt
|
---|
323 | if(ecm88Data[2]==0x0b)
|
---|
324 | {
|
---|
325 | nanoD2 = 1;
|
---|
326 | cs_debug_mask(D_READER, "[viaccess-reader] ECM: nano D2 0x0b");
|
---|
327 | }
|
---|
328 | if(ecm88Data[2]==0x0d)
|
---|
329 | {
|
---|
330 | nanoD2 = 2;
|
---|
331 | cs_debug_mask(D_READER, "[viaccess-reader] ECM: nano D2 0x0d");
|
---|
332 | }
|
---|
333 | // use the d2 arguments to get the key # to be used
|
---|
334 | int len = ecm88Data[1] + 2;
|
---|
335 | D2KeyID=ecm88Data[3];
|
---|
336 | ecm88Data += len;
|
---|
337 | ecm88Len -= len;
|
---|
338 | curEcm88len -=len;
|
---|
339 | hasD2 = 1;
|
---|
340 | }
|
---|
341 | else
|
---|
342 | hasD2 = 0;
|
---|
343 |
|
---|
344 |
|
---|
345 | // 40 07 03 0b 00 -> nano 40, len =7 ident 030B00 (tntsat), key #0 <== we're pointing here
|
---|
346 | // 09 -> use key #9
|
---|
347 | // 05 67 00
|
---|
348 | if ((ecm88Data[0]==0x90 || ecm88Data[0]==0x40) && (ecm88Data[1]==0x03 || ecm88Data[1]==0x07 ) )
|
---|
349 | {
|
---|
350 | uchar ident[3], keynr;
|
---|
351 | uchar *ecmf8Data=0;
|
---|
352 | int ecmf8Len=0;
|
---|
353 |
|
---|
354 | nanoLen=ecm88Data[1] + 2;
|
---|
355 | keynr=ecm88Data[4]&0x0F;
|
---|
356 |
|
---|
357 | // 40 07 03 0b 00 -> nano 40, len =7 ident 030B00 (tntsat), key #0 <== we're pointing here
|
---|
358 | // 09 -> use key #9
|
---|
359 | if(nanoLen>5) {
|
---|
360 | curnumber_ecm =(ecm88Data[6]<<8) | (ecm88Data[7]);
|
---|
361 | cs_debug_mask(D_READER, "checking if the ecm number (%x) match the card one (%x)",curnumber_ecm,reader->last_geo.number_ecm);
|
---|
362 | // if we have an ecm number we check it.
|
---|
363 | // we can't assume that if the nano len is 5 or more we have an ecm number
|
---|
364 | // as some card don't support this
|
---|
365 | if( reader->last_geo.number_ecm > 0 ) {
|
---|
366 | if(reader->last_geo.number_ecm ==curnumber_ecm ) {
|
---|
367 | keynr=ecm88Data[5];
|
---|
368 | cs_debug_mask(D_READER, "keyToUse = %02x",ecm88Data[5]);
|
---|
369 | }
|
---|
370 | else
|
---|
371 | {
|
---|
372 | ecm88Data=nextEcm;
|
---|
373 | ecm88Len-=curEcm88len;
|
---|
374 | continue; //loop to next ecm
|
---|
375 | }
|
---|
376 | }
|
---|
377 | else { // long ecm but we don't have an ecm number so we have to try them all.
|
---|
378 | keynr=ecm88Data[5];
|
---|
379 | cs_debug_mask(D_READER, "keyToUse = %02x",ecm88Data[5]);
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | memcpy (ident, &ecm88Data[2], sizeof(ident));
|
---|
384 | provid = b2i(3, ident);
|
---|
385 | ident[2]&=0xF0;
|
---|
386 |
|
---|
387 | if(hasD2 && reader->aes_list) {
|
---|
388 | // check that we have the AES key to decode the CW
|
---|
389 | // if not there is no need to send the ecm to the card
|
---|
390 | if(!aes_present(reader->aes_list, 0x500, (uint32) (provid & 0xFFFFF0) , D2KeyID))
|
---|
391 | return ERROR;
|
---|
392 | }
|
---|
393 |
|
---|
394 |
|
---|
395 | if (!chk_prov(reader, ident, keynr))
|
---|
396 | {
|
---|
397 | cs_debug_mask(D_READER, "[viaccess-reader] ECM: provider or key not found on card");
|
---|
398 | snprintf( er->msglog, MSGLOGSIZE, "provider(%02x%02x%02x) or key(%d) not found on card", ident[0],ident[1],ident[2], keynr );
|
---|
399 | return ERROR;
|
---|
400 | }
|
---|
401 |
|
---|
402 | ecm88Data+=nanoLen;
|
---|
403 | ecm88Len-=nanoLen;
|
---|
404 | curEcm88len-=nanoLen;
|
---|
405 |
|
---|
406 | // DE04
|
---|
407 | if (ecm88Data[0]==0xDE && ecm88Data[1]==0x04)
|
---|
408 | {
|
---|
409 | memcpy (DE04, &ecm88Data[0], 6);
|
---|
410 | ecm88Data+=6;
|
---|
411 | }
|
---|
412 | //
|
---|
413 |
|
---|
414 | if( reader->last_geo.provid != provid )
|
---|
415 | {
|
---|
416 | reader->last_geo.provid = provid;
|
---|
417 | reader->last_geo.geo_len = 0;
|
---|
418 | reader->last_geo.geo[0] = 0;
|
---|
419 | write_cmd(insa4, ident); // set provider
|
---|
420 | }
|
---|
421 |
|
---|
422 | //Nano D2 0x0b Pre AES decrypt CW
|
---|
423 | if ( hasD2 && nanoD2 == 1)
|
---|
424 | {
|
---|
425 | uchar *ecm88DataCW = ecm88Data;
|
---|
426 | int cwStart = 0;
|
---|
427 | //int cwStartRes = 0;
|
---|
428 | int exit = 0;
|
---|
429 | // find CW start
|
---|
430 | while(cwStart < curEcm88len -1 && !exit)
|
---|
431 | {
|
---|
432 | if(ecm88Data[cwStart] == 0xEA && ecm88Data[cwStart+1] == 0x10)
|
---|
433 | {
|
---|
434 | ecm88DataCW = ecm88DataCW + cwStart + 2;
|
---|
435 | exit = 1;
|
---|
436 | }
|
---|
437 | cwStart++;
|
---|
438 | }
|
---|
439 | // use AES from list to decrypt CW
|
---|
440 | cs_debug_mask(D_READER, "Decoding CW : using AES key id %d for provider %06x",D2KeyID, (provid & 0xFFFFF0));
|
---|
441 | rc=aes_decrypt_from_list(reader->aes_list,0x500, (uint32) (provid & 0xFFFFF0), D2KeyID, &ecm88DataCW[0], 16);
|
---|
442 | if( rc == 0 )
|
---|
443 | snprintf( er->msglog, MSGLOGSIZE, "AES Decrypt : key id %d not found for CAID %04X , provider %06lx", D2KeyID, 0x500, (provid & 0xFFFFF0) );
|
---|
444 | }
|
---|
445 |
|
---|
446 | while(ecm88Len>0 && ecm88Data[0]<0xA0)
|
---|
447 | {
|
---|
448 | int nanoLen=ecm88Data[1]+2;
|
---|
449 | if (!ecmf8Data)
|
---|
450 | ecmf8Data=(uchar *)ecm88Data;
|
---|
451 | ecmf8Len+=nanoLen;
|
---|
452 | ecm88Len-=nanoLen;
|
---|
453 | curEcm88len-=nanoLen;
|
---|
454 | ecm88Data+=nanoLen;
|
---|
455 | }
|
---|
456 | if(ecmf8Len)
|
---|
457 | {
|
---|
458 | if( reader->last_geo.geo_len!=ecmf8Len ||
|
---|
459 | memcmp(reader->last_geo.geo, ecmf8Data, reader->last_geo.geo_len))
|
---|
460 | {
|
---|
461 | memcpy(reader->last_geo.geo, ecmf8Data, ecmf8Len);
|
---|
462 | reader->last_geo.geo_len= ecmf8Len;
|
---|
463 | insf8[3]=keynr;
|
---|
464 | insf8[4]=ecmf8Len;
|
---|
465 | write_cmd(insf8, ecmf8Data);
|
---|
466 | }
|
---|
467 | }
|
---|
468 | ins88[2]=ecmf8Len?1:0;
|
---|
469 | ins88[3]=keynr;
|
---|
470 | ins88[4]= curEcm88len;
|
---|
471 | //
|
---|
472 | // we should check the nano to make sure the ecm is valid
|
---|
473 | // we should look for at least 1 E3 nano, 1 EA nano and the F0 signature nano
|
---|
474 | //
|
---|
475 | // DE04
|
---|
476 | if (DE04[0]==0xDE)
|
---|
477 | {
|
---|
478 | memcpy(DE04+6, (uchar *)ecm88Data, curEcm88len-6);
|
---|
479 | write_cmd(ins88, DE04); // request dcw
|
---|
480 | }
|
---|
481 | else
|
---|
482 | {
|
---|
483 | write_cmd(ins88, (uchar *)ecm88Data); // request dcw
|
---|
484 | }
|
---|
485 | //
|
---|
486 | write_cmd(insc0, NULL); // read dcw
|
---|
487 | switch(cta_res[0])
|
---|
488 | {
|
---|
489 | case 0xe8: // even
|
---|
490 | if(cta_res[1]==8) { memcpy(er->cw,cta_res+2,8); rc=1; }
|
---|
491 | break;
|
---|
492 | case 0xe9: // odd
|
---|
493 | if(cta_res[1]==8) { memcpy(er->cw+8,cta_res+2,8); rc=1; }
|
---|
494 | break;
|
---|
495 | case 0xea: // complete
|
---|
496 | if(cta_res[1]==16) { memcpy(er->cw,cta_res+2,16); rc=1; }
|
---|
497 | break;
|
---|
498 | default :
|
---|
499 | ecm88Data=nextEcm;
|
---|
500 | ecm88Len-=curEcm88len;
|
---|
501 | cs_debug_mask(D_READER, "[viaccess-reader] ECM: key to use is not the current one, trying next ECM");
|
---|
502 | snprintf( er->msglog, MSGLOGSIZE, "key to use is not the current one, trying next ECM" );
|
---|
503 | }
|
---|
504 | }
|
---|
505 | else {
|
---|
506 | ecm88Data=nextEcm;
|
---|
507 | ecm88Len-=curEcm88len;
|
---|
508 | cs_debug_mask(D_READER, "[viaccess-reader] ECM: Unknown ECM type");
|
---|
509 | snprintf( er->msglog, MSGLOGSIZE, "Unknown ECM type" );
|
---|
510 | }
|
---|
511 | }
|
---|
512 |
|
---|
513 | if ( hasD2 && !check_crc(er->cw) && nanoD2 == 2) {
|
---|
514 | cs_debug_mask(D_READER, "Decoding CW : using AES key id %d for provider %06x",D2KeyID, (provid & 0xFFFFF0));
|
---|
515 | rc=aes_decrypt_from_list(reader->aes_list,0x500, (uint32) (provid & 0xFFFFF0), D2KeyID,er->cw, 16);
|
---|
516 | if( rc == 0 )
|
---|
517 | snprintf( er->msglog, MSGLOGSIZE, "AES Decrypt : key id %d not found for CAID %04X , provider %06lx", D2KeyID, 0x500, (provid & 0xFFFFF0) );
|
---|
518 | }
|
---|
519 |
|
---|
520 | return(rc?OK:ERROR);
|
---|
521 | }
|
---|
522 |
|
---|
523 | static int viaccess_get_emm_type(EMM_PACKET *ep, struct s_reader * rdr)
|
---|
524 | {
|
---|
525 | ulong provid=0;
|
---|
526 | cs_debug_mask(D_EMM, "Entered viaccess_get_emm_type ep->emm[0]=%02x",ep->emm[0]);
|
---|
527 |
|
---|
528 | if (ep->emm[3] == 0x90 && ep->emm[4] == 0x03) {
|
---|
529 | provid = ep->emm[5] << 16 | ep->emm[6] << 8 | (ep->emm[7] & 0xFE);
|
---|
530 | memcpy(ep->provid, i2b(4, provid), 4);
|
---|
531 | }
|
---|
532 |
|
---|
533 | switch (ep->emm[0]) {
|
---|
534 | case 0x88:
|
---|
535 | ep->type=UNIQUE;
|
---|
536 | memset(ep->hexserial, 0, 8);
|
---|
537 | memcpy(ep->hexserial, ep->emm + 4, 4);
|
---|
538 | cs_debug_mask(D_EMM, "VIACCESS EMM: UNIQUE");
|
---|
539 | return(!memcmp(rdr->hexserial + 1, ep->hexserial, 4));
|
---|
540 |
|
---|
541 | case 0x8A:
|
---|
542 | case 0x8B:
|
---|
543 | ep->type=GLOBAL;
|
---|
544 | cs_debug_mask(D_EMM, "VIACCESS EMM: GLOBAL");
|
---|
545 | return TRUE;
|
---|
546 |
|
---|
547 | case 0x8C:
|
---|
548 | case 0x8D:
|
---|
549 | ep->type=SHARED;
|
---|
550 | cs_debug_mask(D_EMM, "VIACCESS EMM: SHARED (part)");
|
---|
551 | return FALSE;
|
---|
552 |
|
---|
553 | case 0x8E:
|
---|
554 | ep->type=SHARED;
|
---|
555 | memset(ep->hexserial, 0, 8);
|
---|
556 | memcpy(ep->hexserial, ep->emm + 3, 3);
|
---|
557 | cs_debug_mask(D_EMM, "VIACCESS EMM: SHARED");
|
---|
558 | return(!memcmp(&rdr->sa[0][0], ep->hexserial, 3));
|
---|
559 |
|
---|
560 | default:
|
---|
561 | ep->type = UNKNOWN;
|
---|
562 | cs_debug_mask(D_EMM, "VIACCESS EMM: UNKNOWN");
|
---|
563 | return TRUE;
|
---|
564 | }
|
---|
565 | }
|
---|
566 |
|
---|
567 | static void viaccess_get_emm_filter(struct s_reader * rdr, uchar *filter)
|
---|
568 | {
|
---|
569 | filter[0]=0xFF;
|
---|
570 | filter[1]=3;
|
---|
571 |
|
---|
572 | filter[2]=GLOBAL;
|
---|
573 | filter[3]=0;
|
---|
574 |
|
---|
575 | filter[4+0] = 0x8D;
|
---|
576 | filter[4+0+16] = 0xFE;
|
---|
577 | //filter[4+6] = 0xA0; // FIXME: dummy, flood client with EMM's
|
---|
578 | //filter[4+6+16] = 0xF0;
|
---|
579 |
|
---|
580 |
|
---|
581 | filter[36]=SHARED;
|
---|
582 | filter[37]=0;
|
---|
583 |
|
---|
584 | filter[38+0] = 0x8E;
|
---|
585 | filter[38+0+16] = 0xFF;
|
---|
586 | memcpy(filter+38+1, &rdr->sa[0][0], 3);
|
---|
587 | memset(filter+38+1+16, 0xFF, 3);
|
---|
588 |
|
---|
589 |
|
---|
590 | filter[70]=UNIQUE;
|
---|
591 | filter[71]=0;
|
---|
592 |
|
---|
593 | filter[72+0] = 0x88;
|
---|
594 | filter[72+0+16] = 0xFF;
|
---|
595 | memcpy(filter+72+1, rdr->hexserial + 1, 4);
|
---|
596 | memset(filter+72+1+16, 0xFF, 4);
|
---|
597 |
|
---|
598 | return;
|
---|
599 | }
|
---|
600 |
|
---|
601 | static int viaccess_do_emm(struct s_reader * reader, EMM_PACKET *ep)
|
---|
602 | {
|
---|
603 | def_resp;
|
---|
604 | static const unsigned char insa4[] = { 0xca,0xa4,0x04,0x00,0x03 }; // set provider id
|
---|
605 | unsigned char insf0[] = { 0xca,0xf0,0x00,0x01,0x22 }; // set adf
|
---|
606 | unsigned char insf4[] = { 0xca,0xf4,0x00,0x01,0x00 }; // set adf, encrypted
|
---|
607 | unsigned char ins18[] = { 0xca,0x18,0x01,0x01,0x00 }; // set subscription
|
---|
608 | unsigned char ins1c[] = { 0xca,0x1c,0x01,0x01,0x00 }; // set subscription, encrypted
|
---|
609 | static const unsigned char insc8[] = { 0xca,0xc8,0x00,0x00,0x02 }; // read extended status
|
---|
610 | // static const unsigned char insc8Data[] = { 0x00,0x00 }; // data for read extended status
|
---|
611 |
|
---|
612 | int emmdatastart=7;
|
---|
613 | if (ep->type == UNIQUE) emmdatastart++;
|
---|
614 | int emmLen=SCT_LEN(ep->emm)-emmdatastart;
|
---|
615 | int rc=0;
|
---|
616 |
|
---|
617 | ///cs_dump(ep->emm, emmLen+emmdatastart, "RECEIVED EMM VIACCESS");
|
---|
618 |
|
---|
619 | int emmUpToEnd;
|
---|
620 | uchar *emmParsed = ep->emm+emmdatastart;
|
---|
621 | int provider_ok = 0;
|
---|
622 | unsigned int emm_provid;
|
---|
623 | uchar keynr = 0;
|
---|
624 | int ins18Len = 0;
|
---|
625 | uchar ins18Data[512];
|
---|
626 | uchar insData[512];
|
---|
627 | uchar *nano81Data = 0;
|
---|
628 | uchar *nano91Data = 0;
|
---|
629 | uchar *nano92Data = 0;
|
---|
630 | uchar *nano9EData = 0;
|
---|
631 | uchar *nanoF0Data = 0;
|
---|
632 |
|
---|
633 | for (emmUpToEnd=emmLen; (emmParsed[1] != 0) && (emmUpToEnd > 0); emmUpToEnd -= (2 + emmParsed[1]), emmParsed += (2 + emmParsed[1])) {
|
---|
634 | ///cs_dump (emmParsed, emmParsed[1] + 2, "NANO");
|
---|
635 |
|
---|
636 | if (emmParsed[0]==0x90 && emmParsed[1]==0x03) {
|
---|
637 | /* identification of the service operator */
|
---|
638 |
|
---|
639 | uchar soid[3], ident[3], i;
|
---|
640 |
|
---|
641 | for (i=0; i<3; i++) {
|
---|
642 | soid[i]=ident[i]=emmParsed[2+i];
|
---|
643 | }
|
---|
644 | ident[2]&=0xF0;
|
---|
645 | emm_provid=b2i(3, ident);
|
---|
646 | keynr=soid[2]&0x0F;
|
---|
647 | if (chk_prov(reader, ident, keynr)) {
|
---|
648 | provider_ok = 1;
|
---|
649 | } else {
|
---|
650 | cs_debug_mask(D_READER, "[viaccess-reader] EMM: provider or key not found on card (%x, %x)", ident, keynr);
|
---|
651 | cs_log("[viaccess-reader] EMM: provider or key not found on card (%x, %x)", ident, keynr);
|
---|
652 | return ERROR;
|
---|
653 | }
|
---|
654 |
|
---|
655 | // check if the provider changes. If yes, set the new one. If not, don't .. card will return an error if we do.
|
---|
656 | if( reader->last_geo.provid != emm_provid ) {
|
---|
657 | write_cmd(insa4, ident);
|
---|
658 | if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
659 | cs_dump(insa4, 5, "set provider cmd:");
|
---|
660 | cs_dump(soid, 3, "set provider data:");
|
---|
661 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
662 | return ERROR;
|
---|
663 | }
|
---|
664 | }
|
---|
665 | // as we are maybe changing the used provider, clear the cache, so the next ecm will re-select the correct one
|
---|
666 | reader->last_geo.provid = 0;
|
---|
667 | reader->last_geo.geo_len = 0;
|
---|
668 | reader->last_geo.geo[0] = 0;
|
---|
669 |
|
---|
670 | }
|
---|
671 | else if (emmParsed[0]==0x9e && emmParsed[1]==0x20) {
|
---|
672 | /* adf */
|
---|
673 |
|
---|
674 | if (!nano91Data) {
|
---|
675 | /* adf is not crypted, so test it */
|
---|
676 |
|
---|
677 | uchar custwp;
|
---|
678 | uchar *afd;
|
---|
679 |
|
---|
680 | custwp=reader->sa[0][3];
|
---|
681 | afd=(uchar*)emmParsed+2;
|
---|
682 |
|
---|
683 | if( afd[31-custwp/8] & (1 << (custwp & 7)) )
|
---|
684 | cs_debug_mask(D_READER, "[viaccess-reader] emm for our card %08X", b2i(4, &reader->sa[0][0]));
|
---|
685 | else
|
---|
686 | return SKIPPED;
|
---|
687 | }
|
---|
688 |
|
---|
689 | // memorize
|
---|
690 | nano9EData = emmParsed;
|
---|
691 |
|
---|
692 | } else if (emmParsed[0]==0x81) {
|
---|
693 | nano81Data = emmParsed;
|
---|
694 | } else if (emmParsed[0]==0x91 && emmParsed[1]==0x08) {
|
---|
695 | nano91Data = emmParsed;
|
---|
696 | } else if (emmParsed[0]==0x92 && emmParsed[1]==0x08) {
|
---|
697 | nano92Data = emmParsed;
|
---|
698 | } else if (emmParsed[0]==0xF0 && emmParsed[1]==0x08) {
|
---|
699 | nanoF0Data = emmParsed;
|
---|
700 | } else if (emmParsed[0]==0x1D && emmParsed[0]==0x01 && emmParsed[0]==0x01) {
|
---|
701 | /* from cccam... skip it... */
|
---|
702 | } else {
|
---|
703 | /* other nanos */
|
---|
704 | show_subs(reader, emmParsed);
|
---|
705 |
|
---|
706 | memcpy(ins18Data+ins18Len, emmParsed, emmParsed[1] + 2);
|
---|
707 | ins18Len += emmParsed [1] + 2;
|
---|
708 | }
|
---|
709 | }
|
---|
710 |
|
---|
711 | if (!provider_ok) {
|
---|
712 | cs_debug_mask(D_READER, "[viaccess-reader] provider not found in emm, continue anyway");
|
---|
713 | // force key to 1...
|
---|
714 | keynr = 1;
|
---|
715 | ///return ERROR;
|
---|
716 | }
|
---|
717 |
|
---|
718 | if (!nanoF0Data) {
|
---|
719 | cs_dump(ep->emm, ep->l, "can't find 0xf0 in emm...");
|
---|
720 | return ERROR; // error
|
---|
721 | }
|
---|
722 |
|
---|
723 | if (nano9EData) {
|
---|
724 | if (!nano91Data) {
|
---|
725 | // set adf
|
---|
726 | insf0[3] = keynr; // key
|
---|
727 | insf0[4] = nano9EData[1] + 2;
|
---|
728 | write_cmd(insf0, nano9EData);
|
---|
729 | if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
730 | cs_dump(insf0, 5, "set adf cmd:");
|
---|
731 | cs_dump(nano9EData, insf0[4] , "set adf data:");
|
---|
732 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
733 | return ERROR;
|
---|
734 | }
|
---|
735 | } else {
|
---|
736 | // set adf crypte
|
---|
737 | insf4[3] = keynr; // key
|
---|
738 | insf4[4] = nano91Data[1] + 2 + nano9EData[1] + 2;
|
---|
739 | memcpy (insData, nano91Data, nano91Data[1] + 2);
|
---|
740 | memcpy (insData + nano91Data[1] + 2, nano9EData, nano9EData[1] + 2);
|
---|
741 | write_cmd(insf4, insData);
|
---|
742 | if(( cta_res[cta_lr-2]!=0x90 && cta_res[cta_lr-2]!=0x91) || cta_res[cta_lr-1]!=0x00 ) {
|
---|
743 | cs_dump(insf4, 5, "set adf encrypted cmd:");
|
---|
744 | cs_dump(insData, insf4[4], "set adf encrypted data:");
|
---|
745 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
746 | return ERROR;
|
---|
747 | }
|
---|
748 | }
|
---|
749 | }
|
---|
750 |
|
---|
751 | if (!nano92Data) {
|
---|
752 | // send subscription
|
---|
753 | ins18[2] = nano9EData ? 0x01: 0x00; // found 9E nano ?
|
---|
754 | ins18[3] = keynr; // key
|
---|
755 | ins18[4] = ins18Len + nanoF0Data[1] + 2;
|
---|
756 | memcpy (insData, ins18Data, ins18Len);
|
---|
757 | memcpy (insData + ins18Len, nanoF0Data, nanoF0Data[1] + 2);
|
---|
758 | write_cmd(ins18, insData);
|
---|
759 | if( (cta_res[cta_lr-2]==0x90 || cta_res[cta_lr-2]==0x91) && cta_res[cta_lr-1]==0x00 ) {
|
---|
760 | cs_debug_mask(D_READER, "[viaccess-reader] update successfully written");
|
---|
761 | rc=1; // written
|
---|
762 | } else {
|
---|
763 | cs_dump(ins18, 5, "set subscription cmd:");
|
---|
764 | cs_dump(insData, ins18[4], "set subscription data:");
|
---|
765 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
766 | }
|
---|
767 |
|
---|
768 | } else {
|
---|
769 | // send subscription encrypted
|
---|
770 |
|
---|
771 | if (!nano81Data) {
|
---|
772 | cs_dump(ep->emm, ep->l, "0x92 found, but can't find 0x81 in emm...");
|
---|
773 | return ERROR; // error
|
---|
774 | }
|
---|
775 |
|
---|
776 | ins1c[2] = nano9EData ? 0x01: 0x00; // found 9E nano ?
|
---|
777 | if (ep->type == UNIQUE) ins1c[2] = 0x02;
|
---|
778 | ins1c[3] = keynr; // key
|
---|
779 | ins1c[4] = nano92Data[1] + 2 + nano81Data[1] + 2 + nanoF0Data[1] + 2;
|
---|
780 | memcpy (insData, nano92Data, nano92Data[1] + 2);
|
---|
781 | memcpy (insData + nano92Data[1] + 2, nano81Data, nano81Data[1] + 2);
|
---|
782 | memcpy (insData + nano92Data[1] + 2 + nano81Data[1] + 2, nanoF0Data, nanoF0Data[1] + 2);
|
---|
783 | write_cmd(ins1c, insData);
|
---|
784 |
|
---|
785 | if( (cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0x00) ) {
|
---|
786 | cs_log("[viaccess-reader] update successfully written");
|
---|
787 | rc=1; // written
|
---|
788 | }
|
---|
789 | else {
|
---|
790 | if( cta_res[cta_lr-2]&0x1 )
|
---|
791 | cs_log("[viaccess-reader] update not written. Data already exists or unknown address");
|
---|
792 |
|
---|
793 | //if( cta_res[cta_lr-2]&0x8 ) {
|
---|
794 | write_cmd(insc8, NULL);
|
---|
795 | if( (cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0x00) ) {
|
---|
796 | cs_log("[viaccess-reader] extended status %02X %02X", cta_res[0], cta_res[1]);
|
---|
797 | }
|
---|
798 | //}
|
---|
799 | return ERROR;
|
---|
800 | }
|
---|
801 |
|
---|
802 | }
|
---|
803 |
|
---|
804 | /*
|
---|
805 | Sub Main()
|
---|
806 | Sc.Write("CA A4 04 00 03")
|
---|
807 | RX
|
---|
808 | Sc.Write("02 07 11")
|
---|
809 | RX
|
---|
810 | Sc.Write("CA F0 00 01 22")
|
---|
811 | RX
|
---|
812 | Sc.Write("9E 20")
|
---|
813 | Sc.Write("10 10 08 8A 80 00 04 00 10 10 26 E8 54 80 1E 80")
|
---|
814 | Sc.Write("00 01 00 00 00 00 00 50 00 00 80 02 22 00 08 50")
|
---|
815 | RX
|
---|
816 | Sc.Write("CA 18 01 01 11")
|
---|
817 | RX
|
---|
818 | Sc.Write("A9 05 34 DE 34 FF 80")
|
---|
819 | Sc.Write("F0 08 1A 3E AF B5 2B EE E3 3B")
|
---|
820 | RX
|
---|
821 |
|
---|
822 | End Sub
|
---|
823 | */
|
---|
824 | return rc;
|
---|
825 | }
|
---|
826 |
|
---|
827 | static int viaccess_card_info(struct s_reader * reader)
|
---|
828 | {
|
---|
829 | def_resp;
|
---|
830 | int i, l, scls, show_cls;
|
---|
831 | uchar insac[] = { 0xca, 0xac, 0x00, 0x00, 0x00 }; // select data
|
---|
832 | uchar insb8[] = { 0xca, 0xb8, 0x00, 0x00, 0x00 }; // read selected data
|
---|
833 | uchar insa4[] = { 0xca, 0xa4, 0x00, 0x00, 0x00 }; // select issuer
|
---|
834 | uchar insc0[] = { 0xca, 0xc0, 0x00, 0x00, 0x00 }; // read data item
|
---|
835 | static const uchar ins24[] = { 0xca, 0x24, 0x00, 0x00, 0x09 }; // set pin
|
---|
836 |
|
---|
837 | static const uchar cls[] = { 0x00, 0x21, 0xff, 0x9f};
|
---|
838 | static const uchar pin[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04};
|
---|
839 |
|
---|
840 | show_cls=reader->show_cls;
|
---|
841 | reader->last_geo.provid = 0;
|
---|
842 | reader->last_geo.geo_len = 0;
|
---|
843 | reader->last_geo.geo[0] = 0;
|
---|
844 |
|
---|
845 | cs_log("[viaccess-reader] card detected");
|
---|
846 |
|
---|
847 | // set pin
|
---|
848 | write_cmd(ins24, pin);
|
---|
849 |
|
---|
850 | insac[2]=0xa4; write_cmd(insac, NULL); // request unique id
|
---|
851 | insb8[4]=0x07; write_cmd(insb8, NULL); // read unique id
|
---|
852 | cs_log("[viaccess-reader] serial: %llu", b2ll(5, cta_res+2));
|
---|
853 |
|
---|
854 | scls=0;
|
---|
855 | insa4[2]=0x00; write_cmd(insa4, NULL); // select issuer 0
|
---|
856 | for (i=1; (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0); i++)
|
---|
857 | {
|
---|
858 | ulong l_provid, l_sa;
|
---|
859 | uchar l_name[64];
|
---|
860 | insc0[4]=0x1a; write_cmd(insc0, NULL); // show provider properties
|
---|
861 | cta_res[2]&=0xF0;
|
---|
862 | l_provid=b2i(3, cta_res);
|
---|
863 |
|
---|
864 | insac[2]=0xa5; write_cmd(insac, NULL); // request sa
|
---|
865 | insb8[4]=0x06; write_cmd(insb8, NULL); // read sa
|
---|
866 | l_sa=b2i(4, cta_res+2);
|
---|
867 |
|
---|
868 | insac[2]=0xa7; write_cmd(insac, NULL); // request name
|
---|
869 | insb8[4]=0x02; write_cmd(insb8, NULL); // read name nano + len
|
---|
870 | l=cta_res[1];
|
---|
871 | insb8[4]=l; write_cmd(insb8, NULL); // read name
|
---|
872 | cta_res[l]=0;
|
---|
873 | trim((char *)cta_res);
|
---|
874 | if (cta_res[0])
|
---|
875 | snprintf((char *)l_name, sizeof(l_name), ", name: %s", cta_res);
|
---|
876 | else
|
---|
877 | l_name[0]=0;
|
---|
878 |
|
---|
879 | // read GEO
|
---|
880 | insac[2]=0xa6; write_cmd(insac, NULL); // request GEO
|
---|
881 | insb8[4]=0x02; write_cmd(insb8, NULL); // read GEO nano + len
|
---|
882 | l=cta_res[1];
|
---|
883 | insb8[4]=l; write_cmd(insb8, NULL); // read geo
|
---|
884 | cs_ri_log(reader, "provider: %d, id: %06X%s, sa: %08X, geo: %s",
|
---|
885 | i, l_provid, l_name, l_sa, (l<4) ? "empty" : cs_hexdump(1, cta_res, l));
|
---|
886 |
|
---|
887 | // read classes subscription
|
---|
888 | insac[2]=0xa9; insac[4]=4;
|
---|
889 | write_cmd(insac, cls); // request class subs
|
---|
890 | scls=0;
|
---|
891 | while( (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0) )
|
---|
892 | {
|
---|
893 | insb8[4]=0x02; write_cmd(insb8, NULL); // read class subs nano + len
|
---|
894 | if( (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0) )
|
---|
895 | {
|
---|
896 | int fshow;
|
---|
897 | l=cta_res[1];
|
---|
898 | //fshow=(client[cs_idx].dbglvl==D_DUMP)?1:(scls < show_cls)?1:0;
|
---|
899 | fshow=(scls<show_cls);
|
---|
900 | insb8[4]=l; write_cmd(insb8, NULL); // read class subs
|
---|
901 | if( (cta_res[cta_lr-2]==0x90) && (fshow) &&
|
---|
902 | (cta_res[cta_lr-1]==0x00 || cta_res[cta_lr-1]==0x08) )
|
---|
903 | {
|
---|
904 | show_class(reader, NULL, cta_res, cta_lr-2);
|
---|
905 | scls++;
|
---|
906 | }
|
---|
907 | }
|
---|
908 | }
|
---|
909 |
|
---|
910 | insac[4]=0;
|
---|
911 | insa4[2]=0x02;
|
---|
912 | write_cmd(insa4, NULL); // select next provider
|
---|
913 | }
|
---|
914 | //return ERROR;
|
---|
915 | return OK;
|
---|
916 | }
|
---|
917 |
|
---|
918 | #ifdef HAVE_DVBAPI
|
---|
919 | int viaccess_reassemble_emm(uchar *buffer, uint *len) {
|
---|
920 | static uchar emm_global[512];
|
---|
921 | static int emm_global_len = 0;
|
---|
922 |
|
---|
923 | int pos, emm_len = 0;
|
---|
924 | uint k;
|
---|
925 | uchar emmbuf[512];
|
---|
926 |
|
---|
927 | // Viaccess
|
---|
928 | if (*len>500) return 0;
|
---|
929 |
|
---|
930 | switch(buffer[0]) {
|
---|
931 | case 0x8c:
|
---|
932 | case 0x8d:
|
---|
933 | // emm-s part 1
|
---|
934 | if (!memcmp(emm_global, buffer, *len))
|
---|
935 | return 0;
|
---|
936 |
|
---|
937 | // copy first part of the emm-s
|
---|
938 | memcpy(emm_global, buffer, *len);
|
---|
939 | emm_global_len=*len;
|
---|
940 | //cs_ddump_mask(D_READER, buffer, len, "viaccess global emm:");
|
---|
941 | return 0;
|
---|
942 |
|
---|
943 | case 0x8e:
|
---|
944 | // emm-s part 2
|
---|
945 | if (!emm_global_len) return 0;
|
---|
946 |
|
---|
947 | //copy emm-s header (including sa)
|
---|
948 | memcpy(emmbuf, buffer, 7);
|
---|
949 | pos=7;
|
---|
950 |
|
---|
951 | //copy emm-gh
|
---|
952 | memcpy(emmbuf+pos, emm_global+3, emm_global_len-3);
|
---|
953 | pos=pos+emm_global_len-3;
|
---|
954 |
|
---|
955 | //determine whether fixed or variable length emm-s
|
---|
956 | //if no f0 08 nano is found it has fixed length
|
---|
957 | int fixed = 1;
|
---|
958 | for (k=0; k<(*len); k++) {
|
---|
959 | if (buffer[k]==0xf0 && (k+1) < *len && buffer[k+1] == 0x08) {
|
---|
960 | cs_debug_mask(D_DVBAPI, "viaccess_reassemble_emm: found variable emm");
|
---|
961 | fixed = 0;
|
---|
962 | }
|
---|
963 | }
|
---|
964 |
|
---|
965 | //fixed emm-s
|
---|
966 | if (fixed) {
|
---|
967 | cs_debug_mask(D_DVBAPI, "viaccess_reassemble_emm: found fixed emm");
|
---|
968 | //add 9E 20 nano + first 32 bytes of emm content
|
---|
969 | memcpy(emmbuf+pos, "\x9E\x20", 2);
|
---|
970 | memcpy(emmbuf+pos+2, buffer+7, 32);
|
---|
971 | pos+=34;
|
---|
972 |
|
---|
973 | //add F0 08 nano + 8 subsequent bytes of emm content
|
---|
974 | memcpy(emmbuf+pos, "\xF0\x08", 2);
|
---|
975 | memcpy(emmbuf+pos+2, buffer+39, 8);
|
---|
976 | pos+=10;
|
---|
977 | }
|
---|
978 | //variable emm-s
|
---|
979 | else {
|
---|
980 | //copy emm-s as is
|
---|
981 | memcpy(emmbuf+pos, buffer+7, *len);
|
---|
982 | pos+=*len;
|
---|
983 | }
|
---|
984 |
|
---|
985 | //calculate emm length and set it on position 2
|
---|
986 | emm_len=pos;
|
---|
987 | emmbuf[2]=emm_len-3;
|
---|
988 |
|
---|
989 | //cs_ddump_mask(D_READER, buffer, *len, "original emm:");
|
---|
990 | cs_debug_mask(D_DVBAPI, "viaccess_reassemble_emm: emm-gh ? %s", cs_hexdump(1, emm_global, emm_global_len));
|
---|
991 | cs_debug_mask(D_DVBAPI, "viaccess_reassemble_emm: emm-s ? %s", cs_hexdump(1, buffer, *len));
|
---|
992 | cs_debug_mask(D_DVBAPI, "viaccess_reassemble_emm: assembled emm ? %s", cs_hexdump(1, emmbuf, emm_len));
|
---|
993 |
|
---|
994 | //place assembled emm
|
---|
995 | memcpy(buffer, emmbuf, emm_len);
|
---|
996 | *len=emm_len;
|
---|
997 | break;
|
---|
998 | }
|
---|
999 | return 1;
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | #endif
|
---|
1003 |
|
---|
1004 | void reader_viaccess(struct s_cardsystem *ph)
|
---|
1005 | {
|
---|
1006 | ph->do_emm=viaccess_do_emm;
|
---|
1007 | ph->do_ecm=viaccess_do_ecm;
|
---|
1008 | ph->card_info=viaccess_card_info;
|
---|
1009 | ph->card_init=viaccess_card_init;
|
---|
1010 | ph->get_emm_type=viaccess_get_emm_type;
|
---|
1011 | ph->get_emm_filter=viaccess_get_emm_filter;
|
---|
1012 | ph->caids[0]=0x05;
|
---|
1013 | ph->desc="viaccess";
|
---|
1014 | }
|
---|