metroflip.c 14 KB

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