hf mfdes: stop GetDFNames from permanently disabling DESFire EV1 cards
Sending GetDFNames (0x6D) inside an authenticated session destroys DESFire EV1
silicon. When the response chains, the card answers the first 0xAF continuation
frame with 0xC1 PICC_INTEGRITY_ERROR, "PICC will be disabled", and from that
moment every command returns 0xCD PICC_DISABLED_ERROR. The ATS historical byte
changes 0x80 -> 0x90. Three cards were destroyed establishing this.
It is reached by `hf mfdes dump`, `hf mfdes lsapp` and `hf mfdes info --files`,
all of which authenticate and then call DesfireFillAppList.
HOW IT WAS ISOLATED
One variable at a time, on a card that survived all of it. Unauthenticated, the
identical frames are answered normally:
- 0x6D on a blank card
- 0x6D with an application that has no DF name (empty, per 9.4.4)
- 0x6D with one DF name, single frame, no chaining
- 0x6D chained over two and then three DF names, sent by hand
- the chain abandoned mid-way with the field dropped
- a stray 0xAF with no chain open, answered 91 1C
- the client's own chaining code with splitbysize
Authenticated, the same card died on the first continuation frame. The client
appends no MAC to 0x6D -- it is not in EV1D40TransmitMAC -- so the bytes on the
wire are `90 6D 00 00 00` then `90 AF 00 00 00` either way, byte for byte
identical to the probes that passed. Only the card's session state differs.
Ruled out along the way:
- Failed or wrong-algorithm authentication. `hf mfdes chk` has fired hundreds
of failed auths per card for years without harm.
- Abandoned authentication chains. DesfireCheckAuthCmd sends only the first
frame of an auth and then drops the field, six times per `hf mfdes info`
run; 30 of them left the test card healthy.
- Malformed commands. The only out-of-spec command in the original sequence
(UpdateRecord, 18 bytes into a 16 byte record) was refused client-side and
never reached the card.
- Chaining itself, DF names themselves, and the record count.
THE FIX
Drop the PICC session around the call and restore it afterwards.
It is the card's session that has to go, not the context. Clearing
DesfireContext_t alone would leave the card authenticated and change nothing, so
SelectApplication is used: "each SelectApplication command invalidates the
current authentication status" (M134034 9.4.5).
The session is then re-established on whatever AID was selected when we were
called, not on 000000. The issuer info path enters DesfireFillAppList with an
application already selected, and re-authenticating the PICC with that
application's key would silently fail. A context selected by DF name has no AID
to return to, so the DF name list is skipped rather than guessed at.
No configuration forces the unsafe order. Key settings bit 1 governs whether the
directory commands need authentication, and its "needs auth" branch names only
GetApplicationIDs and GetKeySettings, not GetDFNames (M134034 p.39). A card that
refuses the plain command costs the DF name column; sending it authenticated
costs the card.
WHAT THE DATASHEETS SAY
EV1 never specifies how secure messaging applies across 0xAF frames. EV3 had to
write the rule down later, ev3.pdf 7.3.2.2: "the secure messaging is applied as
if the command or response would have been sent in a single large frame. The
0xAF command and response codes are ignored for these calculations." The exact
interaction that destroys these cards is the one EV1 left unwritten.
EV2 and EV3 then deleted the whole self-disable family: ev3.pdf contains zero
occurrences of PICC_DISABLED or PICC_INTEGRITY, which M134034 defines. EV3
renames 0xEE to MEMORY_ERROR and says the part may execute a reset rather than
disable itself.
Whether the disabled state is recoverable is SILENT in every direction. The five
codes appear once each, in one status table, with no prose anywhere, and there is
no lifecycle command in DESFire to rehabilitate a PICC. Measured on a dead card,
nothing works: FormatPICC cannot authenticate (0x1A -> 91 CD) and is refused raw
(90 FC -> 91 CD); SelectApplication is refused (91 CD) even though it needs no
authentication and no bit can gate it; every ISO 7816 command including ACTIVATE
FILE and TERMINATE DF answers 65 81 "Memory failure". A malformed GetVersion
still answers 91 7E LENGTH_ERROR, so anticollision, RATS, the command parser and
length validation all still run -- only the application layer underneath is gone.
Note M075031 is the D40 datasheet, not EV1; the EV1 document is M134034.
ALSO IN THIS CHANGE, because without them the cause was invisible
Every card error collapsed to PM3_EAPDU_FAIL before reaching a caller, and both
send paths only printed the status when -a was given. That is why the first two
cards told us nothing about how they died.
- The five codes M134034 Table 11 footnotes as "not expected to appear during
normal operation" (0xC1, 0xCD, 0xA1, 0xF1, 0xEE) are now reported from both
DESFIRESendRaw and DESFIRESendApduEx whether or not -a is given. Ordinary
refusals stay at debug level, so chk and the ISO file id probes stay quiet.
- A chained 0xAF is attributed to the command it is continuing, so a failure
reads "command 0x6D frame 0xAF -> 0xC1" rather than a bare 0xAF.
- DesfireContext_t keeps lastRespCode, set where both exchange paths meet.
A command error also ends the authentication on the PICC, so the client now
drops its side of the session rather than continuing to MAC into a session the
card has thrown away. The tree already did this at one call site
(DesfireGetFileISOIDList); it is now done at the choke point every native
command passes through, plus the paths that bypass it: DesfireReadSignature,
DesfireCreateDelegatedApplication, DesfireISOSelectEx, the ISO data plane
(ReadBinary, UpdateBinary, ReadRecords, AppendRecord) and GetDelegateInfo. The
three ISO authentication wrappers are deliberately left alone, since they run
before a session exists and clearing would wipe the handshake's own state.
`hf mfdes pc` checked only the transport result and treated a card error as
success: DesfireExchangeEx returns PM3_SUCCESS with the card's status in
respcode, so PreparePC, the proximity check rounds and VerifyPC went on to parse
error responses as data. They now check respcode.
DesfireFillFileList discarded the result of DesfireFileSettingsStruct, so a file
whose settings could not be read became a zeroed entry indistinguishable from a
valid 0 byte standard data file. FileListElm_t now carries fileSettingsRead and
five consumers honour it: the ISO id mapping loop (positional, an unread file
would shift every id after it onto the wrong file), `hf mfdes chk`'s used-key
set, lsfiles, the application walk print, and `hf mfdes dump`, where settings_ok
was being set true unconditionally.
DesfireFillAppList tested a stale `res` after DesfireGetKeySettings, whose return
value was discarded; only an incidental buflen guard kept it from consuming
garbage key settings.
`hf mfdes sim` now runs through the shared ISO14443-A simulation loop as tag
type 3 rather than a duplicate 14a loop, which had got the ATQA wrong. The
DESFire logic stays in armsrc/desfiresim.c behind a small hook API.
KNOWN, NOT FIXED HERE
DesfireFillAppList reads the DF name list into uint8_t buf[250] while
DesfireCommandEx copies records * 24 bytes. The EV1 limit of 28 applications
would write 672 bytes into it. Harmless at the record counts seen here, but a
stack overflow waiting for a full card.
VERIFICATION
On a fourth card from the same lot (batch B9 0C 17 49 70, week 27 / 2017, EV1 8K,
HW 04010101001A05, SW 04010101041A05): the sequence that killed the third card
now completes, the chained 0xAF is answered 91 00, and the card is healthy
afterwards. The APDU log shows GetDFNames and its continuation carrying no MAC
while the commands either side of them do, confirming the session is dropped and
restored. A full `hf mfdes dump` of three applications (AES, 2TDEA, 3TDEA) and
all five EV1 file types completes and round-trips through the dump format.
Scope: all four cards are from one production lot. This is not established as an
EV1-wide erratum.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
52f3225661
commit
8225d76ccd
2 files changed
+95
-7
No files matched your search
@@ -441,6 +441,11 @@ void DesfirePrintContext(DesfireContext_t *ctx) {
|
||||
// during normal operation": the card saying it has damaged or disabled itself.
|
||||
// Those are never noise, so they are reported whether or not APDU logging is on.
|
||||
// Everything else is an ordinary refusal, and the caller decides what to say.
|
||||
// The command a chained 0xAF frame belongs to. 0xAF on its own says nothing
|
||||
// about which command was being continued, and that is exactly what you want to
|
||||
// know when a card dies on one.
|
||||
static uint8_t s_desfire_logical_cmd = 0;
|
||||
|
||||
static void DesfireReportUnrecoverable(uint8_t cmd, uint8_t respcode) {
|
||||
|
||||
if (respcode != MFDES_E_PICC_INTEGRITY &&
|
||||
@@ -452,6 +457,15 @@ static void DesfireReportUnrecoverable(uint8_t cmd, uint8_t respcode) {
|
||||
}
|
||||
|
||||
uint16_t sw = DESFIRE_GET_ISO_STATUS(respcode);
|
||||
|
||||
if (cmd == MFDES_ADDITIONAL_FRAME && s_desfire_logical_cmd != 0 && s_desfire_logical_cmd != MFDES_ADDITIONAL_FRAME) {
|
||||
PrintAndLogEx(WARNING, "Desfire command " _YELLOW_("0x%02X") " frame " _YELLOW_("0xAF") " -> " _RED_("0x%02X") " %s"
|
||||
, s_desfire_logical_cmd
|
||||
, respcode
|
||||
, DesfireGetErrorString(PM3_EAPDU_FAIL, &sw));
|
||||
return;
|
||||
}
|
||||
|
||||
PrintAndLogEx(WARNING, "Desfire command " _YELLOW_("0x%02X") " -> " _RED_("0x%02X") " %s"
|
||||
, cmd
|
||||
, respcode
|
||||
@@ -970,6 +984,8 @@ static int DesfireExchangeExSplit(bool activate_field, DesfireContext_t *ctx, ui
|
||||
|
||||
size_t databuflen = 0;
|
||||
|
||||
s_desfire_logical_cmd = cmd;
|
||||
|
||||
switch (ctx->cmdSet) {
|
||||
case DCCNative:
|
||||
case DCCNativeISO:
|
||||
@@ -1002,6 +1018,15 @@ static int DesfireExchangeExSplit(bool activate_field, DesfireContext_t *ctx, ui
|
||||
break;
|
||||
}
|
||||
|
||||
// the status the card gave, whichever path produced it. Every card error
|
||||
// collapses to PM3_EAPDU_FAIL on the way out, so callers that want to name
|
||||
// it have to read it from here
|
||||
if (respcode != NULL) {
|
||||
ctx->lastRespCode = *respcode;
|
||||
}
|
||||
|
||||
s_desfire_logical_cmd = 0;
|
||||
|
||||
free(databuf);
|
||||
return res;
|
||||
}
|
||||
@@ -2027,21 +2052,60 @@ int DesfireFillAppList(DesfireContext_t *dctx, PICCInfo_t *PICCInfo, AppListS ap
|
||||
for (int i = 0; i < buflen; i += 3)
|
||||
appList[i / 3].appNum = DesfireAIDByteToUint(&buf[i]);
|
||||
|
||||
// GetDFNames must never be sent inside an authenticated session. Measured on
|
||||
// three DESFire EV1 8K cards: with a session open, the card answers the first
|
||||
// 0xAF continuation frame of the chained response with 0xC1
|
||||
// PICC_INTEGRITY_ERROR, "PICC will be disabled", and is dead from then on.
|
||||
// Unauthenticated the identical frames are answered normally.
|
||||
//
|
||||
// The EV1 datasheet never says how secure messaging applies across 0xAF
|
||||
// frames; EV3 had to spell it out later (ev3.pdf 7.3.2.2), and EV2/EV3
|
||||
// dropped the self-disable status codes altogether.
|
||||
//
|
||||
// Key settings bit 1 governs whether the directory commands need
|
||||
// authentication, and its "needs auth" branch does not name GetDFNames at
|
||||
// all (M134034 p.39), so no configuration obliges us to send it in-session.
|
||||
// A card that refuses the plain command costs us the DF names; sending it
|
||||
// authenticated costs the card.
|
||||
//
|
||||
// It is the PICC's session that has to go, not ours -- clearing the context
|
||||
// alone would leave the card still authenticated and change nothing.
|
||||
// SelectApplication is what ends it on the card side: "each
|
||||
// SelectApplication command invalidates the current authentication status"
|
||||
// (M134034 9.4.5). Afterwards put the caller's session back.
|
||||
DesfireSecureChannel savedchann = dctx->secureChannel;
|
||||
uint32_t savedaid = dctx->selectedAID;
|
||||
bool reauth = DesfireIsAuthenticated(dctx);
|
||||
|
||||
if (reauth && dctx->selectedDFNameLen > 0) {
|
||||
// selected by DF name rather than by AID, so there is no AID to go back
|
||||
// to. Not worth guessing with someone's card -- leave the names empty.
|
||||
PrintAndLogEx(DEBUG, "Selected by DF name, skipping the DF name list");
|
||||
reauth = false;
|
||||
goto dfnames_done;
|
||||
}
|
||||
|
||||
if (reauth) {
|
||||
int sres = DesfireSelectAIDHex(dctx, 0x000000, false, 0);
|
||||
if (sres != PM3_SUCCESS) {
|
||||
PrintAndLogEx(WARNING, "Could not drop the PICC session, skipping the DF names");
|
||||
reauth = false;
|
||||
goto dfnames_done;
|
||||
}
|
||||
}
|
||||
|
||||
// result bytes: 3, 2, 1-16. total record size = 24
|
||||
res = DesfireGetDFList(dctx, buf, &buflen);
|
||||
if (res != PM3_SUCCESS) {
|
||||
// Same hazard as GetFileISOIDList below: the PICC ends the authentication
|
||||
// on a command error, so drop our side of the session too. Left alone,
|
||||
// every following command is still framed as MACed against a session the
|
||||
// card has already thrown away, and the card sees a run of frames whose
|
||||
// integrity it cannot verify. Callers that keep using the card
|
||||
// re-authenticate.
|
||||
// an error ends the authentication on the PICC as well, so our side is
|
||||
// down either way. Callers that keep using the card re-authenticate.
|
||||
dctx->secureChannel = DACNone;
|
||||
uint16_t dfsw = DESFIRE_GET_ISO_STATUS(dctx->lastRespCode);
|
||||
PrintAndLogEx(WARNING, "Desfire GetDFList command " _RED_("error") ". Result: %d, card said " _RED_("0x%02X") " %s. Session dropped by the PICC"
|
||||
PrintAndLogEx(DEBUG, "Desfire GetDFList command " _RED_("error") ". Result: %d, card said " _RED_("0x%02X") " %s"
|
||||
, res
|
||||
, dctx->lastRespCode
|
||||
, DesfireGetErrorString(PM3_EAPDU_FAIL, &dfsw));
|
||||
|
||||
} else if (buflen > 0) {
|
||||
for (int i = 0; i < buflen; i++) {
|
||||
int indx = AppListSearchAID(DesfireAIDByteToUint(&buf[i * 24 + 1]), appList, PICCInfo->appCount);
|
||||
@@ -2057,6 +2121,24 @@ int DesfireFillAppList(DesfireContext_t *dctx, PICCInfo_t *PICCInfo, AppListS ap
|
||||
}
|
||||
}
|
||||
|
||||
dfnames_done:
|
||||
|
||||
if (reauth) {
|
||||
// Back to the session the caller handed us, on whatever was selected
|
||||
// then. This is not always the PICC: the issuer info path selects and
|
||||
// authenticates an application before it gets here, and re-authenticating
|
||||
// AID 000000 with that application's key would simply fail.
|
||||
int rres = DesfireSelectAIDHex(dctx, savedaid, false, 0);
|
||||
if (rres == PM3_SUCCESS) {
|
||||
rres = DesfireAuthenticate(dctx, savedchann, false);
|
||||
}
|
||||
|
||||
if (rres != PM3_SUCCESS) {
|
||||
PrintAndLogEx(WARNING, "Could not restore the session on %06X after reading the DF names, continuing without it", savedaid);
|
||||
DesfireClearSession(dctx);
|
||||
}
|
||||
}
|
||||
|
||||
// field on-off zone
|
||||
DesfireFillPICCInfo(dctx, PICCInfo, deepmode);
|
||||
|
||||
|
||||
Reference in new issue
Block a user