]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhf14a.c
c2d12fd9d20fa56ef15a7ff85cce7ab1490a81f9
[proxmark3-svn] / client / cmdhf14a.c
1 //-----------------------------------------------------------------------------
2 // 2011, 2017 Merlok
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
4 //
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
7 // the license.
8 //-----------------------------------------------------------------------------
9 // High frequency ISO14443A commands
10 //-----------------------------------------------------------------------------
11
12 #include "cmdhf14a.h"
13
14 #include "util.h"
15 #include "util_posix.h"
16 #include "iso14443crc.h"
17 #include "data.h"
18 #include "proxmark3.h"
19 #include "ui.h"
20 #include "cmdparser.h"
21 #include "common.h"
22 #include "cmdmain.h"
23 #include "mifare.h"
24 #include "cmdhfmfu.h"
25 #include "mifarehost.h"
26 #include "emv/apduinfo.h"
27 #include "emv/emvcore.h"
28
29 static int CmdHelp(const char *Cmd);
30 static int waitCmd(uint8_t iLen);
31
32
33 const manufactureName manufactureMapping[] = {
34 // ID, "Vendor Country"
35 { 0x01, "Motorola UK" },
36 { 0x02, "ST Microelectronics SA France" },
37 { 0x03, "Hitachi, Ltd Japan" },
38 { 0x04, "NXP Semiconductors Germany" },
39 { 0x05, "Infineon Technologies AG Germany" },
40 { 0x06, "Cylink USA" },
41 { 0x07, "Texas Instrument France" },
42 { 0x08, "Fujitsu Limited Japan" },
43 { 0x09, "Matsushita Electronics Corporation, Semiconductor Company Japan" },
44 { 0x0A, "NEC Japan" },
45 { 0x0B, "Oki Electric Industry Co. Ltd Japan" },
46 { 0x0C, "Toshiba Corp. Japan" },
47 { 0x0D, "Mitsubishi Electric Corp. Japan" },
48 { 0x0E, "Samsung Electronics Co. Ltd Korea" },
49 { 0x0F, "Hynix / Hyundai, Korea" },
50 { 0x10, "LG-Semiconductors Co. Ltd Korea" },
51 { 0x11, "Emosyn-EM Microelectronics USA" },
52 { 0x12, "INSIDE Technology France" },
53 { 0x13, "ORGA Kartensysteme GmbH Germany" },
54 { 0x14, "SHARP Corporation Japan" },
55 { 0x15, "ATMEL France" },
56 { 0x16, "EM Microelectronic-Marin SA Switzerland" },
57 { 0x17, "KSW Microtec GmbH Germany" },
58 { 0x18, "ZMD AG Germany" },
59 { 0x19, "XICOR, Inc. USA" },
60 { 0x1A, "Sony Corporation Japan Identifier Company Country" },
61 { 0x1B, "Malaysia Microelectronic Solutions Sdn. Bhd Malaysia" },
62 { 0x1C, "Emosyn USA" },
63 { 0x1D, "Shanghai Fudan Microelectronics Co. Ltd. P.R. China" },
64 { 0x1E, "Magellan Technology Pty Limited Australia" },
65 { 0x1F, "Melexis NV BO Switzerland" },
66 { 0x20, "Renesas Technology Corp. Japan" },
67 { 0x21, "TAGSYS France" },
68 { 0x22, "Transcore USA" },
69 { 0x23, "Shanghai belling corp., ltd. China" },
70 { 0x24, "Masktech Germany Gmbh Germany" },
71 { 0x25, "Innovision Research and Technology Plc UK" },
72 { 0x26, "Hitachi ULSI Systems Co., Ltd. Japan" },
73 { 0x27, "Cypak AB Sweden" },
74 { 0x28, "Ricoh Japan" },
75 { 0x29, "ASK France" },
76 { 0x2A, "Unicore Microsystems, LLC Russian Federation" },
77 { 0x2B, "Dallas Semiconductor/Maxim USA" },
78 { 0x2C, "Impinj, Inc. USA" },
79 { 0x2D, "RightPlug Alliance USA" },
80 { 0x2E, "Broadcom Corporation USA" },
81 { 0x2F, "MStar Semiconductor, Inc Taiwan, ROC" },
82 { 0x30, "BeeDar Technology Inc. USA" },
83 { 0x31, "RFIDsec Denmark" },
84 { 0x32, "Schweizer Electronic AG Germany" },
85 { 0x33, "AMIC Technology Corp Taiwan" },
86 { 0x34, "Mikron JSC Russia" },
87 { 0x35, "Fraunhofer Institute for Photonic Microsystems Germany" },
88 { 0x36, "IDS Microchip AG Switzerland" },
89 { 0x37, "Kovio USA" },
90 { 0x38, "HMT Microelectronic Ltd Switzerland Identifier Company Country" },
91 { 0x39, "Silicon Craft Technology Thailand" },
92 { 0x3A, "Advanced Film Device Inc. Japan" },
93 { 0x3B, "Nitecrest Ltd UK" },
94 { 0x3C, "Verayo Inc. USA" },
95 { 0x3D, "HID Global USA" },
96 { 0x3E, "Productivity Engineering Gmbh Germany" },
97 { 0x3F, "Austriamicrosystems AG (reserved) Austria" },
98 { 0x40, "Gemalto SA France" },
99 { 0x41, "Renesas Electronics Corporation Japan" },
100 { 0x42, "3Alogics Inc Korea" },
101 { 0x43, "Top TroniQ Asia Limited Hong Kong" },
102 { 0x44, "Gentag Inc (USA) USA" },
103 { 0x00, "no tag-info available" } // must be the last entry
104 };
105
106 // get a product description based on the UID
107 // uid[8] tag uid
108 // returns description of the best match
109 char* getTagInfo(uint8_t uid) {
110
111 int i;
112 int len = sizeof(manufactureMapping) / sizeof(manufactureName);
113
114 for ( i = 0; i < len; ++i )
115 if ( uid == manufactureMapping[i].uid)
116 return manufactureMapping[i].desc;
117
118 //No match, return default
119 return manufactureMapping[len-1].desc;
120 }
121
122 int CmdHF14AList(const char *Cmd)
123 {
124 PrintAndLog("Deprecated command, use 'hf list 14a' instead");
125 return 0;
126 }
127
128 int CmdHF14AReader(const char *Cmd) {
129 uint32_t cm = ISO14A_CONNECT;
130 bool disconnectAfter = false;
131
132 int cmdp = 0;
133 while(param_getchar(Cmd, cmdp) != 0x00) {
134 switch(param_getchar(Cmd, cmdp)) {
135 case 'h':
136 case 'H':
137 PrintAndLog("Usage: hf 14a reader [d] [3]");
138 PrintAndLog(" d drop the signal field after command executed");
139 PrintAndLog(" x just drop the signal field");
140 PrintAndLog(" 3 ISO14443-3 select only (skip RATS)");
141 return 0;
142 case '3':
143 cm |= ISO14A_NO_RATS;
144 break;
145 case 'd':
146 case 'D':
147 disconnectAfter = true;
148 break;
149 case 'x':
150 case 'X':
151 disconnectAfter = true;
152 cm = cm - ISO14A_CONNECT;
153 break;
154 default:
155 PrintAndLog("Unknown command.");
156 return 1;
157 }
158
159 cmdp++;
160 }
161
162 if (!disconnectAfter)
163 cm |= ISO14A_NO_DISCONNECT;
164
165 UsbCommand c = {CMD_READER_ISO_14443a, {cm, 0, 0}};
166 SendCommand(&c);
167
168 if (ISO14A_CONNECT & cm) {
169 UsbCommand resp;
170 WaitForResponse(CMD_ACK,&resp);
171
172 iso14a_card_select_t card;
173 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
174
175 uint64_t select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
176
177 if(select_status == 0) {
178 PrintAndLog("iso14443a card select failed");
179 return 1;
180 }
181
182 if(select_status == 3) {
183 PrintAndLog("Card doesn't support standard iso14443-3 anticollision");
184 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
185 return 1;
186 }
187
188 PrintAndLog(" UID : %s", sprint_hex(card.uid, card.uidlen));
189 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
190 PrintAndLog(" SAK : %02x [%d]", card.sak, resp.arg[0]);
191 if(card.ats_len >= 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes
192 PrintAndLog(" ATS : %s", sprint_hex(card.ats, card.ats_len));
193 }
194 PrintAndLog("Card is selected. You can now start sending commands");
195 } else {
196 PrintAndLog("Field dropped.");
197 }
198 return 0;
199 }
200
201 int CmdHF14AInfo(const char *Cmd)
202 {
203 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};
204 SendCommand(&c);
205
206 UsbCommand resp;
207 WaitForResponse(CMD_ACK,&resp);
208
209 iso14a_card_select_t card;
210 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
211
212 uint64_t select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
213
214 if(select_status == 0) {
215 if (Cmd[0] != 's') PrintAndLog("iso14443a card select failed");
216 // disconnect
217 c.arg[0] = 0;
218 c.arg[1] = 0;
219 c.arg[2] = 0;
220 SendCommand(&c);
221 return 0;
222 }
223
224 if(select_status == 3) {
225 PrintAndLog("Card doesn't support standard iso14443-3 anticollision");
226 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
227 // disconnect
228 c.arg[0] = 0;
229 c.arg[1] = 0;
230 c.arg[2] = 0;
231 SendCommand(&c);
232 return 0;
233 }
234
235 PrintAndLog(" UID : %s", sprint_hex(card.uid, card.uidlen));
236 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
237 PrintAndLog(" SAK : %02x [%d]", card.sak, resp.arg[0]);
238
239 bool isMifareClassic = true;
240 switch (card.sak) {
241 case 0x00:
242 isMifareClassic = false;
243
244 //***************************************test****************
245 // disconnect
246 c.arg[0] = 0;
247 c.arg[1] = 0;
248 c.arg[2] = 0;
249 SendCommand(&c);
250
251 uint32_t tagT = GetHF14AMfU_Type();
252 ul_print_type(tagT, 0);
253
254 //reconnect for further tests
255 c.arg[0] = ISO14A_CONNECT | ISO14A_NO_DISCONNECT;
256 c.arg[1] = 0;
257 c.arg[2] = 0;
258
259 SendCommand(&c);
260
261 UsbCommand resp;
262 WaitForResponse(CMD_ACK,&resp);
263
264 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
265
266 select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS
267
268 if(select_status == 0) {
269 //PrintAndLog("iso14443a card select failed");
270 // disconnect
271 c.arg[0] = 0;
272 c.arg[1] = 0;
273 c.arg[2] = 0;
274 SendCommand(&c);
275 return 0;
276 }
277
278 /* orig
279 // check if the tag answers to GETVERSION (0x60)
280 c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;
281 c.arg[1] = 1;
282 c.arg[2] = 0;
283 c.d.asBytes[0] = 0x60;
284 SendCommand(&c);
285 WaitForResponse(CMD_ACK,&resp);
286
287 uint8_t version[10] = {0};
288 memcpy(version, resp.d.asBytes, resp.arg[0] < sizeof(version) ? resp.arg[0] : sizeof(version));
289 uint8_t len = resp.arg[0] & 0xff;
290 switch ( len ){
291 // todo, identify "Magic UL-C tags". // they usually have a static nonce response to 0x1A command.
292 // UL-EV1, size, check version[6] == 0x0b (smaller) 0x0b * 4 == 48
293 case 0x0A:PrintAndLog("TYPE : NXP MIFARE Ultralight EV1 %d bytes", (version[6] == 0xB) ? 48 : 128);break;
294 case 0x01:PrintAndLog("TYPE : NXP MIFARE Ultralight C");break;
295 case 0x00:PrintAndLog("TYPE : NXP MIFARE Ultralight");break;
296 }
297 */
298 break;
299 case 0x01: PrintAndLog("TYPE : NXP TNP3xxx Activision Game Appliance"); break;
300 case 0x04: PrintAndLog("TYPE : NXP MIFARE (various !DESFire !DESFire EV1)"); break;
301 case 0x08: PrintAndLog("TYPE : NXP MIFARE CLASSIC 1k | Plus 2k SL1"); break;
302 case 0x09: PrintAndLog("TYPE : NXP MIFARE Mini 0.3k"); break;
303 case 0x10: PrintAndLog("TYPE : NXP MIFARE Plus 2k SL2"); break;
304 case 0x11: PrintAndLog("TYPE : NXP MIFARE Plus 4k SL2"); break;
305 case 0x18: PrintAndLog("TYPE : NXP MIFARE Classic 4k | Plus 4k SL1"); break;
306 case 0x20: PrintAndLog("TYPE : NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k SL3 | JCOP 31/41"); break;
307 case 0x24: PrintAndLog("TYPE : NXP MIFARE DESFire | DESFire EV1"); break;
308 case 0x28: PrintAndLog("TYPE : JCOP31 or JCOP41 v2.3.1"); break;
309 case 0x38: PrintAndLog("TYPE : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break;
310 case 0x88: PrintAndLog("TYPE : Infineon MIFARE CLASSIC 1K"); break;
311 case 0x98: PrintAndLog("TYPE : Gemplus MPCOS"); break;
312 default: ;
313 }
314
315 // Double & triple sized UID, can be mapped to a manufacturer.
316 // HACK: does this apply for Ultralight cards?
317 if ( card.uidlen > 4 ) {
318 PrintAndLog("MANUFACTURER : %s", getTagInfo(card.uid[0]));
319 }
320
321 // try to request ATS even if tag claims not to support it
322 if (select_status == 2) {
323 uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
324 c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;
325 c.arg[1] = 2;
326 c.arg[2] = 0;
327 memcpy(c.d.asBytes, rats, 2);
328 SendCommand(&c);
329 WaitForResponse(CMD_ACK,&resp);
330
331 memcpy(card.ats, resp.d.asBytes, resp.arg[0]);
332 card.ats_len = resp.arg[0]; // note: ats_len includes CRC Bytes
333 }
334
335 if(card.ats_len >= 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes
336 bool ta1 = 0, tb1 = 0, tc1 = 0;
337 int pos;
338
339 if (select_status == 2) {
340 PrintAndLog("SAK incorrectly claims that card doesn't support RATS");
341 }
342 PrintAndLog(" ATS : %s", sprint_hex(card.ats, card.ats_len));
343 PrintAndLog(" - TL : length is %d bytes", card.ats[0]);
344 if (card.ats[0] != card.ats_len - 2) {
345 PrintAndLog("ATS may be corrupted. Length of ATS (%d bytes incl. 2 Bytes CRC) doesn't match TL", card.ats_len);
346 }
347
348 if (card.ats[0] > 1) { // there is a format byte (T0)
349 ta1 = (card.ats[1] & 0x10) == 0x10;
350 tb1 = (card.ats[1] & 0x20) == 0x20;
351 tc1 = (card.ats[1] & 0x40) == 0x40;
352 int16_t fsci = card.ats[1] & 0x0f;
353 PrintAndLog(" - T0 : TA1 is%s present, TB1 is%s present, "
354 "TC1 is%s present, FSCI is %d (FSC = %ld)",
355 (ta1 ? "" : " NOT"), (tb1 ? "" : " NOT"), (tc1 ? "" : " NOT"),
356 fsci,
357 fsci < 5 ? (fsci - 2) * 8 :
358 fsci < 8 ? (fsci - 3) * 32 :
359 fsci == 8 ? 256 :
360 -1
361 );
362 }
363 pos = 2;
364 if (ta1) {
365 char dr[16], ds[16];
366 dr[0] = ds[0] = '\0';
367 if (card.ats[pos] & 0x10) strcat(ds, "2, ");
368 if (card.ats[pos] & 0x20) strcat(ds, "4, ");
369 if (card.ats[pos] & 0x40) strcat(ds, "8, ");
370 if (card.ats[pos] & 0x01) strcat(dr, "2, ");
371 if (card.ats[pos] & 0x02) strcat(dr, "4, ");
372 if (card.ats[pos] & 0x04) strcat(dr, "8, ");
373 if (strlen(ds) != 0) ds[strlen(ds) - 2] = '\0';
374 if (strlen(dr) != 0) dr[strlen(dr) - 2] = '\0';
375 PrintAndLog(" - TA1 : different divisors are%s supported, "
376 "DR: [%s], DS: [%s]",
377 (card.ats[pos] & 0x80 ? " NOT" : ""), dr, ds);
378 pos++;
379 }
380 if (tb1) {
381 uint32_t sfgi = card.ats[pos] & 0x0F;
382 uint32_t fwi = card.ats[pos] >> 4;
383 PrintAndLog(" - TB1 : SFGI = %d (SFGT = %s%ld/fc), FWI = %d (FWT = %ld/fc)",
384 (sfgi),
385 sfgi ? "" : "(not needed) ",
386 sfgi ? (1 << 12) << sfgi : 0,
387 fwi,
388 (1 << 12) << fwi
389 );
390 pos++;
391 }
392 if (tc1) {
393 PrintAndLog(" - TC1 : NAD is%s supported, CID is%s supported",
394 (card.ats[pos] & 0x01) ? "" : " NOT",
395 (card.ats[pos] & 0x02) ? "" : " NOT");
396 pos++;
397 }
398 if (card.ats[0] > pos) {
399 char *tip = "";
400 if (card.ats[0] - pos >= 7) {
401 if (memcmp(card.ats + pos, "\xC1\x05\x2F\x2F\x01\xBC\xD6", 7) == 0) {
402 tip = "-> MIFARE Plus X 2K or 4K";
403 } else if (memcmp(card.ats + pos, "\xC1\x05\x2F\x2F\x00\x35\xC7", 7) == 0) {
404 tip = "-> MIFARE Plus S 2K or 4K";
405 }
406 }
407 PrintAndLog(" - HB : %s%s", sprint_hex(card.ats + pos, card.ats[0] - pos), tip);
408 if (card.ats[pos] == 0xC1) {
409 PrintAndLog(" c1 -> Mifare or (multiple) virtual cards of various type");
410 PrintAndLog(" %02x -> Length is %d bytes",
411 card.ats[pos + 1], card.ats[pos + 1]);
412 switch (card.ats[pos + 2] & 0xf0) {
413 case 0x10:
414 PrintAndLog(" 1x -> MIFARE DESFire");
415 break;
416 case 0x20:
417 PrintAndLog(" 2x -> MIFARE Plus");
418 break;
419 }
420 switch (card.ats[pos + 2] & 0x0f) {
421 case 0x00:
422 PrintAndLog(" x0 -> <1 kByte");
423 break;
424 case 0x01:
425 PrintAndLog(" x1 -> 1 kByte");
426 break;
427 case 0x02:
428 PrintAndLog(" x2 -> 2 kByte");
429 break;
430 case 0x03:
431 PrintAndLog(" x3 -> 4 kByte");
432 break;
433 case 0x04:
434 PrintAndLog(" x4 -> 8 kByte");
435 break;
436 }
437 switch (card.ats[pos + 3] & 0xf0) {
438 case 0x00:
439 PrintAndLog(" 0x -> Engineering sample");
440 break;
441 case 0x20:
442 PrintAndLog(" 2x -> Released");
443 break;
444 }
445 switch (card.ats[pos + 3] & 0x0f) {
446 case 0x00:
447 PrintAndLog(" x0 -> Generation 1");
448 break;
449 case 0x01:
450 PrintAndLog(" x1 -> Generation 2");
451 break;
452 case 0x02:
453 PrintAndLog(" x2 -> Generation 3");
454 break;
455 }
456 switch (card.ats[pos + 4] & 0x0f) {
457 case 0x00:
458 PrintAndLog(" x0 -> Only VCSL supported");
459 break;
460 case 0x01:
461 PrintAndLog(" x1 -> VCS, VCSL, and SVC supported");
462 break;
463 case 0x0E:
464 PrintAndLog(" xE -> no VCS command supported");
465 break;
466 }
467 }
468 }
469 } else {
470 PrintAndLog("proprietary non iso14443-4 card found, RATS not supported");
471 }
472
473
474 // try to see if card responses to "chinese magic backdoor" commands.
475 mfCIdentify();
476
477 if (isMifareClassic) {
478 switch(DetectClassicPrng()) {
479 case 0:
480 PrintAndLog("Prng detection: HARDEND (hardnested)");
481 break;
482 case 1:
483 PrintAndLog("Prng detection: WEAK");
484 break;
485 default:
486 PrintAndLog("Prng detection error.");
487 }
488 }
489
490 return select_status;
491 }
492
493 // Collect ISO14443 Type A UIDs
494 int CmdHF14ACUIDs(const char *Cmd)
495 {
496 // requested number of UIDs
497 int n = atoi(Cmd);
498 // collect at least 1 (e.g. if no parameter was given)
499 n = n > 0 ? n : 1;
500
501 PrintAndLog("Collecting %d UIDs", n);
502 PrintAndLog("Start: %" PRIu64, msclock()/1000);
503 // repeat n times
504 for (int i = 0; i < n; i++) {
505 // execute anticollision procedure
506 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_RATS, 0, 0}};
507 SendCommand(&c);
508
509 UsbCommand resp;
510 WaitForResponse(CMD_ACK,&resp);
511
512 iso14a_card_select_t *card = (iso14a_card_select_t *) resp.d.asBytes;
513
514 // check if command failed
515 if (resp.arg[0] == 0) {
516 PrintAndLog("Card select failed.");
517 } else {
518 char uid_string[20];
519 for (uint16_t i = 0; i < card->uidlen; i++) {
520 sprintf(&uid_string[2*i], "%02X", card->uid[i]);
521 }
522 PrintAndLog("%s", uid_string);
523 }
524 }
525 PrintAndLog("End: %" PRIu64, msclock()/1000);
526
527 return 1;
528 }
529
530 // ## simulate iso14443a tag
531 // ## greg - added ability to specify tag UID
532 int CmdHF14ASim(const char *Cmd)
533 {
534 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,{0,0,0}};
535
536 // Retrieve the tag type
537 uint8_t tagtype = param_get8ex(Cmd,0,0,10);
538
539 // When no argument was given, just print help message
540 if (tagtype == 0) {
541 PrintAndLog("");
542 PrintAndLog(" Emulating ISO/IEC 14443 type A tag with 4 or 7 byte UID");
543 PrintAndLog("");
544 PrintAndLog(" syntax: hf 14a sim <type> <uid>");
545 PrintAndLog(" types: 1 = MIFARE Classic");
546 PrintAndLog(" 2 = MIFARE Ultralight");
547 PrintAndLog(" 3 = MIFARE Desfire");
548 PrintAndLog(" 4 = ISO/IEC 14443-4");
549 PrintAndLog(" 5 = MIFARE Tnp3xxx");
550 PrintAndLog("");
551 return 1;
552 }
553
554 // Store the tag type
555 c.arg[0] = tagtype;
556
557 // Retrieve the full 4 or 7 byte long uid
558 uint64_t long_uid = param_get64ex(Cmd,1,0,16);
559
560 // Are we handling the (optional) second part uid?
561 if (long_uid > 0xffffffff) {
562 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014" PRIx64 ")",long_uid);
563 // Store the second part
564 c.arg[2] = (long_uid & 0xffffffff);
565 long_uid >>= 32;
566 // Store the first part, ignore the first byte, it is replaced by cascade byte (0x88)
567 c.arg[1] = (long_uid & 0xffffff);
568 } else {
569 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 4 byte UID (%08x)",long_uid);
570 // Only store the first part
571 c.arg[1] = long_uid & 0xffffffff;
572 }
573 /*
574 // At lease save the mandatory first part of the UID
575 c.arg[0] = long_uid & 0xffffffff;
576
577 if (c.arg[1] == 0) {
578 PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
579 }
580
581 switch (c.arg[0]) {
582 case 1: {
583 PrintAndLog("Emulating ISO/IEC 14443-3 type A tag with 4 byte UID");
584 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,param_get32ex(Cmd,0,0,10),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16)};
585 } break;
586 case 2: {
587 PrintAndLog("Emulating ISO/IEC 14443-4 type A tag with 7 byte UID");
588 } break;
589 default: {
590 PrintAndLog("Error: unkown tag type (%d)",c.arg[0]);
591 PrintAndLog("syntax: hf 14a sim <uid>",c.arg[0]);
592 PrintAndLog(" type1: 4 ",c.arg[0]);
593
594 return 1;
595 } break;
596 }
597 */
598 /*
599 unsigned int hi = 0, lo = 0;
600 int n = 0, i = 0;
601 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
602 hi= (hi << 4) | (lo >> 28);
603 lo= (lo << 4) | (n & 0xf);
604 }
605 */
606 // UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,param_get32ex(Cmd,0,0,10),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16)};
607 // PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
608 SendCommand(&c);
609 return 0;
610 }
611
612 int CmdHF14ASnoop(const char *Cmd) {
613 int param = 0;
614
615 uint8_t ctmp = param_getchar(Cmd, 0) ;
616 if (ctmp == 'h' || ctmp == 'H') {
617 PrintAndLog("It get data from the field and saves it into command buffer.");
618 PrintAndLog("Buffer accessible from command hf list 14a.");
619 PrintAndLog("Usage: hf 14a snoop [c][r]");
620 PrintAndLog("c - triggered by first data from card");
621 PrintAndLog("r - triggered by first 7-bit request from reader (REQ,WUP,...)");
622 PrintAndLog("sample: hf 14a snoop c r");
623 return 0;
624 }
625
626 for (int i = 0; i < 2; i++) {
627 ctmp = param_getchar(Cmd, i);
628 if (ctmp == 'c' || ctmp == 'C') param |= 0x01;
629 if (ctmp == 'r' || ctmp == 'R') param |= 0x02;
630 }
631
632 UsbCommand c = {CMD_SNOOP_ISO_14443a, {param, 0, 0}};
633 SendCommand(&c);
634 return 0;
635 }
636
637 int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int *dataoutlen) {
638 uint8_t data[USB_CMD_DATA_SIZE];
639 int datalen;
640 uint16_t cmdc = 0;
641 uint8_t first, second;
642 static uint8_t iso14_pcb_blocknum;
643
644 if (activateField) {
645 cmdc |= ISO14A_CONNECT | ISO14A_CLEAR_TRACE;
646 iso14_pcb_blocknum = 0;
647 }
648 if (leaveSignalON)
649 cmdc |= ISO14A_NO_DISCONNECT;
650
651 // ISO 14443 APDU frame: PCB [CID] [NAD] APDU CRC PCB=0x02
652 memcpy(data + 1, datain, datainlen);
653 data[0] = 0x02; // bnr,nad,cid,chn=0; i-block(0x00)
654 data[0] += iso14_pcb_blocknum; // add block number (bnr)
655
656 datalen = datainlen + 1;
657
658 ComputeCrc14443(CRC_14443_A, data, datalen, &first, &second);
659 data[datalen++] = first;
660 data[datalen++] = second;
661
662 // "Command APDU" length should be 5+255+1, but javacard's APDU buffer might be smaller - 133 bytes
663 // https://stackoverflow.com/questions/32994936/safe-max-java-card-apdu-data-command-and-respond-size
664 // here length USB_CMD_DATA_SIZE=512
665 // timeout timeout14a * 1.06 / 100, true, size, &keyBlock[6 * c], e_sector); // timeout is (ms * 106)/10 or us*0.0106
666 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_SET_TIMEOUT | cmdc, (datalen & 0xFFFF), 1000 * 1000 * 1.06 / 100}};
667 memcpy(c.d.asBytes, data, datalen);
668 SendCommand(&c);
669
670 uint8_t *recv;
671 UsbCommand resp;
672
673 if (activateField) {
674 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500))
675 return 1;
676 if (resp.arg[0] != 1)
677 return 1;
678 }
679
680 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
681 recv = resp.d.asBytes;
682 uint8_t iLen = resp.arg[0];
683
684 *dataoutlen = iLen - 1 - 2;
685 if (*dataoutlen < 0)
686 *dataoutlen = 0;
687 memcpy(dataout, recv + 1, *dataoutlen);
688
689 if(!iLen)
690 return 1;
691
692 // check apdu length
693 if (iLen < 5) {
694 PrintAndLog("APDU ERROR: Small APDU response.");
695 return 2;
696 }
697
698 // invert block number field in PCB byte
699 if ( ((recv[0] & 0xC0) == 0 // I-Block
700 || (recv[0] & 0xD0) == 0x80) // R-Block with ACK bit set to 0
701 && (recv[0] & 0x01) == iso14_pcb_blocknum) // equal block numbers
702 {
703 iso14_pcb_blocknum ^= 1;
704 }
705
706 // check block
707 if (data[0] != recv[0]) {
708 PrintAndLog("APDU ERROR: Block type mismatch: %02x-%02x", data[0], recv[0]);
709 return 2;
710 }
711
712 // CRC Check
713 ComputeCrc14443(CRC_14443_A, recv, iLen, &first, &second);
714 if (first || second) {
715 PrintAndLog("APDU ERROR: ISO 14443A CRC error.");
716 return 3;
717 }
718
719 } else {
720 PrintAndLog("APDU ERROR: Reply timeout.");
721 return 4;
722 }
723
724 return 0;
725 }
726
727 int CmdHF14AAPDU(const char *cmd) {
728 uint8_t data[USB_CMD_DATA_SIZE];
729 int datalen = 0;
730 bool activateField = false;
731 bool leaveSignalON = false;
732 bool decodeTLV = false;
733
734 if (strlen(cmd) < 2) {
735 PrintAndLog("Usage: hf 14a apdu [-s] [-k] [-t] <APDU (hex)>");
736 PrintAndLog(" -s activate field and select card");
737 PrintAndLog(" -k leave the signal field ON after receive response");
738 PrintAndLog(" -t executes TLV decoder if it possible. TODO!!!!");
739 return 0;
740 }
741
742 int cmdp = 0;
743 while(param_getchar(cmd, cmdp) != 0x00) {
744 char c = param_getchar(cmd, cmdp);
745 if ((c == '-') && (param_getlength(cmd, cmdp) == 2))
746 switch (param_getchar_indx(cmd, 1, cmdp)) {
747 case 's':
748 case 'S':
749 activateField = true;
750 break;
751 case 'k':
752 case 'K':
753 leaveSignalON = true;
754 break;
755 case 't':
756 case 'T':
757 decodeTLV = true;
758 break;
759 default:
760 PrintAndLog("Unknown parameter '%c'", param_getchar_indx(cmd, 1, cmdp));
761 return 1;
762 }
763
764 if (isxdigit(c)) {
765 // len = data + PCB(1b) + CRC(2b)
766 switch(param_gethex_to_eol(cmd, cmdp, data, sizeof(data) - 1 - 2, &datalen)) {
767 case 1:
768 PrintAndLog("Invalid HEX value.");
769 return 1;
770 case 2:
771 PrintAndLog("APDU too large.");
772 return 1;
773 case 3:
774 PrintAndLog("Hex must have even number of digits.");
775 return 1;
776 }
777
778 // we get all the hex to end of line with spaces
779 break;
780 }
781
782 cmdp++;
783 }
784
785 PrintAndLog("--%s %s %s >>>> %s", activateField ? "sel": "", leaveSignalON ? "keep": "", decodeTLV ? "TLV": "", sprint_hex(data, datalen));
786
787 switch(ExchangeAPDU14a(data, datalen, activateField, leaveSignalON, data, &datalen)) {
788 case 0:
789 break;
790 case 1:
791 PrintAndLog("APDU ERROR: Send APDU error.");
792 return 1;
793 case 2:
794 return 2;
795 case 3:
796 return 3;
797 case 4:
798 return 4;
799 default:
800 return 5;
801 }
802
803 PrintAndLog("<<<< %s", sprint_hex(data, datalen));
804
805 PrintAndLog("APDU response: %02x %02x - %s", data[datalen - 2], data[datalen - 1], GetAPDUCodeDescription(data[datalen - 2], data[datalen - 1]));
806
807 // TLV decoder
808 if (decodeTLV && datalen > 4) {
809 TLVPrintFromBuffer(data, datalen - 2);
810 }
811
812 return 0;
813 }
814
815 int CmdHF14ACmdRaw(const char *cmd) {
816 UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
817 bool reply=1;
818 bool crc = false;
819 bool power = false;
820 bool active = false;
821 bool active_select = false;
822 bool no_rats = false;
823 uint16_t numbits = 0;
824 bool bTimeout = false;
825 uint32_t timeout = 0;
826 bool topazmode = false;
827 char buf[5]="";
828 int i = 0;
829 uint8_t data[USB_CMD_DATA_SIZE];
830 uint16_t datalen = 0;
831 uint32_t temp;
832
833 if (strlen(cmd)<2) {
834 PrintAndLog("Usage: hf 14a raw [-r] [-c] [-p] [-f] [-b] [-t] <number of bits> <0A 0B 0C ... hex>");
835 PrintAndLog(" -r do not read response");
836 PrintAndLog(" -c calculate and append CRC");
837 PrintAndLog(" -p leave the signal field ON after receive");
838 PrintAndLog(" -a active signal field ON without select");
839 PrintAndLog(" -s active signal field ON with select");
840 PrintAndLog(" -b number of bits to send. Useful for send partial byte");
841 PrintAndLog(" -t timeout in ms");
842 PrintAndLog(" -T use Topaz protocol to send command");
843 PrintAndLog(" -3 ISO14443-3 select only (skip RATS)");
844 return 0;
845 }
846
847
848 // strip
849 while (*cmd==' ' || *cmd=='\t') cmd++;
850
851 while (cmd[i]!='\0') {
852 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
853 if (cmd[i]=='-') {
854 switch (cmd[i+1]) {
855 case 'r':
856 reply = false;
857 break;
858 case 'c':
859 crc = true;
860 break;
861 case 'p':
862 power = true;
863 break;
864 case 'a':
865 active = true;
866 break;
867 case 's':
868 active_select = true;
869 break;
870 case 'b':
871 sscanf(cmd+i+2,"%d",&temp);
872 numbits = temp & 0xFFFF;
873 i+=3;
874 while(cmd[i]!=' ' && cmd[i]!='\0') { i++; }
875 i-=2;
876 break;
877 case 't':
878 bTimeout = true;
879 sscanf(cmd+i+2,"%d",&temp);
880 timeout = temp;
881 i+=3;
882 while(cmd[i]!=' ' && cmd[i]!='\0') { i++; }
883 i-=2;
884 break;
885 case 'T':
886 topazmode = true;
887 break;
888 case '3':
889 no_rats = true;
890 break;
891 default:
892 PrintAndLog("Invalid option");
893 return 0;
894 }
895 i+=2;
896 continue;
897 }
898 if ((cmd[i]>='0' && cmd[i]<='9') ||
899 (cmd[i]>='a' && cmd[i]<='f') ||
900 (cmd[i]>='A' && cmd[i]<='F') ) {
901 buf[strlen(buf)+1]=0;
902 buf[strlen(buf)]=cmd[i];
903 i++;
904
905 if (strlen(buf)>=2) {
906 sscanf(buf,"%x",&temp);
907 data[datalen]=(uint8_t)(temp & 0xff);
908 *buf=0;
909 if (datalen > sizeof(data)-1) {
910 if (crc)
911 PrintAndLog("Buffer is full, we can't add CRC to your data");
912 break;
913 } else {
914 datalen++;
915 }
916 }
917 continue;
918 }
919 PrintAndLog("Invalid char on input");
920 return 0;
921 }
922
923 if(crc && datalen>0 && datalen<sizeof(data)-2)
924 {
925 uint8_t first, second;
926 if (topazmode) {
927 ComputeCrc14443(CRC_14443_B, data, datalen, &first, &second);
928 } else {
929 ComputeCrc14443(CRC_14443_A, data, datalen, &first, &second);
930 }
931 data[datalen++] = first;
932 data[datalen++] = second;
933 }
934
935 if(active || active_select)
936 {
937 c.arg[0] |= ISO14A_CONNECT | ISO14A_CLEAR_TRACE;
938 if(active)
939 c.arg[0] |= ISO14A_NO_SELECT;
940 }
941
942 if(bTimeout){
943 #define MAX_TIMEOUT 40542464 // = (2^32-1) * (8*16) / 13560000Hz * 1000ms/s
944 c.arg[0] |= ISO14A_SET_TIMEOUT;
945 if(timeout > MAX_TIMEOUT) {
946 timeout = MAX_TIMEOUT;
947 PrintAndLog("Set timeout to 40542 seconds (11.26 hours). The max we can wait for response");
948 }
949 c.arg[2] = 13560000 / 1000 / (8*16) * timeout; // timeout in ETUs (time to transfer 1 bit, approx. 9.4 us)
950 }
951
952 if(power) {
953 c.arg[0] |= ISO14A_NO_DISCONNECT;
954 }
955
956 if(datalen > 0) {
957 c.arg[0] |= ISO14A_RAW;
958 }
959
960 if(topazmode) {
961 c.arg[0] |= ISO14A_TOPAZMODE;
962 }
963
964 if(no_rats) {
965 c.arg[0] |= ISO14A_NO_RATS;
966 }
967
968 // Max buffer is USB_CMD_DATA_SIZE (512)
969 c.arg[1] = (datalen & 0xFFFF) | ((uint32_t)numbits << 16);
970 memcpy(c.d.asBytes,data,datalen);
971
972 SendCommand(&c);
973
974 if (reply) {
975 int res = 0;
976 if (active_select)
977 res = waitCmd(1);
978 if (!res && datalen > 0)
979 waitCmd(0);
980 } // if reply
981 return 0;
982 }
983
984
985 static int waitCmd(uint8_t iSelect) {
986 uint8_t *recv;
987 UsbCommand resp;
988 char *hexout;
989
990 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
991 recv = resp.d.asBytes;
992 uint8_t iLen = resp.arg[0];
993 if (iSelect){
994 iLen = resp.arg[1];
995 if (iLen){
996 PrintAndLog("Card selected. UID[%i]:", iLen);
997 } else {
998 PrintAndLog("Can't select card.");
999 }
1000 } else {
1001 PrintAndLog("received %i bytes:", iLen);
1002 }
1003 if(!iLen)
1004 return 1;
1005 hexout = (char *)malloc(iLen * 3 + 1);
1006 if (hexout != NULL) {
1007 for (int i = 0; i < iLen; i++) { // data in hex
1008 sprintf(&hexout[i * 3], "%02X ", recv[i]);
1009 }
1010 PrintAndLog("%s", hexout);
1011 free(hexout);
1012 } else {
1013 PrintAndLog("malloc failed your client has low memory?");
1014 return 2;
1015 }
1016 } else {
1017 PrintAndLog("timeout while waiting for reply.");
1018 return 3;
1019 }
1020 return 0;
1021 }
1022
1023 static command_t CommandTable[] =
1024 {
1025 {"help", CmdHelp, 1, "This help"},
1026 {"list", CmdHF14AList, 0, "[Deprecated] List ISO 14443a history"},
1027 {"reader", CmdHF14AReader, 0, "Start acting like an ISO14443 Type A reader"},
1028 {"info", CmdHF14AInfo, 0, "Reads card and shows information about it"},
1029 {"cuids", CmdHF14ACUIDs, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"},
1030 {"sim", CmdHF14ASim, 0, "<UID> -- Simulate ISO 14443a tag"},
1031 {"snoop", CmdHF14ASnoop, 0, "Eavesdrop ISO 14443 Type A"},
1032 {"apdu", CmdHF14AAPDU, 0, "Send ISO 1443-4 APDU to tag"},
1033 {"raw", CmdHF14ACmdRaw, 0, "Send raw hex data to tag"},
1034 {NULL, NULL, 0, NULL}
1035 };
1036
1037 int CmdHF14A(const char *Cmd) {
1038 // flush
1039 WaitForResponseTimeout(CMD_ACK,NULL,100);
1040
1041 // parse
1042 CmdsParse(CommandTable, Cmd);
1043 return 0;
1044 }
1045
1046 int CmdHelp(const char *Cmd)
1047 {
1048 CmdsHelp(CommandTable);
1049 return 0;
1050 }
Impressum, Datenschutz