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