flipbip_file.c 9.6 KB

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