From a76399c03025d3c4e297e1f076e519841a0475bd Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Wed, 26 Aug 2026 23:58:48 +0200 Subject: [PATCH] unify --t0 --t1 selection of T=0, T=1 parameters --- client/src/emv/cmdemv.c | 189 ++++++++++++++++++++++++++++++++++------ 1 file changed, 162 insertions(+), 27 deletions(-) diff --git a/client/src/emv/cmdemv.c b/client/src/emv/cmdemv.c index 9792bacdd..8452a5914 100644 --- a/client/src/emv/cmdemv.c +++ b/client/src/emv/cmdemv.c @@ -575,6 +575,32 @@ static int emv_parse_card_details(uint8_t *response, size_t reslen, bool verbose return PM3_SUCCESS; } +/* + * Resolve --t0 / --t1 for the contact interface. + * + * The ARM already switches a card whose ATR offers no T=0 over to T=1 by + * itself, since a T=0 request to it could never have worked. This is for the + * other case: a card offering both, where T=0 is a legitimate choice and the + * caller wants T=1 anyway. + * + * Reports the problem and returns non-success if the flags make no sense. + */ +static int emv_set_protocol(bool use_t0, bool use_t1, Iso7816CommandChannel channel) { + + if (use_t0 && use_t1) { + PrintAndLogEx(FAILED, "Choose either --t0 or --t1, not both"); + return PM3_EINVARG; + } + + if ((use_t0 || use_t1) && (channel != CC_CONTACT)) { + PrintAndLogEx(FAILED, "--t0 and --t1 only apply to the contact interface, add -w"); + return PM3_EINVARG; + } + + SetSmartcardProtocolT1(use_t1); + return PM3_SUCCESS; +} + static int CmdEMVSelect(const char *Cmd) { uint8_t data[APDU_AID_LEN] = {0}; int datalen = 0; @@ -593,6 +619,8 @@ static int CmdEMVSelect(const char *Cmd) { arg_lit0("t", "tlv", "TLV decode results"), arg_lit0("w", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"), arg_str1(NULL, NULL, "", "Applet AID"), + arg_lit0(NULL, "t0", "use protocol T=0 on the contact interface (default)"), + arg_lit0(NULL, "t1", "use protocol T=1 on the contact interface"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -606,8 +634,14 @@ static int CmdEMVSelect(const char *Cmd) { channel = CC_CONTACT; PrintChannel(channel); CLIGetHexWithReturn(ctx, 6, data, &datalen); + bool use_t0 = arg_get_lit(ctx, 7); + bool use_t1 = arg_get_lit(ctx, 8); CLIParserFree(ctx); + if (emv_set_protocol(use_t0, use_t1, channel) != PM3_SUCCESS) { + return PM3_EINVARG; + } + SetAPDULogging(show_apdu); // exec @@ -619,8 +653,25 @@ static int CmdEMVSelect(const char *Cmd) { if (sw) PrintAndLogEx(INFO, "APDU response status: %04x - %s", sw, GetAPDUCodeDescription(sw >> 8, sw & 0xff)); - if (res) + if (res) { + // No status word means the card said nothing at all, as opposed to + // answering with an error - worth distinguishing, and worth saying: + // this used to return silently and look like the command had worked. + if (sw == 0) { + PrintAndLogEx(FAILED, "No answer from card ( %d )", res); + if (channel == CC_CONTACT) { + // Point the other way when T=1 was forced - suggesting the flag + // that is already in use is worse than saying nothing. + if (GetSmartcardProtocolT1()) { + PrintAndLogEx(HINT, "Hint: card was driven as T=1, drop `" _YELLOW_("--t1") "` to use T=0"); + } else { + PrintAndLogEx(HINT, "Hint: is the card T=1? try `" _YELLOW_("--t1") "`"); + } + } + } + SetAPDULogging(false); return res; + } if (decodeTLV) TLVPrintFromBuffer(buf, len); @@ -727,6 +778,8 @@ static int CmdEMVSearch(const char *Cmd) { arg_lit0("a", "apdu", "Show APDU requests and responses"), arg_lit0("t", "tlv", "TLV decode results of selected applets"), arg_lit0("w", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"), + arg_lit0(NULL, "t0", "use protocol T=0 on the contact interface (default)"), + arg_lit0(NULL, "t1", "use protocol T=1 on the contact interface"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -742,8 +795,14 @@ static int CmdEMVSearch(const char *Cmd) { } PrintChannel(channel); + bool use_t0 = arg_get_lit(ctx, 6); + bool use_t1 = arg_get_lit(ctx, 7); CLIParserFree(ctx); + if (emv_set_protocol(use_t0, use_t1, channel) != PM3_SUCCESS) { + return PM3_EINVARG; + } + SetAPDULogging(show_apdu); const char *al = "Applets list"; @@ -785,6 +844,8 @@ static int CmdEMVPPSE(const char *Cmd) { arg_lit0("a", "apdu", "Show APDU requests and responses"), arg_lit0("t", "tlv", "TLV decode results of selected applets"), arg_lit0("w", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"), + arg_lit0(NULL, "t0", "use protocol T=0 on the contact interface (default)"), + arg_lit0(NULL, "t1", "use protocol T=1 on the contact interface"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -806,8 +867,14 @@ static int CmdEMVPPSE(const char *Cmd) { channel = CC_CONTACT; } PrintChannel(channel); + bool use_t0 = arg_get_lit(ctx, 8); + bool use_t1 = arg_get_lit(ctx, 9); CLIParserFree(ctx); + if (emv_set_protocol(use_t0, use_t1, channel) != PM3_SUCCESS) { + return PM3_EINVARG; + } + SetAPDULogging(show_apdu); // exec @@ -850,6 +917,8 @@ static int CmdEMVGPO(const char *Cmd) { arg_lit0("t", "tlv", "TLV decode results of selected applets"), arg_lit0("w", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"), arg_strx0(NULL, NULL, "", "PDOLdata/PDOL"), + arg_lit0(NULL, "t0", "use protocol T=0 on the contact interface (default)"), + arg_lit0(NULL, "t1", "use protocol T=1 on the contact interface"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -865,8 +934,14 @@ static int CmdEMVGPO(const char *Cmd) { } PrintChannel(channel); CLIGetHexWithReturn(ctx, 7, data, &datalen); + bool use_t0 = arg_get_lit(ctx, 8); + bool use_t1 = arg_get_lit(ctx, 9); CLIParserFree(ctx); + if (emv_set_protocol(use_t0, use_t1, channel) != PM3_SUCCESS) { + return PM3_EINVARG; + } + SetAPDULogging(show_apdu); // Init TLV tree @@ -962,6 +1037,8 @@ static int CmdEMVReadRecord(const char *Cmd) { arg_lit0("t", "tlv", "TLV decode results of selected applets"), arg_lit0("w", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"), arg_strx1(NULL, NULL, "", "", "CDOLdata/CDOL"), + arg_lit0(NULL, "t0", "use protocol T=0 on the contact interface (default)"), + arg_lit0(NULL, "t1", "use protocol T=1 on the contact interface"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); @@ -1070,8 +1155,14 @@ static int CmdEMVAC(const char *Cmd) { PrintChannel(channel); CLIGetHexWithReturn(ctx, 9, data, &datalen); + bool use_t0 = arg_get_lit(ctx, 10); + bool use_t1 = arg_get_lit(ctx, 11); CLIParserFree(ctx); + if (emv_set_protocol(use_t0, use_t1, channel) != PM3_SUCCESS) { + return PM3_EINVARG; + } + SetAPDULogging(show_apdu); // Init TLV tree @@ -1152,6 +1243,8 @@ static int CmdEMVGenerateChallenge(const char *Cmd) { arg_lit0("k", "keep", "Keep field ON for next command"), arg_lit0("a", "apdu", "Show APDU requests and responses"), arg_lit0("w", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"), + arg_lit0(NULL, "t0", "use protocol T=0 on the contact interface (default)"), + arg_lit0(NULL, "t1", "use protocol T=1 on the contact interface"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -1163,8 +1256,14 @@ static int CmdEMVGenerateChallenge(const char *Cmd) { channel = CC_CONTACT; } PrintChannel(channel); + bool use_t0 = arg_get_lit(ctx, 4); + bool use_t1 = arg_get_lit(ctx, 5); CLIParserFree(ctx); + if (emv_set_protocol(use_t0, use_t1, channel) != PM3_SUCCESS) { + return PM3_EINVARG; + } + SetAPDULogging(show_apdu); // exec @@ -1211,6 +1310,8 @@ static int CmdEMVInternalAuthenticate(const char *Cmd) { arg_lit0("t", "tlv", "TLV decode results of selected applets"), arg_lit0("w", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"), arg_strx1(NULL, NULL, "", "DDOLdata/DDOL"), + arg_lit0(NULL, "t0", "use protocol T=0 on the contact interface (default)"), + arg_lit0(NULL, "t1", "use protocol T=1 on the contact interface"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); @@ -1226,8 +1327,14 @@ static int CmdEMVInternalAuthenticate(const char *Cmd) { } PrintChannel(channel); CLIGetHexWithReturn(ctx, 7, data, &datalen); + bool use_t0 = arg_get_lit(ctx, 8); + bool use_t1 = arg_get_lit(ctx, 9); CLIParserFree(ctx); + if (emv_set_protocol(use_t0, use_t1, channel) != PM3_SUCCESS) { + return PM3_EINVARG; + } + SetAPDULogging(show_apdu); // Init TLV tree @@ -1439,6 +1546,8 @@ static int CmdEMVExec(const char *Cmd) { arg_lit0("x", "vsdc", "Transaction type - VSDC. For test only. Not a standard behavior"), arg_lit0("g", "acgpo", "VISA. generate AC from GPO"), arg_lit0("w", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"), + arg_lit0(NULL, "t0", "use protocol T=0 on the contact interface (default)"), + arg_lit0(NULL, "t1", "use protocol T=1 on the contact interface"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -1472,8 +1581,14 @@ static int CmdEMVExec(const char *Cmd) { PrintChannel(channel); uint8_t psenum = (channel == CC_CONTACT) ? 1 : 2; + bool use_t0 = arg_get_lit(ctx, 12); + bool use_t1 = arg_get_lit(ctx, 13); CLIParserFree(ctx); + if (emv_set_protocol(use_t0, use_t1, channel) != PM3_SUCCESS) { + return PM3_EINVARG; + } + if (IfPm3Smartcard() == false) { if (channel == CC_CONTACT) { PrintAndLogEx(WARNING, "PM3 does not have SMARTCARD support. Exiting."); @@ -1508,12 +1623,12 @@ static int CmdEMVExec(const char *Cmd) { PrintAndLogEx(NORMAL, ""); PrintAndLogEx(INFO, "* PPSE."); SetAPDULogging(show_apdu); - res = EMVSearchPSE(channel, activateField, true, psenum, decodeTLV, tlvSelect); + res = EMVSearchPSE(channel, activateField, true, psenum, decodeTLV, tlvSelect, true); // check PPSE instead of PSE and vice versa if (res) { PrintAndLogEx(INFO, "Check PPSE instead of PSE and vice versa..."); - res = EMVSearchPSE(channel, false, true, psenum == 1 ? 2 : 1, decodeTLV, tlvSelect); + res = EMVSearchPSE(channel, false, true, psenum == 1 ? 2 : 1, decodeTLV, tlvSelect, false); } // check PPSE and select application id @@ -2090,6 +2205,8 @@ static int CmdEMVScan(const char *Cmd) { arg_lit0("m", "merge", "Merge output file with card's data. (warning: the file may be corrupted!)"), arg_lit0("w", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"), arg_str1(NULL, NULL, "", "JSON output file name"), + arg_lit0(NULL, "t0", "use protocol T=0 on the contact interface (default)"), + arg_lit0(NULL, "t1", "use protocol T=1 on the contact interface"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -2126,8 +2243,14 @@ static int CmdEMVScan(const char *Cmd) { int filenamelen = sizeof(filename) - 1; // CLIGetStrWithReturn does not guarantee string to be null-terminated CLIGetStrWithReturn(ctx, 12, filename, &filenamelen); + bool use_t0 = arg_get_lit(ctx, 13); + bool use_t1 = arg_get_lit(ctx, 14); CLIParserFree(ctx); + if (emv_set_protocol(use_t0, use_t1, channel) != PM3_SUCCESS) { + return PM3_EINVARG; + } + if (IfPm3Smartcard() == false) { if (channel == CC_CONTACT) { PrintAndLogEx(WARNING, "PM3 does not have SMARTCARD support, exiting"); @@ -2222,7 +2345,7 @@ static int CmdEMVScan(const char *Cmd) { tlvdb_free(fci); } - res = EMVSearchPSE(channel, false, true, psenum, decodeTLV, tlvSelect); + res = EMVSearchPSE(channel, false, true, psenum, decodeTLV, tlvSelect, false); // check PPSE and select application id if (!res) { @@ -2510,6 +2633,8 @@ static int CmdEMVRoca(const char *Cmd) { arg_lit0(NULL, "test", "Perform self tests"), arg_lit0("a", "apdu", "Show APDU requests and responses"), arg_lit0("w", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"), + arg_lit0(NULL, "t0", "use protocol T=0 on the contact interface (default)"), + arg_lit0(NULL, "t1", "use protocol T=1 on the contact interface"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -2525,7 +2650,13 @@ static int CmdEMVRoca(const char *Cmd) { if (arg_get_lit(ctx, 3)) channel = CC_CONTACT; + bool use_t0 = arg_get_lit(ctx, 4); + bool use_t1 = arg_get_lit(ctx, 5); CLIParserFree(ctx); + + if (emv_set_protocol(use_t0, use_t1, channel) != PM3_SUCCESS) { + return PM3_EINVARG; + } PrintChannel(channel); if (IfPm3Smartcard() == false) { @@ -2555,7 +2686,7 @@ static int CmdEMVRoca(const char *Cmd) { // EMV PPSE PrintAndLogEx(INFO, "PPSE"); - res = EMVSearchPSE(channel, false, true, psenum, false, tlvSelect); + res = EMVSearchPSE(channel, false, true, psenum, false, tlvSelect, false); // check PPSE and select application id if (!res) { @@ -2776,17 +2907,17 @@ static int CmdEMVReader(const char *Cmd) { "In `verbose` mode it will also try to extract and decode the transaction logs stored on card in either channel.\n", "emv reader\n" "emv reader -v\n" - "emv reader -w -> contact interface\n" - "emv reader -w -1 -> contact interface, protocol T=1\n" + "emv reader -w -> contact interface\n" + "emv reader -w --t1 -> contact interface, protocol T=1\n" "emv reader -@ -> Continuous mode\n" ); void *argtable[] = { arg_param_begin, arg_lit0("w", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"), - arg_lit0("0", NULL, "use protocol T=0 (default)"), - arg_lit0("1", NULL, "use protocol T=1"), arg_lit0("v", "verbose", "Verbose output"), arg_lit0("@", NULL, "continuous reader mode"), + arg_lit0(NULL, "t0", "use protocol T=0 on the contact interface (default)"), + arg_lit0(NULL, "t1", "use protocol T=1 on the contact interface"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -2796,27 +2927,17 @@ static int CmdEMVReader(const char *Cmd) { channel = CC_CONTACT; } - bool use_t0 = arg_get_lit(ctx, 2); - bool use_t1 = arg_get_lit(ctx, 3); uint8_t psenum = (channel == CC_CONTACT) ? 1 : 2; - bool verbose = arg_get_lit(ctx, 4); - bool continuous = arg_get_lit(ctx, 5); + bool verbose = arg_get_lit(ctx, 2); + bool continuous = arg_get_lit(ctx, 3); + bool use_t0 = arg_get_lit(ctx, 4); + bool use_t1 = arg_get_lit(ctx, 5); CLIParserFree(ctx); - if (use_t0 && use_t1) { - PrintAndLogEx(FAILED, "Choose either -0 or -1, not both"); + if (emv_set_protocol(use_t0, use_t1, channel) != PM3_SUCCESS) { return PM3_EINVARG; } - if ((use_t0 || use_t1) && channel != CC_CONTACT) { - PrintAndLogEx(FAILED, "-0 and -1 only apply to the contact interface, add -w"); - return PM3_EINVARG; - } - - // A card whose ATR offers no T=0 is switched over by the ARM on its own; - // this is for one that offers both and would otherwise be driven as T=0. - SetSmartcardProtocolT1(use_t1); - if (continuous) { PrintAndLogEx(INFO, "Press " _GREEN_("") " to exit"); } @@ -2840,10 +2961,24 @@ static int CmdEMVReader(const char *Cmd) { const char *al = "Applets"; struct tlvdb *tlvSelect = tlvdb_fixed(1, strlen(al), (const unsigned char *)al); - res = EMVSelectPSE(channel, true, true, 2, buf, sizeof(buf), &len, &sw); + /* + * Search the directory this channel expects first, then the other one. + * + * A contact card may carry either: 1PAY was the norm and 2PAY is what + * modern cards use, and plenty carry only one of them. This used to + * select 2PAY, throw the answer away, and then search whichever psenum + * says - 1PAY on contact - so a card with only a PPSE fell through to + * the AID list sweep with its directory already read and discarded. + * The sweep then cannot find anything the built-in list does not carry, + * which is most non-Visa/Mastercard applets. + * + * The first call activates the field; the fallback reuses it. + */ + res = EMVSearchPSE(channel, true, true, psenum, false, tlvSelect, true); + if (res) { + res = EMVSearchPSE(channel, false, true, (psenum == 1) ? 2 : 1, false, tlvSelect, false); + } - // search PSE / PPSE - res |= EMVSearchPSE(channel, false, true, psenum, false, tlvSelect); if (res) { // EMV SEARCH with AID list DropFieldEx(channel);