| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <fcntl.h> |
| 17 |
#include <stdint.h> |
| 18 |
#include <sys/wait.h> |
| 19 |
#include <unistd.h> |
| 20 |
|
| 21 |
#include "api/s2n.h" |
| 22 |
#include "s2n_test.h" |
| 23 |
#include "testlib/s2n_testlib.h" |
| 24 |
#include "tls/s2n_connection.h" |
| 25 |
#include "tls/s2n_internal.h" |
| 26 |
|
| 27 |
struct client_hello_context { |
| 28 |
int invoked; |
| 29 |
int swap_config_during_callback; |
| 30 |
int swap_config_nonblocking_mode; |
| 31 |
int mark_done_during_callback; |
| 32 |
struct s2n_config *config; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
int legacy_rc_for_server_name_used; |
| 40 |
}; |
| 41 |
|
| 42 |
int mock_client(struct s2n_test_io_pair *io_pair, int expect_failure, int expect_server_name_used) |
| 43 |
{ |
| 44 |
struct s2n_connection *conn = NULL; |
| 45 |
struct s2n_config *config = NULL; |
| 46 |
s2n_blocked_status blocked; |
| 47 |
int result = 0; |
| 48 |
int rc = 0; |
| 49 |
const char *protocols[] = { "h2", "http/1.1" }; |
| 50 |
|
| 51 |
|
| 52 |
sleep(1); |
| 53 |
|
| 54 |
conn = s2n_connection_new(S2N_CLIENT); |
| 55 |
config = s2n_config_new(); |
| 56 |
EXPECT_OK(s2n_connection_set_tls12_security_policy(conn)); |
| 57 |
s2n_config_set_protocol_preferences(config, protocols, 2); |
| 58 |
s2n_config_disable_x509_verification(config); |
| 59 |
s2n_connection_set_config(conn, config); |
| 60 |
|
| 61 |
s2n_connection_set_io_pair(conn, io_pair); |
| 62 |
|
| 63 |
s2n_set_server_name(conn, "example.com"); |
| 64 |
|
| 65 |
rc = s2n_negotiate(conn, &blocked); |
| 66 |
if (expect_failure) { |
| 67 |
if (!rc) { |
| 68 |
result = 1; |
| 69 |
} |
| 70 |
|
| 71 |
if (s2n_connection_get_alert(conn) != 40) { |
| 72 |
result = 2; |
| 73 |
} |
| 74 |
} else { |
| 75 |
char buffer[0xffff]; |
| 76 |
|
| 77 |
if (conn->server_name_used != expect_server_name_used) { |
| 78 |
result = 1; |
| 79 |
} |
| 80 |
|
| 81 |
if (rc < 0) { |
| 82 |
result = 2; |
| 83 |
} |
| 84 |
|
| 85 |
for (int i = 1; i < 0xffff; i += 100) { |
| 86 |
memset(buffer, 33, sizeof(char) * i); |
| 87 |
s2n_send(conn, buffer, i, &blocked); |
| 88 |
} |
| 89 |
|
| 90 |
int shutdown_rc = -1; |
| 91 |
do { |
| 92 |
shutdown_rc = s2n_shutdown(conn, &blocked); |
| 93 |
} while (shutdown_rc != 0); |
| 94 |
} |
| 95 |
|
| 96 |
s2n_connection_free(conn); |
| 97 |
s2n_config_free(config); |
| 98 |
|
| 99 |
|
| 100 |
sleep(1); |
| 101 |
|
| 102 |
s2n_cleanup(); |
| 103 |
s2n_io_pair_close_one_end(io_pair, S2N_CLIENT); |
| 104 |
|
| 105 |
exit(result); |
| 106 |
} |
| 107 |
|
| 108 |
int client_hello_swap_config(struct s2n_connection *conn, void *ctx) |
| 109 |
{ |
| 110 |
struct client_hello_context *client_hello_ctx = NULL; |
| 111 |
struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(conn); |
| 112 |
const char *sent_server_name = "example.com"; |
| 113 |
const char *received_server_name = NULL; |
| 114 |
if (ctx == NULL) { |
| 115 |
return -1; |
| 116 |
} |
| 117 |
client_hello_ctx = ctx; |
| 118 |
|
| 119 |
client_hello_ctx->invoked++; |
| 120 |
|
| 121 |
|
| 122 |
uint8_t expected_server_name[] = { |
| 123 |
|
| 124 |
0x00, 0x0E, |
| 125 |
|
| 126 |
0x00, |
| 127 |
|
| 128 |
0x00, 0x0B, |
| 129 |
|
| 130 |
'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm' |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
uint32_t len = s2n_client_hello_get_extension_length(client_hello, S2N_EXTENSION_SERVER_NAME); |
| 135 |
if (len != 16) { |
| 136 |
return -1; |
| 137 |
} |
| 138 |
|
| 139 |
uint8_t ser_name[16] = { 0 }; |
| 140 |
if (s2n_client_hello_get_extension_by_id(client_hello, S2N_EXTENSION_SERVER_NAME, ser_name, len) <= 0) { |
| 141 |
return -1; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
received_server_name = s2n_get_server_name(conn); |
| 146 |
if (received_server_name == NULL || strcmp(received_server_name, sent_server_name)) { |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
if (memcmp(ser_name, expected_server_name, len) != 0) { |
| 151 |
return -1; |
| 152 |
} |
| 153 |
|
| 154 |
if (client_hello_ctx->mark_done_during_callback) { |
| 155 |
EXPECT_SUCCESS(s2n_client_hello_cb_done(conn)); |
| 156 |
} |
| 157 |
|
| 158 |
if (client_hello_ctx->swap_config_during_callback) { |
| 159 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, client_hello_ctx->config)); |
| 160 |
if (client_hello_ctx->legacy_rc_for_server_name_used) { |
| 161 |
return 1; |
| 162 |
} |
| 163 |
EXPECT_SUCCESS(s2n_connection_server_name_extension_used(conn)); |
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
int client_hello_fail_handshake(struct s2n_connection *conn, void *ctx) |
| 171 |
{ |
| 172 |
struct client_hello_context *client_hello_ctx = NULL; |
| 173 |
|
| 174 |
if (ctx == NULL) { |
| 175 |
return -1; |
| 176 |
} |
| 177 |
client_hello_ctx = ctx; |
| 178 |
|
| 179 |
|
| 180 |
client_hello_ctx->invoked++; |
| 181 |
|
| 182 |
|
| 183 |
return -1; |
| 184 |
} |
| 185 |
|
| 186 |
int s2n_negotiate_nonblocking_ch_cb(struct s2n_connection *conn, |
| 187 |
struct client_hello_context *ch_ctx, bool server_name_used) |
| 188 |
{ |
| 189 |
s2n_blocked_status blocked; |
| 190 |
EXPECT_NOT_NULL(conn); |
| 191 |
|
| 192 |
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(conn, &blocked), S2N_ERR_ASYNC_BLOCKED); |
| 193 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_APPLICATION_INPUT); |
| 194 |
|
| 195 |
|
| 196 |
EXPECT_EQUAL(ch_ctx->invoked, 1); |
| 197 |
|
| 198 |
|
| 199 |
if (ch_ctx->swap_config_nonblocking_mode) { |
| 200 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, ch_ctx->config)); |
| 201 |
} |
| 202 |
|
| 203 |
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(conn, &blocked), S2N_ERR_ASYNC_BLOCKED); |
| 204 |
EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_APPLICATION_INPUT); |
| 205 |
|
| 206 |
|
| 207 |
EXPECT_SUCCESS(s2n_client_hello_cb_done(conn)); |
| 208 |
if (server_name_used) { |
| 209 |
EXPECT_SUCCESS(s2n_connection_server_name_extension_used(conn)); |
| 210 |
} |
| 211 |
return s2n_negotiate(conn, &blocked); |
| 212 |
} |
| 213 |
|
| 214 |
int s2n_negotiate_blocking_ch_cb(struct s2n_connection *conn, struct client_hello_context *ch_ctx) |
| 215 |
{ |
| 216 |
s2n_blocked_status blocked; |
| 217 |
EXPECT_NOT_NULL(conn); |
| 218 |
|
| 219 |
int rc = s2n_negotiate(conn, &blocked); |
| 220 |
|
| 221 |
EXPECT_EQUAL(ch_ctx->invoked, 1); |
| 222 |
return rc; |
| 223 |
} |
| 224 |
|
| 225 |
int server_recv(struct s2n_connection *conn) |
| 226 |
{ |
| 227 |
static char buffer[0xffff]; |
| 228 |
s2n_blocked_status blocked; |
| 229 |
|
| 230 |
for (int i = 1; i < 0xffff; i += 100) { |
| 231 |
char *ptr = buffer; |
| 232 |
int size = i; |
| 233 |
|
| 234 |
do { |
| 235 |
int bytes_read = 0; |
| 236 |
EXPECT_SUCCESS(bytes_read = s2n_recv(conn, ptr, size, &blocked)); |
| 237 |
|
| 238 |
size -= bytes_read; |
| 239 |
ptr += bytes_read; |
| 240 |
} while (size); |
| 241 |
|
| 242 |
for (int j = 0; j < i; j++) { |
| 243 |
EXPECT_EQUAL((buffer)[j], 33); |
| 244 |
} |
| 245 |
} |
| 246 |
return S2N_SUCCESS; |
| 247 |
} |
| 248 |
|
| 249 |
int init_server_conn(struct s2n_connection **conn, struct s2n_test_io_pair *io_pair, |
| 250 |
struct s2n_config *config) |
| 251 |
{ |
| 252 |
|
| 253 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(io_pair, S2N_CLIENT)); |
| 254 |
|
| 255 |
EXPECT_NOT_NULL(*conn = s2n_connection_new(S2N_SERVER)); |
| 256 |
|
| 257 |
|
| 258 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(*conn, io_pair)); |
| 259 |
EXPECT_SUCCESS(s2n_connection_set_config(*conn, config)); |
| 260 |
return S2N_SUCCESS; |
| 261 |
} |
| 262 |
|
| 263 |
int start_client_conn(struct s2n_test_io_pair *io_pair, pid_t *pid, |
| 264 |
int expect_failure, int expect_server_name_used) |
| 265 |
{ |
| 266 |
|
| 267 |
EXPECT_SUCCESS(s2n_io_pair_init(io_pair)); |
| 268 |
|
| 269 |
|
| 270 |
*pid = fork(); |
| 271 |
if (*pid == 0) { |
| 272 |
|
| 273 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(io_pair, S2N_SERVER)); |
| 274 |
|
| 275 |
mock_client(io_pair, expect_failure, expect_server_name_used); |
| 276 |
} |
| 277 |
return S2N_SUCCESS; |
| 278 |
} |
| 279 |
|
| 280 |
static int test_case_clean(struct s2n_connection *conn, pid_t client_pid, |
| 281 |
struct s2n_config *config, struct s2n_test_io_pair *io_pair, |
| 282 |
struct client_hello_context *ch_ctx, struct s2n_cert_chain_and_key *chain_and_key) |
| 283 |
{ |
| 284 |
s2n_blocked_status blocked; |
| 285 |
int status = 0; |
| 286 |
|
| 287 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 288 |
EXPECT_EQUAL(waitpid(-1, &status, 0), client_pid); |
| 289 |
EXPECT_EQUAL(status, 0); |
| 290 |
|
| 291 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(io_pair, S2N_SERVER)); |
| 292 |
|
| 293 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 294 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 295 |
memset(ch_ctx, 0, sizeof(struct client_hello_context)); |
| 296 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key)); |
| 297 |
|
| 298 |
return S2N_SUCCESS; |
| 299 |
} |
| 300 |
|
| 301 |
int run_test_config_swap_ch_cb(s2n_client_hello_cb_mode cb_mode, |
| 302 |
struct client_hello_context *ch_ctx) |
| 303 |
{ |
| 304 |
struct s2n_test_io_pair io_pair; |
| 305 |
struct s2n_config *config = NULL; |
| 306 |
struct s2n_connection *conn = NULL; |
| 307 |
struct s2n_config *swap_config = NULL; |
| 308 |
pid_t pid = 0; |
| 309 |
struct s2n_cert_chain_and_key *chain_and_key = NULL; |
| 310 |
|
| 311 |
EXPECT_SUCCESS(start_client_conn(&io_pair, &pid, 0, 1)); |
| 312 |
|
| 313 |
|
| 314 |
static const char *protocols[] = { "h2" }; |
| 315 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY)); |
| 316 |
|
| 317 |
|
| 318 |
EXPECT_NOT_NULL(swap_config = s2n_config_new()); |
| 319 |
EXPECT_OK(s2n_config_set_tls12_security_policy(swap_config)); |
| 320 |
EXPECT_SUCCESS(s2n_config_set_protocol_preferences(swap_config, protocols, 1)); |
| 321 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(swap_config, chain_and_key)); |
| 322 |
ch_ctx->config = swap_config; |
| 323 |
|
| 324 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(swap_config, cb_mode)); |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
EXPECT_NOT_NULL(config = s2n_config_new()); |
| 329 |
|
| 330 |
|
| 331 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(config, cb_mode)); |
| 332 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, client_hello_swap_config, ch_ctx)); |
| 333 |
|
| 334 |
EXPECT_SUCCESS(init_server_conn(&conn, &io_pair, config)); |
| 335 |
|
| 336 |
|
| 337 |
if (cb_mode == S2N_CLIENT_HELLO_CB_NONBLOCKING && !ch_ctx->mark_done_during_callback) { |
| 338 |
|
| 339 |
EXPECT_SUCCESS(s2n_negotiate_nonblocking_ch_cb(conn, ch_ctx, true)); |
| 340 |
} else { |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
EXPECT_SUCCESS(s2n_negotiate_blocking_ch_cb(conn, ch_ctx)); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
EXPECT_NULL(s2n_get_server_name(NULL)); |
| 349 |
EXPECT_EQUAL(s2n_errno, S2N_ERR_NULL); |
| 350 |
|
| 351 |
|
| 352 |
EXPECT_STRING_EQUAL(s2n_get_application_protocol(conn), protocols[0]); |
| 353 |
|
| 354 |
EXPECT_SUCCESS(server_recv(conn)); |
| 355 |
|
| 356 |
EXPECT_SUCCESS(test_case_clean(conn, pid, config, &io_pair, ch_ctx, chain_and_key)); |
| 357 |
EXPECT_SUCCESS(s2n_config_free(swap_config)); |
| 358 |
return S2N_SUCCESS; |
| 359 |
} |
| 360 |
|
| 361 |
int run_test_no_config_swap_ch_cb(s2n_client_hello_cb_mode cb_mode, struct client_hello_context *ch_ctx) |
| 362 |
{ |
| 363 |
struct s2n_test_io_pair io_pair; |
| 364 |
struct s2n_config *config = NULL; |
| 365 |
struct s2n_connection *conn = NULL; |
| 366 |
pid_t pid = 0; |
| 367 |
struct s2n_cert_chain_and_key *chain_and_key = NULL; |
| 368 |
|
| 369 |
EXPECT_SUCCESS(start_client_conn(&io_pair, &pid, 0, 0)); |
| 370 |
|
| 371 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY)); |
| 372 |
EXPECT_NOT_NULL(config = s2n_config_new()); |
| 373 |
EXPECT_OK(s2n_config_set_tls12_security_policy(config)); |
| 374 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key)); |
| 375 |
|
| 376 |
|
| 377 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, client_hello_swap_config, ch_ctx)); |
| 378 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(config, cb_mode)); |
| 379 |
EXPECT_SUCCESS(init_server_conn(&conn, &io_pair, config)); |
| 380 |
|
| 381 |
|
| 382 |
if (cb_mode == S2N_CLIENT_HELLO_CB_NONBLOCKING) { |
| 383 |
|
| 384 |
EXPECT_SUCCESS(s2n_negotiate_nonblocking_ch_cb(conn, ch_ctx, false)); |
| 385 |
} else { |
| 386 |
EXPECT_SUCCESS(s2n_negotiate_blocking_ch_cb(conn, ch_ctx)); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
EXPECT_NULL(s2n_get_server_name(NULL)); |
| 391 |
EXPECT_EQUAL(s2n_errno, S2N_ERR_NULL); |
| 392 |
|
| 393 |
EXPECT_SUCCESS(server_recv(conn)); |
| 394 |
|
| 395 |
EXPECT_SUCCESS(test_case_clean(conn, pid, config, &io_pair, ch_ctx, chain_and_key)); |
| 396 |
return S2N_SUCCESS; |
| 397 |
} |
| 398 |
|
| 399 |
int run_test_reject_handshake_ch_cb(s2n_client_hello_cb_mode cb_mode, struct client_hello_context *ch_ctx) |
| 400 |
{ |
| 401 |
struct s2n_test_io_pair io_pair; |
| 402 |
struct s2n_config *config = NULL; |
| 403 |
struct s2n_connection *conn = NULL; |
| 404 |
pid_t pid = 0; |
| 405 |
s2n_blocked_status blocked; |
| 406 |
struct s2n_cert_chain_and_key *chain_and_key = NULL; |
| 407 |
|
| 408 |
EXPECT_SUCCESS(start_client_conn(&io_pair, &pid, 1, 0)); |
| 409 |
|
| 410 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY)); |
| 411 |
EXPECT_NOT_NULL(config = s2n_config_new()); |
| 412 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key)); |
| 413 |
|
| 414 |
|
| 415 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, client_hello_fail_handshake, ch_ctx)); |
| 416 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(config, cb_mode)); |
| 417 |
|
| 418 |
EXPECT_SUCCESS(init_server_conn(&conn, &io_pair, config)); |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
EXPECT_SUCCESS(s2n_connection_set_blinding(conn, S2N_SELF_SERVICE_BLINDING)); |
| 423 |
|
| 424 |
|
| 425 |
EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(conn, &blocked), S2N_ERR_CANCELLED); |
| 426 |
|
| 427 |
|
| 428 |
EXPECT_EQUAL(s2n_connection_get_delay(conn), 0); |
| 429 |
|
| 430 |
|
| 431 |
EXPECT_EQUAL(ch_ctx->invoked, 1); |
| 432 |
|
| 433 |
|
| 434 |
EXPECT_SUCCESS(test_case_clean(conn, pid, config, &io_pair, ch_ctx, chain_and_key)); |
| 435 |
return S2N_SUCCESS; |
| 436 |
} |
| 437 |
|
| 438 |
int main(int argc, char **argv) |
| 439 |
{ |
| 440 |
struct client_hello_context client_hello_ctx = { 0 }; |
| 441 |
BEGIN_TEST(); |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
client_hello_ctx.swap_config_nonblocking_mode = 1; |
| 446 |
EXPECT_SUCCESS(run_test_config_swap_ch_cb(S2N_CLIENT_HELLO_CB_NONBLOCKING, &client_hello_ctx)); |
| 447 |
|
| 448 |
|
| 449 |
client_hello_ctx.swap_config_during_callback = 1; |
| 450 |
client_hello_ctx.mark_done_during_callback = 1; |
| 451 |
EXPECT_SUCCESS(run_test_config_swap_ch_cb(S2N_CLIENT_HELLO_CB_NONBLOCKING, &client_hello_ctx)); |
| 452 |
|
| 453 |
|
| 454 |
client_hello_ctx.swap_config_during_callback = 1; |
| 455 |
EXPECT_SUCCESS(run_test_config_swap_ch_cb(S2N_CLIENT_HELLO_CB_BLOCKING, &client_hello_ctx)); |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
client_hello_ctx.swap_config_during_callback = 1; |
| 460 |
client_hello_ctx.legacy_rc_for_server_name_used = 1; |
| 461 |
EXPECT_SUCCESS(run_test_config_swap_ch_cb(S2N_CLIENT_HELLO_CB_BLOCKING, &client_hello_ctx)); |
| 462 |
|
| 463 |
|
| 464 |
EXPECT_SUCCESS(run_test_no_config_swap_ch_cb(S2N_CLIENT_HELLO_CB_BLOCKING, &client_hello_ctx)); |
| 465 |
|
| 466 |
EXPECT_SUCCESS(run_test_no_config_swap_ch_cb(S2N_CLIENT_HELLO_CB_NONBLOCKING, &client_hello_ctx)); |
| 467 |
|
| 468 |
|
| 469 |
EXPECT_SUCCESS(run_test_reject_handshake_ch_cb(S2N_CLIENT_HELLO_CB_BLOCKING, &client_hello_ctx)); |
| 470 |
|
| 471 |
EXPECT_SUCCESS(run_test_reject_handshake_ch_cb(S2N_CLIENT_HELLO_CB_NONBLOCKING, &client_hello_ctx)); |
| 472 |
|
| 473 |
END_TEST(); |
| 474 |
|
| 475 |
return 0; |
| 476 |
} |