diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 67674a5df..ce3d45f4f 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -5110,10 +5110,6 @@ static int CmdHF14ADesGetAIDs(const char *Cmd) { DesfireSetCommandChannel(&dctx, DCCNativeISO); - //size_t len = 0; - //uint16_t sw = 0; - //uint8_t buf[APDU_RES_LEN] = {0}; - int res = DesfireSelectAIDHex(&dctx, 0x000000, false, 0); if (res != PM3_SUCCESS) { PrintAndLogEx(ERR, "Desfire select " _RED_("error") "."); @@ -5133,8 +5129,18 @@ static int CmdHF14ADesGetAIDs(const char *Cmd) { else return PM3_ESOFT; + uint8_t buf[APDU_RES_LEN] = {0}; + size_t buflen = 0; + res = DesfireGetAIDList(&dctx, buf, &buflen); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire GetAIDList command " _RED_("error") ". Result: %d", res); + DropField(); + return PM3_ESOFT; + } + for (int i = 0; i < buflen; i += 3) + PrintAndLogEx(INFO, "AID: %06x", DesfireAIDByteToUint(&buf[i])); DropField(); return PM3_SUCCESS; diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index b43e6e7bb..24e6bfa2a 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -148,6 +148,16 @@ const char *DesfireGetErrorString(int res, uint16_t *sw) { return ""; } +uint32_t DesfireAIDByteToUint(uint8_t *data) { + return data[0] + (data[1] << 8) + (data[2] << 16); +} + +void DesfireAIDUintToByte(uint32_t aid, uint8_t *data) { + data[0] = aid & 0xff; + data[1] = (aid >> 8) & 0xff; + data[2] = (aid >> 16) & 0xff; +} + void DesfireClearContext(DesfireContext *ctx) { ctx->keyNum = 0; ctx->keyType = T_DES; @@ -383,13 +393,8 @@ int DesfireSelectAID(DesfireContext *ctx, uint8_t *aid1, uint8_t *aid2) { int DesfireSelectAIDHex(DesfireContext *ctx, uint32_t aid1, bool select_two, uint32_t aid2) { uint8_t data[6] = {0}; - data[0] = aid1 & 0xff; - data[1] = (aid1 >> 8) & 0xff; - data[2] = (aid1 >> 16) & 0xff; - - data[3] = aid2 & 0xff; - data[4] = (aid2 >> 8) & 0xff; - data[5] = (aid2 >> 16) & 0xff; + DesfireAIDUintToByte(aid1, data); + DesfireAIDUintToByte(aid2, &data[3]); return DesfireSelectAID(ctx, data, (select_two) ? &data[3] : NULL); } @@ -663,4 +668,12 @@ PrintAndLogEx(INFO, "sessionKeyEnc : %s", sprint_hex(dctx->sessionKeyEnc, desfir return PM3_SUCCESS; } - +int DesfireGetAIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen) { + uint8_t respcode = 0xff; + int res = DesfireExchange(dctx, MFDES_GET_APPLICATION_IDS, NULL, 0, &respcode, resp, resplen); + if (res != PM3_SUCCESS) + return res; + if (respcode != MFDES_S_OPERATION_OK) + return PM3_EAPDU_FAIL; + return PM3_SUCCESS; +} diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index 529315405..cae333015 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -71,14 +71,20 @@ void DesfireSetKey(DesfireContext *ctx, uint8_t keyNum, enum DESFIRE_CRYPTOALGO void DesfireSetCommandChannel(DesfireContext *ctx, DesfireCommandChannel cmdChannel); const char *DesfireGetErrorString(int res, uint16_t *sw); +uint32_t DesfireAIDByteToUint(uint8_t *data); +void DesfireAIDUintToByte(uint32_t aid, uint8_t *data); int DesfireExchange(DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *respcode, uint8_t *resp, size_t *resplen); int DesfireExchangeEx(bool activate_field, DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *respcode, uint8_t *resp, size_t *resplen, bool enable_chaining); int DesfireSelectAID(DesfireContext *ctx, uint8_t *aid1, uint8_t *aid2); int DesfireSelectAIDHex(DesfireContext *ctx, uint32_t aid1, bool select_two, uint32_t aid2); + int DesfireAuthenticate(DesfireContext *dctx, DesfireAuthChannel authChannel); bool DesfireIsAuthenticated(DesfireContext *dctx); +int DesfireGetAIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); +int DesfireGetDFList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); + #endif // __DESFIRECORE_H