fixed timings, T=1 <> T=0 swaps when emv card doesn't support T=0,

This commit is contained in:
iceman1001 committed 2026-08-26 23:57:47 +02:00
1 parent c5ffec1305
commit 35d1cb4fa7
9 files changed
+356 -57

No files matched your search

+39 -6
View File
@@ -442,8 +442,23 @@ static int smart_responseEx(uint8_t *out, int maxoutlen, bool verbose, uint8_t c
goto out;
}
// data wo ACK
if (datalen != len + 2) {
/*
* Two shapes are valid here, depending on how the GET RESPONSE went out:
*
* len + 2 the data and its status word. This is what SEND_T0
* gives, because the module runs the procedure byte
* exchange itself and strips the echoed INS.
* len + 2 + 1 the same with that procedure byte still in front,
* which is what the raw pass through leaves behind.
*
* Only the second needs unwrapping - but the first used to fall through
* both branches without ever adding to totallen, so the caller got zero
* bytes and reported "result length = 0" while the card had answered
* perfectly. It went unnoticed while GET RESPONSE was always sent raw.
*/
if (datalen == len + 2) {
totallen += datalen;
} else {
// data with ACK
if (datalen == len + 2 + 1) { // 2 - response, 1 - ACK
if (out[ofs] != ISO7816_GET_RESPONSE) {
@@ -1638,6 +1653,24 @@ int CmdSmartcard(const char *Cmd) {
return CmdsParse(CommandTable, Cmd);
}
/*
* Which protocol the contact exchanges below ask for.
*
* T=0 by default, which is what this has always sent. The ARM side already
* redirects a T=0 request to T=1 when the card's ATR offers no T=0 at all - a
* request that could not have worked as asked. This is for the other case: a
* card that offers both, where T=0 would work but you want T=1 anyway.
*/
static smartcard_command_t s_sc_protocol = SC_RAW_T0;
void SetSmartcardProtocolT1(bool use_t1) {
s_sc_protocol = use_t1 ? SC_RAW_T1 : SC_RAW_T0;
}
bool GetSmartcardProtocolT1(void) {
return (s_sc_protocol == SC_RAW_T1);
}
int ExchangeAPDUSC(bool verbose, uint8_t *datain, int datainlen, bool activateCard, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) {
*dataoutlen = 0;
@@ -1647,7 +1680,7 @@ int ExchangeAPDUSC(bool verbose, uint8_t *datain, int datainlen, bool activateCa
PrintAndLogEx(WARNING, "Failed to allocate memory");
return PM3_EMALLOC;
}
payload->flags = (SC_RAW_T0 | SC_LOG);
payload->flags = (s_sc_protocol | SC_LOG);
if (activateCard) {
payload->flags |= (SC_SELECT | SC_CONNECT);
}
@@ -1658,7 +1691,7 @@ int ExchangeAPDUSC(bool verbose, uint8_t *datain, int datainlen, bool activateCa
clearCommandBuffer();
SendCommandNG(CMD_SMART_RAW, (uint8_t *)payload, sizeof(smart_card_raw_t) + datainlen);
int len = smart_responseEx(dataout, maxdataoutlen, verbose, (datainlen > 0) ? datain[0] : 0x00, SC_RAW);
int len = smart_responseEx(dataout, maxdataoutlen, verbose, (datainlen > 0) ? datain[0] : 0x00, s_sc_protocol);
if (len < 0) {
free(payload);
return PM3_ESOFT;
@@ -1667,7 +1700,7 @@ int ExchangeAPDUSC(bool verbose, uint8_t *datain, int datainlen, bool activateCa
// retry
if (len > 1 && dataout[len - 2] == 0x6c && datainlen > 4) {
payload->flags = SC_RAW_T0;
payload->flags = s_sc_protocol;
payload->len = 5;
// transfer length via T=0
datain[4] = dataout[len - 1];
@@ -1675,7 +1708,7 @@ int ExchangeAPDUSC(bool verbose, uint8_t *datain, int datainlen, bool activateCa
clearCommandBuffer();
SendCommandNG(CMD_SMART_RAW, (uint8_t *)payload, sizeof(smart_card_raw_t) + 5);
datain[4] = 0;
len = smart_responseEx(dataout, maxdataoutlen, verbose, datain[0], SC_RAW);
len = smart_responseEx(dataout, maxdataoutlen, verbose, datain[0], s_sc_protocol);
if (len < 0) {
free(payload);
return PM3_ESOFT;
+5
View File
@@ -30,6 +30,11 @@
int CmdSmartcard(const char *Cmd);
bool smart_select(bool verbose, smart_card_atr_t *atr);
// Ask the contact exchanges for T=1 rather than the default T=0. Only needed
// for a card that offers both - the ARM redirects a T=1 only card by itself.
void SetSmartcardProtocolT1(bool use_t1);
bool GetSmartcardProtocolT1(void);
int ExchangeAPDUSC(bool verbose, uint8_t *datain, int datainlen, bool activateCard, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen);
#endif
+23 -3
View File
@@ -2776,11 +2776,15 @@ 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 -@ -> Continuous mode\n"
"emv reader -w -> contact interface\n"
"emv reader -w -1 -> 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_param_end
@@ -2792,11 +2796,27 @@ 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, 2);
bool continuous = arg_get_lit(ctx, 3);
bool verbose = arg_get_lit(ctx, 4);
bool continuous = arg_get_lit(ctx, 5);
CLIParserFree(ctx);
if (use_t0 && use_t1) {
PrintAndLogEx(FAILED, "Choose either -0 or -1, not both");
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_("<Enter>") " to exit");
}
+30 -3
View File
@@ -329,6 +329,12 @@ static int EMVSelectWithRetry(Iso7816CommandChannel channel, bool ActivateField,
// retry if error and not returned sw error
if (res && res != 5) {
// a PM3_E* transport failure will not be fixed by asking again
if (res < 0) {
return res;
}
if (++retrycnt < 3) {
continue;
} else {
@@ -379,7 +385,9 @@ static int EMVCheckAID(Iso7816CommandChannel channel, bool decodeTLV, struct tlv
return res;
}
int EMVSearchPSE(Iso7816CommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, bool decodeTLV, struct tlvdb *tlv) {
// quiet: the caller has a fallback lined up, so a failure here is a probe
// rather than a problem and should not be reported as one.
int EMVSearchPSE(Iso7816CommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, bool decodeTLV, struct tlvdb *tlv, bool quiet) {
uint8_t data[APDU_RES_LEN] = {0};
size_t datalen = 0;
uint16_t sw = 0;
@@ -391,7 +399,9 @@ int EMVSearchPSE(Iso7816CommandChannel channel, bool ActivateField, bool LeaveFi
if (!res) {
if (sw != ISO7816_OK) {
PrintAndLogEx(FAILED, "Select PSE error. APDU error: %04x.", sw);
if (quiet == false) {
PrintAndLogEx(FAILED, "Select PSE error. APDU error: %04x.", sw);
}
return 1;
}
@@ -463,7 +473,7 @@ int EMVSearchPSE(Iso7816CommandChannel channel, bool ActivateField, bool LeaveFi
} else {
PrintAndLogEx(WARNING, "%s ERROR: Can't get TLV from response.", PSE_or_PPSE);
}
} else {
} else if (quiet == false) {
PrintAndLogEx(ERR, "%s ERROR: Can't select PPSE AID. Error: %d", PSE_or_PPSE, res);
}
@@ -492,6 +502,23 @@ int EMVSearch(Iso7816CommandChannel channel, bool ActivateField, bool LeaveField
int res = EMVSelect(channel, (i == 0) ? ActivateField : false, true, aidbuf, aidlen, data, sizeof(data), &datalen, &sw, tlv);
// retry if error and not returned sw error
if (res && res != 5) {
// A negative result is a PM3_E* transport failure rather than
// anything the card said - PM3_EIO when the field has gone
// inactive, for instance. Retrying the same AID cannot fix that,
// and neither can the ~150 AIDs still to come: without this it
// retries each of them three times and prints a failure for every
// attempt.
if (res < 0) {
if (LeaveFieldON == false) {
DropFieldEx(channel);
}
if (verbose) {
PrintAndLogEx(WARNING, "exiting...");
}
return 1;
}
if (++retrycnt < 3) {
i--;
} else {
+1 -1
View File
@@ -62,7 +62,7 @@ struct tlvdb *GetdCVVRawFromTrack2(const struct tlv *track2);
int EMVExchange(Iso7816CommandChannel channel, bool LeaveFieldON, sAPDU_t apdu, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv);
// search application
int EMVSearchPSE(Iso7816CommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, bool decodeTLV, struct tlvdb *tlv);
int EMVSearchPSE(Iso7816CommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, bool decodeTLV, struct tlvdb *tlv, bool quiet);
int EMVSearch(Iso7816CommandChannel channel, bool ActivateField, bool LeaveFieldON, bool decodeTLV, struct tlvdb *tlv, bool verbose);
int EMVSelectPSE(Iso7816CommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw);
int EMVSelect(Iso7816CommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t *AID, size_t AIDLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv);
+41 -4
View File
@@ -208,9 +208,9 @@ int Iso7816Exchange(Iso7816CommandChannel channel, bool leave_field_on, sAPDU_t
int Iso7816Select(Iso7816CommandChannel channel, bool activate_field, bool leave_field_on, uint8_t *aid, size_t aid_len,
uint8_t *result, size_t max_result_len, size_t *result_len, uint16_t *sw) {
return Iso7816ExchangeEx(channel
, activate_field
, leave_field_on
int res = Iso7816ExchangeEx(channel
, activate_field
, leave_field_on
, (sAPDU_t) {0x00, 0xa4, 0x04, 0x00, aid_len, aid}
, (channel == CC_CONTACTLESS)
, 0
@@ -218,5 +218,42 @@ int Iso7816Select(Iso7816CommandChannel channel, bool activate_field, bool leave
, max_result_len
, result_len
, sw
);
);
/*
* A contact card running T=1 needs the Le that a T=0 card must not be
* given. T=0 answers a case 4 command with 61xx and hands the data over
* through GET RESPONSE, so the Le is left off; T=1 carries the whole APDU
* in one block and has no such step, so the command has to ask for its
* length up front. Send one without and a strict card answers 6700.
*
* Rather than work out which protocol the link ended up on - the ARM may
* have switched to T=1 on its own, off the ATR, without the client being
* told - let the card say so and reissue. EMVReadRecord() and
* EMVGenerateChallenge() already do the mirror image of this.
*
* Only on 6700/6F00, and only on contact: a T=0 card has no reason to
* answer a SELECT that way, and if one did the retry costs a single extra
* APDU that it will reject just as it rejected the first.
*/
if ((channel == CC_CONTACT) && (sw != NULL) && ((*sw == 0x6700) || (*sw == 0x6F00))) {
if (APDULogging) {
PrintAndLogEx(INFO, ">>> wrong length, reissuing SELECT with Le...");
}
res = Iso7816ExchangeEx(channel
, false
, leave_field_on
, (sAPDU_t) {0x00, 0xa4, 0x04, 0x00, aid_len, aid}
, true
, 0
, result
, max_result_len
, result_len
, sw
);
}
return res;
}