]> git.zerfleddert.de Git - proxmark3-svn/blame - client/emv/emvcore.c
small improvements in auth (#694)
[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"
12
3c5fce2b
OM
13// Got from here. Thanks)
14// https://eftlab.co.uk/index.php/site-map/knowledge-base/211-emv-aid-rid-pix
87fc2dad 15static const char *PSElist [] = {
3c5fce2b
OM
16 "325041592E5359532E4444463031", // 2PAY.SYS.DDF01 - Visa Proximity Payment System Environment - PPSE
17 "315041592E5359532E4444463031" // 1PAY.SYS.DDF01 - Visa Payment System Environment - PSE
18};
d03fb293 19//static const size_t PSElistLen = sizeof(PSElist)/sizeof(char*);
3c5fce2b 20
87fc2dad 21typedef struct {
22 enum CardPSVendor vendor;
23 const char* aid;
24} TAIDList;
25
26static const TAIDList AIDlist [] = {
3c5fce2b 27 // Visa International
87fc2dad 28 { CV_VISA, "A00000000305076010"}, // VISA ELO Credit
29 { CV_VISA, "A0000000031010" }, // VISA Debit/Credit (Classic)
30 { CV_VISA, "A000000003101001" }, // VISA Credit
31 { CV_VISA, "A000000003101002" }, // VISA Debit
32 { CV_VISA, "A0000000032010" }, // VISA Electron
33 { CV_VISA, "A0000000032020" }, // VISA
34 { CV_VISA, "A0000000033010" }, // VISA Interlink
35 { CV_VISA, "A0000000034010" }, // VISA Specific
36 { CV_VISA, "A0000000035010" }, // VISA Specific
37 { CV_VISA, "A0000000036010" }, // Domestic Visa Cash Stored Value
38 { CV_VISA, "A0000000036020" }, // International Visa Cash Stored Value
39 { CV_VISA, "A0000000038002" }, // VISA Auth, VisaRemAuthen EMV-CAP (DPA)
40 { CV_VISA, "A0000000038010" }, // VISA Plus
41 { CV_VISA, "A0000000039010" }, // VISA Loyalty
42 { CV_VISA, "A000000003999910" }, // VISA Proprietary ATM
3c5fce2b 43 // Visa USA
87fc2dad 44 { CV_VISA, "A000000098" }, // Debit Card
45 { CV_VISA, "A0000000980848" }, // Debit Card
3c5fce2b 46 // Mastercard International
87fc2dad 47 { CV_MASTERCARD, "A00000000401" }, // MasterCard PayPass
48 { CV_MASTERCARD, "A0000000041010" }, // MasterCard Credit
49 { CV_MASTERCARD, "A00000000410101213" }, // MasterCard Credit
50 { CV_MASTERCARD, "A00000000410101215" }, // MasterCard Credit
51 { CV_MASTERCARD, "A0000000042010" }, // MasterCard Specific
52 { CV_MASTERCARD, "A0000000043010" }, // MasterCard Specific
53 { CV_MASTERCARD, "A0000000043060" }, // Maestro (Debit)
54 { CV_MASTERCARD, "A000000004306001" }, // Maestro (Debit)
55 { CV_MASTERCARD, "A0000000044010" }, // MasterCard Specific
56 { CV_MASTERCARD, "A0000000045010" }, // MasterCard Specific
57 { CV_MASTERCARD, "A0000000046000" }, // Cirrus
58 { CV_MASTERCARD, "A0000000048002" }, // SecureCode Auth EMV-CAP
59 { CV_MASTERCARD, "A0000000049999" }, // MasterCard PayPass
3c5fce2b 60 // American Express
87fc2dad 61 { CV_AMERICANEXPRESS, "A000000025" },
62 { CV_AMERICANEXPRESS, "A0000000250000" },
63 { CV_AMERICANEXPRESS, "A00000002501" },
64 { CV_AMERICANEXPRESS, "A000000025010402" },
65 { CV_AMERICANEXPRESS, "A000000025010701" },
66 { CV_AMERICANEXPRESS, "A000000025010801" },
3c5fce2b 67 // Groupement des Cartes Bancaires "CB"
87fc2dad 68 { CV_CB, "A0000000421010" }, // Cartes Bancaire EMV Card
69 { CV_CB, "A0000000422010" },
70 { CV_CB, "A0000000423010" },
71 { CV_CB, "A0000000424010" },
72 { CV_CB, "A0000000425010" },
3c5fce2b 73 // JCB CO., LTD.
87fc2dad 74 { CV_JCB, "A00000006510" }, // JCB
75 { CV_JCB, "A0000000651010" }, // JCB J Smart Credit
76 // Other
77 { CV_OTHER, "A0000001544442" }, // Banricompras Debito - Banrisul - Banco do Estado do Rio Grande do SUL - S.A.
78 { CV_OTHER, "F0000000030001" }, // BRADESCO
79 { CV_OTHER, "A0000005241010" }, // RuPay - RuPay
80 { CV_OTHER, "D5780000021010" } // Bankaxept - Bankaxept
3c5fce2b 81};
87fc2dad 82static const size_t AIDlistLen = sizeof(AIDlist)/sizeof(TAIDList);
3c5fce2b
OM
83
84static bool APDULogging = false;
85void SetAPDULogging(bool logging) {
86 APDULogging = logging;
87}
88
87fc2dad 89enum CardPSVendor GetCardPSVendor(uint8_t * AID, size_t AIDlen) {
90 char buf[100] = {0};
91 if (AIDlen < 1)
92 return CV_NA;
93
94 hex_to_buffer((uint8_t *)buf, AID, AIDlen, sizeof(buf) - 1, 0, 0, true);
95
96 for(int i = 0; i < AIDlistLen; i ++) {
97 if (strncmp(AIDlist[i].aid, buf, strlen(AIDlist[i].aid)) == 0){
98 return AIDlist[i].vendor;
99 }
100 }
101
102 return CV_NA;
103}
104
33a9982c 105static bool print_cb(void *data, const struct tlv *tlv, int level, bool is_leaf) {
43912d63 106 emv_tag_dump(tlv, stdout, level);
33a9982c 107 if (is_leaf) {
108 dump_buffer(tlv->value, tlv->len, stdout, level);
109 }
a2bb2735 110
111 return true;
112}
113
114void TLVPrintFromBuffer(uint8_t *data, int datalen) {
115 struct tlvdb *t = NULL;
23207d74 116 t = tlvdb_parse_multi(data, datalen);
a2bb2735 117 if (t) {
3c5fce2b 118 PrintAndLog("-------------------- TLV decoded --------------------");
a2bb2735 119
43912d63 120 tlvdb_visit(t, print_cb, NULL, 0);
a2bb2735 121 tlvdb_free(t);
122 } else {
123 PrintAndLog("TLV ERROR: Can't parse response as TLV tree.");
124 }
125}
3c5fce2b 126
87fc2dad 127void TLVPrintFromTLVLev(struct tlvdb *tlv, int level) {
3c5fce2b
OM
128 if (!tlv)
129 return;
130
87fc2dad 131 tlvdb_visit(tlv, print_cb, NULL, level);
132}
133
134void TLVPrintFromTLV(struct tlvdb *tlv) {
135 TLVPrintFromTLVLev(tlv, 0);
3c5fce2b
OM
136}
137
138void TLVPrintAIDlistFromSelectTLV(struct tlvdb *tlv) {
139 PrintAndLog("|------------------|--------|-------------------------|");
140 PrintAndLog("| AID |Priority| Name |");
141 PrintAndLog("|------------------|--------|-------------------------|");
142
143 struct tlvdb *ttmp = tlvdb_find(tlv, 0x6f);
144 if (!ttmp)
145 PrintAndLog("| none |");
146
147 while (ttmp) {
148 const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x84, NULL);
149 const struct tlv *tgName = tlvdb_get_inchild(ttmp, 0x50, NULL);
150 const struct tlv *tgPrio = tlvdb_get_inchild(ttmp, 0x87, NULL);
151 if (!tgAID)
152 break;
153 PrintAndLog("|%s| %s |%s|",
154 sprint_hex_inrow_ex(tgAID->value, tgAID->len, 18),
155 (tgPrio) ? sprint_hex(tgPrio->value, 1) : " ",
156 (tgName) ? sprint_ascii_ex(tgName->value, tgName->len, 25) : " ");
157
158 ttmp = tlvdb_find_next(ttmp, 0x6f);
159 }
160
161 PrintAndLog("|------------------|--------|-------------------------|");
162}
163
87fc2dad 164struct tlvdb *GetPANFromTrack2(const struct tlv *track2) {
165 char track2Hex[200] = {0};
166 uint8_t PAN[100] = {0};
167 int PANlen = 0;
168 char *tmp = track2Hex;
3c5fce2b 169
87fc2dad 170 if (!track2)
171 return NULL;
172
173 for (int i = 0; i < track2->len; ++i, tmp += 2)
174 sprintf(tmp, "%02x", (unsigned int)track2->value[i]);
175
176 int posD = strchr(track2Hex, 'd') - track2Hex;
177 if (posD < 1)
178 return NULL;
179
180 track2Hex[posD] = 0;
181 if (strlen(track2Hex) % 2) {
182 track2Hex[posD] = 'F';
183 track2Hex[posD + 1] = '\0';
184 }
185
186 param_gethex_to_eol(track2Hex, 0, PAN, sizeof(PAN), &PANlen);
187
188 return tlvdb_fixed(0x5a, PANlen, PAN);
189}
190
191struct tlvdb *GetdCVVRawFromTrack2(const struct tlv *track2) {
192 char track2Hex[200] = {0};
193 char dCVVHex[100] = {0};
194 uint8_t dCVV[100] = {0};
195 int dCVVlen = 0;
196 const int PINlen = 5; // must calculated from 9F67 MSD Offset but i have not seen this tag)
197 char *tmp = track2Hex;
198
199 if (!track2)
200 return NULL;
201
202 for (int i = 0; i < track2->len; ++i, tmp += 2)
203 sprintf(tmp, "%02x", (unsigned int)track2->value[i]);
204
205 int posD = strchr(track2Hex, 'd') - track2Hex;
206 if (posD < 1)
207 return NULL;
208
209 memset(dCVVHex, '0', 32);
210 // ATC
211 memcpy(dCVVHex + 0, track2Hex + posD + PINlen + 11, 4);
212 // PAN 5 hex
213 memcpy(dCVVHex + 4, track2Hex, 5);
214 // expire date
215 memcpy(dCVVHex + 9, track2Hex + posD + 1, 4);
216 // service code
217 memcpy(dCVVHex + 13, track2Hex + posD + 5, 3);
218
219 param_gethex_to_eol(dCVVHex, 0, dCVV, sizeof(dCVV), &dCVVlen);
220
221 return tlvdb_fixed(0x02, dCVVlen, dCVV);
222}
223
c80eb8ba 224int EMVExchangeEx(bool ActivateField, bool LeaveFieldON, sAPDU apdu, bool IncludeLe, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
3c5fce2b 225 uint8_t data[APDU_RES_LEN] = {0};
87fc2dad 226
3c5fce2b
OM
227 *ResultLen = 0;
228 if (sw) *sw = 0;
229 uint16_t isw = 0;
230
3c5fce2b
OM
231 if (ActivateField)
232 DropField();
233
87fc2dad 234 // COMPUTE APDU
235 memcpy(data, &apdu, 5);
236 if (apdu.data)
237 memcpy(&data[5], apdu.data, apdu.Lc);
238
3c5fce2b 239 if (APDULogging)
c80eb8ba 240 PrintAndLog(">>>> %s", sprint_hex(data, (IncludeLe?6:5) + apdu.Lc));
3c5fce2b 241
c80eb8ba
OM
242 // 6 byes + data = INS + CLA + P1 + P2 + Lc + <data = Nc> + Le(?IncludeLe)
243 int res = ExchangeAPDU14a(data, (IncludeLe?6:5) + apdu.Lc, ActivateField, LeaveFieldON, Result, (int)MaxResultLen, (int *)ResultLen);
3c5fce2b 244
3c5fce2b
OM
245 if (res) {
246 return res;
247 }
87fc2dad 248
10d4f823 249 if (APDULogging)
250 PrintAndLog("<<<< %s", sprint_hex(Result, *ResultLen));
251
3c5fce2b
OM
252 *ResultLen -= 2;
253 isw = Result[*ResultLen] * 0x0100 + Result[*ResultLen + 1];
254 if (sw)
255 *sw = isw;
256
257 if (isw != 0x9000) {
258 if (APDULogging)
87fc2dad 259 PrintAndLog("APDU(%02x%02x) ERROR: [%4X] %s", apdu.CLA, apdu.INS, isw, GetAPDUCodeDescription(*sw >> 8, *sw & 0xff));
3c5fce2b
OM
260 return 5;
261 }
262
263 // add to tlv tree
264 if (tlv) {
265 struct tlvdb *t = tlvdb_parse_multi(Result, *ResultLen);
266 tlvdb_add(tlv, t);
267 }
268
269 return 0;
270}
271
87fc2dad 272int EMVExchange(bool LeaveFieldON, sAPDU apdu, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
c80eb8ba 273 return EMVExchangeEx(false, LeaveFieldON, apdu, true, Result, MaxResultLen, ResultLen, sw, tlv);
87fc2dad 274}
275
276int EMVSelect(bool ActivateField, bool LeaveFieldON, uint8_t *AID, size_t AIDLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
c80eb8ba 277 return EMVExchangeEx(ActivateField, LeaveFieldON, (sAPDU){0x00, 0xa4, 0x04, 0x00, AIDLen, AID}, true, Result, MaxResultLen, ResultLen, sw, tlv);
87fc2dad 278}
279
3c5fce2b
OM
280int EMVSelectPSE(bool ActivateField, bool LeaveFieldON, uint8_t PSENum, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw) {
281 uint8_t buf[APDU_AID_LEN] = {0};
282 *ResultLen = 0;
283 int len = 0;
284 int res = 0;
285 switch (PSENum) {
286 case 1:
287 param_gethex_to_eol(PSElist[1], 0, buf, sizeof(buf), &len);
288 break;
289 case 2:
290 param_gethex_to_eol(PSElist[0], 0, buf, sizeof(buf), &len);
291 break;
292 default:
293 return -1;
294 }
295
296 // select
297 res = EMVSelect(ActivateField, LeaveFieldON, buf, len, Result, MaxResultLen, ResultLen, sw, NULL);
298
299 return res;
300}
301
302int EMVSearchPSE(bool ActivateField, bool LeaveFieldON, bool decodeTLV, struct tlvdb *tlv) {
303 uint8_t data[APDU_RES_LEN] = {0};
304 size_t datalen = 0;
305 uint16_t sw = 0;
306 int res;
307
308 // select PPSE
309 res = EMVSelectPSE(ActivateField, true, 2, data, sizeof(data), &datalen, &sw);
310
311 if (!res){
312 struct tlvdb *t = NULL;
313 t = tlvdb_parse_multi(data, datalen);
314 if (t) {
315 int retrycnt = 0;
316 struct tlvdb *ttmp = tlvdb_find_path(t, (tlv_tag_t[]){0x6f, 0xa5, 0xbf0c, 0x61, 0x00});
317 if (!ttmp)
318 PrintAndLog("PPSE don't have records.");
319
320 while (ttmp) {
321 const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x4f, NULL);
322 if (tgAID) {
323 res = EMVSelect(false, true, (uint8_t *)tgAID->value, tgAID->len, data, sizeof(data), &datalen, &sw, tlv);
324
325 // retry if error and not returned sw error
326 if (res && res != 5) {
327 if (++retrycnt < 3){
328 continue;
329 } else {
330 // card select error, proxmark error
331 if (res == 1) {
332 PrintAndLog("Exit...");
333 return 1;
334 }
335
336 retrycnt = 0;
337 PrintAndLog("Retry failed [%s]. Skiped...", sprint_hex_inrow(tgAID->value, tgAID->len));
338 }
339
340 // next element
341 ttmp = tlvdb_find_next(ttmp, 0x61);
342 continue;
343 }
344 retrycnt = 0;
345
346 // all is ok
347 if (decodeTLV){
348 PrintAndLog("%s:", sprint_hex_inrow(tgAID->value, tgAID->len));
349 TLVPrintFromBuffer(data, datalen);
350 }
351 }
352
353 ttmp = tlvdb_find_next(ttmp, 0x61);
354 }
355
356 tlvdb_free(t);
357 } else {
358 PrintAndLog("PPSE ERROR: Can't get TLV from response.");
359 }
360 } else {
361 PrintAndLog("PPSE ERROR: Can't select PPSE AID. Error: %d", res);
362 }
363
364 if(!LeaveFieldON)
365 DropField();
366
367 return res;
368}
369
370int EMVSearch(bool ActivateField, bool LeaveFieldON, bool decodeTLV, struct tlvdb *tlv) {
371 uint8_t aidbuf[APDU_AID_LEN] = {0};
372 int aidlen = 0;
373 uint8_t data[APDU_RES_LEN] = {0};
374 size_t datalen = 0;
375 uint16_t sw = 0;
376
377 int res = 0;
378 int retrycnt = 0;
379 for(int i = 0; i < AIDlistLen; i ++) {
87fc2dad 380 param_gethex_to_eol(AIDlist[i].aid, 0, aidbuf, sizeof(aidbuf), &aidlen);
3c5fce2b
OM
381 res = EMVSelect((i == 0) ? ActivateField : false, (i == AIDlistLen - 1) ? LeaveFieldON : true, aidbuf, aidlen, data, sizeof(data), &datalen, &sw, tlv);
382 // retry if error and not returned sw error
383 if (res && res != 5) {
384 if (++retrycnt < 3){
385 i--;
386 } else {
387 // card select error, proxmark error
388 if (res == 1) {
389 PrintAndLog("Exit...");
390 return 1;
391 }
392
393 retrycnt = 0;
87fc2dad 394 PrintAndLog("Retry failed [%s]. Skiped...", AIDlist[i].aid);
3c5fce2b
OM
395 }
396 continue;
397 }
398 retrycnt = 0;
399
400 if (res)
401 continue;
402
403 if (decodeTLV){
87fc2dad 404 PrintAndLog("%s:", AIDlist[i].aid);
3c5fce2b
OM
405 TLVPrintFromBuffer(data, datalen);
406 }
407 }
408
409 return 0;
410}
411
412int EMVSelectApplication(struct tlvdb *tlv, uint8_t *AID, size_t *AIDlen) {
87fc2dad 413 // check priority. 0x00 - highest
3c5fce2b
OM
414 int prio = 0xffff;
415
416 *AIDlen = 0;
417
418 struct tlvdb *ttmp = tlvdb_find(tlv, 0x6f);
419 if (!ttmp)
420 return 1;
421
422 while (ttmp) {
423 const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x84, NULL);
424 const struct tlv *tgPrio = tlvdb_get_inchild(ttmp, 0x87, NULL);
425
426 if (!tgAID)
427 break;
428
429 if (tgPrio) {
430 int pt = bytes_to_num((uint8_t*)tgPrio->value, (tgPrio->len < 2) ? tgPrio->len : 2);
431 if (pt < prio) {
432 prio = pt;
433
434 memcpy(AID, tgAID->value, tgAID->len);
435 *AIDlen = tgAID->len;
436 }
437 } else {
438 // takes the first application from list wo priority
439 if (!*AIDlen) {
440 memcpy(AID, tgAID->value, tgAID->len);
441 *AIDlen = tgAID->len;
442 }
443 }
444
445 ttmp = tlvdb_find_next(ttmp, 0x6f);
446 }
447
448 return 0;
449}
450
451int EMVGPO(bool LeaveFieldON, uint8_t *PDOL, size_t PDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
87fc2dad 452 return EMVExchange(LeaveFieldON, (sAPDU){0x80, 0xa8, 0x00, 0x00, PDOLLen, PDOL}, Result, MaxResultLen, ResultLen, sw, tlv);
3c5fce2b
OM
453}
454
455int EMVReadRecord(bool LeaveFieldON, uint8_t SFI, uint8_t SFIrec, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
c80eb8ba
OM
456 int res = EMVExchange(LeaveFieldON, (sAPDU){0x00, 0xb2, SFIrec, (SFI << 3) | 0x04, 0, NULL}, Result, MaxResultLen, ResultLen, sw, tlv);
457 if (*sw == 0x6700) {
458 PrintAndLog(">>> trying to reissue command withouth Le...");
459 res = EMVExchangeEx(false, LeaveFieldON, (sAPDU){0x00, 0xb2, SFIrec, (SFI << 3) | 0x04, 0, NULL}, false, Result, MaxResultLen, ResultLen, sw, tlv);
460 }
461 return res;
87fc2dad 462}
3c5fce2b 463
87fc2dad 464int EMVAC(bool LeaveFieldON, uint8_t RefControl, uint8_t *CDOL, size_t CDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
465 return EMVExchange(LeaveFieldON, (sAPDU){0x80, 0xae, RefControl, 0x00, CDOLLen, CDOL}, Result, MaxResultLen, ResultLen, sw, tlv);
3c5fce2b
OM
466}
467
87fc2dad 468int EMVGenerateChallenge(bool LeaveFieldON, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
c80eb8ba
OM
469 int res = EMVExchange(LeaveFieldON, (sAPDU){0x00, 0x84, 0x00, 0x00, 0x00, NULL}, Result, MaxResultLen, ResultLen, sw, tlv);
470 if (*sw == 0x6700) {
471 PrintAndLog(">>> trying to reissue command withouth Le...");
472 res = EMVExchangeEx(false, LeaveFieldON, (sAPDU){0x00, 0x84, 0x00, 0x00, 0x00, NULL}, false, Result, MaxResultLen, ResultLen, sw, tlv);
473 }
474 return res;
87fc2dad 475}
3c5fce2b 476
d03fb293
OM
477int EMVInternalAuthenticate(bool LeaveFieldON, uint8_t *DDOL, size_t DDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
478 return EMVExchange(LeaveFieldON, (sAPDU){0x00, 0x88, 0x00, 0x00, DDOLLen, DDOL}, Result, MaxResultLen, ResultLen, sw, tlv);
479}
480
87fc2dad 481int MSCComputeCryptoChecksum(bool LeaveFieldON, uint8_t *UDOL, uint8_t UDOLlen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
482 return EMVExchange(LeaveFieldON, (sAPDU){0x80, 0x2a, 0x8e, 0x80, UDOLlen, UDOL}, Result, MaxResultLen, ResultLen, sw, tlv);
483}
10d4f823 484
485// Authentication
d03fb293
OM
486static struct emv_pk *get_ca_pk(struct tlvdb *db) {
487 const struct tlv *df_tlv = tlvdb_get(db, 0x84, NULL);
488 const struct tlv *caidx_tlv = tlvdb_get(db, 0x8f, NULL);
489
490 if (!df_tlv || !caidx_tlv || df_tlv->len < 6 || caidx_tlv->len != 1)
491 return NULL;
492
493 PrintAndLog("CA public key index 0x%0x", caidx_tlv->value[0]);
494 return emv_pk_get_ca_pk(df_tlv->value, caidx_tlv->value[0]);
495}
496
497int trSDA(struct tlvdb *tlv) {
498
499 struct emv_pk *pk = get_ca_pk(tlv);
500 if (!pk) {
501 PrintAndLog("ERROR: Key not found. Exit.");
502 return 2;
503 }
10d4f823 504
d03fb293
OM
505 struct emv_pk *issuer_pk = emv_pki_recover_issuer_cert(pk, tlv);
506 if (!issuer_pk) {
507 emv_pk_free(pk);
508 PrintAndLog("ERROR: Issuer certificate not found. Exit.");
509 return 2;
510 }
511
512 PrintAndLog("Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx",
513 issuer_pk->rid[0],
514 issuer_pk->rid[1],
515 issuer_pk->rid[2],
516 issuer_pk->rid[3],
517 issuer_pk->rid[4],
518 issuer_pk->index,
519 issuer_pk->serial[0],
520 issuer_pk->serial[1],
521 issuer_pk->serial[2]
522 );
523
524 const struct tlv *sda_tlv = tlvdb_get(tlv, 0x21, NULL);
525 if (!sda_tlv || sda_tlv->len < 1) {
526 emv_pk_free(issuer_pk);
527 emv_pk_free(pk);
528 PrintAndLog("ERROR: Can't find input list for Offline Data Authentication. Exit.");
529 return 3;
530 }
10d4f823 531
d03fb293
OM
532 struct tlvdb *dac_db = emv_pki_recover_dac(issuer_pk, tlv, sda_tlv);
533 if (dac_db) {
534 const struct tlv *dac_tlv = tlvdb_get(dac_db, 0x9f45, NULL);
535 PrintAndLog("SDA verified OK. (%02hhx:%02hhx)\n", dac_tlv->value[0], dac_tlv->value[1]);
536 tlvdb_add(tlv, dac_db);
537 } else {
538 emv_pk_free(issuer_pk);
539 emv_pk_free(pk);
540 PrintAndLog("ERROR: SSAD verify error");
541 return 4;
542 }
10d4f823 543
d03fb293
OM
544 emv_pk_free(issuer_pk);
545 emv_pk_free(pk);
546 return 0;
547}
548
549static const unsigned char default_ddol_value[] = {0x9f, 0x37, 0x04};
550static struct tlv default_ddol_tlv = {.tag = 0x9f49, .len = 3, .value = default_ddol_value };
551
552int trDDA(bool decodeTLV, struct tlvdb *tlv) {
553 uint8_t buf[APDU_RES_LEN] = {0};
554 size_t len = 0;
555 uint16_t sw = 0;
556
557 struct emv_pk *pk = get_ca_pk(tlv);
558 if (!pk) {
559 PrintAndLog("ERROR: Key not found. Exit.");
560 return 2;
561 }
562
563 const struct tlv *sda_tlv = tlvdb_get(tlv, 0x21, NULL);
564 if (!sda_tlv || sda_tlv->len < 1) {
565 emv_pk_free(pk);
566 PrintAndLog("ERROR: Can't find input list for Offline Data Authentication. Exit.");
567 return 3;
568 }
569
570 struct emv_pk *issuer_pk = emv_pki_recover_issuer_cert(pk, tlv);
571 if (!issuer_pk) {
572 emv_pk_free(pk);
573 PrintAndLog("ERROR: Issuer certificate not found. Exit.");
574 return 2;
575 }
576 printf("Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
577 issuer_pk->rid[0],
578 issuer_pk->rid[1],
579 issuer_pk->rid[2],
580 issuer_pk->rid[3],
581 issuer_pk->rid[4],
582 issuer_pk->index,
583 issuer_pk->serial[0],
584 issuer_pk->serial[1],
585 issuer_pk->serial[2]
586 );
587
588 struct emv_pk *icc_pk = emv_pki_recover_icc_cert(issuer_pk, tlv, sda_tlv);
589 if (!icc_pk) {
590 emv_pk_free(pk);
591 emv_pk_free(issuer_pk);
592 PrintAndLog("ERROR: ICC setrificate not found. Exit.");
593 return 2;
594 }
595 printf("ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
596 icc_pk->rid[0],
597 icc_pk->rid[1],
598 icc_pk->rid[2],
599 icc_pk->rid[3],
600 icc_pk->rid[4],
601 icc_pk->index,
602 icc_pk->serial[0],
603 icc_pk->serial[1],
604 icc_pk->serial[2]
605 );
606
607 struct emv_pk *icc_pe_pk = emv_pki_recover_icc_pe_cert(issuer_pk, tlv);
608 if (!icc_pe_pk) {
609 PrintAndLog("WARNING: ICC PE PK recover error. ");
610 } else {
611 printf("ICC PE PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
612 icc_pe_pk->rid[0],
613 icc_pe_pk->rid[1],
614 icc_pe_pk->rid[2],
615 icc_pe_pk->rid[3],
616 icc_pe_pk->rid[4],
617 icc_pe_pk->index,
618 icc_pe_pk->serial[0],
619 icc_pe_pk->serial[1],
620 icc_pe_pk->serial[2]
621 );
622 }
623
624 // 9F4B: Signed Dynamic Application Data
625 const struct tlv *sdad_tlv = tlvdb_get(tlv, 0x9f4b, NULL);
626 // DDA with internal authenticate OR fDDA with filled 0x9F4B tag (GPO result)
627 // EMV kernel3 v2.4, contactless book C-3, C.1., page 147
628 if (sdad_tlv) {
629 PrintAndLog("\n* * Got Signed Dynamic Application Data (9F4B) form GPO. Maybe fDDA...");
630
631 const struct tlvdb *atc_db = emv_pki_recover_atc_ex(icc_pk, tlv, true);
632 if (!atc_db) {
633 PrintAndLog("ERROR: Can't recover IDN (ICC Dynamic Number)");
634 emv_pk_free(pk);
635 emv_pk_free(issuer_pk);
636 emv_pk_free(icc_pk);
637 return 8;
638 }
639
640 // 9f36 Application Transaction Counter (ATC)
641 const struct tlv *atc_tlv = tlvdb_get(atc_db, 0x9f36, NULL);
642 if(atc_tlv) {
643 PrintAndLog("\nATC (Application Transaction Counter) [%zu] %s", atc_tlv->len, sprint_hex_inrow(atc_tlv->value, atc_tlv->len));
644
645 const struct tlv *core_atc_tlv = tlvdb_get(tlv, 0x9f36, NULL);
646 if(tlv_equal(core_atc_tlv, atc_tlv)) {
647 PrintAndLog("ATC check OK.");
648 PrintAndLog("fDDA (fast DDA) verified OK.");
649 } else {
650 PrintAndLog("ERROR: fDDA verified, but ATC in the certificate and ATC in the record not the same.");
651 }
652 } else {
653 PrintAndLog("\nERROR: fDDA (fast DDA) verify error");
654 emv_pk_free(pk);
655 emv_pk_free(issuer_pk);
656 emv_pk_free(icc_pk);
657 return 9;
658 }
659 } else {
660 struct tlvdb *dac_db = emv_pki_recover_dac(issuer_pk, tlv, sda_tlv);
661 if (dac_db) {
662 const struct tlv *dac_tlv = tlvdb_get(dac_db, 0x9f45, NULL);
663 printf("SDA verified OK. (%02hhx:%02hhx)\n", dac_tlv->value[0], dac_tlv->value[1]);
664 tlvdb_add(tlv, dac_db);
665 } else {
666 PrintAndLog("ERROR: SSAD verify error");
667 emv_pk_free(pk);
668 emv_pk_free(issuer_pk);
669 emv_pk_free(icc_pk);
670 return 4;
671 }
672
673 PrintAndLog("\n* Calc DDOL");
674 const struct tlv *ddol_tlv = tlvdb_get(tlv, 0x9f49, NULL);
675 if (!ddol_tlv) {
676 ddol_tlv = &default_ddol_tlv;
677 PrintAndLog("DDOL [9f49] not found. Using default DDOL");
678 }
679
680 struct tlv *ddol_data_tlv = dol_process(ddol_tlv, tlv, 0);
681 if (!ddol_data_tlv) {
682 PrintAndLog("ERROR: Can't create DDOL TLV");
683 emv_pk_free(pk);
684 emv_pk_free(issuer_pk);
685 emv_pk_free(icc_pk);
686 return 5;
687 }
688
689 PrintAndLog("DDOL data[%d]: %s", ddol_data_tlv->len, sprint_hex(ddol_data_tlv->value, ddol_data_tlv->len));
690
691 PrintAndLog("\n* Internal Authenticate");
692 int res = EMVInternalAuthenticate(true, (uint8_t *)ddol_data_tlv->value, ddol_data_tlv->len, buf, sizeof(buf), &len, &sw, NULL);
693 if (res) {
694 PrintAndLog("Internal Authenticate error(%d): %4x. Exit...", res, sw);
695 free(ddol_data_tlv);
696 emv_pk_free(pk);
697 emv_pk_free(issuer_pk);
698 emv_pk_free(icc_pk);
699 return 6;
700 }
701
702 struct tlvdb *dda_db = NULL;
703 if (buf[0] == 0x80) {
704 if (len < 3 ) {
705 PrintAndLog("ERROR: Internal Authenticate format1 parsing error. length=%d", len);
706 } else {
707 // 9f4b Signed Dynamic Application Data
708 dda_db = tlvdb_fixed(0x9f4b, len - 2, buf + 2);
709 tlvdb_add(tlv, dda_db);
710 if (decodeTLV){
711 PrintAndLog("* * Decode response format 1:");
712 TLVPrintFromTLV(dda_db);
713 }
714 }
715 } else {
716 dda_db = tlvdb_parse_multi(buf, len);
717 if(!dda_db) {
718 PrintAndLog("ERROR: Can't parse Internal Authenticate result as TLV");
719 free(ddol_data_tlv);
720 emv_pk_free(pk);
721 emv_pk_free(issuer_pk);
722 emv_pk_free(icc_pk);
723 return 7;
724 }
725 tlvdb_add(tlv, dda_db);
726
727 if (decodeTLV)
728 TLVPrintFromTLV(dda_db);
729 }
730
731 struct tlvdb *idn_db = emv_pki_recover_idn_ex(icc_pk, dda_db, ddol_data_tlv, true);
732 free(ddol_data_tlv);
733 if (!idn_db) {
734 PrintAndLog("ERROR: Can't recover IDN (ICC Dynamic Number)");
735 tlvdb_free(dda_db);
736 emv_pk_free(pk);
737 emv_pk_free(issuer_pk);
738 emv_pk_free(icc_pk);
739 return 8;
740 }
741 tlvdb_free(dda_db);
742
743 // 9f4c ICC Dynamic Number
744 const struct tlv *idn_tlv = tlvdb_get(idn_db, 0x9f4c, NULL);
745 if(idn_tlv) {
746 PrintAndLog("\nIDN (ICC Dynamic Number) [%zu] %s", idn_tlv->len, sprint_hex_inrow(idn_tlv->value, idn_tlv->len));
747 PrintAndLog("DDA verified OK.");
748 tlvdb_add(tlv, idn_db);
749 tlvdb_free(idn_db);
750 } else {
751 PrintAndLog("\nERROR: DDA verify error");
752 tlvdb_free(idn_db);
753
754 emv_pk_free(pk);
755 emv_pk_free(issuer_pk);
756 emv_pk_free(icc_pk);
757 return 9;
758 }
759 }
10d4f823 760
d03fb293
OM
761 emv_pk_free(pk);
762 emv_pk_free(issuer_pk);
763 emv_pk_free(icc_pk);
10d4f823 764 return 0;
765}
766
d03fb293
OM
767int trCDA(struct tlvdb *tlv, struct tlvdb *ac_tlv, struct tlv *pdol_data_tlv, struct tlv *ac_data_tlv) {
768
769 struct emv_pk *pk = get_ca_pk(tlv);
770 if (!pk) {
771 PrintAndLog("ERROR: Key not found. Exit.");
772 return 2;
773 }
774
775 const struct tlv *sda_tlv = tlvdb_get(tlv, 0x21, NULL);
776 if (!sda_tlv || sda_tlv->len < 1) {
777 PrintAndLog("ERROR: Can't find input list for Offline Data Authentication. Exit.");
778 emv_pk_free(pk);
779 return 3;
780 }
781
782 struct emv_pk *issuer_pk = emv_pki_recover_issuer_cert(pk, tlv);
783 if (!issuer_pk) {
784 PrintAndLog("ERROR: Issuer certificate not found. Exit.");
785 emv_pk_free(pk);
786 return 2;
787 }
788 printf("Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
789 issuer_pk->rid[0],
790 issuer_pk->rid[1],
791 issuer_pk->rid[2],
792 issuer_pk->rid[3],
793 issuer_pk->rid[4],
794 issuer_pk->index,
795 issuer_pk->serial[0],
796 issuer_pk->serial[1],
797 issuer_pk->serial[2]
798 );
799
800 struct emv_pk *icc_pk = emv_pki_recover_icc_cert(issuer_pk, tlv, sda_tlv);
801 if (!icc_pk) {
802 PrintAndLog("ERROR: ICC setrificate not found. Exit.");
803 emv_pk_free(pk);
804 emv_pk_free(issuer_pk);
805 return 2;
806 }
807 printf("ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
808 icc_pk->rid[0],
809 icc_pk->rid[1],
810 icc_pk->rid[2],
811 icc_pk->rid[3],
812 icc_pk->rid[4],
813 icc_pk->index,
814 icc_pk->serial[0],
815 icc_pk->serial[1],
816 icc_pk->serial[2]
817 );
818
819 struct tlvdb *dac_db = emv_pki_recover_dac(issuer_pk, tlv, sda_tlv);
820 if (dac_db) {
821 const struct tlv *dac_tlv = tlvdb_get(dac_db, 0x9f45, NULL);
822 PrintAndLog("SSAD verified OK. (%02hhx:%02hhx)", dac_tlv->value[0], dac_tlv->value[1]);
823 tlvdb_add(tlv, dac_db);
824 } else {
825 PrintAndLog("ERROR: SSAD verify error");
826 emv_pk_free(pk);
827 emv_pk_free(issuer_pk);
828 emv_pk_free(icc_pk);
829 return 4;
830 }
831
832 PrintAndLog("\n* * Check Signed Dynamic Application Data (SDAD)");
833 struct tlvdb *idn_db = emv_pki_perform_cda_ex(icc_pk, tlv, ac_tlv,
834 pdol_data_tlv, // pdol
835 ac_data_tlv, // cdol1
836 NULL, // cdol2
837 true);
838 if (idn_db) {
839 const struct tlv *idn_tlv = tlvdb_get(idn_db, 0x9f4c, NULL);
840 PrintAndLog("\nIDN (ICC Dynamic Number) [%zu] %s", idn_tlv->len, sprint_hex_inrow(idn_tlv->value, idn_tlv->len));
841 PrintAndLog("CDA verified OK.");
842 tlvdb_add(tlv, idn_db);
843 } else {
844 PrintAndLog("\nERROR: CDA verify error");
845 }
846
847 emv_pk_free(pk);
848 emv_pk_free(issuer_pk);
849 emv_pk_free(icc_pk);
850 return 0;
851}
Impressum, Datenschutz