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