1 | #include "globals.h"
|
---|
2 | #include "reader-common.h"
|
---|
3 |
|
---|
4 | static BIGNUM exp, ucpk;
|
---|
5 |
|
---|
6 | extern uchar cta_res[];
|
---|
7 | extern ushort cta_lr;
|
---|
8 |
|
---|
9 | #define CMD_LEN 5
|
---|
10 |
|
---|
11 | void RotateBytes1(unsigned char *out, unsigned char *in, int n)
|
---|
12 | {
|
---|
13 | // loop is executed atleast once, so it's not a good idea to
|
---|
14 | // call with n=0 !!
|
---|
15 | out+=n;
|
---|
16 | do { *(--out)=*(in++); } while(--n);
|
---|
17 | }
|
---|
18 |
|
---|
19 | void RotateBytes2(unsigned char *in, int n)
|
---|
20 | {
|
---|
21 | // loop is executed atleast once, so it's not a good idea to
|
---|
22 | // call with n=0 !!
|
---|
23 | unsigned char *e=in+n-1;
|
---|
24 | do
|
---|
25 | {
|
---|
26 | unsigned char temp=*in;
|
---|
27 | *in++=*e;
|
---|
28 | *e-- =temp;
|
---|
29 | } while(in<e);
|
---|
30 | }
|
---|
31 |
|
---|
32 | int Input(BIGNUM *d, unsigned char *in, int n, int LE)
|
---|
33 | {
|
---|
34 | if (LE)
|
---|
35 | {
|
---|
36 | unsigned char tmp[n];
|
---|
37 | RotateBytes1(tmp,in,n);
|
---|
38 | return(BN_bin2bn(tmp,n,d)!=0);
|
---|
39 | }
|
---|
40 | else
|
---|
41 | return(BN_bin2bn(in,n,d)!=0);
|
---|
42 | }
|
---|
43 |
|
---|
44 | int Output(unsigned char *out, int n, BIGNUM *r, int LE)
|
---|
45 | {
|
---|
46 | int s=BN_num_bytes(r);
|
---|
47 | if (s>n)
|
---|
48 | {
|
---|
49 | unsigned char buff[s];
|
---|
50 | cs_debug("[cryptoworks-reader] rsa: RSA len %d > %d, truncating", s, n);
|
---|
51 | BN_bn2bin(r,buff);
|
---|
52 | memcpy(out,buff+s-n,n);
|
---|
53 | }
|
---|
54 | else if (s<n)
|
---|
55 | {
|
---|
56 | int l=n-s;
|
---|
57 | cs_debug("[cryptoworks-reader] rsa: RSA len %d < %d, padding", s, n);
|
---|
58 | memset(out,0,l);
|
---|
59 | BN_bn2bin(r,out+l);
|
---|
60 | }
|
---|
61 | else
|
---|
62 | BN_bn2bin(r,out);
|
---|
63 | if (LE)
|
---|
64 | RotateBytes2(out,n);
|
---|
65 | return(s);
|
---|
66 | }
|
---|
67 |
|
---|
68 | int RSA(unsigned char *out, unsigned char *in, int n, BIGNUM *exp, BIGNUM *mod, int LE)
|
---|
69 | {
|
---|
70 | int rc=0;
|
---|
71 | BN_CTX *ctx;
|
---|
72 | BIGNUM *r, *d;
|
---|
73 | ctx=BN_CTX_new();
|
---|
74 | r=BN_new();
|
---|
75 | d=BN_new();
|
---|
76 | if (Input(d,in,n,LE))
|
---|
77 | {
|
---|
78 | if(BN_mod_exp(r,d,exp,mod,ctx))
|
---|
79 | rc=Output(out,n,r,LE);
|
---|
80 | else
|
---|
81 | cs_log("[cryptoworks-reader] rsa: mod-exp failed");
|
---|
82 | }
|
---|
83 | BN_CTX_free(ctx);
|
---|
84 | BN_free(d);
|
---|
85 | BN_free(r);
|
---|
86 | return(rc);
|
---|
87 | }
|
---|
88 |
|
---|
89 | int CheckSctLen(const uchar *data, int off)
|
---|
90 | {
|
---|
91 | int l=SCT_LEN(data);
|
---|
92 | if (l+off > MAX_LEN)
|
---|
93 | {
|
---|
94 | cs_debug("[cryptoworks-reader] smartcard: section too long %d > %d", l, MAX_LEN-off);
|
---|
95 | l=-1;
|
---|
96 | }
|
---|
97 | return(l);
|
---|
98 | }
|
---|
99 |
|
---|
100 | #define write_cmd(cmd, data) \
|
---|
101 | { \
|
---|
102 | if (card_write(cmd, data)) return ERROR; \
|
---|
103 | }
|
---|
104 |
|
---|
105 | #define read_cmd(cmd, data) \
|
---|
106 | { \
|
---|
107 | if (card_write(cmd, NULL)) return ERROR; \
|
---|
108 | }
|
---|
109 |
|
---|
110 | static char *chid_date(uchar *ptr, char *buf, int l)
|
---|
111 | {
|
---|
112 | if (buf)
|
---|
113 | {
|
---|
114 | snprintf(buf, l, "%04d/%02d/%02d",
|
---|
115 | 1990+(ptr[0]>>1), ((ptr[0]&1)<<3)|(ptr[1]>>5), ptr[1]&0x1f);
|
---|
116 | }
|
---|
117 | return(buf);
|
---|
118 | }
|
---|
119 |
|
---|
120 | static int select_file(uchar f1, uchar f2)
|
---|
121 | {
|
---|
122 | uchar insA4[] = {0xA4, 0xA4, 0x00, 0x00, 0x02, 0x00, 0x00};
|
---|
123 | insA4[5]=f1;
|
---|
124 | insA4[6]=f2;
|
---|
125 | write_cmd(insA4, insA4+5); // select file
|
---|
126 | return((cta_res[0]==0x9f)&&(cta_res[1]==0x11));
|
---|
127 | }
|
---|
128 |
|
---|
129 | static int read_record(uchar rec)
|
---|
130 | {
|
---|
131 | uchar insA2[] = {0xA4, 0xA2, 0x00, 0x00, 0x01, 0x00};
|
---|
132 | uchar insB2[] = {0xA4, 0xB2, 0x00, 0x00, 0x00};
|
---|
133 |
|
---|
134 | insA2[5]=rec;
|
---|
135 | write_cmd(insA2, insA2+5); // select record
|
---|
136 | if (cta_res[0]!=0x9f)
|
---|
137 | return(-1);
|
---|
138 | insB2[4]=cta_res[1]; // get len
|
---|
139 | read_cmd(insB2, NULL); // read record
|
---|
140 | if ((cta_res[cta_lr-2]!=0x90) || (cta_res[cta_lr-1]))
|
---|
141 | return(-1);
|
---|
142 | return(cta_lr-2);
|
---|
143 | }
|
---|
144 |
|
---|
145 | int cryptoworks_send_pin(void)
|
---|
146 | {
|
---|
147 | unsigned char insPIN[] = { 0xA4, 0x20, 0x00, 0x00, 0x04, 0x00,0x00,0x00,0x00 }; //Verify PIN
|
---|
148 |
|
---|
149 | if(reader[ridx].pincode[0] && (reader[ridx].pincode[0]&0xF0)==0x30)
|
---|
150 | {
|
---|
151 | memcpy(insPIN+5,reader[ridx].pincode,4);
|
---|
152 |
|
---|
153 | write_cmd(insPIN, insPIN+5);
|
---|
154 | cs_ri_log("sending pincode to card");
|
---|
155 | if((cta_res[0]==0x98)&&(cta_res[1]==0x04)) cs_ri_log("bad pincode");
|
---|
156 |
|
---|
157 | return OK;
|
---|
158 | }
|
---|
159 |
|
---|
160 | return(0);
|
---|
161 | }
|
---|
162 |
|
---|
163 | static int cryptoworks_disable_pin(void)
|
---|
164 | {
|
---|
165 | unsigned char insPIN[] = { 0xA4, 0x26, 0x00, 0x00, 0x04, 0x00,0x00,0x00,0x00 }; //disable PIN
|
---|
166 |
|
---|
167 | if(reader[ridx].pincode[0] && (reader[ridx].pincode[0]&0xF0)==0x30)
|
---|
168 | {
|
---|
169 | memcpy(insPIN+5,reader[ridx].pincode,4);
|
---|
170 |
|
---|
171 | write_cmd(insPIN, insPIN+5);
|
---|
172 | cs_ri_log("disable pincode to card");
|
---|
173 | if((cta_res[0]==0x98)&&(cta_res[1]==0x04)) cs_ri_log("bad pincode");
|
---|
174 | return ERROR;
|
---|
175 | }
|
---|
176 | return OK;
|
---|
177 | }
|
---|
178 |
|
---|
179 | int cryptoworks_card_init(ATR newatr)
|
---|
180 | {
|
---|
181 | get_atr;
|
---|
182 | int i;
|
---|
183 | unsigned int mfid=0x3F20;
|
---|
184 | static uchar cwexp[] = { 1, 0 , 1};
|
---|
185 | uchar insA4C[]= {0xA4, 0xC0, 0x00, 0x00, 0x11};
|
---|
186 | uchar insB8[] = {0xA4, 0xB8, 0x00, 0x00, 0x0c};
|
---|
187 | uchar issuerid=0;
|
---|
188 | char issuer[20]={0};
|
---|
189 | char *unknown="unknown", *pin=unknown, ptxt[CS_MAXPROV<<2]={0};
|
---|
190 |
|
---|
191 | if ((atr[6]!=0xC4) || (atr[9]!=0x8F) || (atr[10]!=0xF1)) return ERROR;
|
---|
192 |
|
---|
193 | reader[ridx].caid[0]=0xD00;
|
---|
194 | reader[ridx].nprov=0;
|
---|
195 | reader[ridx].ucpk_valid = 0;
|
---|
196 | memset(reader[ridx].prid, 0, sizeof(reader[ridx].prid));
|
---|
197 |
|
---|
198 | read_cmd(insA4C, NULL); // read masterfile-ID
|
---|
199 | if ((cta_res[0]==0xDF) && (cta_res[1]>=6))
|
---|
200 | mfid=(cta_res[6]<<8)|cta_res[7];
|
---|
201 |
|
---|
202 | select_file(0x3f, 0x20);
|
---|
203 | insB8[2]=insB8[3]=0; // first
|
---|
204 | for(cta_res[0]=0xdf; cta_res[0]==0xdf;)
|
---|
205 | {
|
---|
206 | read_cmd(insB8, NULL); // read provider id's
|
---|
207 | if (cta_res[0]!=0xdf) break;
|
---|
208 | if (((cta_res[4]&0x1f)==0x1f) && (reader[ridx].nprov<CS_MAXPROV))
|
---|
209 | {
|
---|
210 | sprintf(ptxt+strlen(ptxt), ",%02X", cta_res[5]);
|
---|
211 | reader[ridx].prid[reader[ridx].nprov++][3]=cta_res[5];
|
---|
212 | }
|
---|
213 | insB8[2]=insB8[3]=0xff; // next
|
---|
214 | }
|
---|
215 | for (i=reader[ridx].nprov; i<CS_MAXPROV; i++)
|
---|
216 | memset(&reader[ridx].prid[i][0], 0xff, 4);
|
---|
217 |
|
---|
218 | select_file(0x2f, 0x01); // read caid
|
---|
219 | if (read_record(0xD1)>=4)
|
---|
220 | reader[ridx].caid[0]=(cta_res[2]<<8)|cta_res[3];
|
---|
221 |
|
---|
222 | if (read_record(0x80)>=7) // read serial
|
---|
223 | memcpy(reader[ridx].hexserial, cta_res+2, 5);
|
---|
224 | cs_ri_log("type: CryptoWorks, caid: %04X, ascii serial: %llu, hex serial: %s",
|
---|
225 | reader[ridx].caid[0], b2ll(5, reader[ridx].hexserial),cs_hexdump(0, reader[ridx].hexserial, 5));
|
---|
226 |
|
---|
227 | if (read_record(0x9E)>=66) // read ISK
|
---|
228 | {
|
---|
229 | uchar keybuf[256];
|
---|
230 | BIGNUM *ipk;
|
---|
231 | if (search_boxkey(reader[ridx].caid[0], (char *)keybuf))
|
---|
232 | {
|
---|
233 | ipk=BN_new();
|
---|
234 | BN_bin2bn(cwexp, sizeof(cwexp), &exp);
|
---|
235 | BN_bin2bn(keybuf, 64, ipk);
|
---|
236 | RSA(cta_res+2, cta_res+2, 0x40, &exp, ipk, 0);
|
---|
237 | BN_free(ipk);
|
---|
238 | reader[ridx].ucpk_valid =(cta_res[2]==((mfid & 0xFF)>>1));
|
---|
239 | if (reader[ridx].ucpk_valid)
|
---|
240 | {
|
---|
241 | cta_res[2]|=0x80;
|
---|
242 | BN_bin2bn(cta_res+2, 0x40, &ucpk);
|
---|
243 | cs_ddump(cta_res+2, 0x40, "IPK available -> session-key:");
|
---|
244 | }
|
---|
245 | else
|
---|
246 | {
|
---|
247 | reader[ridx].ucpk_valid =(keybuf[0]==(((mfid & 0xFF)>>1)|0x80));
|
---|
248 | if (reader[ridx].ucpk_valid)
|
---|
249 | {
|
---|
250 | BN_bin2bn(keybuf, 0x40, &ucpk);
|
---|
251 | cs_ddump(keybuf, 0x40, "session-key found:");
|
---|
252 | }
|
---|
253 | else
|
---|
254 | cs_log("[cryptoworks-reader] invalid IPK or session-key for CAID %04X !", reader[ridx].caid[0]);
|
---|
255 | }
|
---|
256 | }
|
---|
257 | }
|
---|
258 | if (read_record(0x9F)>=3)
|
---|
259 | issuerid=cta_res[2];
|
---|
260 | if (read_record(0xC0)>=16)
|
---|
261 | {
|
---|
262 | cs_strncpy(issuer, (const char *)cta_res+2, sizeof(issuer));
|
---|
263 | trim(issuer);
|
---|
264 | }
|
---|
265 | else
|
---|
266 | strcpy(issuer, unknown);
|
---|
267 |
|
---|
268 | select_file(0x3f, 0x20);
|
---|
269 | select_file(0x2f, 0x11); // read pin
|
---|
270 | if (read_record(atr[8])>=7)
|
---|
271 | {
|
---|
272 | cta_res[6]=0;
|
---|
273 | pin=(char *)cta_res+2;
|
---|
274 | }
|
---|
275 | cs_ri_log("issuer: %s, id: %02X, bios: v%d, pin: %s, mfid: %04X", issuer, issuerid, atr[7], pin, mfid);
|
---|
276 | cs_ri_log("providers: %d (%s)", reader[ridx].nprov, ptxt+1);
|
---|
277 | cs_log("[cryptoworks-reader] ready for requests");
|
---|
278 |
|
---|
279 | cryptoworks_disable_pin(); //by KrazyIvan
|
---|
280 |
|
---|
281 | return OK;
|
---|
282 | }
|
---|
283 |
|
---|
284 | #ifdef LALL
|
---|
285 | bool cSmartCardCryptoworks::Decode(const cEcmInfo *ecm, const unsigned char *data, unsigned char *cw)
|
---|
286 | {
|
---|
287 | static unsigned char ins4c[] = { 0xA4,0x4C,0x00,0x00,0x00 };
|
---|
288 |
|
---|
289 | unsigned char nanoD4[10];
|
---|
290 | int l=CheckSctLen(data,-5+(ucpkValid ? sizeof(nanoD4):0));
|
---|
291 | if(l>5) {
|
---|
292 | unsigned char buff[MAX_LEN];
|
---|
293 | if(ucpkValid) {
|
---|
294 | memcpy(buff,data,l);
|
---|
295 | nanoD4[0]=0xD4;
|
---|
296 | nanoD4[1]=0x08;
|
---|
297 | for(unsigned int i=2; i<sizeof(nanoD4); i++) nanoD4[i]=rand();
|
---|
298 | memcpy(&buff[l],nanoD4,sizeof(nanoD4));
|
---|
299 | data=buff; l+=sizeof(nanoD4);
|
---|
300 | }
|
---|
301 | ins4c[3]=ucpkValid ? 2 : 0;
|
---|
302 | ins4c[4]=l-5;
|
---|
303 | if(IsoWrite(ins4c,&data[5]) && Status() &&
|
---|
304 | (l=GetLen())>0 && ReadData(buff,l)==l) {
|
---|
305 | int r=0;
|
---|
306 | for(int i=0; i<l && r<2; ) {
|
---|
307 | int n=buff[i+1];
|
---|
308 | switch(buff[i]) {
|
---|
309 | case 0x80:
|
---|
310 | de(printf("smartcardcryptoworks: nano 80 (serial)\n"))
|
---|
311 | break;
|
---|
312 | case 0xD4:
|
---|
313 | de(printf("smartcardcryptoworks: nano D4 (rand)\n"))
|
---|
314 | if(n<8 || memcmp(&buff[i],nanoD4,sizeof(nanoD4)))
|
---|
315 | di(printf("smartcardcryptoworks: random data check failed after decrypt\n"))
|
---|
316 | break;
|
---|
317 | case 0xDB: // CW
|
---|
318 | de(printf("smartcardcryptoworks: nano DB (cw)\n"))
|
---|
319 | if(n==0x10) {
|
---|
320 | memcpy(cw,&buff[i+2],16);
|
---|
321 | r|=1;
|
---|
322 | }
|
---|
323 | break;
|
---|
324 | case 0xDF: // signature
|
---|
325 | de(printf("smartcardcryptoworks: nano DF %02x (sig)\n",n))
|
---|
326 | if(n==0x08) {
|
---|
327 | if((buff[i+2]&0x50)==0x50 && !(buff[i+3]&0x01) && (buff[i+5]&0x80))
|
---|
328 | r|=2;
|
---|
329 | }
|
---|
330 | else if(n==0x40) { // camcrypt
|
---|
331 | if(ucpkValid) {
|
---|
332 | RSA(&buff[i+2],&buff[i+2],n,exp,ucpk,false);
|
---|
333 | de(printf("smartcardcryptoworks: after camcrypt "))
|
---|
334 | de(HexDump(&buff[i+2],n))
|
---|
335 | r=0; l=n-4; n=4;
|
---|
336 | }
|
---|
337 | else {
|
---|
338 | di(printf("smartcardcryptoworks: valid UCPK needed for camcrypt!\n"))
|
---|
339 | return false;
|
---|
340 | }
|
---|
341 | }
|
---|
342 | break;
|
---|
343 | default:
|
---|
344 | de(printf("smartcardcryptoworks: nano %02x (unhandled)\n",buff[i]))
|
---|
345 | break;
|
---|
346 | }
|
---|
347 | i+=n+2;
|
---|
348 | }
|
---|
349 | return r==3;
|
---|
350 | }
|
---|
351 | }
|
---|
352 | return false;
|
---|
353 | }
|
---|
354 | #endif
|
---|
355 |
|
---|
356 | int cryptoworks_do_ecm(ECM_REQUEST *er)
|
---|
357 | {
|
---|
358 | int r=0;
|
---|
359 | static unsigned char ins4C[] = { 0xA4,0x4C,0x00,0x00,0x00 };
|
---|
360 | static unsigned char insC0[] = { 0xA4,0xC0,0x00,0x00,0x1C };
|
---|
361 | unsigned char nanoD4[10];
|
---|
362 | int secLen=CheckSctLen(er->ecm,-5+(reader[ridx].ucpk_valid ? sizeof(nanoD4):0));
|
---|
363 |
|
---|
364 | if(secLen>5)
|
---|
365 | {
|
---|
366 | int i;
|
---|
367 | uchar *ecm=er->ecm;
|
---|
368 | uchar buff[MAX_LEN];
|
---|
369 |
|
---|
370 | if(reader[ridx].ucpk_valid)
|
---|
371 | {
|
---|
372 | memcpy(buff,er->ecm,secLen);
|
---|
373 | nanoD4[0]=0xD4;
|
---|
374 | nanoD4[1]=0x08;
|
---|
375 | for (i=2; i<(int)sizeof(nanoD4); i++)
|
---|
376 | nanoD4[i]=rand();
|
---|
377 | memcpy(&buff[secLen], nanoD4, sizeof(nanoD4));
|
---|
378 | ecm=buff;
|
---|
379 | secLen+=sizeof(nanoD4);
|
---|
380 | }
|
---|
381 |
|
---|
382 | ins4C[3]=reader[ridx].ucpk_valid ? 2 : 0;
|
---|
383 | ins4C[4]=secLen-5;
|
---|
384 | write_cmd(ins4C, ecm+5);
|
---|
385 | if (cta_res[cta_lr-2]==0x9f)
|
---|
386 | {
|
---|
387 | insC0[4]=cta_res[cta_lr-1];
|
---|
388 | read_cmd(insC0, NULL);
|
---|
389 | for(i=0; i<secLen && r<2; )
|
---|
390 | {
|
---|
391 | int n=cta_res[i+1];
|
---|
392 | switch(cta_res[i])
|
---|
393 | {
|
---|
394 | case 0x80:
|
---|
395 | cs_debug("[cryptoworks-reader] nano 80 (serial)");
|
---|
396 | break;
|
---|
397 | case 0xD4:
|
---|
398 | cs_debug("[cryptoworks-reader] nano D4 (rand)");
|
---|
399 | if(n<8 || memcmp(&cta_res[i],nanoD4,sizeof(nanoD4)))
|
---|
400 | cs_debug("[cryptoworks-reader] random data check failed after decrypt");
|
---|
401 | break;
|
---|
402 | case 0xDB: // CW
|
---|
403 | cs_debug("[cryptoworks-reader] nano DB (cw)");
|
---|
404 | if(n==0x10)
|
---|
405 | {
|
---|
406 | memcpy(er->cw, &cta_res[i+2], 16);
|
---|
407 | r|=1;
|
---|
408 | }
|
---|
409 | break;
|
---|
410 | case 0xDF: // signature
|
---|
411 | cs_debug("[cryptoworks-reader] nano DF %02x (sig)", n);
|
---|
412 | if (n==0x08)
|
---|
413 | {
|
---|
414 | if((cta_res[i+2]&0x50)==0x50 && !(cta_res[i+3]&0x01) && (cta_res[i+5]&0x80))
|
---|
415 | r|=2;
|
---|
416 | }
|
---|
417 | else if (n==0x40) // camcrypt
|
---|
418 | {
|
---|
419 | if(reader[ridx].ucpk_valid)
|
---|
420 | {
|
---|
421 | RSA(&cta_res[i+2],&cta_res[i+2], n, &exp, &ucpk, 0);
|
---|
422 | cs_debug("[cryptoworks-reader] after camcrypt ");
|
---|
423 | r=0; secLen=n-4; n=4;
|
---|
424 | }
|
---|
425 | else
|
---|
426 | {
|
---|
427 | cs_log("[cryptoworks-reader] valid UCPK needed for camcrypt!");
|
---|
428 | return ERROR;
|
---|
429 | }
|
---|
430 | }
|
---|
431 | break;
|
---|
432 | default:
|
---|
433 | cs_debug("[cryptoworks-reader] nano %02x (unhandled)",cta_res[i]);
|
---|
434 | break;
|
---|
435 | }
|
---|
436 | i+=n+2;
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | #ifdef LALL
|
---|
441 | ########################################################################
|
---|
442 | if ((cta_res[cta_lr-2]==0x9f)&&(cta_res[cta_lr-1]==0x1c))
|
---|
443 | {
|
---|
444 | read_cmd(insC0, NULL);
|
---|
445 | if ((cta_lr>26)&&(cta_res[cta_lr-2]==0x90)&&(cta_res[cta_lr-1]==0))
|
---|
446 | {
|
---|
447 | if (rc=(((cta_res[20]&0x50)==0x50) &&
|
---|
448 | (!(cta_res[21]&0x01)) &&
|
---|
449 | (cta_res[23]&0x80)))
|
---|
450 | memcpy(er->cw, cta_res+2, 16);
|
---|
451 | }
|
---|
452 | }
|
---|
453 | #endif
|
---|
454 | }
|
---|
455 | // return(rc ? 1 : 0);
|
---|
456 | return((r==3) ? 1 : 0);
|
---|
457 | }
|
---|
458 |
|
---|
459 | int cryptoworks_get_emm_type(EMM_PACKET *ep) //returns TRUE if shared emm matches SA, unique emm matches serial, or global or unknown
|
---|
460 | {
|
---|
461 | switch (ep->emm[0]) {
|
---|
462 | case 0x82:
|
---|
463 | if(ep->emm[3]==0xA9 && ep->emm[4]==0xFF && ep->emm[13]==0x80 && ep->emm[14]==0x05)
|
---|
464 | ep->type = UNIQUE; //FIXME no ep->hexserial set
|
---|
465 | else
|
---|
466 | ep->type = UNKNOWN;
|
---|
467 | break;
|
---|
468 | case 0x84:
|
---|
469 | if(ep->emm[3]==0xA9 && ep->emm[4]==0xFF && ep->emm[12]==0x80 && ep->emm[13]==0x04)
|
---|
470 | ep->type = SHARED;
|
---|
471 | else
|
---|
472 | ep->type = UNKNOWN;
|
---|
473 | break;
|
---|
474 | case 0x89:
|
---|
475 | if(ep->emm[3]==0xA9 && ep->emm[4]==0xFF && ep->emm[8]==0x83 && ep->emm[9]==0x01)
|
---|
476 | ep->type = GLOBAL;
|
---|
477 | else
|
---|
478 | ep->type = UNKNOWN;
|
---|
479 | break;
|
---|
480 | case 0x8F://incoming via camd3.5x
|
---|
481 | //ep->type=emm[4];
|
---|
482 | default:
|
---|
483 | ep->type = UNKNOWN;
|
---|
484 | }
|
---|
485 | return TRUE; //no check on serial or SA
|
---|
486 | }
|
---|
487 |
|
---|
488 |
|
---|
489 | int cryptoworks_do_emm(EMM_PACKET *ep)
|
---|
490 | {
|
---|
491 | uchar insEMM_GA[] = {0xA4, 0x44, 0x00, 0x00, 0x00};
|
---|
492 | uchar insEMM_SA[] = {0xA4, 0x48, 0x00, 0x00, 0x00};
|
---|
493 | uchar insEMM_UA[] = {0xA4, 0x42, 0x00, 0x00, 0x00};
|
---|
494 | int rc=0;
|
---|
495 | uchar *emm=ep->emm;
|
---|
496 |
|
---|
497 | /* this original
|
---|
498 | if ((emm[0]==0x8f) && (emm[3]==0xa4)) // emm via camd3.5x
|
---|
499 | {
|
---|
500 | ep->type=emm[4];
|
---|
501 | write_cmd(emm+3, emm+3+CMD_LEN);
|
---|
502 | if ((cta_lr==2) && (cta_res[0]==0x90) && (cta_res[1]==0))
|
---|
503 | rc=1;
|
---|
504 | }
|
---|
505 | */
|
---|
506 |
|
---|
507 | //cs_log("[cryptoworks-reader] EMM Dump:..: %s",cs_hexdump(1, emm, emm[2]));
|
---|
508 | switch(ep->type)
|
---|
509 | {
|
---|
510 | // emm via camd3.5x
|
---|
511 | case 0x8F:
|
---|
512 | if(emm[3]==0xA4)
|
---|
513 | {
|
---|
514 | //cs_log("[cryptoworks-reader] EMM Dump: CMD: %s", cs_hexdump(1, emm+3, 5));
|
---|
515 | //cs_log("[cryptoworks-reader] EMM Dump: DATA: %s",cs_hexdump(1, emm+8, emm[7]));
|
---|
516 | write_cmd(emm+3, emm+3+CMD_LEN);
|
---|
517 | rc=((cta_res[0]==0x90)&&(cta_res[1]==0x00));
|
---|
518 | }
|
---|
519 | break;
|
---|
520 |
|
---|
521 | //GA
|
---|
522 | case 0x88:
|
---|
523 | case 0x89:
|
---|
524 | if(emm[3]==0xA9 && emm[4]==0xFF && emm[8]==0x83 && emm[9]==0x01)
|
---|
525 | {
|
---|
526 | insEMM_GA[4]=ep->emm[2]-2;
|
---|
527 | //cs_log("[cryptoworks-reader] EMM Dump: CMD: %s", cs_hexdump(1, insEMM_GA, 5));
|
---|
528 | //cs_log("[cryptoworks-reader] EMM Dump: DATA: %s",cs_hexdump(1, emm+5, insEMM_GA[4]));
|
---|
529 | //cs_log("[cryptoworks-reader] EMM Dump: IF: %02X == %02X",emm[7],(insEMM_GA[4]-3));
|
---|
530 |
|
---|
531 | if(emm[7]==insEMM_GA[4]-3)
|
---|
532 | {
|
---|
533 | write_cmd(insEMM_GA, emm+5);
|
---|
534 | rc=((cta_res[0]==0x90)&&(cta_res[1]==0x00));
|
---|
535 | }
|
---|
536 | }
|
---|
537 | break;
|
---|
538 |
|
---|
539 | //SA
|
---|
540 | case 0x84:
|
---|
541 | if(emm[3]==0xA9 && emm[4]==0xFF && emm[12]==0x80 && emm[13]==0x04)
|
---|
542 | {
|
---|
543 | insEMM_SA[4]=ep->emm[2]-6;
|
---|
544 | //cs_log("[cryptoworks-reader] EMM Dump: CMD: %s", cs_hexdump(1, insEMM_SA, 5));
|
---|
545 | //cs_log("[cryptoworks-reader] EMM Dump: DATA: %s",cs_hexdump(1, emm+9, insEMM_SA[4]));
|
---|
546 | //cs_log("[cryptoworks-reader] EMM Dump: IF: %02X == %02X",emm[11],(insEMM_SA[4]-3));
|
---|
547 |
|
---|
548 | if(emm[11]==insEMM_SA[4]-3)
|
---|
549 | {
|
---|
550 | write_cmd(insEMM_SA, emm+9);
|
---|
551 | rc=((cta_res[0]==0x90)&&(cta_res[1]==0x00));
|
---|
552 | }
|
---|
553 | }
|
---|
554 | break;
|
---|
555 |
|
---|
556 | //UA
|
---|
557 | case 0x82:
|
---|
558 | if(emm[3]==0xA9 && emm[4]==0xFF && emm[13]==0x80 && emm[14]==0x05)
|
---|
559 | {
|
---|
560 | insEMM_UA[4]=ep->emm[2]-7;
|
---|
561 | //cs_log("[cryptoworks-reader] EMM Dump: CMD: %s", cs_hexdump(1, insEMM_UA, 5));
|
---|
562 | //cs_log("[cryptoworks-reader] EMM Dump: DATA: %s",cs_hexdump(1, emm+10, insEMM_UA[4]));
|
---|
563 | //cs_log("[cryptoworks-reader] EMM Dump: IF: %02X == %02X",emm[12],(insEMM_UA[4]-3));
|
---|
564 |
|
---|
565 | if(emm[12]==insEMM_UA[4]-3)
|
---|
566 | {
|
---|
567 | //cryptoworks_send_pin(); //?? may be
|
---|
568 | write_cmd(insEMM_UA, emm+10);
|
---|
569 | rc=((cta_res[0]==0x90)&&(cta_res[1]==0x00));
|
---|
570 | }
|
---|
571 | }
|
---|
572 | break;
|
---|
573 | }
|
---|
574 |
|
---|
575 | return(rc);
|
---|
576 | }
|
---|
577 |
|
---|
578 | int cryptoworks_card_info(void)
|
---|
579 | {
|
---|
580 | int i;
|
---|
581 | uchar insA21[]= {0xA4, 0xA2, 0x01, 0x00, 0x05, 0x8C, 0x00, 0x00, 0x00, 0x00};
|
---|
582 | uchar insB2[] = {0xA4, 0xB2, 0x00, 0x00, 0x00};
|
---|
583 | char l_name[20+8]=", name: ";
|
---|
584 | cs_log("[cryptoworks-reader] card detected");
|
---|
585 | cs_log("[cryptoworks-reader] type: CryptoWorks");
|
---|
586 |
|
---|
587 | for (i=0; i<reader[ridx].nprov; i++)
|
---|
588 | {
|
---|
589 | l_name[8]=0;
|
---|
590 | select_file(0x1f, reader[ridx].prid[i][3]); // select provider
|
---|
591 | select_file(0x0e, 0x11); // read provider name
|
---|
592 | if (read_record(0xD6)>=16)
|
---|
593 | {
|
---|
594 | cs_strncpy(l_name+8, (const char *)cta_res+2, sizeof(l_name)-9);
|
---|
595 | l_name[sizeof(l_name)-1]=0;
|
---|
596 | trim(l_name+8);
|
---|
597 | }
|
---|
598 | l_name[0]=(l_name[8]) ? ',' : 0;
|
---|
599 | cs_ri_log("provider: %d, id: %02X%s", i+1, reader[ridx].prid[i][3], l_name);
|
---|
600 | select_file(0x0f, 0x20); // select provider class
|
---|
601 | write_cmd(insA21, insA21+5);
|
---|
602 | if (cta_res[0]==0x9f)
|
---|
603 | {
|
---|
604 | insB2[4]=cta_res[1];
|
---|
605 | for(insB2[3]=0; (cta_res[0]!=0x94)||(cta_res[1]!=0x2); insB2[3]=1)
|
---|
606 | {
|
---|
607 | read_cmd(insB2, NULL); // read chid
|
---|
608 | if (cta_res[0]!=0x94)
|
---|
609 | {
|
---|
610 | char ds[16], de[16];
|
---|
611 | chid_date(cta_res+28, ds, sizeof(ds)-1);
|
---|
612 | chid_date(cta_res+30, de, sizeof(de)-1);
|
---|
613 | cs_ri_log("chid: %02X%02X, date: %s - %s, name: %s",
|
---|
614 | cta_res[6], cta_res[7], ds, de, trim((char *) cta_res+10));
|
---|
615 | }
|
---|
616 | }
|
---|
617 | }
|
---|
618 | //================================================================================
|
---|
619 | //by KrazyIvan
|
---|
620 | select_file(0x0f, 0x00); // select provider channel
|
---|
621 | write_cmd(insA21, insA21+5);
|
---|
622 | if (cta_res[0]==0x9f)
|
---|
623 | {
|
---|
624 | insB2[4]=cta_res[1];
|
---|
625 | for(insB2[3]=0; (cta_res[0]!=0x94)||(cta_res[1]!=0x2); insB2[3]=1)
|
---|
626 | {
|
---|
627 | read_cmd(insB2, NULL); // read chid
|
---|
628 | if (cta_res[0]!=0x94)
|
---|
629 | {
|
---|
630 | char ds[16], de[16];
|
---|
631 | chid_date(cta_res+28, ds, sizeof(ds)-1);
|
---|
632 | chid_date(cta_res+30, de, sizeof(de)-1);
|
---|
633 | cta_res[27]=0;
|
---|
634 | cs_ri_log("chid: %02X%02X, date: %s - %s, name: %s",
|
---|
635 | cta_res[6], cta_res[7], ds, de, trim((char *)cta_res+10));
|
---|
636 | }
|
---|
637 | }
|
---|
638 | }
|
---|
639 | //================================================================================
|
---|
640 |
|
---|
641 | }
|
---|
642 | return OK;
|
---|
643 | }
|
---|