token_info_iterator.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #include "token_info_iterator.h"
  2. #include <flipper_format/flipper_format_i.h>
  3. #include <flipper_format/flipper_format_stream.h>
  4. #include <toolbox/stream/file_stream.h>
  5. #include "../../types/common.h"
  6. #define CONFIG_FILE_PART_FILE_PATH CONFIG_FILE_DIRECTORY_PATH "/totp.conf.part"
  7. #define STREAM_COPY_BUFFER_SIZE 128
  8. struct TokenInfoIteratorContext {
  9. size_t total_count;
  10. size_t current_index;
  11. size_t last_seek_offset;
  12. size_t last_seek_index;
  13. TokenInfo* current_token;
  14. FlipperFormat* config_file;
  15. uint8_t* iv;
  16. Storage* storage;
  17. };
  18. static bool
  19. flipper_format_seek_to_siblinig_token_start(Stream* stream, StreamDirection direction) {
  20. char buffer[sizeof(TOTP_CONFIG_KEY_TOKEN_NAME) + 1];
  21. bool found = false;
  22. while(!found) {
  23. if(!stream_seek_to_char(stream, '\n', direction)) {
  24. break;
  25. }
  26. size_t buffer_read_size;
  27. if((buffer_read_size = stream_read(stream, (uint8_t*)&buffer[0], sizeof(buffer))) == 0) {
  28. break;
  29. }
  30. if(!stream_seek(stream, -(int32_t)buffer_read_size, StreamOffsetFromCurrent)) {
  31. break;
  32. }
  33. if(strncmp(buffer, "\n" TOTP_CONFIG_KEY_TOKEN_NAME ":", sizeof(buffer)) == 0) {
  34. found = true;
  35. }
  36. }
  37. return found;
  38. }
  39. static bool seek_to_token(size_t token_index, TokenInfoIteratorContext* context) {
  40. furi_check(context != NULL && context->config_file != NULL);
  41. if(token_index >= context->total_count) {
  42. return false;
  43. }
  44. Stream* stream = flipper_format_get_raw_stream(context->config_file);
  45. long token_index_diff = (long)token_index - (long)context->last_seek_index;
  46. size_t token_index_diff_weight = (size_t)labs(token_index_diff);
  47. StreamDirection direction = token_index_diff >= 0 ? StreamDirectionForward :
  48. StreamDirectionBackward;
  49. if(token_index_diff_weight > token_index || context->last_seek_offset == 0) {
  50. context->last_seek_offset = 0;
  51. context->last_seek_index = 0;
  52. token_index_diff = token_index + 1;
  53. direction = StreamDirectionForward;
  54. } else if(token_index_diff_weight > (context->total_count - token_index - 1)) {
  55. context->last_seek_offset = stream_size(stream);
  56. context->last_seek_index = context->total_count - 1;
  57. token_index_diff = -(long)(context->total_count - token_index);
  58. direction = StreamDirectionBackward;
  59. }
  60. stream_seek(stream, context->last_seek_offset, StreamOffsetFromStart);
  61. if(token_index_diff != 0) {
  62. long i = 0;
  63. long i_inc = token_index_diff >= 0 ? 1 : -1;
  64. do {
  65. if(!flipper_format_seek_to_siblinig_token_start(stream, direction)) {
  66. break;
  67. }
  68. i += i_inc;
  69. } while((i_inc > 0 && i < token_index_diff) || (i_inc < 0 && i > token_index_diff));
  70. if((i_inc > 0 && i < token_index_diff) || (i_inc < 0 && i > token_index_diff)) {
  71. context->last_seek_offset = 0;
  72. FURI_LOG_D(LOGGING_TAG, "Was not able to move");
  73. return false;
  74. }
  75. context->last_seek_offset = stream_tell(stream);
  76. context->last_seek_index = token_index;
  77. } else {
  78. if (!stream_seek(stream, context->last_seek_offset, StreamOffsetFromStart)) {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. static bool stream_insert_stream(Stream* dst, Stream* src) {
  85. uint8_t buffer[STREAM_COPY_BUFFER_SIZE];
  86. size_t buffer_read_size;
  87. while((buffer_read_size = stream_read(src, buffer, sizeof(buffer))) != 0) {
  88. if(!stream_insert(dst, buffer, buffer_read_size)) {
  89. return false;
  90. }
  91. }
  92. return true;
  93. }
  94. static bool totp_token_info_iterator_save_current_token_info_changes(TokenInfoIteratorContext* context) {
  95. bool is_new_token = context->current_index >= context->total_count;
  96. Stream* stream = flipper_format_get_raw_stream(context->config_file);
  97. if(is_new_token) {
  98. if(!flipper_format_seek_to_end(context->config_file)) {
  99. return false;
  100. }
  101. } else {
  102. if(!seek_to_token(context->current_index, context)) {
  103. return false;
  104. }
  105. }
  106. size_t offset_start = stream_tell(stream);
  107. size_t offset_end;
  108. if(is_new_token) {
  109. offset_end = offset_start;
  110. } else if(context->current_index + 1 >= context->total_count) {
  111. offset_end = stream_size(stream);
  112. } else if(seek_to_token(context->current_index + 1, context)) {
  113. offset_end = stream_tell(stream);
  114. } else {
  115. return false;
  116. }
  117. FlipperFormat* temp_ff = flipper_format_file_alloc(context->storage);
  118. if (!flipper_format_file_open_always(temp_ff, CONFIG_FILE_PART_FILE_PATH)) {
  119. flipper_format_free(temp_ff);
  120. return false;
  121. }
  122. TokenInfo* token_info = context->current_token;
  123. bool result = false;
  124. do {
  125. if(!flipper_format_write_string(temp_ff, TOTP_CONFIG_KEY_TOKEN_NAME, token_info->name)) {
  126. break;
  127. }
  128. if(!flipper_format_write_hex(
  129. temp_ff,
  130. TOTP_CONFIG_KEY_TOKEN_SECRET,
  131. token_info->token,
  132. token_info->token_length)) {
  133. break;
  134. }
  135. uint32_t tmp_uint32 = token_info->algo;
  136. if(!flipper_format_write_uint32(temp_ff, TOTP_CONFIG_KEY_TOKEN_ALGO, &tmp_uint32, 1)) {
  137. break;
  138. }
  139. tmp_uint32 = token_info->digits;
  140. if(!flipper_format_write_uint32(temp_ff, TOTP_CONFIG_KEY_TOKEN_DIGITS, &tmp_uint32, 1)) {
  141. break;
  142. }
  143. tmp_uint32 = token_info->duration;
  144. if(!flipper_format_write_uint32(temp_ff, TOTP_CONFIG_KEY_TOKEN_DURATION, &tmp_uint32, 1)) {
  145. break;
  146. }
  147. tmp_uint32 = token_info->automation_features;
  148. if(!flipper_format_write_uint32(
  149. temp_ff, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, &tmp_uint32, 1)) {
  150. break;
  151. }
  152. Stream* temp_stream = flipper_format_get_raw_stream(temp_ff);
  153. if(!stream_rewind(temp_stream)) {
  154. break;
  155. }
  156. if(!stream_seek(stream, offset_start, StreamOffsetFromStart)) {
  157. break;
  158. }
  159. if(offset_end != offset_start && !stream_delete(stream, offset_end - offset_start)) {
  160. break;
  161. }
  162. if(!is_new_token && !stream_write_char(stream, '\n')) {
  163. break;
  164. }
  165. if(!stream_insert_stream(stream, temp_stream)) {
  166. break;
  167. }
  168. if(is_new_token) {
  169. context->total_count++;
  170. }
  171. result = true;
  172. } while(false);
  173. flipper_format_free(temp_ff);
  174. storage_common_remove(context->storage, CONFIG_FILE_PART_FILE_PATH);
  175. stream_seek(stream, offset_start, StreamOffsetFromStart);
  176. context->last_seek_offset = offset_start;
  177. context->last_seek_index = context->current_index;
  178. return result;
  179. }
  180. TokenInfoIteratorContext* totp_token_info_iterator_alloc(Storage* storage, FlipperFormat* config_file, uint8_t* iv) {
  181. Stream* stream = flipper_format_get_raw_stream(config_file);
  182. stream_rewind(stream);
  183. size_t tokens_count = 0;
  184. while(true) {
  185. if(!flipper_format_seek_to_siblinig_token_start(stream, StreamDirectionForward)) {
  186. break;
  187. }
  188. tokens_count++;
  189. }
  190. TokenInfoIteratorContext* context = malloc(sizeof(TokenInfoIteratorContext));
  191. furi_check(context != NULL);
  192. context->total_count = tokens_count;
  193. context->current_token = token_info_alloc();
  194. context->config_file = config_file;
  195. context->iv = iv;
  196. context->storage = storage;
  197. return context;
  198. }
  199. void totp_token_info_iterator_free(TokenInfoIteratorContext* context) {
  200. if(context == NULL) return;
  201. token_info_free(context->current_token);
  202. free(context);
  203. }
  204. bool totp_token_info_iterator_remove_current_token_info(TokenInfoIteratorContext* context) {
  205. if(!seek_to_token(context->current_index, context)) {
  206. return false;
  207. }
  208. Stream* stream = flipper_format_get_raw_stream(context->config_file);
  209. size_t begin_offset = stream_tell(stream);
  210. size_t end_offset;
  211. if(context->current_index >= context->total_count - 1) {
  212. end_offset = stream_size(stream);
  213. } else if(seek_to_token(context->current_index + 1, context)) {
  214. end_offset = stream_tell(stream);
  215. } else {
  216. return false;
  217. }
  218. if(!stream_seek(stream, begin_offset, StreamOffsetFromStart) ||
  219. !stream_delete(stream, end_offset - begin_offset)) {
  220. return false;
  221. }
  222. context->total_count--;
  223. if(context->current_index >= context->total_count) {
  224. context->current_index = context->total_count - 1;
  225. }
  226. return true;
  227. }
  228. bool totp_token_info_iterator_move_current_token_info(
  229. TokenInfoIteratorContext* context,
  230. size_t new_index) {
  231. if(context->current_index == new_index) return true;
  232. if(!seek_to_token(context->current_index, context)) {
  233. return false;
  234. }
  235. Stream* stream = flipper_format_get_raw_stream(context->config_file);
  236. size_t begin_offset = stream_tell(stream);
  237. size_t end_offset;
  238. if(context->current_index >= context->total_count - 1) {
  239. end_offset = stream_size(stream);
  240. } else if(seek_to_token(context->current_index + 1, context)) {
  241. end_offset = stream_tell(stream);
  242. } else {
  243. return false;
  244. }
  245. Stream* temp_stream = file_stream_alloc(context->storage);
  246. if (!file_stream_open(temp_stream, CONFIG_FILE_PART_FILE_PATH, FSAM_READ_WRITE, FSOM_CREATE_ALWAYS)) {
  247. stream_free(temp_stream);
  248. return false;
  249. }
  250. size_t moving_size = end_offset - begin_offset;
  251. bool result = false;
  252. do {
  253. if(!stream_seek(stream, begin_offset, StreamOffsetFromStart)) {
  254. break;
  255. }
  256. if(stream_copy(stream, temp_stream, moving_size) < moving_size) {
  257. break;
  258. }
  259. if(!stream_rewind(temp_stream)) {
  260. break;
  261. }
  262. if(!stream_seek(stream, begin_offset, StreamOffsetFromStart)) {
  263. break;
  264. }
  265. if(!stream_delete(stream, moving_size)) {
  266. break;
  267. }
  268. context->last_seek_offset = 0;
  269. context->last_seek_index = 0;
  270. if(new_index >= context->total_count - 1) {
  271. if(!stream_seek(stream, stream_size(stream), StreamOffsetFromStart)) {
  272. break;
  273. }
  274. } else if(!seek_to_token(new_index, context)) {
  275. break;
  276. }
  277. result = stream_insert_stream(stream, temp_stream);
  278. } while(false);
  279. stream_free(temp_stream);
  280. storage_common_remove(context->storage, CONFIG_FILE_PART_FILE_PATH);
  281. context->last_seek_offset = 0;
  282. context->last_seek_index = 0;
  283. return result;
  284. }
  285. TotpIteratorUpdateTokenResult totp_token_info_iterator_update_current_token(TokenInfoIteratorContext* context, TOTP_ITERATOR_UPDATE_TOKEN_ACTION update, const void* update_context) {
  286. TotpIteratorUpdateTokenResult result = update(context->current_token, update_context);
  287. if (result == TotpIteratorUpdateTokenResultSuccess) {
  288. if (!totp_token_info_iterator_save_current_token_info_changes(context)) {
  289. result = TotpIteratorUpdateTokenResultFileUpdateFailed;
  290. }
  291. return result;
  292. }
  293. totp_token_info_iterator_go_to(context, context->current_index);
  294. return result;
  295. }
  296. TotpIteratorUpdateTokenResult totp_token_info_iterator_add_new_token(TokenInfoIteratorContext* context, TOTP_ITERATOR_UPDATE_TOKEN_ACTION update, const void* update_context) {
  297. size_t previous_index = context->current_index;
  298. context->current_index = context->total_count;
  299. token_info_set_defaults(context->current_token);
  300. TotpIteratorUpdateTokenResult result = update(context->current_token, update_context);
  301. if (result == TotpIteratorUpdateTokenResultSuccess &&
  302. !totp_token_info_iterator_save_current_token_info_changes(context)) {
  303. result = TotpIteratorUpdateTokenResultFileUpdateFailed;
  304. }
  305. if (result != TotpIteratorUpdateTokenResultSuccess) {
  306. totp_token_info_iterator_go_to(context, previous_index);
  307. }
  308. return result;
  309. }
  310. bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t token_index) {
  311. furi_check(context != NULL);
  312. context->current_index = token_index;
  313. if(!seek_to_token(context->current_index, context)) {
  314. return false;
  315. }
  316. Stream* stream = flipper_format_get_raw_stream(context->config_file);
  317. size_t original_offset = stream_tell(stream);
  318. if(!flipper_format_read_string(
  319. context->config_file, TOTP_CONFIG_KEY_TOKEN_NAME, context->current_token->name)) {
  320. stream_seek(stream, original_offset, StreamOffsetFromStart);
  321. return false;
  322. }
  323. uint32_t secret_bytes_count;
  324. if(!flipper_format_get_value_count(
  325. context->config_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  326. secret_bytes_count = 0;
  327. }
  328. TokenInfo* tokenInfo = context->current_token;
  329. bool token_update_needed = false;
  330. if(tokenInfo->token != NULL) {
  331. free(tokenInfo->token);
  332. tokenInfo->token_length = 0;
  333. }
  334. if(secret_bytes_count == 1) { // Plain secret key
  335. FuriString* temp_str = furi_string_alloc();
  336. if(flipper_format_read_string(
  337. context->config_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str)) {
  338. if(token_info_set_secret(
  339. tokenInfo,
  340. furi_string_get_cstr(temp_str),
  341. furi_string_size(temp_str),
  342. PLAIN_TOKEN_ENCODING_BASE32,
  343. context->iv)) {
  344. FURI_LOG_W(
  345. LOGGING_TAG,
  346. "Token \"%s\" has plain secret",
  347. furi_string_get_cstr(tokenInfo->name));
  348. token_update_needed = true;
  349. } else {
  350. tokenInfo->token = NULL;
  351. tokenInfo->token_length = 0;
  352. FURI_LOG_W(
  353. LOGGING_TAG,
  354. "Token \"%s\" has invalid secret",
  355. furi_string_get_cstr(tokenInfo->name));
  356. }
  357. } else {
  358. tokenInfo->token = NULL;
  359. tokenInfo->token_length = 0;
  360. }
  361. furi_string_free(temp_str);
  362. } else { // encrypted
  363. tokenInfo->token_length = secret_bytes_count;
  364. if(secret_bytes_count > 0) {
  365. tokenInfo->token = malloc(tokenInfo->token_length);
  366. furi_check(tokenInfo->token != NULL);
  367. if(!flipper_format_read_hex(
  368. context->config_file,
  369. TOTP_CONFIG_KEY_TOKEN_SECRET,
  370. tokenInfo->token,
  371. tokenInfo->token_length)) {
  372. free(tokenInfo->token);
  373. tokenInfo->token = NULL;
  374. tokenInfo->token_length = 0;
  375. }
  376. } else {
  377. tokenInfo->token = NULL;
  378. }
  379. }
  380. uint32_t temp_data32;
  381. if(flipper_format_read_uint32(
  382. context->config_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &temp_data32, 1) &&
  383. temp_data32 <= STEAM) {
  384. tokenInfo->algo = (TokenHashAlgo)temp_data32;
  385. } else {
  386. tokenInfo->algo = SHA1;
  387. }
  388. if(!flipper_format_read_uint32(
  389. context->config_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &temp_data32, 1) ||
  390. !token_info_set_digits_from_int(tokenInfo, temp_data32)) {
  391. tokenInfo->digits = TOTP_6_DIGITS;
  392. }
  393. if(!flipper_format_read_uint32(
  394. context->config_file, TOTP_CONFIG_KEY_TOKEN_DURATION, &temp_data32, 1) ||
  395. !token_info_set_duration_from_int(tokenInfo, temp_data32)) {
  396. tokenInfo->duration = TOTP_TOKEN_DURATION_DEFAULT;
  397. }
  398. if(flipper_format_read_uint32(
  399. context->config_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, &temp_data32, 1)) {
  400. tokenInfo->automation_features = temp_data32;
  401. } else {
  402. tokenInfo->automation_features = TOKEN_AUTOMATION_FEATURE_NONE;
  403. }
  404. stream_seek(stream, original_offset, StreamOffsetFromStart);
  405. if(token_update_needed &&
  406. !totp_token_info_iterator_save_current_token_info_changes(context)) {
  407. return false;
  408. }
  409. return true;
  410. }
  411. const TokenInfo* totp_token_info_iterator_get_current_token(const TokenInfoIteratorContext* context) {
  412. return context->current_token;
  413. }
  414. size_t totp_token_info_iterator_get_current_token_index(const TokenInfoIteratorContext* context) {
  415. return context->current_index;
  416. }
  417. size_t totp_token_info_iterator_get_total_count(const TokenInfoIteratorContext* context) {
  418. return context->total_count;
  419. }
  420. void totp_token_info_iterator_attach_to_config_file(TokenInfoIteratorContext* context, FlipperFormat* config_file) {
  421. context->config_file = config_file;
  422. }