]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/emv/emvjson.c
e56ecbb71dbe95c0d7f00e38e267fa4182048a24
[proxmark3-svn] / client / emv / emvjson.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 Merlok
3 //
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
6 // the license.
7 //-----------------------------------------------------------------------------
8 // EMV json logic
9 //-----------------------------------------------------------------------------
10
11 #include "emvjson.h"
12 #include <ctype.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <inttypes.h>
16 #include <string.h>
17 #include "util.h"
18 #include "ui.h"
19 #include "proxmark3.h"
20 #include "emv_tags.h"
21
22 static const ApplicationDataElm ApplicationData[] = {
23 {0x82, "AIP"},
24 {0x94, "AFL"},
25
26 {0x5A, "PAN"},
27 {0x5F34, "PANSeqNo"},
28 {0x5F24, "ExpirationDate"},
29 {0x5F25, "EffectiveDate"},
30 {0x5F28, "IssuerCountryCode"},
31
32 {0x50, "ApplicationLabel"},
33 {0x9F08, "VersionNumber"},
34 {0x9F42, "CurrencyCode"},
35 {0x5F2D, "LanguagePreference"},
36 {0x87, "PriorityIndicator"},
37 {0x9F36, "ATC"}, //Application Transaction Counter
38
39 {0x5F20, "CardholderName"},
40
41 {0x9F38, "PDOL"},
42 {0x8C, "CDOL1"},
43 {0x8D, "CDOL2"},
44
45 {0x9F07, "AUC"}, // Application Usage Control
46 {0x9F6C, "CTQ"},
47 {0x8E, "CVMList"},
48 {0x9F0D, "IACDefault"},
49 {0x9F0E, "IACDeny"},
50 {0x9F0F, "IACOnline"},
51
52 {0x8F, "CertificationAuthorityPublicKeyIndex"},
53 {0x9F32, "IssuerPublicKeyExponent"},
54 {0x92, "IssuerPublicKeyRemainder"},
55 {0x90, "IssuerPublicKeyCertificate"},
56 {0x9F47, "ICCPublicKeyExponent"},
57 {0x9F46, "ICCPublicKeyCertificate"},
58
59 {0x00, "end..."}
60 };
61 int ApplicationDataLen = sizeof(ApplicationData) / sizeof(ApplicationDataElm);
62
63 char* GetApplicationDataName(tlv_tag_t tag) {
64 for (int i = 0; i < ApplicationDataLen; i++)
65 if (ApplicationData[i].Tag == tag)
66 return ApplicationData[i].Name;
67
68 return NULL;
69 }
70
71 int JsonSaveJsonObject(json_t *root, char *path, json_t *value) {
72 json_error_t error;
73
74 if (strlen(path) < 1)
75 return 1;
76
77 if (path[0] == '$') {
78 if (json_path_set(root, path, value, 0, &error)) {
79 PrintAndLog("ERROR: can't set json path: ", error.text);
80 return 2;
81 } else {
82 return 0;
83 }
84 } else {
85 return json_object_set_new(root, path, value);
86 }
87 }
88
89 int JsonSaveInt(json_t *root, char *path, int value) {
90 return JsonSaveJsonObject(root, path, json_integer(value));
91 }
92
93 int JsonSaveStr(json_t *root, char *path, char *value) {
94 return JsonSaveJsonObject(root, path, json_string(value));
95 };
96
97 int JsonSaveBufAsHexCompact(json_t *elm, char *path, uint8_t *data, size_t datalen) {
98 char * msg = sprint_hex_inrow(data, datalen);
99 if (msg && strlen(msg) && msg[strlen(msg) - 1] == ' ')
100 msg[strlen(msg) - 1] = '\0';
101
102 return JsonSaveStr(elm, path, msg);
103 }
104
105 int JsonSaveBufAsHex(json_t *elm, char *path, uint8_t *data, size_t datalen) {
106 char * msg = sprint_hex(data, datalen);
107 if (msg && strlen(msg) && msg[strlen(msg) - 1] == ' ')
108 msg[strlen(msg) - 1] = '\0';
109
110 return JsonSaveStr(elm, path, msg);
111 }
112
113 int JsonSaveHex(json_t *elm, char *path, uint64_t data, int datalen) {
114 uint8_t bdata[8] = {0};
115 int len = 0;
116 if (!datalen) {
117 for (uint64_t u = 0xffffffffffffffff; u; u = u << 8) {
118 if (!(data & u)) {
119 break;
120 }
121 len++;
122 }
123 if (!len)
124 len = 1;
125 } else {
126 len = datalen;
127 }
128 num_to_bytes(data, len, bdata);
129
130 return JsonSaveBufAsHex(elm, path, bdata, len);
131 }
132
133 int JsonSaveTLVValue(json_t *root, char *path, struct tlvdb *tlvdbelm) {
134 const struct tlv *tlvelm = tlvdb_get_tlv(tlvdbelm);
135 if (tlvelm)
136 return JsonSaveBufAsHex(root, path, (uint8_t *)tlvelm->value, tlvelm->len);
137 else
138 return 1;
139 }
140
141 int JsonSaveTLVElm(json_t *elm, char *path, struct tlv *tlvelm, bool saveName, bool saveValue, bool saveAppDataLink) {
142 json_error_t error;
143
144 if (strlen(path) < 1 || !tlvelm)
145 return 1;
146
147 if (path[0] == '$') {
148
149 json_t *obj = json_path_get(elm, path);
150 if (!obj) {
151 obj = json_object();
152
153 if (json_is_array(elm)) {
154 if (json_array_append_new(elm, obj)) {
155 PrintAndLog("ERROR: can't append array: %s", path);
156 return 2;
157 }
158 } else {
159 if (json_path_set(elm, path, obj, 0, &error)) {
160 PrintAndLog("ERROR: can't set json path: ", error.text);
161 return 2;
162 }
163 }
164 }
165
166 if (saveAppDataLink) {
167 char * AppDataName = GetApplicationDataName(tlvelm->tag);
168 if (AppDataName)
169 JsonSaveStr(obj, "appdata", AppDataName);
170 } else {
171 char * name = emv_get_tag_name(tlvelm);
172 if (saveName && name && strlen(name) > 0 && strncmp(name, "Unknown", 7))
173 JsonSaveStr(obj, "name", emv_get_tag_name(tlvelm));
174 JsonSaveHex(obj, "tag", tlvelm->tag, 0);
175 if (saveValue) {
176 JsonSaveHex(obj, "length", tlvelm->len, 0);
177 JsonSaveBufAsHex(obj, "value", (uint8_t *)tlvelm->value, tlvelm->len);
178 };
179 }
180 }
181
182 return 0;
183 }
184
185 int JsonSaveTLVTreeElm(json_t *elm, char *path, struct tlvdb *tlvdbelm, bool saveName, bool saveValue, bool saveAppDataLink) {
186 return JsonSaveTLVElm(elm, path, (struct tlv *)tlvdb_get_tlv(tlvdbelm), saveName, saveValue, saveAppDataLink);
187 }
188
189 int JsonSaveTLVTree(json_t *root, json_t *elm, char *path, struct tlvdb *tlvdbelm) {
190 struct tlvdb *tlvp = tlvdbelm;
191 while (tlvp) {
192 const struct tlv * tlvpelm = tlvdb_get_tlv(tlvp);
193 char * AppDataName = NULL;
194 if (tlvpelm)
195 AppDataName = GetApplicationDataName(tlvpelm->tag);
196
197 if (AppDataName) {
198 char appdatalink[200] = {0};
199 sprintf(appdatalink, "$.ApplicationData.%s", AppDataName);
200 JsonSaveBufAsHex(root, appdatalink, (uint8_t *)tlvpelm->value, tlvpelm->len);
201 }
202
203 json_t *pelm = json_path_get(elm, path);
204 if (pelm && json_is_array(pelm)) {
205 json_t *appendelm = json_object();
206 json_array_append_new(pelm, appendelm);
207 JsonSaveTLVTreeElm(appendelm, "$", tlvp, !AppDataName, !tlvdb_elm_get_children(tlvp), AppDataName);
208 pelm = appendelm;
209 } else {
210 JsonSaveTLVTreeElm(elm, path, tlvp, !AppDataName, !tlvdb_elm_get_children(tlvp), AppDataName);
211 pelm = json_path_get(elm, path);
212 }
213
214 if (tlvdb_elm_get_children(tlvp)) {
215 // get path element
216 if(!pelm)
217 return 1;
218
219 // check childs element and add it if not found
220 json_t *chjson = json_path_get(pelm, "$.Childs");
221 if (!chjson) {
222 json_object_set_new(pelm, "Childs", json_array());
223
224 chjson = json_path_get(pelm, "$.Childs");
225 }
226
227 // check
228 if (!json_is_array(chjson)) {
229 PrintAndLog("E->Internal logic error. `$.Childs` is not an array.");
230 break;
231 }
232
233 // Recursion
234 JsonSaveTLVTree(root, chjson, "$", tlvdb_elm_get_children(tlvp));
235 }
236
237 tlvp = tlvdb_elm_get_next(tlvp);
238 }
239 return 0;
240 }
241
242 bool HexToBuffer(const char *errormsg, const char *hexvalue, uint8_t * buffer, size_t maxbufferlen, size_t *bufferlen) {
243 int buflen = 0;
244
245 switch(param_gethex_to_eol(hexvalue, 0, buffer, maxbufferlen, &buflen)) {
246 case 1:
247 PrintAndLog("%s Invalid HEX value.", errormsg);
248 return false;
249 case 2:
250 PrintAndLog("%s Hex value too large.", errormsg);
251 return false;
252 case 3:
253 PrintAndLog("%s Hex value must have even number of digits.", errormsg);
254 return false;
255 }
256
257 if (buflen > maxbufferlen) {
258 PrintAndLog("%s HEX length (%d) more than %d", errormsg, *bufferlen, maxbufferlen);
259 return false;
260 }
261
262 *bufferlen = buflen;
263
264 return true;
265 }
266
267 int JsonLoadBufAsHex(json_t *elm, char *path, uint8_t *data, size_t maxbufferlen, size_t *datalen) {
268 if (datalen)
269 *datalen = 0;
270
271 json_t *jelm = json_path_get((const json_t *)elm, path);
272 if (!jelm || !json_is_string(jelm))
273 return 1;
274
275 if (!HexToBuffer("ERROR load", json_string_value(jelm), data, maxbufferlen, datalen))
276 return 2;
277
278 return 0;
279 };
280
281 bool ParamLoadFromJson(struct tlvdb *tlv) {
282 json_t *root;
283 json_error_t error;
284
285 if (!tlv) {
286 PrintAndLog("ERROR load params: tlv tree is NULL.");
287 return false;
288 }
289
290 // current path + file name
291 const char *relfname = "emv/defparams.json";
292 char fname[strlen(get_my_executable_directory()) + strlen(relfname) + 1];
293 strcpy(fname, get_my_executable_directory());
294 strcat(fname, relfname);
295
296 root = json_load_file(fname, 0, &error);
297 if (!root) {
298 PrintAndLog("Load params: json error on line %d: %s", error.line, error.text);
299 return false;
300 }
301
302 if (!json_is_array(root)) {
303 PrintAndLog("Load params: Invalid json format. root must be array.");
304 return false;
305 }
306
307 PrintAndLog("Load params: json(%d) OK", json_array_size(root));
308
309 for(int i = 0; i < json_array_size(root); i++) {
310 json_t *data, *jtag, *jlength, *jvalue;
311
312 data = json_array_get(root, i);
313 if(!json_is_object(data))
314 {
315 PrintAndLog("Load params: data [%d] is not an object", i + 1);
316 json_decref(root);
317 return false;
318 }
319
320 jtag = json_object_get(data, "tag");
321 if(!json_is_string(jtag))
322 {
323 PrintAndLog("Load params: data [%d] tag is not a string", i + 1);
324 json_decref(root);
325 return false;
326 }
327 const char *tlvTag = json_string_value(jtag);
328
329 jvalue = json_object_get(data, "value");
330 if(!json_is_string(jvalue))
331 {
332 PrintAndLog("Load params: data [%d] value is not a string", i + 1);
333 json_decref(root);
334 return false;
335 }
336 const char *tlvValue = json_string_value(jvalue);
337
338 jlength = json_object_get(data, "length");
339 if(!json_is_number(jlength))
340 {
341 PrintAndLog("Load params: data [%d] length is not a number", i + 1);
342 json_decref(root);
343 return false;
344 }
345
346 int tlvLength = json_integer_value(jlength);
347 if (tlvLength > 250) {
348 PrintAndLog("Load params: data [%d] length more than 250", i + 1);
349 json_decref(root);
350 return false;
351 }
352
353 PrintAndLog("TLV param: %s[%d]=%s", tlvTag, tlvLength, tlvValue);
354 uint8_t buf[251] = {0};
355 size_t buflen = 0;
356
357 // 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...
358 if (!HexToBuffer("TLV Error type:", tlvTag, buf, 2, &buflen)) {
359 json_decref(root);
360 return false;
361 }
362 tlv_tag_t tag = 0;
363 for (int i = 0; i < buflen; i++) {
364 tag = (tag << 8) + buf[i];
365 }
366
367 if (!HexToBuffer("TLV Error value:", tlvValue, buf, sizeof(buf) - 1, &buflen)) {
368 json_decref(root);
369 return false;
370 }
371
372 if (buflen != tlvLength) {
373 PrintAndLog("Load params: data [%d] length of HEX must(%d) be identical to length in TLV param(%d)", i + 1, buflen, tlvLength);
374 json_decref(root);
375 return false;
376 }
377
378 tlvdb_change_or_add_node(tlv, tag, tlvLength, (const unsigned char *)buf);
379 }
380
381 json_decref(root);
382
383 return true;
384 }
385
Impressum, Datenschutz