From fd16f156ea12c409b35cef519d9177773cf268e1 Mon Sep 17 00:00:00 2001 From: Sam Clark <3758302+goatgoose@users.noreply.github.com> Date: Tue, 19 Sep 2023 19:39:21 -0400 Subject: [PATCH] improve malformed length tests --- tests/unit/s2n_client_hello_test.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/tests/unit/s2n_client_hello_test.c b/tests/unit/s2n_client_hello_test.c index 88daf77fba2..f8751ec59eb 100644 --- a/tests/unit/s2n_client_hello_test.c +++ b/tests/unit/s2n_client_hello_test.c @@ -1634,7 +1634,8 @@ int main(int argc, char **argv) /* s2n_client_hello_get_supported_groups tests */ { - + /* Allocate space for 2 length bytes and S2N_MAX_RECEIVED_SUPPORTED_GROUPS groups */ +#define TEST_SUPPORTED_GROUPS_EXTENSION_SIZE (2 + (S2N_MAX_RECEIVED_SUPPORTED_GROUPS * 2)) /* Safety */ { @@ -1692,6 +1693,8 @@ int main(int argc, char **argv) /* Test s2n_client_hello_get_supported_groups when a supported groups extension wasn't received. */ { + struct s2n_client_hello client_hello = { 0 }; + } @@ -1702,17 +1705,16 @@ int main(int argc, char **argv) s2n_extension_type_id supported_groups_id = 0; EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(S2N_EXTENSION_SUPPORTED_GROUPS, &supported_groups_id)); - /* Allocate space for 2 length bytes and S2N_MAX_RECEIVED_SUPPORTED_GROUPS groups */ - uint8_t extension_data[2 + (S2N_MAX_RECEIVED_SUPPORTED_GROUPS * 2)] = { 0 }; - struct s2n_blob extension_blob = { 0 }; - EXPECT_SUCCESS(s2n_blob_init(&extension_blob, extension_data, sizeof(extension_data))); - s2n_parsed_extension *supported_groups_extension = &client_hello.extensions.parsed_extensions[supported_groups_id]; - supported_groups_extension->extension = extension_blob; supported_groups_extension->extension_type = S2N_EXTENSION_SUPPORTED_GROUPS; /* Test parsing a correct groups list length */ { + uint8_t extension_data[TEST_SUPPORTED_GROUPS_EXTENSION_SIZE] = { 0 }; + struct s2n_blob extension_blob = { 0 }; + EXPECT_SUCCESS(s2n_blob_init(&extension_blob, extension_data, sizeof(extension_data))); + supported_groups_extension->extension = extension_blob; + struct s2n_stuffer extension_stuffer = { 0 }; EXPECT_SUCCESS(s2n_stuffer_init(&extension_stuffer, &extension_blob)); @@ -1729,6 +1731,11 @@ int main(int argc, char **argv) /* Test parsing a groups list length that is larger than the extension length */ { + uint8_t extension_data[TEST_SUPPORTED_GROUPS_EXTENSION_SIZE] = { 0 }; + struct s2n_blob extension_blob = { 0 }; + EXPECT_SUCCESS(s2n_blob_init(&extension_blob, extension_data, sizeof(extension_data))); + supported_groups_extension->extension = extension_blob; + struct s2n_stuffer extension_stuffer = { 0 }; EXPECT_SUCCESS(s2n_stuffer_init(&extension_stuffer, &extension_blob)); @@ -1746,6 +1753,11 @@ int main(int argc, char **argv) /* Test parsing a groups list that contains a partial supported group */ { + uint8_t extension_data[TEST_SUPPORTED_GROUPS_EXTENSION_SIZE] = { 0 }; + struct s2n_blob extension_blob = { 0 }; + EXPECT_SUCCESS(s2n_blob_init(&extension_blob, extension_data, sizeof(extension_data))); + supported_groups_extension->extension = extension_blob; + struct s2n_stuffer extension_stuffer = { 0 }; EXPECT_SUCCESS(s2n_stuffer_init(&extension_stuffer, &extension_blob)); @@ -1760,7 +1772,6 @@ int main(int argc, char **argv) EXPECT_EQUAL(supported_groups_count, 0); } - } /* Ensure that s2n_client_hello_get_supported_groups writes what is contained in the parsed