| 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 "tls/s2n_tls13.h" |
| 22 |
#include "utils/s2n_safety.h" |
| 23 |
|
| 24 |
#define S2N_FRAG_LEN_SMALLER_THAN_CH 150 |
| 25 |
|
| 26 |
#define TIMES_TO_BLOCK 3 |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
struct s2n_io_wrapper { |
| 36 |
uint8_t times_recv_blocked; |
| 37 |
uint8_t times_send_blocked; |
| 38 |
s2n_recv_fn *inner_recv; |
| 39 |
s2n_send_fn *inner_send; |
| 40 |
void *inner_recv_ctx; |
| 41 |
void *inner_send_ctx; |
| 42 |
}; |
| 43 |
|
| 44 |
struct s2n_io_wrapper_pair { |
| 45 |
struct s2n_io_wrapper client; |
| 46 |
struct s2n_io_wrapper server; |
| 47 |
}; |
| 48 |
|
| 49 |
static int s2n_blocking_read(void *io_context, uint8_t *buf, uint32_t len) |
| 50 |
{ |
| 51 |
struct s2n_io_wrapper *context = (struct s2n_io_wrapper *) io_context; |
| 52 |
if (context->times_recv_blocked < TIMES_TO_BLOCK) { |
| 53 |
context->times_recv_blocked++; |
| 54 |
errno = EAGAIN; |
| 55 |
return -1; |
| 56 |
} |
| 57 |
context->times_recv_blocked = 0; |
| 58 |
return context->inner_recv(context->inner_recv_ctx, buf, len); |
| 59 |
} |
| 60 |
|
| 61 |
static int s2n_blocking_write(void *io_context, const uint8_t *buf, uint32_t len) |
| 62 |
{ |
| 63 |
struct s2n_io_wrapper *context = (struct s2n_io_wrapper *) io_context; |
| 64 |
if (context->times_send_blocked < TIMES_TO_BLOCK) { |
| 65 |
context->times_send_blocked++; |
| 66 |
errno = EAGAIN; |
| 67 |
return -1; |
| 68 |
} |
| 69 |
context->times_send_blocked = 0; |
| 70 |
return context->inner_send(context->inner_send_ctx, buf, len); |
| 71 |
} |
| 72 |
|
| 73 |
static int s2n_client_hello_test_fn(struct s2n_connection *conn, void *ctx) |
| 74 |
{ |
| 75 |
return S2N_SUCCESS; |
| 76 |
} |
| 77 |
|
| 78 |
struct s2n_async_pkey_op *pkey_op = NULL; |
| 79 |
static int async_pkey_test_fn(struct s2n_connection *conn, struct s2n_async_pkey_op *op) |
| 80 |
{ |
| 81 |
pkey_op = op; |
| 82 |
|
| 83 |
|
| 84 |
struct s2n_cert_chain_and_key *chain_and_key = s2n_connection_get_selected_cert(conn); |
| 85 |
POSIX_ENSURE_REF(chain_and_key); |
| 86 |
|
| 87 |
s2n_cert_private_key *pkey = s2n_cert_chain_and_key_get_private_key(chain_and_key); |
| 88 |
POSIX_ENSURE_REF(pkey); |
| 89 |
|
| 90 |
|
| 91 |
EXPECT_SUCCESS(s2n_async_pkey_op_perform(pkey_op, pkey)); |
| 92 |
|
| 93 |
return S2N_SUCCESS; |
| 94 |
} |
| 95 |
|
| 96 |
static struct s2n_config *s2n_test_config_new(struct s2n_cert_chain_and_key *chain_and_key) |
| 97 |
{ |
| 98 |
struct s2n_config *config = s2n_config_new(); |
| 99 |
PTR_GUARD_POSIX(s2n_config_set_cipher_preferences(config, "default_tls13")); |
| 100 |
PTR_GUARD_POSIX(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key)); |
| 101 |
PTR_GUARD_POSIX(s2n_config_disable_x509_verification(config)); |
| 102 |
return config; |
| 103 |
} |
| 104 |
|
| 105 |
static S2N_RESULT s2n_connections_set_blocking_io_pair(struct s2n_io_wrapper_pair *io_context, |
| 106 |
struct s2n_connection *client_conn, struct s2n_connection *server_conn, struct s2n_test_io_stuffer_pair *io_pair) |
| 107 |
{ |
| 108 |
RESULT_GUARD(s2n_io_stuffer_pair_init(io_pair)); |
| 109 |
RESULT_GUARD(s2n_connections_set_io_stuffer_pair(client_conn, server_conn, io_pair)); |
| 110 |
|
| 111 |
io_context->client = (struct s2n_io_wrapper){ |
| 112 |
.inner_recv = client_conn->recv, |
| 113 |
.inner_send = client_conn->send, |
| 114 |
.inner_recv_ctx = client_conn->recv_io_context, |
| 115 |
.inner_send_ctx = client_conn->send_io_context, |
| 116 |
}; |
| 117 |
|
| 118 |
RESULT_GUARD_POSIX(s2n_connection_set_recv_cb(client_conn, s2n_blocking_read)); |
| 119 |
RESULT_GUARD_POSIX(s2n_connection_set_recv_ctx(client_conn, &io_context->client)); |
| 120 |
RESULT_GUARD_POSIX(s2n_connection_set_send_cb(client_conn, s2n_blocking_write)); |
| 121 |
RESULT_GUARD_POSIX(s2n_connection_set_send_ctx(client_conn, &io_context->client)); |
| 122 |
|
| 123 |
io_context->server = (struct s2n_io_wrapper){ |
| 124 |
.inner_recv = server_conn->recv, |
| 125 |
.inner_send = server_conn->send, |
| 126 |
.inner_recv_ctx = server_conn->recv_io_context, |
| 127 |
.inner_send_ctx = server_conn->send_io_context, |
| 128 |
}; |
| 129 |
|
| 130 |
RESULT_GUARD_POSIX(s2n_connection_set_recv_cb(server_conn, s2n_blocking_read)); |
| 131 |
RESULT_GUARD_POSIX(s2n_connection_set_recv_ctx(server_conn, &io_context->server)); |
| 132 |
RESULT_GUARD_POSIX(s2n_connection_set_send_cb(server_conn, s2n_blocking_write)); |
| 133 |
RESULT_GUARD_POSIX(s2n_connection_set_send_ctx(server_conn, &io_context->server)); |
| 134 |
|
| 135 |
return S2N_RESULT_OK; |
| 136 |
} |
| 137 |
|
| 138 |
int main(int argc, char **argv) |
| 139 |
{ |
| 140 |
BEGIN_TEST(); |
| 141 |
|
| 142 |
const uint32_t fragment_sizes[] = { |
| 143 |
1, |
| 144 |
2, |
| 145 |
TLS_HANDSHAKE_HEADER_LENGTH, |
| 146 |
TLS_HANDSHAKE_HEADER_LENGTH + 1, |
| 147 |
S2N_FRAG_LEN_SMALLER_THAN_CH, |
| 148 |
S2N_DEFAULT_FRAGMENT_LENGTH, |
| 149 |
}; |
| 150 |
|
| 151 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free); |
| 152 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, |
| 153 |
S2N_ECDSA_P384_PKCS1_CERT_CHAIN, S2N_ECDSA_P384_PKCS1_KEY)); |
| 154 |
|
| 155 |
|
| 156 |
for (size_t i = 0; i < s2n_array_len(fragment_sizes); i++) { |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
uint32_t server_fragment_size = fragment_sizes[i]; |
| 161 |
uint32_t client_fragment_size = fragment_sizes[i] + 1; |
| 162 |
|
| 163 |
|
| 164 |
if (s2n_is_tls13_fully_supported()) { |
| 165 |
DEFER_CLEANUP(struct s2n_config *config = s2n_test_config_new(chain_and_key), |
| 166 |
s2n_config_ptr_free); |
| 167 |
|
| 168 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 169 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 170 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING)); |
| 171 |
server_conn->max_outgoing_fragment_length = server_fragment_size; |
| 172 |
|
| 173 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free); |
| 174 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 175 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING)); |
| 176 |
client_conn->max_outgoing_fragment_length = client_fragment_size; |
| 177 |
|
| 178 |
struct s2n_io_wrapper_pair io_wrapper = { 0 }; |
| 179 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 180 |
EXPECT_OK(s2n_connections_set_blocking_io_pair(&io_wrapper, client_conn, server_conn, &io_pair)); |
| 181 |
|
| 182 |
while (s2n_negotiate_test_server_and_client(server_conn, client_conn) < S2N_SUCCESS) { |
| 183 |
POSIX_ENSURE(s2n_errno, S2N_ERR_IO_BLOCKED); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
EXPECT_TRUE(IS_NEGOTIATED(server_conn)); |
| 188 |
EXPECT_TRUE(IS_NEGOTIATED(client_conn)); |
| 189 |
EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), APPLICATION_DATA); |
| 190 |
EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), APPLICATION_DATA); |
| 191 |
|
| 192 |
|
| 193 |
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13); |
| 194 |
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13); |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
{ |
| 199 |
DEFER_CLEANUP(struct s2n_config *config = s2n_test_config_new(chain_and_key), |
| 200 |
s2n_config_ptr_free); |
| 201 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all_tls12")); |
| 202 |
|
| 203 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 204 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 205 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING)); |
| 206 |
server_conn->max_outgoing_fragment_length = server_fragment_size; |
| 207 |
|
| 208 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free); |
| 209 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 210 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING)); |
| 211 |
client_conn->max_outgoing_fragment_length = client_fragment_size; |
| 212 |
|
| 213 |
struct s2n_io_wrapper_pair io_wrapper = { 0 }; |
| 214 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 215 |
EXPECT_OK(s2n_connections_set_blocking_io_pair(&io_wrapper, client_conn, server_conn, &io_pair)); |
| 216 |
|
| 217 |
while (s2n_negotiate_test_server_and_client(server_conn, client_conn) < S2N_SUCCESS) { |
| 218 |
POSIX_ENSURE(s2n_errno, S2N_ERR_IO_BLOCKED); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
EXPECT_TRUE(IS_NEGOTIATED(server_conn)); |
| 223 |
EXPECT_TRUE(IS_NEGOTIATED(client_conn)); |
| 224 |
EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), APPLICATION_DATA); |
| 225 |
EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), APPLICATION_DATA); |
| 226 |
|
| 227 |
|
| 228 |
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12); |
| 229 |
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12); |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
{ |
| 239 |
DEFER_CLEANUP(struct s2n_config *config = s2n_test_config_new(chain_and_key), |
| 240 |
s2n_config_ptr_free); |
| 241 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, s2n_client_hello_test_fn, NULL)); |
| 242 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(config, S2N_CLIENT_HELLO_CB_NONBLOCKING)); |
| 243 |
|
| 244 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 245 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 246 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING)); |
| 247 |
server_conn->max_outgoing_fragment_length = server_fragment_size; |
| 248 |
|
| 249 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free); |
| 250 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 251 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING)); |
| 252 |
client_conn->max_outgoing_fragment_length = client_fragment_size; |
| 253 |
|
| 254 |
struct s2n_io_wrapper_pair io_wrapper = { 0 }; |
| 255 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 256 |
EXPECT_OK(s2n_connections_set_blocking_io_pair(&io_wrapper, client_conn, server_conn, &io_pair)); |
| 257 |
|
| 258 |
bool async_block_triggered = false; |
| 259 |
while (s2n_negotiate_test_server_and_client(server_conn, client_conn) < S2N_SUCCESS) { |
| 260 |
if (s2n_errno == S2N_ERR_ASYNC_BLOCKED) { |
| 261 |
EXPECT_SUCCESS(s2n_client_hello_cb_done(server_conn)); |
| 262 |
async_block_triggered = true; |
| 263 |
} else { |
| 264 |
POSIX_ENSURE(s2n_errno, S2N_ERR_IO_BLOCKED); |
| 265 |
} |
| 266 |
} |
| 267 |
EXPECT_TRUE(async_block_triggered); |
| 268 |
|
| 269 |
|
| 270 |
EXPECT_TRUE(IS_NEGOTIATED(server_conn)); |
| 271 |
EXPECT_TRUE(IS_NEGOTIATED(client_conn)); |
| 272 |
EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), APPLICATION_DATA); |
| 273 |
EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), APPLICATION_DATA); |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
{ |
| 285 |
DEFER_CLEANUP(struct s2n_config *config = s2n_test_config_new(chain_and_key), |
| 286 |
s2n_config_ptr_free); |
| 287 |
EXPECT_SUCCESS(s2n_config_set_async_pkey_callback(config, async_pkey_test_fn)); |
| 288 |
|
| 289 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 290 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 291 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING)); |
| 292 |
server_conn->max_outgoing_fragment_length = server_fragment_size; |
| 293 |
|
| 294 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free); |
| 295 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 296 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING)); |
| 297 |
client_conn->max_outgoing_fragment_length = client_fragment_size; |
| 298 |
|
| 299 |
struct s2n_io_wrapper_pair io_wrapper = { 0 }; |
| 300 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 301 |
EXPECT_OK(s2n_connections_set_blocking_io_pair(&io_wrapper, client_conn, server_conn, &io_pair)); |
| 302 |
|
| 303 |
bool async_block_triggered = false; |
| 304 |
while (s2n_negotiate_test_server_and_client(server_conn, client_conn) < S2N_SUCCESS) { |
| 305 |
if (s2n_errno == S2N_ERR_ASYNC_BLOCKED) { |
| 306 |
EXPECT_SUCCESS(s2n_async_pkey_op_apply(pkey_op, server_conn)); |
| 307 |
EXPECT_SUCCESS(s2n_async_pkey_op_free(pkey_op)); |
| 308 |
async_block_triggered = true; |
| 309 |
} else { |
| 310 |
POSIX_ENSURE(s2n_errno, S2N_ERR_IO_BLOCKED); |
| 311 |
} |
| 312 |
} |
| 313 |
EXPECT_TRUE(async_block_triggered); |
| 314 |
|
| 315 |
|
| 316 |
EXPECT_TRUE(IS_NEGOTIATED(server_conn)); |
| 317 |
EXPECT_TRUE(IS_NEGOTIATED(client_conn)); |
| 318 |
EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), APPLICATION_DATA); |
| 319 |
EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), APPLICATION_DATA); |
| 320 |
}; |
| 321 |
|
| 322 |
|
| 323 |
if (s2n_is_tls13_fully_supported()) { |
| 324 |
uint8_t early_data_bytes[] = "hello world"; |
| 325 |
struct s2n_blob early_data = { 0 }; |
| 326 |
EXPECT_SUCCESS(s2n_blob_init(&early_data, early_data_bytes, sizeof(early_data_bytes))); |
| 327 |
|
| 328 |
DEFER_CLEANUP(struct s2n_config *config = s2n_test_config_new(chain_and_key), |
| 329 |
s2n_config_ptr_free); |
| 330 |
|
| 331 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 332 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 333 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING)); |
| 334 |
EXPECT_OK(s2n_append_test_psk_with_early_data(server_conn, early_data.size, &s2n_tls13_aes_256_gcm_sha384)); |
| 335 |
server_conn->max_outgoing_fragment_length = server_fragment_size; |
| 336 |
|
| 337 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free); |
| 338 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 339 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING)); |
| 340 |
EXPECT_OK(s2n_append_test_psk_with_early_data(client_conn, early_data.size, &s2n_tls13_aes_256_gcm_sha384)); |
| 341 |
client_conn->max_outgoing_fragment_length = client_fragment_size; |
| 342 |
|
| 343 |
struct s2n_io_wrapper_pair io_wrapper = { 0 }; |
| 344 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 345 |
EXPECT_OK(s2n_connections_set_blocking_io_pair(&io_wrapper, client_conn, server_conn, &io_pair)); |
| 346 |
|
| 347 |
uint8_t recv_buffer[sizeof(early_data_bytes)] = { 0 }; |
| 348 |
struct s2n_blob early_data_received = { 0 }; |
| 349 |
EXPECT_SUCCESS(s2n_blob_init(&early_data_received, recv_buffer, sizeof(recv_buffer))); |
| 350 |
|
| 351 |
EXPECT_ERROR_WITH_ERRNO(s2n_negotiate_test_server_and_client_with_early_data(server_conn, client_conn, |
| 352 |
&early_data, &early_data_received), |
| 353 |
S2N_ERR_IO_BLOCKED); |
| 354 |
|
| 355 |
|
| 356 |
EXPECT_TRUE(WITH_EARLY_DATA(server_conn)); |
| 357 |
EXPECT_TRUE(WITH_EARLY_DATA(client_conn)); |
| 358 |
S2N_BLOB_EXPECT_EQUAL(early_data, early_data_received); |
| 359 |
|
| 360 |
while (s2n_negotiate_test_server_and_client(server_conn, client_conn) < S2N_SUCCESS) { |
| 361 |
POSIX_ENSURE(s2n_errno, S2N_ERR_IO_BLOCKED); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
EXPECT_TRUE(IS_NEGOTIATED(server_conn)); |
| 366 |
EXPECT_TRUE(IS_NEGOTIATED(client_conn)); |
| 367 |
EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), APPLICATION_DATA); |
| 368 |
EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), APPLICATION_DATA); |
| 369 |
}; |
| 370 |
} |
| 371 |
|
| 372 |
END_TEST(); |
| 373 |
} |