token_info_iterator.c 20 KB

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