1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2017, 2018 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 "test/cryptotest.h"
14 #include "cliparser/cliparser.h"
16 int CmdHFEMVSelect(const char *cmd
) {
17 uint8_t data
[APDU_AID_LEN
] = {0};
21 CLIParserInit("hf 14a select",
22 "Executes select applet command",
23 "Usage:\n\thf emv select -s a00000000101 -> select card, select applet\n\thf emv select -st a00000000101 -> select card, select applet, show result in TLV\n");
27 arg_lit0("sS", "select", "activate field and select card"),
28 arg_lit0("kK", "keep", "keep field for next command"),
29 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
30 arg_lit0("tT", "tlv", "TLV decode results"),
31 arg_str0(NULL
, NULL
, "<HEX applet AID>", NULL
),
34 CLIExecWithReturn(cmd
, argtable
, true);
36 bool activateField
= arg_get_lit(1);
37 bool leaveSignalON
= arg_get_lit(2);
38 bool APDULogging
= arg_get_lit(3);
39 bool decodeTLV
= arg_get_lit(4);
40 CLIGetStrWithReturn(5, data
, &datalen
);
43 SetAPDULogging(APDULogging
);
46 uint8_t buf
[APDU_RES_LEN
] = {0};
49 int res
= EMVSelect(activateField
, leaveSignalON
, data
, datalen
, buf
, sizeof(buf
), &len
, &sw
, NULL
);
52 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
58 TLVPrintFromBuffer(buf
, len
);
63 int CmdHFEMVSearch(const char *cmd
) {
65 CLIParserInit("hf 14a select",
66 "Tries to select all applets from applet list:\n",
67 "Usage:\n\thf emv search -s -> select card and search\n\thf emv search -st -> select card, search and show result in TLV\n");
71 arg_lit0("sS", "select", "activate field and select card"),
72 arg_lit0("kK", "keep", "keep field ON for next command"),
73 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
74 arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
77 CLIExecWithReturn(cmd
, argtable
, true);
79 bool activateField
= arg_get_lit(1);
80 bool leaveSignalON
= arg_get_lit(2);
81 bool APDULogging
= arg_get_lit(3);
82 bool decodeTLV
= arg_get_lit(4);
85 SetAPDULogging(APDULogging
);
87 struct tlvdb
*t
= NULL
;
88 const char *al
= "Applets list";
89 t
= tlvdb_fixed(1, strlen(al
), (const unsigned char *)al
);
91 if (EMVSearch(activateField
, leaveSignalON
, decodeTLV
, t
)) {
96 PrintAndLog("Search completed.");
100 TLVPrintAIDlistFromSelectTLV(t
);
108 int CmdHFEMVPPSE(const char *cmd
) {
110 CLIParserInit("hf 14a pse",
111 "Executes PSE/PPSE select command. It returns list of applet on the card:\n",
112 "Usage:\n\thf emv pse -s1 -> select, get pse\n\thf emv pse -st2 -> select, get ppse, show result in TLV\n");
116 arg_lit0("sS", "select", "activate field and select card"),
117 arg_lit0("kK", "keep", "keep field ON for next command"),
118 arg_lit0("1", "pse", "pse (1PAY.SYS.DDF01) mode"),
119 arg_lit0("2", "ppse", "ppse (2PAY.SYS.DDF01) mode (default mode)"),
120 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
121 arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
124 CLIExecWithReturn(cmd
, argtable
, true);
126 bool activateField
= arg_get_lit(1);
127 bool leaveSignalON
= arg_get_lit(2);
133 bool APDULogging
= arg_get_lit(5);
134 bool decodeTLV
= arg_get_lit(6);
137 SetAPDULogging(APDULogging
);
140 uint8_t buf
[APDU_RES_LEN
] = {0};
143 int res
= EMVSelectPSE(activateField
, leaveSignalON
, PSENum
, buf
, sizeof(buf
), &len
, &sw
);
146 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
153 TLVPrintFromBuffer(buf
, len
);
158 #define TLV_ADD(tag, value)( tlvdb_add(tlvRoot, tlvdb_fixed(tag, sizeof(value) - 1, (const unsigned char *)value)) )
160 int CmdHFEMVGPO(const char *cmd
) {
161 uint8_t data
[APDU_RES_LEN
] = {0};
164 CLIParserInit("hf 14a gpo",
165 "Executes Get Processing Options command. It returns data in TLV format (0x77 - format2) or plain format (0x80 - format1).\nNeeds a EMV applet to be selected.",
166 "Usage:\n\thf emv gpo -k -> execute GPO\n\thf emv gpo -st 01020304 -> execute GPO with 4-byte PDOL data, show result in TLV\n");
167 // here need to add load params from file and gen pdol
171 arg_lit0("kK", "keep", "keep field ON for next command"),
172 arg_lit0("pP", "params", "load parameters for PDOL making from `emv/defparams.json` file (by default uses default parameters) (NOT WORK!!!)"),
173 arg_lit0("mM", "make", "make PDOLdata from PDOL (tag 9F38) and parameters (NOT WORK!!!)"),
174 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
175 arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
176 arg_str0(NULL
, NULL
, "<HEX PDOLdata/PDOL>", NULL
),
179 CLIExecWithReturn(cmd
, argtable
, true);
181 bool leaveSignalON
= arg_get_lit(1);
182 bool paramsLoadFromFile
= arg_get_lit(2);
183 bool dataMakeFromPDOL
= arg_get_lit(3);
184 bool APDULogging
= arg_get_lit(4);
185 bool decodeTLV
= arg_get_lit(5);
186 CLIGetStrWithReturn(6, data
, &datalen
);
189 SetAPDULogging(APDULogging
);
192 const char *alr
= "Root terminal TLV tree";
193 struct tlvdb
*tlvRoot
= tlvdb_fixed(1, strlen(alr
), (const unsigned char *)alr
);
196 struct tlv
*pdol_data_tlv
= NULL
;
197 struct tlv data_tlv
= {
200 .value
= (uint8_t *)data
,
202 if (dataMakeFromPDOL
) {
204 PrintAndLog("Make PDOL data not implemented!");
206 //9F02:(Amount, authorized (Numeric)) len:6
207 TLV_ADD(0x9F02, "\x00\x00\x00\x00\x01\x00");
208 //9F1A:(Terminal Country Code) len:2
209 TLV_ADD(0x9F1A, "ru");
210 //5F2A:(Transaction Currency Code) len:2
211 // USD 840, EUR 978, RUR 810, RUB 643, RUR 810(old), UAH 980, AZN 031, n/a 999
212 TLV_ADD(0x5F2A, "\x09\x80");
213 //9A:(Transaction Date) len:3
214 TLV_ADD(0x9A, "\x00\x00\x00");
215 //9C:(Transaction Type) len:1 | 00 => Goods and service #01 => Cash
216 TLV_ADD(0x9C, "\x00");
217 // 9F37 Unpredictable Number len:4
218 TLV_ADD(0x9F37, "\x01\x02\x03\x04");
219 // 9F6A Unpredictable Number (MSD for UDOL) len:4
220 TLV_ADD(0x9F6A, "\x01\x02\x03\x04");
221 //9F66:(Terminal Transaction Qualifiers (TTQ)) len:4
222 TLV_ADD(0x9F66, "\x26\x00\x00\x00"); // qVSDC
224 if (paramsLoadFromFile
) {
226 /* pdol_data_tlv = dol_process(tlvdb_get(tlvRoot, 0x9f38, NULL), tlvRoot, 0x83);
228 PrintAndLog("ERROR: can't create PDOL TLV.");
234 pdol_data_tlv
= &data_tlv
;
237 size_t pdol_data_tlv_data_len
= 0;
238 unsigned char *pdol_data_tlv_data
= tlv_encode(pdol_data_tlv
, &pdol_data_tlv_data_len
);
239 if (!pdol_data_tlv_data
) {
240 PrintAndLog("ERROR: can't create PDOL data.");
244 PrintAndLog("PDOL data[%d]: %s", pdol_data_tlv_data_len
, sprint_hex(pdol_data_tlv_data
, pdol_data_tlv_data_len
));
247 uint8_t buf
[APDU_RES_LEN
] = {0};
250 int res
= EMVGPO(leaveSignalON
, pdol_data_tlv_data
, pdol_data_tlv_data_len
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
252 free(pdol_data_tlv_data
);
256 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
262 TLVPrintFromBuffer(buf
, len
);
267 int CmdHFEMVReadRecord(const char *cmd
) {
268 uint8_t data
[APDU_RES_LEN
] = {0};
271 CLIParserInit("hf 14a readrec",
272 "Executes Read Record command. It returns data in TLV format.\nNeeds a bank applet to be selected and sometimes needs GPO to be executed.",
273 "Usage:\n\thf emv readrec -k 0101 -> read file SFI=01, SFIrec=01\n\thf emv readrec -kt 0201-> read file 0201 and show result in TLV\n");
277 arg_lit0("kK", "keep", "keep field ON for next command"),
278 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
279 arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
280 arg_str1(NULL
, NULL
, "<SFI 1byte HEX><SFIrec 1byte HEX>", NULL
),
283 CLIExecWithReturn(cmd
, argtable
, true);
285 bool leaveSignalON
= arg_get_lit(1);
286 bool APDULogging
= arg_get_lit(2);
287 bool decodeTLV
= arg_get_lit(3);
288 CLIGetStrWithReturn(4, data
, &datalen
);
292 PrintAndLog("ERROR: Command needs to have 2 bytes of data");
296 SetAPDULogging(APDULogging
);
299 uint8_t buf
[APDU_RES_LEN
] = {0};
302 int res
= EMVReadRecord(leaveSignalON
, data
[0], data
[1], buf
, sizeof(buf
), &len
, &sw
, NULL
);
305 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
312 TLVPrintFromBuffer(buf
, len
);
317 int CmdHFEMVAC(const char *cmd
) {
318 uint8_t data
[APDU_RES_LEN
] = {0};
321 CLIParserInit("hf 14a genac",
322 "Generate Application Cryptogram command. It returns data in TLV format .\nNeeds a EMV applet to be selected and GPO to be executed.",
323 "Usage:\n\thf emv genac -k 0102 -> execute GPO with 2-byte CDOLdata and keep field ON after command\n"
324 "\thf emv genac -t 01020304 -> execute GPO with 4-byte CDOL data, show result in TLV\n");
328 arg_lit0("kK", "keep", "keep field ON for next command"),
329 arg_lit0("cC", "cda", "executes CDA transaction. Needs to get SDAD in results."),
330 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
331 arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
332 arg_str1(NULL
, NULL
, "<HEX CDOLdata>", NULL
),
335 CLIExecWithReturn(cmd
, argtable
, false);
337 bool leaveSignalON
= arg_get_lit(1);
338 bool trTypeCDA
= arg_get_lit(2);
339 bool APDULogging
= arg_get_lit(3);
340 bool decodeTLV
= arg_get_lit(4);
341 CLIGetStrWithReturn(5, data
, &datalen
);
344 SetAPDULogging(APDULogging
);
347 const char *alr
= "Root terminal TLV tree";
348 struct tlvdb
*tlvRoot
= tlvdb_fixed(1, strlen(alr
), (const unsigned char *)alr
);
351 struct tlv
*cdol_data_tlv
= NULL
;
352 // struct tlv *cdol_data_tlv = dol_process(tlvdb_get(tlvRoot, 0x8c, NULL), tlvRoot, 0x01); // 0x01 - dummy tag
353 struct tlv data_tlv
= {
356 .value
= (uint8_t *)data
,
358 cdol_data_tlv
= &data_tlv
;
359 PrintAndLog("CDOL data[%d]: %s", cdol_data_tlv
->len
, sprint_hex(cdol_data_tlv
->value
, cdol_data_tlv
->len
));
362 uint8_t buf
[APDU_RES_LEN
] = {0};
365 int res
= EMVAC(leaveSignalON
, (trTypeCDA
) ? EMVAC_TC
+ EMVAC_CDAREQ
: EMVAC_TC
, (uint8_t *)cdol_data_tlv
->value
, cdol_data_tlv
->len
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
367 // free(cdol_data_tlv);
371 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
377 TLVPrintFromBuffer(buf
, len
);
382 int CmdHFEMVGenerateChallenge(const char *cmd
) {
384 CLIParserInit("hf 14a challenge",
385 "Executes Generate Challenge command. It returns 4 or 8-byte random number from card:\n",
386 "Usage:\n\thf emv challenge -> get challenge\n\thf emv challenge -k -> get challenge, keep fileld ON\n");
390 arg_lit0("kK", "keep", "keep field ON for next command"),
391 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
394 CLIExecWithReturn(cmd
, argtable
, true);
396 bool leaveSignalON
= arg_get_lit(1);
397 bool APDULogging
= arg_get_lit(2);
400 SetAPDULogging(APDULogging
);
403 uint8_t buf
[APDU_RES_LEN
] = {0};
406 int res
= EMVGenerateChallenge(leaveSignalON
, buf
, sizeof(buf
), &len
, &sw
, NULL
);
409 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
414 PrintAndLog("Challenge: %s", sprint_hex(buf
, len
));
416 if (len
!= 4 && len
!= 8)
417 PrintAndLog("WARNING: length of challenge must be 4 or 8, but it %d", len
);
422 int CmdHFEMVInternalAuthenticate(const char *cmd
) {
423 uint8_t data
[APDU_RES_LEN
] = {0};
426 CLIParserInit("hf 14a intauth",
427 "Generate Internal Authenticate command. Usually needs 4-byte random number. It returns data in TLV format .\nNeeds a EMV applet to be selected and GPO to be executed.",
428 "Usage:\n\thf emv intauth -k 01020304 -> execute Internal Authenticate with 4-byte DDOLdata and keep field ON after command\n"
429 "\thf emv intauth -t 01020304 -> execute Internal Authenticate with 4-byte DDOL data, show result in TLV\n");
433 arg_lit0("kK", "keep", "keep field ON for next command"),
434 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
435 arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
436 arg_str1(NULL
, NULL
, "<HEX DDOLdata>", NULL
),
439 CLIExecWithReturn(cmd
, argtable
, false);
441 bool leaveSignalON
= arg_get_lit(1);
442 bool APDULogging
= arg_get_lit(2);
443 bool decodeTLV
= arg_get_lit(3);
444 CLIGetStrWithReturn(4, data
, &datalen
);
447 SetAPDULogging(APDULogging
);
450 PrintAndLog("DDOL data[%d]: %s", datalen
, sprint_hex(data
, datalen
));
453 uint8_t buf
[APDU_RES_LEN
] = {0};
456 int res
= EMVInternalAuthenticate(leaveSignalON
, data
, datalen
, buf
, sizeof(buf
), &len
, &sw
, NULL
);
459 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
465 TLVPrintFromBuffer(buf
, len
);
470 int UsageCmdHFEMVExec(void) {
471 PrintAndLog("HELP : Executes EMV contactless transaction:\n");
472 PrintAndLog("Usage: hf emv exec [-s][-a][-t][-f][-v][-c][-x][-g]\n");
473 PrintAndLog(" Options:");
474 PrintAndLog(" -s : select card");
475 PrintAndLog(" -a : show APDU reqests and responses\n");
476 PrintAndLog(" -t : TLV decode results\n");
477 PrintAndLog(" -f : force search AID. Search AID instead of execute PPSE.\n");
478 PrintAndLog(" -v : transaction type - qVSDC or M/Chip.\n");
479 PrintAndLog(" -c : transaction type - qVSDC or M/Chip plus CDA (SDAD generation).\n");
480 PrintAndLog(" -x : transaction type - VSDC. For test only. Not a standart behavior.\n");
481 PrintAndLog(" -g : VISA. generate AC from GPO\n");
482 PrintAndLog("By default : transaction type - MSD.\n");
483 PrintAndLog("Samples:");
484 PrintAndLog(" hf emv exec -s -a -t -> execute MSD transaction");
485 PrintAndLog(" hf emv exec -s -a -t -c -> execute CDA transaction");
489 #define dreturn(n) {free(pdol_data_tlv);tlvdb_free(tlvSelect);tlvdb_free(tlvRoot);DropField();return n;}
491 int CmdHFEMVExec(const char *cmd
) {
492 bool activateField
= false;
493 bool showAPDU
= false;
494 bool decodeTLV
= false;
495 bool forceSearch
= false;
496 enum TransactionType TrType
= TT_MSD
;
497 bool GenACGPO
= false;
499 uint8_t buf
[APDU_RES_LEN
] = {0};
502 uint8_t AID
[APDU_AID_LEN
] = {0};
504 uint8_t ODAiList
[4096];
505 size_t ODAiListLen
= 0;
509 struct tlvdb
*tlvSelect
= NULL
;
510 struct tlvdb
*tlvRoot
= NULL
;
511 struct tlv
*pdol_data_tlv
= NULL
;
513 if (strlen(cmd
) < 1) {
519 while(param_getchar(cmd
, cmdp
) != 0x00) {
520 char c
= param_getchar(cmd
, cmdp
);
521 if ((c
== '-') && (param_getlength(cmd
, cmdp
) == 2))
522 switch (param_getchar_indx(cmd
, 1, cmdp
)) {
529 activateField
= true;
549 TrType
= TT_QVSDCMCHIP
;
560 PrintAndLog("Unknown parameter '%c'", param_getchar_indx(cmd
, 1, cmdp
));
567 // init applets list tree
568 const char *al
= "Applets list";
569 tlvSelect
= tlvdb_fixed(1, strlen(al
), (const unsigned char *)al
);
571 // Application Selection
572 // https://www.openscdp.org/scripts/tutorial/emv/applicationselection.html
575 PrintAndLog("\n* PPSE.");
576 SetAPDULogging(showAPDU
);
577 res
= EMVSearchPSE(activateField
, true, decodeTLV
, tlvSelect
);
579 // check PPSE and select application id
581 TLVPrintAIDlistFromSelectTLV(tlvSelect
);
582 EMVSelectApplication(tlvSelect
, AID
, &AIDlen
);
588 PrintAndLog("\n* Search AID in list.");
589 SetAPDULogging(false);
590 if (EMVSearch(activateField
, true, decodeTLV
, tlvSelect
)) {
594 // check search and select application id
595 TLVPrintAIDlistFromSelectTLV(tlvSelect
);
596 EMVSelectApplication(tlvSelect
, AID
, &AIDlen
);
600 const char *alr
= "Root terminal TLV tree";
601 tlvRoot
= tlvdb_fixed(1, strlen(alr
), (const unsigned char *)alr
);
603 // check if we found EMV application on card
605 PrintAndLog("Can't select AID. EMV AID not found");
610 PrintAndLog("\n* Selecting AID:%s", sprint_hex_inrow(AID
, AIDlen
));
611 SetAPDULogging(showAPDU
);
612 res
= EMVSelect(false, true, AID
, AIDlen
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
615 PrintAndLog("Can't select AID (%d). Exit...", res
);
620 TLVPrintFromBuffer(buf
, len
);
621 PrintAndLog("* Selected.");
623 PrintAndLog("\n* Init transaction parameters.");
625 //9F66:(Terminal Transaction Qualifiers (TTQ)) len:4
626 char *qVSDC
= "\x26\x00\x00\x00";
628 qVSDC
= "\x26\x80\x00\x00";
632 TLV_ADD(0x9F66, "\x86\x00\x00\x00"); // MSD
634 // not standard for contactless. just for test.
636 TLV_ADD(0x9F66, "\x46\x00\x00\x00"); // VSDC
639 TLV_ADD(0x9F66, qVSDC
); // qVSDC
642 TLV_ADD(0x9F66, qVSDC
); // qVSDC (VISA CDA not enabled)
645 TLV_ADD(0x9F66, "\x26\x00\x00\x00"); // qVSDC
649 //9F02:(Amount, authorized (Numeric)) len:6
650 TLV_ADD(0x9F02, "\x00\x00\x00\x00\x01\x00");
651 //9F1A:(Terminal Country Code) len:2
652 TLV_ADD(0x9F1A, "ru");
653 //5F2A:(Transaction Currency Code) len:2
654 // USD 840, EUR 978, RUR 810, RUB 643, RUR 810(old), UAH 980, AZN 031, n/a 999
655 TLV_ADD(0x5F2A, "\x09\x80");
656 //9A:(Transaction Date) len:3
657 TLV_ADD(0x9A, "\x00\x00\x00");
658 //9C:(Transaction Type) len:1 | 00 => Goods and service #01 => Cash
659 TLV_ADD(0x9C, "\x00");
660 // 9F37 Unpredictable Number len:4
661 TLV_ADD(0x9F37, "\x01\x02\x03\x04");
662 // 9F6A Unpredictable Number (MSD for UDOL) len:4
663 TLV_ADD(0x9F6A, "\x01\x02\x03\x04");
665 TLVPrintFromTLV(tlvRoot
); // TODO delete!!!
667 PrintAndLog("\n* Calc PDOL.");
668 pdol_data_tlv
= dol_process(tlvdb_get(tlvRoot
, 0x9f38, NULL
), tlvRoot
, 0x83);
670 PrintAndLog("ERROR: can't create PDOL TLV.");
674 size_t pdol_data_tlv_data_len
;
675 unsigned char *pdol_data_tlv_data
= tlv_encode(pdol_data_tlv
, &pdol_data_tlv_data_len
);
676 if (!pdol_data_tlv_data
) {
677 PrintAndLog("ERROR: can't create PDOL data.");
680 PrintAndLog("PDOL data[%d]: %s", pdol_data_tlv_data_len
, sprint_hex(pdol_data_tlv_data
, pdol_data_tlv_data_len
));
682 PrintAndLog("\n* GPO.");
683 res
= EMVGPO(true, pdol_data_tlv_data
, pdol_data_tlv_data_len
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
685 free(pdol_data_tlv_data
);
686 //free(pdol_data_tlv); --- free on exit.
689 PrintAndLog("GPO error(%d): %4x. Exit...", res
, sw
);
693 // process response template format 1 [id:80 2b AIP + x4b AFL] and format 2 [id:77 TLV]
694 if (buf
[0] == 0x80) {
696 PrintAndLog("GPO response format1:");
697 TLVPrintFromBuffer(buf
, len
);
700 if (len
< 4 || (len
- 4) % 4) {
701 PrintAndLog("ERROR: GPO response format1 parsing error. length=%d", len
);
704 struct tlvdb
* f1AIP
= tlvdb_fixed(0x82, 2, buf
+ 2);
705 tlvdb_add(tlvRoot
, f1AIP
);
707 PrintAndLog("\n* * Decode response format 1 (0x80) AIP and AFL:");
708 TLVPrintFromTLV(f1AIP
);
712 struct tlvdb
* f1AFL
= tlvdb_fixed(0x94, len
- 4, buf
+ 2 + 2);
713 tlvdb_add(tlvRoot
, f1AFL
);
715 TLVPrintFromTLV(f1AFL
);
719 TLVPrintFromBuffer(buf
, len
);
722 // extract PAN from track2
724 const struct tlv
*track2
= tlvdb_get(tlvRoot
, 0x57, NULL
);
725 if (!tlvdb_get(tlvRoot
, 0x5a, NULL
) && track2
&& track2
->len
>= 8) {
726 struct tlvdb
*pan
= GetPANFromTrack2(track2
);
728 tlvdb_add(tlvRoot
, pan
);
730 const struct tlv
*pantlv
= tlvdb_get(tlvRoot
, 0x5a, NULL
);
731 PrintAndLog("\n* * Extracted PAN from track2: %s", sprint_hex(pantlv
->value
, pantlv
->len
));
733 PrintAndLog("\n* * WARNING: Can't extract PAN from track2.");
738 PrintAndLog("\n* Read records from AFL.");
739 const struct tlv
*AFL
= tlvdb_get(tlvRoot
, 0x94, NULL
);
740 if (!AFL
|| !AFL
->len
) {
741 PrintAndLog("WARNING: AFL not found.");
744 while(AFL
&& AFL
->len
) {
746 PrintAndLog("ERROR: Wrong AFL length: %d", AFL
->len
);
750 for (int i
= 0; i
< AFL
->len
/ 4; i
++) {
751 uint8_t SFI
= AFL
->value
[i
* 4 + 0] >> 3;
752 uint8_t SFIstart
= AFL
->value
[i
* 4 + 1];
753 uint8_t SFIend
= AFL
->value
[i
* 4 + 2];
754 uint8_t SFIoffline
= AFL
->value
[i
* 4 + 3];
756 PrintAndLog("* * SFI[%02x] start:%02x end:%02x offline:%02x", SFI
, SFIstart
, SFIend
, SFIoffline
);
757 if (SFI
== 0 || SFI
== 31 || SFIstart
== 0 || SFIstart
> SFIend
) {
758 PrintAndLog("SFI ERROR! Skipped...");
762 for(int n
= SFIstart
; n
<= SFIend
; n
++) {
763 PrintAndLog("* * * SFI[%02x] %d", SFI
, n
);
765 res
= EMVReadRecord(true, SFI
, n
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
767 PrintAndLog("ERROR SFI[%02x]. APDU error %4x", SFI
, sw
);
772 TLVPrintFromBuffer(buf
, len
);
776 // Build Input list for Offline Data Authentication
777 // EMV 4.3 book3 10.3, page 96
780 const unsigned char *abuf
= buf
;
783 if (tlv_parse_tl(&abuf
, &elmlen
, &e
)) {
784 memcpy(&ODAiList
[ODAiListLen
], &buf
[len
- elmlen
], elmlen
);
785 ODAiListLen
+= elmlen
;
787 PrintAndLog("ERROR SFI[%02x]. Creating input list for Offline Data Authentication error.", SFI
);
790 memcpy(&ODAiList
[ODAiListLen
], buf
, len
);
800 // copy Input list for Offline Data Authentication
802 struct tlvdb
*oda
= tlvdb_fixed(0x21, ODAiListLen
, ODAiList
); // not a standard tag
803 tlvdb_add(tlvRoot
, oda
);
804 PrintAndLog("* Input list for Offline Data Authentication added to TLV. len=%d \n", ODAiListLen
);
808 const struct tlv
*AIPtlv
= tlvdb_get(tlvRoot
, 0x82, NULL
);
809 uint16_t AIP
= AIPtlv
->value
[0] + AIPtlv
->value
[1] * 0x100;
810 PrintAndLog("* * AIP=%04x", AIP
);
814 PrintAndLog("\n* SDA");
820 PrintAndLog("\n* DDA");
821 trDDA(decodeTLV
, tlvRoot
);
827 if (TrType
== TT_QVSDCMCHIP
|| TrType
== TT_CDA
){
828 // 9F26: Application Cryptogram
829 const struct tlv
*AC
= tlvdb_get(tlvRoot
, 0x9F26, NULL
);
831 PrintAndLog("\n--> qVSDC transaction.");
832 PrintAndLog("* AC path");
834 // 9F36: Application Transaction Counter (ATC)
835 const struct tlv
*ATC
= tlvdb_get(tlvRoot
, 0x9F36, NULL
);
838 // 9F10: Issuer Application Data - optional
839 const struct tlv
*IAD
= tlvdb_get(tlvRoot
, 0x9F10, NULL
);
842 PrintAndLog("ATC: %s", sprint_hex(ATC
->value
, ATC
->len
));
843 PrintAndLog("AC: %s", sprint_hex(AC
->value
, AC
->len
));
845 PrintAndLog("IAD: %s", sprint_hex(IAD
->value
, IAD
->len
));
847 if (IAD
->len
>= IAD
->value
[0] + 1) {
848 PrintAndLog("\tKey index: 0x%02x", IAD
->value
[1]);
849 PrintAndLog("\tCrypto ver: 0x%02x(%03d)", IAD
->value
[2], IAD
->value
[2]);
850 PrintAndLog("\tCVR:", sprint_hex(&IAD
->value
[3], IAD
->value
[0] - 2));
851 struct tlvdb
* cvr
= tlvdb_fixed(0x20, IAD
->value
[0] - 2, &IAD
->value
[3]);
852 TLVPrintFromTLVLev(cvr
, 1);
855 PrintAndLog("WARNING: IAD not found.");
859 PrintAndLog("ERROR AC: Application Transaction Counter (ATC) not found.");
865 if (GetCardPSVendor(AID
, AIDlen
) == CV_MASTERCARD
&& (TrType
== TT_QVSDCMCHIP
|| TrType
== TT_CDA
)){
866 const struct tlv
*CDOL1
= tlvdb_get(tlvRoot
, 0x8c, NULL
);
867 if (CDOL1
&& GetCardPSVendor(AID
, AIDlen
) == CV_MASTERCARD
) { // and m/chip transaction flag
868 PrintAndLog("\n--> Mastercard M/Chip transaction.");
870 PrintAndLog("* * Generate challenge");
871 res
= EMVGenerateChallenge(true, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
873 PrintAndLog("ERROR GetChallenge. APDU error %4x", sw
);
877 PrintAndLog("ERROR GetChallenge. Wrong challenge length %d", len
);
881 // ICC Dynamic Number
882 struct tlvdb
* ICCDynN
= tlvdb_fixed(0x9f4c, len
, buf
);
883 tlvdb_add(tlvRoot
, ICCDynN
);
885 PrintAndLog("\n* * ICC Dynamic Number:");
886 TLVPrintFromTLV(ICCDynN
);
889 PrintAndLog("* * Calc CDOL1");
890 struct tlv
*cdol_data_tlv
= dol_process(tlvdb_get(tlvRoot
, 0x8c, NULL
), tlvRoot
, 0x01); // 0x01 - dummy tag
892 PrintAndLog("ERROR: can't create CDOL1 TLV.");
895 PrintAndLog("CDOL1 data[%d]: %s", cdol_data_tlv
->len
, sprint_hex(cdol_data_tlv
->value
, cdol_data_tlv
->len
));
897 PrintAndLog("* * AC1");
898 // EMVAC_TC + EMVAC_CDAREQ --- to get SDAD
899 res
= EMVAC(true, (TrType
== TT_CDA
) ? EMVAC_TC
+ EMVAC_CDAREQ
: EMVAC_TC
, (uint8_t *)cdol_data_tlv
->value
, cdol_data_tlv
->len
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
902 PrintAndLog("AC1 error(%d): %4x. Exit...", res
, sw
);
907 TLVPrintFromBuffer(buf
, len
);
910 PrintAndLog("\n* CDA:");
911 struct tlvdb
*ac_tlv
= tlvdb_parse_multi(buf
, len
);
912 res
= trCDA(tlvRoot
, ac_tlv
, pdol_data_tlv
, cdol_data_tlv
);
914 PrintAndLog("CDA error (%d)", res
);
919 PrintAndLog("\n* M/Chip transaction result:");
920 // 9F27: Cryptogram Information Data (CID)
921 const struct tlv
*CID
= tlvdb_get(tlvRoot
, 0x9F27, NULL
);
923 emv_tag_dump(CID
, stdout
, 0);
924 PrintAndLog("------------------------------");
926 switch(CID
->value
[0] & EMVAC_AC_MASK
){
928 PrintAndLog("Transaction DECLINED.");
931 PrintAndLog("Transaction approved OFFLINE.");
934 PrintAndLog("Transaction approved ONLINE.");
937 PrintAndLog("ERROR: CID transaction code error %2x", CID
->value
[0] & EMVAC_AC_MASK
);
941 PrintAndLog("ERROR: Wrong CID length %d", CID
->len
);
944 PrintAndLog("ERROR: CID(9F27) not found.");
951 if (AIP
& 0x8000 && TrType
== TT_MSD
) {
952 PrintAndLog("\n--> MSD transaction.");
954 PrintAndLog("* MSD dCVV path. Check dCVV");
956 const struct tlv
*track2
= tlvdb_get(tlvRoot
, 0x57, NULL
);
958 PrintAndLog("Track2: %s", sprint_hex(track2
->value
, track2
->len
));
960 struct tlvdb
*dCVV
= GetdCVVRawFromTrack2(track2
);
961 PrintAndLog("dCVV raw data:");
962 TLVPrintFromTLV(dCVV
);
964 if (GetCardPSVendor(AID
, AIDlen
) == CV_MASTERCARD
) {
965 PrintAndLog("\n* Mastercard calculate UDOL");
968 const struct tlv
*UDOL
= tlvdb_get(tlvRoot
, 0x9F69, NULL
);
969 // UDOL(9F69) default: 9F6A (Unpredictable number) 4 bytes
970 const struct tlv defUDOL
= {
973 .value
= (uint8_t *)"\x9f\x6a\x04",
976 PrintAndLog("Use default UDOL.");
978 struct tlv
*udol_data_tlv
= dol_process(UDOL
? UDOL
: &defUDOL
, tlvRoot
, 0x01); // 0x01 - dummy tag
980 PrintAndLog("ERROR: can't create UDOL TLV.");
984 PrintAndLog("UDOL data[%d]: %s", udol_data_tlv
->len
, sprint_hex(udol_data_tlv
->value
, udol_data_tlv
->len
));
986 PrintAndLog("\n* Mastercard compute cryptographic checksum(UDOL)");
988 res
= MSCComputeCryptoChecksum(true, (uint8_t *)udol_data_tlv
->value
, udol_data_tlv
->len
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
990 PrintAndLog("ERROR Compute Crypto Checksum. APDU error %4x", sw
);
996 TLVPrintFromBuffer(buf
, len
);
1003 PrintAndLog("ERROR MSD: Track2 data not found.");
1011 free(pdol_data_tlv
);
1012 tlvdb_free(tlvSelect
);
1013 tlvdb_free(tlvRoot
);
1015 PrintAndLog("\n* Transaction completed.");
1020 int CmdHFEMVTest(const char *cmd
) {
1021 return ExecuteCryptoTests(true);
1024 int CmdHelp(const char *Cmd
);
1025 static command_t CommandTable
[] = {
1026 {"help", CmdHelp
, 1, "This help"},
1027 {"exec", CmdHFEMVExec
, 0, "Executes EMV contactless transaction."},
1028 {"pse", CmdHFEMVPPSE
, 0, "Execute PPSE. It selects 2PAY.SYS.DDF01 or 1PAY.SYS.DDF01 directory."},
1029 {"search", CmdHFEMVSearch
, 0, "Try to select all applets from applets list and print installed applets."},
1030 {"select", CmdHFEMVSelect
, 0, "Select applet."},
1031 {"gpo", CmdHFEMVGPO
, 0, "Execute GetProcessingOptions."},
1032 {"readrec", CmdHFEMVReadRecord
, 0, "Read files from card."},
1033 {"genac", CmdHFEMVAC
, 0, "Generate ApplicationCryptogram."},
1034 {"challenge", CmdHFEMVGenerateChallenge
, 0, "Generate challenge."},
1035 {"intauth", CmdHFEMVInternalAuthenticate
, 0, "Internal authentication."},
1036 {"test", CmdHFEMVTest
, 0, "Crypto logic test."},
1037 {NULL
, NULL
, 0, NULL
}
1040 int CmdHFEMV(const char *Cmd
) {
1041 CmdsParse(CommandTable
, Cmd
);
1045 int CmdHelp(const char *Cmd
) {
1046 CmdsHelp(CommandTable
);