| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <pthread.h> |
| 17 |
#include <sys/types.h> |
| 18 |
#include <sys/wait.h> |
| 19 |
|
| 20 |
#include "crypto/s2n_sequence.h" |
| 21 |
#include "s2n_test.h" |
| 22 |
#include "testlib/s2n_examples.h" |
| 23 |
#include "testlib/s2n_testlib.h" |
| 24 |
#include "utils/s2n_random.h" |
| 25 |
|
| 26 |
#define S2N_TEST_BUFFER_SIZE 1000 |
| 27 |
#define S2N_TEST_ENCRYPTION_LIMIT 3 |
| 28 |
#define S2N_TEST_KEY_UPDATE_COUNT 25 |
| 29 |
#define S2N_TEST_RECORD_COUNT (S2N_TEST_ENCRYPTION_LIMIT * S2N_TEST_KEY_UPDATE_COUNT) |
| 30 |
#define S2N_TEST_BYTES_TO_SEND (S2N_DEFAULT_FRAGMENT_LENGTH * S2N_TEST_RECORD_COUNT) |
| 31 |
|
| 32 |
#define S2N_CIPHER_SUITE_WITH_LIMIT(name, source, limit) \ |
| 33 |
struct s2n_cipher_suite name = *(source); \ |
| 34 |
struct s2n_record_algorithm _##name##_record_alg = *name.record_alg; \ |
| 35 |
_##name##_record_alg.encryption_limit = limit; \ |
| 36 |
name.record_alg = &_##name##_record_alg; |
| 37 |
|
| 38 |
S2N_RESULT s2n_set_key_update_request_for_testing(s2n_peer_key_update request); |
| 39 |
|
| 40 |
static void *s2n_send_random_data(void *arg) |
| 41 |
{ |
| 42 |
struct s2n_connection *conn = (struct s2n_connection *) arg; |
| 43 |
|
| 44 |
uint8_t buffer[S2N_TEST_BUFFER_SIZE] = "hello world"; |
| 45 |
|
| 46 |
size_t bytes_to_send = S2N_TEST_BYTES_TO_SEND; |
| 47 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 48 |
while (bytes_to_send) { |
| 49 |
int r = s2n_send(conn, buffer, S2N_MIN(sizeof(buffer), bytes_to_send), &blocked); |
| 50 |
if (r >= 0) { |
| 51 |
bytes_to_send -= r; |
| 52 |
} else if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) { |
| 53 |
fprintf(stderr, "Send error: %s. %s\n", s2n_strerror(s2n_errno, NULL), s2n_strerror_debug(s2n_errno, NULL)); |
| 54 |
return NULL; |
| 55 |
} |
| 56 |
} |
| 57 |
return conn; |
| 58 |
} |
| 59 |
|
| 60 |
static void *s2n_recv_random_data(void *arg) |
| 61 |
{ |
| 62 |
struct s2n_connection *conn = (struct s2n_connection *) arg; |
| 63 |
|
| 64 |
uint8_t buffer[S2N_TEST_BUFFER_SIZE] = { 0 }; |
| 65 |
|
| 66 |
size_t bytes_to_read = S2N_TEST_BYTES_TO_SEND; |
| 67 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 68 |
while (bytes_to_read) { |
| 69 |
int r = s2n_recv(conn, buffer, S2N_MIN(sizeof(buffer), bytes_to_read), &blocked); |
| 70 |
if (r >= 0) { |
| 71 |
bytes_to_read -= r; |
| 72 |
} else if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) { |
| 73 |
fprintf(stderr, "Recv error: %s. %s\n", s2n_strerror(s2n_errno, NULL), s2n_strerror_debug(s2n_errno, NULL)); |
| 74 |
return NULL; |
| 75 |
} |
| 76 |
} |
| 77 |
return conn; |
| 78 |
} |
| 79 |
|
| 80 |
static S2N_RESULT s2n_send_and_recv_random_data(struct s2n_connection *conn) |
| 81 |
{ |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
pthread_t reader = 0; |
| 88 |
RESULT_ENSURE_EQ(pthread_create(&reader, NULL, s2n_recv_random_data, (void *) conn), 0); |
| 89 |
|
| 90 |
pthread_t writer = 0; |
| 91 |
RESULT_ENSURE_EQ(pthread_create(&writer, NULL, s2n_send_random_data, (void *) conn), 0); |
| 92 |
|
| 93 |
void *reader_return = NULL; |
| 94 |
RESULT_ENSURE_EQ(pthread_join(reader, &reader_return), 0); |
| 95 |
RESULT_ENSURE_REF(reader_return); |
| 96 |
|
| 97 |
void *writer_return = NULL; |
| 98 |
RESULT_ENSURE_EQ(pthread_join(writer, &writer_return), 0); |
| 99 |
RESULT_ENSURE_REF(writer_return); |
| 100 |
|
| 101 |
RESULT_ENSURE_GT(conn->wire_bytes_out, S2N_TEST_BYTES_TO_SEND); |
| 102 |
RESULT_ENSURE_GT(conn->wire_bytes_in, S2N_TEST_BYTES_TO_SEND); |
| 103 |
return S2N_RESULT_OK; |
| 104 |
} |
| 105 |
|
| 106 |
static S2N_RESULT s2n_sanity_check_key_updates_sent(struct s2n_connection *conn) |
| 107 |
{ |
| 108 |
struct s2n_blob seq_num_blob = { 0 }; |
| 109 |
RESULT_GUARD(s2n_connection_get_sequence_number(conn, conn->mode, &seq_num_blob)); |
| 110 |
|
| 111 |
uint64_t seq_num = 0; |
| 112 |
RESULT_GUARD_POSIX(s2n_sequence_number_to_uint64(&seq_num_blob, &seq_num)); |
| 113 |
RESULT_ENSURE_LTE(seq_num, conn->secure->cipher_suite->record_alg->encryption_limit); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
size_t max_record_size = S2N_TLS13_MAX_RECORD_LEN_FOR(conn->max_outgoing_fragment_length); |
| 136 |
RESULT_ENSURE_GT(conn->wire_bytes_out, max_record_size * seq_num); |
| 137 |
|
| 138 |
return S2N_RESULT_OK; |
| 139 |
} |
| 140 |
|
| 141 |
static S2N_RESULT s2n_test_encryption_limits(struct s2n_connection *conn) |
| 142 |
{ |
| 143 |
RESULT_GUARD_POSIX(s2n_example_negotiate(conn)); |
| 144 |
|
| 145 |
struct s2n_cipher_suite *original_suite = conn->secure->cipher_suite; |
| 146 |
S2N_CIPHER_SUITE_WITH_LIMIT(key_limit_suite, original_suite, S2N_TEST_ENCRYPTION_LIMIT); |
| 147 |
|
| 148 |
conn->secure->cipher_suite = &key_limit_suite; |
| 149 |
|
| 150 |
RESULT_GUARD(s2n_send_and_recv_random_data(conn)); |
| 151 |
RESULT_GUARD(s2n_sanity_check_key_updates_sent(conn)); |
| 152 |
|
| 153 |
conn->secure->cipher_suite = original_suite; |
| 154 |
return S2N_RESULT_OK; |
| 155 |
} |
| 156 |
|
| 157 |
static S2N_RESULT s2n_test_peer_requests(struct s2n_connection *conn) |
| 158 |
{ |
| 159 |
RESULT_GUARD_POSIX(s2n_example_negotiate(conn)); |
| 160 |
|
| 161 |
struct s2n_cipher_suite *original_suite = conn->secure->cipher_suite; |
| 162 |
S2N_CIPHER_SUITE_WITH_LIMIT(key_limit_suite, original_suite, S2N_TEST_ENCRYPTION_LIMIT); |
| 163 |
|
| 164 |
conn->secure->cipher_suite = &key_limit_suite; |
| 165 |
if (conn->mode == S2N_CLIENT) { |
| 166 |
RESULT_GUARD(s2n_set_key_update_request_for_testing(S2N_KEY_UPDATE_REQUESTED)); |
| 167 |
} |
| 168 |
|
| 169 |
RESULT_GUARD(s2n_send_and_recv_random_data(conn)); |
| 170 |
RESULT_GUARD(s2n_sanity_check_key_updates_sent(conn)); |
| 171 |
|
| 172 |
conn->secure->cipher_suite = original_suite; |
| 173 |
return S2N_RESULT_OK; |
| 174 |
} |
| 175 |
|
| 176 |
typedef S2N_RESULT (*s2n_test_scenario)(struct s2n_connection *conn); |
| 177 |
static S2N_RESULT s2n_run_self_talk_test(s2n_test_scenario scenario_fn) |
| 178 |
{ |
| 179 |
struct s2n_cert_chain_and_key *chain_and_key = NULL; |
| 180 |
RESULT_GUARD_POSIX(s2n_test_cert_chain_and_key_new(&chain_and_key, |
| 181 |
S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY)); |
| 182 |
|
| 183 |
struct s2n_config *config = s2n_config_new(); |
| 184 |
RESULT_ENSURE_REF(config); |
| 185 |
RESULT_GUARD_POSIX(s2n_config_set_unsafe_for_testing(config)); |
| 186 |
RESULT_GUARD_POSIX(s2n_config_set_cipher_preferences(config, "default_tls13")); |
| 187 |
RESULT_GUARD_POSIX(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key)); |
| 188 |
|
| 189 |
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close); |
| 190 |
RESULT_GUARD_POSIX(s2n_io_pair_init_non_blocking(&io_pair)); |
| 191 |
|
| 192 |
pid_t client_pid = fork(); |
| 193 |
if (client_pid == 0) { |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
fclose(stdout); |
| 198 |
|
| 199 |
struct s2n_connection *client = s2n_connection_new(S2N_CLIENT); |
| 200 |
EXPECT_NOT_NULL(client); |
| 201 |
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 202 |
|
| 203 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 204 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(client, &io_pair)); |
| 205 |
|
| 206 |
EXPECT_OK(scenario_fn(client)); |
| 207 |
|
| 208 |
EXPECT_SUCCESS(s2n_connection_free(client)); |
| 209 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key)); |
| 210 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 211 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT)); |
| 212 |
|
| 213 |
exit(EXIT_SUCCESS); |
| 214 |
} |
| 215 |
|
| 216 |
pid_t server_pid = fork(); |
| 217 |
if (server_pid == 0) { |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
fclose(stdout); |
| 222 |
|
| 223 |
struct s2n_connection *server = s2n_connection_new(S2N_SERVER); |
| 224 |
EXPECT_NOT_NULL(server); |
| 225 |
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 226 |
|
| 227 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT)); |
| 228 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(server, &io_pair)); |
| 229 |
|
| 230 |
EXPECT_OK(scenario_fn(server)); |
| 231 |
|
| 232 |
EXPECT_SUCCESS(s2n_connection_free(server)); |
| 233 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key)); |
| 234 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 235 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 236 |
|
| 237 |
exit(EXIT_SUCCESS); |
| 238 |
} |
| 239 |
|
| 240 |
int status = 0; |
| 241 |
RESULT_ENSURE_EQ(waitpid(client_pid, &status, 0), client_pid); |
| 242 |
RESULT_ENSURE_EQ(status, EXIT_SUCCESS); |
| 243 |
RESULT_ENSURE_EQ(waitpid(server_pid, &status, 0), server_pid); |
| 244 |
RESULT_ENSURE_EQ(status, EXIT_SUCCESS); |
| 245 |
|
| 246 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key)); |
| 247 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 248 |
|
| 249 |
return S2N_RESULT_OK; |
| 250 |
} |
| 251 |
|
| 252 |
int main(int argc, char **argv) |
| 253 |
{ |
| 254 |
BEGIN_TEST(); |
| 255 |
|
| 256 |
|
| 257 |
if (!s2n_is_tls13_fully_supported()) { |
| 258 |
END_TEST(); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
EXPECT_EQUAL(fflush(stdout), 0); |
| 263 |
|
| 264 |
EXPECT_OK(s2n_run_self_talk_test(s2n_test_encryption_limits)); |
| 265 |
EXPECT_OK(s2n_run_self_talk_test(s2n_test_peer_requests)); |
| 266 |
|
| 267 |
END_TEST(); |
| 268 |
} |