1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2017 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
15 #include "util_posix.h"
16 #include "protocols.h"
29 #include "cmdsmartcard.h"
33 // Got from here. Thanks)
34 // https://eftlab.co.uk/index.php/site-map/knowledge-base/211-emv-aid-rid-pix
35 static const char *PSElist
[] = {
36 "325041592E5359532E4444463031", // 2PAY.SYS.DDF01 - Visa Proximity Payment System Environment - PPSE
37 "315041592E5359532E4444463031" // 1PAY.SYS.DDF01 - Visa Payment System Environment - PSE
39 //static const size_t PSElistLen = sizeof(PSElist)/sizeof(char*);
41 char *TransactionTypeStr
[] = {
49 enum CardPSVendor vendor
;
53 static const TAIDList AIDlist
[] = {
55 { CV_VISA
, "A00000000305076010"}, // VISA ELO Credit
56 { CV_VISA
, "A0000000031010" }, // VISA Debit/Credit (Classic)
57 { CV_VISA
, "A000000003101001" }, // VISA Credit
58 { CV_VISA
, "A000000003101002" }, // VISA Debit
59 { CV_VISA
, "A0000000032010" }, // VISA Electron
60 { CV_VISA
, "A0000000032020" }, // VISA
61 { CV_VISA
, "A0000000033010" }, // VISA Interlink
62 { CV_VISA
, "A0000000034010" }, // VISA Specific
63 { CV_VISA
, "A0000000035010" }, // VISA Specific
64 { CV_VISA
, "A0000000036010" }, // Domestic Visa Cash Stored Value
65 { CV_VISA
, "A0000000036020" }, // International Visa Cash Stored Value
66 { CV_VISA
, "A0000000038002" }, // VISA Auth, VisaRemAuthen EMV-CAP (DPA)
67 { CV_VISA
, "A0000000038010" }, // VISA Plus
68 { CV_VISA
, "A0000000039010" }, // VISA Loyalty
69 { CV_VISA
, "A000000003999910" }, // VISA Proprietary ATM
71 { CV_VISA
, "A000000098" }, // Debit Card
72 { CV_VISA
, "A0000000980848" }, // Debit Card
73 // Mastercard International
74 { CV_MASTERCARD
, "A00000000401" }, // MasterCard PayPass
75 { CV_MASTERCARD
, "A0000000041010" }, // MasterCard Credit
76 { CV_MASTERCARD
, "A00000000410101213" }, // MasterCard Credit
77 { CV_MASTERCARD
, "A00000000410101215" }, // MasterCard Credit
78 { CV_MASTERCARD
, "A0000000042010" }, // MasterCard Specific
79 { CV_MASTERCARD
, "A0000000043010" }, // MasterCard Specific
80 { CV_MASTERCARD
, "A0000000043060" }, // Maestro (Debit)
81 { CV_MASTERCARD
, "A000000004306001" }, // Maestro (Debit)
82 { CV_MASTERCARD
, "A0000000044010" }, // MasterCard Specific
83 { CV_MASTERCARD
, "A0000000045010" }, // MasterCard Specific
84 { CV_MASTERCARD
, "A0000000046000" }, // Cirrus
85 { CV_MASTERCARD
, "A0000000048002" }, // SecureCode Auth EMV-CAP
86 { CV_MASTERCARD
, "A0000000049999" }, // MasterCard PayPass
87 { CV_MASTERCARD
, "B012345678" }, // Maestro TEST Used for development
89 { CV_AMERICANEXPRESS
, "A000000025" },
90 { CV_AMERICANEXPRESS
, "A0000000250000" },
91 { CV_AMERICANEXPRESS
, "A00000002501" },
92 { CV_AMERICANEXPRESS
, "A000000025010402" },
93 { CV_AMERICANEXPRESS
, "A000000025010701" },
94 { CV_AMERICANEXPRESS
, "A000000025010801" },
95 // Groupement des Cartes Bancaires "CB"
96 { CV_CB
, "A0000000421010" }, // Cartes Bancaire EMV Card
97 { CV_CB
, "A0000000422010" },
98 { CV_CB
, "A0000000423010" },
99 { CV_CB
, "A0000000424010" },
100 { CV_CB
, "A0000000425010" },
102 { CV_JCB
, "A00000006510" }, // JCB
103 { CV_JCB
, "A0000000651010" }, // JCB J Smart Credit
104 // Switch Card Services Ltd.
105 { CV_SWITCH
, "A0000000050001" }, // Maestro UK
106 { CV_SWITCH
, "A0000000050002" }, // Solo
107 // Diners Club International Ltd.
108 { CV_DINERS
, "A0000001523010" }, // Discover, Pulse D Pas Discover Card
109 { CV_DINERS
, "A0000001524010" }, // Discover, Discover Debit Common Card
111 { CV_OTHER
, "A00000002401" }, // Midland Bank Plc - Self Service
112 { CV_OTHER
, "A0000000291010" }, // LINK Interchange Network Ltd - Link / American Express
113 { CV_OTHER
, "A00000006900" }, // Société Européenne de Monnaie Electronique SEME - Moneo
114 { CV_OTHER
, "A000000077010000021000000000003B" }, // Oberthur Technologies France - Visa AEPN
115 { CV_OTHER
, "A0000001211010" }, // PBS Danmark A/S - Denmark - Dankort (VISA GEM Vision) - Danish domestic debit card
116 { CV_OTHER
, "A0000001410001" }, // Associazione Bancaria Italiana - Italy - PagoBANCOMAT - CoGeBan Consorzio BANCOMAT (Italian domestic debit card)
117 { CV_OTHER
, "A0000001544442" }, // Banricompras Debito - Banrisul - Banco do Estado do Rio Grande do SUL - S.A.
118 { CV_OTHER
, "A000000172950001" }, // Financial Information Service Co. Ltd. - Taiwan - BAROC Financial Application Taiwan- The Bankers Association of the Republic of China
119 { CV_OTHER
, "A0000001850002" }, // Post Office Limited - United Kingdom - UK Post Office Account card
120 { CV_OTHER
, "A0000002281010" }, // Saudi Arabian Monetary Agency (SAMA) - Kingdom of Saudi Arabia - SPAN (M/Chip) - SPAN2 (Saudi Payments Network) - Saudi Arabia domestic credit/debit card (Saudi Arabia Monetary Agency)
121 { CV_OTHER
, "A0000002282010" }, // Saudi Arabian Monetary Agency (SAMA) - Kingdom of Saudi Arabia - SPAN (VIS) - SPAN2 (Saudi Payments Network) - Saudi Arabia domestic credit/debit card (Saudi Arabia Monetary Agency)
122 { CV_OTHER
, "A0000002771010" }, // Interac Association - Canada - INTERAC - Canadian domestic credit/debit card
123 { CV_OTHER
, "A00000031510100528" }, // Currence Holding/PIN BV - The Netherlands- Currence PuC
124 { CV_OTHER
, "A0000003156020" }, // Currence Holding/PIN BV - The Netherlands - Chipknip
125 { CV_OTHER
, "A0000003591010028001" }, // Euro Alliance of Payment Schemes s.c.r.l. (EAPS) - Belgium - Girocard EAPS - ZKA (Germany)
126 { CV_OTHER
, "A0000003710001" }, // Verve - Nigeria - InterSwitch Verve Card - Nigerian local switch company
127 { CV_OTHER
, "A0000004540010" }, // eTranzact - Nigeria - Etranzact Genesis Card - Nigerian local switch company
128 { CV_OTHER
, "A0000004540011" }, // eTranzact - Nigeria - Etranzact Genesis Card 2 - Nigerian local switch company
129 { CV_OTHER
, "A0000004766C" }, // Google - United States - GOOGLE_PAYMENT_AID
130 { CV_OTHER
, "A0000005241010" }, // RuPay - India - RuPay - RuPay (India)
131 { CV_OTHER
, "A0000006723010" }, // TROY - Turkey - TROY chip credit card - Turkey's Payment Method
132 { CV_OTHER
, "A0000006723020" }, // TROY - Turkey - TROY chip debit card - Turkey's Payment Method
133 { CV_OTHER
, "A0000007705850" }, // Indian Oil Corporation Limited - India - XTRAPOWER Fleet Card Program - Indian Oil’s Pre Paid Program
134 { CV_OTHER
, "D27600002545500100" }, // ZKA - Germany - Girocard - ZKA Girocard (Geldkarte) (Germany)
135 { CV_OTHER
, "D5280050218002" }, // The Netherlands - ? - (Netherlands)
136 { CV_OTHER
, "D5780000021010" }, // Bankaxept Norway Bankaxept Norwegian domestic debit card
137 { CV_OTHER
, "F0000000030001" }, // BRADESCO - Brazilian Bank Banco Bradesco
139 static const size_t AIDlistLen
= sizeof(AIDlist
)/sizeof(TAIDList
);
141 static bool APDULogging
= false;
142 void SetAPDULogging(bool logging
) {
143 APDULogging
= logging
;
146 void DropFieldEx(EMVCommandChannel channel
) {
147 if (channel
== ECC_CONTACTLESS
) {
152 enum CardPSVendor
GetCardPSVendor(uint8_t * AID
, size_t AIDlen
) {
157 hex_to_buffer((uint8_t *)buf
, AID
, AIDlen
, sizeof(buf
) - 1, 0, 0, true);
159 for(int i
= 0; i
< AIDlistLen
; i
++) {
160 if (strncmp(AIDlist
[i
].aid
, buf
, strlen(AIDlist
[i
].aid
)) == 0){
161 return AIDlist
[i
].vendor
;
168 static bool print_cb(void *data
, const struct tlv
*tlv
, int level
, bool is_leaf
) {
169 emv_tag_dump(tlv
, stdout
, level
);
171 dump_buffer(tlv
->value
, tlv
->len
, stdout
, level
);
177 bool TLVPrintFromBuffer(uint8_t *data
, int datalen
) {
178 struct tlvdb
*t
= NULL
;
179 t
= tlvdb_parse_multi(data
, datalen
);
181 PrintAndLogEx(NORMAL
, "-------------------- TLV decoded --------------------");
183 tlvdb_visit(t
, print_cb
, NULL
, 0);
187 PrintAndLogEx(WARNING
, "TLV ERROR: Can't parse response as TLV tree.");
192 void TLVPrintFromTLVLev(struct tlvdb
*tlv
, int level
) {
196 tlvdb_visit(tlv
, print_cb
, NULL
, level
);
199 void TLVPrintFromTLV(struct tlvdb
*tlv
) {
200 TLVPrintFromTLVLev(tlv
, 0);
203 void TLVPrintAIDlistFromSelectTLV(struct tlvdb
*tlv
) {
204 PrintAndLogEx(NORMAL
, "|------------------|--------|-------------------------|");
205 PrintAndLogEx(NORMAL
, "| AID |Priority| Name |");
206 PrintAndLogEx(NORMAL
, "|------------------|--------|-------------------------|");
208 struct tlvdb
*ttmp
= tlvdb_find(tlv
, 0x6f);
210 PrintAndLogEx(NORMAL
, "| none |");
213 const struct tlv
*tgAID
= tlvdb_get_inchild(ttmp
, 0x84, NULL
);
214 const struct tlv
*tgName
= tlvdb_get_inchild(ttmp
, 0x50, NULL
);
215 const struct tlv
*tgPrio
= tlvdb_get_inchild(ttmp
, 0x87, NULL
);
218 PrintAndLogEx(NORMAL
, "|%s| %s |%s|",
219 sprint_hex_inrow_ex(tgAID
->value
, tgAID
->len
, 18),
220 (tgPrio
) ? sprint_hex(tgPrio
->value
, 1) : " ",
221 (tgName
) ? sprint_ascii_ex(tgName
->value
, tgName
->len
, 25) : " ");
223 ttmp
= tlvdb_find_next(ttmp
, 0x6f);
226 PrintAndLogEx(NORMAL
, "|------------------|--------|-------------------------|");
229 struct tlvdb
*GetPANFromTrack2(const struct tlv
*track2
) {
230 char track2Hex
[200] = {0};
231 uint8_t PAN
[100] = {0};
233 char *tmp
= track2Hex
;
238 for (int i
= 0; i
< track2
->len
; ++i
, tmp
+= 2)
239 sprintf(tmp
, "%02x", (unsigned int)track2
->value
[i
]);
241 int posD
= strchr(track2Hex
, 'd') - track2Hex
;
246 if (strlen(track2Hex
) % 2) {
247 track2Hex
[posD
] = 'F';
248 track2Hex
[posD
+ 1] = '\0';
251 param_gethex_to_eol(track2Hex
, 0, PAN
, sizeof(PAN
), &PANlen
);
253 return tlvdb_fixed(0x5a, PANlen
, PAN
);
256 struct tlvdb
*GetdCVVRawFromTrack2(const struct tlv
*track2
) {
257 char track2Hex
[200] = {0};
258 char dCVVHex
[100] = {0};
259 uint8_t dCVV
[100] = {0};
261 const int PINlen
= 5; // must calculated from 9F67 MSD Offset but i have not seen this tag)
262 char *tmp
= track2Hex
;
267 for (int i
= 0; i
< track2
->len
; ++i
, tmp
+= 2)
268 sprintf(tmp
, "%02x", (unsigned int)track2
->value
[i
]);
270 int posD
= strchr(track2Hex
, 'd') - track2Hex
;
274 memset(dCVVHex
, '0', 32);
276 memcpy(dCVVHex
+ 0, track2Hex
+ posD
+ PINlen
+ 11, 4);
278 memcpy(dCVVHex
+ 4, track2Hex
, 5);
280 memcpy(dCVVHex
+ 9, track2Hex
+ posD
+ 1, 4);
282 memcpy(dCVVHex
+ 13, track2Hex
+ posD
+ 5, 3);
284 param_gethex_to_eol(dCVVHex
, 0, dCVV
, sizeof(dCVV
), &dCVVlen
);
286 return tlvdb_fixed(0x02, dCVVlen
, dCVV
);
290 int EMVExchangeEx(EMVCommandChannel channel
, bool ActivateField
, bool LeaveFieldON
, uint8_t *apdu
, int apdu_len
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
, struct tlvdb
*tlv
)
298 DropFieldEx( channel
);
303 PrintAndLogEx(SUCCESS
, ">>>> %s", sprint_hex(apdu
, apdu_len
));
305 #ifdef WITH_SMARTCARD
307 case ECC_CONTACTLESS
:
308 // 6 byes + data = INS + CLA + P1 + P2 + Lc + <data = Nc> + Le(?IncludeLe)
309 res
= ExchangeAPDU14a(apdu
, apdu_len
, ActivateField
, LeaveFieldON
, Result
, (int)MaxResultLen
, (int *)ResultLen
);
312 res
= ExchangeAPDUSC(apdu
, apdu_len
, ActivateField
, LeaveFieldON
, Result
, (int)MaxResultLen
, (int *)ResultLen
);
316 res
= ExchangeAPDU14a(apdu
, apdu_len
, ActivateField
, LeaveFieldON
, Result
, (int)MaxResultLen
, (int *)ResultLen
);
324 PrintAndLogEx(SUCCESS
, "<<<< %s", sprint_hex(Result
, *ResultLen
));
326 if (*ResultLen
< 2) {
330 if (Result
[*ResultLen
-2] == 0x61) {
331 uint8_t La
= Result
[*ResultLen
-1];
332 uint8_t get_response
[5] = {apdu
[0], ISO7816_GET_RESPONSE
, 0x00, 0x00, La
};
333 size_t oldlen
= *ResultLen
;
334 res
= EMVExchangeEx(channel
, false, LeaveFieldON
, get_response
, sizeof(get_response
), &Result
[oldlen
-2], MaxResultLen
-oldlen
+2, ResultLen
, sw
, tlv
);
335 *ResultLen
+= oldlen
;
342 isw
= Result
[*ResultLen
] * 0x0100 + Result
[*ResultLen
+ 1];
348 PrintAndLogEx(ERR
, "APDU(%02x%02x) ERROR: [%4X] %s", apdu
[0], apdu
[1], isw
, GetAPDUCodeDescription(*sw
>> 8, *sw
& 0xff));
355 struct tlvdb
*t
= tlvdb_parse_multi(Result
, *ResultLen
);
362 static 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
)
364 uint8_t APDU
[APDU_COMMAND_LEN
];
365 memcpy(APDU
, apdu
, apdu_len
);
366 APDU
[apdu_len
] = 0x00;
367 if (channel
== ECC_CONTACTLESS
) {
369 // there is no Lc but an Le already
370 } else if (apdu_len
> 5 && apdu_len
== 5 + apdu
[4] + 1) {
371 // there is Lc, data and Le
373 apdu_len
++; // no Le, add Le = 0x00 because some vendors require it for contactless
376 return EMVExchangeEx(channel
, false, LeaveFieldON
, APDU
, apdu_len
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
379 int EMVSelect(EMVCommandChannel channel
, 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
)
381 uint8_t Select_APDU
[APDU_COMMAND_LEN
] = {0x00, ISO7816_SELECT_FILE
, 0x04, 0x00, AIDLen
, 0x00};
382 memcpy(Select_APDU
+ 5, AID
, AIDLen
);
383 int apdulen
= 5 + AIDLen
;
384 if (channel
== ECC_CONTACTLESS
) {
385 apdulen
++; // some vendors require Le = 0x00 for contactless operations
387 return EMVExchangeEx(channel
, ActivateField
, LeaveFieldON
, Select_APDU
, apdulen
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
390 int EMVSelectPSE(EMVCommandChannel channel
, bool ActivateField
, bool LeaveFieldON
, uint8_t PSENum
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
) {
391 uint8_t buf
[APDU_DATA_LEN
] = {0};
397 param_gethex_to_eol(PSElist
[1], 0, buf
, sizeof(buf
), &len
);
401 param_gethex_to_eol(PSElist
[0], 0, buf
, sizeof(buf
), &len
);
408 res
= EMVSelect(channel
, ActivateField
, LeaveFieldON
, buf
, len
, Result
, MaxResultLen
, ResultLen
, sw
, NULL
);
414 int EMVSelectWithRetry(EMVCommandChannel channel
, 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
) {
418 res
= EMVSelect(channel
, false, true, AID
, AIDLen
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
420 // retry if error and not returned sw error
421 if (res
&& res
!= 5) {
425 // card select error, proxmark error
427 PrintAndLogEx(WARNING
, "Exit...");
432 PrintAndLogEx(NORMAL
, "Retry failed [%s]. Skiped...", sprint_hex_inrow(AID
, AIDLen
));
436 } while (res
&& res
!= 5);
442 int EMVCheckAID(EMVCommandChannel channel
, bool decodeTLV
, struct tlvdb
*tlvdbelm
, struct tlvdb
*tlv
){
443 uint8_t data
[APDU_RESPONSE_LEN
] = {0};
449 const struct tlv
*tgAID
= tlvdb_get_inchild(tlvdbelm
, 0x4f, NULL
);
451 res
= EMVSelectWithRetry(channel
, false, true, (uint8_t *)tgAID
->value
, tgAID
->len
, data
, sizeof(data
), &datalen
, &sw
, tlv
);
453 // if returned sw error
456 tlvdbelm
= tlvdb_find_next(tlvdbelm
, 0x61);
465 PrintAndLogEx(NORMAL
, "%s:", sprint_hex_inrow(tgAID
->value
, tgAID
->len
));
466 TLVPrintFromBuffer(data
, datalen
);
469 tlvdbelm
= tlvdb_find_next(tlvdbelm
, 0x61);
475 int EMVSearchPSE(EMVCommandChannel channel
, bool ActivateField
, bool LeaveFieldON
, uint8_t PSENum
, bool decodeTLV
, struct tlvdb
*tlv
) {
476 uint8_t data
[APDU_RESPONSE_LEN
] = {0};
478 uint8_t sfidata
[0x11][APDU_RESPONSE_LEN
] = {0};
479 size_t sfidatalen
[0x11] = {0};
482 bool fileFound
= false;
484 char *PSE_or_PPSE
= PSENum
== 1 ? "PSE" : "PPSE";
487 res
= EMVSelectPSE(channel
, ActivateField
, true, PSENum
, data
, sizeof(data
), &datalen
, &sw
);
491 PrintAndLogEx(FAILED
, "Select PSE error. APDU error: %04x.", sw
);
495 struct tlvdb
*t
= NULL
;
496 t
= tlvdb_parse_multi(data
, datalen
);
499 struct tlvdb
*tsfi
= tlvdb_find_path(t
, (tlv_tag_t
[]){0x6f, 0xa5, 0x88, 0x00});
502 tlv_get_uint8(tlvdb_get_tlv(tsfi
), &sfin
);
503 PrintAndLogEx(INFO
, "* PPSE get SFI: 0x%02x.", sfin
);
505 for (uint8_t ui
= 0x01; ui
<= 0x10; ui
++) {
506 PrintAndLogEx(INFO
, "* * Get SFI: 0x%02x. num: 0x%02x", sfin
, ui
);
507 res
= EMVReadRecord(channel
, true, sfin
, ui
, sfidata
[ui
], APDU_RESPONSE_LEN
, &sfidatalen
[ui
], &sw
, NULL
);
512 PrintAndLogEx(INFO
, "* * PPSE get SFI. End of records.");
519 PrintAndLogEx(FAILED
, "PPSE get Error. APDU error: %04x.", sw
);
524 TLVPrintFromBuffer(sfidata
[ui
], sfidatalen
[ui
]);
528 for (uint8_t ui
= 0x01; ui
<= 0x10; ui
++) {
529 if (sfidatalen
[ui
]) {
530 struct tlvdb
*tsfi
= NULL
;
531 tsfi
= tlvdb_parse_multi(sfidata
[ui
], sfidatalen
[ui
]);
533 struct tlvdb
*tsfitmp
= tlvdb_find_path(tsfi
, (tlv_tag_t
[]){0x70, 0x61, 0x00});
535 PrintAndLogEx(FAILED
, "SFI 0x%02d doesn't have any records.", sfidatalen
[ui
]);
538 res
= EMVCheckAID(channel
, decodeTLV
, tsfitmp
, tlv
);
547 // PSE/PPSE plain (wo SFI)
548 struct tlvdb
*ttmp
= tlvdb_find_path(t
, (tlv_tag_t
[]){0x6f, 0xa5, 0xbf0c, 0x61, 0x00});
550 res
= EMVCheckAID(channel
, decodeTLV
, ttmp
, tlv
);
555 PrintAndLogEx(FAILED
, "PPSE doesn't have any records.");
559 PrintAndLogEx(WARNING
, "%s ERROR: Can't get TLV from response.", PSE_or_PPSE
);
562 PrintAndLogEx(WARNING
, "%s ERROR: Can't select PPSE AID. Error: %d", PSE_or_PPSE
, res
);
566 DropFieldEx( channel
);
571 int EMVSearch(EMVCommandChannel channel
, bool ActivateField
, bool LeaveFieldON
, bool decodeTLV
, struct tlvdb
*tlv
) {
572 uint8_t aidbuf
[APDU_DATA_LEN
] = {0};
574 uint8_t data
[APDU_RESPONSE_LEN
] = {0};
580 for(int i
= 0; i
< AIDlistLen
; i
++) {
581 param_gethex_to_eol(AIDlist
[i
].aid
, 0, aidbuf
, sizeof(aidbuf
), &aidlen
);
582 res
= EMVSelect(channel
, (i
== 0) ? ActivateField
: false, true, aidbuf
, aidlen
, data
, sizeof(data
), &datalen
, &sw
, tlv
);
583 // retry if error and not returned sw error
584 if (res
&& res
!= 5) {
588 // (1) - card select error, (4) reply timeout, (200) - result length = 0
589 if (res
== 1 || res
== 4 || res
== 200) {
591 DropFieldEx(channel
);
592 PrintAndLogEx(WARNING
, "Exit...");
597 PrintAndLogEx(FAILED
, "Retry failed [%s]. Skipped...", AIDlist
[i
].aid
);
610 PrintAndLogEx(SUCCESS
, "%s", AIDlist
[i
].aid
);
611 TLVPrintFromBuffer(data
, datalen
);
616 DropFieldEx(channel
);
621 int EMVSelectApplication(struct tlvdb
*tlv
, uint8_t *AID
, size_t *AIDlen
) {
622 // check priority. 0x00 - highest
627 struct tlvdb
*ttmp
= tlvdb_find(tlv
, 0x6f);
632 const struct tlv
*tgAID
= tlvdb_get_inchild(ttmp
, 0x84, NULL
);
633 const struct tlv
*tgPrio
= tlvdb_get_inchild(ttmp
, 0x87, NULL
);
639 int pt
= bytes_to_num((uint8_t*)tgPrio
->value
, (tgPrio
->len
< 2) ? tgPrio
->len
: 2);
643 memcpy(AID
, tgAID
->value
, tgAID
->len
);
644 *AIDlen
= tgAID
->len
;
647 // takes the first application from list wo priority
649 memcpy(AID
, tgAID
->value
, tgAID
->len
);
650 *AIDlen
= tgAID
->len
;
654 ttmp
= tlvdb_find_next(ttmp
, 0x6f);
660 int EMVGPO(EMVCommandChannel channel
, bool LeaveFieldON
, uint8_t *PDOL
, size_t PDOLLen
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
, struct tlvdb
*tlv
)
662 uint8_t GPO_APDU
[APDU_COMMAND_LEN
] = {0x80, ISO7816_GET_PROCESSING_OPTIONS
, 0x00, 0x00, PDOLLen
, 0x00};
663 memcpy(GPO_APDU
+ 5, PDOL
, PDOLLen
);
664 int apdulen
= 5 + PDOLLen
;
666 return EMVExchange(channel
, LeaveFieldON
, GPO_APDU
, apdulen
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
669 int EMVReadRecord(EMVCommandChannel channel
, bool LeaveFieldON
, uint8_t SFI
, uint8_t SFIrec
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
, struct tlvdb
*tlv
)
671 uint8_t read_APDU
[5] = {0x00, ISO7816_READ_RECORDS
, SFIrec
, (SFI
<< 3) | 0x04, 0x00};
672 int res
= EMVExchange(channel
, LeaveFieldON
, read_APDU
, sizeof(read_APDU
), Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
674 PrintAndLogEx(INFO
, ">>> trying to reissue command withouth Le...");
675 res
= EMVExchangeEx(channel
, false, LeaveFieldON
, read_APDU
, sizeof(read_APDU
), Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
680 int EMVAC(EMVCommandChannel channel
, 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
)
682 uint8_t CDOL_APDU
[APDU_COMMAND_LEN
] = {0x80, 0xae, RefControl
, 0x00, CDOLLen
, 0x00};
683 memcpy(CDOL_APDU
+ 5, CDOL
, CDOLLen
);
684 int apdulen
= 5 + CDOLLen
;
686 return EMVExchange(channel
, LeaveFieldON
, CDOL_APDU
, apdulen
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
689 int EMVGenerateChallenge(EMVCommandChannel channel
, bool LeaveFieldON
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
, struct tlvdb
*tlv
)
691 uint8_t get_challenge_APDU
[APDU_COMMAND_LEN
] = {0x00, ISO7816_GET_CHALLENGE
, 0x00, 0x00};
693 int res
= EMVExchange(channel
, LeaveFieldON
, get_challenge_APDU
, 4, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
695 PrintAndLogEx(INFO
, ">>> trying to reissue command withouth Le...");
696 res
= EMVExchangeEx(channel
, false, LeaveFieldON
, get_challenge_APDU
, 4, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
701 int EMVInternalAuthenticate(EMVCommandChannel channel
, bool LeaveFieldON
, uint8_t *DDOL
, size_t DDOLLen
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
, struct tlvdb
*tlv
)
703 uint8_t authenticate_APDU
[APDU_COMMAND_LEN
] = {0x00, ISO7816_INTERNAL_AUTHENTICATE
, 0x00, 0x00, DDOLLen
, 0x00};
704 memcpy(authenticate_APDU
+ 5, DDOL
, DDOLLen
);
705 int apdulen
= 5 + DDOLLen
;
707 return EMVExchange(channel
, LeaveFieldON
, authenticate_APDU
, apdulen
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
710 int MSCComputeCryptoChecksum(EMVCommandChannel channel
, bool LeaveFieldON
, uint8_t *UDOL
, uint8_t UDOLlen
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
, struct tlvdb
*tlv
)
712 uint8_t checksum_APDU
[APDU_COMMAND_LEN
] = {0x80, 0x2a, 0x8e, 0x80, UDOLlen
, 0x00};
713 memcpy(checksum_APDU
+ 5, UDOL
, UDOLlen
);
714 int apdulen
= 5 + UDOLlen
;
716 return EMVExchange(channel
, LeaveFieldON
, checksum_APDU
, apdulen
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
720 struct emv_pk
*get_ca_pk(struct tlvdb
*db
) {
721 const struct tlv
*df_tlv
= tlvdb_get(db
, 0x84, NULL
);
722 const struct tlv
*caidx_tlv
= tlvdb_get(db
, 0x8f, NULL
);
724 if (!df_tlv
|| !caidx_tlv
|| df_tlv
->len
< 6 || caidx_tlv
->len
!= 1)
727 PrintAndLogEx(NORMAL
, "CA Public Key index 0x%0x", caidx_tlv
->value
[0]);
728 return emv_pk_get_ca_pk(df_tlv
->value
, caidx_tlv
->value
[0]);
731 int trSDA(struct tlvdb
*tlv
) {
733 struct emv_pk
*pk
= get_ca_pk(tlv
);
735 PrintAndLogEx(WARNING
, "Error: Key not found. Exit.");
739 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlv
);
742 PrintAndLogEx(WARNING
, "Error: Issuer certificate not found. Exit.");
746 PrintAndLogEx(SUCCESS
, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx",
753 issuer_pk
->serial
[0],
754 issuer_pk
->serial
[1],
758 const struct tlv
*sda_tlv
= tlvdb_get(tlv
, 0x21, NULL
);
759 if (!sda_tlv
|| sda_tlv
->len
< 1) {
760 emv_pk_free(issuer_pk
);
762 PrintAndLogEx(WARNING
, "Can't find input list for Offline Data Authentication. Exit.");
766 struct tlvdb
*dac_db
= emv_pki_recover_dac(issuer_pk
, tlv
, sda_tlv
);
768 const struct tlv
*dac_tlv
= tlvdb_get(dac_db
, 0x9f45, NULL
);
769 PrintAndLogEx(NORMAL
, "SDA verified OK. (Data Authentication Code: %02hhx:%02hhx)\n", dac_tlv
->value
[0], dac_tlv
->value
[1]);
770 tlvdb_add(tlv
, dac_db
);
772 emv_pk_free(issuer_pk
);
774 PrintAndLogEx(WARNING
, "SSAD verify error");
778 emv_pk_free(issuer_pk
);
783 static const unsigned char default_ddol_value
[] = {0x9f, 0x37, 0x04};
784 static struct tlv default_ddol_tlv
= {.tag
= 0x9f49, .len
= 3, .value
= default_ddol_value
};
786 int trDDA(EMVCommandChannel channel
, bool decodeTLV
, struct tlvdb
*tlv
) {
787 uint8_t buf
[APDU_RESPONSE_LEN
] = {0};
791 struct emv_pk
*pk
= get_ca_pk(tlv
);
793 PrintAndLogEx(WARNING
, "Error: Key not found. Exit.");
797 const struct tlv
*sda_tlv
= tlvdb_get(tlv
, 0x21, NULL
);
798 /* if (!sda_tlv || sda_tlv->len < 1) { it may be 0!!!!
800 PrintAndLogEx(WARNING, "Error: Can't find input list for Offline Data Authentication. Exit.");
804 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlv
);
807 PrintAndLogEx(WARNING
, "Error: Issuer certificate not found. Exit.");
810 PrintAndLogEx(SUCCESS
, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
817 issuer_pk
->serial
[0],
818 issuer_pk
->serial
[1],
822 struct emv_pk
*icc_pk
= emv_pki_recover_icc_cert(issuer_pk
, tlv
, sda_tlv
);
825 emv_pk_free(issuer_pk
);
826 PrintAndLogEx(WARNING
, "Error: ICC certificate not found. Exit.");
829 PrintAndLogEx(SUCCESS
, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
841 if (tlvdb_get(tlv
, 0x9f2d, NULL
)) {
842 struct emv_pk
*icc_pe_pk
= emv_pki_recover_icc_pe_cert(issuer_pk
, tlv
);
844 PrintAndLogEx(WARNING
, "WARNING: ICC PE PK recover error. ");
846 PrintAndLogEx(SUCCESS
, "ICC PE PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
853 icc_pe_pk
->serial
[0],
854 icc_pe_pk
->serial
[1],
859 PrintAndLogEx(INFO
, "ICC PE PK (PIN Encipherment Public Key Certificate) not found.\n");
862 // 9F4B: Signed Dynamic Application Data
863 const struct tlv
*sdad_tlv
= tlvdb_get(tlv
, 0x9f4b, NULL
);
864 // DDA with internal authenticate OR fDDA with filled 0x9F4B tag (GPO result)
865 // EMV kernel3 v2.4, contactless book C-3, C.1., page 147
867 PrintAndLogEx(NORMAL
, "\n* * Got Signed Dynamic Application Data (9F4B) form GPO. Maybe fDDA...");
869 const struct tlvdb
*atc_db
= emv_pki_recover_atc_ex(icc_pk
, tlv
, true);
871 PrintAndLogEx(WARNING
, "Error: Can't recover IDN (ICC Dynamic Number)");
873 emv_pk_free(issuer_pk
);
878 // 9f36 Application Transaction Counter (ATC)
879 const struct tlv
*atc_tlv
= tlvdb_get(atc_db
, 0x9f36, NULL
);
881 PrintAndLogEx(NORMAL
, "\nATC (Application Transaction Counter) [%zu] %s", atc_tlv
->len
, sprint_hex_inrow(atc_tlv
->value
, atc_tlv
->len
));
883 const struct tlv
*core_atc_tlv
= tlvdb_get(tlv
, 0x9f36, NULL
);
884 if(tlv_equal(core_atc_tlv
, atc_tlv
)) {
885 PrintAndLogEx(SUCCESS
, "ATC check OK.");
886 PrintAndLogEx(SUCCESS
, "fDDA (fast DDA) verified OK.");
888 PrintAndLogEx(WARNING
, "Error: fDDA verified, but ATC in the certificate and ATC in the record not the same.");
891 PrintAndLogEx(NORMAL
, "\nERROR: fDDA (fast DDA) verify error");
893 emv_pk_free(issuer_pk
);
898 struct tlvdb
*dac_db
= emv_pki_recover_dac(issuer_pk
, tlv
, sda_tlv
);
900 const struct tlv
*dac_tlv
= tlvdb_get(dac_db
, 0x9f45, NULL
);
901 PrintAndLogEx(NORMAL
, "SDAD verified OK. (Data Authentication Code: %02hhx:%02hhx)\n", dac_tlv
->value
[0], dac_tlv
->value
[1]);
902 tlvdb_add(tlv
, dac_db
);
904 PrintAndLogEx(WARNING
, "Error: SSAD verify error");
906 emv_pk_free(issuer_pk
);
911 PrintAndLogEx(NORMAL
, "\n* Calc DDOL");
912 const struct tlv
*ddol_tlv
= tlvdb_get(tlv
, 0x9f49, NULL
);
914 ddol_tlv
= &default_ddol_tlv
;
915 PrintAndLogEx(NORMAL
, "DDOL [9f49] not found. Using default DDOL");
918 struct tlv
*ddol_data_tlv
= dol_process(ddol_tlv
, tlv
, 0);
919 if (!ddol_data_tlv
) {
920 PrintAndLogEx(WARNING
, "Error: Can't create DDOL TLV");
922 emv_pk_free(issuer_pk
);
927 PrintAndLogEx(NORMAL
, "DDOL data[%d]: %s", ddol_data_tlv
->len
, sprint_hex(ddol_data_tlv
->value
, ddol_data_tlv
->len
));
929 PrintAndLogEx(NORMAL
, "\n* Internal Authenticate");
930 int res
= EMVInternalAuthenticate(channel
, true, (uint8_t *)ddol_data_tlv
->value
, ddol_data_tlv
->len
, buf
, sizeof(buf
), &len
, &sw
, NULL
);
932 PrintAndLogEx(WARNING
, "Internal Authenticate error(%d): %4x. Exit...", res
, sw
);
935 emv_pk_free(issuer_pk
);
940 struct tlvdb
*dda_db
= NULL
;
941 if (buf
[0] == 0x80) {
943 PrintAndLogEx(WARNING
, "Error: Internal Authenticate format1 parsing error. length=%d", len
);
945 // parse response 0x80
946 struct tlvdb
*t80
= tlvdb_parse_multi(buf
, len
);
947 const struct tlv
* t80tlv
= tlvdb_get_tlv(t80
);
949 // 9f4b Signed Dynamic Application Data
950 dda_db
= tlvdb_fixed(0x9f4b, t80tlv
->len
, t80tlv
->value
);
951 tlvdb_add(tlv
, dda_db
);
956 PrintAndLogEx(NORMAL
, "* * Decode response format 1:");
957 TLVPrintFromTLV(dda_db
);
961 dda_db
= tlvdb_parse_multi(buf
, len
);
963 PrintAndLogEx(WARNING
, "Error: Can't parse Internal Authenticate result as TLV");
966 emv_pk_free(issuer_pk
);
970 tlvdb_add(tlv
, dda_db
);
973 TLVPrintFromTLV(dda_db
);
976 struct tlvdb
*idn_db
= emv_pki_recover_idn_ex(icc_pk
, dda_db
, ddol_data_tlv
, true);
979 PrintAndLogEx(WARNING
, "Error: Can't recover IDN (ICC Dynamic Number)");
982 emv_pk_free(issuer_pk
);
988 // 9f4c ICC Dynamic Number
989 const struct tlv
*idn_tlv
= tlvdb_get(idn_db
, 0x9f4c, NULL
);
991 PrintAndLogEx(NORMAL
, "\nIDN (ICC Dynamic Number) [%zu] %s", idn_tlv
->len
, sprint_hex_inrow(idn_tlv
->value
, idn_tlv
->len
));
992 PrintAndLogEx(NORMAL
, "DDA verified OK.");
993 tlvdb_add(tlv
, idn_db
);
996 PrintAndLogEx(NORMAL
, "\nERROR: DDA verify error");
1000 emv_pk_free(issuer_pk
);
1001 emv_pk_free(icc_pk
);
1007 emv_pk_free(issuer_pk
);
1008 emv_pk_free(icc_pk
);
1012 int trCDA(struct tlvdb
*tlv
, struct tlvdb
*ac_tlv
, struct tlv
*pdol_data_tlv
, struct tlv
*ac_data_tlv
) {
1014 struct emv_pk
*pk
= get_ca_pk(tlv
);
1016 PrintAndLogEx(WARNING
, "Error: Key not found. Exit.");
1020 const struct tlv
*sda_tlv
= tlvdb_get(tlv
, 0x21, NULL
);
1021 if (!sda_tlv
|| sda_tlv
->len
< 1) {
1022 PrintAndLogEx(WARNING
, "Error: Can't find input list for Offline Data Authentication. Exit.");
1027 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlv
);
1029 PrintAndLogEx(WARNING
, "Error: Issuer certificate not found. Exit.");
1033 PrintAndLogEx(SUCCESS
, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
1040 issuer_pk
->serial
[0],
1041 issuer_pk
->serial
[1],
1042 issuer_pk
->serial
[2]
1045 struct emv_pk
*icc_pk
= emv_pki_recover_icc_cert(issuer_pk
, tlv
, sda_tlv
);
1047 PrintAndLogEx(WARNING
, "Error: ICC certificate not found. Exit.");
1049 emv_pk_free(issuer_pk
);
1052 PrintAndLogEx(SUCCESS
, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
1064 struct tlvdb
*dac_db
= emv_pki_recover_dac(issuer_pk
, tlv
, sda_tlv
);
1066 const struct tlv
*dac_tlv
= tlvdb_get(dac_db
, 0x9f45, NULL
);
1067 PrintAndLogEx(NORMAL
, "SSAD verified OK. (%02hhx:%02hhx)", dac_tlv
->value
[0], dac_tlv
->value
[1]);
1068 tlvdb_add(tlv
, dac_db
);
1070 PrintAndLogEx(WARNING
, "Error: SSAD verify error");
1072 emv_pk_free(issuer_pk
);
1073 emv_pk_free(icc_pk
);
1077 PrintAndLogEx(NORMAL
, "\n* * Check Signed Dynamic Application Data (SDAD)");
1078 struct tlvdb
*idn_db
= emv_pki_perform_cda_ex(icc_pk
, tlv
, ac_tlv
,
1079 pdol_data_tlv
, // pdol
1080 ac_data_tlv
, // cdol1
1084 const struct tlv
*idn_tlv
= tlvdb_get(idn_db
, 0x9f4c, NULL
);
1085 PrintAndLogEx(NORMAL
, "\nIDN (ICC Dynamic Number) [%zu] %s", idn_tlv
->len
, sprint_hex_inrow(idn_tlv
->value
, idn_tlv
->len
));
1086 PrintAndLogEx(NORMAL
, "CDA verified OK.");
1087 tlvdb_add(tlv
, idn_db
);
1089 PrintAndLogEx(NORMAL
, "\nERROR: CDA verify error");
1093 emv_pk_free(issuer_pk
);
1094 emv_pk_free(icc_pk
);
1098 int RecoveryCertificates(struct tlvdb
*tlvRoot
, json_t
*root
) {
1100 struct emv_pk
*pk
= get_ca_pk(tlvRoot
);
1102 PrintAndLog("ERROR: Key not found. Exit.");
1106 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlvRoot
);
1109 PrintAndLog("WARNING: Issuer certificate not found. Exit.");
1112 PrintAndLogEx(SUCCESS
, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx",
1119 issuer_pk
->serial
[0],
1120 issuer_pk
->serial
[1],
1121 issuer_pk
->serial
[2]
1124 JsonSaveBufAsHex(root
, "$.ApplicationData.RID", issuer_pk
->rid
, 5);
1126 char *issuer_pk_c
= emv_pk_dump_pk(issuer_pk
);
1127 JsonSaveStr(root
, "$.ApplicationData.IssuerPublicKeyDec", issuer_pk_c
);
1128 JsonSaveBufAsHex(root
, "$.ApplicationData.IssuerPublicKeyModulus", issuer_pk
->modulus
, issuer_pk
->mlen
);
1131 struct emv_pk
*icc_pk
= emv_pki_recover_icc_cert(issuer_pk
, tlvRoot
, NULL
);
1134 emv_pk_free(issuer_pk
);
1135 PrintAndLogEx(WARNING
, "WARNING: ICC certificate not found. Exit.");
1138 PrintAndLogEx(SUCCESS
, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
1150 char *icc_pk_c
= emv_pk_dump_pk(icc_pk
);
1151 JsonSaveStr(root
, "$.ApplicationData.ICCPublicKeyDec", icc_pk_c
);
1152 JsonSaveBufAsHex(root
, "$.ApplicationData.ICCPublicKeyModulus", icc_pk
->modulus
, icc_pk
->mlen
);