@@ -18,7 +18,30 @@ void totp_cli_command_pin_docopt_commands() {
18
18
}
19
19
20
20
void totp_cli_command_pin_docopt_usage () {
21
- TOTP_CLI_PRINTF (" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_PIN " " DOCOPT_REQUIRED (TOTP_CLI_COMMAND_PIN_COMMAND_SET " | " TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE ) "\r\n" );
21
+ TOTP_CLI_PRINTF (" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_PIN " " DOCOPT_REQUIRED (
22
+ TOTP_CLI_COMMAND_PIN_COMMAND_SET " | " TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE ) "\r\n" );
23
+ }
24
+
25
+ static inline uint8_t totp_cli_key_to_pin_code (uint8_t key ) {
26
+ uint8_t code = 0 ;
27
+ switch (key ) {
28
+ case 0x44 : // left
29
+ code = PinCodeArrowLeft ;
30
+ break ;
31
+ case 0x41 : // up
32
+ code = PinCodeArrowUp ;
33
+ break ;
34
+ case 0x43 : // right
35
+ code = PinCodeArrowRight ;
36
+ break ;
37
+ case 0x42 : // down
38
+ code = PinCodeArrowDown ;
39
+ break ;
40
+ default :
41
+ break ;
42
+ }
43
+
44
+ return code ;
22
45
}
23
46
24
47
static bool totp_cli_read_pin (Cli * cli , uint8_t * pin , uint8_t * pin_length ) {
@@ -30,28 +53,10 @@ static bool totp_cli_read_pin(Cli* cli, uint8_t* pin, uint8_t* pin_length) {
30
53
if (c == CliSymbolAsciiEsc ) {
31
54
uint8_t c2 ;
32
55
uint8_t c3 ;
33
- if (cli_read_timeout (cli , & c2 , 1 , 0 ) == 1 &&
34
- cli_read_timeout (cli , & c3 , 1 , 0 ) == 1 &&
35
- c2 == 0x5b ) {
36
- uint8_t code = 0 ;
37
- switch (c3 ) {
38
- case 0x44 : // left
39
- code = PinCodeArrowLeft ;
40
- break ;
41
- case 0x41 : // up
42
- code = PinCodeArrowUp ;
43
- break ;
44
- case 0x43 : // right
45
- code = PinCodeArrowRight ;
46
- break ;
47
- case 0x42 : // down
48
- code = PinCodeArrowDown ;
49
- break ;
50
- default :
51
- break ;
52
- }
53
-
54
- if (code > 0 ) {
56
+ if (cli_read_timeout (cli , & c2 , 1 , 0 ) == 1 && cli_read_timeout (cli , & c3 , 1 , 0 ) == 1 &&
57
+ c2 == 0x5b ) {
58
+ uint8_t code = totp_cli_key_to_pin_code (c3 );
59
+ if (code > 0 ) {
55
60
pin [* pin_length ] = code ;
56
61
* pin_length = * pin_length + 1 ;
57
62
putc ('*' , stdout );
@@ -63,7 +68,7 @@ static bool totp_cli_read_pin(Cli* cli, uint8_t* pin, uint8_t* pin_length) {
63
68
TOTP_CLI_PRINTF ("Cancelled by user\r\n" );
64
69
return false;
65
70
} else if (c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel ) {
66
- if (* pin_length > 0 ) {
71
+ if (* pin_length > 0 ) {
67
72
* pin_length = * pin_length - 1 ;
68
73
pin [* pin_length ] = 0 ;
69
74
TOTP_CLI_DELETE_LAST_CHAR ();
@@ -85,36 +90,32 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
85
90
bool do_change = false;
86
91
bool do_remove = false;
87
92
UNUSED (do_remove );
88
- do {
89
- if (!args_read_string_and_trim (args , temp_str )) {
90
- TOTP_CLI_PRINT_INVALID_ARGUMENTS ();
91
- break ;
92
- }
93
-
94
- if (furi_string_cmpi_str (temp_str , TOTP_CLI_COMMAND_PIN_COMMAND_SET ) == 0 ) {
93
+ if (args_read_string_and_trim (args , temp_str )) {
94
+ if (furi_string_cmpi_str (temp_str , TOTP_CLI_COMMAND_PIN_COMMAND_SET ) == 0 ) {
95
95
do_change = true;
96
- } else if (furi_string_cmpi_str (temp_str , TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE ) == 0 ) {
96
+ } else if (furi_string_cmpi_str (temp_str , TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE ) == 0 ) {
97
97
do_remove = true;
98
98
} else {
99
99
TOTP_CLI_PRINT_INVALID_ARGUMENTS ();
100
- break ;
101
100
}
102
- } while (false);
101
+ } else {
102
+ TOTP_CLI_PRINT_INVALID_ARGUMENTS ();
103
+ }
103
104
104
- if ((do_change || do_remove ) && totp_cli_ensure_authenticated (plugin_state , cli )) {
105
+ if ((do_change || do_remove ) && totp_cli_ensure_authenticated (plugin_state , cli )) {
105
106
bool load_generate_token_scene = false;
106
107
do {
107
108
uint8_t old_iv [TOTP_IV_SIZE ];
108
109
memcpy (& old_iv [0 ], & plugin_state -> iv [0 ], TOTP_IV_SIZE );
109
110
uint8_t new_pin [TOTP_IV_SIZE ];
110
111
uint8_t new_pin_length = 0 ;
111
- if (do_change ) {
112
- if (!totp_cli_read_pin (cli , & new_pin [0 ], & new_pin_length ) ||
113
- !totp_cli_ensure_authenticated (plugin_state , cli )) {
112
+ if (do_change ) {
113
+ if (!totp_cli_read_pin (cli , & new_pin [0 ], & new_pin_length ) ||
114
+ !totp_cli_ensure_authenticated (plugin_state , cli )) {
114
115
memset_s (& new_pin [0 ], TOTP_IV_SIZE , 0 , TOTP_IV_SIZE );
115
116
break ;
116
117
}
117
- } else if (do_remove ) {
118
+ } else if (do_remove ) {
118
119
new_pin_length = 0 ;
119
120
memset (& new_pin [0 ], 0 , TOTP_IV_SIZE );
120
121
}
@@ -128,34 +129,39 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
128
129
129
130
memset (& plugin_state -> iv [0 ], 0 , TOTP_IV_SIZE );
130
131
memset (& plugin_state -> base_iv [0 ], 0 , TOTP_IV_SIZE );
131
- if (plugin_state -> crypto_verify_data != NULL ) {
132
+ if (plugin_state -> crypto_verify_data != NULL ) {
132
133
free (plugin_state -> crypto_verify_data );
133
134
plugin_state -> crypto_verify_data = NULL ;
134
135
}
135
136
136
- totp_crypto_seed_iv (plugin_state , new_pin_length > 0 ? & new_pin [0 ] : NULL , new_pin_length );
137
- ListNode * node = plugin_state -> tokens_list ;
138
- while (node != NULL ) {
137
+ totp_crypto_seed_iv (
138
+ plugin_state , new_pin_length > 0 ? & new_pin [0 ] : NULL , new_pin_length );
139
+
140
+ TOTP_LIST_FOREACH (plugin_state -> tokens_list , node , {
139
141
TokenInfo * token_info = node -> data ;
140
- size_t plain_token_length ;
141
- uint8_t * plain_token = totp_crypto_decrypt (token_info -> token , token_info -> token_length , & old_iv [0 ], & plain_token_length );
142
+ size_t plain_token_length ;
143
+ uint8_t * plain_token = totp_crypto_decrypt (
144
+ token_info -> token , token_info -> token_length , & old_iv [0 ], & plain_token_length );
142
145
free (token_info -> token );
143
- token_info -> token = totp_crypto_encrypt (plain_token , plain_token_length , & plugin_state -> iv [0 ], & token_info -> token_length );
146
+ token_info -> token = totp_crypto_encrypt (
147
+ plain_token ,
148
+ plain_token_length ,
149
+ & plugin_state -> iv [0 ],
150
+ & token_info -> token_length );
144
151
memset_s (plain_token , plain_token_length , 0 , plain_token_length );
145
152
free (plain_token );
146
- node = node -> next ;
147
- }
153
+ });
148
154
149
155
totp_full_save_config_file (plugin_state );
150
156
151
157
TOTP_CLI_DELETE_LAST_LINE ();
152
158
153
- if (do_change ) {
159
+ if (do_change ) {
154
160
TOTP_CLI_PRINTF ("PIN has been successfully changed\r\n" );
155
- } else if (do_remove ) {
161
+ } else if (do_remove ) {
156
162
TOTP_CLI_PRINTF ("PIN has been successfully removed\r\n" );
157
163
}
158
- } while (false);
164
+ } while (false);
159
165
160
166
if (load_generate_token_scene ) {
161
167
totp_scene_director_activate_scene (plugin_state , TotpSceneGenerateToken , NULL );
0 commit comments