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 //-----------------------------------------------------------------------------
13 #include "util_posix.h"
15 #include "cmdsmartcard.h"
18 // Got from here. Thanks)
19 // https://eftlab.co.uk/index.php/site-map/knowledge-base/211-emv-aid-rid-pix
20 static const char *PSElist
[] = {
21 "325041592E5359532E4444463031", // 2PAY.SYS.DDF01 - Visa Proximity Payment System Environment - PPSE
22 "315041592E5359532E4444463031" // 1PAY.SYS.DDF01 - Visa Payment System Environment - PSE
24 //static const size_t PSElistLen = sizeof(PSElist)/sizeof(char*);
26 char *TransactionTypeStr
[] = {
34 enum CardPSVendor vendor
;
38 static const TAIDList AIDlist
[] = {
40 { CV_VISA
, "A00000000305076010"}, // VISA ELO Credit
41 { CV_VISA
, "A0000000031010" }, // VISA Debit/Credit (Classic)
42 { CV_VISA
, "A000000003101001" }, // VISA Credit
43 { CV_VISA
, "A000000003101002" }, // VISA Debit
44 { CV_VISA
, "A0000000032010" }, // VISA Electron
45 { CV_VISA
, "A0000000032020" }, // VISA
46 { CV_VISA
, "A0000000033010" }, // VISA Interlink
47 { CV_VISA
, "A0000000034010" }, // VISA Specific
48 { CV_VISA
, "A0000000035010" }, // VISA Specific
49 { CV_VISA
, "A0000000036010" }, // Domestic Visa Cash Stored Value
50 { CV_VISA
, "A0000000036020" }, // International Visa Cash Stored Value
51 { CV_VISA
, "A0000000038002" }, // VISA Auth, VisaRemAuthen EMV-CAP (DPA)
52 { CV_VISA
, "A0000000038010" }, // VISA Plus
53 { CV_VISA
, "A0000000039010" }, // VISA Loyalty
54 { CV_VISA
, "A000000003999910" }, // VISA Proprietary ATM
56 { CV_VISA
, "A000000098" }, // Debit Card
57 { CV_VISA
, "A0000000980848" }, // Debit Card
58 // Mastercard International
59 { CV_MASTERCARD
, "A00000000401" }, // MasterCard PayPass
60 { CV_MASTERCARD
, "A0000000041010" }, // MasterCard Credit
61 { CV_MASTERCARD
, "A00000000410101213" }, // MasterCard Credit
62 { CV_MASTERCARD
, "A00000000410101215" }, // MasterCard Credit
63 { CV_MASTERCARD
, "A0000000042010" }, // MasterCard Specific
64 { CV_MASTERCARD
, "A0000000043010" }, // MasterCard Specific
65 { CV_MASTERCARD
, "A0000000043060" }, // Maestro (Debit)
66 { CV_MASTERCARD
, "A000000004306001" }, // Maestro (Debit)
67 { CV_MASTERCARD
, "A0000000044010" }, // MasterCard Specific
68 { CV_MASTERCARD
, "A0000000045010" }, // MasterCard Specific
69 { CV_MASTERCARD
, "A0000000046000" }, // Cirrus
70 { CV_MASTERCARD
, "A0000000048002" }, // SecureCode Auth EMV-CAP
71 { CV_MASTERCARD
, "A0000000049999" }, // MasterCard PayPass
73 { CV_AMERICANEXPRESS
, "A000000025" },
74 { CV_AMERICANEXPRESS
, "A0000000250000" },
75 { CV_AMERICANEXPRESS
, "A00000002501" },
76 { CV_AMERICANEXPRESS
, "A000000025010402" },
77 { CV_AMERICANEXPRESS
, "A000000025010701" },
78 { CV_AMERICANEXPRESS
, "A000000025010801" },
79 // Groupement des Cartes Bancaires "CB"
80 { CV_CB
, "A0000000421010" }, // Cartes Bancaire EMV Card
81 { CV_CB
, "A0000000422010" },
82 { CV_CB
, "A0000000423010" },
83 { CV_CB
, "A0000000424010" },
84 { CV_CB
, "A0000000425010" },
86 { CV_JCB
, "A00000006510" }, // JCB
87 { CV_JCB
, "A0000000651010" }, // JCB J Smart Credit
89 { CV_OTHER
, "A0000001544442" }, // Banricompras Debito - Banrisul - Banco do Estado do Rio Grande do SUL - S.A.
90 { CV_OTHER
, "F0000000030001" }, // BRADESCO
91 { CV_OTHER
, "A0000005241010" }, // RuPay - RuPay
92 { CV_OTHER
, "D5780000021010" } // Bankaxept - Bankaxept
94 static const size_t AIDlistLen
= sizeof(AIDlist
)/sizeof(TAIDList
);
96 static bool APDULogging
= false;
97 void SetAPDULogging(bool logging
) {
98 APDULogging
= logging
;
101 enum CardPSVendor
GetCardPSVendor(uint8_t * AID
, size_t AIDlen
) {
106 hex_to_buffer((uint8_t *)buf
, AID
, AIDlen
, sizeof(buf
) - 1, 0, 0, true);
108 for(int i
= 0; i
< AIDlistLen
; i
++) {
109 if (strncmp(AIDlist
[i
].aid
, buf
, strlen(AIDlist
[i
].aid
)) == 0){
110 return AIDlist
[i
].vendor
;
117 static bool print_cb(void *data
, const struct tlv
*tlv
, int level
, bool is_leaf
) {
118 emv_tag_dump(tlv
, stdout
, level
);
120 dump_buffer(tlv
->value
, tlv
->len
, stdout
, level
);
126 bool TLVPrintFromBuffer(uint8_t *data
, int datalen
) {
127 struct tlvdb
*t
= NULL
;
128 t
= tlvdb_parse_multi(data
, datalen
);
130 PrintAndLogEx(NORMAL
, "-------------------- TLV decoded --------------------");
132 tlvdb_visit(t
, print_cb
, NULL
, 0);
136 PrintAndLogEx(WARNING
, "TLV ERROR: Can't parse response as TLV tree.");
141 void TLVPrintFromTLVLev(struct tlvdb
*tlv
, int level
) {
145 tlvdb_visit(tlv
, print_cb
, NULL
, level
);
148 void TLVPrintFromTLV(struct tlvdb
*tlv
) {
149 TLVPrintFromTLVLev(tlv
, 0);
152 void TLVPrintAIDlistFromSelectTLV(struct tlvdb
*tlv
) {
153 PrintAndLogEx(NORMAL
, "|------------------|--------|-------------------------|");
154 PrintAndLogEx(NORMAL
, "| AID |Priority| Name |");
155 PrintAndLogEx(NORMAL
, "|------------------|--------|-------------------------|");
157 struct tlvdb
*ttmp
= tlvdb_find(tlv
, 0x6f);
159 PrintAndLogEx(NORMAL
, "| none |");
162 const struct tlv
*tgAID
= tlvdb_get_inchild(ttmp
, 0x84, NULL
);
163 const struct tlv
*tgName
= tlvdb_get_inchild(ttmp
, 0x50, NULL
);
164 const struct tlv
*tgPrio
= tlvdb_get_inchild(ttmp
, 0x87, NULL
);
167 PrintAndLogEx(NORMAL
, "|%s| %s |%s|",
168 sprint_hex_inrow_ex(tgAID
->value
, tgAID
->len
, 18),
169 (tgPrio
) ? sprint_hex(tgPrio
->value
, 1) : " ",
170 (tgName
) ? sprint_ascii_ex(tgName
->value
, tgName
->len
, 25) : " ");
172 ttmp
= tlvdb_find_next(ttmp
, 0x6f);
175 PrintAndLogEx(NORMAL
, "|------------------|--------|-------------------------|");
178 struct tlvdb
*GetPANFromTrack2(const struct tlv
*track2
) {
179 char track2Hex
[200] = {0};
180 uint8_t PAN
[100] = {0};
182 char *tmp
= track2Hex
;
187 for (int i
= 0; i
< track2
->len
; ++i
, tmp
+= 2)
188 sprintf(tmp
, "%02x", (unsigned int)track2
->value
[i
]);
190 int posD
= strchr(track2Hex
, 'd') - track2Hex
;
195 if (strlen(track2Hex
) % 2) {
196 track2Hex
[posD
] = 'F';
197 track2Hex
[posD
+ 1] = '\0';
200 param_gethex_to_eol(track2Hex
, 0, PAN
, sizeof(PAN
), &PANlen
);
202 return tlvdb_fixed(0x5a, PANlen
, PAN
);
205 struct tlvdb
*GetdCVVRawFromTrack2(const struct tlv
*track2
) {
206 char track2Hex
[200] = {0};
207 char dCVVHex
[100] = {0};
208 uint8_t dCVV
[100] = {0};
210 const int PINlen
= 5; // must calculated from 9F67 MSD Offset but i have not seen this tag)
211 char *tmp
= track2Hex
;
216 for (int i
= 0; i
< track2
->len
; ++i
, tmp
+= 2)
217 sprintf(tmp
, "%02x", (unsigned int)track2
->value
[i
]);
219 int posD
= strchr(track2Hex
, 'd') - track2Hex
;
223 memset(dCVVHex
, '0', 32);
225 memcpy(dCVVHex
+ 0, track2Hex
+ posD
+ PINlen
+ 11, 4);
227 memcpy(dCVVHex
+ 4, track2Hex
, 5);
229 memcpy(dCVVHex
+ 9, track2Hex
+ posD
+ 1, 4);
231 memcpy(dCVVHex
+ 13, track2Hex
+ posD
+ 5, 3);
233 param_gethex_to_eol(dCVVHex
, 0, dCVV
, sizeof(dCVV
), &dCVVlen
);
235 return tlvdb_fixed(0x02, dCVVlen
, dCVV
);
238 int EMVExchangeEx(EMVCommandChannel channel
, bool ActivateField
, bool LeaveFieldON
, sAPDU apdu
, bool IncludeLe
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
, struct tlvdb
*tlv
) {
239 uint8_t data
[APDU_RES_LEN
] = {0};
252 memcpy(data
, &apdu
, 5);
254 memcpy(&data
[5], apdu
.data
, apdu
.Lc
);
257 PrintAndLogEx(SUCCESS
, ">>>> %s", sprint_hex(data
, (IncludeLe
?6:5) + apdu
.Lc
));
260 case ECC_CONTACTLESS
:
261 // 6 byes + data = INS + CLA + P1 + P2 + Lc + <data = Nc> + Le(?IncludeLe)
262 res
= ExchangeAPDU14a(data
, (IncludeLe
?6:5) + apdu
.Lc
, ActivateField
, LeaveFieldON
, Result
, (int)MaxResultLen
, (int *)ResultLen
);
268 //int ExchangeAPDUSC(uint8_t *datain, int datainlen, bool activateCard, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen);
269 #ifdef WITH_SMARTCARD
270 res
= ExchangeAPDUSC(data
, (IncludeLe
?6:5) + apdu
.Lc
, ActivateField
, LeaveFieldON
, Result
, (int)MaxResultLen
, (int *)ResultLen
);
279 PrintAndLogEx(SUCCESS
, "<<<< %s", sprint_hex(Result
, *ResultLen
));
281 if (*ResultLen
< 2) {
286 isw
= Result
[*ResultLen
] * 0x0100 + Result
[*ResultLen
+ 1];
292 if (*sw
>> 8 == 0x61) {
293 PrintAndLogEx(ERR
, "APDU chaining len:%02x -->", *sw
& 0xff);
295 PrintAndLogEx(ERR
, "APDU(%02x%02x) ERROR: [%4X] %s", apdu
.CLA
, apdu
.INS
, isw
, GetAPDUCodeDescription(*sw
>> 8, *sw
& 0xff));
303 struct tlvdb
*t
= tlvdb_parse_multi(Result
, *ResultLen
);
310 int EMVExchange(EMVCommandChannel channel
, bool LeaveFieldON
, sAPDU apdu
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
, struct tlvdb
*tlv
) {
311 return EMVExchangeEx(channel
, false, LeaveFieldON
, apdu
, (channel
== ECC_CONTACTLESS
), Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
314 int 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
) {
315 return EMVExchangeEx(channel
, ActivateField
, LeaveFieldON
, (sAPDU
){0x00, 0xa4, 0x04, 0x00, AIDLen
, AID
}, (channel
== ECC_CONTACTLESS
), Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
318 int EMVSelectPSE(EMVCommandChannel channel
, bool ActivateField
, bool LeaveFieldON
, uint8_t PSENum
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
) {
319 uint8_t buf
[APDU_AID_LEN
] = {0};
325 param_gethex_to_eol(PSElist
[1], 0, buf
, sizeof(buf
), &len
);
328 param_gethex_to_eol(PSElist
[0], 0, buf
, sizeof(buf
), &len
);
335 res
= EMVSelect(channel
, ActivateField
, LeaveFieldON
, buf
, len
, Result
, MaxResultLen
, ResultLen
, sw
, NULL
);
340 int EMVSearchPSE(EMVCommandChannel channel
, bool ActivateField
, bool LeaveFieldON
, uint8_t PSENum
, bool decodeTLV
, struct tlvdb
*tlv
) {
341 uint8_t data
[APDU_RES_LEN
] = {0};
347 res
= EMVSelectPSE(channel
, ActivateField
, true, PSENum
, data
, sizeof(data
), &datalen
, &sw
);
350 struct tlvdb
*t
= NULL
;
351 t
= tlvdb_parse_multi(data
, datalen
);
354 struct tlvdb
*ttmp
= tlvdb_find_path(t
, (tlv_tag_t
[]){0x6f, 0xa5, 0xbf0c, 0x61, 0x00});
356 PrintAndLogEx(FAILED
, "PPSE don't have records.");
359 const struct tlv
*tgAID
= tlvdb_get_inchild(ttmp
, 0x4f, NULL
);
361 res
= EMVSelect(channel
, false, true, (uint8_t *)tgAID
->value
, tgAID
->len
, data
, sizeof(data
), &datalen
, &sw
, tlv
);
363 // retry if error and not returned sw error
364 if (res
&& res
!= 5) {
368 // card select error, proxmark error
370 PrintAndLogEx(WARNING
, "Exit...");
375 PrintAndLogEx(NORMAL
, "Retry failed [%s]. Skiped...", sprint_hex_inrow(tgAID
->value
, tgAID
->len
));
379 ttmp
= tlvdb_find_next(ttmp
, 0x61);
386 PrintAndLogEx(NORMAL
, "%s:", sprint_hex_inrow(tgAID
->value
, tgAID
->len
));
387 TLVPrintFromBuffer(data
, datalen
);
391 ttmp
= tlvdb_find_next(ttmp
, 0x61);
396 PrintAndLogEx(WARNING
, "PPSE ERROR: Can't get TLV from response.");
399 PrintAndLogEx(WARNING
, "PPSE ERROR: Can't select PPSE AID. Error: %d", res
);
408 int EMVSearch(EMVCommandChannel channel
, bool ActivateField
, bool LeaveFieldON
, bool decodeTLV
, struct tlvdb
*tlv
) {
409 uint8_t aidbuf
[APDU_AID_LEN
] = {0};
411 uint8_t data
[APDU_RES_LEN
] = {0};
417 for(int i
= 0; i
< AIDlistLen
; i
++) {
418 param_gethex_to_eol(AIDlist
[i
].aid
, 0, aidbuf
, sizeof(aidbuf
), &aidlen
);
419 res
= EMVSelect(channel
, (i
== 0) ? ActivateField
: false, (i
== AIDlistLen
- 1) ? LeaveFieldON
: true, aidbuf
, aidlen
, data
, sizeof(data
), &datalen
, &sw
, tlv
);
420 // retry if error and not returned sw error
421 if (res
&& res
!= 5) {
425 // (1) - card select error, proxmark error OR (200) - result length = 0
426 if (res
== 1 || res
== 200) {
427 PrintAndLogEx(WARNING
, "Exit...");
432 PrintAndLogEx(FAILED
, "Retry failed [%s]. Skipped...", AIDlist
[i
].aid
);
445 PrintAndLogEx(SUCCESS
, "%s", AIDlist
[i
].aid
);
446 TLVPrintFromBuffer(data
, datalen
);
453 int EMVSelectApplication(struct tlvdb
*tlv
, uint8_t *AID
, size_t *AIDlen
) {
454 // check priority. 0x00 - highest
459 struct tlvdb
*ttmp
= tlvdb_find(tlv
, 0x6f);
464 const struct tlv
*tgAID
= tlvdb_get_inchild(ttmp
, 0x84, NULL
);
465 const struct tlv
*tgPrio
= tlvdb_get_inchild(ttmp
, 0x87, NULL
);
471 int pt
= bytes_to_num((uint8_t*)tgPrio
->value
, (tgPrio
->len
< 2) ? tgPrio
->len
: 2);
475 memcpy(AID
, tgAID
->value
, tgAID
->len
);
476 *AIDlen
= tgAID
->len
;
479 // takes the first application from list wo priority
481 memcpy(AID
, tgAID
->value
, tgAID
->len
);
482 *AIDlen
= tgAID
->len
;
486 ttmp
= tlvdb_find_next(ttmp
, 0x6f);
492 int 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
) {
493 return EMVExchange(channel
, LeaveFieldON
, (sAPDU
){0x80, 0xa8, 0x00, 0x00, PDOLLen
, PDOL
}, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
496 int 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
) {
497 int res
= EMVExchange(channel
, LeaveFieldON
, (sAPDU
){0x00, 0xb2, SFIrec
, (SFI
<< 3) | 0x04, 0, NULL
}, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
499 PrintAndLogEx(INFO
, ">>> trying to reissue command withouth Le...");
500 res
= EMVExchangeEx(channel
, false, LeaveFieldON
, (sAPDU
){0x00, 0xb2, SFIrec
, (SFI
<< 3) | 0x04, 0, NULL
}, false, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
505 int 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
) {
506 return EMVExchange(channel
, LeaveFieldON
, (sAPDU
){0x80, 0xae, RefControl
, 0x00, CDOLLen
, CDOL
}, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
509 int EMVGenerateChallenge(EMVCommandChannel channel
, bool LeaveFieldON
, uint8_t *Result
, size_t MaxResultLen
, size_t *ResultLen
, uint16_t *sw
, struct tlvdb
*tlv
) {
510 int res
= EMVExchange(channel
, LeaveFieldON
, (sAPDU
){0x00, 0x84, 0x00, 0x00, 0x00, NULL
}, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
512 PrintAndLogEx(INFO
, ">>> trying to reissue command withouth Le...");
513 res
= EMVExchangeEx(channel
, false, LeaveFieldON
, (sAPDU
){0x00, 0x84, 0x00, 0x00, 0x00, NULL
}, false, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
518 int 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
) {
519 return EMVExchange(channel
, LeaveFieldON
, (sAPDU
){0x00, 0x88, 0x00, 0x00, DDOLLen
, DDOL
}, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
522 int 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
) {
523 return EMVExchange(channel
, LeaveFieldON
, (sAPDU
){0x80, 0x2a, 0x8e, 0x80, UDOLlen
, UDOL
}, Result
, MaxResultLen
, ResultLen
, sw
, tlv
);
527 struct emv_pk
*get_ca_pk(struct tlvdb
*db
) {
528 const struct tlv
*df_tlv
= tlvdb_get(db
, 0x84, NULL
);
529 const struct tlv
*caidx_tlv
= tlvdb_get(db
, 0x8f, NULL
);
531 if (!df_tlv
|| !caidx_tlv
|| df_tlv
->len
< 6 || caidx_tlv
->len
!= 1)
534 PrintAndLogEx(NORMAL
, "CA public key index 0x%0x", caidx_tlv
->value
[0]);
535 return emv_pk_get_ca_pk(df_tlv
->value
, caidx_tlv
->value
[0]);
538 int trSDA(struct tlvdb
*tlv
) {
540 struct emv_pk
*pk
= get_ca_pk(tlv
);
542 PrintAndLogEx(WARNING
, "Error: Key not found. Exit.");
546 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlv
);
549 PrintAndLogEx(WARNING
, "Error: Issuer certificate not found. Exit.");
553 PrintAndLogEx(SUCCESS
, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx",
560 issuer_pk
->serial
[0],
561 issuer_pk
->serial
[1],
565 const struct tlv
*sda_tlv
= tlvdb_get(tlv
, 0x21, NULL
);
566 if (!sda_tlv
|| sda_tlv
->len
< 1) {
567 emv_pk_free(issuer_pk
);
569 PrintAndLogEx(WARNING
, "Can't find input list for Offline Data Authentication. Exit.");
573 struct tlvdb
*dac_db
= emv_pki_recover_dac(issuer_pk
, tlv
, sda_tlv
);
575 const struct tlv
*dac_tlv
= tlvdb_get(dac_db
, 0x9f45, NULL
);
576 PrintAndLogEx(NORMAL
, "SDA verified OK. (%02hhx:%02hhx)\n", dac_tlv
->value
[0], dac_tlv
->value
[1]);
577 tlvdb_add(tlv
, dac_db
);
579 emv_pk_free(issuer_pk
);
581 PrintAndLogEx(WARNING
, "SSAD verify error");
585 emv_pk_free(issuer_pk
);
590 static const unsigned char default_ddol_value
[] = {0x9f, 0x37, 0x04};
591 static struct tlv default_ddol_tlv
= {.tag
= 0x9f49, .len
= 3, .value
= default_ddol_value
};
593 int trDDA(EMVCommandChannel channel
, bool decodeTLV
, struct tlvdb
*tlv
) {
594 uint8_t buf
[APDU_RES_LEN
] = {0};
598 struct emv_pk
*pk
= get_ca_pk(tlv
);
600 PrintAndLogEx(WARNING
, "Error: Key not found. Exit.");
604 const struct tlv
*sda_tlv
= tlvdb_get(tlv
, 0x21, NULL
);
605 if (!sda_tlv
|| sda_tlv
->len
< 1) {
607 PrintAndLogEx(WARNING
, "Error: Can't find input list for Offline Data Authentication. Exit.");
611 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlv
);
614 PrintAndLogEx(WARNING
, "Error: Issuer certificate not found. Exit.");
617 PrintAndLogEx(SUCCESS
, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
624 issuer_pk
->serial
[0],
625 issuer_pk
->serial
[1],
629 struct emv_pk
*icc_pk
= emv_pki_recover_icc_cert(issuer_pk
, tlv
, sda_tlv
);
632 emv_pk_free(issuer_pk
);
633 PrintAndLogEx(WARNING
, "Error: ICC setrificate not found. Exit.");
636 PrintAndLogEx(SUCCESS
, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
648 struct emv_pk
*icc_pe_pk
= emv_pki_recover_icc_pe_cert(issuer_pk
, tlv
);
650 PrintAndLogEx(WARNING
, "WARNING: ICC PE PK recover error. ");
652 PrintAndLogEx(SUCCESS
, "ICC PE PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
659 icc_pe_pk
->serial
[0],
660 icc_pe_pk
->serial
[1],
665 // 9F4B: Signed Dynamic Application Data
666 const struct tlv
*sdad_tlv
= tlvdb_get(tlv
, 0x9f4b, NULL
);
667 // DDA with internal authenticate OR fDDA with filled 0x9F4B tag (GPO result)
668 // EMV kernel3 v2.4, contactless book C-3, C.1., page 147
670 PrintAndLogEx(NORMAL
, "\n* * Got Signed Dynamic Application Data (9F4B) form GPO. Maybe fDDA...");
672 const struct tlvdb
*atc_db
= emv_pki_recover_atc_ex(icc_pk
, tlv
, true);
674 PrintAndLogEx(WARNING
, "Error: Can't recover IDN (ICC Dynamic Number)");
676 emv_pk_free(issuer_pk
);
681 // 9f36 Application Transaction Counter (ATC)
682 const struct tlv
*atc_tlv
= tlvdb_get(atc_db
, 0x9f36, NULL
);
684 PrintAndLogEx(NORMAL
, "\nATC (Application Transaction Counter) [%zu] %s", atc_tlv
->len
, sprint_hex_inrow(atc_tlv
->value
, atc_tlv
->len
));
686 const struct tlv
*core_atc_tlv
= tlvdb_get(tlv
, 0x9f36, NULL
);
687 if(tlv_equal(core_atc_tlv
, atc_tlv
)) {
688 PrintAndLogEx(SUCCESS
, "ATC check OK.");
689 PrintAndLogEx(SUCCESS
, "fDDA (fast DDA) verified OK.");
691 PrintAndLogEx(WARNING
, "Error: fDDA verified, but ATC in the certificate and ATC in the record not the same.");
694 PrintAndLogEx(NORMAL
, "\nERROR: fDDA (fast DDA) verify error");
696 emv_pk_free(issuer_pk
);
701 struct tlvdb
*dac_db
= emv_pki_recover_dac(issuer_pk
, tlv
, sda_tlv
);
703 const struct tlv
*dac_tlv
= tlvdb_get(dac_db
, 0x9f45, NULL
);
704 PrintAndLogEx(NORMAL
, "SDA verified OK. (%02hhx:%02hhx)\n", dac_tlv
->value
[0], dac_tlv
->value
[1]);
705 tlvdb_add(tlv
, dac_db
);
707 PrintAndLogEx(WARNING
, "Error: SSAD verify error");
709 emv_pk_free(issuer_pk
);
714 PrintAndLogEx(NORMAL
, "\n* Calc DDOL");
715 const struct tlv
*ddol_tlv
= tlvdb_get(tlv
, 0x9f49, NULL
);
717 ddol_tlv
= &default_ddol_tlv
;
718 PrintAndLogEx(NORMAL
, "DDOL [9f49] not found. Using default DDOL");
721 struct tlv
*ddol_data_tlv
= dol_process(ddol_tlv
, tlv
, 0);
722 if (!ddol_data_tlv
) {
723 PrintAndLogEx(WARNING
, "Error: Can't create DDOL TLV");
725 emv_pk_free(issuer_pk
);
730 PrintAndLogEx(NORMAL
, "DDOL data[%d]: %s", ddol_data_tlv
->len
, sprint_hex(ddol_data_tlv
->value
, ddol_data_tlv
->len
));
732 PrintAndLogEx(NORMAL
, "\n* Internal Authenticate");
733 int res
= EMVInternalAuthenticate(channel
, true, (uint8_t *)ddol_data_tlv
->value
, ddol_data_tlv
->len
, buf
, sizeof(buf
), &len
, &sw
, NULL
);
735 PrintAndLogEx(WARNING
, "Internal Authenticate error(%d): %4x. Exit...", res
, sw
);
738 emv_pk_free(issuer_pk
);
743 struct tlvdb
*dda_db
= NULL
;
744 if (buf
[0] == 0x80) {
746 PrintAndLogEx(WARNING
, "Error: Internal Authenticate format1 parsing error. length=%d", len
);
748 // 9f4b Signed Dynamic Application Data
749 dda_db
= tlvdb_fixed(0x9f4b, len
- 2, buf
+ 2);
750 tlvdb_add(tlv
, dda_db
);
752 PrintAndLogEx(NORMAL
, "* * Decode response format 1:");
753 TLVPrintFromTLV(dda_db
);
757 dda_db
= tlvdb_parse_multi(buf
, len
);
759 PrintAndLogEx(WARNING
, "Error: Can't parse Internal Authenticate result as TLV");
762 emv_pk_free(issuer_pk
);
766 tlvdb_add(tlv
, dda_db
);
769 TLVPrintFromTLV(dda_db
);
772 struct tlvdb
*idn_db
= emv_pki_recover_idn_ex(icc_pk
, dda_db
, ddol_data_tlv
, true);
775 PrintAndLogEx(WARNING
, "Error: Can't recover IDN (ICC Dynamic Number)");
778 emv_pk_free(issuer_pk
);
784 // 9f4c ICC Dynamic Number
785 const struct tlv
*idn_tlv
= tlvdb_get(idn_db
, 0x9f4c, NULL
);
787 PrintAndLogEx(NORMAL
, "\nIDN (ICC Dynamic Number) [%zu] %s", idn_tlv
->len
, sprint_hex_inrow(idn_tlv
->value
, idn_tlv
->len
));
788 PrintAndLogEx(NORMAL
, "DDA verified OK.");
789 tlvdb_add(tlv
, idn_db
);
792 PrintAndLogEx(NORMAL
, "\nERROR: DDA verify error");
796 emv_pk_free(issuer_pk
);
803 emv_pk_free(issuer_pk
);
808 int trCDA(struct tlvdb
*tlv
, struct tlvdb
*ac_tlv
, struct tlv
*pdol_data_tlv
, struct tlv
*ac_data_tlv
) {
810 struct emv_pk
*pk
= get_ca_pk(tlv
);
812 PrintAndLogEx(WARNING
, "Error: Key not found. Exit.");
816 const struct tlv
*sda_tlv
= tlvdb_get(tlv
, 0x21, NULL
);
817 if (!sda_tlv
|| sda_tlv
->len
< 1) {
818 PrintAndLogEx(WARNING
, "Error: Can't find input list for Offline Data Authentication. Exit.");
823 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlv
);
825 PrintAndLogEx(WARNING
, "Error: Issuer certificate not found. Exit.");
829 PrintAndLogEx(SUCCESS
, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
836 issuer_pk
->serial
[0],
837 issuer_pk
->serial
[1],
841 struct emv_pk
*icc_pk
= emv_pki_recover_icc_cert(issuer_pk
, tlv
, sda_tlv
);
843 PrintAndLogEx(WARNING
, "Error: ICC setrificate not found. Exit.");
845 emv_pk_free(issuer_pk
);
848 PrintAndLogEx(SUCCESS
, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
860 struct tlvdb
*dac_db
= emv_pki_recover_dac(issuer_pk
, tlv
, sda_tlv
);
862 const struct tlv
*dac_tlv
= tlvdb_get(dac_db
, 0x9f45, NULL
);
863 PrintAndLogEx(NORMAL
, "SSAD verified OK. (%02hhx:%02hhx)", dac_tlv
->value
[0], dac_tlv
->value
[1]);
864 tlvdb_add(tlv
, dac_db
);
866 PrintAndLogEx(WARNING
, "Error: SSAD verify error");
868 emv_pk_free(issuer_pk
);
873 PrintAndLogEx(NORMAL
, "\n* * Check Signed Dynamic Application Data (SDAD)");
874 struct tlvdb
*idn_db
= emv_pki_perform_cda_ex(icc_pk
, tlv
, ac_tlv
,
875 pdol_data_tlv
, // pdol
876 ac_data_tlv
, // cdol1
880 const struct tlv
*idn_tlv
= tlvdb_get(idn_db
, 0x9f4c, NULL
);
881 PrintAndLogEx(NORMAL
, "\nIDN (ICC Dynamic Number) [%zu] %s", idn_tlv
->len
, sprint_hex_inrow(idn_tlv
->value
, idn_tlv
->len
));
882 PrintAndLogEx(NORMAL
, "CDA verified OK.");
883 tlvdb_add(tlv
, idn_db
);
885 PrintAndLogEx(NORMAL
, "\nERROR: CDA verify error");
889 emv_pk_free(issuer_pk
);
894 int RecoveryCertificates(struct tlvdb
*tlvRoot
, json_t
*root
) {
896 struct emv_pk
*pk
= get_ca_pk(tlvRoot
);
898 PrintAndLog("ERROR: Key not found. Exit.");
902 struct emv_pk
*issuer_pk
= emv_pki_recover_issuer_cert(pk
, tlvRoot
);
905 PrintAndLog("WARNING: Issuer certificate not found. Exit.");
908 PrintAndLogEx(SUCCESS
, "Issuer PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx",
915 issuer_pk
->serial
[0],
916 issuer_pk
->serial
[1],
920 JsonSaveBufAsHex(root
, "$.ApplicationData.RID", issuer_pk
->rid
, 5);
922 char *issuer_pk_c
= emv_pk_dump_pk(issuer_pk
);
923 JsonSaveStr(root
, "$.ApplicationData.IssuerPublicKeyDec", issuer_pk_c
);
924 JsonSaveBufAsHex(root
, "$.ApplicationData.IssuerPublicKeyModulus", issuer_pk
->modulus
, issuer_pk
->mlen
);
927 struct emv_pk
*icc_pk
= emv_pki_recover_icc_cert(issuer_pk
, tlvRoot
, NULL
);
930 emv_pk_free(issuer_pk
);
931 PrintAndLogEx(WARNING
, "WARNING: ICC certificate not found. Exit.");
934 PrintAndLogEx(SUCCESS
, "ICC PK recovered. RID %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx CSN %02hhx:%02hhx:%02hhx\n",
946 char *icc_pk_c
= emv_pk_dump_pk(icc_pk
);
947 JsonSaveStr(root
, "$.ApplicationData.ICCPublicKeyDec", icc_pk_c
);
948 JsonSaveBufAsHex(root
, "$.ApplicationData.ICCPublicKeyModulus", icc_pk
->modulus
, icc_pk
->mlen
);