1 //-----------------------------------------------------------------------------
2 // Copyright (C) 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 "commonutil.h" // ARRAYLEN
16 #include "proxmark3.h"
18 #include "fileutils.h"
21 static const ApplicationDataElm ApplicationData
[] = {
27 {0x5F24, "ExpirationDate"},
28 {0x5F25, "EffectiveDate"},
29 {0x5F28, "IssuerCountryCode"},
31 {0x50, "ApplicationLabel"},
32 {0x9F08, "VersionNumber"},
33 {0x9F42, "CurrencyCode"},
34 {0x5F2D, "LanguagePreference"},
35 {0x87, "PriorityIndicator"},
36 {0x9F36, "ATC"}, //Application Transaction Counter
38 {0x5F20, "CardholderName"},
44 {0x9F07, "AUC"}, // Application Usage Control
47 {0x9F0D, "IACDefault"},
49 {0x9F0F, "IACOnline"},
51 {0x8F, "CertificationAuthorityPublicKeyIndex"},
52 {0x9F32, "IssuerPublicKeyExponent"},
53 {0x92, "IssuerPublicKeyRemainder"},
54 {0x90, "IssuerPublicKeyCertificate"},
55 {0x9F47, "ICCPublicKeyExponent"},
56 {0x9F46, "ICCPublicKeyCertificate"},
61 const char *GetApplicationDataName(tlv_tag_t tag
) {
62 for (int i
= 0; i
< ARRAYLEN(ApplicationData
); i
++)
63 if (ApplicationData
[i
].Tag
== tag
)
64 return ApplicationData
[i
].Name
;
69 int JsonSaveJsonObject(json_t
*root
, const char *path
, json_t
*value
) {
76 if (json_path_set_new(root
, path
, value
, 0, &error
)) {
77 PrintAndLogEx(ERR
, "ERROR: can't set json path: %s", error
.text
);
83 return json_object_set_new(root
, path
, value
);
87 int JsonSaveInt(json_t
*root
, const char *path
, int value
) {
88 return JsonSaveJsonObject(root
, path
, json_integer(value
));
91 int JsonSaveStr(json_t
*root
, const char *path
, const char *value
) {
92 return JsonSaveJsonObject(root
, path
, json_string(value
));
95 int JsonSaveBoolean(json_t
*root
, const char *path
, bool value
) {
96 return JsonSaveJsonObject(root
, path
, json_boolean(value
));
99 int JsonSaveBufAsHexCompact(json_t
*elm
, const char *path
, uint8_t *data
, size_t datalen
) {
100 char *msg
= sprint_hex_inrow(data
, datalen
);
101 if (msg
&& strlen(msg
) && msg
[strlen(msg
) - 1] == ' ')
102 msg
[strlen(msg
) - 1] = '\0';
104 return JsonSaveStr(elm
, path
, msg
);
107 int JsonSaveBufAsHex(json_t
*elm
, const char *path
, uint8_t *data
, size_t datalen
) {
108 char *msg
= sprint_hex(data
, datalen
);
109 if (msg
&& strlen(msg
) && msg
[strlen(msg
) - 1] == ' ')
110 msg
[strlen(msg
) - 1] = '\0';
112 return JsonSaveStr(elm
, path
, msg
);
115 int JsonSaveHex(json_t
*elm
, const char *path
, uint64_t data
, int datalen
) {
116 uint8_t bdata
[8] = {0};
119 for (uint64_t u
= 0xffffffffffffffff; u
; u
= u
<< 8) {
130 num_to_bytes(data
, len
, bdata
);
132 return JsonSaveBufAsHex(elm
, path
, bdata
, len
);
135 int JsonSaveTLVValue(json_t
*root
, const char *path
, struct tlvdb
*tlvdbelm
) {
136 const struct tlv
*tlvelm
= tlvdb_get_tlv(tlvdbelm
);
138 return JsonSaveBufAsHex(root
, path
, (uint8_t *)tlvelm
->value
, tlvelm
->len
);
143 int JsonSaveTLVElm(json_t
*elm
, const char *path
, struct tlv
*tlvelm
, bool saveName
, bool saveValue
, bool saveAppDataLink
) {
146 if (strlen(path
) < 1 || !tlvelm
)
149 if (path
[0] == '$') {
151 json_t
*obj
= json_path_get(elm
, path
);
155 if (json_is_array(elm
)) {
156 if (json_array_append_new(elm
, obj
)) {
157 PrintAndLogEx(ERR
, "ERROR: can't append array: %s", path
);
161 if (json_path_set(elm
, path
, obj
, 0, &error
)) {
162 PrintAndLogEx(ERR
, "ERROR: can't set json path: %s", error
.text
);
168 if (saveAppDataLink
) {
169 const char *AppDataName
= GetApplicationDataName(tlvelm
->tag
);
171 JsonSaveStr(obj
, "appdata", AppDataName
);
173 const char *name
= emv_get_tag_name(tlvelm
);
174 if (saveName
&& name
&& strlen(name
) > 0 && strncmp(name
, "Unknown", 7))
175 JsonSaveStr(obj
, "name", emv_get_tag_name(tlvelm
));
176 JsonSaveHex(obj
, "tag", tlvelm
->tag
, 0);
178 JsonSaveHex(obj
, "length", tlvelm
->len
, 0);
179 JsonSaveBufAsHex(obj
, "value", (uint8_t *)tlvelm
->value
, tlvelm
->len
);
187 int JsonSaveTLVTreeElm(json_t
*elm
, const char *path
, struct tlvdb
*tlvdbelm
, bool saveName
, bool saveValue
, bool saveAppDataLink
) {
188 return JsonSaveTLVElm(elm
, path
, (struct tlv
*)tlvdb_get_tlv(tlvdbelm
), saveName
, saveValue
, saveAppDataLink
);
191 int JsonSaveTLVTree(json_t
*root
, json_t
*elm
, const char *path
, struct tlvdb
*tlvdbelm
) {
192 struct tlvdb
*tlvp
= tlvdbelm
;
194 const struct tlv
*tlvpelm
= tlvdb_get_tlv(tlvp
);
195 const char *AppDataName
= NULL
;
197 AppDataName
= GetApplicationDataName(tlvpelm
->tag
);
200 char appdatalink
[200] = {0};
201 sprintf(appdatalink
, "$.ApplicationData.%s", AppDataName
);
202 JsonSaveBufAsHex(root
, appdatalink
, (uint8_t *)tlvpelm
->value
, tlvpelm
->len
);
205 json_t
*pelm
= json_path_get(elm
, path
);
206 if (pelm
&& json_is_array(pelm
)) {
207 json_t
*appendelm
= json_object();
208 json_array_append_new(pelm
, appendelm
);
209 JsonSaveTLVTreeElm(appendelm
, "$", tlvp
, !AppDataName
, !tlvdb_elm_get_children(tlvp
), AppDataName
);
212 JsonSaveTLVTreeElm(elm
, path
, tlvp
, !AppDataName
, !tlvdb_elm_get_children(tlvp
), AppDataName
);
213 pelm
= json_path_get(elm
, path
);
216 if (tlvdb_elm_get_children(tlvp
)) {
221 // check childs element and add it if not found
222 json_t
*chjson
= json_path_get(pelm
, "$.Childs");
224 json_object_set_new(pelm
, "Childs", json_array());
226 chjson
= json_path_get(pelm
, "$.Childs");
230 if (!json_is_array(chjson
)) {
231 PrintAndLogEx(ERR
, "E->Internal logic error. `$.Childs` is not an array.");
236 JsonSaveTLVTree(root
, chjson
, "$", tlvdb_elm_get_children(tlvp
));
239 tlvp
= tlvdb_elm_get_next(tlvp
);
244 static bool HexToBuffer(const char *errormsg
, const char *hexvalue
, uint8_t *buffer
, size_t maxbufferlen
, size_t *bufferlen
) {
247 switch (param_gethex_to_eol(hexvalue
, 0, buffer
, maxbufferlen
, &buflen
)) {
249 PrintAndLogEx(ERR
, "%s Invalid HEX value.", errormsg
);
252 PrintAndLogEx(ERR
, "%s Hex value too large.", errormsg
);
255 PrintAndLogEx(ERR
, "%s Hex value must have even number of digits.", errormsg
);
259 if (buflen
> maxbufferlen
) {
260 PrintAndLogEx(ERR
, "%s HEX length (%zu) more than %zu", errormsg
, (bufferlen
) ? *bufferlen
: -1, maxbufferlen
);
270 int JsonLoadStr(json_t
*root
, const char *path
, char *value
) {
274 json_t
*jelm
= json_path_get((const json_t
*)root
, path
);
275 if (!jelm
|| !json_is_string(jelm
))
278 const char *strval
= json_string_value(jelm
);
282 memcpy(value
, strval
, strlen(strval
));
287 int JsonLoadBufAsHex(json_t
*elm
, const char *path
, uint8_t *data
, size_t maxbufferlen
, size_t *datalen
) {
291 json_t
*jelm
= json_path_get((const json_t
*)elm
, path
);
292 if (!jelm
|| !json_is_string(jelm
))
295 if (!HexToBuffer("ERROR load", json_string_value(jelm
), data
, maxbufferlen
, datalen
))
301 bool ParamLoadFromJson(struct tlvdb
*tlv
) {
306 PrintAndLogEx(ERR
, "ERROR load params: tlv tree is NULL.");
311 if (searchFile(&path
, RESOURCES_SUBDIR
, "emv_defparams", ".json", false) != PM3_SUCCESS
) {
314 root
= json_load_file(path
, 0, &error
);
317 PrintAndLogEx(ERR
, "Load params: json error on line " _YELLOW_("%d") ": %s", error
.line
, error
.text
);
321 if (!json_is_array(root
)) {
322 PrintAndLogEx(ERR
, "Load params: Invalid json format. root must be array.");
326 PrintAndLogEx(SUCCESS
, "Load params: json(%zu) (%s)", json_array_size(root
), _GREEN_("OK"));
328 for (int i
= 0; i
< json_array_size(root
); i
++) {
329 json_t
*data
, *jtag
, *jlength
, *jvalue
;
331 data
= json_array_get(root
, i
);
332 if (!json_is_object(data
)) {
333 PrintAndLogEx(ERR
, "Load params: data [%d] is not an object", i
+ 1);
338 jtag
= json_object_get(data
, "tag");
339 if (!json_is_string(jtag
)) {
340 PrintAndLogEx(ERR
, "Load params: data [%d] tag is not a string", i
+ 1);
344 const char *tlvTag
= json_string_value(jtag
);
346 jvalue
= json_object_get(data
, "value");
347 if (!json_is_string(jvalue
)) {
348 PrintAndLogEx(ERR
, "Load params: data [%d] value is not a string", i
+ 1);
352 const char *tlvValue
= json_string_value(jvalue
);
354 jlength
= json_object_get(data
, "length");
355 if (!json_is_number(jlength
)) {
356 PrintAndLogEx(ERR
, "Load params: data [%d] length is not a number", i
+ 1);
361 int tlvLength
= json_integer_value(jlength
);
362 if (tlvLength
> 250) {
363 PrintAndLogEx(ERR
, "Load params: data [%d] length more than 250", i
+ 1);
368 PrintAndLogEx(SUCCESS
, "TLV param: %s[%d]=%s", tlvTag
, tlvLength
, tlvValue
);
369 uint8_t buf
[251] = {0};
372 if (!HexToBuffer("TLV Error type:", tlvTag
, buf
, 4, &buflen
)) {
377 for (int j
= 0; j
< buflen
; j
++) {
378 tag
= (tag
<< 8) | buf
[j
];
381 if (!HexToBuffer("TLV Error value:", tlvValue
, buf
, sizeof(buf
) - 1, &buflen
)) {
386 if (buflen
!= tlvLength
) {
387 PrintAndLogEx(ERR
, "Load params: data [%d] length of HEX must(%zu) be identical to length in TLV param(%d)", i
+ 1, buflen
, tlvLength
);
392 tlvdb_change_or_add_node(tlv
, tag
, tlvLength
, (const unsigned char *)buf
);