From 73db86bcc7819408f9322ec68178b8ee6634ba89 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 1 Jul 2021 20:32:04 +0300 Subject: [PATCH] exchange returns result. select works --- client/src/cmdhfmfdes.c | 6 ++--- client/src/mifare/desfirecore.c | 41 ++++++++++++++++++++++++--------- client/src/mifare/desfirecore.h | 4 ++-- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index fa22eabaa..046187dcb 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -5230,12 +5230,12 @@ static int CmdHF14ADesGetAIDs(const char *Cmd) { //size_t len = 0; - uint16_t sw = 0; + //uint16_t sw = 0; //uint8_t buf[APDU_RES_LEN] = {0}; int res = DesfireSelectAIDHex(&dctx, 0x000000, false, 0); - if (res != 0 || sw != 0x9000) { - PrintAndLogEx(ERR, "Desfire select " _RED_("error") ". Card returns 0x%02x", sw); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire select " _RED_("error") "."); DropField(); return PM3_ESOFT; } diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 6116db41e..3d910a0da 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -111,14 +111,16 @@ static int DESFIRESendApdu(bool activate_field, sAPDU apdu, uint8_t *result, uin return PM3_SUCCESS; } -static int DesfireExchangeNative(bool activate_field, DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen, bool enable_chaining) { +static int DesfireExchangeNative(bool activate_field, DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *respcode, uint8_t *resp, size_t *resplen, bool enable_chaining) { return PM3_SUCCESS; } -static int DesfireExchangeISO(bool activate_field, DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen, bool enable_chaining) { +static int DesfireExchangeISO(bool activate_field, DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *respcode, uint8_t *resp, size_t *resplen, bool enable_chaining) { if (resplen) *resplen = 0; + if (respcode) + *respcode = 0xff; // TODO !!! size_t splitbysize = 0; @@ -142,6 +144,10 @@ static int DesfireExchangeISO(bool activate_field, DesfireContext *ctx, uint8_t //PrintAndLogEx(DEBUG, "error DESFIRESendApdu %s", GetErrorString(res, &sw)); return res; } + + if (respcode != NULL && ((sw & 0xff00) == 0x9100)) + *respcode = sw & 0xff; + if (resp) memcpy(resp, buf, buflen); @@ -169,6 +175,9 @@ static int DesfireExchangeISO(bool activate_field, DesfireContext *ctx, uint8_t return res; } + if (respcode != NULL && ((sw & 0xff00) == 0x9100)) + *respcode = sw & 0xff; + if (resp != NULL) { if (splitbysize) { memcpy(&resp[i * splitbysize], buf, buflen); @@ -187,15 +196,15 @@ static int DesfireExchangeISO(bool activate_field, DesfireContext *ctx, uint8_t return PM3_SUCCESS; } -int DesfireExchangeEx(bool activate_field, DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen, bool enable_chaining) { +int DesfireExchangeEx(bool activate_field, DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *respcode, uint8_t *resp, size_t *resplen, bool enable_chaining) { int res = PM3_SUCCESS; switch(ctx->cmdChannel) { case DCCNative: - res = DesfireExchangeNative(activate_field, ctx, cmd, data, datalen, resp, resplen, enable_chaining); + res = DesfireExchangeNative(activate_field, ctx, cmd, data, datalen, respcode, resp, resplen, enable_chaining); break; case DCCNativeISO: - res = DesfireExchangeISO(activate_field, ctx, cmd, data, datalen, resp, resplen, enable_chaining); + res = DesfireExchangeISO(activate_field, ctx, cmd, data, datalen, respcode, resp, resplen, enable_chaining); break; case DCCISO: return PM3_EAPDU_FAIL; @@ -205,8 +214,8 @@ int DesfireExchangeEx(bool activate_field, DesfireContext *ctx, uint8_t cmd, uin return res; } -int DesfireExchange(DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen) { - return DesfireExchangeEx(false, ctx, cmd, data, datalen, resp, resplen, true); +int DesfireExchange(DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *respcode, uint8_t *resp, size_t *resplen) { + return DesfireExchangeEx(false, ctx, cmd, data, datalen, respcode, resp, resplen, true); } int DesfireSelectAID(DesfireContext *ctx, uint8_t *aid1, uint8_t *aid2) { @@ -219,15 +228,25 @@ int DesfireSelectAID(DesfireContext *ctx, uint8_t *aid1, uint8_t *aid2) { memcpy(&data[3], aid2, 3); uint8_t resp[257] = {0}; size_t resplen = 0; - int res = DesfireExchangeEx(true, ctx, MFDES_SELECT_APPLICATION, data, (aid2 == NULL) ? 3 : 6, resp, &resplen, true); - if (res == PM3_SUCCESS && resplen != 0) - return PM3_ECARDEXCHANGE; + uint8_t respcode = 0; + + int res = DesfireExchangeEx(true, ctx, MFDES_SELECT_APPLICATION, data, (aid2 == NULL) ? 3 : 6, &respcode, resp, &resplen, true); + if (res == PM3_SUCCESS) { + if (resplen != 0) + return PM3_ECARDEXCHANGE; + + // select operation fail + if (respcode != MFDES_S_OPERATION_OK) + return PM3_EAPDU_FAIL; + + return PM3_SUCCESS; + } return res; } int DesfireSelectAIDHex(DesfireContext *ctx, uint32_t aid1, bool select_two, uint32_t aid2) { uint8_t data[6] = {0}; - // TODO !!!! + data[0] = aid1 & 0xff; data[1] = (aid1 >> 8) & 0xff; data[2] = (aid1 >> 16) & 0xff; diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index 12acb7e8c..fa212174c 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -68,8 +68,8 @@ void DesfireSetKey(DesfireContext *ctx, uint8_t keyNum, enum DESFIRE_CRYPTOALGO int DesfireSelectAID(DesfireContext *ctx, uint8_t *aid1, uint8_t *aid2); int DesfireSelectAIDHex(DesfireContext *ctx, uint32_t aid1, bool select_two, uint32_t aid2); -int DesfireExchange(DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen); -int DesfireExchangeEx(bool activate_field, DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen, bool enable_chaining); +int DesfireExchange(DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *respcode, uint8_t *resp, size_t *resplen); +int DesfireExchangeEx(bool activate_field, DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *respcode, uint8_t *resp, size_t *resplen, bool enable_chaining); #endif // __DESFIRECORE_H