From 550fa5aa8ffc6bbecba44457bd3cc98384ba17cf Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Fri, 30 Apr 2021 08:51:38 +0200 Subject: [PATCH] 14a exchange apdu now uses defined return codes --- client/src/cmdhf14a.c | 80 ++++++++++++++++++++-------------------- client/src/cmdhfemrtd.c | 2 +- client/src/cmdhfmf.c | 8 ++-- client/src/cmdhfmfdes.c | 2 +- client/src/cmdhfst.c | 36 +++++++++--------- client/src/emv/emvcore.c | 4 +- 6 files changed, 65 insertions(+), 67 deletions(-) diff --git a/client/src/cmdhf14a.c b/client/src/cmdhf14a.c index 0c77a5dae..2516ac8b8 100644 --- a/client/src/cmdhf14a.c +++ b/client/src/cmdhf14a.c @@ -855,34 +855,34 @@ static int SelectCard14443_4(bool disconnect, iso14a_card_select_t *card) { // Anticollision + SELECT card SendCommandMIX(CMD_HF_ISO14443A_READER, ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0, NULL, 0); - if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) { - PrintAndLogEx(ERR, "Proxmark3 connection timeout."); - return 1; + if (WaitForResponseTimeout(CMD_ACK, &resp, 1500) == false) { + PrintAndLogEx(ERR, "Proxmark3 connection timeout"); + return PM3_ETIMEOUT; } // check result if (resp.oldarg[0] == 0) { - PrintAndLogEx(ERR, "No card in field."); - return 1; + PrintAndLogEx(ERR, "No card in fiel."); + return PM3_ECARDEXCHANGE; } if (resp.oldarg[0] != 1 && resp.oldarg[0] != 2) { - PrintAndLogEx(ERR, "Card not in iso14443-4. res=%" PRId64 ".", resp.oldarg[0]); - return 1; + PrintAndLogEx(ERR, "Card not in iso14443-4, res=%" PRId64 ".", resp.oldarg[0]); + return PM3_ECARDEXCHANGE; } if (resp.oldarg[0] == 2) { // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision // get ATS uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0 SendCommandMIX(CMD_HF_ISO14443A_READER, ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT, sizeof(rats), 0, rats, sizeof(rats)); - if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) { - PrintAndLogEx(ERR, "Proxmark3 connection timeout."); - return 1; + if (WaitForResponseTimeout(CMD_ACK, &resp, 1500) == false) { + PrintAndLogEx(ERR, "Proxmark3 connection timeout"); + return PM3_ETIMEOUT; } if (resp.oldarg[0] == 0) { // ats_len - PrintAndLogEx(ERR, "Can't get ATS."); - return 1; + PrintAndLogEx(ERR, "Can't get ATS"); + return PM3_ECARDEXCHANGE; } // get frame length from ATS in data field @@ -907,7 +907,7 @@ static int SelectCard14443_4(bool disconnect, iso14a_card_select_t *card) { if (disconnect) DropField(); - return 0; + return PM3_SUCCESS; } static int CmdExchangeAPDU(bool chainingin, uint8_t *datain, int datainlen, bool activateField, uint8_t *dataout, int maxdataoutlen, int *dataoutlen, bool *chainingout) { @@ -916,7 +916,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); - if (selres) + if (selres != PM3_SUCCESS) return selres; } @@ -946,32 +946,32 @@ static int CmdExchangeAPDU(bool chainingin, uint8_t *datain, int datainlen, bool *dataoutlen += dlen; if (maxdataoutlen && *dataoutlen > maxdataoutlen) { - PrintAndLogEx(ERR, "APDU: Buffer too small(%d). Needs %d bytes", *dataoutlen, maxdataoutlen); - return 2; + PrintAndLogEx(ERR, "APDU: Buffer too small(%d), needs %d bytes", *dataoutlen, maxdataoutlen); + return PM3_EAPDU_FAIL; } // I-block ACK if ((res & 0xf2) == 0xa2) { *dataoutlen = 0; *chainingout = true; - return 0; + return PM3_SUCCESS; } if (!iLen) { - PrintAndLogEx(ERR, "APDU: No APDU response."); - return 1; + PrintAndLogEx(ERR, "APDU: No APDU response"); + return PM3_EAPDU_FAIL; } // check apdu length if (iLen < 2 && iLen >= 0) { - PrintAndLogEx(ERR, "APDU: Small APDU response. Len=%d", iLen); - return 2; + PrintAndLogEx(ERR, "APDU: Small APDU response, len %d", iLen); + return PM3_EAPDU_FAIL; } // check block TODO if (iLen == -2) { - PrintAndLogEx(ERR, "APDU: Block type mismatch."); - return 2; + PrintAndLogEx(ERR, "APDU: Block type mismatch"); + return PM3_EAPDU_FAIL; } memcpy(dataout, recv, dlen); @@ -983,12 +983,12 @@ static int CmdExchangeAPDU(bool chainingin, uint8_t *datain, int datainlen, bool // CRC Check if (iLen == -1) { - PrintAndLogEx(ERR, "APDU: ISO 14443A CRC error."); - return 3; + PrintAndLogEx(ERR, "APDU: ISO 14443A CRC error"); + return PM3_EAPDU_FAIL; } } else { - PrintAndLogEx(ERR, "APDU: Reply timeout."); - return 4; + PrintAndLogEx(ERR, "APDU: Reply timeout"); + return PM3_EAPDU_FAIL; } return PM3_SUCCESS; @@ -1012,8 +1012,8 @@ int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool lea *dataoutlen = 0; res = CmdExchangeAPDU(chainBlockNotLast, &datain[clen], vlen, vActivateField, dataout, maxdataoutlen, dataoutlen, &chaining); - if (res) { - if (!leaveSignalON) + if (res != PM3_SUCCESS) { + if (leaveSignalON == false) DropField(); return 200; @@ -1022,7 +1022,7 @@ int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool lea // check R-block ACK //TODO check this one... if ((*dataoutlen == 0) && (*dataoutlen != 0 || chaining != chainBlockNotLast)) { // *dataoutlen!=0. 'A && (!A || B)' is equivalent to 'A && B' - if (!leaveSignalON) + if (leaveSignalON == false) DropField(); return 201; @@ -1038,8 +1038,8 @@ int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool lea } while (clen < datainlen); } else { res = CmdExchangeAPDU(false, datain, datainlen, activateField, dataout, maxdataoutlen, dataoutlen, &chaining); - if (res) { - if (!leaveSignalON) + if (res != PM3_SUCCESS) { + if (leaveSignalON == false) DropField(); return res; @@ -1049,9 +1049,8 @@ int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool lea while (chaining) { // I-block with chaining res = CmdExchangeAPDU(false, NULL, 0, false, &dataout[*dataoutlen], maxdataoutlen, dataoutlen, &chaining); - - if (res) { - if (!leaveSignalON) + if (res == PM3_SUCCESS) { + if (leaveSignalON == false) DropField(); return 100; @@ -1061,7 +1060,7 @@ int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool lea if (!leaveSignalON) DropField(); - return 0; + return PM3_SUCCESS; } // ISO14443-4. 7. Half-duplex block transmission protocol @@ -1173,8 +1172,7 @@ static int CmdHF14AAPDU(const char *Cmd) { } int res = ExchangeAPDU14a(data, datalen, activateField, leaveSignalON, data, PM3_CMD_DATA_SIZE, &datalen); - - if (res) + if (res != PM3_SUCCESS) return res; PrintAndLogEx(SUCCESS, "<<< %s | %s", sprint_hex_inrow(data, datalen), sprint_ascii(data, datalen)); @@ -2211,8 +2209,8 @@ static int CmdHf14AFindapdu(const char *Cmd) { PrintAndLogEx(INFO, "Sending a test APDU (select file command) to check if the tag is responding to APDU"); param_gethex_to_eol("00a404000aa000000440000101000100", 0, aSELECT_AID, sizeof(aSELECT_AID), &aSELECT_AID_n); int res = ExchangeAPDU14a(aSELECT_AID, aSELECT_AID_n, true, false, response, sizeof(response), &response_n); - if (res) { - PrintAndLogEx(FAILED, "Tag did not responde to a test APDU (select file command). Aborting"); + if (res != PM3_SUCCESS) { + PrintAndLogEx(FAILED, "Tag did not respond to a test APDU (select file command). Aborting"); return res; } PrintAndLogEx(SUCCESS, "Got response. Starting the APDU finder [ CLA " _GREEN_("%02X") " INS " _GREEN_("%02X") " P1 " _GREEN_("%02X") " P2 " _GREEN_("%02X") " ]", cla, ins, p1, p2); @@ -2258,7 +2256,7 @@ retry_ins: for (int i = 0; i < 1 + with_le; i++) { // Send APDU. res = ExchangeAPDU14a(command, command_n + i, activate_field, keep_field_on, response, sizeof(response), &response_n); - if (res) { + if (res != PM3_SUCCESS) { DropField(); activate_field = true; goto retry_ins; diff --git a/client/src/cmdhfemrtd.c b/client/src/cmdhfemrtd.c index 93b5d64e1..f0fcad58e 100644 --- a/client/src/cmdhfemrtd.c +++ b/client/src/cmdhfemrtd.c @@ -201,7 +201,7 @@ static bool emrtd_exchange_commands(const char *cmd, uint8_t *dataout, int *data } else { res = ExchangeAPDU14a(aCMD, aCMD_n, activate_field, keep_field_on, response, sizeof(response), &resplen); } - if (res) { + if (res != PM3_SUCCESS) { DropField(); return false; } diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index b7e12d0af..336d7c574 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -3836,7 +3836,7 @@ int CmdHF14AMfELoad(const char *Cmd) { datalen -= block_width; } free(data); - PrintAndLogEx(NORMAL, "\n"); + PrintAndLogEx(NORMAL, ""); if (block_width == 4) { PrintAndLogEx(HINT, "You are ready to simulate. See " _YELLOW_("`hf mfu sim -h`")); @@ -5674,7 +5674,7 @@ static int CmdHf14AMfSuperCard(const char *Cmd) { // --------------- RESET CARD ---------------- uint8_t aRESET[] = { 0x00, 0xa6, 0xc0, 0x00 }; res = ExchangeAPDU14a(aRESET, sizeof(aRESET), activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { PrintAndLogEx(FAILED, "Super card reset [ " _RED_("fail") " ]"); DropField(); return res; @@ -5692,7 +5692,7 @@ static int CmdHf14AMfSuperCard(const char *Cmd) { // --------------- First ---------------- uint8_t aFIRST[] = { 0x00, 0xa6, 0xb0, 0x00, 0x10 }; res = ExchangeAPDU14a(aFIRST, sizeof(aFIRST), activate_field, keep_field_on, responseA, sizeof(responseA), &respAlen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -5703,7 +5703,7 @@ static int CmdHf14AMfSuperCard(const char *Cmd) { uint8_t aSECOND[] = { 0x00, 0xa6, 0xb0, 0x01, 0x10 }; res = ExchangeAPDU14a(aSECOND, sizeof(aSECOND), activate_field, keep_field_on, responseB, sizeof(responseB), &respBlen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 9829dba2a..b2f5d79ee 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -473,7 +473,7 @@ static int DESFIRESendApdu(bool activate_field, bool leavefield_on, sAPDU apdu, PrintAndLogEx(SUCCESS, ">>>> %s", sprint_hex(data, datalen)); res = ExchangeAPDU14a(data, datalen, activate_field, leavefield_on, result, max_result_len, (int *)result_len); - if (res) { + if (res != PM3_SUCCESS) { return res; } diff --git a/client/src/cmdhfst.c b/client/src/cmdhfst.c index b15c8e8b4..ad768598e 100644 --- a/client/src/cmdhfst.c +++ b/client/src/cmdhfst.c @@ -215,7 +215,7 @@ int infoHFST(void) { int aSELECT_AID_n = 0; param_gethex_to_eol("00a4040007d276000085010100", 0, aSELECT_AID, sizeof(aSELECT_AID), &aSELECT_AID_n); int res = ExchangeAPDU14a(aSELECT_AID, aSELECT_AID_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -240,7 +240,7 @@ int infoHFST(void) { int aSELECT_FILE_CC_n = 0; param_gethex_to_eol("00a4000c02e103", 0, aSELECT_FILE_CC, sizeof(aSELECT_FILE_CC), &aSELECT_FILE_CC_n); res = ExchangeAPDU14a(aSELECT_FILE_CC, aSELECT_FILE_CC_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -256,7 +256,7 @@ int infoHFST(void) { int aREAD_CC_n = 0; param_gethex_to_eol("00b000000f", 0, aREAD_CC, sizeof(aREAD_CC), &aREAD_CC_n); res = ExchangeAPDU14a(aREAD_CC, aREAD_CC_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -276,7 +276,7 @@ int infoHFST(void) { int aSELECT_FILE_SYS_n = 0; param_gethex_to_eol("00a4000c02e101", 0, aSELECT_FILE_SYS, sizeof(aSELECT_FILE_SYS), &aSELECT_FILE_SYS_n); res = ExchangeAPDU14a(aSELECT_FILE_SYS, aSELECT_FILE_SYS_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -294,7 +294,7 @@ int infoHFST(void) { int aREAD_SYS_n = 0; param_gethex_to_eol("00b0000012", 0, aREAD_SYS, sizeof(aREAD_SYS), &aREAD_SYS_n); res = ExchangeAPDU14a(aREAD_SYS, aREAD_SYS_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -400,7 +400,7 @@ static int CmdHFSTNdef(const char *Cmd) { int aSELECT_AID_n = 0; param_gethex_to_eol("00a4040007d276000085010100", 0, aSELECT_AID, sizeof(aSELECT_AID), &aSELECT_AID_n); int res = ExchangeAPDU14a(aSELECT_AID, aSELECT_AID_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -425,7 +425,7 @@ static int CmdHFSTNdef(const char *Cmd) { int aSELECT_FILE_NDEF_n = 0; param_gethex_to_eol("00a4000c020001", 0, aSELECT_FILE_NDEF, sizeof(aSELECT_FILE_NDEF), &aSELECT_FILE_NDEF_n); res = ExchangeAPDU14a(aSELECT_FILE_NDEF, aSELECT_FILE_NDEF_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -443,7 +443,7 @@ static int CmdHFSTNdef(const char *Cmd) { int aVERIFY_n = 0; param_gethex_to_eol("0020000100", 0, aVERIFY, sizeof(aVERIFY), &aVERIFY_n); res = ExchangeAPDU14a(aVERIFY, aVERIFY_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -454,7 +454,7 @@ static int CmdHFSTNdef(const char *Cmd) { param_gethex_to_eol("0020000110", 0, aVERIFY, sizeof(aVERIFY), &aVERIFY_n); memcpy(aVERIFY + aVERIFY_n, pwd, pwdlen); res = ExchangeAPDU14a(aVERIFY, aVERIFY_n + pwdlen, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -473,7 +473,7 @@ static int CmdHFSTNdef(const char *Cmd) { int aREAD_NDEF_n = 0; param_gethex_to_eol("00b000001d", 0, aREAD_NDEF, sizeof(aREAD_NDEF), &aREAD_NDEF_n); res = ExchangeAPDU14a(aREAD_NDEF, aREAD_NDEF_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -563,7 +563,7 @@ static int CmdHFSTProtect(const char *Cmd) { int aSELECT_AID_n = 0; param_gethex_to_eol("00a4040007d276000085010100", 0, aSELECT_AID, sizeof(aSELECT_AID), &aSELECT_AID_n); int res = ExchangeAPDU14a(aSELECT_AID, aSELECT_AID_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -588,7 +588,7 @@ static int CmdHFSTProtect(const char *Cmd) { int aSELECT_FILE_NDEF_n = 0; param_gethex_to_eol("00a4000c020001", 0, aSELECT_FILE_NDEF, sizeof(aSELECT_FILE_NDEF), &aSELECT_FILE_NDEF_n); res = ExchangeAPDU14a(aSELECT_FILE_NDEF, aSELECT_FILE_NDEF_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -607,7 +607,7 @@ static int CmdHFSTProtect(const char *Cmd) { param_gethex_to_eol("0020000210", 0, aVERIFY, sizeof(aVERIFY), &aVERIFY_n); memcpy(aVERIFY + aVERIFY_n, pwd, pwdlen); res = ExchangeAPDU14a(aVERIFY, aVERIFY_n + pwdlen, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -626,7 +626,7 @@ static int CmdHFSTProtect(const char *Cmd) { param_gethex_to_eol("00", 0, aPROTECT, sizeof(aPROTECT), &aPROTECT_n); memcpy(aPROTECT + aPROTECT_n, state, statelen); res = ExchangeAPDU14a(aPROTECT, aPROTECT_n + statelen, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -707,7 +707,7 @@ static int CmdHFSTPwd(const char *Cmd) { int aSELECT_AID_n = 0; param_gethex_to_eol("00a4040007d276000085010100", 0, aSELECT_AID, sizeof(aSELECT_AID), &aSELECT_AID_n); int res = ExchangeAPDU14a(aSELECT_AID, aSELECT_AID_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -732,7 +732,7 @@ static int CmdHFSTPwd(const char *Cmd) { int aSELECT_FILE_NDEF_n = 0; param_gethex_to_eol("00a4000c020001", 0, aSELECT_FILE_NDEF, sizeof(aSELECT_FILE_NDEF), &aSELECT_FILE_NDEF_n); res = ExchangeAPDU14a(aSELECT_FILE_NDEF, aSELECT_FILE_NDEF_n, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -751,7 +751,7 @@ static int CmdHFSTPwd(const char *Cmd) { param_gethex_to_eol("0020000210", 0, aVERIFY, sizeof(aVERIFY), &aVERIFY_n); memcpy(aVERIFY + aVERIFY_n, pwd, pwdlen); res = ExchangeAPDU14a(aVERIFY, aVERIFY_n + pwdlen, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } @@ -772,7 +772,7 @@ static int CmdHFSTPwd(const char *Cmd) { memcpy(aCHG_PWD + aCHG_PWD_n, changePwd, changePwdlen); memcpy(aCHG_PWD + aCHG_PWD_n + changePwdlen, newpwd, newpwdlen); res = ExchangeAPDU14a(aCHG_PWD, aCHG_PWD_n + changePwdlen + newpwdlen, activate_field, keep_field_on, response, sizeof(response), &resplen); - if (res) { + if (res != PM3_SUCCESS) { DropField(); return res; } diff --git a/client/src/emv/emvcore.c b/client/src/emv/emvcore.c index c4b93d9a8..4ab081fff 100644 --- a/client/src/emv/emvcore.c +++ b/client/src/emv/emvcore.c @@ -301,9 +301,9 @@ static int EMVExchangeEx(EMVCommandChannel channel, bool ActivateField, bool Lea switch (channel) { case ECC_CONTACTLESS: res = ExchangeAPDU14a(data, datalen, ActivateField, LeaveFieldON, Result, (int)MaxResultLen, (int *)ResultLen); - if (res) { + if (res != PM3_SUCCESS) { res = exchange_14b_apdu(data, datalen, ActivateField, LeaveFieldON, Result, (int)MaxResultLen, (int *)ResultLen, 4000); - if (res) + if (res != PM3_SUCCESS) return res; } break;