token_info.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. if(plain_secret_length > 0) {
  58. if(token_info->token != NULL) {
  59. free(token_info->token);
  60. }
  61. token_info->token = totp_crypto_encrypt(
  62. plain_secret, plain_secret_length, crypto_settings, &token_info->token_length);
  63. result = true;
  64. } else {
  65. result = false;
  66. }
  67. memset_s(plain_secret, plain_secret_size, 0, plain_secret_size);
  68. free(plain_secret);
  69. return result;
  70. }
  71. bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits) {
  72. switch(digits) {
  73. case 5:
  74. token_info->digits = TokenDigitsCountFive;
  75. return true;
  76. case 6:
  77. token_info->digits = TokenDigitsCountSix;
  78. return true;
  79. case 8:
  80. token_info->digits = TokenDigitsCountEight;
  81. return true;
  82. default:
  83. break;
  84. }
  85. return false;
  86. }
  87. bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration) {
  88. #pragma GCC diagnostic push
  89. #pragma GCC diagnostic ignored "-Wtype-limits"
  90. if(duration >= TokenDurationMin && duration <= TokenDurationMax) { //-V560
  91. token_info->duration = duration;
  92. return true;
  93. }
  94. #pragma GCC diagnostic pop
  95. return false;
  96. }
  97. bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str) {
  98. if(furi_string_cmpi_str(str, TOKEN_HASH_ALGO_SHA1_NAME) == 0) {
  99. token_info->algo = TokenHashAlgoSha1;
  100. return true;
  101. }
  102. if(furi_string_cmpi_str(str, TOKEN_HASH_ALGO_SHA256_NAME) == 0) {
  103. token_info->algo = TokenHashAlgoSha256;
  104. return true;
  105. }
  106. if(furi_string_cmpi_str(str, TOKEN_HASH_ALGO_SHA512_NAME) == 0) {
  107. token_info->algo = TokenHashAlgoSha512;
  108. return true;
  109. }
  110. if(furi_string_cmpi_str(str, TOKEN_HASH_ALGO_STEAM_NAME) == 0) {
  111. token_info->algo = TokenHashAlgoSteam;
  112. return true;
  113. }
  114. return false;
  115. }
  116. bool token_info_set_algo_from_int(TokenInfo* token_info, uint8_t algo_code) {
  117. switch(algo_code) {
  118. case TokenHashAlgoSha1:
  119. token_info->algo = TokenHashAlgoSha1;
  120. break;
  121. case TokenHashAlgoSha256:
  122. token_info->algo = TokenHashAlgoSha256;
  123. break;
  124. case TokenHashAlgoSha512:
  125. token_info->algo = TokenHashAlgoSha512;
  126. break;
  127. case TokenHashAlgoSteam:
  128. token_info->algo = TokenHashAlgoSteam;
  129. break;
  130. default:
  131. return false;
  132. }
  133. return true;
  134. }
  135. const char* token_info_get_algo_as_cstr(const TokenInfo* token_info) {
  136. switch(token_info->algo) {
  137. case TokenHashAlgoSha1:
  138. return TOKEN_HASH_ALGO_SHA1_NAME;
  139. case TokenHashAlgoSha256:
  140. return TOKEN_HASH_ALGO_SHA256_NAME;
  141. case TokenHashAlgoSha512:
  142. return TOKEN_HASH_ALGO_SHA512_NAME;
  143. case TokenHashAlgoSteam:
  144. return TOKEN_HASH_ALGO_STEAM_NAME;
  145. default:
  146. break;
  147. }
  148. return NULL;
  149. }
  150. bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const FuriString* str) {
  151. if(furi_string_cmpi_str(str, TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME) == 0) {
  152. token_info->automation_features |= TokenAutomationFeatureEnterAtTheEnd;
  153. return true;
  154. }
  155. if(furi_string_cmpi_str(str, TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME) == 0) {
  156. token_info->automation_features |= TokenAutomationFeatureTabAtTheEnd;
  157. return true;
  158. }
  159. if(furi_string_cmpi_str(str, TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME) == 0) {
  160. token_info->automation_features |= TokenAutomationFeatureTypeSlower;
  161. return true;
  162. }
  163. if(furi_string_cmpi_str(str, TOKEN_AUTOMATION_FEATURE_NONE_NAME) == 0) {
  164. token_info->automation_features = TokenAutomationFeatureNone;
  165. return true;
  166. }
  167. return false;
  168. }
  169. bool token_info_set_token_type_from_str(TokenInfo* token_info, const FuriString* str) {
  170. if(furi_string_cmpi_str(str, TOKEN_TYPE_TOTP_NAME) == 0) {
  171. token_info->type = TokenTypeTOTP;
  172. return true;
  173. }
  174. if(furi_string_cmpi_str(str, TOKEN_TYPE_HOTP_NAME) == 0) {
  175. token_info->type = TokenTypeHOTP;
  176. return true;
  177. }
  178. return false;
  179. }
  180. const char* token_info_get_type_as_cstr(const TokenInfo* token_info) {
  181. switch(token_info->type) {
  182. case TokenTypeTOTP:
  183. return TOKEN_TYPE_TOTP_NAME;
  184. case TokenTypeHOTP:
  185. return TOKEN_TYPE_HOTP_NAME;
  186. default:
  187. break;
  188. }
  189. return NULL;
  190. }
  191. bool token_info_set_token_counter_from_str(TokenInfo* token_info, const FuriString* str) {
  192. return sscanf(furi_string_get_cstr(str), "%" PRIu64, &token_info->counter) == 1;
  193. }
  194. TokenInfo* token_info_clone(const TokenInfo* src) {
  195. TokenInfo* clone = token_info_alloc();
  196. memcpy(clone, src, sizeof(TokenInfo));
  197. clone->token = malloc(src->token_length);
  198. furi_check(clone->token != NULL);
  199. memcpy(clone->token, src->token, src->token_length);
  200. clone->name = furi_string_alloc();
  201. furi_string_set(clone->name, src->name);
  202. return clone;
  203. }
  204. void token_info_set_defaults(TokenInfo* token_info) {
  205. furi_check(token_info != NULL);
  206. token_info->algo = TokenHashAlgoDefault;
  207. token_info->digits = TokenDigitsCountDefault;
  208. token_info->duration = TokenDurationDefault;
  209. token_info->automation_features = TokenAutomationFeatureNone;
  210. token_info->type = TokenTypeTOTP;
  211. token_info->counter = 0;
  212. furi_string_reset(token_info->name);
  213. }