flipbip_file.c 9.3 KB

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