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