flipbip_file.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "flipbip_file.h"
  2. #include <storage/storage.h>
  3. #include <loader/loader.h>
  4. #include "../helpers/flipbip_string.h"
  5. // From: lib/crypto
  6. #include <memzero.h>
  7. #include <rand.h>
  8. // #define FLIPBIP_APP_BASE_FOLDER APP_DATA_PATH("flipbip")
  9. #define FLIPBIP_APP_BASE_FOLDER EXT_PATH("apps_data/flipbip")
  10. #define FLIPBIP_APP_BASE_FOLDER_PATH(path) FLIPBIP_APP_BASE_FOLDER "/" path
  11. #define FLIPBIP_DAT_FILE_NAME ".flipbip.dat"
  12. // #define FLIPBIP_DAT_FILE_NAME ".flipbip.dat.txt"
  13. #define FLIPBIP_DAT_FILE_NAME_BAK ".flipbip.dat.bak"
  14. #define FLIPBIP_KEY_FILE_NAME ".flipbip.key"
  15. // #define FLIPBIP_KEY_FILE_NAME ".flipbip.key.txt"
  16. #define FLIPBIP_KEY_FILE_NAME_BAK ".flipbip.key.bak"
  17. #define FLIPBIP_DAT_PATH FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_DAT_FILE_NAME)
  18. #define FLIPBIP_DAT_PATH_BAK FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_DAT_FILE_NAME_BAK)
  19. #define FLIPBIP_KEY_PATH FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_KEY_FILE_NAME)
  20. #define FLIPBIP_KEY_PATH_BAK FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_KEY_FILE_NAME_BAK)
  21. const char* TEXT_QRFILE = "Filetype: QRCode\n"
  22. "Version: 0\n"
  23. "Message: "; // 37 chars + 1 null
  24. #define FILE_HLEN 4
  25. #define FILE_KLEN 256
  26. #define FILE_SLEN 512
  27. #define FILE_MAX_PATH_LEN 48
  28. #define FILE_MAX_QRFILE_CONTENT 90
  29. const char* FILE_HSTR = "fb01";
  30. const char* FILE_K1 = "fb0131d5cf688221c109163908ebe51debb46227c6cc8b37641910833222772a"
  31. "baefe6d9ceb651842260e0d1e05e3b90d15e7d5ffaaabc0207bf200a117793a2";
  32. bool flipbip_load_file(
  33. char* settings,
  34. size_t slen,
  35. const FlipBipFile file_type,
  36. const char* file_name) {
  37. bool ret = false;
  38. const char* path;
  39. char path_buf[FILE_MAX_PATH_LEN] = {0};
  40. if(file_type == FlipBipFileKey) {
  41. path = FLIPBIP_KEY_PATH;
  42. } else if(file_type == FlipBipFileDat) {
  43. path = FLIPBIP_DAT_PATH;
  44. } else {
  45. strcpy(path_buf, FLIPBIP_APP_BASE_FOLDER); // 22
  46. strcpy(path_buf + strlen(path_buf), "/");
  47. strcpy(path_buf + strlen(path_buf), file_name);
  48. path = path_buf;
  49. }
  50. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  51. File* settings_file = storage_file_alloc(fs_api);
  52. if(storage_file_open(settings_file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  53. char chr;
  54. size_t i = 0;
  55. while((storage_file_read(settings_file, &chr, 1) == 1) &&
  56. !storage_file_eof(settings_file) && !isspace(chr)) {
  57. if(i < slen) {
  58. settings[i] = chr;
  59. }
  60. i++;
  61. }
  62. ret = true;
  63. } else {
  64. memzero(settings, strlen(settings));
  65. settings[0] = '\0';
  66. ret = false;
  67. }
  68. storage_file_close(settings_file);
  69. storage_file_free(settings_file);
  70. furi_record_close(RECORD_STORAGE);
  71. if(strlen(settings) > 0) {
  72. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  73. FileInfo layout_file_info;
  74. FS_Error file_check_err = storage_common_stat(fs_api, path, &layout_file_info);
  75. furi_record_close(RECORD_STORAGE);
  76. if(file_check_err != FSE_OK) {
  77. memzero(settings, strlen(settings));
  78. settings[0] = '\0';
  79. ret = false;
  80. }
  81. // if(layout_file_info.size != 256) {
  82. // memzero(settings, strlen(settings));
  83. // settings[0] = '\0';
  84. // }
  85. }
  86. return ret;
  87. }
  88. bool flipbip_has_file(const FlipBipFile file_type, const char* file_name, const bool remove) {
  89. bool ret = false;
  90. const char* path;
  91. char path_buf[FILE_MAX_PATH_LEN] = {0};
  92. if(file_type == FlipBipFileKey) {
  93. path = FLIPBIP_KEY_PATH;
  94. } else if(file_type == FlipBipFileDat) {
  95. path = FLIPBIP_DAT_PATH;
  96. } else {
  97. strcpy(path_buf, FLIPBIP_APP_BASE_FOLDER); // 22
  98. strcpy(path_buf + strlen(path_buf), "/");
  99. strcpy(path_buf + strlen(path_buf), file_name);
  100. path = path_buf;
  101. }
  102. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  103. if(remove) {
  104. ret = storage_simply_remove(fs_api, path);
  105. } else {
  106. ret = storage_file_exists(fs_api, path);
  107. }
  108. furi_record_close(RECORD_STORAGE);
  109. return ret;
  110. }
  111. bool flipbip_save_file(
  112. const char* settings,
  113. const FlipBipFile file_type,
  114. const char* file_name,
  115. const bool append) {
  116. bool ret = false;
  117. const char* path;
  118. const char* path_bak;
  119. char path_buf[FILE_MAX_PATH_LEN] = {0};
  120. if(file_type == FlipBipFileKey) {
  121. path = FLIPBIP_KEY_PATH;
  122. path_bak = FLIPBIP_KEY_PATH_BAK;
  123. } else if(file_type == FlipBipFileDat) {
  124. path = FLIPBIP_DAT_PATH;
  125. path_bak = FLIPBIP_DAT_PATH_BAK;
  126. } else {
  127. strcpy(path_buf, FLIPBIP_APP_BASE_FOLDER); // 22
  128. strcpy(path_buf + strlen(path_buf), "/");
  129. strcpy(path_buf + strlen(path_buf), file_name);
  130. path = path_buf;
  131. path_bak = NULL;
  132. }
  133. int open_mode = FSOM_OPEN_ALWAYS;
  134. if(append) {
  135. open_mode = FSOM_OPEN_APPEND;
  136. }
  137. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  138. // // if the key file exists, we don't want to overwrite it
  139. // if (key_file && storage_file_exists(fs_api, path)) {
  140. // furi_record_close(RECORD_STORAGE);
  141. // ret = true;
  142. // return ret;
  143. // }
  144. // try to create the folder
  145. storage_simply_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER);
  146. File* settings_file = storage_file_alloc(fs_api);
  147. if(storage_file_open(settings_file, path, FSAM_WRITE, open_mode)) {
  148. storage_file_write(settings_file, settings, strlen(settings));
  149. storage_file_write(settings_file, "\n", 1);
  150. ret = true;
  151. }
  152. storage_file_close(settings_file);
  153. storage_file_free(settings_file);
  154. if(path_bak != NULL) {
  155. File* settings_file_bak = storage_file_alloc(fs_api);
  156. if(storage_file_open(settings_file_bak, path_bak, FSAM_WRITE, open_mode)) {
  157. storage_file_write(settings_file_bak, settings, strlen(settings));
  158. storage_file_write(settings_file_bak, "\n", 1);
  159. }
  160. storage_file_close(settings_file_bak);
  161. storage_file_free(settings_file_bak);
  162. }
  163. furi_record_close(RECORD_STORAGE);
  164. return ret;
  165. }
  166. bool flipbip_save_qrfile(
  167. const char* qr_msg_prefix,
  168. const char* qr_msg_content,
  169. const char* file_name) {
  170. char qr_buf[FILE_MAX_QRFILE_CONTENT] = {0};
  171. strcpy(qr_buf, TEXT_QRFILE);
  172. strcpy(qr_buf + strlen(qr_buf), qr_msg_prefix);
  173. strcpy(qr_buf + strlen(qr_buf), qr_msg_content);
  174. return flipbip_save_file(qr_buf, FlipBipFileOther, file_name, false);
  175. }
  176. bool flipbip_load_file_secure(char* settings) {
  177. const size_t dlen = FILE_HLEN + FILE_SLEN + 1;
  178. // allocate memory for key/data
  179. char* data = malloc(dlen);
  180. memzero(data, dlen);
  181. // load k2 from file
  182. if(!flipbip_load_file(data, dlen, FlipBipFileKey, NULL)) return false;
  183. // check header
  184. if(data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] ||
  185. data[3] != FILE_HSTR[3]) {
  186. memzero(data, dlen);
  187. free(data);
  188. return false;
  189. }
  190. // seek --> header
  191. data += FILE_HLEN;
  192. // prepare k1
  193. uint8_t k1[64];
  194. flipbip_xtob(FILE_K1, k1, strlen(FILE_K1) / 2);
  195. // load k2 from file buffer (secured by k1)
  196. flipbip_cipher(k1, strlen(FILE_K1) / 2, data, data, FILE_KLEN);
  197. uint8_t k2[128];
  198. flipbip_xtob(data, k2, FILE_KLEN / 2);
  199. // zero k2 buffer
  200. memzero(data, FILE_KLEN);
  201. // seek <-- header
  202. data -= FILE_HLEN;
  203. // load data from file
  204. if(!flipbip_load_file(data, dlen, FlipBipFileDat, NULL)) return false;
  205. // check header
  206. if(data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] ||
  207. data[3] != FILE_HSTR[3]) {
  208. memzero(data, dlen);
  209. free(data);
  210. memzero(k1, strlen(FILE_K1) / 2);
  211. memzero(k2, FILE_KLEN / 2);
  212. return false;
  213. }
  214. // seek --> header
  215. data += FILE_HLEN;
  216. // load settings from file buffer (secured by k2)
  217. flipbip_cipher(k2, FILE_KLEN / 2, data, data, FILE_SLEN);
  218. flipbip_xtob(data, (unsigned char*)data, FILE_SLEN / 2);
  219. // copy to output
  220. strcpy(settings, data);
  221. // seek <-- header
  222. data -= FILE_HLEN;
  223. // clear memory
  224. memzero(data, dlen);
  225. free(data);
  226. memzero(k1, strlen(FILE_K1) / 2);
  227. memzero(k2, FILE_KLEN / 2);
  228. return true;
  229. }
  230. bool flipbip_save_file_secure(const char* settings) {
  231. const size_t dlen = FILE_HLEN + FILE_SLEN + 1;
  232. // cap settings to 256 bytes
  233. size_t len = strlen(settings);
  234. if(len > (FILE_SLEN / 2)) len = FILE_SLEN / 2;
  235. // allocate memory for key/data
  236. char* data = malloc(dlen);
  237. memzero(data, dlen);
  238. // write header
  239. strncpy(data, FILE_HSTR, FILE_HLEN);
  240. // seek --> header
  241. data += FILE_HLEN;
  242. // prepare k1
  243. uint8_t k1[64];
  244. flipbip_xtob(FILE_K1, k1, strlen(FILE_K1) / 2);
  245. // generate k2
  246. uint8_t k2[128];
  247. random_buffer(k2, FILE_KLEN / 2);
  248. // write k2 to file buffer (secured by k1)
  249. flipbip_btox(k2, FILE_KLEN / 2, data);
  250. flipbip_cipher(k1, strlen(FILE_K1) / 2, data, data, FILE_KLEN);
  251. // seek <-- header
  252. data -= FILE_HLEN;
  253. // save k2 to file
  254. flipbip_save_file(data, FlipBipFileKey, NULL, false);
  255. // seek --> header
  256. data += FILE_HLEN;
  257. // zero k2 memory
  258. memzero(data, FILE_KLEN);
  259. // write settings to file buffer (secured by k2)
  260. flipbip_btox((uint8_t*)settings, len, data);
  261. flipbip_cipher(k2, FILE_KLEN / 2, data, data, FILE_SLEN);
  262. // seek <-- header
  263. data -= FILE_HLEN;
  264. // save data to file
  265. flipbip_save_file(data, FlipBipFileDat, NULL, false);
  266. // clear memory
  267. memzero(data, dlen);
  268. free(data);
  269. memzero(k1, strlen(FILE_K1) / 2);
  270. memzero(k2, FILE_KLEN / 2);
  271. return true;
  272. }