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