| 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_handshake.h" |
| 26 |
#include "tls/s2n_tls13.h" |
| 27 |
#include "utils/s2n_random.h" |
| 28 |
#include "utils/s2n_safety.h" |
| 29 |
|
| 30 |
static const float minimum_send_percent = 5.0; |
| 31 |
|
| 32 |
#define MIN_PERCENT_COMPLETE(remaining, total) ((((total - remaining) / (total * 1.0)) * 100.0) > minimum_send_percent) |
| 33 |
|
| 34 |
int mock_client(struct s2n_test_io_pair *io_pair, uint8_t *expected_data, uint32_t size) |
| 35 |
{ |
| 36 |
uint8_t *buffer = malloc(size); |
| 37 |
uint8_t *ptr = buffer; |
| 38 |
struct s2n_connection *client_conn = NULL; |
| 39 |
struct s2n_config *client_config = NULL; |
| 40 |
s2n_blocked_status blocked; |
| 41 |
int result = 0; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
int should_block = 1; |
| 46 |
|
| 47 |
|
| 48 |
sleep(1); |
| 49 |
|
| 50 |
client_conn = s2n_connection_new(S2N_CLIENT); |
| 51 |
client_config = s2n_config_new(); |
| 52 |
s2n_config_disable_x509_verification(client_config); |
| 53 |
s2n_connection_set_config(client_conn, client_config); |
| 54 |
POSIX_GUARD(s2n_config_set_cipher_preferences(client_config, "test_all")); |
| 55 |
|
| 56 |
s2n_connection_set_io_pair(client_conn, io_pair); |
| 57 |
|
| 58 |
result = s2n_negotiate(client_conn, &blocked); |
| 59 |
if (result < 0) { |
| 60 |
return 1; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
uint32_t remaining = size; |
| 65 |
while (remaining) { |
| 66 |
int r = s2n_recv(client_conn, ptr, remaining, &blocked); |
| 67 |
if (r < 0) { |
| 68 |
return 1; |
| 69 |
} |
| 70 |
remaining -= r; |
| 71 |
ptr += r; |
| 72 |
if (should_block && MIN_PERCENT_COMPLETE(remaining, size)) { |
| 73 |
raise(SIGSTOP); |
| 74 |
should_block = 0; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
int shutdown_rc = -1; |
| 79 |
do { |
| 80 |
shutdown_rc = s2n_shutdown(client_conn, &blocked); |
| 81 |
} while (shutdown_rc != 0); |
| 82 |
|
| 83 |
for (size_t i = 0; i < size; i++) { |
| 84 |
if (buffer[i] != expected_data[i]) { |
| 85 |
return 1; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
free(buffer); |
| 90 |
s2n_connection_free(client_conn); |
| 91 |
s2n_config_free(client_config); |
| 92 |
|
| 93 |
s2n_cleanup(); |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
int mock_client_iov(struct s2n_test_io_pair *io_pair, struct iovec *iov, uint32_t iov_size) |
| 99 |
{ |
| 100 |
struct s2n_connection *client_conn = NULL; |
| 101 |
struct s2n_config *client_config = NULL; |
| 102 |
s2n_blocked_status blocked; |
| 103 |
int result = 0; |
| 104 |
int total_size = 0, i = 0; |
| 105 |
int should_block = 1; |
| 106 |
|
| 107 |
for (i = 0; i < iov_size; i++) { |
| 108 |
total_size += iov[i].iov_len; |
| 109 |
} |
| 110 |
uint8_t *buffer = malloc(total_size + iov[0].iov_len); |
| 111 |
int buffer_offs = 0; |
| 112 |
|
| 113 |
|
| 114 |
sleep(1); |
| 115 |
|
| 116 |
client_conn = s2n_connection_new(S2N_CLIENT); |
| 117 |
client_config = s2n_config_new(); |
| 118 |
s2n_config_disable_x509_verification(client_config); |
| 119 |
s2n_connection_set_config(client_conn, client_config); |
| 120 |
POSIX_GUARD(s2n_config_set_cipher_preferences(client_config, "test_all")); |
| 121 |
|
| 122 |
s2n_connection_set_io_pair(client_conn, io_pair); |
| 123 |
|
| 124 |
result = s2n_negotiate(client_conn, &blocked); |
| 125 |
if (result < 0) { |
| 126 |
return 1; |
| 127 |
} |
| 128 |
|
| 129 |
uint32_t remaining = total_size; |
| 130 |
while (remaining) { |
| 131 |
int r = s2n_recv(client_conn, &buffer[buffer_offs], remaining, &blocked); |
| 132 |
if (r < 0) { |
| 133 |
return 1; |
| 134 |
} |
| 135 |
remaining -= r; |
| 136 |
buffer_offs += r; |
| 137 |
if (should_block && MIN_PERCENT_COMPLETE(remaining, total_size)) { |
| 138 |
raise(SIGSTOP); |
| 139 |
should_block = 0; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
remaining = iov[0].iov_len; |
| 144 |
while (remaining) { |
| 145 |
int r = s2n_recv(client_conn, &buffer[buffer_offs], remaining, &blocked); |
| 146 |
if (r < 0) { |
| 147 |
return 1; |
| 148 |
} |
| 149 |
remaining -= r; |
| 150 |
buffer_offs += r; |
| 151 |
} |
| 152 |
|
| 153 |
int shutdown_rc = -1; |
| 154 |
do { |
| 155 |
shutdown_rc = s2n_shutdown(client_conn, &blocked); |
| 156 |
} while (shutdown_rc != 0); |
| 157 |
|
| 158 |
for (i = 0, buffer_offs = 0; i < iov_size; i++) { |
| 159 |
if (memcmp(iov[i].iov_base, &buffer[buffer_offs], iov[i].iov_len)) { |
| 160 |
return 1; |
| 161 |
} |
| 162 |
buffer_offs += iov[i].iov_len; |
| 163 |
} |
| 164 |
|
| 165 |
if (memcmp(iov[0].iov_base, &buffer[buffer_offs], iov[0].iov_len)) { |
| 166 |
return 1; |
| 167 |
} |
| 168 |
|
| 169 |
free(buffer); |
| 170 |
s2n_connection_free(client_conn); |
| 171 |
s2n_config_free(client_config); |
| 172 |
|
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
S2N_RESULT cleanup_io_data(struct iovec **iov, int iov_size, struct s2n_blob *blob) |
| 177 |
{ |
| 178 |
if (*iov) { |
| 179 |
for (int i = 0; i < iov_size; i++) { |
| 180 |
free((*iov)[i].iov_base); |
| 181 |
} |
| 182 |
free(*iov); |
| 183 |
} else { |
| 184 |
s2n_free(blob); |
| 185 |
} |
| 186 |
|
| 187 |
return S2N_RESULT_OK; |
| 188 |
} |
| 189 |
|
| 190 |
int test_send(int use_tls13, int use_iov, int prefer_throughput) |
| 191 |
{ |
| 192 |
s2n_blocked_status blocked; |
| 193 |
int status = 0; |
| 194 |
pid_t pid = 0; |
| 195 |
char cert_chain_pem[S2N_MAX_TEST_PEM_SIZE]; |
| 196 |
char private_key_pem[S2N_MAX_TEST_PEM_SIZE]; |
| 197 |
char dhparams_pem[S2N_MAX_TEST_PEM_SIZE]; |
| 198 |
|
| 199 |
|
| 200 |
uint32_t data_size = 0; |
| 201 |
struct s2n_blob blob = { 0 }; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
int iov_payload_size = 8192, iov_size = 7; |
| 220 |
|
| 221 |
struct iovec *iov = NULL; |
| 222 |
if (!use_iov) { |
| 223 |
data_size = 10000000; |
| 224 |
EXPECT_SUCCESS(s2n_alloc(&blob, data_size)); |
| 225 |
EXPECT_OK(s2n_get_public_random_data(&blob)); |
| 226 |
} else { |
| 227 |
iov = malloc(sizeof(*iov) * iov_size); |
| 228 |
data_size = 0; |
| 229 |
for (int i = 0; i < iov_size; i++, iov_payload_size *= 2) { |
| 230 |
struct s2n_blob blob_local = { 0 }; |
| 231 |
iov[i].iov_base = blob_local.data = malloc(iov_payload_size); |
| 232 |
iov[i].iov_len = blob_local.size = iov_payload_size; |
| 233 |
EXPECT_NOT_NULL(blob_local.data); |
| 234 |
EXPECT_OK(s2n_get_public_random_data(&blob_local)); |
| 235 |
data_size += iov_payload_size; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
struct s2n_test_io_pair io_pair; |
| 241 |
EXPECT_SUCCESS(s2n_io_pair_init(&io_pair)); |
| 242 |
|
| 243 |
|
| 244 |
pid = fork(); |
| 245 |
if (pid == 0) { |
| 246 |
|
| 247 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 248 |
|
| 249 |
|
| 250 |
const int client_rc = !use_iov ? mock_client(&io_pair, blob.data, data_size) : mock_client_iov(&io_pair, iov, iov_size); |
| 251 |
|
| 252 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT)); |
| 253 |
EXPECT_OK(cleanup_io_data(&iov, iov_size, &blob)); |
| 254 |
exit(client_rc); |
| 255 |
} |
| 256 |
|
| 257 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 258 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 259 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(), s2n_cert_chain_and_key_ptr_free); |
| 260 |
|
| 261 |
EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE)); |
| 262 |
EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE)); |
| 263 |
|
| 264 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem)); |
| 265 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key)); |
| 266 |
EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE)); |
| 267 |
EXPECT_SUCCESS(s2n_config_add_dhparams(config, dhparams_pem)); |
| 268 |
|
| 269 |
if (use_tls13) { |
| 270 |
POSIX_GUARD(s2n_config_set_cipher_preferences(config, "test_all")); |
| 271 |
} else { |
| 272 |
POSIX_GUARD(s2n_config_set_cipher_preferences(config, "test_all_tls12")); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT)); |
| 277 |
|
| 278 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 279 |
|
| 280 |
if (prefer_throughput) { |
| 281 |
EXPECT_SUCCESS(s2n_connection_prefer_throughput(conn)); |
| 282 |
} else { |
| 283 |
EXPECT_SUCCESS(s2n_connection_prefer_low_latency(conn)); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(conn, &io_pair)); |
| 288 |
|
| 289 |
EXPECT_SUCCESS(s2n_connection_use_corked_io(conn)); |
| 290 |
|
| 291 |
|
| 292 |
EXPECT_SUCCESS(s2n_negotiate(conn, &blocked)); |
| 293 |
|
| 294 |
|
| 295 |
if (use_tls13) { |
| 296 |
EXPECT_EQUAL(conn->actual_protocol_version, s2n_get_highest_fully_supported_tls_version()); |
| 297 |
} else { |
| 298 |
EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS12); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
s2n_fd_set_non_blocking(io_pair.server); |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
uint32_t remaining = data_size; |
| 307 |
uint8_t *ptr = blob.data; |
| 308 |
uint32_t iov_offs = 0; |
| 309 |
|
| 310 |
while (remaining) { |
| 311 |
int r = !use_iov ? s2n_send(conn, ptr, remaining, &blocked) : |
| 312 |
s2n_sendv_with_offset(conn, iov, iov_size, iov_offs, &blocked); |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
if (r < 0 && !MIN_PERCENT_COMPLETE(remaining, data_size)) { |
| 318 |
continue; |
| 319 |
} |
| 320 |
|
| 321 |
if (r < 0 && blocked == S2N_BLOCKED_ON_WRITE) { |
| 322 |
|
| 323 |
break; |
| 324 |
} |
| 325 |
|
| 326 |
EXPECT_TRUE(r > 0); |
| 327 |
remaining -= r; |
| 328 |
if (!use_iov) { |
| 329 |
ptr += r; |
| 330 |
} else { |
| 331 |
iov_offs += r; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
EXPECT_TRUE(remaining < data_size); |
| 337 |
EXPECT_TRUE(remaining > 0); |
| 338 |
|
| 339 |
|
| 340 |
sleep(1); |
| 341 |
|
| 342 |
EXPECT_SUCCESS(kill(pid, SIGCONT)); |
| 343 |
|
| 344 |
|
| 345 |
s2n_fd_set_blocking(io_pair.server); |
| 346 |
|
| 347 |
|
| 348 |
while (remaining) { |
| 349 |
int r = !use_iov ? s2n_send(conn, ptr, remaining, &blocked) : |
| 350 |
s2n_sendv_with_offset(conn, iov, iov_size, iov_offs, &blocked); |
| 351 |
EXPECT_TRUE(r > 0); |
| 352 |
remaining -= r; |
| 353 |
if (!use_iov) { |
| 354 |
ptr += r; |
| 355 |
} else { |
| 356 |
iov_offs += r; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
if (use_iov) { |
| 361 |
int r = s2n_sendv(conn, iov, 1, &blocked); |
| 362 |
EXPECT_TRUE(r > 0); |
| 363 |
} |
| 364 |
|
| 365 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 366 |
|
| 367 |
EXPECT_EQUAL(waitpid(-1, &status, 0), pid); |
| 368 |
EXPECT_EQUAL(status, 0); |
| 369 |
|
| 370 |
|
| 371 |
EXPECT_OK(cleanup_io_data(&iov, iov_size, &blob)); |
| 372 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 373 |
|
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
int main(int argc, char **argv) |
| 378 |
{ |
| 379 |
|
| 380 |
signal(SIGPIPE, SIG_IGN); |
| 381 |
|
| 382 |
BEGIN_TEST(); |
| 383 |
|
| 384 |
for (int use_tls13 = 0; use_tls13 < 2; use_tls13++) { |
| 385 |
for (int use_iovec = 0; use_iovec < 2; use_iovec++) { |
| 386 |
for (int use_throughput = 0; use_throughput < 2; use_throughput++) { |
| 387 |
test_send(use_tls13, use_iovec, use_throughput); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
END_TEST(); |
| 392 |
} |