| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "s2n_test.h" |
| 17 |
#include "testlib/s2n_testlib.h" |
| 18 |
#include "tls/s2n_alerts.h" |
| 19 |
#include "tls/s2n_tls.h" |
| 20 |
|
| 21 |
#define ERROR_ALERTS_COUNT (UINT16_MAX) |
| 22 |
#define END_OF_DATA 0 |
| 23 |
#define MODE_COUNT 2 |
| 24 |
|
| 25 |
int s2n_test_ch_cb(struct s2n_connection *conn, void *context) |
| 26 |
{ |
| 27 |
return S2N_FAILURE; |
| 28 |
} |
| 29 |
|
| 30 |
typedef enum { |
| 31 |
S2N_TEST_DURING_HANDSHAKE, |
| 32 |
S2N_TEST_AFTER_HANDSHAKE, |
| 33 |
S2N_TEST_TYPE_COUNT |
| 34 |
} s2n_test_type; |
| 35 |
|
| 36 |
int main(int argc, char **argv) |
| 37 |
{ |
| 38 |
BEGIN_TEST(); |
| 39 |
|
| 40 |
if (!s2n_is_tls13_fully_supported()) { |
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
uint8_t alert_header[] = { |
| 45 |
|
| 46 |
TLS_ALERT, |
| 47 |
|
| 48 |
0x03, 0x03, |
| 49 |
|
| 50 |
0x00, 0x02 |
| 51 |
}; |
| 52 |
uint8_t close_notify[] = { 1, S2N_TLS_ALERT_CLOSE_NOTIFY }; |
| 53 |
uint8_t data[] = "hello"; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
uint8_t test_alert_levels[] = { 0, 1, 2, 3, 10, UINT8_MAX }; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
uint8_t error_alerts[ERROR_ALERTS_COUNT][2] = { 0 }; |
| 70 |
size_t error_alerts_count = 0; |
| 71 |
for (size_t level_i = 0; level_i < s2n_array_len(test_alert_levels); level_i++) { |
| 72 |
for (size_t alert_code = 0; alert_code <= UINT8_MAX; alert_code++) { |
| 73 |
|
| 74 |
if (alert_code == S2N_TLS_ALERT_CLOSE_NOTIFY) { |
| 75 |
continue; |
| 76 |
} |
| 77 |
if (alert_code == S2N_TLS_ALERT_USER_CANCELED) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
if (alert_code > 120 && alert_code < 250) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 86 |
EXPECT_TRUE(error_alerts_count < ERROR_ALERTS_COUNT); |
| 87 |
error_alerts[error_alerts_count][0] = test_alert_levels[level_i]; |
| 88 |
error_alerts[error_alerts_count][1] = alert_code; |
| 89 |
error_alerts_count++; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, |
| 94 |
s2n_cert_chain_and_key_ptr_free); |
| 95 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, |
| 96 |
S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY)); |
| 97 |
|
| 98 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *ecdsa_chain_and_key = NULL, |
| 99 |
s2n_cert_chain_and_key_ptr_free); |
| 100 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_chain_and_key, |
| 101 |
S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY)); |
| 102 |
|
| 103 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), |
| 104 |
s2n_config_ptr_free); |
| 105 |
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config)); |
| 106 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13")); |
| 107 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key)); |
| 108 |
|
| 109 |
DEFER_CLEANUP(struct s2n_config *ecdsa_config = s2n_config_new(), |
| 110 |
s2n_config_ptr_free); |
| 111 |
EXPECT_OK(s2n_config_set_tls12_security_policy(ecdsa_config)); |
| 112 |
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(ecdsa_config)); |
| 113 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(ecdsa_config, ecdsa_chain_and_key)); |
| 114 |
|
| 115 |
|
| 116 |
for (size_t i = 0; i < error_alerts_count; i++) { |
| 117 |
for (s2n_test_type type = 0; type < S2N_TEST_TYPE_COUNT; type++) { |
| 118 |
for (uint8_t mode = 0; mode < MODE_COUNT; mode++) { |
| 119 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 120 |
s2n_connection_ptr_free); |
| 121 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING)); |
| 122 |
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 123 |
|
| 124 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 125 |
s2n_connection_ptr_free); |
| 126 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING)); |
| 127 |
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 128 |
|
| 129 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 130 |
EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair)); |
| 131 |
EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair)); |
| 132 |
|
| 133 |
if (type == S2N_TEST_DURING_HANDSHAKE) { |
| 134 |
|
| 135 |
EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_CERT)); |
| 136 |
} else { |
| 137 |
|
| 138 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 139 |
} |
| 140 |
EXPECT_EQUAL(server->actual_protocol_version, S2N_TLS13); |
| 141 |
|
| 142 |
struct s2n_connection *receiver = server; |
| 143 |
struct s2n_connection *sender = client; |
| 144 |
if (mode == S2N_CLIENT) { |
| 145 |
receiver = client; |
| 146 |
sender = server; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
struct s2n_blob alert = { 0 }; |
| 151 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 152 |
EXPECT_SUCCESS(s2n_blob_init(&alert, error_alerts[i], sizeof(error_alerts[0]))); |
| 153 |
EXPECT_OK(s2n_record_write(sender, TLS_ALERT, &alert)); |
| 154 |
EXPECT_SUCCESS(s2n_flush(sender, &blocked)); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
if (type == S2N_TEST_DURING_HANDSHAKE) { |
| 164 |
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(receiver, &blocked), S2N_ERR_ALERT); |
| 165 |
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(receiver, &blocked), S2N_ERR_CLOSED); |
| 166 |
} else { |
| 167 |
EXPECT_FAILURE_WITH_ERRNO(s2n_recv(receiver, data, sizeof(data), &blocked), S2N_ERR_ALERT); |
| 168 |
EXPECT_FAILURE_WITH_ERRNO(s2n_recv(receiver, data, sizeof(data), &blocked), S2N_ERR_CLOSED); |
| 169 |
EXPECT_FAILURE_WITH_ERRNO(s2n_send(receiver, data, sizeof(data), &blocked), S2N_ERR_CLOSED); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
EXPECT_TRUE(s2n_connection_check_io_status(receiver, S2N_IO_CLOSED)); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
EXPECT_EQUAL(error_alerts[i][1], s2n_connection_get_alert(receiver)); |
| 188 |
} |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
{ |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
int test_errors[] = { |
| 198 |
|
| 199 |
S2N_ERR_CANCELLED, |
| 200 |
S2N_ERR_CIPHER_NOT_SUPPORTED, |
| 201 |
|
| 202 |
S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED, |
| 203 |
S2N_ERR_CERT_INVALID_HOSTNAME, |
| 204 |
|
| 205 |
S2N_ERR_DECRYPT, |
| 206 |
}; |
| 207 |
|
| 208 |
DEFER_CLEANUP(struct s2n_config *bad_cb_config = s2n_config_new(), |
| 209 |
s2n_config_ptr_free); |
| 210 |
EXPECT_OK(s2n_config_set_tls12_security_policy(bad_cb_config)); |
| 211 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb(bad_cb_config, s2n_test_ch_cb, NULL)); |
| 212 |
|
| 213 |
DEFER_CLEANUP(struct s2n_config *untrusted_config = s2n_config_new(), |
| 214 |
s2n_config_ptr_free); |
| 215 |
EXPECT_OK(s2n_config_set_tls12_security_policy(untrusted_config)); |
| 216 |
|
| 217 |
for (size_t i = 0; i < s2n_array_len(test_errors); i++) { |
| 218 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 219 |
s2n_connection_ptr_free); |
| 220 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING)); |
| 221 |
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 222 |
|
| 223 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 224 |
s2n_connection_ptr_free); |
| 225 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING)); |
| 226 |
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 227 |
|
| 228 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 229 |
EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair)); |
| 230 |
EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair)); |
| 231 |
|
| 232 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 233 |
uint8_t expected_alert = S2N_TLS_ALERT_CLOSE_NOTIFY; |
| 234 |
struct s2n_connection *failed_conn = server; |
| 235 |
struct s2n_connection *closed_conn = client; |
| 236 |
|
| 237 |
switch (test_errors[i]) { |
| 238 |
case S2N_ERR_CANCELLED: |
| 239 |
|
| 240 |
EXPECT_SUCCESS(s2n_connection_set_config(server, bad_cb_config)); |
| 241 |
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server, client), |
| 242 |
S2N_ERR_CANCELLED); |
| 243 |
|
| 244 |
expected_alert = S2N_TLS_ALERT_HANDSHAKE_FAILURE; |
| 245 |
break; |
| 246 |
case S2N_ERR_CIPHER_NOT_SUPPORTED: |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
EXPECT_SUCCESS(s2n_connection_set_config(server, ecdsa_config)); |
| 252 |
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "20170210")); |
| 253 |
|
| 254 |
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server, client), |
| 255 |
S2N_ERR_CIPHER_NOT_SUPPORTED); |
| 256 |
break; |
| 257 |
case S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED: |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "20170210")); |
| 263 |
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(client, &blocked), S2N_ERR_IO_BLOCKED); |
| 264 |
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(server, &blocked), S2N_ERR_IO_BLOCKED); |
| 265 |
client->client_protocol_version = S2N_TLS13; |
| 266 |
|
| 267 |
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server, client), |
| 268 |
S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED); |
| 269 |
|
| 270 |
failed_conn = client; |
| 271 |
closed_conn = server; |
| 272 |
break; |
| 273 |
case S2N_ERR_CERT_INVALID_HOSTNAME: |
| 274 |
EXPECT_SUCCESS(s2n_connection_set_config(client, untrusted_config)); |
| 275 |
|
| 276 |
EXPECT_FAILURE_WITH_ALERT(s2n_negotiate_test_server_and_client(server, client), |
| 277 |
S2N_ERR_CERT_INVALID_HOSTNAME, S2N_TLS_ALERT_CERTIFICATE_UNKNOWN); |
| 278 |
|
| 279 |
failed_conn = client; |
| 280 |
closed_conn = server; |
| 281 |
break; |
| 282 |
case S2N_ERR_DECRYPT: |
| 283 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
client->secure->client_sequence_number[0] = 0xFF; |
| 289 |
EXPECT_EQUAL(s2n_send(client, data, sizeof(data), &blocked), sizeof(data)); |
| 290 |
EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server, data, sizeof(data), &blocked), |
| 291 |
S2N_ERR_DECRYPT); |
| 292 |
break; |
| 293 |
default: |
| 294 |
FAIL_MSG("Fatal error not implemented for test"); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
failed_conn->delay = 0; |
| 299 |
|
| 300 |
|
| 301 |
EXPECT_SUCCESS(s2n_shutdown_send(failed_conn, &blocked)); |
| 302 |
EXPECT_TRUE(failed_conn->alert_sent); |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
EXPECT_TRUE(s2n_connection_check_io_status(failed_conn, S2N_IO_CLOSED)); |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if (expected_alert == S2N_TLS_ALERT_CLOSE_NOTIFY) { |
| 320 |
EXPECT_EQUAL(s2n_recv(closed_conn, data, sizeof(data), &blocked), END_OF_DATA); |
| 321 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 322 |
} else { |
| 323 |
EXPECT_FAILURE_WITH_ERRNO(s2n_recv(closed_conn, data, sizeof(data), &blocked), S2N_ERR_ALERT); |
| 324 |
} |
| 325 |
EXPECT_EQUAL(expected_alert, s2n_connection_get_alert(closed_conn)); |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
EXPECT_FAILURE_WITH_ERRNO(s2n_send(failed_conn, data, sizeof(data), &blocked), |
| 333 |
S2N_ERR_CLOSED); |
| 334 |
EXPECT_FAILURE_WITH_ERRNO(s2n_recv(failed_conn, data, sizeof(data), &blocked), S2N_ERR_CLOSED); |
| 335 |
} |
| 336 |
}; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
for (s2n_test_type type = 0; type < S2N_TEST_TYPE_COUNT; type++) { |
| 347 |
for (uint8_t mode = 0; mode < MODE_COUNT; mode++) { |
| 348 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 349 |
s2n_connection_ptr_free); |
| 350 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING)); |
| 351 |
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 352 |
|
| 353 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 354 |
s2n_connection_ptr_free); |
| 355 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING)); |
| 356 |
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 357 |
|
| 358 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 359 |
EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair)); |
| 360 |
EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair)); |
| 361 |
|
| 362 |
if (type == S2N_TEST_DURING_HANDSHAKE) { |
| 363 |
|
| 364 |
EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server, client, SERVER_CERT)); |
| 365 |
} else { |
| 366 |
|
| 367 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 368 |
} |
| 369 |
|
| 370 |
struct s2n_connection *receiver = server; |
| 371 |
struct s2n_stuffer *input = &io_pair.server_in; |
| 372 |
if (mode == S2N_CLIENT) { |
| 373 |
receiver = client; |
| 374 |
input = &io_pair.client_in; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(input, alert_header, sizeof(alert_header))); |
| 379 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(input, close_notify, sizeof(close_notify))); |
| 380 |
|
| 381 |
|
| 382 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 383 |
if (type == S2N_TEST_DURING_HANDSHAKE) { |
| 384 |
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(receiver, &blocked), S2N_ERR_CLOSED); |
| 385 |
} else { |
| 386 |
EXPECT_EQUAL(s2n_recv(receiver, data, sizeof(data), &blocked), END_OF_DATA); |
| 387 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 388 |
} |
| 389 |
EXPECT_FALSE(s2n_connection_check_io_status(receiver, S2N_IO_READABLE)); |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(input, data, sizeof(data))); |
| 397 |
if (type == S2N_TEST_DURING_HANDSHAKE) { |
| 398 |
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(receiver, &blocked), S2N_ERR_CLOSED); |
| 399 |
} else { |
| 400 |
EXPECT_EQUAL(s2n_recv(receiver, data, sizeof(data), &blocked), END_OF_DATA); |
| 401 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 402 |
} |
| 403 |
}; |
| 404 |
}; |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
for (uint8_t mode = 0; mode < MODE_COUNT; mode++) { |
| 415 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 416 |
s2n_connection_ptr_free); |
| 417 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING)); |
| 418 |
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 419 |
|
| 420 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 421 |
s2n_connection_ptr_free); |
| 422 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING)); |
| 423 |
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 424 |
|
| 425 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 426 |
EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair)); |
| 427 |
EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair)); |
| 428 |
|
| 429 |
|
| 430 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 431 |
EXPECT_EQUAL(server->actual_protocol_version, S2N_TLS13); |
| 432 |
|
| 433 |
struct s2n_connection *sender = server; |
| 434 |
struct s2n_connection *receiver = client; |
| 435 |
if (mode == S2N_CLIENT) { |
| 436 |
sender = client; |
| 437 |
receiver = server; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 442 |
EXPECT_SUCCESS(s2n_shutdown_send(sender, &blocked)); |
| 443 |
EXPECT_FALSE(s2n_connection_check_io_status(sender, S2N_IO_WRITABLE)); |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
EXPECT_EQUAL(s2n_recv(receiver, data, sizeof(data), &blocked), END_OF_DATA); |
| 454 |
EXPECT_FALSE(s2n_connection_check_io_status(receiver, S2N_IO_READABLE)); |
| 455 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
EXPECT_EQUAL(s2n_send(receiver, data, sizeof(data), &blocked), sizeof(data)); |
| 462 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 463 |
EXPECT_EQUAL(s2n_recv(sender, data, sizeof(data), &blocked), sizeof(data)); |
| 464 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 465 |
|
| 466 |
|
| 467 |
EXPECT_SUCCESS(s2n_shutdown(receiver, &blocked)); |
| 468 |
EXPECT_SUCCESS(s2n_shutdown(sender, &blocked)); |
| 469 |
}; |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
for (uint8_t mode = 0; mode < MODE_COUNT; mode++) { |
| 481 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 482 |
s2n_connection_ptr_free); |
| 483 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING)); |
| 484 |
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 485 |
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server, "20240501")); |
| 486 |
|
| 487 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 488 |
s2n_connection_ptr_free); |
| 489 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING)); |
| 490 |
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 491 |
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client, "20240501")); |
| 492 |
|
| 493 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free); |
| 494 |
EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair)); |
| 495 |
EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair)); |
| 496 |
|
| 497 |
|
| 498 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 499 |
EXPECT_EQUAL(server->actual_protocol_version, S2N_TLS12); |
| 500 |
|
| 501 |
struct s2n_connection *sender = server; |
| 502 |
struct s2n_connection *receiver = client; |
| 503 |
if (mode == S2N_CLIENT) { |
| 504 |
sender = client; |
| 505 |
receiver = server; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 510 |
EXPECT_SUCCESS(s2n_shutdown_send(sender, &blocked)); |
| 511 |
EXPECT_TRUE(s2n_connection_check_io_status(sender, S2N_IO_CLOSED)); |
| 512 |
|
| 513 |
|
| 514 |
EXPECT_EQUAL(s2n_recv(receiver, data, sizeof(data), &blocked), END_OF_DATA); |
| 515 |
EXPECT_TRUE(s2n_connection_check_io_status(receiver, S2N_IO_CLOSED)); |
| 516 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
EXPECT_FAILURE_WITH_ERRNO(s2n_send(receiver, data, sizeof(data), &blocked), |
| 522 |
S2N_ERR_CLOSED); |
| 523 |
|
| 524 |
|
| 525 |
EXPECT_SUCCESS(s2n_shutdown(receiver, &blocked)); |
| 526 |
EXPECT_SUCCESS(s2n_shutdown(sender, &blocked)); |
| 527 |
}; |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
for (uint8_t mode = 0; mode < MODE_COUNT; mode++) { |
| 545 |
|
| 546 |
{ |
| 547 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 548 |
s2n_connection_ptr_free); |
| 549 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING)); |
| 550 |
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 551 |
|
| 552 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 553 |
s2n_connection_ptr_free); |
| 554 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING)); |
| 555 |
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 556 |
|
| 557 |
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close); |
| 558 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 559 |
EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair)); |
| 560 |
|
| 561 |
|
| 562 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 563 |
|
| 564 |
|
| 565 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, mode)); |
| 566 |
|
| 567 |
struct s2n_connection *receiver = client; |
| 568 |
if (mode == S2N_CLIENT) { |
| 569 |
receiver = server; |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 574 |
for (size_t i = 0; i < 5; i++) { |
| 575 |
EXPECT_FAILURE_WITH_ERRNO(s2n_recv(receiver, data, sizeof(data), &blocked), |
| 576 |
S2N_ERR_CLOSED); |
| 577 |
EXPECT_FALSE(s2n_connection_check_io_status(receiver, S2N_IO_READABLE)); |
| 578 |
} |
| 579 |
}; |
| 580 |
|
| 581 |
|
| 582 |
{ |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
struct s2n_config partial_write_config_copy = *config; |
| 588 |
partial_write_config_copy.recv_multi_record = true; |
| 589 |
|
| 590 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 591 |
s2n_connection_ptr_free); |
| 592 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING)); |
| 593 |
EXPECT_SUCCESS(s2n_connection_set_config(server, &partial_write_config_copy)); |
| 594 |
|
| 595 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 596 |
s2n_connection_ptr_free); |
| 597 |
EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING)); |
| 598 |
EXPECT_SUCCESS(s2n_connection_set_config(client, &partial_write_config_copy)); |
| 599 |
|
| 600 |
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close); |
| 601 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 602 |
EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair)); |
| 603 |
|
| 604 |
|
| 605 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 606 |
|
| 607 |
struct s2n_connection *sender = server; |
| 608 |
struct s2n_connection *receiver = client; |
| 609 |
if (mode == S2N_CLIENT) { |
| 610 |
sender = client; |
| 611 |
receiver = server; |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
const uint8_t partial_write = sizeof(data) / 2; |
| 616 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 617 |
EXPECT_EQUAL(s2n_send(sender, data, partial_write, &blocked), partial_write); |
| 618 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 619 |
|
| 620 |
|
| 621 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, mode)); |
| 622 |
|
| 623 |
|
| 624 |
EXPECT_EQUAL(s2n_recv(receiver, data, sizeof(data), &blocked), partial_write); |
| 625 |
EXPECT_FALSE(s2n_connection_check_io_status(receiver, S2N_IO_READABLE)); |
| 626 |
|
| 627 |
|
| 628 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 629 |
|
| 630 |
|
| 631 |
for (size_t i = 0; i < 5; i++) { |
| 632 |
EXPECT_FAILURE_WITH_ERRNO(s2n_recv(receiver, data, sizeof(data), &blocked), |
| 633 |
S2N_ERR_CLOSED); |
| 634 |
} |
| 635 |
}; |
| 636 |
}; |
| 637 |
|
| 638 |
|
| 639 |
{ |
| 640 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 641 |
s2n_connection_ptr_free); |
| 642 |
EXPECT_NOT_NULL(conn); |
| 643 |
|
| 644 |
|
| 645 |
EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->alert_in, S2N_TLS_ALERT_LEVEL_FATAL)); |
| 646 |
EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->alert_in, S2N_TLS_ALERT_HANDSHAKE_FAILURE)); |
| 647 |
|
| 648 |
|
| 649 |
for (size_t i = 0; i < 5; i++) { |
| 650 |
EXPECT_EQUAL(s2n_connection_get_alert(conn), S2N_TLS_ALERT_HANDSHAKE_FAILURE); |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
EXPECT_EQUAL(s2n_stuffer_data_available(&conn->alert_in), 2); |
| 655 |
}; |
| 656 |
|
| 657 |
END_TEST(); |
| 658 |
} |