| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <fcntl.h> |
| 17 |
#include <signal.h> |
| 18 |
#include <stdint.h> |
| 19 |
#include <sys/wait.h> |
| 20 |
#include <unistd.h> |
| 21 |
|
| 22 |
#include "api/s2n.h" |
| 23 |
#include "s2n_test.h" |
| 24 |
#include "testlib/s2n_testlib.h" |
| 25 |
|
| 26 |
#define TLS_ALERT 21 |
| 27 |
#define TLS_ALERT_VERSION 0x03, 0x03 |
| 28 |
#define TLS_ALERT_LENGTH 0x00, 0x02 |
| 29 |
|
| 30 |
#define TLS_ALERT_LEVEL_WARNING 1 |
| 31 |
#define TLS_ALERT_LEVEL_FATAL 2 |
| 32 |
|
| 33 |
#define TLS_ALERT_CLOSE_NOTIFY 0 |
| 34 |
#define TLS_ALERT_UNRECOGNIZED_NAME 122 |
| 35 |
|
| 36 |
struct alert_ctx { |
| 37 |
int write_fd; |
| 38 |
int invoked; |
| 39 |
int count; |
| 40 |
|
| 41 |
uint8_t level; |
| 42 |
uint8_t code; |
| 43 |
}; |
| 44 |
|
| 45 |
int mock_client(struct s2n_test_io_pair *io_pair, s2n_alert_behavior alert_behavior, int expect_failure) |
| 46 |
{ |
| 47 |
struct s2n_connection *conn = NULL; |
| 48 |
struct s2n_config *config = NULL; |
| 49 |
s2n_blocked_status blocked; |
| 50 |
int result = 0; |
| 51 |
int rc = 0; |
| 52 |
|
| 53 |
|
| 54 |
sleep(1); |
| 55 |
|
| 56 |
conn = s2n_connection_new(S2N_CLIENT); |
| 57 |
config = s2n_config_new(); |
| 58 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501")); |
| 59 |
s2n_config_disable_x509_verification(config); |
| 60 |
s2n_config_set_alert_behavior(config, alert_behavior); |
| 61 |
s2n_connection_set_config(conn, config); |
| 62 |
|
| 63 |
s2n_connection_set_io_pair(conn, io_pair); |
| 64 |
|
| 65 |
rc = s2n_negotiate(conn, &blocked); |
| 66 |
if (expect_failure) { |
| 67 |
if (!rc) { |
| 68 |
result = 1; |
| 69 |
} |
| 70 |
} else { |
| 71 |
char buffer[0xffff]; |
| 72 |
if (rc < 0) { |
| 73 |
result = 1; |
| 74 |
} |
| 75 |
|
| 76 |
for (size_t i = 1; i < 0xffff; i += 100) { |
| 77 |
memset(buffer, 33, sizeof(char) * i); |
| 78 |
s2n_send(conn, buffer, i, &blocked); |
| 79 |
} |
| 80 |
|
| 81 |
int shutdown_rc = -1; |
| 82 |
do { |
| 83 |
shutdown_rc = s2n_shutdown(conn, &blocked); |
| 84 |
} while (shutdown_rc != 0); |
| 85 |
} |
| 86 |
|
| 87 |
s2n_connection_free(conn); |
| 88 |
s2n_config_free(config); |
| 89 |
|
| 90 |
s2n_io_pair_close_one_end(io_pair, S2N_CLIENT); |
| 91 |
|
| 92 |
s2n_cleanup(); |
| 93 |
|
| 94 |
exit(result); |
| 95 |
} |
| 96 |
|
| 97 |
int mock_nanoseconds_since_epoch(void *data, uint64_t *nanoseconds) |
| 98 |
{ |
| 99 |
static int called = 0; |
| 100 |
|
| 101 |
|
| 102 |
*nanoseconds = 0; |
| 103 |
|
| 104 |
|
| 105 |
if (called) { |
| 106 |
*nanoseconds += (uint64_t) 31 * 1000000000; |
| 107 |
} |
| 108 |
|
| 109 |
called = 1; |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
int client_hello_send_alerts(struct s2n_connection *conn, void *ctx) |
| 115 |
{ |
| 116 |
struct alert_ctx *alert = ctx; |
| 117 |
uint8_t alert_msg[] = { TLS_ALERT, TLS_ALERT_VERSION, TLS_ALERT_LENGTH, alert->level, alert->code }; |
| 118 |
|
| 119 |
for (int i = 0; i < alert->count; i++) { |
| 120 |
if (write(alert->write_fd, alert_msg, sizeof(alert_msg)) != sizeof(alert_msg)) { |
| 121 |
exit(100); |
| 122 |
} |
| 123 |
|
| 124 |
alert->invoked++; |
| 125 |
} |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
S2N_RESULT cleanup(char **cert_chain_pem, char **private_key_pem, |
| 131 |
struct s2n_cert_chain_and_key **chain_and_key) |
| 132 |
{ |
| 133 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(*chain_and_key)); |
| 134 |
free(*cert_chain_pem); |
| 135 |
free(*private_key_pem); |
| 136 |
|
| 137 |
return S2N_RESULT_OK; |
| 138 |
} |
| 139 |
|
| 140 |
int main(int argc, char **argv) |
| 141 |
{ |
| 142 |
char buffer[0xffff]; |
| 143 |
struct s2n_connection *conn = NULL; |
| 144 |
s2n_blocked_status blocked; |
| 145 |
int status = 0; |
| 146 |
pid_t pid = 0; |
| 147 |
char *cert_chain_pem = NULL; |
| 148 |
char *private_key_pem = NULL; |
| 149 |
struct s2n_cert_chain_and_key *chain_and_key = NULL; |
| 150 |
BEGIN_TEST(); |
| 151 |
|
| 152 |
|
| 153 |
signal(SIGPIPE, SIG_IGN); |
| 154 |
|
| 155 |
EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE)); |
| 156 |
EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE)); |
| 157 |
EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE)); |
| 158 |
EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE)); |
| 159 |
EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new()); |
| 160 |
EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem)); |
| 161 |
|
| 162 |
|
| 163 |
{ |
| 164 |
|
| 165 |
struct s2n_test_io_pair io_pair; |
| 166 |
EXPECT_SUCCESS(s2n_io_pair_init(&io_pair)); |
| 167 |
|
| 168 |
|
| 169 |
pid = fork(); |
| 170 |
if (pid == 0) { |
| 171 |
|
| 172 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 173 |
|
| 174 |
EXPECT_OK(cleanup(&cert_chain_pem, &private_key_pem, &chain_and_key)); |
| 175 |
mock_client(&io_pair, S2N_ALERT_IGNORE_WARNINGS, 0); |
| 176 |
} |
| 177 |
|
| 178 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), |
| 179 |
s2n_config_ptr_free); |
| 180 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key)); |
| 181 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501")); |
| 182 |
|
| 183 |
|
| 184 |
struct alert_ctx warning_alert = { .write_fd = io_pair.server, .invoked = 0, .count = 2, .level = TLS_ALERT_LEVEL_WARNING, .code = TLS_ALERT_UNRECOGNIZED_NAME }; |
| 185 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, client_hello_send_alerts, &warning_alert)); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT)); |
| 190 |
|
| 191 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 192 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 193 |
|
| 194 |
|
| 195 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(conn, &io_pair)); |
| 196 |
|
| 197 |
|
| 198 |
EXPECT_SUCCESS(s2n_negotiate(conn, &blocked)); |
| 199 |
EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS12); |
| 200 |
|
| 201 |
|
| 202 |
EXPECT_EQUAL(warning_alert.invoked, 2); |
| 203 |
|
| 204 |
for (size_t i = 1; i < 0xffff; i += 100) { |
| 205 |
char *ptr = buffer; |
| 206 |
int size = i; |
| 207 |
|
| 208 |
do { |
| 209 |
int bytes_read = 0; |
| 210 |
EXPECT_SUCCESS(bytes_read = s2n_recv(conn, ptr, size, &blocked)); |
| 211 |
|
| 212 |
size -= bytes_read; |
| 213 |
ptr += bytes_read; |
| 214 |
} while (size); |
| 215 |
|
| 216 |
for (int j = 0; j < i; j++) { |
| 217 |
EXPECT_EQUAL(buffer[j], 33); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked)); |
| 222 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 223 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 224 |
|
| 225 |
|
| 226 |
EXPECT_EQUAL(waitpid(-1, &status, 0), pid); |
| 227 |
EXPECT_EQUAL(status, 0); |
| 228 |
}; |
| 229 |
|
| 230 |
|
| 231 |
{ |
| 232 |
|
| 233 |
struct s2n_test_io_pair io_pair; |
| 234 |
EXPECT_SUCCESS(s2n_io_pair_init(&io_pair)); |
| 235 |
|
| 236 |
|
| 237 |
pid = fork(); |
| 238 |
if (pid == 0) { |
| 239 |
|
| 240 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 241 |
|
| 242 |
EXPECT_OK(cleanup(&cert_chain_pem, &private_key_pem, &chain_and_key)); |
| 243 |
mock_client(&io_pair, S2N_ALERT_IGNORE_WARNINGS, 1); |
| 244 |
} |
| 245 |
|
| 246 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), |
| 247 |
s2n_config_ptr_free); |
| 248 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key)); |
| 249 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501")); |
| 250 |
|
| 251 |
|
| 252 |
struct alert_ctx fatal_alert = { .write_fd = io_pair.server, .invoked = 0, .count = 1, .level = TLS_ALERT_LEVEL_FATAL, .code = TLS_ALERT_UNRECOGNIZED_NAME }; |
| 253 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, client_hello_send_alerts, &fatal_alert)); |
| 254 |
|
| 255 |
|
| 256 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT)); |
| 257 |
|
| 258 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 259 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 260 |
|
| 261 |
|
| 262 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(conn, &io_pair)); |
| 263 |
|
| 264 |
|
| 265 |
EXPECT_FAILURE(s2n_negotiate(conn, &blocked)); |
| 266 |
EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS12); |
| 267 |
|
| 268 |
|
| 269 |
EXPECT_EQUAL(fatal_alert.invoked, 1); |
| 270 |
|
| 271 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 272 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 273 |
|
| 274 |
|
| 275 |
EXPECT_EQUAL(waitpid(-1, &status, 0), pid); |
| 276 |
EXPECT_EQUAL(status, 0); |
| 277 |
}; |
| 278 |
|
| 279 |
|
| 280 |
{ |
| 281 |
|
| 282 |
struct s2n_test_io_pair io_pair; |
| 283 |
EXPECT_SUCCESS(s2n_io_pair_init(&io_pair)); |
| 284 |
|
| 285 |
|
| 286 |
pid = fork(); |
| 287 |
if (pid == 0) { |
| 288 |
|
| 289 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 290 |
|
| 291 |
EXPECT_OK(cleanup(&cert_chain_pem, &private_key_pem, &chain_and_key)); |
| 292 |
mock_client(&io_pair, S2N_ALERT_FAIL_ON_WARNINGS, 1); |
| 293 |
} |
| 294 |
|
| 295 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), |
| 296 |
s2n_config_ptr_free); |
| 297 |
|
| 298 |
|
| 299 |
struct alert_ctx warning_alert = { .write_fd = io_pair.server, .invoked = 0, .count = 1, .level = TLS_ALERT_LEVEL_WARNING, .code = TLS_ALERT_UNRECOGNIZED_NAME }; |
| 300 |
EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, client_hello_send_alerts, &warning_alert)); |
| 301 |
|
| 302 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key)); |
| 303 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501")); |
| 304 |
|
| 305 |
|
| 306 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT)); |
| 307 |
|
| 308 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 309 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 310 |
|
| 311 |
|
| 312 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(conn, &io_pair)); |
| 313 |
|
| 314 |
|
| 315 |
EXPECT_FAILURE(s2n_negotiate(conn, &blocked)); |
| 316 |
EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS12); |
| 317 |
|
| 318 |
|
| 319 |
EXPECT_EQUAL(warning_alert.invoked, 1); |
| 320 |
|
| 321 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 322 |
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER)); |
| 323 |
|
| 324 |
|
| 325 |
EXPECT_EQUAL(waitpid(-1, &status, 0), pid); |
| 326 |
EXPECT_EQUAL(status, 0); |
| 327 |
}; |
| 328 |
|
| 329 |
|
| 330 |
EXPECT_OK(cleanup(&cert_chain_pem, &private_key_pem, &chain_and_key)); |
| 331 |
|
| 332 |
END_TEST(); |
| 333 |
|
| 334 |
return 0; |
| 335 |
} |