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