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 //-----------------------------------------------------------------------------
19 #include "proxmark3.h"
22 static const ApplicationDataElm ApplicationData
[] = {
28 {0x5F24, "ExpirationDate"},
29 {0x5F25, "EffectiveDate"},
30 {0x5F28, "IssuerCountryCode"},
32 {0x50, "ApplicationLabel"},
33 {0x9F08, "VersionNumber"},
34 {0x9F42, "CurrencyCode"},
35 {0x5F2D, "LanguagePreference"},
36 {0x87, "PriorityIndicator"},
37 {0x9F36, "ATC"}, //Application Transaction Counter
39 {0x5F20, "CardholderName"},
45 {0x9F07, "AUC"}, // Application Usage Control
48 {0x9F0D, "IACDefault"},
50 {0x9F0F, "IACOnline"},
52 {0x8F, "CertificationAuthorityPublicKeyIndex"},
53 {0x9F32, "IssuerPublicKeyExponent"},
54 {0x92, "IssuerPublicKeyRemainder"},
55 {0x90, "IssuerPublicKeyCertificate"},
56 {0x9F47, "ICCPublicKeyExponent"},
57 {0x9F46, "ICCPublicKeyCertificate"},
61 int ApplicationDataLen
= sizeof(ApplicationData
) / sizeof(ApplicationDataElm
);
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
;
71 int JsonSaveStr(json_t
*root
, char *path
, char *value
) {
78 if (json_path_set(root
, path
, json_string(value
), 0, &error
)) {
79 PrintAndLog("ERROR: can't set json path: ", error
.text
);
85 return json_object_set_new(root
, path
, json_string(value
));
89 int JsonSaveBufAsHex(json_t
*elm
, char *path
, uint8_t *data
, size_t datalen
) {
90 char * msg
= sprint_hex(data
, datalen
);
91 if (msg
&& strlen(msg
) && msg
[strlen(msg
) - 1] == ' ')
92 msg
[strlen(msg
) - 1] = '\0';
94 return JsonSaveStr(elm
, path
, msg
);
97 int JsonSaveHex(json_t
*elm
, char *path
, uint64_t data
, int datalen
) {
98 uint8_t bdata
[8] = {0};
101 for (uint64_t u
= 0xffffffffffffffff; u
; u
= u
<< 8) {
112 num_to_bytes(data
, len
, bdata
);
114 return JsonSaveBufAsHex(elm
, path
, bdata
, len
);
117 int JsonSaveTLVValue(json_t
*root
, char *path
, struct tlvdb
*tlvdbelm
) {
118 const struct tlv
*tlvelm
= tlvdb_get_tlv(tlvdbelm
);
120 return JsonSaveBufAsHex(root
, path
, (uint8_t *)tlvelm
->value
, tlvelm
->len
);
125 int JsonSaveTLVElm(json_t
*elm
, char *path
, struct tlv
*tlvelm
, bool saveName
, bool saveValue
, bool saveAppDataLink
) {
128 if (strlen(path
) < 1 || !tlvelm
)
131 if (path
[0] == '$') {
133 json_t
*obj
= json_path_get(elm
, path
);
137 if (json_is_array(elm
)) {
138 if (json_array_append_new(elm
, obj
)) {
139 PrintAndLog("ERROR: can't append array: %s", path
);
143 if (json_path_set(elm
, path
, obj
, 0, &error
)) {
144 PrintAndLog("ERROR: can't set json path: ", error
.text
);
150 if (saveAppDataLink
) {
151 char * AppDataName
= GetApplicationDataName(tlvelm
->tag
);
153 JsonSaveStr(obj
, "appdata", AppDataName
);
155 char * name
= emv_get_tag_name(tlvelm
);
156 if (saveName
&& name
&& strlen(name
) > 0 && strncmp(name
, "Unknown", 7))
157 JsonSaveStr(obj
, "name", emv_get_tag_name(tlvelm
));
158 JsonSaveHex(obj
, "tag", tlvelm
->tag
, 0);
160 JsonSaveHex(obj
, "length", tlvelm
->len
, 0);
161 JsonSaveBufAsHex(obj
, "value", (uint8_t *)tlvelm
->value
, tlvelm
->len
);
169 int JsonSaveTLVTreeElm(json_t
*elm
, char *path
, struct tlvdb
*tlvdbelm
, bool saveName
, bool saveValue
, bool saveAppDataLink
) {
170 return JsonSaveTLVElm(elm
, path
, (struct tlv
*)tlvdb_get_tlv(tlvdbelm
), saveName
, saveValue
, saveAppDataLink
);
173 int JsonSaveTLVTree(json_t
*root
, json_t
*elm
, char *path
, struct tlvdb
*tlvdbelm
) {
174 struct tlvdb
*tlvp
= tlvdbelm
;
176 const struct tlv
* tlvpelm
= tlvdb_get_tlv(tlvp
);
177 char * AppDataName
= NULL
;
179 AppDataName
= GetApplicationDataName(tlvpelm
->tag
);
182 char appdatalink
[200] = {0};
183 sprintf(appdatalink
, "$.ApplicationData.%s", AppDataName
);
184 JsonSaveBufAsHex(root
, appdatalink
, (uint8_t *)tlvpelm
->value
, tlvpelm
->len
);
187 json_t
*pelm
= json_path_get(elm
, path
);
188 if (pelm
&& json_is_array(pelm
)) {
189 json_t
*appendelm
= json_object();
190 json_array_append_new(pelm
, appendelm
);
191 JsonSaveTLVTreeElm(appendelm
, "$", tlvp
, !AppDataName
, !tlvdb_elm_get_children(tlvp
), AppDataName
);
194 JsonSaveTLVTreeElm(elm
, path
, tlvp
, !AppDataName
, !tlvdb_elm_get_children(tlvp
), AppDataName
);
195 pelm
= json_path_get(elm
, path
);
198 if (tlvdb_elm_get_children(tlvp
)) {
203 // check childs element and add it if not found
204 json_t
*chjson
= json_path_get(pelm
, "$.Childs");
206 json_object_set_new(pelm
, "Childs", json_array());
208 chjson
= json_path_get(pelm
, "$.Childs");
212 if (!json_is_array(chjson
)) {
213 PrintAndLog("E->Internal logic error. `$.Childs` is not an array.");
218 JsonSaveTLVTree(root
, chjson
, "$", tlvdb_elm_get_children(tlvp
));
221 tlvp
= tlvdb_elm_get_next(tlvp
);
226 bool HexToBuffer(const char *errormsg
, const char *hexvalue
, uint8_t * buffer
, size_t maxbufferlen
, size_t *bufferlen
) {
229 switch(param_gethex_to_eol(hexvalue
, 0, buffer
, maxbufferlen
, &buflen
)) {
231 PrintAndLog("%s Invalid HEX value.", errormsg
);
234 PrintAndLog("%s Hex value too large.", errormsg
);
237 PrintAndLog("%s Hex value must have even number of digits.", errormsg
);
241 if (buflen
> maxbufferlen
) {
242 PrintAndLog("%s HEX length (%d) more than %d", errormsg
, *bufferlen
, maxbufferlen
);
251 bool ParamLoadFromJson(struct tlvdb
*tlv
) {
256 PrintAndLog("ERROR load params: tlv tree is NULL.");
260 // current path + file name
261 const char *relfname
= "emv/defparams.json";
262 char fname
[strlen(get_my_executable_directory()) + strlen(relfname
) + 1];
263 strcpy(fname
, get_my_executable_directory());
264 strcat(fname
, relfname
);
266 root
= json_load_file(fname
, 0, &error
);
268 PrintAndLog("Load params: json error on line %d: %s", error
.line
, error
.text
);
272 if (!json_is_array(root
)) {
273 PrintAndLog("Load params: Invalid json format. root must be array.");
277 PrintAndLog("Load params: json(%d) OK", json_array_size(root
));
279 for(int i
= 0; i
< json_array_size(root
); i
++) {
280 json_t
*data
, *jtag
, *jlength
, *jvalue
;
282 data
= json_array_get(root
, i
);
283 if(!json_is_object(data
))
285 PrintAndLog("Load params: data [%d] is not an object", i
+ 1);
290 jtag
= json_object_get(data
, "tag");
291 if(!json_is_string(jtag
))
293 PrintAndLog("Load params: data [%d] tag is not a string", i
+ 1);
297 const char *tlvTag
= json_string_value(jtag
);
299 jvalue
= json_object_get(data
, "value");
300 if(!json_is_string(jvalue
))
302 PrintAndLog("Load params: data [%d] value is not a string", i
+ 1);
306 const char *tlvValue
= json_string_value(jvalue
);
308 jlength
= json_object_get(data
, "length");
309 if(!json_is_number(jlength
))
311 PrintAndLog("Load params: data [%d] length is not a number", i
+ 1);
316 int tlvLength
= json_integer_value(jlength
);
317 if (tlvLength
> 250) {
318 PrintAndLog("Load params: data [%d] length more than 250", i
+ 1);
323 PrintAndLog("TLV param: %s[%d]=%s", tlvTag
, tlvLength
, tlvValue
);
324 uint8_t buf
[251] = {0};
327 // 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...
328 if (!HexToBuffer("TLV Error type:", tlvTag
, buf
, 2, &buflen
)) {
333 for (int i
= 0; i
< buflen
; i
++) {
334 tag
= (tag
<< 8) + buf
[i
];
337 if (!HexToBuffer("TLV Error value:", tlvValue
, buf
, sizeof(buf
) - 1, &buflen
)) {
342 if (buflen
!= tlvLength
) {
343 PrintAndLog("Load params: data [%d] length of HEX must(%d) be identical to length in TLV param(%d)", i
+ 1, buflen
, tlvLength
);
348 tlvdb_change_or_add_node(tlv
, tag
, tlvLength
, (const unsigned char *)buf
);