token_info.c 7.4 KB

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