1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
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.
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 //-----------------------------------------------------------------------------
27 #include "emv/emvjson.h"
28 #include "mifare/mifare4.h"
29 #include "mifare/mifarehost.h"
32 #include "protocols.h" // iclass defines
33 #include "cmdhftopaz.h" // TOPAZ defines
34 #include "mifare/mifaredefault.h" // MFP / AES defines
41 iso14a_mf_extdump_t
*mfc
;
42 iso14a_mf_dump_ev1_t
*mfc_ev1
;
76 jsfFM11RF08SNoncesWithData
106 int fileExists(const char *filename
);
108 // set a path in the path list g_session.defaultPaths
109 bool setDefaultPath(savePaths_t pathIndex
, const char *path
);
111 char *newfilenamemcopy(const char *preferredName
, const char *suffix
);
112 char *newfilenamemcopyEx(const char *preferredName
, const char *suffix
, savePaths_t e_save_path
);
113 void truncate_filename(char *fn
, uint16_t maxlen
);
117 * @brief Utility function to save data to a binary file. This method takes a preferred name, but if that
118 * file already exists, it tries with another name until it finds something suitable.
119 * E.g. dumpdata-15.txt
121 * @param preferredName
122 * @param suffix the file suffix. Including the ".".
123 * @param data The binary data to write to the file
124 * @param datalen the length of the data
125 * @return 0 for ok, 1 for failz
127 int saveFile(const char *preferredName
, const char *suffix
, const void *data
, size_t datalen
);
128 int saveFileEx(const char *preferredName
, const char *suffix
, const void *data
, size_t datalen
, savePaths_t e_save_path
);
131 * @brief Utility function to save JSON data to a file. This method takes a preferred name, but if that
132 * file already exists, it tries with another name until it finds something suitable.
133 * E.g. dumpdata-15.json
135 * @param preferredName
136 * @param ftype type of file.
137 * @param data The binary data to write to the file
138 * @param datalen the length of the data
139 * @return 0 for ok, 1 for failz
141 int saveFileJSON(const char *preferredName
, JSONFileType ftype
, uint8_t *data
, size_t datalen
, void (*callback
)(json_t
*));
142 int saveFileJSONex(const char *preferredName
, JSONFileType ftype
, uint8_t *data
, size_t datalen
, bool verbose
, void (*callback
)(json_t
*), savePaths_t e_save_path
);
143 int saveFileJSONroot(const char *preferredName
, void *root
, size_t flags
, bool verbose
);
144 int saveFileJSONrootEx(const char *preferredName
, const void *root
, size_t flags
, bool verbose
, bool overwrite
, savePaths_t e_save_path
);
145 int prepareJSON(json_t
*root
, JSONFileType ftype
, uint8_t *data
, size_t datalen
, bool verbose
, void (*callback
)(json_t
*));
146 char *sprintJSON(JSONFileType ftype
, uint8_t *data
, size_t datalen
, bool verbose
, void (*callback
)(json_t
*));
148 * @brief Utility function to save WAVE data to a file. This method takes a preferred name, but if that
149 * file already exists, it tries with another name until it finds something suitable.
150 * E.g. dumpdata-15.wav
152 * @param preferredName
153 * @param data The binary data to write to the file
154 * @param datalen the length of the data
157 int saveFileWAVE(const char *preferredName
, const int *data
, size_t datalen
);
160 * @brief Utility function to save PM3 data to a file. This method takes a preferred name, but if that
161 * file already exists, it tries with another name until it finds something suitable.
162 * E.g. dump_trace.pm3
164 * @param preferredName
165 * @param data The binary data to write to the file
166 * @param datalen the length of the data
169 int saveFilePM3(const char *preferredName
, int *data
, size_t datalen
);
172 * @brief Utility function to save a keydump into a binary file.
174 * @param preferredName
175 * @param sectorsCnt the used sectors
176 * @param e_sector the keys in question
177 * @return 0 for ok, 1 for failz
179 int createMfcKeyDump(const char *preferredName
, uint8_t sectorsCnt
, const sector_t
*e_sector
);
182 * @brief Utility function to load data from a binary file. This method takes a preferred name.
183 * E.g. dumpdata-15.bin, tries to search for it, and allocated memory.
185 * @param preferredName
186 * @param suffix the file suffix. Including the ".".
187 * @param data The data array to store the loaded bytes from file
188 * @param datalen the number of bytes loaded from file
189 * @return PM3_SUCCESS for ok, PM3_E* for failz
191 int loadFile_safe(const char *preferredName
, const char *suffix
, void **pdata
, size_t *datalen
);
192 int loadFile_safeEx(const char *preferredName
, const char *suffix
, void **pdata
, size_t *datalen
, bool verbose
);
194 * @brief Utility function to load data from a textfile (EML). This method takes a preferred name.
195 * E.g. dumpdata-15.txt
197 * @param preferredName
198 * @param data The data array to store the loaded bytes from file
199 * @param datalen the number of bytes loaded from file
200 * @return 0 for ok, 1 for failz
202 int loadFileEML_safe(const char *preferredName
, void **pdata
, size_t *datalen
);
205 * @brief Utility function to load data from a textfile (MCT). This method takes a preferred name.
206 * E.g. dumpdata-15.mct
208 * @param preferredName
209 * @param data The data array to store the loaded bytes from file
210 * @param datalen the number of bytes loaded from file
211 * @return 0 for ok, 1 for failz
213 int loadFileMCT_safe(const char *preferredName
, void **pdata
, size_t *datalen
);
216 * @brief Utility function to load data from a textfile (NFC). This method takes a preferred name.
217 * E.g. dumpdata-15.nfc
219 * @param preferredName
220 * @param data The data array to store the loaded bytes from file
221 * @param maxdatalen maximum size of data array in bytes
222 * @param datalen the number of bytes loaded from file
224 * @return 0 for ok, 1 for failz
226 int loadFileNFC_safe(const char *preferredName
, void *data
, size_t maxdatalen
, size_t *datalen
, nfc_df_e ft
);
229 * @brief Utility function to load data from a JSON textfile. This method takes a preferred name.
230 * E.g. dumpdata-15.json
232 * @param preferredName
233 * @param data The data array to store the loaded bytes from file
234 * @param maxdatalen maximum size of data array in bytes
235 * @param datalen the number of bytes loaded from file
236 * @return 0 for ok, 1 for failz
238 int loadFileJSON(const char *preferredName
, void *data
, size_t maxdatalen
, size_t *datalen
, void (*callback
)(json_t
*));
239 int loadFileJSONex(const char *preferredName
, void *data
, size_t maxdatalen
, size_t *datalen
, bool verbose
, void (*callback
)(json_t
*));
240 int loadFileJSONroot(const char *preferredName
, void **proot
, bool verbose
);
243 * @brief Utility function to load data from a DICTIONARY textfile. This method takes a preferred name.
244 * E.g. mfc_default_keys.dic
246 * @param preferredName
247 * @param data The data array to store the loaded bytes from file
248 * @param datalen the number of bytes loaded from file. may be NULL
249 * @param keylen the number of bytes a key per row is
250 * @param keycnt key count that lays in data. may be NULL
251 * @return 0 for ok, 1 for failz
253 int loadFileDICTIONARY(const char *preferredName
, void *data
, size_t *datalen
, uint8_t keylen
, uint32_t *keycnt
);
256 * @brief Utility function to load data from a DICTIONARY textfile. This method takes a preferred name.
257 * E.g. mfc_default_keys.dic
258 * can be executed several times for big dictionaries and checks length of buffer
260 * @param preferredName
261 * @param data The data array to store the loaded bytes from file
262 * @param maxdatalen maximum size of data array in bytes
263 * @param datalen the number of bytes loaded from file. may be NULL
264 * @param keylen the number of bytes a key per row is
265 * @param keycnt key count that lays in data. may be NULL
266 * @param startFilePosition start position in dictionary file. used for big dictionaries.
267 * @param endFilePosition in case we have keys in file and maxdatalen reached it returns current key position in file. may be NULL
268 * @param verbose print messages if true
269 * @return 0 for ok, 1 for failz
271 int loadFileDICTIONARYEx(const char *preferredName
, void *data
, size_t maxdatalen
, size_t *datalen
, uint8_t keylen
, uint32_t *keycnt
,
272 size_t startFilePosition
, size_t *endFilePosition
, bool verbose
);
275 * @brief Utility function to load data safely from a DICTIONARY textfile. This method takes a preferred name.
276 * E.g. mfc_default_keys.dic
278 * @param preferredName
279 * @param pdata A pointer to a pointer (for reverencing the loaded dictionary)
280 * @param keylen the number of bytes a key per row is
281 * @return 0 for ok, 1 for failz
283 int loadFileDICTIONARY_safe(const char *preferredName
, void **pdata
, uint8_t keylen
, uint32_t *keycnt
);
285 int loadFileDICTIONARY_safe_ex(const char *preferredName
, const char *suffix
, void **pdata
, uint8_t keylen
, uint32_t *keycnt
, bool verbose
);
287 int loadFileBinaryKey(const char *preferredName
, const char *suffix
, void **keya
, void **keyb
, size_t *alen
, size_t *blen
);
290 * @brief Utility function to check and convert plain mfu dump format to new mfu binary format.
291 * plain dumps doesn't have any extra data, like version, signature etc.
292 * @param dump pointer to loaded dump to check and convert format
293 * @param dumplen the number of bytes loaded dump and converted
294 * @param verbose - extra debug output
295 * @return PM3_SUCCESS for ok, PM3_ESOFT for fails
297 int convert_mfu_dump_format(uint8_t **dump
, size_t *dumplen
, bool verbose
);
298 mfu_df_e
detect_mfu_dump_format(uint8_t **dump
, bool verbose
);
299 int detect_nfc_dump_format(const char *preferredName
, nfc_df_e
*dump_type
, bool verbose
);
301 int searchAndList(const char *pm3dir
, const char *ext
);
302 int searchFile(char **foundpath
, const char *pm3dir
, const char *searchname
, const char *suffix
, bool silent
);
306 * @brief detects if file is of a supported filetype based on extension
310 DumpFileType_t
get_filetype(const char *filename
);
314 * @brief load dump file into a data array dynamically allocated
316 * @param pdump pointer to loaded dump
317 * @param dumplen the number of bytes loaded from dump file
318 * @param maxdumplen maximum size of data array in bytes (JSON files)
319 * @return PM3_SUCCESS if OK
321 int pm3_load_dump(const char *fn
, void **pdump
, size_t *dumplen
, size_t maxdumplen
);
325 * @brief Utility function to save data to three file files (BIN/JSON).
326 * It also tries to save according to user preferences set dump folder paths.
331 * @param d The binary data to write to the file
332 * @param n the length of the data
333 * @param jsft json format type for the different memory cards (MFC, MFUL, LEGIC, 14B, 15, ICLASS etc)
334 * @return PM3_SUCCESS if OK
336 int pm3_save_dump(const char *fn
, uint8_t *d
, size_t n
, JSONFileType jsft
);
339 * @brief Utility function to save data to three file files (BIN/JSON).
340 * It also tries to save according to user preferences set dump folder paths.
344 * This function is dedicated for MIFARE CLASSIC dumps. Checking for 4 or 7 byte UID in indata.
345 * Saves the corrected data in the json file
348 * @param d The binary data to write to the file
349 * @param n the length of the data
350 * @param jsft json format type for the different memory cards (MFC, MFUL, LEGIC, 14B, 15, ICLASS etc)
351 * @return PM3_SUCCESS if OK
353 int pm3_save_mf_dump(const char *fn
, uint8_t *d
, size_t n
, JSONFileType jsft
);
356 * @brief Utility function to save FM11RF08S recovery data.
359 * @param d iso14a_fm11rf08s_nonces_with_data_t structure
360 * @param n the length of the structure
361 * @param with_data does the structure contain data blocks?
362 * @return PM3_SUCCESS if OK
364 int pm3_save_fm11rf08s_nonces(const char *fn
, iso14a_fm11rf08s_nonces_with_data_t
*d
, bool with_data
);
365 #endif // FILEUTILS_H