metroflip.c 13 KB

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