Merge pull request #1819 from toucan12/ksx6924

Add initialization command response interpretation on the ksx6924 (Tmoney) card
This commit is contained in:
Iceman authored and GitHub committed 2022-11-24 03:52:43 +01:00
commit f3642c1a6e
8 files changed
+197 -101

No files matched your search

+3 -3
View File
@@ -413,7 +413,7 @@ static int CmdHFFidoAuthenticate(const char *cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("a", "apdu", "Show APDU reqests and responses"),
arg_lit0("a", "apdu", "Show APDU requests and responses"),
arg_lit0("v", "verbose", "Verbose mode"),
arg_rem("default mode:", "dont-enforce-user-presence-and-sign"),
arg_lit0("u", "user", "mode: enforce-user-presence-and-sign"),
@@ -675,7 +675,7 @@ static int CmdHFFido2MakeCredential(const char *cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("a", "apdu", "Show APDU reqests and responses"),
arg_lit0("a", "apdu", "Show APDU requests and responses"),
arg_litn("v", "verbose", 0, 2, "Verbose mode. vv - show full certificates data"),
arg_lit0("t", "tlv", "Show DER certificate contents in TLV representation"),
arg_lit0("c", "cbor", "Show CBOR decoded data"),
@@ -794,7 +794,7 @@ static int CmdHFFido2GetAssertion(const char *cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("a", "apdu", "Show APDU reqests and responses"),
arg_lit0("a", "apdu", "Show APDU requests and responses"),
arg_litn("v", "verbose", 0, 2, "Verbose mode. vv - show full certificates data"),
arg_lit0("c", "cbor", "Show CBOR decoded data"),
arg_lit0("l", "list", "Add CredentialId from json to allowList"),
+29 -21
View File
@@ -69,7 +69,7 @@ static int CmdHFKSX6924Balance(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("k", "keep", "keep field ON for next command"),
arg_lit0("a", "apdu", "show APDU reqests and responses"),
arg_lit0("a", "apdu", "Show APDU requests and responses"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
@@ -102,7 +102,7 @@ static int CmdHFKSX6924Info(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("k", "keep", "keep field ON for next command"),
arg_lit0("a", "apdu", "show APDU reqests and responses"),
arg_lit0("a", "apdu", "Show APDU requests and responses"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
@@ -224,7 +224,7 @@ static int CmdHFKSX6924Select(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("a", "apdu", "show APDU reqests and responses"),
arg_lit0("a", "apdu", "Show APDU requests and responses"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
@@ -245,15 +245,15 @@ static int CmdHFKSX6924Select(const char *Cmd) {
static int CmdHFKSX6924Initialize(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf ksx6924 initialize",
"Perform transaction initialization (mpda)",
"hf ksx6924 initialize 000003e8 -> mpda\n");
CLIParserInit(&ctx, "hf ksx6924 init",
"Perform transaction initialization with Mpda (Money of Purchase Transaction)",
"hf ksx6924 init 000003e8 -> Mpda\n");
void *argtable[] = {
arg_param_begin,
arg_lit0("k", "keep", "keep field ON for next command"),
arg_lit0("a", "apdu", "show APDU reqests and responses"),
arg_str1(NULL, NULL, "<mpda 4byte hex>", NULL),
arg_lit0("a", "apdu", "Show APDU requests and responses"),
arg_str1(NULL, NULL, "<Mpda 4 bytes hex>", NULL),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
@@ -269,7 +269,7 @@ static int CmdHFKSX6924Initialize(const char *Cmd) {
SetAPDULogging(APDULogging);
if (datalen != 4) {
PrintAndLogEx(WARNING, "Mpda parameter must be 4 byte long (eg: 000003e8)");
PrintAndLogEx(WARNING, "Mpda parameter must be 4 bytes long (eg: 000003e8)");
return PM3_EINVARG;
}
@@ -278,15 +278,23 @@ static int CmdHFKSX6924Initialize(const char *Cmd) {
goto end;
}
PrintAndLogEx(SUCCESS, "Initialize Card : Mpda -> %02X %02X %02X %02X", data[0], data[1], data[2], data[3]);
uint8_t response[25] = {0};
if (KSX6924InitializeCard(data[0], data[1], data[2], data[3], response)) {
PrintAndLogEx(SUCCESS, "Response : %s", sprint_hex(response, sizeof(response)));
} else {
PrintAndLogEx(FAILED, "Initialize Card Error");
uint8_t resp[APDU_RES_LEN] = {0};
size_t resp_len = 0;
if (KSX6924InitializeCard(data[0], data[1], data[2], data[3], resp, &resp_len) == false) {
goto end;
}
uint8_t *r = resp;
struct ksx6924_initialize_card_response initCardResponse;
bool ret = KSX6924ParseInitializeCardResponse(r, resp_len, &initCardResponse);
if (!ret) {
PrintAndLogEx(FAILED, "Error parsing KS X 6924 initialize card response");
goto end;
}
KSX6924PrintInitializeCardResponse(&initCardResponse);
end:
if (keep == false) {
DropField();
@@ -305,7 +313,7 @@ static int CmdHFKSX6924PRec(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("k", "keep", "keep field ON for next command"),
arg_lit0("a", "apdu", "show APDU reqests and responses"),
arg_lit0("a", "apdu", "Show APDU requests and responses"),
arg_str1(NULL, NULL, "<record 1byte HEX>", NULL),
arg_param_end
};
@@ -348,11 +356,11 @@ end:
static command_t CommandTable[] = {
{"help", CmdHelp, AlwaysAvailable, "This help"},
{"balance", CmdHFKSX6924Balance, IfPm3Iso14443a, "Get current purse balance"},
{"info", CmdHFKSX6924Info, IfPm3Iso14443a, "Get info about a KS X 6924 (T-Money, Snapper+) transit card"},
{"initialize", CmdHFKSX6924Initialize, IfPm3Iso14443a, "Perform transaction initialization (Mpda)"},
{"prec", CmdHFKSX6924PRec, IfPm3Iso14443a, "Send proprietary get record command (CLA=90, INS=4C)"},
{"select", CmdHFKSX6924Select, IfPm3Iso14443a, "Select application, and leave field up"},
{"info", CmdHFKSX6924Info, IfPm3Iso14443a, "Get info about a KS X 6924 (T-Money, Snapper+) transit card"},
{"balance", CmdHFKSX6924Balance, IfPm3Iso14443a, "Get current purse balance"},
{"init", CmdHFKSX6924Initialize, IfPm3Iso14443a, "Perform transaction initialization with Mpda"},
{"prec", CmdHFKSX6924PRec, IfPm3Iso14443a, "Send proprietary get record command (CLA=90, INS=4C)"},
{NULL, NULL, NULL, NULL}
};
+10 -10
View File
@@ -140,7 +140,7 @@ static int CmdEMVSearch(const char *Cmd) {
arg_param_begin,
arg_lit0("sS", "select", "Activate field and select card"),
arg_lit0("kK", "keep", "Keep field ON for next command"),
arg_lit0("aA", "apdu", "Show APDU reqests and responses"),
arg_lit0("aA", "apdu", "Show APDU requests and responses"),
arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
arg_lit0("wW", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"),
arg_param_end
@@ -196,7 +196,7 @@ static int CmdEMVPPSE(const char *Cmd) {
arg_lit0("kK", "keep", "Keep field ON for next command"),
arg_lit0("1", "pse", "PSE (1PAY.SYS.DDF01) mode"),
arg_lit0("2", "ppse", "PPSE (2PAY.SYS.DDF01) mode (def)"),
arg_lit0("aA", "apdu", "Show APDU reqests and responses"),
arg_lit0("aA", "apdu", "Show APDU requests and responses"),
arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
arg_lit0("wW", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"),
arg_param_end
@@ -255,7 +255,7 @@ static int CmdEMVGPO(const char *Cmd) {
arg_lit0("kK", "keep", "Keep field ON for next command"),
arg_lit0("pP", "params", "Load parameters from `emv_defparams.json` file for PDOLdata making from PDOL and parameters"),
arg_lit0("mM", "make", "Make PDOLdata from PDOL (tag 9F38) and parameters (def: uses default parameters)"),
arg_lit0("aA", "apdu", "Show APDU reqests and responses"),
arg_lit0("aA", "apdu", "Show APDU requests and responses"),
arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
arg_lit0("wW", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"),
arg_strx0(NULL, NULL, "<hex>", "PDOLdata/PDOL"),
@@ -362,7 +362,7 @@ static int CmdEMVReadRecord(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("kK", "keep", "Keep field ON for next command"),
arg_lit0("aA", "apdu", "Show APDU reqests and responses"),
arg_lit0("aA", "apdu", "Show APDU requests and responses"),
arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
arg_lit0("wW", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"),
arg_strx1(NULL, NULL, "<hex>", "<SFI 1 byte><SFIrecord 1 byte"),
@@ -426,7 +426,7 @@ static int CmdEMVAC(const char *Cmd) {
arg_str0("dD", "decision", "<aac|tc|arqc>", "Terminal decision. aac - declined, tc - approved, arqc - online authorisation requested"),
arg_lit0("pP", "params", "Load parameters from `emv_defparams.json` file for CDOLdata making from CDOL and parameters"),
arg_lit0("mM", "make", "Make CDOLdata from CDOL (tag 8C and 8D) and parameters (def: use default parameters)"),
arg_lit0("aA", "apdu", "Show APDU reqests and responses"),
arg_lit0("aA", "apdu", "Show APDU requests and responses"),
arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
arg_lit0("wW", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"),
arg_strx1(NULL, NULL, "<hex>", "CDOLdata/CDOL"),
@@ -544,7 +544,7 @@ static int CmdEMVGenerateChallenge(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("kK", "keep", "Keep field ON for next command"),
arg_lit0("aA", "apdu", "Show APDU reqests and responses"),
arg_lit0("aA", "apdu", "Show APDU requests and responses"),
arg_lit0("wW", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"),
arg_param_end
};
@@ -598,7 +598,7 @@ static int CmdEMVInternalAuthenticate(const char *Cmd) {
arg_lit0("kK", "keep", "Keep field ON for next command"),
arg_lit0("pP", "params", "Load parameters from `emv_defparams.json` file for DDOLdata making from DDOL and parameters"),
arg_lit0("mM", "make", "Make DDOLdata from DDOL (tag 9F49) and parameters (def: use default parameters)"),
arg_lit0("aA", "apdu", "Show APDU reqests and responses"),
arg_lit0("aA", "apdu", "Show APDU requests and responses"),
arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
arg_lit0("wW", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"),
arg_strx1(NULL, NULL, "<hex>", "DDOLdata/DDOL"),
@@ -823,7 +823,7 @@ static int CmdEMVExec(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("sS", "select", "Activate field and select card"),
arg_lit0("aA", "apdu", "Show APDU reqests and responses"),
arg_lit0("aA", "apdu", "Show APDU requests and responses"),
arg_lit0("tT", "tlv", "TLV decode results"),
arg_lit0("jJ", "jload", "Load transaction parameters from `emv_defparams.json` file"),
arg_lit0("fF", "forceaid", "Force search AID. Search AID instead of execute PPSE"),
@@ -1448,7 +1448,7 @@ static int CmdEMVScan(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("aA", "apdu", "Show APDU reqests and responses"),
arg_lit0("aA", "apdu", "Show APDU requests and responses"),
arg_lit0("tT", "tlv", "TLV decode results"),
arg_lit0("eE", "extract", "Extract TLV elements and fill Application Data"),
arg_lit0("jJ", "jload", "Load transaction parameters from `emv_defparams.json` file"),
@@ -1872,7 +1872,7 @@ static int CmdEMVRoca(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("tT", "selftest", "Self test"),
arg_lit0("aA", "apdu", "Show APDU reqests and responses"),
arg_lit0("aA", "apdu", "Show APDU requests and responses"),
arg_lit0("wW", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"),
arg_param_end
};
+79 -10
View File
@@ -66,6 +66,16 @@ typedef struct {
uint8_t rfu[8];
} PACKED _ksx6924_internal_purse_info_t;
typedef struct {
uint8_t ALGep;
uint8_t VKep;
uint8_t BALep[4]; // uint32_t big-endian
uint8_t IDcenter;
uint8_t IDep[8]; // bcd
uint8_t NTep[4];
uint8_t Sign1[4];
} PACKED _ksx6924_initialize_card_response_t;
// Declares a structure for simple enums.
#define MAKE_ENUM_TYPE(KEY_TYPE) \
struct _ksx6924_enum_ ## KEY_TYPE { \
@@ -114,17 +124,17 @@ MAKE_ENUM_CONST(Alg, uint8_t,
// KSX6924LookupTMoneyIDCenter
MAKE_ENUM_CONST(TMoneyIDCenter, uint8_t,
{ 0x00, "reserved" },
{ 0x00, "Reserved" },
{ 0x01, "Korea Financial Telecommunications and Clearings Institute" },
{ 0x02, "A-Cash" },
{ 0x03, "Mybi" },
{ 0x04, "Reserved" },
{ 0x05, "V-Cash" },
{ 0x06, "Mondex Korea" },
{ 0x07, "Korea Expressway Corporation" },
{ 0x08, "Korea Smart Card Corporation" },
{ 0x08, "Tmoney Co., Ltd." },
{ 0x09, "KORAIL Networks" },
{ 0x0a, "Reserved" },
{ 0x0b, "EB Card Corporation" },
{ 0x0c, "Seoul Bus Transport Association" },
{ 0x0d, "Cardnet" },
@@ -165,17 +175,17 @@ MAKE_ENUM_CONST(TMoneyTCode, uint8_t,
// KSX6924LookupTMoneyCCode
MAKE_ENUM_CONST(TMoneyCCode, uint8_t,
{ 0x00, "None" },
{ 0x01, "KB Kookmin Bank" },
{ 0x02, "Nonghyup Bank" },
{ 0x01, "KB Card" },
{ 0x02, "NH Card" },
{ 0x03, "Lotte Card" },
{ 0x04, "BC Card" },
{ 0x05, "Samsung Card" },
{ 0x06, "Shinhan Bank" },
{ 0x06, "Shinhan Card" },
{ 0x07, "Citibank Korea" },
{ 0x08, "Korea Exchange Bank" },
{ 0x09, "Woori" },
{ 0x09, "Woori Card" },
{ 0x0a, "Hana SK Card" },
{ 0x0b, "Hyundai Capital Services" },
{ 0x0b, "Hyundai Card" },
);
static const char *KSX6924_UNKNOWN = "Unknown";
@@ -446,7 +456,7 @@ bool KSX6924GetBalance(uint32_t *result) {
/**
* Perform transaction initialization.
*/
bool KSX6924InitializeCard(uint8_t mpda1, uint8_t mpda2, uint8_t mpda3, uint8_t mpda4, uint8_t *result) {
bool KSX6924InitializeCard(uint8_t mpda1, uint8_t mpda2, uint8_t mpda3, uint8_t mpda4, uint8_t *result, size_t *result_len) {
if (result == NULL) {
return false;
@@ -472,9 +482,68 @@ bool KSX6924InitializeCard(uint8_t mpda1, uint8_t mpda2, uint8_t mpda3, uint8_t
//*result = ntohl(*(uint32_t*)(arr));
memcpy(result, arr, rlen + 2); // skip 2 sw bytes
memcpy(result_len, &rlen, sizeof(size_t));
return true;
}
/**
* Parses Initialize Card response
*/
bool KSX6924ParseInitializeCardResponse(const uint8_t *initCardResponse, size_t resp_len, struct ksx6924_initialize_card_response *ret) {
memset(ret, 0, sizeof(struct ksx6924_initialize_card_response));
if (resp_len != sizeof(_ksx6924_initialize_card_response_t)) {
// Invalid size!
PrintAndLogEx(FAILED, "Expected %ld bytes, got %ld\n", sizeof(_ksx6924_initialize_card_response_t), resp_len);
return false;
}
const _ksx6924_initialize_card_response_t *internalInitCardResponse = (const _ksx6924_initialize_card_response_t *)initCardResponse;
// Simple copies
ret->ALGep = internalInitCardResponse->ALGep;
ret->VKep = internalInitCardResponse->VKep;
ret->IDcenter = internalInitCardResponse->IDcenter;
// Fields that need rewriting
hex_to_buffer(ret->IDep, internalInitCardResponse->IDep,
sizeof(internalInitCardResponse->IDep),
sizeof(ret->IDep) - 1,
0, // min_str_len
0, // spaces_between
false // uppercase
);
ret->BALep = MemBeToUint4byte((uint8_t *)internalInitCardResponse->BALep);
ret->NTep = MemBeToUint4byte((uint8_t *)internalInitCardResponse->NTep);
memcpy(&ret->Sign1, &internalInitCardResponse->Sign1, 4);
// TODO
return true;
};
/**
* Prints out a Initialize Card response
*/
void KSX6924PrintInitializeCardResponse(const struct ksx6924_initialize_card_response *response) {
if (response == NULL) {
return;
}
PrintAndLogEx(INFO, "--- " _CYAN_("KS X 6924 Initialize Card Response") " ---------------------------");
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, " ALGep (Algorithm Identifier)........ %02x ( %s )", response->ALGep, KSX6924LookupAlg(response->ALGep, KSX6924_UNKNOWN));
PrintAndLogEx(INFO, " VKep (Version of Key) .............. %02x", response->VKep);
PrintAndLogEx(INFO, " BALep (Balance...................... %" PRIu32, response->BALep);
PrintAndLogEx(INFO, " IDcenter (Issuer ID) ............... %02x ( %s )", response->IDcenter, KSX6924LookupTMoneyIDCenter(response->IDcenter, KSX6924_UNKNOWN));
PrintAndLogEx(INFO, " IDep (Card number) ................. %s", response->IDep);
PrintAndLogEx(INFO, " NTep (Number of Transaction + 1) ... %" PRIu32, response->NTep);
PrintAndLogEx(INFO, " Sign1 .............................. %s", sprint_hex(response->Sign1, sizeof(response->Sign1)));
PrintAndLogEx(INFO, "");
}
/**
* Issues a proprietary "get record" command (CLA=90, INS=4C).
+19 -1
View File
@@ -53,6 +53,18 @@ struct ksx6924_purse_info {
uint8_t rfu[8];
};
// Convenience structure for representing purse information. Actual on-card
// format is in _ksx6924_initialize_card_response_t.
struct ksx6924_initialize_card_response {
uint8_t ALGep;
uint8_t VKep;
uint32_t BALep;
uint8_t IDcenter;
uint8_t IDep[17]; // hex digits + null terminator
uint32_t NTep;
uint8_t Sign1[4];
};
// Get card type description
const char *KSX6924LookupCardType(uint8_t key, const char *defaultValue);
@@ -94,7 +106,13 @@ bool KSX6924TrySelect(void);
bool KSX6924GetBalance(uint32_t *result);
// Perform transaction initialization.
bool KSX6924InitializeCard(uint8_t mpda1, uint8_t mpda2, uint8_t mpda3, uint8_t mpda4, uint8_t *result);
bool KSX6924InitializeCard(uint8_t mpda1, uint8_t mpda2, uint8_t mpda3, uint8_t mpda4, uint8_t *result, size_t *result_len);
// Parses Initialize Card response
bool KSX6924ParseInitializeCardResponse(const uint8_t *initCardResponse, size_t resp_len, struct ksx6924_initialize_card_response *ret);
// Prints out a Initialize Card response
void KSX6924PrintInitializeCardResponse(const struct ksx6924_initialize_card_response *response);
// Proprietary get record command. Function unknown.
// result must be 10 bytes long.
+4 -4
View File
@@ -244,11 +244,11 @@ const static vocabulory_t vocabulory[] = {
{ 1, "hf gallagher diversifykey" },
{ 1, "hf gallagher decode" },
{ 1, "hf ksx6924 help" },
{ 0, "hf ksx6924 balance" },
{ 0, "hf ksx6924 info" },
{ 0, "hf ksx6924 initialize" },
{ 0, "hf ksx6924 prec" },
{ 0, "hf ksx6924 select" },
{ 0, "hf ksx6924 info" },
{ 0, "hf ksx6924 balance" },
{ 0, "hf ksx6924 init" },
{ 0, "hf ksx6924 prec" },
{ 1, "hf jooki help" },
{ 0, "hf jooki clone" },
{ 1, "hf jooki decode" },