diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 5065f0ea2..b8134f208 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -443,6 +443,7 @@ int desfire_print_signature(uint8_t *uid, uint8_t uidlen, uint8_t *signature, si PrintAndLogEx(DEBUG, "UID is NULL"); return PM3_EINVARG; } + if (signature == NULL) { PrintAndLogEx(DEBUG, "SIGNATURE is NULL"); return PM3_EINVARG; @@ -497,8 +498,9 @@ static int CmdDesGetSessionParameters(CLIParserContext *ctx, DesfireContext_t *d memcpy(kdfInput, defaultKdfInput, defaultKdfInputLen); int commmode = defaultCommMode; - if (defcommmode != DCMNone) + if (defcommmode != DCMNone) { commmode = defcommmode; + } int commset = defaultCommSet; int secchann = defaultSecureChannel; @@ -560,7 +562,6 @@ static int CmdDesGetSessionParameters(CLIParserContext *ctx, DesfireContext_t *d } if (schannid) { - if (CLIGetOptionList(arg_get_str(ctx, schannid), DesfireSecureChannelOpts, &secchann)) return PM3_ESOFT; } @@ -570,10 +571,14 @@ static int CmdDesGetSessionParameters(CLIParserContext *ctx, DesfireContext_t *d uint8_t dfname_data[16] = {0}; int dfname_len = 0; if (CLIParamHexToBuf(arg_get_str(ctx, dfnameid), dfname_data, sizeof(dfname_data), &dfname_len) == 0 && dfname_len > 0) { + if (dfname_len <= 16) { + DesfireSetDFName(dctx, dfname_data, dfname_len); - if (selectway) + if (selectway) { *selectway = ISWDFName; + } + } else { PrintAndLogEx(ERR, "DF name length must be between 1-16 bytes, got %d", dfname_len); return PM3_EINVARG; @@ -583,31 +588,37 @@ static int CmdDesGetSessionParameters(CLIParserContext *ctx, DesfireContext_t *d if (appid && id) { *id = 0x000000; - if (CLIGetUint32Hex(ctx, appid, 0x000000, id, NULL, 3, "AID must have 3 bytes length")) + if (CLIGetUint32Hex(ctx, appid, 0x000000, id, NULL, 3, "AID must have 3 bytes length")) { return PM3_EINVARG; - if (selectway) + } + + if (selectway) { *selectway = ISW6bAID; } + } if (appisoid && id) { uint32_t xisoid = 0x0000; bool isoidpresent = false; - if (CLIGetUint32Hex(ctx, appisoid, 0x0000, &xisoid, &isoidpresent, 2, "Application ISO ID (for EF) must have 2 bytes length")) + if (CLIGetUint32Hex(ctx, appisoid, 0x0000, &xisoid, &isoidpresent, 2, "Application ISO ID (for EF) must have 2 bytes length")) { return PM3_EINVARG; + } if (isoidpresent) { *id = xisoid & 0xffff; - if (selectway) + if (selectway) { *selectway = ISWIsoID; } } + } DesfireSetKey(dctx, keynum, algores, key); DesfireSetKdf(dctx, kdfAlgo, kdfInput, kdfInputLen); DesfireSetCommandSet(dctx, commset); DesfireSetCommMode(dctx, commmode); - if (securechannel) + if (securechannel) { *securechannel = secchann; + } return PM3_SUCCESS; } @@ -642,6 +653,10 @@ static int CmdHF14ADesDefault(const char *Cmd) { CLIParserFree(ctx); + // clear out select DF Name length and array when resetting to default the desfire context + dctx.selectedDFNameLen = 0; + memset(dctx.selectedDFName, 0, sizeof(dctx.selectedDFName)); + defaultKeyNum = dctx.keyNum; defaultAlgoId = dctx.keyType; memcpy(defaultKey, dctx.key, DESFIRE_MAX_KEY_SIZE); @@ -2299,10 +2314,11 @@ static int CmdHF14ADesAuth(const char *Cmd) { if (dctx.selectedDFNameLen > 0) { PrintAndLogEx(SUCCESS, "DF selected and authenticated " _GREEN_("successfully")); - } else if (DesfireMFSelected(selectway, id)) + } else if (DesfireMFSelected(selectway, id)) { PrintAndLogEx(SUCCESS, "PICC selected and authenticated " _GREEN_("succesfully")); - else + } else { PrintAndLogEx(SUCCESS, "Application " _CYAN_("%s") " selected and authenticated " _GREEN_("succesfully"), DesfireWayIDStr(selectway, id)); + } PrintAndLogEx(SUCCESS, _CYAN_("Context: ")); DesfirePrintContext(&dctx); diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 9d6fc5084..1d1ad89cb 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -1096,8 +1096,9 @@ int DesfireSelectAndAuthenticate(DesfireContext_t *dctx, DesfireSecureChannel se } int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel secureChannel, DesfireISOSelectWay way, uint32_t id, bool selectfile, uint16_t isofileid, bool noauth, bool verbose) { - if (verbose) + if (verbose) { DesfirePrintContext(dctx); + } int res = 0; @@ -1106,11 +1107,13 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s // Select DF by name using ISO7816 SELECT uint8_t resp[250] = {0}; size_t resplen = 0; + res = DesfireISOSelect(dctx, ISSDFName, dctx->selectedDFName, dctx->selectedDFNameLen, resp, &resplen); if (res != PM3_SUCCESS) { PrintAndLogEx(ERR, "Desfire DF name select " _RED_("error")); return 200; } + if (verbose) { PrintAndLogEx(INFO, "DF %s is " _GREEN_("selected"), sprint_hex(dctx->selectedDFName, dctx->selectedDFNameLen)); } @@ -1119,27 +1122,33 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s if (way == ISW6bAID && id != 0x000000) { if (dctx->cmdSet == DCCISO) { dctx->cmdSet = DCCNativeISO; - if (verbose) + if (verbose) { PrintAndLogEx(INFO, "Select via " _CYAN_("native iso wrapping") " interface"); + } res = DesfireSelectAIDHex(dctx, id, false, 0); if (res != PM3_SUCCESS) { PrintAndLogEx(ERR, "Desfire select " _RED_("error")); return 200; } - if (verbose) + + if (verbose) { PrintAndLogEx(INFO, "App %06x via native iso channel is " _GREEN_("selected"), id); + } dctx->cmdSet = DCCISO; + } else { res = DesfireSelectEx(dctx, false, way, id, NULL); if (res != PM3_SUCCESS) { PrintAndLogEx(ERR, "Desfire %s select " _RED_("error"), DesfireSelectWayToStr(way)); return 202; } - if (verbose) + if (verbose) { PrintAndLogEx(INFO, "%s is " _GREEN_("selected"), DesfireWayIDStr(way, id)); } + } + } else if (way == ISWIsoID && id != 0x0000) { // Also select by ISO ID if specified res = DesfireSelectEx(dctx, false, way, id, NULL); @@ -1147,31 +1156,38 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s PrintAndLogEx(ERR, "Desfire %s select " _RED_("error"), DesfireSelectWayToStr(way)); return 202; } - if (verbose) + + if (verbose) { PrintAndLogEx(INFO, "%s is " _GREEN_("selected"), DesfireWayIDStr(way, id)); + } } } else if (way == ISW6bAID && dctx->cmdSet == DCCISO) { dctx->cmdSet = DCCNativeISO; - if (verbose) + if (verbose) { PrintAndLogEx(INFO, "Select via " _CYAN_("native iso wrapping") " interface"); + } res = DesfireSelectAIDHex(dctx, id, false, 0); if (res != PM3_SUCCESS) { PrintAndLogEx(ERR, "Desfire select " _RED_("error")); return 200; } - if (verbose) + + if (verbose) { PrintAndLogEx(INFO, "App %06x via native iso channel is " _GREEN_("selected"), id); + } dctx->cmdSet = DCCISO; + } else { res = DesfireSelectEx(dctx, true, way, id, NULL); if (res != PM3_SUCCESS) { PrintAndLogEx(ERR, "Desfire %s select " _RED_("error"), DesfireSelectWayToStr(way)); return 202; } - if (verbose) + if (verbose) { PrintAndLogEx(INFO, "%s is " _GREEN_("selected"), DesfireWayIDStr(way, id)); + } } if (selectfile) { @@ -1186,7 +1202,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s } } - if (!noauth) { + if (noauth == false) { res = DesfireAuthenticate(dctx, secureChannel, verbose); if (res != PM3_SUCCESS) { PrintAndLogEx(ERR, "Desfire authenticate " _RED_("error") ". Result: [%d] %s", res, DesfireAuthErrorToStr(res)); diff --git a/client/src/mifare/desfirecrypto.c b/client/src/mifare/desfirecrypto.c index b0097aab7..4135a1f72 100644 --- a/client/src/mifare/desfirecrypto.c +++ b/client/src/mifare/desfirecrypto.c @@ -109,7 +109,7 @@ void DesfireSetDFName(DesfireContext_t *ctx, uint8_t *dfname, uint8_t dfnameLen) ctx->selectedDFNameLen = 0; memset(ctx->selectedDFName, 0, sizeof(ctx->selectedDFName)); - if (dfname && dfnameLen > 0 && dfnameLen <= 16) { + if (dfname && dfnameLen && dfnameLen <= 16) { ctx->selectedDFNameLen = dfnameLen; memcpy(ctx->selectedDFName, dfname, dfnameLen); }