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 //-----------------------------------------------------------------------------
13 #include "util_posix.h"
14 #include "protocols.h"
16 #include "cmdsmartcard.h"
19 // Got from here. Thanks)
20 // https://eftlab.co.uk/index.php/site-map/knowledge-base/211-emv-aid-rid-pix
21 static const char *PSElist
[] = {
22 "325041592E5359532E4444463031", // 2PAY.SYS.DDF01 - Visa Proximity Payment System Environment - PPSE
23 "315041592E5359532E4444463031" // 1PAY.SYS.DDF01 - Visa Payment System Environment - PSE
25 //static const size_t PSElistLen = sizeof(PSElist)/sizeof(char*);
27 char *TransactionTypeStr
[] = {
35 enum CardPSVendor vendor
;
39 static const TAIDList AIDlist
[] = {
41 { CV_VISA
, "A00000000305076010"}, // VISA ELO Credit
42 { CV_VISA
, "A0000000031010" }, // VISA Debit/Credit (Classic)
43 { CV_VISA
, "A000000003101001" }, // VISA Credit
44 { CV_VISA
, "A000000003101002" }, // VISA Debit
45 { CV_VISA
, "A0000000032010" }, // VISA Electron
46 { CV_VISA
, "A0000000032020" }, // VISA
47 { CV_VISA
, "A0000000033010" }, // VISA Interlink
48 { CV_VISA
, "A0000000034010" }, // VISA Specific
49 { CV_VISA
, "A0000000035010" }, // VISA Specific
50 { CV_VISA
, "A0000000036010" }, // Domestic Visa Cash Stored Value
51 { CV_VISA
, "A0000000036020" }, // International Visa Cash Stored Value
52 { CV_VISA
, "A0000000038002" }, // VISA Auth, VisaRemAuthen EMV-CAP (DPA)
53 { CV_VISA
, "A0000000038010" }, // VISA Plus
54 { CV_VISA
, "A0000000039010" }, // VISA Loyalty
55 { CV_VISA
, "A000000003999910" }, // VISA Proprietary ATM
57 { CV_VISA
, "A000000098" }, // Debit Card
58 { CV_VISA
, "A0000000980848" }, // Debit Card
59 // Mastercard International
60 { CV_MASTERCARD
, "A00000000401" }, // MasterCard PayPass
61 { CV_MASTERCARD
, "A0000000041010" }, // MasterCard Credit
62 { CV_MASTERCARD
, "A00000000410101213" }, // MasterCard Credit
63 { CV_MASTERCARD
, "A00000000410101215" }, // MasterCard Credit
64 { CV_MASTERCARD
, "A0000000042010" }, // MasterCard Specific
65 { CV_MASTERCARD
, "A0000000043010" }, // MasterCard Specific
66 { CV_MASTERCARD
, "A0000000043060" }, // Maestro (Debit)
67 { CV_MASTERCARD
, "A000000004306001" }, // Maestro (Debit)
68 { CV_MASTERCARD
, "A0000000044010" }, // MasterCard Specific
69 { CV_MASTERCARD
, "A0000000045010" }, // MasterCard Specific
70 { CV_MASTERCARD
, "A0000000046000" }, // Cirrus
71 { CV_MASTERCARD
, "A0000000048002" }, // SecureCode Auth EMV-CAP
72 { CV_MASTERCARD
, "A0000000049999" }, // MasterCard PayPass
73 { CV_MASTERCARD
, "B012345678" }, // Maestro TEST Used for development
75 { CV_AMERICANEXPRESS
, "A000000025" },
76 { CV_AMERICANEXPRESS
, "A0000000250000" },
77 { CV_AMERICANEXPRESS
, "A00000002501" },
78 { CV_AMERICANEXPRESS
, "A000000025010402" },
79 { CV_AMERICANEXPRESS
, "A000000025010701" },
80 { CV_AMERICANEXPRESS
, "A000000025010801" },
81 // Groupement des Cartes Bancaires "CB"
82 { CV_CB
, "A0000000421010" }, // Cartes Bancaire EMV Card
83 { CV_CB
, "A0000000422010" },
84 { CV_CB
, "A0000000423010" },
85 { CV_CB
, "A0000000424010" },
86 { CV_CB
, "A0000000425010" },
88 { CV_JCB
, "A00000006510" }, // JCB
89 { CV_JCB
, "A0000000651010" }, // JCB J Smart Credit
90 // Switch Card Services Ltd.
91 { CV_SWITCH
, "A0000000050001" }, // Maestro UK
92 { CV_SWITCH
, "A0000000050002" }, // Solo
93 // Diners Club International Ltd.
94 { CV_DINERS
, "A0000001523010" }, // Discover, Pulse D Pas Discover Card
95 { CV_DINERS
, "A0000001524010" }, // Discover, Discover Debit Common Card
97 { CV_OTHER
, "A00000002401" }, // Midland Bank Plc - Self Service
98 { CV_OTHER
, "A0000000291010" }, // LINK Interchange Network Ltd - Link / American Express
99 { CV_OTHER
, "A00000006900" }, // Société Européenne de Monnaie Electronique SEME - Moneo
100 { CV_OTHER
, "A000000077010000021000000000003B" }, // Oberthur Technologies France - Visa AEPN
101 { CV_OTHER
, "A0000001211010" }, // PBS Danmark A/S - Denmark - Dankort (VISA GEM Vision) - Danish domestic debit card
102 { CV_OTHER
, "A0000001410001" }, // Associazione Bancaria Italiana - Italy - PagoBANCOMAT - CoGeBan Consorzio BANCOMAT (Italian domestic debit card)
103 { CV_OTHER
, "A0000001544442" }, // Banricompras Debito - Banrisul - Banco do Estado do Rio Grande do SUL - S.A.
104 { CV_OTHER
, "A000000172950001" }, // Financial Information Service Co. Ltd. - Taiwan - BAROC Financial Application Taiwan- The Bankers Association of the Republic of China
105 { CV_OTHER
, "A0000001850002" }, // Post Office Limited - United Kingdom - UK Post Office Account card
106 { 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)
107 { 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)
108 { CV_OTHER
, "A0000002771010" }, // Interac Association - Canada - INTERAC - Canadian domestic credit/debit card
109 { CV_OTHER
, "A00000031510100528" }, // Currence Holding/PIN BV - The Netherlands- Currence PuC
110 { CV_OTHER
, "A0000003156020" }, // Currence Holding/PIN BV - The Netherlands - Chipknip
111 { CV_OTHER
, "A0000003591010028001" }, // Euro Alliance of Payment Schemes s.c.r.l. (EAPS) - Belgium - Girocard EAPS - ZKA (Germany)
112 { CV_OTHER
, "A0000003710001" }, // Verve - Nigeria - InterSwitch Verve Card - Nigerian local switch company
113 { CV_OTHER
, "A0000004540010" }, // eTranzact - Nigeria - Etranzact Genesis Card - Nigerian local switch company
114 { CV_OTHER
, "A0000004540011" }, // eTranzact - Nigeria - Etranzact Genesis Card 2 - Nigerian local switch company
115 { CV_OTHER
, "A0000004766C" }, // Google - United States - GOOGLE_PAYMENT_AID
116 { CV_OTHER
, "A0000005241010" }, // RuPay - India - RuPay - RuPay (India)
117 { CV_OTHER
, "A0000006723010" }, // TROY - Turkey - TROY chip credit card - Turkey's Payment Method
118 { CV_OTHER
, "A0000006723020" }, // TROY - Turkey - TROY chip debit card - Turkey's Payment Method
119 { CV_OTHER
, "A0000007705850" }, // Indian Oil Corporation Limited - India - XTRAPOWER Fleet Card Program - Indian Oil’s Pre Paid Program
120 { CV_OTHER
, "D27600002545500100" }, // ZKA - Germany - Girocard - ZKA Girocard (Geldkarte) (Germany)
121 { CV_OTHER
, "D5280050218002" }, // The Netherlands - ? - (Netherlands)
122 { CV_OTHER
, "D5780000021010" }, // Bankaxept Norway Bankaxept Norwegian domestic debit card
123 { CV_OTHER
, "F0000000030001" }, // BRADESCO - Brazilian Bank Banco Bradesco
125 static const size_t AIDlistLen
= sizeof(AIDlist
)/sizeof(TAIDList
);
127 static bool APDULogging
= false;
128 void SetAPDULogging(bool logging
) {
129 APDULogging
= logging
;
132 enum CardPSVendor
GetCardPSVendor(uint8_t * AID
, size_t AIDlen
) {
137 hex_to_buffer((uint8_t *)buf
, AID
, AIDlen
, sizeof(buf
) - 1, 0, 0, true);
139 for(int i
= 0; i
< AIDlistLen
; i
++) {
140 if (strncmp(AIDlist
[i
].aid
, buf
, strlen(AIDlist
[i
].aid
)) == 0){
141 return AIDlist
[i
].vendor
;
148 static bool print_cb(void *data
, const struct tlv
*tlv
, int level
, bool is_leaf
) {
149 emv_tag_dump(tlv
, stdout
, level
);
151 dump_buffer(tlv
->value
, tlv
->len
, stdout
, level
);
157 bool TLVPrintFromBuffer(uint8_t *data
, int datalen
) {
158 struct tlvdb
*t
= NULL
;
159 t
= tlvdb_parse_multi(data
, datalen
);
161 PrintAndLogEx(NORMAL
, "-------------------- TLV decoded --------------------");
163 tlvdb_visit(t
, print_cb
, NULL
, 0);
167 PrintAndLogEx(WARNING
, "TLV ERROR: Can't parse response as TLV tree.");
172 void TLVPrintFromTLVLev(struct tlvdb
*tlv
, int level
) {
176 tlvdb_visit(tlv
, print_cb
, NULL
, level
);
179 void TLVPrintFromTLV(struct tlvdb
*tlv
) {
180 TLVPrintFromTLVLev(tlv
, 0);
183 void TLVPrintAIDlistFromSelectTLV(struct tlvdb
*tlv
) {
184 PrintAndLogEx(NORMAL
, "|------------------|--------|-------------------------|");
185 PrintAndLogEx(NORMAL
, "| AID |Priority| Name |");
186 PrintAndLogEx(NORMAL
, "|------------------|--------|-------------------------|");
188 struct tlvdb
*ttmp
= tlvdb_find(tlv
, 0x6f);
190 PrintAndLogEx(NORMAL
, "| none |");
193 const struct tlv
*tgAID
= tlvdb_get_inchild(ttmp
, 0x84, NULL
);
194 const struct tlv
*tgName
= tlvdb_get_inchild(ttmp
, 0x50, NULL
);
195 const struct tlv
*tgPrio
= tlvdb_get_inchild(ttmp
, 0x87, NULL
);
198 PrintAndLogEx(NORMAL
, "|%s| %s |%s|",
199 sprint_hex_inrow_ex(tgAID
->value
, tgAID
->len
, 18),
200 (tgPrio
) ? sprint_hex(tgPrio
->value
, 1) : " ",
201 (tgName
) ? sprint_ascii_ex(tgName
->value
, tgName
->len
, 25) : " ");
203 ttmp
= tlvdb_find_next(ttmp
, 0x6f);
206 PrintAndLogEx(NORMAL
, "|------------------|--------|-------------------------|");
209 struct tlvdb
*GetPANFromTrack2(const struct tlv
*track2
) {
210 char track2Hex
[200] = {0};
211 uint8_t PAN
[100] = {0};
213 char *tmp
= track2Hex
;
218 for (int i
= 0; i
< track2
->len
; ++i
, tmp
+= 2)
219 sprintf(tmp
, "%02x", (unsigned int)track2
->value
[i
]);
221 int posD
= strchr(track2Hex
, 'd') - track2Hex
;
226 if (strlen(track2Hex
) % 2) {
227 track2Hex
[posD
] = 'F';
228 track2Hex
[posD
+ 1] = '\0';
231 param_gethex_to_eol(track2Hex
, 0, PAN
, sizeof(PAN
), &PANlen
);
233 return tlvdb_fixed(0x5a, PANlen
, PAN
);
236 struct tlvdb
*GetdCVVRawFromTrack2(const struct tlv
*track2
) {
237 char track2Hex
[200] = {0};
238 char dCVVHex
[100] = {0};
239 uint8_t dCVV
[100] = {0};
241 const int PINlen
= 5; // must calculated from 9F67 MSD Offset but i have not seen this tag)
242 char *tmp
= track2Hex
;
247 for (int i
= 0; i
< track2
->len
; ++i
, tmp
+= 2)
248 sprintf(tmp
, "%02x", (unsigned int)track2
->value
[i
]);
250 int posD
= strchr(track2Hex
, 'd') - track2Hex
;
254 memset(dCVVHex
, '0', 32);
256 memcpy(dCVVHex
+ 0, track2Hex
+ posD
+ PINlen
+ 11, 4);
258 memcpy(dCVVHex
+ 4, track2Hex
, 5);
260 memcpy(dCVVHex
+ 9, track2Hex
+ posD
+ 1, 4);
262 memcpy(dCVVHex
+ 13, track2Hex
+ posD
+ 5, 3);
264 param_gethex_to_eol(dCVVHex
, 0, dCVV
, sizeof(dCVV
), &dCVVlen
);
266 return tlvdb_fixed(0x02, dCVVlen
, dCVV
);
270 static 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
)
277 if (ActivateField
&& channel
== ECC_CONTACTLESS
) {
283 PrintAndLogEx(SUCCESS
, ">>>> %s", sprint_hex(apdu
, apdu_len
));
286 case ECC_CONTACTLESS
:
287 // 6 byes + data = INS + CLA + P1 + P2 + Lc + <data = Nc> + Le(?IncludeLe)
288 res
= ExchangeAPDU14a(apdu
, apdu_len
, ActivateField
, LeaveFieldON
, Result
, (int)MaxResultLen
, (int *)ResultLen
);
294 //int ExchangeAPDUSC(uint8_t *datain, int datainlen, bool activateCard, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen);
295 #ifdef WITH_SMARTCARD
296 res
= ExchangeAPDUSC(apdu
, apdu_len
, ActivateField
, LeaveFieldON
, Result
, (int)MaxResultLen
, (int *)ResultLen
);
305 PrintAndLogEx(SUCCESS
, "<<<< %s", sprint_hex(Result
, *ResultLen
));
307 if (*ResultLen
< 2) {
311 /* if (Result[*ResultLen-2] == 0x61) {
312 uint8_t La = Result[*ResultLen-1];
313 uint8_t get_response[5] = {apdu[0], ISO7816_GET_RESPONSE, 0x00, 0x00, La};
314 return EMVExchangeEx(channel, false, LeaveFieldON, get_response, sizeof(get_response), Result, MaxResultLen, ResultLen, sw, tlv);
318 isw
= Result
[*ResultLen
] * 0x0100 + Result
[*ResultLen
+ 1];
324 PrintAndLogEx(ERR
, "APDU(%02x%02x) ERROR: [%4X] %s", apdu
[0], apdu
[1], isw
, GetAPDUCodeDescription(*sw
>> 8, *sw
& 0xff));
331 struct tlvdb
*t
= tlvdb_parse_multi(Result
, *ResultLen
);
338 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
)
340 uint8_t APDU
[APDU_COMMAND_LEN
];
341 memcpy(APDU
, apdu
, apdu_len
);
342 APDU
[apdu_len
] = 0x00;
343 if (channel
== ECC_CONTACTLESS
) {
344 if (apdu_len
== 5 && apdu
[4] == 0) {
345 // there is no Lc but an Le == 0 already
346 } else if (apdu_len
> 5 && apdu_len
== 5 + apdu
[4] + 1) {
347 // there is Lc, data and Le
350 apdu_len
++; // no Le, add Le = 0x00 because some vendors require it for contactless
353 return EMVExchangeEx(channel
, false, LeaveFieldON
, APDU
, apdu_len
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
356 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
)
358 uint8_t Select_APDU
[APDU_COMMAND_LEN
] = {0x00, ISO7816_SELECT_FILE
, 0x04, 0x00, AIDLen
, 0x00};
359 memcpy(Select_APDU
+ 5, AID
, AIDLen
);
360 int apdulen
= 5 + AIDLen
;
361 if (channel
== ECC_CONTACTLESS
) {
362 apdulen
++; // some vendors require Le = 0x00 for contactless operations
364 return EMVExchangeEx(channel
, ActivateField
, LeaveFieldON
, Select_APDU
, apdulen
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
367 int EMVSelectPSE(EMVCommandChannel channel
, bool ActivateField
, bool LeaveFieldON
, uint8_t PSENum
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
) {
368 uint8_t buf
[APDU_DATA_LEN
] = {0};
374 param_gethex_to_eol(PSElist
[1], 0, buf
, sizeof(buf
), &len
);
378 param_gethex_to_eol(PSElist
[0], 0, buf
, sizeof(buf
), &len
);
385 res
= EMVSelect(channel
, ActivateField
, LeaveFieldON
, buf
, len
, Result
, MaxResultLen
, ResultLen
, sw
, NULL
);
390 int EMVSearchPSE(EMVCommandChannel channel
, bool ActivateField
, bool LeaveFieldON
, uint8_t PSENum
, bool decodeTLV
, struct tlvdb
*tlv
) {
391 uint8_t data
[APDU_RESPONSE_LEN
] = {0};
396 char *PSE_or_PPSE
= PSENum
== 1 ? "PSE" : "PPSE";
399 res
= EMVSelectPSE(channel
, ActivateField
, true, PSENum
, data
, sizeof(data
), &datalen
, &sw
);
402 struct tlvdb
*t
= NULL
;
403 t
= tlvdb_parse_multi(data
, datalen
);
406 struct tlvdb
*ttmp
= tlvdb_find_path(t
, (tlv_tag_t
[]){0x6f, 0xa5, 0xbf0c, 0x61, 0x00});
408 PrintAndLogEx(FAILED
, "%s doesn't have any records.", PSE_or_PPSE
);
411 const struct tlv
*tgAID
= tlvdb_get_inchild(ttmp
, 0x4f, NULL
);
413 res
= EMVSelect(channel
, false, true, (uint8_t *)tgAID
->value
, tgAID
->len
, data
, sizeof(data
), &datalen
, &sw
, tlv
);
415 // retry if error and not returned sw error
416 if (res
&& res
!= 5) {
420 // card select error, proxmark error
422 PrintAndLogEx(WARNING
, "Exit...");
427 PrintAndLogEx(NORMAL
, "Retry failed [%s]. Skiped...", sprint_hex_inrow(tgAID
->value
, tgAID
->len
));
431 ttmp
= tlvdb_find_next(ttmp
, 0x61);
438 PrintAndLogEx(NORMAL
, "%s:", sprint_hex_inrow(tgAID
->value
, tgAID
->len
));
439 TLVPrintFromBuffer(data
, datalen
);
443 ttmp
= tlvdb_find_next(ttmp
, 0x61);
448 PrintAndLogEx(WARNING
, "%s ERROR: Can't get TLV from response.", PSE_or_PPSE
);
451 PrintAndLogEx(WARNING
, "%s ERROR: Can't select PPSE AID. Error: %d", PSE_or_PPSE
, res
);
454 if(!LeaveFieldON
&& channel
== ECC_CONTACTLESS
)
460 int EMVSearch(EMVCommandChannel channel
, bool ActivateField
, bool LeaveFieldON
, bool decodeTLV
, struct tlvdb
*tlv
) {
461 uint8_t aidbuf
[APDU_DATA_LEN
] = {0};
463 uint8_t data
[APDU_RESPONSE_LEN
] = {0};
469 for(int i
= 0; i
< AIDlistLen
; i
++) {
470 param_gethex_to_eol(AIDlist
[i
].aid
, 0, aidbuf
, sizeof(aidbuf
), &aidlen
);
471 res
= EMVSelect(channel
, (i
== 0) ? ActivateField
: false, (i
== AIDlistLen
- 1) ? LeaveFieldON
: true, aidbuf
, aidlen
, data
, sizeof(data
), &datalen
, &sw
, tlv
);
472 // retry if error and not returned sw error
473 if (res
&& res
!= 5) {
477 // (1) - card select error, proxmark error OR (200) - result length = 0
478 if (res
== 1 || res
== 200) {
479 PrintAndLogEx(WARNING
, "Exit...");
484 PrintAndLogEx(FAILED
, "Retry failed [%s]. Skipped...", AIDlist
[i
].aid
);
497 PrintAndLogEx(SUCCESS
, "%s", AIDlist
[i
].aid
);
498 TLVPrintFromBuffer(data
, datalen
);
505 int EMVSelectApplication(struct tlvdb
*tlv
, uint8_t *AID
, size_t *AIDlen
) {
506 // check priority. 0x00 - highest
511 struct tlvdb
*ttmp
= tlvdb_find(tlv
, 0x6f);
516 const struct tlv
*tgAID
= tlvdb_get_inchild(ttmp
, 0x84, NULL
);
517 const struct tlv
*tgPrio
= tlvdb_get_inchild(ttmp
, 0x87, NULL
);
523 int pt
= bytes_to_num((uint8_t*)tgPrio
->value
, (tgPrio
->len
< 2) ? tgPrio
->len
: 2);
527 memcpy(AID
, tgAID
->value
, tgAID
->len
);
528 *AIDlen
= tgAID
->len
;
531 // takes the first application from list wo priority
533 memcpy(AID
, tgAID
->value
, tgAID
->len
);
534 *AIDlen
= tgAID
->len
;
538 ttmp
= tlvdb_find_next(ttmp
, 0x6f);
544 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
)
546 uint8_t GPO_APDU
[APDU_COMMAND_LEN
] = {0x80, 0xa8, 0x00, 0x00, PDOLLen
, 0x00};
547 memcpy(GPO_APDU
+ 5, PDOL
, PDOLLen
);
548 int apdulen
= 5 + PDOLLen
;
550 return EMVExchange(channel
, LeaveFieldON
, GPO_APDU
, apdulen
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
553 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
)
555 uint8_t read_APDU
[5] = {0x00, ISO7816_READ_RECORDS
, SFIrec
, (SFI
<< 3) | 0x04, 0x00};
556 int res
= EMVExchange(channel
, LeaveFieldON
, read_APDU
, sizeof(read_APDU
), Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
558 PrintAndLogEx(INFO
, ">>> trying to reissue command withouth Le...");
559 res
= EMVExchangeEx(channel
, false, LeaveFieldON
, read_APDU
, sizeof(read_APDU
), Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
564 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
)
566 uint8_t CDOL_APDU
[APDU_COMMAND_LEN
] = {0x80, 0xae, RefControl
, 0x00, CDOLLen
, 0x00};
567 memcpy(CDOL_APDU
+ 5, CDOL
, CDOLLen
);
568 int apdulen
= 5 + CDOLLen
;
570 return EMVExchange(channel
, LeaveFieldON
, CDOL_APDU
, apdulen
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
573 int EMVGenerateChallenge(EMVCommandChannel channel
, bool LeaveFieldON
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
, struct tlvdb
*tlv
)
575 uint8_t get_challenge_APDU
[APDU_COMMAND_LEN
] = {0x00, ISO7816_GET_CHALLENGE
, 0x00, 0x00};
577 int res
= EMVExchange(channel
, LeaveFieldON
, get_challenge_APDU
, 4, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
579 PrintAndLogEx(INFO
, ">>> trying to reissue command withouth Le...");
580 res
= EMVExchangeEx(channel
, false, LeaveFieldON
, get_challenge_APDU
, 4, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
585 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
)
587 uint8_t authenticate_APDU
[APDU_COMMAND_LEN
] = {0x00, ISO7816_INTERNAL_AUTHENTICATION
, 0x00, 0x00, DDOLLen
, 0x00};
588 memcpy(authenticate_APDU
+ 5, DDOL
, DDOLLen
);
589 int apdulen
= 5 + DDOLLen
;
591 return EMVExchange(channel
, LeaveFieldON
, authenticate_APDU
, apdulen
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
594 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
)
596 uint8_t checksum_APDU
[APDU_COMMAND_LEN
] = {0x80, 0x2a, 0x8e, 0x80, UDOLlen
, 0x00};
597 memcpy(checksum_APDU
+ 5, UDOL
, UDOLlen
);
598 int apdulen
= 5 + UDOLlen
;
600 return EMVExchange(channel
, LeaveFieldON
, checksum_APDU
, apdulen
, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
604 struct emv_pk
*get_ca_pk(struct tlvdb
*db
) {
605 const struct tlv
*df_tlv
= tlvdb_get(db
, 0x84, NULL
);
606 const struct tlv
*caidx_tlv
= tlvdb_get(db
, 0x8f, NULL
);
608 if (!df_tlv
|| !caidx_tlv
|| df_tlv
->len
< 6 || caidx_tlv
->len
!= 1)
611 PrintAndLogEx(NORMAL
, "CA public key index 0x%0x", caidx_tlv
->value
[0]);
612 return emv_pk_get_ca_pk(df_tlv
->value
, caidx_tlv
->value
[0]);
615 int trSDA(struct tlvdb
*tlv
) {
617 struct emv_pk
*pk
= get_ca_pk(tlv
);
619 PrintAndLogEx(WARNING
, "Error: Key not found. Exit.");
623 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlv
);
626 PrintAndLogEx(WARNING
, "Error: Issuer certificate not found. Exit.");
630 PrintAndLogEx(SUCCESS
, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx",
637 issuer_pk
->serial
[0],
638 issuer_pk
->serial
[1],
642 const struct tlv
*sda_tlv
= tlvdb_get(tlv
, 0x21, NULL
);
643 if (!sda_tlv
|| sda_tlv
->len
< 1) {
644 emv_pk_free(issuer_pk
);
646 PrintAndLogEx(WARNING
, "Can't find input list for Offline Data Authentication. Exit.");
650 struct tlvdb
*dac_db
= emv_pki_recover_dac(issuer_pk
, tlv
, sda_tlv
);
652 const struct tlv
*dac_tlv
= tlvdb_get(dac_db
, 0x9f45, NULL
);
653 PrintAndLogEx(NORMAL
, "SDA verified OK. (%02hhx:%02hhx)\n", dac_tlv
->value
[0], dac_tlv
->value
[1]);
654 tlvdb_add(tlv
, dac_db
);
656 emv_pk_free(issuer_pk
);
658 PrintAndLogEx(WARNING
, "SSAD verify error");
662 emv_pk_free(issuer_pk
);
667 static const unsigned char default_ddol_value
[] = {0x9f, 0x37, 0x04};
668 static struct tlv default_ddol_tlv
= {.tag
= 0x9f49, .len
= 3, .value
= default_ddol_value
};
670 int trDDA(EMVCommandChannel channel
, bool decodeTLV
, struct tlvdb
*tlv
) {
671 uint8_t buf
[APDU_RESPONSE_LEN
] = {0};
675 struct emv_pk
*pk
= get_ca_pk(tlv
);
677 PrintAndLogEx(WARNING
, "Error: Key not found. Exit.");
681 const struct tlv
*sda_tlv
= tlvdb_get(tlv
, 0x21, NULL
);
682 if (!sda_tlv
|| sda_tlv
->len
< 1) {
684 PrintAndLogEx(WARNING
, "Error: Can't find input list for Offline Data Authentication. Exit.");
688 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlv
);
691 PrintAndLogEx(WARNING
, "Error: Issuer certificate not found. Exit.");
694 PrintAndLogEx(SUCCESS
, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
701 issuer_pk
->serial
[0],
702 issuer_pk
->serial
[1],
706 struct emv_pk
*icc_pk
= emv_pki_recover_icc_cert(issuer_pk
, tlv
, sda_tlv
);
709 emv_pk_free(issuer_pk
);
710 PrintAndLogEx(WARNING
, "Error: ICC setrificate not found. Exit.");
713 PrintAndLogEx(SUCCESS
, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
725 struct emv_pk
*icc_pe_pk
= emv_pki_recover_icc_pe_cert(issuer_pk
, tlv
);
727 PrintAndLogEx(WARNING
, "WARNING: ICC PE PK recover error. ");
729 PrintAndLogEx(SUCCESS
, "ICC PE PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
736 icc_pe_pk
->serial
[0],
737 icc_pe_pk
->serial
[1],
742 // 9F4B: Signed Dynamic Application Data
743 const struct tlv
*sdad_tlv
= tlvdb_get(tlv
, 0x9f4b, NULL
);
744 // DDA with internal authenticate OR fDDA with filled 0x9F4B tag (GPO result)
745 // EMV kernel3 v2.4, contactless book C-3, C.1., page 147
747 PrintAndLogEx(NORMAL
, "\n* * Got Signed Dynamic Application Data (9F4B) form GPO. Maybe fDDA...");
749 const struct tlvdb
*atc_db
= emv_pki_recover_atc_ex(icc_pk
, tlv
, true);
751 PrintAndLogEx(WARNING
, "Error: Can't recover IDN (ICC Dynamic Number)");
753 emv_pk_free(issuer_pk
);
758 // 9f36 Application Transaction Counter (ATC)
759 const struct tlv
*atc_tlv
= tlvdb_get(atc_db
, 0x9f36, NULL
);
761 PrintAndLogEx(NORMAL
, "\nATC (Application Transaction Counter) [%zu] %s", atc_tlv
->len
, sprint_hex_inrow(atc_tlv
->value
, atc_tlv
->len
));
763 const struct tlv
*core_atc_tlv
= tlvdb_get(tlv
, 0x9f36, NULL
);
764 if(tlv_equal(core_atc_tlv
, atc_tlv
)) {
765 PrintAndLogEx(SUCCESS
, "ATC check OK.");
766 PrintAndLogEx(SUCCESS
, "fDDA (fast DDA) verified OK.");
768 PrintAndLogEx(WARNING
, "Error: fDDA verified, but ATC in the certificate and ATC in the record not the same.");
771 PrintAndLogEx(NORMAL
, "\nERROR: fDDA (fast DDA) verify error");
773 emv_pk_free(issuer_pk
);
778 struct tlvdb
*dac_db
= emv_pki_recover_dac(issuer_pk
, tlv
, sda_tlv
);
780 const struct tlv
*dac_tlv
= tlvdb_get(dac_db
, 0x9f45, NULL
);
781 PrintAndLogEx(NORMAL
, "SDA verified OK. (%02hhx:%02hhx)\n", dac_tlv
->value
[0], dac_tlv
->value
[1]);
782 tlvdb_add(tlv
, dac_db
);
784 PrintAndLogEx(WARNING
, "Error: SSAD verify error");
786 emv_pk_free(issuer_pk
);
791 PrintAndLogEx(NORMAL
, "\n* Calc DDOL");
792 const struct tlv
*ddol_tlv
= tlvdb_get(tlv
, 0x9f49, NULL
);
794 ddol_tlv
= &default_ddol_tlv
;
795 PrintAndLogEx(NORMAL
, "DDOL [9f49] not found. Using default DDOL");
798 struct tlv
*ddol_data_tlv
= dol_process(ddol_tlv
, tlv
, 0);
799 if (!ddol_data_tlv
) {
800 PrintAndLogEx(WARNING
, "Error: Can't create DDOL TLV");
802 emv_pk_free(issuer_pk
);
807 PrintAndLogEx(NORMAL
, "DDOL data[%d]: %s", ddol_data_tlv
->len
, sprint_hex(ddol_data_tlv
->value
, ddol_data_tlv
->len
));
809 PrintAndLogEx(NORMAL
, "\n* Internal Authenticate");
810 int res
= EMVInternalAuthenticate(channel
, true, (uint8_t *)ddol_data_tlv
->value
, ddol_data_tlv
->len
, buf
, sizeof(buf
), &len
, &sw
, NULL
);
812 PrintAndLogEx(WARNING
, "Internal Authenticate error(%d): %4x. Exit...", res
, sw
);
815 emv_pk_free(issuer_pk
);
820 struct tlvdb
*dda_db
= NULL
;
821 if (buf
[0] == 0x80) {
823 PrintAndLogEx(WARNING
, "Error: Internal Authenticate format1 parsing error. length=%d", len
);
825 // 9f4b Signed Dynamic Application Data
826 dda_db
= tlvdb_fixed(0x9f4b, len
- 2, buf
+ 2);
827 tlvdb_add(tlv
, dda_db
);
829 PrintAndLogEx(NORMAL
, "* * Decode response format 1:");
830 TLVPrintFromTLV(dda_db
);
834 dda_db
= tlvdb_parse_multi(buf
, len
);
836 PrintAndLogEx(WARNING
, "Error: Can't parse Internal Authenticate result as TLV");
839 emv_pk_free(issuer_pk
);
843 tlvdb_add(tlv
, dda_db
);
846 TLVPrintFromTLV(dda_db
);
849 struct tlvdb
*idn_db
= emv_pki_recover_idn_ex(icc_pk
, dda_db
, ddol_data_tlv
, true);
852 PrintAndLogEx(WARNING
, "Error: Can't recover IDN (ICC Dynamic Number)");
855 emv_pk_free(issuer_pk
);
861 // 9f4c ICC Dynamic Number
862 const struct tlv
*idn_tlv
= tlvdb_get(idn_db
, 0x9f4c, NULL
);
864 PrintAndLogEx(NORMAL
, "\nIDN (ICC Dynamic Number) [%zu] %s", idn_tlv
->len
, sprint_hex_inrow(idn_tlv
->value
, idn_tlv
->len
));
865 PrintAndLogEx(NORMAL
, "DDA verified OK.");
866 tlvdb_add(tlv
, idn_db
);
869 PrintAndLogEx(NORMAL
, "\nERROR: DDA verify error");
873 emv_pk_free(issuer_pk
);
880 emv_pk_free(issuer_pk
);
885 int trCDA(struct tlvdb
*tlv
, struct tlvdb
*ac_tlv
, struct tlv
*pdol_data_tlv
, struct tlv
*ac_data_tlv
) {
887 struct emv_pk
*pk
= get_ca_pk(tlv
);
889 PrintAndLogEx(WARNING
, "Error: Key not found. Exit.");
893 const struct tlv
*sda_tlv
= tlvdb_get(tlv
, 0x21, NULL
);
894 if (!sda_tlv
|| sda_tlv
->len
< 1) {
895 PrintAndLogEx(WARNING
, "Error: Can't find input list for Offline Data Authentication. Exit.");
900 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlv
);
902 PrintAndLogEx(WARNING
, "Error: Issuer certificate not found. Exit.");
906 PrintAndLogEx(SUCCESS
, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
913 issuer_pk
->serial
[0],
914 issuer_pk
->serial
[1],
918 struct emv_pk
*icc_pk
= emv_pki_recover_icc_cert(issuer_pk
, tlv
, sda_tlv
);
920 PrintAndLogEx(WARNING
, "Error: ICC setrificate not found. Exit.");
922 emv_pk_free(issuer_pk
);
925 PrintAndLogEx(SUCCESS
, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
937 struct tlvdb
*dac_db
= emv_pki_recover_dac(issuer_pk
, tlv
, sda_tlv
);
939 const struct tlv
*dac_tlv
= tlvdb_get(dac_db
, 0x9f45, NULL
);
940 PrintAndLogEx(NORMAL
, "SSAD verified OK. (%02hhx:%02hhx)", dac_tlv
->value
[0], dac_tlv
->value
[1]);
941 tlvdb_add(tlv
, dac_db
);
943 PrintAndLogEx(WARNING
, "Error: SSAD verify error");
945 emv_pk_free(issuer_pk
);
950 PrintAndLogEx(NORMAL
, "\n* * Check Signed Dynamic Application Data (SDAD)");
951 struct tlvdb
*idn_db
= emv_pki_perform_cda_ex(icc_pk
, tlv
, ac_tlv
,
952 pdol_data_tlv
, // pdol
953 ac_data_tlv
, // cdol1
957 const struct tlv
*idn_tlv
= tlvdb_get(idn_db
, 0x9f4c, NULL
);
958 PrintAndLogEx(NORMAL
, "\nIDN (ICC Dynamic Number) [%zu] %s", idn_tlv
->len
, sprint_hex_inrow(idn_tlv
->value
, idn_tlv
->len
));
959 PrintAndLogEx(NORMAL
, "CDA verified OK.");
960 tlvdb_add(tlv
, idn_db
);
962 PrintAndLogEx(NORMAL
, "\nERROR: CDA verify error");
966 emv_pk_free(issuer_pk
);
971 int RecoveryCertificates(struct tlvdb
*tlvRoot
, json_t
*root
) {
973 struct emv_pk
*pk
= get_ca_pk(tlvRoot
);
975 PrintAndLog("ERROR: Key not found. Exit.");
979 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlvRoot
);
982 PrintAndLog("WARNING: Issuer certificate not found. Exit.");
985 PrintAndLogEx(SUCCESS
, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx",
992 issuer_pk
->serial
[0],
993 issuer_pk
->serial
[1],
997 JsonSaveBufAsHex(root
, "$.ApplicationData.RID", issuer_pk
->rid
, 5);
999 char *issuer_pk_c
= emv_pk_dump_pk(issuer_pk
);
1000 JsonSaveStr(root
, "$.ApplicationData.IssuerPublicKeyDec", issuer_pk_c
);
1001 JsonSaveBufAsHex(root
, "$.ApplicationData.IssuerPublicKeyModulus", issuer_pk
->modulus
, issuer_pk
->mlen
);
1004 struct emv_pk
*icc_pk
= emv_pki_recover_icc_cert(issuer_pk
, tlvRoot
, NULL
);
1007 emv_pk_free(issuer_pk
);
1008 PrintAndLogEx(WARNING
, "WARNING: ICC certificate not found. Exit.");
1011 PrintAndLogEx(SUCCESS
, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
1023 char *icc_pk_c
= emv_pk_dump_pk(icc_pk
);
1024 JsonSaveStr(root
, "$.ApplicationData.ICCPublicKeyDec", icc_pk_c
);
1025 JsonSaveBufAsHex(root
, "$.ApplicationData.ICCPublicKeyModulus", icc_pk
->modulus
, icc_pk
->mlen
);