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