flipbip_file.c 9.5 KB

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