]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/cmdsmartcard.c
Fix some printf/scanf format strings
[proxmark3-svn] / client / cmdsmartcard.c
... / ...
CommitLineData
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
12#include <ctype.h>
13#include <string.h>
14
15#include "ui.h"
16#include "cmdparser.h"
17#include "proxmark3.h"
18#include "util.h"
19#include "smartcard.h"
20#include "comms.h"
21#include "protocols.h"
22#include "cmdhw.h"
23#include "cmdhflist.h"
24#include "emv/apduinfo.h" // APDUcode description
25#include "emv/emvcore.h" // decodeTVL
26#include "crypto/libpcrypto.h" // sha512hash
27#include "emv/dump.h" // dump_buffer
28#include "pcsc.h"
29
30#define SC_UPGRADE_FILES_DIRECTORY "sc_upgrade_firmware/"
31
32static bool UseAlternativeSmartcardReader = false; // default: use PM3 RDV40 Smartcard Slot (if available)
33
34static int CmdHelp(const char *Cmd);
35
36static int usage_sm_raw(void) {
37 PrintAndLogEx(NORMAL, "Usage: sc raw [h|r|c] d <0A 0B 0C ... hex>");
38 PrintAndLogEx(NORMAL, " h : this help");
39 PrintAndLogEx(NORMAL, " r : do not read response");
40 PrintAndLogEx(NORMAL, " a : active smartcard without select (reset sc module)");
41 PrintAndLogEx(NORMAL, " s : active smartcard with select (get ATR)");
42 PrintAndLogEx(NORMAL, " t : executes TLV decoder if it possible");
43 PrintAndLogEx(NORMAL, " 0 : use protocol T=0");
44 PrintAndLogEx(NORMAL, " d <bytes> : bytes to send");
45 PrintAndLogEx(NORMAL, "");
46 PrintAndLogEx(NORMAL, "Examples:");
47 PrintAndLogEx(NORMAL, " sc raw s 0 d 00a404000e315041592e5359532e4444463031 - `1PAY.SYS.DDF01` PSE directory with get ATR");
48 return 0;
49}
50
51static int usage_sm_select(void) {
52 PrintAndLogEx(NORMAL, "Usage: sc select [h|<reader name>] ");
53 PrintAndLogEx(NORMAL, " h : this help");
54 PrintAndLogEx(NORMAL, " <reader name> : a card reader's name, wildcards allowed, leave empty to pick from available readers");
55 PrintAndLogEx(NORMAL, "");
56 PrintAndLogEx(NORMAL, "Examples:");
57 PrintAndLogEx(NORMAL, " sc select : list available card readers and pick");
58 PrintAndLogEx(NORMAL, " sc select Gemalto* : select a connected Gemalto card reader" );
59 return 0;
60}
61
62static int usage_sm_reader(void) {
63 PrintAndLogEx(NORMAL, "Usage: sc reader [h|s]");
64 PrintAndLogEx(NORMAL, " h : this help");
65 PrintAndLogEx(NORMAL, " s : silent (no messages)");
66 PrintAndLogEx(NORMAL, "");
67 PrintAndLogEx(NORMAL, "Examples:");
68 PrintAndLogEx(NORMAL, " sc reader");
69 return 0;
70}
71
72static int usage_sm_info(void) {
73 PrintAndLogEx(NORMAL, "Usage: s info [h|s]");
74 PrintAndLogEx(NORMAL, " h : this help");
75 PrintAndLogEx(NORMAL, " s : silent (no messages)");
76 PrintAndLogEx(NORMAL, "");
77 PrintAndLogEx(NORMAL, "Examples:");
78 PrintAndLogEx(NORMAL, " sc info");
79 return 0;
80}
81
82static int usage_sm_upgrade(void) {
83 PrintAndLogEx(NORMAL, "Upgrade RDV4.0 Smartcard Socket Firmware");
84 PrintAndLogEx(NORMAL, "Usage: sc upgrade f <file name>");
85 PrintAndLogEx(NORMAL, " h : this help");
86 PrintAndLogEx(NORMAL, " f <filename> : firmware file name");
87 PrintAndLogEx(NORMAL, "");
88 PrintAndLogEx(NORMAL, "Examples:");
89 PrintAndLogEx(NORMAL, " sc upgrade f SIM010.BIN");
90 return 0;
91}
92
93static int usage_sm_setclock(void) {
94 PrintAndLogEx(NORMAL, "Usage: sc setclock [h] c <clockspeed>");
95 PrintAndLogEx(NORMAL, " h : this help");
96 PrintAndLogEx(NORMAL, " c <> : clockspeed (0 = 16mhz, 1=8mhz, 2=4mhz) ");
97 PrintAndLogEx(NORMAL, "");
98 PrintAndLogEx(NORMAL, "Examples:");
99 PrintAndLogEx(NORMAL, " sc setclock c 2");
100 return 0;
101}
102
103static int usage_sm_brute(void) {
104 PrintAndLogEx(NORMAL, "Tries to bruteforce SFI, ");
105 PrintAndLogEx(NORMAL, "Usage: sc brute [h]");
106 PrintAndLogEx(NORMAL, " h : this help");
107 PrintAndLogEx(NORMAL, "");
108 PrintAndLogEx(NORMAL, "Examples:");
109 PrintAndLogEx(NORMAL, " sc brute");
110 return 0;
111}
112
113uint8_t GetATRTA1(uint8_t *atr, size_t atrlen) {
114 if (atrlen > 2) {
115 uint8_t T0 = atr[1];
116 if (T0 & 0x10)
117 return atr[2];
118 }
119
120 return 0x11; // default value is 0x11, corresponding to fmax=5 MHz, Fi=372, Di=1.
121}
122
123int DiArray[] = {
124 0, // b0000 RFU
125 1, // b0001
126 2,
127 4,
128 8,
129 16,
130 32, // b0110
131 64, // b0111. This was RFU in ISO/IEC 7816-3:1997 and former. Some card readers or drivers may erroneously reject cards using this value
132 12,
133 20,
134 0, // b1010 RFU
135 0,
136 0, // ...
137 0,
138 0,
139 0 // b1111 RFU
140};
141
142int FiArray[] = {
143 372, // b0000 Historical note: in ISO/IEC 7816-3:1989, this was assigned to cards with internal clock
144 372, // b0001
145 558, // b0010
146 744, // b0011
147 1116, // b0100
148 1488, // b0101
149 1860, // b0110
150 0, // b0111 RFU
151 0, // b1000 RFU
152 512, // b1001
153 768, // b1010
154 1024, // b1011
155 1536, // b1100
156 2048, // b1101
157 0, // b1110 RFU
158 0 // b1111 RFU
159};
160
161float FArray[] = {
162 4, // b0000 Historical note: in ISO/IEC 7816-3:1989, this was assigned to cards with internal clock
163 5, // b0001
164 6, // b0010
165 8, // b0011
166 12, // b0100
167 16, // b0101
168 20, // b0110
169 0, // b0111 RFU
170 0, // b1000 RFU
171 5, // b1001
172 7.5, // b1010
173 10, // b1011
174 15, // b1100
175 20, // b1101
176 0, // b1110 RFU
177 0 // b1111 RFU
178};
179
180static int GetATRDi(uint8_t *atr, size_t atrlen) {
181 uint8_t TA1 = GetATRTA1(atr, atrlen);
182
183 return DiArray[TA1 & 0x0f]; // The 4 low-order bits of TA1 (4th MSbit to 1st LSbit) encode Di
184}
185
186static int GetATRFi(uint8_t *atr, size_t atrlen) {
187 uint8_t TA1 = GetATRTA1(atr, atrlen);
188
189 return FiArray[TA1 >> 4]; // The 4 high-order bits of TA1 (8th MSbit to 5th LSbit) encode fmax and Fi
190}
191
192static float GetATRF(uint8_t *atr, size_t atrlen) {
193 uint8_t TA1 = GetATRTA1(atr, atrlen);
194
195 return FArray[TA1 >> 4]; // The 4 high-order bits of TA1 (8th MSbit to 5th LSbit) encode fmax and Fi
196}
197
198static int PrintATR(uint8_t *atr, size_t atrlen) {
199
200 uint8_t T0 = atr[1];
201 uint8_t K = T0 & 0x0F;
202 uint8_t TD1 = 0, T1len = 0, TD1len = 0, TDilen = 0;
203 bool protocol_T0_present = true;
204 bool protocol_T15_present = false;
205
206 if (T0 & 0x10) {
207 PrintAndLog("\t- TA1 (Maximum clock frequency, proposed bit duration) [ 0x%02x ]", atr[2 + T1len]);
208 T1len++;
209 }
210
211 if (T0 & 0x20) {
212 PrintAndLog("\t- TB1 (Deprecated: VPP requirements) [ 0x%02x ]", atr[2 + T1len]);
213 T1len++;
214 }
215
216 if (T0 & 0x40) {
217 PrintAndLog("\t- TC1 (Extra delay between bytes required by card) [ 0x%02x ]", atr[2 + T1len]);
218 T1len++;
219 }
220
221 if (T0 & 0x80) {
222 TD1 = atr[2 + T1len];
223 PrintAndLog("\t- TD1 (First offered transmission protocol, presence of TA2..TD2) [ 0x%02x ] Protocol T%d", TD1, TD1 & 0x0f);
224 protocol_T0_present = false;
225 if ((TD1 & 0x0f) == 0) {
226 protocol_T0_present = true;
227 }
228 if ((TD1 & 0x0f) == 15) {
229 protocol_T15_present = true;
230 }
231
232 T1len++;
233
234 if (TD1 & 0x10) {
235 PrintAndLog("\t- TA2 (Specific protocol and parameters to be used after the ATR) [ 0x%02x ]", atr[2 + T1len + TD1len]);
236 TD1len++;
237 }
238 if (TD1 & 0x20) {
239 PrintAndLog("\t- TB2 (Deprecated: VPP precise voltage requirement) [ 0x%02x ]", atr[2 + T1len + TD1len]);
240 TD1len++;
241 }
242 if (TD1 & 0x40) {
243 PrintAndLog("\t- TC2 (Maximum waiting time for protocol T=0) [ 0x%02x ]", atr[2 + T1len + TD1len]);
244 TD1len++;
245 }
246 if (TD1 & 0x80) {
247 uint8_t TDi = atr[2 + T1len + TD1len];
248 PrintAndLog("\t- TD2 (A supported protocol or more global parameters, presence of TA3..TD3) [ 0x%02x ] Protocol T%d", TDi, TDi & 0x0f);
249 if ((TDi & 0x0f) == 0) {
250 protocol_T0_present = true;
251 }
252 if ((TDi & 0x0f) == 15) {
253 protocol_T15_present = true;
254 }
255 TD1len++;
256
257 bool nextCycle = true;
258 uint8_t vi = 3;
259 while (nextCycle) {
260 nextCycle = false;
261 if (TDi & 0x10) {
262 PrintAndLog("\t- TA%d: 0x%02x", vi, atr[2 + T1len + TD1len + TDilen]);
263 TDilen++;
264 }
265 if (TDi & 0x20) {
266 PrintAndLog("\t- TB%d: 0x%02x", vi, atr[2 + T1len + TD1len + TDilen]);
267 TDilen++;
268 }
269 if (TDi & 0x40) {
270 PrintAndLog("\t- TC%d: 0x%02x", vi, atr[2 + T1len + TD1len + TDilen]);
271 TDilen++;
272 }
273 if (TDi & 0x80) {
274 TDi = atr[2 + T1len + TD1len + TDilen];
275 PrintAndLog("\t- TD%d [ 0x%02x ] Protocol T%d", vi, TDi, TDi & 0x0f);
276 TDilen++;
277
278 nextCycle = true;
279 vi++;
280 }
281 }
282 }
283 }
284
285 if (!protocol_T0_present || protocol_T15_present) { // there is CRC Check Byte TCK
286 uint8_t vxor = 0;
287 for (int i = 1; i < atrlen; i++)
288 vxor ^= atr[i];
289
290 if (vxor)
291 PrintAndLogEx(WARNING, "Check sum error. Must be 0 got 0x%02X", vxor);
292 else
293 PrintAndLogEx(INFO, "Check sum OK.");
294 }
295
296 if (atr[0] != 0x3b)
297 PrintAndLogEx(WARNING, "Not a direct convention [ 0x%02x ]", atr[0]);
298
299 uint8_t calen = 2 + T1len + TD1len + TDilen + K;
300
301 if (atrlen != calen && atrlen != calen + 1) // may be CRC
302 PrintAndLogEx(ERR, "ATR length error. len: %d, T1len: %d, TD1len: %d, TDilen: %d, K: %d", atrlen, T1len, TD1len, TDilen, K);
303
304 if (K > 0)
305 PrintAndLogEx(INFO, "\nHistorical bytes | len %02d | format %02x", K, atr[2 + T1len + TD1len + TDilen]);
306
307 if (K > 1) {
308 PrintAndLogEx(INFO, "\tHistorical bytes");
309 dump_buffer(&atr[2 + T1len + TD1len + TDilen], K, NULL, 1);
310 }
311
312 return 0;
313}
314
315bool smart_getATR(smart_card_atr_t *card)
316{
317 if (UseAlternativeSmartcardReader) {
318 return pcscGetATR(card);
319 } else {
320 UsbCommand c = {CMD_SMART_ATR, {0, 0, 0}};
321 SendCommand(&c);
322
323 UsbCommand resp;
324 if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) {
325 return false;
326 }
327
328 if (resp.arg[0] & 0xff) {
329 return resp.arg[0] & 0xFF;
330 }
331
332 memcpy(card, (smart_card_atr_t *)resp.d.asBytes, sizeof(smart_card_atr_t));
333
334 return true;
335 }
336}
337
338static bool smart_select(bool silent) {
339
340 smart_card_atr_t card;
341 if (!smart_getATR(&card)) {
342 if (!silent) PrintAndLogEx(WARNING, "smart card select failed");
343 return false;
344 }
345
346 if (!silent) {
347 PrintAndLogEx(INFO, "ISO7816-3 ATR : %s", sprint_hex(card.atr, card.atr_len));
348 }
349
350 return true;
351}
352
353
354static void smart_transmit(uint8_t *data, uint32_t data_len, uint32_t flags, uint8_t *response, int *response_len, uint32_t max_response_len)
355{
356 // PrintAndLogEx(SUCCESS, "C-TPDU>>>> %s", sprint_hex(data, data_len));
357 if (UseAlternativeSmartcardReader) {
358 *response_len = max_response_len;
359 pcscTransmit(data, data_len, flags, response, response_len);
360 } else {
361 UsbCommand c = {CMD_SMART_RAW, {flags, data_len, 0}};
362 memcpy(c.d.asBytes, data, data_len);
363 SendCommand(&c);
364
365 if (!WaitForResponseTimeout(CMD_ACK, &c, 2500)) {
366 PrintAndLogEx(WARNING, "smart card response timeout");
367 *response_len = -1;
368 return;
369 }
370
371 *response_len = c.arg[0];
372 if (*response_len > 0) {
373 memcpy(response, c.d.asBytes, *response_len);
374 }
375 }
376
377 if (*response_len <= 0) {
378 PrintAndLogEx(WARNING, "smart card response failed");
379 *response_len = -2;
380 return;
381 }
382
383 if (*response_len < 2) {
384 // PrintAndLogEx(SUCCESS, "R-TPDU %02X | ", response[0]);
385 return;
386 }
387
388 // PrintAndLogEx(SUCCESS, "R-TPDU<<<< %s", sprint_hex(response, *response_len));
389 // PrintAndLogEx(SUCCESS, "R-TPDU SW %02X%02X | %s", response[*response_len-2], response[*response_len-1], GetAPDUCodeDescription(response[*response_len-2], response[*response_len-1]));
390}
391
392
393static int CmdSmartSelect(const char *Cmd)
394{
395 const char *readername;
396
397 if (tolower(param_getchar(Cmd, 0)) == 'h') {
398 return usage_sm_select();
399 }
400
401 if (!PM3hasSmartcardSlot() && !pcscCheckForCardReaders()) {
402 PrintAndLogEx(WARNING, "No Smartcard Readers available");
403 UseAlternativeSmartcardReader = false;
404 return 1;
405 }
406
407 int bg, en;
408 if (param_getptr(Cmd, &bg, &en, 0)) {
409 UseAlternativeSmartcardReader = pcscSelectAlternativeCardReader(NULL);
410 } else {
411 readername = Cmd + bg;
412 UseAlternativeSmartcardReader = pcscSelectAlternativeCardReader(readername);
413 }
414
415 return 0;
416}
417
418
419static int CmdSmartRaw(const char *Cmd) {
420
421 int hexlen = 0;
422 bool active = false;
423 bool active_select = false;
424 bool useT0 = false;
425 uint8_t cmdp = 0;
426 bool errors = false, reply = true, decodeTLV = false, breakloop = false;
427 uint8_t data[ISO7816_MAX_FRAME_SIZE] = {0x00};
428
429 while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
430 switch (tolower(param_getchar(Cmd, cmdp))) {
431 case 'h': return usage_sm_raw();
432 case 'r':
433 reply = false;
434 cmdp++;
435 break;
436 case 'a':
437 active = true;
438 cmdp++;
439 break;
440 case 's':
441 active_select = true;
442 cmdp++;
443 break;
444 case 't':
445 decodeTLV = true;
446 cmdp++;
447 break;
448 case '0':
449 useT0 = true;
450 cmdp++;
451 break;
452 case 'd': {
453 switch (param_gethex_to_eol(Cmd, cmdp+1, data, sizeof(data), &hexlen)) {
454 case 1:
455 PrintAndLogEx(WARNING, "Invalid HEX value.");
456 return 1;
457 case 2:
458 PrintAndLogEx(WARNING, "Too many bytes. Max %d bytes", sizeof(data));
459 return 1;
460 case 3:
461 PrintAndLogEx(WARNING, "Hex must have even number of digits.");
462 return 1;
463 }
464 cmdp++;
465 breakloop = true;
466 break;
467 }
468 default:
469 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
470 errors = true;
471 break;
472 }
473
474 if ( breakloop )
475 break;
476 }
477
478 //Validations
479 if (errors || cmdp == 0 ) return usage_sm_raw();
480
481 uint32_t flags = 0;
482 uint32_t protocol = 0;
483 if (active || active_select) {
484 flags |= SC_CONNECT;
485 if (active_select)
486 flags |= SC_SELECT;
487 }
488 if (hexlen > 0) {
489 if (useT0)
490 protocol = SC_RAW_T0;
491 else
492 protocol = SC_RAW;
493 }
494
495 int response_len = 0;
496 uint8_t *response = NULL;
497 if (reply) {
498 response = calloc(ISO7816_MAX_FRAME_SIZE, sizeof(uint8_t));
499 if ( !response )
500 return 1;
501 }
502
503 smart_transmit(data, hexlen, flags|protocol, response, &response_len, ISO7816_MAX_FRAME_SIZE);
504
505 // reading response from smart card
506 if ( reply ) {
507 if ( response_len < 0 ) {
508 free(response);
509 return 2;
510 }
511
512 if ( response[0] == 0x6C ) {
513 data[4] = response[1];
514 smart_transmit(data, hexlen, protocol, response, &response_len, ISO7816_MAX_FRAME_SIZE);
515 data[4] = 0;
516 }
517
518 if (decodeTLV && response_len > 4)
519 TLVPrintFromBuffer(response, response_len-2);
520
521 free(response);
522 }
523 return 0;
524}
525
526
527int ExchangeAPDUSC(uint8_t *APDU, int APDUlen, bool activateCard, bool leaveSignalON, uint8_t *response, int maxresponselen, int *responselen)
528{
529 uint8_t TPDU[ISO7816_MAX_FRAME_SIZE];
530
531 *responselen = 0;
532
533 if (activateCard)
534 smart_select(false);
535
536 uint32_t flags = SC_RAW_T0;
537 if (activateCard) {
538 flags |= SC_SELECT | SC_CONNECT;
539 }
540
541 if (APDUlen == 4) { // Case 1
542 memcpy(TPDU, APDU, 4);
543 TPDU[4] = 0x00;
544 smart_transmit(TPDU, 5, flags, response, responselen, maxresponselen);
545 } else if (APDUlen == 5) { // Case 2 Short
546 smart_transmit(APDU, 5, flags, response, responselen, maxresponselen);
547 if (response[0] == 0x6C) { // wrong Le
548 uint16_t Le = APDU[4] ? APDU[4] : 256;
549 uint8_t La = response[1];
550 memcpy(TPDU, APDU, 5);
551 TPDU[4] = La;
552 smart_transmit(TPDU, 5, SC_RAW_T0, response, responselen, maxresponselen);
553 if (Le < La && *responselen >= 0) {
554 response[Le] = response[*responselen-2];
555 response[Le+1] = response[*responselen-1];
556 *responselen = Le + 2;
557 }
558 }
559 } else if (APDU[4] != 0 && APDUlen == 5 + APDU[4]) { // Case 3 Short
560 smart_transmit(APDU, APDUlen, flags, response, responselen, maxresponselen);
561 } else if (APDU[4] != 0 && APDUlen == 5 + APDU[4] + 1) { // Case 4 Short
562 smart_transmit(APDU, APDUlen-1, flags, response, responselen, maxresponselen);
563 if (response[0] == 0x90 && response[1] == 0x00) {
564 uint8_t Le = APDU[APDUlen-1];
565 uint8_t get_response[5] = {0x00, ISO7816_GET_RESPONSE, 0x00, 0x00, Le};
566 return ExchangeAPDUSC(get_response, 5, false, leaveSignalON, response, maxresponselen, responselen);
567 }
568 } else { // Long Cases not yet implemented
569 PrintAndLogEx(ERR, "Long APDUs not yet implemented");
570 *responselen = -3;
571 }
572
573 if (*responselen < 0 ) {
574 return 1;
575 } else {
576 return 0;
577 }
578}
579
580
581static int CmdSmartUpgrade(const char *Cmd) {
582
583 PrintAndLogEx(NORMAL, "");
584 PrintAndLogEx(WARNING, "WARNING - RDV4.0 Smartcard Socket Firmware upgrade.");
585 PrintAndLogEx(WARNING, "A dangerous command, do wrong and you will brick the smart card socket");
586 PrintAndLogEx(NORMAL, "");
587
588 FILE *f;
589 char filename[FILE_PATH_SIZE] = {0};
590 uint8_t cmdp = 0;
591 bool errors = false;
592
593 while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
594 switch (tolower(param_getchar(Cmd, cmdp))) {
595 case 'f':
596 //File handling and reading
597 if ( param_getstr(Cmd, cmdp+1, filename, FILE_PATH_SIZE) >= FILE_PATH_SIZE ) {
598 PrintAndLogEx(FAILED, "Filename too long");
599 errors = true;
600 break;
601 }
602 cmdp += 2;
603 break;
604 case 'h':
605 return usage_sm_upgrade();
606 default:
607 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
608 errors = true;
609 break;
610 }
611 }
612
613 //Validations
614 if (errors || cmdp == 0 ) return usage_sm_upgrade();
615
616 if (strchr(filename, '\\') || strchr(filename, '/')) {
617 PrintAndLogEx(FAILED, "Filename must not contain \\ or /. Firmware file will be found in client/sc_upgrade_firmware directory.");
618 return 1;
619 }
620
621 char sc_upgrade_file_path[strlen(get_my_executable_directory()) + strlen(SC_UPGRADE_FILES_DIRECTORY) + strlen(filename) + 1];
622 strcpy(sc_upgrade_file_path, get_my_executable_directory());
623 strcat(sc_upgrade_file_path, SC_UPGRADE_FILES_DIRECTORY);
624 strcat(sc_upgrade_file_path, filename);
625 if (strlen(sc_upgrade_file_path) >= FILE_PATH_SIZE ) {
626 PrintAndLogEx(FAILED, "Filename too long");
627 return 1;
628 }
629
630 char sha512filename[FILE_PATH_SIZE] = {'\0'};
631 char *bin_extension = filename;
632 char *dot_position = NULL;
633 while ((dot_position = strchr(bin_extension, '.')) != NULL) {
634 bin_extension = dot_position + 1;
635 }
636 if (!strcmp(bin_extension, "BIN")
637#ifdef _WIN32
638 || !strcmp(bin_extension, "bin")
639#endif
640 ) {
641 memcpy(sha512filename, filename, strlen(filename) - strlen("bin"));
642 strcat(sha512filename, "sha512.txt");
643 } else {
644 PrintAndLogEx(FAILED, "Filename extension of Firmware Upgrade File must be .BIN");
645 return 1;
646 }
647
648 PrintAndLogEx(INFO, "Checking integrity using SHA512 File %s ...", sha512filename);
649 char sc_upgrade_sha512file_path[strlen(get_my_executable_directory()) + strlen(SC_UPGRADE_FILES_DIRECTORY) + strlen(sha512filename) + 1];
650 strcpy(sc_upgrade_sha512file_path, get_my_executable_directory());
651 strcat(sc_upgrade_sha512file_path, SC_UPGRADE_FILES_DIRECTORY);
652 strcat(sc_upgrade_sha512file_path, sha512filename);
653 if (strlen(sc_upgrade_sha512file_path) >= FILE_PATH_SIZE ) {
654 PrintAndLogEx(FAILED, "Filename too long");
655 return 1;
656 }
657
658 // load firmware file
659 f = fopen(sc_upgrade_file_path, "rb");
660 if ( !f ){
661 PrintAndLogEx(FAILED, "Firmware file not found or locked.");
662 return 1;
663 }
664
665 // get filesize in order to malloc memory
666 fseek(f, 0, SEEK_END);
667 size_t fsize = ftell(f);
668 fseek(f, 0, SEEK_SET);
669
670 if (fsize < 0) {
671 PrintAndLogEx(FAILED, "Could not determine size of firmware file");
672 fclose(f);
673 return 1;
674 }
675
676 uint8_t *dump = calloc(fsize, sizeof(uint8_t));
677 if (!dump) {
678 PrintAndLogEx(FAILED, "Could not allocate memory for firmware");
679 fclose(f);
680 return 1;
681 }
682
683 size_t firmware_size = fread(dump, 1, fsize, f);
684 if (f)
685 fclose(f);
686
687 // load sha512 file
688 f = fopen(sc_upgrade_sha512file_path, "rb");
689 if ( !f ){
690 PrintAndLogEx(FAILED, "SHA-512 file not found or locked.");
691 return 1;
692 }
693
694 // get filesize in order to malloc memory
695 fseek(f, 0, SEEK_END);
696 fsize = ftell(f);
697 fseek(f, 0, SEEK_SET);
698
699 if (fsize < 0) {
700 PrintAndLogEx(FAILED, "Could not determine size of SHA-512 file");
701 fclose(f);
702 return 1;
703 }
704
705 if (fsize < 128) {
706 PrintAndLogEx(FAILED, "SHA-512 file too short");
707 fclose(f);
708 return 1;
709 }
710
711 char hashstring[129];
712 size_t bytes_read = fread(hashstring, 1, 128, f);
713 hashstring[128] = '\0';
714
715 if (f)
716 fclose(f);
717
718 uint8_t hash1[64];
719 if (bytes_read != 128 || param_gethex(hashstring, 0, hash1, 128)) {
720 PrintAndLogEx(FAILED, "Couldn't read SHA-512 file");
721 return 1;
722 }
723
724 uint8_t hash2[64];
725 if (sha512hash(dump, firmware_size, hash2)) {
726 PrintAndLogEx(FAILED, "Couldn't calculate SHA-512 of Firmware");
727 return 1;
728 }
729
730 if (memcmp(hash1, hash2, 64)) {
731 PrintAndLogEx(FAILED, "Couldn't verify integrity of Firmware file (wrong SHA-512)");
732 return 1;
733 }
734
735 PrintAndLogEx(SUCCESS, "RDV4.0 Smartcard Socket Firmware uploading to PM3");
736
737 //Send to device
738 uint32_t index = 0;
739 uint32_t bytes_sent = 0;
740 uint32_t bytes_remaining = firmware_size;
741
742 while (bytes_remaining > 0){
743 uint32_t bytes_in_packet = MIN(USB_CMD_DATA_SIZE, bytes_remaining);
744 UsbCommand c = {CMD_SMART_UPLOAD, {index + bytes_sent, bytes_in_packet, 0}};
745
746 // Fill usb bytes with 0xFF
747 memset(c.d.asBytes, 0xFF, USB_CMD_DATA_SIZE);
748 memcpy(c.d.asBytes, dump + bytes_sent, bytes_in_packet);
749 clearCommandBuffer();
750 SendCommand(&c);
751 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000) ) {
752 PrintAndLogEx(WARNING, "timeout while waiting for reply.");
753 free(dump);
754 return 1;
755 }
756
757 bytes_remaining -= bytes_in_packet;
758 bytes_sent += bytes_in_packet;
759 printf("."); fflush(stdout);
760 }
761 free(dump);
762 printf("\n");
763 PrintAndLogEx(SUCCESS, "RDV4.0 Smartcard Socket Firmware updating, don\'t turn off your PM3!");
764
765 // trigger the firmware upgrade
766 UsbCommand c = {CMD_SMART_UPGRADE, {firmware_size, 0, 0}};
767 clearCommandBuffer();
768 SendCommand(&c);
769 UsbCommand resp;
770 if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) {
771 PrintAndLogEx(WARNING, "timeout while waiting for reply.");
772 return 1;
773 }
774 if ( (resp.arg[0] & 0xFF ) )
775 PrintAndLogEx(SUCCESS, "RDV4.0 Smartcard Socket Firmware upgraded successful");
776 else
777 PrintAndLogEx(FAILED, "RDV4.0 Smartcard Socket Firmware Upgrade failed");
778 return 0;
779}
780
781
782static int CmdSmartInfo(const char *Cmd){
783 uint8_t cmdp = 0;
784 bool errors = false, silent = false;
785
786 while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
787 switch (tolower(param_getchar(Cmd, cmdp))) {
788 case 'h': return usage_sm_info();
789 case 's':
790 silent = true;
791 break;
792 default:
793 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
794 errors = true;
795 break;
796 }
797 cmdp++;
798 }
799
800 //Validations
801 if (errors ) return usage_sm_info();
802
803 smart_card_atr_t card;
804 if (!smart_getATR(&card)) {
805 if (!silent) PrintAndLogEx(WARNING, "smart card select failed");
806 return 1;
807 }
808
809 if (!card.atr_len) {
810 if (!silent) PrintAndLogEx(ERR, "can't get ATR from a smart card");
811 return 1;
812 }
813
814 // print header
815 PrintAndLogEx(INFO, "--- Smartcard Information ---------");
816 PrintAndLogEx(INFO, "-------------------------------------------------------------");
817 PrintAndLogEx(INFO, "ISO7618-3 ATR : %s", sprint_hex(card.atr, card.atr_len));
818 PrintAndLogEx(INFO, "\nhttp://smartcard-atr.appspot.com/parse?ATR=%s", sprint_hex_inrow(card.atr, card.atr_len) );
819
820 // print ATR
821 PrintAndLogEx(NORMAL, "");
822 PrintAndLogEx(INFO, "ATR");
823 PrintATR(card.atr, card.atr_len);
824
825 // print D/F (brom byte TA1 or defaults)
826 PrintAndLogEx(NORMAL, "");
827 PrintAndLogEx(INFO, "D/F (TA1)");
828 int Di = GetATRDi(card.atr, card.atr_len);
829 int Fi = GetATRFi(card.atr, card.atr_len);
830 float F = GetATRF(card.atr, card.atr_len);
831 if (GetATRTA1(card.atr, card.atr_len) == 0x11)
832 PrintAndLogEx(INFO, "Using default values...");
833
834 PrintAndLogEx(NORMAL, "\t- Di=%d", Di);
835 PrintAndLogEx(NORMAL, "\t- Fi=%d", Fi);
836 PrintAndLogEx(NORMAL, "\t- F=%.1f MHz", F);
837
838 if (Di && Fi) {
839 PrintAndLogEx(NORMAL, "\t- Cycles/ETU=%d", Fi/Di);
840 PrintAndLogEx(NORMAL, "\t- %.1f bits/sec at 4MHz", (float)4000000 / (Fi/Di));
841 PrintAndLogEx(NORMAL, "\t- %.1f bits/sec at Fmax=%.1fMHz", (F * 1000000) / (Fi/Di), F);
842 } else {
843 PrintAndLogEx(WARNING, "\t- Di or Fi is RFU.");
844 };
845
846 return 0;
847}
848
849int CmdSmartReader(const char *Cmd){
850 uint8_t cmdp = 0;
851 bool errors = false, silent = false;
852
853 while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
854 switch (tolower(param_getchar(Cmd, cmdp))) {
855 case 'h': return usage_sm_reader();
856 case 's':
857 silent = true;
858 break;
859 default:
860 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
861 errors = true;
862 break;
863 }
864 cmdp++;
865 }
866
867 //Validations
868 if (errors ) return usage_sm_reader();
869
870 smart_card_atr_t card;
871 if (!smart_getATR(&card)) {
872 if (!silent) PrintAndLogEx(WARNING, "smart card select failed");
873 return 1;
874 }
875
876 PrintAndLogEx(INFO, "ISO7816-3 ATR : %s", sprint_hex(card.atr, card.atr_len));
877 return 0;
878}
879
880
881static int CmdSmartSetClock(const char *Cmd){
882 uint8_t cmdp = 0;
883 bool errors = false;
884 uint8_t clock = 0;
885 while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
886 switch (tolower(param_getchar(Cmd, cmdp))) {
887 case 'h': return usage_sm_setclock();
888 case 'c':
889 clock = param_get8ex(Cmd, cmdp+1, 2, 10);
890 if ( clock > 2)
891 errors = true;
892
893 cmdp += 2;
894 break;
895 default:
896 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
897 errors = true;
898 break;
899 }
900 }
901
902 //Validations
903 if (errors || cmdp == 0) return usage_sm_setclock();
904
905 UsbCommand c = {CMD_SMART_SETCLOCK, {clock, 0, 0}};
906 clearCommandBuffer();
907 SendCommand(&c);
908 UsbCommand resp;
909 if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) {
910 PrintAndLogEx(WARNING, "smart card select failed");
911 return 1;
912 }
913
914 uint8_t isok = resp.arg[0] & 0xFF;
915 if (!isok) {
916 PrintAndLogEx(WARNING, "smart card set clock failed");
917 return 1;
918 }
919
920 switch (clock) {
921 case 0:
922 PrintAndLogEx(SUCCESS, "Clock changed to 16mhz giving 10800 baudrate");
923 break;
924 case 1:
925 PrintAndLogEx(SUCCESS, "Clock changed to 8mhz giving 21600 baudrate");
926 break;
927 case 2:
928 PrintAndLogEx(SUCCESS, "Clock changed to 4mhz giving 86400 baudrate");
929 break;
930 default:
931 break;
932 }
933 return 0;
934}
935
936
937static int CmdSmartList(const char *Cmd) {
938 if (UseAlternativeSmartcardReader) {
939 CmdHFList("7816 p");
940 } else {
941 CmdHFList("7816");
942 }
943 return 0;
944}
945
946
947static int CmdSmartBruteforceSFI(const char *Cmd) {
948
949 char ctmp = tolower(param_getchar(Cmd, 0));
950 if (ctmp == 'h') return usage_sm_brute();
951
952 uint8_t data[5] = {0x00, 0xB2, 0x00, 0x00, 0x00};
953
954 PrintAndLogEx(INFO, "Selecting card");
955 if ( !smart_select(false) ) {
956 return 1;
957 }
958
959 PrintAndLogEx(INFO, "Selecting PSE aid");
960 CmdSmartRaw("s 0 t d 00a404000e325041592e5359532e4444463031");
961 CmdSmartRaw("0 t d 00a4040007a000000004101000"); // mastercard
962// CmdSmartRaw("0 t d 00a4040007a0000000031010"); // visa
963
964 PrintAndLogEx(INFO, "starting");
965
966 int response_len = 0;
967 uint8_t* response = malloc(ISO7816_MAX_FRAME_SIZE);
968 if (!response)
969 return 1;
970
971 for (uint8_t i=1; i < 4; i++) {
972 for (int p1=1; p1 < 5; p1++) {
973
974 data[2] = p1;
975 data[3] = (i << 3) + 4;
976
977 smart_transmit(data, sizeof(data), SC_RAW_T0, response, &response_len, ISO7816_MAX_FRAME_SIZE);
978
979 if ( response[0] == 0x6C ) {
980 data[4] = response[1];
981 smart_transmit(data, sizeof(data), SC_RAW_T0, response, &response_len, ISO7816_MAX_FRAME_SIZE);
982
983 // TLV decoder
984 if (response_len > 4)
985 TLVPrintFromBuffer(response+1, response_len-3);
986
987 data[4] = 0;
988 }
989 memset(response, 0x00, ISO7816_MAX_FRAME_SIZE);
990 }
991 }
992 free(response);
993 return 0;
994}
995
996static command_t CommandTable[] = {
997 {"help", CmdHelp, 1, "This help"},
998 {"select", CmdSmartSelect, 1, "Select the Smartcard Reader to use"},
999 {"list", CmdSmartList, 1, "List ISO 7816 history"},
1000 {"info", CmdSmartInfo, 1, "Tag information"},
1001 {"reader", CmdSmartReader, 1, "Act like an IS07816 reader"},
1002 {"raw", CmdSmartRaw, 1, "Send raw hex data to tag"},
1003 {"upgrade", CmdSmartUpgrade, 0, "Upgrade firmware"},
1004 {"setclock", CmdSmartSetClock, 1, "Set clock speed"},
1005 {"brute", CmdSmartBruteforceSFI, 1, "Bruteforce SFI"},
1006 {NULL, NULL, 0, NULL}
1007};
1008
1009
1010int CmdSmartcard(const char *Cmd) {
1011 clearCommandBuffer();
1012 CmdsParse(CommandTable, Cmd);
1013 return 0;
1014}
1015
1016
1017static int CmdHelp(const char *Cmd) {
1018 CmdsHelp(CommandTable);
1019 return 0;
1020}
Impressum, Datenschutz