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 | int viaccess_card_init(struct s_reader * reader, ATR newatr)
|
---|
131 | {
|
---|
132 | get_atr;
|
---|
133 | def_resp;
|
---|
134 | int i;
|
---|
135 | uchar buf[256];
|
---|
136 | static uchar insac[] = { 0xca, 0xac, 0x00, 0x00, 0x00 }; // select data
|
---|
137 | static uchar insb8[] = { 0xca, 0xb8, 0x00, 0x00, 0x00 }; // read selected data
|
---|
138 | static uchar insa4[] = { 0xca, 0xa4, 0x00, 0x00, 0x00 }; // select issuer
|
---|
139 | static uchar insc0[] = { 0xca, 0xc0, 0x00, 0x00, 0x00 }; // read data item
|
---|
140 |
|
---|
141 | static uchar insFAC[] = { 0x87, 0x02, 0x00, 0x00, 0x03 }; // init FAC
|
---|
142 | static uchar FacDat[] = { 0x00, 0x00, 0x28 };
|
---|
143 |
|
---|
144 | if ((atr[0]!=0x3f) || (atr[1]!=0x77) || ((atr[2]!=0x18) && (atr[2]!=0x11) && (atr[2]!=0x19)) || (atr[9]!=0x68)) return ERROR;
|
---|
145 |
|
---|
146 | write_cmd(insFAC, FacDat);
|
---|
147 | if( !(cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0) )
|
---|
148 | return ERROR;
|
---|
149 |
|
---|
150 | // switch((atr[atrsize-4]<<8)|atr[atrsize-3])
|
---|
151 | // {
|
---|
152 | // case 0x6268: ver="2.3"; break;
|
---|
153 | // case 0x6668: ver="2.4(?)"; break;
|
---|
154 | // case 0xa268:
|
---|
155 | // default: ver="unknown"; break;
|
---|
156 | // }
|
---|
157 |
|
---|
158 | reader->caid[0]=0x500;
|
---|
159 | memset(reader->prid, 0xff, sizeof(reader->prid));
|
---|
160 | insac[2]=0xa4; write_cmd(insac, NULL); // request unique id
|
---|
161 | insb8[4]=0x07; write_cmd(insb8, NULL); // read unique id
|
---|
162 | memcpy(reader->hexserial, cta_res+2, 5);
|
---|
163 | // cs_log("[viaccess-reader] type: Viaccess, ver: %s serial: %llu", ver, b2ll(5, cta_res+2));
|
---|
164 | cs_ri_log(reader, "type: Viaccess (%sstandard atr), caid: %04X, serial: %llu",
|
---|
165 | atr[9]==0x68?"":"non-",reader->caid[0], b2ll(5, cta_res+2));
|
---|
166 |
|
---|
167 | i=0;
|
---|
168 | insa4[2]=0x00; write_cmd(insa4, NULL); // select issuer 0
|
---|
169 | buf[0]=0;
|
---|
170 | while((cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0))
|
---|
171 | {
|
---|
172 | insc0[4]=0x1a; write_cmd(insc0, NULL); // show provider properties
|
---|
173 | cta_res[2]&=0xF0;
|
---|
174 | reader->prid[i][0]=0;
|
---|
175 | memcpy(&reader->prid[i][1], cta_res, 3);
|
---|
176 | memcpy(&reader->availkeys[i][0], cta_res+10, 16);
|
---|
177 | sprintf((char *)buf+strlen((char *)buf), ",%06lX", b2i(3, &reader->prid[i][1]));
|
---|
178 | //cs_log("[viaccess-reader] buf: %s", buf);
|
---|
179 |
|
---|
180 | insac[2]=0xa5; write_cmd(insac, NULL); // request sa
|
---|
181 | insb8[4]=0x06; write_cmd(insb8, NULL); // read sa
|
---|
182 | memcpy(&reader->sa[i][0], cta_res+2, 4);
|
---|
183 |
|
---|
184 | /*
|
---|
185 | insac[2]=0xa7; write_cmd(insac, NULL); // request name
|
---|
186 | insb8[4]=0x02; write_cmd(insb8, NULL); // read name nano + len
|
---|
187 | l=cta_res[1];
|
---|
188 | insb8[4]=l; write_cmd(insb8, NULL); // read name
|
---|
189 | cta_res[l]=0;
|
---|
190 | cs_log("[viaccess-reader] name: %s", cta_res);
|
---|
191 | */
|
---|
192 |
|
---|
193 | insa4[2]=0x02;
|
---|
194 | write_cmd(insa4, NULL); // select next issuer
|
---|
195 | i++;
|
---|
196 | }
|
---|
197 | reader->nprov=i;
|
---|
198 | cs_ri_log(reader, "providers: %d (%s)", reader->nprov, buf+1);
|
---|
199 |
|
---|
200 | /* init the maybe existing aes key */
|
---|
201 | aes_set_key((char *)reader->aes_key);
|
---|
202 |
|
---|
203 | /* disabling parental lock. assuming pin "0000" */
|
---|
204 | if (cfg->ulparent) {
|
---|
205 | static uchar inDPL[] = {0xca, 0x24, 0x02, 0x00, 0x09};
|
---|
206 | static uchar cmDPL[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F};
|
---|
207 | write_cmd(inDPL,cmDPL);
|
---|
208 | if( !(cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0) )
|
---|
209 | cs_log("[viaccess-reader] Can't disable parental lock. Wrong PIN? I assumed 0000!");
|
---|
210 | else
|
---|
211 | cs_log("[viaccess-reader] Parental lock disabled");
|
---|
212 | }
|
---|
213 |
|
---|
214 | cs_log("[viaccess-reader] ready for requests");
|
---|
215 | memset(&reader->last_geo, 0, sizeof(reader->last_geo));
|
---|
216 | return OK;
|
---|
217 | }
|
---|
218 |
|
---|
219 | int viaccess_do_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
220 | {
|
---|
221 | def_resp;
|
---|
222 | static unsigned char insa4[] = { 0xca,0xa4,0x04,0x00,0x03 }; // set provider id
|
---|
223 | static unsigned char ins88[] = { 0xca,0x88,0x00,0x00,0x00 }; // set ecm
|
---|
224 | static unsigned char insf8[] = { 0xca,0xf8,0x00,0x00,0x00 }; // set geographic info
|
---|
225 | static unsigned char insc0[] = { 0xca,0xc0,0x00,0x00,0x12 }; // read dcw
|
---|
226 |
|
---|
227 | const uchar *ecm88Data=er->ecm+4; //XXX what is the 4th byte for ??
|
---|
228 | int ecm88Len=SCT_LEN(er->ecm)-4;
|
---|
229 | ulong provid=0;
|
---|
230 | int rc=0;
|
---|
231 | int hasD2 = 0;
|
---|
232 | int curEcm88len=0;
|
---|
233 | int nanoLen=0;
|
---|
234 | const uchar *nextEcm;
|
---|
235 | uchar keyToUse=0;
|
---|
236 | uchar DE04[256];
|
---|
237 | int D2KeyID=0;
|
---|
238 | memset(DE04, 0, sizeof(DE04)); //fix dorcel de04 bug
|
---|
239 |
|
---|
240 | nextEcm=ecm88Data;
|
---|
241 |
|
---|
242 | while (ecm88Len && !rc) {
|
---|
243 |
|
---|
244 | // 80 33 nano 80 (ecm) + len (33)
|
---|
245 | if(ecm88Data[0]==0x80) { // nano 80, give ecm len
|
---|
246 | curEcm88len=ecm88Data[1];
|
---|
247 | nextEcm=ecm88Data+curEcm88len+2;
|
---|
248 | ecm88Data += 2;
|
---|
249 | ecm88Len -= 2;
|
---|
250 | }
|
---|
251 |
|
---|
252 | if(!curEcm88len) { //there was no nano 80 -> simple ecm
|
---|
253 | curEcm88len=ecm88Len;
|
---|
254 | }
|
---|
255 |
|
---|
256 | // d2 02 0d 02 -> D2 nano, len 2, select the AES key to be used
|
---|
257 | if(ecm88Data[0]==0xd2) {
|
---|
258 | // FIXME: use the d2 arguments
|
---|
259 | int len = ecm88Data[1] + 2;
|
---|
260 | D2KeyID=ecm88Data[3];
|
---|
261 | ecm88Data += len;
|
---|
262 | ecm88Len -= len;
|
---|
263 | curEcm88len -=len;
|
---|
264 | hasD2 = 1;
|
---|
265 | }
|
---|
266 | else
|
---|
267 | hasD2 = 0;
|
---|
268 |
|
---|
269 | // 40 07 03 0b 00 -> nano 40, len =7 ident 030B00 (tntsat), key #0 <== we're pointing here
|
---|
270 | // 09 -> use key #9
|
---|
271 | // 05 67 00
|
---|
272 | if ((ecm88Data[0]==0x90 || ecm88Data[0]==0x40) && (ecm88Data[1]==0x03 || ecm88Data[1]==0x07 ) )
|
---|
273 | {
|
---|
274 | uchar ident[3], keynr;
|
---|
275 | //uchar buff[256]; // MAX_LEN
|
---|
276 | uchar *ecmf8Data=0;
|
---|
277 | int ecmf8Len=0;
|
---|
278 |
|
---|
279 | nanoLen=ecm88Data[1] + 2;
|
---|
280 |
|
---|
281 | memcpy (ident, &ecm88Data[2], sizeof(ident));
|
---|
282 | provid = b2i(3, ident);
|
---|
283 | ident[2]&=0xF0;
|
---|
284 | keynr=ecm88Data[4]&0x0F;
|
---|
285 | // 40 07 03 0b 00 -> nano 40, len =7 ident 030B00 (tntsat), key #0 <== we're pointing here
|
---|
286 | // 09 -> use key #9
|
---|
287 | if(nanoLen>5) {
|
---|
288 | keyToUse=ecm88Data[5];
|
---|
289 | keynr=keyToUse;
|
---|
290 | cs_debug("keyToUse = %d",keyToUse);
|
---|
291 | }
|
---|
292 |
|
---|
293 | if (!chk_prov(reader, ident, keynr))
|
---|
294 | {
|
---|
295 | cs_debug("[viaccess-reader] ECM: provider or key not found on card");
|
---|
296 | return ERROR;
|
---|
297 | }
|
---|
298 |
|
---|
299 | ecm88Data+=nanoLen;
|
---|
300 | ecm88Len-=nanoLen;
|
---|
301 | curEcm88len-=nanoLen;
|
---|
302 |
|
---|
303 | // DE04
|
---|
304 | if (ecm88Data[0]==0xDE && ecm88Data[1]==0x04)
|
---|
305 | {
|
---|
306 | memcpy (DE04, &ecm88Data[0], 6);
|
---|
307 | ecm88Data+=6;
|
---|
308 | }
|
---|
309 | //
|
---|
310 |
|
---|
311 | if( reader->last_geo.provid != provid )
|
---|
312 | {
|
---|
313 | reader->last_geo.provid = provid;
|
---|
314 | reader->last_geo.geo_len = 0;
|
---|
315 | reader->last_geo.geo[0] = 0;
|
---|
316 | write_cmd(insa4, ident); // set provider
|
---|
317 | }
|
---|
318 |
|
---|
319 | while(ecm88Len>0 && ecm88Data[0]<0xA0)
|
---|
320 | {
|
---|
321 | int nanoLen=ecm88Data[1]+2;
|
---|
322 | if (!ecmf8Data)
|
---|
323 | ecmf8Data=(uchar *)ecm88Data;
|
---|
324 | ecmf8Len+=nanoLen;
|
---|
325 | ecm88Len-=nanoLen;
|
---|
326 | curEcm88len-=nanoLen;
|
---|
327 | ecm88Data+=nanoLen;
|
---|
328 | }
|
---|
329 | if(ecmf8Len)
|
---|
330 | {
|
---|
331 | if( reader->last_geo.geo_len!=ecmf8Len ||
|
---|
332 | memcmp(reader->last_geo.geo, ecmf8Data, reader->last_geo.geo_len))
|
---|
333 | {
|
---|
334 | memcpy(reader->last_geo.geo, ecmf8Data, ecmf8Len);
|
---|
335 | reader->last_geo.geo_len= ecmf8Len;
|
---|
336 | insf8[3]=keynr;
|
---|
337 | insf8[4]=ecmf8Len;
|
---|
338 | write_cmd(insf8, ecmf8Data);
|
---|
339 | }
|
---|
340 | }
|
---|
341 | ins88[2]=ecmf8Len?1:0;
|
---|
342 | ins88[3]=keynr;
|
---|
343 | ins88[4]= curEcm88len;
|
---|
344 |
|
---|
345 | // DE04
|
---|
346 | if (DE04[0]==0xDE)
|
---|
347 | {
|
---|
348 | memcpy(DE04+6, (uchar *)ecm88Data, curEcm88len-6);
|
---|
349 | write_cmd(ins88, DE04); // request dcw
|
---|
350 | }
|
---|
351 | else
|
---|
352 | {
|
---|
353 | write_cmd(ins88, (uchar *)ecm88Data); // request dcw
|
---|
354 | }
|
---|
355 | //
|
---|
356 | write_cmd(insc0, NULL); // read dcw
|
---|
357 | switch(cta_res[0])
|
---|
358 | {
|
---|
359 | case 0xe8: // even
|
---|
360 | if(cta_res[1]==8) { memcpy(er->cw,cta_res+2,8); rc=1; }
|
---|
361 | break;
|
---|
362 | case 0xe9: // odd
|
---|
363 | if(cta_res[1]==8) { memcpy(er->cw+8,cta_res+2,8); rc=1; }
|
---|
364 | break;
|
---|
365 | case 0xea: // complete
|
---|
366 | if(cta_res[1]==16) { memcpy(er->cw,cta_res+2,16); rc=1; }
|
---|
367 | break;
|
---|
368 | default :
|
---|
369 | ecm88Data=nextEcm;
|
---|
370 | ecm88Len-=curEcm88len;
|
---|
371 | cs_debug("[viaccess-reader] ECM: key to use is not the current one, trying next ECM");
|
---|
372 | }
|
---|
373 | }
|
---|
374 | else {
|
---|
375 | ecm88Data=nextEcm;
|
---|
376 | ecm88Len-=curEcm88len;
|
---|
377 | cs_debug("[viaccess-reader] ECM: Unknown ECM type");
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | if (hasD2) {
|
---|
382 | if(reader->aes_list) {
|
---|
383 | cs_debug("Decoding CW : using AES key id %d for provider %06x",D2KeyID,provid);
|
---|
384 | rc=aes_decrypt_from_list(reader->aes_list,0x500, (uint32) provid, D2KeyID,er->cw, 16);
|
---|
385 | }
|
---|
386 | else
|
---|
387 | aes_decrypt(er->cw, 16);
|
---|
388 | }
|
---|
389 |
|
---|
390 | return(rc?OK:ERROR);
|
---|
391 | }
|
---|
392 |
|
---|
393 | int viaccess_get_emm_type(EMM_PACKET *ep, struct s_reader * rdr)
|
---|
394 | {
|
---|
395 | cs_debug_mask(D_EMM, "Entered viaccess_get_emm_type ep->emm[0]=%02x",ep->emm[0]);
|
---|
396 |
|
---|
397 | switch (ep->emm[0]) {
|
---|
398 | case 0x88:
|
---|
399 | ep->type=UNIQUE;
|
---|
400 | memset(ep->hexserial, 0, 8);
|
---|
401 | memcpy(ep->hexserial, ep->emm + 3, 3);
|
---|
402 | cs_debug_mask(D_EMM, "VIACCESS EMM: UNIQUE");
|
---|
403 | return(!memcmp(rdr->hexserial + 1, ep->hexserial, 4));
|
---|
404 |
|
---|
405 | case 0x8A:
|
---|
406 | case 0x8B:
|
---|
407 | ep->type=GLOBAL;
|
---|
408 | cs_debug_mask(D_EMM, "VIACCESS EMM: GLOBAL");
|
---|
409 | return TRUE;
|
---|
410 |
|
---|
411 | case 0x8C:
|
---|
412 | case 0x8D:
|
---|
413 | ep->type=SHARED;
|
---|
414 | cs_debug_mask(D_EMM, "VIACCESS EMM: SHARED (part)");
|
---|
415 | return FALSE;
|
---|
416 |
|
---|
417 | case 0x8E:
|
---|
418 | ep->type=SHARED;
|
---|
419 | memset(ep->hexserial, 0, 8);
|
---|
420 | memcpy(ep->hexserial, ep->emm + 3, 3);
|
---|
421 | cs_debug_mask(D_EMM, "VIACCESS EMM: SHARED");
|
---|
422 | return(!memcmp(&rdr->sa[0][0], ep->hexserial, 3));
|
---|
423 |
|
---|
424 | default:
|
---|
425 | ep->type = UNKNOWN;
|
---|
426 | cs_debug_mask(D_EMM, "VIACCESS EMM: UNKNOWN");
|
---|
427 | return TRUE;
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|
431 | void viaccess_get_emm_filter(struct s_reader * rdr, uchar *filter)
|
---|
432 | {
|
---|
433 | filter[0]=0xFF;
|
---|
434 | filter[1]=3;
|
---|
435 |
|
---|
436 | filter[2]=GLOBAL;
|
---|
437 | filter[3]=0;
|
---|
438 |
|
---|
439 | filter[4+0] = 0x8D;
|
---|
440 | filter[4+0+16] = 0xFE;
|
---|
441 | //filter[4+6] = 0xA0; // FIXME: dummy, flood client with EMM's
|
---|
442 | //filter[4+6+16] = 0xF0;
|
---|
443 |
|
---|
444 |
|
---|
445 | filter[36]=SHARED;
|
---|
446 | filter[37]=0;
|
---|
447 |
|
---|
448 | filter[38+0] = 0x8E;
|
---|
449 | filter[38+0+16] = 0xFF;
|
---|
450 | memcpy(filter+38+1, &rdr->sa[0][0], 3);
|
---|
451 | memset(filter+38+1+16, 0xFF, 3);
|
---|
452 |
|
---|
453 |
|
---|
454 | filter[70]=UNIQUE;
|
---|
455 | filter[71]=0;
|
---|
456 |
|
---|
457 | filter[72+0] = 0x88;
|
---|
458 | filter[72+0+16] = 0xFF;
|
---|
459 | memcpy(filter+72+1, rdr->hexserial + 1, 4);
|
---|
460 | memset(filter+72+1+16, 0xFF, 4);
|
---|
461 |
|
---|
462 | return;
|
---|
463 | }
|
---|
464 |
|
---|
465 | int viaccess_do_emm(struct s_reader * reader, EMM_PACKET *ep)
|
---|
466 | {
|
---|
467 | def_resp;
|
---|
468 | static unsigned char insa4[] = { 0xca,0xa4,0x04,0x00,0x03 }; // set provider id
|
---|
469 | static unsigned char insf0[] = { 0xca,0xf0,0x00,0x01,0x22 }; // set adf
|
---|
470 | static unsigned char insf4[] = { 0xca,0xf4,0x00,0x01,0x00 }; // set adf, encrypted
|
---|
471 | static unsigned char ins18[] = { 0xca,0x18,0x01,0x01,0x00 }; // set subscription
|
---|
472 | static unsigned char ins1c[] = { 0xca,0x1c,0x01,0x01,0x00 }; // set subscription, encrypted
|
---|
473 | static unsigned char insc8[] = { 0xca,0xc8,0x00,0x00,0x02 }; // read extended status
|
---|
474 | static unsigned char insc8Data[] = { 0x00,0x00 }; // data for read extended status
|
---|
475 |
|
---|
476 | int emmLen=SCT_LEN(ep->emm)-7;
|
---|
477 | int rc=0;
|
---|
478 |
|
---|
479 | ///cs_dump(ep->emm, emmLen+7, "RECEIVED EMM VIACCESS");
|
---|
480 |
|
---|
481 | int emmUpToEnd;
|
---|
482 | uchar *emmParsed = ep->emm+7;
|
---|
483 | int provider_ok = 0;
|
---|
484 | uchar keynr = 0;
|
---|
485 | int ins18Len = 0;
|
---|
486 | uchar ins18Data[512];
|
---|
487 | uchar insData[512];
|
---|
488 | uchar *nano81Data = 0;
|
---|
489 | uchar *nano91Data = 0;
|
---|
490 | uchar *nano92Data = 0;
|
---|
491 | uchar *nano9EData = 0;
|
---|
492 | uchar *nanoF0Data = 0;
|
---|
493 |
|
---|
494 | for (emmUpToEnd=emmLen; (emmParsed[1] != 0) && (emmUpToEnd > 0); emmUpToEnd -= (2 + emmParsed[1]), emmParsed += (2 + emmParsed[1])) {
|
---|
495 | ///cs_dump (emmParsed, emmParsed[1] + 2, "NANO");
|
---|
496 |
|
---|
497 | if (emmParsed[0]==0x90 && emmParsed[1]==0x03) {
|
---|
498 | /* identification of the service operator */
|
---|
499 |
|
---|
500 | uchar soid[3], ident[3], i;
|
---|
501 |
|
---|
502 | for (i=0; i<3; i++) {
|
---|
503 | soid[i]=ident[i]=emmParsed[2+i];
|
---|
504 | }
|
---|
505 | ident[2]&=0xF0;
|
---|
506 | keynr=soid[2]&0x0F;
|
---|
507 | if (chk_prov(reader, ident, keynr)) {
|
---|
508 | provider_ok = 1;
|
---|
509 | } else {
|
---|
510 | cs_debug("[viaccess-reader] EMM: provider or key not found on card (%x, %x)", ident, keynr);
|
---|
511 | return ERROR;
|
---|
512 | }
|
---|
513 |
|
---|
514 | // as we are maybe changing the used provider, clear the cache, so the next ecm will re-select the correct one
|
---|
515 | memset(&reader->last_geo, 0, sizeof(reader->last_geo));
|
---|
516 |
|
---|
517 | // set provider
|
---|
518 | write_cmd(insa4, soid);
|
---|
519 | if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
520 | cs_dump(insa4, 5, "set provider cmd:");
|
---|
521 | cs_dump(soid, 3, "set provider data:");
|
---|
522 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
523 | return ERROR;
|
---|
524 | }
|
---|
525 | } else if (emmParsed[0]==0x9e && emmParsed[1]==0x20) {
|
---|
526 | /* adf */
|
---|
527 |
|
---|
528 | if (!nano91Data) {
|
---|
529 | /* adf is not crypted, so test it */
|
---|
530 |
|
---|
531 | uchar custwp;
|
---|
532 | uchar *afd;
|
---|
533 |
|
---|
534 | custwp=reader->sa[0][3];
|
---|
535 | afd=(uchar*)emmParsed+2;
|
---|
536 |
|
---|
537 | if( afd[31-custwp/8] & (1 << (custwp & 7)) )
|
---|
538 | cs_debug("[viaccess-reader] emm for our card %08X", b2i(4, &reader->sa[0][0]));
|
---|
539 | else
|
---|
540 | return SKIPPED;
|
---|
541 | }
|
---|
542 |
|
---|
543 | // memorize
|
---|
544 | nano9EData = emmParsed;
|
---|
545 |
|
---|
546 | } else if (emmParsed[0]==0x81) {
|
---|
547 | nano81Data = emmParsed;
|
---|
548 | } else if (emmParsed[0]==0x91 && emmParsed[1]==0x08) {
|
---|
549 | nano91Data = emmParsed;
|
---|
550 | } else if (emmParsed[0]==0x92 && emmParsed[1]==0x08) {
|
---|
551 | nano92Data = emmParsed;
|
---|
552 | } else if (emmParsed[0]==0xF0 && emmParsed[1]==0x08) {
|
---|
553 | nanoF0Data = emmParsed;
|
---|
554 | } else if (emmParsed[0]==0x1D && emmParsed[0]==0x01 && emmParsed[0]==0x01) {
|
---|
555 | /* from cccam... skip it... */
|
---|
556 | } else {
|
---|
557 | /* other nanos */
|
---|
558 | show_subs(reader, emmParsed);
|
---|
559 |
|
---|
560 | memcpy(ins18Data+ins18Len, emmParsed, emmParsed[1] + 2);
|
---|
561 | ins18Len += emmParsed [1] + 2;
|
---|
562 | }
|
---|
563 | }
|
---|
564 |
|
---|
565 | if (!provider_ok) {
|
---|
566 | cs_debug("[viaccess-reader] provider not found in emm, continue anyway");
|
---|
567 | // force key to 1...
|
---|
568 | keynr = 1;
|
---|
569 | ///return ERROR;
|
---|
570 | }
|
---|
571 |
|
---|
572 | if (!nanoF0Data) {
|
---|
573 | cs_dump(ep->emm, ep->l, "can't find 0xf0 in emm...");
|
---|
574 | return ERROR; // error
|
---|
575 | }
|
---|
576 |
|
---|
577 | if (nano9EData) {
|
---|
578 | if (!nano91Data) {
|
---|
579 | // set adf
|
---|
580 | insf0[3] = keynr; // key
|
---|
581 | write_cmd(insf0, nano9EData);
|
---|
582 | if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
583 | cs_dump(insf0, 5, "set adf cmd:");
|
---|
584 | cs_dump(nano9EData, 0x22, "set adf data:");
|
---|
585 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
586 | return ERROR;
|
---|
587 | }
|
---|
588 | } else {
|
---|
589 | // set adf crypte
|
---|
590 | insf4[3] = keynr; // key
|
---|
591 | insf4[4] = nano91Data[1] + 2 + nano9EData[1] + 2;
|
---|
592 | memcpy (insData, nano91Data, nano91Data[1] + 2);
|
---|
593 | memcpy (insData + nano91Data[1] + 2, nano9EData, nano9EData[1] + 2);
|
---|
594 | write_cmd(insf4, insData);
|
---|
595 | if(( cta_res[cta_lr-2]!=0x90 && cta_res[cta_lr-2]!=0x91) || cta_res[cta_lr-1]!=0x00 ) {
|
---|
596 | cs_dump(insf4, 5, "set adf encrypted cmd:");
|
---|
597 | cs_dump(insData, insf4[4], "set adf encrypted data:");
|
---|
598 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
599 | return ERROR;
|
---|
600 | }
|
---|
601 | }
|
---|
602 | }
|
---|
603 |
|
---|
604 | if (!nano92Data) {
|
---|
605 | // send subscription
|
---|
606 | ins18[4] = ins18Len + nanoF0Data[1] + 2;
|
---|
607 | memcpy (insData, ins18Data, ins18Len);
|
---|
608 | memcpy (insData + ins18Len, nanoF0Data, nanoF0Data[1] + 2);
|
---|
609 | write_cmd(ins18, insData);
|
---|
610 | if( cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0x00 ) {
|
---|
611 | cs_debug("[viaccess-reader] update successfully written");
|
---|
612 | rc=1; // written
|
---|
613 | } else {
|
---|
614 | cs_dump(ins18, 5, "set subscription cmd:");
|
---|
615 | cs_dump(insData, ins18[4], "set subscription data:");
|
---|
616 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
617 | }
|
---|
618 |
|
---|
619 | } else {
|
---|
620 | // send subscription encrypted
|
---|
621 |
|
---|
622 | if (!nano81Data) {
|
---|
623 | cs_dump(ep->emm, ep->l, "0x92 found, but can't find 0x81 in emm...");
|
---|
624 | return ERROR; // error
|
---|
625 | }
|
---|
626 |
|
---|
627 | ins1c[3] = keynr; // key
|
---|
628 | ins1c[4] = nano92Data[1] + 2 + nano81Data[1] + 2 + nanoF0Data[1] + 2;
|
---|
629 | memcpy (insData, nano92Data, nano92Data[1] + 2);
|
---|
630 | memcpy (insData + nano92Data[1] + 2, nano81Data, nano81Data[1] + 2);
|
---|
631 | memcpy (insData + nano92Data[1] + 2 + nano81Data[1] + 2, nanoF0Data, nanoF0Data[1] + 2);
|
---|
632 | write_cmd(ins1c, insData);
|
---|
633 | if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
634 | /* maybe a 2nd level status, so read it */
|
---|
635 | ///cs_dump(ins1c, 5, "set subscription encrypted cmd:");
|
---|
636 | ///cs_dump(insData, ins1c[4], "set subscription encrypted data:");
|
---|
637 | ///cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
638 |
|
---|
639 | write_cmd(insc8, insc8Data);
|
---|
640 | if( cta_res[0] != 0x00 || cta_res[1] != 00 || cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
641 | ///cs_dump(cta_res, cta_lr, "extended status error:");
|
---|
642 | return ERROR;
|
---|
643 | } else {
|
---|
644 | cs_debug("[viaccess-reader] update successfully written (with extended status OK)");
|
---|
645 | rc=1; // written
|
---|
646 | }
|
---|
647 | } else {
|
---|
648 | cs_debug("[viaccess-reader] update successfully written");
|
---|
649 | rc=1; // written
|
---|
650 | }
|
---|
651 | }
|
---|
652 |
|
---|
653 | /*
|
---|
654 | Sub Main()
|
---|
655 | Sc.Write("CA A4 04 00 03")
|
---|
656 | RX
|
---|
657 | Sc.Write("02 07 11")
|
---|
658 | RX
|
---|
659 | Sc.Write("CA F0 00 01 22")
|
---|
660 | RX
|
---|
661 | Sc.Write("9E 20")
|
---|
662 | Sc.Write("10 10 08 8A 80 00 04 00 10 10 26 E8 54 80 1E 80")
|
---|
663 | Sc.Write("00 01 00 00 00 00 00 50 00 00 80 02 22 00 08 50")
|
---|
664 | RX
|
---|
665 | Sc.Write("CA 18 01 01 11")
|
---|
666 | RX
|
---|
667 | Sc.Write("A9 05 34 DE 34 FF 80")
|
---|
668 | Sc.Write("F0 08 1A 3E AF B5 2B EE E3 3B")
|
---|
669 | RX
|
---|
670 |
|
---|
671 | End Sub
|
---|
672 | */
|
---|
673 | return rc;
|
---|
674 | }
|
---|
675 |
|
---|
676 | int viaccess_card_info(struct s_reader * reader)
|
---|
677 | {
|
---|
678 | def_resp;
|
---|
679 | int i, l, scls, show_cls;
|
---|
680 | static uchar insac[] = { 0xca, 0xac, 0x00, 0x00, 0x00 }; // select data
|
---|
681 | static uchar insb8[] = { 0xca, 0xb8, 0x00, 0x00, 0x00 }; // read selected data
|
---|
682 | static uchar insa4[] = { 0xca, 0xa4, 0x00, 0x00, 0x00 }; // select issuer
|
---|
683 | static uchar insc0[] = { 0xca, 0xc0, 0x00, 0x00, 0x00 }; // read data item
|
---|
684 | static uchar ins24[] = { 0xca, 0x24, 0x00, 0x00, 0x09 }; // set pin
|
---|
685 |
|
---|
686 | static uchar cls[] = { 0x00, 0x21, 0xff, 0x9f};
|
---|
687 | static uchar pin[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04};
|
---|
688 |
|
---|
689 | show_cls=reader->show_cls;
|
---|
690 | memset(&reader->last_geo, 0, sizeof(reader->last_geo));
|
---|
691 |
|
---|
692 | cs_log("[viaccess-reader] card detected");
|
---|
693 |
|
---|
694 | // set pin
|
---|
695 | write_cmd(ins24, pin);
|
---|
696 |
|
---|
697 | insac[2]=0xa4; write_cmd(insac, NULL); // request unique id
|
---|
698 | insb8[4]=0x07; write_cmd(insb8, NULL); // read unique id
|
---|
699 | cs_log("[viaccess-reader] serial: %llu", b2ll(5, cta_res+2));
|
---|
700 |
|
---|
701 | scls=0;
|
---|
702 | insa4[2]=0x00; write_cmd(insa4, NULL); // select issuer 0
|
---|
703 | for (i=1; (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0); i++)
|
---|
704 | {
|
---|
705 | ulong l_provid, l_sa;
|
---|
706 | uchar l_name[64];
|
---|
707 | insc0[4]=0x1a; write_cmd(insc0, NULL); // show provider properties
|
---|
708 | cta_res[2]&=0xF0;
|
---|
709 | l_provid=b2i(3, cta_res);
|
---|
710 |
|
---|
711 | insac[2]=0xa5; write_cmd(insac, NULL); // request sa
|
---|
712 | insb8[4]=0x06; write_cmd(insb8, NULL); // read sa
|
---|
713 | l_sa=b2i(4, cta_res+2);
|
---|
714 |
|
---|
715 | insac[2]=0xa7; write_cmd(insac, NULL); // request name
|
---|
716 | insb8[4]=0x02; write_cmd(insb8, NULL); // read name nano + len
|
---|
717 | l=cta_res[1];
|
---|
718 | insb8[4]=l; write_cmd(insb8, NULL); // read name
|
---|
719 | cta_res[l]=0;
|
---|
720 | trim((char *)cta_res);
|
---|
721 | if (cta_res[0])
|
---|
722 | snprintf((char *)l_name, sizeof(l_name), ", name: %s", cta_res);
|
---|
723 | else
|
---|
724 | l_name[0]=0;
|
---|
725 |
|
---|
726 | // read GEO
|
---|
727 | insac[2]=0xa6; write_cmd(insac, NULL); // request GEO
|
---|
728 | insb8[4]=0x02; write_cmd(insb8, NULL); // read GEO nano + len
|
---|
729 | l=cta_res[1];
|
---|
730 | insb8[4]=l; write_cmd(insb8, NULL); // read geo
|
---|
731 | cs_ri_log(reader, "provider: %d, id: %06X%s, sa: %08X, geo: %s",
|
---|
732 | i, l_provid, l_name, l_sa, (l<4) ? "empty" : cs_hexdump(1, cta_res, l));
|
---|
733 |
|
---|
734 | // read classes subscription
|
---|
735 | insac[2]=0xa9; insac[4]=4;
|
---|
736 | write_cmd(insac, cls); // request class subs
|
---|
737 | scls=0;
|
---|
738 | while( (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0) )
|
---|
739 | {
|
---|
740 | insb8[4]=0x02; write_cmd(insb8, NULL); // read class subs nano + len
|
---|
741 | if( (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0) )
|
---|
742 | {
|
---|
743 | int fshow;
|
---|
744 | l=cta_res[1];
|
---|
745 | //fshow=(client[cs_idx].dbglvl==D_DUMP)?1:(scls < show_cls)?1:0;
|
---|
746 | fshow=(scls<show_cls);
|
---|
747 | insb8[4]=l; write_cmd(insb8, NULL); // read class subs
|
---|
748 | if( (cta_res[cta_lr-2]==0x90) && (fshow) &&
|
---|
749 | (cta_res[cta_lr-1]==0x00 || cta_res[cta_lr-1]==0x08) )
|
---|
750 | {
|
---|
751 | show_class(reader, NULL, cta_res, cta_lr-2);
|
---|
752 | scls++;
|
---|
753 | }
|
---|
754 | }
|
---|
755 | }
|
---|
756 |
|
---|
757 | insac[4]=0;
|
---|
758 | insa4[2]=0x02;
|
---|
759 | write_cmd(insa4, NULL); // select next provider
|
---|
760 | }
|
---|
761 | //return ERROR;
|
---|
762 | return OK;
|
---|
763 | }
|
---|