diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index 520011e39..ae9b43c9a 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -3685,11 +3685,14 @@ b5,b6 = 00 - DESELECT 11 - WTX */ int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chaining, void *data, uint16_t data_len, uint8_t *res) { - uint8_t *real_cmd = BigBuf_calloc(cmd_len + 4); - if (real_cmd == NULL) { - return -1; + // PCB(1) + APDU + CRC(2) has to fit inside one ISO14443 frame, which is + // also the limit ReaderTransmit() can encode into the tosend buffer. + if (cmd_len + 3 > MAX_FRAME_SIZE) { + return PM3_EINVARG; } + uint8_t real_cmd[MAX_FRAME_SIZE] = {0}; + if (cmd_len) { // ISO 14443 APDU frame: PCB [CID] [NAD] APDU CRC PCB=0x02 real_cmd[0] = 0x02; // bnr,nad,cid,chn=0; i-block(0x00) @@ -3710,7 +3713,6 @@ int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chaining, void *data, u // tearoff occurred if (tearoff_hook() == PM3_ETEAROFF) { - BigBuf_free(); return -1; } @@ -3718,7 +3720,6 @@ int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chaining, void *data, u uint8_t *data_bytes = (uint8_t *) data; if (len == 0) { - BigBuf_free(); return 0; // DATA LINK ERROR } @@ -3728,12 +3729,10 @@ int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chaining, void *data, u while (len && ((data_bytes[0] & 0xF2) == 0xF2)) { if (BUTTON_PRESS() || data_available()) { - BigBuf_free(); return -3; } - // Inform client of WTX of timeout in ms - // 38ms == MAX_ISO14A_TIMEOUT + // Inform client of WTX 38ms == MAX_ISO14A_TIMEOUT send_wtx(38); // byte1 - WTXM [1..59]. command FWT=FWT*WTXM @@ -3754,7 +3753,6 @@ int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chaining, void *data, u } - // restore timeout iso14a_set_timeout(save_iso14a_timeout); // if we received an I- or R(ACK)-Block with a block number equal to the @@ -3771,22 +3769,18 @@ int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chaining, void *data, u *res = data_bytes[0]; } - // crc check if (len >= 3 && !CheckCrc14A(data_bytes, len)) { - BigBuf_free(); return -1; } if (len) { // cut frame byte len -= 1; - // memmove(data_bytes, data_bytes + 1, len); for (int i = 0; i < len; i++) { data_bytes[i] = data_bytes[i + 1]; } } - BigBuf_free(); return len; } diff --git a/armsrc/secc.c b/armsrc/secc.c index 85166b656..d4c357f9a 100644 --- a/armsrc/secc.c +++ b/armsrc/secc.c @@ -421,8 +421,15 @@ bool hid_config_card_jam(const uint8_t *cmd, int len, uint8_t *dma_buf) { // --------------------------------------------------------------------------- int hid_config_card_iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chaining, void *data, uint16_t data_len, uint8_t *res) { + // PCB(1) + CID(1) + APDU + CRC(2) has to fit inside one ISO14443 frame, + // which is also the limit ReaderTransmit() can encode into the tosend + // buffer. + if (cmd_len + 4 > MAX_FRAME_SIZE) { + return PM3_EINVARG; + } + uint8_t parity[MAX_PARITY_SIZE] = {0}; - uint8_t *real_cmd = BigBuf_calloc(cmd_len + 5); // PCB(1) + CID(1) + APDU + CRC(2) + uint8_t real_cmd[MAX_FRAME_SIZE] = {0}; if (cmd_len) { real_cmd[0] = 0x0A; // I-block, CID present (bit 3), block number in bit 0 @@ -441,7 +448,6 @@ int hid_config_card_iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chainin ReaderTransmit(real_cmd, cmd_len + 4, NULL); // PCB(1) + CID(1) + APDU + CRC(2) if (tearoff_hook() == PM3_ETEAROFF) { - BigBuf_free(); return -1; } @@ -449,7 +455,6 @@ int hid_config_card_iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chainin uint8_t *data_bytes = (uint8_t *)data; if (len == 0) { - BigBuf_free(); return 0; } @@ -458,7 +463,6 @@ int hid_config_card_iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chainin // S-Block WTX while (len && ((data_bytes[0] & 0xF2) == 0xF2)) { if (BUTTON_PRESS() || data_available()) { - BigBuf_free(); return -3; } send_wtx(38); @@ -482,7 +486,6 @@ int hid_config_card_iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chainin *res = data_bytes[0]; if (len >= 3 && !CheckCrc14A(data_bytes, len)) { - BigBuf_free(); return -1; } @@ -493,7 +496,6 @@ int hid_config_card_iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chainin memmove(data_bytes, data_bytes + header_len, len); } - BigBuf_free(); return len; }