1 //-----------------------------------------------------------------------------
2 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
3 // at your option, any later version. See the LICENSE.txt file for the text of
5 //-----------------------------------------------------------------------------
6 // AID DESFire functions
7 //-----------------------------------------------------------------------------
9 #include "aiddesfire.h"
11 #include "fileutils.h"
14 static json_t
*df_known_aids
= NULL
;
16 static int open_aiddf_file(json_t
**root
, bool verbose
) {
19 int res
= searchFile(&path
, RESOURCES_SUBDIR
, "aid_desfire", ".json", true);
20 if (res
!= PM3_SUCCESS
) {
24 int retval
= PM3_SUCCESS
;
27 *root
= json_load_file(path
, 0, &error
);
29 PrintAndLogEx(ERR
, "json (%s) error on line %d: %s", path
, error
.line
, error
.text
);
34 if (!json_is_array(*root
)) {
35 PrintAndLogEx(ERR
, "Invalid json (%s) format. root must be an array.", path
);
41 PrintAndLogEx(SUCCESS
, "Loaded file " _YELLOW_("`%s`") " (%s) %zu records.", path
, _GREEN_("ok"), json_array_size(*root
));
47 static int close_aiddf_file(json_t
*root
) {
52 static const char *aiddf_json_get_str(json_t
*data
, const char *name
) {
54 json_t
*jstr
= json_object_get(data
, name
);
58 if (!json_is_string(jstr
)) {
59 PrintAndLogEx(WARNING
, _YELLOW_("`%s`") " is not a string", name
);
63 const char *cstr
= json_string_value(jstr
);
64 if (strlen(cstr
) == 0)
70 static int print_aiddf_description(json_t
*root
, uint8_t aid
[3], char *fmt
, bool verbose
) {
72 sprintf(laid
, "%02x%02x%02x", aid
[2], aid
[1], aid
[0]); // must be lowercase
76 for (uint32_t idx
= 0; idx
< json_array_size(root
); idx
++) {
77 json_t
*data
= json_array_get(root
, idx
);
78 if (!json_is_object(data
)) {
79 PrintAndLogEx(ERR
, "data [%d] is not an object\n", idx
);
82 const char *faid
= aiddf_json_get_str(data
, "AID");
83 char lfaid
[strlen(faid
) + 1];
86 if (strcmp(laid
, lfaid
) == 0) {
93 PrintAndLogEx(INFO
, fmt
, " (unknown)");
96 const char *vaid
= aiddf_json_get_str(elm
, "AID");
97 const char *vendor
= aiddf_json_get_str(elm
, "Vendor");
98 const char *country
= aiddf_json_get_str(elm
, "Country");
99 const char *name
= aiddf_json_get_str(elm
, "Name");
100 const char *description
= aiddf_json_get_str(elm
, "Description");
101 const char *type
= aiddf_json_get_str(elm
, "Type");
103 if (name
&& vendor
) {
104 char result
[5 + strlen(name
) + strlen(vendor
)];
105 sprintf(result
, " %s [%s]", name
, vendor
);
106 PrintAndLogEx(INFO
, fmt
, result
);
110 PrintAndLogEx(SUCCESS
, " AID: %s", vaid
);
112 PrintAndLogEx(SUCCESS
, " Name: %s", name
);
114 PrintAndLogEx(SUCCESS
, " Description: %s", description
);
116 PrintAndLogEx(SUCCESS
, " Type: %s", type
);
118 PrintAndLogEx(SUCCESS
, " Vendor: %s", vendor
);
120 PrintAndLogEx(SUCCESS
, " Country: %s", country
);
125 int AIDDFDecodeAndPrint(uint8_t aid
[3]) {
126 open_aiddf_file(&df_known_aids
, false);
129 sprintf(fmt
, " DF AID Function %02X%02X%02X :" _YELLOW_("%s"), aid
[2], aid
[1], aid
[0], "%s");
130 print_aiddf_description(df_known_aids
, aid
, fmt
, false);
131 close_aiddf_file(df_known_aids
);