| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "api/s2n.h" |
| 17 |
#include "s2n_test.h" |
| 18 |
#include "testlib/s2n_testlib.h" |
| 19 |
#include "tls/s2n_connection.h" |
| 20 |
#include "tls/s2n_handshake.h" |
| 21 |
#include "utils/s2n_safety.h" |
| 22 |
|
| 23 |
#define TEST_DATA_SIZE 8 |
| 24 |
|
| 25 |
int main(int argc, char **argv) |
| 26 |
{ |
| 27 |
const char *protocols[] = { "http/1.1", "spdy/3.1", "h2" }; |
| 28 |
const char *mismatch_protocols[] = { "spdy/2" }; |
| 29 |
|
| 30 |
BEGIN_TEST(); |
| 31 |
|
| 32 |
DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free); |
| 33 |
EXPECT_NOT_NULL(server_config); |
| 34 |
EXPECT_SUCCESS(s2n_config_set_protocol_preferences(server_config, protocols, |
| 35 |
s2n_array_len(protocols))); |
| 36 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "20240501")); |
| 37 |
|
| 38 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, |
| 39 |
s2n_cert_chain_and_key_ptr_free); |
| 40 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, S2N_DEFAULT_TEST_CERT_CHAIN, |
| 41 |
S2N_DEFAULT_TEST_PRIVATE_KEY)); |
| 42 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key)); |
| 43 |
|
| 44 |
struct { |
| 45 |
int client_protocol_count; |
| 46 |
const char *const *client_protocols; |
| 47 |
const char *expected_protocol; |
| 48 |
} test_cases[] = { |
| 49 |
|
| 50 |
{ |
| 51 |
.client_protocol_count = 0, |
| 52 |
.client_protocols = NULL, |
| 53 |
.expected_protocol = NULL, |
| 54 |
}, |
| 55 |
|
| 56 |
{ |
| 57 |
.client_protocol_count = s2n_array_len(protocols), |
| 58 |
.client_protocols = protocols, |
| 59 |
.expected_protocol = protocols[0], |
| 60 |
}, |
| 61 |
|
| 62 |
{ |
| 63 |
.client_protocol_count = 1, |
| 64 |
.client_protocols = &protocols[1], |
| 65 |
.expected_protocol = protocols[1], |
| 66 |
}, |
| 67 |
|
| 68 |
{ |
| 69 |
.client_protocol_count = 1, |
| 70 |
.client_protocols = mismatch_protocols, |
| 71 |
.expected_protocol = NULL, |
| 72 |
}, |
| 73 |
}; |
| 74 |
|
| 75 |
for (size_t i = 0; i < s2n_array_len(test_cases); i++) { |
| 76 |
DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free); |
| 77 |
EXPECT_NOT_NULL(client_config); |
| 78 |
EXPECT_OK(s2n_config_set_tls12_security_policy(client_config)); |
| 79 |
EXPECT_SUCCESS(s2n_config_set_protocol_preferences(client_config, |
| 80 |
test_cases[i].client_protocols, test_cases[i].client_protocol_count)); |
| 81 |
EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config)); |
| 82 |
|
| 83 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 84 |
s2n_connection_ptr_free); |
| 85 |
EXPECT_NOT_NULL(client); |
| 86 |
EXPECT_SUCCESS(s2n_connection_set_config(client, client_config)); |
| 87 |
|
| 88 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 89 |
s2n_connection_ptr_free); |
| 90 |
EXPECT_NOT_NULL(server); |
| 91 |
EXPECT_SUCCESS(s2n_connection_set_config(server, server_config)); |
| 92 |
|
| 93 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 94 |
EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair)); |
| 95 |
EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair)); |
| 96 |
|
| 97 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 98 |
|
| 99 |
if (test_cases[i].expected_protocol == NULL) { |
| 100 |
EXPECT_NULL(s2n_get_application_protocol(server)); |
| 101 |
} else { |
| 102 |
EXPECT_STRING_EQUAL(s2n_get_application_protocol(server), |
| 103 |
test_cases[i].expected_protocol); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 108 |
uint8_t data_send[TEST_DATA_SIZE] = { 3, 1, 4, 1, 5, 9, 2, 6 }; |
| 109 |
uint8_t data_recv[TEST_DATA_SIZE] = { 0 }; |
| 110 |
EXPECT_EQUAL(s2n_send(client, data_send, sizeof(data_send), &blocked), sizeof(data_send)); |
| 111 |
EXPECT_EQUAL(s2n_recv(server, data_recv, sizeof(data_recv), &blocked), sizeof(data_recv)); |
| 112 |
EXPECT_BYTEARRAY_EQUAL(data_send, data_recv, sizeof(data_send)); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
{ |
| 117 |
DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free); |
| 118 |
EXPECT_NOT_NULL(client_config); |
| 119 |
EXPECT_OK(s2n_config_set_tls12_security_policy(client_config)); |
| 120 |
EXPECT_SUCCESS(s2n_config_set_protocol_preferences(client_config, protocols, |
| 121 |
s2n_array_len(protocols))); |
| 122 |
EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config)); |
| 123 |
|
| 124 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 125 |
s2n_connection_ptr_free); |
| 126 |
EXPECT_NOT_NULL(client); |
| 127 |
EXPECT_SUCCESS(s2n_connection_set_config(client, client_config)); |
| 128 |
|
| 129 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 130 |
s2n_connection_ptr_free); |
| 131 |
EXPECT_NOT_NULL(server); |
| 132 |
EXPECT_SUCCESS(s2n_connection_set_config(server, server_config)); |
| 133 |
|
| 134 |
|
| 135 |
EXPECT_SUCCESS(s2n_connection_set_protocol_preferences(server, &protocols[2], 1)); |
| 136 |
|
| 137 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 138 |
EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair)); |
| 139 |
EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair)); |
| 140 |
|
| 141 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
EXPECT_STRING_EQUAL(s2n_get_application_protocol(server), protocols[2]); |
| 147 |
} |
| 148 |
|
| 149 |
END_TEST(); |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |