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"
17 int UsageCmdHFEMVSelect(void) {
18 PrintAndLog("HELP : Executes select applet command:\n");
19 PrintAndLog("Usage: hf emv select [-s][-k][-a][-t] <HEX applet AID>\n");
20 PrintAndLog(" Options:");
21 PrintAndLog(" -s : select card");
22 PrintAndLog(" -k : keep field for next command");
23 PrintAndLog(" -a : show APDU reqests and responses\n");
24 PrintAndLog(" -t : TLV decode results\n");
25 PrintAndLog("Samples:");
26 PrintAndLog(" hf emv select -s a00000000101 -> select card, select applet");
27 PrintAndLog(" hf emv select -s -t a00000000101 -> select card, select applet, show result in TLV");
31 int CmdHFEMVSelect(const char *cmd
) {
32 uint8_t data
[APDU_AID_LEN
] = {0};
36 CLIParserInit("hf emv select",
37 "Executes select applet command",
38 "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");
42 arg_lit0("sS", "select", "activate field and select card"),
43 arg_lit0("kK", "keep", "keep field for next command"),
44 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
45 arg_lit0("tT", "tlv", "TLV decode results"),
46 arg_str0(NULL
, NULL
, "<HEX applet AID>", NULL
),
49 CLIExecWithReturn(cmd
, argtable
, true);
51 bool activateField
= arg_get_lit(1);
52 bool leaveSignalON
= arg_get_lit(2);
53 bool APDULogging
= arg_get_lit(3);
54 bool decodeTLV
= arg_get_lit(4);
55 CLIGetStrWithReturn(5, data
, &datalen
);
58 SetAPDULogging(APDULogging
);
61 uint8_t buf
[APDU_RES_LEN
] = {0};
64 int res
= EMVSelect(activateField
, leaveSignalON
, data
, datalen
, buf
, sizeof(buf
), &len
, &sw
, NULL
);
67 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
73 TLVPrintFromBuffer(buf
, len
);
78 int CmdHFEMVSearch(const char *cmd
) {
80 CLIParserInit("hf emv search",
81 "Tries to select all applets from applet list:\n",
82 "Usage:\n\thf emv search -s -> select card and search\n\thf emv search -st -> select card, search and show result in TLV\n");
86 arg_lit0("sS", "select", "activate field and select card"),
87 arg_lit0("kK", "keep", "keep field ON for next command"),
88 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
89 arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
92 CLIExecWithReturn(cmd
, argtable
, true);
94 bool activateField
= arg_get_lit(1);
95 bool leaveSignalON
= arg_get_lit(2);
96 bool APDULogging
= arg_get_lit(3);
97 bool decodeTLV
= arg_get_lit(4);
100 SetAPDULogging(APDULogging
);
102 struct tlvdb
*t
= NULL
;
103 const char *al
= "Applets list";
104 t
= tlvdb_fixed(1, strlen(al
), (const unsigned char *)al
);
106 if (EMVSearch(activateField
, leaveSignalON
, decodeTLV
, t
)) {
111 PrintAndLog("Search completed.");
115 TLVPrintAIDlistFromSelectTLV(t
);
123 int CmdHFEMVPPSE(const char *cmd
) {
125 CLIParserInit("hf emv pse",
126 "Executes PSE/PPSE select command. It returns list of applet on the card:\n",
127 "Usage:\n\thf emv pse -s1 -> select, get pse\n\thf emv pse -st2 -> select, get ppse, show result in TLV\n");
131 arg_lit0("sS", "select", "activate field and select card"),
132 arg_lit0("kK", "keep", "keep field ON for next command"),
133 arg_lit0("1", "pse", "pse (1PAY.SYS.DDF01) mode"),
134 arg_lit0("2", "ppse", "ppse (2PAY.SYS.DDF01) mode (default mode)"),
135 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
136 arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
139 CLIExecWithReturn(cmd
, argtable
, true);
141 bool activateField
= arg_get_lit(1);
142 bool leaveSignalON
= arg_get_lit(2);
148 bool APDULogging
= arg_get_lit(5);
149 bool decodeTLV
= arg_get_lit(6);
152 SetAPDULogging(APDULogging
);
155 uint8_t buf
[APDU_RES_LEN
] = {0};
158 int res
= EMVSelectPSE(activateField
, leaveSignalON
, PSENum
, buf
, sizeof(buf
), &len
, &sw
);
161 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
168 TLVPrintFromBuffer(buf
, len
);
173 #define TLV_ADD(tag, value)( tlvdb_change_or_add_node(tlvRoot, tag, sizeof(value) - 1, (const unsigned char *)value) )
175 int CmdHFEMVGPO(const char *cmd
) {
176 uint8_t data
[APDU_RES_LEN
] = {0};
179 CLIParserInit("hf emv gpo",
180 "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.",
181 "Usage:\n\thf emv gpo -k -> execute GPO\n"
182 "\thf emv gpo -st 01020304 -> execute GPO with 4-byte PDOL data, show result in TLV\n");
183 // here need to add load params from file and gen pdol
187 arg_lit0("kK", "keep", "keep field ON for next command"),
188 arg_lit0("pP", "params", "load parameters for PDOL making from `emv/defparams.json` file (by default uses default parameters) (NOT WORK!!!)"),
189 arg_lit0("mM", "make", "make PDOLdata from PDOL (tag 9F38) and parameters (NOT WORK!!!)"),
190 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
191 arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
192 arg_str0(NULL
, NULL
, "<HEX PDOLdata/PDOL>", NULL
),
195 CLIExecWithReturn(cmd
, argtable
, true);
197 bool leaveSignalON
= arg_get_lit(1);
198 bool paramsLoadFromFile
= arg_get_lit(2);
199 bool dataMakeFromPDOL
= arg_get_lit(3);
200 bool APDULogging
= arg_get_lit(4);
201 bool decodeTLV
= arg_get_lit(5);
202 CLIGetStrWithReturn(6, data
, &datalen
);
205 SetAPDULogging(APDULogging
);
208 const char *alr
= "Root terminal TLV tree";
209 struct tlvdb
*tlvRoot
= tlvdb_fixed(1, strlen(alr
), (const unsigned char *)alr
);
212 struct tlv
*pdol_data_tlv
= NULL
;
213 struct tlv data_tlv
= {
216 .value
= (uint8_t *)data
,
218 if (dataMakeFromPDOL
) {
220 PrintAndLog("Make PDOL data not implemented!");
222 //9F02:(Amount, authorized (Numeric)) len:6
223 TLV_ADD(0x9F02, "\x00\x00\x00\x00\x01\x00");
224 //9F1A:(Terminal Country Code) len:2
225 TLV_ADD(0x9F1A, "ru");
226 //5F2A:(Transaction Currency Code) len:2
227 // USD 840, EUR 978, RUR 810, RUB 643, RUR 810(old), UAH 980, AZN 031, n/a 999
228 TLV_ADD(0x5F2A, "\x09\x80");
229 //9A:(Transaction Date) len:3
230 TLV_ADD(0x9A, "\x00\x00\x00");
231 //9C:(Transaction Type) len:1 | 00 => Goods and service #01 => Cash
232 TLV_ADD(0x9C, "\x00");
233 // 9F37 Unpredictable Number len:4
234 TLV_ADD(0x9F37, "\x01\x02\x03\x04");
235 // 9F6A Unpredictable Number (MSD for UDOL) len:4
236 TLV_ADD(0x9F6A, "\x01\x02\x03\x04");
237 //9F66:(Terminal Transaction Qualifiers (TTQ)) len:4
238 TLV_ADD(0x9F66, "\x26\x00\x00\x00"); // qVSDC
240 if (paramsLoadFromFile
) {
242 /* pdol_data_tlv = dol_process(tlvdb_get(tlvRoot, 0x9f38, NULL), tlvRoot, 0x83);
244 PrintAndLog("ERROR: can't create PDOL TLV.");
250 pdol_data_tlv
= &data_tlv
;
253 size_t pdol_data_tlv_data_len
= 0;
254 unsigned char *pdol_data_tlv_data
= tlv_encode(pdol_data_tlv
, &pdol_data_tlv_data_len
);
255 if (!pdol_data_tlv_data
) {
256 PrintAndLog("ERROR: can't create PDOL data.");
260 PrintAndLog("PDOL data[%d]: %s", pdol_data_tlv_data_len
, sprint_hex(pdol_data_tlv_data
, pdol_data_tlv_data_len
));
263 uint8_t buf
[APDU_RES_LEN
] = {0};
266 int res
= EMVGPO(leaveSignalON
, pdol_data_tlv_data
, pdol_data_tlv_data_len
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
268 free(pdol_data_tlv_data
);
272 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
278 TLVPrintFromBuffer(buf
, len
);
283 int CmdHFEMVReadRecord(const char *cmd
) {
284 uint8_t data
[APDU_RES_LEN
] = {0};
287 CLIParserInit("hf emv readrec",
288 "Executes Read Record command. It returns data in TLV format.\nNeeds a bank applet to be selected and sometimes needs GPO to be executed.",
289 "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");
293 arg_lit0("kK", "keep", "keep field ON for next command"),
294 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
295 arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
296 arg_str1(NULL
, NULL
, "<SFI 1byte HEX><SFIrec 1byte HEX>", NULL
),
299 CLIExecWithReturn(cmd
, argtable
, true);
301 bool leaveSignalON
= arg_get_lit(1);
302 bool APDULogging
= arg_get_lit(2);
303 bool decodeTLV
= arg_get_lit(3);
304 CLIGetStrWithReturn(4, data
, &datalen
);
308 PrintAndLog("ERROR: Command needs to have 2 bytes of data");
312 SetAPDULogging(APDULogging
);
315 uint8_t buf
[APDU_RES_LEN
] = {0};
318 int res
= EMVReadRecord(leaveSignalON
, data
[0], data
[1], buf
, sizeof(buf
), &len
, &sw
, NULL
);
321 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
328 TLVPrintFromBuffer(buf
, len
);
333 int CmdHFEMVAC(const char *cmd
) {
334 uint8_t data
[APDU_RES_LEN
] = {0};
337 CLIParserInit("hf emv genac",
338 "Generate Application Cryptogram command. It returns data in TLV format .\nNeeds a EMV applet to be selected and GPO to be executed.",
339 "Usage:\n\thf emv genac -k 0102 -> generate AC with 2-byte CDOLdata and keep field ON after command\n"
340 "\thf emv genac -t 01020304 -> generate AC with 4-byte CDOL data, show result in TLV\n"
341 "\thf emv genac -Daac 01020304 -> generate AC with 4-byte CDOL data and terminal decision 'declined'\n");
345 arg_lit0("kK", "keep", "keep field ON for next command"),
346 arg_lit0("cC", "cda", "executes CDA transaction. Needs to get SDAD in results."),
347 arg_str0("dD", "decision", "<aac|tc|arqc>", "Terminal decision. aac - declined, tc - approved, arqc - online authorisation requested"),
348 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
349 arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
350 arg_str1(NULL
, NULL
, "<HEX CDOLdata>", NULL
),
353 CLIExecWithReturn(cmd
, argtable
, false);
355 bool leaveSignalON
= arg_get_lit(1);
356 bool trTypeCDA
= arg_get_lit(2);
357 uint8_t termDecision
= 0xff;
358 if (arg_get_str_len(3)) {
359 if (!strncmp(arg_get_str(3)->sval
[0], "aac", 4))
360 termDecision
= EMVAC_AAC
;
361 if (!strncmp(arg_get_str(3)->sval
[0], "tc", 4))
362 termDecision
= EMVAC_TC
;
363 if (!strncmp(arg_get_str(3)->sval
[0], "arqc", 4))
364 termDecision
= EMVAC_ARQC
;
366 if (termDecision
== 0xff) {
367 PrintAndLog("ERROR: can't find terminal decision '%s'", arg_get_str(3)->sval
[0]);
371 termDecision
= EMVAC_TC
;
374 termDecision
= termDecision
| EMVAC_CDAREQ
;
375 bool APDULogging
= arg_get_lit(4);
376 bool decodeTLV
= arg_get_lit(5);
377 CLIGetStrWithReturn(6, data
, &datalen
);
380 SetAPDULogging(APDULogging
);
383 const char *alr
= "Root terminal TLV tree";
384 struct tlvdb
*tlvRoot
= tlvdb_fixed(1, strlen(alr
), (const unsigned char *)alr
);
387 struct tlv
*cdol_data_tlv
= NULL
;
388 // struct tlv *cdol_data_tlv = dol_process(tlvdb_get(tlvRoot, 0x8c, NULL), tlvRoot, 0x01); // 0x01 - dummy tag
389 struct tlv data_tlv
= {
392 .value
= (uint8_t *)data
,
394 cdol_data_tlv
= &data_tlv
;
395 PrintAndLog("CDOL data[%d]: %s", cdol_data_tlv
->len
, sprint_hex(cdol_data_tlv
->value
, cdol_data_tlv
->len
));
398 uint8_t buf
[APDU_RES_LEN
] = {0};
401 int res
= EMVAC(leaveSignalON
, termDecision
, (uint8_t *)cdol_data_tlv
->value
, cdol_data_tlv
->len
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
403 // free(cdol_data_tlv);
407 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
413 TLVPrintFromBuffer(buf
, len
);
418 int CmdHFEMVGenerateChallenge(const char *cmd
) {
420 CLIParserInit("hf emv challenge",
421 "Executes Generate Challenge command. It returns 4 or 8-byte random number from card.\nNeeds a EMV applet to be selected and GPO to be executed.",
422 "Usage:\n\thf emv challenge -> get challenge\n\thf emv challenge -k -> get challenge, keep fileld ON\n");
426 arg_lit0("kK", "keep", "keep field ON for next command"),
427 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
430 CLIExecWithReturn(cmd
, argtable
, true);
432 bool leaveSignalON
= arg_get_lit(1);
433 bool APDULogging
= arg_get_lit(2);
436 SetAPDULogging(APDULogging
);
439 uint8_t buf
[APDU_RES_LEN
] = {0};
442 int res
= EMVGenerateChallenge(leaveSignalON
, buf
, sizeof(buf
), &len
, &sw
, NULL
);
445 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
450 PrintAndLog("Challenge: %s", sprint_hex(buf
, len
));
452 if (len
!= 4 && len
!= 8)
453 PrintAndLog("WARNING: length of challenge must be 4 or 8, but it %d", len
);
458 int CmdHFEMVInternalAuthenticate(const char *cmd
) {
459 uint8_t data
[APDU_RES_LEN
] = {0};
462 CLIParserInit("hf emv intauth",
463 "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.",
464 "Usage:\n\thf emv intauth -k 01020304 -> execute Internal Authenticate with 4-byte DDOLdata and keep field ON after command\n"
465 "\thf emv intauth -t 01020304 -> execute Internal Authenticate with 4-byte DDOL data, show result in TLV\n");
469 arg_lit0("kK", "keep", "keep field ON for next command"),
470 arg_lit0("aA", "apdu", "show APDU reqests and responses"),
471 arg_lit0("tT", "tlv", "TLV decode results of selected applets"),
472 arg_str1(NULL
, NULL
, "<HEX DDOLdata>", NULL
),
475 CLIExecWithReturn(cmd
, argtable
, false);
477 bool leaveSignalON
= arg_get_lit(1);
478 bool APDULogging
= arg_get_lit(2);
479 bool decodeTLV
= arg_get_lit(3);
480 CLIGetStrWithReturn(4, data
, &datalen
);
483 SetAPDULogging(APDULogging
);
486 PrintAndLog("DDOL data[%d]: %s", datalen
, sprint_hex(data
, datalen
));
489 uint8_t buf
[APDU_RES_LEN
] = {0};
492 int res
= EMVInternalAuthenticate(leaveSignalON
, data
, datalen
, buf
, sizeof(buf
), &len
, &sw
, NULL
);
495 PrintAndLog("APDU response status: %04x - %s", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
501 TLVPrintFromBuffer(buf
, len
);
506 int UsageCmdHFEMVExec(void) {
507 PrintAndLog("HELP : Executes EMV contactless transaction:\n");
508 PrintAndLog("Usage: hf emv exec [-s][-a][-t][-j][-f][-v][-c][-x][-g]\n");
509 PrintAndLog(" Options:");
510 PrintAndLog(" -s : select card");
511 PrintAndLog(" -a : show APDU reqests and responses\n");
512 PrintAndLog(" -t : TLV decode results\n");
513 PrintAndLog(" -j : load transaction parameters from `emv/defparams.json` file\n");
514 PrintAndLog(" -f : force search AID. Search AID instead of execute PPSE.\n");
515 PrintAndLog(" -v : transaction type - qVSDC or M/Chip.\n");
516 PrintAndLog(" -c : transaction type - qVSDC or M/Chip plus CDA (SDAD generation).\n");
517 PrintAndLog(" -x : transaction type - VSDC. For test only. Not a standart behavior.\n");
518 PrintAndLog(" -g : VISA. generate AC from GPO\n");
519 PrintAndLog("By default : transaction type - MSD.\n");
520 PrintAndLog("Samples:");
521 PrintAndLog(" hf emv exec -s -a -t -> execute MSD transaction");
522 PrintAndLog(" hf emv exec -s -a -t -c -> execute CDA transaction");
526 #define dreturn(n) {free(pdol_data_tlv);tlvdb_free(tlvSelect);tlvdb_free(tlvRoot);DropField();return n;}
528 bool HexToBuffer(const char *errormsg
, const char *hexvalue
, uint8_t * buffer
, size_t maxbufferlen
, size_t *bufferlen
) {
531 switch(param_gethex_to_eol(hexvalue
, 0, buffer
, maxbufferlen
, &buflen
)) {
533 PrintAndLog("%s Invalid HEX value.", errormsg
);
536 PrintAndLog("%s Hex value too large.", errormsg
);
539 PrintAndLog("%s Hex value must have even number of digits.", errormsg
);
543 if (buflen
> maxbufferlen
) {
544 PrintAndLog("%s HEX length (%d) more than %d", errormsg
, *bufferlen
, maxbufferlen
);
553 bool ParamLoadFromJson(struct tlvdb
*tlv
) {
558 PrintAndLog("ERROR load params: tlv tree is NULL.");
562 // current path + file name
563 const char *relfname
= "emv/defparams.json";
564 char fname
[strlen(get_my_executable_directory()) + strlen(relfname
) + 1];
565 strcpy(fname
, get_my_executable_directory());
566 strcat(fname
, relfname
);
568 root
= json_load_file(fname
, 0, &error
);
570 PrintAndLog("Load params: json error on line %d: %s", error
.line
, error
.text
);
574 if (!json_is_array(root
)) {
575 PrintAndLog("Load params: Invalid json format. root must be array.");
579 PrintAndLog("Load params: json OK");
581 for(int i
= 0; i
< json_array_size(root
); i
++) {
582 json_t
*data
, *jtype
, *jlength
, *jvalue
;
584 data
= json_array_get(root
, i
);
585 if(!json_is_object(data
))
587 PrintAndLog("Load params: data [%d] is not an object", i
+ 1);
592 jtype
= json_object_get(data
, "type");
593 if(!json_is_string(jtype
))
595 PrintAndLog("Load params: data [%d] type is not a string", i
+ 1);
599 const char *tlvType
= json_string_value(jtype
);
601 jvalue
= json_object_get(data
, "value");
602 if(!json_is_string(jvalue
))
604 PrintAndLog("Load params: data [%d] value is not a string", i
+ 1);
608 const char *tlvValue
= json_string_value(jvalue
);
610 jlength
= json_object_get(data
, "length");
611 if(!json_is_number(jlength
))
613 PrintAndLog("Load params: data [%d] length is not a number", i
+ 1);
618 int tlvLength
= json_integer_value(jlength
);
619 if (tlvLength
> 250) {
620 PrintAndLog("Load params: data [%d] length more than 250", i
+ 1);
625 PrintAndLog("TLV param: %s[%d]=%s", tlvType
, tlvLength
, tlvValue
);
626 uint8_t buf
[251] = {0};
629 // here max length must be 4, but now tlv_tag_t is 2-byte var. so let it be 2 by now... TODO: needs refactoring tlv_tag_t...
630 if (!HexToBuffer("TLV Error type:", tlvType
, buf
, 2, &buflen
)) {
635 for (int i
= 0; i
< buflen
; i
++) {
636 tag
= (tag
<< 8) + buf
[i
];
639 if (!HexToBuffer("TLV Error value:", tlvValue
, buf
, sizeof(buf
) - 1, &buflen
)) {
644 if (buflen
!= tlvLength
) {
645 PrintAndLog("Load params: data [%d] length of HEX must(%d) be identical to length in TLV param(%d)", i
+ 1, buflen
, tlvLength
);
650 tlvdb_change_or_add_node(tlv
, tag
, tlvLength
, (const unsigned char *)buf
);
658 void ParamLoadDefaults(struct tlvdb
*tlvRoot
) {
659 //9F02:(Amount, authorized (Numeric)) len:6
660 TLV_ADD(0x9F02, "\x00\x00\x00\x00\x01\x00");
661 //9F1A:(Terminal Country Code) len:2
662 TLV_ADD(0x9F1A, "ru");
663 //5F2A:(Transaction Currency Code) len:2
664 // USD 840, EUR 978, RUR 810, RUB 643, RUR 810(old), UAH 980, AZN 031, n/a 999
665 TLV_ADD(0x5F2A, "\x09\x80");
666 //9A:(Transaction Date) len:3
667 TLV_ADD(0x9A, "\x00\x00\x00");
668 //9C:(Transaction Type) len:1 | 00 => Goods and service #01 => Cash
669 TLV_ADD(0x9C, "\x00");
670 // 9F37 Unpredictable Number len:4
671 TLV_ADD(0x9F37, "\x01\x02\x03\x04");
672 // 9F6A Unpredictable Number (MSD for UDOL) len:4
673 TLV_ADD(0x9F6A, "\x01\x02\x03\x04");
674 //9F66:(Terminal Transaction Qualifiers (TTQ)) len:4
675 TLV_ADD(0x9F66, "\x26\x00\x00\x00"); // qVSDC
678 int CmdHFEMVExec(const char *cmd
) {
679 bool activateField
= false;
680 bool showAPDU
= false;
681 bool decodeTLV
= false;
682 bool forceSearch
= false;
683 enum TransactionType TrType
= TT_MSD
;
684 bool GenACGPO
= false;
685 bool paramLoadJSON
= false;
687 uint8_t buf
[APDU_RES_LEN
] = {0};
690 uint8_t AID
[APDU_AID_LEN
] = {0};
692 uint8_t ODAiList
[4096];
693 size_t ODAiListLen
= 0;
697 struct tlvdb
*tlvSelect
= NULL
;
698 struct tlvdb
*tlvRoot
= NULL
;
699 struct tlv
*pdol_data_tlv
= NULL
;
701 if (strlen(cmd
) < 1) {
707 while(param_getchar(cmd
, cmdp
) != 0x00) {
708 char c
= param_getchar(cmd
, cmdp
);
709 if ((c
== '-') && (param_getlength(cmd
, cmdp
) == 2))
710 switch (param_getchar_indx(cmd
, 1, cmdp
)) {
717 activateField
= true;
737 TrType
= TT_QVSDCMCHIP
;
749 paramLoadJSON
= true;
752 PrintAndLog("Unknown parameter '%c'", param_getchar_indx(cmd
, 1, cmdp
));
759 // init applets list tree
760 const char *al
= "Applets list";
761 tlvSelect
= tlvdb_fixed(1, strlen(al
), (const unsigned char *)al
);
763 // Application Selection
764 // https://www.openscdp.org/scripts/tutorial/emv/applicationselection.html
767 PrintAndLog("\n* PPSE.");
768 SetAPDULogging(showAPDU
);
769 res
= EMVSearchPSE(activateField
, true, decodeTLV
, tlvSelect
);
771 // check PPSE and select application id
773 TLVPrintAIDlistFromSelectTLV(tlvSelect
);
774 EMVSelectApplication(tlvSelect
, AID
, &AIDlen
);
780 PrintAndLog("\n* Search AID in list.");
781 SetAPDULogging(false);
782 if (EMVSearch(activateField
, true, decodeTLV
, tlvSelect
)) {
786 // check search and select application id
787 TLVPrintAIDlistFromSelectTLV(tlvSelect
);
788 EMVSelectApplication(tlvSelect
, AID
, &AIDlen
);
792 const char *alr
= "Root terminal TLV tree";
793 tlvRoot
= tlvdb_fixed(1, strlen(alr
), (const unsigned char *)alr
);
795 // check if we found EMV application on card
797 PrintAndLog("Can't select AID. EMV AID not found");
802 PrintAndLog("\n* Selecting AID:%s", sprint_hex_inrow(AID
, AIDlen
));
803 SetAPDULogging(showAPDU
);
804 res
= EMVSelect(false, true, AID
, AIDlen
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
807 PrintAndLog("Can't select AID (%d). Exit...", res
);
812 TLVPrintFromBuffer(buf
, len
);
813 PrintAndLog("* Selected.");
815 PrintAndLog("\n* Init transaction parameters.");
817 ParamLoadDefaults(tlvRoot
);
820 PrintAndLog("* * Transaction parameters loading from JSON...");
821 ParamLoadFromJson(tlvRoot
);
824 //9F66:(Terminal Transaction Qualifiers (TTQ)) len:4
825 char *qVSDC
= "\x26\x00\x00\x00";
827 qVSDC
= "\x26\x80\x00\x00";
831 TLV_ADD(0x9F66, "\x86\x00\x00\x00"); // MSD
833 // not standard for contactless. just for test.
835 TLV_ADD(0x9F66, "\x46\x00\x00\x00"); // VSDC
838 TLV_ADD(0x9F66, qVSDC
); // qVSDC
841 TLV_ADD(0x9F66, qVSDC
); // qVSDC (VISA CDA not enabled)
847 TLVPrintFromTLV(tlvRoot
); // TODO delete!!!
849 PrintAndLog("\n* Calc PDOL.");
850 pdol_data_tlv
= dol_process(tlvdb_get(tlvRoot
, 0x9f38, NULL
), tlvRoot
, 0x83);
852 PrintAndLog("ERROR: can't create PDOL TLV.");
856 size_t pdol_data_tlv_data_len
;
857 unsigned char *pdol_data_tlv_data
= tlv_encode(pdol_data_tlv
, &pdol_data_tlv_data_len
);
858 if (!pdol_data_tlv_data
) {
859 PrintAndLog("ERROR: can't create PDOL data.");
862 PrintAndLog("PDOL data[%d]: %s", pdol_data_tlv_data_len
, sprint_hex(pdol_data_tlv_data
, pdol_data_tlv_data_len
));
864 PrintAndLog("\n* GPO.");
865 res
= EMVGPO(true, pdol_data_tlv_data
, pdol_data_tlv_data_len
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
867 free(pdol_data_tlv_data
);
868 //free(pdol_data_tlv); --- free on exit.
871 PrintAndLog("GPO error(%d): %4x. Exit...", res
, sw
);
875 // process response template format 1 [id:80 2b AIP + x4b AFL] and format 2 [id:77 TLV]
876 if (buf
[0] == 0x80) {
878 PrintAndLog("GPO response format1:");
879 TLVPrintFromBuffer(buf
, len
);
882 if (len
< 4 || (len
- 4) % 4) {
883 PrintAndLog("ERROR: GPO response format1 parsing error. length=%d", len
);
886 struct tlvdb
* f1AIP
= tlvdb_fixed(0x82, 2, buf
+ 2);
887 tlvdb_add(tlvRoot
, f1AIP
);
889 PrintAndLog("\n* * Decode response format 1 (0x80) AIP and AFL:");
890 TLVPrintFromTLV(f1AIP
);
894 struct tlvdb
* f1AFL
= tlvdb_fixed(0x94, len
- 4, buf
+ 2 + 2);
895 tlvdb_add(tlvRoot
, f1AFL
);
897 TLVPrintFromTLV(f1AFL
);
901 TLVPrintFromBuffer(buf
, len
);
904 // extract PAN from track2
906 const struct tlv
*track2
= tlvdb_get(tlvRoot
, 0x57, NULL
);
907 if (!tlvdb_get(tlvRoot
, 0x5a, NULL
) && track2
&& track2
->len
>= 8) {
908 struct tlvdb
*pan
= GetPANFromTrack2(track2
);
910 tlvdb_add(tlvRoot
, pan
);
912 const struct tlv
*pantlv
= tlvdb_get(tlvRoot
, 0x5a, NULL
);
913 PrintAndLog("\n* * Extracted PAN from track2: %s", sprint_hex(pantlv
->value
, pantlv
->len
));
915 PrintAndLog("\n* * WARNING: Can't extract PAN from track2.");
920 PrintAndLog("\n* Read records from AFL.");
921 const struct tlv
*AFL
= tlvdb_get(tlvRoot
, 0x94, NULL
);
922 if (!AFL
|| !AFL
->len
) {
923 PrintAndLog("WARNING: AFL not found.");
926 while(AFL
&& AFL
->len
) {
928 PrintAndLog("ERROR: Wrong AFL length: %d", AFL
->len
);
932 for (int i
= 0; i
< AFL
->len
/ 4; i
++) {
933 uint8_t SFI
= AFL
->value
[i
* 4 + 0] >> 3;
934 uint8_t SFIstart
= AFL
->value
[i
* 4 + 1];
935 uint8_t SFIend
= AFL
->value
[i
* 4 + 2];
936 uint8_t SFIoffline
= AFL
->value
[i
* 4 + 3];
938 PrintAndLog("* * SFI[%02x] start:%02x end:%02x offline:%02x", SFI
, SFIstart
, SFIend
, SFIoffline
);
939 if (SFI
== 0 || SFI
== 31 || SFIstart
== 0 || SFIstart
> SFIend
) {
940 PrintAndLog("SFI ERROR! Skipped...");
944 for(int n
= SFIstart
; n
<= SFIend
; n
++) {
945 PrintAndLog("* * * SFI[%02x] %d", SFI
, n
);
947 res
= EMVReadRecord(true, SFI
, n
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
949 PrintAndLog("ERROR SFI[%02x]. APDU error %4x", SFI
, sw
);
954 TLVPrintFromBuffer(buf
, len
);
958 // Build Input list for Offline Data Authentication
959 // EMV 4.3 book3 10.3, page 96
962 const unsigned char *abuf
= buf
;
965 if (tlv_parse_tl(&abuf
, &elmlen
, &e
)) {
966 memcpy(&ODAiList
[ODAiListLen
], &buf
[len
- elmlen
], elmlen
);
967 ODAiListLen
+= elmlen
;
969 PrintAndLog("ERROR SFI[%02x]. Creating input list for Offline Data Authentication error.", SFI
);
972 memcpy(&ODAiList
[ODAiListLen
], buf
, len
);
982 // copy Input list for Offline Data Authentication
984 struct tlvdb
*oda
= tlvdb_fixed(0x21, ODAiListLen
, ODAiList
); // not a standard tag
985 tlvdb_add(tlvRoot
, oda
);
986 PrintAndLog("* Input list for Offline Data Authentication added to TLV. len=%d \n", ODAiListLen
);
990 const struct tlv
*AIPtlv
= tlvdb_get(tlvRoot
, 0x82, NULL
);
991 uint16_t AIP
= AIPtlv
->value
[0] + AIPtlv
->value
[1] * 0x100;
992 PrintAndLog("* * AIP=%04x", AIP
);
996 PrintAndLog("\n* SDA");
1002 PrintAndLog("\n* DDA");
1003 trDDA(decodeTLV
, tlvRoot
);
1006 // transaction check
1009 if (TrType
== TT_QVSDCMCHIP
|| TrType
== TT_CDA
){
1010 // 9F26: Application Cryptogram
1011 const struct tlv
*AC
= tlvdb_get(tlvRoot
, 0x9F26, NULL
);
1013 PrintAndLog("\n--> qVSDC transaction.");
1014 PrintAndLog("* AC path");
1016 // 9F36: Application Transaction Counter (ATC)
1017 const struct tlv
*ATC
= tlvdb_get(tlvRoot
, 0x9F36, NULL
);
1020 // 9F10: Issuer Application Data - optional
1021 const struct tlv
*IAD
= tlvdb_get(tlvRoot
, 0x9F10, NULL
);
1024 PrintAndLog("ATC: %s", sprint_hex(ATC
->value
, ATC
->len
));
1025 PrintAndLog("AC: %s", sprint_hex(AC
->value
, AC
->len
));
1027 PrintAndLog("IAD: %s", sprint_hex(IAD
->value
, IAD
->len
));
1029 if (IAD
->len
>= IAD
->value
[0] + 1) {
1030 PrintAndLog("\tKey index: 0x%02x", IAD
->value
[1]);
1031 PrintAndLog("\tCrypto ver: 0x%02x(%03d)", IAD
->value
[2], IAD
->value
[2]);
1032 PrintAndLog("\tCVR:", sprint_hex(&IAD
->value
[3], IAD
->value
[0] - 2));
1033 struct tlvdb
* cvr
= tlvdb_fixed(0x20, IAD
->value
[0] - 2, &IAD
->value
[3]);
1034 TLVPrintFromTLVLev(cvr
, 1);
1037 PrintAndLog("WARNING: IAD not found.");
1041 PrintAndLog("ERROR AC: Application Transaction Counter (ATC) not found.");
1046 // Mastercard M/CHIP
1047 if (GetCardPSVendor(AID
, AIDlen
) == CV_MASTERCARD
&& (TrType
== TT_QVSDCMCHIP
|| TrType
== TT_CDA
)){
1048 const struct tlv
*CDOL1
= tlvdb_get(tlvRoot
, 0x8c, NULL
);
1049 if (CDOL1
&& GetCardPSVendor(AID
, AIDlen
) == CV_MASTERCARD
) { // and m/chip transaction flag
1050 PrintAndLog("\n--> Mastercard M/Chip transaction.");
1052 PrintAndLog("* * Generate challenge");
1053 res
= EMVGenerateChallenge(true, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
1055 PrintAndLog("ERROR GetChallenge. APDU error %4x", sw
);
1059 PrintAndLog("ERROR GetChallenge. Wrong challenge length %d", len
);
1063 // ICC Dynamic Number
1064 struct tlvdb
* ICCDynN
= tlvdb_fixed(0x9f4c, len
, buf
);
1065 tlvdb_add(tlvRoot
, ICCDynN
);
1067 PrintAndLog("\n* * ICC Dynamic Number:");
1068 TLVPrintFromTLV(ICCDynN
);
1071 PrintAndLog("* * Calc CDOL1");
1072 struct tlv
*cdol_data_tlv
= dol_process(tlvdb_get(tlvRoot
, 0x8c, NULL
), tlvRoot
, 0x01); // 0x01 - dummy tag
1073 if (!cdol_data_tlv
){
1074 PrintAndLog("ERROR: can't create CDOL1 TLV.");
1077 PrintAndLog("CDOL1 data[%d]: %s", cdol_data_tlv
->len
, sprint_hex(cdol_data_tlv
->value
, cdol_data_tlv
->len
));
1079 PrintAndLog("* * AC1");
1080 // EMVAC_TC + EMVAC_CDAREQ --- to get SDAD
1081 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
);
1084 PrintAndLog("AC1 error(%d): %4x. Exit...", res
, sw
);
1089 TLVPrintFromBuffer(buf
, len
);
1092 PrintAndLog("\n* CDA:");
1093 struct tlvdb
*ac_tlv
= tlvdb_parse_multi(buf
, len
);
1094 res
= trCDA(tlvRoot
, ac_tlv
, pdol_data_tlv
, cdol_data_tlv
);
1096 PrintAndLog("CDA error (%d)", res
);
1099 free(cdol_data_tlv
);
1101 PrintAndLog("\n* M/Chip transaction result:");
1102 // 9F27: Cryptogram Information Data (CID)
1103 const struct tlv
*CID
= tlvdb_get(tlvRoot
, 0x9F27, NULL
);
1105 emv_tag_dump(CID
, stdout
, 0);
1106 PrintAndLog("------------------------------");
1108 switch(CID
->value
[0] & EMVAC_AC_MASK
){
1110 PrintAndLog("Transaction DECLINED.");
1113 PrintAndLog("Transaction approved OFFLINE.");
1116 PrintAndLog("Transaction approved ONLINE.");
1119 PrintAndLog("ERROR: CID transaction code error %2x", CID
->value
[0] & EMVAC_AC_MASK
);
1123 PrintAndLog("ERROR: Wrong CID length %d", CID
->len
);
1126 PrintAndLog("ERROR: CID(9F27) not found.");
1133 if (AIP
& 0x8000 && TrType
== TT_MSD
) {
1134 PrintAndLog("\n--> MSD transaction.");
1136 PrintAndLog("* MSD dCVV path. Check dCVV");
1138 const struct tlv
*track2
= tlvdb_get(tlvRoot
, 0x57, NULL
);
1140 PrintAndLog("Track2: %s", sprint_hex(track2
->value
, track2
->len
));
1142 struct tlvdb
*dCVV
= GetdCVVRawFromTrack2(track2
);
1143 PrintAndLog("dCVV raw data:");
1144 TLVPrintFromTLV(dCVV
);
1146 if (GetCardPSVendor(AID
, AIDlen
) == CV_MASTERCARD
) {
1147 PrintAndLog("\n* Mastercard calculate UDOL");
1150 const struct tlv
*UDOL
= tlvdb_get(tlvRoot
, 0x9F69, NULL
);
1151 // UDOL(9F69) default: 9F6A (Unpredictable number) 4 bytes
1152 const struct tlv defUDOL
= {
1155 .value
= (uint8_t *)"\x9f\x6a\x04",
1158 PrintAndLog("Use default UDOL.");
1160 struct tlv
*udol_data_tlv
= dol_process(UDOL
? UDOL
: &defUDOL
, tlvRoot
, 0x01); // 0x01 - dummy tag
1161 if (!udol_data_tlv
){
1162 PrintAndLog("ERROR: can't create UDOL TLV.");
1166 PrintAndLog("UDOL data[%d]: %s", udol_data_tlv
->len
, sprint_hex(udol_data_tlv
->value
, udol_data_tlv
->len
));
1168 PrintAndLog("\n* Mastercard compute cryptographic checksum(UDOL)");
1170 res
= MSCComputeCryptoChecksum(true, (uint8_t *)udol_data_tlv
->value
, udol_data_tlv
->len
, buf
, sizeof(buf
), &len
, &sw
, tlvRoot
);
1172 PrintAndLog("ERROR Compute Crypto Checksum. APDU error %4x", sw
);
1173 free(udol_data_tlv
);
1178 TLVPrintFromBuffer(buf
, len
);
1181 free(udol_data_tlv
);
1185 PrintAndLog("ERROR MSD: Track2 data not found.");
1193 free(pdol_data_tlv
);
1194 tlvdb_free(tlvSelect
);
1195 tlvdb_free(tlvRoot
);
1197 PrintAndLog("\n* Transaction completed.");
1202 int UsageCmdHFEMVScan(void) {
1203 PrintAndLog("HELP : Scan EMV card and save it contents to a file. \n");
1204 PrintAndLog(" It executes EMV contactless transaction and saves result to a file which can be used for emulation.\n");
1205 PrintAndLog("Usage: hf emv scan [-a][-t][-v][-c][-x][-g] <file_name>\n");
1206 PrintAndLog(" Options:");
1207 PrintAndLog(" -a : show APDU reqests and responses\n");
1208 PrintAndLog(" -t : TLV decode results\n");
1209 PrintAndLog(" -v : transaction type - qVSDC or M/Chip.\n");
1210 PrintAndLog(" -c : transaction type - qVSDC or M/Chip plus CDA (SDAD generation).\n");
1211 PrintAndLog(" -x : transaction type - VSDC. For test only. Not a standart behavior.\n");
1212 PrintAndLog(" -g : VISA. generate AC from GPO\n");
1213 PrintAndLog("By default : transaction type - MSD.\n");
1214 PrintAndLog("Samples:");
1215 PrintAndLog(" hf emv scan -a -t -> scan MSD transaction mode");
1216 PrintAndLog(" hf emv scan -a -t -c -> scan CDA transaction mode");
1220 int CmdHFEMVScan(const char *cmd
) {
1221 UsageCmdHFEMVScan();
1226 int CmdHFEMVTest(const char *cmd
) {
1227 return ExecuteCryptoTests(true);
1230 int CmdHelp(const char *Cmd
);
1231 static command_t CommandTable
[] = {
1232 {"help", CmdHelp
, 1, "This help"},
1233 {"exec", CmdHFEMVExec
, 0, "Executes EMV contactless transaction."},
1234 {"pse", CmdHFEMVPPSE
, 0, "Execute PPSE. It selects 2PAY.SYS.DDF01 or 1PAY.SYS.DDF01 directory."},
1235 {"search", CmdHFEMVSearch
, 0, "Try to select all applets from applets list and print installed applets."},
1236 {"select", CmdHFEMVSelect
, 0, "Select applet."},
1237 {"gpo", CmdHFEMVGPO
, 0, "Execute GetProcessingOptions."},
1238 {"readrec", CmdHFEMVReadRecord
, 0, "Read files from card."},
1239 {"genac", CmdHFEMVAC
, 0, "Generate ApplicationCryptogram."},
1240 {"challenge", CmdHFEMVGenerateChallenge
, 0, "Generate challenge."},
1241 {"intauth", CmdHFEMVInternalAuthenticate
, 0, "Internal authentication."},
1242 // {"scan", CmdHFEMVScan, 0, "Scan EMV card and save it contents to json file for emulator."},
1243 {"test", CmdHFEMVTest
, 0, "Crypto logic test."},
1244 {NULL
, NULL
, 0, NULL
}
1247 int CmdHFEMV(const char *Cmd
) {
1248 CmdsParse(CommandTable
, Cmd
);
1252 int CmdHelp(const char *Cmd
) {
1253 CmdsHelp(CommandTable
);