new sim module firmware, shorter delays for T=0 transfers
This commit is contained in:
1 parent
a76399c030
commit
84ad2fb8d4
9 files changed
+116
-220
No files matched your search
Binary file not shown.
@@ -0,0 +1 @@
|
||||
9a3d623aa3c749ffb5125b6a4912f74e46902a9135441c427e1635be24d43c048e40e1f60a052175f383a1316c63dbddb999cac170a4224fe9a0d7df896391d4 *sim018.bin
|
||||
+24
-24
@@ -818,17 +818,10 @@ void annotateTopaz(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) {
|
||||
|
||||
// iso 7816-3
|
||||
//
|
||||
// `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".
|
||||
// contact tells the two framings apart: a contactless frame is always a block
|
||||
// typed by its first byte, while a contact T=0 frame is a bare APDU and a T=1
|
||||
// frame is NAD PCB LEN INF EDC. Without it a GSM APDU starting with CLA 'A0'
|
||||
// matches the T=CL R-block test.
|
||||
void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response, bool contact) {
|
||||
|
||||
if (cmdsize < 2) {
|
||||
@@ -839,21 +832,29 @@ void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool
|
||||
return;
|
||||
}
|
||||
|
||||
// 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.
|
||||
// T=1 puts the block type in cmd[1], not cmd[0] as T=CL does. Match only
|
||||
// the one byte LRC length - a five byte APDU with P1 zero is otherwise
|
||||
// indistinguishable from a LEN=0 block with a CRC. NAD must be 0x00.
|
||||
if (contact) {
|
||||
|
||||
if ((cmdsize >= 4) && (cmd[0] == 0x00) && ((int)cmdsize == (int)cmd[2] + 4)) {
|
||||
|
||||
uint8_t pcb = cmd[1];
|
||||
|
||||
// Length alone is not enough - "00 B2 01 0C 00" is a READ RECORD
|
||||
// that matches it. Require PCB and LEN to agree: an R-block carries
|
||||
// no INF, an S-block at most one byte.
|
||||
bool pcb_agrees = true;
|
||||
if ((pcb & 0xC0) == 0x80) {
|
||||
pcb_agrees = (cmd[2] == 0); /* R */
|
||||
} else if ((pcb & 0xC0) == 0xC0) {
|
||||
pcb_agrees = (cmd[2] <= 1); /* S */
|
||||
}
|
||||
|
||||
if (pcb_agrees == false) {
|
||||
goto not_a_block;
|
||||
}
|
||||
|
||||
if ((pcb & 0x80) == 0x00) {
|
||||
snprintf(exp, size, "I-block N(S)=%u%s", (pcb >> 6) & 1,
|
||||
(pcb & 0x20) ? " chained" : "");
|
||||
@@ -875,14 +876,13 @@ void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool
|
||||
}
|
||||
return;
|
||||
}
|
||||
// not a block, so it is a bare APDU - decoded below with pos = 1
|
||||
not_a_block:
|
||||
; // a bare APDU then - 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.
|
||||
// S-block
|
||||
if (blocks && ((cmd[0] & 0xC0) == 0xC0) && ((cmdsize == 3) || (cmdsize == 4))) {
|
||||
|
||||
switch ((cmd[0] & 0x3F)) {
|
||||
|
||||
+12
-40
@@ -327,18 +327,9 @@ static int smart_wait(uint8_t *out, int maxoutlen, bool verbose) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Which class byte a GET RESPONSE following a 61xx / 9Fxx should carry.
|
||||
//
|
||||
// There is no rule that can be derived from the command's own class, because
|
||||
// two cards that both use CLA 'A0' disagree:
|
||||
//
|
||||
// - a GSM 11.11 / TS 51.011 SIM answers 6D00 to '00 C0 ...' and needs 'A0 C0'
|
||||
// - a HID iCLASS SE SAM answers 6D00 to 'A0 C0 ...' and needs '00 C0'
|
||||
//
|
||||
// and EMV (Book 1, 9.3.1) fixes GET RESPONSE at '00' even after a proprietary
|
||||
// '80' command. So '00' is tried first - the historical behaviour, right for
|
||||
// EMV and for the SAM - and a card that rejects the class gets one retry with
|
||||
// the class of the command that produced the status word.
|
||||
// Class byte for a GET RESPONSE after 61xx / 9Fxx. No single rule works: a GSM
|
||||
// SIM needs 'A0 C0' and rejects '00 C0', a HID iCLASS SE SAM is the other way
|
||||
// round, and EMV fixes it at '00'. Try '00' first, then the command's own.
|
||||
#define GETRESP_TRY_FIRST 0
|
||||
#define GETRESP_TRY_RETRY 1
|
||||
|
||||
@@ -400,8 +391,7 @@ static int smart_responseEx(uint8_t *out, int maxoutlen, bool verbose, uint8_t c
|
||||
|
||||
if (verbose) PrintAndLogEx(INFO, "Requesting " _YELLOW_("0x%02X") " bytes response", len);
|
||||
|
||||
// '00' first, then one retry with the command's own class if the card
|
||||
// rejects it - see get_response_cla() for why neither works everywhere.
|
||||
// '00' first, then the command's own class if the card rejects it
|
||||
for (int attempt = GETRESP_TRY_FIRST; attempt <= GETRESP_TRY_RETRY; attempt++) {
|
||||
|
||||
uint8_t cmd_getresp[] = {
|
||||
@@ -442,20 +432,9 @@ static int smart_responseEx(uint8_t *out, int maxoutlen, bool verbose, uint8_t c
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
// Two valid shapes: len+2 is data plus SW, which SEND_T0 gives since
|
||||
// the module strips the procedure byte; len+2+1 still has it in front,
|
||||
// as the raw pass through leaves it.
|
||||
if (datalen == len + 2) {
|
||||
totallen += datalen;
|
||||
} else {
|
||||
@@ -823,15 +802,14 @@ static int CmdSmartInfo(const char *Cmd) {
|
||||
SendCommandNG(CMD_SMART_ATR, NULL, 0);
|
||||
PacketResponseNG resp;
|
||||
if (WaitForResponseTimeout(CMD_SMART_ATR, &resp, 2500) == false) {
|
||||
if (verbose) {
|
||||
PrintAndLogEx(WARNING, "smart card timeout");
|
||||
}
|
||||
PrintAndLogEx(WARNING, "smart card timeout");
|
||||
return PM3_ETIMEOUT;
|
||||
}
|
||||
|
||||
if (resp.status != PM3_SUCCESS) {
|
||||
PrintAndLogEx(WARNING, "no ATR - check the card is present and seated");
|
||||
if (verbose) {
|
||||
PrintAndLogEx(WARNING, "smart card select failed");
|
||||
PrintAndLogEx(INFO, "module returned status %d", resp.status);
|
||||
}
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
@@ -1653,14 +1631,8 @@ 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.
|
||||
*/
|
||||
// Protocol the contact exchanges ask for. T=0 by default; the ARM redirects a
|
||||
// card offering no T=0 by itself, so this is for one that offers both.
|
||||
static smartcard_command_t s_sc_protocol = SC_RAW_T0;
|
||||
|
||||
void SetSmartcardProtocolT1(bool use_t1) {
|
||||
|
||||
+5
-25
@@ -575,16 +575,8 @@ 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.
|
||||
*/
|
||||
// Resolve --t0 / --t1 for the contact interface. The ARM switches a card
|
||||
// offering no T=0 over by itself; this is for one that offers both.
|
||||
static int emv_set_protocol(bool use_t0, bool use_t1, Iso7816CommandChannel channel) {
|
||||
|
||||
if (use_t0 && use_t1) {
|
||||
@@ -660,8 +652,6 @@ static int CmdEMVSelect(const char *Cmd) {
|
||||
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 {
|
||||
@@ -2961,19 +2951,9 @@ static int CmdEMVReader(const char *Cmd) {
|
||||
const char *al = "Applets";
|
||||
struct tlvdb *tlvSelect = tlvdb_fixed(1, strlen(al), (const unsigned char *)al);
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
// Search the expected directory then the other - a contact card may
|
||||
// carry 1PAY or 2PAY and plenty carry only one. 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);
|
||||
|
||||
@@ -475,6 +475,17 @@ int EMVSearchPSE(Iso7816CommandChannel channel, bool ActivateField, bool LeaveFi
|
||||
}
|
||||
} else if (quiet == false) {
|
||||
PrintAndLogEx(ERR, "%s ERROR: Can't select PPSE AID. Error: %d", PSE_or_PPSE, res);
|
||||
|
||||
// an empty slot and a card that is simply not an EMV card both land
|
||||
// here, so ask the module which one it is
|
||||
if ((res < 0) && (channel == CC_CONTACT)) {
|
||||
smart_card_atr_t atr;
|
||||
if (smart_select(false, &atr)) {
|
||||
PrintAndLogEx(HINT, "Hint: card answered its ATR but not the APDU - is this an EMV card? try `" _YELLOW_("smart info") "`");
|
||||
} else {
|
||||
PrintAndLogEx(HINT, "Hint: no answer at all - is a card in the slot? try `" _YELLOW_("smart info") "`");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!LeaveFieldON)
|
||||
@@ -503,12 +514,9 @@ int EMVSearch(Iso7816CommandChannel channel, bool ActivateField, bool LeaveField
|
||||
// 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.
|
||||
// A negative result is a PM3_E* transport failure, not something
|
||||
// the card said. Retrying cannot fix it, nor can the ~150 AIDs
|
||||
// still to come.
|
||||
if (res < 0) {
|
||||
if (LeaveFieldON == false) {
|
||||
DropFieldEx(channel);
|
||||
|
||||
@@ -80,9 +80,11 @@ int Iso7816Connect(Iso7816CommandChannel channel) {
|
||||
return res;
|
||||
}
|
||||
|
||||
int Iso7816ExchangeEx(Iso7816CommandChannel channel, bool activate_field, bool leave_field_on,
|
||||
sAPDU_t apdu, bool include_le, uint16_t le, uint8_t *result,
|
||||
size_t max_result_len, size_t *result_len, uint16_t *sw) {
|
||||
// allow_le_retry gates the 6Cxx reissue below; the retry itself passes false
|
||||
static int iso7816_exchange_core(Iso7816CommandChannel channel, bool activate_field, bool leave_field_on,
|
||||
sAPDU_t apdu, bool include_le, uint16_t le, uint8_t *result,
|
||||
size_t max_result_len, size_t *result_len, uint16_t *sw,
|
||||
bool allow_le_retry) {
|
||||
|
||||
*result_len = 0;
|
||||
if (sw) {
|
||||
@@ -178,6 +180,29 @@ int Iso7816ExchangeEx(Iso7816CommandChannel channel, bool activate_field, bool l
|
||||
*sw = isw;
|
||||
}
|
||||
|
||||
// 6Cxx is "wrong length, ask again for xx". Handled here rather than per
|
||||
// command since any case 2/4 APDU can get it - READ RECORD and GET DATA
|
||||
// both do. Once only; the card has named the length.
|
||||
if (allow_le_retry && ((isw >> 8) == 0x6C)) {
|
||||
|
||||
if (APDULogging) {
|
||||
PrintAndLogEx(INFO, ">>> wrong length, reissuing with Le=%02X...", isw & 0xFF);
|
||||
}
|
||||
|
||||
return iso7816_exchange_core(channel
|
||||
, false
|
||||
, leave_field_on
|
||||
, apdu
|
||||
, true
|
||||
, (uint16_t)(isw & 0xFF)
|
||||
, result
|
||||
, max_result_len
|
||||
, result_len
|
||||
, sw
|
||||
, false
|
||||
);
|
||||
}
|
||||
|
||||
if (isw != ISO7816_OK) {
|
||||
if (APDULogging) {
|
||||
if (*sw >> 8 == 0x61) {
|
||||
@@ -191,6 +216,14 @@ int Iso7816ExchangeEx(Iso7816CommandChannel channel, bool activate_field, bool l
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
int Iso7816ExchangeEx(Iso7816CommandChannel channel, bool activate_field, bool leave_field_on,
|
||||
sAPDU_t apdu, bool include_le, uint16_t le, uint8_t *result,
|
||||
size_t max_result_len, size_t *result_len, uint16_t *sw) {
|
||||
|
||||
return iso7816_exchange_core(channel, activate_field, leave_field_on, apdu, include_le, le,
|
||||
result, max_result_len, result_len, sw, true);
|
||||
}
|
||||
|
||||
int Iso7816Exchange(Iso7816CommandChannel channel, bool leave_field_on, sAPDU_t apdu, uint8_t *result, size_t max_result_len, size_t *result_len, uint16_t *sw) {
|
||||
return Iso7816ExchangeEx(channel
|
||||
, false
|
||||
@@ -220,22 +253,10 @@ int Iso7816Select(Iso7816CommandChannel channel, bool activate_field, bool leave
|
||||
, 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.
|
||||
*/
|
||||
// T=1 carries the whole APDU in one block, so a case 4 command must include
|
||||
// Le - unlike T=0, which answers 61xx and hands the data over via GET
|
||||
// RESPONSE. A strict card answers 6700 without it. Reissue rather than work
|
||||
// out which protocol the link ended up on.
|
||||
if ((channel == CC_CONTACT) && (sw != NULL) && ((*sw == 0x6700) || (*sw == 0x6F00))) {
|
||||
|
||||
if (APDULogging) {
|
||||
|
||||
Reference in new issue
Block a user