flipbip_file.c 9.5 KB

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