Skip to content

Commit 2e5a038

Browse files
joerchankartben
authored andcommitted
net: tls_credentials_shell: Add credential buffer load argument
Add argument to the TLS credential `cred buf` command that enables a shell bypass to write the TLS credential directly to the credential buffer. This is useful for writing load credentials that cannot fit in a single `cred buf` command and would otherwise have to be split into multiple cred buf commands. Sending multiple in succession like that from a script for example very easily causes the shell RX buffer to get full, resulting in multiple `RX ring buffer full.` warnings. This is very difficult for a script to handle. Using a bypass has much better performance and can easily avoid the RX ring buffer full condition without increasing the RX ring buffer to much. It is also easier for a script to use. Signed-off-by: Joakim Andersson <joerchan@gmail.com>
1 parent d4758f0 commit 2e5a038

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

subsys/net/lib/tls_credentials/tls_credentials_shell.c

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,9 @@ static bool cred_buf_clear(void)
152152
}
153153

154154
/* Parse a (possibly incomplete) chunk into the credential buffer */
155-
static int cred_buf_write(char *chunk)
155+
static int cred_buf_write(char *chunk, size_t chunk_len)
156156
{
157157
char *writehead = cred_buf + cred_written;
158-
size_t chunk_len = strlen(chunk);
159158

160159
/* Verify that there is room for the incoming chunk */
161160
if ((writehead + chunk_len) >= (cred_buf + sizeof(cred_buf) - 1)) {
@@ -327,7 +326,8 @@ static void shell_clear_cred_buf(const struct shell *sh)
327326
/* Write data into the credential buffer, with shell feedback. */
328327
static int shell_write_cred_buf(const struct shell *sh, char *chunk)
329328
{
330-
int res = cred_buf_write(chunk);
329+
size_t chunk_len = strlen(chunk);
330+
int res = cred_buf_write(chunk, chunk_len);
331331

332332
/* Report results. */
333333

@@ -515,15 +515,59 @@ static int tls_cred_cmd_add(const struct shell *sh, size_t argc, char *argv[])
515515
return err;
516516
}
517517

518-
/* Buffers credential data into the credential buffer. */
519-
static int tls_cred_cmd_buf(const struct shell *sh, size_t argc, char *argv[])
518+
#define ASCII_CTRL_C 0x03
519+
520+
static void tls_cred_cmd_load_bypass(const struct shell *sh, uint8_t *data, size_t len)
520521
{
521-
/* If the "clear" keyword is provided, clear the buffer rather than write to it. */
522-
if (strcmp(argv[1], "clear") == 0) {
522+
bool terminate = false;
523+
int res;
524+
size_t write_len = len;
525+
526+
for (size_t i = 0; i < len; i++) {
527+
if (data[i] == ASCII_CTRL_C) {
528+
write_len = i;
529+
terminate = true;
530+
break;
531+
}
532+
}
533+
534+
res = cred_buf_write(data, write_len);
535+
if (res == -ENOMEM) {
536+
shell_set_bypass(sh, NULL);
537+
shell_fprintf(sh, SHELL_ERROR, "Not enough room in credential buffer for "
538+
"provided data. Increase "
539+
"CONFIG_TLS_CREDENTIALS_SHELL_CRED_BUF_SIZE.\n");
523540
shell_clear_cred_buf(sh);
524-
return 0;
541+
return;
525542
}
526543

544+
if (terminate) {
545+
shell_set_bypass(sh, NULL);
546+
shell_fprintf(sh, SHELL_NORMAL, "Stored %d bytes.\n", cred_written);
547+
}
548+
}
549+
550+
static int tls_cred_cmd_buf_clear(const struct shell *sh, size_t argc, char *argv[])
551+
{
552+
/* If the "clear" keyword is provided, clear the buffer rather than write to it. */
553+
(void)cred_buf_clear();
554+
shell_fprintf(sh, SHELL_NORMAL, "Credential buffer cleared.\n");
555+
556+
return 0;
557+
}
558+
559+
static int tls_cred_cmd_buf_load(const struct shell *sh, size_t argc, char *argv[])
560+
{
561+
shell_clear_cred_buf(sh);
562+
563+
shell_fprintf(sh, SHELL_NORMAL, "Input credential, finish with CTRL+C.\n");
564+
shell_set_bypass(sh, tls_cred_cmd_load_bypass);
565+
return 0;
566+
}
567+
568+
/* Buffers credential data into the credential buffer. */
569+
static int tls_cred_cmd_buf(const struct shell *sh, size_t argc, char *argv[])
570+
{
527571
/* Otherwise, assume provided arg is base64 and attempt to write it into the credential
528572
* buffer.
529573
*/
@@ -793,8 +837,15 @@ static int tls_cred_cmd_list(const struct shell *sh, size_t argc, char *argv[])
793837
return 0;
794838
}
795839

840+
SHELL_STATIC_SUBCMD_SET_CREATE(tls_cred_buf_cmds,
841+
SHELL_CMD(clear, NULL, "Clear the credential buffer", tls_cred_cmd_buf_clear),
842+
SHELL_CMD(load, NULL, "Load credential directly to buffer so it can be added.",
843+
tls_cred_cmd_buf_load),
844+
SHELL_SUBCMD_SET_END
845+
);
846+
796847
SHELL_STATIC_SUBCMD_SET_CREATE(tls_cred_cmds,
797-
SHELL_CMD_ARG(buf, NULL, "Buffer in credential data so it can be added.",
848+
SHELL_CMD_ARG(buf, &tls_cred_buf_cmds, "Buffer in credential data so it can be added.",
798849
tls_cred_cmd_buf, 2, 0),
799850
SHELL_CMD_ARG(add, NULL, "Add a TLS credential.",
800851
tls_cred_cmd_add, 5, 1),

0 commit comments

Comments
 (0)