]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf14a.c
moved includes, added clear_trace flag (it was there...) and fixed multiapdu iso14443...
[proxmark3-svn] / client / cmdhf14a.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
fe346768 2// 2011, 2017 Merlok
534983d7 3// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
a553f267 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
7fe9b0b7 12#include "cmdhf14a.h"
13
eb6e8de4 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
7fe9b0b7 29static int CmdHelp(const char *Cmd);
f1a983a3 30static int waitCmd(uint8_t iLen);
7fe9b0b7 31
eb6e8de4 32
52ab55ab 33const 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
52ab55ab 106// get a product description based on the UID
107// uid[8] tag uid
108// returns description of the best match
b915fda3 109char* getTagInfo(uint8_t uid) {
52ab55ab 110
68033ed7 111 int i;
52ab55ab 112 int len = sizeof(manufactureMapping) / sizeof(manufactureName);
113
68033ed7
MHS
114 for ( i = 0; i < len; ++i )
115 if ( uid == manufactureMapping[i].uid)
116 return manufactureMapping[i].desc;
52ab55ab 117
68033ed7
MHS
118 //No match, return default
119 return manufactureMapping[len-1].desc;
52ab55ab 120}
121
7fe9b0b7 122int CmdHF14AList(const char *Cmd)
123{
4c3de57a 124 PrintAndLog("Deprecated command, use 'hf list 14a' instead");
f89c7050 125 return 0;
7fe9b0b7 126}
127
7dac1034
OM
128int 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
201int CmdHF14AInfo(const char *Cmd)
7fe9b0b7 202{
19d6d91f 203 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};
534983d7 204 SendCommand(&c);
902cb3c0 205
206 UsbCommand resp;
7bc95e2e 207 WaitForResponse(CMD_ACK,&resp);
902cb3c0 208
19d6d91f 209 iso14a_card_select_t card;
210 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
534983d7 211
ee1eadee 212 uint64_t select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
19d6d91f 213
214 if(select_status == 0) {
6ce0e538 215 if (Cmd[0] != 's') PrintAndLog("iso14443a card select failed");
52bfb955 216 // disconnect
217 c.arg[0] = 0;
218 c.arg[1] = 0;
219 c.arg[2] = 0;
220 SendCommand(&c);
534983d7 221 return 0;
222 }
223
ee1eadee 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
19d6d91f 235 PrintAndLog(" UID : %s", sprint_hex(card.uid, card.uidlen));
4745afb6 236 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
19d6d91f 237 PrintAndLog(" SAK : %02x [%d]", card.sak, resp.arg[0]);
713e7ffb 238
fe6bf3c5 239 bool isMifareClassic = true;
19d6d91f 240 switch (card.sak) {
4745afb6 241 case 0x00:
fe6bf3c5 242 isMifareClassic = false;
8ceb6b03 243
244 //***************************************test****************
245 // disconnect
246 c.arg[0] = 0;
247 c.arg[1] = 0;
248 c.arg[2] = 0;
249 SendCommand(&c);
250
c7442b76 251 uint32_t tagT = GetHF14AMfU_Type();
8ceb6b03 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
4745afb6 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
4b360379
MHS
287 uint8_t version[10] = {0};
288 memcpy(version, resp.d.asBytes, resp.arg[0] < sizeof(version) ? resp.arg[0] : sizeof(version));
4745afb6 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 }
8ceb6b03 297 */
4745afb6 298 break;
3fe4ff4f 299 case 0x01: PrintAndLog("TYPE : NXP TNP3xxx Activision Game Appliance"); break;
79a73ab2 300 case 0x04: PrintAndLog("TYPE : NXP MIFARE (various !DESFire !DESFire EV1)"); break;
e691fc45 301 case 0x08: PrintAndLog("TYPE : NXP MIFARE CLASSIC 1k | Plus 2k SL1"); break;
79a73ab2 302 case 0x09: PrintAndLog("TYPE : NXP MIFARE Mini 0.3k"); break;
e691fc45 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;
79a73ab2 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;
9ca155ba
M
312 default: ;
313 }
19d6d91f 314
4745afb6 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
19d6d91f 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
4b360379 331 memcpy(card.ats, resp.d.asBytes, resp.arg[0]);
9a573554 332 card.ats_len = resp.arg[0]; // note: ats_len includes CRC Bytes
19d6d91f 333 }
334
9a573554 335 if(card.ats_len >= 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes
561f7c11 336 bool ta1 = 0, tb1 = 0, tc1 = 0;
337 int pos;
338
9a573554 339 if (select_status == 2) {
19d6d91f 340 PrintAndLog("SAK incorrectly claims that card doesn't support RATS");
341 }
342 PrintAndLog(" ATS : %s", sprint_hex(card.ats, card.ats_len));
9a573554 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);
561f7c11 346 }
9a573554 347
348 if (card.ats[0] > 1) { // there is a format byte (T0)
19d6d91f 349 ta1 = (card.ats[1] & 0x10) == 0x10;
350 tb1 = (card.ats[1] & 0x20) == 0x20;
351 tc1 = (card.ats[1] & 0x40) == 0x40;
9a573554 352 int16_t fsci = card.ats[1] & 0x0f;
561f7c11 353 PrintAndLog(" - T0 : TA1 is%s present, TB1 is%s present, "
9a573554 354 "TC1 is%s present, FSCI is %d (FSC = %ld)",
561f7c11 355 (ta1 ? "" : " NOT"), (tb1 ? "" : " NOT"), (tc1 ? "" : " NOT"),
9a573554 356 fsci,
357 fsci < 5 ? (fsci - 2) * 8 :
358 fsci < 8 ? (fsci - 3) * 32 :
359 fsci == 8 ? 256 :
360 -1
361 );
561f7c11 362 }
363 pos = 2;
9a573554 364 if (ta1) {
561f7c11 365 char dr[16], ds[16];
366 dr[0] = ds[0] = '\0';
19d6d91f 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, ");
561f7c11 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]",
19d6d91f 377 (card.ats[pos] & 0x80 ? " NOT" : ""), dr, ds);
561f7c11 378 pos++;
379 }
9a573554 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 );
561f7c11 390 pos++;
391 }
9a573554 392 if (tc1) {
561f7c11 393 PrintAndLog(" - TC1 : NAD is%s supported, CID is%s supported",
19d6d91f 394 (card.ats[pos] & 0x01) ? "" : " NOT",
395 (card.ats[pos] & 0x02) ? "" : " NOT");
561f7c11 396 pos++;
397 }
9a573554 398 if (card.ats[0] > pos) {
561f7c11 399 char *tip = "";
9a573554 400 if (card.ats[0] - pos >= 7) {
19d6d91f 401 if (memcmp(card.ats + pos, "\xC1\x05\x2F\x2F\x01\xBC\xD6", 7) == 0) {
561f7c11 402 tip = "-> MIFARE Plus X 2K or 4K";
19d6d91f 403 } else if (memcmp(card.ats + pos, "\xC1\x05\x2F\x2F\x00\x35\xC7", 7) == 0) {
561f7c11 404 tip = "-> MIFARE Plus S 2K or 4K";
405 }
406 }
9a573554 407 PrintAndLog(" - HB : %s%s", sprint_hex(card.ats + pos, card.ats[0] - pos), tip);
19d6d91f 408 if (card.ats[pos] == 0xC1) {
561f7c11 409 PrintAndLog(" c1 -> Mifare or (multiple) virtual cards of various type");
410 PrintAndLog(" %02x -> Length is %d bytes",
19d6d91f 411 card.ats[pos + 1], card.ats[pos + 1]);
412 switch (card.ats[pos + 2] & 0xf0) {
561f7c11 413 case 0x10:
414 PrintAndLog(" 1x -> MIFARE DESFire");
415 break;
416 case 0x20:
417 PrintAndLog(" 2x -> MIFARE Plus");
418 break;
419 }
19d6d91f 420 switch (card.ats[pos + 2] & 0x0f) {
561f7c11 421 case 0x00:
422 PrintAndLog(" x0 -> <1 kByte");
423 break;
424 case 0x01:
7cdf6236 425 PrintAndLog(" x1 -> 1 kByte");
561f7c11 426 break;
427 case 0x02:
7cdf6236 428 PrintAndLog(" x2 -> 2 kByte");
561f7c11 429 break;
430 case 0x03:
7cdf6236 431 PrintAndLog(" x3 -> 4 kByte");
561f7c11 432 break;
433 case 0x04:
7cdf6236 434 PrintAndLog(" x4 -> 8 kByte");
561f7c11 435 break;
436 }
19d6d91f 437 switch (card.ats[pos + 3] & 0xf0) {
561f7c11 438 case 0x00:
439 PrintAndLog(" 0x -> Engineering sample");
440 break;
441 case 0x20:
442 PrintAndLog(" 2x -> Released");
443 break;
444 }
19d6d91f 445 switch (card.ats[pos + 3] & 0x0f) {
561f7c11 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 }
19d6d91f 456 switch (card.ats[pos + 4] & 0x0f) {
561f7c11 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 }
79a73ab2 469 } else {
19d6d91f 470 PrintAndLog("proprietary non iso14443-4 card found, RATS not supported");
471 }
534983d7 472
52ab55ab 473
474 // try to see if card responses to "chinese magic backdoor" commands.
e17660d5 475 mfCIdentify();
52ab55ab 476
fe6bf3c5
OM
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
19d6d91f 490 return select_status;
7fe9b0b7 491}
492
db22dfe6 493// Collect ISO14443 Type A UIDs
494int 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);
acf0582d 502 PrintAndLog("Start: %" PRIu64, msclock()/1000);
db22dfe6 503 // repeat n times
504 for (int i = 0; i < n; i++) {
505 // execute anticollision procedure
c04a4b60 506 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_RATS, 0, 0}};
db22dfe6 507 SendCommand(&c);
902cb3c0 508
72b1090a 509 UsbCommand resp;
510 WaitForResponse(CMD_ACK,&resp);
902cb3c0 511
72b1090a 512 iso14a_card_select_t *card = (iso14a_card_select_t *) resp.d.asBytes;
db22dfe6 513
514 // check if command failed
902cb3c0 515 if (resp.arg[0] == 0) {
db22dfe6 516 PrintAndLog("Card select failed.");
517 } else {
72b1090a 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]);
db22dfe6 521 }
72b1090a 522 PrintAndLog("%s", uid_string);
db22dfe6 523 }
524 }
acf0582d 525 PrintAndLog("End: %" PRIu64, msclock()/1000);
db22dfe6 526
527 return 1;
528}
529
7fe9b0b7 530// ## simulate iso14443a tag
531// ## greg - added ability to specify tag UID
532int CmdHF14ASim(const char *Cmd)
81cd0474 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");
5ee70129 547 PrintAndLog(" 3 = MIFARE Desfire");
81cd0474 548 PrintAndLog(" 4 = ISO/IEC 14443-4");
5ee70129 549 PrintAndLog(" 5 = MIFARE Tnp3xxx");
81cd0474 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) {
43534cba 562 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014" PRIx64 ")",long_uid);
81cd0474 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
81cd0474 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/*
7fe9b0b7 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 }
81cd0474 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]);
7fe9b0b7 608 SendCommand(&c);
609 return 0;
610}
611
5cd9ec01
M
612int CmdHF14ASnoop(const char *Cmd) {
613 int param = 0;
614
df3e429d 615 uint8_t ctmp = param_getchar(Cmd, 0) ;
616 if (ctmp == 'h' || ctmp == 'H') {
5cd9ec01 617 PrintAndLog("It get data from the field and saves it into command buffer.");
4c3de57a 618 PrintAndLog("Buffer accessible from command hf list 14a.");
5cd9ec01
M
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++) {
df3e429d 627 ctmp = param_getchar(Cmd, i);
5cd9ec01
M
628 if (ctmp == 'c' || ctmp == 'C') param |= 0x01;
629 if (ctmp == 'r' || ctmp == 'R') param |= 0x02;
630 }
631
e57c8b2e 632 UsbCommand c = {CMD_SNOOP_ISO_14443a, {param, 0, 0}};
633 SendCommand(&c);
634 return 0;
7fe9b0b7 635}
636
d1300b47 637int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int *dataoutlen) {
1208cdcb 638 uint8_t data[USB_CMD_DATA_SIZE];
d1300b47 639 int datalen;
eb6e8de4 640 uint16_t cmdc = 0;
f2b0169c 641 uint8_t first, second;
eb6e8de4 642 static uint8_t iso14_pcb_blocknum;
d1300b47 643
eb6e8de4 644 if (activateField) {
645 cmdc |= ISO14A_CONNECT | ISO14A_CLEAR_TRACE;
646 iso14_pcb_blocknum = 0;
647 }
d1300b47 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)
eb6e8de4 654 data[0] += iso14_pcb_blocknum; // add block number (bnr)
655
d1300b47 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 }
eb6e8de4 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 }
d1300b47 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
727int CmdHF14AAPDU(const char *cmd) {
728 uint8_t data[USB_CMD_DATA_SIZE];
729 int datalen = 0;
980417ea 730 bool activateField = false;
731 bool leaveSignalON = false;
732 bool decodeTLV = false;
1208cdcb 733
8019540b 734 if (strlen(cmd) < 2) {
7710983b 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");
5bcb3496 738 PrintAndLog(" -t executes TLV decoder if it possible. TODO!!!!");
7710983b 739 return 0;
740 }
980417ea 741
8019540b 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)) {
980417ea 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:
8019540b 760 PrintAndLog("Unknown parameter '%c'", param_getchar_indx(cmd, 1, cmdp));
980417ea 761 return 1;
762 }
8019540b 763
764 if (isxdigit(c)) {
d1300b47 765 // len = data + PCB(1b) + CRC(2b)
766 switch(param_gethex_to_eol(cmd, cmdp, data, sizeof(data) - 1 - 2, &datalen)) {
8019540b 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;
980417ea 776 }
8019540b 777
778 // we get all the hex to end of line with spaces
779 break;
980417ea 780 }
8019540b 781
782 cmdp++;
980417ea 783 }
f2b0169c 784
785 PrintAndLog("--%s %s %s >>>> %s", activateField ? "sel": "", leaveSignalON ? "keep": "", decodeTLV ? "TLV": "", sprint_hex(data, datalen));
7710983b 786
d1300b47 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:
f1a983a3 794 return 2;
d1300b47 795 case 3:
796 return 3;
797 case 4:
798 return 4;
799 default:
800 return 5;
980417ea 801 }
802
d1300b47 803 PrintAndLog("<<<< %s", sprint_hex(data, datalen));
804
fe346768 805 PrintAndLog("APDU response: %02x %02x - %s", data[datalen - 2], data[datalen - 1], GetAPDUCodeDescription(data[datalen - 2], data[datalen - 1]));
f2b0169c 806
23207d74 807 // TLV decoder
a2bb2735 808 if (decodeTLV && datalen > 4) {
809 TLVPrintFromBuffer(data, datalen - 2);
d1300b47 810 }
7710983b 811
812 return 0;
813}
48ece4a7 814
5f6d6c90 815int CmdHF14ACmdRaw(const char *cmd) {
e57c8b2e 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;
c04a4b60 822 bool no_rats = false;
e57c8b2e 823 uint16_t numbits = 0;
7cb8516c 824 bool bTimeout = false;
48ece4a7 825 uint32_t timeout = 0;
7cb8516c 826 bool topazmode = false;
e57c8b2e 827 char buf[5]="";
828 int i = 0;
829 uint8_t data[USB_CMD_DATA_SIZE];
48ece4a7 830 uint16_t datalen = 0;
19a700a8 831 uint32_t temp;
5f6d6c90 832
e57c8b2e 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");
19a700a8 841 PrintAndLog(" -t timeout in ms");
48ece4a7 842 PrintAndLog(" -T use Topaz protocol to send command");
c04a4b60 843 PrintAndLog(" -3 ISO14443-3 select only (skip RATS)");
e57c8b2e 844 return 0;
845 }
5f6d6c90 846
e57c8b2e 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;
79bf1ad2 877 case 't':
7cb8516c 878 bTimeout = true;
79bf1ad2 879 sscanf(cmd+i+2,"%d",&temp);
19a700a8 880 timeout = temp;
79bf1ad2 881 i+=3;
882 while(cmd[i]!=' ' && cmd[i]!='\0') { i++; }
04bc1c66 883 i-=2;
79bf1ad2 884 break;
e57c8b2e 885 case 'T':
7cb8516c 886 topazmode = true;
48ece4a7 887 break;
c04a4b60 888 case '3':
889 no_rats = true;
890 break;
e57c8b2e 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) {
79bf1ad2 910 if (crc)
911 PrintAndLog("Buffer is full, we can't add CRC to your data");
912 break;
e57c8b2e 913 } else {
b4810303 914 datalen++;
79bf1ad2 915 }
e57c8b2e 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;
48ece4a7 926 if (topazmode) {
927 ComputeCrc14443(CRC_14443_B, data, datalen, &first, &second);
928 } else {
929 ComputeCrc14443(CRC_14443_A, data, datalen, &first, &second);
930 }
e57c8b2e 931 data[datalen++] = first;
932 data[datalen++] = second;
933 }
5f6d6c90 934
e57c8b2e 935 if(active || active_select)
936 {
eb6e8de4 937 c.arg[0] |= ISO14A_CONNECT | ISO14A_CLEAR_TRACE;
e57c8b2e 938 if(active)
939 c.arg[0] |= ISO14A_NO_SELECT;
940 }
04bc1c66 941
79bf1ad2 942 if(bTimeout){
e57c8b2e 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 }
04bc1c66 949 c.arg[2] = 13560000 / 1000 / (8*16) * timeout; // timeout in ETUs (time to transfer 1 bit, approx. 9.4 us)
79bf1ad2 950 }
48ece4a7 951
e57c8b2e 952 if(power) {
953 c.arg[0] |= ISO14A_NO_DISCONNECT;
954 }
48ece4a7 955
29435274 956 if(datalen > 0) {
e57c8b2e 957 c.arg[0] |= ISO14A_RAW;
958 }
5f6d6c90 959
29435274 960 if(topazmode) {
48ece4a7 961 c.arg[0] |= ISO14A_TOPAZMODE;
e57c8b2e 962 }
963
c04a4b60 964 if(no_rats) {
965 c.arg[0] |= ISO14A_NO_RATS;
966 }
967
e57c8b2e 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) {
f1a983a3 975 int res = 0;
976 if (active_select)
977 res = waitCmd(1);
978 if (!res && datalen > 0)
e57c8b2e 979 waitCmd(0);
980 } // if reply
981 return 0;
5f6d6c90 982}
983
48ece4a7 984
f1a983a3 985static int waitCmd(uint8_t iSelect) {
5f6d6c90 986 uint8_t *recv;
987 UsbCommand resp;
988 char *hexout;
989
b915fda3 990 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
5f6d6c90 991 recv = resp.d.asBytes;
618c220c
OM
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 }
5f6d6c90 1003 if(!iLen)
f1a983a3 1004 return 1;
5f6d6c90 1005 hexout = (char *)malloc(iLen * 3 + 1);
1006 if (hexout != NULL) {
5f6d6c90 1007 for (int i = 0; i < iLen; i++) { // data in hex
f66021cf 1008 sprintf(&hexout[i * 3], "%02X ", recv[i]);
5f6d6c90 1009 }
1010 PrintAndLog("%s", hexout);
1011 free(hexout);
1012 } else {
1013 PrintAndLog("malloc failed your client has low memory?");
f1a983a3 1014 return 2;
5f6d6c90 1015 }
1016 } else {
1017 PrintAndLog("timeout while waiting for reply.");
f1a983a3 1018 return 3;
5f6d6c90 1019 }
f1a983a3 1020 return 0;
5f6d6c90 1021}
1022
7fe9b0b7 1023static command_t CommandTable[] =
1024{
5acd09bd 1025 {"help", CmdHelp, 1, "This help"},
4c3de57a 1026 {"list", CmdHF14AList, 0, "[Deprecated] List ISO 14443a history"},
7dac1034
OM
1027 {"reader", CmdHF14AReader, 0, "Start acting like an ISO14443 Type A reader"},
1028 {"info", CmdHF14AInfo, 0, "Reads card and shows information about it"},
5acd09bd 1029 {"cuids", CmdHF14ACUIDs, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"},
df3e429d 1030 {"sim", CmdHF14ASim, 0, "<UID> -- Simulate ISO 14443a tag"},
5acd09bd 1031 {"snoop", CmdHF14ASnoop, 0, "Eavesdrop ISO 14443 Type A"},
7710983b 1032 {"apdu", CmdHF14AAPDU, 0, "Send ISO 1443-4 APDU to tag"},
5f6d6c90 1033 {"raw", CmdHF14ACmdRaw, 0, "Send raw hex data to tag"},
7fe9b0b7 1034 {NULL, NULL, 0, NULL}
1035};
1036
902cb3c0 1037int CmdHF14A(const char *Cmd) {
f397b5cc 1038 // flush
902cb3c0 1039 WaitForResponseTimeout(CMD_ACK,NULL,100);
f397b5cc
M
1040
1041 // parse
7fe9b0b7 1042 CmdsParse(CommandTable, Cmd);
1043 return 0;
1044}
1045
1046int CmdHelp(const char *Cmd)
1047{
1048 CmdsHelp(CommandTable);
1049 return 0;
1050}
Impressum, Datenschutz