From af6ff40748b5fe12223107a2efa7b42f25466f10 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 2 Jun 2021 23:38:43 +0200 Subject: [PATCH] simplify Iso7816Connect --- client/src/cmdhf14a.c | 4 ++-- client/src/cmdhf14a.h | 2 +- client/src/cmdhf14b.c | 2 +- client/src/cmdhf14b.h | 2 ++ client/src/cmdhfmfdes.c | 2 +- client/src/iso7816/iso7816core.c | 35 ++++++++------------------------ 6 files changed, 16 insertions(+), 31 deletions(-) diff --git a/client/src/cmdhf14a.c b/client/src/cmdhf14a.c index 4e081c94f..6396323ca 100644 --- a/client/src/cmdhf14a.c +++ b/client/src/cmdhf14a.c @@ -846,7 +846,7 @@ int ExchangeRAW14a(uint8_t *datain, int datainlen, bool activateField, bool leav return 0; } -int SelectCard14443_4(bool disconnect, iso14a_card_select_t *card) { +int SelectCard14443A_4(bool disconnect, iso14a_card_select_t *card) { PacketResponseNG resp; frameLength = 0; @@ -918,7 +918,7 @@ static int CmdExchangeAPDU(bool chainingin, uint8_t *datain, int datainlen, bool if (activateField) { // select with no disconnect and set frameLength - int selres = SelectCard14443_4(false, NULL); + int selres = SelectCard14443A_4(false, NULL); if (selres != PM3_SUCCESS) return selres; } diff --git a/client/src/cmdhf14a.h b/client/src/cmdhf14a.h index a6791dfe5..874a073c5 100644 --- a/client/src/cmdhf14a.h +++ b/client/src/cmdhf14a.h @@ -35,5 +35,5 @@ int Hf14443_4aGetCardData(iso14a_card_select_t *card); int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen); int ExchangeRAW14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen, bool silentMode); -int SelectCard14443_4(bool disconnect, iso14a_card_select_t *card); +int SelectCard14443A_4(bool disconnect, iso14a_card_select_t *card); #endif diff --git a/client/src/cmdhf14b.c b/client/src/cmdhf14b.c index f390b96a1..2d86fb51f 100644 --- a/client/src/cmdhf14b.c +++ b/client/src/cmdhf14b.c @@ -1558,7 +1558,7 @@ static int srix4kValid(const char *Cmd) { } */ -static int select_card_14443b_4(bool disconnect, iso14b_card_select_t *card) { +int select_card_14443b_4(bool disconnect, iso14b_card_select_t *card) { if (card) memset(card, 0, sizeof(iso14b_card_select_t)); diff --git a/client/src/cmdhf14b.h b/client/src/cmdhf14b.h index 25f94eabf..7f311898f 100644 --- a/client/src/cmdhf14b.h +++ b/client/src/cmdhf14b.h @@ -12,11 +12,13 @@ #define CMDHF14B_H__ #include "common.h" +#include "iso14b.h" int CmdHF14B(const char *Cmd); int CmdHF14BNdefRead(const char *Cmd); int exchange_14b_apdu(uint8_t *datain, int datainlen, bool activate_field, bool leave_signal_on, uint8_t *dataout, int maxdataoutlen, int *dataoutlen, int user_timeout); +int select_card_14443b_4(bool disconnect, iso14b_card_select_t *card); int infoHF14B(bool verbose, bool do_aid_search); int readHF14B(bool loop, bool verbose); diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 9755c915e..ae0d7f2c4 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -3613,7 +3613,7 @@ static int CmdHF14ADesInfo(const char *Cmd) { iso14a_card_select_t card; - res = SelectCard14443_4(true, &card); + res = SelectCard14443A_4(true, &card); if (res == PM3_SUCCESS) { static const char STANDALONE_DESFIRE[] = { 0x75, 0x77, 0x81, 0x02}; static const char JCOP_DESFIRE[] = { 0x75, 0xf7, 0xb1, 0x02 }; diff --git a/client/src/iso7816/iso7816core.c b/client/src/iso7816/iso7816core.c index 707caabc1..fae43b9dd 100644 --- a/client/src/iso7816/iso7816core.c +++ b/client/src/iso7816/iso7816core.c @@ -50,39 +50,22 @@ int Iso7816Connect(Iso7816CommandChannel channel) { return PM3_ENOTIMPL; } // Try to 14a - SendCommandMIX(CMD_HF_ISO14443A_READER, ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0, NULL, 0); - PacketResponseNG resp; - bool failed_14a = false; - if (WaitForResponseTimeout(CMD_ACK, &resp, 2000) == false) { - DropField(); - failed_14a = true; - } - - if ((!failed_14a) && resp.oldarg[0] != 0) { + // select with no disconnect and set frameLength + int selres = SelectCard14443A_4(false, NULL); + if (selres == PM3_SUCCESS) { SetISODEPState(ISODEP_NFCA); return PM3_SUCCESS; } PrintAndLogEx(DEBUG, "No 14a tag spotted, trying 14b"); // If not 14a, try to 14b - iso14b_raw_cmd_t packet = { - .flags = (ISO14B_CONNECT | ISO14B_SELECT_STD), - .timeout = 0, - .rawlen = 0, - }; - clearCommandBuffer(); - SendCommandNG(CMD_HF_ISO14443B_COMMAND, (uint8_t *)&packet, sizeof(iso14b_raw_cmd_t)); - if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND, &resp, 2000) == false) { - PrintAndLogEx(DEBUG, "Timeout, no 14b tag spotted, exiting"); - return PM3_ETIMEOUT; + selres = select_card_14443b_4(false, NULL); + if (selres == PM3_SUCCESS) { + SetISODEPState(ISODEP_NFCB); + return PM3_SUCCESS; } - - if (resp.oldarg[0] != 0) { - PrintAndLogEx(DEBUG, "No 14b tag spotted, failed to find any tag."); - return PM3_ENODATA; - } - SetISODEPState(ISODEP_NFCB); - return PM3_SUCCESS; + PrintAndLogEx(DEBUG, "No 14b tag spotted, failed to find any tag."); + return selres; } int Iso7816ExchangeEx(Iso7816CommandChannel channel, bool ActivateField, bool LeaveFieldON, sAPDU apdu, bool includeLe, uint16_t Le, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw) {