Moved HRT parser logic under client/src/parsers + Restructured parser calling code to its own file that is hooked from hf mfdes info command
This commit is contained in:
8 files changed
+161
-80
No files matched your search
@@ -468,7 +468,6 @@ set (TARGET_SOURCES
|
||||
${PM3_ROOT}/client/src/cmdmain.c
|
||||
${PM3_ROOT}/client/src/cmdmqtt.c
|
||||
${PM3_ROOT}/client/src/cmdnfc.c
|
||||
${PM3_ROOT}/client/src/cmdparsehrt.c
|
||||
${PM3_ROOT}/client/src/cmdparser.c
|
||||
${PM3_ROOT}/client/src/cmdpiv.c
|
||||
${PM3_ROOT}/client/src/cmdscript.c
|
||||
@@ -484,6 +483,8 @@ set (TARGET_SOURCES
|
||||
${PM3_ROOT}/client/src/iso4217.c
|
||||
${PM3_ROOT}/client/src/jansson_path.c
|
||||
${PM3_ROOT}/client/src/lua_bitlib.c
|
||||
${PM3_ROOT}/client/src/parsers/parsehrt.c
|
||||
${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c
|
||||
${PM3_ROOT}/client/src/pla.c
|
||||
${PM3_ROOT}/client/src/preferences.c
|
||||
${PM3_ROOT}/client/src/pm3.c
|
||||
|
||||
+2
-1
@@ -809,7 +809,6 @@ SRCS = mifare/aiddesfire.c \
|
||||
cmdmain.c \
|
||||
cmdmqtt.c \
|
||||
cmdnfc.c \
|
||||
cmdparsehrt.c \
|
||||
cmdparser.c \
|
||||
cmdpiv.c \
|
||||
cmdscript.c \
|
||||
@@ -883,6 +882,8 @@ SRCS = mifare/aiddesfire.c \
|
||||
mifare/prime.c \
|
||||
mifare/gen4.c \
|
||||
nfc/ndef.c \
|
||||
parsers/parsehrt.c \
|
||||
parsers/hrtparser/hrtparser.c \
|
||||
pm3.c \
|
||||
pm3_binlib.c \
|
||||
pm3_bitlib.c \
|
||||
|
||||
@@ -388,7 +388,6 @@ set (TARGET_SOURCES
|
||||
${PM3_ROOT}/client/src/cmdmain.c
|
||||
${PM3_ROOT}/client/src/cmdmqtt.c
|
||||
${PM3_ROOT}/client/src/cmdnfc.c
|
||||
${PM3_ROOT}/client/src/cmdparsehrt.c
|
||||
${PM3_ROOT}/client/src/cmdparser.c
|
||||
${PM3_ROOT}/client/src/cmdpiv.c
|
||||
${PM3_ROOT}/client/src/cmdscript.c
|
||||
@@ -404,6 +403,8 @@ set (TARGET_SOURCES
|
||||
${PM3_ROOT}/client/src/iso4217.c
|
||||
${PM3_ROOT}/client/src/jansson_path.c
|
||||
${PM3_ROOT}/client/src/lua_bitlib.c
|
||||
${PM3_ROOT}/client/src/parsers/parsehrt.c
|
||||
${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c
|
||||
${PM3_ROOT}/client/src/pla.c
|
||||
${PM3_ROOT}/client/src/preferences.c
|
||||
${PM3_ROOT}/client/src/pm3.c
|
||||
|
||||
+4
-72
@@ -24,7 +24,6 @@
|
||||
#include <string.h>
|
||||
#include "jansson.h"
|
||||
#include "commonutil.h" // ARRAYLEN
|
||||
#include "cmdparsehrt.h"
|
||||
#include "cmdparser.h" // command_t
|
||||
#include "comms.h"
|
||||
#include "mifare/desfirecrypto.h"
|
||||
@@ -35,6 +34,7 @@
|
||||
#include "crypto/libpcrypto.h"
|
||||
#include "crypto/duoxcrypto.h"
|
||||
#include "protocols.h"
|
||||
#include "parsers/parsehrt.h"
|
||||
#include "cmdtrace.h"
|
||||
#include "cliparser.h"
|
||||
#include "iso7816/apduinfo.h" // APDU manipulation / errorcodes
|
||||
@@ -420,76 +420,6 @@ static const char *getProductTypeStr(const uint8_t *versionhw) {
|
||||
return "UNKNOWN PROD";
|
||||
}
|
||||
|
||||
static bool hf_mfdes_try_parse_hrt(DesfireContext_t *dctx) {
|
||||
DesfireSetCommMode(dctx, DCMPlain);
|
||||
DesfireSetCommandSet(dctx, DCCNative);
|
||||
|
||||
// TODO: Add support for older travelcard (AID EF2011)
|
||||
|
||||
if (DesfireSelectAIDHex(dctx, 0xEF2014, false, 0) == PM3_SUCCESS) {
|
||||
uint8_t buf[APDU_RES_LEN] = {0};
|
||||
size_t len = 0;
|
||||
|
||||
hrt_travel_card_t card;
|
||||
hrt_travelcard_init_empty(&card, 2);
|
||||
|
||||
// File 0x08: Application info (11 bytes)
|
||||
len = 0;
|
||||
if (DesfireReadFile(dctx, 0x08, 0, 11, buf, &len) == PM3_SUCCESS) {
|
||||
hrt_travelcard_set_application_info(&card, buf, len);
|
||||
}
|
||||
|
||||
// Check that application info parses before reading other files
|
||||
const char *application_id = hrt_travelcard_get_application_instance_id(&card);
|
||||
if (application_id == NULL || application_id[0] == '\0') {
|
||||
hrt_travelcard_free(&card);
|
||||
return false;
|
||||
}
|
||||
PrintAndLogEx(SUCCESS, "HRT travel card detected (AID EF2014)");
|
||||
|
||||
// File 0x00: Control info (10 bytes)
|
||||
len = 0;
|
||||
if (DesfireReadFile(dctx, 0x00, 0, 10, buf, &len) == PM3_SUCCESS) {
|
||||
hrt_travelcard_set_control_info(&card, buf, len);
|
||||
}
|
||||
|
||||
// File 0x01: Period pass (35 bytes)
|
||||
len = 0;
|
||||
if (DesfireReadFile(dctx, 0x01, 0, 35, buf, &len) == PM3_SUCCESS) {
|
||||
hrt_travelcard_set_period_pass(&card, buf, len);
|
||||
}
|
||||
|
||||
// File 0x02: Stored value (13 bytes)
|
||||
len = 0;
|
||||
if (DesfireReadFile(dctx, 0x02, 0, 13, buf, &len) == PM3_SUCCESS) {
|
||||
hrt_travelcard_set_stored_value(&card, buf, len);
|
||||
|
||||
// Demo: Read card stored value
|
||||
int stored_value = hrt_travelcard_get_stored_value_counter(&card);
|
||||
char price_buf[32] = {0};
|
||||
hrt_price_to_string(stored_value, price_buf, sizeof(price_buf));
|
||||
PrintAndLogEx(SUCCESS, "HRT stored value: %s", price_buf);
|
||||
}
|
||||
|
||||
// File 0x03: eTicket (45 bytes)
|
||||
len = 0;
|
||||
if (DesfireReadFile(dctx, 0x03, 0, 45, buf, &len) == PM3_SUCCESS) {
|
||||
hrt_travelcard_set_eticket(&card, buf, len);
|
||||
}
|
||||
|
||||
// File 0x04: History records (all)
|
||||
len = 0;
|
||||
if (DesfireReadRecords(dctx, 0x04, 0, 0, buf, &len) == PM3_SUCCESS) {
|
||||
hrt_travelcard_set_history(&card, buf, len);
|
||||
}
|
||||
|
||||
hrt_travelcard_free(&card);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int mfdes_get_info(mfdes_info_res_t *info) {
|
||||
|
||||
PacketResponseNG resp;
|
||||
@@ -1202,7 +1132,9 @@ static int CmdHF14ADesInfo(const char *Cmd) {
|
||||
|
||||
*/
|
||||
|
||||
(void)hf_mfdes_try_parse_hrt(&dctx);
|
||||
if (is_valid_hrt_card(&dctx, aidbuf, aidbuflen)) {
|
||||
(void)hrt_parser_parse(&dctx);
|
||||
}
|
||||
|
||||
DropField();
|
||||
return PM3_SUCCESS;
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
#endif
|
||||
|
||||
#include "cmdparser.h"
|
||||
#include "cmdparsehrt.h"
|
||||
#include "comms.h"
|
||||
#include "hrtparser.h"
|
||||
#include "common.h"
|
||||
#include "ui.h"
|
||||
#include <stdlib.h>
|
||||
@@ -812,7 +812,7 @@ static int CmdHelp(const char *Cmd) {
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
int CmdParseHRT(const char *Cmd) {
|
||||
int HRTParser(const char *Cmd) {
|
||||
clearCommandBuffer();
|
||||
return CmdsParse(CommandTable, Cmd);
|
||||
}
|
||||
@@ -16,8 +16,8 @@
|
||||
// Parse Helsinki Region Transport (HRT) travel cards
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef CMDParseHRT_H__
|
||||
#define CMDParseHRT_H__
|
||||
#ifndef HRTPARSER_H__
|
||||
#define HRTPARSER_H__
|
||||
|
||||
#include "common.h"
|
||||
#include <stddef.h>
|
||||
@@ -221,7 +221,7 @@ int hrt_travelcard_get_period_loading_device_number(const hrt_travel_card
|
||||
int hrt_travelcard_get_app_status(const hrt_travel_card_t *card);
|
||||
int hrt_travelcard_get_version(const hrt_travel_card_t *card);
|
||||
|
||||
int CmdParseHRT(const char *Cmd);
|
||||
int HRTParser(const char *Cmd);
|
||||
|
||||
// Setter Functions
|
||||
void hrt_travelcard_init_empty(hrt_travel_card_t *card, int version);
|
||||
@@ -0,0 +1,135 @@
|
||||
#include "hrtparser/hrtparser.h"
|
||||
#include "iso7816/iso7816core.h"
|
||||
#include "mifare/desfirecore.h"
|
||||
#include "mifare/desfirecrypto.h"
|
||||
#include "parsehrt.h"
|
||||
#include "pm3_cmd.h"
|
||||
#include "ui.h"
|
||||
|
||||
#define HRT_NEW_AID 0xEF2014
|
||||
#define HRT_OLD_AID 0xEF2011
|
||||
#define HRT_APPLICATION_INFO_FILE_ID 0x08
|
||||
#define HRT_APPLICATION_INFO_LEN 11
|
||||
|
||||
static bool hrt_aid_list_contains(const uint8_t *aidbuf, size_t aidbuflen) {
|
||||
if (aidbuf == NULL || aidbuflen < 3) return false;
|
||||
|
||||
for (size_t i = 0; i + 2 < aidbuflen; i += 3) {
|
||||
// TODO: Also add HRT_OLD_AID check. Currently it is not
|
||||
// checked since parsing the older cards is not yet implemented.
|
||||
if (DesfireAIDByteToUint(&aidbuf[i]) == HRT_NEW_AID) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool hrt_read_and_parse_application_info(DesfireContext_t *dctx, hrt_travel_card_t *card) {
|
||||
if (dctx == NULL || card == NULL) return false;
|
||||
|
||||
uint8_t buf[APDU_RES_LEN] = {0};
|
||||
size_t len = 0;
|
||||
|
||||
if (DesfireReadFile(dctx, HRT_APPLICATION_INFO_FILE_ID, 0, HRT_APPLICATION_INFO_LEN, buf, &len) != PM3_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (hrt_travelcard_set_application_info(card, buf, len) == false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *application_id = hrt_travelcard_get_application_instance_id(card);
|
||||
return application_id != NULL && application_id[0] != '\0';
|
||||
}
|
||||
|
||||
bool is_valid_hrt_card(DesfireContext_t *dctx, const uint8_t *aidbuf, size_t aidbuflen) {
|
||||
if (dctx == NULL) return false;
|
||||
|
||||
if (hrt_aid_list_contains(aidbuf, aidbuflen) == false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DesfireSetCommMode(dctx, DCMPlain);
|
||||
DesfireSetCommandSet(dctx, DCCNative);
|
||||
|
||||
// TODO: Add support for older travelcard (AID EF2011)
|
||||
|
||||
if (DesfireSelectAIDHex(dctx, HRT_NEW_AID, false, 0) != PM3_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
hrt_travel_card_t card;
|
||||
hrt_travelcard_init_empty(&card, 2);
|
||||
|
||||
bool is_valid = hrt_read_and_parse_application_info(dctx, &card);
|
||||
|
||||
hrt_travelcard_free(&card);
|
||||
return is_valid;
|
||||
}
|
||||
|
||||
bool hrt_parser_parse(DesfireContext_t *dctx) {
|
||||
if (dctx == NULL) return false;
|
||||
|
||||
DesfireSetCommMode(dctx, DCMPlain);
|
||||
DesfireSetCommandSet(dctx, DCCNative);
|
||||
|
||||
// TODO: Add support for older travelcard (AID EF2011)
|
||||
|
||||
if (DesfireSelectAIDHex(dctx, HRT_NEW_AID, false, 0) == PM3_SUCCESS) {
|
||||
uint8_t buf[APDU_RES_LEN] = {0};
|
||||
size_t len = 0;
|
||||
|
||||
hrt_travel_card_t card;
|
||||
hrt_travelcard_init_empty(&card, 2);
|
||||
|
||||
// File 0x08: Application info (11 bytes)
|
||||
// Check that application info parses before reading other files
|
||||
if (hrt_read_and_parse_application_info(dctx, &card) == false) {
|
||||
hrt_travelcard_free(&card);
|
||||
return false;
|
||||
}
|
||||
PrintAndLogEx(SUCCESS, "HRT travel card detected (AID %06X)", HRT_NEW_AID);
|
||||
|
||||
// File 0x00: Control info (10 bytes)
|
||||
len = 0;
|
||||
if (DesfireReadFile(dctx, 0x00, 0, 10, buf, &len) == PM3_SUCCESS) {
|
||||
hrt_travelcard_set_control_info(&card, buf, len);
|
||||
}
|
||||
|
||||
// File 0x01: Period pass (35 bytes)
|
||||
len = 0;
|
||||
if (DesfireReadFile(dctx, 0x01, 0, 35, buf, &len) == PM3_SUCCESS) {
|
||||
hrt_travelcard_set_period_pass(&card, buf, len);
|
||||
}
|
||||
|
||||
// File 0x02: Stored value (13 bytes)
|
||||
len = 0;
|
||||
if (DesfireReadFile(dctx, 0x02, 0, 13, buf, &len) == PM3_SUCCESS) {
|
||||
hrt_travelcard_set_stored_value(&card, buf, len);
|
||||
|
||||
// Demo: Read card stored value
|
||||
int stored_value = hrt_travelcard_get_stored_value_counter(&card);
|
||||
char price_buf[32] = {0};
|
||||
hrt_price_to_string(stored_value, price_buf, sizeof(price_buf));
|
||||
PrintAndLogEx(SUCCESS, "HRT stored value: %s", price_buf);
|
||||
}
|
||||
|
||||
// File 0x03: eTicket (45 bytes)
|
||||
len = 0;
|
||||
if (DesfireReadFile(dctx, 0x03, 0, 45, buf, &len) == PM3_SUCCESS) {
|
||||
hrt_travelcard_set_eticket(&card, buf, len);
|
||||
}
|
||||
|
||||
// File 0x04: History records (all)
|
||||
len = 0;
|
||||
if (DesfireReadRecords(dctx, 0x04, 0, 0, buf, &len) == PM3_SUCCESS) {
|
||||
hrt_travelcard_set_history(&card, buf, len);
|
||||
}
|
||||
|
||||
hrt_travelcard_free(&card);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef PARSEHRT_H__
|
||||
#define PARSEHRT_H__
|
||||
|
||||
#include "mifare/desfirecore.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
bool is_valid_hrt_card(DesfireContext_t *dctx, const uint8_t *aidbuf, size_t aidbuflen);
|
||||
bool hrt_parser_parse(DesfireContext_t *dctx);
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user