diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 64ce354b3..609381ace 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; @@ -5218,7 +5218,133 @@ 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; +} + +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_str0(NULL, "aid", "", "Application ID of delegated application (3 hex bytes, big endian)"), + 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; + 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; + } + + SetAPDULogging(APDULogging); + CLIParserFree(ctx); + + res = DesfireSelectAndAuthenticate(&dctx, securechann, appid, 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; @@ -5636,7 +5762,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"}, + {"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"}, diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index e797d78b0..f1328a568 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -901,92 +901,91 @@ int DesfireAuthenticate(DesfireContext *dctx, DesfireSecureChannel secureChannel return PM3_SUCCESS; } -int DesfireGetUID(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { +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; - int res = DesfireExchange(dctx, MFDES_GET_UID, NULL, 0, &respcode, resp, resplen); + uint8_t xresp[257] = {0}; + size_t xresplen = 0; + 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) 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 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) { + 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); +} + +int DesfireFormatPICC(DesfireContext *dctx) { + return DesfireCommandNoData(dctx, MFDES_FORMAT_PICC); +} + +int DesfireGetFreeMem(DesfireContext *dctx, uint32_t *freemem) { + *freemem = 0; + + uint8_t resp[257] = {0}; + 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) { + 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 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) { - 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 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) { diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index 4fd55909c..acc7bd7af 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -40,6 +40,8 @@ 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 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); 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},