metroflip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include "metroflip_i.h"
  2. #define TAG "Metroflip"
  3. #include "api/metroflip/metroflip_api.h"
  4. #include "api/metroflip/metroflip_api_interface.h"
  5. #include "metroflip_plugins.h"
  6. struct MfClassicKeyCache {
  7. MfClassicDeviceKeys keys;
  8. MfClassicKeyType current_key_type;
  9. uint8_t current_sector;
  10. };
  11. static bool metroflip_custom_event_callback(void* context, uint32_t event) {
  12. furi_assert(context);
  13. Metroflip* app = context;
  14. return scene_manager_handle_custom_event(app->scene_manager, event);
  15. }
  16. static bool metroflip_back_event_callback(void* context) {
  17. furi_assert(context);
  18. Metroflip* app = context;
  19. return scene_manager_handle_back_event(app->scene_manager);
  20. }
  21. Metroflip* metroflip_alloc() {
  22. Metroflip* app = malloc(sizeof(Metroflip));
  23. app->gui = furi_record_open(RECORD_GUI);
  24. //nfc device
  25. app->nfc = nfc_alloc();
  26. app->nfc_device = nfc_device_alloc();
  27. app->detected_protocols = nfc_detected_protocols_alloc();
  28. // key cache
  29. app->mfc_key_cache = mf_classic_key_cache_alloc();
  30. // notifs
  31. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  32. // View Dispatcher and Scene Manager
  33. app->view_dispatcher = view_dispatcher_alloc();
  34. app->scene_manager = scene_manager_alloc(&metroflip_scene_handlers, app);
  35. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  36. view_dispatcher_set_custom_event_callback(
  37. app->view_dispatcher, metroflip_custom_event_callback);
  38. view_dispatcher_set_navigation_event_callback(
  39. app->view_dispatcher, metroflip_back_event_callback);
  40. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  41. // Custom Widget
  42. app->widget = widget_alloc();
  43. view_dispatcher_add_view(
  44. app->view_dispatcher, MetroflipViewWidget, widget_get_view(app->widget));
  45. // Gui Modules
  46. app->submenu = submenu_alloc();
  47. view_dispatcher_add_view(
  48. app->view_dispatcher, MetroflipViewSubmenu, submenu_get_view(app->submenu));
  49. app->text_input = text_input_alloc();
  50. view_dispatcher_add_view(
  51. app->view_dispatcher, MetroflipViewTextInput, text_input_get_view(app->text_input));
  52. app->byte_input = byte_input_alloc();
  53. view_dispatcher_add_view(
  54. app->view_dispatcher, MetroflipViewByteInput, byte_input_get_view(app->byte_input));
  55. app->popup = popup_alloc();
  56. view_dispatcher_add_view(app->view_dispatcher, MetroflipViewPopup, popup_get_view(app->popup));
  57. app->nfc_device = nfc_device_alloc();
  58. // TextBox
  59. app->text_box = text_box_alloc();
  60. view_dispatcher_add_view(
  61. app->view_dispatcher, MetroflipViewTextBox, text_box_get_view(app->text_box));
  62. app->text_box_store = furi_string_alloc();
  63. // Dialog for loading
  64. app->dialogs = furi_record_open(RECORD_DIALOGS);
  65. app->data_loaded = false;
  66. return app;
  67. }
  68. void metroflip_free(Metroflip* app) {
  69. furi_assert(app);
  70. //nfc device
  71. nfc_free(app->nfc);
  72. nfc_device_free(app->nfc_device);
  73. nfc_detected_protocols_free(app->detected_protocols);
  74. // key cache
  75. mf_classic_key_cache_free(app->mfc_key_cache);
  76. //notifs
  77. furi_record_close(RECORD_NOTIFICATION);
  78. app->notifications = NULL;
  79. // Gui modules
  80. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewSubmenu);
  81. submenu_free(app->submenu);
  82. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewTextInput);
  83. text_input_free(app->text_input);
  84. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewByteInput);
  85. byte_input_free(app->byte_input);
  86. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewPopup);
  87. popup_free(app->popup);
  88. // Custom Widget
  89. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewWidget);
  90. widget_free(app->widget);
  91. // TextBox
  92. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewTextBox);
  93. text_box_free(app->text_box);
  94. furi_string_free(app->text_box_store);
  95. // View Dispatcher and Scene Manager
  96. view_dispatcher_free(app->view_dispatcher);
  97. scene_manager_free(app->scene_manager);
  98. // Records
  99. furi_record_close(RECORD_GUI);
  100. // Dialogs
  101. furi_record_close(RECORD_DIALOGS);
  102. free(app);
  103. }
  104. static const NotificationSequence metroflip_app_sequence_blink_start_blue = {
  105. &message_blink_start_10,
  106. &message_blink_set_color_blue,
  107. &message_do_not_reset,
  108. NULL,
  109. };
  110. static const NotificationSequence metroflip_app_sequence_blink_stop = {
  111. &message_blink_stop,
  112. NULL,
  113. };
  114. void metroflip_app_blink_start(Metroflip* metroflip) {
  115. notification_message(metroflip->notifications, &metroflip_app_sequence_blink_start_blue);
  116. }
  117. void metroflip_app_blink_stop(Metroflip* metroflip) {
  118. notification_message(metroflip->notifications, &metroflip_app_sequence_blink_stop);
  119. }
  120. void metroflip_exit_widget_callback(GuiButtonType result, InputType type, void* context) {
  121. Metroflip* app = context;
  122. UNUSED(result);
  123. if(type == InputTypeShort) {
  124. scene_manager_next_scene(app->scene_manager, MetroflipSceneSave);
  125. }
  126. }
  127. void metroflip_save_widget_callback(GuiButtonType result, InputType type, void* context) {
  128. Metroflip* app = context;
  129. UNUSED(result);
  130. if(type == InputTypeShort) {
  131. scene_manager_next_scene(app->scene_manager, MetroflipSceneSave);
  132. }
  133. }
  134. // Calypso
  135. void byte_to_binary(uint8_t byte, char* bits) {
  136. for(int i = 7; i >= 0; i--) {
  137. bits[7 - i] = (byte & (1 << i)) ? '1' : '0';
  138. }
  139. bits[8] = '\0';
  140. }
  141. int binary_to_decimal(const char binary[]) {
  142. int decimal = 0;
  143. int length = strlen(binary);
  144. for(int i = 0; i < length; i++) {
  145. decimal = decimal * 2 + (binary[i] - '0');
  146. }
  147. return decimal;
  148. }
  149. void locale_format_datetime_cat(FuriString* out, const DateTime* dt, bool time) {
  150. // helper to print datetimes
  151. FuriString* s = furi_string_alloc();
  152. LocaleDateFormat date_format = locale_get_date_format();
  153. const char* separator = (date_format == LocaleDateFormatDMY) ? "." : "/";
  154. locale_format_date(s, dt, date_format, separator);
  155. furi_string_cat(out, s);
  156. if(time) {
  157. locale_format_time(s, dt, locale_get_time_format(), false);
  158. furi_string_cat_printf(out, " ");
  159. furi_string_cat(out, s);
  160. }
  161. furi_string_free(s);
  162. }
  163. // read file
  164. uint8_t read_file[5] = {0x94, 0xb2, 0x01, 0x04, 0x1D};
  165. // ^^^
  166. // |||
  167. // FID
  168. // select app
  169. uint8_t select_app[8] = {0x94, 0xA4, 0x00, 0x00, 0x02, 0x20, 0x00, 0x00};
  170. // ^^^^^^^^^
  171. // |||||||||
  172. // AID: 20XX
  173. uint8_t apdu_success[2] = {0x90, 0x00};
  174. char* bit_slice(const char* bit_representation, int start, int end) {
  175. static char bit_slice[32 * 8 + 1];
  176. strncpy(bit_slice, bit_representation + start, end - start + 1);
  177. bit_slice[end - start + 1] = '\0';
  178. return bit_slice;
  179. }
  180. int bit_slice_to_dec(const char* bit_representation, int start, int end) {
  181. return binary_to_decimal(bit_slice(bit_representation, start, end));
  182. }
  183. extern int32_t metroflip(void* p) {
  184. UNUSED(p);
  185. /*
  186. plugin_manager_load_single(PluginManager * manager, const char* path)
  187. uint32_t plugin_count = plugin_manager_get_count(manager);
  188. FURI_LOG_I(TAG, "Loaded %lu plugin(s)", plugin_count);
  189. for(uint32_t i = 0; i < plugin_count; i++) {
  190. const MetroflipPlugin* plugin = plugin_manager_get_ep(manager, i);
  191. FURI_LOG_I(TAG, "plugin name: %s", plugin->name);
  192. }
  193. */
  194. Metroflip* app = metroflip_alloc();
  195. scene_manager_set_scene_state(app->scene_manager, MetroflipSceneStart, MetroflipSceneAuto);
  196. scene_manager_next_scene(app->scene_manager, MetroflipSceneStart);
  197. view_dispatcher_run(app->view_dispatcher);
  198. metroflip_free(app);
  199. return 0;
  200. }
  201. KeyfileManager manage_keyfiles(
  202. char uid_str[],
  203. const uint8_t* uid,
  204. size_t uid_len,
  205. MfClassicKeyCache* instance,
  206. uint64_t key_mask_a_required,
  207. uint64_t key_mask_b_required) {
  208. Storage* storage = furi_record_open(RECORD_STORAGE);
  209. File* source = storage_file_alloc(storage);
  210. char source_path[64];
  211. UNUSED(key_mask_b_required);
  212. FURI_LOG_I("TAG", "%s", uid_str);
  213. size_t source_required_size =
  214. strlen("/ext/nfc/.cache/") + strlen(uid_str) + strlen(".keys") + 1;
  215. snprintf(source_path, source_required_size, "/ext/nfc/.cache/%s.keys", uid_str);
  216. bool cache_file = storage_file_open(source, source_path, FSAM_READ, FSOM_OPEN_EXISTING);
  217. /*-----------------Open assets cache file (if exists)------------*/
  218. File* dest = storage_file_alloc(storage);
  219. char dest_path[64];
  220. size_t dest_required_size =
  221. strlen("/ext/nfc/assets/.") + strlen(uid_str) + strlen(".keys") + 1;
  222. snprintf(dest_path, dest_required_size, "/ext/nfc/assets/.%s.keys", uid_str);
  223. bool dest_cache_file = storage_file_open(dest, dest_path, FSAM_READ, FSOM_OPEN_EXISTING);
  224. /*-----------------Check cache file------------*/
  225. if(!cache_file) {
  226. /*-----------------Check assets cache file------------*/
  227. FURI_LOG_I("TAG", "cache dont exist, checking assets");
  228. if(!dest_cache_file) {
  229. FURI_LOG_I("TAG", "assets dont exist, prompting user to fix..");
  230. storage_file_close(source);
  231. storage_file_close(dest);
  232. return MISSING_KEYFILE;
  233. } else {
  234. size_t dest_file_length = storage_file_size(dest);
  235. FURI_LOG_I(
  236. "TAG", "assets exist, but cache doesnt, proceeding to copy assets to cache");
  237. // Close, then open both files
  238. storage_file_close(source);
  239. storage_file_close(dest);
  240. storage_file_open(
  241. source, source_path, FSAM_WRITE, FSOM_OPEN_ALWAYS); // create new file
  242. storage_file_open(
  243. dest, dest_path, FSAM_READ, FSOM_OPEN_EXISTING); // open existing assets keyfile
  244. FURI_LOG_I("TAG", "creating cache file at %s from %s", source_path, dest_path);
  245. /*-----Clone keyfile from assets to cache (creates temporary buffer)----*/
  246. uint8_t* cloned_buffer = malloc(dest_file_length);
  247. storage_file_read(dest, cloned_buffer, dest_file_length);
  248. storage_file_write(source, cloned_buffer, dest_file_length);
  249. free(cloned_buffer);
  250. storage_file_close(source);
  251. storage_file_close(dest);
  252. return SUCCESSFUL;
  253. }
  254. } else {
  255. size_t source_file_length = storage_file_size(source);
  256. storage_file_close(source);
  257. mf_classic_key_cache_load(instance, uid, uid_len);
  258. if(KEY_MASK_BIT_CHECK(key_mask_a_required, instance->keys.key_a_mask) &&
  259. KEY_MASK_BIT_CHECK(key_mask_b_required, instance->keys.key_b_mask)) {
  260. FURI_LOG_I("TAG", "cache exist, creating assets cache if not already exists");
  261. storage_file_close(dest);
  262. storage_file_close(source);
  263. storage_file_open(dest, dest_path, FSAM_WRITE, FSOM_OPEN_ALWAYS);
  264. storage_file_open(source, source_path, FSAM_READ, FSOM_OPEN_EXISTING);
  265. FURI_LOG_I("TAG", "creating assets cache");
  266. /*-----Clone keyfile from assets to cache (creates temporary buffer)----*/
  267. uint8_t* cloned_buffer = malloc(source_file_length);
  268. storage_file_read(source, cloned_buffer, source_file_length);
  269. storage_file_write(dest, cloned_buffer, source_file_length);
  270. free(cloned_buffer);
  271. storage_file_close(source);
  272. storage_file_close(dest);
  273. return SUCCESSFUL;
  274. } else {
  275. FURI_LOG_I("TAG", "incomplete cache file, aborting.");
  276. storage_file_close(source);
  277. storage_file_close(dest);
  278. return INCOMPLETE_KEYFILE;
  279. }
  280. }
  281. FURI_LOG_I("TAG", "proceeding to read");
  282. storage_file_close(source);
  283. storage_file_close(dest);
  284. }
  285. void uid_to_string(const uint8_t* uid, size_t uid_len, char* uid_str, size_t max_len) {
  286. size_t pos = 0;
  287. for(size_t i = 0; i < uid_len && pos + 2 < max_len; ++i) {
  288. pos += snprintf(&uid_str[pos], max_len - pos, "%02X", uid[i]);
  289. }
  290. uid_str[pos] = '\0'; // Null-terminate the string
  291. }
  292. void handle_keyfile_case(
  293. Metroflip* app,
  294. const char* message_title,
  295. const char* log_message,
  296. FuriString* parsed_data,
  297. char card_type[]) {
  298. FURI_LOG_I(card_type, log_message);
  299. dolphin_deed(DolphinDeedNfcReadSuccess);
  300. furi_string_reset(parsed_data);
  301. furi_string_printf(
  302. parsed_data,
  303. "\e#%s\n\n"
  304. "To read a %s, \nyou need to read \nit in NFC "
  305. "app on \nthe flipper, and it\nneeds to show \n32/32 keys and\n"
  306. "16/16 sectors read\n"
  307. "Here is a guide to \nfollow to read \nMIFARE Classic:\n"
  308. "https://flipper.wiki/mifareclassic/\n"
  309. "Once completed, Scan again\n\n",
  310. message_title,
  311. card_type);
  312. widget_add_text_scroll_element(app->widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  313. widget_add_button_element(
  314. app->widget, GuiButtonTypeRight, "Exit", metroflip_exit_widget_callback, app);
  315. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  316. metroflip_app_blink_stop(app);
  317. }
  318. void metroflip_plugin_manager_alloc(Metroflip* app) {
  319. app->resolver = composite_api_resolver_alloc();
  320. composite_api_resolver_add(app->resolver, firmware_api_interface);
  321. composite_api_resolver_add(app->resolver, metroflip_api_interface);
  322. app->plugin_manager = plugin_manager_alloc(
  323. METROFLIP_SUPPORTED_CARD_PLUGIN_APP_ID,
  324. METROFLIP_SUPPORTED_CARD_PLUGIN_API_VERSION,
  325. composite_api_resolver_get(app->resolver));
  326. }