Skip to content
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

fix: Fix redundant code #4504

Merged
merged 4 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 6 additions & 28 deletions tests/unit/s2n_client_psk_extension_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,6 @@ static int mock_time(void *data, uint64_t *nanoseconds)
return S2N_SUCCESS;
}

static int s2n_setup_ticket_key(struct s2n_config *config)
{
/**
*= https://tools.ietf.org/rfc/rfc5869#appendix-A.1
*# PRK = 0x077709362c2e32df0ddc3f0dc47bba63
*# 90b6c73bb50f9c3122ec844ad7c2b3e5 (32 octets)
**/
S2N_BLOB_FROM_HEX(ticket_key,
"077709362c2e32df0ddc3f0dc47bba63"
"90b6c73bb50f9c3122ec844ad7c2b3e5");

/* Set up encryption key */
uint64_t current_time = 0;
uint8_t ticket_key_name[16] = "2016.07.26.15\0";

POSIX_GUARD(s2n_config_set_session_tickets_onoff(config, 1));
POSIX_GUARD(config->wall_clock(config->sys_clock_ctx, &current_time));
POSIX_GUARD(s2n_config_add_ticket_crypto_key(config, ticket_key_name, strlen((char *) ticket_key_name),
ticket_key.data, ticket_key.size, current_time / ONE_SEC_IN_NANOS));
return S2N_SUCCESS;
}

static S2N_RESULT s2n_setup_encrypted_ticket(struct s2n_connection *conn, struct s2n_stuffer *output)
{
conn->tls13_ticket_fields = (struct s2n_ticket_fields){ 0 };
Expand Down Expand Up @@ -701,7 +679,7 @@ int main(int argc, char **argv)

struct s2n_config *config = s2n_config_new();
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_setup_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));

conn->actual_protocol_version = S2N_TLS13;
Expand Down Expand Up @@ -736,7 +714,7 @@ int main(int argc, char **argv)

struct s2n_config *config = s2n_config_new();
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_setup_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));

conn->actual_protocol_version = S2N_TLS13;
Expand Down Expand Up @@ -809,7 +787,7 @@ int main(int argc, char **argv)

struct s2n_config *config = s2n_config_new();
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_setup_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));

conn->actual_protocol_version = S2N_TLS13;
Expand Down Expand Up @@ -1044,11 +1022,11 @@ int main(int argc, char **argv)
EXPECT_NOT_NULL(config_with_cb);
uint16_t expected_wire_choice = 0;
EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config_with_cb, s2n_test_select_psk_identity_callback, &expected_wire_choice));
EXPECT_SUCCESS(s2n_setup_ticket_key(config_with_cb));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config_with_cb));

struct s2n_config *config = s2n_config_new();
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_setup_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));

EXPECT_SUCCESS(s2n_connection_set_config(conn, config));

Expand Down Expand Up @@ -1549,7 +1527,7 @@ int main(int argc, char **argv)
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
EXPECT_SUCCESS(s2n_setup_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));

DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
Expand Down
26 changes: 2 additions & 24 deletions tests/unit/s2n_psk_offered_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,6 @@

#define MILLIS_TO_NANOS(millis) (millis * (uint64_t) ONE_MILLISEC_IN_NANOS)

static int s2n_setup_ticket_key(struct s2n_config *config)
{
/**
*= https://tools.ietf.org/rfc/rfc5869#appendix-A.1
*# PRK = 0x077709362c2e32df0ddc3f0dc47bba63
*# 90b6c73bb50f9c3122ec844ad7c2b3e5 (32 octets)
**/
S2N_BLOB_FROM_HEX(ticket_key,
"077709362c2e32df0ddc3f0dc47bba63"
"90b6c73bb50f9c3122ec844ad7c2b3e5");

/* Set up encryption key */
uint64_t current_time = 0;
uint8_t ticket_key_name[16] = "2016.07.26.15\0";

POSIX_GUARD(s2n_config_set_session_tickets_onoff(config, 1));
POSIX_GUARD(config->wall_clock(config->sys_clock_ctx, &current_time));
POSIX_GUARD(s2n_config_add_ticket_crypto_key(config, ticket_key_name, strlen((char *) ticket_key_name),
ticket_key.data, ticket_key.size, current_time / ONE_SEC_IN_NANOS));
return S2N_SUCCESS;
}

static S2N_RESULT s2n_setup_encrypted_ticket(struct s2n_connection *conn, struct s2n_stuffer *output)
{
conn->tls13_ticket_fields = (struct s2n_ticket_fields){ 0 };
Expand Down Expand Up @@ -470,7 +448,7 @@ int main(int argc, char **argv)

struct s2n_config *config = s2n_config_new();
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_setup_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));

server_conn->psk_params.type = S2N_PSK_TYPE_RESUMPTION;
Expand Down Expand Up @@ -509,7 +487,7 @@ int main(int argc, char **argv)

