From 5f05cbf576977f87b0819a0cc213f1295274ae01 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Wed, 26 Aug 2026 20:44:34 +0200 Subject: [PATCH] the annotations for 7816 handles contact vs contactless situations better now. Thanks Claude --- CHANGELOG.md | 1 + client/src/cmdhflist.c | 98 ++++++++++++++++++++++++++++++++++-------- client/src/cmdhflist.h | 2 +- client/src/cmdtrace.c | 13 +++++- 4 files changed, 95 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54f86abd5..fcde36a14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ 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] +- 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) - Added tools script `tools/ePassport/ePassport` - kivy python3 script that dumps a passport with BAC/CAN and a nice viewer (@iceman1001) diff --git a/client/src/cmdhflist.c b/client/src/cmdhflist.c index c2ff68501..6711c2a5a 100644 --- a/client/src/cmdhflist.c +++ b/client/src/cmdhflist.c @@ -817,7 +817,19 @@ void annotateTopaz(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { } // iso 7816-3 -void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response) { +// +// `contact` tells the two framings that reach this decoder apart: +// +// - contactless (ISO 14443-4 / T=CL): every frame is a block, its type is in +// the first byte, and the length comes from the transport layer. +// - contact (the SIM module): a T=0 frame is a bare APDU with no block layer +// at all, and a T=1 frame is NAD PCB LEN INF[LEN] EDC, so its length is +// fixed by LEN. +// +// Without that distinction a GSM APDU beginning with CLA 'A0' satisfies the +// T=CL R-block test - 0xA0 & 0xD0 == 0x80 - and "A0 A4 00 00 02 3F 00" gets +// annotated as "R-block ACK". +void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response, bool contact) { if (cmdsize < 2) { return; @@ -827,8 +839,51 @@ void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool return; } - // S-block - if ((cmd[0] & 0xC0) && ((cmdsize == 3) || (cmdsize == 4))) { + // A contact frame is either a bare T=0 APDU or a T=1 block, and the two + // framings put things in different places: T=1 is NAD PCB LEN INF[LEN] EDC, + // so the block type is in cmd[1], not cmd[0] the way T=CL has it. + // + // Only the one byte LRC is considered when matching the length. A five + // byte APDU whose P1 is zero - "A0 C0 00 00 22" - is indistinguishable from + // a LEN=0 block with a two byte CRC, and CRC EDC is vanishingly rare. + // Requiring NAD 0x00 rules out the rest; every card and this SIM module use + // it exclusively. + if (contact) { + + if ((cmdsize >= 4) && (cmd[0] == 0x00) && ((int)cmdsize == (int)cmd[2] + 4)) { + + uint8_t pcb = cmd[1]; + + if ((pcb & 0x80) == 0x00) { + snprintf(exp, size, "I-block N(S)=%u%s", (pcb >> 6) & 1, + (pcb & 0x20) ? " chained" : ""); + } else if ((pcb & 0xC0) == 0x80) { + snprintf(exp, size, "R-block N(R)=%u%s", (pcb >> 4) & 1, + (pcb & 0x0F) ? " error" : ""); + } else { + switch (pcb & 0x3F) { + case 0x00: snprintf(exp, size, "S-block RESYNCH req"); break; + case 0x20: snprintf(exp, size, "S-block RESYNCH resp"); break; + case 0x01: snprintf(exp, size, "S-block IFS req"); break; + case 0x21: snprintf(exp, size, "S-block IFS resp"); break; + case 0x02: snprintf(exp, size, "S-block ABORT req"); break; + case 0x22: snprintf(exp, size, "S-block ABORT resp"); break; + case 0x03: snprintf(exp, size, "S-block WTX req"); break; + case 0x23: snprintf(exp, size, "S-block WTX resp"); break; + default: snprintf(exp, size, "S-block"); break; + } + } + return; + } + // not a block, so it is a bare APDU - decoded below with pos = 1 + } + + bool blocks = (contact == false); + + // S-block. The test used to be a plain truthiness check on cmd[0] & 0xC0, + // so any first byte from 0x40 up landed here - an R-block 0xA2 in a 3 byte + // frame was reported as an S-block. + if (blocks && ((cmd[0] & 0xC0) == 0xC0) && ((cmdsize == 3) || (cmdsize == 4))) { switch ((cmd[0] & 0x3F)) { case 0x00 : @@ -864,27 +919,36 @@ void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool } } // R-block (ack) - else if (((cmd[0] & 0xD0) == 0x80) && (cmdsize > 2)) { + else if (blocks && ((cmd[0] & 0xD0) == 0x80) && (cmdsize > 2)) { if ((cmd[0] & 0x10) == 0) snprintf(exp, size, "R-block ACK"); else snprintf(exp, size, "R-block NACK"); } - // I-block + // I-block, or a bare APDU when there is no block layer else { int pos = 0; - switch (cmd[0]) { - case 2: - case 3: - pos = 2; - break; - case 0: - pos = 1; - break; - default: - pos = 3; - break; + if (blocks == false) { + // CLA INS P1 P2 ... + pos = 1; + } else { + switch (cmd[0]) { + case 2: + case 3: + pos = 2; + break; + case 0: + pos = 1; + break; + default: + pos = 3; + break; + } + } + + if (pos >= cmdsize) { + return; } switch (cmd[pos]) { @@ -2223,7 +2287,7 @@ void annotateSeos(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is // it's basically a ISO14443a tag, so try annotation from there if (applyIso14443a(exp, size, cmd, cmdsize, false) != PM3_SUCCESS) { - annotateIso7816(exp, size, cmd, cmdsize, isResponse); + annotateIso7816(exp, size, cmd, cmdsize, isResponse, false); int pos = 0; switch (cmd[0]) { diff --git a/client/src/cmdhflist.h b/client/src/cmdhflist.h index a32305504..7a41de402 100644 --- a/client/src/cmdhflist.h +++ b/client/src/cmdhflist.h @@ -55,7 +55,7 @@ void annotateIso15693(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize); void annotateTopaz(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize); void annotateLegic(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize); void annotateFelica(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response); -void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response); +void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response, bool contact); void annotateCalypso(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response); void annotateIso14443b(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response); void annotateIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response); diff --git a/client/src/cmdtrace.c b/client/src/cmdtrace.c index a2552277c..9e23d0f66 100644 --- a/client/src/cmdtrace.c +++ b/client/src/cmdtrace.c @@ -577,6 +577,16 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr crcStatus = seos_CRC_check(hdr->isResponse, frame, data_len); break; case ISO_7816_4: + // A contact ISO 7816-3 frame carries no trailing CRC. T=0 has + // no frame checksum at all, and T=1 keeps its EDC inside the + // block - so running the ISO 14443 A/B CRC checks over one only + // ever produced a red "!!" on every line. + // + // The T=1 LRC is not verified here either: TRACE_CRC_OK/FAIL + // make the renderer below highlight the last *two* bytes as the + // checksum, which would be wrong for a one byte LRC. + crcStatus = TRACE_CRC_NONE; + break; case PROTO_CALYPSO: { uint8_t crcA = iso14443A_CRC_check(hdr->isResponse, frame, data_len); uint8_t crcB = iso14443B_CRC_check(frame, data_len); @@ -870,7 +880,8 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr annotateTopaz(explanation, sizeof(explanation), frame, data_len); break; case ISO_7816_4: - annotateIso7816(explanation, sizeof(explanation), frame, data_len, hdr->isResponse); + // trace type "7816" is the contact reader, i.e. `smart list` + annotateIso7816(explanation, sizeof(explanation), frame, data_len, hdr->isResponse, true); break; case ISO_15693: annotateIso15693(explanation, sizeof(explanation), frame, data_len);