1 | #include "globals.h"
|
---|
2 | #include "reader-common.h"
|
---|
3 |
|
---|
4 | extern uchar cta_res[];
|
---|
5 | extern ushort cta_lr;
|
---|
6 |
|
---|
7 | struct geo_cache
|
---|
8 | {
|
---|
9 | ulong provid;
|
---|
10 | uchar geo[256];
|
---|
11 | uchar geo_len;
|
---|
12 | };
|
---|
13 |
|
---|
14 | static struct geo_cache last_geo;
|
---|
15 |
|
---|
16 | struct via_date {
|
---|
17 | ushort day_s : 5;
|
---|
18 | ushort month_s : 4;
|
---|
19 | ushort year_s : 7;
|
---|
20 |
|
---|
21 | ushort day_e : 5;
|
---|
22 | ushort month_e : 4;
|
---|
23 | ushort year_e : 7;
|
---|
24 | };
|
---|
25 |
|
---|
26 | static void parse_via_date(const uchar *buf, struct via_date *vd, int fend)
|
---|
27 | {
|
---|
28 | ushort date;
|
---|
29 |
|
---|
30 | date = (buf[0]<<8) | buf[1];
|
---|
31 | vd->day_s = date & 0x1f;
|
---|
32 | vd->month_s = (date>>5) & 0x0f;
|
---|
33 | vd->year_s = (date>>9) & 0x7f;
|
---|
34 |
|
---|
35 | if( fend )
|
---|
36 | {
|
---|
37 | date = (buf[2]<<8) | buf[3];
|
---|
38 | vd->day_e = date & 0x1f;
|
---|
39 | vd->month_e = (date>>5) & 0x0f;
|
---|
40 | vd->year_e = (date>>9) & 0x7f;
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | static void show_class(const char *p, const uchar *b, int l)
|
---|
45 | {
|
---|
46 | int i, j;
|
---|
47 |
|
---|
48 | // b -> via date (4 bytes)
|
---|
49 | b+=4;
|
---|
50 | l-=4;
|
---|
51 |
|
---|
52 | j=l-1;
|
---|
53 | for (; j>=0; j--)
|
---|
54 | for (i=0; i<8; i++)
|
---|
55 | if (b[j] & (1 << (i&7)))
|
---|
56 | {
|
---|
57 | uchar cls;
|
---|
58 | struct via_date vd;
|
---|
59 | parse_via_date(b-4, &vd, 1);
|
---|
60 | cls=(l-(j+1))*8+i;
|
---|
61 | if (p)
|
---|
62 | cs_log("%sclass: %02X, expiry date: %04d/%02d/%02d - %04d/%02d/%02d", p, cls,
|
---|
63 | vd.year_s+1980, vd.month_s, vd.day_s,
|
---|
64 | vd.year_e+1980, vd.month_e, vd.day_e);
|
---|
65 | else
|
---|
66 | cs_ri_log("class: %02X, expiry date: %04d/%02d/%02d - %04d/%02d/%02d", cls,
|
---|
67 | vd.year_s+1980, vd.month_s, vd.day_s,
|
---|
68 | vd.year_e+1980, vd.month_e, vd.day_e);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | static void show_subs(const uchar *emm)
|
---|
73 | {
|
---|
74 | // emm -> A9, A6, B6
|
---|
75 |
|
---|
76 | switch( emm[0] )
|
---|
77 | {
|
---|
78 | case 0xA9:
|
---|
79 | show_class("nano A9: ", emm+2, emm[1]);
|
---|
80 | break;
|
---|
81 | /*
|
---|
82 | {
|
---|
83 | int i, j, byts;
|
---|
84 | const uchar *oemm;
|
---|
85 |
|
---|
86 | oemm = emm;
|
---|
87 | byts = emm[1]-4;
|
---|
88 | emm+=6;
|
---|
89 |
|
---|
90 | j=byts-1;
|
---|
91 | for( ; j>=0; j-- )
|
---|
92 | for( i=0; i<8; i++ )
|
---|
93 | if( emm[j] & (1 << (i&7)) )
|
---|
94 | {
|
---|
95 | uchar cls;
|
---|
96 | struct via_date vd;
|
---|
97 | parse_via_date(emm-4, &vd, 1);
|
---|
98 | cls=(byts-(j+1))*8+i;
|
---|
99 | cs_log("%sclass %02X: expiry date: %02d/%02d/%04d - %02d/%02d/%04d",
|
---|
100 | fnano?"nano A9: ":"", cls,
|
---|
101 | vd.day_s, vd.month_s, vd.year_s+1980,
|
---|
102 | vd.day_e, vd.month_e, vd.year_e+1980);
|
---|
103 | }
|
---|
104 | break;
|
---|
105 | }
|
---|
106 | */
|
---|
107 | case 0xA6:
|
---|
108 | {
|
---|
109 | char szGeo[256];
|
---|
110 |
|
---|
111 | memset(szGeo, 0, 256);
|
---|
112 | strncpy(szGeo, (char *)emm+2, emm[1]);
|
---|
113 | cs_log("[viaccess-reader] nano A6: geo %s", szGeo);
|
---|
114 | break;
|
---|
115 | }
|
---|
116 | case 0xB6:
|
---|
117 | {
|
---|
118 | uchar m; // modexp
|
---|
119 | struct via_date vd;
|
---|
120 |
|
---|
121 | m=emm[emm[1]+1];
|
---|
122 | parse_via_date(emm+2, &vd, 0);
|
---|
123 | cs_log("[viaccess-reader] nano B6: modexp %d%d%d%d%d%d: %02d/%02d/%04d", (m&0x20)?1:0,
|
---|
124 | (m&0x10)?1:0,(m&0x08)?1:0,(m&0x04)?1:0,(m&0x02)?1:0,(m&0x01)?1:0,
|
---|
125 | vd.day_s, vd.month_s, vd.year_s+1980);
|
---|
126 | break;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | static int chk_prov(uchar *id, uchar keynr)
|
---|
132 | {
|
---|
133 | int i, j, rc;
|
---|
134 | for (rc=i=0; (!rc) && (i<reader[ridx].nprov); i++)
|
---|
135 | if(!memcmp(&reader[ridx].prid[i][1], id, 3))
|
---|
136 | for (j=0; (!rc) && (j<16); j++)
|
---|
137 | if (reader[ridx].availkeys[i][j]==keynr)
|
---|
138 | rc=1;
|
---|
139 | return(rc);
|
---|
140 | }
|
---|
141 |
|
---|
142 | #define write_cmd(cmd, data) \
|
---|
143 | { \
|
---|
144 | if (card_write(cmd, data)) return ERROR; \
|
---|
145 | }
|
---|
146 |
|
---|
147 | #define read_cmd(cmd, data) \
|
---|
148 | { \
|
---|
149 | if (card_write(cmd, NULL)) return ERROR; \
|
---|
150 | }
|
---|
151 |
|
---|
152 | int viaccess_card_init(ATR newatr)
|
---|
153 | {
|
---|
154 | get_atr;
|
---|
155 | int i;
|
---|
156 | uchar buf[256];
|
---|
157 | static uchar insac[] = { 0xca, 0xac, 0x00, 0x00, 0x00 }; // select data
|
---|
158 | static uchar insb8[] = { 0xca, 0xb8, 0x00, 0x00, 0x00 }; // read selected data
|
---|
159 | static uchar insa4[] = { 0xca, 0xa4, 0x00, 0x00, 0x00 }; // select issuer
|
---|
160 | static uchar insc0[] = { 0xca, 0xc0, 0x00, 0x00, 0x00 }; // read data item
|
---|
161 |
|
---|
162 | static uchar insFAC[] = { 0x87, 0x02, 0x00, 0x00, 0x03 }; // init FAC
|
---|
163 | static uchar FacDat[] = { 0x00, 0x00, 0x28 };
|
---|
164 |
|
---|
165 | if ((atr[0]!=0x3f) || (atr[1]!=0x77) || ((atr[2]!=0x18) && (atr[2]!=0x11)) || (atr[9]!=0x68)) return ERROR;
|
---|
166 |
|
---|
167 | write_cmd(insFAC, FacDat);
|
---|
168 | if( !(cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0) )
|
---|
169 | return ERROR;
|
---|
170 |
|
---|
171 | // switch((atr[atrsize-4]<<8)|atr[atrsize-3])
|
---|
172 | // {
|
---|
173 | // case 0x6268: ver="2.3"; break;
|
---|
174 | // case 0x6668: ver="2.4(?)"; break;
|
---|
175 | // case 0xa268:
|
---|
176 | // default: ver="unknown"; break;
|
---|
177 | // }
|
---|
178 |
|
---|
179 | reader[ridx].caid[0]=0x500;
|
---|
180 | memset(reader[ridx].prid, 0xff, sizeof(reader[ridx].prid));
|
---|
181 | insac[2]=0xa4; write_cmd(insac, NULL); // request unique id
|
---|
182 | insb8[4]=0x07; read_cmd(insb8, NULL); // read unique id
|
---|
183 | memcpy(reader[ridx].hexserial, cta_res+2, 5);
|
---|
184 | // cs_log("[viaccess-reader] type: Viaccess, ver: %s serial: %llu", ver, b2ll(5, cta_res+2));
|
---|
185 | cs_ri_log("type: Viaccess (%sstandard atr), caid: %04X, serial: %llu",
|
---|
186 | atr[9]==0x68?"":"non-",reader[ridx].caid[0], b2ll(5, cta_res+2));
|
---|
187 |
|
---|
188 | i=0;
|
---|
189 | insa4[2]=0x00; write_cmd(insa4, NULL); // select issuer 0
|
---|
190 | buf[0]=0;
|
---|
191 | while((cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0))
|
---|
192 | {
|
---|
193 | insc0[4]=0x1a; read_cmd(insc0, NULL); // show provider properties
|
---|
194 | cta_res[2]&=0xF0;
|
---|
195 | reader[ridx].prid[i][0]=0;
|
---|
196 | memcpy(&reader[ridx].prid[i][1], cta_res, 3);
|
---|
197 | memcpy(&reader[ridx].availkeys[i][0], cta_res+10, 16);
|
---|
198 | sprintf((char *)buf+strlen((char *)buf), ",%06lX", b2i(3, &reader[ridx].prid[i][1]));
|
---|
199 | //cs_log("[viaccess-reader] buf: %s", buf);
|
---|
200 |
|
---|
201 | insac[2]=0xa5; write_cmd(insac, NULL); // request sa
|
---|
202 | insb8[4]=0x06; read_cmd(insb8, NULL); // read sa
|
---|
203 | memcpy(&reader[ridx].sa[i][0], cta_res+2, 4);
|
---|
204 |
|
---|
205 | /*
|
---|
206 | insac[2]=0xa7; write_cmd(insac, NULL); // request name
|
---|
207 | insb8[4]=0x02; read_cmd(insb8, NULL); // read name nano + len
|
---|
208 | l=cta_res[1];
|
---|
209 | insb8[4]=l; read_cmd(insb8, NULL); // read name
|
---|
210 | cta_res[l]=0;
|
---|
211 | cs_log("[viaccess-reader] name: %s", cta_res);
|
---|
212 | */
|
---|
213 |
|
---|
214 | insa4[2]=0x02;
|
---|
215 | write_cmd(insa4, NULL); // select next issuer
|
---|
216 | i++;
|
---|
217 | }
|
---|
218 | reader[ridx].nprov=i;
|
---|
219 | cs_ri_log("providers: %d (%s)", reader[ridx].nprov, buf+1);
|
---|
220 |
|
---|
221 | /* init the maybe existing aes key */
|
---|
222 | aes_set_key((char *)reader[ridx].aes_key);
|
---|
223 |
|
---|
224 | /* disabling parental lock. assuming pin "0000" */
|
---|
225 | if (cfg->ulparent) {
|
---|
226 | static uchar inDPL[] = {0xca, 0x24, 0x02, 0x00, 0x09};
|
---|
227 | static uchar cmDPL[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F};
|
---|
228 | write_cmd(inDPL,cmDPL);
|
---|
229 | if( !(cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0) )
|
---|
230 | cs_log("[viaccess-reader] Can't disable parental lock. Wrong PIN? I assumed 0000!");
|
---|
231 | else
|
---|
232 | cs_log("[viaccess-reader] Parental lock disabled");
|
---|
233 | }
|
---|
234 |
|
---|
235 | cs_log("[viaccess-reader] ready for requests");
|
---|
236 | memset(&last_geo, 0, sizeof(last_geo));
|
---|
237 | return OK;
|
---|
238 | }
|
---|
239 |
|
---|
240 | int viaccess_do_ecm(ECM_REQUEST *er)
|
---|
241 | {
|
---|
242 | static unsigned char insa4[] = { 0xca,0xa4,0x04,0x00,0x03 }; // set provider id
|
---|
243 | static unsigned char ins88[] = { 0xca,0x88,0x00,0x00,0x00 }; // set ecm
|
---|
244 | static unsigned char insf8[] = { 0xca,0xf8,0x00,0x00,0x00 }; // set geographic info
|
---|
245 | static unsigned char insc0[] = { 0xca,0xc0,0x00,0x00,0x12 }; // read dcw
|
---|
246 |
|
---|
247 | const uchar *ecm88Data=er->ecm+4; //XXX what is the 4th byte for ??
|
---|
248 | int ecm88Len=SCT_LEN(er->ecm)-4;
|
---|
249 | ulong provid;
|
---|
250 | int rc=0;
|
---|
251 | int hasD2 = 0;
|
---|
252 | uchar DE04[256];
|
---|
253 |
|
---|
254 | if(ecm88Data[0]==0xd2)
|
---|
255 | {
|
---|
256 | // FIXME: use the d2 arguments
|
---|
257 | int len = ecm88Data[1] + 2;
|
---|
258 | ecm88Data += len;
|
---|
259 | ecm88Len -= len;
|
---|
260 | hasD2 = 1;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if ((ecm88Data[0]==0x90 || ecm88Data[0]==0x40) && ecm88Data[1]==0x03)
|
---|
264 | {
|
---|
265 | uchar ident[3], keynr;
|
---|
266 | //uchar buff[256]; // MAX_LEN
|
---|
267 | uchar *ecmf8Data=0;
|
---|
268 | int ecmf8Len=0;
|
---|
269 |
|
---|
270 | memcpy (ident, &ecm88Data[2], sizeof(ident));
|
---|
271 | provid = b2i(3, ident);
|
---|
272 | ident[2]&=0xF0;
|
---|
273 | keynr=ecm88Data[4]&0x0F;
|
---|
274 | if (!chk_prov(ident, keynr))
|
---|
275 | {
|
---|
276 | cs_debug("[viaccess-reader] EMM: provider or key not found on card");
|
---|
277 | return ERROR;
|
---|
278 | }
|
---|
279 | ecm88Data+=5;
|
---|
280 | ecm88Len-=5;
|
---|
281 |
|
---|
282 | // DE04
|
---|
283 | if (ecm88Data[0]==0xDE && ecm88Data[1]==0x04)
|
---|
284 | {
|
---|
285 | memcpy (DE04, &ecm88Data[0], 6);
|
---|
286 | ecm88Data+=6;
|
---|
287 | }
|
---|
288 | //
|
---|
289 |
|
---|
290 | if( last_geo.provid != provid )
|
---|
291 | {
|
---|
292 | last_geo.provid = provid;
|
---|
293 | last_geo.geo_len = 0;
|
---|
294 | last_geo.geo[0] = 0;
|
---|
295 | write_cmd(insa4, ident); // set provider
|
---|
296 | }
|
---|
297 |
|
---|
298 | while(ecm88Len>0 && ecm88Data[0]<0xA0)
|
---|
299 | {
|
---|
300 | int nanoLen=ecm88Data[1]+2;
|
---|
301 | if (!ecmf8Data)
|
---|
302 | ecmf8Data=(uchar *)ecm88Data;
|
---|
303 | ecmf8Len+=nanoLen;
|
---|
304 | ecm88Len-=nanoLen;
|
---|
305 | ecm88Data+=nanoLen;
|
---|
306 | }
|
---|
307 | if(ecmf8Len)
|
---|
308 | {
|
---|
309 | if( last_geo.geo_len!=ecmf8Len ||
|
---|
310 | memcmp(last_geo.geo, ecmf8Data, last_geo.geo_len))
|
---|
311 | {
|
---|
312 | memcpy(last_geo.geo, ecmf8Data, ecmf8Len);
|
---|
313 | last_geo.geo_len= ecmf8Len;
|
---|
314 | insf8[3]=keynr;
|
---|
315 | insf8[4]=ecmf8Len;
|
---|
316 | write_cmd(insf8, ecmf8Data);
|
---|
317 | }
|
---|
318 | }
|
---|
319 | ins88[2]=ecmf8Len?1:0;
|
---|
320 | ins88[3]=keynr;
|
---|
321 | ins88[4]=ecm88Len;
|
---|
322 |
|
---|
323 | // DE04
|
---|
324 | if (DE04[0]==0xDE)
|
---|
325 | {
|
---|
326 | memcpy(DE04+6, (uchar *)ecm88Data, ecm88Len-6);
|
---|
327 | write_cmd(ins88, DE04); // request dcw
|
---|
328 | }
|
---|
329 | else
|
---|
330 | {
|
---|
331 | write_cmd(ins88, (uchar *)ecm88Data); // request dcw
|
---|
332 | }
|
---|
333 | //
|
---|
334 |
|
---|
335 | read_cmd(insc0, NULL); // read dcw
|
---|
336 | switch(cta_res[0])
|
---|
337 | {
|
---|
338 | case 0xe8: // even
|
---|
339 | if(cta_res[1]==8) { memcpy(er->cw,cta_res+2,8); rc=1; }
|
---|
340 | break;
|
---|
341 | case 0xe9: // odd
|
---|
342 | if(cta_res[1]==8) { memcpy(er->cw+8,cta_res+2,8); rc=1; }
|
---|
343 | break;
|
---|
344 | case 0xea: // complete
|
---|
345 | if(cta_res[1]==16) { memcpy(er->cw,cta_res+2,16); rc=1; }
|
---|
346 | break;
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | if (hasD2) {
|
---|
351 | aes_decrypt(er->cw, 16);
|
---|
352 | }
|
---|
353 |
|
---|
354 | return(rc?OK:ERROR);
|
---|
355 | }
|
---|
356 |
|
---|
357 | int viaccess_do_emm(EMM_PACKET *ep)
|
---|
358 | {
|
---|
359 | static unsigned char insa4[] = { 0xca,0xa4,0x04,0x00,0x03 }; // set provider id
|
---|
360 | static unsigned char insf0[] = { 0xca,0xf0,0x00,0x01,0x22 }; // set adf
|
---|
361 | static unsigned char insf4[] = { 0xca,0xf4,0x00,0x01,0x00 }; // set adf, encrypted
|
---|
362 | static unsigned char ins18[] = { 0xca,0x18,0x01,0x01,0x00 }; // set subscription
|
---|
363 | static unsigned char ins1c[] = { 0xca,0x1c,0x01,0x01,0x00 }; // set subscription, encrypted
|
---|
364 | static unsigned char insc8[] = { 0xca,0xc8,0x00,0x00,0x02 }; // read extended status
|
---|
365 | static unsigned char insc8Data[] = { 0x00,0x00 }; // data for read extended status
|
---|
366 |
|
---|
367 | int emmLen=SCT_LEN(ep->emm)-7;
|
---|
368 | int rc=0;
|
---|
369 |
|
---|
370 | ///cs_dump(ep->emm, emmLen+7, "RECEIVED EMM VIACCESS");
|
---|
371 |
|
---|
372 | int emmUpToEnd;
|
---|
373 | uchar *emmParsed = ep->emm+7;
|
---|
374 | int provider_ok = 0;
|
---|
375 | uchar keynr = 0;
|
---|
376 | int ins18Len = 0;
|
---|
377 | uchar ins18Data[512];
|
---|
378 | uchar insData[512];
|
---|
379 | uchar *nano81Data = 0;
|
---|
380 | uchar *nano91Data = 0;
|
---|
381 | uchar *nano92Data = 0;
|
---|
382 | uchar *nano9EData = 0;
|
---|
383 | uchar *nanoF0Data = 0;
|
---|
384 |
|
---|
385 | for (emmUpToEnd=emmLen; (emmParsed[1] != 0) && (emmUpToEnd > 0); emmUpToEnd -= (2 + emmParsed[1]), emmParsed += (2 + emmParsed[1])) {
|
---|
386 | ///cs_dump (emmParsed, emmParsed[1] + 2, "NANO");
|
---|
387 |
|
---|
388 | if (emmParsed[0]==0x90 && emmParsed[1]==0x03) {
|
---|
389 | /* identification of the service operator */
|
---|
390 |
|
---|
391 | uchar soid[3], ident[3], i;
|
---|
392 |
|
---|
393 | for (i=0; i<3; i++) {
|
---|
394 | soid[i]=ident[i]=emmParsed[2+i];
|
---|
395 | }
|
---|
396 | ident[2]&=0xF0;
|
---|
397 | keynr=soid[2]&0x0F;
|
---|
398 | if (chk_prov(ident, keynr)) {
|
---|
399 | provider_ok = 1;
|
---|
400 | } else {
|
---|
401 | cs_debug("[viaccess-reader] EMM: provider or key not found on card (%x, %x)", ident, keynr);
|
---|
402 | return ERROR;
|
---|
403 | }
|
---|
404 |
|
---|
405 | // set provider
|
---|
406 | write_cmd(insa4, soid);
|
---|
407 | if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
408 | cs_dump(insa4, 5, "set provider cmd:");
|
---|
409 | cs_dump(soid, 3, "set provider data:");
|
---|
410 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
411 | return ERROR;
|
---|
412 | }
|
---|
413 | } else if (emmParsed[0]==0x9e && emmParsed[1]==0x20) {
|
---|
414 | /* adf */
|
---|
415 |
|
---|
416 | if (!nano91Data) {
|
---|
417 | /* adf is not crypted, so test it */
|
---|
418 |
|
---|
419 | uchar custwp;
|
---|
420 | uchar *afd;
|
---|
421 |
|
---|
422 | custwp=reader[ridx].sa[0][3];
|
---|
423 | afd=(uchar*)emmParsed+2;
|
---|
424 |
|
---|
425 | if( afd[31-custwp/8] & (1 << (custwp & 7)) )
|
---|
426 | cs_debug("[viaccess-reader] emm for our card %08X", b2i(4, &reader[ridx].sa[0][0]));
|
---|
427 | else
|
---|
428 | return SKIPPED;
|
---|
429 | }
|
---|
430 |
|
---|
431 | // memorize
|
---|
432 | nano9EData = emmParsed;
|
---|
433 |
|
---|
434 | } else if (emmParsed[0]==0x81) {
|
---|
435 | nano81Data = emmParsed;
|
---|
436 | } else if (emmParsed[0]==0x91 && emmParsed[1]==0x08) {
|
---|
437 | nano91Data = emmParsed;
|
---|
438 | } else if (emmParsed[0]==0x92 && emmParsed[1]==0x08) {
|
---|
439 | nano92Data = emmParsed;
|
---|
440 | } else if (emmParsed[0]==0xF0 && emmParsed[1]==0x08) {
|
---|
441 | nanoF0Data = emmParsed;
|
---|
442 | } else if (emmParsed[0]==0x1D && emmParsed[0]==0x01 && emmParsed[0]==0x01) {
|
---|
443 | /* from cccam... skip it... */
|
---|
444 | } else {
|
---|
445 | /* other nanos */
|
---|
446 | show_subs(emmParsed);
|
---|
447 |
|
---|
448 | memcpy(ins18Data+ins18Len, emmParsed, emmParsed[1] + 2);
|
---|
449 | ins18Len += emmParsed [1] + 2;
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | if (!provider_ok) {
|
---|
454 | cs_debug("[viaccess-reader] provider not found in emm, continue anyway");
|
---|
455 | // force key to 1...
|
---|
456 | keynr = 1;
|
---|
457 | ///return ERROR;
|
---|
458 | }
|
---|
459 |
|
---|
460 | if (!nanoF0Data) {
|
---|
461 | cs_dump(ep->emm, ep->l, "can't find 0xf0 in emm...");
|
---|
462 | return ERROR; // error
|
---|
463 | }
|
---|
464 |
|
---|
465 | if (nano9EData) {
|
---|
466 | if (!nano91Data) {
|
---|
467 | // set adf
|
---|
468 | insf0[3] = keynr; // key
|
---|
469 | write_cmd(insf0, nano9EData);
|
---|
470 | if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
471 | cs_dump(insf0, 5, "set adf cmd:");
|
---|
472 | cs_dump(nano9EData, 0x22, "set adf data:");
|
---|
473 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
474 | return ERROR;
|
---|
475 | }
|
---|
476 | } else {
|
---|
477 | // set adf crypte
|
---|
478 | insf4[3] = keynr; // key
|
---|
479 | insf4[4] = nano91Data[1] + 2 + nano9EData[1] + 2;
|
---|
480 | memcpy (insData, nano91Data, nano91Data[1] + 2);
|
---|
481 | memcpy (insData + nano91Data[1] + 2, nano9EData, nano9EData[1] + 2);
|
---|
482 | write_cmd(insf4, insData);
|
---|
483 | if(( cta_res[cta_lr-2]!=0x90 && cta_res[cta_lr-2]!=0x91) || cta_res[cta_lr-1]!=0x00 ) {
|
---|
484 | cs_dump(insf4, 5, "set adf encrypted cmd:");
|
---|
485 | cs_dump(insData, insf4[4], "set adf encrypted data:");
|
---|
486 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
487 | return ERROR;
|
---|
488 | }
|
---|
489 | }
|
---|
490 | }
|
---|
491 |
|
---|
492 | if (!nano92Data) {
|
---|
493 | // send subscription
|
---|
494 | ins18[4] = ins18Len + nanoF0Data[1] + 2;
|
---|
495 | memcpy (insData, ins18Data, ins18Len);
|
---|
496 | memcpy (insData + ins18Len, nanoF0Data, nanoF0Data[1] + 2);
|
---|
497 | write_cmd(ins18, insData);
|
---|
498 | if( cta_res[cta_lr-2]==0x90 && cta_res[cta_lr-1]==0x00 ) {
|
---|
499 | cs_debug("[viaccess-reader] update successfully written");
|
---|
500 | rc=1; // written
|
---|
501 | } else {
|
---|
502 | cs_dump(ins18, 5, "set subscription cmd:");
|
---|
503 | cs_dump(insData, ins18[4], "set subscription data:");
|
---|
504 | cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
505 | }
|
---|
506 |
|
---|
507 | } else {
|
---|
508 | // send subscription encrypted
|
---|
509 |
|
---|
510 | if (!nano81Data) {
|
---|
511 | cs_dump(ep->emm, ep->l, "0x92 found, but can't find 0x81 in emm...");
|
---|
512 | return ERROR; // error
|
---|
513 | }
|
---|
514 |
|
---|
515 | ins1c[3] = keynr; // key
|
---|
516 | ins1c[4] = nano92Data[1] + 2 + nano81Data[1] + 2 + nanoF0Data[1] + 2;
|
---|
517 | memcpy (insData, nano92Data, nano92Data[1] + 2);
|
---|
518 | memcpy (insData + nano92Data[1] + 2, nano81Data, nano81Data[1] + 2);
|
---|
519 | memcpy (insData + nano92Data[1] + 2 + nano81Data[1] + 2, nanoF0Data, nanoF0Data[1] + 2);
|
---|
520 | write_cmd(ins1c, insData);
|
---|
521 | if( cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
522 | /* maybe a 2nd level status, so read it */
|
---|
523 | ///cs_dump(ins1c, 5, "set subscription encrypted cmd:");
|
---|
524 | ///cs_dump(insData, ins1c[4], "set subscription encrypted data:");
|
---|
525 | ///cs_log("[viaccess-reader] update error: %02X %02X", cta_res[cta_lr-2], cta_res[cta_lr-1]);
|
---|
526 |
|
---|
527 | //read_cmd(insc8, insc8Data); this cannot be right, insc8Data would be ignored -- dingo35
|
---|
528 | write_cmd(insc8, insc8Data);
|
---|
529 | if( cta_res[0] != 0x00 || cta_res[1] != 00 || cta_res[cta_lr-2]!=0x90 || cta_res[cta_lr-1]!=0x00 ) {
|
---|
530 | ///cs_dump(cta_res, cta_lr, "extended status error:");
|
---|
531 | return ERROR;
|
---|
532 | } else {
|
---|
533 | cs_debug("[viaccess-reader] update successfully written (with extended status OK)");
|
---|
534 | rc=1; // written
|
---|
535 | }
|
---|
536 | } else {
|
---|
537 | cs_debug("[viaccess-reader] update successfully written");
|
---|
538 | rc=1; // written
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | memset(&last_geo, 0, sizeof(last_geo));
|
---|
543 |
|
---|
544 | /*
|
---|
545 | Sub Main()
|
---|
546 | Sc.Write("CA A4 04 00 03")
|
---|
547 | RX
|
---|
548 | Sc.Write("02 07 11")
|
---|
549 | RX
|
---|
550 | Sc.Write("CA F0 00 01 22")
|
---|
551 | RX
|
---|
552 | Sc.Write("9E 20")
|
---|
553 | Sc.Write("10 10 08 8A 80 00 04 00 10 10 26 E8 54 80 1E 80")
|
---|
554 | Sc.Write("00 01 00 00 00 00 00 50 00 00 80 02 22 00 08 50")
|
---|
555 | RX
|
---|
556 | Sc.Write("CA 18 01 01 11")
|
---|
557 | RX
|
---|
558 | Sc.Write("A9 05 34 DE 34 FF 80")
|
---|
559 | Sc.Write("F0 08 1A 3E AF B5 2B EE E3 3B")
|
---|
560 | RX
|
---|
561 |
|
---|
562 | End Sub
|
---|
563 | */
|
---|
564 | return rc;
|
---|
565 | }
|
---|
566 |
|
---|
567 | int viaccess_card_info(void)
|
---|
568 | {
|
---|
569 | int i, l, scls, show_cls;
|
---|
570 | static uchar insac[] = { 0xca, 0xac, 0x00, 0x00, 0x00 }; // select data
|
---|
571 | static uchar insb8[] = { 0xca, 0xb8, 0x00, 0x00, 0x00 }; // read selected data
|
---|
572 | static uchar insa4[] = { 0xca, 0xa4, 0x00, 0x00, 0x00 }; // select issuer
|
---|
573 | static uchar insc0[] = { 0xca, 0xc0, 0x00, 0x00, 0x00 }; // read data item
|
---|
574 | static uchar ins24[] = { 0xca, 0x24, 0x00, 0x00, 0x09 }; // set pin
|
---|
575 |
|
---|
576 | static uchar cls[] = { 0x00, 0x21, 0xff, 0x9f};
|
---|
577 | static uchar pin[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04};
|
---|
578 |
|
---|
579 | show_cls=reader[ridx].show_cls;
|
---|
580 | memset(&last_geo, 0, sizeof(last_geo));
|
---|
581 |
|
---|
582 | cs_log("[viaccess-reader] card detected");
|
---|
583 |
|
---|
584 | // set pin
|
---|
585 | write_cmd(ins24, pin);
|
---|
586 |
|
---|
587 | insac[2]=0xa4; write_cmd(insac, NULL); // request unique id
|
---|
588 | insb8[4]=0x07; read_cmd(insb8, NULL); // read unique id
|
---|
589 | cs_log("[viaccess-reader] serial: %llu", b2ll(5, cta_res+2));
|
---|
590 |
|
---|
591 | scls=0;
|
---|
592 | insa4[2]=0x00; write_cmd(insa4, NULL); // select issuer 0
|
---|
593 | for (i=1; (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0); i++)
|
---|
594 | {
|
---|
595 | ulong l_provid, l_sa;
|
---|
596 | uchar l_name[64];
|
---|
597 | insc0[4]=0x1a; read_cmd(insc0, NULL); // show provider properties
|
---|
598 | cta_res[2]&=0xF0;
|
---|
599 | l_provid=b2i(3, cta_res);
|
---|
600 |
|
---|
601 | insac[2]=0xa5; write_cmd(insac, NULL); // request sa
|
---|
602 | insb8[4]=0x06; read_cmd(insb8, NULL); // read sa
|
---|
603 | l_sa=b2i(4, cta_res+2);
|
---|
604 |
|
---|
605 | insac[2]=0xa7; write_cmd(insac, NULL); // request name
|
---|
606 | insb8[4]=0x02; read_cmd(insb8, NULL); // read name nano + len
|
---|
607 | l=cta_res[1];
|
---|
608 | insb8[4]=l; read_cmd(insb8, NULL); // read name
|
---|
609 | cta_res[l]=0;
|
---|
610 | trim((char *)cta_res);
|
---|
611 | if (cta_res[0])
|
---|
612 | snprintf((char *)l_name, sizeof(l_name), ", name: %s", cta_res);
|
---|
613 | else
|
---|
614 | l_name[0]=0;
|
---|
615 |
|
---|
616 | // read GEO
|
---|
617 | insac[2]=0xa6; write_cmd(insac, NULL); // request GEO
|
---|
618 | insb8[4]=0x02; read_cmd(insb8, NULL); // read GEO nano + len
|
---|
619 | l=cta_res[1];
|
---|
620 | insb8[4]=l; read_cmd(insb8, NULL); // read geo
|
---|
621 | cs_ri_log("provider: %d, id: %06X%s, sa: %08X, geo: %s",
|
---|
622 | i, l_provid, l_name, l_sa, (l<4) ? "empty" : cs_hexdump(1, cta_res, l));
|
---|
623 |
|
---|
624 | // read classes subscription
|
---|
625 | insac[2]=0xa9; insac[4]=4;
|
---|
626 | write_cmd(insac, cls); // request class subs
|
---|
627 | scls=0;
|
---|
628 | while( (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0) )
|
---|
629 | {
|
---|
630 | insb8[4]=0x02; read_cmd(insb8, NULL); // read class subs nano + len
|
---|
631 | if( (cta_res[cta_lr-2]==0x90) && (cta_res[cta_lr-1]==0) )
|
---|
632 | {
|
---|
633 | int fshow;
|
---|
634 | l=cta_res[1];
|
---|
635 | //fshow=(client[cs_idx].dbglvl==D_DUMP)?1:(scls < show_cls)?1:0;
|
---|
636 | fshow=(scls<show_cls);
|
---|
637 | insb8[4]=l; read_cmd(insb8, NULL); // read class subs
|
---|
638 | if( (cta_res[cta_lr-2]==0x90) && (fshow) &&
|
---|
639 | (cta_res[cta_lr-1]==0x00 || cta_res[cta_lr-1]==0x08) )
|
---|
640 | {
|
---|
641 | show_class(NULL, cta_res, cta_lr-2);
|
---|
642 | scls++;
|
---|
643 | }
|
---|
644 | }
|
---|
645 | }
|
---|
646 |
|
---|
647 | insac[4]=0;
|
---|
648 | insa4[2]=0x02;
|
---|
649 | write_cmd(insa4, NULL); // select next provider
|
---|
650 | }
|
---|
651 | //return ERROR;
|
---|
652 | return OK;
|
---|
653 | }
|
---|