recover_pk.py: replace secp192r1 by prime192v1
[RRG-proxmark3.git] / client / src / aidsearch.c
blob48ea3fdc6abcaef104635d62b3d415bc0580723c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Proxmark3 RDV40 AID list library
17 //-----------------------------------------------------------------------------
18 #include "aidsearch.h"
19 #include <ctype.h>
20 #include <string.h>
21 #include "fileutils.h"
22 #include "pm3_cmd.h"
24 static int openAIDFile(json_t **root, bool verbose) {
25 json_error_t error;
27 char *path;
28 int res = searchFile(&path, RESOURCES_SUBDIR, "aidlist", ".json", false);
29 if (res != PM3_SUCCESS) {
30 return PM3_EFILE;
33 int retval = PM3_SUCCESS;
34 *root = json_load_file(path, 0, &error);
35 if (!*root) {
36 PrintAndLogEx(ERR, "json (%s) error on line %d: %s", path, error.line, error.text);
37 retval = PM3_ESOFT;
38 goto out;
41 if (!json_is_array(*root)) {
42 PrintAndLogEx(ERR, "Invalid json (%s) format. root must be an array.", path);
43 retval = PM3_ESOFT;
44 goto out;
47 PrintAndLogEx(DEBUG, "Loaded file " _YELLOW_("%s") " " _GREEN_("%zu") " records ( " _GREEN_("ok") " )"
48 , path
49 , json_array_size(*root)
51 out:
52 free(path);
53 return retval;
56 static int closeAIDFile(json_t *root) {
57 json_decref(root);
58 return PM3_SUCCESS;
61 json_t *AIDSearchInit(bool verbose) {
62 json_t *root = NULL;
63 int res = openAIDFile(&root, verbose);
64 if (res != PM3_SUCCESS)
65 return NULL;
67 return root;
70 json_t *AIDSearchGetElm(json_t *root, size_t elmindx) {
71 json_t *data = json_array_get(root, elmindx);
72 if (!json_is_object(data)) {
73 PrintAndLogEx(ERR, "data [%zu] is not an object\n", elmindx);
74 return NULL;
76 return data;
79 int AIDSearchFree(json_t *root) {
80 return closeAIDFile(root);
83 static const char *jsonStrGet(json_t *data, const char *name) {
84 json_t *jstr;
86 jstr = json_object_get(data, name);
87 if (jstr == NULL)
88 return NULL;
89 if (!json_is_string(jstr)) {
90 PrintAndLogEx(ERR, "`%s` is not a string", name);
91 return NULL;
94 const char *cstr = json_string_value(jstr);
95 if (strlen(cstr) == 0)
96 return NULL;
97 return cstr;
100 static bool aidCompare(const char *aidlarge, const char *aidsmall) {
101 if (strcmp(aidlarge, aidsmall) == 0)
102 return true;
104 if (strlen(aidlarge) > strlen(aidsmall))
105 if (strncmp(aidlarge, aidsmall, strlen(aidsmall)) == 0)
106 return true;
108 return false;
111 bool AIDGetFromElm(json_t *data, uint8_t *aid, size_t aidmaxlen, int *aidlen) {
112 *aidlen = 0;
113 const char *hexaid = jsonStrGet(data, "AID");
114 if (hexaid == NULL || strlen(hexaid) == 0)
115 return false;
117 int res = param_gethex_to_eol(hexaid, 0, aid, (int)aidmaxlen, aidlen);
118 if (res)
119 return false;
121 return true;
124 int PrintAIDDescription(json_t *xroot, char *aid, bool verbose) {
125 int retval = PM3_SUCCESS;
127 json_t *root = xroot;
128 if (root == NULL)
129 root = AIDSearchInit(verbose);
130 if (root == NULL)
131 goto out;
133 json_t *elm = NULL;
134 size_t maxaidlen = 0;
135 for (size_t elmindx = 0; elmindx < json_array_size(root); elmindx++) {
136 json_t *data = AIDSearchGetElm(root, elmindx);
137 if (data == NULL)
138 continue;
139 const char *dictaid = jsonStrGet(data, "AID");
140 if (aidCompare(aid, dictaid)) { // dictaid may be less length than requested aid
141 if (maxaidlen < strlen(dictaid) && strlen(dictaid) <= strlen(aid)) {
142 maxaidlen = strlen(dictaid);
143 elm = data;
148 if (elm == NULL)
149 goto out;
151 // print here
152 const char *vaid = jsonStrGet(elm, "AID");
153 const char *vendor = jsonStrGet(elm, "Vendor");
154 const char *name = jsonStrGet(elm, "Name");
155 const char *country = jsonStrGet(elm, "Country");
156 const char *description = jsonStrGet(elm, "Description");
157 const char *type = jsonStrGet(elm, "Type");
159 if (verbose == false) {
160 PrintAndLogEx(SUCCESS, "AID : " _YELLOW_("%s") " | %s | %s", vaid, vendor, name);
161 } else {
162 PrintAndLogEx(SUCCESS, "Input AID..... " _YELLOW_("%s"), aid);
163 if (aid)
164 PrintAndLogEx(SUCCESS, "Found AID..... " _YELLOW_("%s"), vaid);
165 if (vendor)
166 PrintAndLogEx(SUCCESS, "Vendor........ " _YELLOW_("%s"), vendor);
167 if (type)
168 PrintAndLogEx(SUCCESS, "Type.......... " _YELLOW_("%s"), type);
169 if (name)
170 PrintAndLogEx(SUCCESS, "Name.......... " _YELLOW_("%s"), name);
171 if (country)
172 PrintAndLogEx(SUCCESS, "Country....... %s", country);
173 if (description)
174 PrintAndLogEx(SUCCESS, "Description... %s", description);
177 out:
178 if (xroot == NULL)
179 AIDSearchFree(root);
180 return retval;
183 int PrintAIDDescriptionBuf(json_t *root, uint8_t *aid, size_t aidlen, bool verbose) {
184 return PrintAIDDescription(root, sprint_hex_inrow(aid, aidlen), verbose);