token_info_iterator.c 17 KB

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