]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdsmartcard.c
Merge branch 'master' into fix_iso15693_fpga
[proxmark3-svn] / client / cmdsmartcard.c
CommitLineData
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"
8d7d7b61 11
12#include <ctype.h>
b8ed9975 13#include <string.h>
8d7d7b61 14
15#include "ui.h"
16#include "cmdparser.h"
9f596ec7 17#include "proxmark3.h"
8d7d7b61 18#include "util.h"
43591e64 19#include "smartcard.h"
20#include "comms.h"
21#include "protocols.h"
a9104f7e 22#include "cmdhw.h"
0d2624a0 23#include "cmdhflist.h"
8d7d7b61 24#include "emv/apduinfo.h" // APDUcode description
25#include "emv/emvcore.h" // decodeTVL
151a33c0 26#include "crypto/libpcrypto.h" // sha512hash
6b6c3be6 27#include "emv/dump.h" // dump_buffer
a9104f7e 28#include "pcsc.h"
43591e64 29
9f596ec7 30#define SC_UPGRADE_FILES_DIRECTORY "sc_upgrade_firmware/"
43591e64 31
a9104f7e 32static bool UseAlternativeSmartcardReader = false; // default: use PM3 RDV40 Smartcard Slot (if available)
33
43591e64 34static int CmdHelp(const char *Cmd);
35
8d7d7b61 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");
151a33c0 40 PrintAndLogEx(NORMAL, " a : active smartcard without select (reset sc module)");
41 PrintAndLogEx(NORMAL, " s : active smartcard with select (get ATR)");
8d7d7b61 42 PrintAndLogEx(NORMAL, " t : executes TLV decoder if it possible");
151a33c0 43 PrintAndLogEx(NORMAL, " 0 : use protocol T=0");
8d7d7b61 44 PrintAndLogEx(NORMAL, " d <bytes> : bytes to send");
45 PrintAndLogEx(NORMAL, "");
46 PrintAndLogEx(NORMAL, "Examples:");
6b5105be 47 PrintAndLogEx(NORMAL, " sc raw s 0 d 00a404000e315041592e5359532e4444463031 - `1PAY.SYS.DDF01` PSE directory with get ATR");
43591e64 48 return 0;
49}
8d7d7b61 50
a9104f7e 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
8d7d7b61 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");
43591e64 69 return 0;
70}
8d7d7b61 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");
43591e64 79 return 0;
80}
8d7d7b61 81
82static int usage_sm_upgrade(void) {
9f596ec7 83 PrintAndLogEx(NORMAL, "Upgrade RDV4.0 Smartcard Socket Firmware");
8d7d7b61 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:");
9f596ec7 89 PrintAndLogEx(NORMAL, " sc upgrade f SIM010.BIN");
43591e64 90 return 0;
91}
8d7d7b61 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");
43591e64 100 return 0;
101}
102
8d7d7b61 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
6b6c3be6 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
6b5105be 180static int GetATRDi(uint8_t *atr, size_t atrlen) {
6b6c3be6 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
6b5105be 186static int GetATRFi(uint8_t *atr, size_t atrlen) {
6b6c3be6 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
6b5105be 192static float GetATRF(uint8_t *atr, size_t atrlen) {
6b6c3be6 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) {
6b6c3be6 199
200 uint8_t T0 = atr[1];
201 uint8_t K = T0 & 0x0F;
151a33c0 202 uint8_t TD1 = 0, T1len = 0, TD1len = 0, TDilen = 0;
a9104f7e 203 bool protocol_T0_present = true;
204 bool protocol_T15_present = false;
6b6c3be6 205
206 if (T0 & 0x10) {
151a33c0 207 PrintAndLog("\t- TA1 (Maximum clock frequency, proposed bit duration) [ 0x%02x ]", atr[2 + T1len]);
6b6c3be6 208 T1len++;
209 }
151a33c0 210
6b6c3be6 211 if (T0 & 0x20) {
151a33c0 212 PrintAndLog("\t- TB1 (Deprecated: VPP requirements) [ 0x%02x ]", atr[2 + T1len]);
6b6c3be6 213 T1len++;
214 }
151a33c0 215
6b6c3be6 216 if (T0 & 0x40) {
151a33c0 217 PrintAndLog("\t- TC1 (Extra delay between bytes required by card) [ 0x%02x ]", atr[2 + T1len]);
6b6c3be6 218 T1len++;
219 }
151a33c0 220
6b6c3be6 221 if (T0 & 0x80) {
222 TD1 = atr[2 + T1len];
151a33c0 223 PrintAndLog("\t- TD1 (First offered transmission protocol, presence of TA2..TD2) [ 0x%02x ] Protocol T%d", TD1, TD1 & 0x0f);
a9104f7e 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
6b6c3be6 232 T1len++;
233
234 if (TD1 & 0x10) {
151a33c0 235 PrintAndLog("\t- TA2 (Specific protocol and parameters to be used after the ATR) [ 0x%02x ]", atr[2 + T1len + TD1len]);
6b6c3be6 236 TD1len++;
237 }
238 if (TD1 & 0x20) {
151a33c0 239 PrintAndLog("\t- TB2 (Deprecated: VPP precise voltage requirement) [ 0x%02x ]", atr[2 + T1len + TD1len]);
6b6c3be6 240 TD1len++;
241 }
242 if (TD1 & 0x40) {
151a33c0 243 PrintAndLog("\t- TC2 (Maximum waiting time for protocol T=0) [ 0x%02x ]", atr[2 + T1len + TD1len]);
6b6c3be6 244 TD1len++;
245 }
246 if (TD1 & 0x80) {
247 uint8_t TDi = atr[2 + T1len + TD1len];
151a33c0 248 PrintAndLog("\t- TD2 (A supported protocol or more global parameters, presence of TA3..TD3) [ 0x%02x ] Protocol T%d", TDi, TDi & 0x0f);
a9104f7e 249 if ((TDi & 0x0f) == 0) {
250 protocol_T0_present = true;
251 }
252 if ((TDi & 0x0f) == 15) {
253 protocol_T15_present = true;
254 }
6b6c3be6 255 TD1len++;
256
257 bool nextCycle = true;
258 uint8_t vi = 3;
259 while (nextCycle) {
260 nextCycle = false;
261 if (TDi & 0x10) {
151a33c0 262 PrintAndLog("\t- TA%d: 0x%02x", vi, atr[2 + T1len + TD1len + TDilen]);
6b6c3be6 263 TDilen++;
264 }
265 if (TDi & 0x20) {
151a33c0 266 PrintAndLog("\t- TB%d: 0x%02x", vi, atr[2 + T1len + TD1len + TDilen]);
6b6c3be6 267 TDilen++;
268 }
269 if (TDi & 0x40) {
151a33c0 270 PrintAndLog("\t- TC%d: 0x%02x", vi, atr[2 + T1len + TD1len + TDilen]);
6b6c3be6 271 TDilen++;
272 }
273 if (TDi & 0x80) {
274 TDi = atr[2 + T1len + TD1len + TDilen];
151a33c0 275 PrintAndLog("\t- TD%d [ 0x%02x ] Protocol T%d", vi, TDi, TDi & 0x0f);
6b6c3be6 276 TDilen++;
277
278 nextCycle = true;
279 vi++;
280 }
281 }
282 }
283 }
284
a9104f7e 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 }
151a33c0 295
151a33c0 296 if (atr[0] != 0x3b)
297 PrintAndLogEx(WARNING, "Not a direct convention [ 0x%02x ]", atr[0]);
298
6b6c3be6 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);
6b6c3be6 303
6b6c3be6 304 if (K > 0)
a9104f7e 305 PrintAndLogEx(INFO, "\nHistorical bytes | len %02d | format %02x", K, atr[2 + T1len + TD1len + TDilen]);
151a33c0 306
6b6c3be6 307 if (K > 1) {
151a33c0 308 PrintAndLogEx(INFO, "\tHistorical bytes");
6b6c3be6 309 dump_buffer(&atr[2 + T1len + TD1len + TDilen], K, NULL, 1);
310 }
311
312 return 0;
313}
314
0b6efd01 315bool smart_getATR(smart_card_atr_t *card)
a9104f7e 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;
8d7d7b61 335 }
a9104f7e 336}
8d7d7b61 337
a9104f7e 338static bool smart_select(bool silent) {
339
340 smart_card_atr_t card;
341 if (!smart_getATR(&card)) {
8d7d7b61 342 if (!silent) PrintAndLogEx(WARNING, "smart card select failed");
343 return false;
344 }
345
346 if (!silent) {
8d7d7b61 347 PrintAndLogEx(INFO, "ISO7816-3 ATR : %s", sprint_hex(card.atr, card.atr_len));
348 }
349
350 return true;
351}
352
8d7d7b61 353
6b5105be 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);
151a33c0 360 } else {
6b5105be 361 UsbCommand c = {CMD_SMART_RAW, {flags, data_len, 0}};
362 memcpy(c.d.asBytes, data, data_len);
363 SendCommand(&c);
8d7d7b61 364
6b5105be 365 if (!WaitForResponseTimeout(CMD_ACK, &c, 2500)) {
366 PrintAndLogEx(WARNING, "smart card response timeout");
367 *response_len = -1;
368 return;
369 }
8d7d7b61 370
6b5105be 371 *response_len = c.arg[0];
372 if (*response_len > 0) {
373 memcpy(response, c.d.asBytes, *response_len);
374 }
8d7d7b61 375 }
376
6b5105be 377 if (*response_len <= 0) {
378 PrintAndLogEx(WARNING, "smart card response failed");
379 *response_len = -2;
380 return;
6b6c3be6 381 }
8d7d7b61 382
6b5105be 383 if (*response_len < 2) {
384 // PrintAndLogEx(SUCCESS, "R-TPDU %02X | ", response[0]);
385 return;
6b6c3be6 386 }
387
6b5105be 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]));
8d7d7b61 390}
391
a9104f7e 392
6b5105be 393static int CmdSmartSelect(const char *Cmd)
394{
a9104f7e 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
6b5105be 418
419static int CmdSmartRaw(const char *Cmd) {
43591e64 420
421 int hexlen = 0;
422 bool active = false;
423 bool active_select = false;
151a33c0 424 bool useT0 = false;
43591e64 425 uint8_t cmdp = 0;
426 bool errors = false, reply = true, decodeTLV = false, breakloop = false;
6b5105be 427 uint8_t data[ISO7816_MAX_FRAME_SIZE] = {0x00};
43591e64 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;
151a33c0 448 case '0':
449 useT0 = true;
450 cmdp++;
451 break;
43591e64 452 case 'd': {
453 switch (param_gethex_to_eol(Cmd, cmdp+1, data, sizeof(data), &hexlen)) {
454 case 1:
8d7d7b61 455 PrintAndLogEx(WARNING, "Invalid HEX value.");
43591e64 456 return 1;
457 case 2:
8d7d7b61 458 PrintAndLogEx(WARNING, "Too many bytes. Max %d bytes", sizeof(data));
43591e64 459 return 1;
460 case 3:
8d7d7b61 461 PrintAndLogEx(WARNING, "Hex must have even number of digits.");
43591e64 462 return 1;
463 }
464 cmdp++;
465 breakloop = true;
466 break;
467 }
468 default:
8d7d7b61 469 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
43591e64 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
6b5105be 481 uint32_t flags = 0;
482 uint32_t protocol = 0;
43591e64 483 if (active || active_select) {
6b5105be 484 flags |= SC_CONNECT;
6b6c3be6 485 if (active_select)
6b5105be 486 flags |= SC_SELECT;
6b6c3be6 487 }
43591e64 488 if (hexlen > 0) {
151a33c0 489 if (useT0)
6b5105be 490 protocol = SC_RAW_T0;
151a33c0 491 else
6b5105be 492 protocol = SC_RAW;
43591e64 493 }
6b5105be 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);
43591e64 504
505 // reading response from smart card
506 if ( reply ) {
6b5105be 507 if ( response_len < 0 ) {
508 free(response);
8d7d7b61 509 return 2;
43591e64 510 }
43591e64 511
6b5105be 512 if ( response[0] == 0x6C ) {
513 data[4] = response[1];
514 smart_transmit(data, hexlen, protocol, response, &response_len, ISO7816_MAX_FRAME_SIZE);
8d7d7b61 515 data[4] = 0;
43591e64 516 }
517
6b5105be 518 if (decodeTLV && response_len > 4)
519 TLVPrintFromBuffer(response, response_len-2);
43591e64 520
6b5105be 521 free(response);
8d7d7b61 522 }
523 return 0;
524}
43591e64 525
6b5105be 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;
43591e64 532
8d7d7b61 533 if (activateCard)
534 smart_select(false);
43591e64 535
6b5105be 536 uint32_t flags = SC_RAW_T0;
8d7d7b61 537 if (activateCard) {
6b5105be 538 flags |= SC_SELECT | SC_CONNECT;
43591e64 539 }
6b5105be 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;
8d7d7b61 571 }
572
6b5105be 573 if (*responselen < 0 ) {
4cdd63b2 574 return 1;
6b5105be 575 } else {
576 return 0;
6b6c3be6 577 }
43591e64 578}
579
8d7d7b61 580
6b5105be 581static int CmdSmartUpgrade(const char *Cmd) {
43591e64 582
9f596ec7 583 PrintAndLogEx(NORMAL, "");
584 PrintAndLogEx(WARNING, "WARNING - RDV4.0 Smartcard Socket Firmware upgrade.");
8d7d7b61 585 PrintAndLogEx(WARNING, "A dangerous command, do wrong and you will brick the smart card socket");
9f596ec7 586 PrintAndLogEx(NORMAL, "");
43591e64 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 ) {
8d7d7b61 598 PrintAndLogEx(FAILED, "Filename too long");
43591e64 599 errors = true;
600 break;
601 }
602 cmdp += 2;
603 break;
604 case 'h':
605 return usage_sm_upgrade();
606 default:
8d7d7b61 607 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
43591e64 608 errors = true;
609 break;
610 }
611 }
612
613 //Validations
614 if (errors || cmdp == 0 ) return usage_sm_upgrade();
615
9f596ec7 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
d3c60657 630 char sha512filename[FILE_PATH_SIZE] = {'\0'};
9f596ec7 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 ) {
d3c60657 641 memcpy(sha512filename, filename, strlen(filename) - strlen("bin"));
9f596ec7 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");
8d7d7b61 660 if ( !f ){
9f596ec7 661 PrintAndLogEx(FAILED, "Firmware file not found or locked.");
43591e64 662 return 1;
663 }
664
665 // get filesize in order to malloc memory
666 fseek(f, 0, SEEK_END);
9f596ec7 667 size_t fsize = ftell(f);
43591e64 668 fseek(f, 0, SEEK_SET);
669
9f596ec7 670 if (fsize < 0) {
671 PrintAndLogEx(FAILED, "Could not determine size of firmware file");
43591e64 672 fclose(f);
673 return 1;
674 }
8d7d7b61 675
43591e64 676 uint8_t *dump = calloc(fsize, sizeof(uint8_t));
677 if (!dump) {
9f596ec7 678 PrintAndLogEx(FAILED, "Could not allocate memory for firmware");
43591e64 679 fclose(f);
680 return 1;
681 }
682
9f596ec7 683 size_t firmware_size = fread(dump, 1, fsize, f);
43591e64 684 if (f)
685 fclose(f);
686
9f596ec7 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
43591e64 737 //Send to device
738 uint32_t index = 0;
739 uint32_t bytes_sent = 0;
9f596ec7 740 uint32_t bytes_remaining = firmware_size;
43591e64 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) ) {
8d7d7b61 752 PrintAndLogEx(WARNING, "timeout while waiting for reply.");
43591e64 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");
9f596ec7 763 PrintAndLogEx(SUCCESS, "RDV4.0 Smartcard Socket Firmware updating, don\'t turn off your PM3!");
43591e64 764
765 // trigger the firmware upgrade
9f596ec7 766 UsbCommand c = {CMD_SMART_UPGRADE, {firmware_size, 0, 0}};
43591e64 767 clearCommandBuffer();
768 SendCommand(&c);
769 UsbCommand resp;
770 if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) {
8d7d7b61 771 PrintAndLogEx(WARNING, "timeout while waiting for reply.");
43591e64 772 return 1;
773 }
8d7d7b61 774 if ( (resp.arg[0] & 0xFF ) )
9f596ec7 775 PrintAndLogEx(SUCCESS, "RDV4.0 Smartcard Socket Firmware upgraded successful");
43591e64 776 else
9f596ec7 777 PrintAndLogEx(FAILED, "RDV4.0 Smartcard Socket Firmware Upgrade failed");
43591e64 778 return 0;
779}
780
6b5105be 781
782static int CmdSmartInfo(const char *Cmd){
43591e64 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();
8d7d7b61 789 case 's':
43591e64 790 silent = true;
791 break;
792 default:
8d7d7b61 793 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
43591e64 794 errors = true;
795 break;
796 }
797 cmdp++;
798 }
799
800 //Validations
801 if (errors ) return usage_sm_info();
802
a9104f7e 803 smart_card_atr_t card;
804 if (!smart_getATR(&card)) {
8d7d7b61 805 if (!silent) PrintAndLogEx(WARNING, "smart card select failed");
43591e64 806 return 1;
807 }
0b6efd01
OM
808
809 if (!card.atr_len) {
810 if (!silent) PrintAndLogEx(ERR, "can't get ATR from a smart card");
811 return 1;
812 }
43591e64 813
43591e64 814 // print header
151a33c0 815 PrintAndLogEx(INFO, "--- Smartcard Information ---------");
8d7d7b61 816 PrintAndLogEx(INFO, "-------------------------------------------------------------");
151a33c0 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) );
6b6c3be6 819
820 // print ATR
821 PrintAndLogEx(NORMAL, "");
151a33c0 822 PrintAndLogEx(INFO, "ATR");
6b6c3be6 823 PrintATR(card.atr, card.atr_len);
824
825 // print D/F (brom byte TA1 or defaults)
826 PrintAndLogEx(NORMAL, "");
151a33c0 827 PrintAndLogEx(INFO, "D/F (TA1)");
6b6c3be6 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
151a33c0 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 };
6b6c3be6 845
43591e64 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();
8d7d7b61 856 case 's':
43591e64 857 silent = true;
858 break;
859 default:
8d7d7b61 860 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
43591e64 861 errors = true;
862 break;
863 }
864 cmdp++;
865 }
866
867 //Validations
868 if (errors ) return usage_sm_reader();
869
a9104f7e 870 smart_card_atr_t card;
871 if (!smart_getATR(&card)) {
8d7d7b61 872 if (!silent) PrintAndLogEx(WARNING, "smart card select failed");
43591e64 873 return 1;
874 }
8d7d7b61 875
876 PrintAndLogEx(INFO, "ISO7816-3 ATR : %s", sprint_hex(card.atr, card.atr_len));
43591e64 877 return 0;
878}
879
6b5105be 880
881static int CmdSmartSetClock(const char *Cmd){
43591e64 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();
8d7d7b61 888 case 'c':
43591e64 889 clock = param_get8ex(Cmd, cmdp+1, 2, 10);
890 if ( clock > 2)
891 errors = true;
8d7d7b61 892
43591e64 893 cmdp += 2;
894 break;
895 default:
8d7d7b61 896 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
43591e64 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) ) {
8d7d7b61 910 PrintAndLogEx(WARNING, "smart card select failed");
43591e64 911 return 1;
912 }
913
914 uint8_t isok = resp.arg[0] & 0xFF;
915 if (!isok) {
8d7d7b61 916 PrintAndLogEx(WARNING, "smart card set clock failed");
43591e64 917 return 1;
918 }
919
920 switch (clock) {
921 case 0:
8d7d7b61 922 PrintAndLogEx(SUCCESS, "Clock changed to 16mhz giving 10800 baudrate");
43591e64 923 break;
924 case 1:
8d7d7b61 925 PrintAndLogEx(SUCCESS, "Clock changed to 8mhz giving 21600 baudrate");
43591e64 926 break;
927 case 2:
8d7d7b61 928 PrintAndLogEx(SUCCESS, "Clock changed to 4mhz giving 86400 baudrate");
43591e64 929 break;
930 default:
931 break;
932 }
933 return 0;
934}
935
6b5105be 936
937static int CmdSmartList(const char *Cmd) {
53fb848a 938 if (UseAlternativeSmartcardReader) {
939 CmdHFList("7816 p");
940 } else {
941 CmdHFList("7816");
942 }
8d7d7b61 943 return 0;
43591e64 944}
945
6b5105be 946
947static int CmdSmartBruteforceSFI(const char *Cmd) {
43591e64 948
8d7d7b61 949 char ctmp = tolower(param_getchar(Cmd, 0));
950 if (ctmp == 'h') return usage_sm_brute();
43591e64 951
8d7d7b61 952 uint8_t data[5] = {0x00, 0xB2, 0x00, 0x00, 0x00};
43591e64 953
8d7d7b61 954 PrintAndLogEx(INFO, "Selecting card");
955 if ( !smart_select(false) ) {
956 return 1;
43591e64 957 }
958
6b5105be 959 PrintAndLogEx(INFO, "Selecting PSE aid");
151a33c0 960 CmdSmartRaw("s 0 t d 00a404000e325041592e5359532e4444463031");
961 CmdSmartRaw("0 t d 00a4040007a000000004101000"); // mastercard
962// CmdSmartRaw("0 t d 00a4040007a0000000031010"); // visa
43591e64 963
8d7d7b61 964 PrintAndLogEx(INFO, "starting");
43591e64 965
6b5105be 966 int response_len = 0;
967 uint8_t* response = malloc(ISO7816_MAX_FRAME_SIZE);
968 if (!response)
8d7d7b61 969 return 1;
43591e64 970
8d7d7b61 971 for (uint8_t i=1; i < 4; i++) {
972 for (int p1=1; p1 < 5; p1++) {
43591e64 973
8d7d7b61 974 data[2] = p1;
975 data[3] = (i << 3) + 4;
43591e64 976
6b5105be 977 smart_transmit(data, sizeof(data), SC_RAW_T0, response, &response_len, ISO7816_MAX_FRAME_SIZE);
43591e64 978
6b5105be 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);
43591e64 982
8d7d7b61 983 // TLV decoder
6b5105be 984 if (response_len > 4)
985 TLVPrintFromBuffer(response+1, response_len-3);
43591e64 986
8d7d7b61 987 data[4] = 0;
43591e64 988 }
6b5105be 989 memset(response, 0x00, ISO7816_MAX_FRAME_SIZE);
43591e64 990 }
991 }
6b5105be 992 free(response);
43591e64 993 return 0;
994}
995
996static command_t CommandTable[] = {
9f596ec7 997 {"help", CmdHelp, 1, "This help"},
a9104f7e 998 {"select", CmdSmartSelect, 1, "Select the Smartcard Reader to use"},
6b5105be 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"},
9f596ec7 1003 {"upgrade", CmdSmartUpgrade, 0, "Upgrade firmware"},
6b5105be 1004 {"setclock", CmdSmartSetClock, 1, "Set clock speed"},
1005 {"brute", CmdSmartBruteforceSFI, 1, "Bruteforce SFI"},
9f596ec7 1006 {NULL, NULL, 0, NULL}
43591e64 1007};
1008
6b5105be 1009
43591e64 1010int CmdSmartcard(const char *Cmd) {
1011 clearCommandBuffer();
1012 CmdsParse(CommandTable, Cmd);
1013 return 0;
1014}
1015
6b5105be 1016
1017static int CmdHelp(const char *Cmd) {
43591e64 1018 CmdsHelp(CommandTable);
1019 return 0;
1020}
Impressum, Datenschutz