diff --git a/CHANGELOG.md b/CHANGELOG.md index fcde36a14..2f28fbe9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Changed `emv search` - now has better table output (@iceman1001) +- Changed client side polling. Should make client more spiffy (@iceman1001) - Fixed `trace list -t 7816` - now handles contact vs contactless annotation better (@iceman1001) - Added `smart pps` - ISO 7816-3 protocol and parameter selection exchange (@iceman1001) - Added `sim015.bin` - sim modul firmware now supports T=0 and T=1 (@iceman1001) diff --git a/client/src/emv/emvcore.c b/client/src/emv/emvcore.c index 51f1e9f8a..985c3b769 100644 --- a/client/src/emv/emvcore.c +++ b/client/src/emv/emvcore.c @@ -187,31 +187,124 @@ void TLVPrintFromTLV(struct tlvdb *tlv) { TLVPrintFromTLVLev(tlv, 0); } +#define AIDLIST_MAX_ROWS 32 +#define AIDLIST_MAX_CELL 66 + +typedef struct { + char aid[AIDLIST_MAX_CELL]; + char prio[8]; + char name[AIDLIST_MAX_CELL]; +} aidlist_row_t; + +// A rule of '-' the width of one padded column, plus its trailing '+'. +static void aidlist_rule(char *dst, size_t dstlen, size_t w_aid, size_t w_prio, size_t w_name) { + const size_t widths[3] = { w_aid, w_prio, w_name }; + size_t pos = 0; + + if (dstlen == 0) { + return; + } + + dst[pos++] = '+'; + for (size_t c = 0; c < 3; c++) { + for (size_t i = 0; (i < widths[c] + 2) && (pos + 2 < dstlen); i++) { + dst[pos++] = '-'; + } + if (pos + 1 < dstlen) { + dst[pos++] = '+'; + } + } + dst[pos] = 0; +} + void TLVPrintAIDlistFromSelectTLV(struct tlvdb *tlv) { - PrintAndLogEx(INFO, "|------------------+--------+-------------------------|"); - PrintAndLogEx(INFO, "| AID |Priority| Name |"); - PrintAndLogEx(INFO, "|------------------+--------+-------------------------|"); + static aidlist_row_t rows[AIDLIST_MAX_ROWS]; + size_t n = 0; + bool truncated = false; + + // Collect first: the column widths come from the data, and the sprint_* + // helpers hand back a shared static buffer that the next call overwrites. struct tlvdb *ttmp = tlvdb_find(tlv, 0x6f); - if (ttmp == NULL) - PrintAndLogEx(INFO, "| none |"); - while (ttmp) { + const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x84, NULL); const struct tlv *tgName = tlvdb_get_inchild(ttmp, 0x50, NULL); const struct tlv *tgPrio = tlvdb_get_inchild(ttmp, 0x87, NULL); - if (!tgAID) { + if (tgAID == NULL) { break; } - PrintAndLogEx(INFO, "| %s| %s | %s|", - sprint_hex_inrow_ex(tgAID->value, tgAID->len, 16), - (tgPrio) ? sprint_hex(tgPrio->value, 1) : " ", - (tgName) ? sprint_ascii_ex(tgName->value, tgName->len, 24) : " "); + if (n >= AIDLIST_MAX_ROWS) { + truncated = true; + break; + } + + snprintf(rows[n].aid, sizeof(rows[n].aid), "%s", + sprint_hex_inrow_ex(tgAID->value, tgAID->len, 0)); + + if (tgPrio && tgPrio->len) { + snprintf(rows[n].prio, sizeof(rows[n].prio), "%02X", tgPrio->value[0]); + } else { + snprintf(rows[n].prio, sizeof(rows[n].prio), "-"); + } + + rows[n].name[0] = 0; + if (tgName && tgName->len) { + size_t len = tgName->len; + if (len > sizeof(rows[n].name) - 1) { + len = sizeof(rows[n].name) - 1; + } + for (size_t i = 0; i < len; i++) { + uint8_t c = tgName->value[i]; + rows[n].name[i] = (c >= 0x20 && c < 0x7F) ? (char)c : '.'; + } + rows[n].name[len] = 0; + } + + n++; ttmp = tlvdb_find_next(ttmp, 0x6f); } - PrintAndLogEx(INFO, "|------------------+--------+-------------------------|"); + size_t w_aid = strlen("AID"); + size_t w_prio = strlen("Priority"); + size_t w_name = strlen("Name"); + + for (size_t i = 0; i < n; i++) { + size_t l = strlen(rows[i].aid); + if (l > w_aid) { + w_aid = l; + } + l = strlen(rows[i].name); + if (l > w_name) { + w_name = l; + } + } + + char rule[AIDLIST_MAX_CELL * 3 + 16]; + aidlist_rule(rule, sizeof(rule), w_aid, w_prio, w_name); + + PrintAndLogEx(INFO, "%s", rule); + PrintAndLogEx(INFO, "| %-*s | %-*s | %-*s |", + (int)w_aid, "AID", (int)w_prio, "Priority", (int)w_name, "Name"); + PrintAndLogEx(INFO, "%s", rule); + + if (n == 0) { + PrintAndLogEx(INFO, "| %-*s |", (int)(w_aid + w_prio + w_name + 6), "none"); + } + + for (size_t i = 0; i < n; i++) { + PrintAndLogEx(INFO, "| " _YELLOW_("%-*s") " | %-*s | " _CYAN_("%-*s") " |", + (int)w_aid, rows[i].aid, + (int)w_prio, rows[i].prio, + (int)w_name, rows[i].name); + } + + PrintAndLogEx(INFO, "%s", rule); + + if (truncated) { + PrintAndLogEx(INFO, "( showing first %d applications )", AIDLIST_MAX_ROWS); + } } struct tlvdb *GetPANFromTrack2(const struct tlv *track2) {