]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfemv.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2014 Peter Fillmore
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
9 // High frequency EMV commands
10 //-----------------------------------------------------------------------------
13 static int CmdHelp(const char *Cmd
);
15 int usage_hf_emv_test(void){
16 PrintAndLog("EMV test ");
17 PrintAndLog("Usage: hf emv test [h]");
18 PrintAndLog("Options:");
19 PrintAndLog(" h : this help");
21 PrintAndLog("Samples:");
22 PrintAndLog(" hf emv test");
25 int usage_hf_emv_readrecord(void){
26 PrintAndLog("Read a EMV record ");
27 PrintAndLog("Usage: hf emv readrecord [h] <records> <sfi>");
28 PrintAndLog("Options:");
29 PrintAndLog(" h : this help");
30 PrintAndLog(" <records> : number of records");
31 PrintAndLog(" <sfi> : number of SFI records");
33 PrintAndLog("Samples:");
34 PrintAndLog(" hf emv readrecord 1 1");
37 int usage_hf_emv_clone(void){
38 PrintAndLog("Usage: hf emv clone [h] <records> <SFI> ");
39 PrintAndLog("Options:");
40 PrintAndLog(" h : this help");
41 PrintAndLog(" <records> : number of records");
42 PrintAndLog(" <sfi> : number of SFI records");
44 PrintAndLog("Samples:");
45 PrintAndLog(" hf emv clone 10 10");
48 int usage_hf_emv_transaction(void){
49 PrintAndLog("Performs EMV reader transaction");
50 PrintAndLog("Usage: hf emv trans [h]");
51 PrintAndLog("Options:");
52 PrintAndLog(" h : this help");
54 PrintAndLog("Samples:");
55 PrintAndLog(" hf emv trans");
58 int usage_hf_emv_getrnd(void){
59 PrintAndLog("retrieve the UN number from a terminal");
60 PrintAndLog("Usage: hf emv getrnd [h]");
61 PrintAndLog("Options:");
62 PrintAndLog(" h : this help");
64 PrintAndLog("Samples:");
65 PrintAndLog(" hf emv getrnd");
68 int usage_hf_emv_eload(void){
69 PrintAndLog("set EMV tags in the device to use in a transaction");
70 PrintAndLog("Usage: hf emv eload [h] o <filename w/o .bin>");
71 PrintAndLog("Options:");
72 PrintAndLog(" h : this help");
73 PrintAndLog(" o <filename> : filename w/o '.bin'");
75 PrintAndLog("Samples:");
76 PrintAndLog(" hf emv eload o myfile");
79 int usage_hf_emv_dump(void){
80 PrintAndLog("Gets EMV contactless tag values.");
81 PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`");
82 PrintAndLog("Usage: hf emv dump [h] o <filename w/o .bin>");
83 PrintAndLog("Options:");
84 PrintAndLog(" h : this help");
85 PrintAndLog(" o <filename> : filename w/o '.bin' to dump bytes");
87 PrintAndLog("Samples:");
88 PrintAndLog(" hf emv dump");
89 PrintAndLog(" hf emv dump o myfile");
92 int usage_hf_emv_sim(void){
93 PrintAndLog("Simulates a EMV contactless card");
94 PrintAndLog("Usage: hf emv sim [h]");
95 PrintAndLog("Options:");
96 PrintAndLog(" h : this help");
98 PrintAndLog("Samples:");
99 PrintAndLog(" hf emv sim");
103 int CmdHfEmvTest(const char *Cmd
) {
104 char cmdp
= param_getchar(Cmd
, 0);
105 if ( cmdp
== 'h' || cmdp
== 'H') return usage_hf_emv_test();
107 UsbCommand c
= {CMD_EMV_TEST
, {0, 0, 0}};
108 clearCommandBuffer();
111 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
112 PrintAndLog("Command execute time-out");
115 uint8_t isOK
= resp
.arg
[0] & 0xff;
116 PrintAndLog("isOk: %02x", isOK
);
120 int CmdHfEmvReadRecord(const char *Cmd
) {
121 char cmdp
= param_getchar(Cmd
, 0);
122 if ((strlen(Cmd
)<3) || cmdp
== 'h' || cmdp
== 'H') return usage_hf_emv_readrecord();
124 uint8_t record
= param_get8(Cmd
, 0);
125 uint8_t sfi
= param_getchar(Cmd
, 1);
127 PrintAndLog("Record must be less than 32");
130 PrintAndLog("--record no:%02x SFI:%02x ", record
, sfi
);
132 UsbCommand c
= {CMD_EMV_READ_RECORD
, {record
, sfi
, 0}};
133 clearCommandBuffer();
136 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
137 PrintAndLog("Command execute timeout");
140 uint8_t isOK
= resp
.arg
[0] & 0xff;
141 PrintAndLog("isOk:%02x", isOK
);
145 int CmdHfEmvClone(const char *Cmd
) {
146 char cmdp
= param_getchar(Cmd
, 0);
147 if ((strlen(Cmd
)<3) || cmdp
== 'h' || cmdp
== 'H') return usage_hf_emv_clone();
149 uint8_t record
= param_get8(Cmd
, 0);
150 uint8_t sfi
= param_get8(Cmd
, 1);
152 PrintAndLog("Record must be less than 32");
155 UsbCommand c
= {CMD_EMV_CLONE
, {sfi
, record
, 0}};
156 clearCommandBuffer();
159 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
160 PrintAndLog("Command execute timeout");
163 uint8_t isOK
= resp
.arg
[0] & 0xff;
164 PrintAndLog("isOk:%02x", isOK
);
168 int CmdHfEmvTrans(const char *Cmd
) {
169 char cmdp
= param_getchar(Cmd
, 0);
170 if ( cmdp
== 'h' || cmdp
== 'H') return usage_hf_emv_transaction();
172 UsbCommand c
= {CMD_EMV_TRANSACTION
, {0, 0, 0}};
173 clearCommandBuffer();
176 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 5000)) {
177 PrintAndLog("Command execute time-out");
180 uint8_t isOK
= resp
.arg
[0] & 0xff;
181 PrintAndLog("isOk: %02x", isOK
);
182 print_hex_break(resp
.d
.asBytes
, 512, 32);
185 //retrieve the UN number from a terminal
186 int CmdHfEmvGetrng(const char *Cmd
) {
187 char cmdp
= param_getchar(Cmd
, 0);
188 if ( cmdp
== 'h' || cmdp
== 'H') return usage_hf_emv_getrnd();
189 UsbCommand c
= {CMD_EMV_GET_RANDOM_NUM
, {0, 0, 0}};
190 clearCommandBuffer();
194 //Load a dumped EMV tag on to emulator memory
195 int CmdHfEmvELoad(const char *Cmd
) {
197 char filename
[FILE_PATH_SIZE
];
198 char *fnameptr
= filename
;
203 while(param_getchar(Cmd
, cmdp
) != 0x00) {
204 switch(param_getchar(Cmd
, cmdp
)) {
207 return usage_hf_emv_eload();
210 len
= param_getstr(Cmd
, cmdp
+1, filename
);
213 if (len
> FILE_PATH_SIZE
-5)
214 len
= FILE_PATH_SIZE
-5;
215 sprintf(fnameptr
+ len
,".bin");
219 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
227 if(errors
) return usage_hf_emv_eload();
230 f
= fopen(filename
,"r");
232 PrintAndLog("File %s not found or locked", filename
);
240 UsbCommand c
= {CMD_EMV_LOAD_VALUE
, {0,0,0}};
242 // transfer to device
243 while (fgets(line
, sizeof (line
), f
)) {
244 printf("LINE = %s\n", line
);
246 token
= strtok(line
, ":");
247 tag
= (uint16_t)strtol(token
, NULL
, 0);
248 token
= strtok(NULL
,"");
251 memcpy(c
.d
.asBytes
, token
, strlen(token
));
253 clearCommandBuffer();
256 printf("Loaded TAG = %04x\n", tag
);
257 printf("Loaded VALUE = %s\n", token
);
261 PrintAndLog("loaded %s", filename
);
262 //PrintAndLog("\nLoaded %d bytes from file: %s to emulator memory", numofbytes, filename);
266 int CmdHfEmvDump(const char *Cmd
){
270 while(param_getchar(Cmd
, cmdp
) != 0x00) {
271 switch(param_getchar(Cmd
, cmdp
)) {
274 return usage_hf_emv_dump();
276 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
284 if(errors
) return usage_hf_emv_dump();
286 UsbCommand c
= {CMD_EMV_DUMP_CARD
, {0, 0, 0}};
287 clearCommandBuffer();
290 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 3000)) {
291 PrintAndLog("Command execute time-out");
297 int CmdHfEmvSim(const char *Cmd
) {
301 while(param_getchar(Cmd
, cmdp
) != 0x00) {
302 switch(param_getchar(Cmd
, cmdp
)) {
305 return usage_hf_emv_sim();
307 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
315 if(errors
) return usage_hf_emv_sim();
317 UsbCommand c
= {CMD_EMV_SIM
, {0,0,0}};
318 clearCommandBuffer();
321 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
322 PrintAndLog("Command execute time-out");
325 uint8_t isOK
= resp
.arg
[0] & 0xff;
326 PrintAndLog("isOk:%02x", isOK
);
330 int CmdHfEmvList(const char *Cmd
) {
331 return CmdHFList("7816");
334 static command_t CommandTable
[] = {
335 {"help", CmdHelp
, 1, "This help"},
336 {"readrecord", CmdHfEmvReadRecord
, 0, "EMV Read Record"},
337 {"transaction", CmdHfEmvTrans
, 0, "Perform EMV Transaction"},
338 {"getrng", CmdHfEmvGetrng
, 0, "get random number from terminal"},
339 {"eload", CmdHfEmvELoad
, 0, "load EMV tag into device"},
340 {"dump", CmdHfEmvDump
, 0, "dump EMV tag values"},
341 {"sim", CmdHfEmvSim
, 0, "simulate EMV tag"},
342 {"clone", CmdHfEmvClone
, 0, "clone an EMV tag"},
343 {"list", CmdHfEmvList
, 0, "[Deprecated] List ISO7816 history"},
344 {"test", CmdHfEmvTest
, 0, "Test Function"},
345 {NULL
, NULL
, 0, NULL
}
348 int CmdHFEmv(const char *Cmd
) {
349 clearCommandBuffer();
350 CmdsParse(CommandTable
, Cmd
);
354 int CmdHelp(const char *Cmd
) {
355 CmdsHelp(CommandTable
);