]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhfemv.c
CHG: `lf em` - some minor refactoring in 4x50 commands
[proxmark3-svn] / client / cmdhfemv.c
CommitLineData
6a1687cc 1//-----------------------------------------------------------------------------
2// Copyright (C) 2014 Peter Fillmore
3// 2017 iceman
4//
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
7// the license.
8//-----------------------------------------------------------------------------
9// High frequency EMV commands
10//-----------------------------------------------------------------------------
11#include "cmdhfemv.h"
12
13static int CmdHelp(const char *Cmd);
14
15int usage_hf_emv_trans(void){
16 PrintAndLog("perform an EMV transaction");
17 PrintAndLog("Usage: hf emv trans [h]");
18 PrintAndLog("Options:");
19 PrintAndLog(" h : this help");
20 PrintAndLog("");
21 PrintAndLog("Samples:");
22 PrintAndLog(" hf emv trans");
23 return 0;
24}
25int usage_hf_emv_getrnd(void){
26 PrintAndLog("retrieve the UN number from a terminal");
27 PrintAndLog("Usage: hf emv getrnd [h]");
28 PrintAndLog("Options:");
29 PrintAndLog(" h : this help");
30 PrintAndLog("");
31 PrintAndLog("Samples:");
32 PrintAndLog(" hf emv getrnd");
33 return 0;
34}
35int usage_hf_emv_eload(void){
36 PrintAndLog("set EMV tags in the device to use in a transaction");
37 PrintAndLog("Usage: hf emv eload [h] o <filename w/o .bin>");
38 PrintAndLog("Options:");
39 PrintAndLog(" h : this help");
40 PrintAndLog(" o <filename> : filename w/o '.bin'");
41 PrintAndLog("");
42 PrintAndLog("Samples:");
43 PrintAndLog(" hf emv eload o myfile");
44 return 0;
45}
46int usage_hf_emv_sim(void){
47 PrintAndLog("Simulates a EMV contactless card");
48 PrintAndLog("Usage: hf emv sim [h]");
49 PrintAndLog("Options:");
50 PrintAndLog(" h : this help");
51 PrintAndLog("");
52 PrintAndLog("Samples:");
53 PrintAndLog(" hf emv sim");
54 return 0;
55}
56int usage_hf_emv_dump(void){
57 PrintAndLog("Gets EMV contactless tag values.");
58 PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`");
59 PrintAndLog("Usage: hf emv dump [h] o <filename w/o .bin>");
60 PrintAndLog("Options:");
61 PrintAndLog(" h : this help");
62 PrintAndLog(" o <filename> : filename w/o '.bin' to dump bytes");
63 PrintAndLog("");
64 PrintAndLog("Samples:");
65 PrintAndLog(" hf emv dump");
66 PrintAndLog(" hf emv dump o myfile");
67 return 0;
68}
69
70//perform an EMV transaction
71int CmdHfEmvTrans(const char *Cmd) {
72 char cmdp = param_getchar(Cmd, 0);
73 if ( cmdp == 'h' || cmdp == 'H') return usage_hf_emv_trans();
74 UsbCommand c = {CMD_EMV_TRANSACTION, {0, 0, 0}};
75 clearCommandBuffer();
76 SendCommand(&c);
77 return 0;
78}
79//retrieve the UN number from a terminal
80int CmdHfEmvGetrng(const char *Cmd) {
81 char cmdp = param_getchar(Cmd, 0);
82 if ( cmdp == 'h' || cmdp == 'H') return usage_hf_emv_getrnd();
83 UsbCommand c = {CMD_EMV_GET_RANDOM_NUM, {0, 0, 0}};
84 clearCommandBuffer();
85 SendCommand(&c);
86 return 0;
87}
88
89//set EMV tags in the device to use in a transaction
90int CmdHfEmvELoad(const char *Cmd) {
91 FILE * f;
92 char filename[FILE_PATH_SIZE];
93 char *fnameptr = filename;
94 int len;
95 bool errors = false;
96 uint8_t cmdp = 0;
97
98 while(param_getchar(Cmd, cmdp) != 0x00) {
99 switch(param_getchar(Cmd, cmdp)) {
100 case 'h':
101 case 'H':
102 return usage_hf_emv_eload();
103 case 'o':
104 case 'O':
105 len = param_getstr(Cmd, cmdp+1, filename);
106 if (!len)
107 errors = true;
108 if (len > FILE_PATH_SIZE-5)
109 len = FILE_PATH_SIZE-5;
110 sprintf(fnameptr + len,".bin");
111 cmdp += 2;
112 break;
113 default:
114 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
115 errors = true;
116 break;
117 }
118 if(errors) break;
119 }
120
121 //Validations
122 if(errors) return usage_hf_emv_eload();
123
124 // open file
125 f = fopen(filename,"r");
126 if (!f) {
127 PrintAndLog("File %s not found or locked", filename);
128 return 1;
129 }
130
131 char line[512];
132 char *token;
133 uint16_t tag;
134
135 UsbCommand c = {CMD_EMV_LOAD_VALUE, {0,0,0}};
136
137 // transfer to device
138 while (fgets(line, sizeof (line), f)) {
139 printf("LINE = %s\n", line);
140
141 token = strtok(line, ":");
142 tag = (uint16_t)strtol(token, NULL, 0);
143 token = strtok(NULL,"");
144
145 c.arg[0] = tag;
146 memcpy(c.d.asBytes, token, strlen(token));
147
148 clearCommandBuffer();
149 SendCommand(&c);
150
151 printf("Loaded TAG = %04x\n", tag);
152 printf("Loaded VALUE = %s\n", token);
153 }
154
155 fclose(f);
156 PrintAndLog("loaded %s", filename);
157 //PrintAndLog("\nLoaded %d bytes from file: %s to emulator memory", numofbytes, filename);
158 return 0;
159}
160
161int CmdHfEmvDump(const char *Cmd){
162
163 bool errors = false;
164 uint8_t cmdp = 0;
165
166 while(param_getchar(Cmd, cmdp) != 0x00) {
167 switch(param_getchar(Cmd, cmdp)) {
168 case 'h':
169 case 'H':
170 return usage_hf_emv_dump();
171 default:
172 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
173 errors = true;
174 break;
175 }
176 if(errors) break;
177 }
178
179 //Validations
180 if(errors) return usage_hf_emv_dump();
181
182 UsbCommand c = {CMD_EMV_DUMP_CARD, {0, 0, 0}};
183 clearCommandBuffer();
184 SendCommand(&c);
185 UsbCommand resp;
186 if (!WaitForResponseTimeout(CMD_ACK, &resp, 3000)) {
187 PrintAndLog("Command execute time-out");
188 return 1;
189 }
190 return 0;
191}
192
193
194/*
195int CmdHfEmvSim(const char *Cmd) {
196
197 bool errors = false;
198 uint8_t cmdp = 0;
199
200 while(param_getchar(Cmd, cmdp) != 0x00) {
201 switch(param_getchar(Cmd, cmdp)) {
202 case 'h':
203 case 'H':
204 return usage_hf_emv_sim();
205 default:
206 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
207 errors = true;
208 break;
209 }
210 if(errors) break;
211 }
212
213 //Validations
214 if(errors) return usage_hf_emv_sim();
215
216 UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}};
217 sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]);
218 clearCommandBuffer();
219 SendCommand(&c);
220 return 0;
221}
222*/
223
224int CmdHfEmvList(const char *Cmd) {
225 CmdHFList("7816");
226 return 0;
227}
228
229static command_t CommandTable[] = {
230 {"help", CmdHelp, 1, "This help"},
231 {"trans", CmdHfEmvTrans, 0, "Perform EMV Reader Transaction"},
232 {"getrng", CmdHfEmvGetrng, 0, "get random number from terminal"},
233 {"eload", CmdHfEmvELoad, 0, "load EMV tag into device"},
234 {"dump", CmdHfEmvDump, 0, "Dump EMV tag values"},
235// {"sim", CmdHfEmvSim, 0, "Start tag simulator"},
236 {"list", CmdHfEmvList, 1, "[Deprecated] List ISO7816 history"},
237 {NULL, NULL, 0, NULL}
238};
239
240int CmdHFEmv(const char *Cmd) {
241 clearCommandBuffer();
242 CmdsParse(CommandTable, Cmd);
243 return 0;
244}
245
246int CmdHelp(const char *Cmd) {
247 CmdsHelp(CommandTable);
248 return 0;
249}
Impressum, Datenschutz