-int EMVExchange(EMVCommandChannel channel, bool LeaveFieldON, sAPDU apdu, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
- return EMVExchangeEx(channel, false, LeaveFieldON, apdu, (channel == ECC_CONTACTLESS), Result, MaxResultLen, ResultLen, sw, tlv);
+int EMVExchange(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *apdu, int apdu_len, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv)
+{
+ uint8_t APDU[APDU_COMMAND_LEN];
+ memcpy(APDU, apdu, apdu_len);
+ APDU[apdu_len] = 0x00;
+ if (channel == ECC_CONTACTLESS) {
+ if (apdu_len == 5 && apdu[4] == 0) {
+ // there is no Lc but an Le == 0 already
+ } else if (apdu_len > 5 && apdu_len == 5 + apdu[4] + 1) {
+ // there is Lc, data and Le
+ } else {
+ apdu_len++; // no Le, add Le = 0x00 because some vendors require it for contactless
+ }
+ }
+ return EMVExchangeEx(channel, false, LeaveFieldON, APDU, apdu_len, Result, MaxResultLen, ResultLen, sw, tlv);