From 8584b89d306c03fa108d1a3e9dae079658013bbb Mon Sep 17 00:00:00 2001 From: Wesley Rosenblum <55108558+WesleyRosenblum@users.noreply.github.com> Date: Mon, 15 May 2023 19:36:21 -0400 Subject: [PATCH] fix(s2nd): parse psk given to s2nd non-destructively (#4006) * fix: use stuffer for psk params * specify precision when printing the psk identity * fix formatting * cast token pointer * Copy the string instead of using stuffer * Remove empty line * remove empty line * fix comment format --- bin/common.c | 8 +++++++- bin/echo.c | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bin/common.c b/bin/common.c index 6e709a743ad..35295753394 100644 --- a/bin/common.c +++ b/bin/common.c @@ -263,8 +263,13 @@ static int s2n_setup_external_psk(struct s2n_psk **psk, char *params) GUARD_EXIT_NULL(psk); GUARD_EXIT_NULL(params); + /* duplicate params as strtok will modify the input string */ + char *params_dup = malloc(strlen(params) + 1); + GUARD_EXIT_NULL(params_dup); + strcpy(params_dup, params); + size_t token_idx = 0; - for (char *token = strtok(params, ","); token != NULL; token = strtok(NULL, ","), token_idx++) { + for (char *token = strtok(params_dup, ","); token != NULL; token = strtok(NULL, ","), token_idx++) { switch (token_idx) { case 0: GUARD_EXIT(s2n_psk_set_identity(*psk, (const uint8_t *) token, strlen(token)), @@ -288,6 +293,7 @@ static int s2n_setup_external_psk(struct s2n_psk **psk, char *params) } } + free(params_dup); return S2N_SUCCESS; } diff --git a/bin/echo.c b/bin/echo.c index 24438d2cdce..4b7cc1db969 100644 --- a/bin/echo.c +++ b/bin/echo.c @@ -213,7 +213,7 @@ int print_connection_info(struct s2n_connection *conn) uint8_t *identity = (uint8_t *) malloc(identity_length); GUARD_EXIT_NULL(identity); GUARD_EXIT(s2n_connection_get_negotiated_psk_identity(conn, identity, identity_length), "Error getting negotiated psk identity from the connection\n"); - printf("Negotiated PSK identity: %s\n", identity); + printf("Negotiated PSK identity: %.*s\n", identity_length, identity); free(identity); }