flipbip_file.c 9.3 KB

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