]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/emv/emvcore.c
f9dd0cbe62546c550893f60dd478914f458a499f
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2017 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
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
20 //static const size_t PSElistLen = sizeof(PSElist)/sizeof(char*);
22 char *TransactionTypeStr
[] = {
30 enum CardPSVendor vendor
;
34 static const TAIDList AIDlist
[] = {
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
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
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" },
82 { CV_JCB
, "A00000006510" }, // JCB
83 { CV_JCB
, "A0000000651010" }, // JCB J Smart Credit
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
90 static const size_t AIDlistLen
= sizeof(AIDlist
)/sizeof(TAIDList
);
92 static bool APDULogging
= false;
93 void SetAPDULogging(bool logging
) {
94 APDULogging
= logging
;
97 enum CardPSVendor
GetCardPSVendor(uint8_t * AID
, size_t AIDlen
) {
102 hex_to_buffer((uint8_t *)buf
, AID
, AIDlen
, sizeof(buf
) - 1, 0, 0, true);
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
;
113 static bool print_cb(void *data
, const struct tlv
*tlv
, int level
, bool is_leaf
) {
114 emv_tag_dump(tlv
, stdout
, level
);
116 dump_buffer(tlv
->value
, tlv
->len
, stdout
, level
);
122 void TLVPrintFromBuffer(uint8_t *data
, int datalen
) {
123 struct tlvdb
*t
= NULL
;
124 t
= tlvdb_parse_multi(data
, datalen
);
126 PrintAndLog("-------------------- TLV decoded --------------------");
128 tlvdb_visit(t
, print_cb
, NULL
, 0);
131 PrintAndLog("TLV ERROR: Can't parse response as TLV tree.");
135 void TLVPrintFromTLVLev(struct tlvdb
*tlv
, int level
) {
139 tlvdb_visit(tlv
, print_cb
, NULL
, level
);
142 void TLVPrintFromTLV(struct tlvdb
*tlv
) {
143 TLVPrintFromTLVLev(tlv
, 0);
146 void TLVPrintAIDlistFromSelectTLV(struct tlvdb
*tlv
) {
147 PrintAndLog("|------------------|--------|-------------------------|");
148 PrintAndLog("| AID |Priority| Name |");
149 PrintAndLog("|------------------|--------|-------------------------|");
151 struct tlvdb
*ttmp
= tlvdb_find(tlv
, 0x6f);
153 PrintAndLog("| none |");
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
);
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) : " ");
166 ttmp
= tlvdb_find_next(ttmp
, 0x6f);
169 PrintAndLog("|------------------|--------|-------------------------|");
172 struct tlvdb
*GetPANFromTrack2(const struct tlv
*track2
) {
173 char track2Hex
[200] = {0};
174 uint8_t PAN
[100] = {0};
176 char *tmp
= track2Hex
;
181 for (int i
= 0; i
< track2
->len
; ++i
, tmp
+= 2)
182 sprintf(tmp
, "%02x", (unsigned int)track2
->value
[i
]);
184 int posD
= strchr(track2Hex
, 'd') - track2Hex
;
189 if (strlen(track2Hex
) % 2) {
190 track2Hex
[posD
] = 'F';
191 track2Hex
[posD
+ 1] = '\0';
194 param_gethex_to_eol(track2Hex
, 0, PAN
, sizeof(PAN
), &PANlen
);
196 return tlvdb_fixed(0x5a, PANlen
, PAN
);
199 struct tlvdb
*GetdCVVRawFromTrack2(const struct tlv
*track2
) {
200 char track2Hex
[200] = {0};
201 char dCVVHex
[100] = {0};
202 uint8_t dCVV
[100] = {0};
204 const int PINlen
= 5; // must calculated from 9F67 MSD Offset but i have not seen this tag)
205 char *tmp
= track2Hex
;
210 for (int i
= 0; i
< track2
->len
; ++i
, tmp
+= 2)
211 sprintf(tmp
, "%02x", (unsigned int)track2
->value
[i
]);
213 int posD
= strchr(track2Hex
, 'd') - track2Hex
;
217 memset(dCVVHex
, '0', 32);
219 memcpy(dCVVHex
+ 0, track2Hex
+ posD
+ PINlen
+ 11, 4);
221 memcpy(dCVVHex
+ 4, track2Hex
, 5);
223 memcpy(dCVVHex
+ 9, track2Hex
+ posD
+ 1, 4);
225 memcpy(dCVVHex
+ 13, track2Hex
+ posD
+ 5, 3);
227 param_gethex_to_eol(dCVVHex
, 0, dCVV
, sizeof(dCVV
), &dCVVlen
);
229 return tlvdb_fixed(0x02, dCVVlen
, dCVV
);
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};
243 memcpy(data
, &apdu
, 5);
245 memcpy(&data
[5], apdu
.data
, apdu
.Lc
);
248 PrintAndLog(">>>> %s", sprint_hex(data
, (IncludeLe
?6:5) + apdu
.Lc
));
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
);
258 PrintAndLog("<<<< %s", sprint_hex(Result
, *ResultLen
));
261 isw
= Result
[*ResultLen
] * 0x0100 + Result
[*ResultLen
+ 1];
267 PrintAndLog("APDU(%02x%02x) ERROR: [%4X] %s", apdu
.CLA
, apdu
.INS
, isw
, GetAPDUCodeDescription(*sw
>> 8, *sw
& 0xff));
273 struct tlvdb
*t
= tlvdb_parse_multi(Result
, *ResultLen
);
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
);
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
);
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};
295 param_gethex_to_eol(PSElist
[1], 0, buf
, sizeof(buf
), &len
);
298 param_gethex_to_eol(PSElist
[0], 0, buf
, sizeof(buf
), &len
);
305 res
= EMVSelect(ActivateField
, LeaveFieldON
, buf
, len
, Result
, MaxResultLen
, ResultLen
, sw
, NULL
);
310 int EMVSearchPSE(bool ActivateField
, bool LeaveFieldON
, bool decodeTLV
, struct tlvdb
*tlv
) {
311 uint8_t data
[APDU_RES_LEN
] = {0};
317 res
= EMVSelectPSE(ActivateField
, true, 2, data
, sizeof(data
), &datalen
, &sw
);
320 struct tlvdb
*t
= NULL
;
321 t
= tlvdb_parse_multi(data
, datalen
);
324 struct tlvdb
*ttmp
= tlvdb_find_path(t
, (tlv_tag_t
[]){0x6f, 0xa5, 0xbf0c, 0x61, 0x00});
326 PrintAndLog("PPSE don't have records.");
329 const struct tlv
*tgAID
= tlvdb_get_inchild(ttmp
, 0x4f, NULL
);
331 res
= EMVSelect(false, true, (uint8_t *)tgAID
->value
, tgAID
->len
, data
, sizeof(data
), &datalen
, &sw
, tlv
);
333 // retry if error and not returned sw error
334 if (res
&& res
!= 5) {
338 // card select error, proxmark error
340 PrintAndLog("Exit...");
345 PrintAndLog("Retry failed [%s]. Skiped...", sprint_hex_inrow(tgAID
->value
, tgAID
->len
));
349 ttmp
= tlvdb_find_next(ttmp
, 0x61);
356 PrintAndLog("%s:", sprint_hex_inrow(tgAID
->value
, tgAID
->len
));
357 TLVPrintFromBuffer(data
, datalen
);
361 ttmp
= tlvdb_find_next(ttmp
, 0x61);
366 PrintAndLog("PPSE ERROR: Can't get TLV from response.");
369 PrintAndLog("PPSE ERROR: Can't select PPSE AID. Error: %d", res
);
378 int EMVSearch(bool ActivateField
, bool LeaveFieldON
, bool decodeTLV
, struct tlvdb
*tlv
) {
379 uint8_t aidbuf
[APDU_AID_LEN
] = {0};
381 uint8_t data
[APDU_RES_LEN
] = {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) {
395 // card select error, proxmark error
397 PrintAndLog("Exit...");
402 PrintAndLog("Retry failed [%s]. Skiped...", AIDlist
[i
].aid
);
412 PrintAndLog("%s:", AIDlist
[i
].aid
);
413 TLVPrintFromBuffer(data
, datalen
);
420 int EMVSelectApplication(struct tlvdb
*tlv
, uint8_t *AID
, size_t *AIDlen
) {
421 // check priority. 0x00 - highest
426 struct tlvdb
*ttmp
= tlvdb_find(tlv
, 0x6f);
431 const struct tlv
*tgAID
= tlvdb_get_inchild(ttmp
, 0x84, NULL
);
432 const struct tlv
*tgPrio
= tlvdb_get_inchild(ttmp
, 0x87, NULL
);
438 int pt
= bytes_to_num((uint8_t*)tgPrio
->value
, (tgPrio
->len
< 2) ? tgPrio
->len
: 2);
442 memcpy(AID
, tgAID
->value
, tgAID
->len
);
443 *AIDlen
= tgAID
->len
;
446 // takes the first application from list wo priority
448 memcpy(AID
, tgAID
->value
, tgAID
->len
);
449 *AIDlen
= tgAID
->len
;
453 ttmp
= tlvdb_find_next(ttmp
, 0x6f);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
498 if (!df_tlv
|| !caidx_tlv
|| df_tlv
->len
< 6 || caidx_tlv
->len
!= 1)
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]);
505 int trSDA(struct tlvdb
*tlv
) {
507 struct emv_pk
*pk
= get_ca_pk(tlv
);
509 PrintAndLog("ERROR: Key not found. Exit.");
513 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlv
);
516 PrintAndLog("ERROR: Issuer certificate not found. Exit.");
520 PrintAndLog("Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx",
527 issuer_pk
->serial
[0],
528 issuer_pk
->serial
[1],
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
);
536 PrintAndLog("ERROR: Can't find input list for Offline Data Authentication. Exit.");
540 struct tlvdb
*dac_db
= emv_pki_recover_dac(issuer_pk
, tlv
, sda_tlv
);
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
);
546 emv_pk_free(issuer_pk
);
548 PrintAndLog("ERROR: SSAD verify error");
552 emv_pk_free(issuer_pk
);
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
};
560 int trDDA(bool decodeTLV
, struct tlvdb
*tlv
) {
561 uint8_t buf
[APDU_RES_LEN
] = {0};
565 struct emv_pk
*pk
= get_ca_pk(tlv
);
567 PrintAndLog("ERROR: Key not found. Exit.");
571 const struct tlv
*sda_tlv
= tlvdb_get(tlv
, 0x21, NULL
);
572 if (!sda_tlv
|| sda_tlv
->len
< 1) {
574 PrintAndLog("ERROR: Can't find input list for Offline Data Authentication. Exit.");
578 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlv
);
581 PrintAndLog("ERROR: Issuer certificate not found. Exit.");
584 printf("Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
591 issuer_pk
->serial
[0],
592 issuer_pk
->serial
[1],
596 struct emv_pk
*icc_pk
= emv_pki_recover_icc_cert(issuer_pk
, tlv
, sda_tlv
);
599 emv_pk_free(issuer_pk
);
600 PrintAndLog("ERROR: ICC setrificate not found. Exit.");
603 printf("ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
615 struct emv_pk
*icc_pe_pk
= emv_pki_recover_icc_pe_cert(issuer_pk
, tlv
);
617 PrintAndLog("WARNING: ICC PE PK recover error. ");
619 printf("ICC PE PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
626 icc_pe_pk
->serial
[0],
627 icc_pe_pk
->serial
[1],
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
637 PrintAndLog("\n* * Got Signed Dynamic Application Data (9F4B) form GPO. Maybe fDDA...");
639 const struct tlvdb
*atc_db
= emv_pki_recover_atc_ex(icc_pk
, tlv
, true);
641 PrintAndLog("ERROR: Can't recover IDN (ICC Dynamic Number)");
643 emv_pk_free(issuer_pk
);
648 // 9f36 Application Transaction Counter (ATC)
649 const struct tlv
*atc_tlv
= tlvdb_get(atc_db
, 0x9f36, NULL
);
651 PrintAndLog("\nATC (Application Transaction Counter) [%zu] %s", atc_tlv
->len
, sprint_hex_inrow(atc_tlv
->value
, atc_tlv
->len
));
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.");
658 PrintAndLog("ERROR: fDDA verified, but ATC in the certificate and ATC in the record not the same.");
661 PrintAndLog("\nERROR: fDDA (fast DDA) verify error");
663 emv_pk_free(issuer_pk
);
668 struct tlvdb
*dac_db
= emv_pki_recover_dac(issuer_pk
, tlv
, sda_tlv
);
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
);
674 PrintAndLog("ERROR: SSAD verify error");
676 emv_pk_free(issuer_pk
);
681 PrintAndLog("\n* Calc DDOL");
682 const struct tlv
*ddol_tlv
= tlvdb_get(tlv
, 0x9f49, NULL
);
684 ddol_tlv
= &default_ddol_tlv
;
685 PrintAndLog("DDOL [9f49] not found. Using default DDOL");
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");
692 emv_pk_free(issuer_pk
);
697 PrintAndLog("DDOL data[%d]: %s", ddol_data_tlv
->len
, sprint_hex(ddol_data_tlv
->value
, ddol_data_tlv
->len
));
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
);
702 PrintAndLog("Internal Authenticate error(%d): %4x. Exit...", res
, sw
);
705 emv_pk_free(issuer_pk
);
710 struct tlvdb
*dda_db
= NULL
;
711 if (buf
[0] == 0x80) {
713 PrintAndLog("ERROR: Internal Authenticate format1 parsing error. length=%d", len
);
715 // 9f4b Signed Dynamic Application Data
716 dda_db
= tlvdb_fixed(0x9f4b, len
- 2, buf
+ 2);
717 tlvdb_add(tlv
, dda_db
);
719 PrintAndLog("* * Decode response format 1:");
720 TLVPrintFromTLV(dda_db
);
724 dda_db
= tlvdb_parse_multi(buf
, len
);
726 PrintAndLog("ERROR: Can't parse Internal Authenticate result as TLV");
729 emv_pk_free(issuer_pk
);
733 tlvdb_add(tlv
, dda_db
);
736 TLVPrintFromTLV(dda_db
);
739 struct tlvdb
*idn_db
= emv_pki_recover_idn_ex(icc_pk
, dda_db
, ddol_data_tlv
, true);
742 PrintAndLog("ERROR: Can't recover IDN (ICC Dynamic Number)");
745 emv_pk_free(issuer_pk
);
751 // 9f4c ICC Dynamic Number
752 const struct tlv
*idn_tlv
= tlvdb_get(idn_db
, 0x9f4c, NULL
);
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
);
759 PrintAndLog("\nERROR: DDA verify error");
763 emv_pk_free(issuer_pk
);
770 emv_pk_free(issuer_pk
);
775 int trCDA(struct tlvdb
*tlv
, struct tlvdb
*ac_tlv
, struct tlv
*pdol_data_tlv
, struct tlv
*ac_data_tlv
) {
777 struct emv_pk
*pk
= get_ca_pk(tlv
);
779 PrintAndLog("ERROR: Key not found. Exit.");
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.");
790 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlv
);
792 PrintAndLog("ERROR: Issuer certificate not found. Exit.");
796 printf("Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
803 issuer_pk
->serial
[0],
804 issuer_pk
->serial
[1],
808 struct emv_pk
*icc_pk
= emv_pki_recover_icc_cert(issuer_pk
, tlv
, sda_tlv
);
810 PrintAndLog("ERROR: ICC setrificate not found. Exit.");
812 emv_pk_free(issuer_pk
);
815 printf("ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
827 struct tlvdb
*dac_db
= emv_pki_recover_dac(issuer_pk
, tlv
, sda_tlv
);
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
);
833 PrintAndLog("ERROR: SSAD verify error");
835 emv_pk_free(issuer_pk
);
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
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
);
852 PrintAndLog("\nERROR: CDA verify error");
856 emv_pk_free(issuer_pk
);
861 int RecoveryCertificates(struct tlvdb
*tlvRoot
, json_t
*root
) {
863 struct emv_pk
*pk
= get_ca_pk(tlvRoot
);
865 PrintAndLog("ERROR: Key not found. Exit.");
869 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlvRoot
);
872 PrintAndLog("WARNING: Issuer certificate not found. Exit.");
875 PrintAndLog("Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx",
882 issuer_pk
->serial
[0],
883 issuer_pk
->serial
[1],
887 JsonSaveBufAsHex(root
, "$.ApplicationData.RID", issuer_pk
->rid
, 5);
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
);
894 struct emv_pk
*icc_pk
= emv_pki_recover_icc_cert(issuer_pk
, tlvRoot
, NULL
);
897 emv_pk_free(issuer_pk
);
898 PrintAndLog("WARNING: ICC certificate not found. Exit.");
901 printf("ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
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
);