From 971c318d78e9d793f6246c42efc6cf92ef6ede93 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 12 Sep 2026 23:54:39 +0200 Subject: [PATCH] smart raw: make --t1 switch the card to T=1 by itself A card runs the protocol its TD1 names until a PPS changes it. --t1 only picked the module's T=1 send opcode, so on a card whose ATR offers T=0 first the APDU went out as T=1 blocks to a card still speaking T=0 and got no answer at all: smart raw --t1 -s -d 00a4040007a000000004101000 [!] smart card response failed 'smart pps --t1' in the same client session made it work, which is what the help for 'smart pps' already promised was unnecessary: Note 'smart raw --t1' already switches a card to T=1 by itself when the ATR offers it; this is for negotiating Fi/Di explicitly. Do it for real. SmartCardRaw() now runs the PPS when T=1 is asked for and nothing has selected it yet, taking the ATR it needs first because PPS is only legal in that window. It fires once per activation: a second --t1 apdu sees the protocol already in force and sends straight away. A card that does not offer T=1 is left alone and the apdu still goes out, as before. Also fix the flag names in the docs and in the error path. 'smart raw's first example and both 'Choose either' messages said -0 / -1, which have never existed. tools/pm3_online_tests.sh had copied the example, so its T=0 checks failed with 'invalid option' on every run; its T=1 checks needed no change beyond dropping the now unnecessary reset. Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 2 ++ armsrc/i2c.c | 19 +++++++++++++++++++ client/src/cmdsmartcard.c | 14 +++++++------- tools/pm3_online_tests.sh | 8 ++++---- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eb13dad8..5161247cf 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] +- Fixed `smart raw --t1` - a card runs the protocol its ATR names until a PPS changes it (@iceman1001) +- Fixed `tools/pm3_online_tests.sh smartcard` - the T=0 checks used the non existent `-0` flag (@iceman1001) - Fixed `hf mfdes dump` - the ISO file id probe ends the PICC session on applications without ISO file ids (@iceman1001) - Changed `hf mfdes lsfiles` / `hf mfdes dump` - no longer warn about a missing ISO file id list (@iceman1001) - Fixed `hf mfdes read` / `hf mfdes write` - a file whose access right is free (0x0E) is served in plain by the PICC (@iceman1001) diff --git a/armsrc/i2c.c b/armsrc/i2c.c index edb4a3487..d33533b12 100644 --- a/armsrc/i2c.c +++ b/armsrc/i2c.c @@ -1388,6 +1388,25 @@ void SmartCardRaw(const smart_card_raw_t *p) { } } + // A card runs the protocol its TD1 names until a PPS changes it, so an APDU + // framed as T=1 against a card that came up in T=0 gets no answer at all. + // Do the switch here rather than making every caller run `smart pps --t1` + // first. PPS is only legal straight after an ATR, so take one; the exchange + // could not have worked without the switch anyway, so nothing is lost. + if (((flags & SC_RAW_T1) == SC_RAW_T1) && (s_pps_proto_cmd != I2C_DEVICE_CMD_SEND_T1)) { + + smart_card_atr_t card; + s_pps.reapply = false; // this is the negotiation + bool got_atr = GetATR(&card, false); + s_pps.reapply = true; + + if (got_atr && ((s_card_protocols & SC_PROTO_T1) == SC_PROTO_T1)) { + if ((sc_pps(1, 0x11) == false) && (g_dbglevel >= DBG_ERROR)) { + DbpString("SC: " _YELLOW_("PPS to T=1 refused") ", sending it anyway"); + } + } + } + if (((flags & SC_RAW) == SC_RAW) || ((flags & SC_RAW_T0) == SC_RAW_T0) || ((flags & SC_RAW_T1) == SC_RAW_T1)) { diff --git a/client/src/cmdsmartcard.c b/client/src/cmdsmartcard.c index a0faf1533..97cdc4f23 100644 --- a/client/src/cmdsmartcard.c +++ b/client/src/cmdsmartcard.c @@ -475,12 +475,12 @@ static int CmdSmartRaw(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "smart raw", "Sends raw bytes to card", - "smart raw -s -0 -d 00a404000e315041592e5359532e4444463031 -> `1PAY.SYS.DDF01` PPSE directory with get ATR\n" - "smart raw --t0 -d 00a404000e325041592e5359532e4444463031 -> `2PAY.SYS.DDF01` PPSE directory\n" - "smart raw --t0 -t -d 00a4040007a0000000041010 -> Mastercard\n" - "smart raw --t0 -t -d 00a4040007a0000000031010 -> Visa\n" + "smart raw --t0 -s -d 00a404000e315041592e5359532e4444463031 -> `1PAY.SYS.DDF01` PPSE directory with get ATR\n" + "smart raw --t0 -d 00a404000e325041592e5359532e4444463031 -> `2PAY.SYS.DDF01` PPSE directory\n" + "smart raw --t0 -t -d 00a4040007a0000000041010 -> Mastercard\n" + "smart raw --t0 -t -d 00a4040007a0000000031010 -> Visa\n" "smart raw --t1 -s -d 00a404000e325041592e5359532e444446303100 -> PPSE over T=1\n" - " (T=1 carries the whole APDU, so case 4 needs its Le)" + " (T=1 carries the whole APDU, so case 4 needs its Le)" ); void *argtable[] = { @@ -516,7 +516,7 @@ static int CmdSmartRaw(const char *Cmd) { } if (use_t0 && use_t1) { - PrintAndLogEx(FAILED, "Choose either -0 or -1, not both"); + PrintAndLogEx(FAILED, "Choose either --t0 or --t1, not both"); return PM3_EINVARG; } @@ -968,7 +968,7 @@ static int CmdSmartPPS(const char *Cmd) { } if (use_t0 && use_t1) { - PrintAndLogEx(FAILED, "Choose either -0 or -1, not both"); + PrintAndLogEx(FAILED, "Choose either --t0 or --t1, not both"); return PM3_EINVARG; } diff --git a/tools/pm3_online_tests.sh b/tools/pm3_online_tests.sh index 780903cf2..c71d28706 100755 --- a/tools/pm3_online_tests.sh +++ b/tools/pm3_online_tests.sh @@ -559,8 +559,8 @@ while true; do if ! CheckExecute "ATR stable over 5 reads" "for i in 1 2 3 4 5; do $PM3CMD -c 'smart info' 2>&1 | grep -oE 'ISO7816-3 ATR\.+ [0-9A-F ]+'; done | sort -u | wc -l" "^ *1$"; then break; fi # any status word will do - the point is that the exchange completed - if ! CheckExecute "T=0 apdu gets a status word" "$PM3CMD -c 'smart raw -a -s -0 -d 00a4040007a0000000041010' 2>&1" "\[[+-]\] [0-9A-Fa-f]{4} \|"; then break; fi - if ! CheckExecute "T=0 answer stable over 3" "for i in 1 2 3; do $PM3CMD -c 'smart raw -a -s -0 -d 00a4040007a0000000041010' 2>&1 | grep -oE '^\[[+-]\] [0-9A-Fa-f]{4}'; done | sort -u | wc -l" "^ *1$"; then break; fi + if ! CheckExecute "T=0 apdu gets a status word" "$PM3CMD -c 'smart raw --t0 -a -s -d 00a4040007a0000000041010' 2>&1" "\[[+-]\] [0-9A-Fa-f]{4} \|"; then break; fi + if ! CheckExecute "T=0 answer stable over 3" "for i in 1 2 3; do $PM3CMD -c 'smart raw --t0 -a -s -d 00a4040007a0000000041010' 2>&1 | grep -oE '^\[[+-]\] [0-9A-Fa-f]{4}'; done | sort -u | wc -l" "^ *1$"; then break; fi # loose guard against the 1200 ms per-read wait coming back echo -n " timing three exchanges... " @@ -574,8 +574,8 @@ while true; do if $PM3CMD -c 'smart info' 2>&1 | grep -q "Protocol T1"; then echo " card offers T=1" if ! CheckExecute "module reports T=1 support" "$PM3CMD -c 'hw status' 2>&1" "T=1, PPS\.+ \( .*supported"; then break; fi - if ! CheckExecute "T=1 apdu gets a status word" "$PM3CMD -c 'smart raw --t1 -s -d 00a4040007a000000004101000' 2>&1" "\[[+-]\] [0-9A-Fa-f]{4} \|"; then break; fi - if ! CheckExecute "T=1 answer stable over 3" "for i in 1 2 3; do $PM3CMD -c 'smart raw --t1 -s -d 00a4040007a000000004101000' 2>&1 | grep -oE '^\[[+-]\] [0-9A-Fa-f]{4}'; done | sort -u | wc -l" "^ *1$"; then break; fi + if ! CheckExecute "T=1 apdu gets a status word" "$PM3CMD -c 'smart raw --t1 -d 00a4040007a000000004101000' 2>&1" "\[[+-]\] [0-9A-Fa-f]{4} \|"; then break; fi + if ! CheckExecute "T=1 answer stable over 3" "for i in 1 2 3; do $PM3CMD -c 'smart raw --t1 -d 00a4040007a000000004101000' 2>&1 | grep -oE '^\[[+-]\] [0-9A-Fa-f]{4}'; done | sort -u | wc -l" "^ *1$"; then break; fi else echo " card is T=0 only, skipping the T=1 checks" fi