From b5bcf1654d2dbc4cb85ef9a6ebe5323dfdac7b5d Mon Sep 17 00:00:00 2001 From: klks Date: Mon, 25 May 2026 12:45:12 +0800 Subject: [PATCH 1/4] Fix code formatting, added a few more bounds checks and warnings --- client/src/cmdhffmcos.c | 1420 +++++++++++++++++++++++++++++---------- 1 file changed, 1077 insertions(+), 343 deletions(-) diff --git a/client/src/cmdhffmcos.c b/client/src/cmdhffmcos.c index f8efe8380..44f1138bc 100644 --- a/client/src/cmdhffmcos.c +++ b/client/src/cmdhffmcos.c @@ -14,7 +14,6 @@ // See LICENSE.txt for the text of the license. //----------------------------------------------------------------------------- // Commands for FMCOS CPU smart cards (Fudan Microelectronics) -// Reference implementation: client/pyscripts/fmcos/ //----------------------------------------------------------------------------- #include "cmdhffmcos.h" @@ -69,7 +68,10 @@ static const char *fmcos_print_sw(uint8_t sw1, uint8_t sw2) { switch (sw1) { case 0x62: - if (sw2 >= 0x02 && sw2 <= 0x80) { desc = "Triggering by the card"; break; } + if (sw2 >= 0x02 && sw2 <= 0x80) { + desc = "Triggering by the card"; + break; + } switch (sw2) { case 0x81: desc = "Part of returned data may be corrupted"; @@ -95,21 +97,39 @@ static const char *fmcos_print_sw(uint8_t sw1, uint8_t sw2) { break; case 0x63: - if (sw2 == 0x81) { desc = "File filled up by last write"; break; } - if ((sw2 & 0xF0) == 0xC0) { desc = "Counter (0-15) encoded in SW2 low nibble"; break; } + if (sw2 == 0x81) { + desc = "File filled up by last write"; + break; + } + if ((sw2 & 0xF0) == 0xC0) { + desc = "Counter (0-15) encoded in SW2 low nibble"; + break; + } break; case 0x64: - if (sw2 >= 0x02 && sw2 <= 0x80) { desc = "Triggering by the card"; break; } - if (sw2 == 0x01) { desc = "Immediate response required by card"; break; } + if (sw2 >= 0x02 && sw2 <= 0x80) { + desc = "Triggering by the card"; + break; + } + if (sw2 == 0x01) { + desc = "Immediate response required by card"; + break; + } break; case 0x65: - if (sw2 == 0x81) { desc = "Memory failure"; break; } + if (sw2 == 0x81) { + desc = "Memory failure"; + break; + } break; case 0x67: - if (sw2 == 0x00) { desc = "Invalid length"; break; } + if (sw2 == 0x00) { + desc = "Invalid length"; + break; + } break; case 0x68: @@ -203,15 +223,24 @@ static const char *fmcos_print_sw(uint8_t sw1, uint8_t sw2) { break; case 0x6D: - if (sw2 == 0x00) { desc = "Invalid INS"; break; } + if (sw2 == 0x00) { + desc = "Invalid INS"; + break; + } break; case 0x6E: - if (sw2 == 0x00) { desc = "Invalid CLA"; break; } + if (sw2 == 0x00) { + desc = "Invalid CLA"; + break; + } break; case 0x93: - if (sw2 == 0x02) { desc = "Invalid MAC"; break; } + if (sw2 == 0x02) { + desc = "Invalid MAC"; + break; + } break; case 0x94: @@ -228,7 +257,10 @@ static const char *fmcos_print_sw(uint8_t sw1, uint8_t sw2) { break; case 0x90: - if (sw2 == 0x00) { desc = "Success"; break; } + if (sw2 == 0x00) { + desc = "Success"; + break; + } break; default: @@ -310,8 +342,9 @@ static size_t fmcos_iso7816_pad(const uint8_t *in, size_t in_len, uint8_t *out) memcpy(out, in, in_len); out[in_len] = 0x80; size_t padded = in_len + 1; - while (padded % 8 != 0) + while (padded % 8 != 0) { out[padded++] = 0x00; + } return padded; } @@ -319,14 +352,16 @@ static size_t fmcos_iso7816_pad(const uint8_t *in, size_t in_len, uint8_t *out) // DES CBC-MAC with ISO7816 padding. key must be exactly 8 bytes. // iv is 8 bytes (typically the GET CHALLENGE response). // Writes mac_len bytes (<= 8) into mac_out. -static void fmcos_des_mac(const uint8_t *buf, size_t buf_len, +static bool fmcos_des_mac(const uint8_t *buf, size_t buf_len, const uint8_t key[8], const uint8_t iv[8], uint8_t *mac_out, size_t mac_len) { size_t max_padded = buf_len + 8; uint8_t *padded = calloc(max_padded, 1); - if (padded == NULL) - return; + if (padded == NULL) { + PrintAndLogEx(ERR, "calloc failed"); + return false; + } size_t padded_len = fmcos_iso7816_pad(buf, buf_len, padded); @@ -335,22 +370,25 @@ static void fmcos_des_mac(const uint8_t *buf, size_t buf_len, for (size_t i = 0; i < padded_len; i += 8) { uint8_t xored[8]; - for (int j = 0; j < 8; j++) + for (int j = 0; j < 8; j++) { xored[j] = val[j] ^ padded[i + j]; + } fmcos_des8_ecb_enc(key, xored, val); } free(padded); - if (mac_len > 8) + if (mac_len > 8) { mac_len = 8; + } memcpy(mac_out, val, mac_len); + return true; } // 3DES Retail MAC: DES-CBC-MAC with left-key-half (8 bytes), then decrypt // with right-key-half, then re-encrypt with left-key-half. // key must be exactly 16 bytes. Writes mac_len bytes (<= 8) into mac_out. -static void fmcos_3des_mac(const uint8_t *buf, size_t buf_len, +static bool fmcos_3des_mac(const uint8_t *buf, size_t buf_len, const uint8_t key[16], const uint8_t iv[8], uint8_t *mac_out, size_t mac_len) { @@ -358,15 +396,19 @@ static void fmcos_3des_mac(const uint8_t *buf, size_t buf_len, const uint8_t *key_r = key + 8; uint8_t val[8]; - fmcos_des_mac(buf, buf_len, key_l, iv, val, 8); + if (!fmcos_des_mac(buf, buf_len, key_l, iv, val, 8)) { + return false; + } uint8_t tmp[8]; fmcos_des8_ecb_dec(key_r, val, tmp); fmcos_des8_ecb_enc(key_l, tmp, val); - if (mac_len > 8) + if (mac_len > 8) { mac_len = 8; + } memcpy(mac_out, val, mac_len); + return true; } @@ -374,31 +416,36 @@ static void fmcos_3des_mac(const uint8_t *buf, size_t buf_len, // iv is the 8-byte GET CHALLENGE response used as the CBC IV. // key_len 8 -> fmcos_des_mac; key_len 16 -> fmcos_3des_mac. // Lc encodes payload_len + 4 (reserves space for the MAC itself). -static void fmcos_packet_mac(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, +static bool fmcos_packet_mac(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, const uint8_t *data, size_t data_len, const uint8_t *iv, const uint8_t *key, size_t key_len, uint8_t *mac_out) { size_t buf_len = 5 + data_len; uint8_t *mac_buf = calloc(buf_len, 1); - if (mac_buf == NULL) - return; + if (mac_buf == NULL) { + PrintAndLogEx(ERR, "calloc failed"); + return false; + } mac_buf[0] = cla; mac_buf[1] = ins; mac_buf[2] = p1; mac_buf[3] = p2; mac_buf[4] = (uint8_t)((data_len + 4) & 0xFF); - if (data_len > 0) + if (data_len > 0) { memcpy(&mac_buf[5], data, data_len); + } + bool ok; if (key_len == 8) { - fmcos_des_mac(mac_buf, buf_len, key, iv, mac_out, 4); + ok = fmcos_des_mac(mac_buf, buf_len, key, iv, mac_out, 4); } else { - fmcos_3des_mac(mac_buf, buf_len, key, iv, mac_out, 4); + ok = fmcos_3des_mac(mac_buf, buf_len, key, iv, mac_out, 4); } free(mac_buf); + return ok; } // --------------------------------------------------------------------------- @@ -407,11 +454,17 @@ static void fmcos_packet_mac(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, // Remove ISO7816 padding: scan backwards for 0x80 marker. static int fmcos_iso7816_unpad(const uint8_t *buf, size_t padded_len, size_t *out_len) { - if (padded_len == 0 || padded_len % 8 != 0) + if (padded_len == 0 || padded_len % 8 != 0) { return PM3_ESOFT; + } for (size_t i = padded_len; i > 0; i--) { - if (buf[i - 1] == 0x80) { *out_len = i - 1; return PM3_SUCCESS; } - if (buf[i - 1] != 0x00) break; + if (buf[i - 1] == 0x80) { + *out_len = i - 1; + return PM3_SUCCESS; + } + if (buf[i - 1] != 0x00) { + break; + } } PrintAndLogEx(ERR, "ISO7816 padding marker not found"); return PM3_ESOFT; @@ -422,9 +475,14 @@ static int fmcos_iso7816_unpad(const uint8_t *buf, size_t padded_len, size_t *ou static size_t fmcos_encrypt(const uint8_t *key, size_t key_len, const uint8_t *data, size_t data_len, uint8_t *out) { uint8_t padded[512] = {0}; + if (data_len + 8 > sizeof(padded)) { + PrintAndLogEx(ERR, "fmcos_encrypt: data too large (%zu bytes)", data_len); + return 0; + } size_t padded_len = fmcos_iso7816_pad(data, data_len, padded); - for (size_t off = 0; off < padded_len; off += 8) + for (size_t off = 0; off < padded_len; off += 8) { fmcos_ecb_encrypt(key, key_len, padded + off, out + off); + } return padded_len; } @@ -502,8 +560,9 @@ static int fmcos_get_challenge(uint8_t chal_len, bool activate, uint8_t *chal_ou int resp_len = 0; int res = fmcos_send_apdu(apdu, sizeof(apdu), activate, true, resp, &resp_len); - if (res != PM3_SUCCESS) + if (res != PM3_SUCCESS) { return res; + } if (resp_len < 2) { PrintAndLogEx(ERR, "Empty response to GET CHALLENGE"); @@ -533,15 +592,16 @@ static const uint8_t *fmcos_tlv_find(const uint8_t *buf, size_t len, uint8_t tag, size_t *vlen) { size_t i = 0; while (i + 1 < len) { - uint8_t t = buf[i++]; - uint8_t l = buf[i++]; - if (i + l > len) + uint8_t cur_tag = buf[i++]; + uint8_t cur_len = buf[i++]; + if (i + cur_len > len) { break; - if (t == tag) { - *vlen = l; + } + if (cur_tag == tag) { + *vlen = cur_len; return &buf[i]; } - i += l; + i += cur_len; } return NULL; } @@ -707,13 +767,17 @@ static int CmdHFFmcosSelect(const char *Cmd) { } if (res != PM3_SUCCESS) { - if (!keep) DropField(); + if (!keep) { + DropField(); + } return res; } if (resp_len < 2) { PrintAndLogEx(ERR, "Empty card response"); - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } @@ -728,10 +792,29 @@ static int CmdHFFmcosSelect(const char *Cmd) { } } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } +// Parse a required hex integer argument; returns false and prints an error on +// malformed input. Accepts bare hex digits (no "0x" prefix required). +static bool fmcos_parse_hex_int(const char *s, int *out) { + if (s == NULL || *s == '\0') { + PrintAndLogEx(ERR, "Empty hex value"); + return false; + } + char *end = NULL; + long v = strtol(s, &end, 16); + if (end == s || *end != '\0') { + PrintAndLogEx(ERR, "Invalid hex value: %s", s); + return false; + } + *out = (int)v; + return true; +} + // --------------------------------------------------------------------------- // hf fmcos auth external (Phase 2) // --------------------------------------------------------------------------- @@ -755,7 +838,11 @@ static int CmdHFFmcosAuthExternal(const char *Cmd) { }; CLIExecWithReturn(ctx, Cmd, argtable, false); - int key_id = (int)strtol(arg_get_str(ctx, 1)->sval[0], NULL, 16); + int key_id; + if (!fmcos_parse_hex_int(arg_get_str(ctx, 1)->sval[0], &key_id)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } uint8_t key[16] = {0}; int key_len = 0; @@ -780,7 +867,9 @@ static int CmdHFFmcosAuthExternal(const char *Cmd) { uint8_t challenge[8] = {0}; int res = fmcos_get_challenge(8, true, challenge); if (res != PM3_SUCCESS) { - if (!keep) DropField(); + if (!keep) { + DropField(); + } return res; } @@ -790,7 +879,9 @@ static int CmdHFFmcosAuthExternal(const char *Cmd) { uint8_t encrypted[8] = {0}; res = fmcos_ecb_encrypt(key, (size_t)key_len, challenge, encrypted); if (res != PM3_SUCCESS) { - if (!keep) DropField(); + if (!keep) { + DropField(); + } return res; } @@ -807,13 +898,17 @@ static int CmdHFFmcosAuthExternal(const char *Cmd) { int ea_resp_len = 0; res = fmcos_send_apdu(ea_apdu, sizeof(ea_apdu), false, keep, ea_resp, &ea_resp_len); if (res != PM3_SUCCESS) { - if (!keep) DropField(); + if (!keep) { + DropField(); + } return res; } if (ea_resp_len < 2) { PrintAndLogEx(ERR, "Empty response to EXTERNAL AUTHENTICATE"); - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } @@ -827,7 +922,9 @@ static int CmdHFFmcosAuthExternal(const char *Cmd) { PrintAndLogEx(FAILED, "External authentication " _RED_("failed")); } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -884,13 +981,17 @@ static int CmdHFFmcosAuthInternal(const char *Cmd) { int resp_len = 0; int res = fmcos_send_apdu(apdu, (size_t)(5 + data_len), true, keep, resp, &resp_len); if (res != PM3_SUCCESS) { - if (!keep) DropField(); + if (!keep) { + DropField(); + } return res; } if (resp_len < 2) { PrintAndLogEx(ERR, "Empty response to INTERNAL AUTHENTICATE"); - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } @@ -909,7 +1010,9 @@ static int CmdHFFmcosAuthInternal(const char *Cmd) { PrintAndLogEx(FAILED, "Internal authentication " _RED_("failed")); } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -963,26 +1066,37 @@ static int fmcos_write_cmd(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, if (prot == FMCOS_PROT_ENC) { uint8_t enc_in[256]; + if (data_len >= sizeof(enc_in)) { + PrintAndLogEx(ERR, "Write data too large for ENC mode (%zu bytes, max 255)", data_len); + return PM3_EINVARG; + } enc_in[0] = (uint8_t)(data_len & 0xFF); memcpy(enc_in + 1, data, data_len); payload_len = fmcos_encrypt(key, key_len, enc_in, 1 + data_len, payload); + if (payload_len == 0) { + return PM3_EMALLOC; + } cla |= 0x04; } else { memcpy(payload, data, data_len); payload_len = data_len; - if (prot == FMCOS_PROT_MAC) + if (prot == FMCOS_PROT_MAC) { cla |= 0x04; + } } if (prot != FMCOS_PROT_NONE) { uint8_t chal[8] = {0}; int res = fmcos_get_challenge(8, activate, chal); - if (res != PM3_SUCCESS) + if (res != PM3_SUCCESS) { return res; + } activate = false; uint8_t mac[4] = {0}; - fmcos_packet_mac(cla, ins, p1, p2, payload, payload_len, chal, key, key_len, mac); + if (!fmcos_packet_mac(cla, ins, p1, p2, payload, payload_len, chal, key, key_len, mac)) { + return PM3_EMALLOC; + } memcpy(payload + payload_len, mac, 4); payload_len += 4; } @@ -1011,8 +1125,9 @@ static int fmcos_read_cmd(uint8_t ins, uint8_t p1, uint8_t p2, uint8_t read_len, if (prot != FMCOS_PROT_NONE) { cla = 0x04; int res = fmcos_get_challenge(8, activate, mac_iv); - if (res != PM3_SUCCESS) + if (res != PM3_SUCCESS) { return res; + } activate = false; } @@ -1021,7 +1136,9 @@ static int fmcos_read_cmd(uint8_t ins, uint8_t p1, uint8_t p2, uint8_t read_len, if (prot != FMCOS_PROT_NONE) { // Case 4: CLA INS P1 P2 Lc=4 [MAC4] Le uint8_t mac[4] = {0}; - fmcos_packet_mac(cla, ins, p1, p2, NULL, 0, mac_iv, key, key_len, mac); + if (!fmcos_packet_mac(cla, ins, p1, p2, NULL, 0, mac_iv, key, key_len, mac)) { + return PM3_EMALLOC; + } apdu[0] = cla; apdu[1] = ins; apdu[2] = p1; @@ -1043,15 +1160,18 @@ static int fmcos_read_cmd(uint8_t ins, uint8_t p1, uint8_t p2, uint8_t read_len, uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; int res = fmcos_send_apdu(apdu, apdu_len, activate, leave_on, resp, &resp_len); - if (res != PM3_SUCCESS) + if (res != PM3_SUCCESS) { return res; - if (resp_len < 2) + } + if (resp_len < 2) { return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 != 0x90 || sw2 != 0x00) + if (sw1 != 0x90 || sw2 != 0x00) { return PM3_ESOFT; + } int data_len = resp_len - 2; @@ -1070,10 +1190,15 @@ static int fmcos_read_cmd(uint8_t ins, uint8_t p1, uint8_t p2, uint8_t read_len, uint8_t *ret_mac = resp + msg_len; uint8_t calc_mac[4] = {0}; - if (key_len == 8) - fmcos_des_mac(resp, (size_t)msg_len, key, mac_iv, calc_mac, 4); - else - fmcos_3des_mac(resp, (size_t)msg_len, key, mac_iv, calc_mac, 4); + bool mac_ok; + if (key_len == 8) { + mac_ok = fmcos_des_mac(resp, (size_t)msg_len, key, mac_iv, calc_mac, 4); + } else { + mac_ok = fmcos_3des_mac(resp, (size_t)msg_len, key, mac_iv, calc_mac, 4); + } + if (!mac_ok) { + return PM3_EMALLOC; + } if (memcmp(calc_mac, ret_mac, 4) != 0) { PrintAndLogEx(ERR, "Response MAC " _RED_("mismatch")); @@ -1085,13 +1210,18 @@ static int fmcos_read_cmd(uint8_t ins, uint8_t p1, uint8_t p2, uint8_t read_len, uint8_t plain[256] = {0}; size_t plain_len = 0; res = fmcos_decrypt(key, key_len, resp, (size_t)msg_len, plain, &plain_len); - if (res != PM3_SUCCESS) + if (res != PM3_SUCCESS) { return res; + } if (plain_len == 0) { PrintAndLogEx(ERR, "Decrypted length byte missing"); return PM3_ESOFT; } uint8_t actual_len = plain[0]; + if (actual_len > plain_len - 1) { + PrintAndLogEx(ERR, "Decrypted length byte %u exceeds payload %zu", actual_len, plain_len - 1); + return PM3_ESOFT; + } memcpy(data_out, plain + 1, actual_len); *data_out_len = (int)actual_len; } else { @@ -1124,17 +1254,30 @@ static int CmdHFFmcosErase(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; int res = fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "DF " _GREEN_("erased")); - else + } else { PrintAndLogEx(FAILED, "Erase " _RED_("failed")); + } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -1160,7 +1303,11 @@ static int CmdHFFmcosCreateDir(const char *Cmd) { uint8_t id_buf[2] = {0}; int id_len = 0; CLIGetHexWithReturn(ctx, 1, id_buf, &id_len); - int space = (int)strtol(arg_get_str(ctx, 2)->sval[0], NULL, 16); + int space; + if (!fmcos_parse_hex_int(arg_get_str(ctx, 2)->sval[0], &space)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } uint8_t cperm[1] = {0}; int cperm_len = 0; CLIGetHexWithReturn(ctx, 3, cperm, &cperm_len); @@ -1177,8 +1324,14 @@ static int CmdHFFmcosCreateDir(const char *Cmd) { bool apdu_log = arg_get_lit(ctx, 8); CLIParserFree(ctx); - if (id_len != 2) { PrintAndLogEx(ERR, "--id must be 2 bytes"); return PM3_EINVARG; } - if (space < 1 || space > 0xFFFF) { PrintAndLogEx(ERR, "--space out of range"); return PM3_EINVARG; } + if (id_len != 2) { + PrintAndLogEx(ERR, "--id must be 2 bytes"); + return PM3_EINVARG; + } + if (space < 1 || space > 0xFFFF) { + PrintAndLogEx(ERR, "--space out of range"); + return PM3_EINVARG; + } if (cperm_len != 1 || eperm_len != 1 || appid_len != 1) { PrintAndLogEx(ERR, "--cperm, --eperm, --appid must each be 1 byte"); return PM3_EINVARG; @@ -1195,7 +1348,10 @@ static int CmdHFFmcosCreateDir(const char *Cmd) { data[data_len++] = appid[0]; data[data_len++] = 0xFF; data[data_len++] = 0xFF; - if (name_len > 0) { memcpy(data + data_len, name, (size_t)name_len); data_len += (size_t)name_len; } + if (name_len > 0) { + memcpy(data + data_len, name, (size_t)name_len); + data_len += (size_t)name_len; + } uint8_t apdu[30]; apdu[0] = 0x80; @@ -1208,16 +1364,29 @@ static int CmdHFFmcosCreateDir(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; int res = fmcos_send_apdu(apdu, 5 + data_len, true, keep, resp, &resp_len); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "Directory " _GREEN_("created")); - else + } else { PrintAndLogEx(FAILED, "Create directory " _RED_("failed")); - if (!keep) DropField(); + } + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -1274,8 +1443,16 @@ static int CmdHFFmcosCreateFile(const char *Cmd) { int id_len = 0; CLIGetHexWithReturn(ctx, 1, id_buf, &id_len); int ftype = 0; - if (CLIGetOptionList(arg_get_str(ctx, 2), g_fmcos_filetype_opts, &ftype)) { CLIParserFree(ctx); return PM3_EINVARG; } - int size = (int)strtol(arg_get_str(ctx, 3)->sval[0], NULL, 16); + if (CLIGetOptionList(arg_get_str(ctx, 2), g_fmcos_filetype_opts, &ftype)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } + + int size; + if (!fmcos_parse_hex_int(arg_get_str(ctx, 3)->sval[0], &size)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } uint8_t rperm[1] = {0}; int rperm_len = 0; CLIGetHexWithReturn(ctx, 4, rperm, &rperm_len); @@ -1286,13 +1463,23 @@ static int CmdHFFmcosCreateFile(const char *Cmd) { int access_len = 0; CLIGetHexWithReturn(ctx, 6, access, &access_len); int prot = FMCOS_PROT_NONE; - if (CLIGetOptionList(arg_get_str(ctx, 7), g_fmcos_prot_opts, &prot)) { CLIParserFree(ctx); return PM3_EINVARG; } + if (CLIGetOptionList(arg_get_str(ctx, 7), g_fmcos_prot_opts, &prot)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } + bool keep = arg_get_lit(ctx, 8); bool apdu_log = arg_get_lit(ctx, 9); CLIParserFree(ctx); - if (id_len != 2) { PrintAndLogEx(ERR, "--id must be 2 bytes"); return PM3_EINVARG; } - if (size < 1 || size > 0xFFFF) { PrintAndLogEx(ERR, "--size out of range"); return PM3_EINVARG; } + if (id_len != 2) { + PrintAndLogEx(ERR, "--id must be 2 bytes"); + return PM3_EINVARG; + } + if (size < 1 || size > 0xFFFF) { + PrintAndLogEx(ERR, "--size out of range"); + return PM3_EINVARG; + } if (rperm_len != 1 || wperm_len != 1 || access_len != 1) { PrintAndLogEx(ERR, "--rperm, --wperm, --access must each be 1 byte"); return PM3_EINVARG; @@ -1322,16 +1509,29 @@ static int CmdHFFmcosCreateFile(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; int res = fmcos_send_apdu(apdu, 12, true, keep, resp, &resp_len); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "File " _GREEN_("created")); - else + } else { PrintAndLogEx(FAILED, "Create file " _RED_("failed")); - if (!keep) DropField(); + } + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -1355,7 +1555,11 @@ static int CmdHFFmcosCreateKeyfile(const char *Cmd) { uint8_t id_buf[2] = {0}; int id_len = 0; CLIGetHexWithReturn(ctx, 1, id_buf, &id_len); - int space = (int)strtol(arg_get_str(ctx, 2)->sval[0], NULL, 16); + int space; + if (!fmcos_parse_hex_int(arg_get_str(ctx, 2)->sval[0], &space)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } uint8_t dfsid[1] = {0}; int dfsid_len = 0; CLIGetHexWithReturn(ctx, 3, dfsid, &dfsid_len); @@ -1366,8 +1570,14 @@ static int CmdHFFmcosCreateKeyfile(const char *Cmd) { bool apdu_log = arg_get_lit(ctx, 6); CLIParserFree(ctx); - if (id_len != 2) { PrintAndLogEx(ERR, "--id must be 2 bytes"); return PM3_EINVARG; } - if (space < 1 || space > 0xFFFF) { PrintAndLogEx(ERR, "--space out of range"); return PM3_EINVARG; } + if (id_len != 2) { + PrintAndLogEx(ERR, "--id must be 2 bytes"); + return PM3_EINVARG; + } + if (space < 1 || space > 0xFFFF) { + PrintAndLogEx(ERR, "--space out of range"); + return PM3_EINVARG; + } if (dfsid_len != 1 || perm_len != 1) { PrintAndLogEx(ERR, "--dfsid and --perm must each be 1 byte"); return PM3_EINVARG; @@ -1391,16 +1601,29 @@ static int CmdHFFmcosCreateKeyfile(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; int res = fmcos_send_apdu(apdu, 12, true, keep, resp, &resp_len); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "Keyfile " _GREEN_("created")); - else + } else { PrintAndLogEx(FAILED, "Create keyfile " _RED_("failed")); - if (!keep) DropField(); + } + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -1458,7 +1681,11 @@ static int CmdHFFmcosReadBinary(const char *Cmd) { CLIGetHexWithReturn(ctx, 2, p2b, &p2_len); int rlen = arg_get_int_def(ctx, 3, 0); int prot = FMCOS_PROT_NONE; - if (CLIGetOptionList(arg_get_str(ctx, 4), g_fmcos_prot_opts, &prot)) { CLIParserFree(ctx); return PM3_EINVARG; } + if (CLIGetOptionList(arg_get_str(ctx, 4), g_fmcos_prot_opts, &prot)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } + uint8_t key[16] = {0}; int key_len = 0; CLIGetHexWithReturn(ctx, 5, key, &key_len); @@ -1466,10 +1693,19 @@ static int CmdHFFmcosReadBinary(const char *Cmd) { bool apdu_log = arg_get_lit(ctx, 7); CLIParserFree(ctx); - if (p1_len != 1 || p2_len != 1) { PrintAndLogEx(ERR, "--p1 and --p2 must each be 1 byte"); return PM3_EINVARG; } - if (rlen < 0 || rlen > 255) { PrintAndLogEx(ERR, "--len must be 0-255"); return PM3_EINVARG; } + if (p1_len != 1 || p2_len != 1) { + PrintAndLogEx(ERR, "--p1 and --p2 must each be 1 byte"); + return PM3_EINVARG; + } + if (rlen < 0 || rlen > 255) { + PrintAndLogEx(ERR, "--len must be 0-255"); + return PM3_EINVARG; + } if (prot != FMCOS_PROT_NONE) { - if (key_len != 8 && key_len != 16) { PrintAndLogEx(ERR, "--key must be 8 or 16 bytes when --prot is set"); return PM3_EINVARG; } + if (key_len != 8 && key_len != 16) { + PrintAndLogEx(ERR, "--key must be 8 or 16 bytes when --prot is set"); + return PM3_EINVARG; + } } SetAPDULogging(apdu_log); @@ -1478,9 +1714,12 @@ static int CmdHFFmcosReadBinary(const char *Cmd) { int res = fmcos_read_cmd(0xB0, p1b[0], p2b[0], (uint8_t)rlen, prot, key, (size_t)key_len, true, keep, data_out, &data_out_len); - if (res == PM3_SUCCESS) + if (res == PM3_SUCCESS) { PrintAndLogEx(SUCCESS, "Data: " _GREEN_("%s"), sprint_hex(data_out, (size_t)data_out_len)); - if (!keep) DropField(); + } + if (!keep) { + DropField(); + } return res; } @@ -1513,7 +1752,10 @@ static int CmdHFFmcosReadRecord(const char *Cmd) { CLIGetHexWithReturn(ctx, 2, fid_buf, &fid_len); int rlen = arg_get_int_def(ctx, 3, 0); int prot = FMCOS_PROT_NONE; - if (CLIGetOptionList(arg_get_str(ctx, 4), g_fmcos_prot_opts, &prot)) { CLIParserFree(ctx); return PM3_EINVARG; } + if (CLIGetOptionList(arg_get_str(ctx, 4), g_fmcos_prot_opts, &prot)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } uint8_t key[16] = {0}; int key_len = 0; CLIGetHexWithReturn(ctx, 5, key, &key_len); @@ -1522,11 +1764,23 @@ static int CmdHFFmcosReadRecord(const char *Cmd) { bool apdu_log = arg_get_lit(ctx, 8); CLIParserFree(ctx); - if (rec < 1 || rec > 255) { PrintAndLogEx(ERR, "--rec must be 1-255"); return PM3_EINVARG; } - if (fid_len != 1) { PrintAndLogEx(ERR, "--fid must be 1 byte"); return PM3_EINVARG; } - if (rlen < 0 || rlen > 253) { PrintAndLogEx(ERR, "--len must be 0-253"); return PM3_EINVARG; } + if (rec < 1 || rec > 255) { + PrintAndLogEx(ERR, "--rec must be 1-255"); + return PM3_EINVARG; + } + if (fid_len != 1) { + PrintAndLogEx(ERR, "--fid must be 1 byte"); + return PM3_EINVARG; + } + if (rlen < 0 || rlen > 253) { + PrintAndLogEx(ERR, "--len must be 0-253"); + return PM3_EINVARG; + } if (prot != FMCOS_PROT_NONE) { - if (key_len != 8 && key_len != 16) { PrintAndLogEx(ERR, "--key must be 8 or 16 bytes when --prot is set"); return PM3_EINVARG; } + if (key_len != 8 && key_len != 16) { + PrintAndLogEx(ERR, "--key must be 8 or 16 bytes when --prot is set"); + return PM3_EINVARG; + } } SetAPDULogging(apdu_log); @@ -1552,7 +1806,9 @@ static int CmdHFFmcosReadRecord(const char *Cmd) { } PrintAndLogEx(SUCCESS, "Record: " _GREEN_("%s"), sprint_hex(payload, (size_t)payload_len)); } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return res; } @@ -1606,7 +1862,10 @@ static int CmdHFFmcosWriteBinary(const char *Cmd) { int wdata_len = 0; CLIGetHexWithReturn(ctx, 3, wdata, &wdata_len); int prot = FMCOS_PROT_NONE; - if (CLIGetOptionList(arg_get_str(ctx, 4), g_fmcos_prot_opts, &prot)) { CLIParserFree(ctx); return PM3_EINVARG; } + if (CLIGetOptionList(arg_get_str(ctx, 4), g_fmcos_prot_opts, &prot)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } uint8_t key[16] = {0}; int key_len = 0; CLIGetHexWithReturn(ctx, 5, key, &key_len); @@ -1614,8 +1873,14 @@ static int CmdHFFmcosWriteBinary(const char *Cmd) { bool apdu_log = arg_get_lit(ctx, 7); CLIParserFree(ctx); - if (p1_len != 1 || p2_len != 1) { PrintAndLogEx(ERR, "--p1 and --p2 must each be 1 byte"); return PM3_EINVARG; } - if (wdata_len < 1) { PrintAndLogEx(ERR, "--data required"); return PM3_EINVARG; } + if (p1_len != 1 || p2_len != 1) { + PrintAndLogEx(ERR, "--p1 and --p2 must each be 1 byte"); + return PM3_EINVARG; + } + if (wdata_len < 1) { + PrintAndLogEx(ERR, "--data required"); + return PM3_EINVARG; + } if (prot != FMCOS_PROT_NONE && key_len != 8 && key_len != 16) { PrintAndLogEx(ERR, "--key must be 8 or 16 bytes when --prot is set"); return PM3_EINVARG; @@ -1628,16 +1893,29 @@ static int CmdHFFmcosWriteBinary(const char *Cmd) { wdata, (size_t)wdata_len, prot, key, (size_t)key_len, true, keep, resp, &resp_len); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "Binary " _GREEN_("written")); - else + } else { PrintAndLogEx(FAILED, "Write binary " _RED_("failed")); - if (!keep) DropField(); + } + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -1672,7 +1950,10 @@ static int CmdHFFmcosWriteRecord(const char *Cmd) { int wdata_len = 0; CLIGetHexWithReturn(ctx, 3, wdata, &wdata_len); int prot = FMCOS_PROT_NONE; - if (CLIGetOptionList(arg_get_str(ctx, 4), g_fmcos_prot_opts, &prot)) { CLIParserFree(ctx); return PM3_EINVARG; } + if (CLIGetOptionList(arg_get_str(ctx, 4), g_fmcos_prot_opts, &prot)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } uint8_t key[16] = {0}; int key_len = 0; CLIGetHexWithReturn(ctx, 5, key, &key_len); @@ -1681,10 +1962,22 @@ static int CmdHFFmcosWriteRecord(const char *Cmd) { bool apdu_log = arg_get_lit(ctx, 8); CLIParserFree(ctx); - if (rec < 1 || rec > 255) { PrintAndLogEx(ERR, "--rec must be 1-255"); return PM3_EINVARG; } - if (fid_len != 1) { PrintAndLogEx(ERR, "--fid must be 1 byte"); return PM3_EINVARG; } - if (wdata_len < 1) { PrintAndLogEx(ERR, "--data required"); return PM3_EINVARG; } - if (use_tlv && wdata_len > 243) { PrintAndLogEx(ERR, "--data too long for TLV write (max 243 bytes)"); return PM3_EINVARG; } + if (rec < 1 || rec > 255) { + PrintAndLogEx(ERR, "--rec must be 1-255"); + return PM3_EINVARG; + } + if (fid_len != 1) { + PrintAndLogEx(ERR, "--fid must be 1 byte"); + return PM3_EINVARG; + } + if (wdata_len < 1) { + PrintAndLogEx(ERR, "--data required"); + return PM3_EINVARG; + } + if (use_tlv && wdata_len > 243) { + PrintAndLogEx(ERR, "--data too long for TLV write (max 243 bytes)"); + return PM3_EINVARG; + } if (prot != FMCOS_PROT_NONE && key_len != 8 && key_len != 16) { PrintAndLogEx(ERR, "--key must be 8 or 16 bytes when --prot is set"); return PM3_EINVARG; @@ -1711,16 +2004,29 @@ static int CmdHFFmcosWriteRecord(const char *Cmd) { send_buf, send_len, prot, key, (size_t)key_len, true, keep, resp, &resp_len); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "Record " _GREEN_("written")); - else + } else { PrintAndLogEx(FAILED, "Write record " _RED_("failed")); - if (!keep) DropField(); + } + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -1770,7 +2076,10 @@ static int CmdHFFmcosAppend(const char *Cmd) { int wdata_len = 0; CLIGetHexWithReturn(ctx, 2, wdata, &wdata_len); int prot = FMCOS_PROT_NONE; - if (CLIGetOptionList(arg_get_str(ctx, 3), g_fmcos_prot_opts, &prot)) { CLIParserFree(ctx); return PM3_EINVARG; } + if (CLIGetOptionList(arg_get_str(ctx, 3), g_fmcos_prot_opts, &prot)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } uint8_t key[16] = {0}; int key_len = 0; CLIGetHexWithReturn(ctx, 4, key, &key_len); @@ -1778,8 +2087,14 @@ static int CmdHFFmcosAppend(const char *Cmd) { bool apdu_log = arg_get_lit(ctx, 6); CLIParserFree(ctx); - if (fid_len != 1) { PrintAndLogEx(ERR, "--fid must be 1 byte"); return PM3_EINVARG; } - if (wdata_len < 1) { PrintAndLogEx(ERR, "--data required"); return PM3_EINVARG; } + if (fid_len != 1) { + PrintAndLogEx(ERR, "--fid must be 1 byte"); + return PM3_EINVARG; + } + if (wdata_len < 1) { + PrintAndLogEx(ERR, "--data required"); + return PM3_EINVARG; + } if (prot != FMCOS_PROT_NONE && key_len != 8 && key_len != 16) { PrintAndLogEx(ERR, "--key must be 8 or 16 bytes when --prot is set"); return PM3_EINVARG; @@ -1794,16 +2109,29 @@ static int CmdHFFmcosAppend(const char *Cmd) { wdata, (size_t)wdata_len, prot, key, (size_t)key_len, true, keep, resp, &resp_len); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "Record " _GREEN_("appended")); - else + } else { PrintAndLogEx(FAILED, "Append record " _RED_("failed")); - if (!keep) DropField(); + } + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -1865,7 +2193,10 @@ static int CmdHFFmcosWriteKey(const char *Cmd) { int id_len = 0; CLIGetHexWithReturn(ctx, 2, id_buf, &id_len); int ktype = 0; - if (CLIGetOptionList(arg_get_str(ctx, 3), g_fmcos_keytype_opts, &ktype)) { CLIParserFree(ctx); return PM3_EINVARG; } + if (CLIGetOptionList(arg_get_str(ctx, 3), g_fmcos_keytype_opts, &ktype)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } uint8_t usage_b[1] = {0}; int usage_len = 0; CLIGetHexWithReturn(ctx, 4, usage_b, &usage_len); @@ -1891,17 +2222,32 @@ static int CmdHFFmcosWriteKey(const char *Cmd) { int authkey_len = 0; CLIGetHexWithReturn(ctx, 11, authkey, &authkey_len); int prot = FMCOS_PROT_NONE; - if (CLIGetOptionList(arg_get_str(ctx, 12), g_fmcos_prot_opts, &prot)) { CLIParserFree(ctx); return PM3_EINVARG; } + if (CLIGetOptionList(arg_get_str(ctx, 12), g_fmcos_prot_opts, &prot)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } bool keep = arg_get_lit(ctx, 13); bool apdu_log = arg_get_lit(ctx, 14); CLIParserFree(ctx); - if (op_len != 1 || id_len != 1) { PrintAndLogEx(ERR, "--op and --id must each be 1 byte"); return PM3_EINVARG; } - if (usage_len != 1) { PrintAndLogEx(ERR, "--usage must be 1 byte"); return PM3_EINVARG; } + if (op_len != 1 || id_len != 1) { + PrintAndLogEx(ERR, "--op and --id must each be 1 byte"); + return PM3_EINVARG; + } + if (usage_len != 1) { + PrintAndLogEx(ERR, "--usage must be 1 byte"); + return PM3_EINVARG; + } if (ktype == 0x3A) { - if (kval_len < 2 || kval_len > 6) { PrintAndLogEx(ERR, "--key for pin type must be 2-6 bytes (PIN value)"); return PM3_EINVARG; } + if (kval_len < 2 || kval_len > 6) { + PrintAndLogEx(ERR, "--key for pin type must be 2-6 bytes (PIN value)"); + return PM3_EINVARG; + } } else { - if (kval_len != 8 && kval_len != 16) { PrintAndLogEx(ERR, "--key must be 8 or 16 bytes"); return PM3_EINVARG; } + if (kval_len != 8 && kval_len != 16) { + PrintAndLogEx(ERR, "--key must be 8 or 16 bytes"); + return PM3_EINVARG; + } } if (prot != FMCOS_PROT_NONE && authkey_len != 8 && authkey_len != 16) { PrintAndLogEx(ERR, "--authkey must be 8 or 16 bytes when --prot is set"); @@ -1925,9 +2271,8 @@ static int CmdHFFmcosWriteKey(const char *Cmd) { data[data_len++] = change_b[0]; data[data_len++] = version_b[0]; data[data_len++] = algo_b[0]; - } // Group B-extauth: extauth - else if (ktype == 0x39) { + } else if (ktype == 0x39) { if (change_len != 1 || followup_len != 1 || errcnt_len != 1) { PrintAndLogEx(ERR, "extauth key type needs --change --followup --errcount"); return PM3_EINVARG; @@ -1935,9 +2280,8 @@ static int CmdHFFmcosWriteKey(const char *Cmd) { data[data_len++] = change_b[0]; data[data_len++] = followup_b[0]; data[data_len++] = errcnt_b[0]; - } // Group B-pin: pin - else if (ktype == 0x3A) { + } else if (ktype == 0x3A) { if (followup_len != 1 || errcnt_len != 1) { PrintAndLogEx(ERR, "pin key type needs --followup --errcount"); return PM3_EINVARG; @@ -1945,9 +2289,8 @@ static int CmdHFFmcosWriteKey(const char *Cmd) { data[data_len++] = 0xEF; data[data_len++] = followup_b[0]; data[data_len++] = errcnt_b[0]; - } // Group C: lineprotect unlockpin changepin - else if (ktype == 0x36 || ktype == 0x37 || ktype == 0x38) { + } else if (ktype == 0x36 || ktype == 0x37 || ktype == 0x38) { if (change_len != 1 || errcnt_len != 1) { PrintAndLogEx(ERR, "This key type needs --change --errcount"); return PM3_EINVARG; @@ -1972,16 +2315,29 @@ static int CmdHFFmcosWriteKey(const char *Cmd) { data, data_len, prot, authkey, (size_t)authkey_len, true, keep, resp, &resp_len); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "Key " _GREEN_("written")); - else + } else { PrintAndLogEx(FAILED, "Write key " _RED_("failed")); - if (!keep) DropField(); + } + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -2016,8 +2372,14 @@ static int CmdHFFmcosPinVerify(const char *Cmd) { bool apdu_log = arg_get_lit(ctx, 4); CLIParserFree(ctx); - if (id_len != 1) { PrintAndLogEx(ERR, "--id must be 1 byte"); return PM3_EINVARG; } - if (pin_len < 2 || pin_len > 6) { PrintAndLogEx(ERR, "--pin must be 2-6 bytes"); return PM3_EINVARG; } + if (id_len != 1) { + PrintAndLogEx(ERR, "--id must be 1 byte"); + return PM3_EINVARG; + } + if (pin_len < 2 || pin_len > 6) { + PrintAndLogEx(ERR, "--pin must be 2-6 bytes"); + return PM3_EINVARG; + } SetAPDULogging(apdu_log); // CLA=00 INS=20 P1=00 P2=key_id Lc=pin_len Data=pin @@ -2032,19 +2394,32 @@ static int CmdHFFmcosPinVerify(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; int res = fmcos_send_apdu(apdu, 5 + (size_t)pin_len, true, keep, resp, &resp_len); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "PIN " _GREEN_("verified")); - else if (sw1 == 0x63) + } else if (sw1 == 0x63) { PrintAndLogEx(FAILED, "Wrong PIN, retries remaining: %d", sw2 & 0x0F); - else + } else { PrintAndLogEx(FAILED, "PIN verify " _RED_("failed")); + } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -2079,9 +2454,18 @@ static int CmdHFFmcosPinChange(const char *Cmd) { bool apdu_log = arg_get_lit(ctx, 5); CLIParserFree(ctx); - if (id_len != 1) { PrintAndLogEx(ERR, "--id must be 1 byte"); return PM3_EINVARG; } - if (old_len < 2 || old_len > 6) { PrintAndLogEx(ERR, "--old must be 2-6 bytes"); return PM3_EINVARG; } - if (new_len < 2 || new_len > 6) { PrintAndLogEx(ERR, "--new must be 2-6 bytes"); return PM3_EINVARG; } + if (id_len != 1) { + PrintAndLogEx(ERR, "--id must be 1 byte"); + return PM3_EINVARG; + } + if (old_len < 2 || old_len > 6) { + PrintAndLogEx(ERR, "--old must be 2-6 bytes"); + return PM3_EINVARG; + } + if (new_len < 2 || new_len > 6) { + PrintAndLogEx(ERR, "--new must be 2-6 bytes"); + return PM3_EINVARG; + } SetAPDULogging(apdu_log); // Data = old_pin + 0xFF + new_pin @@ -2104,17 +2488,30 @@ static int CmdHFFmcosPinChange(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; int res = fmcos_send_apdu(apdu, 5 + data_len, true, keep, resp, &resp_len); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "PIN " _GREEN_("changed")); - else + } else { PrintAndLogEx(FAILED, "PIN change " _RED_("failed")); + } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -2150,20 +2547,32 @@ static int CmdHFFmcosPinReset(const char *Cmd) { bool apdu_log = arg_get_lit(ctx, 5); CLIParserFree(ctx); - if (id_len != 1) { PrintAndLogEx(ERR, "--id must be 1 byte"); return PM3_EINVARG; } - if (pin_len < 2 || pin_len > 6) { PrintAndLogEx(ERR, "--pin must be 2-6 bytes"); return PM3_EINVARG; } - if (key_len != 16) { PrintAndLogEx(ERR, "--key must be 16 bytes (change-PIN key)"); return PM3_EINVARG; } + if (id_len != 1) { + PrintAndLogEx(ERR, "--id must be 1 byte"); + return PM3_EINVARG; + } + if (pin_len < 2 || pin_len > 6) { + PrintAndLogEx(ERR, "--pin must be 2-6 bytes"); + return PM3_EINVARG; + } + if (key_len != 16) { + PrintAndLogEx(ERR, "--key must be 16 bytes (change-PIN key)"); + return PM3_EINVARG; + } SetAPDULogging(apdu_log); // mac_key = key_left XOR key_right uint8_t mac_key[8]; - for (int i = 0; i < 8; i++) + for (int i = 0; i < 8; i++) { mac_key[i] = key[i] ^ key[i + 8]; + } // MAC = DES-CBC-MAC(new_pin, mac_key, iv=0) uint8_t zero_iv[8] = {0}; uint8_t mac[4] = {0}; - fmcos_des_mac(pin, (size_t)pin_len, mac_key, zero_iv, mac, 4); + if (!fmcos_des_mac(pin, (size_t)pin_len, mac_key, zero_iv, mac, 4)) { + return PM3_EMALLOC; + } // Data = new_pin + mac[4] uint8_t data[10]; @@ -2182,17 +2591,30 @@ static int CmdHFFmcosPinReset(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; int res = fmcos_send_apdu(apdu, 5 + data_len, true, keep, resp, &resp_len); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "PIN " _GREEN_("reset")); - else + } else { PrintAndLogEx(FAILED, "PIN reset " _RED_("failed")); + } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -2228,9 +2650,18 @@ static int CmdHFFmcosPinUnblock(const char *Cmd) { bool apdu_log = arg_get_lit(ctx, 5); CLIParserFree(ctx); - if (id_len != 1) { PrintAndLogEx(ERR, "--id must be 1 byte"); return PM3_EINVARG; } - if (pin_len < 2 || pin_len > 6) { PrintAndLogEx(ERR, "--pin must be 2-6 bytes"); return PM3_EINVARG; } - if (key_len != 8 && key_len != 16) { PrintAndLogEx(ERR, "--key must be 8 or 16 bytes"); return PM3_EINVARG; } + if (id_len != 1) { + PrintAndLogEx(ERR, "--id must be 1 byte"); + return PM3_EINVARG; + } + if (pin_len < 2 || pin_len > 6) { + PrintAndLogEx(ERR, "--pin must be 2-6 bytes"); + return PM3_EINVARG; + } + if (key_len != 8 && key_len != 16) { + PrintAndLogEx(ERR, "--key must be 8 or 16 bytes"); + return PM3_EINVARG; + } SetAPDULogging(apdu_log); // Encrypt [len_byte | pin] with the unlock key @@ -2243,11 +2674,18 @@ static int CmdHFFmcosPinUnblock(const char *Cmd) { // GET CHALLENGE for packet MAC IV - CLA=84, INS=24 uint8_t chal[8] = {0}; int res = fmcos_get_challenge(8, true, chal); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } uint8_t mac[4] = {0}; - fmcos_packet_mac(0x84, 0x24, id_buf[0], 0x00, - enc_data, enc_len, chal, key, (size_t)key_len, mac); + if (!fmcos_packet_mac(0x84, 0x24, id_buf[0], 0x00, + enc_data, enc_len, chal, key, (size_t)key_len, mac)) { + return PM3_EMALLOC; + } // Build data = enc_data + mac uint8_t data[20]; @@ -2266,17 +2704,30 @@ static int CmdHFFmcosPinUnblock(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; res = fmcos_send_apdu(apdu, 5 + data_len, false, keep, resp, &resp_len); - if (res != PM3_SUCCESS) { if (!keep) DropField(); return res; } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (res != PM3_SUCCESS) { + if (!keep) { + DropField(); + } + return res; + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "PIN " _GREEN_("unblocked")); - else + } else { PrintAndLogEx(FAILED, "PIN unblock " _RED_("failed")); + } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -2312,10 +2763,12 @@ static void fmcos_get_datetime_bcd(uint8_t date_out[4], uint8_t time_out[3]) { char ds[9], ts[7]; strftime(ds, sizeof(ds), "%Y%m%d", t); strftime(ts, sizeof(ts), "%H%M%S", t); - for (int i = 0; i < 4; i++) + for (int i = 0; i < 4; i++) { date_out[i] = (uint8_t)((((ds[i * 2] - '0') & 0xF) << 4) | ((ds[i * 2 + 1] - '0') & 0xF)); - for (int i = 0; i < 3; i++) + } + for (int i = 0; i < 3; i++) { time_out[i] = (uint8_t)((((ts[i * 2] - '0') & 0xF) << 4) | ((ts[i * 2 + 1] - '0') & 0xF)); + } } // --------------------------------------------------------------------------- @@ -2341,14 +2794,17 @@ static int CmdHFFmcosBalance(const char *Cmd) { bool keep = arg_get_lit(ctx, 2); CLIParserFree(ctx); - if (res != PM3_SUCCESS) return PM3_EINVARG; + if (res != PM3_SUCCESS) { + return PM3_EINVARG; + } uint8_t apdu[5] = {0x80, 0x5C, 0x00, (uint8_t)bal_type, 0x04}; uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; - if (fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; + } if (resp_len < 2) { PrintAndLogEx(ERR, "Short response"); @@ -2391,7 +2847,11 @@ static int CmdHFFmcosCredit(const char *Cmd) { int bal_type = 0; int res = CLIGetOptionList(arg_get_str(ctx, 1), g_fmcos_baltype_opts, &bal_type); - int key_id = (int)strtol(arg_get_str(ctx, 2)->sval[0], NULL, 16); + int key_id; + if (!fmcos_parse_hex_int(arg_get_str(ctx, 2)->sval[0], &key_id)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } int amount_i = arg_get_int(ctx, 3); uint8_t terminal[6] = {0}; @@ -2409,12 +2869,30 @@ static int CmdHFFmcosCredit(const char *Cmd) { bool keep = arg_get_lit(ctx, 7); CLIParserFree(ctx); - if (res != PM3_SUCCESS) return PM3_EINVARG; - if (key_id < 0 || key_id > 0xFF) { PrintAndLogEx(ERR, "Key ID must be 0-255"); return PM3_EINVARG; } - if (amount_i <= 0) { PrintAndLogEx(ERR, "Amount must be positive"); return PM3_EINVARG; } - if (terminal_len != 6) { PrintAndLogEx(ERR, "Terminal ID must be 6 bytes"); return PM3_EINVARG; } - if (crde_key_len != 16) { PrintAndLogEx(ERR, "Credit key must be 16 bytes"); return PM3_EINVARG; } - if (ikey_len != 16) { PrintAndLogEx(ERR, "Internal key must be 16 bytes"); return PM3_EINVARG; } + if (res != PM3_SUCCESS) { + return PM3_EINVARG; + } + + if (key_id < 0 || key_id > 0xFF) { + PrintAndLogEx(ERR, "Key ID must be 0-255"); + return PM3_EINVARG; + } + if (amount_i <= 0) { + PrintAndLogEx(ERR, "Amount must be positive"); + return PM3_EINVARG; + } + if (terminal_len != 6) { + PrintAndLogEx(ERR, "Terminal ID must be 6 bytes"); + return PM3_EINVARG; + } + if (crde_key_len != 16) { + PrintAndLogEx(ERR, "Credit key must be 16 bytes"); + return PM3_EINVARG; + } + if (ikey_len != 16) { + PrintAndLogEx(ERR, "Internal key must be 16 bytes"); + return PM3_EINVARG; + } uint32_t amount = (uint32_t)amount_i; uint8_t tx_type = (uint8_t)bal_type; // transaction_type == balance_type for credit @@ -2438,22 +2916,29 @@ static int CmdHFFmcosCredit(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; - if (fmcos_send_apdu(ph1, sizeof(ph1), true, true, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(ph1, sizeof(ph1), true, true, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; + } // Response: old_balance[4] online_serial[2] key_ver[1] algo[1] random_1[4] mac_1[4] SW[2] if (resp_len < 18) { - if (resp_len >= 2) fmcos_print_sw(resp[resp_len - 2], resp[resp_len - 1]); + if (resp_len >= 2) { + fmcos_print_sw(resp[resp_len - 2], resp[resp_len - 1]); + } PrintAndLogEx(ERR, "Phase 1 short response (%d bytes) -- DF selected?", resp_len); g_fmcos_session_active = keep; - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; if (sw1 != 0x90 || sw2 != 0x00) { fmcos_print_sw(sw1, sw2); g_fmcos_session_active = keep; - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } @@ -2483,12 +2968,16 @@ static int CmdHFFmcosCredit(const char *Cmd) { uint8_t zero_iv[8] = {0}; uint8_t mac1_calc[4] = {0}; - fmcos_des_mac(mac1_buf, 15, process_key, zero_iv, mac1_calc, 4); + if (!fmcos_des_mac(mac1_buf, 15, process_key, zero_iv, mac1_calc, 4)) { + return PM3_EMALLOC; + } if (memcmp(mac1_calc, resp + 12, 4) != 0) { PrintAndLogEx(ERR, "MAC1 mismatch - card response invalid"); g_fmcos_session_active = keep; - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } PrintAndLogEx(INFO, "MAC1 OK old balance %u", old_balance); @@ -2508,7 +2997,9 @@ static int CmdHFFmcosCredit(const char *Cmd) { memcpy(mac2_buf + 15, ttime, 3); uint8_t mac2[4] = {0}; - fmcos_des_mac(mac2_buf, 18, process_key, zero_iv, mac2, 4); + if (!fmcos_des_mac(mac2_buf, 18, process_key, zero_iv, mac2, 4)) { + return PM3_EMALLOC; + } // ---- Phase 2: CREDIT (INS 52) ---- // APDU: CLA INS P1 P2 Lc[=11] date[4] time[3] mac2[4] Le[=4] @@ -2525,8 +3016,9 @@ static int CmdHFFmcosCredit(const char *Cmd) { memset(resp, 0, sizeof(resp)); resp_len = 0; - if (fmcos_send_apdu(ph2, sizeof(ph2), false, keep, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(ph2, sizeof(ph2), false, keep, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; + } // Response: TAC[4] SW[2] if (resp_len < 6) { @@ -2544,7 +3036,9 @@ static int CmdHFFmcosCredit(const char *Cmd) { // tac_key = XOR of the two 8-byte halves of the internal key uint32_t new_balance = old_balance + amount; uint8_t tac_key[8]; - for (int i = 0; i < 8; i++) tac_key[i] = ikey[i] ^ ikey[i + 8]; + for (int i = 0; i < 8; i++) { + tac_key[i] = ikey[i] ^ ikey[i + 8]; + } uint8_t tac_buf[24]; tac_buf[0] = (new_balance >> 24) & 0xFF; @@ -2555,7 +3049,9 @@ static int CmdHFFmcosCredit(const char *Cmd) { memcpy(tac_buf + 6, mac2_buf, 18); uint8_t tac_calc[4] = {0}; - fmcos_des_mac(tac_buf, 24, tac_key, zero_iv, tac_calc, 4); + if (!fmcos_des_mac(tac_buf, 24, tac_key, zero_iv, tac_calc, 4)) { + return PM3_EMALLOC; + } if (memcmp(tac_calc, resp, 4) != 0) { PrintAndLogEx(WARNING, "TAC mismatch - new balance %u may be incorrect", new_balance); @@ -2593,7 +3089,11 @@ static int CmdHFFmcosPurchase(const char *Cmd) { int bal_type = 0; int res = CLIGetOptionList(arg_get_str(ctx, 1), g_fmcos_baltype_opts, &bal_type); - int key_id = (int)strtol(arg_get_str(ctx, 2)->sval[0], NULL, 16); + int key_id; + if (!fmcos_parse_hex_int(arg_get_str(ctx, 2)->sval[0], &key_id)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } int amount_i = arg_get_int(ctx, 3); uint8_t terminal[6] = {0}; @@ -2615,13 +3115,34 @@ static int CmdHFFmcosPurchase(const char *Cmd) { bool keep = arg_get_lit(ctx, 8); CLIParserFree(ctx); - if (res != PM3_SUCCESS) return PM3_EINVARG; - if (key_id < 0 || key_id > 0xFF) { PrintAndLogEx(ERR, "Key ID must be 0-255"); return PM3_EINVARG; } - if (amount_i <= 0) { PrintAndLogEx(ERR, "Amount must be positive"); return PM3_EINVARG; } - if (terminal_len != 6) { PrintAndLogEx(ERR, "Terminal ID must be 6 bytes"); return PM3_EINVARG; } - if (purch_key_len != 16) { PrintAndLogEx(ERR, "Purchase key must be 16 bytes"); return PM3_EINVARG; } - if (ikey_len != 16) { PrintAndLogEx(ERR, "Internal key must be 16 bytes"); return PM3_EINVARG; } - if (serial_len != 0 && serial_len != 4) { PrintAndLogEx(ERR, "Serial must be 4 bytes"); return PM3_EINVARG; } + if (res != PM3_SUCCESS) { + return PM3_EINVARG; + } + + if (key_id < 0 || key_id > 0xFF) { + PrintAndLogEx(ERR, "Key ID must be 0-255"); + return PM3_EINVARG; + } + if (amount_i <= 0) { + PrintAndLogEx(ERR, "Amount must be positive"); + return PM3_EINVARG; + } + if (terminal_len != 6) { + PrintAndLogEx(ERR, "Terminal ID must be 6 bytes"); + return PM3_EINVARG; + } + if (purch_key_len != 16) { + PrintAndLogEx(ERR, "Purchase key must be 16 bytes"); + return PM3_EINVARG; + } + if (ikey_len != 16) { + PrintAndLogEx(ERR, "Internal key must be 16 bytes"); + return PM3_EINVARG; + } + if (serial_len != 0 && serial_len != 4) { + PrintAndLogEx(ERR, "Serial must be 4 bytes"); + return PM3_EINVARG; + } uint32_t amount = (uint32_t)amount_i; // transaction_type: 0x05 passbook purchase, 0x06 wallet purchase @@ -2646,22 +3167,29 @@ static int CmdHFFmcosPurchase(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; - if (fmcos_send_apdu(ph1, sizeof(ph1), true, true, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(ph1, sizeof(ph1), true, true, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; + } // Response: old_balance[4] offline_serial[2] overdraft_lim[3] key_ver[1] algo[1] random_1[4] SW[2] if (resp_len < 17) { - if (resp_len >= 2) fmcos_print_sw(resp[resp_len - 2], resp[resp_len - 1]); + if (resp_len >= 2) { + fmcos_print_sw(resp[resp_len - 2], resp[resp_len - 1]); + } PrintAndLogEx(ERR, "Phase 1 short response (%d bytes) -- DF selected?", resp_len); g_fmcos_session_active = keep; - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; if (sw1 != 0x90 || sw2 != 0x00) { fmcos_print_sw(sw1, sw2); g_fmcos_session_active = keep; - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } @@ -2696,7 +3224,9 @@ static int CmdHFFmcosPurchase(const char *Cmd) { uint8_t zero_iv[8] = {0}; uint8_t mac1[4] = {0}; - fmcos_des_mac(mac1_buf, 18, process_key, zero_iv, mac1, 4); + if (!fmcos_des_mac(mac1_buf, 18, process_key, zero_iv, mac1, 4)) { + return PM3_EMALLOC; + } // ---- Phase 2: DEBIT (INS 54, P1 01, P2 00) ---- // APDU: CLA INS P1 P2 Lc[=15] tx_serial[4] date[4] time[3] mac1[4] Le[=8] @@ -2714,8 +3244,9 @@ static int CmdHFFmcosPurchase(const char *Cmd) { memset(resp, 0, sizeof(resp)); resp_len = 0; - if (fmcos_send_apdu(ph2, sizeof(ph2), false, keep, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(ph2, sizeof(ph2), false, keep, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; + } // Response: TAC[4] mac2_card[4] SW[2] if (resp_len < 10) { @@ -2733,7 +3264,9 @@ static int CmdHFFmcosPurchase(const char *Cmd) { // tac_key = XOR of the two 8-byte halves of the internal key uint32_t new_balance = old_balance - amount; uint8_t tac_key[8]; - for (int i = 0; i < 8; i++) tac_key[i] = ikey[i] ^ ikey[i + 8]; + for (int i = 0; i < 8; i++) { + tac_key[i] = ikey[i] ^ ikey[i + 8]; + } uint8_t tac_buf[22]; tac_buf[0] = (amount >> 24) & 0xFF; @@ -2747,7 +3280,9 @@ static int CmdHFFmcosPurchase(const char *Cmd) { memcpy(tac_buf + 19, ttime, 3); uint8_t tac_calc[4] = {0}; - fmcos_des_mac(tac_buf, 22, tac_key, zero_iv, tac_calc, 4); + if (!fmcos_des_mac(tac_buf, 22, tac_key, zero_iv, tac_calc, 4)) { + return PM3_EMALLOC; + } if (memcmp(tac_calc, resp, 4) != 0) { PrintAndLogEx(WARNING, "TAC mismatch - new balance %u may be incorrect", new_balance); @@ -2781,7 +3316,11 @@ static int CmdHFFmcosOverdraft(const char *Cmd) { }; CLIExecWithReturn(ctx, Cmd, argtable, false); - int key_id = (int)strtol(arg_get_str(ctx, 1)->sval[0], NULL, 16); + int key_id; + if (!fmcos_parse_hex_int(arg_get_str(ctx, 1)->sval[0], &key_id)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } int limit_i = arg_get_int(ctx, 2); uint8_t terminal[6] = {0}; @@ -2799,11 +3338,26 @@ static int CmdHFFmcosOverdraft(const char *Cmd) { bool keep = arg_get_lit(ctx, 6); CLIParserFree(ctx); - if (key_id < 0 || key_id > 0xFF) { PrintAndLogEx(ERR, "Key ID must be 0-255"); return PM3_EINVARG; } - if (limit_i < 0 || limit_i > 0xFFFFFF) { PrintAndLogEx(ERR, "Limit must be 0-16777215"); return PM3_EINVARG; } - if (terminal_len != 6) { PrintAndLogEx(ERR, "Terminal ID must be 6 bytes"); return PM3_EINVARG; } - if (od_key_len != 16) { PrintAndLogEx(ERR, "Overdraft key must be 16 bytes"); return PM3_EINVARG; } - if (ikey_len != 0 && ikey_len != 16) { PrintAndLogEx(ERR, "Internal key must be 16 bytes"); return PM3_EINVARG; } + if (key_id < 0 || key_id > 0xFF) { + PrintAndLogEx(ERR, "Key ID must be 0-255"); + return PM3_EINVARG; + } + if (limit_i < 0 || limit_i > 0xFFFFFF) { + PrintAndLogEx(ERR, "Limit must be 0-16777215"); + return PM3_EINVARG; + } + if (terminal_len != 6) { + PrintAndLogEx(ERR, "Terminal ID must be 6 bytes"); + return PM3_EINVARG; + } + if (od_key_len != 16) { + PrintAndLogEx(ERR, "Overdraft key must be 16 bytes"); + return PM3_EINVARG; + } + if (ikey_len != 0 && ikey_len != 16) { + PrintAndLogEx(ERR, "Internal key must be 16 bytes"); + return PM3_EINVARG; + } bool verify_tac = (ikey_len == 16); uint32_t new_limit = (uint32_t)limit_i; @@ -2823,22 +3377,29 @@ static int CmdHFFmcosOverdraft(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; - if (fmcos_send_apdu(ph1, sizeof(ph1), true, true, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(ph1, sizeof(ph1), true, true, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; + } // Response: old_balance[4] online_serial[2] old_od_limit[3] key_ver[1] algo[1] random_1[4] card_mac1[4] SW[2] if (resp_len < 21) { - if (resp_len >= 2) fmcos_print_sw(resp[resp_len - 2], resp[resp_len - 1]); + if (resp_len >= 2) { + fmcos_print_sw(resp[resp_len - 2], resp[resp_len - 1]); + } PrintAndLogEx(ERR, "Phase 1 short response (%d bytes) -- DF selected?", resp_len); g_fmcos_session_active = keep; - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; if (sw1 != 0x90 || sw2 != 0x00) { fmcos_print_sw(sw1, sw2); g_fmcos_session_active = keep; - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } @@ -2867,12 +3428,16 @@ static int CmdHFFmcosOverdraft(const char *Cmd) { uint8_t zero_iv[8] = {0}; uint8_t mac1_calc[4] = {0}; - fmcos_des_mac(mac1_buf, 14, process_key, zero_iv, mac1_calc, 4); + if (!fmcos_des_mac(mac1_buf, 14, process_key, zero_iv, mac1_calc, 4)) { + return PM3_EMALLOC; + } if (memcmp(mac1_calc, resp + 15, 4) != 0) { PrintAndLogEx(ERR, "MAC1 mismatch - card response invalid"); g_fmcos_session_active = keep; - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } PrintAndLogEx(INFO, "MAC1 OK"); @@ -2891,7 +3456,9 @@ static int CmdHFFmcosOverdraft(const char *Cmd) { memcpy(mac2_buf + 14, ttime, 3); uint8_t mac2[4] = {0}; - fmcos_des_mac(mac2_buf, 17, process_key, zero_iv, mac2, 4); + if (!fmcos_des_mac(mac2_buf, 17, process_key, zero_iv, mac2, 4)) { + return PM3_EMALLOC; + } // ---- Phase 2: UPDATE OVERDRAFT (INS 58, P1 00, P2 00) ---- // APDU: CLA INS P1 P2 Lc[=14] new_limit[3] date[4] time[3] mac2[4] Le[=4] @@ -2911,8 +3478,9 @@ static int CmdHFFmcosOverdraft(const char *Cmd) { memset(resp, 0, sizeof(resp)); resp_len = 0; - if (fmcos_send_apdu(ph2, sizeof(ph2), false, keep, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(ph2, sizeof(ph2), false, keep, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; + } if (resp_len < 2) { PrintAndLogEx(ERR, "Phase 2 short response"); @@ -2922,8 +3490,9 @@ static int CmdHFFmcosOverdraft(const char *Cmd) { sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 != 0x90 || sw2 != 0x00) + if (sw1 != 0x90 || sw2 != 0x00) { return PM3_ESOFT; + } PrintAndLogEx(SUCCESS, "Overdraft limit updated to " _GREEN_("%u"), new_limit); @@ -2933,7 +3502,9 @@ static int CmdHFFmcosOverdraft(const char *Cmd) { } else { // TAC key: XOR of the two 8-byte halves of the internal key (same as credit/purchase) uint8_t tac_key[8]; - for (int i = 0; i < 8; i++) tac_key[i] = ikey[i] ^ ikey[i + 8]; + for (int i = 0; i < 8; i++) { + tac_key[i] = ikey[i] ^ ikey[i + 8]; + } // TAC buffer: tac_bal[4]|serial[2]|new_limit[3]|0x07[1]|terminal[6]|date[4]|time[3] // The card stores (actual_funds + od_limit) as its balance field. When the limit @@ -2954,7 +3525,9 @@ static int CmdHFFmcosOverdraft(const char *Cmd) { memcpy(tac_buf + 20, ttime, 3); uint8_t tac_calc[4] = {0}; - fmcos_des_mac(tac_buf, 23, tac_key, zero_iv, tac_calc, 4); + if (!fmcos_des_mac(tac_buf, 23, tac_key, zero_iv, tac_calc, 4)) { + return PM3_EMALLOC; + } if (memcmp(tac_calc, resp, 4) != 0) { PrintAndLogEx(WARNING, "TAC mismatch - overdraft limit update may be unverified"); @@ -3022,7 +3595,9 @@ static int CmdHFFmcosBlock(const char *Cmd) { uint8_t chal[8] = {0}; int res = fmcos_get_challenge(8, true, chal); if (res != PM3_SUCCESS) { - if (!keep) DropField(); + if (!keep) { + DropField(); + } return res; } @@ -3040,7 +3615,9 @@ static int CmdHFFmcosBlock(const char *Cmd) { uint8_t p1 = 0x00; uint8_t mac[4] = {0}; - fmcos_packet_mac(cla, ins, p1, p2, NULL, 0, chal, key, (size_t)key_len, mac); + if (!fmcos_packet_mac(cla, ins, p1, p2, NULL, 0, chal, key, (size_t)key_len, mac)) { + return PM3_EMALLOC; + } uint8_t apdu[9]; apdu[0] = cla; @@ -3054,13 +3631,17 @@ static int CmdHFFmcosBlock(const char *Cmd) { int resp_len = 0; res = fmcos_send_apdu(apdu, sizeof(apdu), false, keep, resp, &resp_len); if (res != PM3_SUCCESS) { - if (!keep) DropField(); + if (!keep) { + DropField(); + } return res; } if (resp_len < 2) { PrintAndLogEx(ERR, "Empty response"); - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } @@ -3079,7 +3660,9 @@ static int CmdHFFmcosBlock(const char *Cmd) { PrintAndLogEx(FAILED, "Block command " _RED_("failed")); } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -3118,13 +3701,17 @@ static int CmdHFFmcosUnblock(const char *Cmd) { uint8_t chal[8] = {0}; int res = fmcos_get_challenge(8, true, chal); if (res != PM3_SUCCESS) { - if (!keep) DropField(); + if (!keep) { + DropField(); + } return res; } // APP UNBLOCK: CLA=84 INS=18 P1=00 P2=00 uint8_t mac[4] = {0}; - fmcos_packet_mac(0x84, 0x18, 0x00, 0x00, NULL, 0, chal, key, (size_t)key_len, mac); + if (!fmcos_packet_mac(0x84, 0x18, 0x00, 0x00, NULL, 0, chal, key, (size_t)key_len, mac)) { + return PM3_EMALLOC; + } uint8_t apdu[9]; apdu[0] = 0x84; @@ -3138,13 +3725,17 @@ static int CmdHFFmcosUnblock(const char *Cmd) { int resp_len = 0; res = fmcos_send_apdu(apdu, sizeof(apdu), false, keep, resp, &resp_len); if (res != PM3_SUCCESS) { - if (!keep) DropField(); + if (!keep) { + DropField(); + } return res; } if (resp_len < 2) { PrintAndLogEx(ERR, "Empty response"); - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_ESOFT; } @@ -3158,7 +3749,9 @@ static int CmdHFFmcosUnblock(const char *Cmd) { PrintAndLogEx(FAILED, "Unblock command " _RED_("failed")); } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -3192,9 +3785,17 @@ static int CmdHFFmcosHistory(const char *Cmd) { bool apdu_log = arg_get_lit(ctx, 4); CLIParserFree(ctx); - if (fid_len != 1) { PrintAndLogEx(ERR, "--fid must be 1 byte"); return PM3_EINVARG; } - if (count < 0 || count > 255) { PrintAndLogEx(ERR, "--count must be 0-255"); return PM3_EINVARG; } - if (count == 0) count = 255; + if (fid_len != 1) { + PrintAndLogEx(ERR, "--fid must be 1 byte"); + return PM3_EINVARG; + } + if (count < 0 || count > 255) { + PrintAndLogEx(ERR, "--count must be 0-255"); + return PM3_EINVARG; + } + if (count == 0) { + count = 255; + } SetAPDULogging(apdu_log); @@ -3219,16 +3820,23 @@ static int CmdHFFmcosHistory(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; - if (fmcos_send_apdu(apdu, sizeof(apdu), true, true, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(apdu, sizeof(apdu), true, true, resp, &resp_len) != PM3_SUCCESS) { break; + } - if (resp_len < 2) break; + if (resp_len < 2) { + break; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; if (sw1 != 0x90 || sw2 != 0x00) { - if (rec == 1) fmcos_print_sw(sw1, sw2); + if (rec == 1) { + fmcos_print_sw(sw1, sw2); + } + break; + } + if (resp_len < 25) { break; } - if (resp_len < 25) break; uint8_t *p = resp; uint16_t serial = ((uint16_t)p[0] << 8) | p[1]; @@ -3253,7 +3861,10 @@ static int CmdHFFmcosHistory(const char *Cmd) { snprintf(type_hex_buf, sizeof(type_hex_buf), "0x%02X ", tx_type); const char *type_name = type_hex_buf; for (size_t i = 0; i < ntypes; i++) { - if (tx_types[i].code == tx_type) { type_name = tx_types[i].name; break; } + if (tx_types[i].code == tx_type) { + type_name = tx_types[i].name; + break; + } } PrintAndLogEx(INFO, "%2d | %s | %s | %s | %10u | %8u | %06X | %s", @@ -3263,12 +3874,15 @@ static int CmdHFFmcosHistory(const char *Cmd) { found++; } - if (!keep) DropField(); + if (!keep) { + DropField(); + } - if (found == 0) + if (found == 0) { PrintAndLogEx(INFO, "(no records found)"); - else + } else { PrintAndLogEx(SUCCESS, "%d record%s", found, found == 1 ? "" : "s"); + } return PM3_SUCCESS; } @@ -3316,18 +3930,27 @@ static int CmdHFFmcosTidSetCard(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; - if (fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; + } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "SET CARD " _GREEN_("OK")); - else + } else { PrintAndLogEx(FAILED, "SET CARD " _RED_("failed")); + } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -3360,18 +3983,27 @@ static int CmdHFFmcosTidSetUID(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; - if (fmcos_send_apdu(apdu, 5 + (size_t)uid_len, true, keep, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(apdu, 5 + (size_t)uid_len, true, keep, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; + } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "SET UID " _GREEN_("OK")); - else + } else { PrintAndLogEx(FAILED, "SET UID " _RED_("failed")); + } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -3410,19 +4042,28 @@ static int CmdHFFmcosTidSetAuth(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; - if (fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; + } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "SET INTERNAL AUTH " _GREEN_("OK") " (key %s)", lock ? _RED_("locked") : "unlocked"); - else + } else { PrintAndLogEx(FAILED, "SET INTERNAL AUTH " _RED_("failed")); + } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -3445,18 +4086,27 @@ static int CmdHFFmcosTidErase(const char *Cmd) { uint8_t apdu[5] = {0xE0, 0xEC, 0x00, 0x00, 0x00}; uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; - if (fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; + } - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "ERASE " _GREEN_("OK")); - else + } else { PrintAndLogEx(FAILED, "ERASE " _RED_("failed")); + } - if (!keep) DropField(); + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -3487,8 +4137,14 @@ static int CmdHFFmcosTidProvision(const char *Cmd) { bool keep = arg_get_lit(ctx, 4); CLIParserFree(ctx); - if (uid_len < 4 || uid_len > 7) { PrintAndLogEx(ERR, "--uid must be 4-7 bytes"); return PM3_EINVARG; } - if (key_len != 8) { PrintAndLogEx(ERR, "--key must be 8 bytes"); return PM3_EINVARG; } + if (uid_len < 4 || uid_len > 7) { + PrintAndLogEx(ERR, "--uid must be 4-7 bytes"); + return PM3_EINVARG; + } + if (key_len != 8) { + PrintAndLogEx(ERR, "--key must be 8 bytes"); + return PM3_EINVARG; + } uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; @@ -3507,67 +4163,69 @@ static int CmdHFFmcosTidProvision(const char *Cmd) { // 1 - SET CARD { - uint8_t a[44] = {0x00, 0xEF, 0x00, 0x00, 0x27}; - memcpy(a + 5, g_fmcos_tid_setcard_data, 39); - TID_STEP("SET CARD", a, sizeof(a), true, true); + uint8_t apdu[44] = {0x00, 0xEF, 0x00, 0x00, 0x27}; + memcpy(apdu + 5, g_fmcos_tid_setcard_data, 39); + TID_STEP("SET CARD", apdu, sizeof(apdu), true, true); } // 2 - SET UID { - uint8_t a[12] = {0x00, 0x85, 0x00, 0x00, (uint8_t)uid_len}; - memcpy(a + 5, uid, (size_t)uid_len); - TID_STEP("SET UID", a, 5 + (size_t)uid_len, true, true); + uint8_t apdu[12] = {0x00, 0x85, 0x00, 0x00, (uint8_t)uid_len}; + memcpy(apdu + 5, uid, (size_t)uid_len); + TID_STEP("SET UID", apdu, 5 + (size_t)uid_len, true, true); } // 3 - SET INTERNAL AUTH { - uint8_t a[15] = {0x00, 0x21, 0x00, 0x00, 0x0A}; - memcpy(a + 5, key, 8); - a[13] = lock ? FMCOS_TID_AUTH_LOCKED : FMCOS_TID_AUTH_UNLOCKED; - a[14] = 0x00; - TID_STEP("SET INTERNAL AUTH", a, sizeof(a), true, true); + uint8_t apdu[15] = {0x00, 0x21, 0x00, 0x00, 0x0A}; + memcpy(apdu + 5, key, 8); + apdu[13] = lock ? FMCOS_TID_AUTH_LOCKED : FMCOS_TID_AUTH_UNLOCKED; + apdu[14] = 0x00; + TID_STEP("SET INTERNAL AUTH", apdu, sizeof(apdu), true, true); } // 4 - ERASE PrintAndLogEx(WARNING, "Erasing card file system..."); { - uint8_t a[5] = {0xE0, 0xEC, 0x00, 0x00, 0x00}; - TID_STEP("ERASE", a, sizeof(a), true, true); + uint8_t apdu[5] = {0xE0, 0xEC, 0x00, 0x00, 0x00}; + TID_STEP("ERASE", apdu, sizeof(apdu), true, true); } // 5 - SELECT MF (3F00) { - uint8_t a[7] = {0x00, 0xA4, 0x00, 0x00, 0x02, 0x3F, 0x00}; - TID_STEP("SELECT MF", a, sizeof(a), true, true); + uint8_t apdu[7] = {0x00, 0xA4, 0x00, 0x00, 0x02, 0x3F, 0x00}; + TID_STEP("SELECT MF", apdu, sizeof(apdu), true, true); } // 6 - CREATE DF (3F00, SFI=02, name="1PAY.SYS.DDF01") { uint8_t name_len = (uint8_t)sizeof(g_fmcos_tid_mf_name); - uint8_t a[32] = {0x80, 0xE0, 0x00, 0x00, (uint8_t)(name_len + 9), - 0x3F, 0x00, 0x6F, 0xFF, 0xF0, 0xF0, 0x02, 0x01, 0x00 - }; - memcpy(a + 14, g_fmcos_tid_mf_name, name_len); - TID_STEP("CREATE DF (3F00)", a, 14 + name_len, true, true); + uint8_t apdu[32] = {0x80, 0xE0, 0x00, 0x00, (uint8_t)(name_len + 9), + 0x3F, 0x00, 0x6F, 0xFF, 0xF0, 0xF0, 0x02, 0x01, 0x00 + }; + memcpy(apdu + 14, g_fmcos_tid_mf_name, name_len); + TID_STEP("CREATE DF (3F00)", apdu, 14 + name_len, true, true); } // 7 - SELECT DF 3F00 { - uint8_t a[7] = {0x00, 0xA4, 0x00, 0x00, 0x02, 0x3F, 0x00}; - TID_STEP("SELECT DF (3F00)", a, sizeof(a), true, true); + uint8_t apdu[7] = {0x00, 0xA4, 0x00, 0x00, 0x02, 0x3F, 0x00}; + TID_STEP("SELECT DF (3F00)", apdu, sizeof(apdu), true, true); } // 8 - CREATE KEYFILE { - uint8_t a[16] = {0x80, 0xE0, 0x02, 0x00, 0x0B}; - memcpy(a + 5, g_fmcos_tid_keyfile_data, 11); - TID_STEP("CREATE KEYFILE", a, sizeof(a), true, keep); + uint8_t apdu[16] = {0x80, 0xE0, 0x02, 0x00, 0x0B}; + memcpy(apdu + 5, g_fmcos_tid_keyfile_data, 11); + TID_STEP("CREATE KEYFILE", apdu, sizeof(apdu), true, keep); } #undef TID_STEP PrintAndLogEx(SUCCESS, "TID provisioning " _GREEN_("complete")); - if (!keep) DropField(); + if (!keep) { + DropField(); + } return PM3_SUCCESS; tid_provision_fail: @@ -3594,7 +4252,11 @@ static int CmdHFFmcosTidCreateDF(const char *Cmd) { uint8_t id_buf[2] = {0}; int id_len = 0; CLIGetHexWithReturn(ctx, 1, id_buf, &id_len); - int size = (int)strtol(arg_get_str(ctx, 2)->sval[0], NULL, 16); + int size; + if (!fmcos_parse_hex_int(arg_get_str(ctx, 2)->sval[0], &size)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } uint8_t sfi_buf[1] = {0}; int sfi_len = 0; CLIGetHexWithReturn(ctx, 3, sfi_buf, &sfi_len); @@ -3604,9 +4266,18 @@ static int CmdHFFmcosTidCreateDF(const char *Cmd) { bool keep = arg_get_lit(ctx, 5); CLIParserFree(ctx); - if (id_len != 2) { PrintAndLogEx(ERR, "--id must be 2 bytes"); return PM3_EINVARG; } - if (sfi_len != 1) { PrintAndLogEx(ERR, "--sfi must be 1 byte"); return PM3_EINVARG; } - if (size < 1 || size > 0xFFFF) { PrintAndLogEx(ERR, "--size out of range"); return PM3_EINVARG; } + if (id_len != 2) { + PrintAndLogEx(ERR, "--id must be 2 bytes"); + return PM3_EINVARG; + } + if (sfi_len != 1) { + PrintAndLogEx(ERR, "--sfi must be 1 byte"); + return PM3_EINVARG; + } + if (size < 1 || size > 0xFFFF) { + PrintAndLogEx(ERR, "--size out of range"); + return PM3_EINVARG; + } // 80 E0 01 00 [fid_hi][fid_lo] [size_hi][size_lo] F0 F0 [sfi] 01 FF [name...] uint8_t apdu[32] = { @@ -3621,16 +4292,25 @@ static int CmdHFFmcosTidCreateDF(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; - if (fmcos_send_apdu(apdu, 14 + (size_t)name_len, true, keep, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(apdu, 14 + (size_t)name_len, true, keep, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "CREATE DF " _GREEN_("OK")); - else + } else { PrintAndLogEx(FAILED, "CREATE DF " _RED_("failed")); - if (!keep) DropField(); + } + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -3663,15 +4343,22 @@ static int CmdHFFmcosTidCreateBin(const char *Cmd) { CLIExecWithReturn(ctx, Cmd, argtable, true); int ftype = 0; - if (CLIGetOptionList(arg_get_str(ctx, 1), g_fmcos_tid_create_opts, &ftype)) { CLIParserFree(ctx); return PM3_EINVARG; } + if (CLIGetOptionList(arg_get_str(ctx, 1), g_fmcos_tid_create_opts, &ftype)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } bool is_keyfile = (ftype == 1); uint8_t id_buf[2] = {0}; int id_len = 0; CLIGetHexWithReturn(ctx, 2, id_buf, &id_len); int size = 0; - if (arg_get_str(ctx, 3)->count > 0) - size = (int)strtol(arg_get_str(ctx, 3)->sval[0], NULL, 16); + if (arg_get_str(ctx, 3)->count > 0) { + if (!fmcos_parse_hex_int(arg_get_str(ctx, 3)->sval[0], &size)) { + CLIParserFree(ctx); + return PM3_EINVARG; + } + } uint8_t sfi_buf[1] = {0}; int sfi_len = 0; CLIGetHexWithReturn(ctx, 4, sfi_buf, &sfi_len); @@ -3690,11 +4377,24 @@ static int CmdHFFmcosTidCreateBin(const char *Cmd) { // Fixed TID keyfile: 80 E0 02 00 0B 1E 00 00 00 30 FF FF 00 30 00 00 memcpy(apdu + 5, g_fmcos_tid_keyfile_data, 11); } else { - if (rperm_len == 0) rperm_buf[0] = 0xF0; - if (wperm_len == 0) wperm_buf[0] = 0xF0; - if (id_len != 2) { PrintAndLogEx(ERR, "--id must be 2 bytes"); return PM3_EINVARG; } - if (sfi_len != 1) { PrintAndLogEx(ERR, "--sfi must be 1 byte"); return PM3_EINVARG; } - if (size < 1 || size > 0xFFFF) { PrintAndLogEx(ERR, "--size out of range"); return PM3_EINVARG; } + if (rperm_len == 0) { + rperm_buf[0] = 0xF0; + } + if (wperm_len == 0) { + wperm_buf[0] = 0xF0; + } + if (id_len != 2) { + PrintAndLogEx(ERR, "--id must be 2 bytes"); + return PM3_EINVARG; + } + if (sfi_len != 1) { + PrintAndLogEx(ERR, "--sfi must be 1 byte"); + return PM3_EINVARG; + } + if (size < 1 || size > 0xFFFF) { + PrintAndLogEx(ERR, "--size out of range"); + return PM3_EINVARG; + } // 80 E0 02 00 0B 00 [fid_hi][fid_lo] [size_hi][size_lo] [rperm][wperm] [sfi] 00 FF 00 apdu[5] = 0x00; apdu[6] = id_buf[0]; @@ -3711,16 +4411,25 @@ static int CmdHFFmcosTidCreateBin(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; - if (fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, is_keyfile ? "CREATE KEYFILE " _GREEN_("OK") : "CREATE binary EF " _GREEN_("OK")); - else + } else { PrintAndLogEx(FAILED, is_keyfile ? "CREATE KEYFILE " _RED_("failed") : "CREATE binary EF " _RED_("failed")); - if (!keep) DropField(); + } + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } @@ -3764,13 +4473,29 @@ static int CmdHFFmcosTidCreateRec(const char *Cmd) { bool keep = arg_get_lit(ctx, 7); CLIParserFree(ctx); - if (rperm_len == 0) rperm_buf[0] = 0xF0; - if (wperm_len == 0) wperm_buf[0] = 0xF0; + if (rperm_len == 0) { + rperm_buf[0] = 0xF0; + } + if (wperm_len == 0) { + wperm_buf[0] = 0xF0; + } - if (id_len != 2) { PrintAndLogEx(ERR, "--id must be 2 bytes"); return PM3_EINVARG; } - if (cnt_len != 1) { PrintAndLogEx(ERR, "--count must be 1 byte"); return PM3_EINVARG; } - if (rlen_len != 1) { PrintAndLogEx(ERR, "--reclen must be 1 byte"); return PM3_EINVARG; } - if (sfi_len != 1) { PrintAndLogEx(ERR, "--sfi must be 1 byte"); return PM3_EINVARG; } + if (id_len != 2) { + PrintAndLogEx(ERR, "--id must be 2 bytes"); + return PM3_EINVARG; + } + if (cnt_len != 1) { + PrintAndLogEx(ERR, "--count must be 1 byte"); + return PM3_EINVARG; + } + if (rlen_len != 1) { + PrintAndLogEx(ERR, "--reclen must be 1 byte"); + return PM3_EINVARG; + } + if (sfi_len != 1) { + PrintAndLogEx(ERR, "--sfi must be 1 byte"); + return PM3_EINVARG; + } // 80 E0 02 00 0B 01 [fid_hi][fid_lo] [count][reclen] [rperm][wperm] [sfi] 00 FF 00 uint8_t apdu[16] = { @@ -3785,16 +4510,25 @@ static int CmdHFFmcosTidCreateRec(const char *Cmd) { uint8_t resp[APDU_RES_LEN] = {0}; int resp_len = 0; - if (fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len) != PM3_SUCCESS) + if (fmcos_send_apdu(apdu, sizeof(apdu), true, keep, resp, &resp_len) != PM3_SUCCESS) { return PM3_ESOFT; - if (resp_len < 2) { if (!keep) DropField(); return PM3_ESOFT; } + } + if (resp_len < 2) { + if (!keep) { + DropField(); + } + return PM3_ESOFT; + } uint8_t sw1 = resp[resp_len - 2], sw2 = resp[resp_len - 1]; fmcos_print_sw(sw1, sw2); - if (sw1 == 0x90 && sw2 == 0x00) + if (sw1 == 0x90 && sw2 == 0x00) { PrintAndLogEx(SUCCESS, "CREATE record EF " _GREEN_("OK")); - else + } else { PrintAndLogEx(FAILED, "CREATE record EF " _RED_("failed")); - if (!keep) DropField(); + } + if (!keep) { + DropField(); + } return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } From 729c276fd36ca42c902e2c09db346efabd40b93b Mon Sep 17 00:00:00 2001 From: klks Date: Mon, 25 May 2026 13:37:32 +0800 Subject: [PATCH 2/4] Merge tid commands down one level per iceman's advice, updated docs --- client/src/cmdhffmcos.c | 98 ++++++++++++------------- doc/fmcos.md | 153 ++++++++++++++++++++-------------------- 2 files changed, 124 insertions(+), 127 deletions(-) diff --git a/client/src/cmdhffmcos.c b/client/src/cmdhffmcos.c index 44f1138bc..646b3a244 100644 --- a/client/src/cmdhffmcos.c +++ b/client/src/cmdhffmcos.c @@ -672,7 +672,20 @@ static int CmdHFFmcosInfo(const char *Cmd) { size_t dfname_len = 0; const uint8_t *dfname = fmcos_tlv_find(outer, outer_len, 0x84, &dfname_len); if (dfname != NULL) { - PrintAndLogEx(INFO, "DF Name (84): %s", sprint_hex(dfname, dfname_len)); + bool name_is_ascii = true; + for (size_t i = 0; i < dfname_len; i++) { + if (dfname[i] < 0x20 || dfname[i] > 0x7E) { + name_is_ascii = false; + break; + } + } + if (name_is_ascii) { + PrintAndLogEx(INFO, "DF Name (84): %s ( " _GREEN_("%.*s") " )", + sprint_hex(dfname, dfname_len), + (int)dfname_len, (const char *)dfname); + } else { + PrintAndLogEx(INFO, "DF Name (84): %s", sprint_hex(dfname, dfname_len)); + } } size_t prop_len = 0; @@ -3913,9 +3926,9 @@ static const uint8_t g_fmcos_tid_mf_name[] = { static int CmdHFFmcosTidSetCard(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos tid setcard", + CLIParserInit(&ctx, "hf fmcos tidsetcard", "Send the TID SET CARD configuration APDU (fixed 39-byte payload).", - "hf fmcos tid setcard"); + "hf fmcos tidsetcard"); void *argtable[] = { arg_param_begin, arg_lit0("k", "keep", "keep field ON after command"), @@ -3956,10 +3969,10 @@ static int CmdHFFmcosTidSetCard(const char *Cmd) { static int CmdHFFmcosTidSetUID(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos tid setuid", + CLIParserInit(&ctx, "hf fmcos tidsetuid", "Program the TID card UID (4-7 bytes).", - "hf fmcos tid setuid --uid 13371337\n" - "hf fmcos tid setuid --uid 0102030405060708"); + "hf fmcos tidsetuid --uid 13371337\n" + "hf fmcos tidsetuid --uid 0102030405060708"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "uid", "", "UID bytes (4-7 bytes)"), @@ -4009,11 +4022,11 @@ static int CmdHFFmcosTidSetUID(const char *Cmd) { static int CmdHFFmcosTidSetAuth(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos tid setauth", + CLIParserInit(&ctx, "hf fmcos tidsetauth", "Write the TID internal authentication key and set the lock state.\n" "Lock byte: 0xAA = locked (permanent), 0x55 = unlocked (default).", - "hf fmcos tid setauth --key 1122334455667788\n" - "hf fmcos tid setauth --key 1122334455667788 --lock"); + "hf fmcos tidsetauth --key 1122334455667788\n" + "hf fmcos tidsetauth --key 1122334455667788 --lock"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "key", "", "internal auth key (8 bytes)"), @@ -4069,9 +4082,9 @@ static int CmdHFFmcosTidSetAuth(const char *Cmd) { static int CmdHFFmcosTidErase(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos tid erase", + CLIParserInit(&ctx, "hf fmcos tiderase", "Erase the TID card file system (CLA=E0 INS=EC -- irreversible).", - "hf fmcos tid erase"); + "hf fmcos tiderase"); void *argtable[] = { arg_param_begin, arg_lit0("k", "keep", "keep field ON after command"), @@ -4112,12 +4125,12 @@ static int CmdHFFmcosTidErase(const char *Cmd) { static int CmdHFFmcosTidProvision(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos tid provision", + CLIParserInit(&ctx, "hf fmcos tidprovision", "Full TID card provisioning sequence:\n" " SET CARD -> SET UID -> SET INTERNAL AUTH -> ERASE\n" " -> SELECT MF -> CREATE MF (3F00, 1PAY.SYS.DDF01) -> SELECT MF -> CREATE KEYFILE", - "hf fmcos tid provision --uid 13371337 --key 1122334455667788\n" - "hf fmcos tid provision --uid 13371337 --key 1122334455667788 --lock"); + "hf fmcos tidprovision --uid 13371337 --key 1122334455667788\n" + "hf fmcos tidprovision --uid 13371337 --key 1122334455667788 --lock"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "uid", "", "UID bytes (4-7 bytes)"), @@ -4235,10 +4248,10 @@ tid_provision_fail: static int CmdHFFmcosTidCreateDF(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos tid createdf", + CLIParserInit(&ctx, "hf fmcos tidcreatedf", "CREATE sub-DF using TID format (P1=01, FID in data).\n" "Note: TID CREATE DF has a different layout from standard 'hf fmcos create dir'.", - "hf fmcos tid createdf --id 3f01 --size 0f00 --sfi 96 --name 44444630 31"); + "hf fmcos tidcreatedf --id 3f01 --size 0f00 --sfi 96 --name 44444630 31"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "id", "<4hex>", "2-byte file ID"), @@ -4322,13 +4335,13 @@ static const CLIParserOption g_fmcos_tid_create_opts[] = { static int CmdHFFmcosTidCreateBin(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos tid createbin", + CLIParserInit(&ctx, "hf fmcos tidcreatebin", "CREATE binary EF or KEYFILE using TID format (P1=02, FID in data, fixed Lc=11).\n" "Use --type keyfile to create the fixed TID keyfile in the currently selected DF.\n" "Note: TID CREATE EF has a different layout from standard 'hf fmcos create file'.", - "hf fmcos tid createbin --id 0001 --size 0100 --sfi 01\n" - "hf fmcos tid createbin --id 0002 --size 0040 --sfi 02 --rperm 20 --wperm f0\n" - "hf fmcos tid createbin --type keyfile"); + "hf fmcos tidcreatebin --id 0001 --size 0100 --sfi 01\n" + "hf fmcos tidcreatebin --id 0002 --size 0040 --sfi 02 --rperm 20 --wperm f0\n" + "hf fmcos tidcreatebin --type keyfile"); void *argtable[] = { arg_param_begin, arg_str0(NULL, "type", "", "file type: bin (default) or keyfile"), @@ -4435,11 +4448,11 @@ static int CmdHFFmcosTidCreateBin(const char *Cmd) { static int CmdHFFmcosTidCreateRec(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos tid createrec", + CLIParserInit(&ctx, "hf fmcos tidcreaterec", "CREATE fixed-length record EF using TID format (P1=02, subtype=01, FID in data, fixed Lc=11).\n" "Note: TID CREATE EF has a different layout from standard 'hf fmcos create file'.", - "hf fmcos tid createrec --id 0003 --count 04 --reclen 08 --sfi 03\n" - "hf fmcos tid createrec --id 0003 --count 04 --reclen 10 --sfi 03 --rperm 20 --wperm f0"); + "hf fmcos tidcreaterec --id 0003 --count 04 --reclen 08 --sfi 03\n" + "hf fmcos tidcreaterec --id 0003 --count 04 --reclen 10 --sfi 03 --rperm 20 --wperm f0"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "id", "<4hex>", "2-byte file ID"), @@ -4532,33 +4545,6 @@ static int CmdHFFmcosTidCreateRec(const char *Cmd) { return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } -static int CmdHFFmcosTidHelp(const char *Cmd); - -static command_t TidCommandTable[] = { - {"help", CmdHFFmcosTidHelp, AlwaysAvailable, "This help"}, - {"setcard", CmdHFFmcosTidSetCard, IfPm3Iso14443a, "SET CARD configuration block"}, - {"setuid", CmdHFFmcosTidSetUID, IfPm3Iso14443a, "SET UID"}, - {"setauth", CmdHFFmcosTidSetAuth, IfPm3Iso14443a, "SET INTERNAL AUTH key"}, - {"erase", CmdHFFmcosTidErase, IfPm3Iso14443a, "ERASE card file system"}, - {"provision", CmdHFFmcosTidProvision, IfPm3Iso14443a, "Full provisioning sequence"}, - {"--------", CmdHFFmcosTidHelp, AlwaysAvailable, "--------- " _CYAN_("File creation") " ----------"}, - {"createdf", CmdHFFmcosTidCreateDF, IfPm3Iso14443a, "CREATE sub-DF (TID format)"}, - {"createbin", CmdHFFmcosTidCreateBin, IfPm3Iso14443a, "CREATE binary EF (TID format)"}, - {"createrec", CmdHFFmcosTidCreateRec, IfPm3Iso14443a, "CREATE record EF (TID format)"}, - {NULL, NULL, NULL, NULL} -}; - -static int CmdHFFmcosTidHelp(const char *Cmd) { - (void)Cmd; - CmdsHelp(TidCommandTable); - return PM3_SUCCESS; -} - -static int CmdHFFmcosTid(const char *Cmd) { - clearCommandBuffer(); - return CmdsParse(TidCommandTable, Cmd); -} - // --------------------------------------------------------------------------- // Top-level command table // --------------------------------------------------------------------------- @@ -4588,8 +4574,16 @@ static command_t CommandTable[] = { {"history", CmdHFFmcosHistory, IfPm3Iso14443a, "READ transaction history from loop EF"}, {"block", CmdHFFmcosBlock, IfPm3Iso14443a, "BLOCK card or application"}, {"unblock", CmdHFFmcosUnblock, IfPm3Iso14443a, "UNBLOCK application"}, - {"--------", CmdHelp, AlwaysAvailable, "--------- " _CYAN_("TID provisioning") " ----------"}, - {"tid", CmdHFFmcosTid, IfPm3Iso14443a, "{ TID card provisioning }"}, + {"--------", CmdHelp, AlwaysAvailable, "--------- " _CYAN_("TID provisioning") " ----------"}, + {"tidsetcard", CmdHFFmcosTidSetCard, IfPm3Iso14443a, "SET CARD configuration block"}, + {"tidsetuid", CmdHFFmcosTidSetUID, IfPm3Iso14443a, "SET UID"}, + {"tidsetauth", CmdHFFmcosTidSetAuth, IfPm3Iso14443a, "SET INTERNAL AUTH key"}, + {"tiderase", CmdHFFmcosTidErase, IfPm3Iso14443a, "ERASE TID card file system"}, + {"tidprovision", CmdHFFmcosTidProvision, IfPm3Iso14443a, "Full TID provisioning sequence"}, + {"--------", CmdHelp, AlwaysAvailable, "--------- " _CYAN_("TID file creation") " ---------"}, + {"tidcreatedf", CmdHFFmcosTidCreateDF, IfPm3Iso14443a, "CREATE sub-DF (TID format)"}, + {"tidcreatebin", CmdHFFmcosTidCreateBin, IfPm3Iso14443a, "CREATE binary EF (TID format)"}, + {"tidcreaterec", CmdHFFmcosTidCreateRec, IfPm3Iso14443a, "CREATE record EF (TID format)"}, {NULL, NULL, NULL, NULL} }; diff --git a/doc/fmcos.md b/doc/fmcos.md index 5d65b1845..1eb2047d6 100644 --- a/doc/fmcos.md +++ b/doc/fmcos.md @@ -48,14 +48,14 @@ All commands in this family are reachable via `hf fmcos `. - [Access Rights Byte](#access-rights-byte) - [Complete Wallet Session Walkthrough](#complete-wallet-session-walkthrough) - [TID Tag Provisioning](#tid-tag-provisioning) - - [tid setcard](#tid-setcard) - - [tid setuid](#tid-setuid) - - [tid setauth](#tid-setauth) - - [tid erase](#tid-erase) - - [tid provision](#tid-provision) - - [tid createdf](#tid-createdf) - - [tid createbin](#tid-createbin) - - [tid createrec](#tid-createrec) + - [tidsetcard](#tidsetcard) + - [tidsetuid](#tidsetuid) + - [tidsetauth](#tidsetauth) + - [tiderase](#tiderase) + - [tidprovision](#tidprovision) + - [tidcreatedf](#tidcreatedf) + - [tidcreatebin](#tidcreatebin) + - [tidcreaterec](#tidcreaterec) - [TID Vendor Card Templates](#tid-vendor-card-templates) --- @@ -1005,28 +1005,31 @@ When hf fmcos auth external is called with any key, the card will always return [+] SW: 9000 - Success [+] External authentication successful -All TID subcommands are reached via `hf fmcos tid `. +All TID commands are direct subcommands of `hf fmcos`, prefixed with `tid`. -| Subcommand | Description | -|------------|-------------| -| `setcard` | Write fixed card configuration block (`INS 0xEF`) | -| `setuid` | Program the ISO14443-A UID (`INS 0x85`) | -| `setauth` | Write the internal auth key and lock state (`INS 0x21`) | -| `erase` | Erase the card file system (`CLA 0xE0 INS 0xEC`) | -| `provision` | Full provisioning sequence in one command | +| Command | Description | +|---------|-------------| +| `tidsetcard` | Write fixed card configuration block (`INS 0xEF`) | +| `tidsetuid` | Program the ISO14443-A UID (`INS 0x85`) | +| `tidsetauth` | Write the internal auth key and lock state (`INS 0x21`) | +| `tiderase` | Erase the card file system (`CLA 0xE0 INS 0xEC`) | +| `tidprovision` | Full provisioning sequence in one command | +| `tidcreatedf` | CREATE sub-DF (TID format) | +| `tidcreatebin` | CREATE binary EF or KEYFILE (TID format) | +| `tidcreaterec` | CREATE record EF (TID format) | -> **Order matters.** When provisioning manually, always run `setcard` -> `setuid` -> `setauth` -> -> `erase` -> create file structure. `provision` does this automatically. +> **Order matters.** When provisioning manually, always run `tidsetcard` -> `tidsetuid` -> `tidsetauth` +> -> `tiderase` -> create file structure. `tidprovision` does this automatically. --- -### tid setcard +### tidsetcard Send the fixed 39-byte SET CARD configuration APDU. The payload is hardcoded - there is limited information on what the fields do. This must be sent before any other provisioning step. ``` -hf fmcos tid setcard +hf fmcos tidsetcard ``` | Flag | Description | @@ -1037,13 +1040,13 @@ hf fmcos tid setcard --- -### tid setuid +### tidsetuid Program the card's ISO14443-A UID. ``` -hf fmcos tid setuid --uid 13371337 -hf fmcos tid setuid --uid 0102030405060708 +hf fmcos tidsetuid --uid 13371337 +hf fmcos tidsetuid --uid 0102030405060708 ``` | Flag | Description | @@ -1055,13 +1058,13 @@ hf fmcos tid setuid --uid 0102030405060708 --- -### tid setauth +### tidsetauth Write the 8-byte internal authentication key and set the lock state. ``` -hf fmcos tid setauth --key 1122334455667788 -hf fmcos tid setauth --key 1122334455667788 --lock +hf fmcos tidsetauth --key 1122334455667788 +hf fmcos tidsetauth --key 1122334455667788 --lock ``` | Flag | Description | @@ -1076,12 +1079,12 @@ hf fmcos tid setauth --key 1122334455667788 --lock --- -### tid erase +### tiderase Erase the card's file system. Uses `CLA=0xE0` (not `0x80` as in standard FMCOS erase). ``` -hf fmcos tid erase +hf fmcos tiderase ``` | Flag | Description | @@ -1092,7 +1095,7 @@ hf fmcos tid erase --- -### tid provision +### tidprovision Full provisioning sequence in a single command. Chains all steps with the RF field held on and aborts with an error message if any step fails. @@ -1108,8 +1111,8 @@ Steps performed: 8. CREATE KEYFILE ``` -hf fmcos tid provision --uid 13371337 --key 1122334455667788 -hf fmcos tid provision --uid 13371337 --key 1122334455667788 --lock +hf fmcos tidprovision --uid 13371337 --key 1122334455667788 +hf fmcos tidprovision --uid 13371337 --key 1122334455667788 --lock ``` | Flag | Description | @@ -1119,19 +1122,19 @@ hf fmcos tid provision --uid 13371337 --key 1122334455667788 --lock | `--lock` | Lock the auth key permanently after writing | | `-k` / `--keep` | Keep field on after completion | -After `provision`, use `tid createdf` / `tid createbin` / `tid createrec` to build +After `tidprovision`, use `tidcreatedf` / `tidcreatebin` / `tidcreaterec` to build the file structure, then `hf fmcos write binary` / `hf fmcos write record` to populate data. --- -### tid createdf +### tidcreatedf CREATE a sub-DF using the TID APDU format. The standard `hf fmcos create dir` uses a different data layout (FID in P1/P2, leading `0x38` byte); this command uses the TID layout where P1=`0x01` and the FID is the first field in the data. ``` -hf fmcos tid createdf --id 3f01 --size 0f00 --sfi 96 --name 444446303133 +hf fmcos tidcreatedf --id 3f01 --size 0f00 --sfi 96 --name 444446303133 ``` | Flag | Description | @@ -1146,7 +1149,7 @@ hf fmcos tid createdf --id 3f01 --size 0f00 --sfi 96 --name 444446303133 --- -### tid createbin +### tidcreatebin CREATE a binary EF or KEYFILE using the TID APDU format. The standard `hf fmcos create file --type bin` uses a different layout (FID in P1/P2, 7-byte payload); this command uses the TID layout @@ -1156,9 +1159,9 @@ Use `--type keyfile` to create the fixed TID keyfile (subtype `1E`) in the curre Every sub-DF needs a keyfile before EFs can be created inside it. ``` -hf fmcos tid createbin --id 0001 --size 0100 --sfi 01 -hf fmcos tid createbin --id 0002 --size 0040 --sfi 02 --rperm 20 --wperm f0 -hf fmcos tid createbin --type keyfile +hf fmcos tidcreatebin --id 0001 --size 0100 --sfi 01 +hf fmcos tidcreatebin --id 0002 --size 0040 --sfi 02 --rperm 20 --wperm f0 +hf fmcos tidcreatebin --type keyfile ``` | Flag | Description | @@ -1177,15 +1180,15 @@ hf fmcos tid createbin --type keyfile --- -### tid createrec +### tidcreaterec CREATE a fixed-length record EF using the TID APDU format. The standard `hf fmcos create file --type fix` uses a different layout; this command uses the TID layout where P1=`0x02`, subtype=`0x01`, and count+reclen replace the size field. ``` -hf fmcos tid createrec --id 0003 --count 04 --reclen 08 --sfi 03 -hf fmcos tid createrec --id 0003 --count 04 --reclen 10 --sfi 03 --rperm 20 --wperm f0 +hf fmcos tidcreaterec --id 0003 --count 04 --reclen 08 --sfi 03 +hf fmcos tidcreaterec --id 0003 --count 04 --reclen 10 --sfi 03 --rperm 20 --wperm f0 ``` | Flag | Description | @@ -1217,7 +1220,7 @@ normally provided when you buy these TID cards. command finishes without `-k` the field drops and the card resets to MF context on the next activation. - Each sub-DF requires a TID-format keyfile created immediately after selecting it. Use - `hf fmcos tid createbin --type keyfile` - the standard `hf fmcos create keyfile` uses a + `hf fmcos tidcreatebin --type keyfile` - the standard `hf fmcos create keyfile` uses a different APDU layout and cannot create TID keyfiles. --- @@ -1231,11 +1234,11 @@ MF 3F00 "1PAY.SYS.DDF01" SFI 0x02 ``` ``` -hf fmcos tid provision --uid 13371337 --key 0001020304050607 -hf fmcos tid createdf --id 7572 --size 0200 --sfi 02 --name 390130990807 -k +hf fmcos tidprovision --uid 13371337 --key 0001020304050607 +hf fmcos tidcreatedf --id 7572 --size 0200 --sfi 02 --name 390130990807 -k hf fmcos select --id 7572 -k -hf fmcos tid createbin --type keyfile -k -hf fmcos tid createrec --id 0001 --count 06 --reclen 10 --sfi 03 -k +hf fmcos tidcreatebin --type keyfile -k +hf fmcos tidcreaterec --id 0001 --count 06 --reclen 10 --sfi 03 -k hf fmcos write record --rec 01 --fid 03 --data 018609DD110000000000010100000000 -k hf fmcos write record --rec 02 --fid 03 --data 00000000000000000000000000000000 -k hf fmcos write record --rec 03 --fid 03 --data 0000000000070A0D1707120000008800 -k @@ -1256,14 +1259,14 @@ MF 3F00 "1PAY.SYS.DDF01" SFI 0x02 ``` ``` -hf fmcos tid provision --uid 13371337 --key 0001020304050607 -hf fmcos tid createdf --id 1001 --size 0200 --sfi 02 --name A00000000386980701 -k +hf fmcos tidprovision --uid 13371337 --key 0001020304050607 +hf fmcos tidcreatedf --id 1001 --size 0200 --sfi 02 --name A00000000386980701 -k hf fmcos select --id 1001 -k -hf fmcos tid createbin --type keyfile -k -hf fmcos tid createbin --id 0018 --size 008C --sfi 18 -k +hf fmcos tidcreatebin --type keyfile -k +hf fmcos tidcreatebin --id 0018 --size 008C --sfi 18 -k hf fmcos select --id 0018 -k hf fmcos write binary --p1 00 --p2 00 --data 001FD921090700000001000023590000000000E0FFFFFF7F0700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -k -hf fmcos tid createbin --id 0019 --size 008C --sfi 19 -k +hf fmcos tidcreatebin --id 0019 --size 008C --sfi 19 -k hf fmcos select --id 0019 -k hf fmcos write binary --p1 00 --p2 00 --data 001FD921090700000001000023590000FFFFFFFFFFFF010000000000E0FFFFFF0F0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ``` @@ -1279,11 +1282,11 @@ MF 3F00 "1PAY.SYS.DDF01" SFI 0x02 ``` ``` -hf fmcos tid provision --uid 13371337 --key 0001020304050607 -hf fmcos tid createdf --id 4A54 --size 0300 --sfi 02 --name 6A696E00000000626FA5049F080102 -k +hf fmcos tidprovision --uid 13371337 --key 0001020304050607 +hf fmcos tidcreatedf --id 4A54 --size 0300 --sfi 02 --name 6A696E00000000626FA5049F080102 -k hf fmcos select --id 4A54 -k -hf fmcos tid createbin --type keyfile -k -hf fmcos tid createbin --id 4200 --size 0270 --sfi 01 -k +hf fmcos tidcreatebin --type keyfile -k +hf fmcos tidcreatebin --id 4200 --size 0270 --sfi 01 -k hf fmcos select --id 4200 -k hf fmcos write binary --p1 02 --p2 50 --data 530030FFFFFFFFFFFFFF3A2B0000000022012200002403081106000000007F00 ``` @@ -1304,24 +1307,24 @@ MF 3F00 "1PAY.SYS.DDF01" SFI 0x02 ``` ``` -hf fmcos tid provision --uid 13371337 --key 0001020304050607 -hf fmcos tid createdf --id 3F01 --size 0400 --sfi 02 --name 4444463031 -k +hf fmcos tidprovision --uid 13371337 --key 0001020304050607 +hf fmcos tidcreatedf --id 3F01 --size 0400 --sfi 02 --name 4444463031 -k hf fmcos select --id 3F01 -k -hf fmcos tid createbin --type keyfile -k -hf fmcos tid createbin --id 0001 --size 0001 --sfi 01 -k -hf fmcos tid createbin --id 0003 --size 001C --sfi 03 -k +hf fmcos tidcreatebin --type keyfile -k +hf fmcos tidcreatebin --id 0001 --size 0001 --sfi 01 -k +hf fmcos tidcreatebin --id 0003 --size 001C --sfi 03 -k hf fmcos select --id 0003 -k hf fmcos write binary --p1 00 --p2 00 --data FE937B922D7EDEF50000000000000000000000000000000000000000 -k -hf fmcos tid createbin --id 0004 --size 0078 --sfi 04 -k +hf fmcos tidcreatebin --id 0004 --size 0078 --sfi 04 -k hf fmcos select --id 0004 -k hf fmcos write binary --p1 00 --p2 00 --data 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -k -hf fmcos tid createbin --id 0005 --size 0024 --sfi 05 -k +hf fmcos tidcreatebin --id 0005 --size 0024 --sfi 05 -k hf fmcos select --id 0005 -k hf fmcos write binary --p1 00 --p2 00 --data 6403FE9C7434FCBC6E00FC9F2802F1448F06F3BA8CF6F0B98DF7F1B84D0BF60000000000 -k -hf fmcos tid createbin --id 0006 --size 0084 --sfi 06 -k +hf fmcos tidcreatebin --id 0006 --size 0084 --sfi 06 -k hf fmcos select --id 0006 -k hf fmcos write binary --p1 00 --p2 00 --data 616BEACE7705FCBD7604FDBC7107F2BB7006F3BA7309F0B97208F1B84D0B09B74C0AF7494F0DF4B5B10CF5B449F0EAB3480E14B24B31E84E4A30E9B0BA33EEAF44CDEFAE473513AD4634ED534137E2ABBF36E3AA43C6E0A942381EA85D3BE6585C3AE7A6A03DE4A55E000000000000000000000000000000000000000000000000000000 -k -hf fmcos tid createbin --id 0007 --size 0066 --sfi 07 -k +hf fmcos tidcreatebin --id 0007 --size 0066 --sfi 07 -k hf fmcos select --id 0007 -k hf fmcos write binary --p1 00 --p2 00 --data 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ``` @@ -1339,17 +1342,17 @@ MF 3F00 "1PAY.SYS.DDF01" SFI 0x02 ``` ``` -hf fmcos tid provision --uid 13371337 --key 0001020304050607 -hf fmcos tid createdf --id D0F1 --size 0100 --sfi 02 --name 584C313233 -k +hf fmcos tidprovision --uid 13371337 --key 0001020304050607 +hf fmcos tidcreatedf --id D0F1 --size 0100 --sfi 02 --name 584C313233 -k hf fmcos select --id D0F1 -k -hf fmcos tid createbin --type keyfile -k -hf fmcos tid createbin --id 0005 --size 0040 --sfi 05 -k +hf fmcos tidcreatebin --type keyfile -k +hf fmcos tidcreatebin --id 0005 --size 0040 --sfi 05 -k hf fmcos select --id 0005 -k hf fmcos write binary --p1 00 --p2 00 --data 0200FC20006200000000000000000000000000272E2C6A0000000000000000080000000000000000000000000000000000000000000000000000000000000000 -hf fmcos tid createdf --id D0F2 --size 0100 --sfi 02 --name 584C343536 -k +hf fmcos tidcreatedf --id D0F2 --size 0100 --sfi 02 --name 584C343536 -k hf fmcos select --id D0F2 -k -hf fmcos tid createbin --type keyfile -k -hf fmcos tid createbin --id 0005 --size 0040 --sfi 05 -k +hf fmcos tidcreatebin --type keyfile -k +hf fmcos tidcreatebin --id 0005 --size 0040 --sfi 05 -k hf fmcos select --id 0005 -k hf fmcos write binary --p1 00 --p2 00 --data 0200FC20006200000000000000000000000000272E2C6A0000000000000000080000000000000000000000000000000000000000000000000000000000000000 ``` @@ -1365,11 +1368,11 @@ MF 3F00 "1PAY.SYS.DDF01" SFI 0x02 ``` ``` -hf fmcos tid provision --uid 13371337 --key 0001020304050607 -hf fmcos tid createdf --id 3F01 --size 0200 --sfi 02 --name 4144463031 -k +hf fmcos tidprovision --uid 13371337 --key 0001020304050607 +hf fmcos tidcreatedf --id 3F01 --size 0200 --sfi 02 --name 4144463031 -k hf fmcos select --id 3F01 -k -hf fmcos tid createbin --type keyfile -k -hf fmcos tid createbin --id 0003 --size 00FA --sfi 03 -k +hf fmcos tidcreatebin --type keyfile -k +hf fmcos tidcreatebin --id 0003 --size 00FA --sfi 03 -k hf fmcos select --id 0003 -k hf fmcos write binary --p1 00 --p2 00 --data D15190D7E1E379732295C97D62A3172BE3BBA1D1B32CE32FED72CB3DCDB115E7DC2670978E241822F298C9951260FC55D54F9988C7FCAC5032F94281DFC39C973E570101764D5BBF367F84EBDA1B012ABD4568F35D5BC08BAFD76B988CA916C985692337FCF02C9FD2C8BDD583BC05EF55582C3921FA2CAFAE26308FBADE0598DB750EE1F0522D29EAB6FA5D0F3971F785692337FCF02C9FD2C8BDD583BC05EF55582C3921FA2CAFAE26308FBADE0598DB750EE1F0522D29EAB6FA5D0F3971F7EA545FC5B27B7F40DF6D0F71FCEE2A1BCED2DEDE67BB57B1C1F98C8CDA5259CC7BD83158086F215F5E1E0246EE0504760000000000 -k hf fmcos write binary --p1 00 --p2 F5 --data 0000000000 From 57234ba459a81c6a128750635b100c8c5bd10a1f Mon Sep 17 00:00:00 2001 From: klks Date: Mon, 25 May 2026 14:27:48 +0800 Subject: [PATCH 3/4] Merge down other commands, and update docs --- client/src/cmdhffmcos.c | 209 +++++++++----------------------- doc/fmcos.md | 154 ++++++++++++------------ doc/fmcos_example.md | 260 ++++++++++++++++++++-------------------- 3 files changed, 266 insertions(+), 357 deletions(-) diff --git a/client/src/cmdhffmcos.c b/client/src/cmdhffmcos.c index 646b3a244..6912c3043 100644 --- a/client/src/cmdhffmcos.c +++ b/client/src/cmdhffmcos.c @@ -713,7 +713,7 @@ static int CmdHFFmcosInfo(const char *Cmd) { } PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("hf fmcos select --id 3f00") "` to navigate the file system"); - PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("hf fmcos auth external --id 0 --key ffffffffffffffff") "` to authenticate"); + PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("hf fmcos authexternal --id 0 --key ffffffffffffffff") "` to authenticate"); PrintAndLogEx(NORMAL, ""); DropField(); @@ -829,17 +829,17 @@ static bool fmcos_parse_hex_int(const char *s, int *out) { } // --------------------------------------------------------------------------- -// hf fmcos auth external (Phase 2) +// hf fmcos authexternal (Phase 2) // --------------------------------------------------------------------------- static int CmdHFFmcosAuthExternal(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos auth external", + CLIParserInit(&ctx, "hf fmcos authexternal", "EXTERNAL AUTHENTICATE.\n" "Requests a challenge from the card, encrypts it with the provided\n" "DES (8-byte) or 3DES (16-byte) key, then sends EXTERNAL AUTHENTICATE.", - "hf fmcos auth external --id 0 --key ffffffffffffffff\n" - "hf fmcos auth external --id 1 --key 0102030405060708090a0b0c0d0e0f10"); + "hf fmcos authexternal --id 0 --key ffffffffffffffff\n" + "hf fmcos authexternal --id 1 --key 0102030405060708090a0b0c0d0e0f10"); void *argtable[] = { arg_param_begin, @@ -942,15 +942,15 @@ static int CmdHFFmcosAuthExternal(const char *Cmd) { } // --------------------------------------------------------------------------- -// hf fmcos auth internal (Phase 2) +// hf fmcos authinternal (Phase 2) // --------------------------------------------------------------------------- static int CmdHFFmcosAuthInternal(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos auth internal", + CLIParserInit(&ctx, "hf fmcos authinternal", "INTERNAL AUTHENTICATE.\n" "Sends a challenge to the card and the card proves it knows the key.", - "hf fmcos auth internal --p1 00 --p2 00 --data 0102030405060708"); + "hf fmcos authinternal --p1 00 --p2 00 --data 0102030405060708"); void *argtable[] = { arg_param_begin, @@ -1030,28 +1030,9 @@ static int CmdHFFmcosAuthInternal(const char *Cmd) { } // --------------------------------------------------------------------------- -// hf fmcos auth - sub-dispatcher (Phase 2) +// hf fmcos authexternal / authinternal (Phase 2) // --------------------------------------------------------------------------- -static int CmdHFFmcosAuthHelp(const char *Cmd); - -static command_t AuthCommandTable[] = { - {"help", CmdHFFmcosAuthHelp, AlwaysAvailable, "This help"}, - {"external", CmdHFFmcosAuthExternal, IfPm3Iso14443a, "EXTERNAL AUTHENTICATE using DES/3DES key"}, - {"internal", CmdHFFmcosAuthInternal, IfPm3Iso14443a, "INTERNAL AUTHENTICATE (card proves key knowledge)"}, - {NULL, NULL, NULL, NULL} -}; - -static int CmdHFFmcosAuthHelp(const char *Cmd) { - (void)Cmd; - CmdsHelp(AuthCommandTable); - return PM3_SUCCESS; -} - -static int CmdHFFmcosAuth(const char *Cmd) { - clearCommandBuffer(); - return CmdsParse(AuthCommandTable, Cmd); -} // --------------------------------------------------------------------------- // Phase 4 - File management @@ -1296,9 +1277,9 @@ static int CmdHFFmcosErase(const char *Cmd) { static int CmdHFFmcosCreateDir(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos create dir", + CLIParserInit(&ctx, "hf fmcos createdir", "CREATE DIRECTORY (DF) inside the currently selected directory", - "hf fmcos create dir --id 3F01 --space 200 --cperm F0 --eperm F0 --appid 95 --name 77616C6C6574546573740A"); + "hf fmcos createdir --id 3F01 --space 200 --cperm F0 --eperm F0 --appid 95 --name 77616C6C6574546573740A"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "id", "<4hex>", "2-byte file ID"), @@ -1428,15 +1409,15 @@ static const CLIParserOption g_fmcos_baltype_opts[] = { static int CmdHFFmcosCreateFile(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos create file", + CLIParserInit(&ctx, "hf fmcos createfile", "CREATE EF in the current DF\n" "Types: bin(0x28) fix(0x2A) var(0x2C) loop(0x2E) wallet(0x2F)\n" "Prot: none mac(0x80) enc(0xC0) -- ORed into the type byte\n" "For wallet/passbook type: --rperm=usage rights, --wperm ignored (EDEP write always 0x00),\n" "--access=loop file link (low byte of the linked loop EF's file ID)", - "hf fmcos create file --id 0101 --type bin --size 32 --rperm FF --wperm FF --access 00\n" - "hf fmcos create file --id 0018 --type loop --size 0517 --rperm F0 --wperm EF --access FF\n" - "hf fmcos create file --id 0002 --type wallet --size 0208 --rperm F0 --wperm 00 --access 18"); + "hf fmcos createfile --id 0101 --type bin --size 32 --rperm FF --wperm FF --access 00\n" + "hf fmcos createfile --id 0018 --type loop --size 0517 --rperm F0 --wperm EF --access FF\n" + "hf fmcos createfile --id 0002 --type wallet --size 0208 --rperm F0 --wperm 00 --access 18"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "id", "<4hex>", "2-byte file ID"), @@ -1550,9 +1531,9 @@ static int CmdHFFmcosCreateFile(const char *Cmd) { static int CmdHFFmcosCreateKeyfile(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos create keyfile", + CLIParserInit(&ctx, "hf fmcos createkeyfile", "CREATE KEYFILE in the current DF", - "hf fmcos create keyfile --id 0000 --space 200 --dfsid 95 --perm F0"); + "hf fmcos createkeyfile --id 0000 --space 200 --dfsid 95 --perm F0"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "id", "<4hex>", "2-byte file ID (usually 0000)"), @@ -1640,39 +1621,18 @@ static int CmdHFFmcosCreateKeyfile(const char *Cmd) { return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } -static int CmdHFFmcosCreateHelp(const char *Cmd); - -static command_t CreateCommandTable[] = { - {"help", CmdHFFmcosCreateHelp, AlwaysAvailable, "This help"}, - {"dir", CmdHFFmcosCreateDir, IfPm3Iso14443a, "CREATE DIRECTORY (DF)"}, - {"file", CmdHFFmcosCreateFile, IfPm3Iso14443a, "CREATE EF (binary / fixed / variable / loop / wallet)"}, - {"keyfile", CmdHFFmcosCreateKeyfile, IfPm3Iso14443a, "CREATE KEYFILE"}, - {NULL, NULL, NULL, NULL} -}; - -static int CmdHFFmcosCreateHelp(const char *Cmd) { - (void)Cmd; - CmdsHelp(CreateCommandTable); - return PM3_SUCCESS; -} - -static int CmdHFFmcosCreate(const char *Cmd) { - clearCommandBuffer(); - return CmdsParse(CreateCommandTable, Cmd); -} - // --------------------------------------------------------------------------- // Phase 5 - Data access // --------------------------------------------------------------------------- static int CmdHFFmcosReadBinary(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos read binary", + CLIParserInit(&ctx, "hf fmcos readbinary", "READ BINARY from the current transparent EF\n" "p1/p2 encode the file offset (p1=offset_hi, p2=offset_lo).\n" "Protection: none(def) mac enc", - "hf fmcos read binary --p1 00 --p2 00 --len 16\n" - "hf fmcos read binary --p1 00 --p2 00 --len 16 --prot mac --key aabbccddeeff0011"); + "hf fmcos readbinary --p1 00 --p2 00 --len 16\n" + "hf fmcos readbinary --p1 00 --p2 00 --len 16 --prot mac --key aabbccddeeff0011"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "p1", "", "P1 byte (offset high)"), @@ -1738,13 +1698,13 @@ static int CmdHFFmcosReadBinary(const char *Cmd) { static int CmdHFFmcosReadRecord(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos read record", + CLIParserInit(&ctx, "hf fmcos readrecord", "READ RECORD from the current record-based EF\n" "Protection: none(def) mac enc\n" "Use --tlv for variable-length (VAR) files: requests 2 extra bytes and strips the 00[len] TLV prefix from the response.", - "hf fmcos read record --rec 01 --fid 01 --len 20\n" - "hf fmcos read record --rec 01 --fid 06 --len 16 --tlv\n" - "hf fmcos read record --rec 01 --fid 01 --len 20 --prot mac --key aabbccddeeff0011"); + "hf fmcos readrecord --rec 01 --fid 01 --len 20\n" + "hf fmcos readrecord --rec 01 --fid 06 --len 16 --tlv\n" + "hf fmcos readrecord --rec 01 --fid 01 --len 20 --prot mac --key aabbccddeeff0011"); void *argtable[] = { arg_param_begin, arg_int1(NULL, "rec", "", "record number (1-based)"), @@ -1825,33 +1785,14 @@ static int CmdHFFmcosReadRecord(const char *Cmd) { return res; } -static int CmdHFFmcosReadHelp(const char *Cmd); - -static command_t ReadCommandTable[] = { - {"help", CmdHFFmcosReadHelp, AlwaysAvailable, "This help"}, - {"binary", CmdHFFmcosReadBinary, IfPm3Iso14443a, "READ BINARY from transparent EF"}, - {"record", CmdHFFmcosReadRecord, IfPm3Iso14443a, "READ RECORD from record-based EF"}, - {NULL, NULL, NULL, NULL} -}; - -static int CmdHFFmcosReadHelp(const char *Cmd) { - (void)Cmd; - CmdsHelp(ReadCommandTable); - return PM3_SUCCESS; -} - -static int CmdHFFmcosRead(const char *Cmd) { - clearCommandBuffer(); - return CmdsParse(ReadCommandTable, Cmd); -} static int CmdHFFmcosWriteBinary(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos write binary", + CLIParserInit(&ctx, "hf fmcos writebinary", "UPDATE BINARY in the current transparent EF\n" "Protection: none(def) mac enc", - "hf fmcos write binary --p1 00 --p2 00 --data 0102030405060708\n" - "hf fmcos write binary --p1 00 --p2 00 --data 01020304 --prot mac --key aabbccddeeff0011"); + "hf fmcos writebinary --p1 00 --p2 00 --data 0102030405060708\n" + "hf fmcos writebinary --p1 00 --p2 00 --data 01020304 --prot mac --key aabbccddeeff0011"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "p1", "", "P1 byte (offset high)"), @@ -1934,13 +1875,13 @@ static int CmdHFFmcosWriteBinary(const char *Cmd) { static int CmdHFFmcosWriteRecord(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos write record", + CLIParserInit(&ctx, "hf fmcos writerecord", "UPDATE RECORD in the current record-based EF\n" "Protection: none(def) mac enc\n" "Use --tlv for variable-length (VAR) files: wraps data as 00[len][data] before sending.", - "hf fmcos write record --rec 01 --fid 01 --data 0102030405060708\n" - "hf fmcos write record --rec 01 --fid 06 --data 0102030405060708 --tlv\n" - "hf fmcos write record --rec 01 --fid 01 --data 01020304 --prot mac --key aabbccddeeff0011"); + "hf fmcos writerecord --rec 01 --fid 01 --data 0102030405060708\n" + "hf fmcos writerecord --rec 01 --fid 06 --data 0102030405060708 --tlv\n" + "hf fmcos writerecord --rec 01 --fid 01 --data 01020304 --prot mac --key aabbccddeeff0011"); void *argtable[] = { arg_param_begin, arg_int1(NULL, "rec", "", "record number (1-based)"), @@ -2043,25 +1984,6 @@ static int CmdHFFmcosWriteRecord(const char *Cmd) { return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } -static int CmdHFFmcosWriteHelp(const char *Cmd); - -static command_t WriteCommandTable[] = { - {"help", CmdHFFmcosWriteHelp, AlwaysAvailable, "This help"}, - {"binary", CmdHFFmcosWriteBinary, IfPm3Iso14443a, "UPDATE BINARY in transparent EF"}, - {"record", CmdHFFmcosWriteRecord, IfPm3Iso14443a, "UPDATE RECORD in record-based EF"}, - {NULL, NULL, NULL, NULL} -}; - -static int CmdHFFmcosWriteHelp(const char *Cmd) { - (void)Cmd; - CmdsHelp(WriteCommandTable); - return PM3_SUCCESS; -} - -static int CmdHFFmcosWrite(const char *Cmd) { - clearCommandBuffer(); - return CmdsParse(WriteCommandTable, Cmd); -} static int CmdHFFmcosAppend(const char *Cmd) { CLIParserContext *ctx; @@ -2284,7 +2206,7 @@ static int CmdHFFmcosWriteKey(const char *Cmd) { data[data_len++] = change_b[0]; data[data_len++] = version_b[0]; data[data_len++] = algo_b[0]; - // Group B-extauth: extauth + // Group B-extauth: extauth } else if (ktype == 0x39) { if (change_len != 1 || followup_len != 1 || errcnt_len != 1) { PrintAndLogEx(ERR, "extauth key type needs --change --followup --errcount"); @@ -2293,7 +2215,7 @@ static int CmdHFFmcosWriteKey(const char *Cmd) { data[data_len++] = change_b[0]; data[data_len++] = followup_b[0]; data[data_len++] = errcnt_b[0]; - // Group B-pin: pin + // Group B-pin: pin } else if (ktype == 0x3A) { if (followup_len != 1 || errcnt_len != 1) { PrintAndLogEx(ERR, "pin key type needs --followup --errcount"); @@ -2302,7 +2224,7 @@ static int CmdHFFmcosWriteKey(const char *Cmd) { data[data_len++] = 0xEF; data[data_len++] = followup_b[0]; data[data_len++] = errcnt_b[0]; - // Group C: lineprotect unlockpin changepin + // Group C: lineprotect unlockpin changepin } else if (ktype == 0x36 || ktype == 0x37 || ktype == 0x38) { if (change_len != 1 || errcnt_len != 1) { PrintAndLogEx(ERR, "This key type needs --change --errcount"); @@ -2361,10 +2283,10 @@ static int CmdHFFmcosWriteKey(const char *Cmd) { // VERIFY PIN (INS=20): present the PIN to the card. static int CmdHFFmcosPinVerify(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos pin verify", + CLIParserInit(&ctx, "hf fmcos pinverify", "VERIFY PIN (INS 20) - present PIN to the card", - "hf fmcos pin verify --id 00 --pin 123456\n" - "hf fmcos pin verify --id 00 --pin 1234"); + "hf fmcos pinverify --id 00 --pin 123456\n" + "hf fmcos pinverify --id 00 --pin 1234"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "id", "", "key slot (P2), 1 byte"), @@ -2439,10 +2361,10 @@ static int CmdHFFmcosPinVerify(const char *Cmd) { // CHANGE PIN (INS=5E P1=01): present old PIN and new PIN. static int CmdHFFmcosPinChange(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos pin change", + CLIParserInit(&ctx, "hf fmcos pinchange", "CHANGE PIN (INS 5E P1=01) - change PIN with old PIN authorization\n" "Data sent: old_pin + 0xFF + new_pin", - "hf fmcos pin change --id 00 --old 123456 --new 13371337"); + "hf fmcos pinchange --id 00 --old 123456 --new 13371337"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "id", "", "key slot (P2), 1 byte"), @@ -2532,10 +2454,10 @@ static int CmdHFFmcosPinChange(const char *Cmd) { // MAC = DES-MAC(new_pin, XOR(key_left, key_right)) static int CmdHFFmcosPinReset(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos pin reset", + CLIParserInit(&ctx, "hf fmcos pinreset", "RESET PIN (INS 5E P1=00) - set new PIN using change-PIN key\n" "Appends DES-MAC(new_pin, key_left XOR key_right) to the data.", - "hf fmcos pin reset --id 00 --pin 123456 --key aabbccddeeff001122334455667788aa"); + "hf fmcos pinreset --id 00 --pin 123456 --key aabbccddeeff001122334455667788aa"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "id", "", "key slot (P2), 1 byte"), @@ -2635,10 +2557,10 @@ static int CmdHFFmcosPinReset(const char *Cmd) { // Data = encrypt([len|new_pin], unlock_key) + packet_mac(cla, ins, p1, p2, enc_data, chal_iv, unlock_key) static int CmdHFFmcosPinUnblock(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf fmcos pin unblock", + CLIParserInit(&ctx, "hf fmcos pinunblock", "UNBLOCK PIN (INS 24) - unblock a locked PIN using the unlock-PIN key\n" "Data = encrypt([len|new_pin], unlock_key) + packet_MAC", - "hf fmcos pin unblock --id 00 --pin 123456 --key aabbccddeeff001122334455667788aa"); + "hf fmcos pinunblock --id 00 --pin 123456 --key aabbccddeeff001122334455667788aa"); void *argtable[] = { arg_param_begin, arg_str1(NULL, "id", "", "PIN key slot (P1), 1 byte"), @@ -2744,27 +2666,6 @@ static int CmdHFFmcosPinUnblock(const char *Cmd) { return (sw1 == 0x90 && sw2 == 0x00) ? PM3_SUCCESS : PM3_ESOFT; } -static int CmdHFFmcosPinHelp(const char *Cmd); - -static command_t PinCommandTable[] = { - {"help", CmdHFFmcosPinHelp, AlwaysAvailable, "This help"}, - {"verify", CmdHFFmcosPinVerify, IfPm3Iso14443a, "VERIFY PIN (present PIN to card)"}, - {"change", CmdHFFmcosPinChange, IfPm3Iso14443a, "CHANGE PIN (old + new, requires old PIN)"}, - {"reset", CmdHFFmcosPinReset, IfPm3Iso14443a, "RESET PIN (new PIN + change-PIN key MAC)"}, - {"unblock", CmdHFFmcosPinUnblock, IfPm3Iso14443a, "UNBLOCK PIN (encrypted new PIN + MAC)"}, - {NULL, NULL, NULL, NULL} -}; - -static int CmdHFFmcosPinHelp(const char *Cmd) { - (void)Cmd; - CmdsHelp(PinCommandTable); - return PM3_SUCCESS; -} - -static int CmdHFFmcosPin(const char *Cmd) { - clearCommandBuffer(); - return CmdsParse(PinCommandTable, Cmd); -} // --------------------------------------------------------------------------- // Phase 7 helpers @@ -4250,7 +4151,7 @@ static int CmdHFFmcosTidCreateDF(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf fmcos tidcreatedf", "CREATE sub-DF using TID format (P1=01, FID in data).\n" - "Note: TID CREATE DF has a different layout from standard 'hf fmcos create dir'.", + "Note: TID CREATE DF has a different layout from standard 'hf fmcos createdir'.", "hf fmcos tidcreatedf --id 3f01 --size 0f00 --sfi 96 --name 44444630 31"); void *argtable[] = { arg_param_begin, @@ -4338,7 +4239,7 @@ static int CmdHFFmcosTidCreateBin(const char *Cmd) { CLIParserInit(&ctx, "hf fmcos tidcreatebin", "CREATE binary EF or KEYFILE using TID format (P1=02, FID in data, fixed Lc=11).\n" "Use --type keyfile to create the fixed TID keyfile in the currently selected DF.\n" - "Note: TID CREATE EF has a different layout from standard 'hf fmcos create file'.", + "Note: TID CREATE EF has a different layout from standard 'hf fmcos createfile'.", "hf fmcos tidcreatebin --id 0001 --size 0100 --sfi 01\n" "hf fmcos tidcreatebin --id 0002 --size 0040 --sfi 02 --rperm 20 --wperm f0\n" "hf fmcos tidcreatebin --type keyfile"); @@ -4450,7 +4351,7 @@ static int CmdHFFmcosTidCreateRec(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf fmcos tidcreaterec", "CREATE fixed-length record EF using TID format (P1=02, subtype=01, FID in data, fixed Lc=11).\n" - "Note: TID CREATE EF has a different layout from standard 'hf fmcos create file'.", + "Note: TID CREATE EF has a different layout from standard 'hf fmcos createfile'.", "hf fmcos tidcreaterec --id 0003 --count 04 --reclen 08 --sfi 03\n" "hf fmcos tidcreaterec --id 0003 --count 04 --reclen 10 --sfi 03 --rperm 20 --wperm f0"); void *argtable[] = { @@ -4556,16 +4457,24 @@ static command_t CommandTable[] = { {"select", CmdHFFmcosSelect, IfPm3Iso14443a, "SELECT FILE by 2-byte ID or AID name"}, {"--------", CmdHelp, AlwaysAvailable, "--------- " _CYAN_("File management") " ----------"}, {"erase", CmdHFFmcosErase, IfPm3Iso14443a, "ERASE DF contents"}, - {"create", CmdHFFmcosCreate, IfPm3Iso14443a, "{ Create directory / EF / keyfile... }"}, + {"createdir", CmdHFFmcosCreateDir, IfPm3Iso14443a, "CREATE DIRECTORY (DF)"}, + {"createfile", CmdHFFmcosCreateFile, IfPm3Iso14443a, "CREATE EF (binary / fixed / variable / loop / wallet)"}, + {"createkeyfile", CmdHFFmcosCreateKeyfile, IfPm3Iso14443a, "CREATE KEYFILE"}, {"--------", CmdHelp, AlwaysAvailable, "--------- " _CYAN_("Data access") " --------------"}, - {"read", CmdHFFmcosRead, IfPm3Iso14443a, "{ Read binary / record... }"}, - {"write", CmdHFFmcosWrite, IfPm3Iso14443a, "{ Write binary / record... }"}, + {"readbinary", CmdHFFmcosReadBinary, IfPm3Iso14443a, "READ BINARY from transparent EF"}, + {"readrecord", CmdHFFmcosReadRecord, IfPm3Iso14443a, "READ RECORD from record-based EF"}, + {"writebinary", CmdHFFmcosWriteBinary, IfPm3Iso14443a, "UPDATE BINARY in transparent EF"}, + {"writerecord", CmdHFFmcosWriteRecord, IfPm3Iso14443a, "UPDATE RECORD in record-based EF"}, {"append", CmdHFFmcosAppend, IfPm3Iso14443a, "APPEND RECORD to cyclic / linear EF"}, {"--------", CmdHelp, AlwaysAvailable, "--------- " _CYAN_("Authentication") " -----------"}, - {"auth", CmdHFFmcosAuth, IfPm3Iso14443a, "{ External / internal authenticate... }"}, + {"authexternal", CmdHFFmcosAuthExternal, IfPm3Iso14443a, "EXTERNAL AUTHENTICATE using DES/3DES key"}, + {"authinternal", CmdHFFmcosAuthInternal, IfPm3Iso14443a, "INTERNAL AUTHENTICATE (card proves key knowledge)"}, {"key", CmdHFFmcosWriteKey, IfPm3Iso14443a, "WRITE KEY to keyfile"}, {"--------", CmdHelp, AlwaysAvailable, "--------- " _CYAN_("PIN management") " -----------"}, - {"pin", CmdHFFmcosPin, IfPm3Iso14443a, "{ Verify / change / reset / unblock PIN }"}, + {"pinverify", CmdHFFmcosPinVerify, IfPm3Iso14443a, "VERIFY PIN (present PIN to card)"}, + {"pinchange", CmdHFFmcosPinChange, IfPm3Iso14443a, "CHANGE PIN (old + new, requires old PIN)"}, + {"pinreset", CmdHFFmcosPinReset, IfPm3Iso14443a, "RESET PIN (new PIN + change-PIN key MAC)"}, + {"pinunblock", CmdHFFmcosPinUnblock, IfPm3Iso14443a, "UNBLOCK PIN (encrypted new PIN + MAC)"}, {"--------", CmdHelp, AlwaysAvailable, "--------- " _CYAN_("Financial") " ----------------"}, {"balance", CmdHFFmcosBalance, IfPm3Iso14443a, "GET BALANCE (wallet or passbook)"}, {"credit", CmdHFFmcosCredit, IfPm3Iso14443a, "ADD CREDIT to wallet or passbook"}, diff --git a/doc/fmcos.md b/doc/fmcos.md index 1eb2047d6..0316f4678 100644 --- a/doc/fmcos.md +++ b/doc/fmcos.md @@ -15,24 +15,24 @@ All commands in this family are reachable via `hf fmcos `. - [select](#select) - [File Management](#file-management) - [erase](#erase) - - [create dir](#create-dir) - - [create file](#create-file) - - [create keyfile](#create-keyfile) + - [createdir](#createdir) + - [createfile](#createfile) + - [createkeyfile](#createkeyfile) - [Data Access](#data-access) - - [read binary](#read-binary) - - [read record](#read-record) - - [write binary](#write-binary) - - [write record](#write-record) + - [readbinary](#readbinary) + - [readrecord](#readrecord) + - [writebinary](#writebinary) + - [writerecord](#writerecord) - [append](#append) - [key (write key)](#key-write-key) - [Authentication](#authentication) - - [auth external](#auth-external) - - [auth internal](#auth-internal) + - [authexternal](#authexternal) + - [authinternal](#authinternal) - [PIN Management](#pin-management) - - [pin verify](#pin-verify) - - [pin change](#pin-change) - - [pin reset](#pin-reset) - - [pin unblock](#pin-unblock) + - [pinverify](#pinverify) + - [pinchange](#pinchange) + - [pinreset](#pinreset) + - [pinunblock](#pinunblock) - [Financial Operations](#financial-operations) - [balance](#balance) - [credit](#credit) @@ -102,12 +102,12 @@ hf fmcos select --id 3f00 hf fmcos erase ``` -### create dir +### createdir CREATE DF (directory / application directory). ``` -hf fmcos create dir --id 3f01 --space 1500 --cperm f0 --eperm f0 --appid 95 --name 77616C6C657454657374 +hf fmcos createdir --id 3f01 --space 1500 --cperm f0 --eperm f0 --appid 95 --name 77616C6C657454657374 ``` | Flag | Description | @@ -119,22 +119,22 @@ hf fmcos create dir --id 3f01 --space 1500 --cperm f0 --eperm f0 --appid 95 --na | `--appid ` | 1-byte application SID / AID tag | | `--name ` | Optional DF name bytes as hex (up to 16 bytes, enables select-by-name) | -### create file +### createfile CREATE EF (elementary file) in the currently selected DF. ``` # Unprotected binary file -hf fmcos create file --id 0002 --type bin --size 50 --rperm f0 --wperm f0 --access ff +hf fmcos createfile --id 0002 --type bin --size 50 --rperm f0 --wperm f0 --access ff # Variable-length record file with MAC-only line protection -hf fmcos create file --id 0006 --type var --size 50 --rperm f0 --wperm f0 --access 7f --prot mac +hf fmcos createfile --id 0006 --type var --size 50 --rperm f0 --wperm f0 --access 7f --prot mac # Loop (cyclic) file with MAC+encryption -hf fmcos create file --id 000a --type loop --size 210 --rperm f0 --wperm f0 --access 7f --prot enc +hf fmcos createfile --id 000a --type loop --size 210 --rperm f0 --wperm f0 --access 7f --prot enc # Wallet/passbook balance file (EDEP) linked to loop file 0x0018 -hf fmcos create file --id 0002 --type wallet --size 0208 --rperm f0 --wperm 00 --access 18 +hf fmcos createfile --id 0002 --type wallet --size 0208 --rperm f0 --wperm 00 --access 18 ``` | Flag | Description | @@ -231,13 +231,13 @@ have its low nibble set to `4` (`0x04` plain, `0x84` for ISO-secure) when line p | `loop` | 0x2E | Cyclic (loop) file -- used for transaction logs | | `wallet` | 0x2F | E-purse wallet / passbook balance file | -### create keyfile +### createkeyfile CREATE KEYFILE in the currently selected DF. A DF must have a keyfile before any keys can be written to it. ``` -hf fmcos create keyfile --id 0000 --space 200 --dfsid 95 --perm f0 +hf fmcos createkeyfile --id 0000 --space 200 --dfsid 95 --perm f0 ``` | Flag | Description | @@ -278,19 +278,19 @@ Common values: ## Data Access -### read binary +### readbinary READ BINARY from the currently selected transparent (bin) EF. ``` # Plain read -hf fmcos read binary --p1 00 --p2 00 --len 10 +hf fmcos readbinary --p1 00 --p2 00 --len 10 # With MAC line-protection (verifies response MAC) -hf fmcos read binary --p1 00 --p2 00 --len 10 --prot mac --key 36363636363636363636363636363636 +hf fmcos readbinary --p1 00 --p2 00 --len 10 --prot mac --key 36363636363636363636363636363636 # With MAC+encryption (decrypts response) -hf fmcos read binary --p1 00 --p2 00 --len 10 --prot enc --key 36363636363636363636363636363636 +hf fmcos readbinary --p1 00 --p2 00 --len 10 --prot enc --key 36363636363636363636363636363636 ``` | Flag | Description | @@ -301,19 +301,19 @@ hf fmcos read binary --p1 00 --p2 00 --len 10 --prot enc --key 36363636363636363 | `--prot ` | `none`, `mac`, or `enc` | | `--key ` | Line-protection key (8 or 16 bytes, required when `--prot` is mac/enc) | -### read record +### readrecord READ RECORD from the currently selected record or cyclic EF. ``` # Read record 1 from var file 0x06 (plain) -hf fmcos read record --rec 01 --fid 06 --len 10 +hf fmcos readrecord --rec 01 --fid 06 --len 10 # Read with MAC verification -hf fmcos read record --rec 01 --fid 07 --len 10 --prot mac --key 36363636363636363636363636363636 +hf fmcos readrecord --rec 01 --fid 07 --len 10 --prot mac --key 36363636363636363636363636363636 # Read with decryption -hf fmcos read record --rec 01 --fid 08 --len 10 --prot enc --key 36363636363636363636363636363636 +hf fmcos readrecord --rec 01 --fid 08 --len 10 --prot enc --key 36363636363636363636363636363636 ``` | Flag | Description | @@ -324,20 +324,20 @@ hf fmcos read record --rec 01 --fid 08 --len 10 --prot enc --key 363636363636363 | `--prot ` | `none`, `mac`, or `enc` | | `--key ` | Line-protection key when prot is mac/enc | -### write binary +### writebinary UPDATE BINARY -- write data to the currently selected transparent EF. ``` # Plain write -hf fmcos write binary --p1 00 --p2 00 --data 11121314151617181910 +hf fmcos writebinary --p1 00 --p2 00 --data 11121314151617181910 # Write with MAC -hf fmcos write binary --p1 00 --p2 00 --data 21222324252627282920 \ +hf fmcos writebinary --p1 00 --p2 00 --data 21222324252627282920 \ --prot mac --key 36363636363636363636363636363636 # Write with MAC+encryption (data is encrypted before sending) -hf fmcos write binary --p1 00 --p2 00 --data 31323334353637383930 \ +hf fmcos writebinary --p1 00 --p2 00 --data 31323334353637383930 \ --prot enc --key 36363636363636363636363636363636 ``` @@ -349,20 +349,20 @@ hf fmcos write binary --p1 00 --p2 00 --data 31323334353637383930 \ | `--prot ` | `none`, `mac`, or `enc` | | `--key ` | Line-protection key | -### write record +### writerecord UPDATE RECORD -- write a record into the currently selected EF. ``` # Plain record write (P1=record number, P2=file-id<<3|04) -hf fmcos write record --rec 01 --fid 06 --data 5152535455565758595a +hf fmcos writerecord --rec 01 --fid 06 --data 5152535455565758595a # With MAC -hf fmcos write record --rec 01 --fid 07 --data 6162636465666768696a \ +hf fmcos writerecord --rec 01 --fid 07 --data 6162636465666768696a \ --prot mac --key 36363636363636363636363636363636 # With MAC+encryption -hf fmcos write record --rec 01 --fid 08 --data 7172737475767778797a \ +hf fmcos writerecord --rec 01 --fid 08 --data 7172737475767778797a \ --prot enc --key 36363636363636363636363636363636 ``` @@ -477,13 +477,13 @@ hf fmcos key --op 01 --id 00 --type changepin \ ## Authentication -### auth external +### authexternal EXTERNAL AUTHENTICATE -- authenticate the reader to the card. The card issues a challenge, the reader encrypts it with the external-auth key, and sends the response back. ``` -hf fmcos auth external --id 00 --key f49dc1ba1b4deb5264718bc559106c0d +hf fmcos authexternal --id 00 --key f49dc1ba1b4deb5264718bc559106c0d ``` | Flag | Description | @@ -491,14 +491,14 @@ hf fmcos auth external --id 00 --key f49dc1ba1b4deb5264718bc559106c0d | `--id ` | Key slot ID | | `--key ` | External-auth key (8 or 16 bytes) | -### auth internal +### authinternal INTERNAL AUTHENTICATE -- authenticate the card to the reader. The reader sends an 8-byte challenge (`--data`); the card responds with a DES-encrypted value that the reader verifies offline. ``` -hf fmcos auth internal --p1 00 --p2 00 --data 0102030405060708 +hf fmcos authinternal --p1 00 --p2 00 --data 0102030405060708 ``` | Flag | Description | @@ -511,13 +511,13 @@ hf fmcos auth internal --p1 00 --p2 00 --data 0102030405060708 ## PIN Management -### pin verify +### pinverify VERIFY PIN -- present the PIN code to the card to unlock PIN-gated operations. PIN is 2-6 raw bytes. ``` -hf fmcos pin verify --id 00 --pin 123456 +hf fmcos pinverify --id 00 --pin 123456 ``` | Flag | Description | @@ -525,12 +525,12 @@ hf fmcos pin verify --id 00 --pin 123456 | `--id ` | PIN key slot ID | | `--pin ` | PIN bytes (2-6 bytes) | -### pin change +### pinchange CHANGE PIN -- change the PIN using the current (old) PIN for authorization. ``` -hf fmcos pin change --id 00 --old 123456 --new 13371337 +hf fmcos pinchange --id 00 --old 123456 --new 13371337 ``` | Flag | Description | @@ -539,13 +539,13 @@ hf fmcos pin change --id 00 --old 123456 --new 13371337 | `--old ` | Current PIN (2-6 bytes) | | `--new ` | New PIN (2-6 bytes) | -### pin reset +### pinreset RESET PIN -- set a new PIN using the change-PIN key MAC for authorization (no old PIN needed). The command computes a MAC over the new PIN using the change-PIN key and sends it to the card. ``` -hf fmcos pin reset --id 00 --pin 13371337 \ +hf fmcos pinreset --id 00 --pin 13371337 \ --key fb487a6d1b7cbf1bf84c666b8338376e ``` @@ -555,13 +555,13 @@ hf fmcos pin reset --id 00 --pin 13371337 \ | `--pin ` | New PIN (2-6 bytes) | | `--key ` | Change-PIN key (16 bytes); MAC = DES-MAC(new_pin, XOR_halves(key)) | -### pin unblock +### pinunblock UNBLOCK PIN -- clear the PIN blocked state and set a new PIN. The new PIN is encrypted with the unlock-PIN key and a GET CHALLENGE IV. ``` -hf fmcos pin unblock --id 00 --pin 123456 \ +hf fmcos pinunblock --id 00 --pin 123456 \ --key d8f60fa2d791f3a658d27c054582430e ``` @@ -974,7 +974,7 @@ over `CLA|INS|P1|P2|Lc[|data]` with a GET CHALLENGE response as the CBC IV. ## Access Rights Byte -The access-rights byte passed to `create file` controls whether line protection is needed +The access-rights byte passed to `createfile` controls whether line protection is needed and which key slot guards read / write access. ``` @@ -1001,7 +1001,7 @@ allows custom UID. These commands below allow a TID tag to be provisioned - UID, auth key, and file system, these commands are not the same as the standard fmcos commands. These cards can often be found on taobao by searching for "CPU TID card". -When hf fmcos auth external is called with any key, the card will always return +When hf fmcos authexternal is called with any key, the card will always return [+] SW: 9000 - Success [+] External authentication successful @@ -1123,13 +1123,13 @@ hf fmcos tidprovision --uid 13371337 --key 1122334455667788 --lock | `-k` / `--keep` | Keep field on after completion | After `tidprovision`, use `tidcreatedf` / `tidcreatebin` / `tidcreaterec` to build -the file structure, then `hf fmcos write binary` / `hf fmcos write record` to populate data. +the file structure, then `hf fmcos writebinary` / `hf fmcos writerecord` to populate data. --- ### tidcreatedf -CREATE a sub-DF using the TID APDU format. The standard `hf fmcos create dir` uses a +CREATE a sub-DF using the TID APDU format. The standard `hf fmcos createdir` uses a different data layout (FID in P1/P2, leading `0x38` byte); this command uses the TID layout where P1=`0x01` and the FID is the first field in the data. @@ -1151,7 +1151,7 @@ hf fmcos tidcreatedf --id 3f01 --size 0f00 --sfi 96 --name 444446303133 ### tidcreatebin -CREATE a binary EF or KEYFILE using the TID APDU format. The standard `hf fmcos create file --type bin` +CREATE a binary EF or KEYFILE using the TID APDU format. The standard `hf fmcos createfile --type bin` uses a different layout (FID in P1/P2, 7-byte payload); this command uses the TID layout where P1=`0x02` and the FID is in the data with a fixed 11-byte payload. @@ -1182,7 +1182,7 @@ hf fmcos tidcreatebin --type keyfile ### tidcreaterec -CREATE a fixed-length record EF using the TID APDU format. The standard `hf fmcos create file --type fix` +CREATE a fixed-length record EF using the TID APDU format. The standard `hf fmcos createfile --type fix` uses a different layout; this command uses the TID layout where P1=`0x02`, subtype=`0x01`, and count+reclen replace the size field. @@ -1203,8 +1203,8 @@ hf fmcos tidcreaterec --id 0003 --count 04 --reclen 10 --sfi 03 --rperm 20 --wpe **APDU:** `80 E0 02 00 0B 01 [fid_hi][fid_lo] [count][reclen] [rperm][wperm] [sfi] 00 FF 00` -> **Note**: To write data into TID EFs after creation, use the standard `hf fmcos write binary` -> and `hf fmcos write record` - those APDUs (`00 D6` / `00 DC`) are identical in TID and standard FMCOS. +> **Note**: To write data into TID EFs after creation, use the standard `hf fmcos writebinary` +> and `hf fmcos writerecord` - those APDUs (`00 D6` / `00 DC`) are identical in TID and standard FMCOS. --- @@ -1220,7 +1220,7 @@ normally provided when you buy these TID cards. command finishes without `-k` the field drops and the card resets to MF context on the next activation. - Each sub-DF requires a TID-format keyfile created immediately after selecting it. Use - `hf fmcos tidcreatebin --type keyfile` - the standard `hf fmcos create keyfile` uses a + `hf fmcos tidcreatebin --type keyfile` - the standard `hf fmcos createkeyfile` uses a different APDU layout and cannot create TID keyfiles. --- @@ -1239,12 +1239,12 @@ hf fmcos tidcreatedf --id 7572 --size 0200 --sfi 02 --name 390130990807 -k hf fmcos select --id 7572 -k hf fmcos tidcreatebin --type keyfile -k hf fmcos tidcreaterec --id 0001 --count 06 --reclen 10 --sfi 03 -k -hf fmcos write record --rec 01 --fid 03 --data 018609DD110000000000010100000000 -k -hf fmcos write record --rec 02 --fid 03 --data 00000000000000000000000000000000 -k -hf fmcos write record --rec 03 --fid 03 --data 0000000000070A0D1707120000008800 -k -hf fmcos write record --rec 04 --fid 03 --data 00000000000000000000000000000000 -k -hf fmcos write record --rec 05 --fid 03 --data 00000000000000000000000000000000 -k -hf fmcos write record --rec 06 --fid 03 --data 00000000000000000000000000000000 +hf fmcos writerecord --rec 01 --fid 03 --data 018609DD110000000000010100000000 -k +hf fmcos writerecord --rec 02 --fid 03 --data 00000000000000000000000000000000 -k +hf fmcos writerecord --rec 03 --fid 03 --data 0000000000070A0D1707120000008800 -k +hf fmcos writerecord --rec 04 --fid 03 --data 00000000000000000000000000000000 -k +hf fmcos writerecord --rec 05 --fid 03 --data 00000000000000000000000000000000 -k +hf fmcos writerecord --rec 06 --fid 03 --data 00000000000000000000000000000000 ``` --- @@ -1265,10 +1265,10 @@ hf fmcos select --id 1001 -k hf fmcos tidcreatebin --type keyfile -k hf fmcos tidcreatebin --id 0018 --size 008C --sfi 18 -k hf fmcos select --id 0018 -k -hf fmcos write binary --p1 00 --p2 00 --data 001FD921090700000001000023590000000000E0FFFFFF7F0700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -k +hf fmcos writebinary --p1 00 --p2 00 --data 001FD921090700000001000023590000000000E0FFFFFF7F0700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -k hf fmcos tidcreatebin --id 0019 --size 008C --sfi 19 -k hf fmcos select --id 0019 -k -hf fmcos write binary --p1 00 --p2 00 --data 001FD921090700000001000023590000FFFFFFFFFFFF010000000000E0FFFFFF0F0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +hf fmcos writebinary --p1 00 --p2 00 --data 001FD921090700000001000023590000FFFFFFFFFFFF010000000000E0FFFFFF0F0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ``` --- @@ -1288,7 +1288,7 @@ hf fmcos select --id 4A54 -k hf fmcos tidcreatebin --type keyfile -k hf fmcos tidcreatebin --id 4200 --size 0270 --sfi 01 -k hf fmcos select --id 4200 -k -hf fmcos write binary --p1 02 --p2 50 --data 530030FFFFFFFFFFFFFF3A2B0000000022012200002403081106000000007F00 +hf fmcos writebinary --p1 02 --p2 50 --data 530030FFFFFFFFFFFFFF3A2B0000000022012200002403081106000000007F00 ``` --- @@ -1314,19 +1314,19 @@ hf fmcos tidcreatebin --type keyfile -k hf fmcos tidcreatebin --id 0001 --size 0001 --sfi 01 -k hf fmcos tidcreatebin --id 0003 --size 001C --sfi 03 -k hf fmcos select --id 0003 -k -hf fmcos write binary --p1 00 --p2 00 --data FE937B922D7EDEF50000000000000000000000000000000000000000 -k +hf fmcos writebinary --p1 00 --p2 00 --data FE937B922D7EDEF50000000000000000000000000000000000000000 -k hf fmcos tidcreatebin --id 0004 --size 0078 --sfi 04 -k hf fmcos select --id 0004 -k -hf fmcos write binary --p1 00 --p2 00 --data 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -k +hf fmcos writebinary --p1 00 --p2 00 --data 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -k hf fmcos tidcreatebin --id 0005 --size 0024 --sfi 05 -k hf fmcos select --id 0005 -k -hf fmcos write binary --p1 00 --p2 00 --data 6403FE9C7434FCBC6E00FC9F2802F1448F06F3BA8CF6F0B98DF7F1B84D0BF60000000000 -k +hf fmcos writebinary --p1 00 --p2 00 --data 6403FE9C7434FCBC6E00FC9F2802F1448F06F3BA8CF6F0B98DF7F1B84D0BF60000000000 -k hf fmcos tidcreatebin --id 0006 --size 0084 --sfi 06 -k hf fmcos select --id 0006 -k -hf fmcos write binary --p1 00 --p2 00 --data 616BEACE7705FCBD7604FDBC7107F2BB7006F3BA7309F0B97208F1B84D0B09B74C0AF7494F0DF4B5B10CF5B449F0EAB3480E14B24B31E84E4A30E9B0BA33EEAF44CDEFAE473513AD4634ED534137E2ABBF36E3AA43C6E0A942381EA85D3BE6585C3AE7A6A03DE4A55E000000000000000000000000000000000000000000000000000000 -k +hf fmcos writebinary --p1 00 --p2 00 --data 616BEACE7705FCBD7604FDBC7107F2BB7006F3BA7309F0B97208F1B84D0B09B74C0AF7494F0DF4B5B10CF5B449F0EAB3480E14B24B31E84E4A30E9B0BA33EEAF44CDEFAE473513AD4634ED534137E2ABBF36E3AA43C6E0A942381EA85D3BE6585C3AE7A6A03DE4A55E000000000000000000000000000000000000000000000000000000 -k hf fmcos tidcreatebin --id 0007 --size 0066 --sfi 07 -k hf fmcos select --id 0007 -k -hf fmcos write binary --p1 00 --p2 00 --data 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +hf fmcos writebinary --p1 00 --p2 00 --data 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ``` --- @@ -1348,13 +1348,13 @@ hf fmcos select --id D0F1 -k hf fmcos tidcreatebin --type keyfile -k hf fmcos tidcreatebin --id 0005 --size 0040 --sfi 05 -k hf fmcos select --id 0005 -k -hf fmcos write binary --p1 00 --p2 00 --data 0200FC20006200000000000000000000000000272E2C6A0000000000000000080000000000000000000000000000000000000000000000000000000000000000 +hf fmcos writebinary --p1 00 --p2 00 --data 0200FC20006200000000000000000000000000272E2C6A0000000000000000080000000000000000000000000000000000000000000000000000000000000000 hf fmcos tidcreatedf --id D0F2 --size 0100 --sfi 02 --name 584C343536 -k hf fmcos select --id D0F2 -k hf fmcos tidcreatebin --type keyfile -k hf fmcos tidcreatebin --id 0005 --size 0040 --sfi 05 -k hf fmcos select --id 0005 -k -hf fmcos write binary --p1 00 --p2 00 --data 0200FC20006200000000000000000000000000272E2C6A0000000000000000080000000000000000000000000000000000000000000000000000000000000000 +hf fmcos writebinary --p1 00 --p2 00 --data 0200FC20006200000000000000000000000000272E2C6A0000000000000000080000000000000000000000000000000000000000000000000000000000000000 ``` --- @@ -1374,6 +1374,6 @@ hf fmcos select --id 3F01 -k hf fmcos tidcreatebin --type keyfile -k hf fmcos tidcreatebin --id 0003 --size 00FA --sfi 03 -k hf fmcos select --id 0003 -k -hf fmcos write binary --p1 00 --p2 00 --data D15190D7E1E379732295C97D62A3172BE3BBA1D1B32CE32FED72CB3DCDB115E7DC2670978E241822F298C9951260FC55D54F9988C7FCAC5032F94281DFC39C973E570101764D5BBF367F84EBDA1B012ABD4568F35D5BC08BAFD76B988CA916C985692337FCF02C9FD2C8BDD583BC05EF55582C3921FA2CAFAE26308FBADE0598DB750EE1F0522D29EAB6FA5D0F3971F785692337FCF02C9FD2C8BDD583BC05EF55582C3921FA2CAFAE26308FBADE0598DB750EE1F0522D29EAB6FA5D0F3971F7EA545FC5B27B7F40DF6D0F71FCEE2A1BCED2DEDE67BB57B1C1F98C8CDA5259CC7BD83158086F215F5E1E0246EE0504760000000000 -k -hf fmcos write binary --p1 00 --p2 F5 --data 0000000000 +hf fmcos writebinary --p1 00 --p2 00 --data D15190D7E1E379732295C97D62A3172BE3BBA1D1B32CE32FED72CB3DCDB115E7DC2670978E241822F298C9951260FC55D54F9988C7FCAC5032F94281DFC39C973E570101764D5BBF367F84EBDA1B012ABD4568F35D5BC08BAFD76B988CA916C985692337FCF02C9FD2C8BDD583BC05EF55582C3921FA2CAFAE26308FBADE0598DB750EE1F0522D29EAB6FA5D0F3971F785692337FCF02C9FD2C8BDD583BC05EF55582C3921FA2CAFAE26308FBADE0598DB750EE1F0522D29EAB6FA5D0F3971F7EA545FC5B27B7F40DF6D0F71FCEE2A1BCED2DEDE67BB57B1C1F98C8CDA5259CC7BD83158086F215F5E1E0246EE0504760000000000 -k +hf fmcos writebinary --p1 00 --p2 F5 --data 0000000000 ``` diff --git a/doc/fmcos_example.md b/doc/fmcos_example.md index 9dda57ff5..3f000cf0e 100644 --- a/doc/fmcos_example.md +++ b/doc/fmcos_example.md @@ -26,7 +26,7 @@ Add `-k` (keep field on) to every command in a chain except the last one. --- -## 1. reset — select MF and erase DF +## 1. reset - select MF and erase DF Select the Master File then erase the application directory from a previous run: @@ -41,7 +41,7 @@ itself cannot be erased this way). --- -## 2. setup — create directory, keyfile, keys, loop files, balance files +## 2. setup - create directory, keyfile, keys, loop files, balance files ### 2a. Create the application directory (ADF) @@ -52,7 +52,7 @@ itself cannot be erased this way). ``` hf fmcos select --id 3f00 -k -hf fmcos create dir --id 3F01 --space 1500 --cperm F0 --eperm F0 --appid 95 --name 77616C6C657454657374 -k +hf fmcos createdir --id 3F01 --space 1500 --cperm F0 --eperm F0 --appid 95 --name 77616C6C657454657374 -k ``` ### 2b. Select the new ADF by name @@ -66,7 +66,7 @@ All subsequent setup commands assume this DF remains selected (field stays on). ### 2c. Create the keyfile ``` -hf fmcos create keyfile --id 0000 --space 200 --dfsid 95 --perm F0 -k +hf fmcos createkeyfile --id 0000 --space 200 --dfsid 95 --perm F0 -k ``` ### 2d. Write keys @@ -77,74 +77,74 @@ hf fmcos create keyfile --id 0000 --space 200 --dfsid 95 --perm F0 -k All key writes continue in the same session so every command carries `-k`. -**Key 0 — DES Encrypt (int_enc)** +**Key 0 - DES Encrypt (int_enc)** ``` hf fmcos key --op 01 --id 00 --usage F0 --type desenc --change F4 --version 05 --algo 98 --key c4608b786af1992343e91a076670ae7c -k ``` -**Key 1 — DES Decrypt (int_dec)** +**Key 1 - DES Decrypt (int_dec)** ``` hf fmcos key --op 01 --id 00 --usage F0 --type desdec --change F4 --version 05 --algo 98 --key b8d4190c76856901fc686f36ab9b1ce0 -k ``` -**Key 2 — DES MAC (int_mac)** +**Key 2 - DES MAC (int_mac)** ``` hf fmcos key --op 01 --id 00 --usage F0 --type desmac --change F4 --version 05 --algo 98 --key 46a3ea8b254ee2749cc681050fd0dbcc -k ``` -**Key 3 — Internal Key (internal_key)** +**Key 3 - Internal Key (internal_key)** ``` hf fmcos key --op 01 --id 00 --usage F0 --type internal --change 02 --version 00 --algo 01 --key 2b8a438742c851566f02d881b09d58c0 -k ``` -**Key 4 — File Line Protection Key (line_protection_key)** +**Key 4 - File Line Protection Key (line_protection_key)** -Group C type — uses `--change` and `--errcount`. +Group C type - uses `--change` and `--errcount`. ``` hf fmcos key --op 01 --id 00 --usage F0 --type lineprotect --change 02 --errcount 33 --key 8a021972bfec9d152ca9eb82d7d12c09 -k ``` -**Key 5 — Unlock PIN Key (unlock_pin_key)** +**Key 5 - Unlock PIN Key (unlock_pin_key)** ``` hf fmcos key --op 01 --id 00 --usage F0 --type unlockpin --change 02 --errcount 33 --key d8f60fa2d791f3a658d27c05458243ed -k ``` -**Key 6 — Change PIN Key (change_pin_key)** +**Key 6 - Change PIN Key (change_pin_key)** ``` hf fmcos key --op 01 --id 00 --usage F0 --type changepin --change 02 --errcount 33 --key fb487a6d1b7cbf1bf84c666b8338376e -k ``` -**Key 7 — External Authentication Key (external_auth_key)** +**Key 7 - External Authentication Key (external_auth_key)** -Group B type with `--change` — uses `--change`, `--followup`, and `--errcount`. +Group B type with `--change` - uses `--change`, `--followup`, and `--errcount`. ``` hf fmcos key --op 01 --id 00 --usage F0 --type extauth --change 02 --followup 44 --errcount 33 --key f49dc1ba1b4deb52647186bc59106c0d -k ``` -**Key 8 — Purchase Key (purchase_key)** +**Key 8 - Purchase Key (purchase_key)** ``` hf fmcos key --op 01 --id 00 --usage F0 --type purchase --change 02 --version 00 --algo 01 --key eb18ce6986c820970e876219052ce0cf -k ``` -**Key 9 — Credit Key (credit_key)** +**Key 9 - Credit Key (credit_key)** ``` hf fmcos key --op 01 --id 00 --usage F0 --type credit --change 02 --version 00 --algo 01 --key a9e6e145f5df09500a58eef8575d49db -k ``` -**Key 10 — Debit Key (debit_key)** +**Key 10 - Debit Key (debit_key)** ``` hf fmcos key --op 01 --id 00 --usage F0 --type debit --change 02 --version 00 --algo 01 --key 97fb4eda4b5237035946ee62d325d909 -k ``` -**Key 11 — Overdraw Limit Key (overdraw_limit_key)** +**Key 11 - Overdraw Limit Key (overdraw_limit_key)** ``` hf fmcos key --op 01 --id 00 --usage F0 --type overdraft --change 02 --version 00 --algo 01 --key 94f63c4fae5e4977d749928ad12bc128 -k ``` -**Key 12 — PIN Key (pin_code)** +**Key 12 - PIN Key (pin_code)** The PIN `\x12\x34\x56` is 3 raw BCD bytes. -Group B type — uses `--followup` and `--errcount`. +Group B type - uses `--followup` and `--errcount`. ``` hf fmcos key --op 01 --id 00 --usage F0 --type pin --followup 01 --errcount 33 --key 123456 -k ``` @@ -153,46 +153,46 @@ hf fmcos key --op 01 --id 00 --usage F0 --type pin --followup 01 --errcount 33 - Loop file 0x0018 (to be linked to the wallet balance file): ``` -hf fmcos create file --id 0018 --type loop --size 0517 --rperm F0 --wperm EF --access FF -k +hf fmcos createfile --id 0018 --type loop --size 0517 --rperm F0 --wperm EF --access FF -k ``` Loop file 0x0019 (to be linked to the passbook balance file): ``` -hf fmcos create file --id 0019 --type loop --size 0517 --rperm F0 --wperm EF --access FF -k +hf fmcos createfile --id 0019 --type loop --size 0517 --rperm F0 --wperm EF --access FF -k ``` ### 2f. Create wallet and passbook balance files Wallet balance file (EF 0x0002, linked to loop file 0x0018): ``` -hf fmcos create file --id 0002 --type wallet --size 0208 --rperm F0 --wperm 00 --access 18 -k +hf fmcos createfile --id 0002 --type wallet --size 0208 --rperm F0 --wperm 00 --access 18 -k ``` Passbook balance file (EF 0x0001, linked to loop file 0x0019): ``` -hf fmcos create file --id 0001 --type wallet --size 0208 --rperm F0 --wperm 00 --access 19 +hf fmcos createfile --id 0001 --type wallet --size 0208 --rperm F0 --wperm 00 --access 19 ``` The last command drops the field to end the setup session. --- -## 3. verify_pin — verify the PIN +## 3. verify_pin - verify the PIN The PIN `\x12\x34\x56` is 3 raw BCD bytes. ``` hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 +hf fmcos pinverify --id 00 --pin 123456 ``` --- -## 4. get_balance — read wallet and passbook balances +## 4. get_balance - read wallet and passbook balances ``` hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos balance --type wallet -k hf fmcos balance --type passbook ``` @@ -201,16 +201,16 @@ The command prints the 4-byte big-endian balance in decimal and hex. --- -## 5. add_money — credit (load funds) +## 5. add_money - credit (load funds) Credit key index is 9 (written ninth in step 2d, 0-based index = 9 = 0x09). Credit 1000 units to the wallet, then 2000 to the passbook in one session: ``` hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos credit --type wallet --id 09 --amount 1000 --terminal 666666666666 --key a9e6e145f5df09500a58eef8575d49db --ikey 2b8a438742c851566f02d881b09d58c0 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos credit --type passbook --id 09 --amount 2000 --terminal 666666666666 --key a9e6e145f5df09500a58eef8575d49db --ikey 2b8a438742c851566f02d881b09d58c0 ``` @@ -223,21 +223,21 @@ hf fmcos credit --type passbook --id 09 --amount 2000 --terminal 666666666666 -- --- -## 6. spend_wallet / spend_passbook — purchase (deduct funds) +## 6. spend_wallet / spend_passbook - purchase (deduct funds) Purchase key index is 8 (0-based index = 8 = 0x08). Purchase (deduct) 50 units from the wallet: ``` hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos purchase --type wallet --id 08 --amount 50 --terminal 666666666666 --key eb18ce6986c820970e876219052ce0cf --ikey 2b8a438742c851566f02d881b09d58c0 ``` Purchase 50 units from the passbook: ``` hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos purchase --type passbook --id 08 --amount 50 --terminal 666666666666 --key eb18ce6986c820970e876219052ce0cf --ikey 2b8a438742c851566f02d881b09d58c0 ``` @@ -246,21 +246,21 @@ hf fmcos purchase --type passbook --id 08 --amount 50 --terminal 666666666666 -- --- -## 7. withdraw_money — cash withdrawal (NOT SUPPORTED) +## 7. withdraw_money - cash withdrawal (NOT SUPPORTED) Cash withdrawal uses INS=0x50 P1=0x02. There is currently no `hf fmcos` command for this operation. --- -## 8. pin_block — deliberately block the PIN +## 8. pin_block - deliberately block the PIN ``` hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 11223344 -k -hf fmcos pin verify --id 00 --pin 11223344 -k -hf fmcos pin verify --id 00 --pin 11223344 -k -hf fmcos pin verify --id 00 --pin 11223344 +hf fmcos pinverify --id 00 --pin 11223344 -k +hf fmcos pinverify --id 00 --pin 11223344 -k +hf fmcos pinverify --id 00 --pin 11223344 -k +hf fmcos pinverify --id 00 --pin 11223344 ``` After the error counter reaches zero the card blocks the PIN and returns @@ -268,31 +268,31 @@ SW=`6983`. --- -## 9. pin_unblock — restore a blocked PIN +## 9. pin_unblock - restore a blocked PIN Unlock PIN key index is 5 (0-based index = 5 = 0x05). ``` hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin unblock --id 00 --pin 123456 --key d8f60fa2d791f3a658d27c05458243ed +hf fmcos pinunblock --id 00 --pin 123456 --key d8f60fa2d791f3a658d27c05458243ed ``` --- -## 10. online_debit — online transfer (NOT SUPPORTED) +## 10. online_debit - online transfer (NOT SUPPORTED) Online debit uses INS=0x50 P1=0x05 (initialize) and INS=0x54 P1=0x03 (commit). There is currently no `hf fmcos` command for this operation. --- -## 11. update_overdraft — set the overdraft limit +## 11. update_overdraft - set the overdraft limit Overdraft key index is 11 (0-based index = 11 = 0x0B). ``` hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos overdraft --id 0B --limit 1000 --terminal 666666666666 --key 94f63c4fae5e4977d749928ad12bc128 --ikey 2b8a438742c851566f02d881b09d58c0 ``` @@ -304,7 +304,7 @@ verified using DES-CBC-MAC with `tac_key = XOR(ikey[0:8], ikey[8:16])` over: --- -## 12. get_history — read transaction history +## 12. get_history - read transaction history Read the most recent 10 records from the wallet loop file (SFI 0x18): @@ -335,30 +335,30 @@ always the most recently written entry. --- -## 13. pin_change — change PIN using old PIN authorization +## 13. pin_change - change PIN using old PIN authorization ``` hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 -k -hf fmcos pin change --id 00 --old 123456 --new 13371337 -k -hf fmcos pin verify --id 00 --pin 13371337 -k -hf fmcos pin change --id 00 --old 13371337 --new 123456 -k -hf fmcos pin verify --id 00 --pin 123456 +hf fmcos pinverify --id 00 --pin 123456 -k +hf fmcos pinchange --id 00 --old 123456 --new 13371337 -k +hf fmcos pinverify --id 00 --pin 13371337 -k +hf fmcos pinchange --id 00 --old 13371337 --new 123456 -k +hf fmcos pinverify --id 00 --pin 123456 ``` --- -## 14. pin_reset — set new PIN using change-PIN key (no old PIN needed) +## 14. pin_reset - set new PIN using change-PIN key (no old PIN needed) Change PIN key index is 6 (0-based index = 6 = 0x06). ``` hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 -k -hf fmcos pin reset --id 00 --pin 13371337 --key fb487a6d1b7cbf1bf84c666b8338376e -k -hf fmcos pin verify --id 00 --pin 13371337 -k -hf fmcos pin change --id 00 --old 13371337 --new 123456 -k -hf fmcos pin verify --id 00 --pin 123456 +hf fmcos pinverify --id 00 --pin 123456 -k +hf fmcos pinreset --id 00 --pin 13371337 --key fb487a6d1b7cbf1bf84c666b8338376e -k +hf fmcos pinverify --id 00 --pin 13371337 -k +hf fmcos pinchange --id 00 --old 13371337 --new 123456 -k +hf fmcos pinverify --id 00 --pin 123456 ``` --- @@ -369,24 +369,24 @@ The following block shows a full session in order: reset, setup, load money, spend, and check balance. Each sub-section starts a new session (field activates) and ends when the last command drops the field. -> **Note**: `--space` and `--size` are hex — `--space 1500` = 5376 bytes, `--space 400` = 1024 bytes, `--size 0517` = 1303 bytes, `--size 0208` = 520 bytes. +> **Note**: `--space` and `--size` are hex - `--space 1500` = 5376 bytes, `--space 400` = 1024 bytes, `--size 0517` = 1303 bytes, `--size 0208` = 520 bytes. ``` -# ── 1. Reset ──────────────────────────────────────────────────────────────── +# -- 1. Reset ---------------------------------------------------------------- hf fmcos select --id 3f00 -k hf fmcos erase -# ── 2. Create ADF ──────────────────────────────────────────────────────────── +# -- 2. Create ADF ------------------------------------------------------------ hf fmcos select --id 3f00 -k -hf fmcos create dir --id 3F01 --space 1500 --cperm F0 --eperm F0 --appid 95 --name 77616C6C657454657374 -k +hf fmcos createdir --id 3F01 --space 1500 --cperm F0 --eperm F0 --appid 95 --name 77616C6C657454657374 -k -# ── 3. Select ADF ──────────────────────────────────────────────────────────── +# -- 3. Select ADF ------------------------------------------------------------ hf fmcos select --name 77616C6C657454657374 -k -# ── 4. Create keyfile ──────────────────────────────────────────────────────── -hf fmcos create keyfile --id 0000 --space 400 --dfsid 95 --perm F0 -k +# -- 4. Create keyfile -------------------------------------------------------- +hf fmcos createkeyfile --id 0000 --space 400 --dfsid 95 --perm F0 -k -# ── 5. Write 13 keys (indexes 0-12) ───────────────────────────────────────── +# -- 5. Write 13 keys (indexes 0-12) ----------------------------------------- hf fmcos key --op 01 --id 00 --usage F0 --type desenc --change F4 --version 05 --algo 98 --key c4608b786af1992343e91a076670ae7c -k hf fmcos key --op 01 --id 00 --usage F0 --type desdec --change F4 --version 05 --algo 98 --key b8d4190c76856901fc686f36ab9b1ce0 -k hf fmcos key --op 01 --id 00 --usage F0 --type desmac --change F4 --version 05 --algo 98 --key 46a3ea8b254ee2749cc681050fd0dbcc -k @@ -401,50 +401,50 @@ hf fmcos key --op 01 --id 00 --usage F0 --type debit --change 02 --version 00 hf fmcos key --op 01 --id 00 --usage F0 --type overdraft --change 02 --version 00 --algo 01 --key 94f63c4fae5e4977d749928ad12bc128 -k hf fmcos key --op 01 --id 00 --usage F0 --type pin --followup 01 --errcount 33 --key 123456 -k -# ── 6. Create loop files ───────────────────────────────────────────────────── -hf fmcos create file --id 0018 --type loop --size 0517 --rperm F0 --wperm EF --access FF -k -hf fmcos create file --id 0019 --type loop --size 0517 --rperm F0 --wperm EF --access FF -k +# -- 6. Create loop files ----------------------------------------------------- +hf fmcos createfile --id 0018 --type loop --size 0517 --rperm F0 --wperm EF --access FF -k +hf fmcos createfile --id 0019 --type loop --size 0517 --rperm F0 --wperm EF --access FF -k -# ── 7. Create balance files ────────────────────────────────────────────────── -hf fmcos create file --id 0002 --type wallet --size 0208 --rperm F0 --wperm 00 --access 18 -k -hf fmcos create file --id 0001 --type wallet --size 0208 --rperm F0 --wperm 00 --access 19 +# -- 7. Create balance files -------------------------------------------------- +hf fmcos createfile --id 0002 --type wallet --size 0208 --rperm F0 --wperm 00 --access 18 -k +hf fmcos createfile --id 0001 --type wallet --size 0208 --rperm F0 --wperm 00 --access 19 -# ── 8. Verify PIN ──────────────────────────────────────────────────────────── +# -- 8. Verify PIN ------------------------------------------------------------ hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 +hf fmcos pinverify --id 00 --pin 123456 -# ── 9. Credit wallet +1000, passbook +2000 ─────────────────────────────────── +# -- 9. Credit wallet +1000, passbook +2000 ----------------------------------- hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos credit --type wallet --id 09 --amount 1000 --terminal 666666666666 --key a9e6e145f5df09500a58eef8575d49db --ikey 2b8a438742c851566f02d881b09d58c0 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos credit --type passbook --id 09 --amount 2000 --terminal 666666666666 --key a9e6e145f5df09500a58eef8575d49db --ikey 2b8a438742c851566f02d881b09d58c0 -# ── 10. Check balances ─────────────────────────────────────────────────────── +# -- 10. Check balances ------------------------------------------------------- hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos balance --type wallet -k hf fmcos balance --type passbook -# ── 11. Purchase (spend) from wallet then passbook ─────────────────────────── +# -- 11. Purchase (spend) from wallet then passbook --------------------------- hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos purchase --type wallet --id 08 --amount 50 --terminal 666666666666 --key eb18ce6986c820970e876219052ce0cf --ikey 2b8a438742c851566f02d881b09d58c0 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos purchase --type passbook --id 08 --amount 50 --terminal 666666666666 --key eb18ce6986c820970e876219052ce0cf --ikey 2b8a438742c851566f02d881b09d58c0 -# ── 12. Check balances again ───────────────────────────────────────────────── +# -- 12. Check balances again ------------------------------------------------- hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos balance --type wallet -k hf fmcos balance --type passbook -# ── 13. Update overdraft limit to 1000 ────────────────────────────────────── +# -- 13. Update overdraft limit to 1000 -------------------------------------- hf fmcos select --name 77616C6C657454657374 -k -hf fmcos pin verify --id 00 --pin 123456 -k +hf fmcos pinverify --id 00 --pin 123456 -k hf fmcos overdraft --id 0B --limit 1000 --terminal 666666666666 --key 94f63c4fae5e4977d749928ad12bc128 --ikey 2b8a438742c851566f02d881b09d58c0 -# ── 14. Read transaction history ───────────────────────────────────────────── +# -- 14. Read transaction history --------------------------------------------- hf fmcos select --name 77616C6C657454657374 -k hf fmcos history --fid 18 -k hf fmcos history --fid 19 @@ -457,8 +457,8 @@ hf fmcos history --fid 19 That script demos binary/variable/loop file creation with line protection (none, MAC, MAC+enc), plus application block/unblock. Two DFs are created on the same card: -- **blockTest** (DF 3FFF, appid 0x94) — block/unblock target -- **fileTest** (DF 3F01, appid 0x95) — file read/write target +- **blockTest** (DF 3FFF, appid 0x94) - block/unblock target +- **fileTest** (DF 3F01, appid 0x95) - file read/write target ### Keys used @@ -485,36 +485,36 @@ hf fmcos erase --- -### setup — create both DFs +### setup - create both DFs #### blockTest DF (3FFF) ``` hf fmcos select --id 3f00 -k -hf fmcos create dir --id 3FFF --space 500 --cperm F0 --eperm F0 --appid 94 --name 626c6f636b54657374 -k +hf fmcos createdir --id 3FFF --space 500 --cperm F0 --eperm F0 --appid 94 --name 626c6f636b54657374 -k hf fmcos select --name 626c6f636b54657374 -k -hf fmcos create keyfile --id 0001 --space 200 --dfsid 94 --perm F0 -k +hf fmcos createkeyfile --id 0001 --space 200 --dfsid 94 --perm F0 -k ``` Keys written into blockTest (keyfile 0001): ``` -# Key 0 — lineprotect (line_protection_key_1) +# Key 0 - lineprotect (line_protection_key_1) hf fmcos key --op 01 --id 00 --usage F0 --type lineprotect --change 02 --errcount 33 --key 49439874a1f623fc5e14818365d34699 -k -# Key 1 — extauth (external_auth_key_1) +# Key 1 - extauth (external_auth_key_1) hf fmcos key --op 01 --id 00 --usage F0 --type extauth --change F0 --followup AA --errcount FF --key da152a9a56def40a1386ca258788fea6 -k -# Key 2 — internal (internal_key_1) +# Key 2 - internal (internal_key_1) hf fmcos key --op 01 --id 00 --usage F0 --type internal --change F0 --version 00 --algo 01 --key bb4a314981b20ce696d6c1e1cda5820c -k ``` Seed binary file 0x0002 in blockTest: ``` -hf fmcos create file --id 0002 --type bin --size 50 --rperm F0 --wperm F0 --access FF -k +hf fmcos createfile --id 0002 --type bin --size 50 --rperm F0 --wperm F0 --access FF -k hf fmcos select --id 0002 -k -hf fmcos write binary --p1 00 --p2 00 --data 62696e66696c655f626c6f636b5f74657374 +hf fmcos writebinary --p1 00 --p2 00 --data 62696e66696c655f626c6f636b5f74657374 ``` (`62696e66696c655f626c6f636b5f74657374` = ASCII `binfile_block_test`) @@ -523,25 +523,25 @@ hf fmcos write binary --p1 00 --p2 00 --data 62696e66696c655f626c6f636b5f7465737 ``` hf fmcos select --id 3f00 -k -hf fmcos create dir --id 3F01 --space 1500 --cperm F0 --eperm F0 --appid 95 --name 66696c6554657374 -k +hf fmcos createdir --id 3F01 --space 1500 --cperm F0 --eperm F0 --appid 95 --name 66696c6554657374 -k hf fmcos select --name 66696c6554657374 -k -hf fmcos create keyfile --id 0001 --space 200 --dfsid 95 --perm F0 -k +hf fmcos createkeyfile --id 0001 --space 200 --dfsid 95 --perm F0 -k ``` Keys written into fileTest (keyfile 0001). Keys 2 and 3 are written with MAC+enc line protection (`--prot enc --authkey `): ``` -# Key 0 — extauth (external_auth_key), unprotected write +# Key 0 - extauth (external_auth_key), unprotected write hf fmcos key --op 01 --id 00 --usage F0 --type extauth --change F0 --followup AA --errcount FF --key 7c3f149ed331b11211d2fb62e2df9637 -k -# Key 1 — internal (internal_key), unprotected write +# Key 1 - internal (internal_key), unprotected write hf fmcos key --op 01 --id 00 --usage F0 --type internal --change F0 --version 00 --algo 01 --key 659a500f0f1fce35b6884bdff966576a -k -# Key 2 — lineprotect (line_protection_key), written with enc protection +# Key 2 - lineprotect (line_protection_key), written with enc protection hf fmcos key --op 01 --id 00 --usage F0 --type lineprotect --change F0 --errcount FF --key 980093b4d77ff65f7476bf9019a80892 --authkey 7c3f149ed331b11211d2fb62e2df9637 --prot enc -k -# Key 3 (slot 02) — extauth (enc_external_auth_key), written with enc protection +# Key 3 (slot 02) - extauth (enc_external_auth_key), written with enc protection hf fmcos key --op 01 --id 02 --usage F0 --type extauth --change F0 --followup AA --errcount FF --key 44ea0184094995a845b612522a8ab463 --authkey 7c3f149ed331b11211d2fb62e2df9637 --prot enc -k ``` @@ -550,20 +550,20 @@ MAC+enc (`--access 7F --prot enc`). `--access 7F` = protection required, use ke ``` # Binary files -hf fmcos create file --id 0002 --type bin --size 50 --rperm F0 --wperm F0 --access FF -k -hf fmcos create file --id 0003 --type bin --size 50 --rperm F0 --wperm F0 --access 7F --prot mac -k -hf fmcos create file --id 0004 --type bin --size 50 --rperm F0 --wperm F0 --access 7F --prot enc -k +hf fmcos createfile --id 0002 --type bin --size 50 --rperm F0 --wperm F0 --access FF -k +hf fmcos createfile --id 0003 --type bin --size 50 --rperm F0 --wperm F0 --access 7F --prot mac -k +hf fmcos createfile --id 0004 --type bin --size 50 --rperm F0 --wperm F0 --access 7F --prot enc -k # Variable-length record files -hf fmcos create file --id 0006 --type var --size 50 --rperm F0 --wperm F0 --access FF -k -hf fmcos create file --id 0007 --type var --size 50 --rperm F0 --wperm F0 --access 7F --prot mac -k -hf fmcos create file --id 0008 --type var --size 50 --rperm F0 --wperm F0 --access 7F --prot enc -k +hf fmcos createfile --id 0006 --type var --size 50 --rperm F0 --wperm F0 --access FF -k +hf fmcos createfile --id 0007 --type var --size 50 --rperm F0 --wperm F0 --access 7F --prot mac -k +hf fmcos createfile --id 0008 --type var --size 50 --rperm F0 --wperm F0 --access 7F --prot enc -k # Loop (cyclic) files --size 210 = 0x210 = 528 bytes -# space = record_count*(record_len+1)+8; e.g. 5 records × (0x50+1) + 8 = 0x19D -hf fmcos create file --id 000A --type loop --size 210 --rperm F0 --wperm F0 --access FF -k -hf fmcos create file --id 000B --type loop --size 210 --rperm F0 --wperm F0 --access 7F --prot mac -k -hf fmcos create file --id 000C --type loop --size 210 --rperm F0 --wperm F0 --access 7F --prot enc +# space = record_count*(record_len+1)+8; e.g. 5 records x (0x50+1) + 8 = 0x19D +hf fmcos createfile --id 000A --type loop --size 210 --rperm F0 --wperm F0 --access FF -k +hf fmcos createfile --id 000B --type loop --size 210 --rperm F0 --wperm F0 --access 7F --prot mac -k +hf fmcos createfile --id 000C --type loop --size 210 --rperm F0 --wperm F0 --access 7F --prot enc ``` --- @@ -573,13 +573,13 @@ hf fmcos create file --id 000C --type loop --size 210 --rperm F0 --wperm F0 --ac ``` hf fmcos select --id 3f01 -k hf fmcos select --id 0002 -k -hf fmcos write binary --p1 00 --p2 00 --data 111213141516171819101a1b1c1d1e1f -k +hf fmcos writebinary --p1 00 --p2 00 --data 111213141516171819101a1b1c1d1e1f -k hf fmcos select --id 0003 -k -hf fmcos write binary --p1 00 --p2 00 --data 212223242526272829202a2b2c2d2e2f --prot mac --key 980093b4d77ff65f7476bf9019a80892 -k +hf fmcos writebinary --p1 00 --p2 00 --data 212223242526272829202a2b2c2d2e2f --prot mac --key 980093b4d77ff65f7476bf9019a80892 -k hf fmcos select --id 0004 -k -hf fmcos write binary --p1 00 --p2 00 --data 313233343536373839303a3b3c3d3e3f --prot enc --key 980093b4d77ff65f7476bf9019a80892 +hf fmcos writebinary --p1 00 --p2 00 --data 313233343536373839303a3b3c3d3e3f --prot enc --key 980093b4d77ff65f7476bf9019a80892 ``` --- @@ -603,20 +603,20 @@ hf fmcos append --fid 0c --data b1b2b3b4b5b6b7b8b9b0babbbcbdbebf --prot enc --ke ### write_record (UPDATE RECORD) P2 encodes the SFI: `(file_id & 0x1F) << 3 | 4`. The CLI `--fid` takes the raw file ID byte -(1–30) and encodes P2 automatically. +(1-30) and encodes P2 automatically. Variable-length files require `--tlv` so the data is wrapped as `00[len][data]`. ``` hf fmcos select --id 3f01 -k hf fmcos select --id 0006 -k -hf fmcos write record --rec 1 --fid 06 --data 515253545556575859505a5b5c5d5e5f --tlv -k +hf fmcos writerecord --rec 1 --fid 06 --data 515253545556575859505a5b5c5d5e5f --tlv -k hf fmcos select --id 0007 -k -hf fmcos write record --rec 1 --fid 07 --data 616263646566676869606a6b6c6d6e6f --tlv --prot mac --key 980093b4d77ff65f7476bf9019a80892 -k +hf fmcos writerecord --rec 1 --fid 07 --data 616263646566676869606a6b6c6d6e6f --tlv --prot mac --key 980093b4d77ff65f7476bf9019a80892 -k hf fmcos select --id 0008 -k -hf fmcos write record --rec 1 --fid 08 --data 717273747576777879707a7b7c7d7e7f --tlv --prot enc --key 980093b4d77ff65f7476bf9019a80892 +hf fmcos writerecord --rec 1 --fid 08 --data 717273747576777879707a7b7c7d7e7f --tlv --prot enc --key 980093b4d77ff65f7476bf9019a80892 ``` --- @@ -628,13 +628,13 @@ For MAC-protected reads the card appends a 4-byte MAC before the SW; the CLI str ``` hf fmcos select --id 3f01 -k hf fmcos select --id 0002 -k -hf fmcos read binary --p1 00 --p2 00 --len 16 -k +hf fmcos readbinary --p1 00 --p2 00 --len 16 -k hf fmcos select --id 0003 -k -hf fmcos read binary --p1 00 --p2 00 --len 16 --prot mac --key 980093b4d77ff65f7476bf9019a80892 -k +hf fmcos readbinary --p1 00 --p2 00 --len 16 --prot mac --key 980093b4d77ff65f7476bf9019a80892 -k hf fmcos select --id 0004 -k -hf fmcos read binary --p1 00 --p2 00 --len 16 --prot enc --key 980093b4d77ff65f7476bf9019a80892 +hf fmcos readbinary --p1 00 --p2 00 --len 16 --prot enc --key 980093b4d77ff65f7476bf9019a80892 ``` --- @@ -645,9 +645,9 @@ Variable-length files need `--tlv`; the CLI requests 2 extra bytes and strips th ``` hf fmcos select --id 3f01 -k -hf fmcos read record --rec 01 --fid 06 --len 16 --tlv -k -hf fmcos read record --rec 01 --fid 07 --len 16 --tlv --prot mac --key 980093b4d77ff65f7476bf9019a80892 -k -hf fmcos read record --rec 01 --fid 08 --len 16 --tlv --prot enc --key 980093b4d77ff65f7476bf9019a80892 +hf fmcos readrecord --rec 01 --fid 06 --len 16 --tlv -k +hf fmcos readrecord --rec 01 --fid 07 --len 16 --tlv --prot mac --key 980093b4d77ff65f7476bf9019a80892 -k +hf fmcos readrecord --rec 01 --fid 08 --len 16 --tlv --prot enc --key 980093b4d77ff65f7476bf9019a80892 ``` --- @@ -658,9 +658,9 @@ Loop files use the same READ RECORD command; `--rec 01` reads the most recently ``` hf fmcos select --id 3f01 -k -hf fmcos read record --rec 01 --fid 0a --len 16 -k -hf fmcos read record --rec 01 --fid 0b --len 16 --prot mac --key 980093b4d77ff65f7476bf9019a80892 -k -hf fmcos read record --rec 01 --fid 0c --len 16 --prot enc --key 980093b4d77ff65f7476bf9019a80892 +hf fmcos readrecord --rec 01 --fid 0a --len 16 -k +hf fmcos readrecord --rec 01 --fid 0b --len 16 --prot mac --key 980093b4d77ff65f7476bf9019a80892 -k +hf fmcos readrecord --rec 01 --fid 0c --len 16 --prot enc --key 980093b4d77ff65f7476bf9019a80892 ``` --- @@ -693,5 +693,5 @@ hf fmcos select --name 626c6f636b54657374 -k hf fmcos unblock --key 49439874a1f623fc5e14818365d34699 -k hf fmcos select --name 626c6f636b54657374 -k hf fmcos select --id 0002 -k -hf fmcos read binary --p1 00 --p2 00 --len 16 +hf fmcos readbinary --p1 00 --p2 00 --len 16 ``` From 1fea325d6fbe3dc5f0eed8e5c99d8953f4544dd9 Mon Sep 17 00:00:00 2001 From: klks Date: Mon, 25 May 2026 15:05:16 +0800 Subject: [PATCH 4/4] Add more details on phase 1 and 2 for purchase/credit/overdraft --- doc/fmcos.md | 201 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 169 insertions(+), 32 deletions(-) diff --git a/doc/fmcos.md b/doc/fmcos.md index 0316f4678..64e4718c2 100644 --- a/doc/fmcos.md +++ b/doc/fmcos.md @@ -114,11 +114,44 @@ hf fmcos createdir --id 3f01 --space 1500 --cperm f0 --eperm f0 --appid 95 --nam |------|-------------| | `--id ` | 2-byte file ID for the new DF | | `--space ` | Total byte space to reserve (hex, e.g. `1500` = 5376 bytes) | -| `--cperm ` | Create-permission byte (e.g. `f0` = always allowed) | -| `--eperm ` | Erase-permission byte | -| `--appid ` | 1-byte application SID / AID tag | +| `--cperm ` | Create-permission byte - who may create files inside this DF | +| `--eperm ` | Erase-permission byte - who may erase this DF | +| `--appid ` | 1-byte application file ID (DF type + short identifier) | | `--name ` | Optional DF name bytes as hex (up to 16 bytes, enables select-by-name) | +**`--cperm` (create permission):** + +Controls the minimum security state required to create new EFs or sub-DFs inside this DF. +Uses the same XY nibble encoding as `--rperm` / `--wperm` in `createfile` (see the permission byte table there). + +After `erase` clears the DF, the create-permission check is suspended and any file may be created freely. +The restriction is reinstated the next time the DF is re-entered once a KEY file has been established inside it. + +**`--eperm` (erase permission):** + +Controls the minimum security state required to run `erase` on this DF. +Uses the same XY nibble encoding. +Erasing a DF destroys all EFs and sub-DFs inside it; the DF record, its permissions, and its allocated space +are preserved. +Set to `ef` to prohibit erasing entirely: since E (14) < F (15), the range E..F is impossible and the condition +can never be satisfied. + +**`--appid` (application file ID):** + +A 1-byte value stored in the DF control block that encodes both the DF type and a short numeric identifier. + +| Bits 7-5 | Meaning | +|----------|---------| +| `000` | Dedicated Directory File (DDF) - a plain directory, no application context | +| `100` | Application Dedicated File (ADF) - holds a PBOC / EMV application | + +Bits 4-0 are the short file identifier (0-31) used by the card to link this DF to its keyfile. +This value must equal the `--dfsid` argument when creating the keyfile for this DF with `createkeyfile`. + +Example: `--appid 95` = binary `10010101` - bits 7-5 are `100` (ADF), bits 4-0 are `10101` = 21 = `0x15`, +so the FCI returned on SELECT FILE references short identifier `0x15` and the matching keyfile uses `--dfsid 95`. +Standard PBOC wallet DFs conventionally use `0x95`. + ### createfile CREATE EF (elementary file) in the currently selected DF. @@ -616,16 +649,43 @@ Example output: ### credit -ADD CREDIT -- two-phase credit transaction. +ADD CREDIT -- two-phase credit (load) transaction (PBOC INITIALIZE FOR LOAD + CREDIT FOR LOAD). -**Phase 1** (`INS 50`, P1=00): send key ID, amount, terminal ID. Card returns old balance, -transaction serial, key version, algo ID, random seed, and MAC1. The implementation: -- Derives the process key: `encrypt(random[4] | serial[2], credit_key)` -> first 8 bytes -- Verifies MAC1: `DES-CBC-MAC(old_bal[4] | amount[4] | type[1] | terminal[6], process_key)` +**Phase 1 -- INITIALIZE FOR LOAD** (`INS 50`, P1=00): -**Phase 2** (`INS 52`, P1=00): send date, time, MAC2. Card returns TAC. The implementation: -- Computes MAC2: `DES-CBC-MAC(amount[4] | type[1] | terminal[6] | date[4] | time[3], process_key)` -- Verifies TAC: `DES-CBC-MAC(new_bal[4] | serial[2] | MAC2_buf[18], tac_key)` +Initiates the credit transaction and authenticates the card to the terminal. +The terminal sends the credit key slot ID, the load amount (4 bytes), and the terminal ID (6 bytes). +The card responds with 16 bytes: + +| Field | Length | Description | +|-------|--------|-------------| +| Old balance | 4 bytes | Current balance before loading | +| Online serial number | 2 bytes | Incremented by the card after each successful load | +| Key version | 1 byte | Version of the credit key identified by the key slot ID | +| Algorithm ID | 1 byte | Algorithm of that credit key | +| Random seed | 4 bytes | Card-generated pseudorandom number for session key derivation | +| MAC1 | 4 bytes | Card-computed MAC proving it holds the credit key | + +The session key (process key) is derived as: `encrypt(random[4] | serial[2] | 0x8000, credit_key)`, first 8 bytes. +MAC1 is: `DES-CBC-MAC(old_bal[4] | amount[4] | type[1] | terminal[6], process_key)`. +The terminal verifies MAC1 to confirm the card holds the correct key before proceeding. +If MAC1 does not match, the transaction is aborted and the balance is unchanged. + +**Phase 2 -- CREDIT FOR LOAD** (`INS 52`, P1=00): + +Authorizes and commits the balance update. +The terminal sends the host transaction date (4 bytes), time (3 bytes), and MAC2 (4 bytes). +MAC2 is: `DES-CBC-MAC(amount[4] | type[1] | terminal[6] | date[4] | time[3], process_key)`. + +On success the card: +- Adds the loaded amount to the balance. +- Increments the online serial number by 1. +- Appends a 23-byte transaction record (serial, overdraft limit, amount, type, terminal, date, time) to the linked loop EF for auditing. + +The card responds with the TAC (Transaction Authentication Code, 4 bytes): +`DES-CBC-MAC(new_bal[4] | serial[2] | amount[4] | type[1] | terminal[6] | date[4] | time[3], dtk_xor)`, +where `dtk_xor` is the left 8 bytes XOR right 8 bytes of the internal key (DTK). +The terminal verifies the TAC to confirm the card committed the transaction. ``` hf fmcos credit --type wallet --id 01 --amount 1000 \ @@ -662,17 +722,56 @@ Example output: ### purchase -PURCHASE -- two-phase debit transaction from wallet or passbook. +PURCHASE -- two-phase offline debit transaction from wallet or passbook +(PBOC INITIALIZE FOR PURCHASE + DEBIT FOR PURCHASE). -**Phase 1** (`INS 50`, P1=01): send key ID, amount, terminal. Card returns old balance, -offline serial, overdraft limit, key version, algo, random seed (15 bytes total). -- Derives process key: `encrypt(random[4] | offline_serial[2] | tx_serial[2], purchase_key)[:8]` -- Computes MAC1: `DES-CBC-MAC(amount[4] | tx_type[1] | terminal[6] | date[4] | time[3], process_key)` +Passbook purchase requires a successful PIN verify beforehand; wallet purchase does not. -**Phase 2** (`INS 54`, P1=01, P2=00): send tx serial, date, time, MAC1. Card returns TAC[4]+MAC2[4]. -- Verifies TAC: `DES-CBC-MAC(amount[4] | tx_type[1] | terminal[6] | serial[4] | date[4] | time[3], tac_key)` +**Phase 1 -- INITIALIZE FOR PURCHASE** (`INS 50`, P1=01): -Transaction type byte: 0x05 for passbook purchase, 0x06 for wallet purchase. +Initiates the purchase transaction and returns the card state needed for the terminal to derive +the session key and compute the authorization MAC. +The terminal sends the purchase key slot ID, the debit amount (4 bytes), and the terminal ID (6 bytes). +The card responds with 15 bytes: + +| Field | Length | Description | +|-------|--------|-------------| +| Old balance | 4 bytes | Current balance before the debit | +| Offline serial number | 2 bytes | Incremented by the card after each successful purchase | +| Overdraft limit | 3 bytes | Maximum permitted overdraft on this file | +| Key version | 1 byte | Version of the purchase key identified by the key slot ID | +| Algorithm ID | 1 byte | Algorithm of that purchase key | +| Random seed | 4 bytes | Card-generated pseudorandom number for session key derivation | + +Unlike the credit transaction, the card does not return a MAC1 in this phase. +Instead, the terminal uses the card's response to derive the session key and compute MAC1 locally: + +- Session key (process key): `encrypt(random[4] | offline_serial[2] | tx_serial_low2[2], purchase_key)[:8]`, + where `tx_serial_low2` is the rightmost 2 bytes of the terminal transaction serial number. +- MAC1 (computed by terminal): `DES-CBC-MAC(amount[4] | type[1] | terminal[6] | date[4] | time[3], process_key)`. + +If the card returns a non-9000 status the transaction is aborted and the balance is unchanged. + +**Phase 2 -- DEBIT FOR PURCHASE** (`INS 54`, P1=01, P2=00): + +Authorizes and commits the balance deduction. +The terminal sends the terminal transaction serial (4 bytes), date (4 bytes), time (3 bytes), and MAC1 (4 bytes). +The card verifies MAC1, then deducts the amount and returns 8 bytes: + +| Field | Length | Description | +|-------|--------|-------------| +| TAC | 4 bytes | Transaction Authentication Code for terminal verification | +| MAC2 | 4 bytes | Card-computed MAC over the debit amount | + +TAC is: `DES-CBC-MAC(amount[4] | type[1] | terminal[6] | tx_serial[4] | date[4] | time[3], dtk_xor)`, +where `dtk_xor` is the left 8 bytes XOR right 8 bytes of the internal key (DTK). +MAC2 is: `DES-CBC-MAC(amount[4], process_key)`. + +On success the card: +- Deducts the purchase amount from the balance. +- Increments the offline serial number by 1. + +Transaction type byte: `0x05` for passbook purchase, `0x06` for wallet purchase. ``` # Wallet purchase of 50 units @@ -710,20 +809,58 @@ Example output: ### overdraft -UPDATE OVERDRAFT LIMIT -- two-phase overdraft-limit update on the passbook. +UPDATE OVERDRAFT LIMIT -- two-phase online overdraft-limit update on the passbook +(PBOC INITIALIZE FOR UPDATE + UPDATE OVERDRAW LIMIT). -**Phase 1** (`INS 50`, P1=04, P2=01): send key ID and terminal. Card returns old balance, -online serial, old limit, key version, algo, random seed, and MAC1 (19 bytes total). -- Derives process key: `encrypt(random[4] | serial[2], overdraft_key)[:8]` -- Verifies MAC1: `DES-CBC-MAC(old_bal[4] | old_limit[3] | 0x07[1] | terminal[6], process_key)` +The overdraft limit allows transactions to continue when the actual passbook funds are +insufficient, up to the issuer-permitted limit. +This transaction must be performed online at a financial terminal and requires a successful +PIN verify beforehand. It applies to passbook only; wallet files do not carry an overdraft limit. -**Phase 2** (`INS 58`, P1=00, P2=00): send new limit (3 bytes), date, time, MAC2. -- Computes MAC2: `DES-CBC-MAC(new_limit[3] | 0x07[1] | terminal[6] | date[4] | time[3], process_key)` -- Card returns TAC[4]. When `--ikey` is provided the TAC is verified: - `DES-CBC-MAC(XOR(ikey[0:8], ikey[8:16]), tac_bal[4] | online_serial[2] | new_limit[3] | 0x07[1] | terminal[6] | date[4] | time[3])` - where `tac_bal = old_balance + new_limit - old_od_limit`. The card stores - `actual_funds + overdraft_limit` as its balance field, so when the limit changes the new - stored balance shifts by the limit delta. +**Phase 1 -- INITIALIZE FOR UPDATE** (`INS 50`, P1=04, P2=01): + +Initiates the overdraft-limit update and authenticates the card to the terminal. +The terminal sends the overdraft key slot ID (1 byte) and the terminal ID (6 bytes). +Note that no amount is sent in this phase; the new limit is supplied in Phase 2. +The card responds with 19 bytes: + +| Field | Length | Description | +|-------|--------|-------------| +| Old balance | 4 bytes | Current stored balance (actual funds + current overdraft limit) | +| Online serial number | 2 bytes | Incremented by the card after each successful online transaction | +| Old overdraft limit | 3 bytes | Current overdraft limit before the update | +| Key version | 1 byte | Version of the overdraft key identified by the key slot ID | +| Algorithm ID | 1 byte | Algorithm of that overdraft key | +| Random seed | 4 bytes | Card-generated pseudorandom number for session key derivation | +| MAC1 | 4 bytes | Card-computed MAC proving it holds the overdraft key | + +Session key (process key): `encrypt(random[4] | serial[2] | 0x8000, overdraft_key)[:8]`. +MAC1 is: `DES-CBC-MAC(old_bal[4] | old_limit[3] | 0x07[1] | terminal[6], process_key)`, +where `0x07` is the fixed transaction type identifier for overdraft limit updates. +The terminal verifies MAC1 to confirm the card holds the correct key before proceeding. +If MAC1 does not match, the transaction is aborted and the limit is unchanged. + +**Phase 2 -- UPDATE OVERDRAW LIMIT** (`INS 58`, P1=00, P2=00): + +Authorizes and commits the new overdraft limit. +The terminal sends the new limit (3 bytes), host transaction date (4 bytes), time (3 bytes), +and MAC2 (4 bytes). +MAC2 is: `DES-CBC-MAC(new_limit[3] | 0x07[1] | terminal[6] | date[4] | time[3], process_key)`. + +The card responds with TAC (4 bytes). +TAC is: `DES-CBC-MAC(tac_bal[4] | serial[2] | new_limit[3] | 0x07[1] | terminal[6] | date[4] | time[3], dtk_xor)`, +where `dtk_xor` is the left 8 bytes XOR right 8 bytes of the internal key (DTK), and +`tac_bal = old_balance + new_limit - old_overdraft_limit` (the new stored balance after the limit shift). + +The card stores `actual_funds + overdraft_limit` as its balance field, so changing the limit +by a delta shifts the stored balance value by the same delta without altering actual funds. + +On success the card: +- Updates the overdraft limit to the new value. +- Adjusts the stored balance to `actual_funds + new_limit`. +- Increments the online serial number by 1. +- Appends a 23-byte transaction record (serial, new limit, amount, type `0x07`, terminal, date, time) + to the linked loop EF for auditing. ``` hf fmcos overdraft --id 01 --limit 1000 \ @@ -746,9 +883,9 @@ Example output: ``` [=] Old balance: 1000 old overdraft limit: 0 [=] MAC1 OK +[+] SW: 9000 - Success [+] Overdraft limit updated to 1000 [+] TAC OK aabbccdd -[+] SW: 9000 - Success ``` ### history