]> git.zerfleddert.de Git - proxmark3-svn/blame - client/emv/emvcore.c
fix rare bug in tlv.c (#788)
[proxmark3-svn] / client / emv / emvcore.c
CommitLineData
a2bb2735 1//-----------------------------------------------------------------------------
2// Copyright (C) 2017 Merlok
3//
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
6// the license.
7//-----------------------------------------------------------------------------
8// EMV core functions
9//-----------------------------------------------------------------------------
10
11#include "emvcore.h"
41bdfce3 12
13#include <string.h>
95b697f0 14#include "emvjson.h"
8fa68384 15#include "util_posix.h"
6b5105be 16#include "protocols.h"
41bdfce3 17#include "ui.h"
18#include "util.h"
19#include "emv_tags.h"
20#include "emv_pk.h"
21#include "emv_pki.h"
22#include "cmdhf14a.h"
23#include "apduinfo.h"
24#include "tlv.h"
25#include "dump.h"
26#include "dol.h"
27
8d7d7b61 28#ifdef WITH_SMARTCARD
29#include "cmdsmartcard.h"
30#endif
a2bb2735 31
41bdfce3 32
3c5fce2b
OM
33// Got from here. Thanks)
34// https://eftlab.co.uk/index.php/site-map/knowledge-base/211-emv-aid-rid-pix
151a33c0 35static const char *PSElist [] = {
3c5fce2b
OM
36 "325041592E5359532E4444463031", // 2PAY.SYS.DDF01 - Visa Proximity Payment System Environment - PPSE
37 "315041592E5359532E4444463031" // 1PAY.SYS.DDF01 - Visa Payment System Environment - PSE
38};
d03fb293 39//static const size_t PSElistLen = sizeof(PSElist)/sizeof(char*);
3c5fce2b 40
95b697f0
OM
41char *TransactionTypeStr[] = {
42 "MSD",
43 "VSDC",
44 "qVCDCMCHIP",
45 "CDA"
46};
47
87fc2dad 48typedef struct {
49 enum CardPSVendor vendor;
50 const char* aid;
51} TAIDList;
52
151a33c0 53static const TAIDList AIDlist [] = {
3c5fce2b 54 // Visa International
97096af6 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
3c5fce2b 70 // Visa USA
97096af6 71 { CV_VISA, "A000000098" }, // Debit Card
72 { CV_VISA, "A0000000980848" }, // Debit Card
3c5fce2b 73 // Mastercard International
97096af6 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
3c5fce2b 88 // American Express
87fc2dad 89 { CV_AMERICANEXPRESS, "A000000025" },
90 { CV_AMERICANEXPRESS, "A0000000250000" },
91 { CV_AMERICANEXPRESS, "A00000002501" },
92 { CV_AMERICANEXPRESS, "A000000025010402" },
93 { CV_AMERICANEXPRESS, "A000000025010701" },
94 { CV_AMERICANEXPRESS, "A000000025010801" },
3c5fce2b 95 // Groupement des Cartes Bancaires "CB"
97096af6 96 { CV_CB, "A0000000421010" }, // Cartes Bancaire EMV Card
151a33c0 97 { CV_CB, "A0000000422010" },
98 { CV_CB, "A0000000423010" },
99 { CV_CB, "A0000000424010" },
100 { CV_CB, "A0000000425010" },
3c5fce2b 101 // JCB CO., LTD.
97096af6 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
87fc2dad 110 // Other
97096af6 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
3c5fce2b 138};
87fc2dad 139static const size_t AIDlistLen = sizeof(AIDlist)/sizeof(TAIDList);
3c5fce2b
OM
140
141static bool APDULogging = false;
142void SetAPDULogging(bool logging) {
143 APDULogging = logging;
144}
145
41bdfce3 146void DropFieldEx(EMVCommandChannel channel) {
147 if (channel == ECC_CONTACTLESS) {
148 DropField();
149 }
150}
151
87fc2dad 152enum CardPSVendor GetCardPSVendor(uint8_t * AID, size_t AIDlen) {
153 char buf[100] = {0};
154 if (AIDlen < 1)
155 return CV_NA;
156
157 hex_to_buffer((uint8_t *)buf, AID, AIDlen, sizeof(buf) - 1, 0, 0, true);
158
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;
162 }
151a33c0 163 }
164
87fc2dad 165 return CV_NA;
166}
167
33a9982c 168static bool print_cb(void *data, const struct tlv *tlv, int level, bool is_leaf) {
43912d63 169 emv_tag_dump(tlv, stdout, level);
33a9982c 170 if (is_leaf) {
171 dump_buffer(tlv->value, tlv->len, stdout, level);
172 }
a2bb2735 173
174 return true;
175}
176
1511ea28 177bool TLVPrintFromBuffer(uint8_t *data, int datalen) {
a2bb2735 178 struct tlvdb *t = NULL;
23207d74 179 t = tlvdb_parse_multi(data, datalen);
a2bb2735 180 if (t) {
151a33c0 181 PrintAndLogEx(NORMAL, "-------------------- TLV decoded --------------------");
182
43912d63 183 tlvdb_visit(t, print_cb, NULL, 0);
a2bb2735 184 tlvdb_free(t);
1511ea28 185 return true;
a2bb2735 186 } else {
151a33c0 187 PrintAndLogEx(WARNING, "TLV ERROR: Can't parse response as TLV tree.");
a2bb2735 188 }
1511ea28 189 return false;
a2bb2735 190}
3c5fce2b 191
87fc2dad 192void TLVPrintFromTLVLev(struct tlvdb *tlv, int level) {
151a33c0 193 if (!tlv)
3c5fce2b 194 return;
151a33c0 195
87fc2dad 196 tlvdb_visit(tlv, print_cb, NULL, level);
197}
198
199void TLVPrintFromTLV(struct tlvdb *tlv) {
200 TLVPrintFromTLVLev(tlv, 0);
3c5fce2b
OM
201}
202
203void TLVPrintAIDlistFromSelectTLV(struct tlvdb *tlv) {
151a33c0 204 PrintAndLogEx(NORMAL, "|------------------|--------|-------------------------|");
205 PrintAndLogEx(NORMAL, "| AID |Priority| Name |");
206 PrintAndLogEx(NORMAL, "|------------------|--------|-------------------------|");
3c5fce2b
OM
207
208 struct tlvdb *ttmp = tlvdb_find(tlv, 0x6f);
209 if (!ttmp)
151a33c0 210 PrintAndLogEx(NORMAL, "| none |");
211
3c5fce2b
OM
212 while (ttmp) {
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);
216 if (!tgAID)
217 break;
151a33c0 218 PrintAndLogEx(NORMAL, "|%s| %s |%s|",
219 sprint_hex_inrow_ex(tgAID->value, tgAID->len, 18),
220 (tgPrio) ? sprint_hex(tgPrio->value, 1) : " ",
3c5fce2b 221 (tgName) ? sprint_ascii_ex(tgName->value, tgName->len, 25) : " ");
151a33c0 222
3c5fce2b
OM
223 ttmp = tlvdb_find_next(ttmp, 0x6f);
224 }
225
151a33c0 226 PrintAndLogEx(NORMAL, "|------------------|--------|-------------------------|");
3c5fce2b
OM
227}
228
87fc2dad 229struct tlvdb *GetPANFromTrack2(const struct tlv *track2) {
230 char track2Hex[200] = {0};
231 uint8_t PAN[100] = {0};
232 int PANlen = 0;
233 char *tmp = track2Hex;
3c5fce2b 234
87fc2dad 235 if (!track2)
236 return NULL;
237
238 for (int i = 0; i < track2->len; ++i, tmp += 2)
239 sprintf(tmp, "%02x", (unsigned int)track2->value[i]);
151a33c0 240
87fc2dad 241 int posD = strchr(track2Hex, 'd') - track2Hex;
242 if (posD < 1)
243 return NULL;
151a33c0 244
87fc2dad 245 track2Hex[posD] = 0;
246 if (strlen(track2Hex) % 2) {
247 track2Hex[posD] = 'F';
248 track2Hex[posD + 1] = '\0';
249 }
151a33c0 250
87fc2dad 251 param_gethex_to_eol(track2Hex, 0, PAN, sizeof(PAN), &PANlen);
151a33c0 252
87fc2dad 253 return tlvdb_fixed(0x5a, PANlen, PAN);
254}
255
256struct tlvdb *GetdCVVRawFromTrack2(const struct tlv *track2) {
257 char track2Hex[200] = {0};
258 char dCVVHex[100] = {0};
259 uint8_t dCVV[100] = {0};
260 int dCVVlen = 0;
261 const int PINlen = 5; // must calculated from 9F67 MSD Offset but i have not seen this tag)
262 char *tmp = track2Hex;
151a33c0 263
87fc2dad 264 if (!track2)
265 return NULL;
151a33c0 266
87fc2dad 267 for (int i = 0; i < track2->len; ++i, tmp += 2)
268 sprintf(tmp, "%02x", (unsigned int)track2->value[i]);
151a33c0 269
87fc2dad 270 int posD = strchr(track2Hex, 'd') - track2Hex;
271 if (posD < 1)
272 return NULL;
273
274 memset(dCVVHex, '0', 32);
275 // ATC
276 memcpy(dCVVHex + 0, track2Hex + posD + PINlen + 11, 4);
277 // PAN 5 hex
278 memcpy(dCVVHex + 4, track2Hex, 5);
279 // expire date
280 memcpy(dCVVHex + 9, track2Hex + posD + 1, 4);
281 // service code
282 memcpy(dCVVHex + 13, track2Hex + posD + 5, 3);
151a33c0 283
87fc2dad 284 param_gethex_to_eol(dCVVHex, 0, dCVV, sizeof(dCVV), &dCVVlen);
151a33c0 285
87fc2dad 286 return tlvdb_fixed(0x02, dCVVlen, dCVV);
287}
288
87fc2dad 289
41bdfce3 290int 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)
6b5105be 291{
3c5fce2b
OM
292 *ResultLen = 0;
293 if (sw) *sw = 0;
294 uint16_t isw = 0;
8d7d7b61 295 int res = 0;
151a33c0 296
41bdfce3 297 if (ActivateField) {
298 DropFieldEx( channel );
8fa68384
OM
299 msleep(50);
300 }
151a33c0 301
3c5fce2b 302 if (APDULogging)
6b5105be 303 PrintAndLogEx(SUCCESS, ">>>> %s", sprint_hex(apdu, apdu_len));
3c5fce2b 304
41bdfce3 305#ifdef WITH_SMARTCARD
8d7d7b61 306 switch(channel) {
6b5105be 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);
6b5105be 310 break;
311 case ECC_CONTACT:
6b5105be 312 res = ExchangeAPDUSC(apdu, apdu_len, ActivateField, LeaveFieldON, Result, (int)MaxResultLen, (int *)ResultLen);
6b5105be 313 break;
3c5fce2b 314 }
41bdfce3 315#else
316 res = ExchangeAPDU14a(apdu, apdu_len, ActivateField, LeaveFieldON, Result, (int)MaxResultLen, (int *)ResultLen);
317#endif
318
319 if (res) {
320 return res;
321 }
87fc2dad 322
10d4f823 323 if (APDULogging)
151a33c0 324 PrintAndLogEx(SUCCESS, "<<<< %s", sprint_hex(Result, *ResultLen));
10d4f823 325
8d7d7b61 326 if (*ResultLen < 2) {
327 return 200;
328 }
151a33c0 329
41bdfce3 330 if (Result[*ResultLen-2] == 0x61) {
6b5105be 331 uint8_t La = Result[*ResultLen-1];
332 uint8_t get_response[5] = {apdu[0], ISO7816_GET_RESPONSE, 0x00, 0x00, La};
41bdfce3 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;
336 }
337
338 if (res) return res;
6b5105be 339
3c5fce2b 340 *ResultLen -= 2;
41bdfce3 341
3c5fce2b
OM
342 isw = Result[*ResultLen] * 0x0100 + Result[*ResultLen + 1];
343 if (sw)
344 *sw = isw;
345
346 if (isw != 0x9000) {
39cc1c87 347 if (APDULogging) {
6b5105be 348 PrintAndLogEx(ERR, "APDU(%02x%02x) ERROR: [%4X] %s", apdu[0], apdu[1], isw, GetAPDUCodeDescription(*sw >> 8, *sw & 0xff));
349 return 5;
39cc1c87 350 }
3c5fce2b
OM
351 }
352
353 // add to tlv tree
354 if (tlv) {
355 struct tlvdb *t = tlvdb_parse_multi(Result, *ResultLen);
356 tlvdb_add(tlv, t);
357 }
151a33c0 358
3c5fce2b
OM
359 return 0;
360}
361
41bdfce3 362static 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)
6b5105be 363{
364 uint8_t APDU[APDU_COMMAND_LEN];
365 memcpy(APDU, apdu, apdu_len);
366 APDU[apdu_len] = 0x00;
367 if (channel == ECC_CONTACTLESS) {
41bdfce3 368 if (apdu_len == 5) {
369 // there is no Lc but an Le already
6b5105be 370 } else if (apdu_len > 5 && apdu_len == 5 + apdu[4] + 1) {
371 // there is Lc, data and Le
372 } else {
41bdfce3 373 apdu_len++; // no Le, add Le = 0x00 because some vendors require it for contactless
6b5105be 374 }
375 }
376 return EMVExchangeEx(channel, false, LeaveFieldON, APDU, apdu_len, Result, MaxResultLen, ResultLen, sw, tlv);
87fc2dad 377}
378
6b5105be 379int 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)
380{
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
386 }
387 return EMVExchangeEx(channel, ActivateField, LeaveFieldON, Select_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv);
87fc2dad 388}
389
8d7d7b61 390int EMVSelectPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw) {
6b5105be 391 uint8_t buf[APDU_DATA_LEN] = {0};
3c5fce2b
OM
392 *ResultLen = 0;
393 int len = 0;
394 int res = 0;
395 switch (PSENum) {
396 case 1:
397 param_gethex_to_eol(PSElist[1], 0, buf, sizeof(buf), &len);
398 break;
399 case 2:
6b5105be 400
3c5fce2b
OM
401 param_gethex_to_eol(PSElist[0], 0, buf, sizeof(buf), &len);
402 break;
403 default:
404 return -1;
405 }
151a33c0 406
3c5fce2b 407 // select
8d7d7b61 408 res = EMVSelect(channel, ActivateField, LeaveFieldON, buf, len, Result, MaxResultLen, ResultLen, sw, NULL);
3c5fce2b
OM
409
410 return res;
411}
412
4cdd63b2 413
414int 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) {
415 int retrycnt = 0;
416 int res = 0;
417 do {
418 res = EMVSelect(channel, false, true, AID, AIDLen, Result, MaxResultLen, ResultLen, sw, tlv);
419
420 // retry if error and not returned sw error
421 if (res && res != 5) {
422 if (++retrycnt < 3){
423 continue;
424 } else {
425 // card select error, proxmark error
426 if (res == 1) {
427 PrintAndLogEx(WARNING, "Exit...");
428 return 1;
429 }
430
431 retrycnt = 0;
432 PrintAndLogEx(NORMAL, "Retry failed [%s]. Skiped...", sprint_hex_inrow(AID, AIDLen));
433 return res;
434 }
435 }
436 } while (res && res != 5);
437
438 return res;
439}
440
441
442int EMVCheckAID(EMVCommandChannel channel, bool decodeTLV, struct tlvdb *tlvdbelm, struct tlvdb *tlv){
443 uint8_t data[APDU_RESPONSE_LEN] = {0};
444 size_t datalen = 0;
445 int res = 0;
446 uint16_t sw = 0;
447
448 while (tlvdbelm) {
449 const struct tlv *tgAID = tlvdb_get_inchild(tlvdbelm, 0x4f, NULL);
450 if (tgAID) {
451 res = EMVSelectWithRetry(channel, false, true, (uint8_t *)tgAID->value, tgAID->len, data, sizeof(data), &datalen, &sw, tlv);
452
453 // if returned sw error
454 if (res == 5) {
455 // next element
456 tlvdbelm = tlvdb_find_next(tlvdbelm, 0x61);
457 continue;
458 }
459
460 if (res)
461 break;
462
463 // all is ok
464 if (decodeTLV){
465 PrintAndLogEx(NORMAL, "%s:", sprint_hex_inrow(tgAID->value, tgAID->len));
466 TLVPrintFromBuffer(data, datalen);
467 }
468 }
469 tlvdbelm = tlvdb_find_next(tlvdbelm, 0x61);
470 }
471 return res;
472}
473
474
1511ea28 475int EMVSearchPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, bool decodeTLV, struct tlvdb *tlv) {
6b5105be 476 uint8_t data[APDU_RESPONSE_LEN] = {0};
3c5fce2b 477 size_t datalen = 0;
4cdd63b2 478 uint8_t sfidata[0x11][APDU_RESPONSE_LEN] = {0};
479 size_t sfidatalen[0x11] = {0};
3c5fce2b
OM
480 uint16_t sw = 0;
481 int res;
4cdd63b2 482 bool fileFound = false;
3c5fce2b 483
6b5105be 484 char *PSE_or_PPSE = PSENum == 1 ? "PSE" : "PPSE";
485
3c5fce2b 486 // select PPSE
1511ea28 487 res = EMVSelectPSE(channel, ActivateField, true, PSENum, data, sizeof(data), &datalen, &sw);
3c5fce2b
OM
488
489 if (!res){
4cdd63b2 490 if (sw != 0x9000) {
491 PrintAndLogEx(FAILED, "Select PSE error. APDU error: %04x.", sw);
492 return 1;
493 }
494
3c5fce2b
OM
495 struct tlvdb *t = NULL;
496 t = tlvdb_parse_multi(data, datalen);
497 if (t) {
4cdd63b2 498 // PSE/PPSE with SFI
499 struct tlvdb *tsfi = tlvdb_find_path(t, (tlv_tag_t[]){0x6f, 0xa5, 0x88, 0x00});
500 if (tsfi) {
501 uint8_t sfin = 0;
502 tlv_get_uint8(tlvdb_get_tlv(tsfi), &sfin);
503 PrintAndLogEx(INFO, "* PPSE get SFI: 0x%02x.", sfin);
504
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);
508
509 // end of records
510 if (sw == 0x6a83) {
511 sfidatalen[ui] = 0;
512 PrintAndLogEx(INFO, "* * PPSE get SFI. End of records.");
513 break;
514 }
515
516 // error catch!
517 if (sw != 0x9000) {
518 sfidatalen[ui] = 0;
519 PrintAndLogEx(FAILED, "PPSE get Error. APDU error: %04x.", sw);
520 break;
3c5fce2b 521 }
3c5fce2b 522
3c5fce2b 523 if (decodeTLV){
4cdd63b2 524 TLVPrintFromBuffer(sfidata[ui], sfidatalen[ui]);
3c5fce2b
OM
525 }
526 }
151a33c0 527
4cdd63b2 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]);
532 if (tsfi) {
533 struct tlvdb *tsfitmp = tlvdb_find_path(tsfi, (tlv_tag_t[]){0x70, 0x61, 0x00});
534 if (!tsfitmp) {
535 PrintAndLogEx(FAILED, "SFI 0x%02d doesn't have any records.", sfidatalen[ui]);
536 continue;
537 }
538 res = EMVCheckAID(channel, decodeTLV, tsfitmp, tlv);
539 fileFound = true;
540 }
541 tlvdb_free(tsfi);
542 }
543 }
3c5fce2b
OM
544 }
545
4cdd63b2 546
547 // PSE/PPSE plain (wo SFI)
548 struct tlvdb *ttmp = tlvdb_find_path(t, (tlv_tag_t[]){0x6f, 0xa5, 0xbf0c, 0x61, 0x00});
549 if (ttmp) {
550 res = EMVCheckAID(channel, decodeTLV, ttmp, tlv);
551 fileFound = true;
552 }
553
554 if (!fileFound)
555 PrintAndLogEx(FAILED, "PPSE doesn't have any records.");
556
3c5fce2b
OM
557 tlvdb_free(t);
558 } else {
6b5105be 559 PrintAndLogEx(WARNING, "%s ERROR: Can't get TLV from response.", PSE_or_PPSE);
151a33c0 560 }
3c5fce2b 561 } else {
6b5105be 562 PrintAndLogEx(WARNING, "%s ERROR: Can't select PPSE AID. Error: %d", PSE_or_PPSE, res);
3c5fce2b 563 }
151a33c0 564
41bdfce3 565 if (!LeaveFieldON)
566 DropFieldEx( channel );
151a33c0 567
3c5fce2b
OM
568 return res;
569}
570
8d7d7b61 571int EMVSearch(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, bool decodeTLV, struct tlvdb *tlv) {
6b5105be 572 uint8_t aidbuf[APDU_DATA_LEN] = {0};
3c5fce2b 573 int aidlen = 0;
6b5105be 574 uint8_t data[APDU_RESPONSE_LEN] = {0};
3c5fce2b
OM
575 size_t datalen = 0;
576 uint16_t sw = 0;
151a33c0 577
3c5fce2b
OM
578 int res = 0;
579 int retrycnt = 0;
580 for(int i = 0; i < AIDlistLen; i ++) {
87fc2dad 581 param_gethex_to_eol(AIDlist[i].aid, 0, aidbuf, sizeof(aidbuf), &aidlen);
8d7d7b61 582 res = EMVSelect(channel, (i == 0) ? ActivateField : false, (i == AIDlistLen - 1) ? LeaveFieldON : true, aidbuf, aidlen, data, sizeof(data), &datalen, &sw, tlv);
3c5fce2b
OM
583 // retry if error and not returned sw error
584 if (res && res != 5) {
585 if (++retrycnt < 3){
586 i--;
587 } else {
8d7d7b61 588 // (1) - card select error, proxmark error OR (200) - result length = 0
589 if (res == 1 || res == 200) {
151a33c0 590 PrintAndLogEx(WARNING, "Exit...");
3c5fce2b
OM
591 return 1;
592 }
151a33c0 593
3c5fce2b 594 retrycnt = 0;
151a33c0 595 PrintAndLogEx(FAILED, "Retry failed [%s]. Skipped...", AIDlist[i].aid);
3c5fce2b
OM
596 }
597 continue;
598 }
599 retrycnt = 0;
151a33c0 600
3c5fce2b
OM
601 if (res)
602 continue;
151a33c0 603
604 if (!datalen)
605 continue;
606
607 if (decodeTLV) {
608 PrintAndLogEx(SUCCESS, "%s", AIDlist[i].aid);
3c5fce2b
OM
609 TLVPrintFromBuffer(data, datalen);
610 }
611 }
612
613 return 0;
614}
615
616int EMVSelectApplication(struct tlvdb *tlv, uint8_t *AID, size_t *AIDlen) {
87fc2dad 617 // check priority. 0x00 - highest
3c5fce2b 618 int prio = 0xffff;
151a33c0 619
3c5fce2b
OM
620 *AIDlen = 0;
621
622 struct tlvdb *ttmp = tlvdb_find(tlv, 0x6f);
623 if (!ttmp)
624 return 1;
151a33c0 625
3c5fce2b
OM
626 while (ttmp) {
627 const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x84, NULL);
628 const struct tlv *tgPrio = tlvdb_get_inchild(ttmp, 0x87, NULL);
151a33c0 629
3c5fce2b
OM
630 if (!tgAID)
631 break;
632
633 if (tgPrio) {
151a33c0 634 int pt = bytes_to_num((uint8_t*)tgPrio->value, (tgPrio->len < 2) ? tgPrio->len : 2);
3c5fce2b
OM
635 if (pt < prio) {
636 prio = pt;
151a33c0 637
3c5fce2b
OM
638 memcpy(AID, tgAID->value, tgAID->len);
639 *AIDlen = tgAID->len;
640 }
641 } else {
642 // takes the first application from list wo priority
643 if (!*AIDlen) {
644 memcpy(AID, tgAID->value, tgAID->len);
151a33c0 645 *AIDlen = tgAID->len;
3c5fce2b
OM
646 }
647 }
151a33c0 648
3c5fce2b
OM
649 ttmp = tlvdb_find_next(ttmp, 0x6f);
650 }
151a33c0 651
3c5fce2b
OM
652 return 0;
653}
654
6b5105be 655int 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)
656{
53fb848a 657 uint8_t GPO_APDU[APDU_COMMAND_LEN] = {0x80, ISO7816_GET_PROCESSING_OPTIONS, 0x00, 0x00, PDOLLen, 0x00};
6b5105be 658 memcpy(GPO_APDU + 5, PDOL, PDOLLen);
659 int apdulen = 5 + PDOLLen;
660
661 return EMVExchange(channel, LeaveFieldON, GPO_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv);
3c5fce2b
OM
662}
663
6b5105be 664int 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)
665{
666 uint8_t read_APDU[5] = {0x00, ISO7816_READ_RECORDS, SFIrec, (SFI << 3) | 0x04, 0x00};
667 int res = EMVExchange(channel, LeaveFieldON, read_APDU, sizeof(read_APDU), Result, MaxResultLen, ResultLen, sw, tlv);
c80eb8ba 668 if (*sw == 0x6700) {
151a33c0 669 PrintAndLogEx(INFO, ">>> trying to reissue command withouth Le...");
6b5105be 670 res = EMVExchangeEx(channel, false, LeaveFieldON, read_APDU, sizeof(read_APDU), Result, MaxResultLen, ResultLen, sw, tlv);
c80eb8ba
OM
671 }
672 return res;
87fc2dad 673}
3c5fce2b 674
6b5105be 675int 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)
676{
677 uint8_t CDOL_APDU[APDU_COMMAND_LEN] = {0x80, 0xae, RefControl, 0x00, CDOLLen, 0x00};
678 memcpy(CDOL_APDU + 5, CDOL, CDOLLen);
679 int apdulen = 5 + CDOLLen;
680
681 return EMVExchange(channel, LeaveFieldON, CDOL_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv);
3c5fce2b
OM
682}
683
6b5105be 684int EMVGenerateChallenge(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv)
685{
686 uint8_t get_challenge_APDU[APDU_COMMAND_LEN] = {0x00, ISO7816_GET_CHALLENGE, 0x00, 0x00};
687
688 int res = EMVExchange(channel, LeaveFieldON, get_challenge_APDU, 4, Result, MaxResultLen, ResultLen, sw, tlv);
c80eb8ba 689 if (*sw == 0x6700) {
151a33c0 690 PrintAndLogEx(INFO, ">>> trying to reissue command withouth Le...");
6b5105be 691 res = EMVExchangeEx(channel, false, LeaveFieldON, get_challenge_APDU, 4, Result, MaxResultLen, ResultLen, sw, tlv);
c80eb8ba
OM
692 }
693 return res;
87fc2dad 694}
3c5fce2b 695
6b5105be 696int 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)
697{
53fb848a 698 uint8_t authenticate_APDU[APDU_COMMAND_LEN] = {0x00, ISO7816_INTERNAL_AUTHENTICATE, 0x00, 0x00, DDOLLen, 0x00};
6b5105be 699 memcpy(authenticate_APDU + 5, DDOL, DDOLLen);
700 int apdulen = 5 + DDOLLen;
701
702 return EMVExchange(channel, LeaveFieldON, authenticate_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv);
d03fb293
OM
703}
704
6b5105be 705int 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)
706{
707 uint8_t checksum_APDU[APDU_COMMAND_LEN] = {0x80, 0x2a, 0x8e, 0x80, UDOLlen, 0x00};
708 memcpy(checksum_APDU + 5, UDOL, UDOLlen);
709 int apdulen = 5 + UDOLlen;
710
711 return EMVExchange(channel, LeaveFieldON, checksum_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv);
87fc2dad 712}
10d4f823 713
151a33c0 714// Authentication
1511ea28 715struct emv_pk *get_ca_pk(struct tlvdb *db) {
d03fb293
OM
716 const struct tlv *df_tlv = tlvdb_get(db, 0x84, NULL);
717 const struct tlv *caidx_tlv = tlvdb_get(db, 0x8f, NULL);
718
719 if (!df_tlv || !caidx_tlv || df_tlv->len < 6 || caidx_tlv->len != 1)
720 return NULL;
721
41bdfce3 722 PrintAndLogEx(NORMAL, "CA Public Key index 0x%0x", caidx_tlv->value[0]);
d03fb293
OM
723 return emv_pk_get_ca_pk(df_tlv->value, caidx_tlv->value[0]);
724}
725
726int trSDA(struct tlvdb *tlv) {
727
728 struct emv_pk *pk = get_ca_pk(tlv);
729 if (!pk) {
151a33c0 730 PrintAndLogEx(WARNING, "Error: Key not found. Exit.");
d03fb293
OM
731 return 2;
732 }
151a33c0 733
d03fb293
OM
734 struct emv_pk *issuer_pk = emv_pki_recover_issuer_cert(pk, tlv);
735 if (!issuer_pk) {
736 emv_pk_free(pk);
151a33c0 737 PrintAndLogEx(WARNING, "Error: Issuer certificate not found. Exit.");
d03fb293
OM
738 return 2;
739 }
740
151a33c0 741 PrintAndLogEx(SUCCESS, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx",
d03fb293
OM
742 issuer_pk->rid[0],
743 issuer_pk->rid[1],
744 issuer_pk->rid[2],
745 issuer_pk->rid[3],
746 issuer_pk->rid[4],
747 issuer_pk->index,
748 issuer_pk->serial[0],
749 issuer_pk->serial[1],
750 issuer_pk->serial[2]
751 );
752
753 const struct tlv *sda_tlv = tlvdb_get(tlv, 0x21, NULL);
754 if (!sda_tlv || sda_tlv->len < 1) {
755 emv_pk_free(issuer_pk);
756 emv_pk_free(pk);
151a33c0 757 PrintAndLogEx(WARNING, "Can't find input list for Offline Data Authentication. Exit.");
d03fb293
OM
758 return 3;
759 }
151a33c0 760
d03fb293
OM
761 struct tlvdb *dac_db = emv_pki_recover_dac(issuer_pk, tlv, sda_tlv);
762 if (dac_db) {
763 const struct tlv *dac_tlv = tlvdb_get(dac_db, 0x9f45, NULL);
4cdd63b2 764 PrintAndLogEx(NORMAL, "SDA verified OK. (Data Authentication Code: %02hhx:%02hhx)\n", dac_tlv->value[0], dac_tlv->value[1]);
d03fb293
OM
765 tlvdb_add(tlv, dac_db);
766 } else {
767 emv_pk_free(issuer_pk);
768 emv_pk_free(pk);
151a33c0 769 PrintAndLogEx(WARNING, "SSAD verify error");
d03fb293
OM
770 return 4;
771 }
151a33c0 772
d03fb293
OM
773 emv_pk_free(issuer_pk);
774 emv_pk_free(pk);
775 return 0;
776}
777
778static const unsigned char default_ddol_value[] = {0x9f, 0x37, 0x04};
779static struct tlv default_ddol_tlv = {.tag = 0x9f49, .len = 3, .value = default_ddol_value };
780
8d7d7b61 781int trDDA(EMVCommandChannel channel, bool decodeTLV, struct tlvdb *tlv) {
6b5105be 782 uint8_t buf[APDU_RESPONSE_LEN] = {0};
d03fb293
OM
783 size_t len = 0;
784 uint16_t sw = 0;
785
786 struct emv_pk *pk = get_ca_pk(tlv);
787 if (!pk) {
151a33c0 788 PrintAndLogEx(WARNING, "Error: Key not found. Exit.");
d03fb293
OM
789 return 2;
790 }
791
792 const struct tlv *sda_tlv = tlvdb_get(tlv, 0x21, NULL);
4cdd63b2 793/* if (!sda_tlv || sda_tlv->len < 1) { it may be 0!!!!
d03fb293 794 emv_pk_free(pk);
151a33c0 795 PrintAndLogEx(WARNING, "Error: Can't find input list for Offline Data Authentication. Exit.");
d03fb293
OM
796 return 3;
797 }
4cdd63b2 798*/
d03fb293
OM
799 struct emv_pk *issuer_pk = emv_pki_recover_issuer_cert(pk, tlv);
800 if (!issuer_pk) {
801 emv_pk_free(pk);
151a33c0 802 PrintAndLogEx(WARNING, "Error: Issuer certificate not found. Exit.");
d03fb293
OM
803 return 2;
804 }
151a33c0 805 PrintAndLogEx(SUCCESS, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
d03fb293
OM
806 issuer_pk->rid[0],
807 issuer_pk->rid[1],
808 issuer_pk->rid[2],
809 issuer_pk->rid[3],
810 issuer_pk->rid[4],
811 issuer_pk->index,
812 issuer_pk->serial[0],
813 issuer_pk->serial[1],
814 issuer_pk->serial[2]
815 );
151a33c0 816
d03fb293
OM
817 struct emv_pk *icc_pk = emv_pki_recover_icc_cert(issuer_pk, tlv, sda_tlv);
818 if (!icc_pk) {
819 emv_pk_free(pk);
820 emv_pk_free(issuer_pk);
4cdd63b2 821 PrintAndLogEx(WARNING, "Error: ICC certificate not found. Exit.");
d03fb293
OM
822 return 2;
823 }
151a33c0 824 PrintAndLogEx(SUCCESS, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
d03fb293
OM
825 icc_pk->rid[0],
826 icc_pk->rid[1],
827 icc_pk->rid[2],
828 icc_pk->rid[3],
829 icc_pk->rid[4],
830 icc_pk->index,
831 icc_pk->serial[0],
832 icc_pk->serial[1],
833 icc_pk->serial[2]
834 );
835
4cdd63b2 836 if (tlvdb_get(tlv, 0x9f2d, NULL)) {
837 struct emv_pk *icc_pe_pk = emv_pki_recover_icc_pe_cert(issuer_pk, tlv);
838 if (!icc_pe_pk) {
839 PrintAndLogEx(WARNING, "WARNING: ICC PE PK recover error. ");
840 } else {
841 PrintAndLogEx(SUCCESS, "ICC PE PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
842 icc_pe_pk->rid[0],
843 icc_pe_pk->rid[1],
844 icc_pe_pk->rid[2],
845 icc_pe_pk->rid[3],
846 icc_pe_pk->rid[4],
847 icc_pe_pk->index,
848 icc_pe_pk->serial[0],
849 icc_pe_pk->serial[1],
850 icc_pe_pk->serial[2]
851 );
852 }
d03fb293 853 } else {
4cdd63b2 854 PrintAndLogEx(INFO, "ICC PE PK (PIN Encipherment Public Key Certificate) not found.\n");
d03fb293
OM
855 }
856
857 // 9F4B: Signed Dynamic Application Data
858 const struct tlv *sdad_tlv = tlvdb_get(tlv, 0x9f4b, NULL);
859 // DDA with internal authenticate OR fDDA with filled 0x9F4B tag (GPO result)
860 // EMV kernel3 v2.4, contactless book C-3, C.1., page 147
861 if (sdad_tlv) {
151a33c0 862 PrintAndLogEx(NORMAL, "\n* * Got Signed Dynamic Application Data (9F4B) form GPO. Maybe fDDA...");
d03fb293
OM
863
864 const struct tlvdb *atc_db = emv_pki_recover_atc_ex(icc_pk, tlv, true);
865 if (!atc_db) {
151a33c0 866 PrintAndLogEx(WARNING, "Error: Can't recover IDN (ICC Dynamic Number)");
d03fb293
OM
867 emv_pk_free(pk);
868 emv_pk_free(issuer_pk);
869 emv_pk_free(icc_pk);
870 return 8;
871 }
872
873 // 9f36 Application Transaction Counter (ATC)
874 const struct tlv *atc_tlv = tlvdb_get(atc_db, 0x9f36, NULL);
875 if(atc_tlv) {
151a33c0 876 PrintAndLogEx(NORMAL, "\nATC (Application Transaction Counter) [%zu] %s", atc_tlv->len, sprint_hex_inrow(atc_tlv->value, atc_tlv->len));
877
d03fb293
OM
878 const struct tlv *core_atc_tlv = tlvdb_get(tlv, 0x9f36, NULL);
879 if(tlv_equal(core_atc_tlv, atc_tlv)) {
151a33c0 880 PrintAndLogEx(SUCCESS, "ATC check OK.");
881 PrintAndLogEx(SUCCESS, "fDDA (fast DDA) verified OK.");
d03fb293 882 } else {
151a33c0 883 PrintAndLogEx(WARNING, "Error: fDDA verified, but ATC in the certificate and ATC in the record not the same.");
d03fb293
OM
884 }
885 } else {
151a33c0 886 PrintAndLogEx(NORMAL, "\nERROR: fDDA (fast DDA) verify error");
d03fb293
OM
887 emv_pk_free(pk);
888 emv_pk_free(issuer_pk);
889 emv_pk_free(icc_pk);
890 return 9;
891 }
892 } else {
893 struct tlvdb *dac_db = emv_pki_recover_dac(issuer_pk, tlv, sda_tlv);
894 if (dac_db) {
895 const struct tlv *dac_tlv = tlvdb_get(dac_db, 0x9f45, NULL);
4cdd63b2 896 PrintAndLogEx(NORMAL, "SDAD verified OK. (Data Authentication Code: %02hhx:%02hhx)\n", dac_tlv->value[0], dac_tlv->value[1]);
d03fb293
OM
897 tlvdb_add(tlv, dac_db);
898 } else {
151a33c0 899 PrintAndLogEx(WARNING, "Error: SSAD verify error");
d03fb293
OM
900 emv_pk_free(pk);
901 emv_pk_free(issuer_pk);
902 emv_pk_free(icc_pk);
903 return 4;
904 }
151a33c0 905
906 PrintAndLogEx(NORMAL, "\n* Calc DDOL");
d03fb293
OM
907 const struct tlv *ddol_tlv = tlvdb_get(tlv, 0x9f49, NULL);
908 if (!ddol_tlv) {
909 ddol_tlv = &default_ddol_tlv;
151a33c0 910 PrintAndLogEx(NORMAL, "DDOL [9f49] not found. Using default DDOL");
d03fb293
OM
911 }
912
913 struct tlv *ddol_data_tlv = dol_process(ddol_tlv, tlv, 0);
914 if (!ddol_data_tlv) {
151a33c0 915 PrintAndLogEx(WARNING, "Error: Can't create DDOL TLV");
d03fb293
OM
916 emv_pk_free(pk);
917 emv_pk_free(issuer_pk);
918 emv_pk_free(icc_pk);
919 return 5;
920 }
921
151a33c0 922 PrintAndLogEx(NORMAL, "DDOL data[%d]: %s", ddol_data_tlv->len, sprint_hex(ddol_data_tlv->value, ddol_data_tlv->len));
d03fb293 923
151a33c0 924 PrintAndLogEx(NORMAL, "\n* Internal Authenticate");
8d7d7b61 925 int res = EMVInternalAuthenticate(channel, true, (uint8_t *)ddol_data_tlv->value, ddol_data_tlv->len, buf, sizeof(buf), &len, &sw, NULL);
151a33c0 926 if (res) {
927 PrintAndLogEx(WARNING, "Internal Authenticate error(%d): %4x. Exit...", res, sw);
d03fb293
OM
928 free(ddol_data_tlv);
929 emv_pk_free(pk);
930 emv_pk_free(issuer_pk);
931 emv_pk_free(icc_pk);
932 return 6;
933 }
934
935 struct tlvdb *dda_db = NULL;
936 if (buf[0] == 0x80) {
937 if (len < 3 ) {
151a33c0 938 PrintAndLogEx(WARNING, "Error: Internal Authenticate format1 parsing error. length=%d", len);
d03fb293 939 } else {
4cdd63b2 940 // parse response 0x80
941 struct tlvdb *t80 = tlvdb_parse_multi(buf, len);
942 const struct tlv * t80tlv = tlvdb_get_tlv(t80);
943
d03fb293 944 // 9f4b Signed Dynamic Application Data
4cdd63b2 945 dda_db = tlvdb_fixed(0x9f4b, t80tlv->len, t80tlv->value);
d03fb293 946 tlvdb_add(tlv, dda_db);
4cdd63b2 947
948 tlvdb_free(t80);
949
d03fb293 950 if (decodeTLV){
151a33c0 951 PrintAndLogEx(NORMAL, "* * Decode response format 1:");
d03fb293
OM
952 TLVPrintFromTLV(dda_db);
953 }
954 }
955 } else {
956 dda_db = tlvdb_parse_multi(buf, len);
957 if(!dda_db) {
151a33c0 958 PrintAndLogEx(WARNING, "Error: Can't parse Internal Authenticate result as TLV");
d03fb293
OM
959 free(ddol_data_tlv);
960 emv_pk_free(pk);
961 emv_pk_free(issuer_pk);
962 emv_pk_free(icc_pk);
963 return 7;
964 }
965 tlvdb_add(tlv, dda_db);
151a33c0 966
d03fb293
OM
967 if (decodeTLV)
968 TLVPrintFromTLV(dda_db);
969 }
970
971 struct tlvdb *idn_db = emv_pki_recover_idn_ex(icc_pk, dda_db, ddol_data_tlv, true);
972 free(ddol_data_tlv);
973 if (!idn_db) {
151a33c0 974 PrintAndLogEx(WARNING, "Error: Can't recover IDN (ICC Dynamic Number)");
d03fb293
OM
975 tlvdb_free(dda_db);
976 emv_pk_free(pk);
977 emv_pk_free(issuer_pk);
978 emv_pk_free(icc_pk);
979 return 8;
980 }
981 tlvdb_free(dda_db);
982
983 // 9f4c ICC Dynamic Number
984 const struct tlv *idn_tlv = tlvdb_get(idn_db, 0x9f4c, NULL);
985 if(idn_tlv) {
151a33c0 986 PrintAndLogEx(NORMAL, "\nIDN (ICC Dynamic Number) [%zu] %s", idn_tlv->len, sprint_hex_inrow(idn_tlv->value, idn_tlv->len));
987 PrintAndLogEx(NORMAL, "DDA verified OK.");
d03fb293
OM
988 tlvdb_add(tlv, idn_db);
989 tlvdb_free(idn_db);
990 } else {
151a33c0 991 PrintAndLogEx(NORMAL, "\nERROR: DDA verify error");
d03fb293
OM
992 tlvdb_free(idn_db);
993
994 emv_pk_free(pk);
995 emv_pk_free(issuer_pk);
996 emv_pk_free(icc_pk);
997 return 9;
998 }
999 }
151a33c0 1000
d03fb293
OM
1001 emv_pk_free(pk);
1002 emv_pk_free(issuer_pk);
1003 emv_pk_free(icc_pk);
10d4f823 1004 return 0;
1005}
1006
d03fb293
OM
1007int trCDA(struct tlvdb *tlv, struct tlvdb *ac_tlv, struct tlv *pdol_data_tlv, struct tlv *ac_data_tlv) {
1008
1009 struct emv_pk *pk = get_ca_pk(tlv);
1010 if (!pk) {
151a33c0 1011 PrintAndLogEx(WARNING, "Error: Key not found. Exit.");
d03fb293
OM
1012 return 2;
1013 }
1014
1015 const struct tlv *sda_tlv = tlvdb_get(tlv, 0x21, NULL);
1016 if (!sda_tlv || sda_tlv->len < 1) {
151a33c0 1017 PrintAndLogEx(WARNING, "Error: Can't find input list for Offline Data Authentication. Exit.");
d03fb293
OM
1018 emv_pk_free(pk);
1019 return 3;
1020 }
1021
1022 struct emv_pk *issuer_pk = emv_pki_recover_issuer_cert(pk, tlv);
1023 if (!issuer_pk) {
151a33c0 1024 PrintAndLogEx(WARNING, "Error: Issuer certificate not found. Exit.");
d03fb293
OM
1025 emv_pk_free(pk);
1026 return 2;
1027 }
151a33c0 1028 PrintAndLogEx(SUCCESS, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
d03fb293
OM
1029 issuer_pk->rid[0],
1030 issuer_pk->rid[1],
1031 issuer_pk->rid[2],
1032 issuer_pk->rid[3],
1033 issuer_pk->rid[4],
1034 issuer_pk->index,
1035 issuer_pk->serial[0],
1036 issuer_pk->serial[1],
1037 issuer_pk->serial[2]
1038 );
151a33c0 1039
d03fb293
OM
1040 struct emv_pk *icc_pk = emv_pki_recover_icc_cert(issuer_pk, tlv, sda_tlv);
1041 if (!icc_pk) {
41bdfce3 1042 PrintAndLogEx(WARNING, "Error: ICC certificate not found. Exit.");
d03fb293
OM
1043 emv_pk_free(pk);
1044 emv_pk_free(issuer_pk);
1045 return 2;
1046 }
151a33c0 1047 PrintAndLogEx(SUCCESS, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
d03fb293
OM
1048 icc_pk->rid[0],
1049 icc_pk->rid[1],
1050 icc_pk->rid[2],
1051 icc_pk->rid[3],
1052 icc_pk->rid[4],
1053 icc_pk->index,
1054 icc_pk->serial[0],
1055 icc_pk->serial[1],
1056 icc_pk->serial[2]
1057 );
1058
1059 struct tlvdb *dac_db = emv_pki_recover_dac(issuer_pk, tlv, sda_tlv);
1060 if (dac_db) {
1061 const struct tlv *dac_tlv = tlvdb_get(dac_db, 0x9f45, NULL);
151a33c0 1062 PrintAndLogEx(NORMAL, "SSAD verified OK. (%02hhx:%02hhx)", dac_tlv->value[0], dac_tlv->value[1]);
d03fb293
OM
1063 tlvdb_add(tlv, dac_db);
1064 } else {
151a33c0 1065 PrintAndLogEx(WARNING, "Error: SSAD verify error");
d03fb293
OM
1066 emv_pk_free(pk);
1067 emv_pk_free(issuer_pk);
1068 emv_pk_free(icc_pk);
1069 return 4;
1070 }
151a33c0 1071
1072 PrintAndLogEx(NORMAL, "\n* * Check Signed Dynamic Application Data (SDAD)");
d03fb293
OM
1073 struct tlvdb *idn_db = emv_pki_perform_cda_ex(icc_pk, tlv, ac_tlv,
1074 pdol_data_tlv, // pdol
1075 ac_data_tlv, // cdol1
151a33c0 1076 NULL, // cdol2
d03fb293
OM
1077 true);
1078 if (idn_db) {
1079 const struct tlv *idn_tlv = tlvdb_get(idn_db, 0x9f4c, NULL);
151a33c0 1080 PrintAndLogEx(NORMAL, "\nIDN (ICC Dynamic Number) [%zu] %s", idn_tlv->len, sprint_hex_inrow(idn_tlv->value, idn_tlv->len));
1081 PrintAndLogEx(NORMAL, "CDA verified OK.");
d03fb293
OM
1082 tlvdb_add(tlv, idn_db);
1083 } else {
151a33c0 1084 PrintAndLogEx(NORMAL, "\nERROR: CDA verify error");
d03fb293
OM
1085 }
1086
1087 emv_pk_free(pk);
1088 emv_pk_free(issuer_pk);
1089 emv_pk_free(icc_pk);
1090 return 0;
1091}
95b697f0
OM
1092
1093int RecoveryCertificates(struct tlvdb *tlvRoot, json_t *root) {
151a33c0 1094
95b697f0
OM
1095 struct emv_pk *pk = get_ca_pk(tlvRoot);
1096 if (!pk) {
1097 PrintAndLog("ERROR: Key not found. Exit.");
1098 return 1;
1099 }
1100
1101 struct emv_pk *issuer_pk = emv_pki_recover_issuer_cert(pk, tlvRoot);
1102 if (!issuer_pk) {
1103 emv_pk_free(pk);
1104 PrintAndLog("WARNING: Issuer certificate not found. Exit.");
1105 return 2;
1106 }
1511ea28 1107 PrintAndLogEx(SUCCESS, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx",
95b697f0
OM
1108 issuer_pk->rid[0],
1109 issuer_pk->rid[1],
1110 issuer_pk->rid[2],
1111 issuer_pk->rid[3],
1112 issuer_pk->rid[4],
1113 issuer_pk->index,
1114 issuer_pk->serial[0],
1115 issuer_pk->serial[1],
1116 issuer_pk->serial[2]
1117 );
1118
1119 JsonSaveBufAsHex(root, "$.ApplicationData.RID", issuer_pk->rid, 5);
1120
1121 char *issuer_pk_c = emv_pk_dump_pk(issuer_pk);
1122 JsonSaveStr(root, "$.ApplicationData.IssuerPublicKeyDec", issuer_pk_c);
1123 JsonSaveBufAsHex(root, "$.ApplicationData.IssuerPublicKeyModulus", issuer_pk->modulus, issuer_pk->mlen);
1124 free(issuer_pk_c);
1125
1126 struct emv_pk *icc_pk = emv_pki_recover_icc_cert(issuer_pk, tlvRoot, NULL);
1127 if (!icc_pk) {
1128 emv_pk_free(pk);
1129 emv_pk_free(issuer_pk);
1511ea28 1130 PrintAndLogEx(WARNING, "WARNING: ICC certificate not found. Exit.");
95b697f0
OM
1131 return 2;
1132 }
1511ea28 1133 PrintAndLogEx(SUCCESS, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
95b697f0
OM
1134 icc_pk->rid[0],
1135 icc_pk->rid[1],
1136 icc_pk->rid[2],
1137 icc_pk->rid[3],
1138 icc_pk->rid[4],
1139 icc_pk->index,
1140 icc_pk->serial[0],
1141 icc_pk->serial[1],
1142 icc_pk->serial[2]
1143 );
151a33c0 1144
95b697f0
OM
1145 char *icc_pk_c = emv_pk_dump_pk(icc_pk);
1146 JsonSaveStr(root, "$.ApplicationData.ICCPublicKeyDec", icc_pk_c);
1147 JsonSaveBufAsHex(root, "$.ApplicationData.ICCPublicKeyModulus", icc_pk->modulus, icc_pk->mlen);
1148 free(issuer_pk_c);
151a33c0 1149
95b697f0
OM
1150 return 0;
1151}
Impressum, Datenschutz