+ UsbCommand c = {CMD_SNOOP_ISO_14443a, {param, 0, 0}};
+ SendCommand(&c);
+ return 0;
+}
+
+
+void DropField() {
+ UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
+ SendCommand(&c);
+}
+
+
+int ExchangeRAW14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) {
+ static bool responseNum = false;
+ uint16_t cmdc = 0;
+ *dataoutlen = 0;
+
+ if (activateField) {
+ responseNum = false;
+ UsbCommand resp;
+
+ // Anticollision + SELECT card
+ UsbCommand ca = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT | ISO14A_CLEAR_TRACE, 0, 0}};
+ SendCommand(&ca);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
+ PrintAndLog("14aRAW ERROR: Proxmark connection timeout.");
+ return 1;
+ }
+
+ // check result
+ if (resp.arg[0] == 0) {
+ PrintAndLog("14aRAW ERROR: No card in field.");
+ return 1;
+ }
+
+ if (resp.arg[0] != 1 && resp.arg[0] != 2) {
+ PrintAndLog("14aRAW ERROR: card not in iso14443-4. res=%d.", resp.arg[0]);
+ return 1;
+ }
+
+ if (resp.arg[0] == 2) { // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
+ // get ATS
+ UsbCommand cr = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT, 2, 0}};
+ uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
+ memcpy(cr.d.asBytes, rats, 2);
+ SendCommand(&cr);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
+ PrintAndLog("14aRAW ERROR: Proxmark connection timeout.");
+ return 1;
+ }
+
+ if (resp.arg[0] <= 0) { // ats_len
+ PrintAndLog("14aRAW ERROR: Can't get ATS.");
+ return 1;
+ }
+ }
+ }
+
+ if (leaveSignalON)
+ cmdc |= ISO14A_NO_DISCONNECT;
+
+ UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_APPEND_CRC | cmdc, (datainlen & 0xFFFF) + 2, 0}};
+ uint8_t header[] = {0x0a | responseNum, 0x00};
+ responseNum ^= 1;
+ memcpy(c.d.asBytes, header, 2);
+ memcpy(&c.d.asBytes[2], datain, datainlen);
+ SendCommand(&c);
+
+ uint8_t *recv;
+ UsbCommand resp;
+
+ if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
+ recv = resp.d.asBytes;
+ int iLen = resp.arg[0];
+
+ if(!iLen) {
+ PrintAndLog("14aRAW ERROR: No card response.");
+ return 1;
+ }
+
+ *dataoutlen = iLen - 2;
+ if (*dataoutlen < 0)
+ *dataoutlen = 0;
+
+ if (maxdataoutlen && *dataoutlen > maxdataoutlen) {
+ PrintAndLog("14aRAW ERROR: Buffer too small(%d). Needs %d bytes", *dataoutlen, maxdataoutlen);
+ return 2;
+ }
+
+ if (recv[0] != header[0]) {
+ PrintAndLog("14aRAW ERROR: iso14443-4 framing error. Card send %2x must be %2x", dataout[0], header[0]);
+ return 2;
+ }
+
+ memcpy(dataout, &recv[2], *dataoutlen);
+
+ // CRC Check
+ if (iLen == -1) {
+ PrintAndLog("14aRAW ERROR: ISO 14443A CRC error.");
+ return 3;
+ }
+
+
+ } else {
+ PrintAndLog("14aRAW ERROR: Reply timeout.");
+ return 4;
+ }
+
+ return 0;
+}
+
+
+static int SelectCard14443_4(bool disconnect, iso14a_card_select_t *card) {
+ UsbCommand resp;
+
+ frameLength = 0;
+
+ if (card)
+ memset(card, 0, sizeof(iso14a_card_select_t));
+
+ DropField();
+
+ // Anticollision + SELECT card
+ UsbCommand ca = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};
+ SendCommand(&ca);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
+ PrintAndLogEx(ERR, "Proxmark connection timeout.");
+ return 1;
+ }
+
+ // check result
+ if (resp.arg[0] == 0) {
+ PrintAndLogEx(ERR, "No card in field.");
+ return 1;
+ }
+
+ if (resp.arg[0] != 1 && resp.arg[0] != 2) {
+ PrintAndLogEx(ERR, "Card not in iso14443-4. res=%d.", resp.arg[0]);
+ return 1;
+ }
+
+ if (resp.arg[0] == 2) { // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
+ // try to get ATS although SAK indicated that it is not ISO14443-4 compliant
+ UsbCommand cr = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT, 2, 0}};
+ uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
+ memcpy(cr.d.asBytes, rats, 2);
+ SendCommand(&cr);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
+ PrintAndLogEx(ERR, "Proxmark connection timeout.");
+ return 1;
+ }
+
+ if (resp.arg[0] <= 0) { // ats_len
+ PrintAndLogEx(ERR, "Can't get ATS.");
+ return 1;
+ }
+ }
+
+ // get frame length from ATS
+ iso14a_card_select_t *vcard = (iso14a_card_select_t *) resp.d.asBytes;
+ if (vcard->ats_len > 1) {
+ uint8_t fsci = vcard->ats[1] & 0x0f;
+ if (fsci < sizeof(atsFSC))
+ frameLength = atsFSC[fsci];
+ }
+
+ if (card) {
+ memcpy(card, vcard, sizeof(iso14a_card_select_t));
+ }
+
+ if (disconnect) {
+ DropField();
+ }
+
+ return 0;
+}
+
+
+static int ExchangeAPDU(bool chainingin, uint8_t *datain, int datainlen, bool activateField, uint8_t *dataout, int maxdataoutlen, int *dataoutlen, bool *chainingout)
+{
+ *chainingout = false;
+
+ if (activateField) {
+ // select with no disconnect and set frameLength
+ int selres = SelectCard14443_4(false, NULL);
+ if (selres)
+ return selres;
+ }
+
+ uint16_t cmdc = 0;
+ if (chainingin)
+ cmdc = ISO14A_SEND_CHAINING;
+
+ // "Command APDU" length should be 5+255+1, but javacard's APDU buffer might be smaller - 133 bytes
+ // https://stackoverflow.com/questions/32994936/safe-max-java-card-apdu-data-command-and-respond-size
+ // here length USB_CMD_DATA_SIZE=512
+ // timeout must be authomatically set by "get ATS"
+ UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_APDU | ISO14A_NO_DISCONNECT | cmdc, (datainlen & 0xFFFF), 0}};
+ memcpy(c.d.asBytes, datain, datainlen);
+ SendCommand(&c);
+
+ uint8_t *recv;
+ UsbCommand resp;
+
+ if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
+ recv = resp.d.asBytes;
+ int iLen = resp.arg[0];
+ uint8_t res = resp.arg[1];
+
+ int dlen = iLen - 2;
+ if (dlen < 0)
+ dlen = 0;
+ *dataoutlen += dlen;
+
+ if (maxdataoutlen && *dataoutlen > maxdataoutlen) {
+ PrintAndLog("APDU ERROR: Buffer too small(%d). Needs %d bytes", *dataoutlen, maxdataoutlen);
+ return 2;
+ }
+
+ // I-block ACK
+ if ((res & 0xf2) == 0xa2) {
+ *dataoutlen = 0;
+ *chainingout = true;
+ return 0;
+ }
+
+ if(!iLen) {
+ PrintAndLog("APDU ERROR: No APDU response.");
+ return 1;
+ }
+
+ // check apdu length
+ if (iLen < 2 && iLen >= 0) {
+ PrintAndLog("APDU ERROR: Small APDU response. Len=%d", iLen);
+ return 2;
+ }
+
+ // check block TODO
+ if (iLen == -2) {
+ PrintAndLog("APDU ERROR: Block type mismatch.");
+ return 2;
+ }
+
+ memcpy(dataout, recv, dlen);
+
+ // chaining
+ if ((res & 0x10) != 0) {
+ *chainingout = true;
+ }
+
+ // CRC Check
+ if (iLen == -1) {
+ PrintAndLog("APDU ERROR: ISO 14443A CRC error.");
+ return 3;
+ }
+ } else {
+ PrintAndLog("APDU ERROR: Reply timeout.");
+ return 4;
+ }
+
+ return 0;