11#include "flipbip_file.h"
2- #include "../helpers/flipbip_string.h"
3-
4- #include "../crypto/memzero.h"
5- #include "../crypto/rand.h"
6-
72#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>
89
10+ // #define FLIPBIP_APP_BASE_FOLDER APP_DATA_PATH("flipbip")
911#define FLIPBIP_APP_BASE_FOLDER EXT_PATH("apps_data/flipbip")
12+ #define FLIPBIP_APP_BASE_FOLDER_PATH (path ) FLIPBIP_APP_BASE_FOLDER "/" path
1013#define FLIPBIP_DAT_FILE_NAME ".flipbip.dat"
1114// #define FLIPBIP_DAT_FILE_NAME ".flipbip.dat.txt"
1215#define FLIPBIP_DAT_FILE_NAME_BAK ".flipbip.dat.bak"
1316#define FLIPBIP_KEY_FILE_NAME ".flipbip.key"
1417// #define FLIPBIP_KEY_FILE_NAME ".flipbip.key.txt"
1518#define FLIPBIP_KEY_FILE_NAME_BAK ".flipbip.key.bak"
16- #define FLIPBIP_DAT_PATH FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_DAT_FILE_NAME
17- #define FLIPBIP_DAT_PATH_BAK FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_DAT_FILE_NAME_BAK
18- #define FLIPBIP_KEY_PATH FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_KEY_FILE_NAME
19- #define FLIPBIP_KEY_PATH_BAK FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_KEY_FILE_NAME_BAK
20-
21- const size_t FILE_HLEN = 4 ;
22- const size_t FILE_KLEN = 256 ;
23- const size_t FILE_SLEN = 512 ;
19+ #define FLIPBIP_DAT_PATH FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_DAT_FILE_NAME)
20+ #define FLIPBIP_DAT_PATH_BAK FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_DAT_FILE_NAME_BAK)
21+ #define FLIPBIP_KEY_PATH FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_KEY_FILE_NAME)
22+ #define FLIPBIP_KEY_PATH_BAK FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_KEY_FILE_NAME_BAK)
23+
24+ const char * TEXT_QRFILE = "Filetype: QRCode\n"
25+ "Version: 0\n"
26+ "Message: " ; // 37 chars + 1 null
27+ #define FILE_HLEN 4
28+ #define FILE_KLEN 256
29+ #define FILE_SLEN 512
30+ #define FILE_MAX_PATH_LEN 48
2431const char * FILE_HSTR = "fb01" ;
2532const char * FILE_K1 = "fb0131d5cf688221c109163908ebe51debb46227c6cc8b37641910833222772a"
2633 "baefe6d9ceb651842260e0d1e05e3b90d15e7d5ffaaabc0207bf200a117793a2" ;
2734
28- bool flipbip_load_settings (char * settings , bool key_file ) {
35+ bool flipbip_load_file (char * settings , const FlipBipFile file_type , const char * file_name ) {
2936 bool ret = false;
3037 const char * path ;
31- if (key_file ) {
38+ if (file_type == FlipBipFileKey ) {
3239 path = FLIPBIP_KEY_PATH ;
33- } else {
40+ } else if ( file_type == FlipBipFileDat ) {
3441 path = FLIPBIP_DAT_PATH ;
42+ } else {
43+ char path_buf [32 ] = {0 };
44+ strcpy (path_buf , FLIPBIP_APP_BASE_FOLDER );
45+ strcpy (path_buf + strlen (path_buf ), "/" );
46+ strcpy (path_buf + strlen (path_buf ), file_name );
47+ path = path_buf ;
3548 }
3649
3750 Storage * fs_api = furi_record_open (RECORD_STORAGE );
@@ -74,34 +87,53 @@ bool flipbip_load_settings(char* settings, bool key_file) {
7487 return ret ;
7588}
7689
77- bool flipbip_has_settings ( bool key_file ) {
90+ bool flipbip_has_file ( const FlipBipFile file_type , const char * file_name , const bool remove ) {
7891 bool ret = false;
7992 const char * path ;
80- if (key_file ) {
93+ if (file_type == FlipBipFileKey ) {
8194 path = FLIPBIP_KEY_PATH ;
82- } else {
95+ } else if ( file_type == FlipBipFileDat ) {
8396 path = FLIPBIP_DAT_PATH ;
97+ } else {
98+ char path_buf [32 ] = {0 };
99+ strcpy (path_buf , FLIPBIP_APP_BASE_FOLDER );
100+ strcpy (path_buf + strlen (path_buf ), "/" );
101+ strcpy (path_buf + strlen (path_buf ), file_name );
102+ path = path_buf ;
84103 }
85104
86105 Storage * fs_api = furi_record_open (RECORD_STORAGE );
87- if (storage_file_exists (fs_api , path )) {
88- ret = true;
106+ if (remove ) {
107+ ret = storage_simply_remove (fs_api , path );
108+ } else {
109+ ret = storage_file_exists (fs_api , path );
89110 }
90111 furi_record_close (RECORD_STORAGE );
91112
92113 return ret ;
93114}
94115
95- bool flipbip_save_settings (const char * settings , bool key_file , bool append ) {
116+ bool flipbip_save_file (
117+ const char * settings ,
118+ const FlipBipFile file_type ,
119+ const char * file_name ,
120+ const bool append ) {
96121 bool ret = false;
97122 const char * path ;
98123 const char * path_bak ;
99- if (key_file ) {
124+ if (file_type == FlipBipFileKey ) {
100125 path = FLIPBIP_KEY_PATH ;
101126 path_bak = FLIPBIP_KEY_PATH_BAK ;
102- } else {
127+ } else if ( file_type == FlipBipFileDat ) {
103128 path = FLIPBIP_DAT_PATH ;
104129 path_bak = FLIPBIP_DAT_PATH_BAK ;
130+ } else {
131+ char path_buf [FILE_MAX_PATH_LEN ] = {0 };
132+ strcpy (path_buf , FLIPBIP_APP_BASE_FOLDER ); // 22
133+ strcpy (path_buf + strlen (path_buf ), "/" );
134+ strcpy (path_buf + strlen (path_buf ), file_name );
135+ path = path_buf ;
136+ path_bak = NULL ;
105137 }
106138 int open_mode = FSOM_OPEN_ALWAYS ;
107139 if (append ) {
@@ -116,7 +148,7 @@ bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
116148 // return ret;
117149 // }
118150 // try to create the folder
119- storage_common_mkdir (fs_api , FLIPBIP_APP_BASE_FOLDER );
151+ storage_simply_mkdir (fs_api , FLIPBIP_APP_BASE_FOLDER );
120152
121153 File * settings_file = storage_file_alloc (fs_api );
122154 if (storage_file_open (settings_file , path , FSAM_WRITE , open_mode )) {
@@ -127,28 +159,41 @@ bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
127159 storage_file_close (settings_file );
128160 storage_file_free (settings_file );
129161
130- File * settings_file_bak = storage_file_alloc (fs_api );
131- if (storage_file_open (settings_file_bak , path_bak , FSAM_WRITE , open_mode )) {
132- storage_file_write (settings_file_bak , settings , strlen (settings ));
133- storage_file_write (settings_file_bak , "\n" , 1 );
162+ if (path_bak != NULL ) {
163+ File * settings_file_bak = storage_file_alloc (fs_api );
164+ if (storage_file_open (settings_file_bak , path_bak , FSAM_WRITE , open_mode )) {
165+ storage_file_write (settings_file_bak , settings , strlen (settings ));
166+ storage_file_write (settings_file_bak , "\n" , 1 );
167+ }
168+ storage_file_close (settings_file_bak );
169+ storage_file_free (settings_file_bak );
134170 }
135- storage_file_close (settings_file_bak );
136- storage_file_free (settings_file_bak );
137171
138172 furi_record_close (RECORD_STORAGE );
139173
140174 return ret ;
141175}
142176
143- bool flipbip_load_settings_secure (char * settings ) {
177+ bool flipbip_save_qrfile (
178+ const char * qr_msg_prefix ,
179+ const char * qr_msg_content ,
180+ const char * file_name ) {
181+ char qr_buf [90 ] = {0 };
182+ strcpy (qr_buf , TEXT_QRFILE );
183+ strcpy (qr_buf + strlen (qr_buf ), qr_msg_prefix );
184+ strcpy (qr_buf + strlen (qr_buf ), qr_msg_content );
185+ return flipbip_save_file (qr_buf , FlipBipFileOther , file_name , false);
186+ }
187+
188+ bool flipbip_load_file_secure (char * settings ) {
144189 const size_t dlen = FILE_HLEN + FILE_SLEN + 1 ;
145190
146191 // allocate memory for key/data
147192 char * data = malloc (dlen );
148193 memzero (data , dlen );
149194
150195 // load k2 from file
151- if (!flipbip_load_settings (data , true )) return false;
196+ if (!flipbip_load_file (data , FlipBipFileKey , NULL )) return false;
152197
153198 // check header
154199 if (data [0 ] != FILE_HSTR [0 ] || data [1 ] != FILE_HSTR [1 ] || data [2 ] != FILE_HSTR [2 ] ||
@@ -174,7 +219,7 @@ bool flipbip_load_settings_secure(char* settings) {
174219 data -= FILE_HLEN ;
175220
176221 // load data from file
177- if (!flipbip_load_settings (data , false )) return false;
222+ if (!flipbip_load_file (data , FlipBipFileDat , NULL )) return false;
178223
179224 // check header
180225 if (data [0 ] != FILE_HSTR [0 ] || data [1 ] != FILE_HSTR [1 ] || data [2 ] != FILE_HSTR [2 ] ||
@@ -207,7 +252,7 @@ bool flipbip_load_settings_secure(char* settings) {
207252 return true;
208253}
209254
210- bool flipbip_save_settings_secure (const char * settings ) {
255+ bool flipbip_save_file_secure (const char * settings ) {
211256 const size_t dlen = FILE_HLEN + FILE_SLEN + 1 ;
212257
213258 // cap settings to 256 bytes
@@ -238,7 +283,7 @@ bool flipbip_save_settings_secure(const char* settings) {
238283 // seek <-- header
239284 data -= FILE_HLEN ;
240285 // save k2 to file
241- flipbip_save_settings (data , true , false);
286+ flipbip_save_file (data , FlipBipFileKey , NULL , false);
242287 // seek --> header
243288 data += FILE_HLEN ;
244289 // zero k2 memory
@@ -251,7 +296,7 @@ bool flipbip_save_settings_secure(const char* settings) {
251296 // seek <-- header
252297 data -= FILE_HLEN ;
253298 // save data to file
254- flipbip_save_settings (data , false , false);
299+ flipbip_save_file (data , FlipBipFileDat , NULL , false);
255300
256301 // clear memory
257302 memzero (data , dlen );
@@ -260,4 +305,4 @@ bool flipbip_save_settings_secure(const char* settings) {
260305 memzero (k2 , FILE_KLEN / 2 );
261306
262307 return true;
263- }
308+ }
0 commit comments