token_info_iterator.c 17 KB

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