Fix prolematic return codes in mifare.

Parts of the code returned positive values for error codes, which
could result in the client exiting (return value 2).
This commit is contained in:
Jean-Michel Picod 2022-11-04 11:06:59 +01:00
commit 32d47cb6a4
3 changed files with 19 additions and 19 deletions

View file

@ -115,14 +115,14 @@ static int GetHFMF14AUID(uint8_t *uid, int *uidlen) {
if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)) { if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)) {
PrintAndLogEx(WARNING, "iso14443a card select failed"); PrintAndLogEx(WARNING, "iso14443a card select failed");
DropField(); DropField();
return 0; return PM3_ERFTRANS;
} }
iso14a_card_select_t card; iso14a_card_select_t card;
memcpy(&card, (iso14a_card_select_t *)resp.data.asBytes, sizeof(iso14a_card_select_t)); memcpy(&card, (iso14a_card_select_t *)resp.data.asBytes, sizeof(iso14a_card_select_t));
memcpy(uid, card.uid, card.uidlen * sizeof(uint8_t)); memcpy(uid, card.uid, card.uidlen * sizeof(uint8_t));
*uidlen = card.uidlen; *uidlen = card.uidlen;
return 1; return PM3_SUCCESS;
} }
static char *GenerateFilename(const char *prefix, const char *suffix) { static char *GenerateFilename(const char *prefix, const char *suffix) {
@ -133,8 +133,8 @@ static char *GenerateFilename(const char *prefix, const char *suffix) {
int uidlen = 0; int uidlen = 0;
char *fptr = calloc(sizeof(char) * (strlen(prefix) + strlen(suffix)) + sizeof(uid) * 2 + 1, sizeof(uint8_t)); char *fptr = calloc(sizeof(char) * (strlen(prefix) + strlen(suffix)) + sizeof(uid) * 2 + 1, sizeof(uint8_t));
GetHFMF14AUID(uid, &uidlen); int res = GetHFMF14AUID(uid, &uidlen);
if (!uidlen) { if (res != PM3_SUCCESS || !uidlen) {
PrintAndLogEx(WARNING, "No tag found."); PrintAndLogEx(WARNING, "No tag found.");
free(fptr); free(fptr);
return NULL; return NULL;
@ -234,7 +234,7 @@ static bool mfc_value(const uint8_t *d, int32_t *val) {
if (val) { if (val) {
*val = a; *val = a;
} }
return (val_checks); return val_checks;
} }
static void mf_print_block(uint8_t blockno, uint8_t *d, bool verbose) { static void mf_print_block(uint8_t blockno, uint8_t *d, bool verbose) {
if (blockno == 0) { if (blockno == 0) {
@ -788,7 +788,7 @@ static int CmdHF14AMfDump(const char *Cmd) {
uint64_t select_status = resp.oldarg[0]; uint64_t select_status = resp.oldarg[0];
if (select_status == 0) { if (select_status == 0) {
PrintAndLogEx(WARNING, "iso14443a card select failed"); PrintAndLogEx(WARNING, "iso14443a card select failed");
return select_status; return PM3_SUCCESS;
} }
// store card info // store card info
@ -4907,7 +4907,7 @@ static int CmdHF14AMfCSave(const char *Cmd) {
uint64_t select_status = resp.oldarg[0]; uint64_t select_status = resp.oldarg[0];
if (select_status == 0) { if (select_status == 0) {
PrintAndLogEx(WARNING, "iso14443a card select failed"); PrintAndLogEx(WARNING, "iso14443a card select failed");
return select_status; return PM3_SUCCESS;
} }
// store card info // store card info
@ -5058,7 +5058,7 @@ static int CmdHF14AMfCView(const char *Cmd) {
if (select_status == 0) { if (select_status == 0) {
PrintAndLogEx(WARNING, "iso14443a card select failed"); PrintAndLogEx(WARNING, "iso14443a card select failed");
return select_status; return PM3_ERFTRANS;
} }
iso14a_card_select_t card; iso14a_card_select_t card;
@ -5826,7 +5826,7 @@ int CmdHFMFNDEFFormat(const char *Cmd) {
uint64_t select_status = resp.oldarg[0]; uint64_t select_status = resp.oldarg[0];
if (select_status == 0) { if (select_status == 0) {
PrintAndLogEx(WARNING, "iso14443a card select failed"); PrintAndLogEx(WARNING, "iso14443a card select failed");
return select_status; return PM3_SUCCESS;
} }
DropField(); DropField();
@ -6834,7 +6834,7 @@ static int CmdHF14AGen4View(const char *Cmd) {
if (select_status == 0) { if (select_status == 0) {
PrintAndLogEx(WARNING, "iso14443a card select failed"); PrintAndLogEx(WARNING, "iso14443a card select failed");
return select_status; return PM3_SUCCESS;
} }
iso14a_card_select_t card; iso14a_card_select_t card;

View file

@ -317,7 +317,7 @@ int MAD1DecodeAndPrint(uint8_t *sector, bool swapmad, bool verbose, bool *haveMA
int ibs = MADInfoByteDecode(sector, swapmad, 1, verbose); int ibs = MADInfoByteDecode(sector, swapmad, 1, verbose);
if (ibs) { if (ibs > 0) {
PrintAndLogEx(SUCCESS, "Card publisher sector " _MAGENTA_("0x%02x"), ibs); PrintAndLogEx(SUCCESS, "Card publisher sector " _MAGENTA_("0x%02x"), ibs);
} else { } else {
PrintAndLogEx(WARNING, "Card publisher " _RED_("not") " present " _YELLOW_("0x%02x"), ibs); PrintAndLogEx(WARNING, "Card publisher " _RED_("not") " present " _YELLOW_("0x%02x"), ibs);
@ -360,7 +360,7 @@ int MAD2DecodeAndPrint(uint8_t *sector, bool swapmad, bool verbose) {
} }
int ibs = MADInfoByteDecode(sector, swapmad, 2, verbose); int ibs = MADInfoByteDecode(sector, swapmad, 2, verbose);
if (ibs) { if (ibs > 0) {
PrintAndLogEx(SUCCESS, "Card publisher sector " _MAGENTA_("0x%02x"), ibs); PrintAndLogEx(SUCCESS, "Card publisher sector " _MAGENTA_("0x%02x"), ibs);
} else { } else {
PrintAndLogEx(WARNING, "Card publisher " _RED_("not") " present " _YELLOW_("0x%02x"), ibs); PrintAndLogEx(WARNING, "Card publisher " _RED_("not") " present " _YELLOW_("0x%02x"), ibs);

View file

@ -198,7 +198,7 @@ int MifareAuth4(mf4Session_t *mf4session, uint8_t *keyn, uint8_t *key, bool acti
if (res) { if (res) {
if (!silentMode) PrintAndLogEx(ERR, "Exchange raw error: %d", res); if (!silentMode) PrintAndLogEx(ERR, "Exchange raw error: %d", res);
if (dropFieldIfError) DropField(); if (dropFieldIfError) DropField();
return 2; return PM3_ERFTRANS;
} }
if (verbose) if (verbose)
@ -207,19 +207,19 @@ int MifareAuth4(mf4Session_t *mf4session, uint8_t *keyn, uint8_t *key, bool acti
if (datalen < 1) { if (datalen < 1) {
if (!silentMode) PrintAndLogEx(ERR, "Card response wrong length: %d", datalen); if (!silentMode) PrintAndLogEx(ERR, "Card response wrong length: %d", datalen);
if (dropFieldIfError) DropField(); if (dropFieldIfError) DropField();
return 3; return PM3_EWRONGANSWER;
} }
if (data[0] != 0x90) { if (data[0] != 0x90) {
if (!silentMode) PrintAndLogEx(ERR, "Card response error: %02x %s", data[0], mfpGetErrorDescription(data[0])); if (!silentMode) PrintAndLogEx(ERR, "Card response error: %02x %s", data[0], mfpGetErrorDescription(data[0]));
if (dropFieldIfError) DropField(); if (dropFieldIfError) DropField();
return 3; return PM3_EWRONGANSWER;
} }
if (datalen != 19) { // code 1b + 16b + crc 2b if (datalen != 19) { // code 1b + 16b + crc 2b
if (!silentMode) PrintAndLogEx(ERR, "Card response must be 19 bytes long instead of: %d", datalen); if (!silentMode) PrintAndLogEx(ERR, "Card response must be 19 bytes long instead of: %d", datalen);
if (dropFieldIfError) DropField(); if (dropFieldIfError) DropField();
return 3; return PM3_EWRONGANSWER;
} }
aes_decode(NULL, key, &data[1], RndB, 16); aes_decode(NULL, key, &data[1], RndB, 16);
@ -242,7 +242,7 @@ int MifareAuth4(mf4Session_t *mf4session, uint8_t *keyn, uint8_t *key, bool acti
if (res) { if (res) {
if (!silentMode) PrintAndLogEx(ERR, "Exchange raw error: %d", res); if (!silentMode) PrintAndLogEx(ERR, "Exchange raw error: %d", res);
if (dropFieldIfError) DropField(); if (dropFieldIfError) DropField();
return 4; return PM3_ERFTRANS;
} }
if (verbose) if (verbose)
@ -262,7 +262,7 @@ int MifareAuth4(mf4Session_t *mf4session, uint8_t *keyn, uint8_t *key, bool acti
PrintAndLogEx(ERR, "RndA card: %s", sprint_hex(&raw[4], 16)); PrintAndLogEx(ERR, "RndA card: %s", sprint_hex(&raw[4], 16));
} }
if (dropFieldIfError) DropField(); if (dropFieldIfError) DropField();
return 5; return PM3_EWRONGANSWER;
} }
if (verbose) { if (verbose) {
@ -319,7 +319,7 @@ int MifareAuth4(mf4Session_t *mf4session, uint8_t *keyn, uint8_t *key, bool acti
if (verbose) if (verbose)
PrintAndLogEx(INFO, "Authentication OK"); PrintAndLogEx(INFO, "Authentication OK");
return 0; return PM3_SUCCESS;
} }
static int intExchangeRAW14aPlus(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) { static int intExchangeRAW14aPlus(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) {