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