]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf14a.c
eliminate double DropField command
[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
b7d3e899 637void DropField() {
638 UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
639 SendCommand(&c);
640}
641
d1300b47 642int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int *dataoutlen) {
eb6e8de4 643 uint16_t cmdc = 0;
d1300b47 644
eb6e8de4 645 if (activateField) {
646 cmdc |= ISO14A_CONNECT | ISO14A_CLEAR_TRACE;
eb6e8de4 647 }
d1300b47 648 if (leaveSignalON)
649 cmdc |= ISO14A_NO_DISCONNECT;
650
d1300b47 651 // "Command APDU" length should be 5+255+1, but javacard's APDU buffer might be smaller - 133 bytes
652 // https://stackoverflow.com/questions/32994936/safe-max-java-card-apdu-data-command-and-respond-size
653 // here length USB_CMD_DATA_SIZE=512
654 // timeout timeout14a * 1.06 / 100, true, size, &keyBlock[6 * c], e_sector); // timeout is (ms * 106)/10 or us*0.0106
b7d3e899 655 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_APDU | ISO14A_SET_TIMEOUT | cmdc, (datainlen & 0xFFFF), 1000 * 1000 * 1.06 / 100}};
656 memcpy(c.d.asBytes, datain, datainlen);
d1300b47 657 SendCommand(&c);
658
659 uint8_t *recv;
660 UsbCommand resp;
661
662 if (activateField) {
b7d3e899 663 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
664 PrintAndLog("APDU ERROR: Proxmark connection timeout.");
d1300b47 665 return 1;
b7d3e899 666 }
667 if (resp.arg[0] != 1) {
668 PrintAndLog("APDU ERROR: Proxmark error %d.", resp.arg[0]);
d1300b47 669 return 1;
b7d3e899 670 }
d1300b47 671 }
672
673 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
674 recv = resp.d.asBytes;
b7d3e899 675 int iLen = resp.arg[0];
d1300b47 676
b7d3e899 677 *dataoutlen = iLen - 2;
d1300b47 678 if (*dataoutlen < 0)
679 *dataoutlen = 0;
b7d3e899 680 memcpy(dataout, recv, *dataoutlen);
d1300b47 681
b7d3e899 682 if(!iLen) {
683 PrintAndLog("APDU ERROR: No APDU response.");
d1300b47 684 return 1;
d1300b47 685 }
eb6e8de4 686
b7d3e899 687 // check block TODO
688 if (iLen == -2) {
689 PrintAndLog("APDU ERROR: Block type mismatch.");
d1300b47 690 return 2;
691 }
692
693 // CRC Check
b7d3e899 694 if (iLen == -1) {
d1300b47 695 PrintAndLog("APDU ERROR: ISO 14443A CRC error.");
696 return 3;
697 }
b7d3e899 698
699 // check apdu length
700 if (iLen < 4) {
701 PrintAndLog("APDU ERROR: Small APDU response. Len=%d", iLen);
702 return 2;
703 }
d1300b47 704
705 } else {
706 PrintAndLog("APDU ERROR: Reply timeout.");
707 return 4;
708 }
709
710 return 0;
711}
712
713int CmdHF14AAPDU(const char *cmd) {
714 uint8_t data[USB_CMD_DATA_SIZE];
715 int datalen = 0;
980417ea 716 bool activateField = false;
717 bool leaveSignalON = false;
718 bool decodeTLV = false;
1208cdcb 719
8019540b 720 if (strlen(cmd) < 2) {
7710983b 721 PrintAndLog("Usage: hf 14a apdu [-s] [-k] [-t] <APDU (hex)>");
722 PrintAndLog(" -s activate field and select card");
723 PrintAndLog(" -k leave the signal field ON after receive response");
5bcb3496 724 PrintAndLog(" -t executes TLV decoder if it possible. TODO!!!!");
7710983b 725 return 0;
726 }
980417ea 727
8019540b 728 int cmdp = 0;
729 while(param_getchar(cmd, cmdp) != 0x00) {
730 char c = param_getchar(cmd, cmdp);
731 if ((c == '-') && (param_getlength(cmd, cmdp) == 2))
732 switch (param_getchar_indx(cmd, 1, cmdp)) {
980417ea 733 case 's':
734 case 'S':
735 activateField = true;
736 break;
737 case 'k':
738 case 'K':
739 leaveSignalON = true;
740 break;
741 case 't':
742 case 'T':
743 decodeTLV = true;
744 break;
745 default:
8019540b 746 PrintAndLog("Unknown parameter '%c'", param_getchar_indx(cmd, 1, cmdp));
980417ea 747 return 1;
748 }
8019540b 749
750 if (isxdigit(c)) {
d1300b47 751 // len = data + PCB(1b) + CRC(2b)
752 switch(param_gethex_to_eol(cmd, cmdp, data, sizeof(data) - 1 - 2, &datalen)) {
8019540b 753 case 1:
754 PrintAndLog("Invalid HEX value.");
755 return 1;
756 case 2:
757 PrintAndLog("APDU too large.");
758 return 1;
759 case 3:
760 PrintAndLog("Hex must have even number of digits.");
761 return 1;
980417ea 762 }
8019540b 763
764 // we get all the hex to end of line with spaces
765 break;
980417ea 766 }
8019540b 767
768 cmdp++;
980417ea 769 }
f2b0169c 770
b7d3e899 771 PrintAndLog(">>>>[%s%s%s] %s", activateField ? "sel ": "", leaveSignalON ? "keep ": "", decodeTLV ? "TLV": "", sprint_hex(data, datalen));
7710983b 772
b7d3e899 773 int res = ExchangeAPDU14a(data, datalen, activateField, leaveSignalON, data, &datalen);
774
775 if (res)
776 return res;
980417ea 777
d1300b47 778 PrintAndLog("<<<< %s", sprint_hex(data, datalen));
779
fe346768 780 PrintAndLog("APDU response: %02x %02x - %s", data[datalen - 2], data[datalen - 1], GetAPDUCodeDescription(data[datalen - 2], data[datalen - 1]));
f2b0169c 781
23207d74 782 // TLV decoder
a2bb2735 783 if (decodeTLV && datalen > 4) {
784 TLVPrintFromBuffer(data, datalen - 2);
d1300b47 785 }
7710983b 786
787 return 0;
788}
48ece4a7 789
5f6d6c90 790int CmdHF14ACmdRaw(const char *cmd) {
e57c8b2e 791 UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
792 bool reply=1;
793 bool crc = false;
794 bool power = false;
795 bool active = false;
796 bool active_select = false;
c04a4b60 797 bool no_rats = false;
e57c8b2e 798 uint16_t numbits = 0;
7cb8516c 799 bool bTimeout = false;
48ece4a7 800 uint32_t timeout = 0;
7cb8516c 801 bool topazmode = false;
e57c8b2e 802 char buf[5]="";
803 int i = 0;
804 uint8_t data[USB_CMD_DATA_SIZE];
48ece4a7 805 uint16_t datalen = 0;
19a700a8 806 uint32_t temp;
5f6d6c90 807
e57c8b2e 808 if (strlen(cmd)<2) {
809 PrintAndLog("Usage: hf 14a raw [-r] [-c] [-p] [-f] [-b] [-t] <number of bits> <0A 0B 0C ... hex>");
810 PrintAndLog(" -r do not read response");
811 PrintAndLog(" -c calculate and append CRC");
812 PrintAndLog(" -p leave the signal field ON after receive");
813 PrintAndLog(" -a active signal field ON without select");
814 PrintAndLog(" -s active signal field ON with select");
815 PrintAndLog(" -b number of bits to send. Useful for send partial byte");
19a700a8 816 PrintAndLog(" -t timeout in ms");
48ece4a7 817 PrintAndLog(" -T use Topaz protocol to send command");
c04a4b60 818 PrintAndLog(" -3 ISO14443-3 select only (skip RATS)");
e57c8b2e 819 return 0;
820 }
5f6d6c90 821
e57c8b2e 822
823 // strip
824 while (*cmd==' ' || *cmd=='\t') cmd++;
825
826 while (cmd[i]!='\0') {
827 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
828 if (cmd[i]=='-') {
829 switch (cmd[i+1]) {
830 case 'r':
831 reply = false;
832 break;
833 case 'c':
834 crc = true;
835 break;
836 case 'p':
837 power = true;
838 break;
839 case 'a':
840 active = true;
841 break;
842 case 's':
843 active_select = true;
844 break;
845 case 'b':
846 sscanf(cmd+i+2,"%d",&temp);
847 numbits = temp & 0xFFFF;
848 i+=3;
849 while(cmd[i]!=' ' && cmd[i]!='\0') { i++; }
850 i-=2;
851 break;
79bf1ad2 852 case 't':
7cb8516c 853 bTimeout = true;
79bf1ad2 854 sscanf(cmd+i+2,"%d",&temp);
19a700a8 855 timeout = temp;
79bf1ad2 856 i+=3;
857 while(cmd[i]!=' ' && cmd[i]!='\0') { i++; }
04bc1c66 858 i-=2;
79bf1ad2 859 break;
e57c8b2e 860 case 'T':
7cb8516c 861 topazmode = true;
48ece4a7 862 break;
c04a4b60 863 case '3':
864 no_rats = true;
865 break;
e57c8b2e 866 default:
867 PrintAndLog("Invalid option");
868 return 0;
869 }
870 i+=2;
871 continue;
872 }
873 if ((cmd[i]>='0' && cmd[i]<='9') ||
874 (cmd[i]>='a' && cmd[i]<='f') ||
875 (cmd[i]>='A' && cmd[i]<='F') ) {
876 buf[strlen(buf)+1]=0;
877 buf[strlen(buf)]=cmd[i];
878 i++;
879
880 if (strlen(buf)>=2) {
881 sscanf(buf,"%x",&temp);
882 data[datalen]=(uint8_t)(temp & 0xff);
883 *buf=0;
884 if (datalen > sizeof(data)-1) {
79bf1ad2 885 if (crc)
886 PrintAndLog("Buffer is full, we can't add CRC to your data");
887 break;
e57c8b2e 888 } else {
b4810303 889 datalen++;
79bf1ad2 890 }
e57c8b2e 891 }
892 continue;
893 }
894 PrintAndLog("Invalid char on input");
895 return 0;
896 }
897
898 if(crc && datalen>0 && datalen<sizeof(data)-2)
899 {
900 uint8_t first, second;
48ece4a7 901 if (topazmode) {
902 ComputeCrc14443(CRC_14443_B, data, datalen, &first, &second);
903 } else {
904 ComputeCrc14443(CRC_14443_A, data, datalen, &first, &second);
905 }
e57c8b2e 906 data[datalen++] = first;
907 data[datalen++] = second;
908 }
5f6d6c90 909
e57c8b2e 910 if(active || active_select)
911 {
eb6e8de4 912 c.arg[0] |= ISO14A_CONNECT | ISO14A_CLEAR_TRACE;
e57c8b2e 913 if(active)
914 c.arg[0] |= ISO14A_NO_SELECT;
915 }
04bc1c66 916
79bf1ad2 917 if(bTimeout){
e57c8b2e 918 #define MAX_TIMEOUT 40542464 // = (2^32-1) * (8*16) / 13560000Hz * 1000ms/s
919 c.arg[0] |= ISO14A_SET_TIMEOUT;
920 if(timeout > MAX_TIMEOUT) {
921 timeout = MAX_TIMEOUT;
922 PrintAndLog("Set timeout to 40542 seconds (11.26 hours). The max we can wait for response");
923 }
04bc1c66 924 c.arg[2] = 13560000 / 1000 / (8*16) * timeout; // timeout in ETUs (time to transfer 1 bit, approx. 9.4 us)
79bf1ad2 925 }
48ece4a7 926
e57c8b2e 927 if(power) {
928 c.arg[0] |= ISO14A_NO_DISCONNECT;
929 }
48ece4a7 930
29435274 931 if(datalen > 0) {
e57c8b2e 932 c.arg[0] |= ISO14A_RAW;
933 }
5f6d6c90 934
29435274 935 if(topazmode) {
48ece4a7 936 c.arg[0] |= ISO14A_TOPAZMODE;
e57c8b2e 937 }
938
c04a4b60 939 if(no_rats) {
940 c.arg[0] |= ISO14A_NO_RATS;
941 }
942
e57c8b2e 943 // Max buffer is USB_CMD_DATA_SIZE (512)
944 c.arg[1] = (datalen & 0xFFFF) | ((uint32_t)numbits << 16);
945 memcpy(c.d.asBytes,data,datalen);
946
947 SendCommand(&c);
948
949 if (reply) {
f1a983a3 950 int res = 0;
951 if (active_select)
952 res = waitCmd(1);
953 if (!res && datalen > 0)
e57c8b2e 954 waitCmd(0);
955 } // if reply
956 return 0;
5f6d6c90 957}
958
48ece4a7 959
f1a983a3 960static int waitCmd(uint8_t iSelect) {
5f6d6c90 961 uint8_t *recv;
962 UsbCommand resp;
963 char *hexout;
964
b915fda3 965 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
5f6d6c90 966 recv = resp.d.asBytes;
618c220c
OM
967 uint8_t iLen = resp.arg[0];
968 if (iSelect){
969 iLen = resp.arg[1];
970 if (iLen){
971 PrintAndLog("Card selected. UID[%i]:", iLen);
972 } else {
973 PrintAndLog("Can't select card.");
974 }
975 } else {
976 PrintAndLog("received %i bytes:", iLen);
977 }
5f6d6c90 978 if(!iLen)
f1a983a3 979 return 1;
5f6d6c90 980 hexout = (char *)malloc(iLen * 3 + 1);
981 if (hexout != NULL) {
5f6d6c90 982 for (int i = 0; i < iLen; i++) { // data in hex
f66021cf 983 sprintf(&hexout[i * 3], "%02X ", recv[i]);
5f6d6c90 984 }
985 PrintAndLog("%s", hexout);
986 free(hexout);
987 } else {
988 PrintAndLog("malloc failed your client has low memory?");
f1a983a3 989 return 2;
5f6d6c90 990 }
991 } else {
992 PrintAndLog("timeout while waiting for reply.");
f1a983a3 993 return 3;
5f6d6c90 994 }
f1a983a3 995 return 0;
5f6d6c90 996}
997
7fe9b0b7 998static command_t CommandTable[] =
999{
5acd09bd 1000 {"help", CmdHelp, 1, "This help"},
4c3de57a 1001 {"list", CmdHF14AList, 0, "[Deprecated] List ISO 14443a history"},
7dac1034
OM
1002 {"reader", CmdHF14AReader, 0, "Start acting like an ISO14443 Type A reader"},
1003 {"info", CmdHF14AInfo, 0, "Reads card and shows information about it"},
5acd09bd 1004 {"cuids", CmdHF14ACUIDs, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"},
df3e429d 1005 {"sim", CmdHF14ASim, 0, "<UID> -- Simulate ISO 14443a tag"},
5acd09bd 1006 {"snoop", CmdHF14ASnoop, 0, "Eavesdrop ISO 14443 Type A"},
7710983b 1007 {"apdu", CmdHF14AAPDU, 0, "Send ISO 1443-4 APDU to tag"},
5f6d6c90 1008 {"raw", CmdHF14ACmdRaw, 0, "Send raw hex data to tag"},
7fe9b0b7 1009 {NULL, NULL, 0, NULL}
1010};
1011
902cb3c0 1012int CmdHF14A(const char *Cmd) {
f397b5cc 1013 // flush
902cb3c0 1014 WaitForResponseTimeout(CMD_ACK,NULL,100);
f397b5cc
M
1015
1016 // parse
7fe9b0b7 1017 CmdsParse(CommandTable, Cmd);
1018 return 0;
1019}
1020
1021int CmdHelp(const char *Cmd)
1022{
1023 CmdsHelp(CommandTable);
1024 return 0;
1025}
Impressum, Datenschutz