From e03501bc8606a4a8f828db078fe50f8ebb55cc77 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Mon, 12 Jul 2021 22:38:25 +0300 Subject: [PATCH 01/10] formatpicc command --- client/src/mifare/desfirecore.c | 12 ++++++++++++ client/src/mifare/desfirecore.h | 1 + 2 files changed, 13 insertions(+) diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index e797d78b0..778d48d9d 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -901,6 +901,18 @@ int DesfireAuthenticate(DesfireContext *dctx, DesfireSecureChannel secureChannel return PM3_SUCCESS; } +int DesfireFormatPICC(DesfireContext *dctx) { + uint8_t respcode = 0xff; + uint8_t resp[257] = {0}; + size_t resplen = 0; + int res = DesfireExchange(dctx, MFDES_GET_UID, NULL, 0, &respcode, resp, &resplen); + if (res != PM3_SUCCESS) + return res; + if (respcode != MFDES_S_OPERATION_OK || resplen != 0) + return PM3_EAPDU_FAIL; + return PM3_SUCCESS; +} + int DesfireGetUID(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { uint8_t respcode = 0xff; int res = DesfireExchange(dctx, MFDES_GET_UID, NULL, 0, &respcode, resp, resplen); diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index 4fd55909c..0da4fcbfe 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -40,6 +40,7 @@ int DesfireSelectAIDHex(DesfireContext *ctx, uint32_t aid1, bool select_two, uin int DesfireSelectAndAuthenticate(DesfireContext *dctx, DesfireSecureChannel secureChannel, uint32_t aid, bool verbose); int DesfireAuthenticate(DesfireContext *dctx, DesfireSecureChannel secureChannel); +int DesfireFormatPICC(DesfireContext *dctx); int DesfireGetUID(DesfireContext *dctx, uint8_t *resp, size_t *resplen); int DesfireGetAIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); int DesfireGetDFList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); From 9c63d09179f6fa68621ffade55ee3b50b29fac4b Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Mon, 12 Jul 2021 22:40:22 +0300 Subject: [PATCH 02/10] cmdcode --- client/src/mifare/desfirecore.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 778d48d9d..608059882 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -905,7 +905,7 @@ int DesfireFormatPICC(DesfireContext *dctx) { uint8_t respcode = 0xff; uint8_t resp[257] = {0}; size_t resplen = 0; - int res = DesfireExchange(dctx, MFDES_GET_UID, NULL, 0, &respcode, resp, &resplen); + int res = DesfireExchange(dctx, MFDES_FORMAT_PICC, NULL, 0, &respcode, resp, &resplen); if (res != PM3_SUCCESS) return res; if (respcode != MFDES_S_OPERATION_OK || resplen != 0) From 64422239378f5f4af0f0a7e86b47f961e2a29d72 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Mon, 12 Jul 2021 22:46:55 +0300 Subject: [PATCH 03/10] getfreemem --- client/src/mifare/desfirecore.c | 14 ++++++++++++++ client/src/mifare/desfirecore.h | 1 + 2 files changed, 15 insertions(+) diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 608059882..2e0654c30 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -913,6 +913,20 @@ int DesfireFormatPICC(DesfireContext *dctx) { return PM3_SUCCESS; } +int DesfireGetFreeMem(DesfireContext *dctx, uint32_t *freemem) { + *freemem = 0; + uint8_t respcode = 0xff; + uint8_t resp[257] = {0}; + size_t resplen = 0; + int res = DesfireExchange(dctx, MFDES_GET_FREE_MEMORY, NULL, 0, &respcode, resp, &resplen); + if (res != PM3_SUCCESS) + return res; + if (respcode != MFDES_S_OPERATION_OK || resplen != 3) + return PM3_EAPDU_FAIL; + *freemem = DesfireAIDByteToUint(resp); + return PM3_SUCCESS; +} + int DesfireGetUID(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { uint8_t respcode = 0xff; int res = DesfireExchange(dctx, MFDES_GET_UID, NULL, 0, &respcode, resp, resplen); diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index 0da4fcbfe..acc7bd7af 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -41,6 +41,7 @@ int DesfireSelectAndAuthenticate(DesfireContext *dctx, DesfireSecureChannel secu int DesfireAuthenticate(DesfireContext *dctx, DesfireSecureChannel secureChannel); int DesfireFormatPICC(DesfireContext *dctx); +int DesfireGetFreeMem(DesfireContext *dctx, uint32_t *freemem); int DesfireGetUID(DesfireContext *dctx, uint8_t *resp, size_t *resplen); int DesfireGetAIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); int DesfireGetDFList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); From cf96dc3acba32c87a331cdc4557307d40b78936d Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Mon, 12 Jul 2021 23:33:13 +0300 Subject: [PATCH 04/10] get free mem an format icc commands --- client/src/cmdhfmfdes.c | 131 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 128 insertions(+), 3 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 64ce354b3..d6d44b534 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -3442,7 +3442,7 @@ static int CmdHF14ADesCreateValueFile(const char *Cmd) { DropFieldDesfire(); return res; } - +/* static int CmdHF14ADesFormatPICC(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf mfdes formatpicc", @@ -3467,7 +3467,7 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { } DropFieldDesfire(); return res; -} +}*/ static int CmdHF14ADesInfo(const char *Cmd) { CLIParserContext *ctx; @@ -5224,6 +5224,130 @@ static int CmdHF14ADesGetUID(const char *Cmd) { return PM3_SUCCESS; } +static int CmdHF14ADesFormatPICC(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "hf mfdes formatpicc", + "Format card. Can be done only if enabled in the configuration. Master key needs to be provided. ", + "hf mfdes formatpicc -> execute with default factory setup"); + + void *argtable[] = { + arg_param_begin, + arg_lit0("a", "apdu", "show APDU requests and responses"), + arg_lit0("v", "verbose", "show technical data"), + arg_int0("n", "keyno", "", "Key number"), + arg_str0("t", "algo", "", "Crypt algo: DES, 2TDEA, 3TDEA, AES"), + arg_str0("k", "key", "", "Key for authenticate (HEX 8(DES), 16(2TDEA or AES) or 24(3TDEA) bytes)"), + arg_str0("f", "kdf", "", "Key Derivation Function (KDF): None, AN10922, Gallagher"), + arg_str0("i", "kdfi", "", "KDF input (HEX 1-31 bytes)"), + arg_str0("m", "cmode", "", "Communicaton mode: plain/mac/encrypt"), + arg_str0("c", "ccset", "", "Communicaton command set: native/niso/iso"), + arg_str0("s", "schann", "", "Secure channel: d40/ev1/ev2"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + + bool APDULogging = arg_get_lit(ctx, 1); + bool verbose = arg_get_lit(ctx, 2); + + DesfireContext dctx; + int securechann = defaultSecureChannel; + int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 0, &securechann, DCMMACed, NULL); + if (res) { + CLIParserFree(ctx); + return res; + } + + SetAPDULogging(APDULogging); + CLIParserFree(ctx); + + res = DesfireSelectAndAuthenticate(&dctx, securechann, 0x000000, verbose); + if (res != PM3_SUCCESS) { + DropField(); + return res; + } + + res = DesfireFormatPICC(&dctx); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire DesfireFormatPICC command " _RED_("error") ". Result: %d", res); + DropField(); + return PM3_ESOFT; + } + + PrintAndLogEx(SUCCESS, "Desfire format: " _GREEN_("done")); + + DropField(); + return PM3_SUCCESS; +} + +static int CmdHF14ADesGetFreeMem(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "hf mfdes getfreemem", + "Get card's free memory. Can be doe with ot without authentication. Master key may be provided. ", + "hf mfdes getfreemem -> execute with default factory setup"); + + void *argtable[] = { + arg_param_begin, + arg_lit0("a", "apdu", "show APDU requests and responses"), + arg_lit0("v", "verbose", "show technical data"), + arg_int0("n", "keyno", "", "Key number"), + arg_str0("t", "algo", "", "Crypt algo: DES, 2TDEA, 3TDEA, AES"), + arg_str0("k", "key", "", "Key for authenticate (HEX 8(DES), 16(2TDEA or AES) or 24(3TDEA) bytes)"), + arg_str0("f", "kdf", "", "Key Derivation Function (KDF): None, AN10922, Gallagher"), + arg_str0("i", "kdfi", "", "KDF input (HEX 1-31 bytes)"), + arg_str0("m", "cmode", "", "Communicaton mode: plain/mac/encrypt"), + arg_str0("c", "ccset", "", "Communicaton command set: native/niso/iso"), + arg_str0("s", "schann", "", "Secure channel: d40/ev1/ev2"), + arg_lit0(NULL, "no-auth", "execute without authentication"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + + bool APDULogging = arg_get_lit(ctx, 1); + bool verbose = arg_get_lit(ctx, 2); + + bool noauth = arg_get_lit(ctx, 11); + + DesfireContext dctx; + int securechann = defaultSecureChannel; + int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 0, &securechann, (noauth) ? DCMPlain : DCMMACed, NULL); + if (res) { + CLIParserFree(ctx); + return res; + } + + SetAPDULogging(APDULogging); + CLIParserFree(ctx); + + if (noauth) { + res = DesfireSelectAIDHex(&dctx, 0x000000, false, 0); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire select " _RED_("error") "."); + DropField(); + return res; + } + } else { + res = DesfireSelectAndAuthenticate(&dctx, securechann, 0x000000, verbose); + if (res != PM3_SUCCESS) { + DropField(); + return res; + } + } + + uint32_t freemem = 0; + + res = DesfireGetFreeMem(&dctx, &freemem); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire DesfireGetFreeMem command " _RED_("error") ". Result: %d", res); + DropField(); + return PM3_ESOFT; + } + + PrintAndLogEx(SUCCESS, "Free memory 0x%06x, %d bytes", freemem, freemem); + + DropField(); + return PM3_SUCCESS; +} + static int CmdHF14ADesChKeySettings(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf mfdes chkeysetings", @@ -5636,7 +5760,8 @@ static command_t CommandTable[] = { {"auth", CmdHF14ADesAuth, IfPm3Iso14443a, "Tries a MIFARE DesFire Authentication"}, {"chk", CmdHF14aDesChk, IfPm3Iso14443a, "Check keys"}, {"enum", CmdHF14ADesEnumApplications, IfPm3Iso14443a, "Tries enumerate all applications"}, - {"formatpicc", CmdHF14ADesFormatPICC, IfPm3Iso14443a, "Format PICC"}, + {"formatpicc", CmdHF14ADesFormatPICC, IfPm3Iso14443a, "[new]Format PICC"}, + {"getfreemem", CmdHF14ADesGetFreeMem, IfPm3Iso14443a, "[new]Get free memory size"}, {"getuid", CmdHF14ADesGetUID, IfPm3Iso14443a, "[new]Get uid from card"}, {"info", CmdHF14ADesInfo, IfPm3Iso14443a, "Tag information"}, {"list", CmdHF14ADesList, AlwaysAvailable, "List DESFire (ISO 14443A) history"}, From 12969cf728f690d51e3e1a67e73b0c341942392d Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Mon, 12 Jul 2021 23:36:25 +0300 Subject: [PATCH 05/10] text --- client/src/cmdhfmfdes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index d6d44b534..5a9e54f83 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -5342,7 +5342,7 @@ static int CmdHF14ADesGetFreeMem(const char *Cmd) { return PM3_ESOFT; } - PrintAndLogEx(SUCCESS, "Free memory 0x%06x, %d bytes", freemem, freemem); + PrintAndLogEx(SUCCESS, "Free memory [0x%06x] %d bytes", freemem, freemem); DropField(); return PM3_SUCCESS; From 8e68dafa3aad0e5c89141f27235df817e6a96c4b Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Mon, 12 Jul 2021 23:53:38 +0300 Subject: [PATCH 06/10] freemem conditions --- client/src/mifare/desfiresecurechan.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client/src/mifare/desfiresecurechan.c b/client/src/mifare/desfiresecurechan.c index c60ef1bb6..2e49ce5ac 100644 --- a/client/src/mifare/desfiresecurechan.c +++ b/client/src/mifare/desfiresecurechan.c @@ -30,6 +30,7 @@ AllowedChannelModesS AllowedChannelModes[] = { {MFDES_GET_DF_NAMES, DACd40, DCCNative, DCMPlain}, {MFDES_GET_KEY_SETTINGS, DACd40, DCCNative, DCMPlain}, {MFDES_GET_KEY_VERSION, DACd40, DCCNative, DCMPlain}, + {MFDES_GET_FREE_MEMORY, DACd40, DCCNative, DCMPlain}, {MFDES_READ_DATA, DACd40, DCCNative, DCMMACed}, {MFDES_WRITE_DATA, DACd40, DCCNative, DCMMACed}, @@ -52,6 +53,7 @@ AllowedChannelModesS AllowedChannelModes[] = { {MFDES_WRITE_DATA, DACd40, DCCNative, DCMEncrypted}, {MFDES_GET_KEY_VERSION, DACEV1, DCCNative, DCMPlain}, + {MFDES_GET_FREE_MEMORY, DACEV1, DCCNative, DCMPlain}, {MFDES_CREATE_APPLICATION, DACEV1, DCCNative, DCMMACed}, {MFDES_DELETE_APPLICATION, DACEV1, DCCNative, DCMMACed}, From d4646ce6d6956978722e2f4e72c60c8682e3831f Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Mon, 12 Jul 2021 23:53:57 +0300 Subject: [PATCH 07/10] add aid to formatpicc --- client/src/cmdhfmfdes.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 5a9e54f83..a3e02c779 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -5242,6 +5242,7 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { arg_str0("m", "cmode", "", "Communicaton mode: plain/mac/encrypt"), arg_str0("c", "ccset", "", "Communicaton command set: native/niso/iso"), arg_str0("s", "schann", "", "Secure channel: d40/ev1/ev2"), + arg_str0(NULL, "aid", "", "Application ID of delegated application (3 hex bytes, big endian)"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -5251,7 +5252,8 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { DesfireContext dctx; int securechann = defaultSecureChannel; - int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 0, &securechann, DCMMACed, NULL); + uint32_t appid = 0x000000; + int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 11, &securechann, DCMMACed, &appid); if (res) { CLIParserFree(ctx); return res; @@ -5260,7 +5262,7 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { SetAPDULogging(APDULogging); CLIParserFree(ctx); - res = DesfireSelectAndAuthenticate(&dctx, securechann, 0x000000, verbose); + res = DesfireSelectAndAuthenticate(&dctx, securechann, appid, verbose); if (res != PM3_SUCCESS) { DropField(); return res; @@ -5761,7 +5763,7 @@ static command_t CommandTable[] = { {"chk", CmdHF14aDesChk, IfPm3Iso14443a, "Check keys"}, {"enum", CmdHF14ADesEnumApplications, IfPm3Iso14443a, "Tries enumerate all applications"}, {"formatpicc", CmdHF14ADesFormatPICC, IfPm3Iso14443a, "[new]Format PICC"}, - {"getfreemem", CmdHF14ADesGetFreeMem, IfPm3Iso14443a, "[new]Get free memory size"}, + {"freemem", CmdHF14ADesGetFreeMem, IfPm3Iso14443a, "[new]Get free memory size"}, {"getuid", CmdHF14ADesGetUID, IfPm3Iso14443a, "[new]Get uid from card"}, {"info", CmdHF14ADesInfo, IfPm3Iso14443a, "Tag information"}, {"list", CmdHF14ADesList, AlwaysAvailable, "List DESFire (ISO 14443A) history"}, From afbe5deda216e39c111dfa86f97c9c15c20ee729 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Tue, 13 Jul 2021 00:59:07 +0300 Subject: [PATCH 08/10] commands refactoring --- client/src/mifare/desfirecore.c | 91 +++++++++++++++------------------ 1 file changed, 42 insertions(+), 49 deletions(-) diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 2e0654c30..5a8a06745 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -901,60 +901,65 @@ int DesfireAuthenticate(DesfireContext *dctx, DesfireSecureChannel secureChannel return PM3_SUCCESS; } -int DesfireFormatPICC(DesfireContext *dctx) { +static int DesfireCommand(DesfireContext *dctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen, int checklength) { + if (resplen) + *resplen = 0; + uint8_t respcode = 0xff; - uint8_t resp[257] = {0}; - size_t resplen = 0; - int res = DesfireExchange(dctx, MFDES_FORMAT_PICC, NULL, 0, &respcode, resp, &resplen); + uint8_t xresp[257] = {0}; + size_t xresplen = 0; + int res = DesfireExchange(dctx, cmd, data, datalen, &respcode, xresp, &xresplen); if (res != PM3_SUCCESS) return res; - if (respcode != MFDES_S_OPERATION_OK || resplen != 0) + if (respcode != MFDES_S_OPERATION_OK) return PM3_EAPDU_FAIL; + if (checklength >= 0 && xresplen != checklength) + return PM3_EAPDU_FAIL; + + if (resplen) + *resplen = xresplen; + if (resp) + memcpy(resp, xresp, xresplen); return PM3_SUCCESS; } +static int DesfireCommandNoData(DesfireContext *dctx, uint8_t cmd) { + return DesfireCommand(dctx, cmd, NULL, 0, NULL, NULL, 0); +} +/* +static int DesfireCommandTxData(DesfireContext *dctx, uint8_t cmd, uint8_t *data, size_t datalen, int checklength) { + return DesfireCommand(dctx, cmd, data, 0, datalen, NULL, checklength); +} +*/ +static int DesfireCommandRxData(DesfireContext *dctx, uint8_t cmd, uint8_t *resp, size_t *resplen, int checklength) { + return DesfireCommand(dctx, cmd, NULL, 0, resp, resplen, checklength); +} + +int DesfireFormatPICC(DesfireContext *dctx) { + return DesfireCommandNoData(dctx, MFDES_FORMAT_PICC); +} + int DesfireGetFreeMem(DesfireContext *dctx, uint32_t *freemem) { *freemem = 0; - uint8_t respcode = 0xff; + uint8_t resp[257] = {0}; - size_t resplen = 0; - int res = DesfireExchange(dctx, MFDES_GET_FREE_MEMORY, NULL, 0, &respcode, resp, &resplen); - if (res != PM3_SUCCESS) - return res; - if (respcode != MFDES_S_OPERATION_OK || resplen != 3) - return PM3_EAPDU_FAIL; - *freemem = DesfireAIDByteToUint(resp); - return PM3_SUCCESS; + size_t resplen = 0; + int res = DesfireCommandRxData(dctx, MFDES_GET_FREE_MEMORY, resp, &resplen, 3); + if (res == PM3_SUCCESS) + *freemem = DesfireAIDByteToUint(resp); + return res; } int DesfireGetUID(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { - uint8_t respcode = 0xff; - int res = DesfireExchange(dctx, MFDES_GET_UID, NULL, 0, &respcode, resp, resplen); - if (res != PM3_SUCCESS) - return res; - if (respcode != MFDES_S_OPERATION_OK) - return PM3_EAPDU_FAIL; - return PM3_SUCCESS; + return DesfireCommandRxData(dctx, MFDES_GET_UID, resp, resplen, -1); } int DesfireGetAIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { - uint8_t respcode = 0xff; - int res = DesfireExchange(dctx, MFDES_GET_APPLICATION_IDS, NULL, 0, &respcode, resp, resplen); - if (res != PM3_SUCCESS) - return res; - if (respcode != MFDES_S_OPERATION_OK) - return PM3_EAPDU_FAIL; - return PM3_SUCCESS; + return DesfireCommandRxData(dctx, MFDES_GET_APPLICATION_IDS, resp, resplen, -1); } int DesfireGetDFList(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { - uint8_t respcode = 0xff; - int res = DesfireExchangeEx(false, dctx, MFDES_GET_DF_NAMES, NULL, 0, &respcode, resp, resplen, true, 24); - if (res != PM3_SUCCESS) - return res; - if (respcode != MFDES_S_OPERATION_OK) - return PM3_EAPDU_FAIL; - return PM3_SUCCESS; + return DesfireCommandRxData(dctx, MFDES_GET_DF_NAMES, resp, resplen, -1); } int DesfireCreateApplication(DesfireContext *dctx, uint8_t *appdata, size_t appdatalen) { @@ -984,23 +989,11 @@ int DesfireDeleteApplication(DesfireContext *dctx, uint32_t aid) { } int DesfireGetKeySettings(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { - uint8_t respcode = 0xff; - int res = DesfireExchange(dctx, MFDES_GET_KEY_SETTINGS, NULL, 0, &respcode, resp, resplen); - if (res != PM3_SUCCESS) - return res; - if (respcode != MFDES_S_OPERATION_OK) - return PM3_EAPDU_FAIL; - return PM3_SUCCESS; + return DesfireCommandRxData(dctx, MFDES_GET_KEY_SETTINGS, resp, resplen, -1); } int DesfireGetKeyVersion(DesfireContext *dctx, uint8_t *data, size_t len, uint8_t *resp, size_t *resplen) { - uint8_t respcode = 0xff; - int res = DesfireExchange(dctx, MFDES_GET_KEY_VERSION, data, len, &respcode, resp, resplen); - if (res != PM3_SUCCESS) - return res; - if (respcode != MFDES_S_OPERATION_OK) - return PM3_EAPDU_FAIL; - return PM3_SUCCESS; + return DesfireCommandRxData(dctx, MFDES_GET_KEY_VERSION, resp, resplen, -1); } int DesfireChangeKeySettings(DesfireContext *dctx, uint8_t *data, size_t len) { From 89450c2feed180bab37a605a0ec48a6e46007d1b Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Tue, 13 Jul 2021 11:56:12 +0300 Subject: [PATCH 09/10] commands refactoring --- client/src/mifare/desfirecore.c | 50 ++++++++++----------------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 5a8a06745..f1328a568 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -901,14 +901,14 @@ int DesfireAuthenticate(DesfireContext *dctx, DesfireSecureChannel secureChannel return PM3_SUCCESS; } -static int DesfireCommand(DesfireContext *dctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen, int checklength) { +static int DesfireCommandEx(DesfireContext *dctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen, int checklength, size_t splitbysize) { if (resplen) *resplen = 0; uint8_t respcode = 0xff; uint8_t xresp[257] = {0}; size_t xresplen = 0; - int res = DesfireExchange(dctx, cmd, data, datalen, &respcode, xresp, &xresplen); + int res = DesfireExchangeEx(false, dctx, cmd, data, datalen, &respcode, xresp, &xresplen, true, splitbysize); if (res != PM3_SUCCESS) return res; if (respcode != MFDES_S_OPERATION_OK) @@ -923,14 +923,18 @@ static int DesfireCommand(DesfireContext *dctx, uint8_t cmd, uint8_t *data, size return PM3_SUCCESS; } +static int DesfireCommand(DesfireContext *dctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen, int checklength) { + return DesfireCommandEx(dctx, cmd, data, datalen, resp, resplen, checklength, 0); +} + static int DesfireCommandNoData(DesfireContext *dctx, uint8_t cmd) { return DesfireCommand(dctx, cmd, NULL, 0, NULL, NULL, 0); } -/* -static int DesfireCommandTxData(DesfireContext *dctx, uint8_t cmd, uint8_t *data, size_t datalen, int checklength) { - return DesfireCommand(dctx, cmd, data, 0, datalen, NULL, checklength); + +static int DesfireCommandTxData(DesfireContext *dctx, uint8_t cmd, uint8_t *data, size_t datalen) { + return DesfireCommand(dctx, cmd, data, datalen, NULL, NULL, 0); } -*/ + static int DesfireCommandRxData(DesfireContext *dctx, uint8_t cmd, uint8_t *resp, size_t *resplen, int checklength) { return DesfireCommand(dctx, cmd, NULL, 0, resp, resplen, checklength); } @@ -959,33 +963,17 @@ int DesfireGetAIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { } int DesfireGetDFList(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { - return DesfireCommandRxData(dctx, MFDES_GET_DF_NAMES, resp, resplen, -1); + return DesfireCommandEx(dctx, MFDES_GET_DF_NAMES, NULL, 0, resp, resplen, -1, 24); } int DesfireCreateApplication(DesfireContext *dctx, uint8_t *appdata, size_t appdatalen) { - uint8_t respcode = 0xff; - uint8_t resp[257] = {0}; - size_t resplen = 0; - int res = DesfireExchangeEx(false, dctx, MFDES_CREATE_APPLICATION, appdata, appdatalen, &respcode, resp, &resplen, true, 0); - if (res != PM3_SUCCESS) - return res; - if (respcode != MFDES_S_OPERATION_OK || resplen != 0) - return PM3_EAPDU_FAIL; - return PM3_SUCCESS; + return DesfireCommandTxData(dctx, MFDES_CREATE_APPLICATION, appdata, appdatalen); } int DesfireDeleteApplication(DesfireContext *dctx, uint32_t aid) { - uint8_t respcode = 0xff; uint8_t data[3] = {0}; DesfireAIDUintToByte(aid, data); - uint8_t resp[257] = {0}; - size_t resplen = 0; - int res = DesfireExchangeEx(false, dctx, MFDES_DELETE_APPLICATION, data, sizeof(data), &respcode, resp, &resplen, true, 0); - if (res != PM3_SUCCESS) - return res; - if (respcode != MFDES_S_OPERATION_OK || resplen != 0) - return PM3_EAPDU_FAIL; - return PM3_SUCCESS; + return DesfireCommandTxData(dctx, MFDES_DELETE_APPLICATION, data, sizeof(data)); } int DesfireGetKeySettings(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { @@ -993,19 +981,11 @@ int DesfireGetKeySettings(DesfireContext *dctx, uint8_t *resp, size_t *resplen) } int DesfireGetKeyVersion(DesfireContext *dctx, uint8_t *data, size_t len, uint8_t *resp, size_t *resplen) { - return DesfireCommandRxData(dctx, MFDES_GET_KEY_VERSION, resp, resplen, -1); + return DesfireCommand(dctx, MFDES_GET_KEY_VERSION, data, len, resp, resplen, -1); } int DesfireChangeKeySettings(DesfireContext *dctx, uint8_t *data, size_t len) { - uint8_t respcode = 0xff; - uint8_t resp[257] = {0}; - size_t resplen = 0; - int res = DesfireExchange(dctx, MFDES_CHANGE_KEY_SETTINGS, data, len, &respcode, resp, &resplen); - if (res != PM3_SUCCESS) - return res; - if (respcode != MFDES_S_OPERATION_OK || resplen != 0) - return PM3_EAPDU_FAIL; - return PM3_SUCCESS; + return DesfireCommandTxData(dctx, MFDES_CHANGE_KEY_SETTINGS, data, len); } static void PrintKeyType(uint8_t keytype) { From 3e86c15a33afd1086404c8028244b13cf8b52044 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Tue, 13 Jul 2021 12:16:30 +0300 Subject: [PATCH 10/10] cov 351017 --- client/src/cmdhfmfdes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index a3e02c779..609381ace 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -5218,7 +5218,7 @@ static int CmdHF14ADesGetUID(const char *Cmd) { return PM3_ESOFT; } - PrintAndLogEx(SUCCESS, "Desfire UID[%d]: %s", buflen, sprint_hex(buf, buflen)); + PrintAndLogEx(SUCCESS, "Desfire UID[%zu]: %s", buflen, sprint_hex(buf, buflen)); DropField(); return PM3_SUCCESS;