+
+void TLVPrintFromTLVLev(struct tlvdb *tlv, int level) {
+ if (!tlv)
+ return;
+
+ tlvdb_visit(tlv, print_cb, NULL, level);
+}
+
+void TLVPrintFromTLV(struct tlvdb *tlv) {
+ TLVPrintFromTLVLev(tlv, 0);
+}
+
+void TLVPrintAIDlistFromSelectTLV(struct tlvdb *tlv) {
+ PrintAndLog("|------------------|--------|-------------------------|");
+ PrintAndLog("| AID |Priority| Name |");
+ PrintAndLog("|------------------|--------|-------------------------|");
+
+ struct tlvdb *ttmp = tlvdb_find(tlv, 0x6f);
+ if (!ttmp)
+ PrintAndLog("| none |");
+
+ while (ttmp) {
+ const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x84, NULL);
+ const struct tlv *tgName = tlvdb_get_inchild(ttmp, 0x50, NULL);
+ const struct tlv *tgPrio = tlvdb_get_inchild(ttmp, 0x87, NULL);
+ if (!tgAID)
+ break;
+ PrintAndLog("|%s| %s |%s|",
+ sprint_hex_inrow_ex(tgAID->value, tgAID->len, 18),
+ (tgPrio) ? sprint_hex(tgPrio->value, 1) : " ",
+ (tgName) ? sprint_ascii_ex(tgName->value, tgName->len, 25) : " ");
+
+ ttmp = tlvdb_find_next(ttmp, 0x6f);
+ }
+
+ PrintAndLog("|------------------|--------|-------------------------|");
+}
+
+struct tlvdb *GetPANFromTrack2(const struct tlv *track2) {
+ char track2Hex[200] = {0};
+ uint8_t PAN[100] = {0};
+ int PANlen = 0;
+ char *tmp = track2Hex;
+
+ if (!track2)
+ return NULL;
+
+ for (int i = 0; i < track2->len; ++i, tmp += 2)
+ sprintf(tmp, "%02x", (unsigned int)track2->value[i]);
+
+ int posD = strchr(track2Hex, 'd') - track2Hex;
+ if (posD < 1)
+ return NULL;
+
+ track2Hex[posD] = 0;
+ if (strlen(track2Hex) % 2) {
+ track2Hex[posD] = 'F';
+ track2Hex[posD + 1] = '\0';
+ }
+
+ param_gethex_to_eol(track2Hex, 0, PAN, sizeof(PAN), &PANlen);
+
+ return tlvdb_fixed(0x5a, PANlen, PAN);
+}
+
+struct tlvdb *GetdCVVRawFromTrack2(const struct tlv *track2) {
+ char track2Hex[200] = {0};
+ char dCVVHex[100] = {0};
+ uint8_t dCVV[100] = {0};
+ int dCVVlen = 0;
+ const int PINlen = 5; // must calculated from 9F67 MSD Offset but i have not seen this tag)
+ char *tmp = track2Hex;
+
+ if (!track2)
+ return NULL;
+
+ for (int i = 0; i < track2->len; ++i, tmp += 2)
+ sprintf(tmp, "%02x", (unsigned int)track2->value[i]);
+
+ int posD = strchr(track2Hex, 'd') - track2Hex;
+ if (posD < 1)
+ return NULL;
+
+ memset(dCVVHex, '0', 32);
+ // ATC
+ memcpy(dCVVHex + 0, track2Hex + posD + PINlen + 11, 4);
+ // PAN 5 hex
+ memcpy(dCVVHex + 4, track2Hex, 5);
+ // expire date
+ memcpy(dCVVHex + 9, track2Hex + posD + 1, 4);
+ // service code
+ memcpy(dCVVHex + 13, track2Hex + posD + 5, 3);
+
+ param_gethex_to_eol(dCVVHex, 0, dCVV, sizeof(dCVV), &dCVVlen);
+
+ return tlvdb_fixed(0x02, dCVVlen, dCVV);
+}
+
+int EMVExchangeEx(bool ActivateField, bool LeaveFieldON, sAPDU apdu, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
+ uint8_t data[APDU_RES_LEN] = {0};
+
+ *ResultLen = 0;
+ if (sw) *sw = 0;
+ uint16_t isw = 0;
+
+ if (ActivateField)
+ DropField();
+
+ // COMPUTE APDU
+ memcpy(data, &apdu, 5);
+ if (apdu.data)
+ memcpy(&data[5], apdu.data, apdu.Lc);
+
+ if (APDULogging)
+ PrintAndLog(">>>> %s", sprint_hex(data, 6 + apdu.Lc));
+
+ // 6 byes + data = INS + CLA + P1 + P2 + Lc + <data = Nc> + Le
+ int res = ExchangeAPDU14a(data, 6 + apdu.Lc, ActivateField, LeaveFieldON, Result, (int)MaxResultLen, (int *)ResultLen);
+
+ if (res) {
+ return res;
+ }
+
+ if (APDULogging)
+ PrintAndLog("<<<< %s", sprint_hex(Result, *ResultLen));
+
+ *ResultLen -= 2;
+ isw = Result[*ResultLen] * 0x0100 + Result[*ResultLen + 1];
+ if (sw)
+ *sw = isw;
+
+ if (isw != 0x9000) {
+ if (APDULogging)
+ PrintAndLog("APDU(%02x%02x) ERROR: [%4X] %s", apdu.CLA, apdu.INS, isw, GetAPDUCodeDescription(*sw >> 8, *sw & 0xff));
+ return 5;
+ }
+
+ // add to tlv tree
+ if (tlv) {
+ struct tlvdb *t = tlvdb_parse_multi(Result, *ResultLen);
+ tlvdb_add(tlv, t);
+ }
+
+ return 0;
+}
+
+int EMVExchange(bool LeaveFieldON, sAPDU apdu, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
+ return EMVExchangeEx(false, LeaveFieldON, apdu, Result, MaxResultLen, ResultLen, sw, tlv);
+}
+
+int EMVSelect(bool ActivateField, bool LeaveFieldON, uint8_t *AID, size_t AIDLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
+ return EMVExchangeEx(ActivateField, LeaveFieldON, (sAPDU){0x00, 0xa4, 0x04, 0x00, AIDLen, AID}, Result, MaxResultLen, ResultLen, sw, tlv);
+}
+
+int EMVSelectPSE(bool ActivateField, bool LeaveFieldON, uint8_t PSENum, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw) {
+ uint8_t buf[APDU_AID_LEN] = {0};
+ *ResultLen = 0;
+ int len = 0;
+ int res = 0;
+ switch (PSENum) {
+ case 1:
+ param_gethex_to_eol(PSElist[1], 0, buf, sizeof(buf), &len);
+ break;
+ case 2:
+ param_gethex_to_eol(PSElist[0], 0, buf, sizeof(buf), &len);
+ break;
+ default:
+ return -1;
+ }
+
+ // select
+ res = EMVSelect(ActivateField, LeaveFieldON, buf, len, Result, MaxResultLen, ResultLen, sw, NULL);
+
+ return res;
+}
+
+int EMVSearchPSE(bool ActivateField, bool LeaveFieldON, bool decodeTLV, struct tlvdb *tlv) {
+ uint8_t data[APDU_RES_LEN] = {0};
+ size_t datalen = 0;
+ uint16_t sw = 0;
+ int res;
+
+ // select PPSE
+ res = EMVSelectPSE(ActivateField, true, 2, data, sizeof(data), &datalen, &sw);
+
+ if (!res){
+ struct tlvdb *t = NULL;
+ t = tlvdb_parse_multi(data, datalen);
+ if (t) {
+ int retrycnt = 0;
+ struct tlvdb *ttmp = tlvdb_find_path(t, (tlv_tag_t[]){0x6f, 0xa5, 0xbf0c, 0x61, 0x00});
+ if (!ttmp)
+ PrintAndLog("PPSE don't have records.");
+
+ while (ttmp) {
+ const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x4f, NULL);
+ if (tgAID) {
+ res = EMVSelect(false, true, (uint8_t *)tgAID->value, tgAID->len, data, sizeof(data), &datalen, &sw, tlv);
+
+ // retry if error and not returned sw error
+ if (res && res != 5) {
+ if (++retrycnt < 3){
+ continue;
+ } else {
+ // card select error, proxmark error
+ if (res == 1) {
+ PrintAndLog("Exit...");
+ return 1;
+ }
+
+ retrycnt = 0;
+ PrintAndLog("Retry failed [%s]. Skiped...", sprint_hex_inrow(tgAID->value, tgAID->len));
+ }
+
+ // next element
+ ttmp = tlvdb_find_next(ttmp, 0x61);
+ continue;
+ }
+ retrycnt = 0;
+
+ // all is ok
+ if (decodeTLV){
+ PrintAndLog("%s:", sprint_hex_inrow(tgAID->value, tgAID->len));
+ TLVPrintFromBuffer(data, datalen);
+ }
+ }
+
+ ttmp = tlvdb_find_next(ttmp, 0x61);
+ }
+
+ tlvdb_free(t);
+ } else {
+ PrintAndLog("PPSE ERROR: Can't get TLV from response.");
+ }
+ } else {
+ PrintAndLog("PPSE ERROR: Can't select PPSE AID. Error: %d", res);
+ }
+
+ if(!LeaveFieldON)
+ DropField();
+
+ return res;
+}
+
+int EMVSearch(bool ActivateField, bool LeaveFieldON, bool decodeTLV, struct tlvdb *tlv) {
+ uint8_t aidbuf[APDU_AID_LEN] = {0};
+ int aidlen = 0;
+ uint8_t data[APDU_RES_LEN] = {0};
+ size_t datalen = 0;
+ uint16_t sw = 0;
+
+ int res = 0;
+ int retrycnt = 0;
+ for(int i = 0; i < AIDlistLen; i ++) {
+ param_gethex_to_eol(AIDlist[i].aid, 0, aidbuf, sizeof(aidbuf), &aidlen);
+ res = EMVSelect((i == 0) ? ActivateField : false, (i == AIDlistLen - 1) ? LeaveFieldON : true, aidbuf, aidlen, data, sizeof(data), &datalen, &sw, tlv);
+ // retry if error and not returned sw error
+ if (res && res != 5) {
+ if (++retrycnt < 3){
+ i--;
+ } else {
+ // card select error, proxmark error
+ if (res == 1) {
+ PrintAndLog("Exit...");
+ return 1;
+ }
+
+ retrycnt = 0;
+ PrintAndLog("Retry failed [%s]. Skiped...", AIDlist[i].aid);
+ }
+ continue;
+ }
+ retrycnt = 0;
+
+ if (res)
+ continue;
+
+ if (decodeTLV){
+ PrintAndLog("%s:", AIDlist[i].aid);
+ TLVPrintFromBuffer(data, datalen);
+ }
+ }
+
+ return 0;
+}
+
+int EMVSelectApplication(struct tlvdb *tlv, uint8_t *AID, size_t *AIDlen) {
+ // check priority. 0x00 - highest
+ int prio = 0xffff;
+
+ *AIDlen = 0;
+
+ struct tlvdb *ttmp = tlvdb_find(tlv, 0x6f);
+ if (!ttmp)
+ return 1;
+
+ while (ttmp) {
+ const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x84, NULL);
+ const struct tlv *tgPrio = tlvdb_get_inchild(ttmp, 0x87, NULL);
+
+ if (!tgAID)
+ break;
+
+ if (tgPrio) {
+ int pt = bytes_to_num((uint8_t*)tgPrio->value, (tgPrio->len < 2) ? tgPrio->len : 2);
+ if (pt < prio) {
+ prio = pt;
+
+ memcpy(AID, tgAID->value, tgAID->len);
+ *AIDlen = tgAID->len;
+ }
+ } else {
+ // takes the first application from list wo priority
+ if (!*AIDlen) {
+ memcpy(AID, tgAID->value, tgAID->len);
+ *AIDlen = tgAID->len;
+ }
+ }
+
+ ttmp = tlvdb_find_next(ttmp, 0x6f);
+ }
+
+ return 0;
+}
+
+int EMVGPO(bool LeaveFieldON, uint8_t *PDOL, size_t PDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
+ return EMVExchange(LeaveFieldON, (sAPDU){0x80, 0xa8, 0x00, 0x00, PDOLLen, PDOL}, Result, MaxResultLen, ResultLen, sw, tlv);
+}
+
+int EMVReadRecord(bool LeaveFieldON, uint8_t SFI, uint8_t SFIrec, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
+ return EMVExchange(LeaveFieldON, (sAPDU){0x00, 0xb2, SFIrec, (SFI << 3) | 0x04, 0, NULL}, Result, MaxResultLen, ResultLen, sw, tlv);
+}
+
+int EMVAC(bool LeaveFieldON, uint8_t RefControl, uint8_t *CDOL, size_t CDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
+ return EMVExchange(LeaveFieldON, (sAPDU){0x80, 0xae, RefControl, 0x00, CDOLLen, CDOL}, Result, MaxResultLen, ResultLen, sw, tlv);
+}
+
+int EMVGenerateChallenge(bool LeaveFieldON, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
+ return EMVExchange(LeaveFieldON, (sAPDU){0x00, 0x84, 0x00, 0x00, 0x00, NULL}, Result, MaxResultLen, ResultLen, sw, tlv);
+}
+
+int MSCComputeCryptoChecksum(bool LeaveFieldON, uint8_t *UDOL, uint8_t UDOLlen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
+ return EMVExchange(LeaveFieldON, (sAPDU){0x80, 0x2a, 0x8e, 0x80, UDOLlen, UDOL}, Result, MaxResultLen, ResultLen, sw, tlv);
+}
+
+// Authentication
+int trSDA(uint8_t *AID, size_t AIDlen, struct tlvdb *tlv) {
+ if (AIDlen < 5)
+ return 1;
+
+ // Get public key index (0x8F)
+ //int PubKeyIndx = 0;
+
+ // Get public key from key storage
+ // GetPublicKey(AID(0..5), PubKeyIndx)
+
+ // Processing of Issuer Public Key Certificate (0x90)
+ //Certificate =
+
+ // check issuer public key certificate
+
+ // Verification of Signed Static Application Data (SSAD) (0x93)
+
+ // get 9F4A Static Data Authentication Tag List
+
+ // set Data Auth Code (9F45) from SSAD
+
+ return 0;
+}
+