+ *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;
+}
+
+
+int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) {
+ *dataoutlen = 0;
+ bool chaining = false;
+ int res;
+
+ // 3 byte here - 1b framing header, 2b crc16
+ if ( (frameLength && (datainlen > frameLength - 3)) || (datainlen > USB_CMD_DATA_SIZE - 3) ) {
+ int clen = 0;
+
+ bool vActivateField = activateField;
+
+ do {
+ int vlen = MIN(frameLength - 3, datainlen - clen);
+ bool chainBlockNotLast = ((clen + vlen) < datainlen);
+
+ *dataoutlen = 0;
+ res = ExchangeAPDU(chainBlockNotLast, &datain[clen], vlen, vActivateField, dataout, maxdataoutlen, dataoutlen, &chaining);
+ if (res) {
+ if (!leaveSignalON)
+ DropField();
+
+ return 200;
+ }
+
+ // check R-block ACK
+ if ((*dataoutlen == 0) && (*dataoutlen != 0 || chaining != chainBlockNotLast)) {
+ if (!leaveSignalON)
+ DropField();
+
+ return 201;
+ }
+
+ clen += vlen;
+ vActivateField = false;
+ if (*dataoutlen) {
+ if (clen != datainlen)
+ PrintAndLogEx(WARNING, "APDU: I-block/R-block sequence error. Data len=%d, Sent=%d, Last packet len=%d", datainlen, clen, *dataoutlen);
+ break;
+ }
+ } while (clen < datainlen);
+ } else {
+ res = ExchangeAPDU(false, datain, datainlen, activateField, dataout, maxdataoutlen, dataoutlen, &chaining);
+ if (res) {
+ if (!leaveSignalON)
+ DropField();
+
+ return res;
+ }
+ }
+
+ while (chaining) {
+ // I-block with chaining
+ res = ExchangeAPDU(false, NULL, 0, false, &dataout[*dataoutlen], maxdataoutlen, dataoutlen, &chaining);
+
+ if (res) {
+ if (!leaveSignalON)
+ DropField();
+
+ return 100;
+ }
+ }
+
+ if (!leaveSignalON)
+ DropField();
+
+ return 0;
+}
+
+// ISO14443-4. 7. Half-duplex block transmission protocol
+int CmdHF14AAPDU(const char *cmd) {
+ uint8_t data[USB_CMD_DATA_SIZE];
+ int datalen = 0;
+ uint8_t header[5];
+ int headerlen = 0;
+ bool activateField = false;
+ bool leaveSignalON = false;
+ bool decodeTLV = false;
+ bool decodeAPDU = false;
+ bool makeAPDU = false;
+ bool extendedAPDU = false;
+ int le = 0;
+ int res = 0;
+
+ CLIParserInit("hf 14a apdu",
+ "Sends an ISO 7816-4 APDU via ISO 14443-4 block transmission protocol (T=CL). Works with all APDU types from ISO 7816-4:2013",
+ "Examples:\n\thf 14a apdu -st 00A404000E325041592E5359532E444446303100\n"
+ "\thf 14a apdu -sd 00A404000E325041592E5359532E444446303100 - decode APDU\n"
+ "\thf 14a apdu -sm 00A40400 325041592E5359532E4444463031 -l 256 - encode standard APDU\n"
+ "\thf 14a apdu -sm 00A40400 325041592E5359532E4444463031 -el 65536 - encode extended APDU\n");
+
+ void* argtable[] = {
+ arg_param_begin,
+ arg_lit0("sS", "select", "activate field and select card"),
+ arg_lit0("kK", "keep", "leave the signal field ON after receive response"),
+ arg_lit0("tT", "tlv", "executes TLV decoder if it possible"),
+ arg_lit0("dD", "decapdu", "decode APDU request if it possible"),
+ arg_str0("mM", "make", "<head (CLA INS P1 P2) hex>", "make APDU with head from this field and data from data field. Must be 4 bytes length: <CLA INS P1 P2>"),
+ arg_lit0("eE", "extended", "make extended length APDU (requires `-m`)"),
+ arg_int0("lL", "le", "<Le (int)>", "Le APDU parameter (requires `-m`)"),
+ arg_strx1(NULL, NULL, "<APDU (hex) | data (hex)>", "APDU (without `-m`), or data (with `-m`)"),
+ arg_param_end
+ };
+ CLIExecWithReturn(cmd, argtable, false);
+
+ activateField = arg_get_lit(1);
+ leaveSignalON = arg_get_lit(2);
+ decodeTLV = arg_get_lit(3);
+ decodeAPDU = arg_get_lit(4);
+
+ res = CLIParamHexToBuf(arg_get_str(5), header, sizeof(header), &headerlen);
+ makeAPDU = headerlen > 0;
+ if (res || (makeAPDU && headerlen != 4)) {
+ PrintAndLogEx(ERR, "header length must be exactly 4 bytes");
+ CLIParserFree();
+ return 1;
+ }
+ extendedAPDU = arg_get_lit(6);
+ le = arg_get_int_def(7, 0);
+
+ if (makeAPDU) {
+ uint8_t apdudata[USB_CMD_DATA_SIZE] = {0};
+ int apdudatalen = 0;
+
+ CLIGetHexBLessWithReturn(8, apdudata, &apdudatalen, 1 + 2);
+
+ APDUStruct apdu;
+ apdu.cla = header[0];
+ apdu.ins = header[1];
+ apdu.p1 = header[2];
+ apdu.p2 = header[3];
+
+ apdu.lc = apdudatalen;
+ apdu.data = apdudata;
+
+ apdu.extended_apdu = extendedAPDU;
+ apdu.le = le;
+
+ if (APDUEncode(&apdu, data, &datalen)) {
+ PrintAndLogEx(ERR, "can't make apdu with provided parameters.");
+ CLIParserFree();
+ return 2;
+ }
+ } else {
+ if (extendedAPDU) {
+ PrintAndLogEx(ERR, "`-e` without `-m`.");
+ CLIParserFree();
+ return 3;
+ }
+ if (le > 0) {
+ PrintAndLogEx(ERR, "`-l` without `-m`.");
+ CLIParserFree();
+ return 3;
+ }
+
+ // len = data + PCB(1b) + CRC(2b)
+ CLIGetHexBLessWithReturn(8, data, &datalen, 1 + 2);
+ }
+
+ CLIParserFree();
+// PrintAndLog("---str [%d] %s", arg_get_str(4)->count, arg_get_str(4)->sval[0]);
+ PrintAndLogEx(NORMAL, ">>>>[%s%s%s] %s", activateField ? "sel ": "", leaveSignalON ? "keep ": "", decodeTLV ? "TLV": "", sprint_hex(data, datalen));
+
+ if (decodeAPDU) {
+ APDUStruct apdu;
+
+ if (APDUDecode(data, datalen, &apdu) == 0)
+ APDUPrint(apdu);
+ else
+ PrintAndLogEx(WARNING, "can't decode APDU.");
+ }
+
+ res = ExchangeAPDU14a(data, datalen, activateField, leaveSignalON, data, USB_CMD_DATA_SIZE, &datalen);
+
+ if (res)
+ return res;
+
+ PrintAndLog("<<<< %s", sprint_hex(data, datalen));
+
+ PrintAndLog("APDU response: %02x %02x - %s", data[datalen - 2], data[datalen - 1], GetAPDUCodeDescription(data[datalen - 2], data[datalen - 1]));
+
+ // TLV decoder
+ if (decodeTLV && datalen > 4) {
+ TLVPrintFromBuffer(data, datalen - 2);
+ }
+
+ return 0;