token_info.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "token_info.h"
  2. #include <base32.h>
  3. #include <base64.h>
  4. #include <memset_s.h>
  5. #include "common.h"
  6. #include "../services/crypto/crypto_facade.h"
  7. TokenInfo* token_info_alloc() {
  8. TokenInfo* tokenInfo = malloc(sizeof(TokenInfo));
  9. furi_check(tokenInfo != NULL);
  10. tokenInfo->name = furi_string_alloc();
  11. token_info_set_defaults(tokenInfo);
  12. return tokenInfo;
  13. }
  14. void token_info_free(TokenInfo* token_info) {
  15. if(token_info == NULL) return;
  16. free(token_info->token);
  17. furi_string_free(token_info->name);
  18. free(token_info);
  19. }
  20. bool token_info_set_secret(
  21. TokenInfo* token_info,
  22. const char* plain_token_secret,
  23. size_t token_secret_length,
  24. PlainTokenSecretEncoding plain_token_secret_encoding,
  25. const uint8_t* iv,
  26. uint8_t crypto_version,
  27. uint8_t crypto_key_slot) {
  28. if(token_secret_length == 0) return false;
  29. uint8_t* plain_secret;
  30. size_t plain_secret_length;
  31. size_t plain_secret_size;
  32. if(plain_token_secret_encoding == PlainTokenSecretEncodingBase32) {
  33. plain_secret_size = token_secret_length;
  34. plain_secret = malloc(plain_secret_size);
  35. furi_check(plain_secret != NULL);
  36. plain_secret_length =
  37. base32_decode((const uint8_t*)plain_token_secret, plain_secret, plain_secret_size);
  38. } else if(plain_token_secret_encoding == PlainTokenSecretEncodingBase64) {
  39. plain_secret_length = 0;
  40. plain_secret = base64_decode(
  41. (const uint8_t*)plain_token_secret,
  42. token_secret_length,
  43. &plain_secret_length,
  44. &plain_secret_size);
  45. furi_check(plain_secret != NULL);
  46. } else {
  47. return false;
  48. }
  49. bool result;
  50. if(plain_secret_length > 0) {
  51. if(token_info->token != NULL) {
  52. free(token_info->token);
  53. }
  54. token_info->token = totp_crypto_encrypt(
  55. plain_secret,
  56. plain_secret_length,
  57. iv,
  58. crypto_version,
  59. crypto_key_slot,
  60. &token_info->token_length);
  61. result = true;
  62. } else {
  63. result = false;
  64. }
  65. memset_s(plain_secret, plain_secret_size, 0, plain_secret_size);
  66. free(plain_secret);
  67. return result;
  68. }
  69. bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits) {
  70. switch(digits) {
  71. case 5:
  72. token_info->digits = TokenDigitsCountFive;
  73. return true;
  74. case 6:
  75. token_info->digits = TokenDigitsCountSix;
  76. return true;
  77. case 8:
  78. token_info->digits = TokenDigitsCountEight;
  79. return true;
  80. default:
  81. break;
  82. }
  83. return false;
  84. }
  85. bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration) {
  86. #pragma GCC diagnostic push
  87. #pragma GCC diagnostic ignored "-Wtype-limits"
  88. if(duration >= TokenDurationMin && duration <= TokenDurationMax) { //-V560
  89. token_info->duration = duration;
  90. return true;
  91. }
  92. #pragma GCC diagnostic pop
  93. return false;
  94. }
  95. bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str) {
  96. if(furi_string_cmpi_str(str, TOKEN_HASH_ALGO_SHA1_NAME) == 0) {
  97. token_info->algo = TokenHashAlgoSha1;
  98. return true;
  99. }
  100. if(furi_string_cmpi_str(str, TOKEN_HASH_ALGO_SHA256_NAME) == 0) {
  101. token_info->algo = TokenHashAlgoSha256;
  102. return true;
  103. }
  104. if(furi_string_cmpi_str(str, TOKEN_HASH_ALGO_SHA512_NAME) == 0) {
  105. token_info->algo = TokenHashAlgoSha512;
  106. return true;
  107. }
  108. if(furi_string_cmpi_str(str, TOKEN_HASH_ALGO_STEAM_NAME) == 0) {
  109. token_info->algo = TokenHashAlgoSteam;
  110. return true;
  111. }
  112. return false;
  113. }
  114. bool token_info_set_algo_from_int(TokenInfo* token_info, uint8_t algo_code) {
  115. switch(algo_code) {
  116. case TokenHashAlgoSha1:
  117. token_info->algo = TokenHashAlgoSha1;
  118. break;
  119. case TokenHashAlgoSha256:
  120. token_info->algo = TokenHashAlgoSha256;
  121. break;
  122. case TokenHashAlgoSha512:
  123. token_info->algo = TokenHashAlgoSha512;
  124. break;
  125. case TokenHashAlgoSteam:
  126. token_info->algo = TokenHashAlgoSteam;
  127. break;
  128. default:
  129. return false;
  130. }
  131. return true;
  132. }
  133. const char* token_info_get_algo_as_cstr(const TokenInfo* token_info) {
  134. switch(token_info->algo) {
  135. case TokenHashAlgoSha1:
  136. return TOKEN_HASH_ALGO_SHA1_NAME;
  137. case TokenHashAlgoSha256:
  138. return TOKEN_HASH_ALGO_SHA256_NAME;
  139. case TokenHashAlgoSha512:
  140. return TOKEN_HASH_ALGO_SHA512_NAME;
  141. case TokenHashAlgoSteam:
  142. return TOKEN_HASH_ALGO_STEAM_NAME;
  143. default:
  144. break;
  145. }
  146. return NULL;
  147. }
  148. bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const FuriString* str) {
  149. if(furi_string_cmpi_str(str, TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME) == 0) {
  150. token_info->automation_features |= TokenAutomationFeatureEnterAtTheEnd;
  151. return true;
  152. }
  153. if(furi_string_cmpi_str(str, TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME) == 0) {
  154. token_info->automation_features |= TokenAutomationFeatureTabAtTheEnd;
  155. return true;
  156. }
  157. if(furi_string_cmpi_str(str, TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME) == 0) {
  158. token_info->automation_features |= TokenAutomationFeatureTypeSlower;
  159. return true;
  160. }
  161. if(furi_string_cmpi_str(str, TOKEN_AUTOMATION_FEATURE_NONE_NAME) == 0) {
  162. token_info->automation_features = TokenAutomationFeatureNone;
  163. return true;
  164. }
  165. return false;
  166. }
  167. TokenInfo* token_info_clone(const TokenInfo* src) {
  168. TokenInfo* clone = token_info_alloc();
  169. memcpy(clone, src, sizeof(TokenInfo));
  170. clone->token = malloc(src->token_length);
  171. furi_check(clone->token != NULL);
  172. memcpy(clone->token, src->token, src->token_length);
  173. clone->name = furi_string_alloc();
  174. furi_string_set(clone->name, src->name);
  175. return clone;
  176. }
  177. void token_info_set_defaults(TokenInfo* token_info) {
  178. furi_check(token_info != NULL);
  179. token_info->algo = TokenHashAlgoDefault;
  180. token_info->digits = TokenDigitsCountDefault;
  181. token_info->duration = TokenDurationDefault;
  182. token_info->automation_features = TokenAutomationFeatureNone;
  183. furi_string_reset(token_info->name);
  184. }