metroflip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  125. scene_manager_set_scene_state(app->scene_manager, MetroflipSceneStart, MetroflipSceneAuto);
  126. }
  127. }
  128. void metroflip_save_widget_callback(GuiButtonType result, InputType type, void* context) {
  129. Metroflip* app = context;
  130. UNUSED(result);
  131. if(type == InputTypeShort) {
  132. scene_manager_next_scene(app->scene_manager, MetroflipSceneSave);
  133. }
  134. }
  135. void metroflip_delete_widget_callback(GuiButtonType result, InputType type, void* context) {
  136. Metroflip* app = context;
  137. UNUSED(result);
  138. if(type == InputTypeShort) {
  139. scene_manager_next_scene(app->scene_manager, MetroflipSceneDelete);
  140. }
  141. }
  142. // Calypso
  143. void byte_to_binary(uint8_t byte, char* bits) {
  144. for(int i = 7; i >= 0; i--) {
  145. bits[7 - i] = (byte & (1 << i)) ? '1' : '0';
  146. }
  147. bits[8] = '\0';
  148. }
  149. int binary_to_decimal(const char binary[]) {
  150. int decimal = 0;
  151. int length = strlen(binary);
  152. for(int i = 0; i < length; i++) {
  153. decimal = decimal * 2 + (binary[i] - '0');
  154. }
  155. return decimal;
  156. }
  157. void locale_format_datetime_cat(FuriString* out, const DateTime* dt, bool time) {
  158. // helper to print datetimes
  159. FuriString* s = furi_string_alloc();
  160. LocaleDateFormat date_format = locale_get_date_format();
  161. const char* separator = (date_format == LocaleDateFormatDMY) ? "." : "/";
  162. locale_format_date(s, dt, date_format, separator);
  163. furi_string_cat(out, s);
  164. if(time) {
  165. locale_format_time(s, dt, locale_get_time_format(), false);
  166. furi_string_cat_printf(out, " ");
  167. furi_string_cat(out, s);
  168. }
  169. furi_string_free(s);
  170. }
  171. // read file
  172. uint8_t read_file[5] = {0x94, 0xb2, 0x01, 0x04, 0x1D};
  173. // ^^^
  174. // |||
  175. // FID
  176. // select app
  177. uint8_t select_app[8] = {0x94, 0xA4, 0x00, 0x00, 0x02, 0x20, 0x00, 0x00};
  178. // ^^^^^^^^^
  179. // |||||||||
  180. // AID: 20XX
  181. uint8_t apdu_success[2] = {0x90, 0x00};
  182. char* bit_slice(const char* bit_representation, int start, int end) {
  183. static char bit_slice[32 * 8 + 1];
  184. strncpy(bit_slice, bit_representation + start, end - start + 1);
  185. bit_slice[end - start + 1] = '\0';
  186. return bit_slice;
  187. }
  188. int bit_slice_to_dec(const char* bit_representation, int start, int end) {
  189. return binary_to_decimal(bit_slice(bit_representation, start, end));
  190. }
  191. extern int32_t metroflip(void* p) {
  192. UNUSED(p);
  193. /*
  194. plugin_manager_load_single(PluginManager * manager, const char* path)
  195. uint32_t plugin_count = plugin_manager_get_count(manager);
  196. FURI_LOG_I(TAG, "Loaded %lu plugin(s)", plugin_count);
  197. for(uint32_t i = 0; i < plugin_count; i++) {
  198. const MetroflipPlugin* plugin = plugin_manager_get_ep(manager, i);
  199. FURI_LOG_I(TAG, "plugin name: %s", plugin->name);
  200. }
  201. */
  202. Metroflip* app = metroflip_alloc();
  203. scene_manager_set_scene_state(app->scene_manager, MetroflipSceneStart, MetroflipSceneAuto);
  204. scene_manager_next_scene(app->scene_manager, MetroflipSceneStart);
  205. view_dispatcher_run(app->view_dispatcher);
  206. metroflip_free(app);
  207. return 0;
  208. }
  209. KeyfileManager manage_keyfiles(
  210. char uid_str[],
  211. const uint8_t* uid,
  212. size_t uid_len,
  213. MfClassicKeyCache* instance,
  214. uint64_t key_mask_a_required,
  215. uint64_t key_mask_b_required) {
  216. Storage* storage = furi_record_open(RECORD_STORAGE);
  217. File* source = storage_file_alloc(storage);
  218. char source_path[64];
  219. UNUSED(key_mask_b_required);
  220. FURI_LOG_I("TAG", "%s", uid_str);
  221. size_t source_required_size =
  222. strlen("/ext/nfc/.cache/") + strlen(uid_str) + strlen(".keys") + 1;
  223. snprintf(source_path, source_required_size, "/ext/nfc/.cache/%s.keys", uid_str);
  224. bool cache_file = storage_file_open(source, source_path, FSAM_READ, FSOM_OPEN_EXISTING);
  225. /*-----------------Open assets cache file (if exists)------------*/
  226. File* dest = storage_file_alloc(storage);
  227. char dest_path[64];
  228. size_t dest_required_size =
  229. strlen("/ext/nfc/assets/.") + strlen(uid_str) + strlen(".keys") + 1;
  230. snprintf(dest_path, dest_required_size, "/ext/nfc/assets/.%s.keys", uid_str);
  231. bool dest_cache_file = storage_file_open(dest, dest_path, FSAM_READ, FSOM_OPEN_EXISTING);
  232. /*-----------------Check cache file------------*/
  233. if(!cache_file) {
  234. /*-----------------Check assets cache file------------*/
  235. FURI_LOG_I("TAG", "cache dont exist, checking assets");
  236. if(!dest_cache_file) {
  237. FURI_LOG_I("TAG", "assets dont exist, prompting user to fix..");
  238. storage_file_close(source);
  239. storage_file_close(dest);
  240. return MISSING_KEYFILE;
  241. } else {
  242. size_t dest_file_length = storage_file_size(dest);
  243. FURI_LOG_I(
  244. "TAG", "assets exist, but cache doesnt, proceeding to copy assets to cache");
  245. // Close, then open both files
  246. storage_file_close(source);
  247. storage_file_close(dest);
  248. storage_file_open(
  249. source, source_path, FSAM_WRITE, FSOM_OPEN_ALWAYS); // create new file
  250. storage_file_open(
  251. dest, dest_path, FSAM_READ, FSOM_OPEN_EXISTING); // open existing assets keyfile
  252. FURI_LOG_I("TAG", "creating cache file at %s from %s", source_path, dest_path);
  253. /*-----Clone keyfile from assets to cache (creates temporary buffer)----*/
  254. uint8_t* cloned_buffer = malloc(dest_file_length);
  255. storage_file_read(dest, cloned_buffer, dest_file_length);
  256. storage_file_write(source, cloned_buffer, dest_file_length);
  257. free(cloned_buffer);
  258. storage_file_close(source);
  259. storage_file_close(dest);
  260. return SUCCESSFUL;
  261. }
  262. } else {
  263. size_t source_file_length = storage_file_size(source);
  264. storage_file_close(source);
  265. mf_classic_key_cache_load(instance, uid, uid_len);
  266. if(KEY_MASK_BIT_CHECK(key_mask_a_required, instance->keys.key_a_mask) &&
  267. KEY_MASK_BIT_CHECK(key_mask_b_required, instance->keys.key_b_mask)) {
  268. FURI_LOG_I("TAG", "cache exist, creating assets cache if not already exists");
  269. storage_file_close(dest);
  270. storage_file_close(source);
  271. storage_file_open(dest, dest_path, FSAM_WRITE, FSOM_OPEN_ALWAYS);
  272. storage_file_open(source, source_path, FSAM_READ, FSOM_OPEN_EXISTING);
  273. FURI_LOG_I("TAG", "creating assets cache");
  274. /*-----Clone keyfile from assets to cache (creates temporary buffer)----*/
  275. uint8_t* cloned_buffer = malloc(source_file_length);
  276. storage_file_read(source, cloned_buffer, source_file_length);
  277. storage_file_write(dest, cloned_buffer, source_file_length);
  278. free(cloned_buffer);
  279. storage_file_close(source);
  280. storage_file_close(dest);
  281. return SUCCESSFUL;
  282. } else {
  283. FURI_LOG_I("TAG", "incomplete cache file, aborting.");
  284. storage_file_close(source);
  285. storage_file_close(dest);
  286. return INCOMPLETE_KEYFILE;
  287. }
  288. }
  289. FURI_LOG_I("TAG", "proceeding to read");
  290. storage_file_close(source);
  291. storage_file_close(dest);
  292. }
  293. void uid_to_string(const uint8_t* uid, size_t uid_len, char* uid_str, size_t max_len) {
  294. size_t pos = 0;
  295. for(size_t i = 0; i < uid_len && pos + 2 < max_len; ++i) {
  296. pos += snprintf(&uid_str[pos], max_len - pos, "%02X", uid[i]);
  297. }
  298. uid_str[pos] = '\0'; // Null-terminate the string
  299. }
  300. void handle_keyfile_case(
  301. Metroflip* app,
  302. const char* message_title,
  303. const char* log_message,
  304. FuriString* parsed_data,
  305. char card_type[]) {
  306. FURI_LOG_I(card_type, log_message);
  307. dolphin_deed(DolphinDeedNfcReadSuccess);
  308. furi_string_reset(parsed_data);
  309. furi_string_printf(
  310. parsed_data,
  311. "\e#%s\n\n"
  312. "To read a %s, \nyou need to read \nit in NFC "
  313. "app on \nthe flipper, and it\nneeds to show \n32/32 keys and\n"
  314. "16/16 sectors read\n"
  315. "Here is a guide to \nfollow to read \nMIFARE Classic:\n"
  316. "https://flipper.wiki/mifareclassic/\n"
  317. "Once completed, Scan again\n\n",
  318. message_title,
  319. card_type);
  320. widget_add_text_scroll_element(app->widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  321. widget_add_button_element(
  322. app->widget, GuiButtonTypeRight, "Exit", metroflip_exit_widget_callback, app);
  323. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  324. metroflip_app_blink_stop(app);
  325. }
  326. void metroflip_plugin_manager_alloc(Metroflip* app) {
  327. app->resolver = composite_api_resolver_alloc();
  328. composite_api_resolver_add(app->resolver, firmware_api_interface);
  329. composite_api_resolver_add(app->resolver, metroflip_api_interface);
  330. app->plugin_manager = plugin_manager_alloc(
  331. METROFLIP_SUPPORTED_CARD_PLUGIN_APP_ID,
  332. METROFLIP_SUPPORTED_CARD_PLUGIN_API_VERSION,
  333. composite_api_resolver_get(app->resolver));
  334. }