struct s2n_config *config = s2n_config_new();
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_setup_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));

server_conn->psk_params.type = S2N_PSK_TYPE_RESUMPTION;
Expand Down
28 changes: 2 additions & 26 deletions tests/unit/s2n_self_talk_session_resumption_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,6 @@ static int s2n_test_session_ticket_cb(struct s2n_connection *conn, void *ctx, st
return S2N_SUCCESS;
}

static int s2n_setup_test_ticket_key(struct s2n_config *config)
{
POSIX_ENSURE_REF(config);

/**
*= https://tools.ietf.org/rfc/rfc5869#appendix-A.1
*# PRK = 0x077709362c2e32df0ddc3f0dc47bba63
*# 90b6c73bb50f9c3122ec844ad7c2b3e5 (32 octets)
**/
S2N_BLOB_FROM_HEX(ticket_key,
"077709362c2e32df0ddc3f0dc47bba63"
"90b6c73bb50f9c3122ec844ad7c2b3e5");

/* Set up encryption key */
uint64_t current_time = 0;
uint8_t ticket_key_name[16] = "2016.07.26.15\0";
EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(config, 1));
EXPECT_SUCCESS(config->wall_clock(config->sys_clock_ctx, &current_time));
EXPECT_SUCCESS(s2n_config_add_ticket_crypto_key(config, ticket_key_name, strlen((char *) ticket_key_name),
ticket_key.data, ticket_key.size, current_time / ONE_SEC_IN_NANOS));

return S2N_SUCCESS;
}

static S2N_RESULT s2n_test_issue_new_session_ticket(struct s2n_connection *server_conn, struct s2n_connection *client_conn,
const struct s2n_early_data_test_case *early_data_case)
{
Expand Down Expand Up @@ -248,7 +224,7 @@ int main(int argc, char **argv)
S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, tls13_chain_and_key));
EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(server_config, true));
EXPECT_SUCCESS(s2n_setup_test_ticket_key(server_config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(server_config));

/* Setup TLS1.2 server config */
struct s2n_config *tls12_server_config = s2n_config_new();
Expand All @@ -260,7 +236,7 @@ int main(int argc, char **argv)
S2N_DEFAULT_TEST_PRIVATE_KEY));
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(tls12_server_config, tls12_chain_and_key));
EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(tls12_server_config, true));
EXPECT_SUCCESS(s2n_setup_test_ticket_key(tls12_server_config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(tls12_server_config));

/* Setup TLS1.3 client config */
struct s2n_config *tls13_client_config = s2n_config_new();
Expand Down
56 changes: 16 additions & 40 deletions tests/unit/s2n_server_new_session_ticket_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,6 @@ static int s2n_test_session_ticket_cb(struct s2n_connection *conn, void *ctx, st
return S2N_SUCCESS;
}

static int s2n_setup_test_ticket_key(struct s2n_config *config)
{
POSIX_ENSURE_REF(config);

/**
*= https://tools.ietf.org/rfc/rfc5869#appendix-A.1
*# PRK = 0x077709362c2e32df0ddc3f0dc47bba63
*# 90b6c73bb50f9c3122ec844ad7c2b3e5 (32 octets)
**/
S2N_BLOB_FROM_HEX(ticket_key,
"077709362c2e32df0ddc3f0dc47bba63"
"90b6c73bb50f9c3122ec844ad7c2b3e5");

/* Set up encryption key */
uint64_t current_time = 0;
uint8_t ticket_key_name[16] = "2016.07.26.15\0";
EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(config, 1));
EXPECT_SUCCESS(config->wall_clock(config->sys_clock_ctx, &current_time));
EXPECT_SUCCESS(s2n_config_add_ticket_crypto_key(config, ticket_key_name, strlen((char *) ticket_key_name),
ticket_key.data, ticket_key.size, current_time / ONE_SEC_IN_NANOS));

return S2N_SUCCESS;
}

static int s2n_setup_test_resumption_secret(struct s2n_connection *conn)
{
POSIX_ENSURE_REF(conn);
Expand Down Expand Up @@ -117,7 +93,7 @@ int main(int argc, char **argv)
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
EXPECT_NOT_NULL(config = s2n_config_new());

EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));

conn->actual_protocol_version = S2N_TLS13;
Expand Down Expand Up @@ -189,7 +165,7 @@ int main(int argc, char **argv)
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
EXPECT_NOT_NULL(config = s2n_config_new());

EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));

conn->actual_protocol_version = S2N_TLS13;
Expand Down Expand Up @@ -219,7 +195,7 @@ int main(int argc, char **argv)
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
EXPECT_NOT_NULL(config = s2n_config_new());

EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));

conn->actual_protocol_version = S2N_TLS13;
Expand Down Expand Up @@ -261,7 +237,7 @@ int main(int argc, char **argv)

struct s2n_config *config = s2n_config_new();
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));

struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(conn);
Expand Down Expand Up @@ -300,7 +276,7 @@ int main(int argc, char **argv)
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(config, 1));
EXPECT_SUCCESS(s2n_config_set_session_ticket_cb(config, s2n_test_session_ticket_cb, NULL));
EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));

struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(server_conn);
Expand Down Expand Up @@ -655,7 +631,7 @@ int main(int argc, char **argv)
EXPECT_NOT_NULL(server_conn);

EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(config, 1));
EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));

EXPECT_SUCCESS(s2n_config_set_session_ticket_cb(config, s2n_test_session_ticket_cb, NULL));
Expand Down Expand Up @@ -688,7 +664,7 @@ int main(int argc, char **argv)
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(config, 1));
EXPECT_SUCCESS(s2n_config_set_session_ticket_cb(config, s2n_test_session_ticket_cb, NULL));
EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));

struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(client_conn);
Expand Down Expand Up @@ -726,7 +702,7 @@ int main(int argc, char **argv)
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(config, 1));
EXPECT_SUCCESS(s2n_config_set_session_ticket_cb(config, s2n_test_session_ticket_cb, NULL));
EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));

struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(client_conn);
Expand Down Expand Up @@ -854,7 +830,7 @@ int main(int argc, char **argv)
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
s2n_config_ptr_free);
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));

DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
Expand Down Expand Up @@ -882,7 +858,7 @@ int main(int argc, char **argv)
{
struct s2n_config *config = s2n_config_new();
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));

struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(conn);
Expand Down Expand Up @@ -916,7 +892,7 @@ int main(int argc, char **argv)
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
s2n_config_ptr_free);
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));

DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
s2n_connection_ptr_free);
Expand Down Expand Up @@ -949,7 +925,7 @@ int main(int argc, char **argv)
struct s2n_connection *conn = NULL;
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
EXPECT_NOT_NULL(config = s2n_config_new());
EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));

conn->actual_protocol_version = S2N_TLS13;
Expand Down Expand Up @@ -998,7 +974,7 @@ int main(int argc, char **argv)

struct s2n_config *config = s2n_config_new();
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));

struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(conn);
Expand Down Expand Up @@ -1135,7 +1111,7 @@ int main(int argc, char **argv)
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
EXPECT_NOT_NULL(config = s2n_config_new());

EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));

conn->actual_protocol_version = S2N_TLS13;
Expand Down Expand Up @@ -1176,7 +1152,7 @@ int main(int argc, char **argv)
{
struct s2n_config *config = s2n_config_new();
EXPECT_NOT_NULL(config);
EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));

struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
EXPECT_NOT_NULL(conn);
Expand Down Expand Up @@ -1285,7 +1261,7 @@ int main(int argc, char **argv)
EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(config, 1));
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));

EXPECT_SUCCESS(s2n_setup_test_ticket_key(config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));

uint16_t tickets_to_send = 5;
Expand Down
26 changes: 1 addition & 25 deletions tests/unit/s2n_tls13_new_session_ticket_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,6 @@ static int s2n_test_session_ticket_cb(struct s2n_connection *conn, void *ctx, st
return S2N_SUCCESS;
}

static int s2n_setup_test_ticket_key(struct s2n_config *config)
{
POSIX_ENSURE_REF(config);

/**
*= https://tools.ietf.org/rfc/rfc5869#appendix-A.1
*# PRK = 0x077709362c2e32df0ddc3f0dc47bba63
*# 90b6c73bb50f9c3122ec844ad7c2b3e5 (32 octets)
**/
S2N_BLOB_FROM_HEX(ticket_key,
"077709362c2e32df0ddc3f0dc47bba63"
"90b6c73bb50f9c3122ec844ad7c2b3e5");

/* Set up encryption key */
uint64_t current_time = 0;
uint8_t ticket_key_name[S2N_TICKET_KEY_NAME_LEN] = "2016.07.26.15\0";
EXPECT_SUCCESS(config->wall_clock(config->sys_clock_ctx, &current_time));
EXPECT_SUCCESS(s2n_config_add_ticket_crypto_key(config, ticket_key_name, strlen((char *) ticket_key_name),
ticket_key.data, ticket_key.size, current_time / ONE_SEC_IN_NANOS));

return S2N_SUCCESS;
}

int main(int argc, char **argv)
{
BEGIN_TEST();
Expand All @@ -93,9 +70,8 @@ int main(int argc, char **argv)
struct s2n_cert_chain_and_key *chain_and_key = NULL;
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(server_config, 1));
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
EXPECT_SUCCESS(s2n_setup_test_ticket_key(server_config));
EXPECT_OK(s2n_resumption_test_ticket_key_setup(server_config));

EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(client_config, 1));
EXPECT_SUCCESS(s2n_config_set_session_ticket_cb(client_config, s2n_test_session_ticket_cb, NULL));
Expand Down
Loading