From 68e93fa63967f0a164de09d83557a28aad04c543 Mon Sep 17 00:00:00 2001 From: marshmellow42 Date: Mon, 6 Aug 2018 18:54:47 -0400 Subject: [PATCH] add smartcard to menu + make get atr work sc is now functioning as far as my limited knowledge takes me --- armsrc/appmain.c | 33 ++++++++++++++++++ armsrc/i2c.c | 18 +++++++++- client/cmdmain.c | 7 +++- client/cmdsmartcard.c | 48 ++++++-------------------- common/Makefile_Enabled_Options.common | 3 +- common/lfdemod.c | 4 +-- 6 files changed, 71 insertions(+), 42 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 27f43b3f..6385cafc 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -30,6 +30,10 @@ #ifdef WITH_LCD #include "LCD.h" #endif +#ifdef WITH_SMARTCARD + #include "i2c.h" +#endif + // Craig Young - 14a stand-alone code #ifdef WITH_ISO14443a @@ -1253,6 +1257,35 @@ void UsbPacketReceived(uint8_t *packet, int len) HfSnoop(c->arg[0], c->arg[1]); break; #endif +#ifdef WITH_SMARTCARD + case CMD_SMART_ATR: { + SmartCardAtr(); + break; + } + case CMD_SMART_SETBAUD:{ + SmartCardSetBaud(c->arg[0]); + break; + } + case CMD_SMART_SETCLOCK:{ + SmartCardSetClock(c->arg[0]); + break; + } + case CMD_SMART_RAW: { + SmartCardRaw(c->arg[0], c->arg[1], c->d.asBytes); + break; + } + case CMD_SMART_UPLOAD: { + // upload file from client + uint8_t *mem = BigBuf_get_addr(); + memcpy( mem + c->arg[0], c->d.asBytes, USB_CMD_DATA_SIZE); + cmd_send(CMD_ACK,1,0,0,0,0); + break; + } + case CMD_SMART_UPGRADE: { + SmartCardUpgrade(c->arg[0]); + break; + } +#endif case CMD_BUFF_CLEAR: BigBuf_Clear(); diff --git a/armsrc/i2c.c b/armsrc/i2c.c index 15c1bc24..8fb9f397 100644 --- a/armsrc/i2c.c +++ b/armsrc/i2c.c @@ -259,7 +259,7 @@ bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address) { do { if (!I2C_Start()) return false; - + //[C0] I2C_SendByte(device_address & 0xFE); if (!I2C_WaitAck()) break; @@ -545,6 +545,8 @@ bool GetATR(smart_card_atr_t *card_ptr) { // Send ATR // start [C0 01] stop start C1 len aa bb cc stop] I2C_WriteCmd(I2C_DEVICE_CMD_GENERATE_ATR, I2C_DEVICE_ADDRESS_MAIN); + uint8_t cmd[1] = {1}; + LogTrace(cmd, 1, 0, 0, NULL, true); //wait for sim card to answer. if (!I2C_WaitForSim()) @@ -556,10 +558,24 @@ bool GetATR(smart_card_atr_t *card_ptr) { if ( len == 0 ) return false; + // for some reason we only get first byte of atr, if that is so, send dummy command to retrieve the rest of the atr + if (len == 1) { + + uint8_t data[1] = {0}; + I2C_BufferWrite(data, len, I2C_DEVICE_CMD_SEND, I2C_DEVICE_ADDRESS_MAIN); + + if ( !I2C_WaitForSim() ) + return false; + + uint8_t len2 = I2C_BufferRead(card_ptr->atr + len, sizeof(card_ptr->atr) - len, I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN); + len = len + len2; + } + if ( card_ptr ) { card_ptr->atr_len = len; LogTrace(card_ptr->atr, card_ptr->atr_len, 0, 0, NULL, false); } + return true; } diff --git a/client/cmdmain.c b/client/cmdmain.c index 01d4c9a7..f503021a 100644 --- a/client/cmdmain.c +++ b/client/cmdmain.c @@ -26,7 +26,9 @@ #include "util.h" #include "util_posix.h" #include "cmdscript.h" - +#ifdef WITH_SMARTCARD + #include "cmdsmartcard.h" +#endif static int CmdHelp(const char *Cmd); static int CmdQuit(const char *Cmd); @@ -39,6 +41,9 @@ static command_t CommandTable[] = {"hf", CmdHF, 1, "{ High Frequency commands... }"}, {"hw", CmdHW, 1, "{ Hardware commands... }"}, {"lf", CmdLF, 1, "{ Low Frequency commands... }"}, +#ifdef WITH_SMARTCARD + {"sc", CmdSmartcard,1,"{ Smartcard commands... }"}, +#endif {"script",CmdScript,1, "{ Scripting commands }"}, {"quit", CmdQuit, 1, "Exit program"}, {"exit", CmdQuit, 1, "Exit program"}, diff --git a/client/cmdsmartcard.c b/client/cmdsmartcard.c index 3ac0ed8a..1b7ef138 100644 --- a/client/cmdsmartcard.c +++ b/client/cmdsmartcard.c @@ -372,7 +372,6 @@ int CmdSmartReader(const char *Cmd){ } smart_card_atr_t card; memcpy(&card, (smart_card_atr_t *)resp.d.asBytes, sizeof(smart_card_atr_t)); - PrintAndLog("ISO7816-3 ATR : %s", sprint_hex(card.atr, card.atr_len)); return 0; } @@ -510,7 +509,6 @@ uint16_t printScTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace) } parity_len = (data_len-1)/8 + 1; - PrintAndLog("TODO REMOVE ME: parlen=%u",parity_len); if (tracepos + data_len + parity_len > traceLen) { return traceLen; } @@ -562,49 +560,26 @@ uint16_t printScTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace) int ScTraceList(const char *Cmd) { bool loadFromFile = false; bool saveToFile = false; - char param1 = '\0'; - char param2 = '\0'; - char type[40] = {0}; + char type[5] = {0}; char filename[FILE_PATH_SIZE] = {0}; // parse command line - int tlen = param_getstr(Cmd, 0, type, sizeof(type)); - if (param_getlength(Cmd, 1) == 1) { - param1 = param_getchar(Cmd, 1); - } else { - param_getstr(Cmd, 1, filename, sizeof(filename)); - } - if (param_getlength(Cmd, 2) == 1) { - param2 = param_getchar(Cmd, 2); - } else if (strlen(filename) == 0) { - param_getstr(Cmd, 2, filename, sizeof(filename)); - } + param_getstr(Cmd, 0, type, sizeof(type)); + param_getstr(Cmd, 1, filename, sizeof(filename)); - // Validate param1 bool errors = false; - - if(tlen == 0) { - errors = true; - } - - if(param1 == 'h' - || (param1 != 0 && param1 != 'l') - || (param2 != 0 && param2 != 'l')) { + if(type[0] == 'h') { errors = true; } if(!errors) { - if (strcmp(type, "save") == 0) { + if (strcmp(type, "s") == 0) { saveToFile = true; - } else { - errors = true; + } else if (strcmp(type,"l") == 0) { + loadFromFile = true; } } - if (param1 == 'l' || param2 == 'l') { - loadFromFile = true; - } - if ((loadFromFile || saveToFile) && strlen(filename) == 0) { errors = true; } @@ -616,9 +591,9 @@ int ScTraceList(const char *Cmd) { if (errors) { PrintAndLog("List or save protocol data."); PrintAndLog("Usage: sc list [l ]"); - PrintAndLog(" sc list save "); + PrintAndLog(" sc list [s ]"); PrintAndLog(" l - load data from file instead of trace buffer"); - PrintAndLog(" save - save data to file"); + PrintAndLog(" s - save data to file"); PrintAndLog(""); PrintAndLog("example: sc list"); PrintAndLog("example: sc list save myCardTrace.trc"); @@ -626,7 +601,6 @@ int ScTraceList(const char *Cmd) { return 0; } - uint8_t *trace; uint32_t tracepos = 0; uint32_t traceLen = 0; @@ -692,8 +666,8 @@ int ScTraceList(const char *Cmd) { PrintAndLog(""); PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer"); PrintAndLog(""); - PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |"); - PrintAndLog("------------|------------|-----|-----------------------------------------------------------------|-----|--------------------|"); + PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |"); + PrintAndLog("------------|------------|-----|-------------------------------------------------------------------------|-----|--------------------|"); while(tracepos < traceLen) { diff --git a/common/Makefile_Enabled_Options.common b/common/Makefile_Enabled_Options.common index 3d1269c7..d9f6d3b9 100644 --- a/common/Makefile_Enabled_Options.common +++ b/common/Makefile_Enabled_Options.common @@ -16,6 +16,7 @@ APP_CFLAGS += -DWITH_ISO14443a_StandAlone \ -DWITH_SMARTCARD \ -DWITH_GUI #END + ### Standalone modes: #-DWITH_ISO14443a_StandAlone @@ -39,4 +40,4 @@ APP_CFLAGS += -DWITH_ISO14443a_StandAlone \ #-DWITH_GUI \ include QT GUI/Graph support in build #-DWITH_LCD \ include LCD support in build (experimental?) -#marshmellow NOTE: tested GUI, and smartcard removal only... +#marshmellow NOTE: tested GUI, and SMARTCARD removal only... diff --git a/common/lfdemod.c b/common/lfdemod.c index f470371a..76900047 100644 --- a/common/lfdemod.c +++ b/common/lfdemod.c @@ -10,8 +10,8 @@ // // NOTES: // LF Demod functions are placed here to allow the flexability to use client or -// device side. Most BUT NOT ALL of these functions are currenlty safe for -// device side use currently. (DetectST for example...) +// device side. Most BUT NOT ALL of these functions are currently safe for +// device side use. (DetectST for example...) // // There are likely many improvements to the code that could be made, please // make suggestions...