| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/s2n_alerts.h" |
| 17 |
|
| 18 |
#include "s2n_test.h" |
| 19 |
#include "testlib/s2n_testlib.h" |
| 20 |
#include "tls/s2n_quic_support.h" |
| 21 |
|
| 22 |
#define ALERT_LEN (sizeof(uint16_t)) |
| 23 |
|
| 24 |
int s2n_flush(struct s2n_connection *conn, s2n_blocked_status *blocked); |
| 25 |
|
| 26 |
int main(int argc, char **argv) |
| 27 |
{ |
| 28 |
BEGIN_TEST(); |
| 29 |
|
| 30 |
|
| 31 |
{ |
| 32 |
uint8_t alert = 0; |
| 33 |
|
| 34 |
|
| 35 |
EXPECT_FAILURE_WITH_ERRNO(s2n_error_get_alert(S2N_ERR_OK, &alert), S2N_ERR_NO_ALERT); |
| 36 |
|
| 37 |
|
| 38 |
EXPECT_FAILURE_WITH_ERRNO(s2n_error_get_alert(S2N_ERR_CLOSED, &alert), S2N_ERR_NO_ALERT); |
| 39 |
|
| 40 |
|
| 41 |
EXPECT_FAILURE_WITH_ERRNO(s2n_error_get_alert(S2N_ERR_ALERT, &alert), S2N_ERR_NO_ALERT); |
| 42 |
|
| 43 |
|
| 44 |
for (size_t i = S2N_ERR_T_BLOCKED_START; i < S2N_ERR_T_BLOCKED_END; i++) { |
| 45 |
EXPECT_FAILURE_WITH_ERRNO(s2n_error_get_alert(i, &alert), S2N_ERR_NO_ALERT); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
for (size_t i = S2N_ERR_T_USAGE_START; i < S2N_ERR_T_USAGE_END; i++) { |
| 50 |
EXPECT_FAILURE_WITH_ERRNO(s2n_error_get_alert(i, &alert), S2N_ERR_NO_ALERT); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
{ |
| 55 |
|
| 56 |
for (size_t i = S2N_ERR_T_PROTO_START; i < S2N_ERR_T_PROTO_END; i++) { |
| 57 |
int ret_val = s2n_error_get_alert(i, &alert); |
| 58 |
if (ret_val != S2N_SUCCESS && s2n_errno == S2N_ERR_UNIMPLEMENTED) { |
| 59 |
fprintf(stdout, "\n\nNo alert mapping for protocol error %s\n\n", s2n_strerror_name(i)); |
| 60 |
FAIL_MSG("Missing alert mapping for protocol error."); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
{ |
| 66 |
EXPECT_SUCCESS(s2n_error_get_alert(S2N_ERR_MISSING_EXTENSION, &alert)); |
| 67 |
EXPECT_EQUAL(S2N_TLS_ALERT_MISSING_EXTENSION, alert); |
| 68 |
|
| 69 |
EXPECT_SUCCESS(s2n_error_get_alert(S2N_ERR_BAD_MESSAGE, &alert)); |
| 70 |
EXPECT_EQUAL(S2N_TLS_ALERT_UNEXPECTED_MESSAGE, alert); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
EXPECT_FAILURE_WITH_ERRNO(s2n_error_get_alert(S2N_ERR_EARLY_DATA_TRIAL_DECRYPT, &alert), S2N_ERR_NO_ALERT); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
{ |
| 79 |
EXPECT_SUCCESS(s2n_error_get_alert(S2N_ERR_IO, &alert)); |
| 80 |
EXPECT_EQUAL(alert, S2N_TLS_ALERT_INTERNAL_ERROR); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
for (size_t i = S2N_ERR_T_INTERNAL_START; i < S2N_ERR_T_INTERNAL_END; i++) { |
| 85 |
EXPECT_SUCCESS(s2n_error_get_alert(i, &alert)); |
| 86 |
EXPECT_EQUAL(alert, S2N_TLS_ALERT_INTERNAL_ERROR); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
{ |
| 92 |
const uint8_t close_notify_alert[] = { |
| 93 |
2 , |
| 94 |
0 |
| 95 |
}; |
| 96 |
|
| 97 |
const uint8_t not_close_notify_alert[] = { |
| 98 |
2 , |
| 99 |
10 |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
{ |
| 104 |
struct s2n_connection *conn = NULL; |
| 105 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 106 |
|
| 107 |
|
| 108 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 109 |
|
| 110 |
|
| 111 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, not_close_notify_alert, sizeof(not_close_notify_alert))); |
| 112 |
|
| 113 |
|
| 114 |
EXPECT_FAILURE_WITH_ERRNO(s2n_process_alert_fragment(conn), S2N_ERR_ALERT); |
| 115 |
|
| 116 |
|
| 117 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 118 |
|
| 119 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
{ |
| 124 |
struct s2n_connection *conn = NULL; |
| 125 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 126 |
|
| 127 |
|
| 128 |
EXPECT_FALSE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 129 |
|
| 130 |
|
| 131 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, close_notify_alert, sizeof(close_notify_alert))); |
| 132 |
EXPECT_SUCCESS(s2n_process_alert_fragment(conn)); |
| 133 |
|
| 134 |
|
| 135 |
EXPECT_TRUE(s2n_atomic_flag_test(&conn->close_notify_received)); |
| 136 |
|
| 137 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
{ |
| 143 |
|
| 144 |
EXPECT_FAILURE_WITH_ERRNO(s2n_process_alert_fragment(NULL), S2N_ERR_NULL); |
| 145 |
|
| 146 |
|
| 147 |
if (s2n_is_tls13_fully_supported()) { |
| 148 |
struct s2n_config *config = NULL; |
| 149 |
EXPECT_NOT_NULL(config = s2n_config_new()); |
| 150 |
|
| 151 |
struct s2n_connection *conn = NULL; |
| 152 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 153 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 154 |
|
| 155 |
|
| 156 |
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&conn->in, 0)); |
| 157 |
EXPECT_SUCCESS(s2n_process_alert_fragment(conn)); |
| 158 |
|
| 159 |
|
| 160 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->alert_in)); |
| 161 |
|
| 162 |
|
| 163 |
EXPECT_SUCCESS(s2n_config_enable_quic(config)); |
| 164 |
EXPECT_SUCCESS(s2n_stuffer_write_uint16(&conn->in, 0)); |
| 165 |
EXPECT_FAILURE_WITH_ERRNO(s2n_process_alert_fragment(conn), S2N_ERR_BAD_MESSAGE); |
| 166 |
|
| 167 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 168 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
{ |
| 173 |
const uint8_t warning_alert[] = { |
| 174 |
1 , |
| 175 |
70 |
| 176 |
}; |
| 177 |
|
| 178 |
const uint8_t user_canceled_alert[] = { |
| 179 |
1 , |
| 180 |
90 |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
{ |
| 185 |
struct s2n_connection *conn = NULL; |
| 186 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 187 |
EXPECT_EQUAL(conn->config->alert_behavior, S2N_ALERT_FAIL_ON_WARNINGS); |
| 188 |
conn->actual_protocol_version = S2N_TLS12; |
| 189 |
conn->actual_protocol_version_established = true; |
| 190 |
|
| 191 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, warning_alert, sizeof(warning_alert))); |
| 192 |
|
| 193 |
EXPECT_FAILURE_WITH_ERRNO(s2n_process_alert_fragment(conn), S2N_ERR_ALERT); |
| 194 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 195 |
|
| 196 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
{ |
| 201 |
struct s2n_connection *conn = NULL; |
| 202 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 203 |
EXPECT_EQUAL(conn->config->alert_behavior, S2N_ALERT_FAIL_ON_WARNINGS); |
| 204 |
conn->actual_protocol_version = S2N_TLS13; |
| 205 |
conn->actual_protocol_version_established = true; |
| 206 |
|
| 207 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, warning_alert, sizeof(warning_alert))); |
| 208 |
|
| 209 |
EXPECT_FAILURE_WITH_ERRNO(s2n_process_alert_fragment(conn), S2N_ERR_ALERT); |
| 210 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 211 |
|
| 212 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
{ |
| 217 |
struct s2n_config *config = NULL; |
| 218 |
EXPECT_NOT_NULL(config = s2n_config_new()); |
| 219 |
EXPECT_SUCCESS(s2n_config_set_alert_behavior(config, S2N_ALERT_IGNORE_WARNINGS)); |
| 220 |
|
| 221 |
struct s2n_connection *conn = NULL; |
| 222 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 223 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 224 |
conn->actual_protocol_version = S2N_TLS12; |
| 225 |
conn->actual_protocol_version_established = true; |
| 226 |
|
| 227 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, warning_alert, sizeof(warning_alert))); |
| 228 |
|
| 229 |
EXPECT_SUCCESS(s2n_process_alert_fragment(conn)); |
| 230 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 231 |
|
| 232 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 233 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
{ |
| 238 |
struct s2n_config *config = NULL; |
| 239 |
EXPECT_NOT_NULL(config = s2n_config_new()); |
| 240 |
EXPECT_SUCCESS(s2n_config_set_alert_behavior(config, S2N_ALERT_IGNORE_WARNINGS)); |
| 241 |
|
| 242 |
struct s2n_connection *conn = NULL; |
| 243 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 244 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 245 |
conn->actual_protocol_version = S2N_TLS13; |
| 246 |
conn->actual_protocol_version_established = true; |
| 247 |
|
| 248 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, warning_alert, sizeof(warning_alert))); |
| 249 |
|
| 250 |
EXPECT_FAILURE_WITH_ERRNO(s2n_process_alert_fragment(conn), S2N_ERR_ALERT); |
| 251 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_CLOSED)); |
| 252 |
|
| 253 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 254 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
{ |
| 259 |
struct s2n_config *config = NULL; |
| 260 |
EXPECT_NOT_NULL(config = s2n_config_new()); |
| 261 |
EXPECT_SUCCESS(s2n_config_set_alert_behavior(config, S2N_ALERT_IGNORE_WARNINGS)); |
| 262 |
|
| 263 |
struct s2n_connection *conn = NULL; |
| 264 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 265 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 266 |
conn->actual_protocol_version = S2N_TLS12; |
| 267 |
conn->actual_protocol_version_established = false; |
| 268 |
|
| 269 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, warning_alert, sizeof(warning_alert))); |
| 270 |
|
| 271 |
EXPECT_SUCCESS(s2n_process_alert_fragment(conn)); |
| 272 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 273 |
|
| 274 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 275 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
{ |
| 280 |
struct s2n_config *config = NULL; |
| 281 |
EXPECT_NOT_NULL(config = s2n_config_new()); |
| 282 |
EXPECT_SUCCESS(s2n_config_set_alert_behavior(config, S2N_ALERT_IGNORE_WARNINGS)); |
| 283 |
|
| 284 |
struct s2n_connection *conn = NULL; |
| 285 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 286 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 287 |
conn->actual_protocol_version = S2N_TLS13; |
| 288 |
conn->actual_protocol_version_established = false; |
| 289 |
|
| 290 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, warning_alert, sizeof(warning_alert))); |
| 291 |
|
| 292 |
EXPECT_SUCCESS(s2n_process_alert_fragment(conn)); |
| 293 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 294 |
|
| 295 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 296 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
{ |
| 301 |
struct s2n_config *config = NULL; |
| 302 |
EXPECT_NOT_NULL(config = s2n_config_new()); |
| 303 |
|
| 304 |
struct s2n_connection *conn = NULL; |
| 305 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 306 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 307 |
conn->actual_protocol_version = S2N_TLS13; |
| 308 |
conn->actual_protocol_version_established = true; |
| 309 |
|
| 310 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->in, user_canceled_alert, sizeof(user_canceled_alert))); |
| 311 |
|
| 312 |
EXPECT_SUCCESS(s2n_process_alert_fragment(conn)); |
| 313 |
|
| 314 |
|
| 315 |
EXPECT_TRUE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX)); |
| 316 |
|
| 317 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 318 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
{ |
| 325 |
|
| 326 |
EXPECT_FAILURE_WITH_ERRNO(s2n_queue_reader_unsupported_protocol_version_alert(NULL), |
| 327 |
S2N_ERR_NULL); |
| 328 |
EXPECT_FAILURE_WITH_ERRNO(s2n_queue_reader_handshake_failure_alert(NULL), |
| 329 |
S2N_ERR_NULL); |
| 330 |
|
| 331 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 332 |
s2n_connection_ptr_free); |
| 333 |
EXPECT_NOT_NULL(conn); |
| 334 |
|
| 335 |
|
| 336 |
EXPECT_SUCCESS(s2n_queue_reader_unsupported_protocol_version_alert(conn)); |
| 337 |
EXPECT_EQUAL(conn->reader_alert_out, S2N_TLS_ALERT_PROTOCOL_VERSION); |
| 338 |
|
| 339 |
|
| 340 |
EXPECT_SUCCESS(s2n_queue_reader_handshake_failure_alert(conn)); |
| 341 |
EXPECT_EQUAL(conn->reader_alert_out, S2N_TLS_ALERT_PROTOCOL_VERSION); |
| 342 |
|
| 343 |
|
| 344 |
conn->reader_alert_out = 0; |
| 345 |
EXPECT_SUCCESS(s2n_queue_reader_handshake_failure_alert(conn)); |
| 346 |
EXPECT_EQUAL(conn->reader_alert_out, S2N_TLS_ALERT_HANDSHAKE_FAILURE); |
| 347 |
}; |
| 348 |
|
| 349 |
|
| 350 |
{ |
| 351 |
const uint8_t expected_alert = S2N_TLS_ALERT_INTERNAL_ERROR; |
| 352 |
const uint8_t wrong_alert = S2N_TLS_ALERT_CERTIFICATE_UNKNOWN; |
| 353 |
|
| 354 |
|
| 355 |
{ |
| 356 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 357 |
s2n_connection_ptr_free); |
| 358 |
EXPECT_NOT_NULL(conn); |
| 359 |
|
| 360 |
EXPECT_OK(s2n_alerts_write_error_or_close_notify(conn)); |
| 361 |
|
| 362 |
|
| 363 |
uint8_t level = 0, code = 0; |
| 364 |
EXPECT_SUCCESS(s2n_stuffer_skip_read(&conn->out, S2N_TLS_RECORD_HEADER_LENGTH)); |
| 365 |
EXPECT_SUCCESS(s2n_stuffer_read_uint8(&conn->out, &level)); |
| 366 |
EXPECT_EQUAL(level, S2N_TLS_ALERT_LEVEL_WARNING); |
| 367 |
EXPECT_SUCCESS(s2n_stuffer_read_uint8(&conn->out, &code)); |
| 368 |
EXPECT_EQUAL(code, S2N_TLS_ALERT_CLOSE_NOTIFY); |
| 369 |
EXPECT_EQUAL(s2n_stuffer_data_available(&conn->out), 0); |
| 370 |
}; |
| 371 |
|
| 372 |
|
| 373 |
{ |
| 374 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 375 |
s2n_connection_ptr_free); |
| 376 |
EXPECT_NOT_NULL(conn); |
| 377 |
conn->reader_alert_out = expected_alert; |
| 378 |
|
| 379 |
EXPECT_OK(s2n_alerts_write_error_or_close_notify(conn)); |
| 380 |
|
| 381 |
|
| 382 |
uint8_t level = 0, code = 0; |
| 383 |
EXPECT_SUCCESS(s2n_stuffer_skip_read(&conn->out, S2N_TLS_RECORD_HEADER_LENGTH)); |
| 384 |
EXPECT_SUCCESS(s2n_stuffer_read_uint8(&conn->out, &level)); |
| 385 |
EXPECT_EQUAL(level, S2N_TLS_ALERT_LEVEL_FATAL); |
| 386 |
EXPECT_SUCCESS(s2n_stuffer_read_uint8(&conn->out, &code)); |
| 387 |
EXPECT_EQUAL(code, expected_alert); |
| 388 |
EXPECT_EQUAL(s2n_stuffer_data_available(&conn->out), 0); |
| 389 |
}; |
| 390 |
|
| 391 |
|
| 392 |
{ |
| 393 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 394 |
s2n_connection_ptr_free); |
| 395 |
EXPECT_NOT_NULL(conn); |
| 396 |
conn->writer_alert_out = expected_alert; |
| 397 |
conn->reader_alert_out = wrong_alert; |
| 398 |
|
| 399 |
EXPECT_OK(s2n_alerts_write_error_or_close_notify(conn)); |
| 400 |
|
| 401 |
|
| 402 |
uint8_t level = 0, code = 0; |
| 403 |
EXPECT_SUCCESS(s2n_stuffer_skip_read(&conn->out, S2N_TLS_RECORD_HEADER_LENGTH)); |
| 404 |
EXPECT_SUCCESS(s2n_stuffer_read_uint8(&conn->out, &level)); |
| 405 |
EXPECT_EQUAL(level, S2N_TLS_ALERT_LEVEL_FATAL); |
| 406 |
EXPECT_SUCCESS(s2n_stuffer_read_uint8(&conn->out, &code)); |
| 407 |
EXPECT_EQUAL(code, expected_alert); |
| 408 |
EXPECT_EQUAL(s2n_stuffer_data_available(&conn->out), 0); |
| 409 |
}; |
| 410 |
|
| 411 |
|
| 412 |
{ |
| 413 |
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), |
| 414 |
s2n_connection_ptr_free); |
| 415 |
EXPECT_NOT_NULL(conn); |
| 416 |
conn->quic_enabled = true; |
| 417 |
conn->writer_alert_out = expected_alert; |
| 418 |
conn->reader_alert_out = wrong_alert; |
| 419 |
|
| 420 |
EXPECT_OK(s2n_alerts_write_error_or_close_notify(conn)); |
| 421 |
EXPECT_EQUAL(s2n_stuffer_data_available(&conn->out), 0); |
| 422 |
}; |
| 423 |
} |
| 424 |
|
| 425 |
END_TEST(); |
| 426 |
} |