nfc_maker_scene_result.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include "../nfc_maker.h"
  2. #include <furi_hal_random.h>
  3. enum PopupEvent {
  4. PopupEventExit,
  5. };
  6. static void nfc_maker_scene_result_popup_callback(void* context) {
  7. NfcMaker* app = context;
  8. view_dispatcher_send_custom_event(app->view_dispatcher, PopupEventExit);
  9. }
  10. void nfc_maker_scene_result_on_enter(void* context) {
  11. NfcMaker* app = context;
  12. Popup* popup = app->popup;
  13. bool success = false;
  14. FlipperFormat* file = flipper_format_file_alloc(furi_record_open(RECORD_STORAGE));
  15. FuriString* path = furi_string_alloc();
  16. furi_string_printf(path, NFC_MK_APP_FOLDER "/%s" NFC_MK_APP_EXTENSION, app->save_buf);
  17. uint32_t pages = 135;
  18. size_t size = pages * 4;
  19. uint8_t* buf = malloc(size);
  20. do {
  21. if(!flipper_format_file_open_new(file, furi_string_get_cstr(path))) break;
  22. if(!flipper_format_write_header_cstr(file, "Flipper NFC device", 4)) break;
  23. if(!flipper_format_write_string_cstr(file, "Device type", "NTAG/Ultralight")) break;
  24. // Serial number
  25. size_t i = 0;
  26. buf[i++] = 0x04;
  27. furi_hal_random_fill_buf(&buf[i], 8);
  28. i += 8;
  29. uint8_t uid[7];
  30. memcpy(&uid[0], &buf[0], 3);
  31. memcpy(&uid[3], &buf[4], 4);
  32. if(!flipper_format_write_hex(file, "UID", uid, sizeof(uid))) break;
  33. if(!flipper_format_write_string_cstr(file, "ATQA", "00 44")) break;
  34. if(!flipper_format_write_string_cstr(file, "SAK", "00")) break;
  35. if(!flipper_format_write_string_cstr(file, "Data format version", "2")) break;
  36. if(!flipper_format_write_string_cstr(file, "NTAG/Ultralight type", "NTAG215")) break;
  37. // TODO: Maybe randomize?
  38. if(!flipper_format_write_string_cstr(
  39. file,
  40. "Signature",
  41. "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"))
  42. break;
  43. if(!flipper_format_write_string_cstr(file, "Mifare version", "00 04 04 02 01 00 11 03"))
  44. break;
  45. if(!flipper_format_write_string_cstr(file, "Counter 0", "0")) break;
  46. if(!flipper_format_write_string_cstr(file, "Tearing 0", "00")) break;
  47. if(!flipper_format_write_string_cstr(file, "Counter 1", "0")) break;
  48. if(!flipper_format_write_string_cstr(file, "Tearing 1", "00")) break;
  49. if(!flipper_format_write_string_cstr(file, "Counter 2", "0")) break;
  50. if(!flipper_format_write_string_cstr(file, "Tearing 2", "00")) break;
  51. if(!flipper_format_write_uint32(file, "Pages total", &pages, 1)) break;
  52. if(!flipper_format_write_uint32(file, "Pages read", &pages, 1)) break;
  53. // Static data
  54. buf[i++] = 0x48; // Internal
  55. buf[i++] = 0x00; // Lock bytes
  56. buf[i++] = 0x00; // ...
  57. buf[i++] = 0xE1; // Capability container
  58. buf[i++] = 0x10; // ...
  59. buf[i++] = 0x3E; // ...
  60. buf[i++] = 0x00; // ...
  61. buf[i++] = 0x03; // NDEF TLV block
  62. // NDEF Docs: https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/protocols/nfc/index.html#nfc-data-exchange-format-ndef
  63. uint8_t tnf = 0x00;
  64. const char* type = "";
  65. uint8_t* payload = NULL;
  66. size_t payload_len = 0;
  67. size_t data_len = 0;
  68. size_t j = 0;
  69. switch(scene_manager_get_scene_state(app->scene_manager, NfcMakerSceneStart)) {
  70. case NfcMakerSceneBluetooth: {
  71. tnf = 0x02; // Media-type [RFC 2046]
  72. type = "application/vnd.bluetooth.ep.oob";
  73. data_len = MAC_INPUT_LEN;
  74. payload_len = data_len + 2;
  75. payload = malloc(payload_len);
  76. payload[j++] = 0x08;
  77. payload[j++] = 0x00;
  78. memcpy(&payload[j], app->mac_buf, data_len);
  79. j += data_len;
  80. break;
  81. }
  82. case NfcMakerSceneContact: {
  83. tnf = 0x02; // Media-type [RFC 2046]
  84. type = "text/vcard";
  85. FuriString* vcard = furi_string_alloc_set("BEGIN:VCARD\r\nVERSION:3.0\r\n");
  86. furi_string_cat_printf(
  87. vcard, "PRODID:-//Flipper Xtreme//%s//EN\r\n", version_get_version(NULL));
  88. furi_string_cat_printf(vcard, "N:%s;%s;;;\r\n", app->small_buf2, app->small_buf1);
  89. furi_string_cat_printf(
  90. vcard,
  91. "FN:%s%s%s\r\n",
  92. app->small_buf1,
  93. newstrnlen(app->small_buf2, SMALL_INPUT_LEN) ? " " : "",
  94. app->small_buf2);
  95. if(newstrnlen(app->mail_buf, MAIL_INPUT_LEN)) {
  96. furi_string_cat_printf(vcard, "EMAIL:%s\r\n", app->mail_buf);
  97. }
  98. if(newstrnlen(app->phone_buf, PHONE_INPUT_LEN)) {
  99. furi_string_cat_printf(vcard, "TEL:%s\r\n", app->phone_buf);
  100. }
  101. if(newstrnlen(app->big_buf, BIG_INPUT_LEN)) {
  102. furi_string_cat_printf(vcard, "URL:%s\r\n", app->big_buf);
  103. }
  104. furi_string_cat_printf(vcard, "END:VCARD\r\n");
  105. payload_len = furi_string_size(vcard);
  106. payload = malloc(payload_len);
  107. memcpy(payload, furi_string_get_cstr(vcard), payload_len);
  108. furi_string_free(vcard);
  109. break;
  110. }
  111. case NfcMakerSceneHttps: {
  112. tnf = 0x01; // NFC Forum well-known type [NFC RTD]
  113. type = "U";
  114. data_len = newstrnlen(app->big_buf, BIG_INPUT_LEN);
  115. payload_len = data_len + 1;
  116. payload = malloc(payload_len);
  117. payload[j++] = 0x04; // Prepend "https://"
  118. memcpy(&payload[j], app->big_buf, data_len);
  119. j += data_len;
  120. break;
  121. }
  122. case NfcMakerSceneMail: {
  123. tnf = 0x01; // NFC Forum well-known type [NFC RTD]
  124. type = "U";
  125. data_len = newstrnlen(app->mail_buf, MAIL_INPUT_LEN);
  126. payload_len = data_len + 1;
  127. payload = malloc(payload_len);
  128. payload[j++] = 0x06; // Prepend "mailto:"
  129. memcpy(&payload[j], app->mail_buf, data_len);
  130. j += data_len;
  131. break;
  132. }
  133. case NfcMakerScenePhone: {
  134. tnf = 0x01; // NFC Forum well-known type [NFC RTD]
  135. type = "U";
  136. data_len = newstrnlen(app->phone_buf, PHONE_INPUT_LEN);
  137. payload_len = data_len + 1;
  138. payload = malloc(payload_len);
  139. payload[j++] = 0x05; // Prepend "tel:"
  140. memcpy(&payload[j], app->phone_buf, data_len);
  141. j += data_len;
  142. break;
  143. }
  144. case NfcMakerSceneText: {
  145. tnf = 0x01; // NFC Forum well-known type [NFC RTD]
  146. type = "T";
  147. data_len = newstrnlen(app->big_buf, BIG_INPUT_LEN);
  148. payload_len = data_len + 3;
  149. payload = malloc(payload_len);
  150. payload[j++] = 0x02;
  151. payload[j++] = 'e';
  152. payload[j++] = 'n';
  153. memcpy(&payload[j], app->big_buf, data_len);
  154. j += data_len;
  155. break;
  156. }
  157. case NfcMakerSceneUrl: {
  158. tnf = 0x01; // NFC Forum well-known type [NFC RTD]
  159. type = "U";
  160. data_len = newstrnlen(app->big_buf, BIG_INPUT_LEN);
  161. payload_len = data_len + 1;
  162. payload = malloc(payload_len);
  163. payload[j++] = 0x00; // No prepend
  164. memcpy(&payload[j], app->big_buf, data_len);
  165. j += data_len;
  166. break;
  167. }
  168. case NfcMakerSceneWifi: {
  169. tnf = 0x02; // Media-type [RFC 2046]
  170. type = "application/vnd.wfa.wsc";
  171. uint8_t ssid_len = newstrnlen(app->small_buf1, SMALL_INPUT_LEN);
  172. uint8_t pass_len = newstrnlen(app->small_buf2, SMALL_INPUT_LEN);
  173. uint8_t data_len = ssid_len + pass_len;
  174. payload_len = data_len + 39;
  175. payload = malloc(payload_len);
  176. payload[j++] = 0x10;
  177. payload[j++] = 0x0E;
  178. payload[j++] = 0x00;
  179. payload[j++] = data_len + 43;
  180. payload[j++] = 0x10;
  181. payload[j++] = 0x26;
  182. payload[j++] = 0x00;
  183. payload[j++] = 0x01;
  184. payload[j++] = 0x01;
  185. payload[j++] = 0x10;
  186. payload[j++] = 0x45;
  187. payload[j++] = 0x00;
  188. payload[j++] = ssid_len;
  189. memcpy(&payload[j], app->small_buf1, ssid_len);
  190. j += ssid_len;
  191. payload[j++] = 0x10;
  192. payload[j++] = 0x03;
  193. payload[j++] = 0x00;
  194. payload[j++] = 0x02;
  195. payload[j++] = 0x00;
  196. payload[j++] =
  197. scene_manager_get_scene_state(app->scene_manager, NfcMakerSceneWifiAuth);
  198. payload[j++] = 0x10;
  199. payload[j++] = 0x0F;
  200. payload[j++] = 0x00;
  201. payload[j++] = 0x02;
  202. payload[j++] = 0x00;
  203. payload[j++] =
  204. scene_manager_get_scene_state(app->scene_manager, NfcMakerSceneWifiEncr);
  205. payload[j++] = 0x10;
  206. payload[j++] = 0x27;
  207. payload[j++] = 0x00;
  208. payload[j++] = pass_len;
  209. memcpy(&payload[j], app->small_buf2, pass_len);
  210. j += pass_len;
  211. payload[j++] = 0x10;
  212. payload[j++] = 0x20;
  213. payload[j++] = 0x00;
  214. payload[j++] = 0x06;
  215. payload[j++] = 0xFF;
  216. payload[j++] = 0xFF;
  217. payload[j++] = 0xFF;
  218. payload[j++] = 0xFF;
  219. payload[j++] = 0xFF;
  220. payload[j++] = 0xFF;
  221. break;
  222. }
  223. default:
  224. break;
  225. }
  226. // Record header
  227. uint8_t flags = 0;
  228. flags |= 1 << 7; // MB (Message Begin)
  229. flags |= 1 << 6; // ME (Message End)
  230. flags |= tnf; // TNF (Type Name Format)
  231. size_t type_len = strlen(type);
  232. size_t header_len = 0;
  233. header_len += 1; // Flags and TNF
  234. header_len += 1; // Type length
  235. if(payload_len < 0xFF) {
  236. flags |= 1 << 4; // SR (Short Record)
  237. header_len += 1; // Payload length
  238. } else {
  239. header_len += 4; // Payload length
  240. }
  241. header_len += type_len; // Payload type
  242. size_t record_len = header_len + payload_len;
  243. if(record_len < 0xFF) {
  244. buf[i++] = record_len; // TLV length
  245. } else {
  246. buf[i++] = 0xFF; // TLV length
  247. buf[i++] = record_len >> 8; // ...
  248. buf[i++] = record_len & 0xFF; // ...
  249. }
  250. buf[i++] = flags; // Flags and TNF
  251. buf[i++] = type_len; // Type length
  252. if(flags & (1 << 4)) { // SR (Short Record)
  253. buf[i++] = payload_len; // Payload length
  254. } else {
  255. buf[i++] = 0x00; // Payload length
  256. buf[i++] = 0x00; // ...
  257. buf[i++] = payload_len >> 8; // ...
  258. buf[i++] = payload_len & 0xFF; // ...
  259. }
  260. memcpy(&buf[i], type, type_len); // Payload type
  261. i += type_len;
  262. // Record payload
  263. memcpy(&buf[i], payload, payload_len);
  264. i += payload_len;
  265. free(payload);
  266. // Record terminator
  267. buf[i++] = 0xFE;
  268. // Padding until last 5 pages
  269. for(; i < size - 20; i++) {
  270. buf[i] = 0x00;
  271. }
  272. // Last 5 static pages
  273. buf[i++] = 0x00;
  274. buf[i++] = 0x00;
  275. buf[i++] = 0x00;
  276. buf[i++] = 0xBD;
  277. buf[i++] = 0x04;
  278. buf[i++] = 0x00;
  279. buf[i++] = 0x00;
  280. buf[i++] = 0xFF;
  281. buf[i++] = 0x00;
  282. buf[i++] = 0x05;
  283. buf[i++] = 0x00;
  284. buf[i++] = 0x00;
  285. buf[i++] = 0xFF;
  286. buf[i++] = 0xFF;
  287. buf[i++] = 0xFF;
  288. buf[i++] = 0xFF;
  289. buf[i++] = 0x00;
  290. buf[i++] = 0x00;
  291. buf[i++] = 0x00;
  292. buf[i++] = 0x00;
  293. // Write pages
  294. char str[16];
  295. bool ok = true;
  296. for(size_t page = 0; page < pages; page++) {
  297. snprintf(str, sizeof(str), "Page %u", page);
  298. if(!flipper_format_write_hex(file, str, &buf[page * 4], 4)) {
  299. ok = false;
  300. break;
  301. }
  302. }
  303. if(!ok) break;
  304. if(!flipper_format_write_string_cstr(file, "Failed authentication attempts", "0")) break;
  305. success = true;
  306. } while(false);
  307. free(buf);
  308. furi_string_free(path);
  309. flipper_format_free(file);
  310. furi_record_close(RECORD_STORAGE);
  311. if(success) {
  312. popup_set_icon(popup, 36, 5, &I_DolphinDone_80x58);
  313. popup_set_header(popup, "Saved!", 13, 22, AlignLeft, AlignBottom);
  314. } else {
  315. popup_set_icon(popup, 69, 15, &I_WarningDolphinFlip_45x42);
  316. popup_set_header(popup, "Error!", 13, 22, AlignLeft, AlignBottom);
  317. }
  318. popup_set_timeout(popup, 1500);
  319. popup_set_context(popup, app);
  320. popup_set_callback(popup, nfc_maker_scene_result_popup_callback);
  321. popup_enable_timeout(popup);
  322. view_dispatcher_switch_to_view(app->view_dispatcher, NfcMakerViewPopup);
  323. }
  324. bool nfc_maker_scene_result_on_event(void* context, SceneManagerEvent event) {
  325. NfcMaker* app = context;
  326. bool consumed = false;
  327. if(event.type == SceneManagerEventTypeCustom) {
  328. consumed = true;
  329. switch(event.event) {
  330. case PopupEventExit:
  331. scene_manager_search_and_switch_to_previous_scene(
  332. app->scene_manager, NfcMakerSceneStart);
  333. break;
  334. default:
  335. break;
  336. }
  337. }
  338. return consumed;
  339. }
  340. void nfc_maker_scene_result_on_exit(void* context) {
  341. NfcMaker* app = context;
  342. popup_reset(app->popup);
  343. }