Skip to content

Commit 1a626bc

Browse files
committed
hf mfdes default - zero out dfname select name and length
1 parent d6aeda7 commit 1a626bc

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

client/src/cmdhfmfdes.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ int desfire_print_signature(uint8_t *uid, uint8_t uidlen, uint8_t *signature, si
443443
PrintAndLogEx(DEBUG, "UID is NULL");
444444
return PM3_EINVARG;
445445
}
446+
446447
if (signature == NULL) {
447448
PrintAndLogEx(DEBUG, "SIGNATURE is NULL");
448449
return PM3_EINVARG;
@@ -497,8 +498,9 @@ static int CmdDesGetSessionParameters(CLIParserContext *ctx, DesfireContext_t *d
497498
memcpy(kdfInput, defaultKdfInput, defaultKdfInputLen);
498499

499500
int commmode = defaultCommMode;
500-
if (defcommmode != DCMNone)
501+
if (defcommmode != DCMNone) {
501502
commmode = defcommmode;
503+
}
502504

503505
int commset = defaultCommSet;
504506
int secchann = defaultSecureChannel;
@@ -560,7 +562,6 @@ static int CmdDesGetSessionParameters(CLIParserContext *ctx, DesfireContext_t *d
560562
}
561563

562564
if (schannid) {
563-
564565
if (CLIGetOptionList(arg_get_str(ctx, schannid), DesfireSecureChannelOpts, &secchann))
565566
return PM3_ESOFT;
566567
}
@@ -570,10 +571,14 @@ static int CmdDesGetSessionParameters(CLIParserContext *ctx, DesfireContext_t *d
570571
uint8_t dfname_data[16] = {0};
571572
int dfname_len = 0;
572573
if (CLIParamHexToBuf(arg_get_str(ctx, dfnameid), dfname_data, sizeof(dfname_data), &dfname_len) == 0 && dfname_len > 0) {
574+
573575
if (dfname_len <= 16) {
576+
574577
DesfireSetDFName(dctx, dfname_data, dfname_len);
575-
if (selectway)
578+
if (selectway) {
576579
*selectway = ISWDFName;
580+
}
581+
577582
} else {
578583
PrintAndLogEx(ERR, "DF name length must be between 1-16 bytes, got %d", dfname_len);
579584
return PM3_EINVARG;
@@ -583,31 +588,37 @@ static int CmdDesGetSessionParameters(CLIParserContext *ctx, DesfireContext_t *d
583588

584589
if (appid && id) {
585590
*id = 0x000000;
586-
if (CLIGetUint32Hex(ctx, appid, 0x000000, id, NULL, 3, "AID must have 3 bytes length"))
591+
if (CLIGetUint32Hex(ctx, appid, 0x000000, id, NULL, 3, "AID must have 3 bytes length")) {
587592
return PM3_EINVARG;
588-
if (selectway)
593+
}
594+
595+
if (selectway) {
589596
*selectway = ISW6bAID;
590597
}
598+
}
591599

592600
if (appisoid && id) {
593601
uint32_t xisoid = 0x0000;
594602
bool isoidpresent = false;
595-
if (CLIGetUint32Hex(ctx, appisoid, 0x0000, &xisoid, &isoidpresent, 2, "Application ISO ID (for EF) must have 2 bytes length"))
603+
if (CLIGetUint32Hex(ctx, appisoid, 0x0000, &xisoid, &isoidpresent, 2, "Application ISO ID (for EF) must have 2 bytes length")) {
596604
return PM3_EINVARG;
605+
}
597606

598607
if (isoidpresent) {
599608
*id = xisoid & 0xffff;
600-
if (selectway)
609+
if (selectway) {
601610
*selectway = ISWIsoID;
602611
}
603612
}
613+
}
604614

605615
DesfireSetKey(dctx, keynum, algores, key);
606616
DesfireSetKdf(dctx, kdfAlgo, kdfInput, kdfInputLen);
607617
DesfireSetCommandSet(dctx, commset);
608618
DesfireSetCommMode(dctx, commmode);
609-
if (securechannel)
619+
if (securechannel) {
610620
*securechannel = secchann;
621+
}
611622

612623
return PM3_SUCCESS;
613624
}
@@ -642,6 +653,10 @@ static int CmdHF14ADesDefault(const char *Cmd) {
642653

643654
CLIParserFree(ctx);
644655

656+
// clear out select DF Name length and array when resetting to default the desfire context
657+
dctx.selectedDFNameLen = 0;
658+
memset(dctx.selectedDFName, 0, sizeof(dctx.selectedDFName));
659+
645660
defaultKeyNum = dctx.keyNum;
646661
defaultAlgoId = dctx.keyType;
647662
memcpy(defaultKey, dctx.key, DESFIRE_MAX_KEY_SIZE);
@@ -2299,10 +2314,11 @@ static int CmdHF14ADesAuth(const char *Cmd) {
22992314

23002315
if (dctx.selectedDFNameLen > 0) {
23012316
PrintAndLogEx(SUCCESS, "DF selected and authenticated " _GREEN_("successfully"));
2302-
} else if (DesfireMFSelected(selectway, id))
2317+
} else if (DesfireMFSelected(selectway, id)) {
23032318
PrintAndLogEx(SUCCESS, "PICC selected and authenticated " _GREEN_("succesfully"));
2304-
else
2319+
} else {
23052320
PrintAndLogEx(SUCCESS, "Application " _CYAN_("%s") " selected and authenticated " _GREEN_("succesfully"), DesfireWayIDStr(selectway, id));
2321+
}
23062322

23072323
PrintAndLogEx(SUCCESS, _CYAN_("Context: "));
23082324
DesfirePrintContext(&dctx);

client/src/mifare/desfirecore.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,8 +1096,9 @@ int DesfireSelectAndAuthenticate(DesfireContext_t *dctx, DesfireSecureChannel se
10961096
}
10971097

10981098
int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel secureChannel, DesfireISOSelectWay way, uint32_t id, bool selectfile, uint16_t isofileid, bool noauth, bool verbose) {
1099-
if (verbose)
1099+
if (verbose) {
11001100
DesfirePrintContext(dctx);
1101+
}
11011102

11021103
int res = 0;
11031104

@@ -1106,11 +1107,13 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s
11061107
// Select DF by name using ISO7816 SELECT
11071108
uint8_t resp[250] = {0};
11081109
size_t resplen = 0;
1110+
11091111
res = DesfireISOSelect(dctx, ISSDFName, dctx->selectedDFName, dctx->selectedDFNameLen, resp, &resplen);
11101112
if (res != PM3_SUCCESS) {
11111113
PrintAndLogEx(ERR, "Desfire DF name select " _RED_("error"));
11121114
return 200;
11131115
}
1116+
11141117
if (verbose) {
11151118
PrintAndLogEx(INFO, "DF %s is " _GREEN_("selected"), sprint_hex(dctx->selectedDFName, dctx->selectedDFNameLen));
11161119
}
@@ -1119,59 +1122,72 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s
11191122
if (way == ISW6bAID && id != 0x000000) {
11201123
if (dctx->cmdSet == DCCISO) {
11211124
dctx->cmdSet = DCCNativeISO;
1122-
if (verbose)
1125+
if (verbose) {
11231126
PrintAndLogEx(INFO, "Select via " _CYAN_("native iso wrapping") " interface");
1127+
}
11241128

11251129
res = DesfireSelectAIDHex(dctx, id, false, 0);
11261130
if (res != PM3_SUCCESS) {
11271131
PrintAndLogEx(ERR, "Desfire select " _RED_("error"));
11281132
return 200;
11291133
}
1130-
if (verbose)
1134+
1135+
if (verbose) {
11311136
PrintAndLogEx(INFO, "App %06x via native iso channel is " _GREEN_("selected"), id);
1137+
}
11321138

11331139
dctx->cmdSet = DCCISO;
1140+
11341141
} else {
11351142
res = DesfireSelectEx(dctx, false, way, id, NULL);
11361143
if (res != PM3_SUCCESS) {
11371144
PrintAndLogEx(ERR, "Desfire %s select " _RED_("error"), DesfireSelectWayToStr(way));
11381145
return 202;
11391146
}
1140-
if (verbose)
1147+
if (verbose) {
11411148
PrintAndLogEx(INFO, "%s is " _GREEN_("selected"), DesfireWayIDStr(way, id));
11421149
}
1150+
}
1151+
11431152
} else if (way == ISWIsoID && id != 0x0000) {
11441153
// Also select by ISO ID if specified
11451154
res = DesfireSelectEx(dctx, false, way, id, NULL);
11461155
if (res != PM3_SUCCESS) {
11471156
PrintAndLogEx(ERR, "Desfire %s select " _RED_("error"), DesfireSelectWayToStr(way));
11481157
return 202;
11491158
}
1150-
if (verbose)
1159+
1160+
if (verbose) {
11511161
PrintAndLogEx(INFO, "%s is " _GREEN_("selected"), DesfireWayIDStr(way, id));
1162+
}
11521163
}
11531164
} else if (way == ISW6bAID && dctx->cmdSet == DCCISO) {
11541165
dctx->cmdSet = DCCNativeISO;
1155-
if (verbose)
1166+
if (verbose) {
11561167
PrintAndLogEx(INFO, "Select via " _CYAN_("native iso wrapping") " interface");
1168+
}
11571169

11581170
res = DesfireSelectAIDHex(dctx, id, false, 0);
11591171
if (res != PM3_SUCCESS) {
11601172
PrintAndLogEx(ERR, "Desfire select " _RED_("error"));
11611173
return 200;
11621174
}
1163-
if (verbose)
1175+
1176+
if (verbose) {
11641177
PrintAndLogEx(INFO, "App %06x via native iso channel is " _GREEN_("selected"), id);
1178+
}
11651179

11661180
dctx->cmdSet = DCCISO;
1181+
11671182
} else {
11681183
res = DesfireSelectEx(dctx, true, way, id, NULL);
11691184
if (res != PM3_SUCCESS) {
11701185
PrintAndLogEx(ERR, "Desfire %s select " _RED_("error"), DesfireSelectWayToStr(way));
11711186
return 202;
11721187
}
1173-
if (verbose)
1188+
if (verbose) {
11741189
PrintAndLogEx(INFO, "%s is " _GREEN_("selected"), DesfireWayIDStr(way, id));
1190+
}
11751191
}
11761192

11771193
if (selectfile) {
@@ -1186,7 +1202,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s
11861202
}
11871203
}
11881204

1189-
if (!noauth) {
1205+
if (noauth == false) {
11901206
res = DesfireAuthenticate(dctx, secureChannel, verbose);
11911207
if (res != PM3_SUCCESS) {
11921208
PrintAndLogEx(ERR, "Desfire authenticate " _RED_("error") ". Result: [%d] %s", res, DesfireAuthErrorToStr(res));

client/src/mifare/desfirecrypto.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void DesfireSetDFName(DesfireContext_t *ctx, uint8_t *dfname, uint8_t dfnameLen)
109109
ctx->selectedDFNameLen = 0;
110110
memset(ctx->selectedDFName, 0, sizeof(ctx->selectedDFName));
111111

112-
if (dfname && dfnameLen > 0 && dfnameLen <= 16) {
112+
if (dfname && dfnameLen && dfnameLen <= 16) {
113113
ctx->selectedDFNameLen = dfnameLen;
114114
memcpy(ctx->selectedDFName, dfname, dfnameLen);
115115
}

0 commit comments

Comments
 (0)