transit parsers: identify API for info commands
Each card system can now be named from the partial data an info command already has: sector keys and MAD block 1 for MIFARE Classic, the application list and ATS for DESFire, system codes for FeliCa, and the same data a dump has for Ultralight, ST25TB and Calypso. Dispatch rows gain a display name, region, decode hint and identify entry. Detection is shared, not duplicated: the MFC key checks become key sets matched against either a dump or the keys an info probe found, the DESFire checks run over an application list built from the dump. transit_mfdes_summary_files() lists the free files a parser decodes card number and balance from. Every identify path has a positive and a negative selftest. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4c4a719686
commit
a0a3d52d52
44 files changed
+824
-163
No files matched your search
@@ -29,24 +29,18 @@
|
||||
#define BIP_BLK(s, b) ((((s) * 4) + (b)) * 16)
|
||||
|
||||
// Key A and key B of sector 0, key A and key B of sector 1
|
||||
static const uint64_t bip_keys[4] = {
|
||||
0x3A42F33AF429, 0x1FC235AC1309, 0x6338A371C0ED, 0x243F160918D1
|
||||
static const transit_mfc_key_t bip_keys[4] = {
|
||||
{0, TRANSIT_MFC_KEY_A, 0x3A42F33AF429}, {0, TRANSIT_MFC_KEY_B, 0x1FC235AC1309},
|
||||
{1, TRANSIT_MFC_KEY_A, 0x6338A371C0ED}, {1, TRANSIT_MFC_KEY_B, 0x243F160918D1},
|
||||
};
|
||||
|
||||
static uint64_t bip_key(const uint8_t *dump, uint8_t sector, bool key_b) {
|
||||
return MemBeToUint6byte(dump + BIP_BLK(sector, 3) + (key_b ? 10 : 0));
|
||||
}
|
||||
|
||||
static bool bip_sector_key_ok(const uint8_t *dump, uint8_t sector) {
|
||||
return bip_key(dump, sector, false) == bip_keys[sector * 2] ||
|
||||
bip_key(dump, sector, true) == bip_keys[(sector * 2) + 1];
|
||||
}
|
||||
const transit_mfc_keyset_t bip_keyset = { bip_keys, ARRAYLEN(bip_keys), false };
|
||||
|
||||
bool is_valid_bip_card(const uint8_t *dump, size_t dumplen) {
|
||||
if (dump == NULL || dumplen < BIP_DUMP_MIN) {
|
||||
return false;
|
||||
}
|
||||
return bip_sector_key_ok(dump, 0) && bip_sector_key_ok(dump, 1);
|
||||
return transit_mfc_keyset_match_dump(&bip_keyset, dump, dumplen);
|
||||
}
|
||||
|
||||
// Timestamp packed least significant bit first in the first five bytes
|
||||
@@ -173,8 +167,8 @@ int bip_selftest(void) {
|
||||
for (uint8_t s = 0; s < 2; s++) {
|
||||
uint8_t *t = d + BIP_BLK(s, 3);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
t[i] = (uint8_t)(bip_keys[s * 2] >> (40 - (8 * i)));
|
||||
t[10 + i] = (uint8_t)(bip_keys[(s * 2) + 1] >> (40 - (8 * i)));
|
||||
t[i] = (uint8_t)(bip_keys[s * 2].key >> (40 - (8 * i)));
|
||||
t[10 + i] = (uint8_t)(bip_keys[(s * 2) + 1].key >> (40 - (8 * i)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,6 +194,7 @@ int bip_selftest(void) {
|
||||
|
||||
bool ok = true;
|
||||
ok &= transit_check("detected", is_valid_bip_card(d, sizeof(d)), true);
|
||||
ok &= transit_mfc_check_identify("identify", d, sizeof(d), "Bip!", TRANSIT_ID_MATCH);
|
||||
ok &= transit_check("card number", MemLeToUint4byte(d + BIP_BLK(0, 1) + 4), 12345678);
|
||||
ok &= transit_check("balance, negated", (uint64_t)(-bip_balance(d)), 1500);
|
||||
ok &= transit_check("holder id", bip_holder_id(d), 7654321);
|
||||
@@ -215,6 +210,7 @@ int bip_selftest(void) {
|
||||
ok &= transit_check("sector 1 key B alone", is_valid_bip_card(d, sizeof(d)), true);
|
||||
d[BIP_BLK(1, 3) + 10] ^= 0xFF;
|
||||
ok &= transit_check("no sector 1 key", is_valid_bip_card(d, sizeof(d)), false);
|
||||
ok &= transit_mfc_check_identify("not identified", d, sizeof(d), "Bip!", TRANSIT_ID_NONE);
|
||||
ok &= transit_check("short dump", is_valid_bip_card(d, 512), false);
|
||||
|
||||
return transit_selftest_result("Bip!", ok);
|
||||
|
||||
@@ -23,6 +23,10 @@
|
||||
#define PARSEBIP_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "parsetransit.h" // transit_mfc_keyset_t
|
||||
|
||||
// Sector 0 and 1 keys, identifying for info commands too
|
||||
extern const transit_mfc_keyset_t bip_keyset;
|
||||
|
||||
// True when the sector 0 and 1 keys are the Bip! ones
|
||||
bool is_valid_bip_card(const uint8_t *dump, size_t dumplen);
|
||||
|
||||
@@ -24,33 +24,26 @@
|
||||
#include "commonutil.h" // MemBeToUint4byte
|
||||
#include "ui.h" // PrintAndLogEx
|
||||
#include "parsetransit.h"
|
||||
#include "parsetransitmfc.h"
|
||||
|
||||
#define CHARLIE_DUMP_MIN 1024
|
||||
#define CHARLIE_BLK(s, b) ((((s) * 4) + (b)) * 16)
|
||||
#define CHARLIE_TRIPS 12
|
||||
|
||||
// Sector 0 keys, either may sit in A or B depending on the dump
|
||||
static const uint64_t charlie_s0_keys[2] = { 0x3060206F5B0A, 0xF1B9F5669CC8 };
|
||||
// Data sector keys, A and B
|
||||
static const uint64_t charlie_data_keys[2] = { 0x5EC39B022F2B, 0xF662248E7E89 };
|
||||
// Sector 0 keys, either may sit in A or B depending on the dump,
|
||||
// then the data sector keys, A and B
|
||||
static const transit_mfc_key_t charlie_keys[4] = {
|
||||
{0, TRANSIT_MFC_KEY_AB, 0x3060206F5B0A}, {0, TRANSIT_MFC_KEY_AB, 0xF1B9F5669CC8},
|
||||
{1, TRANSIT_MFC_KEY_AB, 0x5EC39B022F2B}, {1, TRANSIT_MFC_KEY_AB, 0xF662248E7E89},
|
||||
};
|
||||
|
||||
static uint64_t charlie_key(const uint8_t *dump, uint8_t sector, bool key_b) {
|
||||
return MemBeToUint6byte(dump + CHARLIE_BLK(sector, 3) + (key_b ? 10 : 0));
|
||||
}
|
||||
|
||||
static bool charlie_key_in(uint64_t k, const uint64_t *set) {
|
||||
return k == set[0] || k == set[1];
|
||||
}
|
||||
const transit_mfc_keyset_t charlie_keyset = { charlie_keys, ARRAYLEN(charlie_keys), false };
|
||||
|
||||
bool is_valid_charlie_card(const uint8_t *dump, size_t dumplen) {
|
||||
if (dump == NULL || dumplen < CHARLIE_DUMP_MIN) {
|
||||
return false;
|
||||
}
|
||||
bool s0 = charlie_key_in(charlie_key(dump, 0, false), charlie_s0_keys) ||
|
||||
charlie_key_in(charlie_key(dump, 0, true), charlie_s0_keys);
|
||||
bool s1 = charlie_key_in(charlie_key(dump, 1, false), charlie_data_keys) ||
|
||||
charlie_key_in(charlie_key(dump, 1, true), charlie_data_keys);
|
||||
return s0 && s1;
|
||||
return transit_mfc_keyset_match_dump(&charlie_keyset, dump, dumplen);
|
||||
}
|
||||
|
||||
// Money in half cents, top bit is the sign
|
||||
@@ -184,10 +177,8 @@ int charlie_selftest(void) {
|
||||
memset(d, 0, sizeof(d));
|
||||
|
||||
charlie_put_be(d, 0x12345678, 4);
|
||||
charlie_put_be(d + CHARLIE_BLK(0, 3), (uint32_t)(charlie_s0_keys[1] >> 16), 4);
|
||||
charlie_put_be(d + CHARLIE_BLK(0, 3) + 4, (uint32_t)(charlie_s0_keys[1] & 0xFFFF), 2);
|
||||
charlie_put_be(d + CHARLIE_BLK(1, 3), (uint32_t)(charlie_data_keys[0] >> 16), 4);
|
||||
charlie_put_be(d + CHARLIE_BLK(1, 3) + 4, (uint32_t)(charlie_data_keys[0] & 0xFFFF), 2);
|
||||
transit_mfc_set_key(d, sizeof(d), 0, false, charlie_keys[1].key);
|
||||
transit_mfc_set_key(d, sizeof(d), 1, false, charlie_keys[2].key);
|
||||
|
||||
// sector 3 newer than sector 2, adult card, $12.50, issued 2020-01-01 00:00
|
||||
transit_set_bits_be(d + CHARLIE_BLK(2, 0), 16, 81, 16, 4);
|
||||
@@ -206,6 +197,7 @@ int charlie_selftest(void) {
|
||||
|
||||
bool ok = true;
|
||||
ok &= transit_check("detected", is_valid_charlie_card(d, sizeof(d)), true);
|
||||
ok &= transit_mfc_check_identify("identify", d, sizeof(d), "CharlieCard", TRANSIT_ID_MATCH);
|
||||
ok &= transit_check("balance sector", charlie_balance_sector(d), 3);
|
||||
ok &= transit_check("balance cents", (uint64_t)charlie_price(b1 + 5), 1250);
|
||||
ok &= transit_check("card type", MemBeToUint2byte(b1) >> 6, 367);
|
||||
@@ -218,6 +210,7 @@ int charlie_selftest(void) {
|
||||
|
||||
d[CHARLIE_BLK(1, 3)] ^= 0xFF;
|
||||
ok &= transit_check("wrong data key", is_valid_charlie_card(d, sizeof(d)), false);
|
||||
ok &= transit_mfc_check_identify("not identified", d, sizeof(d), "CharlieCard", TRANSIT_ID_NONE);
|
||||
|
||||
return transit_selftest_result("CharlieCard", ok);
|
||||
}
|
||||
@@ -23,6 +23,10 @@
|
||||
#define PARSECHARLIE_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "parsetransit.h" // transit_mfc_keyset_t
|
||||
|
||||
// Sector 0 and 1 keys, identifying for info commands too
|
||||
extern const transit_mfc_keyset_t charlie_keyset;
|
||||
|
||||
// True when the sector 0 and 1 keys are the CharlieCard ones
|
||||
bool is_valid_charlie_card(const uint8_t *dump, size_t dumplen);
|
||||
|
||||
@@ -27,12 +27,6 @@
|
||||
#include "util.h" // sprint_hex_inrow
|
||||
#include "parsetransit.h"
|
||||
|
||||
#define CLIPPER_FID_INFO 0x01 // expiry
|
||||
#define CLIPPER_FID_ECASH 0x02 // balance
|
||||
#define CLIPPER_FID_REFILLS 0x04
|
||||
#define CLIPPER_FID_IDENTITY 0x08 // serial
|
||||
#define CLIPPER_FID_HISTORY 0x0E
|
||||
|
||||
#define CLIPPER_RECORD_LEN 32
|
||||
#define CLIPPER_MAX_RECORDS 64
|
||||
|
||||
@@ -284,8 +278,14 @@ static size_t clipper_collect_trips(const uint8_t *d, uint32_t len, clipper_trip
|
||||
// Detection and printing
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
transit_id_t clipper_identify(const transit_mfdes_info_t *info) {
|
||||
return (transit_mfdes_info_has_aid(info, CLIPPER_AID_CARD) || transit_mfdes_info_has_aid(info, CLIPPER_AID_MOBILE)) ? TRANSIT_ID_MATCH : TRANSIT_ID_NONE;
|
||||
}
|
||||
|
||||
bool is_valid_clipper_card(const desfire_dump_t *dump) {
|
||||
return (clipper_find_app(dump) != NULL);
|
||||
transit_mfdes_info_t info;
|
||||
transit_mfdes_info_from_dump(dump, &info);
|
||||
return clipper_identify(&info) == TRANSIT_ID_MATCH;
|
||||
}
|
||||
|
||||
static void clipper_print_trips(const desfire_dump_app_t *app) {
|
||||
@@ -447,6 +447,12 @@ int clipper_selftest(void) {
|
||||
|
||||
bool ok = true;
|
||||
ok &= transit_check("detected", is_valid_clipper_card(dump), true);
|
||||
ok &= transit_mfdes_check_identify("identify", dump, "Clipper", TRANSIT_ID_MATCH);
|
||||
|
||||
transit_mfdes_info_t info;
|
||||
transit_mfdes_info_from_dump(dump, &info);
|
||||
transit_mfdes_file_t sf[4];
|
||||
ok &= transit_check("summary files", transit_mfdes_summary_files(&info, sf, ARRAYLEN(sf)), 2);
|
||||
ok &= transit_check("serial", MemBeToUint4byte(clipper_file_data(app, CLIPPER_FID_IDENTITY, 5, NULL) + 1), 572691763);
|
||||
ok &= transit_check("balance cents", (uint16_t)MemBeToUint2byte(ecash + 0x12), 30583);
|
||||
|
||||
@@ -479,6 +485,7 @@ int clipper_selftest(void) {
|
||||
|
||||
app->aid = 0x534531;
|
||||
ok &= transit_check("other AID rejected", is_valid_clipper_card(dump), false);
|
||||
ok &= transit_mfdes_check_identify("not identified", dump, "Clipper", TRANSIT_ID_NONE);
|
||||
app->aid = CLIPPER_AID_CARD;
|
||||
|
||||
if (ok) {
|
||||
|
||||
@@ -24,12 +24,23 @@
|
||||
|
||||
#include "common.h"
|
||||
#include "fileutils.h" // desfire_dump_t
|
||||
#include "parsetransit.h" // transit_mfdes_info_t
|
||||
|
||||
// AIDs as pm3 holds them, wire order 90 11 F2 and 91 11 F2
|
||||
#define CLIPPER_AID_CARD 0xF21190
|
||||
#define CLIPPER_AID_MOBILE 0xF21191
|
||||
|
||||
#define CLIPPER_FID_INFO 0x01 // expiry
|
||||
#define CLIPPER_FID_ECASH 0x02 // balance
|
||||
#define CLIPPER_FID_REFILLS 0x04
|
||||
#define CLIPPER_FID_IDENTITY 0x08 // serial
|
||||
#define CLIPPER_FID_HISTORY 0x0E
|
||||
|
||||
bool is_valid_clipper_card(const desfire_dump_t *dump);
|
||||
|
||||
// Card or mobile AID in the application list
|
||||
transit_id_t clipper_identify(const transit_mfdes_info_t *info);
|
||||
|
||||
int clipper_parser_parse(const desfire_dump_t *dump);
|
||||
int clipper_selftest(void);
|
||||
|
||||
|
||||
@@ -662,6 +662,8 @@ static bool intercode_selftest_common(bool navigo) {
|
||||
ok &= transit_check("network id", calypso_intercode_network(&card), net);
|
||||
ok &= transit_check("navigo detect", is_valid_navigo_card(&card), navigo);
|
||||
ok &= transit_check("intercode detect", is_valid_intercode_card(&card), navigo == false);
|
||||
ok &= transit_calypso_check_identify("navigo identify", &card, "Navigo", navigo ? TRANSIT_ID_MATCH : TRANSIT_ID_NONE);
|
||||
ok &= transit_calypso_check_identify("intercode identify", &card, "Intercode", navigo ? TRANSIT_ID_NONE : TRANSIT_ID_MATCH);
|
||||
|
||||
en1545_parsed_t env;
|
||||
(void)en1545_parse(card.env.data, card.env.len, 0, &calypso_intercode_env, &env);
|
||||
|
||||
@@ -461,6 +461,7 @@ int intertic_selftest(void) {
|
||||
const intertic_network_t *n = intertic_find_network(d.country, d.authority, d.provider);
|
||||
ok &= transit_check_str("network", n ? n->city : NULL, "Rennes");
|
||||
ok &= transit_check("detected", is_valid_intertic_card(card, sizeof(card), NULL), 1);
|
||||
ok &= transit_st25tb_check_identify("identify", card, sizeof(card), "Intertic", TRANSIT_ID_POSSIBLE);
|
||||
ok &= transit_check("parse", (uint64_t)intertic_parser_parse(card, sizeof(card), NULL), PM3_SUCCESS);
|
||||
|
||||
// PID 10, distribution runs 32..95 and on into block 0, usage A is split around the counter
|
||||
@@ -487,6 +488,7 @@ int intertic_selftest(void) {
|
||||
ok &= transit_check("unknown pid rejected", is_valid_intertic_card(card, sizeof(card), NULL), 0);
|
||||
memset(card, 0xFF, sizeof(card));
|
||||
ok &= transit_check("blank rejected", is_valid_intertic_card(card, sizeof(card), NULL), 0);
|
||||
ok &= transit_st25tb_check_identify("not identified", card, sizeof(card), "Intertic", TRANSIT_ID_NONE);
|
||||
|
||||
return transit_selftest_result("Intertic", ok);
|
||||
}
|
||||
@@ -73,8 +73,14 @@ static bool itso_isrn_str(const uint8_t *d, char *out, size_t outlen) {
|
||||
return true;
|
||||
}
|
||||
|
||||
transit_id_t itso_identify(const transit_mfdes_info_t *info) {
|
||||
return (transit_mfdes_info_has_aid(info, ITSO_AID)) ? TRANSIT_ID_MATCH : TRANSIT_ID_NONE;
|
||||
}
|
||||
|
||||
bool is_valid_itso_card(const desfire_dump_t *dump) {
|
||||
return (itso_find_app(dump) != NULL);
|
||||
transit_mfdes_info_t info;
|
||||
transit_mfdes_info_from_dump(dump, &info);
|
||||
return itso_identify(&info) == TRANSIT_ID_MATCH;
|
||||
}
|
||||
|
||||
int itso_parser_parse(const desfire_dump_t *dump) {
|
||||
@@ -141,9 +147,12 @@ int itso_selftest(void) {
|
||||
dump->app[0].files[0].data = shell;
|
||||
dump->app[0].files[0].datalen = sizeof(shell);
|
||||
ok &= transit_check("detected", is_valid_itso_card(dump), true);
|
||||
ok &= transit_mfdes_check_identify("identify", dump, "ITSO", TRANSIT_ID_MATCH);
|
||||
if (ok) {
|
||||
itso_parser_parse(dump);
|
||||
}
|
||||
dump->app[0].aid = 0x000001;
|
||||
ok &= transit_mfdes_check_identify("not identified", dump, "ITSO", TRANSIT_ID_NONE);
|
||||
free(dump);
|
||||
|
||||
return transit_selftest_result("ITSO", ok);
|
||||
|
||||
@@ -23,12 +23,17 @@
|
||||
|
||||
#include "common.h"
|
||||
#include "fileutils.h" // desfire_dump_t
|
||||
#include "parsetransit.h" // transit_mfdes_info_t
|
||||
|
||||
// AID as pm3 holds it, wire order 16 02 A0
|
||||
#define ITSO_AID 0xA00216
|
||||
#define ITSO_FID_SHELL 0x0F
|
||||
|
||||
bool is_valid_itso_card(const desfire_dump_t *dump);
|
||||
|
||||
// ITSO AID in the application list
|
||||
transit_id_t itso_identify(const transit_mfdes_info_t *info);
|
||||
|
||||
int itso_parser_parse(const desfire_dump_t *dump);
|
||||
int itso_selftest(void);
|
||||
|
||||
|
||||
@@ -31,11 +31,13 @@
|
||||
#define KAZAN_TYPE_PURSE 0x53
|
||||
|
||||
// Sector 8 keys of the known key sets
|
||||
static const uint64_t kazan_keys[] = {
|
||||
0xE954024EE754, 0x0CD464CDC100,
|
||||
0x2058EAEE8446, 0xCB9B23815F87,
|
||||
static const transit_mfc_key_t kazan_keys[] = {
|
||||
{KAZAN_TICKET_SECTOR, TRANSIT_MFC_KEY_AB, 0xE954024EE754}, {KAZAN_TICKET_SECTOR, TRANSIT_MFC_KEY_AB, 0x0CD464CDC100},
|
||||
{KAZAN_TICKET_SECTOR, TRANSIT_MFC_KEY_AB, 0x2058EAEE8446}, {KAZAN_TICKET_SECTOR, TRANSIT_MFC_KEY_AB, 0xCB9B23815F87},
|
||||
};
|
||||
|
||||
const transit_mfc_keyset_t kazan_keyset = { kazan_keys, ARRAYLEN(kazan_keys), false };
|
||||
|
||||
typedef struct {
|
||||
uint8_t type;
|
||||
const char *name;
|
||||
@@ -57,12 +59,7 @@ bool is_valid_kazan_card(const uint8_t *dump, size_t dumplen) {
|
||||
if (transit_mfc_block(dump, dumplen, KAZAN_COUNTER_SECTOR, 3) == NULL) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < ARRAYLEN(kazan_keys); i++) {
|
||||
if (transit_mfc_has_key(dump, dumplen, KAZAN_TICKET_SECTOR, kazan_keys[i])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return transit_mfc_keyset_match_dump(&kazan_keyset, dump, dumplen);
|
||||
}
|
||||
|
||||
// YY MM DD, false when unset or out of range
|
||||
@@ -139,6 +136,7 @@ int kazan_selftest(void) {
|
||||
dump[36 * 16] = 150;
|
||||
|
||||
bool ok = transit_check("detected", is_valid_kazan_card(dump, sizeof(dump)), 1);
|
||||
ok &= transit_mfc_check_identify("identify", dump, sizeof(dump), "Kazan", TRANSIT_ID_MATCH);
|
||||
ok &= transit_check("card number", MemLeToUint4byte(dump), 0x12345678);
|
||||
ok &= transit_check("counter", MemLeToUint4byte(dump + (36 * 16)), 150);
|
||||
|
||||
@@ -153,6 +151,7 @@ int kazan_selftest(void) {
|
||||
|
||||
transit_mfc_set_key(dump, sizeof(dump), KAZAN_TICKET_SECTOR, false, 0xFFFFFFFFFFFF);
|
||||
ok &= transit_check("wrong key rejected", is_valid_kazan_card(dump, sizeof(dump)), 0);
|
||||
ok &= transit_mfc_check_identify("not identified", dump, sizeof(dump), "Kazan", TRANSIT_ID_NONE);
|
||||
|
||||
return transit_selftest_result("Kazan", ok);
|
||||
}
|
||||
@@ -23,6 +23,10 @@
|
||||
#define PARSEKAZAN_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "parsetransit.h" // transit_mfc_keyset_t
|
||||
|
||||
// Sector 8 keys, identifying for info commands too
|
||||
extern const transit_mfc_keyset_t kazan_keyset;
|
||||
|
||||
// Sector 8 under one of the Kazan key sets
|
||||
bool is_valid_kazan_card(const uint8_t *dump, size_t dumplen);
|
||||
|
||||
@@ -33,13 +33,18 @@
|
||||
// The stored balance carries a +1.00 GEL offset (Flipper), Metrodroid shows it raw
|
||||
#define METROMONEY_OFFSET 100
|
||||
|
||||
static const transit_mfc_key_t metromoney_keys[] = {
|
||||
{0, TRANSIT_MFC_KEY_AB, METROMONEY_S0_KEY_A}, {0, TRANSIT_MFC_KEY_AB, METROMONEY_S0_KEY_B},
|
||||
{1, TRANSIT_MFC_KEY_AB, METROMONEY_S1_KEY_A}, {1, TRANSIT_MFC_KEY_AB, METROMONEY_S1_KEY_B},
|
||||
};
|
||||
|
||||
const transit_mfc_keyset_t metromoney_keyset = { metromoney_keys, ARRAYLEN(metromoney_keys), false };
|
||||
|
||||
bool is_valid_metromoney_card(const uint8_t *dump, size_t dumplen) {
|
||||
if (transit_mfc_block(dump, dumplen, 1, 3) == NULL) {
|
||||
return false;
|
||||
}
|
||||
bool s0 = transit_mfc_has_key(dump, dumplen, 0, METROMONEY_S0_KEY_A) || transit_mfc_has_key(dump, dumplen, 0, METROMONEY_S0_KEY_B);
|
||||
bool s1 = transit_mfc_has_key(dump, dumplen, 1, METROMONEY_S1_KEY_A) || transit_mfc_has_key(dump, dumplen, 1, METROMONEY_S1_KEY_B);
|
||||
return s0 && s1;
|
||||
return transit_mfc_keyset_match_dump(&metromoney_keyset, dump, dumplen);
|
||||
}
|
||||
|
||||
int metromoney_parser_parse(const uint8_t *dump, size_t dumplen) {
|
||||
@@ -74,11 +79,13 @@ int metromoney_selftest(void) {
|
||||
memcpy(dump + (5 * 16), value, sizeof(value));
|
||||
|
||||
bool ok = transit_check("detected", is_valid_metromoney_card(dump, sizeof(dump)), 1);
|
||||
ok &= transit_mfc_check_identify("identify", dump, sizeof(dump), "Metromoney", TRANSIT_ID_MATCH);
|
||||
ok &= transit_check("card number", MemLeToUint4byte(dump), 0xDEADBEEF);
|
||||
ok &= transit_check("stored value", MemLeToUint4byte(dump + (5 * 16)), 1234);
|
||||
|
||||
transit_mfc_set_key(dump, sizeof(dump), 0, false, 0xFFFFFFFFFFFF);
|
||||
ok &= transit_check("sector 0 key required", is_valid_metromoney_card(dump, sizeof(dump)), 0);
|
||||
ok &= transit_mfc_check_identify("not identified", dump, sizeof(dump), "Metromoney", TRANSIT_ID_NONE);
|
||||
|
||||
return transit_selftest_result("Metromoney", ok);
|
||||
}
|
||||
@@ -23,6 +23,10 @@
|
||||
#define PARSEMETROMONEY_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "parsetransit.h" // transit_mfc_keyset_t
|
||||
|
||||
// Sector 0 and 1 keys, identifying for info commands too
|
||||
extern const transit_mfc_keyset_t metromoney_keyset;
|
||||
|
||||
// Sectors 0 and 1 under the Metromoney keys
|
||||
bool is_valid_metromoney_card(const uint8_t *dump, size_t dumplen);
|
||||
|
||||
@@ -80,8 +80,14 @@ static bool myki_serial_str(const uint8_t *d, char *out, size_t outlen) {
|
||||
return true;
|
||||
}
|
||||
|
||||
transit_id_t myki_identify(const transit_mfdes_info_t *info) {
|
||||
return (transit_mfdes_info_has_aid(info, MYKI_AID_1) && transit_mfdes_info_has_aid(info, MYKI_AID_2)) ? TRANSIT_ID_MATCH : TRANSIT_ID_NONE;
|
||||
}
|
||||
|
||||
bool is_valid_myki_card(const desfire_dump_t *dump) {
|
||||
return (myki_find_app(dump, MYKI_AID_1) != NULL) && (myki_find_app(dump, MYKI_AID_2) != NULL);
|
||||
transit_mfdes_info_t info;
|
||||
transit_mfdes_info_from_dump(dump, &info);
|
||||
return myki_identify(&info) == TRANSIT_ID_MATCH;
|
||||
}
|
||||
|
||||
int myki_parser_parse(const desfire_dump_t *dump) {
|
||||
@@ -141,11 +147,13 @@ int myki_selftest(void) {
|
||||
dump->app[0].files[0].datalen = sizeof(file);
|
||||
dump->app[1].aid = MYKI_AID_2;
|
||||
ok &= transit_check("detected", is_valid_myki_card(dump), true);
|
||||
ok &= transit_mfdes_check_identify("identify", dump, "myki", TRANSIT_ID_MATCH);
|
||||
if (ok) {
|
||||
myki_parser_parse(dump);
|
||||
}
|
||||
dump->appcount = 1;
|
||||
ok &= transit_check("one AID only rejected", is_valid_myki_card(dump), false);
|
||||
ok &= transit_mfdes_check_identify("not identified", dump, "myki", TRANSIT_ID_NONE);
|
||||
free(dump);
|
||||
|
||||
return transit_selftest_result("myki", ok);
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
|
||||
#include "common.h"
|
||||
#include "fileutils.h" // desfire_dump_t
|
||||
#include "parsetransit.h" // transit_mfdes_info_t
|
||||
|
||||
// AIDs as pm3 holds them, wire order 00 11 F2 and F0 10 F2
|
||||
#define MYKI_AID_1 0xF21100
|
||||
@@ -31,6 +32,10 @@
|
||||
#define MYKI_FID_SERIAL 0x0F
|
||||
|
||||
bool is_valid_myki_card(const desfire_dump_t *dump);
|
||||
|
||||
// Both myki AIDs in the application list
|
||||
transit_id_t myki_identify(const transit_mfdes_info_t *info);
|
||||
|
||||
int myki_parser_parse(const desfire_dump_t *dump);
|
||||
int myki_selftest(void);
|
||||
|
||||
|
||||
@@ -45,11 +45,19 @@ typedef enum {
|
||||
NF_MSPGOTO,
|
||||
} nf_system_t;
|
||||
|
||||
static bool nf_block1_match(const uint8_t *block1) {
|
||||
return (block1 != NULL) && (memcmp(block1 + 1, nf_manufacturer, sizeof(nf_manufacturer)) == 0);
|
||||
}
|
||||
|
||||
bool is_valid_nextfare_card(const uint8_t *dump, size_t dumplen) {
|
||||
if (dump == NULL || dumplen < NF_DUMP_MIN) {
|
||||
return false;
|
||||
}
|
||||
return memcmp(dump + NF_BLK(0, 1) + 1, nf_manufacturer, sizeof(nf_manufacturer)) == 0;
|
||||
return nf_block1_match(dump + NF_BLK(0, 1));
|
||||
}
|
||||
|
||||
transit_id_t nextfare_identify(const transit_mfc_info_t *info) {
|
||||
return nf_block1_match(info->block1) ? TRANSIT_ID_MATCH : TRANSIT_ID_NONE;
|
||||
}
|
||||
|
||||
static nf_system_t nf_system(const uint8_t *dump) {
|
||||
@@ -355,6 +363,7 @@ int nextfare_selftest(void) {
|
||||
memcpy(d + NF_BLK(1, 0), bal[0], 16);
|
||||
memcpy(d + NF_BLK(1, 2), cfg[0], 16);
|
||||
ok &= transit_check("detected", is_valid_nextfare_card(d, sizeof(d)), true);
|
||||
ok &= transit_mfc_check_identify("identify", d, sizeof(d), "Nextfare", TRANSIT_ID_MATCH);
|
||||
ok &= transit_check("Go card system", nf_system(d), NF_SEQGO);
|
||||
ok &= transit_check("parse", nextfare_parser_parse(d, sizeof(d)), PM3_SUCCESS);
|
||||
|
||||
@@ -366,5 +375,8 @@ int nextfare_selftest(void) {
|
||||
memset(d + NF_BLK(0, 2), 0xFF, 16);
|
||||
ok &= transit_check("unknown system", nf_system(d), NF_UNKNOWN);
|
||||
|
||||
d[NF_BLK(0, 1) + 1] ^= 0xFF;
|
||||
ok &= transit_mfc_check_identify("not identified", d, sizeof(d), "Nextfare", TRANSIT_ID_NONE);
|
||||
|
||||
return transit_selftest_result("Nextfare", ok);
|
||||
}
|
||||
@@ -24,10 +24,14 @@
|
||||
#define PARSENEXTFARE_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "parsetransit.h" // transit_mfc_info_t
|
||||
|
||||
// True when sector 0 block 1 carries the Nextfare manufacturer signature
|
||||
bool is_valid_nextfare_card(const uint8_t *dump, size_t dumplen);
|
||||
|
||||
// Same signature in the block 1 an info command read
|
||||
transit_id_t nextfare_identify(const transit_mfc_info_t *info);
|
||||
|
||||
// Card number, balance, ticket type, expiry, top ups and taps
|
||||
int nextfare_parser_parse(const uint8_t *dump, size_t dumplen);
|
||||
|
||||
|
||||
@@ -66,8 +66,14 @@ static void nol_serial_str(const uint8_t *d, char *out, size_t outlen) {
|
||||
snprintf(out, outlen, "%03u %03u %04u", (s / 10000000U) % 1000U, (s / 10000U) % 1000U, s % 10000U);
|
||||
}
|
||||
|
||||
transit_id_t nol_identify(const transit_mfdes_info_t *info) {
|
||||
return (transit_mfdes_info_has_aid(info, NOL_AID_TRANSIT) && transit_mfdes_info_has_aid(info, NOL_AID_SERIAL)) ? TRANSIT_ID_MATCH : TRANSIT_ID_NONE;
|
||||
}
|
||||
|
||||
bool is_valid_nol_card(const desfire_dump_t *dump) {
|
||||
return (nol_find_app(dump, NOL_AID_TRANSIT) != NULL) && (nol_find_app(dump, NOL_AID_SERIAL) != NULL);
|
||||
transit_mfdes_info_t info;
|
||||
transit_mfdes_info_from_dump(dump, &info);
|
||||
return nol_identify(&info) == TRANSIT_ID_MATCH;
|
||||
}
|
||||
|
||||
int nol_parser_parse(const desfire_dump_t *dump) {
|
||||
@@ -125,11 +131,13 @@ int nol_selftest(void) {
|
||||
dump->app[1].files[0].data = file;
|
||||
dump->app[1].files[0].datalen = sizeof(file);
|
||||
ok &= transit_check("detected", is_valid_nol_card(dump), true);
|
||||
ok &= transit_mfdes_check_identify("identify", dump, "nol", TRANSIT_ID_MATCH);
|
||||
if (ok) {
|
||||
nol_parser_parse(dump);
|
||||
}
|
||||
dump->app[0].aid = 0x000001;
|
||||
ok &= transit_check("FFFFFF alone rejected", is_valid_nol_card(dump), false);
|
||||
ok &= transit_mfdes_check_identify("not identified", dump, "nol", TRANSIT_ID_NONE);
|
||||
free(dump);
|
||||
|
||||
return transit_selftest_result("nol", ok);
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
|
||||
#include "common.h"
|
||||
#include "fileutils.h" // desfire_dump_t
|
||||
#include "parsetransit.h" // transit_mfdes_info_t
|
||||
|
||||
// AIDs as pm3 holds them, wire order 00 40 78 and FF FF FF
|
||||
#define NOL_AID_TRANSIT 0x784000
|
||||
@@ -31,6 +32,10 @@
|
||||
#define NOL_FID_SERIAL 0x08
|
||||
|
||||
bool is_valid_nol_card(const desfire_dump_t *dump);
|
||||
|
||||
// Both nol AIDs in the application list
|
||||
transit_id_t nol_identify(const transit_mfdes_info_t *info);
|
||||
|
||||
int nol_parser_parse(const desfire_dump_t *dump);
|
||||
int nol_selftest(void);
|
||||
|
||||
|
||||
@@ -46,6 +46,14 @@ bool is_valid_octopus_card(const transit_felica_card_t *card) {
|
||||
(octopus_block(card, SZT_SYSTEM_CODE, SZT_SERVICE_BALANCE) != NULL);
|
||||
}
|
||||
|
||||
transit_id_t octopus_identify(const uint16_t *system_codes, size_t count) {
|
||||
return transit_felica_has_system_code(system_codes, count, OCTOPUS_SYSTEM_CODE) ? TRANSIT_ID_MATCH : TRANSIT_ID_NONE;
|
||||
}
|
||||
|
||||
transit_id_t szt_identify(const uint16_t *system_codes, size_t count) {
|
||||
return transit_felica_has_system_code(system_codes, count, SZT_SYSTEM_CODE) ? TRANSIT_ID_MATCH : TRANSIT_ID_NONE;
|
||||
}
|
||||
|
||||
int octopus_parser_parse(const transit_felica_card_t *card) {
|
||||
|
||||
if (is_valid_octopus_card(card) == false) {
|
||||
@@ -111,6 +119,10 @@ int octopus_selftest(void) {
|
||||
|
||||
sys.system_code = 0x0003;
|
||||
ok &= transit_check("other system not detected", is_valid_octopus_card(&card), 0);
|
||||
ok &= transit_felica_check_identify("identify", OCTOPUS_SYSTEM_CODE, "Octopus", TRANSIT_ID_MATCH);
|
||||
ok &= transit_felica_check_identify("identify Shenzhen Tong", SZT_SYSTEM_CODE, "Shenzhen Tong", TRANSIT_ID_MATCH);
|
||||
ok &= transit_felica_check_identify("Shenzhen Tong is not Octopus", SZT_SYSTEM_CODE, "Octopus", TRANSIT_ID_NONE);
|
||||
ok &= transit_felica_check_identify("other system not identified", 0x0003, "Octopus", TRANSIT_ID_NONE);
|
||||
|
||||
return transit_selftest_result("Octopus", ok);
|
||||
}
|
||||
@@ -32,6 +32,10 @@
|
||||
// True when either balance service was read
|
||||
bool is_valid_octopus_card(const transit_felica_card_t *card);
|
||||
|
||||
// System 8008 resp. 8005 among the system codes
|
||||
transit_id_t octopus_identify(const uint16_t *system_codes, size_t count);
|
||||
transit_id_t szt_identify(const uint16_t *system_codes, size_t count);
|
||||
|
||||
// Print the Octopus and / or Shenzhen Tong balance
|
||||
int octopus_parser_parse(const transit_felica_card_t *card);
|
||||
|
||||
|
||||
@@ -131,8 +131,14 @@ static void opal_time_str(char *out, size_t outlen, const opal_card_t *c) {
|
||||
}
|
||||
}
|
||||
|
||||
transit_id_t opal_identify(const transit_mfdes_info_t *info) {
|
||||
return (transit_mfdes_info_has_aid(info, OPAL_AID)) ? TRANSIT_ID_MATCH : TRANSIT_ID_NONE;
|
||||
}
|
||||
|
||||
bool is_valid_opal_card(const desfire_dump_t *dump) {
|
||||
return (opal_find_app(dump) != NULL);
|
||||
transit_mfdes_info_t info;
|
||||
transit_mfdes_info_from_dump(dump, &info);
|
||||
return opal_identify(&info) == TRANSIT_ID_MATCH;
|
||||
}
|
||||
|
||||
int opal_parser_parse(const desfire_dump_t *dump) {
|
||||
@@ -229,11 +235,13 @@ int opal_selftest(void) {
|
||||
dump->app[0].files[0].data = litter;
|
||||
dump->app[0].files[0].datalen = sizeof(litter);
|
||||
ok &= transit_check("detected", is_valid_opal_card(dump), true);
|
||||
ok &= transit_mfdes_check_identify("identify", dump, "Opal", TRANSIT_ID_MATCH);
|
||||
if (ok) {
|
||||
opal_parser_parse(dump);
|
||||
}
|
||||
dump->app[0].aid = 0xF21190;
|
||||
ok &= transit_check("other AID rejected", is_valid_opal_card(dump), false);
|
||||
ok &= transit_mfdes_check_identify("not identified", dump, "Opal", TRANSIT_ID_NONE);
|
||||
free(dump);
|
||||
|
||||
return transit_selftest_result("Opal", ok);
|
||||
|
||||
@@ -23,12 +23,17 @@
|
||||
|
||||
#include "common.h"
|
||||
#include "fileutils.h" // desfire_dump_t
|
||||
#include "parsetransit.h" // transit_mfdes_info_t
|
||||
|
||||
// AID as pm3 holds it, wire order 31 45 53
|
||||
#define OPAL_AID 0x534531
|
||||
#define OPAL_FID_CARD 0x07
|
||||
|
||||
bool is_valid_opal_card(const desfire_dump_t *dump);
|
||||
|
||||
// Opal AID in the application list
|
||||
transit_id_t opal_identify(const transit_mfdes_info_t *info);
|
||||
|
||||
int opal_parser_parse(const desfire_dump_t *dump);
|
||||
int opal_selftest(void);
|
||||
|
||||
|
||||
@@ -250,6 +250,7 @@ int opus_selftest(void) {
|
||||
|
||||
bool ok = true;
|
||||
ok &= transit_check("detect", is_valid_opus_card(&card), true);
|
||||
ok &= transit_calypso_check_identify("identify", &card, "Opus", TRANSIT_ID_MATCH);
|
||||
|
||||
en1545_parsed_t cp;
|
||||
(void)en1545_parse(c->data, c->len, 0, &opus_contract, &cp);
|
||||
@@ -271,6 +272,7 @@ int opus_selftest(void) {
|
||||
// an Intercode card must not be taken for Opus
|
||||
transit_set_bits_be(card.env.data, card.env.len, 13, 24, 0x250901);
|
||||
ok &= transit_check("rejects Navigo env", is_valid_opus_card(&card), false);
|
||||
ok &= transit_calypso_check_identify("not identified", &card, "Opus", TRANSIT_ID_NONE);
|
||||
transit_set_bits_be(card.env.data, card.env.len, 13, 24, OPUS_NETWORK_ID);
|
||||
|
||||
if (ok) {
|
||||
|
||||
@@ -68,13 +68,18 @@ static void plantain_time_str(char *out, size_t outlen, uint32_t mins) {
|
||||
transit_datetime_str(out, outlen, ((int64_t)PLANTAIN_DAYS_TO_2010 * 86400) + ((int64_t)mins * 60));
|
||||
}
|
||||
|
||||
static const transit_mfc_key_t plantain_keys[] = {
|
||||
{4, TRANSIT_MFC_KEY_AB, PLANTAIN_S4_KEY_A}, {4, TRANSIT_MFC_KEY_AB, PLANTAIN_S4_KEY_B},
|
||||
{5, TRANSIT_MFC_KEY_AB, PLANTAIN_S5_KEY_A}, {5, TRANSIT_MFC_KEY_AB, PLANTAIN_S5_KEY_B},
|
||||
};
|
||||
|
||||
const transit_mfc_keyset_t plantain_keyset = { plantain_keys, ARRAYLEN(plantain_keys), false };
|
||||
|
||||
bool is_valid_plantain_card(const uint8_t *dump, size_t dumplen) {
|
||||
if (transit_mfc_block(dump, dumplen, 5, 3) == NULL) {
|
||||
return false;
|
||||
}
|
||||
bool s4 = transit_mfc_has_key(dump, dumplen, 4, PLANTAIN_S4_KEY_A) || transit_mfc_has_key(dump, dumplen, 4, PLANTAIN_S4_KEY_B);
|
||||
bool s5 = transit_mfc_has_key(dump, dumplen, 5, PLANTAIN_S5_KEY_A) || transit_mfc_has_key(dump, dumplen, 5, PLANTAIN_S5_KEY_B);
|
||||
return s4 && s5;
|
||||
return transit_mfc_keyset_match_dump(&plantain_keyset, dump, dumplen);
|
||||
}
|
||||
|
||||
static void plantain_decode(const uint8_t *dump, size_t dumplen, plantain_t *p) {
|
||||
@@ -223,6 +228,7 @@ int plantain_selftest(void) {
|
||||
memcpy(s5 + 32, s5b2, sizeof(s5b2));
|
||||
|
||||
bool ok = transit_check("detected", is_valid_plantain_card(dump, sizeof(dump)), 1);
|
||||
ok &= transit_mfc_check_identify("identify", dump, sizeof(dump), "Podorozhnik", TRANSIT_ID_MATCH);
|
||||
|
||||
plantain_t p;
|
||||
plantain_decode(dump, sizeof(dump), &p);
|
||||
@@ -247,6 +253,7 @@ int plantain_selftest(void) {
|
||||
|
||||
transit_mfc_set_key(dump, sizeof(dump), 5, true, 0xFFFFFFFFFFFF);
|
||||
ok &= transit_check("wrong sector 5 key rejected", is_valid_plantain_card(dump, sizeof(dump)), 0);
|
||||
ok &= transit_mfc_check_identify("not identified", dump, sizeof(dump), "Podorozhnik", TRANSIT_ID_NONE);
|
||||
|
||||
return transit_selftest_result("Podorozhnik", ok);
|
||||
}
|
||||
@@ -24,6 +24,10 @@
|
||||
#define PARSEPLANTAIN_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "parsetransit.h" // transit_mfc_keyset_t
|
||||
|
||||
// Sector 4 and 5 keys, identifying for info commands too
|
||||
extern const transit_mfc_keyset_t plantain_keyset;
|
||||
|
||||
// Sectors 4 and 5 under the Podorozhnik keys
|
||||
bool is_valid_plantain_card(const uint8_t *dump, size_t dumplen);
|
||||
|
||||
@@ -293,6 +293,7 @@ int ravkav_selftest(void) {
|
||||
|
||||
bool ok = true;
|
||||
ok &= transit_check("detect", is_valid_ravkav_card(&card), true);
|
||||
ok &= transit_calypso_check_identify("identify", &card, "Rav-Kav", TRANSIT_ID_MATCH);
|
||||
|
||||
en1545_parsed_t env;
|
||||
(void)en1545_parse(card.env.data, card.env.len, 0, &ravkav_env, &env);
|
||||
@@ -332,6 +333,7 @@ int ravkav_selftest(void) {
|
||||
transit_set_bits_be(fr.env.data, fr.env.len, 6, 7, 0x01);
|
||||
transit_set_bits_be(fr.env.data, fr.env.len, 13, 24, 0x250901);
|
||||
ok &= transit_check("rejects Navigo env", is_valid_ravkav_card(&fr), false);
|
||||
ok &= transit_calypso_check_identify("not identified", &fr, "Rav-Kav", TRANSIT_ID_NONE);
|
||||
|
||||
if (ok) {
|
||||
ok &= (ravkav_parser_parse(&card) == PM3_SUCCESS);
|
||||
|
||||
@@ -29,7 +29,11 @@
|
||||
#define RENFE_DUMP_MIN 1024
|
||||
#define RENFE_BLK(b) ((b) * 16)
|
||||
|
||||
static const uint64_t renfe_keys[2] = { 0xA8844B0BCA06, 0xCB5ED0E57B08 };
|
||||
static const transit_mfc_key_t renfe_keys[2] = {
|
||||
{0, TRANSIT_MFC_KEY_A, 0xA8844B0BCA06}, {1, TRANSIT_MFC_KEY_A, 0xCB5ED0E57B08},
|
||||
};
|
||||
|
||||
const transit_mfc_keyset_t renfe_keyset = { renfe_keys, ARRAYLEN(renfe_keys), false };
|
||||
|
||||
// Blocks Metroflip found the event log in
|
||||
static const uint8_t renfe_history_blocks[] = { 18, 22, 28, 29, 30, 44, 45, 46 };
|
||||
@@ -38,8 +42,7 @@ bool is_valid_renfe_card(const uint8_t *dump, size_t dumplen) {
|
||||
if (dump == NULL || dumplen < RENFE_DUMP_MIN) {
|
||||
return false;
|
||||
}
|
||||
return MemBeToUint6byte(dump + RENFE_BLK(3)) == renfe_keys[0] &&
|
||||
MemBeToUint6byte(dump + RENFE_BLK(7)) == renfe_keys[1];
|
||||
return transit_mfc_keyset_match_dump(&renfe_keyset, dump, dumplen);
|
||||
}
|
||||
|
||||
static const char *renfe_event_name(uint8_t type) {
|
||||
@@ -166,12 +169,14 @@ int renfe_selftest(void) {
|
||||
|
||||
bool ok = true;
|
||||
ok &= transit_check("detected", is_valid_renfe_card(d, sizeof(d)), true);
|
||||
ok &= transit_mfc_check_identify("identify", d, sizeof(d), "RENFE Suma 10", TRANSIT_ID_MATCH);
|
||||
ok &= transit_check("events", n, 2);
|
||||
ok &= transit_check("newest is exit", (n > 0) ? ev[0][0] : 0, 0x1A);
|
||||
ok &= transit_check("parse", renfe_parser_parse(d, sizeof(d)), PM3_SUCCESS);
|
||||
|
||||
d[RENFE_BLK(7)] ^= 0xFF;
|
||||
ok &= transit_check("wrong sector 1 key", is_valid_renfe_card(d, sizeof(d)), false);
|
||||
ok &= transit_mfc_check_identify("not identified", d, sizeof(d), "RENFE Suma 10", TRANSIT_ID_NONE);
|
||||
|
||||
return transit_selftest_result("RENFE Suma 10", ok);
|
||||
}
|
||||
@@ -23,6 +23,10 @@
|
||||
#define PARSERENFE_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "parsetransit.h" // transit_mfc_keyset_t
|
||||
|
||||
// Sector 0 and 1 key A, identifying for info commands too
|
||||
extern const transit_mfc_keyset_t renfe_keyset;
|
||||
|
||||
// True when the sector 0 and 1 key A are the RENFE ones
|
||||
bool is_valid_renfe_card(const uint8_t *dump, size_t dumplen);
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "util.h" // sprint_hex_inrow
|
||||
#include "mbedtls/md5.h"
|
||||
#include "parsetransit.h"
|
||||
#include "parsetransitmfc.h"
|
||||
|
||||
#define SR_DUMP_MIN 1024
|
||||
#define SR_BLK(s, b) ((((s) * 4) + (b)) * 16)
|
||||
@@ -39,7 +40,11 @@ typedef enum {
|
||||
} sr_type_t;
|
||||
|
||||
// Sector 7 common keys, SmartRider key A and key B
|
||||
static const uint64_t sr_s7_keys[2] = { 0x9189449EA24E, 0x568C9083F71C };
|
||||
static const transit_mfc_key_t sr_s7_keys[2] = {
|
||||
{7, TRANSIT_MFC_KEY_A, 0x9189449EA24E}, {7, TRANSIT_MFC_KEY_B, 0x568C9083F71C},
|
||||
};
|
||||
|
||||
const transit_mfc_keyset_t smartrider_keyset = { sr_s7_keys, ARRAYLEN(sr_s7_keys), false };
|
||||
|
||||
// md5("myway" || key || "myway") of the MyWay sector 7 key, from Metrodroid
|
||||
static const uint8_t sr_myway_digest[16] = {
|
||||
@@ -68,7 +73,7 @@ static sr_type_t sr_detect(const uint8_t *dump, size_t dumplen) {
|
||||
return SR_UNKNOWN;
|
||||
}
|
||||
const uint8_t *t = dump + SR_BLK(7, 3);
|
||||
if (MemBeToUint6byte(t) == sr_s7_keys[0] || MemBeToUint6byte(t + 10) == sr_s7_keys[1]) {
|
||||
if (transit_mfc_keyset_match_dump(&smartrider_keyset, dump, dumplen)) {
|
||||
return SR_SMARTRIDER;
|
||||
}
|
||||
if (sr_key_digest_is(t, "myway", sr_myway_digest) || sr_key_digest_is(t + 10, "myway", sr_myway_digest)) {
|
||||
@@ -269,9 +274,10 @@ int smartrider_selftest(void) {
|
||||
// key A of sector 7
|
||||
uint8_t *t = d + SR_BLK(7, 3);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
t[i] = (uint8_t)(sr_s7_keys[0] >> (40 - (8 * i)));
|
||||
t[i] = (uint8_t)(sr_s7_keys[0].key >> (40 - (8 * i)));
|
||||
}
|
||||
ok &= transit_check("detected", is_valid_smartrider_card(d, sizeof(d)), true);
|
||||
ok &= transit_mfc_check_identify("identify", d, sizeof(d), "SmartRider", TRANSIT_ID_MATCH);
|
||||
|
||||
// Metrodroid's published digest of the SmartRider key A proves the hashing
|
||||
static const uint8_t sr_key2_digest[16] = {
|
||||
@@ -314,6 +320,7 @@ int smartrider_selftest(void) {
|
||||
|
||||
t[0] ^= 0xFF;
|
||||
ok &= transit_check("wrong sector 7 key", is_valid_smartrider_card(d, sizeof(d)), false);
|
||||
ok &= transit_mfc_check_identify("not identified", d, sizeof(d), "SmartRider", TRANSIT_ID_NONE);
|
||||
|
||||
return transit_selftest_result("SmartRider", ok);
|
||||
}
|
||||
@@ -22,6 +22,11 @@
|
||||
#define PARSESMARTRIDER_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "parsetransit.h" // transit_mfc_keyset_t
|
||||
|
||||
// SmartRider sector 7 keys, identifying for info commands too. MyWay is
|
||||
// only known by a digest of its key, so it cannot be probed for
|
||||
extern const transit_mfc_keyset_t smartrider_keyset;
|
||||
|
||||
// True when the sector 7 keys are the SmartRider or MyWay common keys
|
||||
bool is_valid_smartrider_card(const uint8_t *dump, size_t dumplen);
|
||||
|
||||
@@ -319,6 +319,10 @@ bool is_valid_suica_card(const transit_felica_card_t *card) {
|
||||
return (h != NULL && h->block_count > 0);
|
||||
}
|
||||
|
||||
transit_id_t suica_identify(const uint16_t *system_codes, size_t count) {
|
||||
return transit_felica_has_system_code(system_codes, count, SUICA_SYSTEM_CODE) ? TRANSIT_ID_MATCH : TRANSIT_ID_NONE;
|
||||
}
|
||||
|
||||
int suica_parser_parse(const transit_felica_card_t *card) {
|
||||
|
||||
if (is_valid_suica_card(card) == false) {
|
||||
@@ -487,6 +491,8 @@ int suica_selftest(void) {
|
||||
|
||||
sys.system_code = 0x8008;
|
||||
ok &= transit_check("other system not detected", is_valid_suica_card(&card), 0);
|
||||
ok &= transit_felica_check_identify("identify", SUICA_SYSTEM_CODE, "Japan IC", TRANSIT_ID_MATCH);
|
||||
ok &= transit_felica_check_identify("other system not identified", 0x8008, "Japan IC", TRANSIT_ID_NONE);
|
||||
|
||||
return transit_selftest_result("Japan IC", ok);
|
||||
}
|
||||
@@ -33,6 +33,9 @@
|
||||
// True when system 0003 carries a readable history service
|
||||
bool is_valid_suica_card(const transit_felica_card_t *card);
|
||||
|
||||
// System 0003 among the system codes
|
||||
transit_id_t suica_identify(const uint16_t *system_codes, size_t count);
|
||||
|
||||
// Name the card from its service codes, print balance and history
|
||||
int suica_parser_parse(const transit_felica_card_t *card);
|
||||
|
||||
|
||||
@@ -63,20 +63,26 @@ static void tmobilitat_number_str(uint32_t n, char *out, size_t outlen) {
|
||||
snprintf(out, outlen, "%03u %03u %03u%s", (n / 1000000) % 1000, (n / 1000) % 1000, n % 1000, ctl);
|
||||
}
|
||||
|
||||
bool is_valid_tmobilitat_card(const desfire_dump_t *dump) {
|
||||
if (dump == NULL) {
|
||||
return false;
|
||||
}
|
||||
bool aid = false;
|
||||
for (uint8_t i = 0; i < dump->appcount && i < DESFIRE_MAX_APP_COUNT; i++) {
|
||||
if (dump->app[i].aid == TMOBILITAT_AID) {
|
||||
aid = true;
|
||||
break;
|
||||
}
|
||||
transit_id_t tmobilitat_identify(const transit_mfdes_info_t *info) {
|
||||
if (info == NULL || info->card == NULL) {
|
||||
return TRANSIT_ID_NONE;
|
||||
}
|
||||
uint8_t hlen = 0;
|
||||
const uint8_t *h = tmobilitat_hist(&dump->card_info, &hlen);
|
||||
return aid && h != NULL && hlen >= 6 && memcmp(h, tmobilitat_hist_tag, sizeof(tmobilitat_hist_tag)) == 0;
|
||||
const uint8_t *h = tmobilitat_hist(info->card, &hlen);
|
||||
if (h == NULL || hlen < 6 || memcmp(h, tmobilitat_hist_tag, sizeof(tmobilitat_hist_tag)) != 0) {
|
||||
return TRANSIT_ID_NONE;
|
||||
}
|
||||
// historical bytes alone are a short tag
|
||||
if (info->aids_known == false) {
|
||||
return TRANSIT_ID_POSSIBLE;
|
||||
}
|
||||
return transit_mfdes_info_has_aid(info, TMOBILITAT_AID) ? TRANSIT_ID_MATCH : TRANSIT_ID_NONE;
|
||||
}
|
||||
|
||||
bool is_valid_tmobilitat_card(const desfire_dump_t *dump) {
|
||||
transit_mfdes_info_t info;
|
||||
transit_mfdes_info_from_dump(dump, &info);
|
||||
return tmobilitat_identify(&info) == TRANSIT_ID_MATCH;
|
||||
}
|
||||
|
||||
int tmobilitat_parser_parse(const desfire_dump_t *dump) {
|
||||
@@ -125,6 +131,13 @@ int tmobilitat_selftest(void) {
|
||||
ok &= transit_check("historical length", hlen, 8);
|
||||
ok &= transit_check("number", (h != NULL) ? MemBeToUint4byte(h + hlen - 4) : 0, 123456789);
|
||||
ok &= transit_check("detected", is_valid_tmobilitat_card(dump), true);
|
||||
ok &= transit_mfdes_check_identify("identify", dump, "T-Mobilitat", TRANSIT_ID_MATCH);
|
||||
|
||||
// ATS only, as `hf 14a info` sees it
|
||||
transit_mfdes_info_t info = { .card = &dump->card_info };
|
||||
transit_idents_t ids;
|
||||
transit_mfdes_identify(&info, &ids);
|
||||
ok &= transit_check_ident("ATS only possible", &ids, "T-Mobilitat", TRANSIT_ID_POSSIBLE);
|
||||
if (ok) {
|
||||
tmobilitat_parser_parse(dump);
|
||||
}
|
||||
@@ -132,6 +145,7 @@ int tmobilitat_selftest(void) {
|
||||
// Ljubljana Urbana shares the AID
|
||||
dump->card_info.ats[5] = 0x80;
|
||||
ok &= transit_check("other ATS rejected", is_valid_tmobilitat_card(dump), false);
|
||||
ok &= transit_mfdes_check_identify("not identified", dump, "T-Mobilitat", TRANSIT_ID_NONE);
|
||||
free(dump);
|
||||
|
||||
return transit_selftest_result("T-Mobilitat", ok);
|
||||
|
||||
@@ -24,11 +24,16 @@
|
||||
|
||||
#include "common.h"
|
||||
#include "fileutils.h" // desfire_dump_t
|
||||
#include "parsetransit.h" // transit_mfdes_info_t
|
||||
|
||||
// AID as pm3 holds it, wire order 05 00 00. Ljubljana Urbana uses it too.
|
||||
#define TMOBILITAT_AID 0x000005
|
||||
|
||||
bool is_valid_tmobilitat_card(const desfire_dump_t *dump);
|
||||
|
||||
// Historical bytes and AID, only possible when the application list is unknown
|
||||
transit_id_t tmobilitat_identify(const transit_mfdes_info_t *info);
|
||||
|
||||
int tmobilitat_parser_parse(const desfire_dump_t *dump);
|
||||
int tmobilitat_selftest(void);
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "commonutil.h" // num_to_bytes
|
||||
#include "ui.h" // PrintAndLogEx
|
||||
|
||||
#include "parsebip.h"
|
||||
@@ -49,6 +50,7 @@
|
||||
#include "parseumarsh.h"
|
||||
#include "parseventra.h"
|
||||
#include "parsezolotayakorona.h"
|
||||
#include "parsetransitmfc.h"
|
||||
|
||||
#define TRANSIT_LABEL_WIDTH 17
|
||||
|
||||
@@ -306,94 +308,272 @@ static int transit_helpers_selftest(void) {
|
||||
return transit_selftest_result("Transit helpers", ok);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Dispatch tables, one row per card system, NULL name ends a table
|
||||
// Identification output
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static void transit_ident_add(transit_idents_t *ids, const char *name, const char *region, const char *hint, transit_id_t id) {
|
||||
if (id == TRANSIT_ID_NONE || ids->count >= TRANSIT_MAX_IDENTS) {
|
||||
return;
|
||||
}
|
||||
transit_ident_t *t = &ids->ident[ids->count++];
|
||||
t->name = name;
|
||||
t->region = region;
|
||||
t->hint = hint;
|
||||
t->possible = (id == TRANSIT_ID_POSSIBLE);
|
||||
}
|
||||
|
||||
void transit_print_idents(const transit_idents_t *ids) {
|
||||
for (uint8_t i = 0; ids != NULL && i < ids->count; i++) {
|
||||
const transit_ident_t *t = &ids->ident[i];
|
||||
if (t->possible) {
|
||||
transit_print_kv("Transit", "Possible " _YELLOW_("%s") " ( %s )", t->name, t->region);
|
||||
} else {
|
||||
transit_print_kv("Transit", _GREEN_("%s") " ( %s )", t->name, t->region);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void transit_print_ident_hints(const transit_idents_t *ids, const char *current_cmd) {
|
||||
for (uint8_t i = 0; ids != NULL && i < ids->count; i++) {
|
||||
const char *hint = ids->ident[i].hint;
|
||||
if (hint == NULL || (current_cmd != NULL && strcmp(hint, current_cmd) == 0)) {
|
||||
continue;
|
||||
}
|
||||
bool seen = false;
|
||||
for (uint8_t j = 0; j < i; j++) {
|
||||
seen |= (ids->ident[j].hint != NULL && strcmp(ids->ident[j].hint, hint) == 0);
|
||||
}
|
||||
if (seen == false) {
|
||||
PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("%s") "` to decode", hint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool transit_check_ident(const char *what, const transit_idents_t *ids, const char *name, transit_id_t want) {
|
||||
transit_id_t got = TRANSIT_ID_NONE;
|
||||
for (uint8_t i = 0; ids != NULL && i < ids->count; i++) {
|
||||
if (strcmp(ids->ident[i].name, name) == 0) {
|
||||
got = ids->ident[i].possible ? TRANSIT_ID_POSSIBLE : TRANSIT_ID_MATCH;
|
||||
}
|
||||
}
|
||||
return transit_check(what, got, want);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// MIFARE Classic key sets, matched against a dump or the keys an info command found
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef bool (*transit_mfc_has_key_fn)(const void *ctx, uint8_t sector, uint8_t keytype, uint64_t key);
|
||||
|
||||
typedef struct {
|
||||
const uint8_t *dump;
|
||||
size_t dumplen;
|
||||
} transit_mfc_dumpref_t;
|
||||
|
||||
static bool transit_mfc_dump_has(const void *ctx, uint8_t sector, uint8_t keytype, uint64_t key) {
|
||||
const transit_mfc_dumpref_t *d = ctx;
|
||||
if (keytype != TRANSIT_MFC_KEY_B && transit_mfc_key(d->dump, d->dumplen, sector, false) == key) {
|
||||
return true;
|
||||
}
|
||||
return (keytype != TRANSIT_MFC_KEY_A) && (transit_mfc_key(d->dump, d->dumplen, sector, true) == key);
|
||||
}
|
||||
|
||||
static bool transit_mfc_info_has(const void *ctx, uint8_t sector, uint8_t keytype, uint64_t key) {
|
||||
const transit_mfc_info_t *info = ctx;
|
||||
for (uint8_t t = 0; t < 2; t++) {
|
||||
if ((keytype == t || keytype == TRANSIT_MFC_KEY_AB) && info->found[sector][t] && info->key[sector][t] == key) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool transit_mfc_keyset_match(const transit_mfc_keyset_t *ks, transit_mfc_has_key_fn has, const void *ctx) {
|
||||
if (ks == NULL || ks->count == 0) {
|
||||
return false;
|
||||
}
|
||||
uint64_t listed = 0, matched = 0;
|
||||
for (uint8_t i = 0; i < ks->count; i++) {
|
||||
const transit_mfc_key_t *k = &ks->keys[i];
|
||||
if (k->sector >= TRANSIT_MFC_MAX_SECTORS) {
|
||||
return false;
|
||||
}
|
||||
uint64_t bit = 1ULL << k->sector;
|
||||
listed |= bit;
|
||||
if ((matched & bit) == 0 && has(ctx, k->sector, k->keytype, k->key)) {
|
||||
matched |= bit;
|
||||
}
|
||||
}
|
||||
return ks->any_sector ? (matched != 0) : (matched == listed);
|
||||
}
|
||||
|
||||
bool transit_mfc_keyset_match_dump(const transit_mfc_keyset_t *ks, const uint8_t *dump, size_t dumplen) {
|
||||
transit_mfc_dumpref_t d = { dump, dumplen };
|
||||
return (dump != NULL) && transit_mfc_keyset_match(ks, transit_mfc_dump_has, &d);
|
||||
}
|
||||
|
||||
bool transit_mfc_keyset_match_info(const transit_mfc_keyset_t *ks, const transit_mfc_info_t *info) {
|
||||
return (info != NULL) && transit_mfc_keyset_match(ks, transit_mfc_info_has, info);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// DESFire application list
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void transit_mfdes_info_from_dump(const desfire_dump_t *dump, transit_mfdes_info_t *info) {
|
||||
memset(info, 0, sizeof(*info));
|
||||
if (dump == NULL) {
|
||||
return;
|
||||
}
|
||||
info->aids_known = true;
|
||||
for (uint8_t i = 0; i < dump->appcount && i < DESFIRE_MAX_APP_COUNT; i++) {
|
||||
info->aids[info->aidcount++] = dump->app[i].aid;
|
||||
}
|
||||
info->card = &dump->card_info;
|
||||
}
|
||||
|
||||
bool transit_mfdes_info_has_aid(const transit_mfdes_info_t *info, uint32_t aid) {
|
||||
for (uint8_t i = 0; info != NULL && i < info->aidcount && i < DESFIRE_MAX_APP_COUNT; i++) {
|
||||
if (info->aids[i] == aid) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Dispatch tables, one row per card system, NULL name ends a table.
|
||||
// A row without detect only identifies.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#define TRANSIT_HINT_MFC "hf mf autopwn"
|
||||
#define TRANSIT_HINT_MFDES "hf mfdes dump --no-auth"
|
||||
#define TRANSIT_HINT_MFU "hf mfu dump"
|
||||
#define TRANSIT_HINT_ST25TB "hf 14b dump"
|
||||
#define TRANSIT_HINT_FELICA "hf felica dump"
|
||||
#define TRANSIT_HINT_CALYPSO "hf calypso info"
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char *region;
|
||||
bool (*detect)(const uint8_t *dump, size_t dumplen);
|
||||
int (*parse)(const uint8_t *dump, size_t dumplen);
|
||||
int (*selftest)(void);
|
||||
} transit_buf_parser_t;
|
||||
const transit_mfc_keyset_t *keyset; // identify by sector keys
|
||||
transit_id_t (*identify)(const transit_mfc_info_t *info);
|
||||
} transit_mfc_parser_t;
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char *region;
|
||||
const char *hint;
|
||||
bool (*detect)(const desfire_dump_t *dump);
|
||||
int (*parse)(const desfire_dump_t *dump);
|
||||
int (*selftest)(void);
|
||||
transit_id_t (*identify)(const transit_mfdes_info_t *info);
|
||||
const transit_mfdes_file_t *summary; // free files with card number, balance
|
||||
uint8_t summary_count;
|
||||
} transit_mfdes_parser_t;
|
||||
|
||||
// Info commands read the same data as a dump, id_level is what a detect hit is worth
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char *region;
|
||||
bool (*detect)(const uint8_t *data, size_t datalen);
|
||||
int (*parse)(const uint8_t *data, size_t datalen);
|
||||
int (*selftest)(void);
|
||||
transit_id_t id_level;
|
||||
} transit_mfu_parser_t;
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char *region;
|
||||
bool (*detect)(const uint8_t *data, size_t datalen, const uint8_t *uid);
|
||||
int (*parse)(const uint8_t *data, size_t datalen, const uint8_t *uid);
|
||||
int (*selftest)(void);
|
||||
transit_id_t id_level;
|
||||
} transit_st25tb_parser_t;
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char *region;
|
||||
bool (*detect)(const transit_felica_card_t *card);
|
||||
int (*parse)(const transit_felica_card_t *card);
|
||||
int (*selftest)(void);
|
||||
transit_id_t (*identify)(const uint16_t *system_codes, size_t count);
|
||||
} transit_felica_parser_t;
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char *region;
|
||||
bool (*detect)(const transit_calypso_card_t *card);
|
||||
int (*parse)(const transit_calypso_card_t *card);
|
||||
int (*selftest)(void);
|
||||
transit_id_t id_level;
|
||||
} transit_calypso_parser_t;
|
||||
|
||||
static const transit_buf_parser_t mfc_parsers[] = {
|
||||
{"Bip!", is_valid_bip_card, bip_parser_parse, bip_selftest},
|
||||
{"CharlieCard", is_valid_charlie_card, charlie_parser_parse, charlie_selftest},
|
||||
{"Kazan", is_valid_kazan_card, kazan_parser_parse, kazan_selftest},
|
||||
{"Metromoney", is_valid_metromoney_card, metromoney_parser_parse, metromoney_selftest},
|
||||
{"Nextfare", is_valid_nextfare_card, nextfare_parser_parse, nextfare_selftest},
|
||||
{"Podorozhnik", is_valid_plantain_card, plantain_parser_parse, plantain_selftest},
|
||||
{"RENFE Suma 10", is_valid_renfe_card, renfe_parser_parse, renfe_selftest},
|
||||
{"SmartRider", is_valid_smartrider_card, smartrider_parser_parse, smartrider_selftest},
|
||||
{"Troika", is_valid_troika_card, troika_parser_parse, troika_selftest},
|
||||
{"Umarsh", is_valid_umarsh_card, umarsh_parser_parse, umarsh_selftest},
|
||||
{"Zolotaya Korona", is_valid_zolotayakorona_card, zolotayakorona_parser_parse, zolotayakorona_selftest},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
static const transit_mfc_parser_t mfc_parsers[] = {
|
||||
{"Bip!", "Santiago, Chile", is_valid_bip_card, bip_parser_parse, bip_selftest, &bip_keyset, NULL},
|
||||
{"CharlieCard", "MBTA, Boston", is_valid_charlie_card, charlie_parser_parse, charlie_selftest, &charlie_keyset, NULL},
|
||||
{"Kazan", "Kazan, Russia", is_valid_kazan_card, kazan_parser_parse, kazan_selftest, &kazan_keyset, NULL},
|
||||
{"Metromoney", "Tbilisi, Georgia", is_valid_metromoney_card, metromoney_parser_parse, metromoney_selftest, &metromoney_keyset, NULL},
|
||||
{"Nextfare", "Cubic, Brisbane / Los Angeles / Minneapolis", is_valid_nextfare_card, nextfare_parser_parse, nextfare_selftest, NULL, nextfare_identify},
|
||||
{"Podorozhnik", "Saint Petersburg, Russia", is_valid_plantain_card, plantain_parser_parse, plantain_selftest, &plantain_keyset, NULL},
|
||||
{"RENFE Suma 10", "Renfe Cercanias, Spain", is_valid_renfe_card, renfe_parser_parse, renfe_selftest, &renfe_keyset, NULL},
|
||||
{"SmartRider", "Transperth, Perth", is_valid_smartrider_card, smartrider_parser_parse, smartrider_selftest, &smartrider_keyset, NULL},
|
||||
{"Troika", "Moscow, Russia", is_valid_troika_card, troika_parser_parse, troika_selftest, &troika_keyset, NULL},
|
||||
{"Umarsh", "Russia", is_valid_umarsh_card, umarsh_parser_parse, umarsh_selftest, NULL, NULL},
|
||||
{"Zolotaya Korona", "Russia", is_valid_zolotayakorona_card, zolotayakorona_parser_parse, zolotayakorona_selftest, NULL, zolotayakorona_identify},
|
||||
{NULL, NULL, NULL, NULL, NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
static const transit_mfdes_file_t clipper_summary[] = {
|
||||
{CLIPPER_AID_CARD, CLIPPER_FID_IDENTITY}, {CLIPPER_AID_CARD, CLIPPER_FID_ECASH},
|
||||
{CLIPPER_AID_MOBILE, CLIPPER_FID_IDENTITY}, {CLIPPER_AID_MOBILE, CLIPPER_FID_ECASH},
|
||||
};
|
||||
static const transit_mfdes_file_t itso_summary[] = { {ITSO_AID, ITSO_FID_SHELL} };
|
||||
static const transit_mfdes_file_t myki_summary[] = { {MYKI_AID_1, MYKI_FID_SERIAL} };
|
||||
static const transit_mfdes_file_t nol_summary[] = { {NOL_AID_SERIAL, NOL_FID_SERIAL} };
|
||||
static const transit_mfdes_file_t opal_summary[] = { {OPAL_AID, OPAL_FID_CARD} };
|
||||
|
||||
static const transit_mfdes_parser_t mfdes_parsers[] = {
|
||||
{"Clipper", is_valid_clipper_card, clipper_parser_parse, clipper_selftest},
|
||||
{"ITSO", is_valid_itso_card, itso_parser_parse, itso_selftest},
|
||||
{"myki", is_valid_myki_card, myki_parser_parse, myki_selftest},
|
||||
{"nol", is_valid_nol_card, nol_parser_parse, nol_selftest},
|
||||
{"Opal", is_valid_opal_card, opal_parser_parse, opal_selftest},
|
||||
{"T-Mobilitat", is_valid_tmobilitat_card, tmobilitat_parser_parse, tmobilitat_selftest},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
{"Clipper", "San Francisco Bay Area", TRANSIT_HINT_MFDES, is_valid_clipper_card, clipper_parser_parse, clipper_selftest, clipper_identify, clipper_summary, ARRAYLEN(clipper_summary)},
|
||||
{"ITSO", "United Kingdom", TRANSIT_HINT_MFDES, is_valid_itso_card, itso_parser_parse, itso_selftest, itso_identify, itso_summary, ARRAYLEN(itso_summary)},
|
||||
{"myki", "Melbourne, Australia", TRANSIT_HINT_MFDES, is_valid_myki_card, myki_parser_parse, myki_selftest, myki_identify, myki_summary, ARRAYLEN(myki_summary)},
|
||||
{"nol", "Dubai, UAE", TRANSIT_HINT_MFDES, is_valid_nol_card, nol_parser_parse, nol_selftest, nol_identify, nol_summary, ARRAYLEN(nol_summary)},
|
||||
{"Opal", "Sydney, Australia", TRANSIT_HINT_MFDES, is_valid_opal_card, opal_parser_parse, opal_selftest, opal_identify, opal_summary, ARRAYLEN(opal_summary)},
|
||||
// card number is in the ATS, balance and trips need keys
|
||||
{"T-Mobilitat", "Barcelona, Spain", "hf mfdes info", is_valid_tmobilitat_card, tmobilitat_parser_parse, tmobilitat_selftest, tmobilitat_identify, NULL, 0},
|
||||
{NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0}
|
||||
};
|
||||
|
||||
static const transit_buf_parser_t mfu_parsers[] = {
|
||||
{"Troika Ultralight", is_valid_troika_ul_card, troika_ul_parser_parse, troika_ul_selftest},
|
||||
{"TRT", is_valid_trt_card, trt_parser_parse, trt_selftest},
|
||||
{"Ventra", is_valid_ventra_card, ventra_parser_parse, ventra_selftest},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
static const transit_mfu_parser_t mfu_parsers[] = {
|
||||
{"Troika Ultralight", "Moscow, Russia", is_valid_troika_ul_card, troika_ul_parser_parse, troika_ul_selftest, TRANSIT_ID_POSSIBLE},
|
||||
{"TRT", "Tianjin Metro, China", is_valid_trt_card, trt_parser_parse, trt_selftest, TRANSIT_ID_POSSIBLE},
|
||||
{"Ventra", "Chicago Transit Authority", is_valid_ventra_card, ventra_parser_parse, ventra_selftest, TRANSIT_ID_POSSIBLE},
|
||||
{NULL, NULL, NULL, NULL, NULL, TRANSIT_ID_NONE}
|
||||
};
|
||||
|
||||
static const transit_st25tb_parser_t st25tb_parsers[] = {
|
||||
{"Intertic", is_valid_intertic_card, intertic_parser_parse, intertic_selftest},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
{"Intertic", "France / Morocco", is_valid_intertic_card, intertic_parser_parse, intertic_selftest, TRANSIT_ID_POSSIBLE},
|
||||
{NULL, NULL, NULL, NULL, NULL, TRANSIT_ID_NONE}
|
||||
};
|
||||
|
||||
static const transit_felica_parser_t felica_parsers[] = {
|
||||
{"Japan IC (Suica)", is_valid_suica_card, suica_parser_parse, suica_selftest},
|
||||
{"Octopus", is_valid_octopus_card, octopus_parser_parse, octopus_selftest},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
{"Japan IC", "Suica / PASMO / ICOCA ..., Japan", is_valid_suica_card, suica_parser_parse, suica_selftest, suica_identify},
|
||||
{"Octopus", "Hong Kong", is_valid_octopus_card, octopus_parser_parse, octopus_selftest, octopus_identify},
|
||||
{"Shenzhen Tong", "Shenzhen, China", NULL, NULL, NULL, szt_identify},
|
||||
{NULL, NULL, NULL, NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
static const transit_calypso_parser_t calypso_parsers[] = {
|
||||
{"Navigo", is_valid_navigo_card, navigo_parser_parse, navigo_selftest},
|
||||
{"Intercode", is_valid_intercode_card, intercode_parser_parse, intercode_selftest},
|
||||
{"Opus", is_valid_opus_card, opus_parser_parse, opus_selftest},
|
||||
{"Rav-Kav", is_valid_ravkav_card, ravkav_parser_parse, ravkav_selftest},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
{"Navigo", "Ile-de-France Mobilites, Paris", is_valid_navigo_card, navigo_parser_parse, navigo_selftest, TRANSIT_ID_MATCH},
|
||||
{"Intercode", "France", is_valid_intercode_card, intercode_parser_parse, intercode_selftest, TRANSIT_ID_MATCH},
|
||||
{"Opus", "Montreal / Quebec", is_valid_opus_card, opus_parser_parse, opus_selftest, TRANSIT_ID_MATCH},
|
||||
{"Rav-Kav", "Israel", is_valid_ravkav_card, ravkav_parser_parse, ravkav_selftest, TRANSIT_ID_MATCH},
|
||||
{NULL, NULL, NULL, NULL, NULL, TRANSIT_ID_NONE}
|
||||
};
|
||||
|
||||
// Runs every self test of a table, all of them even after a failure
|
||||
@@ -411,92 +591,243 @@ static const transit_calypso_parser_t calypso_parsers[] = {
|
||||
return transit_selftest_result("Transit", ok); \
|
||||
} while (0)
|
||||
|
||||
// Runs the parser of every row whose detect matches
|
||||
#define TRANSIT_RUN_PARSERS(table, valid, ...) do { \
|
||||
int hits = 0; \
|
||||
for (size_t i = 0; (valid) && table[i].name != NULL; i++) { \
|
||||
if (table[i].detect != NULL && table[i].detect(__VA_ARGS__)) { \
|
||||
(void)table[i].parse(__VA_ARGS__); \
|
||||
hits++; \
|
||||
} \
|
||||
} \
|
||||
return hits; \
|
||||
} while (0)
|
||||
|
||||
int transit_mfc_parse(const uint8_t *dump, size_t dumplen) {
|
||||
int hits = 0;
|
||||
for (size_t i = 0; dump != NULL && mfc_parsers[i].name != NULL; i++) {
|
||||
if (mfc_parsers[i].detect(dump, dumplen)) {
|
||||
(void)mfc_parsers[i].parse(dump, dumplen);
|
||||
hits++;
|
||||
}
|
||||
}
|
||||
return hits;
|
||||
TRANSIT_RUN_PARSERS(mfc_parsers, dump != NULL, dump, dumplen);
|
||||
}
|
||||
|
||||
int transit_mfc_selftest(void) {
|
||||
TRANSIT_RUN_SELFTESTS(mfc_parsers);
|
||||
}
|
||||
|
||||
int transit_mfdes_parse(const desfire_dump_t *dump) {
|
||||
int hits = 0;
|
||||
for (size_t i = 0; dump != NULL && mfdes_parsers[i].name != NULL; i++) {
|
||||
if (mfdes_parsers[i].detect(dump)) {
|
||||
(void)mfdes_parsers[i].parse(dump);
|
||||
hits++;
|
||||
uint64_t transit_mfc_probe_sectors(void) {
|
||||
uint64_t mask = 0;
|
||||
for (size_t i = 0; mfc_parsers[i].name != NULL; i++) {
|
||||
const transit_mfc_keyset_t *ks = mfc_parsers[i].keyset;
|
||||
for (uint8_t k = 0; ks != NULL && k < ks->count; k++) {
|
||||
mask |= 1ULL << ks->keys[k].sector;
|
||||
}
|
||||
}
|
||||
return hits;
|
||||
return mask;
|
||||
}
|
||||
|
||||
size_t transit_mfc_probe_keys(uint8_t sector, uint8_t keytype, uint8_t *keys, size_t maxkeys) {
|
||||
size_t n = 0;
|
||||
for (size_t i = 0; mfc_parsers[i].name != NULL; i++) {
|
||||
const transit_mfc_keyset_t *ks = mfc_parsers[i].keyset;
|
||||
for (uint8_t k = 0; ks != NULL && k < ks->count && n < maxkeys; k++) {
|
||||
const transit_mfc_key_t *e = &ks->keys[k];
|
||||
if (e->sector != sector || (e->keytype != keytype && e->keytype != TRANSIT_MFC_KEY_AB)) {
|
||||
continue;
|
||||
}
|
||||
bool dup = false;
|
||||
for (size_t j = 0; j < n; j++) {
|
||||
dup |= (bytes_to_num(keys + (j * 6), 6) == e->key);
|
||||
}
|
||||
if (dup == false) {
|
||||
num_to_bytes(e->key, 6, keys + (n * 6));
|
||||
n++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static void transit_mfc_info_probe_dump(const uint8_t *dump, size_t dumplen, transit_mfc_info_t *info) {
|
||||
memset(info, 0, sizeof(*info));
|
||||
uint64_t mask = transit_mfc_probe_sectors();
|
||||
for (uint8_t s = 0; s < TRANSIT_MFC_MAX_SECTORS; s++) {
|
||||
if ((mask & (1ULL << s)) == 0) {
|
||||
continue;
|
||||
}
|
||||
for (uint8_t t = 0; t < 2; t++) {
|
||||
uint8_t keys[16 * 6];
|
||||
size_t n = transit_mfc_probe_keys(s, t, keys, 16);
|
||||
uint64_t have = transit_mfc_key(dump, dumplen, s, t == TRANSIT_MFC_KEY_B);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
if (bytes_to_num(keys + (i * 6), 6) == have) {
|
||||
info->found[s][t] = true;
|
||||
info->key[s][t] = have;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
info->block1 = transit_mfc_block(dump, dumplen, 0, 1);
|
||||
}
|
||||
|
||||
int transit_mfc_identify(const transit_mfc_info_t *info, transit_idents_t *ids) {
|
||||
memset(ids, 0, sizeof(*ids));
|
||||
for (size_t i = 0; info != NULL && mfc_parsers[i].name != NULL; i++) {
|
||||
transit_id_t id = TRANSIT_ID_NONE;
|
||||
if (transit_mfc_keyset_match_info(mfc_parsers[i].keyset, info)) {
|
||||
id = TRANSIT_ID_MATCH;
|
||||
} else if (mfc_parsers[i].identify != NULL) {
|
||||
id = mfc_parsers[i].identify(info);
|
||||
}
|
||||
transit_ident_add(ids, mfc_parsers[i].name, mfc_parsers[i].region, TRANSIT_HINT_MFC, id);
|
||||
}
|
||||
return ids->count;
|
||||
}
|
||||
|
||||
bool transit_mfc_check_identify(const char *what, const uint8_t *dump, size_t dumplen, const char *name, transit_id_t want) {
|
||||
transit_mfc_info_t info;
|
||||
transit_mfc_info_probe_dump(dump, dumplen, &info);
|
||||
transit_idents_t ids;
|
||||
transit_mfc_identify(&info, &ids);
|
||||
return transit_check_ident(what, &ids, name, want);
|
||||
}
|
||||
|
||||
int transit_mfdes_parse(const desfire_dump_t *dump) {
|
||||
TRANSIT_RUN_PARSERS(mfdes_parsers, dump != NULL, dump);
|
||||
}
|
||||
|
||||
int transit_mfdes_selftest(void) {
|
||||
TRANSIT_RUN_SELFTESTS(mfdes_parsers);
|
||||
}
|
||||
|
||||
int transit_mfu_parse(const uint8_t *data, size_t datalen) {
|
||||
int hits = 0;
|
||||
for (size_t i = 0; data != NULL && mfu_parsers[i].name != NULL; i++) {
|
||||
if (mfu_parsers[i].detect(data, datalen)) {
|
||||
(void)mfu_parsers[i].parse(data, datalen);
|
||||
hits++;
|
||||
int transit_mfdes_identify(const transit_mfdes_info_t *info, transit_idents_t *ids) {
|
||||
memset(ids, 0, sizeof(*ids));
|
||||
for (size_t i = 0; info != NULL && mfdes_parsers[i].name != NULL; i++) {
|
||||
transit_ident_add(ids, mfdes_parsers[i].name, mfdes_parsers[i].region, mfdes_parsers[i].hint, mfdes_parsers[i].identify(info));
|
||||
}
|
||||
return ids->count;
|
||||
}
|
||||
|
||||
bool transit_mfdes_check_identify(const char *what, const desfire_dump_t *dump, const char *name, transit_id_t want) {
|
||||
transit_mfdes_info_t info;
|
||||
transit_mfdes_info_from_dump(dump, &info);
|
||||
transit_idents_t ids;
|
||||
transit_mfdes_identify(&info, &ids);
|
||||
return transit_check_ident(what, &ids, name, want);
|
||||
}
|
||||
|
||||
size_t transit_mfdes_summary_files(const transit_mfdes_info_t *info, transit_mfdes_file_t *files, size_t maxfiles) {
|
||||
size_t n = 0;
|
||||
for (size_t i = 0; info != NULL && mfdes_parsers[i].name != NULL; i++) {
|
||||
if (mfdes_parsers[i].identify(info) != TRANSIT_ID_MATCH) {
|
||||
continue;
|
||||
}
|
||||
for (uint8_t k = 0; k < mfdes_parsers[i].summary_count && n < maxfiles; k++) {
|
||||
if (transit_mfdes_info_has_aid(info, mfdes_parsers[i].summary[k].aid)) {
|
||||
files[n++] = mfdes_parsers[i].summary[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
return hits;
|
||||
return n;
|
||||
}
|
||||
|
||||
int transit_mfu_parse(const uint8_t *data, size_t datalen) {
|
||||
TRANSIT_RUN_PARSERS(mfu_parsers, data != NULL, data, datalen);
|
||||
}
|
||||
|
||||
int transit_mfu_selftest(void) {
|
||||
TRANSIT_RUN_SELFTESTS(mfu_parsers);
|
||||
}
|
||||
|
||||
int transit_st25tb_parse(const uint8_t *data, size_t datalen, const uint8_t *uid) {
|
||||
int hits = 0;
|
||||
for (size_t i = 0; data != NULL && st25tb_parsers[i].name != NULL; i++) {
|
||||
if (st25tb_parsers[i].detect(data, datalen, uid)) {
|
||||
(void)st25tb_parsers[i].parse(data, datalen, uid);
|
||||
hits++;
|
||||
int transit_mfu_identify(const uint8_t *data, size_t datalen, transit_idents_t *ids) {
|
||||
memset(ids, 0, sizeof(*ids));
|
||||
for (size_t i = 0; data != NULL && mfu_parsers[i].name != NULL; i++) {
|
||||
if (mfu_parsers[i].detect(data, datalen)) {
|
||||
transit_ident_add(ids, mfu_parsers[i].name, mfu_parsers[i].region, TRANSIT_HINT_MFU, mfu_parsers[i].id_level);
|
||||
}
|
||||
}
|
||||
return hits;
|
||||
return ids->count;
|
||||
}
|
||||
|
||||
int transit_st25tb_parse(const uint8_t *data, size_t datalen, const uint8_t *uid) {
|
||||
TRANSIT_RUN_PARSERS(st25tb_parsers, data != NULL, data, datalen, uid);
|
||||
}
|
||||
|
||||
int transit_st25tb_selftest(void) {
|
||||
TRANSIT_RUN_SELFTESTS(st25tb_parsers);
|
||||
}
|
||||
|
||||
int transit_felica_parse(const transit_felica_card_t *card) {
|
||||
int hits = 0;
|
||||
for (size_t i = 0; card != NULL && felica_parsers[i].name != NULL; i++) {
|
||||
if (felica_parsers[i].detect(card)) {
|
||||
(void)felica_parsers[i].parse(card);
|
||||
hits++;
|
||||
int transit_st25tb_identify(const uint8_t *data, size_t datalen, const uint8_t *uid, transit_idents_t *ids) {
|
||||
memset(ids, 0, sizeof(*ids));
|
||||
for (size_t i = 0; data != NULL && st25tb_parsers[i].name != NULL; i++) {
|
||||
if (st25tb_parsers[i].detect(data, datalen, uid)) {
|
||||
transit_ident_add(ids, st25tb_parsers[i].name, st25tb_parsers[i].region, TRANSIT_HINT_ST25TB, st25tb_parsers[i].id_level);
|
||||
}
|
||||
}
|
||||
return hits;
|
||||
return ids->count;
|
||||
}
|
||||
|
||||
int transit_felica_parse(const transit_felica_card_t *card) {
|
||||
TRANSIT_RUN_PARSERS(felica_parsers, card != NULL, card);
|
||||
}
|
||||
|
||||
int transit_felica_selftest(void) {
|
||||
TRANSIT_RUN_SELFTESTS(felica_parsers);
|
||||
}
|
||||
|
||||
int transit_calypso_parse(const transit_calypso_card_t *card) {
|
||||
int hits = 0;
|
||||
for (size_t i = 0; card != NULL && calypso_parsers[i].name != NULL; i++) {
|
||||
if (calypso_parsers[i].detect(card)) {
|
||||
(void)calypso_parsers[i].parse(card);
|
||||
hits++;
|
||||
bool transit_felica_has_system_code(const uint16_t *system_codes, size_t count, uint16_t code) {
|
||||
for (size_t i = 0; system_codes != NULL && i < count; i++) {
|
||||
if (system_codes[i] == code) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return hits;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool transit_felica_check_identify(const char *what, uint16_t system_code, const char *name, transit_id_t want) {
|
||||
// common area first, as a card lists it
|
||||
const uint16_t codes[] = { 0xFE00, system_code };
|
||||
transit_idents_t ids;
|
||||
transit_felica_identify(codes, ARRAYLEN(codes), &ids);
|
||||
return transit_check_ident(what, &ids, name, want);
|
||||
}
|
||||
|
||||
int transit_felica_identify(const uint16_t *system_codes, size_t count, transit_idents_t *ids) {
|
||||
memset(ids, 0, sizeof(*ids));
|
||||
for (size_t i = 0; system_codes != NULL && felica_parsers[i].name != NULL; i++) {
|
||||
transit_ident_add(ids, felica_parsers[i].name, felica_parsers[i].region, TRANSIT_HINT_FELICA, felica_parsers[i].identify(system_codes, count));
|
||||
}
|
||||
return ids->count;
|
||||
}
|
||||
|
||||
int transit_calypso_parse(const transit_calypso_card_t *card) {
|
||||
TRANSIT_RUN_PARSERS(calypso_parsers, card != NULL, card);
|
||||
}
|
||||
|
||||
int transit_calypso_selftest(void) {
|
||||
TRANSIT_RUN_SELFTESTS(calypso_parsers);
|
||||
}
|
||||
|
||||
int transit_calypso_identify(const transit_calypso_card_t *card, transit_idents_t *ids) {
|
||||
memset(ids, 0, sizeof(*ids));
|
||||
for (size_t i = 0; card != NULL && calypso_parsers[i].name != NULL; i++) {
|
||||
if (calypso_parsers[i].detect(card)) {
|
||||
transit_ident_add(ids, calypso_parsers[i].name, calypso_parsers[i].region, TRANSIT_HINT_CALYPSO, calypso_parsers[i].id_level);
|
||||
}
|
||||
}
|
||||
return ids->count;
|
||||
}
|
||||
|
||||
bool transit_mfu_check_identify(const char *what, const uint8_t *data, size_t datalen, const char *name, transit_id_t want) {
|
||||
transit_idents_t ids;
|
||||
transit_mfu_identify(data, datalen, &ids);
|
||||
return transit_check_ident(what, &ids, name, want);
|
||||
}
|
||||
|
||||
bool transit_st25tb_check_identify(const char *what, const uint8_t *data, size_t datalen, const char *name, transit_id_t want) {
|
||||
transit_idents_t ids;
|
||||
transit_st25tb_identify(data, datalen, NULL, &ids);
|
||||
return transit_check_ident(what, &ids, name, want);
|
||||
}
|
||||
|
||||
bool transit_calypso_check_identify(const char *what, const transit_calypso_card_t *card, const char *name, transit_id_t want) {
|
||||
transit_idents_t ids;
|
||||
transit_calypso_identify(card, &ids);
|
||||
return transit_check_ident(what, &ids, name, want);
|
||||
}
|
||||
@@ -18,7 +18,7 @@
|
||||
// Each card system lives in its own parsers/parse<name>.c and exposes
|
||||
// is_valid_<name>_card(), <name>_parser_parse() and <name>_selftest() over
|
||||
// plain byte buffers. The dispatchers below run every parser of a technology
|
||||
// whose detection matches.
|
||||
// whose detection matches, the identify functions name a card for info commands.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef PARSETRANSIT_H__
|
||||
@@ -142,6 +142,120 @@ typedef struct {
|
||||
transit_calypso_record_t events[TRANSIT_CALYPSO_MAX_EVENTS]; // SFI 08, 2010
|
||||
} transit_calypso_card_t;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Identification from the partial data an info command has at hand
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef enum {
|
||||
TRANSIT_ID_NONE = 0,
|
||||
TRANSIT_ID_POSSIBLE, // heuristic evidence only
|
||||
TRANSIT_ID_MATCH,
|
||||
} transit_id_t;
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char *region; // area or operator
|
||||
const char *hint; // command that fully decodes it
|
||||
bool possible;
|
||||
} transit_ident_t;
|
||||
|
||||
#define TRANSIT_MAX_IDENTS 4
|
||||
|
||||
typedef struct {
|
||||
uint8_t count;
|
||||
transit_ident_t ident[TRANSIT_MAX_IDENTS];
|
||||
} transit_idents_t;
|
||||
|
||||
// "Transit.......... name ( region )" per identification
|
||||
void transit_print_idents(const transit_idents_t *ids);
|
||||
|
||||
// One hint per distinct decode command, skipping the command already running
|
||||
void transit_print_ident_hints(const transit_idents_t *ids, const char *current_cmd);
|
||||
|
||||
// Self test helper, checks how ids name a card system
|
||||
bool transit_check_ident(const char *what, const transit_idents_t *ids, const char *name, transit_id_t want);
|
||||
|
||||
// MIFARE Classic: keys found per sector, MAD block when read
|
||||
#define TRANSIT_MFC_MAX_SECTORS 40
|
||||
#define TRANSIT_MFC_KEY_A 0 // same as MF_KEY_A
|
||||
#define TRANSIT_MFC_KEY_B 1
|
||||
#define TRANSIT_MFC_KEY_AB 2
|
||||
|
||||
typedef struct {
|
||||
bool found[TRANSIT_MFC_MAX_SECTORS][2];
|
||||
uint64_t key[TRANSIT_MFC_MAX_SECTORS][2];
|
||||
const uint8_t *block1; // sector 0 block 1, NULL when not read
|
||||
} transit_mfc_info_t;
|
||||
|
||||
// Identifying key of a sector
|
||||
typedef struct {
|
||||
uint8_t sector;
|
||||
uint8_t keytype;
|
||||
uint64_t key;
|
||||
} transit_mfc_key_t;
|
||||
|
||||
// Every listed sector needs one matching key, or just one sector with any_sector
|
||||
typedef struct {
|
||||
const transit_mfc_key_t *keys;
|
||||
uint8_t count;
|
||||
bool any_sector;
|
||||
} transit_mfc_keyset_t;
|
||||
|
||||
bool transit_mfc_keyset_match_dump(const transit_mfc_keyset_t *ks, const uint8_t *dump, size_t dumplen);
|
||||
bool transit_mfc_keyset_match_info(const transit_mfc_keyset_t *ks, const transit_mfc_info_t *info);
|
||||
|
||||
// Sectors with identifying keys, bit n for sector n
|
||||
uint64_t transit_mfc_probe_sectors(void);
|
||||
|
||||
// Distinct identifying keys of a sector for one key type, 6 bytes each, returns the count
|
||||
size_t transit_mfc_probe_keys(uint8_t sector, uint8_t keytype, uint8_t *keys, size_t maxkeys);
|
||||
|
||||
// Self test helper, identifies a dump from the keys an info command probe would find
|
||||
bool transit_mfc_check_identify(const char *what, const uint8_t *dump, size_t dumplen, const char *name, transit_id_t want);
|
||||
|
||||
int transit_mfc_identify(const transit_mfc_info_t *info, transit_idents_t *ids);
|
||||
|
||||
// MIFARE DESFire: application list and ATS
|
||||
typedef struct {
|
||||
bool aids_known;
|
||||
uint8_t aidcount;
|
||||
uint32_t aids[DESFIRE_MAX_APP_COUNT];
|
||||
const iso14a_card_select_t *card; // NULL when the ATS is unknown
|
||||
} transit_mfdes_info_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t aid;
|
||||
uint8_t fid;
|
||||
} transit_mfdes_file_t;
|
||||
|
||||
void transit_mfdes_info_from_dump(const desfire_dump_t *dump, transit_mfdes_info_t *info);
|
||||
bool transit_mfdes_info_has_aid(const transit_mfdes_info_t *info, uint32_t aid);
|
||||
|
||||
int transit_mfdes_identify(const transit_mfdes_info_t *info, transit_idents_t *ids);
|
||||
|
||||
// Self test helper, identifies a dump from its application list and ATS
|
||||
bool transit_mfdes_check_identify(const char *what, const desfire_dump_t *dump, const char *name, transit_id_t want);
|
||||
|
||||
// Free files the identified parsers decode a summary from, returns the count
|
||||
size_t transit_mfdes_summary_files(const transit_mfdes_info_t *info, transit_mfdes_file_t *files, size_t maxfiles);
|
||||
|
||||
// FeliCa: system codes as the specs write them
|
||||
bool transit_felica_has_system_code(const uint16_t *system_codes, size_t count, uint16_t code);
|
||||
int transit_felica_identify(const uint16_t *system_codes, size_t count, transit_idents_t *ids);
|
||||
|
||||
// Self test helper, identifies a card from one system code
|
||||
bool transit_felica_check_identify(const char *what, uint16_t system_code, const char *name, transit_id_t want);
|
||||
|
||||
// Ultralight, ST25TB and Calypso info commands read the same data a dump has
|
||||
int transit_mfu_identify(const uint8_t *data, size_t datalen, transit_idents_t *ids);
|
||||
int transit_st25tb_identify(const uint8_t *data, size_t datalen, const uint8_t *uid, transit_idents_t *ids);
|
||||
int transit_calypso_identify(const transit_calypso_card_t *card, transit_idents_t *ids);
|
||||
|
||||
// Self test helpers for the three above
|
||||
bool transit_mfu_check_identify(const char *what, const uint8_t *data, size_t datalen, const char *name, transit_id_t want);
|
||||
bool transit_st25tb_check_identify(const char *what, const uint8_t *data, size_t datalen, const char *name, transit_id_t want);
|
||||
bool transit_calypso_check_identify(const char *what, const transit_calypso_card_t *card, const char *name, transit_id_t want);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Dispatch per technology, return how many parsers matched
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
@@ -83,11 +83,13 @@ typedef struct {
|
||||
} troika_block_t;
|
||||
|
||||
// Sector keys A, 1K and 4K agree on these
|
||||
#define TROIKA_S4_KEY_A 0x73068F118C13
|
||||
#define TROIKA_S8_KEY_A 0xA73F5DC1D333
|
||||
static const uint64_t troika_sector_keys[16] = {
|
||||
[1] = 0xA82607B01C0D,
|
||||
[4] = 0x73068F118C13,
|
||||
[4] = TROIKA_S4_KEY_A,
|
||||
[7] = 0xAE3D65A3DAD4,
|
||||
[8] = 0xA73F5DC1D333,
|
||||
[8] = TROIKA_S8_KEY_A,
|
||||
};
|
||||
|
||||
// Sector 1 on a 4K card uses the MAD key A, key B tells it apart
|
||||
@@ -464,6 +466,12 @@ static void troika_print_block(const troika_block_t *t, const char *where) {
|
||||
// MIFARE Classic
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static const transit_mfc_key_t troika_id_keys[] = {
|
||||
{8, TRANSIT_MFC_KEY_AB, TROIKA_S8_KEY_A}, {4, TRANSIT_MFC_KEY_AB, TROIKA_S4_KEY_A},
|
||||
};
|
||||
|
||||
const transit_mfc_keyset_t troika_keyset = { troika_id_keys, ARRAYLEN(troika_id_keys), true };
|
||||
|
||||
static bool troika_sector_has_key(const uint8_t *dump, size_t dumplen, uint8_t sector) {
|
||||
if (transit_mfc_has_key(dump, dumplen, sector, troika_sector_keys[sector])) {
|
||||
return true;
|
||||
@@ -584,12 +592,14 @@ int troika_ul_selftest(void) {
|
||||
PrintAndLogEx(INFO, "Testing Troika Ultralight decode");
|
||||
|
||||
bool ok = transit_check("detected", is_valid_troika_ul_card(troika_ul_vector, sizeof(troika_ul_vector)), 1);
|
||||
ok &= transit_mfu_check_identify("identify", troika_ul_vector, sizeof(troika_ul_vector), "Troika Ultralight", TRANSIT_ID_POSSIBLE);
|
||||
ok &= troika_check_ul_vector(troika_ul_vector + (TROIKA_UL_FIRST_PAGE * 4));
|
||||
|
||||
uint8_t bad[sizeof(troika_ul_vector)];
|
||||
memcpy(bad, troika_ul_vector, sizeof(bad));
|
||||
bad[16] = 0x00;
|
||||
ok &= transit_check("bad header rejected", is_valid_troika_ul_card(bad, sizeof(bad)), 0);
|
||||
ok &= transit_mfu_check_identify("not identified", bad, sizeof(bad), "Troika Ultralight", TRANSIT_ID_NONE);
|
||||
return transit_selftest_result("Troika Ultralight", ok);
|
||||
}
|
||||
|
||||
@@ -606,6 +616,7 @@ int troika_selftest(void) {
|
||||
transit_mfc_set_key(dump, sizeof(dump), 8, false, troika_sector_keys[8]);
|
||||
|
||||
bool ok = transit_check("detected", is_valid_troika_card(dump, sizeof(dump)), 1);
|
||||
ok &= transit_mfc_check_identify("identify", dump, sizeof(dump), "Troika", TRANSIT_ID_MATCH);
|
||||
|
||||
uint8_t b[TROIKA_BLOCK_LEN];
|
||||
ok &= transit_check("sector 8 read", troika_sector_block(dump, sizeof(dump), 8, b), 1);
|
||||
@@ -641,6 +652,7 @@ int troika_selftest(void) {
|
||||
transit_mfc_set_key(dump, sizeof(dump), 8, false, 0xFFFFFFFFFFFF);
|
||||
transit_mfc_set_key(dump, sizeof(dump), 4, false, 0xFFFFFFFFFFFF);
|
||||
ok &= transit_check("wrong keys rejected", is_valid_troika_card(dump, sizeof(dump)), 0);
|
||||
ok &= transit_mfc_check_identify("not identified", dump, sizeof(dump), "Troika", TRANSIT_ID_NONE);
|
||||
ok &= transit_check("short dump rejected", is_valid_troika_card(dump, 512), 0);
|
||||
|
||||
return transit_selftest_result("Troika", ok);
|
||||
|
||||
@@ -23,6 +23,10 @@
|
||||
#define PARSETROIKA_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "parsetransit.h" // transit_mfc_keyset_t
|
||||
|
||||
// Sector 8 or 4 key, identifying for info commands, the data check needs a dump
|
||||
extern const transit_mfc_keyset_t troika_keyset;
|
||||
|
||||
// MIFARE Classic, a Troika block in sector 8 or 4 under the Troika keys
|
||||
bool is_valid_troika_card(const uint8_t *dump, size_t dumplen);
|
||||
|
||||
@@ -120,6 +120,7 @@ int trt_selftest(void) {
|
||||
ok &= transit_check("hour", t.hour, 13);
|
||||
ok &= transit_check("minute", t.minute, 45);
|
||||
ok &= transit_check("balance", t.balance, 200);
|
||||
ok &= transit_mfu_check_identify("identify", data, sizeof(data), "TRT", TRANSIT_ID_POSSIBLE);
|
||||
if (ok) {
|
||||
trt_parser_parse(data, sizeof(data));
|
||||
}
|
||||
@@ -130,6 +131,7 @@ int trt_selftest(void) {
|
||||
data[(TRT_PAGE_SALE_A - 1) * TRT_PAGE_SIZE] ^= 0x01;
|
||||
data[3 * TRT_PAGE_SIZE] = TRT_NDEF_CC_MAGIC;
|
||||
ok &= transit_check("NDEF tag rejected", is_valid_trt_card(data, sizeof(data)), false);
|
||||
ok &= transit_mfu_check_identify("not identified", data, sizeof(data), "TRT", TRANSIT_ID_NONE);
|
||||
|
||||
return transit_selftest_result("TRT", ok);
|
||||
}
|
||||
@@ -233,6 +233,7 @@ int ventra_selftest(void) {
|
||||
|
||||
bool ok = true;
|
||||
ok &= transit_check("detected", is_valid_ventra_card(data, sizeof(data)), true);
|
||||
ok &= transit_mfu_check_identify("identify", data, sizeof(data), "Ventra", TRANSIT_ID_POSSIBLE);
|
||||
char buf[40];
|
||||
transit_date_str(buf, sizeof(buf), ventra_base_days(data));
|
||||
ok &= transit_check_str("expiry", buf, "2024-05-19");
|
||||
@@ -254,6 +255,7 @@ int ventra_selftest(void) {
|
||||
|
||||
p[4 * 4] = 0x0B;
|
||||
ok &= transit_check("other header rejected", is_valid_ventra_card(data, sizeof(data)), false);
|
||||
ok &= transit_mfu_check_identify("not identified", data, sizeof(data), "Ventra", TRANSIT_ID_NONE);
|
||||
|
||||
return transit_selftest_result("Ventra", ok);
|
||||
}
|
||||
@@ -93,14 +93,18 @@ static bool zk_has_signature(const uint8_t *dump, size_t dumplen) {
|
||||
return (memcmp(b0, zk_signature, 16) == 0) && (memcmp(b1, zk_signature + 16, sizeof(zk_signature) - 16) == 0);
|
||||
}
|
||||
|
||||
static bool zk_has_mad(const uint8_t *dump, size_t dumplen) {
|
||||
const uint8_t *toc = transit_mfc_block(dump, dumplen, 0, 1);
|
||||
// MAD block 1, sectors 4 and 6
|
||||
static bool zk_mad_match(const uint8_t *toc) {
|
||||
if (toc == NULL) {
|
||||
return false;
|
||||
}
|
||||
return toc[8] == ZK_MAD_AID_HI && toc[9] == ZK_MAD_AID_LO && toc[12] == ZK_MAD_AID_HI && toc[13] == ZK_MAD_AID_LO;
|
||||
}
|
||||
|
||||
static bool zk_has_mad(const uint8_t *dump, size_t dumplen) {
|
||||
return zk_mad_match(transit_mfc_block(dump, dumplen, 0, 1));
|
||||
}
|
||||
|
||||
bool is_valid_zolotayakorona_card(const uint8_t *dump, size_t dumplen) {
|
||||
if (transit_mfc_block(dump, dumplen, ZK_INFO_SECTOR, 3) == NULL) {
|
||||
return false;
|
||||
@@ -108,6 +112,10 @@ bool is_valid_zolotayakorona_card(const uint8_t *dump, size_t dumplen) {
|
||||
return zk_has_signature(dump, dumplen) || zk_has_mad(dump, dumplen);
|
||||
}
|
||||
|
||||
transit_id_t zolotayakorona_identify(const transit_mfc_info_t *info) {
|
||||
return zk_mad_match(info->block1) ? TRANSIT_ID_MATCH : TRANSIT_ID_NONE;
|
||||
}
|
||||
|
||||
static void zk_decode(const uint8_t *dump, size_t dumplen, zk_t *z) {
|
||||
memset(z, 0, sizeof(*z));
|
||||
|
||||
@@ -248,11 +256,13 @@ int zolotayakorona_selftest(void) {
|
||||
|
||||
s15[11] ^= 0x01;
|
||||
ok &= transit_check("bad signature rejected", is_valid_zolotayakorona_card(dump, sizeof(dump)), 0);
|
||||
ok &= transit_mfc_check_identify("not identified", dump, sizeof(dump), "Zolotaya Korona", TRANSIT_ID_NONE);
|
||||
dump[16 + 8] = ZK_MAD_AID_HI;
|
||||
dump[16 + 9] = ZK_MAD_AID_LO;
|
||||
dump[16 + 12] = ZK_MAD_AID_HI;
|
||||
dump[16 + 13] = ZK_MAD_AID_LO;
|
||||
ok &= transit_check("detected by MAD", is_valid_zolotayakorona_card(dump, sizeof(dump)), 1);
|
||||
ok &= transit_mfc_check_identify("identify by MAD", dump, sizeof(dump), "Zolotaya Korona", TRANSIT_ID_MATCH);
|
||||
|
||||
return transit_selftest_result("Zolotaya Korona", ok);
|
||||
}
|
||||
@@ -24,9 +24,13 @@
|
||||
#define PARSEZOLOTAYAKORONA_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "parsetransit.h" // transit_mfc_info_t
|
||||
|
||||
// MAD entries for AID 18EE or the issuer name in sector 15
|
||||
bool is_valid_zolotayakorona_card(const uint8_t *dump, size_t dumplen);
|
||||
|
||||
// The MAD entries in the block 1 an info command read
|
||||
transit_id_t zolotayakorona_identify(const transit_mfc_info_t *info);
|
||||
int zolotayakorona_parser_parse(const uint8_t *dump, size_t dumplen);
|
||||
int zolotayakorona_selftest(void);
|
||||
|
||||
|
||||
Reference in new issue
Block a user