]>
Commit | Line | Data |
---|---|---|
43591e64 | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2018 iceman | |
3 | // | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // Proxmark3 RDV40 Smartcard module commands | |
9 | //----------------------------------------------------------------------------- | |
10 | #include "cmdsmartcard.h" | |
11 | #include "smartcard.h" | |
12 | #include "comms.h" | |
13 | #include "protocols.h" | |
14 | ||
15 | ||
16 | static int CmdHelp(const char *Cmd); | |
17 | ||
18 | int usage_sm_raw(void) { | |
19 | PrintAndLog("Usage: sc raw [h|r|c] d <0A 0B 0C ... hex>"); | |
20 | PrintAndLog(" h : this help"); | |
21 | PrintAndLog(" r : do not read response"); | |
22 | PrintAndLog(" a : active signal field ON without select"); | |
23 | PrintAndLog(" s : active signal field ON with select"); | |
24 | PrintAndLog(" t : executes TLV decoder if it is possible"); | |
25 | PrintAndLog(" d <bytes> : bytes to send"); | |
26 | PrintAndLog(""); | |
27 | PrintAndLog("Examples:"); | |
28 | PrintAndLog(" sc raw d 11223344"); | |
29 | return 0; | |
30 | } | |
31 | int usage_sm_reader(void) { | |
32 | PrintAndLog("Usage: sc reader [h|s]"); | |
33 | PrintAndLog(" h : this help"); | |
34 | PrintAndLog(" s : silent (no messages)"); | |
35 | PrintAndLog(""); | |
36 | PrintAndLog("Examples:"); | |
37 | PrintAndLog(" sc reader"); | |
38 | return 0; | |
39 | } | |
40 | int usage_sm_info(void) { | |
41 | PrintAndLog("Usage: sc info [h|s]"); | |
42 | PrintAndLog(" h : this help"); | |
43 | PrintAndLog(" s : silent (no messages)"); | |
44 | PrintAndLog(""); | |
45 | PrintAndLog("Examples:"); | |
46 | PrintAndLog(" sc info"); | |
47 | return 0; | |
48 | } | |
49 | int usage_sm_upgrade(void) { | |
50 | PrintAndLog("Upgrade firmware"); | |
51 | PrintAndLog("Usage: sc upgrade f <file name>"); | |
52 | PrintAndLog(" h : this help"); | |
53 | PrintAndLog(" f <filename> : firmware file name"); | |
54 | PrintAndLog(""); | |
55 | PrintAndLog("Examples:"); | |
56 | PrintAndLog(" sc upgrade f myfile"); | |
57 | PrintAndLog(""); | |
58 | PrintAndLog("WARNING - Dangerous command, do wrong and you will brick the smart card socket"); | |
59 | return 0; | |
60 | } | |
61 | int usage_sm_setclock(void) { | |
62 | PrintAndLog("Usage: sc setclock [h] c <clockspeed>"); | |
63 | PrintAndLog(" h : this help"); | |
64 | PrintAndLog(" c <> : clockspeed (0 = 16mhz, 1=8mhz, 2=4mhz) "); | |
65 | PrintAndLog(""); | |
66 | PrintAndLog("Examples:"); | |
67 | PrintAndLog(" sc setclock c 2"); | |
68 | return 0; | |
69 | } | |
70 | ||
71 | int CmdSmartRaw(const char *Cmd) { | |
72 | ||
73 | int hexlen = 0; | |
74 | bool active = false; | |
75 | bool active_select = false; | |
76 | uint8_t cmdp = 0; | |
77 | bool errors = false, reply = true, decodeTLV = false, breakloop = false; | |
78 | uint8_t data[USB_CMD_DATA_SIZE] = {0x00}; | |
79 | ||
80 | while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { | |
81 | switch (tolower(param_getchar(Cmd, cmdp))) { | |
82 | case 'h': return usage_sm_raw(); | |
83 | case 'r': | |
84 | reply = false; | |
85 | cmdp++; | |
86 | break; | |
87 | case 'a': | |
88 | active = true; | |
89 | cmdp++; | |
90 | break; | |
91 | case 's': | |
92 | active_select = true; | |
93 | cmdp++; | |
94 | break; | |
95 | case 't': | |
96 | decodeTLV = true; | |
97 | cmdp++; | |
98 | break; | |
99 | case 'd': { | |
100 | switch (param_gethex_to_eol(Cmd, cmdp+1, data, sizeof(data), &hexlen)) { | |
101 | case 1: | |
102 | PrintAndLog("Invalid HEX value."); | |
103 | return 1; | |
104 | case 2: | |
105 | PrintAndLog("Too many bytes. Max %d bytes", sizeof(data)); | |
106 | return 1; | |
107 | case 3: | |
108 | PrintAndLog("Hex must have an even number of digits."); | |
109 | return 1; | |
110 | } | |
111 | cmdp++; | |
112 | breakloop = true; | |
113 | break; | |
114 | } | |
115 | default: | |
116 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
117 | errors = true; | |
118 | break; | |
119 | } | |
120 | ||
121 | if ( breakloop ) | |
122 | break; | |
123 | } | |
124 | ||
125 | //Validations | |
126 | if (errors || cmdp == 0 ) return usage_sm_raw(); | |
127 | ||
128 | // arg0 = RFU flags | |
129 | // arg1 = length | |
130 | UsbCommand c = {CMD_SMART_RAW, {0, hexlen, 0}}; | |
131 | ||
132 | if (active || active_select) { | |
133 | c.arg[0] |= SC_CONNECT; | |
134 | if (active) | |
135 | c.arg[0] |= SC_NO_SELECT; | |
136 | } | |
137 | ||
138 | if (hexlen > 0) { | |
139 | c.arg[0] |= SC_RAW; | |
140 | } | |
141 | ||
142 | memcpy(c.d.asBytes, data, hexlen ); | |
143 | clearCommandBuffer(); | |
144 | SendCommand(&c); | |
145 | ||
146 | // reading response from smart card | |
147 | if ( reply ) { | |
148 | UsbCommand resp; | |
149 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)) { | |
150 | PrintAndLog("smart card response failed"); | |
151 | return 1; | |
152 | } | |
153 | uint32_t datalen = resp.arg[0]; | |
154 | ||
155 | if ( !datalen ) { | |
156 | PrintAndLog("smart card response failed"); | |
157 | return 1; | |
158 | } | |
159 | ||
160 | PrintAndLog("received %i bytes", datalen); | |
161 | ||
162 | if (!datalen) | |
163 | return 1; | |
164 | ||
165 | uint8_t *data = resp.d.asBytes; | |
166 | ||
167 | // TLV decoder | |
168 | if (decodeTLV ) { | |
169 | ||
170 | if (datalen >= 2) { | |
171 | PrintAndLog("%02x %02x | %s", data[datalen - 2], data[datalen - 1], GetAPDUCodeDescription(data[datalen - 2], data[datalen - 1])); | |
172 | } | |
173 | if (datalen > 4) { | |
174 | TLVPrintFromBuffer(data, datalen - 2); | |
175 | } | |
176 | } else { | |
177 | PrintAndLog("%s", sprint_hex(data, datalen)); | |
178 | } | |
179 | } | |
180 | return 0; | |
181 | } | |
182 | ||
183 | int CmdSmartUpgrade(const char *Cmd) { | |
184 | ||
185 | PrintAndLog("WARNING - Smartcard socket firmware upgrade."); | |
186 | PrintAndLog("Dangerous command, do wrong and you will brick the smart card socket"); | |
187 | ||
188 | FILE *f; | |
189 | char filename[FILE_PATH_SIZE] = {0}; | |
190 | uint8_t cmdp = 0; | |
191 | bool errors = false; | |
192 | ||
193 | while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { | |
194 | switch (tolower(param_getchar(Cmd, cmdp))) { | |
195 | case 'f': | |
196 | //File handling and reading | |
197 | if ( param_getstr(Cmd, cmdp+1, filename, FILE_PATH_SIZE) >= FILE_PATH_SIZE ) { | |
198 | PrintAndLog("Filename too long"); | |
199 | errors = true; | |
200 | break; | |
201 | } | |
202 | cmdp += 2; | |
203 | break; | |
204 | case 'h': | |
205 | return usage_sm_upgrade(); | |
206 | default: | |
207 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
208 | errors = true; | |
209 | break; | |
210 | } | |
211 | } | |
212 | ||
213 | //Validations | |
214 | if (errors || cmdp == 0 ) return usage_sm_upgrade(); | |
215 | ||
216 | // load file | |
217 | f = fopen(filename, "rb"); | |
218 | if ( !f ) { | |
219 | PrintAndLog("File: %s: not found or locked.", filename); | |
220 | return 1; | |
221 | } | |
222 | ||
223 | // get filesize in order to malloc memory | |
224 | fseek(f, 0, SEEK_END); | |
225 | long fsize = ftell(f); | |
226 | fseek(f, 0, SEEK_SET); | |
227 | ||
228 | if (fsize < 0) { | |
229 | PrintAndLog("error, when getting filesize"); | |
230 | fclose(f); | |
231 | return 1; | |
232 | } | |
233 | ||
234 | uint8_t *dump = calloc(fsize, sizeof(uint8_t)); | |
235 | if (!dump) { | |
236 | PrintAndLog("error, cannot allocate memory "); | |
237 | fclose(f); | |
238 | return 1; | |
239 | } | |
240 | ||
241 | size_t bytes_read = fread(dump, 1, fsize, f); | |
242 | if (f) | |
243 | fclose(f); | |
244 | ||
245 | PrintAndLog("Smartcard socket firmware uploading to PM3"); | |
246 | //Send to device | |
247 | uint32_t index = 0; | |
248 | uint32_t bytes_sent = 0; | |
249 | uint32_t bytes_remaining = bytes_read; | |
250 | ||
251 | while (bytes_remaining > 0){ | |
252 | uint32_t bytes_in_packet = MIN(USB_CMD_DATA_SIZE, bytes_remaining); | |
253 | UsbCommand c = {CMD_SMART_UPLOAD, {index + bytes_sent, bytes_in_packet, 0}}; | |
254 | ||
255 | // Fill usb bytes with 0xFF | |
256 | memset(c.d.asBytes, 0xFF, USB_CMD_DATA_SIZE); | |
257 | memcpy(c.d.asBytes, dump + bytes_sent, bytes_in_packet); | |
258 | clearCommandBuffer(); | |
259 | SendCommand(&c); | |
260 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000) ) { | |
261 | PrintAndLog("timeout while waiting for reply."); | |
262 | free(dump); | |
263 | return 1; | |
264 | } | |
265 | ||
266 | bytes_remaining -= bytes_in_packet; | |
267 | bytes_sent += bytes_in_packet; | |
268 | printf("."); fflush(stdout); | |
269 | } | |
270 | free(dump); | |
271 | printf("\n"); | |
272 | PrintAndLog("Smartcard socket firmware updating, don\'t turn off your PM3!"); | |
273 | ||
274 | // trigger the firmware upgrade | |
275 | UsbCommand c = {CMD_SMART_UPGRADE, {bytes_read, 0, 0}}; | |
276 | clearCommandBuffer(); | |
277 | SendCommand(&c); | |
278 | UsbCommand resp; | |
279 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) { | |
280 | PrintAndLog("timeout while waiting for reply."); | |
281 | return 1; | |
282 | } | |
283 | if ( (resp.arg[0] && 0xFF ) ) | |
284 | PrintAndLog("Smartcard socket firmware upgraded successful"); | |
285 | else | |
286 | PrintAndLog("Smartcard socket firmware updating failed"); | |
287 | return 0; | |
288 | } | |
289 | ||
290 | int CmdSmartInfo(const char *Cmd){ | |
291 | uint8_t cmdp = 0; | |
292 | bool errors = false, silent = false; | |
293 | ||
294 | while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { | |
295 | switch (tolower(param_getchar(Cmd, cmdp))) { | |
296 | case 'h': return usage_sm_info(); | |
297 | case 's': | |
298 | silent = true; | |
299 | break; | |
300 | default: | |
301 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
302 | errors = true; | |
303 | break; | |
304 | } | |
305 | cmdp++; | |
306 | } | |
307 | ||
308 | //Validations | |
309 | if (errors ) return usage_sm_info(); | |
310 | ||
311 | UsbCommand c = {CMD_SMART_ATR, {0, 0, 0}}; | |
312 | clearCommandBuffer(); | |
313 | SendCommand(&c); | |
314 | UsbCommand resp; | |
315 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) { | |
316 | if (!silent) PrintAndLog("smart card select failed"); | |
317 | return 1; | |
318 | } | |
319 | ||
320 | uint8_t isok = resp.arg[0] & 0xFF; | |
321 | if (!isok) { | |
322 | if (!silent) PrintAndLog("smart card select failed"); | |
323 | return 1; | |
324 | } | |
325 | ||
326 | smart_card_atr_t card; | |
327 | memcpy(&card, (smart_card_atr_t *)resp.d.asBytes, sizeof(smart_card_atr_t)); | |
328 | ||
329 | // print header | |
330 | PrintAndLog("\n--- Smartcard Information ---------"); | |
331 | PrintAndLog("-------------------------------------------------------------"); | |
332 | PrintAndLog("ISO76183 ATR : %s", sprint_hex(card.atr, card.atr_len)); | |
333 | PrintAndLog("look up ATR"); | |
334 | PrintAndLog("http://smartcard-atr.appspot.com/parse?ATR=%s", sprint_hex_inrow(card.atr, card.atr_len) ); | |
335 | return 0; | |
336 | } | |
337 | ||
338 | int CmdSmartReader(const char *Cmd){ | |
339 | uint8_t cmdp = 0; | |
340 | bool errors = false, silent = false; | |
341 | ||
342 | while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { | |
343 | switch (tolower(param_getchar(Cmd, cmdp))) { | |
344 | case 'h': return usage_sm_reader(); | |
345 | case 's': | |
346 | silent = true; | |
347 | break; | |
348 | default: | |
349 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
350 | errors = true; | |
351 | break; | |
352 | } | |
353 | cmdp++; | |
354 | } | |
355 | ||
356 | //Validations | |
357 | if (errors ) return usage_sm_reader(); | |
358 | ||
359 | UsbCommand c = {CMD_SMART_ATR, {0, 0, 0}}; | |
360 | clearCommandBuffer(); | |
361 | SendCommand(&c); | |
362 | UsbCommand resp; | |
363 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) { | |
364 | if (!silent) PrintAndLog("smart card select failed"); | |
365 | return 1; | |
366 | } | |
367 | ||
368 | uint8_t isok = resp.arg[0] & 0xFF; | |
369 | if (!isok) { | |
370 | if (!silent) PrintAndLog("smart card select failed"); | |
371 | return 1; | |
372 | } | |
373 | smart_card_atr_t card; | |
374 | memcpy(&card, (smart_card_atr_t *)resp.d.asBytes, sizeof(smart_card_atr_t)); | |
375 | PrintAndLog("ISO7816-3 ATR : %s", sprint_hex(card.atr, card.atr_len)); | |
376 | return 0; | |
377 | } | |
378 | ||
379 | int CmdSmartSetClock(const char *Cmd){ | |
380 | uint8_t cmdp = 0; | |
381 | bool errors = false; | |
382 | uint8_t clock = 0; | |
383 | while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { | |
384 | switch (tolower(param_getchar(Cmd, cmdp))) { | |
385 | case 'h': return usage_sm_setclock(); | |
386 | case 'c': | |
387 | clock = param_get8ex(Cmd, cmdp+1, 2, 10); | |
388 | if ( clock > 2) | |
389 | errors = true; | |
390 | ||
391 | cmdp += 2; | |
392 | break; | |
393 | default: | |
394 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
395 | errors = true; | |
396 | break; | |
397 | } | |
398 | } | |
399 | ||
400 | //Validations | |
401 | if (errors || cmdp == 0) return usage_sm_setclock(); | |
402 | ||
403 | UsbCommand c = {CMD_SMART_SETCLOCK, {clock, 0, 0}}; | |
404 | clearCommandBuffer(); | |
405 | SendCommand(&c); | |
406 | UsbCommand resp; | |
407 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) { | |
408 | PrintAndLog("smart card select failed"); | |
409 | return 1; | |
410 | } | |
411 | ||
412 | uint8_t isok = resp.arg[0] & 0xFF; | |
413 | if (!isok) { | |
414 | PrintAndLog("smart card set clock failed"); | |
415 | return 1; | |
416 | } | |
417 | ||
418 | switch (clock) { | |
419 | case 0: | |
420 | PrintAndLog("Clock changed to 16mhz giving 10800 baudrate"); | |
421 | break; | |
422 | case 1: | |
423 | PrintAndLog("Clock changed to 8mhz giving 21600 baudrate"); | |
424 | break; | |
425 | case 2: | |
426 | PrintAndLog("Clock changed to 4mhz giving 86400 baudrate"); | |
427 | break; | |
428 | default: | |
429 | break; | |
430 | } | |
431 | return 0; | |
432 | } | |
433 | ||
434 | ||
435 | // iso 7816-3 | |
436 | void annotateIso7816(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize){ | |
437 | // S-block | |
438 | if ( (cmd[0] & 0xC0) && (cmdsize == 3) ) { | |
439 | switch ( (cmd[0] & 0x3f) ) { | |
440 | case 0x00 : snprintf(exp, size, "S-block RESYNCH req"); break; | |
441 | case 0x20 : snprintf(exp, size, "S-block RESYNCH resp"); break; | |
442 | case 0x01 : snprintf(exp, size, "S-block IFS req"); break; | |
443 | case 0x21 : snprintf(exp, size, "S-block IFS resp"); break; | |
444 | case 0x02 : snprintf(exp, size, "S-block ABORT req"); break; | |
445 | case 0x22 : snprintf(exp, size, "S-block ABORT resp"); break; | |
446 | case 0x03 : snprintf(exp, size, "S-block WTX reqt"); break; | |
447 | case 0x23 : snprintf(exp, size, "S-block WTX resp"); break; | |
448 | default : snprintf(exp, size, "S-block"); break; | |
449 | } | |
450 | } | |
451 | // R-block (ack) | |
452 | else if ( ((cmd[0] & 0xD0) == 0x80) && ( cmdsize > 2) ) { | |
453 | if ( (cmd[0] & 0x10) == 0 ) | |
454 | snprintf(exp, size, "R-block ACK"); | |
455 | else | |
456 | snprintf(exp, size, "R-block NACK"); | |
457 | } | |
458 | // I-block | |
459 | else { | |
460 | ||
461 | int pos = (cmd[0] == 2 || cmd[0] == 3) ? 2 : 3; | |
462 | switch ( cmd[pos] ) { | |
463 | case ISO7816_READ_BINARY :snprintf(exp, size, "READ BIN");break; | |
464 | case ISO7816_WRITE_BINARY :snprintf(exp, size, "WRITE BIN");break; | |
465 | case ISO7816_UPDATE_BINARY :snprintf(exp, size, "UPDATE BIN");break; | |
466 | case ISO7816_ERASE_BINARY :snprintf(exp, size, "ERASE BIN");break; | |
467 | case ISO7816_READ_RECORDS :snprintf(exp, size, "READ RECORDS");break; | |
468 | case ISO7816_WRITE_RECORDS :snprintf(exp, size, "WRITE RECORDS");break; | |
469 | case ISO7816_APPEND_RECORD :snprintf(exp, size, "APPEND RECORD");break; | |
470 | case ISO7816_UPDATE_RECORD :snprintf(exp, size, "UPDATE RECORD");break; | |
471 | case ISO7816_GET_DATA :snprintf(exp, size, "GET DATA");break; | |
472 | case ISO7816_PUT_DATA :snprintf(exp, size, "PUT DATA");break; | |
473 | case ISO7816_SELECT_FILE :snprintf(exp, size, "SELECT FILE");break; | |
474 | case ISO7816_VERIFY :snprintf(exp, size, "VERIFY");break; | |
475 | case ISO7816_INTERNAL_AUTHENTICATION :snprintf(exp, size, "INTERNAL AUTH");break; | |
476 | case ISO7816_EXTERNAL_AUTHENTICATION :snprintf(exp, size, "EXTERNAL AUTH");break; | |
477 | case ISO7816_GET_CHALLENGE :snprintf(exp, size, "GET CHALLENGE");break; | |
478 | case ISO7816_MANAGE_CHANNEL :snprintf(exp, size, "MANAGE CHANNEL");break; | |
479 | default :snprintf(exp, size, "?"); break; | |
480 | } | |
481 | } | |
482 | } | |
483 | ||
484 | ||
485 | uint16_t printScTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace) { | |
486 | // sanity check | |
487 | if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen; | |
488 | ||
489 | bool isResponse; | |
490 | uint16_t data_len, parity_len; | |
491 | uint32_t duration, timestamp, first_timestamp, EndOfTransmissionTimestamp; | |
492 | char explanation[30] = {0}; | |
493 | ||
494 | first_timestamp = *((uint32_t *)(trace)); | |
495 | timestamp = *((uint32_t *)(trace + tracepos)); | |
496 | tracepos += 4; | |
497 | ||
498 | duration = *((uint16_t *)(trace + tracepos)); | |
499 | tracepos += 2; | |
500 | ||
501 | data_len = *((uint16_t *)(trace + tracepos)); | |
502 | tracepos += 2; | |
503 | ||
504 | if (data_len & 0x8000) { | |
505 | data_len &= 0x7fff; | |
506 | isResponse = true; | |
507 | } else { | |
508 | isResponse = false; | |
509 | } | |
510 | ||
511 | parity_len = (data_len-1)/8 + 1; | |
512 | if (tracepos + data_len + parity_len > traceLen) { | |
513 | return traceLen; | |
514 | } | |
515 | uint8_t *frame = trace + tracepos; | |
516 | tracepos += data_len; | |
517 | //uint8_t *parityBytes = trace + tracepos; | |
518 | tracepos += parity_len; | |
519 | ||
520 | //--- Draw the data column | |
521 | char line[18][110]; | |
522 | ||
523 | if (data_len == 0 ) { | |
524 | sprintf(line[0],"<empty trace - possible error>"); | |
525 | return tracepos; | |
526 | } | |
527 | ||
528 | for (int j = 0; j < data_len && j/18 < 18; j++) { | |
529 | snprintf(line[j/18]+(( j % 18) * 4),110, "%02x ", frame[j]); | |
530 | } | |
531 | ||
532 | EndOfTransmissionTimestamp = timestamp + duration; | |
533 | ||
534 | annotateIso7816(explanation,sizeof(explanation),frame,data_len); | |
535 | ||
536 | int num_lines = MIN((data_len - 1)/18 + 1, 18); | |
537 | for (int j = 0; j < num_lines ; j++) { | |
538 | if (j == 0) { | |
539 | PrintAndLog(" %10u | %10u | %s |%-72s | %s| %s", | |
540 | (timestamp - first_timestamp), | |
541 | (EndOfTransmissionTimestamp - first_timestamp), | |
542 | (isResponse ? "Tag" : "Rdr"), | |
543 | line[j], | |
544 | " ", | |
545 | (j == num_lines-1) ? explanation : ""); | |
546 | } else { | |
547 | PrintAndLog(" | | |%-72s | %s| %s", | |
548 | line[j], | |
549 | " ", | |
550 | (j == num_lines-1) ? explanation : ""); | |
551 | } | |
552 | } | |
553 | ||
554 | // if is last record | |
555 | if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) >= traceLen) return traceLen; | |
556 | ||
557 | return tracepos; | |
558 | } | |
559 | ||
560 | int ScTraceList(const char *Cmd) { | |
561 | bool loadFromFile = false; | |
562 | bool saveToFile = false; | |
563 | char type[5] = {0}; | |
564 | char filename[FILE_PATH_SIZE] = {0}; | |
565 | ||
566 | // parse command line | |
567 | param_getstr(Cmd, 0, type, sizeof(type)); | |
568 | param_getstr(Cmd, 1, filename, sizeof(filename)); | |
569 | ||
570 | bool errors = false; | |
571 | if(type[0] == 'h') { | |
572 | errors = true; | |
573 | } | |
574 | ||
575 | if(!errors) { | |
576 | if (strcmp(type, "s") == 0) { | |
577 | saveToFile = true; | |
578 | } else if (strcmp(type,"l") == 0) { | |
579 | loadFromFile = true; | |
580 | } | |
581 | } | |
582 | ||
583 | if ((loadFromFile || saveToFile) && strlen(filename) == 0) { | |
584 | errors = true; | |
585 | } | |
586 | ||
587 | if (loadFromFile && saveToFile) { | |
588 | errors = true; | |
589 | } | |
590 | ||
591 | if (errors) { | |
592 | PrintAndLog("List or save protocol data."); | |
593 | PrintAndLog("Usage: sc list [l <filename>]"); | |
594 | PrintAndLog(" sc list [s <filename>]"); | |
595 | PrintAndLog(" l - load data from file instead of trace buffer"); | |
596 | PrintAndLog(" s - save data to file"); | |
597 | PrintAndLog(""); | |
598 | PrintAndLog("example: sc list"); | |
599 | PrintAndLog("example: sc list save myCardTrace.trc"); | |
600 | PrintAndLog("example: sc list l myCardTrace.trc"); | |
601 | return 0; | |
602 | } | |
603 | ||
604 | uint8_t *trace; | |
605 | uint32_t tracepos = 0; | |
606 | uint32_t traceLen = 0; | |
607 | ||
608 | if (loadFromFile) { | |
609 | #define TRACE_CHUNK_SIZE (1<<16) // 64K to start with. Will be enough for BigBuf and some room for future extensions | |
610 | FILE *tracefile = NULL; | |
611 | size_t bytes_read; | |
612 | trace = malloc(TRACE_CHUNK_SIZE); | |
613 | if (trace == NULL) { | |
614 | PrintAndLog("Cannot allocate memory for trace"); | |
615 | return 2; | |
616 | } | |
617 | if ((tracefile = fopen(filename,"rb")) == NULL) { | |
618 | PrintAndLog("Could not open file %s", filename); | |
619 | free(trace); | |
620 | return 0; | |
621 | } | |
622 | while (!feof(tracefile)) { | |
623 | bytes_read = fread(trace+traceLen, 1, TRACE_CHUNK_SIZE, tracefile); | |
624 | traceLen += bytes_read; | |
625 | if (!feof(tracefile)) { | |
626 | uint8_t *p = realloc(trace, traceLen + TRACE_CHUNK_SIZE); | |
627 | if (p == NULL) { | |
628 | PrintAndLog("Cannot allocate memory for trace"); | |
629 | free(trace); | |
630 | fclose(tracefile); | |
631 | return 2; | |
632 | } | |
633 | trace = p; | |
634 | } | |
635 | } | |
636 | fclose(tracefile); | |
637 | } else { | |
638 | trace = malloc(USB_CMD_DATA_SIZE); | |
639 | // Query for the size of the trace | |
640 | UsbCommand response; | |
641 | GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0, &response, -1, false); | |
642 | traceLen = response.arg[2]; | |
643 | if (traceLen > USB_CMD_DATA_SIZE) { | |
644 | uint8_t *p = realloc(trace, traceLen); | |
645 | if (p == NULL) { | |
646 | PrintAndLog("Cannot allocate memory for trace"); | |
647 | free(trace); | |
648 | return 2; | |
649 | } | |
650 | trace = p; | |
651 | GetFromBigBuf(trace, traceLen, 0, NULL, -1, false); | |
652 | } | |
653 | } | |
654 | ||
655 | if (saveToFile) { | |
656 | FILE *tracefile = NULL; | |
657 | if ((tracefile = fopen(filename,"wb")) == NULL) { | |
658 | PrintAndLog("Could not create file %s", filename); | |
659 | return 1; | |
660 | } | |
661 | fwrite(trace, 1, traceLen, tracefile); | |
662 | PrintAndLog("Recorded Activity (TraceLen = %d bytes) written to file %s", traceLen, filename); | |
663 | fclose(tracefile); | |
664 | } else { | |
665 | PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen); | |
666 | PrintAndLog(""); | |
667 | PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer"); | |
668 | PrintAndLog(""); | |
669 | PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |"); | |
670 | PrintAndLog("------------|------------|-----|-------------------------------------------------------------------------|-----|--------------------|"); | |
671 | ||
672 | while(tracepos < traceLen) | |
673 | { | |
674 | tracepos = printScTraceLine(tracepos, traceLen, trace); | |
675 | } | |
676 | } | |
677 | ||
678 | free(trace); | |
679 | return 0; | |
680 | } | |
681 | ||
682 | int CmdSmartList(const char *Cmd) { | |
683 | ScTraceList(Cmd); | |
684 | return 0; | |
685 | } | |
686 | ||
687 | static command_t CommandTable[] = { | |
688 | {"help", CmdHelp, 1, "This help"}, | |
689 | {"list", CmdSmartList, 0, "List ISO 7816 history"}, | |
690 | {"info", CmdSmartInfo, 1, "Tag information [rdv40]"}, | |
691 | {"reader", CmdSmartReader, 1, "Act like an IS07816 reader [rdv40]"}, | |
692 | {"raw", CmdSmartRaw, 1, "Send raw hex data to tag [rdv40]"}, | |
693 | {"upgrade", CmdSmartUpgrade, 1, "Upgrade firmware [rdv40]"}, | |
694 | {"setclock",CmdSmartSetClock, 1, "Set clock speed"}, | |
695 | {NULL, NULL, 0, NULL} | |
696 | }; | |
697 | ||
698 | int CmdSmartcard(const char *Cmd) { | |
699 | clearCommandBuffer(); | |
700 | CmdsParse(CommandTable, Cmd); | |
701 | return 0; | |
702 | } | |
703 | ||
704 | int CmdHelp(const char *Cmd) { | |
705 | CmdsHelp(CommandTable); | |
706 | return 0; | |
707 | } |