| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/s2n_shutdown.c" |
| 17 |
|
| 18 |
#include "s2n_test.h" |
| 19 |
#ifndef _WIN32 |
| 20 |
#include "testlib/s2n_ktls_test_utils.h" |
| 21 |
#endif |
| 22 |
#include "testlib/s2n_testlib.h" |
| 23 |
#include "tls/s2n_alerts.h" |
| 24 |
|
| 25 |
#define ALERT_LEN (sizeof(uint16_t)) |
| 26 |
|
| 27 |
int main(int argc, char **argv) |
| 28 |
{ |
| 29 |
BEGIN_TEST(); |
| 30 |
|
| 31 |
const uint8_t close_notify_alert[] = { |
| 32 |
S2N_TLS_ALERT_LEVEL_WARNING, |
| 33 |
S2N_TLS_ALERT_CLOSE_NOTIFY |
| 34 |
}; |
| 35 |
|
| 36 |
const uint8_t alert_record_header[] = { |
| 37 |
|
| 38 |
TLS_ALERT, |
| 39 |
|
| 40 |
S2N_TLS12 / 10, |
| 41 |
S2N_TLS12 % 10, |
| 42 |
|
| 43 |
0, |
| 44 |
S2N_ALERT_LENGTH, |
| 45 |
}; |
| 46 |
|
| 47 |
const uint8_t alert_record_size = sizeof(alert_record_header) + S2N_ALERT_LENGTH; |
| 48 |
|
| 49 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key * chain_and_key, |
| 50 |
s2n_cert_chain_and_key_ptr_free); |
| 51 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, |
| 52 |
S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY)); |
| 53 |
|
| 54 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), |
| 55 |
s2n_config_ptr_free); |
| 56 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key)); |
| 57 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13")); |
| 58 |
EXPECT_SUCCESS(s2n_config_disable_x509_verification(config)); |
| 59 |
|
| 60 |
|
| 61 |
{ |
| 62 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 63 |
s2n_connection_ptr_free); |
| 64 |
EXPECT_NOT_NULL(conn); |
| 65 |
EXPECT_OK(s2n_skip_handshake(conn)); |
| 66 |
|
| 67 |
|
| 68 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 69 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 70 |
EXPECT_SUCCESS(s2n_connection_set_send_io_stuffer(&output, conn)); |
| 71 |
|
| 72 |
|
| 73 |
EXPECT_TRUE(s2n_handshake_is_complete(conn)); |
| 74 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 75 |
EXPECT_FALSE(conn->alert_sent); |
| 76 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 77 |
|
| 78 |
|
| 79 |
EXPECT_SUCCESS(s2n_queue_reader_handshake_failure_alert(conn)); |
| 80 |
|
| 81 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 82 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 83 |
|
| 84 |
|
| 85 |
EXPECT_TRUE(s2n_handshake_is_complete(conn)); |
| 86 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 87 |
EXPECT_TRUE(conn->alert_sent); |
| 88 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 89 |
|
| 90 |
|
| 91 |
EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size); |
| 92 |
|
| 93 |
|
| 94 |
uint8_t level = 0, code = 0; |
| 95 |
EXPECT_SUCCESS(s2n_stuffer_skip_read(&output, sizeof(alert_record_header))); |
| 96 |
EXPECT_SUCCESS(s2n_stuffer_read_uint8(&output, &level)); |
| 97 |
EXPECT_EQUAL(level, S2N_TLS_ALERT_LEVEL_FATAL); |
| 98 |
EXPECT_SUCCESS(s2n_stuffer_read_uint8(&output, &code)); |
| 99 |
EXPECT_EQUAL(code, S2N_TLS_ALERT_HANDSHAKE_FAILURE); |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
{ |
| 104 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 105 |
s2n_connection_ptr_free); |
| 106 |
EXPECT_NOT_NULL(conn); |
| 107 |
EXPECT_OK(s2n_skip_handshake(conn)); |
| 108 |
|
| 109 |
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free); |
| 110 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0)); |
| 111 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 112 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 113 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn)); |
| 114 |
|
| 115 |
|
| 116 |
EXPECT_TRUE(s2n_handshake_is_complete(conn)); |
| 117 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 118 |
EXPECT_FALSE(conn->alert_sent); |
| 119 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 120 |
|
| 121 |
|
| 122 |
EXPECT_OK(s2n_queue_reader_no_renegotiation_alert(conn)); |
| 123 |
|
| 124 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 125 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), |
| 126 |
S2N_ERR_IO_BLOCKED); |
| 127 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ); |
| 128 |
|
| 129 |
|
| 130 |
EXPECT_TRUE(s2n_handshake_is_complete(conn)); |
| 131 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 132 |
EXPECT_TRUE(conn->alert_sent); |
| 133 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 134 |
|
| 135 |
|
| 136 |
EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size * 2); |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
{ |
| 141 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 142 |
s2n_connection_ptr_free); |
| 143 |
EXPECT_NOT_NULL(conn); |
| 144 |
EXPECT_OK(s2n_skip_handshake(conn)); |
| 145 |
|
| 146 |
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free); |
| 147 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0)); |
| 148 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 149 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 150 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn)); |
| 151 |
|
| 152 |
|
| 153 |
EXPECT_TRUE(s2n_handshake_is_complete(conn)); |
| 154 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 155 |
EXPECT_FALSE(conn->alert_sent); |
| 156 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 157 |
|
| 158 |
|
| 159 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, alert_record_header, |
| 160 |
sizeof(alert_record_header))); |
| 161 |
EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, S2N_TLS_ALERT_LEVEL_FATAL)); |
| 162 |
EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, S2N_TLS_ALERT_INTERNAL_ERROR)); |
| 163 |
|
| 164 |
|
| 165 |
uint8_t buffer[1] = { 0 }; |
| 166 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 167 |
EXPECT_FAILURE_WITH_ERRNO(s2n_recv(conn, buffer, sizeof(buffer), &blocked), |
| 168 |
S2N_ERR_ALERT); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
EXPECT_EQUAL(s2n_connection_get_alert(conn), S2N_TLS_ALERT_INTERNAL_ERROR); |
| 174 |
|
| 175 |
|
| 176 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 177 |
|
| 178 |
|
| 179 |
EXPECT_TRUE(s2n_handshake_is_complete(conn)); |
| 180 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 181 |
EXPECT_FALSE(conn->alert_sent); |
| 182 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 183 |
|
| 184 |
|
| 185 |
EXPECT_EQUAL(s2n_stuffer_data_available(&output), 0); |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
{ |
| 190 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 191 |
s2n_connection_ptr_free); |
| 192 |
EXPECT_NOT_NULL(conn); |
| 193 |
|
| 194 |
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free); |
| 195 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0)); |
| 196 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 197 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 198 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn)); |
| 199 |
|
| 200 |
|
| 201 |
EXPECT_FALSE(s2n_handshake_is_complete(conn)); |
| 202 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 203 |
EXPECT_FALSE(conn->alert_sent); |
| 204 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 205 |
|
| 206 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 207 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 208 |
|
| 209 |
|
| 210 |
EXPECT_FALSE(s2n_handshake_is_complete(conn)); |
| 211 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 212 |
EXPECT_TRUE(conn->alert_sent); |
| 213 |
|
| 214 |
|
| 215 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 216 |
}; |
| 217 |
|
| 218 |
|
| 219 |
{ |
| 220 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 221 |
s2n_connection_ptr_free); |
| 222 |
EXPECT_NOT_NULL(conn); |
| 223 |
EXPECT_OK(s2n_skip_handshake(conn)); |
| 224 |
|
| 225 |
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free); |
| 226 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0)); |
| 227 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 228 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 229 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn)); |
| 230 |
|
| 231 |
|
| 232 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 233 |
EXPECT_FALSE(conn->alert_sent); |
| 234 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 235 |
|
| 236 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 237 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_IO_BLOCKED); |
| 238 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ); |
| 239 |
|
| 240 |
|
| 241 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 242 |
EXPECT_TRUE(conn->alert_sent); |
| 243 |
|
| 244 |
|
| 245 |
EXPECT_EQUAL(s2n_connection_get_protocol_version(conn), S2N_TLS13); |
| 246 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 247 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
{ |
| 252 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 253 |
s2n_connection_ptr_free); |
| 254 |
EXPECT_NOT_NULL(conn); |
| 255 |
EXPECT_OK(s2n_skip_handshake(conn)); |
| 256 |
|
| 257 |
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free); |
| 258 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0)); |
| 259 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 260 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 261 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn)); |
| 262 |
|
| 263 |
|
| 264 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 265 |
EXPECT_FALSE(conn->alert_sent); |
| 266 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 267 |
|
| 268 |
|
| 269 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, close_notify_alert, sizeof(close_notify_alert))); |
| 270 |
EXPECT_SUCCESS(s2n_process_alert_fragment(conn)); |
| 271 |
|
| 272 |
|
| 273 |
EXPECT_TRUE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 274 |
EXPECT_FALSE(conn->alert_sent); |
| 275 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 276 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 277 |
|
| 278 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 279 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 280 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 281 |
|
| 282 |
|
| 283 |
EXPECT_TRUE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 284 |
EXPECT_TRUE(conn->alert_sent); |
| 285 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 286 |
}; |
| 287 |
|
| 288 |
|
| 289 |
{ |
| 290 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 291 |
s2n_connection_ptr_free); |
| 292 |
EXPECT_NOT_NULL(conn); |
| 293 |
EXPECT_OK(s2n_skip_handshake(conn)); |
| 294 |
|
| 295 |
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free); |
| 296 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0)); |
| 297 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 298 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 299 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn)); |
| 300 |
|
| 301 |
|
| 302 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 303 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_IO_BLOCKED); |
| 304 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ); |
| 305 |
EXPECT_TRUE(conn->alert_sent); |
| 306 |
|
| 307 |
|
| 308 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, alert_record_header, sizeof(alert_record_header))); |
| 309 |
EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, 2)); |
| 310 |
EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, S2N_TLS_ALERT_INTERNAL_ERROR)); |
| 311 |
|
| 312 |
|
| 313 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_ALERT); |
| 314 |
|
| 315 |
|
| 316 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 317 |
EXPECT_TRUE(conn->alert_sent); |
| 318 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 319 |
EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size); |
| 320 |
|
| 321 |
|
| 322 |
for (size_t i = 0; i < 5; i++) { |
| 323 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 324 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 325 |
EXPECT_TRUE(conn->alert_sent); |
| 326 |
} |
| 327 |
}; |
| 328 |
|
| 329 |
|
| 330 |
{ |
| 331 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 332 |
s2n_connection_ptr_free); |
| 333 |
EXPECT_NOT_NULL(conn); |
| 334 |
EXPECT_OK(s2n_skip_handshake(conn)); |
| 335 |
|
| 336 |
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free); |
| 337 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0)); |
| 338 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 339 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 340 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn)); |
| 341 |
|
| 342 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 343 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_IO_BLOCKED); |
| 344 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ); |
| 345 |
EXPECT_TRUE(conn->alert_sent); |
| 346 |
|
| 347 |
|
| 348 |
uint8_t record_bytes[] = { |
| 349 |
|
| 350 |
TLS_HANDSHAKE, |
| 351 |
|
| 352 |
S2N_TLS12 / 10, |
| 353 |
S2N_TLS12 % 10, |
| 354 |
|
| 355 |
0, |
| 356 |
1, |
| 357 |
|
| 358 |
'x' |
| 359 |
}; |
| 360 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, record_bytes, sizeof(record_bytes))); |
| 361 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_IO_BLOCKED); |
| 362 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ); |
| 363 |
|
| 364 |
|
| 365 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, alert_record_header, sizeof(alert_record_header))); |
| 366 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, close_notify_alert, sizeof(close_notify_alert))); |
| 367 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
{ |
| 372 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 373 |
s2n_connection_ptr_free); |
| 374 |
EXPECT_NOT_NULL(conn); |
| 375 |
EXPECT_OK(s2n_skip_handshake(conn)); |
| 376 |
EXPECT_SUCCESS(s2n_connection_set_blinding(conn, S2N_SELF_SERVICE_BLINDING)); |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
conn->actual_protocol_version_established = true; |
| 382 |
conn->actual_protocol_version = S2N_TLS13; |
| 383 |
|
| 384 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 385 |
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free); |
| 386 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0)); |
| 387 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 388 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 389 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn)); |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
uint8_t header_bytes[] = { |
| 395 |
|
| 396 |
TLS_HANDSHAKE, |
| 397 |
|
| 398 |
0, |
| 399 |
0, |
| 400 |
|
| 401 |
0, |
| 402 |
0, |
| 403 |
}; |
| 404 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, header_bytes, sizeof(header_bytes))); |
| 405 |
uint8_t recv_buffer[1] = { 0 }; |
| 406 |
EXPECT_FAILURE_WITH_ERRNO( |
| 407 |
s2n_recv(conn, recv_buffer, sizeof(recv_buffer), &blocked), |
| 408 |
S2N_ERR_BAD_MESSAGE); |
| 409 |
|
| 410 |
|
| 411 |
EXPECT_TRUE(conn->delay > 0); |
| 412 |
conn->write_timer.time = 0; |
| 413 |
|
| 414 |
|
| 415 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 416 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 417 |
}; |
| 418 |
|
| 419 |
|
| 420 |
{ |
| 421 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 422 |
s2n_connection_ptr_free); |
| 423 |
EXPECT_NOT_NULL(client); |
| 424 |
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 425 |
|
| 426 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 427 |
s2n_connection_ptr_free); |
| 428 |
EXPECT_NOT_NULL(server); |
| 429 |
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 430 |
|
| 431 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, |
| 432 |
s2n_io_stuffer_pair_free); |
| 433 |
EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair)); |
| 434 |
EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair)); |
| 435 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 436 |
|
| 437 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 438 |
|
| 439 |
|
| 440 |
uint8_t test_data[] = "hello world"; |
| 441 |
EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data)); |
| 442 |
|
| 443 |
|
| 444 |
io_pair.server_in.write_cursor--; |
| 445 |
EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server, test_data, sizeof(test_data), &blocked), |
| 446 |
S2N_ERR_IO_BLOCKED); |
| 447 |
|
| 448 |
|
| 449 |
io_pair.server_in.write_cursor++; |
| 450 |
EXPECT_SUCCESS(s2n_shutdown_send(client, &blocked)); |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
EXPECT_SUCCESS(s2n_shutdown(server, &blocked)); |
| 456 |
EXPECT_TRUE(s2n_atomic_flag_test(&server->close_notify_received)); |
| 457 |
}; |
| 458 |
|
| 459 |
|
| 460 |
{ |
| 461 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 462 |
s2n_connection_ptr_free); |
| 463 |
EXPECT_NOT_NULL(client); |
| 464 |
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 465 |
|
| 466 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 467 |
s2n_connection_ptr_free); |
| 468 |
EXPECT_NOT_NULL(server); |
| 469 |
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 470 |
|
| 471 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, |
| 472 |
s2n_io_stuffer_pair_free); |
| 473 |
EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair)); |
| 474 |
EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair)); |
| 475 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 476 |
|
| 477 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 478 |
|
| 479 |
|
| 480 |
EXPECT_SUCCESS(s2n_shutdown_send(client, &blocked)); |
| 481 |
|
| 482 |
|
| 483 |
uint8_t read = 0; |
| 484 |
io_pair.server_in.write_cursor--; |
| 485 |
EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server, &read, 1, &blocked), S2N_ERR_IO_BLOCKED); |
| 486 |
EXPECT_FALSE(s2n_atomic_flag_test(&server->close_notify_received)); |
| 487 |
|
| 488 |
|
| 489 |
io_pair.server_in.write_cursor++; |
| 490 |
EXPECT_SUCCESS(s2n_shutdown(server, &blocked)); |
| 491 |
EXPECT_TRUE(s2n_atomic_flag_test(&server->close_notify_received)); |
| 492 |
}; |
| 493 |
|
| 494 |
|
| 495 |
{ |
| 496 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 497 |
s2n_connection_ptr_free); |
| 498 |
EXPECT_NOT_NULL(client); |
| 499 |
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 500 |
|
| 501 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 502 |
s2n_connection_ptr_free); |
| 503 |
EXPECT_NOT_NULL(server); |
| 504 |
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 505 |
EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING)); |
| 506 |
|
| 507 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, |
| 508 |
s2n_io_stuffer_pair_free); |
| 509 |
EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair)); |
| 510 |
EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair)); |
| 511 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 512 |
|
| 513 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 514 |
|
| 515 |
|
| 516 |
uint8_t test_data[] = "hello world"; |
| 517 |
EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data)); |
| 518 |
const uint8_t overwrite_size = sizeof(test_data) / 2; |
| 519 |
EXPECT_SUCCESS(s2n_stuffer_wipe_n(&io_pair.server_in, overwrite_size)); |
| 520 |
EXPECT_SUCCESS(s2n_stuffer_skip_write(&io_pair.server_in, overwrite_size)); |
| 521 |
|
| 522 |
|
| 523 |
io_pair.server_in.write_cursor--; |
| 524 |
EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server, test_data, sizeof(test_data), &blocked), |
| 525 |
S2N_ERR_IO_BLOCKED); |
| 526 |
|
| 527 |
|
| 528 |
io_pair.server_in.write_cursor++; |
| 529 |
EXPECT_SUCCESS(s2n_shutdown_send(client, &blocked)); |
| 530 |
|
| 531 |
|
| 532 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(server, &blocked), S2N_ERR_DECRYPT); |
| 533 |
EXPECT_FALSE(s2n_atomic_flag_test(&server->close_notify_received)); |
| 534 |
}; |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
{ |
| 542 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 543 |
s2n_connection_ptr_free); |
| 544 |
EXPECT_NOT_NULL(client); |
| 545 |
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 546 |
|
| 547 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 548 |
s2n_connection_ptr_free); |
| 549 |
EXPECT_NOT_NULL(server); |
| 550 |
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 551 |
|
| 552 |
DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, |
| 553 |
s2n_io_stuffer_pair_free); |
| 554 |
EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair)); |
| 555 |
EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair)); |
| 556 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 557 |
|
| 558 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 559 |
|
| 560 |
|
| 561 |
uint8_t test_data[] = "hello world"; |
| 562 |
EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data)); |
| 563 |
|
| 564 |
|
| 565 |
EXPECT_EQUAL(s2n_recv(server, test_data, 1, &blocked), 1); |
| 566 |
|
| 567 |
|
| 568 |
EXPECT_SUCCESS(s2n_shutdown_send(client, &blocked)); |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
EXPECT_SUCCESS(s2n_shutdown(server, &blocked)); |
| 574 |
EXPECT_TRUE(s2n_atomic_flag_test(&server->close_notify_received)); |
| 575 |
}; |
| 576 |
|
| 577 |
|
| 578 |
{ |
| 579 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), |
| 580 |
s2n_connection_ptr_free); |
| 581 |
EXPECT_NOT_NULL(server_conn); |
| 582 |
EXPECT_OK(s2n_skip_handshake(server_conn)); |
| 583 |
|
| 584 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), |
| 585 |
s2n_connection_ptr_free); |
| 586 |
EXPECT_NOT_NULL(client_conn); |
| 587 |
EXPECT_OK(s2n_skip_handshake(client_conn)); |
| 588 |
|
| 589 |
struct s2n_test_io_pair io_pair = { 0 }; |
| 590 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 591 |
EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair)); |
| 592 |
|
| 593 |
|
| 594 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 595 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(client_conn, &blocked), S2N_ERR_IO_BLOCKED); |
| 596 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ); |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
EXPECT_SUCCESS(s2n_shutdown(server_conn, &blocked)); |
| 603 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
EXPECT_SUCCESS(s2n_shutdown(client_conn, &blocked)); |
| 609 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT)); |
| 610 |
}; |
| 611 |
|
| 612 |
|
| 613 |
if (s2n_is_tls13_fully_supported()) { |
| 614 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 615 |
s2n_connection_ptr_free); |
| 616 |
EXPECT_NOT_NULL(conn); |
| 617 |
EXPECT_SUCCESS(s2n_connection_enable_quic(conn)); |
| 618 |
|
| 619 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 620 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 621 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 622 |
|
| 623 |
|
| 624 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 625 |
EXPECT_FALSE(conn->alert_sent); |
| 626 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 627 |
}; |
| 628 |
|
| 629 |
|
| 630 |
{ |
| 631 |
|
| 632 |
{ |
| 633 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 634 |
s2n_connection_ptr_free); |
| 635 |
EXPECT_NOT_NULL(conn); |
| 636 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 637 |
|
| 638 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown_send(NULL, &blocked), S2N_ERR_NULL); |
| 639 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown_send(conn, NULL), S2N_ERR_NULL); |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
{ |
| 644 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 645 |
s2n_connection_ptr_free); |
| 646 |
EXPECT_NOT_NULL(conn); |
| 647 |
|
| 648 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 655 |
EXPECT_SUCCESS(s2n_connection_set_send_io_stuffer(&output, conn)); |
| 656 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
EXPECT_OK(s2n_skip_handshake(conn)); |
| 663 |
|
| 664 |
|
| 665 |
EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked)); |
| 666 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 667 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 668 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 669 |
EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size); |
| 670 |
EXPECT_TRUE(conn->alert_sent); |
| 671 |
}; |
| 672 |
|
| 673 |
|
| 674 |
{ |
| 675 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 676 |
s2n_connection_ptr_free); |
| 677 |
EXPECT_NOT_NULL(conn); |
| 678 |
|
| 679 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 685 |
EXPECT_SUCCESS(s2n_connection_set_send_io_stuffer(&output, conn)); |
| 686 |
|
| 687 |
|
| 688 |
for (size_t i = 0; i < 5; i++) { |
| 689 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown_send(conn, &blocked), |
| 690 |
S2N_ERR_IO_BLOCKED); |
| 691 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE); |
| 692 |
EXPECT_EQUAL(s2n_stuffer_data_available(&output), 0); |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 699 |
|
| 700 |
|
| 701 |
EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked)); |
| 702 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 703 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 704 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 705 |
EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size); |
| 706 |
EXPECT_TRUE(conn->alert_sent); |
| 707 |
}; |
| 708 |
|
| 709 |
|
| 710 |
{ |
| 711 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 712 |
s2n_connection_ptr_free); |
| 713 |
EXPECT_NOT_NULL(conn); |
| 714 |
EXPECT_SUCCESS(s2n_connection_wipe(conn)); |
| 715 |
|
| 716 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 717 |
EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked)); |
| 718 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 719 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 720 |
EXPECT_FALSE(conn->alert_sent); |
| 721 |
}; |
| 722 |
|
| 723 |
|
| 724 |
{ |
| 725 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 726 |
s2n_connection_ptr_free); |
| 727 |
EXPECT_NOT_NULL(conn); |
| 728 |
EXPECT_OK(s2n_skip_handshake(conn)); |
| 729 |
|
| 730 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 731 |
|
| 732 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 733 |
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free); |
| 734 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 735 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0)); |
| 736 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn)); |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
for (size_t i = 0; i < 5; i++) { |
| 742 |
EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked)); |
| 743 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 744 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 745 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 746 |
EXPECT_EQUAL(s2n_stuffer_data_available(&output), alert_record_size); |
| 747 |
EXPECT_TRUE(conn->alert_sent); |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
for (size_t i = 0; i < 5; i++) { |
| 752 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(conn, &blocked), S2N_ERR_IO_BLOCKED); |
| 753 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ); |
| 754 |
EXPECT_FALSE(s2n_connection_check_io_status(conn, S2N_IO_WRITABLE)); |
| 755 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_READABLE)); |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
EXPECT_SUCCESS(s2n_stuffer_copy(&output, &input, s2n_stuffer_data_available(&output))); |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
for (size_t i = 0; i < 5; i++) { |
| 765 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 766 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 767 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 768 |
EXPECT_EQUAL(s2n_stuffer_data_available(&input), 0); |
| 769 |
EXPECT_EQUAL(s2n_stuffer_data_available(&output), 0); |
| 770 |
EXPECT_TRUE(conn->alert_sent); |
| 771 |
} |
| 772 |
}; |
| 773 |
|
| 774 |
|
| 775 |
{ |
| 776 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 777 |
s2n_connection_ptr_free); |
| 778 |
EXPECT_NOT_NULL(conn); |
| 779 |
EXPECT_OK(s2n_skip_handshake(conn)); |
| 780 |
|
| 781 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 782 |
|
| 783 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 784 |
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free); |
| 785 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 786 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0)); |
| 787 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn)); |
| 788 |
|
| 789 |
|
| 790 |
EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked)); |
| 791 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 792 |
EXPECT_TRUE(conn->alert_sent); |
| 793 |
|
| 794 |
|
| 795 |
EXPECT_SUCCESS(s2n_queue_reader_handshake_failure_alert(conn)); |
| 796 |
|
| 797 |
|
| 798 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 799 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 800 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 801 |
}; |
| 802 |
|
| 803 |
|
| 804 |
{ |
| 805 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 806 |
s2n_connection_ptr_free); |
| 807 |
EXPECT_NOT_NULL(conn); |
| 808 |
EXPECT_OK(s2n_skip_handshake(conn)); |
| 809 |
|
| 810 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 811 |
|
| 812 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 813 |
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free); |
| 814 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0)); |
| 815 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0)); |
| 816 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, conn)); |
| 817 |
|
| 818 |
|
| 819 |
EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked)); |
| 820 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 821 |
EXPECT_TRUE(conn->alert_sent); |
| 822 |
|
| 823 |
|
| 824 |
uint8_t buffer[1] = { 0 }; |
| 825 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&input, alert_record_header, |
| 826 |
sizeof(alert_record_header))); |
| 827 |
EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, S2N_TLS_ALERT_LEVEL_FATAL)); |
| 828 |
EXPECT_SUCCESS(s2n_stuffer_write_uint8(&input, S2N_TLS_ALERT_INTERNAL_ERROR)); |
| 829 |
EXPECT_FAILURE_WITH_ERRNO(s2n_recv(conn, buffer, sizeof(buffer), &blocked), |
| 830 |
S2N_ERR_ALERT); |
| 831 |
|
| 832 |
|
| 833 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 834 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 835 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 836 |
}; |
| 837 |
}; |
| 838 |
|
| 839 |
#ifndef _WIN32 |
| 840 |
|
| 841 |
{ |
| 842 |
|
| 843 |
{ |
| 844 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 845 |
s2n_connection_ptr_free); |
| 846 |
EXPECT_NOT_NULL(conn); |
| 847 |
s2n_ktls_configure_connection(conn, S2N_KTLS_MODE_SEND); |
| 848 |
|
| 849 |
DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 }, |
| 850 |
s2n_ktls_io_stuffer_free); |
| 851 |
EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out)); |
| 852 |
|
| 853 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 854 |
EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked)); |
| 855 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 856 |
EXPECT_TRUE(conn->alert_sent); |
| 857 |
EXPECT_EQUAL(out.sendmsg_invoked_count, 1); |
| 858 |
EXPECT_OK(s2n_test_validate_ancillary(&out, TLS_ALERT, S2N_ALERT_LENGTH)); |
| 859 |
EXPECT_OK(s2n_test_validate_data(&out, |
| 860 |
close_notify_alert, sizeof(close_notify_alert))); |
| 861 |
|
| 862 |
|
| 863 |
for (size_t i = 0; i < 5; i++) { |
| 864 |
EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked)); |
| 865 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 866 |
EXPECT_TRUE(conn->alert_sent); |
| 867 |
EXPECT_EQUAL(out.sendmsg_invoked_count, 1); |
| 868 |
} |
| 869 |
}; |
| 870 |
|
| 871 |
|
| 872 |
{ |
| 873 |
|
| 874 |
const size_t partial_write = 1; |
| 875 |
const size_t second_write = sizeof(close_notify_alert) - partial_write; |
| 876 |
EXPECT_TRUE(second_write > 0); |
| 877 |
|
| 878 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 879 |
s2n_connection_ptr_free); |
| 880 |
EXPECT_NOT_NULL(conn); |
| 881 |
s2n_ktls_configure_connection(conn, S2N_KTLS_MODE_SEND); |
| 882 |
|
| 883 |
DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer out = { 0 }, |
| 884 |
s2n_ktls_io_stuffer_free); |
| 885 |
EXPECT_OK(s2n_test_init_ktls_io_stuffer_send(conn, &out)); |
| 886 |
EXPECT_SUCCESS(s2n_stuffer_free(&out.data_buffer)); |
| 887 |
EXPECT_SUCCESS(s2n_stuffer_alloc(&out.data_buffer, partial_write)); |
| 888 |
|
| 889 |
|
| 890 |
size_t expected_calls = 2; |
| 891 |
|
| 892 |
|
| 893 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 894 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown_send(conn, &blocked), |
| 895 |
S2N_ERR_IO_BLOCKED); |
| 896 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE); |
| 897 |
EXPECT_TRUE(conn->alert_sent); |
| 898 |
EXPECT_EQUAL(out.sendmsg_invoked_count, expected_calls); |
| 899 |
EXPECT_OK(s2n_test_validate_ancillary(&out, TLS_ALERT, partial_write)); |
| 900 |
EXPECT_OK(s2n_test_validate_data(&out, close_notify_alert, partial_write)); |
| 901 |
|
| 902 |
|
| 903 |
out.data_buffer.growable = true; |
| 904 |
expected_calls++; |
| 905 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&out.ancillary_buffer)); |
| 906 |
|
| 907 |
|
| 908 |
EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked)); |
| 909 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 910 |
EXPECT_TRUE(conn->alert_sent); |
| 911 |
EXPECT_EQUAL(out.sendmsg_invoked_count, expected_calls); |
| 912 |
EXPECT_OK(s2n_test_validate_ancillary(&out, TLS_ALERT, second_write)); |
| 913 |
EXPECT_OK(s2n_test_validate_data(&out, close_notify_alert, |
| 914 |
sizeof(close_notify_alert))); |
| 915 |
|
| 916 |
|
| 917 |
for (size_t i = 0; i < 5; i++) { |
| 918 |
EXPECT_SUCCESS(s2n_shutdown_send(conn, &blocked)); |
| 919 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 920 |
EXPECT_TRUE(conn->alert_sent); |
| 921 |
EXPECT_EQUAL(out.sendmsg_invoked_count, expected_calls); |
| 922 |
} |
| 923 |
}; |
| 924 |
}; |
| 925 |
|
| 926 |
|
| 927 |
{ |
| 928 |
|
| 929 |
{ |
| 930 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 931 |
s2n_connection_ptr_free); |
| 932 |
EXPECT_NOT_NULL(client); |
| 933 |
s2n_ktls_configure_connection(client, S2N_KTLS_MODE_SEND); |
| 934 |
s2n_ktls_configure_connection(client, S2N_KTLS_MODE_RECV); |
| 935 |
EXPECT_OK(s2n_skip_handshake(client)); |
| 936 |
|
| 937 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 938 |
s2n_connection_ptr_free); |
| 939 |
EXPECT_NOT_NULL(server); |
| 940 |
s2n_ktls_configure_connection(server, S2N_KTLS_MODE_SEND); |
| 941 |
s2n_ktls_configure_connection(server, S2N_KTLS_MODE_RECV); |
| 942 |
EXPECT_OK(s2n_skip_handshake(server)); |
| 943 |
|
| 944 |
DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 }, |
| 945 |
s2n_ktls_io_stuffer_pair_free); |
| 946 |
EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair)); |
| 947 |
|
| 948 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 949 |
EXPECT_SUCCESS(s2n_shutdown_send(client, &blocked)); |
| 950 |
EXPECT_TRUE(client->alert_sent); |
| 951 |
|
| 952 |
EXPECT_SUCCESS(s2n_shutdown(server, &blocked)); |
| 953 |
EXPECT_TRUE(server->alert_sent); |
| 954 |
EXPECT_TRUE(s2n_connection_check_io_status(server, S2N_IO_CLOSED)); |
| 955 |
}; |
| 956 |
|
| 957 |
|
| 958 |
{ |
| 959 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 960 |
s2n_connection_ptr_free); |
| 961 |
EXPECT_NOT_NULL(client); |
| 962 |
s2n_ktls_configure_connection(client, S2N_KTLS_MODE_SEND); |
| 963 |
s2n_ktls_configure_connection(client, S2N_KTLS_MODE_RECV); |
| 964 |
EXPECT_OK(s2n_skip_handshake(client)); |
| 965 |
|
| 966 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 967 |
s2n_connection_ptr_free); |
| 968 |
EXPECT_NOT_NULL(server); |
| 969 |
s2n_ktls_configure_connection(server, S2N_KTLS_MODE_SEND); |
| 970 |
s2n_ktls_configure_connection(server, S2N_KTLS_MODE_RECV); |
| 971 |
EXPECT_OK(s2n_skip_handshake(server)); |
| 972 |
|
| 973 |
DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 }, |
| 974 |
s2n_ktls_io_stuffer_pair_free); |
| 975 |
EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair)); |
| 976 |
|
| 977 |
|
| 978 |
EXPECT_SUCCESS(s2n_stuffer_free(&io_pair.server_in.data_buffer)); |
| 979 |
EXPECT_SUCCESS(s2n_stuffer_alloc(&io_pair.server_in.data_buffer, 1)); |
| 980 |
|
| 981 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 982 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(client, &blocked), S2N_ERR_IO_BLOCKED); |
| 983 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE); |
| 984 |
EXPECT_FALSE(s2n_connection_check_io_status(client, S2N_IO_WRITABLE)); |
| 985 |
EXPECT_TRUE(s2n_connection_check_io_status(client, S2N_IO_READABLE)); |
| 986 |
|
| 987 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(server, &blocked), S2N_ERR_IO_BLOCKED); |
| 988 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ); |
| 989 |
EXPECT_FALSE(s2n_connection_check_io_status(server, S2N_IO_WRITABLE)); |
| 990 |
EXPECT_TRUE(s2n_connection_check_io_status(server, S2N_IO_READABLE)); |
| 991 |
|
| 992 |
|
| 993 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&io_pair.server_in.data_buffer)); |
| 994 |
|
| 995 |
EXPECT_SUCCESS(s2n_shutdown(client, &blocked)); |
| 996 |
EXPECT_SUCCESS(s2n_shutdown(server, &blocked)); |
| 997 |
}; |
| 998 |
|
| 999 |
|
| 1000 |
{ |
| 1001 |
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 1002 |
s2n_connection_ptr_free); |
| 1003 |
EXPECT_NOT_NULL(client); |
| 1004 |
s2n_ktls_configure_connection(client, S2N_KTLS_MODE_SEND); |
| 1005 |
s2n_ktls_configure_connection(client, S2N_KTLS_MODE_RECV); |
| 1006 |
EXPECT_OK(s2n_skip_handshake(client)); |
| 1007 |
|
| 1008 |
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 1009 |
s2n_connection_ptr_free); |
| 1010 |
EXPECT_NOT_NULL(server); |
| 1011 |
s2n_ktls_configure_connection(server, S2N_KTLS_MODE_SEND); |
| 1012 |
s2n_ktls_configure_connection(server, S2N_KTLS_MODE_RECV); |
| 1013 |
EXPECT_OK(s2n_skip_handshake(server)); |
| 1014 |
|
| 1015 |
DEFER_CLEANUP(struct s2n_test_ktls_io_stuffer_pair io_pair = { 0 }, |
| 1016 |
s2n_ktls_io_stuffer_pair_free); |
| 1017 |
EXPECT_OK(s2n_test_init_ktls_io_stuffer(server, client, &io_pair)); |
| 1018 |
|
| 1019 |
|
| 1020 |
uint8_t app_data[] = "hello world"; |
| 1021 |
size_t app_data_size = sizeof(app_data); |
| 1022 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 1023 |
size_t app_data_count = 5; |
| 1024 |
for (size_t i = 0; i < app_data_count; i++) { |
| 1025 |
EXPECT_SUCCESS(s2n_send(client, app_data, app_data_size, &blocked)); |
| 1026 |
EXPECT_SUCCESS(s2n_send(server, app_data, app_data_size, &blocked)); |
| 1027 |
} |
| 1028 |
EXPECT_OK(s2n_test_validate_ancillary(&io_pair.client_in, TLS_APPLICATION_DATA, app_data_size)); |
| 1029 |
EXPECT_OK(s2n_test_validate_ancillary(&io_pair.server_in, TLS_APPLICATION_DATA, app_data_size)); |
| 1030 |
EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.client_in, app_data_count)); |
| 1031 |
EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.server_in, app_data_count)); |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(client, &blocked), S2N_ERR_IO_BLOCKED); |
| 1036 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ); |
| 1037 |
EXPECT_FALSE(s2n_connection_check_io_status(client, S2N_IO_WRITABLE)); |
| 1038 |
EXPECT_TRUE(s2n_connection_check_io_status(client, S2N_IO_READABLE)); |
| 1039 |
EXPECT_TRUE(client->alert_sent); |
| 1040 |
EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.client_in, 0)); |
| 1041 |
EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.server_in, app_data_count + 1)); |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
EXPECT_SUCCESS(s2n_shutdown(server, &blocked)); |
| 1046 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 1047 |
EXPECT_TRUE(s2n_connection_check_io_status(server, S2N_IO_CLOSED)); |
| 1048 |
EXPECT_TRUE(server->alert_sent); |
| 1049 |
EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.client_in, 1)); |
| 1050 |
EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.server_in, 0)); |
| 1051 |
|
| 1052 |
|
| 1053 |
EXPECT_SUCCESS(s2n_shutdown(client, &blocked)); |
| 1054 |
EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED); |
| 1055 |
EXPECT_TRUE(s2n_connection_check_io_status(client, S2N_IO_CLOSED)); |
| 1056 |
EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.client_in, 0)); |
| 1057 |
EXPECT_OK(s2n_test_records_in_ancillary(&io_pair.server_in, 0)); |
| 1058 |
}; |
| 1059 |
}; |
| 1060 |
#endif |
| 1061 |
|
| 1062 |
END_TEST(); |
| 1063 |
} |