diff --git a/CHANGELOG.md b/CHANGELOG.md index e4214034d..229ccb8d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Fixed `hf 14a info` now correctly handles NAK when receiving RATS (@iceman1001) - Fixed `hf mfu restore` to support Ultralight AES (@iceman1001) - Fixed `hf mfu info` to print UL-AES config pages again (@iceman1001) diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index e0af71124..c89130d95 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -4076,6 +4076,15 @@ int iso14443a_select_cardEx(uint8_t *uid_ptr, iso14a_card_select_t *p_card, uint return 0; } + // An ATS is TL, the bytes TL counts, and two CRC bytes on top. + // Anything shorter, like a 4 bit NAK is not ATS + if (len < 3 || resp[0] != len - 2) { + if (g_dbglevel >= DBG_INFO) { + Dbprintf("Card answered RATS with %i byte(s), not an ATS. Staying at ISO14443-3", len); + } + return 2; + } + if (p_card) { memcpy(p_card->ats, resp, sizeof(p_card->ats)); p_card->ats_len = len; diff --git a/client/src/cmdhf14a.c b/client/src/cmdhf14a.c index ba3b0bf73..dfac1011e 100644 --- a/client/src/cmdhf14a.c +++ b/client/src/cmdhf14a.c @@ -2994,9 +2994,11 @@ int infoHF14A(bool verbose, bool do_nack_test, bool do_aid_search) { return PM3_ETIMEOUT; } - memcpy(card.ats, resp.data.asBytes, rlen_2863); - card.ats_len = rlen_2863; // note: ats_len includes CRC Bytes - if (card.ats_len > 3) { + // Only a well formed ATS, TL plus the bytes TL counts plus CRC, means the + // PICC entered ISO14443-4. A 4 bit NAK to RATS is not an ATS. + if ((rlen_2863 > 3) && (resp.data.asBytes[0] == rlen_2863 - 2)) { + memcpy(card.ats, resp.data.asBytes, rlen_2863); + card.ats_len = rlen_2863; // note: ats_len includes CRC Bytes select_status = 4; } } diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index b37506de7..7d3f8b6fc 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -405,9 +405,11 @@ int mf_read_uid(uint8_t *uid, int *uidlen, int *nxptype) { return PM3_ETIMEOUT; } - memcpy(card.ats, resp.data.asBytes, rlen_398); - card.ats_len = rlen_398; // note: ats_len includes CRC Bytes - if (card.ats_len > 3) { + // Only a well formed ATS, TL plus the bytes TL counts plus CRC, means the + // PICC entered ISO14443-4. A 4 bit NAK to RATS is not an ATS. + if ((rlen_398 > 3) && (resp.data.asBytes[0] == rlen_398 - 2)) { + memcpy(card.ats, resp.data.asBytes, rlen_398); + card.ats_len = rlen_398; // note: ats_len includes CRC Bytes select_status = 4; } } @@ -3199,9 +3201,11 @@ static int CmdHF14AMfAutoPWN(const char *Cmd) { return PM3_ETIMEOUT; } - memcpy(card.ats, resp.data.asBytes, rlen_3120); - card.ats_len = rlen_3120; // note: ats_len includes CRC Bytes - if (card.ats_len > 3) { + // Only a well formed ATS, TL plus the bytes TL counts plus CRC, means the + // PICC entered ISO14443-4. A 4 bit NAK to RATS is not an ATS. + if ((rlen_3120 > 3) && (resp.data.asBytes[0] == rlen_3120 - 2)) { + memcpy(card.ats, resp.data.asBytes, rlen_3120); + card.ats_len = rlen_3120; // note: ats_len includes CRC Bytes select_status = 4; } } @@ -11458,9 +11462,11 @@ static int CmdHF14AMfInfo(const char *Cmd) { return PM3_ETIMEOUT; } - memcpy(card.ats, resp.data.asBytes, rlen_11266); - card.ats_len = rlen_11266; // note: ats_len includes CRC Bytes - if (card.ats_len > 3) { + // Only a well formed ATS, TL plus the bytes TL counts plus CRC, means the + // PICC entered ISO14443-4. A 4 bit NAK to RATS is not an ATS. + if ((rlen_11266 > 3) && (resp.data.asBytes[0] == rlen_11266 - 2)) { + memcpy(card.ats, resp.data.asBytes, rlen_11266); + card.ats_len = rlen_11266; // note: ats_len includes CRC Bytes select_status = 4; } }