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[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=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, 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 | snprintf((char *)buf+strlen((char *)buf), sizeof(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 && ( ecm88Data[nanoLen-1] != 0x01)) {
|
---|
367 | keynr=ecm88Data[5];
|
---|
368 | cs_debug_mask(D_READER, "keyToUse = %02x, ECM ending with %02x",ecm88Data[5], ecm88Data[nanoLen-1]);
|
---|
369 | } else {
|
---|
370 | cs_debug_mask(D_READER, "Skip ECM ending with = %02x for ecm number (%x)",ecm88Data[nanoLen-1], curnumber_ecm);
|
---|
371 | ecm88Data=nextEcm;
|
---|
372 | ecm88Len-=curEcm88len;
|
---|
373 | continue; //loop to next ecm
|
---|
374 | }
|
---|
375 | }
|
---|
376 | else { // long ecm but we don't have an ecm number so we have to try them all.
|
---|
377 | keynr=ecm88Data[5];
|
---|
378 | cs_debug_mask(D_READER, "keyToUse = %02x",ecm88Data[5]);
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | memcpy (ident, &ecm88Data[2], sizeof(ident));
|
---|
383 | provid = b2i(3, ident);
|
---|
384 | ident[2]&=0xF0;
|
---|
385 |
|
---|
386 | if(hasD2 && reader->aes_list) {
|
---|
387 | // check that we have the AES key to decode the CW
|
---|
388 | // if not there is no need to send the ecm to the card
|
---|
389 | if(!aes_present(reader->aes_list, 0x500, (uint32) (provid & 0xFFFFF0) , D2KeyID))
|
---|
390 | return ERROR;
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | if (!chk_prov(reader, ident, keynr))
|
---|
395 | {
|
---|
396 | cs_debug_mask(D_READER, "[viaccess-reader] ECM: provider or key not found on card");
|
---|
397 | snprintf( er->msglog, MSGLOGSIZE, "provider(%02x%02x%02x) or key(%d) not found on card", ident[0],ident[1],ident[2], keynr );
|
---|
398 | return ERROR;
|
---|
399 | }
|
---|
400 |
|
---|
401 | ecm88Data+=nanoLen;
|
---|
402 | ecm88Len-=nanoLen;
|
---|
403 | curEcm88len-=nanoLen;
|
---|
404 |
|
---|
405 | // DE04
|
---|
406 | if (ecm88Data[0]==0xDE && ecm88Data[1]==0x04)
|
---|
407 | {
|
---|
408 | memcpy (DE04, &ecm88Data[0], 6);
|
---|
409 | ecm88Data+=6;
|
---|
410 | }
|
---|
411 | //
|
---|
412 |
|
---|
413 | if( reader->last_geo.provid != provid )
|
---|
414 | {
|
---|
415 | reader->last_geo.provid = provid;
|
---|
416 | reader->last_geo.geo_len = 0;
|
---|
417 | reader->last_geo.geo[0] = 0;
|
---|
418 | write_cmd(insa4, ident); // set provider
|
---|
419 | }
|
---|
420 |
|
---|
421 | //Nano D2 0x0b Pre AES decrypt CW
|
---|
422 | if ( hasD2 && nanoD2 == 1)
|
---|
423 | {
|
---|
424 | uchar *ecm88DataCW = ecm88Data;
|
---|
425 | int cwStart = 0;
|
---|
426 | //int cwStartRes = 0;
|
---|
427 | int exit = 0;
|
---|
428 | // find CW start
|
---|
429 | while(cwStart < curEcm88len -1 && !exit)
|
---|
430 | {
|
---|
431 | if(ecm88Data[cwStart] == 0xEA && ecm88Data[cwStart+1] == 0x10)
|
---|
432 | {
|
---|
433 | ecm88DataCW = ecm88DataCW + cwStart + 2;
|
---|
434 | exit = 1;
|
---|
435 | }
|
---|
436 | cwStart++;
|
---|
437 | }
|
---|
438 | // use AES from list to decrypt CW
|
---|
439 | cs_debug_mask(D_READER, "Decoding CW : using AES key id %d for provider %06x",D2KeyID, (provid & 0xFFFFF0));
|
---|
440 | if (aes_decrypt_from_list(reader->aes_list,0x500, (uint32) (provid & 0xFFFFF0), D2KeyID, &ecm88DataCW[0], 16) == 0)
|
---|
441 | snprintf( er->msglog, MSGLOGSIZE, "AES Decrypt : key id %d not found for CAID %04X , provider %06lx", D2KeyID, 0x500, (provid & 0xFFFFF0) );
|
---|
442 | }
|
---|
443 |
|
---|
444 | while(ecm88Len>0 && ecm88Data[0]<0xA0)
|
---|
445 | {
|
---|
446 | int nanoLen=ecm88Data[1]+2;
|
---|
447 | if (!ecmf8Data)
|
---|
448 | ecmf8Data=(uchar *)ecm88Data;
|
---|
449 | ecmf8Len+=nanoLen;
|
---|
450 | ecm88Len-=nanoLen;
|
---|
451 | curEcm88len-=nanoLen;
|
---|
452 | ecm88Data+=nanoLen;
|
---|
453 | }
|
---|
454 | if(ecmf8Len)
|
---|
455 | {
|
---|
456 | if( reader->last_geo.geo_len!=ecmf8Len ||
|
---|
457 | memcmp(reader->last_geo.geo, ecmf8Data, reader->last_geo.geo_len))
|
---|
458 | {
|
---|
459 | memcpy(reader->last_geo.geo, ecmf8Data, ecmf8Len);
|
---|
460 | reader->last_geo.geo_len= ecmf8Len;
|
---|
461 | insf8[3]=keynr;
|
---|
462 | insf8[4]=ecmf8Len;
|
---|
463 | write_cmd(insf8, ecmf8Data);
|
---|
464 | }
|
---|
465 | }
|
---|
466 | ins88[2]=ecmf8Len?1:0;
|
---|
467 | ins88[3]=keynr;
|
---|
468 | ins88[4]= curEcm88len;
|
---|
469 | //
|
---|
470 | // we should check the nano to make sure the ecm is valid
|
---|
471 | // we should look for at least 1 E3 nano, 1 EA nano and the F0 signature nano
|
---|
472 | //
|
---|
473 | // DE04
|
---|
474 | if (DE04[0]==0xDE)
|
---|
475 | {
|
---|
476 | memcpy(DE04+6, (uchar *)ecm88Data, curEcm88len-6);
|
---|
477 | write_cmd(ins88, DE04); // request dcw
|
---|
478 | }
|
---|
479 | else
|
---|
480 | {
|
---|
481 | write_cmd(ins88, (uchar *)ecm88Data); // request dcw
|
---|
482 | }
|
---|
483 | //
|
---|
484 | write_cmd(insc0, NULL); // read dcw
|
---|
485 | switch(cta_res[0])
|
---|
486 | {
|
---|
487 | case 0xe8: // even
|
---|
488 | if(cta_res[1]==8) { memcpy(er->cw,cta_res+2,8); rc=1; }
|
---|
489 | break;
|
---|
490 | case 0xe9: // odd
|
---|
491 | if(cta_res[1]==8) { memcpy(er->cw+8,cta_res+2,8); rc=1; }
|
---|
492 | break;
|
---|
493 | case 0xea: // complete
|
---|
494 | if(cta_res[1]==16) { memcpy(er->cw,cta_res+2,16); rc=1; }
|
---|
495 | break;
|
---|
496 | default :
|
---|
497 | ecm88Data=nextEcm;
|
---|
498 | ecm88Len-=curEcm88len;
|
---|
499 | cs_debug_mask(D_READER, "[viaccess-reader] ECM: key to use is not the current one, trying next ECM");
|
---|
500 | snprintf( er->msglog, MSGLOGSIZE, "key to use is not the current one, trying next ECM" );
|
---|
501 | }
|
---|
502 | }
|
---|
503 | else {
|
---|
504 | ecm88Data=nextEcm;
|
---|
505 | ecm88Len-=curEcm88len;
|
---|
506 | cs_debug_mask(D_READER, "[viaccess-reader] ECM: Unknown ECM type");
|
---|
507 | snprintf( er->msglog, MSGLOGSIZE, "Unknown ECM type" );
|
---|
508 | }
|
---|
509 | }
|
---|
510 |
|
---|
511 | if ( hasD2 && !check_crc(er->cw) && nanoD2 == 2) {
|
---|
512 | cs_debug_mask(D_READER, "Decoding CW : using AES key id %d for provider %06x",D2KeyID, (provid & 0xFFFFF0));
|
---|
513 | rc=aes_decrypt_from_list(reader->aes_list,0x500, (uint32) (provid & 0xFFFFF0), D2KeyID,er->cw, 16);
|
---|
514 | if( rc == 0 )
|
---|
515 | snprintf( er->msglog, MSGLOGSIZE, "AES Decrypt : key id %d not found for CAID %04X , provider %06lx", D2KeyID, 0x500, (provid & 0xFFFFF0) );
|
---|
516 | }
|
---|
517 |
|
---|
518 | return(rc?OK:ERROR);
|
---|
519 | }
|
---|
520 |
|
---|
521 | static int viaccess_get_emm_type(EMM_PACKET *ep, struct s_reader * rdr)
|
---|
522 | {
|
---|
523 | ulong provid=0;
|
---|
524 | cs_debug_mask(D_EMM, "Entered viaccess_get_emm_type ep->emm[0]=%02x",ep->emm[0]);
|
---|
525 |
|
---|
526 | if (ep->emm[3] == 0x90 && ep->emm[4] == 0x03) {
|
---|
527 | provid = ep->emm[5] << 16 | ep->emm[6] << 8 | (ep->emm[7] & 0xFE);
|
---|
528 | memcpy(ep->provid, i2b(4, provid), 4);
|
---|
529 | }
|
---|
530 |
|
---|
531 | switch (ep->emm[0]) {
|
---|
532 | case 0x88:
|
---|
533 | ep->type=UNIQUE;
|
---|
534 | memset(ep->hexserial, 0, 8);
|
---|
535 | memcpy(ep->hexserial, ep->emm + 4, 4);
|
---|
536 | cs_debug_mask(D_EMM, "VIACCESS EMM: UNIQUE");
|
---|
537 | return(!memcmp(rdr->hexserial + 1, ep->hexserial, 4));
|
---|
538 |
|
---|
539 | case 0x8A:
|
---|
540 | case 0x8B:
|
---|
541 | ep->type=GLOBAL;
|
---|
542 | cs_debug_mask(D_EMM, "VIACCESS EMM: GLOBAL");
|
---|
543 | return TRUE;
|
---|
544 |
|
---|
545 | case 0x8C:
|
---|
546 | case 0x8D:
|
---|
547 | ep->type=SHARED;
|
---|
548 | cs_debug_mask(D_EMM, "VIACCESS EMM: SHARED (part)");
|
---|
549 | return FALSE;
|
---|
550 |
|
---|
551 | case 0x8E:
|
---|
552 | ep->type=SHARED;
|
---|
553 | memset(ep->hexserial, 0, 8);
|
---|
554 | memcpy(ep->hexserial, ep->emm + 3, 3);
|
---|
555 | cs_debug_mask(D_EMM, "VIACCESS EMM: SHARED");
|
---|
556 | return(!memcmp(&rdr->sa[0][0], ep->hexserial, 3));
|
---|
557 |
|
---|
558 | default:
|
---|
559 | ep->type = UNKNOWN;
|
---|
560 | cs_debug_mask(D_EMM, "VIACCESS EMM: UNKNOWN");
|
---|
561 | return TRUE;
|
---|
562 | }
|
---|
563 | }
|
---|
564 |
|
---|
565 | static void viaccess_get_emm_filter(struct s_reader * rdr, uchar *filter)
|
---|
566 | {
|
---|
567 | int idx = 2;
|
---|
568 |
|
---|
569 | filter[0]=0xFF;
|
---|
570 | filter[1]=0;
|
---|
571 |
|
---|
572 | filter[idx++]=EMM_GLOBAL;
|
---|
573 | filter[idx++]=0;
|
---|
574 | filter[idx+0] = 0x8D;
|
---|
575 | filter[idx+0+16] = 0xFE;
|
---|
576 | //filter[idx+6] = 0xA0; // FIXME: dummy, flood client with EMM's
|
---|
577 | //filter[idx+6+16] = 0xF0;
|
---|
578 | filter[1]++;
|
---|
579 | idx += 32;
|
---|
580 |
|
---|
581 | filter[idx++]=EMM_SHARED;
|
---|
582 | filter[idx++]=0;
|
---|
583 | filter[idx+0] = 0x8E;
|
---|
584 | filter[idx+0+16] = 0xFF;
|
---|
585 | memcpy(filter+idx+1, &rdr->sa[0][0], 3);
|
---|
586 | memset(filter+idx+1+16, 0xFF, 3);
|
---|
587 | filter[1]++;
|
---|
588 | idx += 32;
|
---|
589 |
|
---|
590 | filter[idx++]=EMM_UNIQUE;
|
---|
591 | filter[idx++]=0;
|
---|
592 | filter[idx+0] = 0x88;
|
---|
593 | filter[idx+0+16] = 0xFF;
|
---|
594 | memcpy(filter+idx+1, rdr->hexserial + 1, 4);
|
---|
595 | memset(filter+idx+1+16, 0xFF, 4);
|
---|
596 | filter[1]++;
|
---|
597 | idx += 32;
|
---|
598 |
|
---|
599 | return;
|
---|
600 | }
|
---|
601 |
|
---|
602 | static int viaccess_do_emm(struct s_reader * reader, EMM_PACKET *ep)
|
---|
603 | {
|
---|
604 | def_resp;
|
---|
605 | static const unsigned char insa4[] = { 0xca,0xa4,0x04,0x00,0x03 }; // set provider id
|
---|
606 | unsigned char insf0[] = { 0xca,0xf0,0x00,0x01,0x22 }; // set adf
|
---|
607 | unsigned char insf4[] = { 0xca,0xf4,0x00,0x01,0x00 }; // set adf, encrypted
|
---|
608 | unsigned char ins18[] = { 0xca,0x18,0x01,0x01,0x00 }; // set subscription
|
---|
609 | unsigned char ins1c[] = { 0xca,0x1c,0x01,0x01,0x00 }; // set subscription, encrypted
|
---|
610 | static const unsigned char insc8[] = { 0xca,0xc8,0x00,0x00,0x02 }; // read extended status
|
---|
611 | // static const unsigned char insc8Data[] = { 0x00,0x00 }; // data for read extended status
|
---|
612 |
|
---|
613 | int emmdatastart=7;
|
---|
614 | if (ep->type == UNIQUE) emmdatastart++;
|
---|
615 | int emmLen=SCT_LEN(ep->emm)-emmdatastart;
|
---|
616 | int rc=0;
|
---|
617 |
|
---|
618 | ///cs_dump(ep->emm, emmLen+emmdatastart, "RECEIVED EMM VIACCESS");
|
---|
619 |
|
---|
620 | int emmUpToEnd;
|
---|
621 | uchar *emmParsed = ep->emm+emmdatastart;
|
---|
622 | int provider_ok = 0;
|
---|
623 | unsigned int emm_provid;
|
---|
624 | uchar keynr = 0;
|
---|
625 | int ins18Len = 0;
|
---|
626 | uchar ins18Data[512];
|
---|
627 | uchar insData[512];
|
---|
628 | uchar *nano81Data = 0;
|
---|
629 | uchar *nano91Data = 0;
|
---|
630 | uchar *nano92Data = 0;
|
---|
631 | uchar *nano9EData = 0;
|
---|
632 | uchar *nanoF0Data = 0;
|
---|
633 |
|
---|
634 | for (emmUpToEnd=emmLen; (emmParsed[1] != 0) && (emmUpToEnd > 0); emmUpToEnd -= (2 + emmParsed[1]), emmParsed += (2 + emmParsed[1])) {
|
---|
635 | ///cs_dump (emmParsed, emmParsed[1] + 2, "NANO");
|
---|
636 |
|
---|
637 | if (emmParsed[0]==0x90 && emmParsed[1]==0x03) {
|
---|
638 | /* identification of the service operator */
|
---|
639 |
|
---|
640 | uchar soid[3], ident[3], i;
|
---|
641 |
|
---|
642 | for (i=0; i<3; i++) {
|
---|
643 | soid[i]=ident[i]=emmParsed[2+i];
|
---|
644 | }
|
---|
645 | ident[2]&=0xF0;
|
---|
646 | emm_provid=b2i(3, ident);
|
---|
647 | keynr=soid[2]&0x0F;
|
---|
648 | if (chk_prov(reader, ident, keynr)) {
|
---|
649 | provider_ok = 1;
|
---|
650 | } else {
|
---|
651 | cs_debug_mask(D_READER, "[viaccess-reader] EMM: provider or key not found on card (%x, %x)", ident, keynr);
|
---|
652 | cs_log("[viaccess-reader] EMM: provider or key not found on card (%x, %x)", ident, keynr);
|
---|
653 | return ERROR;
|
---|
654 | }
|
---|
655 |
|
---|
656 | // check if the provider changes. If yes, set the new one. If not, don't .. card will return an error if we do.
|
---|
657 | if( reader->last_geo.provid != emm_provid ) {
|
---|
658 | write_cmd(insa4, ident);
|
---|
659 | if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
660 | cs_dump(insa4, 5, "set provider cmd:");
|
---|
661 | cs_dump(soid, 3, "set provider data:");
|
---|
662 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
663 | return ERROR;
|
---|
664 | }
|
---|
665 | }
|
---|
666 | // as we are maybe changing the used provider, clear the cache, so the next ecm will re-select the correct one
|
---|
667 | reader->last_geo.provid = 0;
|
---|
668 | reader->last_geo.geo_len = 0;
|
---|
669 | reader->last_geo.geo[0] = 0;
|
---|
670 |
|
---|
671 | }
|
---|
672 | else if (emmParsed[0]==0x9e && emmParsed[1]==0x20) {
|
---|
673 | /* adf */
|
---|
674 |
|
---|
675 | if (!nano91Data) {
|
---|
676 | /* adf is not crypted, so test it */
|
---|
677 |
|
---|
678 | uchar custwp;
|
---|
679 | uchar *afd;
|
---|
680 |
|
---|
681 | custwp=reader->sa[0][3];
|
---|
682 | afd=(uchar*)emmParsed+2;
|
---|
683 |
|
---|
684 | if( afd[31-custwp/8] & (1 << (custwp & 7)) )
|
---|
685 | cs_debug_mask(D_READER, "[viaccess-reader] emm for our card %08X", b2i(4, &reader->sa[0][0]));
|
---|
686 | else
|
---|
687 | return SKIPPED;
|
---|
688 | }
|
---|
689 |
|
---|
690 | // memorize
|
---|
691 | nano9EData = emmParsed;
|
---|
692 |
|
---|
693 | } else if (emmParsed[0]==0x81) {
|
---|
694 | nano81Data = emmParsed;
|
---|
695 | } else if (emmParsed[0]==0x91 && emmParsed[1]==0x08) {
|
---|
696 | nano91Data = emmParsed;
|
---|
697 | } else if (emmParsed[0]==0x92 && emmParsed[1]==0x08) {
|
---|
698 | nano92Data = emmParsed;
|
---|
699 | } else if (emmParsed[0]==0xF0 && emmParsed[1]==0x08) {
|
---|
700 | nanoF0Data = emmParsed;
|
---|
701 | } else if (emmParsed[0]==0x1D && emmParsed[0]==0x01 && emmParsed[0]==0x01) {
|
---|
702 | /* from cccam... skip it... */
|
---|
703 | } else {
|
---|
704 | /* other nanos */
|
---|
705 | show_subs(reader, emmParsed);
|
---|
706 |
|
---|
707 | memcpy(ins18Data+ins18Len, emmParsed, emmParsed[1] + 2);
|
---|
708 | ins18Len += emmParsed [1] + 2;
|
---|
709 | }
|
---|
710 | }
|
---|
711 |
|
---|
712 | if (!provider_ok) {
|
---|
713 | cs_debug_mask(D_READER, "[viaccess-reader] provider not found in emm, continue anyway");
|
---|
714 | // force key to 1...
|
---|
715 | keynr = 1;
|
---|
716 | ///return ERROR;
|
---|
717 | }
|
---|
718 |
|
---|
719 | if (!nanoF0Data) {
|
---|
720 | cs_dump(ep->emm, ep->l, "can't find 0xf0 in emm...");
|
---|
721 | return ERROR; // error
|
---|
722 | }
|
---|
723 |
|
---|
724 | if (nano9EData) {
|
---|
725 | if (!nano91Data) {
|
---|
726 | // set adf
|
---|
727 | insf0[3] = keynr; // key
|
---|
728 | insf0[4] = nano9EData[1] + 2;
|
---|
729 | write_cmd(insf0, nano9EData);
|
---|
730 | if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
731 | cs_dump(insf0, 5, "set adf cmd:");
|
---|
732 | cs_dump(nano9EData, insf0[4] , "set adf data:");
|
---|
733 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
734 | return ERROR;
|
---|
735 | }
|
---|
736 | } else {
|
---|
737 | // set adf crypte
|
---|
738 | insf4[3] = keynr; // key
|
---|
739 | insf4[4] = nano91Data[1] + 2 + nano9EData[1] + 2;
|
---|
740 | memcpy (insData, nano91Data, nano91Data[1] + 2);
|
---|
741 | memcpy (insData + nano91Data[1] + 2, nano9EData, nano9EData[1] + 2);
|
---|
742 | write_cmd(insf4, insData);
|
---|
743 | if(( cta_res[cta_lr-2]!=0x90 && cta_res[cta_lr-2]!=0x91) || cta_res[cta_lr-1]!=0x00 ) {
|
---|
744 | cs_dump(insf4, 5, "set adf encrypted cmd:");
|
---|
745 | cs_dump(insData, insf4[4], "set adf encrypted data:");
|
---|
746 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
747 | return ERROR;
|
---|
748 | }
|
---|
749 | }
|
---|
750 | }
|
---|
751 |
|
---|
752 | if (!nano92Data) {
|
---|
753 | // send subscription
|
---|
754 | ins18[2] = nano9EData ? 0x01: 0x00; // found 9E nano ?
|
---|
755 | ins18[3] = keynr; // key
|
---|
756 | ins18[4] = ins18Len + nanoF0Data[1] + 2;
|
---|
757 | memcpy (insData, ins18Data, ins18Len);
|
---|
758 | memcpy (insData + ins18Len, nanoF0Data, nanoF0Data[1] + 2);
|
---|
759 | write_cmd(ins18, insData);
|
---|
760 | if( (cta_res[cta_lr-2]==0x90 || cta_res[cta_lr-2]==0x91) && cta_res[cta_lr-1]==0x00 ) {
|
---|
761 | cs_debug_mask(D_READER, "[viaccess-reader] update successfully written");
|
---|
762 | rc=1; // written
|
---|
763 | } else {
|
---|
764 | cs_dump(ins18, 5, "set subscription cmd:");
|
---|
765 | cs_dump(insData, ins18[4], "set subscription data:");
|
---|
766 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
767 | }
|
---|
768 |
|
---|
769 | } else {
|
---|
770 | // send subscription encrypted
|
---|
771 |
|
---|
772 | if (!nano81Data) {
|
---|
773 | cs_dump(ep->emm, ep->l, "0x92 found, but can't find 0x81 in emm...");
|
---|
774 | return ERROR; // error
|
---|
775 | }
|
---|
776 |
|
---|
777 | ins1c[2] = nano9EData ? 0x01: 0x00; // found 9E nano ?
|
---|
778 | if (ep->type == UNIQUE) ins1c[2] = 0x02;
|
---|
779 | ins1c[3] = keynr; // key
|
---|
780 | ins1c[4] = nano92Data[1] + 2 + nano81Data[1] + 2 + nanoF0Data[1] + 2;
|
---|
781 | memcpy (insData, nano92Data, nano92Data[1] + 2);
|
---|
782 | memcpy (insData + nano92Data[1] + 2, nano81Data, nano81Data[1] + 2);
|
---|
783 | memcpy (insData + nano92Data[1] + 2 + nano81Data[1] + 2, nanoF0Data, nanoF0Data[1] + 2);
|
---|
784 | write_cmd(ins1c, insData);
|
---|
785 |
|
---|
786 | if( (cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0x00) ) {
|
---|
787 | cs_log("[viaccess-reader] update successfully written");
|
---|
788 | rc=1; // written
|
---|
789 | }
|
---|
790 | else {
|
---|
791 | if( cta_res[cta_lr-2]&0x1 )
|
---|
792 | cs_log("[viaccess-reader] update not written. Data already exists or unknown address");
|
---|
793 |
|
---|
794 | //if( cta_res[cta_lr-2]&0x8 ) {
|
---|
795 | write_cmd(insc8, NULL);
|
---|
796 | if( (cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0x00) ) {
|
---|
797 | cs_log("[viaccess-reader] extended status %02X %02X", cta_res[0], cta_res[1]);
|
---|
798 | }
|
---|
799 | //}
|
---|
800 | return ERROR;
|
---|
801 | }
|
---|
802 |
|
---|
803 | }
|
---|
804 |
|
---|
805 | /*
|
---|
806 | Sub Main()
|
---|
807 | Sc.Write("CA A4 04 00 03")
|
---|
808 | RX
|
---|
809 | Sc.Write("02 07 11")
|
---|
810 | RX
|
---|
811 | Sc.Write("CA F0 00 01 22")
|
---|
812 | RX
|
---|
813 | Sc.Write("9E 20")
|
---|
814 | Sc.Write("10 10 08 8A 80 00 04 00 10 10 26 E8 54 80 1E 80")
|
---|
815 | Sc.Write("00 01 00 00 00 00 00 50 00 00 80 02 22 00 08 50")
|
---|
816 | RX
|
---|
817 | Sc.Write("CA 18 01 01 11")
|
---|
818 | RX
|
---|
819 | Sc.Write("A9 05 34 DE 34 FF 80")
|
---|
820 | Sc.Write("F0 08 1A 3E AF B5 2B EE E3 3B")
|
---|
821 | RX
|
---|
822 |
|
---|
823 | End Sub
|
---|
824 | */
|
---|
825 | return rc;
|
---|
826 | }
|
---|
827 |
|
---|
828 | static int viaccess_card_info(struct s_reader * reader)
|
---|
829 | {
|
---|
830 | def_resp;
|
---|
831 | int i, l, scls, show_cls;
|
---|
832 | uchar insac[] = { 0xca, 0xac, 0x00, 0x00, 0x00 }; // select data
|
---|
833 | uchar insb8[] = { 0xca, 0xb8, 0x00, 0x00, 0x00 }; // read selected data
|
---|
834 | uchar insa4[] = { 0xca, 0xa4, 0x00, 0x00, 0x00 }; // select issuer
|
---|
835 | uchar insc0[] = { 0xca, 0xc0, 0x00, 0x00, 0x00 }; // read data item
|
---|
836 | static const uchar ins24[] = { 0xca, 0x24, 0x00, 0x00, 0x09 }; // set pin
|
---|
837 |
|
---|
838 | static const uchar cls[] = { 0x00, 0x21, 0xff, 0x9f};
|
---|
839 | static const uchar pin[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04};
|
---|
840 |
|
---|
841 | show_cls=reader->show_cls;
|
---|
842 | reader->last_geo.provid = 0;
|
---|
843 | reader->last_geo.geo_len = 0;
|
---|
844 | reader->last_geo.geo[0] = 0;
|
---|
845 |
|
---|
846 | cs_log("[viaccess-reader] card detected");
|
---|
847 |
|
---|
848 | // set pin
|
---|
849 | write_cmd(ins24, pin);
|
---|
850 |
|
---|
851 | insac[2]=0xa4; write_cmd(insac, NULL); // request unique id
|
---|
852 | insb8[4]=0x07; write_cmd(insb8, NULL); // read unique id
|
---|
853 | cs_log("[viaccess-reader] serial: %llu", b2ll(5, cta_res+2));
|
---|
854 |
|
---|
855 | scls=0;
|
---|
856 | insa4[2]=0x00; write_cmd(insa4, NULL); // select issuer 0
|
---|
857 | for (i=1; (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0); i++)
|
---|
858 | {
|
---|
859 | ulong l_provid, l_sa;
|
---|
860 | uchar l_name[64];
|
---|
861 | insc0[4]=0x1a; write_cmd(insc0, NULL); // show provider properties
|
---|
862 | cta_res[2]&=0xF0;
|
---|
863 | l_provid=b2i(3, cta_res);
|
---|
864 |
|
---|
865 | insac[2]=0xa5; write_cmd(insac, NULL); // request sa
|
---|
866 | insb8[4]=0x06; write_cmd(insb8, NULL); // read sa
|
---|
867 | l_sa=b2i(4, cta_res+2);
|
---|
868 |
|
---|
869 | insac[2]=0xa7; write_cmd(insac, NULL); // request name
|
---|
870 | insb8[4]=0x02; write_cmd(insb8, NULL); // read name nano + len
|
---|
871 | l=cta_res[1];
|
---|
872 | insb8[4]=l; write_cmd(insb8, NULL); // read name
|
---|
873 | cta_res[l]=0;
|
---|
874 | trim((char *)cta_res);
|
---|
875 | if (cta_res[0])
|
---|
876 | snprintf((char *)l_name, sizeof(l_name), ", name: %s", cta_res);
|
---|
877 | else
|
---|
878 | l_name[0]=0;
|
---|
879 |
|
---|
880 | // read GEO
|
---|
881 | insac[2]=0xa6; write_cmd(insac, NULL); // request GEO
|
---|
882 | insb8[4]=0x02; write_cmd(insb8, NULL); // read GEO nano + len
|
---|
883 | l=cta_res[1];
|
---|
884 | insb8[4]=l; write_cmd(insb8, NULL); // read geo
|
---|
885 | cs_ri_log(reader, "provider: %d, id: %06X%s, sa: %08X, geo: %s",
|
---|
886 | i, l_provid, l_name, l_sa, (l<4) ? "empty" : cs_hexdump(1, cta_res, l));
|
---|
887 |
|
---|
888 | // read classes subscription
|
---|
889 | insac[2]=0xa9; insac[4]=4;
|
---|
890 | write_cmd(insac, cls); // request class subs
|
---|
891 | scls=0;
|
---|
892 | while( (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0) )
|
---|
893 | {
|
---|
894 | insb8[4]=0x02; write_cmd(insb8, NULL); // read class subs nano + len
|
---|
895 | if( (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0) )
|
---|
896 | {
|
---|
897 | int fshow;
|
---|
898 | l=cta_res[1];
|
---|
899 | //fshow=(client[cs_idx].dbglvl==D_DUMP)?1:(scls < show_cls)?1:0;
|
---|
900 | fshow=(scls<show_cls);
|
---|
901 | insb8[4]=l; write_cmd(insb8, NULL); // read class subs
|
---|
902 | if( (cta_res[cta_lr-2]==0x90) && (fshow) &&
|
---|
903 | (cta_res[cta_lr-1]==0x00 || cta_res[cta_lr-1]==0x08) )
|
---|
904 | {
|
---|
905 | show_class(reader, NULL, cta_res, cta_lr-2);
|
---|
906 | scls++;
|
---|
907 | }
|
---|
908 | }
|
---|
909 | }
|
---|
910 |
|
---|
911 | insac[4]=0;
|
---|
912 | insa4[2]=0x02;
|
---|
913 | write_cmd(insa4, NULL); // select next provider
|
---|
914 | }
|
---|
915 | //return ERROR;
|
---|
916 | return OK;
|
---|
917 | }
|
---|
918 |
|
---|
919 | #ifdef HAVE_DVBAPI
|
---|
920 | void dvbapi_sort_nanos(unsigned char *dest, const unsigned char *src, int len);
|
---|
921 |
|
---|
922 | int viaccess_reassemble_emm(uchar *buffer, uint *len) {
|
---|
923 | static uchar emm_global[512];
|
---|
924 | static int emm_global_len = 0;
|
---|
925 |
|
---|
926 | int pos=0, i;
|
---|
927 | uint k;
|
---|
928 |
|
---|
929 | // Viaccess
|
---|
930 | if (*len>500) return 0;
|
---|
931 |
|
---|
932 | switch(buffer[0]) {
|
---|
933 | case 0x8c:
|
---|
934 | case 0x8d:
|
---|
935 | // emm-s part 1
|
---|
936 | if (!memcmp(emm_global, buffer, *len))
|
---|
937 | return 0;
|
---|
938 |
|
---|
939 | // copy first part of the emm-s
|
---|
940 | memcpy(emm_global, buffer, *len);
|
---|
941 | emm_global_len=*len;
|
---|
942 | //cs_ddump_mask(D_READER, buffer, len, "viaccess global emm:");
|
---|
943 | return 0;
|
---|
944 |
|
---|
945 | case 0x8e:
|
---|
946 | // emm-s part 2
|
---|
947 | if (!emm_global_len) return 0;
|
---|
948 |
|
---|
949 | //extract nanos from emm-gh and emm-s
|
---|
950 | uchar emmbuf[512];
|
---|
951 |
|
---|
952 | cs_debug_mask(D_DVBAPI, "viaccess_reassemble_emm: start extracting nanos");
|
---|
953 | //extract from emm-gh
|
---|
954 | for (i=3; i<emm_global_len; i+=emm_global[i+1]+2) {
|
---|
955 | //copy nano (length determined by i+1)
|
---|
956 | memcpy(emmbuf+pos, emm_global+i, emm_global[i+1]+2);
|
---|
957 | pos+=emm_global[i+1]+2;
|
---|
958 | }
|
---|
959 |
|
---|
960 | if (buffer[2]==0x2c) {
|
---|
961 | //add 9E 20 nano + first 32 bytes of emm content
|
---|
962 | memcpy(emmbuf+pos, "\x9E\x20", 2);
|
---|
963 | memcpy(emmbuf+pos+2, buffer+7, 32);
|
---|
964 | pos+=34;
|
---|
965 |
|
---|
966 | //add F0 08 nano + 8 subsequent bytes of emm content
|
---|
967 | memcpy(emmbuf+pos, "\xF0\x08", 2);
|
---|
968 | memcpy(emmbuf+pos+2, buffer+39, 8);
|
---|
969 | pos+=10;
|
---|
970 | } else {
|
---|
971 | //extract from variable emm-s
|
---|
972 | for (k=7; k<(*len); k+=buffer[k+1]+2) {
|
---|
973 | //copy nano (length determined by k+1)
|
---|
974 | memcpy(emmbuf+pos, buffer+k, buffer[k+1]+2);
|
---|
975 | pos+=buffer[k+1]+2;
|
---|
976 | }
|
---|
977 | }
|
---|
978 |
|
---|
979 | cs_ddump_mask(D_DVBAPI, buffer, *len, "viaccess_reassemble_emm: %s emm-s", (buffer[2]==0x2c) ? "fixed" : "variable");
|
---|
980 |
|
---|
981 | dvbapi_sort_nanos(buffer+7, emmbuf, pos);
|
---|
982 | pos+=7;
|
---|
983 |
|
---|
984 | //calculate emm length and set it on position 2
|
---|
985 | buffer[2]=pos-3;
|
---|
986 |
|
---|
987 | cs_ddump_mask(D_DVBAPI, emm_global, emm_global_len, "viaccess_reassemble_emm: emm-gh");
|
---|
988 | cs_ddump_mask(D_DVBAPI, buffer, pos, "viaccess_reassemble_emm: assembled emm");
|
---|
989 |
|
---|
990 | *len=pos;
|
---|
991 | break;
|
---|
992 | }
|
---|
993 | return 1;
|
---|
994 | }
|
---|
995 | #endif
|
---|
996 |
|
---|
997 | void reader_viaccess(struct s_cardsystem *ph)
|
---|
998 | {
|
---|
999 | ph->do_emm=viaccess_do_emm;
|
---|
1000 | ph->do_ecm=viaccess_do_ecm;
|
---|
1001 | ph->card_info=viaccess_card_info;
|
---|
1002 | ph->card_init=viaccess_card_init;
|
---|
1003 | ph->get_emm_type=viaccess_get_emm_type;
|
---|
1004 | ph->get_emm_filter=viaccess_get_emm_filter;
|
---|
1005 | ph->caids[0]=0x05;
|
---|
1006 | ph->desc="viaccess";
|
---|
1007 | }
|
---|