hf mfdes: derive the communication mode from the access rights
The PICC applies a file's communication mode only when access is granted by
a key right matching the authenticated key. When the operation is granted by
the free access right (0x0E) instead, it runs in plain, whatever the file
settings say. The client used the file comm mode unconditionally, so any file
created with the default EEEE rights failed with 0x917E on write, and a free
access Full file was printed as its data plus eight unstripped CMAC bytes
instead of being decrypted. Fixes #3393.
Which rights can grant an operation varies, and the rule above is evaluated
over that set. Measured on DESFire EV2, ev1 and ev2 secure channels:
read / write data w (or r), rw
GetValue 0x6C r, w, rw
Credit 0x0C rw only
Debit 0xDC r, w, rw
LimCredit 0x1C r, w, rw
Credit is the odd one out: a file with r=key0 w=key0 rw=free needs Credit in
plain but Debit in MAC, in the same session. The FreeValue option bit forces
GetValue to plain regardless of the rights.
hf mfdes value never read the file settings at all, which is #2712. e1598cd62
worked around that by dropping CREDIT/DEBIT/LIMITED_CREDIT from
EV1D40TransmitMAC and retrying in plain on a length error. That made MACed
value files impossible to credit by any route, and the retry resent a
byte-identical APDU after the failure had already desynced the CMAC chain,
turning a clear 0x7E into a misleading 0x91AE. Both are reverted and the
command now reads the file settings like read and write do. Fixes #2712.
hf mfdes write resolves the comm mode even when --type is given explicitly,
since that path skipped detection entirely.
The DACLRP branch in the read path is left alone: no LRP capable card was
available to test against.
Co-Authored-By: Claude Opus 5 (1M context)
This commit is contained in:
1 parent
08e389c0bb
commit
2449bb2472
6 files changed
+155
-83
No files matched your search
+119
-65
@@ -6428,6 +6428,23 @@ static int CmdHF14ADesDeleteFile(const char *Cmd) {
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
// Which access rights can grant each value file operation. Credit needs read&write
|
||||
// only, the others are granted by any of read / write / read&write. The FreeValue
|
||||
// option bit makes GetValue free whatever the rights say.
|
||||
// Measured on DESFire EV2 hw, `ev1` and `ev2` secure channels.
|
||||
static DesfireCommunicationMode DesfireValueOpCommMode(DesfireContext_t *dctx, const FileSettings_t *fsettings, uint8_t vop) {
|
||||
if (vop == MFDES_GET_VALUE && (fsettings->limitedCredit & 0x02) != 0)
|
||||
return DCMPlain;
|
||||
|
||||
if (vop == MFDES_CREDIT) {
|
||||
const uint8_t rwonly[] = { fsettings->rwAccess };
|
||||
return DesfireEffectiveCommMode(dctx, fsettings->commMode, rwonly, ARRAYLEN(rwonly));
|
||||
}
|
||||
|
||||
const uint8_t anyright[] = { fsettings->rAccess, fsettings->wAccess, fsettings->rwAccess };
|
||||
return DesfireEffectiveCommMode(dctx, fsettings->commMode, anyright, ARRAYLEN(anyright));
|
||||
}
|
||||
|
||||
static int CmdHF14ADesValueOperations(const char *Cmd) {
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "hf mfdes value",
|
||||
@@ -6509,19 +6526,51 @@ static int CmdHF14ADesValueOperations(const char *Cmd) {
|
||||
if (verbose)
|
||||
PrintAndLogEx(INFO, "%s file %02x operation: %s value: 0x%08x", DesfireWayIDStr(selectway, id), fileid, CLIGetOptionListStr(DesfireValueFileOperOpts, op), value);
|
||||
|
||||
// the PICC picks the communication mode from the file settings and the access rights,
|
||||
// so read them instead of guessing from the command line
|
||||
FileSettings_t fsettings = {0};
|
||||
bool havesettings = false;
|
||||
|
||||
DesfireCommunicationMode chmode = dctx.commMode;
|
||||
DesfireSetCommMode(&dctx, DCMMACed);
|
||||
res = DesfireFileSettingsStruct(&dctx, fileid, &fsettings);
|
||||
DesfireSetCommMode(&dctx, chmode);
|
||||
|
||||
if (res == PM3_SUCCESS) {
|
||||
havesettings = true;
|
||||
|
||||
if (fsettings.fileType != 0x02) {
|
||||
PrintAndLogEx(WARNING, "File %02x is " _RED_("not") " a value file, got %s", fileid, GetDesfireFileType(fsettings.fileType));
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, "Value file %02x comm mode: %s", fileid, CLIGetOptionListStr(DesfireCommunicationModeOpts, fsettings.commMode));
|
||||
}
|
||||
|
||||
} else {
|
||||
PrintAndLogEx(WARNING, "GetFileSettings error. Can't get the file communication mode.");
|
||||
}
|
||||
|
||||
if (op != 0xff) {
|
||||
if (havesettings) {
|
||||
DesfireSetCommMode(&dctx, DesfireValueOpCommMode(&dctx, &fsettings, op));
|
||||
}
|
||||
|
||||
res = DesfireValueFileOperations(&dctx, fileid, op, &value);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Desfire ValueFileOperations (0x%02x) command ( " _RED_("error") " ) Result: %d", op, res);
|
||||
DropField();
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
if (verbose)
|
||||
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, "Operation ( %s )" _GREEN_("ok"), CLIGetOptionListStr(DesfireValueFileOperOpts, op));
|
||||
}
|
||||
|
||||
if (op == MFDES_GET_VALUE) {
|
||||
PrintAndLogEx(SUCCESS, "Value: " _GREEN_("%d (0x%08x)"), value, value);
|
||||
} else {
|
||||
|
||||
DesfireSetCommMode(&dctx, DCMMACed);
|
||||
res = DesfireCommitTransaction(&dctx, false, 0);
|
||||
if (res != PM3_SUCCESS) {
|
||||
@@ -6529,44 +6578,32 @@ static int CmdHF14ADesValueOperations(const char *Cmd) {
|
||||
DropField();
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
if (verbose)
|
||||
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, "Commit ( " _GREEN_("ok") " )");
|
||||
}
|
||||
|
||||
PrintAndLogEx(SUCCESS, "Value changed " _GREEN_("successfully"));
|
||||
}
|
||||
} else {
|
||||
DesfireCommunicationMode fileCommMode = dctx.commMode;
|
||||
if (havesettings == false || fsettings.fileType != 0x02) {
|
||||
PrintAndLogEx(ERR, "Need the value file settings to clear it");
|
||||
DropField();
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
DesfireSetCommMode(&dctx, DesfireValueOpCommMode(&dctx, &fsettings, MFDES_GET_VALUE));
|
||||
res = DesfireValueFileOperations(&dctx, fileid, MFDES_GET_VALUE, &value);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Desfire GetValue command ( " _RED_("error") ") Result: %d", res);
|
||||
DropField();
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
if (verbose)
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, _YELLOW_("GetValue") " command is " _GREEN_("ok") ". Current value: 0x%08x", value);
|
||||
|
||||
uint8_t buf[250] = {0};
|
||||
size_t buflen = 0;
|
||||
|
||||
DesfireSetCommMode(&dctx, DCMMACed);
|
||||
res = DesfireGetFileSettings(&dctx, fileid, buf, &buflen);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Desfire GetFileSettings command ( " _RED_("error") " ) Result: %d", res);
|
||||
DropField();
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
PrintAndLogEx(INFO, _YELLOW_("GetFileSettings") " is " _GREEN_("ok") " . File settings[%zu]: %s", buflen, sprint_hex(buf, buflen));
|
||||
|
||||
if (buflen < 8 || buf[0] != 0x02) {
|
||||
PrintAndLogEx(ERR, "Desfire GetFileSettings command returns " _RED_("wrong") " data");
|
||||
DropField();
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
int32_t minvalue = (int)MemLeToUint4byte(&buf[4]);
|
||||
int32_t minvalue = (int)fsettings.lowerLimit;
|
||||
uint32_t delta = ((int64_t)value > minvalue) ? value - minvalue : 0;
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, "value: 0x%08x (%d)", value, value);
|
||||
@@ -6575,10 +6612,11 @@ static int CmdHF14ADesValueOperations(const char *Cmd) {
|
||||
}
|
||||
|
||||
if (delta > 0) {
|
||||
DesfireSetCommMode(&dctx, fileCommMode);
|
||||
DesfireSetCommMode(&dctx, DesfireValueOpCommMode(&dctx, &fsettings, MFDES_DEBIT));
|
||||
|
||||
uint32_t maxdelta = 0x7fffffff;
|
||||
if (delta > maxdelta) {
|
||||
|
||||
res = DesfireValueFileOperations(&dctx, fileid, MFDES_DEBIT, &maxdelta);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Desfire Debit maxdelta operation ( " _RED_("error") " ) Result: %d", res);
|
||||
@@ -6600,8 +6638,9 @@ static int CmdHF14ADesValueOperations(const char *Cmd) {
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, "Value debited " _GREEN_("ok"));
|
||||
}
|
||||
|
||||
DesfireSetCommMode(&dctx, DCMMACed);
|
||||
res = DesfireCommitTransaction(&dctx, false, 0);
|
||||
@@ -6611,11 +6650,13 @@ static int CmdHF14ADesValueOperations(const char *Cmd) {
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, "Transaction :" _GREEN_("committed"));
|
||||
}
|
||||
} else {
|
||||
if (verbose)
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, "Nothing to clear. Value already in the minimum level.");
|
||||
}
|
||||
}
|
||||
|
||||
PrintAndLogEx(SUCCESS, "Value cleared " _GREEN_("successfully"));
|
||||
@@ -7376,15 +7417,19 @@ static int DesfileReadFileAndPrint(DesfireContext_t *dctx,
|
||||
}
|
||||
}
|
||||
|
||||
commMode = fsettings.commMode;
|
||||
// free access (0x0e) makes the PICC answer in plain, whatever the file comm mode is
|
||||
if (filetype == RFTValue) {
|
||||
commMode = DesfireValueOpCommMode(dctx, &fsettings, MFDES_GET_VALUE);
|
||||
} else {
|
||||
const uint8_t rrights[] = { fsettings.rAccess, fsettings.rwAccess };
|
||||
commMode = DesfireEffectiveCommMode(dctx, fsettings.commMode, rrights, ARRAYLEN(rrights));
|
||||
}
|
||||
|
||||
// lrp needs to point exact mode
|
||||
if (dctx->secureChannel == DACLRP) {
|
||||
// read right == free
|
||||
if (fsettings.rAccess == 0xe)
|
||||
commMode = DCMPlain;
|
||||
// get value access == free
|
||||
if (filetype == RFTValue && (fsettings.limitedCredit & 0x02) != 0)
|
||||
commMode = DCMPlain;
|
||||
}
|
||||
|
||||
// calc max length
|
||||
@@ -7407,7 +7452,7 @@ static int DesfileReadFileAndPrint(DesfireContext_t *dctx,
|
||||
PrintAndLogEx(INFO, _CYAN_("File type:") " %s Option: %s comm mode: %s",
|
||||
GetDesfireFileType(fsettings.fileType),
|
||||
CLIGetOptionListStr(DesfireReadFileTypeOpts, filetype),
|
||||
CLIGetOptionListStr(DesfireCommunicationModeOpts, fsettings.commMode)
|
||||
CLIGetOptionListStr(DesfireCommunicationModeOpts, commMode)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
@@ -7859,8 +7904,9 @@ static int CmdHF14ADesWriteData(const char *Cmd) {
|
||||
return res;
|
||||
}
|
||||
|
||||
// get file settings
|
||||
if (op == RFTAuto) {
|
||||
// get file settings. Needed for the file type in `auto` mode, and always for the
|
||||
// communication mode the PICC expects for this operation
|
||||
{
|
||||
FileSettings_t fsettings;
|
||||
|
||||
DesfireCommunicationMode commMode = dctx.commMode;
|
||||
@@ -7869,38 +7915,46 @@ static int CmdHF14ADesWriteData(const char *Cmd) {
|
||||
DesfireSetCommMode(&dctx, commMode);
|
||||
|
||||
if (res == PM3_SUCCESS) {
|
||||
switch (fsettings.fileType) {
|
||||
case 0x00:
|
||||
case 0x01: {
|
||||
op = RFTData;
|
||||
if (!commit)
|
||||
commit = (fsettings.fileType == 0x01);
|
||||
break;
|
||||
}
|
||||
case 0x02: {
|
||||
op = RFTValue;
|
||||
commit = true;
|
||||
break;
|
||||
}
|
||||
case 0x03:
|
||||
case 0x04: {
|
||||
op = RFTRecord;
|
||||
commit = true;
|
||||
if (datalen > fsettings.recordSize)
|
||||
PrintAndLogEx(WARNING, "Record size (%d) " _RED_("is less") " than data length (%d)", fsettings.recordSize, datalen);
|
||||
break;
|
||||
}
|
||||
case 0x05: {
|
||||
op = RFTMAC;
|
||||
commit = false;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
if (op == RFTAuto) {
|
||||
switch (fsettings.fileType) {
|
||||
case 0x00:
|
||||
case 0x01: {
|
||||
op = RFTData;
|
||||
if (!commit)
|
||||
commit = (fsettings.fileType == 0x01);
|
||||
break;
|
||||
}
|
||||
case 0x02: {
|
||||
op = RFTValue;
|
||||
commit = true;
|
||||
break;
|
||||
}
|
||||
case 0x03:
|
||||
case 0x04: {
|
||||
op = RFTRecord;
|
||||
commit = true;
|
||||
if (datalen > fsettings.recordSize)
|
||||
PrintAndLogEx(WARNING, "Record size (%d) " _RED_("is less") " than data length (%d)", fsettings.recordSize, datalen);
|
||||
break;
|
||||
}
|
||||
case 0x05: {
|
||||
op = RFTMAC;
|
||||
commit = false;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DesfireSetCommMode(&dctx, fsettings.commMode);
|
||||
// free access (0x0e) makes the PICC expect the command in plain, whatever the file comm mode is
|
||||
if (op == RFTValue) {
|
||||
DesfireSetCommMode(&dctx, DesfireValueOpCommMode(&dctx, &fsettings, (debit) ? MFDES_DEBIT : MFDES_CREDIT));
|
||||
} else {
|
||||
const uint8_t wrights[] = { fsettings.wAccess, fsettings.rwAccess };
|
||||
DesfireSetCommMode(&dctx, DesfireEffectiveCommMode(&dctx, fsettings.commMode, wrights, ARRAYLEN(wrights)));
|
||||
}
|
||||
|
||||
if (fsettings.fileCommMode != 0 && noauth)
|
||||
PrintAndLogEx(WARNING, "File needs communication mode `%s` but there is no authentication", CLIGetOptionListStr(DesfireCommunicationModeOpts, fsettings.commMode));
|
||||
@@ -7915,7 +7969,7 @@ static int CmdHF14ADesWriteData(const char *Cmd) {
|
||||
PrintAndLogEx(INFO, "Got file type: %s. Option: %s. comm mode: %s",
|
||||
GetDesfireFileType(fsettings.fileType),
|
||||
CLIGetOptionListStr(DesfireReadFileTypeOpts, op),
|
||||
CLIGetOptionListStr(DesfireCommunicationModeOpts, fsettings.commMode));
|
||||
CLIGetOptionListStr(DesfireCommunicationModeOpts, dctx.commMode));
|
||||
} else {
|
||||
PrintAndLogEx(WARNING, "GetFileSettings error. Can't get file type.");
|
||||
}
|
||||
|
||||
@@ -2453,20 +2453,6 @@ int DesfireValueFileOperations(DesfireContext_t *dctx, uint8_t fid, uint8_t oper
|
||||
|
||||
int res = DesfireCommand(dctx, operation, data, datalen, resp, &resplen, -1);
|
||||
|
||||
// Auto-detection fallback: if MAC mode fails with length error, retry with plain mode
|
||||
if ((res == 0x7E || res == -20) && dctx->commMode == DCMMACed) {
|
||||
PrintAndLogEx(INFO, "MAC mode failed with length error, retrying with plain mode");
|
||||
DesfireCommunicationMode original_mode = dctx->commMode;
|
||||
dctx->commMode = DCMPlain;
|
||||
|
||||
memset(resp, 0, sizeof(resp));
|
||||
resplen = 0;
|
||||
res = DesfireCommand(dctx, operation, data, datalen, resp, &resplen, -1);
|
||||
|
||||
// Restore original mode for future commands
|
||||
dctx->commMode = original_mode;
|
||||
}
|
||||
|
||||
if (resplen == 4 && value) {
|
||||
*value = MemLeToUint4byte(resp);
|
||||
}
|
||||
|
||||
@@ -574,19 +574,42 @@ uint8_t DesfireCommModeToFileCommMode(DesfireCommunicationMode comm_mode) {
|
||||
return fmode;
|
||||
}
|
||||
|
||||
// The PICC applies the file communication mode only when access is granted via a key
|
||||
// that matches the authenticated one. When the operation is granted by the free access
|
||||
// right (0x0e) instead, the PICC runs it in plain, whatever the file settings say.
|
||||
// `rights` lists every access right that can grant the operation.
|
||||
DesfireCommunicationMode DesfireEffectiveCommMode(DesfireContext_t *ctx, DesfireCommunicationMode filemode, const uint8_t *rights, size_t rightslen) {
|
||||
bool freeaccess = false;
|
||||
|
||||
for (size_t i = 0; i < rightslen; i++) {
|
||||
// a key based right we hold takes precedence over free access
|
||||
if (rights[i] == ctx->keyNum && DesfireIsAuthenticated(ctx)) {
|
||||
return filemode;
|
||||
}
|
||||
|
||||
if (rights[i] == 0x0e) {
|
||||
freeaccess = true;
|
||||
}
|
||||
}
|
||||
|
||||
return (freeaccess) ? DCMPlain : filemode;
|
||||
}
|
||||
|
||||
void DesfireGenSessionKeyEV1(const uint8_t rnda[], const uint8_t rndb[], DesfireCryptoAlgorithm keytype, uint8_t *key) {
|
||||
switch (keytype) {
|
||||
case T_DES:
|
||||
case T_DES: {
|
||||
memcpy(key, rnda, 4);
|
||||
memcpy(key + 4, rndb, 4);
|
||||
break;
|
||||
case T_3DES:
|
||||
}
|
||||
case T_3DES: {
|
||||
memcpy(key, rnda, 4);
|
||||
memcpy(key + 4, rndb, 4);
|
||||
memcpy(key + 8, rnda + 4, 4);
|
||||
memcpy(key + 12, rndb + 4, 4);
|
||||
break;
|
||||
case T_3K3DES:
|
||||
}
|
||||
case T_3K3DES: {
|
||||
memcpy(key, rnda, 4);
|
||||
memcpy(key + 4, rndb, 4);
|
||||
memcpy(key + 8, rnda + 6, 4);
|
||||
@@ -594,12 +617,14 @@ void DesfireGenSessionKeyEV1(const uint8_t rnda[], const uint8_t rndb[], Desfire
|
||||
memcpy(key + 16, rnda + 12, 4);
|
||||
memcpy(key + 20, rndb + 12, 4);
|
||||
break;
|
||||
case T_AES:
|
||||
}
|
||||
case T_AES: {
|
||||
memcpy(key, rnda, 4);
|
||||
memcpy(key + 4, rndb, 4);
|
||||
memcpy(key + 8, rnda + 12, 4);
|
||||
memcpy(key + 12, rndb + 12, 4);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -125,6 +125,7 @@ void DesfirePrintCardKeyType(uint8_t keyType);
|
||||
|
||||
DesfireCommunicationMode DesfireFileCommModeToCommMode(uint8_t file_comm_mode);
|
||||
uint8_t DesfireCommModeToFileCommMode(DesfireCommunicationMode comm_mode);
|
||||
DesfireCommunicationMode DesfireEffectiveCommMode(DesfireContext_t *ctx, DesfireCommunicationMode filemode, const uint8_t *rights, size_t rightslen);
|
||||
|
||||
void DesfireGenSessionKeyEV1(const uint8_t rnda[], const uint8_t rndb[], DesfireCryptoAlgorithm keytype, uint8_t *key);
|
||||
void DesfireGenSessionKeyEV2(uint8_t *key, uint8_t *rndA, uint8_t *rndB, bool enckey, uint8_t *sessionkey);
|
||||
|
||||
@@ -235,6 +235,9 @@ static uint8_t DesfireGetCmdHeaderLen(uint8_t cmd, size_t datalen) {
|
||||
|
||||
static const uint8_t EV1D40TransmitMAC[] = {
|
||||
MFDES_WRITE_DATA,
|
||||
MFDES_CREDIT,
|
||||
MFDES_DEBIT,
|
||||
MFDES_LIMITED_CREDIT,
|
||||
MFDES_WRITE_RECORD,
|
||||
MFDES_UPDATE_RECORD,
|
||||
MFDES_COMMIT_READER_ID,
|
||||
|
||||
Reference in new issue
Block a user