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