Skip to content

net: tls_credentials_shell: Add credential buffer load argument #90358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion doc/connectivity/networking/api/tls_credentials_shell.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ Buffer Credential (``buf``)

Buffer data incrementally into the credential buffer so that it can be added using the :ref:`tls_credentials_shell_add_cred` command.

Alternatively, clear the credential buffer.
Alternatively:

- Clear the credential buffer.

- Load credential directly to the credential buffer, ending with ``Ctrl + c``.

Usage
-----
Expand All @@ -28,6 +32,14 @@ To append ``<DATA>`` to the credential buffer, use:

Use this as many times as needed to load the full credential into the credential buffer, then use the :ref:`tls_credentials_shell_add_cred` command to store it.

To load ``<DATA>`` directly to the credential buffer, use:

.. code-block:: shell

cred buf load
<DATA>
Ctrl + c

To clear the credential buffer, use:

.. code-block:: shell
Expand Down
44 changes: 41 additions & 3 deletions subsys/net/lib/tls_credentials/tls_credentials_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,9 @@ static bool cred_buf_clear(void)
}

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

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

/* Report results. */

Expand Down Expand Up @@ -515,6 +515,37 @@ static int tls_cred_cmd_add(const struct shell *sh, size_t argc, char *argv[])
return err;
}

#define ASCII_CTRL_C 0x03

static void tls_cred_cmd_load_bypass(const struct shell *sh, uint8_t *data, size_t len)
{
bool escape = false;
int res;

for (size_t i = 0; i < len; i++) {
if (data[i] == ASCII_CTRL_C) {
len = i > 1 ? i - 1 : 0;
escape = true;
break;
}
}

res = cred_buf_write(data, len);
if (res == -ENOMEM) {
shell_set_bypass(sh, NULL);
shell_fprintf(sh, SHELL_ERROR, "Not enough room in credential buffer for "
"provided data. Increase "
"CONFIG_TLS_CREDENTIALS_SHELL_CRED_BUF_SIZE.\n");
shell_clear_cred_buf(sh);
return;
}

if (escape) {
shell_set_bypass(sh, NULL);
shell_fprintf(sh, SHELL_NORMAL, "Stored %d bytes.\n", cred_written);
}
}

/* Buffers credential data into the credential buffer. */
static int tls_cred_cmd_buf(const struct shell *sh, size_t argc, char *argv[])
{
Expand All @@ -524,6 +555,13 @@ static int tls_cred_cmd_buf(const struct shell *sh, size_t argc, char *argv[])
return 0;
}

if (strcmp(argv[1], "load") == 0) {
shell_clear_cred_buf(sh);

shell_set_bypass(sh, tls_cred_cmd_load_bypass);
return 0;
}

/* Otherwise, assume provided arg is base64 and attempt to write it into the credential
* buffer.
*/
Expand Down
Loading