source: trunk/reader-conax.c@ 4149

Last change on this file since 4149 was 3893, checked in by schlocke, 13 years ago

webinf/Entitlements cccam: OK

File size: 9.0 KB
Line 
1#include "globals.h"
2#include "reader-common.h"
3
4static char *chid_date(const uchar *ptr, char *buf, int l)
5{
6 if (buf)
7 {
8 snprintf(buf, l, "%04d/%02d/%02d",
9 1990+(ptr[1]>>4)+(((ptr[0]>>5)&7)*10), ptr[1]&0xf, ptr[0]&0x1f);
10 }
11 return(buf);
12}
13
14static int read_record(struct s_reader * reader, const uchar *cmd, const uchar *data, uchar * cta_res)
15{
16 ushort cta_lr;
17 uchar insCA[] = {0xDD, 0xCA, 0x00, 0x00, 0x00};
18
19 write_cmd(cmd, data); // select record
20 if (cta_res[0]!=0x98)
21 return(-1);
22
23 insCA[4]=cta_res[1]; // get len
24 write_cmd(insCA, NULL); // read record
25 if ((cta_res[cta_lr-2]!=0x90) || (cta_res[cta_lr-1]))
26 return(-1);
27 return(cta_lr-2);
28}
29
30static int conax_card_init(struct s_reader * reader, ATR newatr)
31{
32 unsigned char cta_res[CTA_RES_LEN];
33 int i, j, n;
34 static const uchar ins26[] = {0xDD, 0x26, 0x00, 0x00, 0x03, 0x10, 0x01, 0x40};
35 uchar ins82[] = {0xDD, 0x82, 0x00, 0x00, 0x11, 0x11, 0x0f, 0x01, 0xb0, 0x0f, 0xff, \
36 0xff, 0xfb, 0x00, 0x00, 0x09, 0x04, 0x0b, 0x00, 0xe0, 0x30, 0x2b };
37
38 uchar cardver=0;
39
40 get_hist;
41 if ((hist_size < 4) || (memcmp(hist,"0B00",4)))
42 return ERROR;
43
44 reader->caid[0]=0xB00;
45
46 if ((n=read_record(reader, ins26, ins26+5, cta_res))<=0) return ERROR; // read caid, card-version
47
48 for (i=0; i<n; i+=cta_res[i+1]+2)
49 switch(cta_res[i])
50 {
51 case 0x20: cardver=cta_res[i+2]; break;
52 case 0x28: reader->caid[0]=(cta_res[i+2]<<8)|cta_res[i+3];
53 }
54
55 // Ins82 command needs to use the correct CAID reported in nano 0x28
56 ins82[17]=(reader->caid[0]>>8)&0xFF;
57 ins82[18]=(reader->caid[0])&0xFF;
58
59 if ((n=read_record(reader, ins82, ins82+5, cta_res))<=0) return ERROR; // read serial
60
61 for (j=0, i=2; i<n; i+=cta_res[i+1]+2)
62 switch(cta_res[i])
63 {
64 case 0x23:
65 if (cta_res[i+5] != 0x00) {
66 memcpy(reader->hexserial, &cta_res[i+3], 6);
67 }
68 else {
69 memcpy(reader->sa[j], &cta_res[i+5], 4);
70 j++;
71 }
72 break;
73 }
74
75 // we have one provider, 0x0000
76 reader->nprov = 1;
77 memset(reader->prid, 0x00, sizeof(reader->prid));
78
79 cs_ri_log(reader, "type: Conax, caid: %04X, serial: %llu, hex serial: %02x%02x%02x%02x, card: v%d",
80 reader->caid[0], b2ll(6, reader->hexserial), reader->hexserial[2],
81 reader->hexserial[3], reader->hexserial[4], reader->hexserial[5], cardver);
82
83 cs_ri_log(reader, "Providers: %d", reader->nprov);
84
85 for (j=0; j<reader->nprov; j++)
86 {
87 cs_ri_log(reader, "Provider: %d Provider-Id: %06X", j+1, b2ll(4, reader->prid[j]));
88 cs_ri_log(reader, "Provider: %d SharedAddress: %08X", j+1, b2ll(4, reader->sa[j]));
89 }
90
91 return OK;
92}
93
94static int conax_send_pin(struct s_reader * reader)
95{
96 def_resp;
97 unsigned char insPIN[] = { 0xDD,0xC8,0x00,0x00,0x07,0x1D,0x05,0x01,0x00,0x00,0x00,0x00 }; //Last four are the Pin-Code
98 memcpy(insPIN+8,reader->pincode,4);
99
100 write_cmd(insPIN, insPIN+5);
101 cs_ri_log(reader, "sending pincode to card");
102
103 return OK;
104}
105
106
107static int conax_do_ecm(struct s_reader * reader, ECM_REQUEST *er)
108{
109 def_resp;
110 int i,j,n, rc=0;
111 unsigned char insA2[] = { 0xDD,0xA2,0x00,0x00,0x00 };
112 unsigned char insCA[] = { 0xDD,0xCA,0x00,0x00,0x00 };
113
114 unsigned char buf[256];
115
116 if ((n=check_sct_len(er->ecm, 3))<0)
117 return ERROR;
118
119 buf[0]=0x14;
120 buf[1]=n+1;
121 buf[2]=0;
122
123 memcpy(buf+3, er->ecm, n);
124 insA2[4]=n+3;
125
126 write_cmd(insA2, buf); // write Header + ECM
127
128 while ((cta_res[cta_lr-2]==0x98) && // Antwort
129 ((insCA[4]=cta_res[cta_lr-1])>0) && (insCA[4]!=0xFF))
130 {
131 write_cmd(insCA, NULL); //Codeword auslesen
132
133 if ((cta_res[cta_lr-2]==0x98) ||
134 ((cta_res[cta_lr-2]==0x90) ))
135 {
136 for(i=0; i<cta_lr-2; i+=cta_res[i+1]+2)
137 {
138 switch (cta_res[i])
139 {
140 case 0x25:
141 if ( (cta_res[i+1]>=0xD) && !((n=cta_res[i+4])&0xFE) )
142 {
143 rc|=(1<<n);
144 memcpy(er->cw+(n<<3), cta_res+i+7, 8);
145 }
146 break;
147 case 0x31:
148 if ( (cta_res[i+1]==0x02 && cta_res[i+2]==0x00 && cta_res[i+3]==0x00) || \
149 (cta_res[i+1]==0x02 && cta_res[i+2]==0x40 && cta_res[i+3]==0x00) )
150 break;
151 else if (strcmp(reader->pincode, "none"))
152 {
153 conax_send_pin(reader);
154 write_cmd(insA2, buf); // write Header + ECM
155
156 while ((cta_res[cta_lr-2]==0x98) && // Antwort
157 ((insCA[4]=cta_res[cta_lr-1])>0) && (insCA[4]!=0xFF))
158 {
159 write_cmd(insCA, NULL); //Codeword auslesen
160
161 if ((cta_res[cta_lr-2]==0x98) ||
162 ((cta_res[cta_lr-2]==0x90) && (!cta_res[cta_lr-1])))
163 {
164 for(j=0;j<cta_lr-2; j+=cta_res[j+1]+2)
165 if ((cta_res[j]==0x25) && // access: is cw
166 (cta_res[j+1]>=0xD) && // 0xD: 5 header + 8 cw
167 !((n=cta_res[j+4])&0xFE)) // cw idx must be 0 or 1
168 {
169 rc|=(1<<n);
170 memcpy(er->cw+(n<<3), cta_res+j+7, 8);
171 }
172 }
173 }
174 }
175 break;
176 }
177 }
178 }
179 }
180 if (rc==3)
181 return OK;
182 else
183 return ERROR;
184}
185
186static int conax_get_emm_type(EMM_PACKET *ep, struct s_reader * rdr)
187{
188 int i, ok = 0;
189
190 cs_debug_mask(D_EMM, "Entered conax_get_emm_type ep->emm[2]=%02x", ep->emm[2]);
191
192 for (i = 0; i < rdr->nprov; i++) {
193 ok = (!memcmp(&ep->emm[6], rdr->sa[i], 4));
194 if (ok) break;
195 }
196
197 if (ok) {
198 ep->type = SHARED;
199 memset(ep->hexserial, 0, 8);
200 memcpy(ep->hexserial, &ep->emm[6], 4);
201 cs_debug_mask(D_EMM, "CONAX EMM: SHARED, ep->hexserial = %s", cs_hexdump(1, ep->hexserial, 8));
202 return TRUE;
203 }
204 else {
205 if (!memcmp(&ep->emm[6], rdr->hexserial+2, 4)) {
206 ep->type = UNIQUE;
207 memset(ep->hexserial, 0, 8);
208 memcpy(ep->hexserial+2, &ep->emm[6], 4);
209 cs_debug_mask(D_EMM, "CONAX EMM: UNIQUE, ep->hexserial = %s", cs_hexdump(1, ep->hexserial, 8));
210 return TRUE;
211 }
212 else {
213 ep->type = GLOBAL;
214 cs_debug_mask(D_EMM, "CONAX EMM: GLOBAL");
215 memset(ep->hexserial, 0, 8);
216 return TRUE;
217 }
218 }
219}
220
221static void conax_get_emm_filter(struct s_reader * rdr, uchar *filter)
222{
223 filter[0]=0xFF; //header
224 filter[1]=3; //filter count
225
226 filter[2]=GLOBAL;
227 filter[3]=1; // FIXME: dont see any conax global EMM yet
228
229 filter[4+0] = 0x82;
230 filter[4+0+16] = 0xFF;
231 filter[4+8] = 0x70;
232 filter[4+8+16] = 0xFF;
233
234 filter[36]=SHARED;
235 filter[37]=0;
236
237 filter[38+0] = 0x82;
238 filter[38+0+16] = 0xFF;
239 filter[38+8] = 0x70;
240 filter[38+8+16] = 0xFF;
241 memcpy(filter+38+4, rdr->sa[0], 4);
242 memset(filter+38+4+16, 0xFF, 4);
243
244 filter[70]=UNIQUE;
245 filter[71]=0;
246 filter[72+0] = 0x82;
247 filter[72+0+16] = 0xFF;
248 filter[72+8] = 0x70;
249 filter[72+8+16] = 0xFF;
250 memcpy(filter+72+4, rdr->hexserial + 2, 4);
251 memset(filter+72+4+16, 0xFF, 4);
252
253 return;
254}
255
256static int conax_do_emm(struct s_reader * reader, EMM_PACKET *ep)
257{
258 def_resp;
259 unsigned char insEMM[] = { 0xDD,0x84,0x00,0x00,0x00 };
260 unsigned char buf[255];
261 int rc=0;
262
263 const int l = ep->emm[2];
264
265 insEMM[4]=l+5;
266 buf[0]=0x12;
267 buf[1]=l+3;
268 memcpy(buf+2, ep->emm, buf[1]);
269 write_cmd(insEMM, buf);
270
271 rc=((cta_res[0]==0x90)&&(cta_res[1]==0x00));
272
273 if (rc)
274 return OK;
275 else
276 return ERROR;
277}
278
279static int conax_card_info(struct s_reader * reader)
280{
281 def_resp;
282 int type, i, j, k, n=0;
283 ushort provid;
284 char provname[32], pdate[32];
285 static const uchar insC6[] = {0xDD, 0xC6, 0x00, 0x00, 0x03, 0x1C, 0x01, 0x00};
286 static const uchar ins26[] = {0xDD, 0x26, 0x00, 0x00, 0x03, 0x1C, 0x01, 0x01};
287 uchar insCA[] = {0xDD, 0xCA, 0x00, 0x00, 0x00};
288 char *txt[] = { "Package", "PPV-Event" };
289 static const uchar *cmd[] = { insC6, ins26 };
290
291 for (type=0; type<2; type++)
292 {
293 n=0;
294 write_cmd(cmd[type], cmd[type]+5);
295 while (cta_res[cta_lr-2]==0x98)
296 {
297 insCA[4]=cta_res[cta_lr-1]; // get len
298 write_cmd(insCA, NULL); // read
299 if ((cta_res[cta_lr-2]==0x90) || (cta_res[cta_lr-2]==0x98))
300 {
301 for (j=0; j<cta_lr-2; j+=cta_res[j+1]+2)
302 {
303 provid=(cta_res[j+2+type]<<8) | cta_res[j+3+type];
304 for (k=0, i=j+4+type; (i<j+cta_res[j+1]) && (k<2); i+=cta_res[i+1]+2)
305 {
306 int l;
307 switch(cta_res[i])
308 {
309 case 0x01: l=(cta_res[i+1]<(sizeof(provname)-1)) ?
310 cta_res[i+1] : sizeof(provname)-1;
311 memcpy(provname, cta_res+i+2, l);
312 provname[l]='\0';
313 break;
314 case 0x30: chid_date(cta_res+i+2, pdate+(k++<<4), 15);
315 break;
316 }
317 }
318 cs_ri_log(reader, "%s: %d, id: %04X, date: %s - %s, name: %s",
319 txt[type], ++n, provid, pdate, pdate+16, trim(provname));
320 }
321 }
322 }
323 }
324 cs_log("[conax-reader] ready for requests");
325 return OK;
326}
327
328void reader_conax(struct s_cardsystem *ph)
329{
330 ph->do_emm=conax_do_emm;
331 ph->do_ecm=conax_do_ecm;
332 ph->card_info=conax_card_info;
333 ph->card_init=conax_card_init;
334 ph->get_emm_type=conax_get_emm_type;
335 ph->get_emm_filter=conax_get_emm_filter;
336 ph->caids[0]=0x0B;
337 ph->desc="conax";
338}
Note: See TracBrowser for help on using the repository browser.