Skip to content

Commit

Permalink
parse supported groups into a buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
goatgoose committed Sep 19, 2023
1 parent b4f0c5c commit 201852c
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions tls/extensions/s2n_client_supported_groups.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "tls/s2n_tls_parameters.h"
#include "utils/s2n_safety.h"

#define S2N_MAX_RECEIVED_SUPPORTED_GROUPS 64

static int s2n_client_supported_groups_send(struct s2n_connection *conn, struct s2n_stuffer *out);
static int s2n_client_supported_groups_recv(struct s2n_connection *conn, struct s2n_stuffer *extension);

Expand Down Expand Up @@ -78,6 +80,40 @@ static int s2n_client_supported_groups_send(struct s2n_connection *conn, struct
return S2N_SUCCESS;
}

static S2N_RESULT s2n_client_supported_groups_validate_list_size(struct s2n_stuffer *extension, uint16_t list_size)
{
RESULT_ENSURE_LTE(list_size, s2n_stuffer_data_available(extension));
RESULT_ENSURE_EQ(list_size % sizeof(uint16_t), 0);
return S2N_RESULT_OK;
}

S2N_RESULT s2n_client_supported_groups_parse(struct s2n_stuffer *extension, uint16_t *supported_groups_list,
uint16_t supported_groups_list_len, uint16_t *supported_groups_count_out)
{
RESULT_ENSURE_REF(supported_groups_count_out);
*supported_groups_count_out = 0;

RESULT_ENSURE_REF(extension);
RESULT_ENSURE_REF(supported_groups_list);

uint16_t supported_groups_list_size = 0;
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(extension, &supported_groups_list_size));
if (s2n_result_is_error(s2n_client_supported_groups_validate_list_size(extension, supported_groups_list_size))) {
/* Malformed length, ignore the extension */
return S2N_RESULT_OK;
}

uint16_t supported_groups_count = supported_groups_list_size / 2;
RESULT_ENSURE_LTE(supported_groups_count, supported_groups_list_len);
*supported_groups_count_out = supported_groups_count;

for (size_t i = 0; i < supported_groups_count; i++) {
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(extension, &supported_groups_list[i]));
}

return S2N_RESULT_OK;
}

/* Populates the appropriate index of either the mutually_supported_curves or
* mutually_supported_kem_groups array based on the received IANA ID. Will
* ignore unrecognized IANA IDs (and return success). */
Expand Down Expand Up @@ -165,17 +201,13 @@ static int s2n_client_supported_groups_recv(struct s2n_connection *conn, struct
POSIX_ENSURE_REF(conn);
POSIX_ENSURE_REF(extension);

uint16_t size_of_all;
POSIX_GUARD(s2n_stuffer_read_uint16(extension, &size_of_all));
if (size_of_all > s2n_stuffer_data_available(extension) || (size_of_all % sizeof(uint16_t))) {
/* Malformed length, ignore the extension */
return S2N_SUCCESS;
}
uint16_t supported_groups[S2N_MAX_RECEIVED_SUPPORTED_GROUPS] = { 0 };
uint16_t supported_groups_count = 0;
POSIX_GUARD_RESULT(s2n_client_supported_groups_parse(extension, supported_groups, S2N_MAX_RECEIVED_SUPPORTED_GROUPS,
&supported_groups_count));

for (size_t i = 0; i < (size_of_all / sizeof(uint16_t)); i++) {
uint16_t iana_id;
POSIX_GUARD(s2n_stuffer_read_uint16(extension, &iana_id));
POSIX_GUARD(s2n_client_supported_groups_recv_iana_id(conn, iana_id));
for (size_t i = 0; i < supported_groups_count; i++) {
POSIX_GUARD(s2n_client_supported_groups_recv_iana_id(conn, supported_groups[i]));
}

POSIX_GUARD(s2n_choose_supported_group(conn));
Expand Down

0 comments on commit 201852c

Please sign in to comment.