| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "s2n_test.h" |
| 17 |
#include "testlib/s2n_testlib.h" |
| 18 |
|
| 19 |
|
| 20 |
#include "tls/extensions/s2n_client_psk.c" |
| 21 |
|
| 22 |
#define TEST_BYTES 0x01, 0xFF, 0x23 |
| 23 |
#define TEST_BYTES_SIZE 0x00, 0x03 |
| 24 |
|
| 25 |
#define TEST_BYTES_2 0x0A, 0x0B, 0x0C |
| 26 |
#define TEST_BYTES_SIZE_2 0x00, 0x03 |
| 27 |
|
| 28 |
#define MILLIS_TO_NANOS(millis) (millis * (uint64_t) ONE_MILLISEC_IN_NANOS) |
| 29 |
|
| 30 |
#define BINDER_SIZE 32 |
| 31 |
|
| 32 |
struct s2n_psk_test_case { |
| 33 |
s2n_hmac_algorithm hmac_alg; |
| 34 |
uint8_t hash_size; |
| 35 |
const uint8_t *identity; |
| 36 |
size_t identity_size; |
| 37 |
}; |
| 38 |
|
| 39 |
static int s2n_test_select_psk_identity_callback(struct s2n_connection *conn, void *context, |
| 40 |
struct s2n_offered_psk_list *psk_identity_list) |
| 41 |
{ |
| 42 |
uint16_t *wire_index_choice = (uint16_t *) context; |
| 43 |
|
| 44 |
struct s2n_offered_psk offered_psk = { 0 }; |
| 45 |
uint16_t idx = 0; |
| 46 |
while (s2n_offered_psk_list_has_next(psk_identity_list)) { |
| 47 |
POSIX_GUARD(s2n_offered_psk_list_next(psk_identity_list, &offered_psk)); |
| 48 |
if (idx == *wire_index_choice) { |
| 49 |
POSIX_GUARD(s2n_offered_psk_list_choose_psk(psk_identity_list, &offered_psk)); |
| 50 |
break; |
| 51 |
} |
| 52 |
idx++; |
| 53 |
}; |
| 54 |
return S2N_SUCCESS; |
| 55 |
} |
| 56 |
|
| 57 |
static int s2n_test_error_select_psk_identity_callback(struct s2n_connection *conn, void *context, |
| 58 |
struct s2n_offered_psk_list *psk_identity_list) |
| 59 |
{ |
| 60 |
POSIX_BAIL(S2N_ERR_UNIMPLEMENTED); |
| 61 |
} |
| 62 |
|
| 63 |
static S2N_RESULT s2n_write_test_identity(struct s2n_stuffer *out, struct s2n_blob *identity) |
| 64 |
{ |
| 65 |
RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(out, identity->size)); |
| 66 |
RESULT_GUARD_POSIX(s2n_stuffer_write(out, identity)); |
| 67 |
RESULT_GUARD_POSIX(s2n_stuffer_write_uint32(out, 0)); |
| 68 |
return S2N_RESULT_OK; |
| 69 |
} |
| 70 |
|
| 71 |
static int mock_time(void *data, uint64_t *nanoseconds) |
| 72 |
{ |
| 73 |
*nanoseconds = MILLIS_TO_NANOS(500); |
| 74 |
return S2N_SUCCESS; |
| 75 |
} |
| 76 |
|
| 77 |
static S2N_RESULT s2n_setup_encrypted_ticket(struct s2n_connection *conn, struct s2n_stuffer *output) |
| 78 |
{ |
| 79 |
conn->tls13_ticket_fields = (struct s2n_ticket_fields){ 0 }; |
| 80 |
uint8_t test_secret_data[] = "test secret"; |
| 81 |
RESULT_GUARD_POSIX(s2n_alloc(&conn->tls13_ticket_fields.session_secret, sizeof(test_secret_data))); |
| 82 |
RESULT_CHECKED_MEMCPY(conn->tls13_ticket_fields.session_secret.data, test_secret_data, sizeof(test_secret_data)); |
| 83 |
|
| 84 |
struct s2n_ticket_key *key = s2n_get_ticket_encrypt_decrypt_key(conn->config); |
| 85 |
RESULT_ENSURE(key != NULL, S2N_ERR_NO_TICKET_ENCRYPT_DECRYPT_KEY); |
| 86 |
|
| 87 |
|
| 88 |
RESULT_GUARD(s2n_resume_encrypt_session_ticket(conn, key, output)); |
| 89 |
|
| 90 |
output->blob.size = s2n_stuffer_data_available(output); |
| 91 |
|
| 92 |
return S2N_RESULT_OK; |
| 93 |
} |
| 94 |
|
| 95 |
int main(int argc, char **argv) |
| 96 |
{ |
| 97 |
BEGIN_TEST(); |
| 98 |
|
| 99 |
uint8_t test_bytes_data[] = { TEST_BYTES }; |
| 100 |
uint8_t test_bytes_data_2[] = { TEST_BYTES_2 }; |
| 101 |
uint8_t test_identity[] = "test identity"; |
| 102 |
uint8_t test_identity_2[] = "another identity"; |
| 103 |
uint8_t test_identity_3[] = "identity 3"; |
| 104 |
uint8_t test_secret[] = "test secret"; |
| 105 |
|
| 106 |
uint8_t single_wire_identity[] = { |
| 107 |
TEST_BYTES_SIZE, |
| 108 |
TEST_BYTES, |
| 109 |
0x00, 0x00, 0x00, 0x00, |
| 110 |
}; |
| 111 |
|
| 112 |
uint8_t wire_identities[] = { |
| 113 |
0x00, 0x01, |
| 114 |
0x01, |
| 115 |
0x00, 0x00, 0x00, 0x00, |
| 116 |
|
| 117 |
0x00, 0x01, |
| 118 |
0xFF, |
| 119 |
0x00, 0x00, 0x00, 0x00, |
| 120 |
|
| 121 |
TEST_BYTES_SIZE, |
| 122 |
TEST_BYTES, |
| 123 |
0x00, 0x00, 0x00, 0x00, |
| 124 |
|
| 125 |
TEST_BYTES_SIZE, |
| 126 |
TEST_BYTES, |
| 127 |
0x00, 0x00, 0x00, 0x00, |
| 128 |
|
| 129 |
0x00, 0x02, |
| 130 |
0x00, 0x01, |
| 131 |
0x00, 0x00, 0x00, 0x00, |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
{ |
| 136 |
struct s2n_connection *conn = NULL; |
| 137 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 138 |
|
| 139 |
|
| 140 |
conn->early_data_state = S2N_EARLY_DATA_NOT_REQUESTED; |
| 141 |
EXPECT_SUCCESS(s2n_client_psk_extension.if_missing(conn)); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
conn->early_data_state = S2N_EARLY_DATA_REQUESTED; |
| 152 |
EXPECT_FAILURE_WITH_ERRNO(s2n_client_psk_extension.if_missing(conn), S2N_ERR_UNSUPPORTED_EXTENSION); |
| 153 |
|
| 154 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
{ |
| 159 |
struct s2n_psk *psk = NULL; |
| 160 |
|
| 161 |
struct s2n_connection *conn = NULL; |
| 162 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 163 |
|
| 164 |
EXPECT_FALSE(s2n_client_psk_extension.should_send(NULL)); |
| 165 |
|
| 166 |
conn->actual_protocol_version = S2N_TLS12; |
| 167 |
EXPECT_FALSE(s2n_client_psk_extension.should_send(conn)); |
| 168 |
conn->actual_protocol_version = S2N_TLS13; |
| 169 |
EXPECT_FALSE(s2n_client_psk_extension.should_send(conn)); |
| 170 |
|
| 171 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk)); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
conn->actual_protocol_version = S2N_TLS12; |
| 177 |
EXPECT_SUCCESS(s2n_extension_send(&s2n_client_psk_extension, conn, NULL)); |
| 178 |
conn->actual_protocol_version = S2N_TLS13; |
| 179 |
EXPECT_FAILURE(s2n_extension_send(&s2n_client_psk_extension, conn, NULL)); |
| 180 |
|
| 181 |
|
| 182 |
{ |
| 183 |
conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256; |
| 184 |
const s2n_hmac_algorithm matching_hmac_alg = conn->secure->cipher_suite->prf_alg; |
| 185 |
const s2n_hmac_algorithm different_hmac_alg = conn->secure->cipher_suite->prf_alg + 1; |
| 186 |
|
| 187 |
|
| 188 |
conn->handshake.handshake_type = INITIAL; |
| 189 |
psk->hmac_alg = different_hmac_alg; |
| 190 |
EXPECT_TRUE(s2n_client_psk_extension.should_send(conn)); |
| 191 |
|
| 192 |
|
| 193 |
conn->handshake.handshake_type = HELLO_RETRY_REQUEST; |
| 194 |
psk->hmac_alg = matching_hmac_alg; |
| 195 |
EXPECT_TRUE(s2n_client_psk_extension.should_send(conn)); |
| 196 |
|
| 197 |
|
| 198 |
conn->handshake.handshake_type = HELLO_RETRY_REQUEST; |
| 199 |
psk->hmac_alg = different_hmac_alg; |
| 200 |
EXPECT_FALSE(s2n_client_psk_extension.should_send(conn)); |
| 201 |
|
| 202 |
|
| 203 |
conn->handshake.handshake_type = HELLO_RETRY_REQUEST; |
| 204 |
psk->hmac_alg = different_hmac_alg; |
| 205 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk)); |
| 206 |
psk->hmac_alg = matching_hmac_alg; |
| 207 |
EXPECT_TRUE(s2n_client_psk_extension.should_send(conn)); |
| 208 |
}; |
| 209 |
|
| 210 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
{ |
| 215 |
uint32_t output = 0; |
| 216 |
uint64_t current_time = 0; |
| 217 |
struct s2n_psk psk = { 0 }; |
| 218 |
psk.type = S2N_PSK_TYPE_RESUMPTION; |
| 219 |
|
| 220 |
|
| 221 |
{ |
| 222 |
current_time = 0; |
| 223 |
psk.ticket_issue_time = 10; |
| 224 |
EXPECT_ERROR_WITH_ERRNO(s2n_generate_obfuscated_ticket_age(&psk, current_time, &output), S2N_ERR_SAFETY); |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
{ |
| 229 |
current_time = UINT64_MAX; |
| 230 |
psk.ticket_issue_time = 0; |
| 231 |
EXPECT_ERROR_WITH_ERRNO(s2n_generate_obfuscated_ticket_age(&psk, current_time, &output), S2N_ERR_SAFETY); |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
{ |
| 236 |
psk.type = S2N_PSK_TYPE_EXTERNAL; |
| 237 |
EXPECT_OK(s2n_generate_obfuscated_ticket_age(&psk, current_time, &output)); |
| 238 |
EXPECT_EQUAL(output, 0); |
| 239 |
}; |
| 240 |
|
| 241 |
struct { |
| 242 |
uint64_t current_time; |
| 243 |
uint64_t ticket_issue_time; |
| 244 |
uint32_t ticket_age_add; |
| 245 |
uint32_t expected_output; |
| 246 |
} test_cases[] = { |
| 247 |
{ .current_time = MILLIS_TO_NANOS(50), .ticket_issue_time = 0, .ticket_age_add = 50, .expected_output = 100 }, |
| 248 |
{ .current_time = MILLIS_TO_NANOS(500), .ticket_issue_time = 0, .ticket_age_add = 50, .expected_output = 550 }, |
| 249 |
{ .current_time = MILLIS_TO_NANOS(UINT32_MAX), .ticket_issue_time = 0, .ticket_age_add = 1, .expected_output = 0 }, |
| 250 |
{ .current_time = MILLIS_TO_NANOS(UINT32_MAX), .ticket_issue_time = 0, .ticket_age_add = 2, .expected_output = 1 }, |
| 251 |
{ .current_time = MILLIS_TO_NANOS(UINT32_MAX), .ticket_issue_time = 0, .ticket_age_add = 50, .expected_output = 49 }, |
| 252 |
{ .current_time = MILLIS_TO_NANOS(0), .ticket_issue_time = 0, .ticket_age_add = 50, .expected_output = 50 }, |
| 253 |
}; |
| 254 |
|
| 255 |
for (size_t i = 0; i < s2n_array_len(test_cases); i++) { |
| 256 |
output = 0; |
| 257 |
current_time = test_cases[i].current_time; |
| 258 |
psk.ticket_issue_time = test_cases[i].ticket_issue_time; |
| 259 |
psk.ticket_age_add = test_cases[i].ticket_age_add; |
| 260 |
psk.type = S2N_PSK_TYPE_RESUMPTION; |
| 261 |
|
| 262 |
EXPECT_OK(s2n_generate_obfuscated_ticket_age(&psk, current_time, &output)); |
| 263 |
EXPECT_EQUAL(output, test_cases[i].expected_output); |
| 264 |
} |
| 265 |
}; |
| 266 |
|
| 267 |
|
| 268 |
{ |
| 269 |
|
| 270 |
{ |
| 271 |
struct s2n_stuffer out = { 0 }; |
| 272 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0)); |
| 273 |
|
| 274 |
struct s2n_connection *conn = NULL; |
| 275 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 276 |
|
| 277 |
struct s2n_psk *psk = NULL; |
| 278 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk)); |
| 279 |
EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_EXTERNAL)); |
| 280 |
EXPECT_SUCCESS(s2n_psk_set_identity(psk, test_identity, sizeof(test_identity))); |
| 281 |
psk->hmac_alg = S2N_HMAC_SHA384; |
| 282 |
|
| 283 |
EXPECT_SUCCESS(s2n_client_psk_extension.send(conn, &out)); |
| 284 |
|
| 285 |
uint32_t offered_psks_size = 0; |
| 286 |
EXPECT_OK(s2n_psk_parameters_offered_psks_size(&conn->psk_params, &offered_psks_size)); |
| 287 |
EXPECT_EQUAL(offered_psks_size, s2n_stuffer_data_available(&out)); |
| 288 |
|
| 289 |
uint16_t identity_list_size = 0; |
| 290 |
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &identity_list_size)); |
| 291 |
|
| 292 |
uint16_t identity_size = 0; |
| 293 |
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &identity_size)); |
| 294 |
EXPECT_EQUAL(identity_size, sizeof(test_identity)); |
| 295 |
|
| 296 |
uint8_t *identity_data = NULL; |
| 297 |
EXPECT_NOT_NULL(identity_data = s2n_stuffer_raw_read(&out, identity_size)); |
| 298 |
EXPECT_BYTEARRAY_EQUAL(identity_data, test_identity, sizeof(test_identity)); |
| 299 |
|
| 300 |
uint32_t obfuscated_ticket_age = 0; |
| 301 |
EXPECT_SUCCESS(s2n_stuffer_read_uint32(&out, &obfuscated_ticket_age)); |
| 302 |
EXPECT_EQUAL(obfuscated_ticket_age, 0); |
| 303 |
|
| 304 |
EXPECT_EQUAL(s2n_stuffer_data_available(&out), |
| 305 |
SHA384_DIGEST_LENGTH |
| 306 |
+ sizeof(uint8_t) |
| 307 |
+ sizeof(uint16_t)); |
| 308 |
|
| 309 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 310 |
EXPECT_SUCCESS(s2n_stuffer_free(&out)); |
| 311 |
}; |
| 312 |
|
| 313 |
|
| 314 |
{ |
| 315 |
struct s2n_stuffer out = { 0 }; |
| 316 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0)); |
| 317 |
|
| 318 |
struct s2n_connection *conn = NULL; |
| 319 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 320 |
|
| 321 |
struct s2n_psk_test_case test_cases[] = { |
| 322 |
{ .hmac_alg = S2N_HMAC_SHA224, .hash_size = SHA224_DIGEST_LENGTH, .identity = test_identity, .identity_size = sizeof(test_identity) }, |
| 323 |
{ .hmac_alg = S2N_HMAC_SHA384, .hash_size = SHA384_DIGEST_LENGTH, .identity = test_identity_2, .identity_size = sizeof(test_identity_2) }, |
| 324 |
}; |
| 325 |
|
| 326 |
uint16_t binder_list_size = 0; |
| 327 |
for (size_t i = 0; i < s2n_array_len(test_cases); i++) { |
| 328 |
struct s2n_psk *psk = NULL; |
| 329 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk)); |
| 330 |
EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_EXTERNAL)); |
| 331 |
EXPECT_SUCCESS(s2n_psk_set_identity(psk, test_cases[i].identity, test_cases[i].identity_size)); |
| 332 |
psk->hmac_alg = test_cases[i].hmac_alg; |
| 333 |
|
| 334 |
binder_list_size += test_cases[i].hash_size |
| 335 |
+ sizeof(uint8_t) ; |
| 336 |
} |
| 337 |
|
| 338 |
EXPECT_SUCCESS(s2n_client_psk_extension.send(conn, &out)); |
| 339 |
|
| 340 |
uint32_t offered_psks_size = 0; |
| 341 |
EXPECT_OK(s2n_psk_parameters_offered_psks_size(&conn->psk_params, &offered_psks_size)); |
| 342 |
EXPECT_EQUAL(offered_psks_size, s2n_stuffer_data_available(&out)); |
| 343 |
|
| 344 |
uint16_t identity_list_size = 0; |
| 345 |
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &identity_list_size)); |
| 346 |
|
| 347 |
for (size_t i = 0; i < s2n_array_len(test_cases); i++) { |
| 348 |
uint16_t identity_size = 0; |
| 349 |
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &identity_size)); |
| 350 |
EXPECT_EQUAL(identity_size, test_cases[i].identity_size); |
| 351 |
|
| 352 |
uint8_t *identity_data = NULL; |
| 353 |
EXPECT_NOT_NULL(identity_data = s2n_stuffer_raw_read(&out, identity_size)); |
| 354 |
EXPECT_BYTEARRAY_EQUAL(identity_data, test_cases[i].identity, test_cases[i].identity_size); |
| 355 |
|
| 356 |
uint32_t obfuscated_ticket_age = 0; |
| 357 |
EXPECT_SUCCESS(s2n_stuffer_read_uint32(&out, &obfuscated_ticket_age)); |
| 358 |
EXPECT_EQUAL(obfuscated_ticket_age, 0); |
| 359 |
} |
| 360 |
|
| 361 |
EXPECT_EQUAL(s2n_stuffer_data_available(&out), |
| 362 |
binder_list_size |
| 363 |
+ sizeof(uint16_t)); |
| 364 |
|
| 365 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 366 |
EXPECT_SUCCESS(s2n_stuffer_free(&out)); |
| 367 |
}; |
| 368 |
|
| 369 |
|
| 370 |
{ |
| 371 |
struct s2n_stuffer out = { 0 }; |
| 372 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0)); |
| 373 |
|
| 374 |
struct s2n_connection *conn = NULL; |
| 375 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 376 |
|
| 377 |
struct s2n_config *config = s2n_config_new(); |
| 378 |
EXPECT_NOT_NULL(config); |
| 379 |
EXPECT_SUCCESS(s2n_config_set_wall_clock(config, mock_time, NULL)); |
| 380 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 381 |
|
| 382 |
struct s2n_psk *psk = NULL; |
| 383 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk)); |
| 384 |
EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_RESUMPTION)); |
| 385 |
EXPECT_SUCCESS(s2n_psk_set_identity(psk, test_identity, sizeof(test_identity))); |
| 386 |
psk->hmac_alg = S2N_HMAC_SHA384; |
| 387 |
psk->ticket_age_add = 50; |
| 388 |
psk->ticket_issue_time = 0; |
| 389 |
|
| 390 |
EXPECT_SUCCESS(s2n_client_psk_extension.send(conn, &out)); |
| 391 |
|
| 392 |
EXPECT_SUCCESS(s2n_stuffer_skip_read(&out, sizeof(uint16_t) + sizeof(uint16_t) + sizeof(test_identity))); |
| 393 |
|
| 394 |
uint32_t obfuscated_ticket_age = 0; |
| 395 |
EXPECT_SUCCESS(s2n_stuffer_read_uint32(&out, &obfuscated_ticket_age)); |
| 396 |
EXPECT_TRUE(obfuscated_ticket_age == 550); |
| 397 |
|
| 398 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 399 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 400 |
EXPECT_SUCCESS(s2n_stuffer_free(&out)); |
| 401 |
}; |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
{ |
| 413 |
struct s2n_stuffer out = { 0 }; |
| 414 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0)); |
| 415 |
|
| 416 |
const struct s2n_psk_test_case matching_psk = { |
| 417 |
.hmac_alg = S2N_HMAC_SHA384, |
| 418 |
.hash_size = SHA384_DIGEST_LENGTH, |
| 419 |
.identity = test_identity, |
| 420 |
.identity_size = sizeof(test_identity) |
| 421 |
}; |
| 422 |
const struct s2n_psk_test_case non_matching_psk = { |
| 423 |
.hmac_alg = S2N_HMAC_SHA224, |
| 424 |
.hash_size = SHA224_DIGEST_LENGTH, |
| 425 |
.identity = test_identity, |
| 426 |
.identity_size = sizeof(test_identity) |
| 427 |
}; |
| 428 |
struct s2n_psk_test_case test_cases[] = { matching_psk, non_matching_psk }; |
| 429 |
|
| 430 |
struct s2n_connection *conn = NULL; |
| 431 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT)); |
| 432 |
conn->handshake.handshake_type = HELLO_RETRY_REQUEST; |
| 433 |
conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384; |
| 434 |
EXPECT_EQUAL(conn->secure->cipher_suite->prf_alg, matching_psk.hmac_alg); |
| 435 |
|
| 436 |
for (size_t i = 0; i < s2n_array_len(test_cases); i++) { |
| 437 |
struct s2n_psk *psk = NULL; |
| 438 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk)); |
| 439 |
EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_EXTERNAL)); |
| 440 |
EXPECT_SUCCESS(s2n_psk_set_identity(psk, test_cases[i].identity, test_cases[i].identity_size)); |
| 441 |
psk->hmac_alg = test_cases[i].hmac_alg; |
| 442 |
} |
| 443 |
|
| 444 |
EXPECT_SUCCESS(s2n_client_psk_extension.send(conn, &out)); |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
uint16_t identity_list_size = 0; |
| 450 |
uint16_t identity_size = 0; |
| 451 |
uint8_t *identity_data = NULL; |
| 452 |
uint32_t obfuscated_ticket_age = 0; |
| 453 |
|
| 454 |
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &identity_list_size)); |
| 455 |
EXPECT_EQUAL(identity_list_size, sizeof(identity_size) + matching_psk.identity_size + sizeof(obfuscated_ticket_age)); |
| 456 |
|
| 457 |
EXPECT_SUCCESS(s2n_stuffer_read_uint16(&out, &identity_size)); |
| 458 |
EXPECT_EQUAL(identity_size, matching_psk.identity_size); |
| 459 |
EXPECT_NOT_NULL(identity_data = s2n_stuffer_raw_read(&out, identity_size)); |
| 460 |
EXPECT_BYTEARRAY_EQUAL(identity_data, matching_psk.identity, matching_psk.identity_size); |
| 461 |
|
| 462 |
EXPECT_SUCCESS(s2n_stuffer_read_uint32(&out, &obfuscated_ticket_age)); |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
EXPECT_EQUAL(s2n_stuffer_data_available(&out), |
| 467 |
matching_psk.hash_size |
| 468 |
+ sizeof(uint8_t) |
| 469 |
+ sizeof(uint16_t)) |
| 470 |
; |
| 471 |
|
| 472 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 473 |
EXPECT_SUCCESS(s2n_stuffer_free(&out)); |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
if (s2n_is_tls13_fully_supported()) { |
| 486 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, |
| 487 |
s2n_cert_chain_and_key_ptr_free); |
| 488 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, |
| 489 |
S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY)); |
| 490 |
|
| 491 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), |
| 492 |
s2n_config_ptr_free); |
| 493 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key)); |
| 494 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13")); |
| 495 |
EXPECT_SUCCESS(s2n_config_disable_x509_verification(config)); |
| 496 |
|
| 497 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), |
| 498 |
s2n_connection_ptr_free); |
| 499 |
EXPECT_NOT_NULL(server_conn); |
| 500 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 501 |
|
| 502 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), |
| 503 |
s2n_connection_ptr_free); |
| 504 |
EXPECT_NOT_NULL(server_conn); |
| 505 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 506 |
|
| 507 |
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close); |
| 508 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 509 |
EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair)); |
| 510 |
|
| 511 |
|
| 512 |
client_conn->security_policy_override = &security_policy_test_tls13_retry; |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
DEFER_CLEANUP(struct s2n_psk *psk = s2n_test_psk_new(client_conn), s2n_psk_free); |
| 519 |
psk->hmac_alg = S2N_HMAC_SHA384; |
| 520 |
EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, psk)); |
| 521 |
EXPECT_TRUE(s2n_client_psk_extension.should_send(client_conn)); |
| 522 |
|
| 523 |
|
| 524 |
s2n_blocked_status blocked = 0; |
| 525 |
EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO)); |
| 526 |
EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG)); |
| 527 |
EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO)); |
| 528 |
EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO)); |
| 529 |
|
| 530 |
|
| 531 |
EXPECT_FALSE(s2n_client_psk_extension.should_send(client_conn)); |
| 532 |
|
| 533 |
|
| 534 |
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn)); |
| 535 |
} |
| 536 |
}; |
| 537 |
|
| 538 |
|
| 539 |
{ |
| 540 |
|
| 541 |
{ |
| 542 |
struct s2n_connection *conn = NULL; |
| 543 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 544 |
|
| 545 |
EXPECT_ERROR_WITH_ERRNO(s2n_select_external_psk(conn, NULL), S2N_ERR_NULL); |
| 546 |
|
| 547 |
struct s2n_offered_psk_list wire_identity_list = { 0 }; |
| 548 |
EXPECT_ERROR_WITH_ERRNO(s2n_select_external_psk(NULL, &wire_identity_list), S2N_ERR_NULL); |
| 549 |
|
| 550 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 551 |
}; |
| 552 |
|
| 553 |
struct s2n_blob identity_1 = { 0 }; |
| 554 |
EXPECT_SUCCESS(s2n_blob_init(&identity_1, test_identity, sizeof(test_identity))); |
| 555 |
struct s2n_blob identity_2 = { 0 }; |
| 556 |
EXPECT_SUCCESS(s2n_blob_init(&identity_2, test_identity_2, sizeof(test_identity_2))); |
| 557 |
struct s2n_blob identity_3 = { 0 }; |
| 558 |
EXPECT_SUCCESS(s2n_blob_init(&identity_3, test_identity_3, sizeof(test_identity_3))); |
| 559 |
|
| 560 |
struct s2n_blob *all_psks_list[] = { &identity_1, &identity_2, &identity_3 }; |
| 561 |
struct s2n_blob *reverse_order_list[] = { &identity_3, &identity_2, &identity_1 }; |
| 562 |
struct s2n_blob *list_without_psk1[] = { &identity_2, &identity_3 }; |
| 563 |
struct s2n_blob *multiple_psk1[] = { &identity_1, &identity_1, &identity_1 }; |
| 564 |
struct s2n_blob *multiple_psk2[] = { &identity_2, &identity_2, &identity_2 }; |
| 565 |
struct s2n_blob *only_psk1[] = { &identity_1 }; |
| 566 |
struct s2n_blob *only_psk2[] = { &identity_2 }; |
| 567 |
struct s2n_blob *only_psk3[] = { &identity_3 }; |
| 568 |
|
| 569 |
struct { |
| 570 |
bool match; |
| 571 |
struct s2n_blob **wire_identities; |
| 572 |
size_t wire_identities_len; |
| 573 |
struct s2n_blob **local_identities; |
| 574 |
size_t local_identities_len; |
| 575 |
size_t wire_match_index; |
| 576 |
size_t local_match_index; |
| 577 |
} test_cases[] = { |
| 578 |
#define WIRE_IDENTITIES(list) .wire_identities = list, .wire_identities_len = s2n_array_len(list) |
| 579 |
#define LOCAL_IDENTITIES(list) .local_identities = list, .local_identities_len = s2n_array_len(list) |
| 580 |
|
| 581 |
{ .match = false }, |
| 582 |
|
| 583 |
|
| 584 |
{ .match = false, WIRE_IDENTITIES(only_psk1) }, |
| 585 |
{ .match = false, WIRE_IDENTITIES(all_psks_list) }, |
| 586 |
|
| 587 |
|
| 588 |
{ .match = false, LOCAL_IDENTITIES(only_psk1) }, |
| 589 |
{ .match = false, LOCAL_IDENTITIES(all_psks_list) }, |
| 590 |
|
| 591 |
|
| 592 |
{ .match = false, WIRE_IDENTITIES(only_psk1), LOCAL_IDENTITIES(only_psk2) }, |
| 593 |
{ .match = false, WIRE_IDENTITIES(only_psk1), LOCAL_IDENTITIES(list_without_psk1) }, |
| 594 |
{ .match = false, WIRE_IDENTITIES(list_without_psk1), LOCAL_IDENTITIES(only_psk1) }, |
| 595 |
|
| 596 |
|
| 597 |
{ .match = true, WIRE_IDENTITIES(only_psk1), LOCAL_IDENTITIES(only_psk1), .wire_match_index = 0, .local_match_index = 0 }, |
| 598 |
|
| 599 |
|
| 600 |
{ .match = true, WIRE_IDENTITIES(only_psk1), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 0, .local_match_index = 0 }, |
| 601 |
{ .match = true, WIRE_IDENTITIES(only_psk2), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 0, .local_match_index = 1 }, |
| 602 |
{ .match = true, WIRE_IDENTITIES(only_psk3), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 0, .local_match_index = 2 }, |
| 603 |
|
| 604 |
|
| 605 |
{ .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(only_psk1), .wire_match_index = 0, .local_match_index = 0 }, |
| 606 |
{ .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(only_psk2), .wire_match_index = 1, .local_match_index = 0 }, |
| 607 |
{ .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(only_psk3), .wire_match_index = 2, .local_match_index = 0 }, |
| 608 |
|
| 609 |
|
| 610 |
{ .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 0, .local_match_index = 0 }, |
| 611 |
{ .match = true, WIRE_IDENTITIES(reverse_order_list), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 2, .local_match_index = 0 }, |
| 612 |
{ .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(reverse_order_list), .wire_match_index = 2, .local_match_index = 0 }, |
| 613 |
{ .match = true, WIRE_IDENTITIES(list_without_psk1), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 0, .local_match_index = 1 }, |
| 614 |
{ .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(list_without_psk1), .wire_match_index = 1, .local_match_index = 0 }, |
| 615 |
{ .match = true, WIRE_IDENTITIES(list_without_psk1), LOCAL_IDENTITIES(reverse_order_list), .wire_match_index = 1, .local_match_index = 0 }, |
| 616 |
{ .match = true, WIRE_IDENTITIES(reverse_order_list), LOCAL_IDENTITIES(list_without_psk1), .wire_match_index = 1, .local_match_index = 0 }, |
| 617 |
|
| 618 |
|
| 619 |
{ .match = true, WIRE_IDENTITIES(multiple_psk1), LOCAL_IDENTITIES(multiple_psk1), .wire_match_index = 0, .local_match_index = 0 }, |
| 620 |
{ .match = false, WIRE_IDENTITIES(multiple_psk1), LOCAL_IDENTITIES(multiple_psk2) }, |
| 621 |
{ .match = true, WIRE_IDENTITIES(multiple_psk1), LOCAL_IDENTITIES(all_psks_list), .wire_match_index = 0, .local_match_index = 0 }, |
| 622 |
{ .match = true, WIRE_IDENTITIES(all_psks_list), LOCAL_IDENTITIES(multiple_psk1), .wire_match_index = 0, .local_match_index = 0 }, |
| 623 |
{ .match = true, WIRE_IDENTITIES(multiple_psk1), LOCAL_IDENTITIES(reverse_order_list), .wire_match_index = 0, .local_match_index = 2 }, |
| 624 |
{ .match = true, WIRE_IDENTITIES(reverse_order_list), LOCAL_IDENTITIES(multiple_psk1), .wire_match_index = 2, .local_match_index = 0 }, |
| 625 |
}; |
| 626 |
|
| 627 |
for (size_t i = 0; i < s2n_array_len(test_cases); i++) { |
| 628 |
struct s2n_connection *conn = NULL; |
| 629 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 630 |
conn->psk_params.type = S2N_PSK_TYPE_EXTERNAL; |
| 631 |
|
| 632 |
struct s2n_offered_psk_list client_identity_list = { .conn = conn }; |
| 633 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_identity_list.wire_data, 0)); |
| 634 |
for (size_t wire_i = 0; wire_i < test_cases[i].wire_identities_len; wire_i++) { |
| 635 |
EXPECT_OK(s2n_write_test_identity(&client_identity_list.wire_data, test_cases[i].wire_identities[wire_i])); |
| 636 |
} |
| 637 |
|
| 638 |
struct s2n_psk *expected_chosen_psk = NULL; |
| 639 |
for (size_t local_i = 0; local_i < test_cases[i].local_identities_len; local_i++) { |
| 640 |
struct s2n_psk *server_psk = NULL; |
| 641 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &server_psk)); |
| 642 |
EXPECT_NOT_NULL(server_psk); |
| 643 |
EXPECT_SUCCESS(s2n_psk_set_identity(server_psk, test_cases[i].local_identities[local_i]->data, |
| 644 |
test_cases[i].local_identities[local_i]->size)); |
| 645 |
|
| 646 |
if (local_i == test_cases[i].local_match_index) { |
| 647 |
expected_chosen_psk = server_psk; |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
if (test_cases[i].match) { |
| 652 |
EXPECT_OK(s2n_select_external_psk(conn, &client_identity_list)); |
| 653 |
EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, test_cases[i].wire_match_index); |
| 654 |
EXPECT_EQUAL(conn->psk_params.chosen_psk, expected_chosen_psk); |
| 655 |
} else { |
| 656 |
EXPECT_ERROR_WITH_ERRNO(s2n_select_external_psk(conn, &client_identity_list), S2N_ERR_NULL); |
| 657 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 658 |
} |
| 659 |
|
| 660 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 661 |
EXPECT_SUCCESS(s2n_stuffer_free(&client_identity_list.wire_data)); |
| 662 |
} |
| 663 |
}; |
| 664 |
|
| 665 |
|
| 666 |
{ |
| 667 |
|
| 668 |
{ |
| 669 |
struct s2n_offered_psk_list identity_list = { 0 }; |
| 670 |
struct s2n_connection *conn = s2n_connection_new(S2N_SERVER); |
| 671 |
EXPECT_NOT_NULL(conn); |
| 672 |
|
| 673 |
EXPECT_ERROR_WITH_ERRNO(s2n_select_resumption_psk(NULL, &identity_list), S2N_ERR_NULL); |
| 674 |
EXPECT_ERROR_WITH_ERRNO(s2n_select_resumption_psk(conn, NULL), S2N_ERR_NULL); |
| 675 |
|
| 676 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 677 |
}; |
| 678 |
|
| 679 |
|
| 680 |
{ |
| 681 |
struct s2n_connection *conn = s2n_connection_new(S2N_SERVER); |
| 682 |
EXPECT_NOT_NULL(conn); |
| 683 |
|
| 684 |
struct s2n_config *config = s2n_config_new(); |
| 685 |
EXPECT_NOT_NULL(config); |
| 686 |
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config)); |
| 687 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 688 |
|
| 689 |
conn->actual_protocol_version = S2N_TLS13; |
| 690 |
conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256; |
| 691 |
|
| 692 |
DEFER_CLEANUP(struct s2n_stuffer psk_identity = { 0 }, s2n_stuffer_free); |
| 693 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_identity, 0)); |
| 694 |
EXPECT_OK(s2n_setup_encrypted_ticket(conn, &psk_identity)); |
| 695 |
|
| 696 |
struct s2n_offered_psk_list identity_list = { .conn = conn }; |
| 697 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&identity_list.wire_data, 0)); |
| 698 |
|
| 699 |
EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &psk_identity.blob)); |
| 700 |
|
| 701 |
EXPECT_OK(s2n_select_resumption_psk(conn, &identity_list)); |
| 702 |
|
| 703 |
EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0); |
| 704 |
EXPECT_NOT_NULL(conn->psk_params.chosen_psk); |
| 705 |
|
| 706 |
|
| 707 |
EXPECT_EQUAL(conn->psk_params.chosen_psk->hmac_alg, s2n_tls13_aes_128_gcm_sha256.prf_alg); |
| 708 |
|
| 709 |
EXPECT_SUCCESS(s2n_stuffer_free(&identity_list.wire_data)); |
| 710 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 711 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 712 |
}; |
| 713 |
|
| 714 |
|
| 715 |
{ |
| 716 |
struct s2n_connection *conn = s2n_connection_new(S2N_SERVER); |
| 717 |
EXPECT_NOT_NULL(conn); |
| 718 |
|
| 719 |
struct s2n_config *config = s2n_config_new(); |
| 720 |
EXPECT_NOT_NULL(config); |
| 721 |
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config)); |
| 722 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 723 |
|
| 724 |
conn->actual_protocol_version = S2N_TLS13; |
| 725 |
conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256; |
| 726 |
|
| 727 |
DEFER_CLEANUP(struct s2n_stuffer psk_identity = { 0 }, s2n_stuffer_free); |
| 728 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_identity, 0)); |
| 729 |
EXPECT_OK(s2n_setup_encrypted_ticket(conn, &psk_identity)); |
| 730 |
|
| 731 |
struct s2n_offered_psk_list identity_list = { .conn = conn }; |
| 732 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&identity_list.wire_data, 0)); |
| 733 |
|
| 734 |
|
| 735 |
uint8_t bad_identity_data[] = "hello"; |
| 736 |
struct s2n_blob bad_identity = { 0 }; |
| 737 |
EXPECT_SUCCESS(s2n_blob_init(&bad_identity, bad_identity_data, sizeof(bad_identity_data))); |
| 738 |
|
| 739 |
uint8_t psk_idx = MAX_REJECTED_TICKETS - 1; |
| 740 |
for (size_t i = 0; i < psk_idx; i++) { |
| 741 |
EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &bad_identity)); |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &psk_identity.blob)); |
| 746 |
|
| 747 |
|
| 748 |
EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &psk_identity.blob)); |
| 749 |
|
| 750 |
EXPECT_OK(s2n_select_resumption_psk(conn, &identity_list)); |
| 751 |
|
| 752 |
EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, psk_idx); |
| 753 |
EXPECT_NOT_NULL(conn->psk_params.chosen_psk); |
| 754 |
|
| 755 |
|
| 756 |
EXPECT_EQUAL(conn->psk_params.chosen_psk->hmac_alg, s2n_tls13_aes_128_gcm_sha256.prf_alg); |
| 757 |
|
| 758 |
EXPECT_SUCCESS(s2n_stuffer_free(&identity_list.wire_data)); |
| 759 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 760 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 761 |
}; |
| 762 |
|
| 763 |
|
| 764 |
{ |
| 765 |
struct s2n_connection *conn = s2n_connection_new(S2N_SERVER); |
| 766 |
EXPECT_NOT_NULL(conn); |
| 767 |
|
| 768 |
conn->actual_protocol_version = S2N_TLS13; |
| 769 |
conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256; |
| 770 |
|
| 771 |
struct s2n_offered_psk_list identity_list = { .conn = conn }; |
| 772 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&identity_list.wire_data, 0)); |
| 773 |
|
| 774 |
|
| 775 |
uint8_t bad_identity_data[] = "hello"; |
| 776 |
struct s2n_blob bad_identity = { 0 }; |
| 777 |
EXPECT_SUCCESS(s2n_blob_init(&bad_identity, bad_identity_data, sizeof(bad_identity_data))); |
| 778 |
EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &bad_identity)); |
| 779 |
|
| 780 |
EXPECT_ERROR_WITH_ERRNO(s2n_select_resumption_psk(conn, &identity_list), S2N_ERR_INVALID_SESSION_TICKET); |
| 781 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 782 |
|
| 783 |
EXPECT_SUCCESS(s2n_stuffer_free(&identity_list.wire_data)); |
| 784 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 785 |
}; |
| 786 |
|
| 787 |
|
| 788 |
{ |
| 789 |
struct s2n_connection *conn = s2n_connection_new(S2N_SERVER); |
| 790 |
EXPECT_NOT_NULL(conn); |
| 791 |
|
| 792 |
struct s2n_config *config = s2n_config_new(); |
| 793 |
EXPECT_NOT_NULL(config); |
| 794 |
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config)); |
| 795 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 796 |
|
| 797 |
conn->actual_protocol_version = S2N_TLS13; |
| 798 |
conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256; |
| 799 |
|
| 800 |
DEFER_CLEANUP(struct s2n_stuffer psk_identity = { 0 }, s2n_stuffer_free); |
| 801 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_identity, 0)); |
| 802 |
EXPECT_OK(s2n_setup_encrypted_ticket(conn, &psk_identity)); |
| 803 |
|
| 804 |
|
| 805 |
uint8_t bad_identity_data[] = "hello"; |
| 806 |
struct s2n_blob bad_identity = { 0 }; |
| 807 |
EXPECT_SUCCESS(s2n_blob_init(&bad_identity, bad_identity_data, sizeof(bad_identity_data))); |
| 808 |
|
| 809 |
struct s2n_offered_psk_list identity_list = { .conn = conn }; |
| 810 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&identity_list.wire_data, 0)); |
| 811 |
|
| 812 |
uint8_t bad_psk_count = MAX_REJECTED_TICKETS; |
| 813 |
for (size_t i = 0; i < bad_psk_count; i++) { |
| 814 |
EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &bad_identity)); |
| 815 |
} |
| 816 |
EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &psk_identity.blob)); |
| 817 |
|
| 818 |
EXPECT_ERROR_WITH_ERRNO(s2n_select_resumption_psk(conn, &identity_list), S2N_ERR_INVALID_SESSION_TICKET); |
| 819 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 820 |
|
| 821 |
EXPECT_SUCCESS(s2n_stuffer_free(&identity_list.wire_data)); |
| 822 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 823 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 824 |
}; |
| 825 |
}; |
| 826 |
|
| 827 |
|
| 828 |
{ |
| 829 |
|
| 830 |
{ |
| 831 |
struct s2n_stuffer wire_identities_in = { 0 }; |
| 832 |
|
| 833 |
struct s2n_connection *conn = NULL; |
| 834 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 835 |
|
| 836 |
EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_identity_list(conn, NULL), S2N_ERR_NULL); |
| 837 |
EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_identity_list(NULL, &wire_identities_in), S2N_ERR_NULL); |
| 838 |
|
| 839 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 840 |
}; |
| 841 |
|
| 842 |
|
| 843 |
{ |
| 844 |
struct s2n_stuffer empty_wire_identities_in = { 0 }; |
| 845 |
|
| 846 |
struct s2n_connection *conn = NULL; |
| 847 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 848 |
EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL)); |
| 849 |
|
| 850 |
EXPECT_ERROR(s2n_client_psk_recv_identity_list(conn, &empty_wire_identities_in)); |
| 851 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 852 |
|
| 853 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 854 |
}; |
| 855 |
|
| 856 |
|
| 857 |
{ |
| 858 |
struct s2n_connection *conn = NULL; |
| 859 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 860 |
EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL)); |
| 861 |
|
| 862 |
struct s2n_stuffer wire_identities_in = { 0 }; |
| 863 |
EXPECT_SUCCESS(s2n_stuffer_alloc(&wire_identities_in, sizeof(wire_identities))); |
| 864 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&wire_identities_in, wire_identities, sizeof(wire_identities))); |
| 865 |
|
| 866 |
struct s2n_psk *no_match_psk = NULL; |
| 867 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &no_match_psk)); |
| 868 |
EXPECT_OK(s2n_psk_init(no_match_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 869 |
EXPECT_SUCCESS(s2n_psk_set_identity(no_match_psk, test_bytes_data_2, sizeof(test_bytes_data_2))); |
| 870 |
|
| 871 |
EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_identity_list(conn, &wire_identities_in), S2N_ERR_NULL); |
| 872 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 873 |
|
| 874 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 875 |
EXPECT_SUCCESS(s2n_stuffer_free(&wire_identities_in)); |
| 876 |
}; |
| 877 |
|
| 878 |
|
| 879 |
{ |
| 880 |
struct s2n_connection *conn = NULL; |
| 881 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 882 |
EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL)); |
| 883 |
|
| 884 |
struct s2n_stuffer wire_identities_in = { 0 }; |
| 885 |
EXPECT_SUCCESS(s2n_stuffer_alloc(&wire_identities_in, sizeof(single_wire_identity))); |
| 886 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&wire_identities_in, single_wire_identity, sizeof(single_wire_identity))); |
| 887 |
|
| 888 |
struct s2n_psk *match_psk = NULL; |
| 889 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &match_psk)); |
| 890 |
EXPECT_OK(s2n_psk_init(match_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 891 |
EXPECT_SUCCESS(s2n_psk_set_identity(match_psk, test_bytes_data, sizeof(test_bytes_data))); |
| 892 |
|
| 893 |
EXPECT_OK(s2n_client_psk_recv_identity_list(conn, &wire_identities_in)); |
| 894 |
EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0); |
| 895 |
EXPECT_EQUAL(conn->psk_params.chosen_psk, match_psk); |
| 896 |
|
| 897 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 898 |
EXPECT_SUCCESS(s2n_stuffer_free(&wire_identities_in)); |
| 899 |
}; |
| 900 |
|
| 901 |
|
| 902 |
{ |
| 903 |
struct s2n_config *config = s2n_config_new(); |
| 904 |
EXPECT_NOT_NULL(config); |
| 905 |
EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config, s2n_test_error_select_psk_identity_callback, NULL)); |
| 906 |
|
| 907 |
struct s2n_connection *conn = NULL; |
| 908 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 909 |
EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL)); |
| 910 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 911 |
|
| 912 |
struct s2n_stuffer wire_identities_in = { 0 }; |
| 913 |
EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_identity_list(conn, &wire_identities_in), |
| 914 |
S2N_ERR_UNIMPLEMENTED); |
| 915 |
EXPECT_EQUAL(conn->psk_params.chosen_psk, NULL); |
| 916 |
|
| 917 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 918 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 919 |
EXPECT_SUCCESS(s2n_stuffer_free(&wire_identities_in)); |
| 920 |
}; |
| 921 |
|
| 922 |
|
| 923 |
{ |
| 924 |
struct s2n_config *config = s2n_config_new(); |
| 925 |
EXPECT_NOT_NULL(config); |
| 926 |
|
| 927 |
uint16_t expected_wire_choice = 0; |
| 928 |
EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config, s2n_test_select_psk_identity_callback, &expected_wire_choice)); |
| 929 |
|
| 930 |
struct s2n_connection *conn = NULL; |
| 931 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 932 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 933 |
EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL)); |
| 934 |
|
| 935 |
struct s2n_psk *match_psk = NULL; |
| 936 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &match_psk)); |
| 937 |
EXPECT_OK(s2n_psk_init(match_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 938 |
EXPECT_SUCCESS(s2n_psk_set_identity(match_psk, test_bytes_data, sizeof(test_bytes_data))); |
| 939 |
|
| 940 |
struct s2n_stuffer wire_identities_in = { 0 }; |
| 941 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire_identities_in, 0)); |
| 942 |
EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &match_psk->identity)); |
| 943 |
|
| 944 |
EXPECT_OK(s2n_client_psk_recv_identity_list(conn, &wire_identities_in)); |
| 945 |
EXPECT_EQUAL(conn->psk_params.chosen_psk, match_psk); |
| 946 |
EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0); |
| 947 |
|
| 948 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 949 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 950 |
EXPECT_SUCCESS(s2n_stuffer_free(&wire_identities_in)); |
| 951 |
}; |
| 952 |
|
| 953 |
|
| 954 |
{ |
| 955 |
struct s2n_config *config = s2n_config_new(); |
| 956 |
EXPECT_NOT_NULL(config); |
| 957 |
|
| 958 |
uint16_t expected_wire_choice = 10; |
| 959 |
EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config, s2n_test_select_psk_identity_callback, &expected_wire_choice)); |
| 960 |
|
| 961 |
struct s2n_connection *conn = NULL; |
| 962 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 963 |
EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL)); |
| 964 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 965 |
|
| 966 |
struct s2n_psk *match_psk = NULL; |
| 967 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &match_psk)); |
| 968 |
EXPECT_OK(s2n_psk_init(match_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 969 |
EXPECT_SUCCESS(s2n_psk_set_identity(match_psk, test_bytes_data, sizeof(test_bytes_data))); |
| 970 |
|
| 971 |
struct s2n_stuffer wire_identities_in = { 0 }; |
| 972 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire_identities_in, 0)); |
| 973 |
EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &match_psk->identity)); |
| 974 |
|
| 975 |
EXPECT_ERROR(s2n_client_psk_recv_identity_list(conn, &wire_identities_in)); |
| 976 |
EXPECT_EQUAL(conn->psk_params.chosen_psk, NULL); |
| 977 |
|
| 978 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 979 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 980 |
EXPECT_SUCCESS(s2n_stuffer_free(&wire_identities_in)); |
| 981 |
}; |
| 982 |
|
| 983 |
|
| 984 |
{ |
| 985 |
struct s2n_config *config = s2n_config_new(); |
| 986 |
EXPECT_NOT_NULL(config); |
| 987 |
|
| 988 |
uint16_t expected_wire_choice = 0; |
| 989 |
EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config, s2n_test_select_psk_identity_callback, &expected_wire_choice)); |
| 990 |
|
| 991 |
struct s2n_connection *conn = NULL; |
| 992 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 993 |
EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL)); |
| 994 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 995 |
|
| 996 |
struct s2n_psk *local_psk = NULL; |
| 997 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &local_psk)); |
| 998 |
EXPECT_OK(s2n_psk_init(local_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 999 |
EXPECT_SUCCESS(s2n_psk_set_identity(local_psk, test_bytes_data, sizeof(test_bytes_data))); |
| 1000 |
|
| 1001 |
struct s2n_blob wire_identity = { 0 }; |
| 1002 |
EXPECT_SUCCESS(s2n_blob_init(&wire_identity, test_bytes_data_2, sizeof(test_bytes_data_2))); |
| 1003 |
|
| 1004 |
struct s2n_stuffer wire_identities_in = { 0 }; |
| 1005 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire_identities_in, 0)); |
| 1006 |
EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &wire_identity)); |
| 1007 |
|
| 1008 |
EXPECT_ERROR(s2n_client_psk_recv_identity_list(conn, &wire_identities_in)); |
| 1009 |
EXPECT_EQUAL(conn->psk_params.chosen_psk, NULL); |
| 1010 |
|
| 1011 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 1012 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 1013 |
EXPECT_SUCCESS(s2n_stuffer_free(&wire_identities_in)); |
| 1014 |
}; |
| 1015 |
|
| 1016 |
|
| 1017 |
{ |
| 1018 |
DEFER_CLEANUP(struct s2n_stuffer wire_identities_in = { 0 }, s2n_stuffer_free); |
| 1019 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire_identities_in, 0)); |
| 1020 |
|
| 1021 |
struct s2n_connection *conn = s2n_connection_new(S2N_SERVER); |
| 1022 |
EXPECT_NOT_NULL(conn); |
| 1023 |
EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_RESUMPTION)); |
| 1024 |
|
| 1025 |
struct s2n_config *config_with_cb = s2n_config_new(); |
| 1026 |
EXPECT_NOT_NULL(config_with_cb); |
| 1027 |
uint16_t expected_wire_choice = 0; |
| 1028 |
EXPECT_SUCCESS(s2n_config_set_psk_selection_callback(config_with_cb, s2n_test_select_psk_identity_callback, &expected_wire_choice)); |
| 1029 |
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config_with_cb)); |
| 1030 |
|
| 1031 |
struct s2n_config *config = s2n_config_new(); |
| 1032 |
EXPECT_NOT_NULL(config); |
| 1033 |
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config)); |
| 1034 |
|
| 1035 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config)); |
| 1036 |
|
| 1037 |
conn->actual_protocol_version = S2N_TLS13; |
| 1038 |
conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256; |
| 1039 |
|
| 1040 |
DEFER_CLEANUP(struct s2n_stuffer psk_identity = { 0 }, s2n_stuffer_free); |
| 1041 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_identity, 0)); |
| 1042 |
EXPECT_OK(s2n_setup_encrypted_ticket(conn, &psk_identity)); |
| 1043 |
|
| 1044 |
|
| 1045 |
{ |
| 1046 |
|
| 1047 |
struct s2n_blob wire_identity = { 0 }; |
| 1048 |
EXPECT_SUCCESS(s2n_blob_init(&wire_identity, test_bytes_data_2, sizeof(test_bytes_data_2))); |
| 1049 |
EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &wire_identity)); |
| 1050 |
|
| 1051 |
|
| 1052 |
EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &psk_identity.blob)); |
| 1053 |
|
| 1054 |
EXPECT_OK(s2n_client_psk_recv_identity_list(conn, &wire_identities_in)); |
| 1055 |
|
| 1056 |
EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 1); |
| 1057 |
EXPECT_NOT_NULL(conn->psk_params.chosen_psk); |
| 1058 |
}; |
| 1059 |
|
| 1060 |
|
| 1061 |
{ |
| 1062 |
conn->psk_params.chosen_psk = NULL; |
| 1063 |
EXPECT_SUCCESS(s2n_stuffer_rewrite(&wire_identities_in)); |
| 1064 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config_with_cb)); |
| 1065 |
EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &psk_identity.blob)); |
| 1066 |
|
| 1067 |
EXPECT_OK(s2n_client_psk_recv_identity_list(conn, &wire_identities_in)); |
| 1068 |
|
| 1069 |
EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0); |
| 1070 |
EXPECT_NOT_NULL(conn->psk_params.chosen_psk); |
| 1071 |
}; |
| 1072 |
|
| 1073 |
|
| 1074 |
{ |
| 1075 |
conn->psk_params.chosen_psk = NULL; |
| 1076 |
EXPECT_SUCCESS(s2n_stuffer_rewrite(&wire_identities_in)); |
| 1077 |
EXPECT_SUCCESS(s2n_connection_set_config(conn, config_with_cb)); |
| 1078 |
|
| 1079 |
struct s2n_blob wire_identity = { 0 }; |
| 1080 |
EXPECT_SUCCESS(s2n_blob_init(&wire_identity, test_bytes_data_2, sizeof(test_bytes_data_2))); |
| 1081 |
EXPECT_OK(s2n_write_test_identity(&wire_identities_in, &wire_identity)); |
| 1082 |
|
| 1083 |
EXPECT_ERROR(s2n_client_psk_recv_identity_list(conn, &wire_identities_in)); |
| 1084 |
|
| 1085 |
EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0); |
| 1086 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 1087 |
}; |
| 1088 |
|
| 1089 |
EXPECT_SUCCESS(s2n_config_free(config_with_cb)); |
| 1090 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 1091 |
EXPECT_SUCCESS(s2n_config_free(config)); |
| 1092 |
}; |
| 1093 |
}; |
| 1094 |
|
| 1095 |
|
| 1096 |
{ |
| 1097 |
const uint8_t filler_binders[] = { |
| 1098 |
0x00, |
| 1099 |
|
| 1100 |
0x01, |
| 1101 |
0xFF, |
| 1102 |
}; |
| 1103 |
const size_t zero_length_binder_index = 0; |
| 1104 |
const size_t valid_binder_index = 2; |
| 1105 |
|
| 1106 |
uint8_t partial_client_hello_data[] = "Hello"; |
| 1107 |
uint8_t secret_data[] = "Secret"; |
| 1108 |
uint8_t binder_hash_data[SHA256_DIGEST_LENGTH] = { 0 }; |
| 1109 |
uint8_t valid_binder_data[SHA256_DIGEST_LENGTH] = { 0 }; |
| 1110 |
|
| 1111 |
struct s2n_blob partial_client_hello = { 0 }; |
| 1112 |
EXPECT_SUCCESS(s2n_blob_init(&partial_client_hello, |
| 1113 |
partial_client_hello_data, sizeof(partial_client_hello_data))); |
| 1114 |
|
| 1115 |
struct s2n_blob binder_hash = { 0 }; |
| 1116 |
EXPECT_SUCCESS(s2n_blob_init(&binder_hash, binder_hash_data, sizeof(binder_hash_data))); |
| 1117 |
|
| 1118 |
struct s2n_blob valid_binder = { 0 }; |
| 1119 |
EXPECT_SUCCESS(s2n_blob_init(&valid_binder, valid_binder_data, sizeof(valid_binder_data))); |
| 1120 |
|
| 1121 |
struct s2n_connection *conn = NULL; |
| 1122 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 1123 |
|
| 1124 |
DEFER_CLEANUP(struct s2n_psk psk = { 0 }, s2n_psk_wipe); |
| 1125 |
EXPECT_OK(s2n_psk_init(&psk, S2N_PSK_TYPE_EXTERNAL)); |
| 1126 |
EXPECT_SUCCESS(s2n_psk_set_secret(&psk, secret_data, sizeof(secret_data))); |
| 1127 |
|
| 1128 |
EXPECT_SUCCESS(s2n_psk_calculate_binder_hash(conn, psk.hmac_alg, &partial_client_hello, &binder_hash)); |
| 1129 |
EXPECT_SUCCESS(s2n_psk_calculate_binder(&psk, &binder_hash, &valid_binder)); |
| 1130 |
|
| 1131 |
struct s2n_stuffer wire_binders_in = { 0 }; |
| 1132 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&wire_binders_in, sizeof(filler_binders))); |
| 1133 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&wire_binders_in, filler_binders, sizeof(filler_binders))); |
| 1134 |
EXPECT_SUCCESS(s2n_stuffer_write_uint8(&wire_binders_in, valid_binder.size)); |
| 1135 |
EXPECT_SUCCESS(s2n_stuffer_write(&wire_binders_in, &valid_binder)); |
| 1136 |
|
| 1137 |
|
| 1138 |
{ |
| 1139 |
uint8_t empty_wire_binders[] = { 0 }; |
| 1140 |
conn->psk_params.chosen_psk = &psk; |
| 1141 |
conn->psk_params.chosen_psk_wire_index = valid_binder_index; |
| 1142 |
|
| 1143 |
struct s2n_stuffer empty_wire_binders_in = { 0 }; |
| 1144 |
EXPECT_SUCCESS(s2n_stuffer_alloc(&empty_wire_binders_in, sizeof(empty_wire_binders))); |
| 1145 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&empty_wire_binders_in, empty_wire_binders, sizeof(empty_wire_binders))); |
| 1146 |
|
| 1147 |
EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_binder_list(conn, &partial_client_hello, &empty_wire_binders_in), |
| 1148 |
S2N_ERR_BAD_MESSAGE); |
| 1149 |
|
| 1150 |
EXPECT_SUCCESS(s2n_stuffer_free(&empty_wire_binders_in)); |
| 1151 |
}; |
| 1152 |
|
| 1153 |
|
| 1154 |
{ |
| 1155 |
EXPECT_SUCCESS(s2n_stuffer_reread(&wire_binders_in)); |
| 1156 |
conn->psk_params.chosen_psk = NULL; |
| 1157 |
conn->psk_params.chosen_psk_wire_index = 0; |
| 1158 |
|
| 1159 |
EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_binder_list(conn, &partial_client_hello, &wire_binders_in), |
| 1160 |
S2N_ERR_NULL); |
| 1161 |
}; |
| 1162 |
|
| 1163 |
|
| 1164 |
{ |
| 1165 |
EXPECT_SUCCESS(s2n_stuffer_reread(&wire_binders_in)); |
| 1166 |
conn->psk_params.chosen_psk = &psk; |
| 1167 |
conn->psk_params.chosen_psk_wire_index = valid_binder_index + 10; |
| 1168 |
|
| 1169 |
EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_binder_list(conn, &partial_client_hello, &wire_binders_in), |
| 1170 |
S2N_ERR_BAD_MESSAGE); |
| 1171 |
}; |
| 1172 |
|
| 1173 |
|
| 1174 |
{ |
| 1175 |
EXPECT_SUCCESS(s2n_stuffer_reread(&wire_binders_in)); |
| 1176 |
conn->psk_params.chosen_psk = &psk; |
| 1177 |
conn->psk_params.chosen_psk_wire_index = zero_length_binder_index; |
| 1178 |
|
| 1179 |
EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_binder_list(conn, &partial_client_hello, &wire_binders_in), |
| 1180 |
S2N_ERR_SAFETY); |
| 1181 |
}; |
| 1182 |
|
| 1183 |
|
| 1184 |
{ |
| 1185 |
EXPECT_SUCCESS(s2n_stuffer_reread(&wire_binders_in)); |
| 1186 |
conn->psk_params.chosen_psk = &psk; |
| 1187 |
conn->psk_params.chosen_psk_wire_index = valid_binder_index; |
| 1188 |
|
| 1189 |
|
| 1190 |
struct s2n_blob *different_partial_client_hello = &valid_binder; |
| 1191 |
|
| 1192 |
EXPECT_ERROR_WITH_ERRNO(s2n_client_psk_recv_binder_list(conn, different_partial_client_hello, &wire_binders_in), |
| 1193 |
S2N_ERR_BAD_MESSAGE); |
| 1194 |
}; |
| 1195 |
|
| 1196 |
|
| 1197 |
{ |
| 1198 |
EXPECT_SUCCESS(s2n_stuffer_reread(&wire_binders_in)); |
| 1199 |
conn->psk_params.chosen_psk = &psk; |
| 1200 |
conn->psk_params.chosen_psk_wire_index = valid_binder_index; |
| 1201 |
|
| 1202 |
EXPECT_OK(s2n_client_psk_recv_binder_list(conn, &partial_client_hello, &wire_binders_in)); |
| 1203 |
}; |
| 1204 |
|
| 1205 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 1206 |
EXPECT_SUCCESS(s2n_stuffer_free(&wire_binders_in)); |
| 1207 |
}; |
| 1208 |
|
| 1209 |
|
| 1210 |
{ |
| 1211 |
const uint8_t client_hello_data[] = "ClientHello"; |
| 1212 |
s2n_extension_type_id key_share_id = 0; |
| 1213 |
s2n_extension_type_id psk_ke_mode_id = 0; |
| 1214 |
EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_PSK_KEY_EXCHANGE_MODES, &psk_ke_mode_id)); |
| 1215 |
EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_KEY_SHARE, &key_share_id)); |
| 1216 |
|
| 1217 |
|
| 1218 |
{ |
| 1219 |
const uint8_t extension_data[] = { |
| 1220 |
0x00, 0x00, |
| 1221 |
0x00, 0x00, |
| 1222 |
}; |
| 1223 |
|
| 1224 |
struct s2n_connection *conn = NULL; |
| 1225 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 1226 |
conn->client_hello.extensions.count = 1; |
| 1227 |
|
| 1228 |
|
| 1229 |
conn->psk_params.psk_ke_mode = S2N_PSK_DHE_KE; |
| 1230 |
S2N_CBIT_SET(conn->extension_requests_received, psk_ke_mode_id); |
| 1231 |
S2N_CBIT_SET(conn->extension_requests_received, key_share_id); |
| 1232 |
|
| 1233 |
|
| 1234 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->handshake.io, client_hello_data, sizeof(client_hello_data))); |
| 1235 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->handshake.io, extension_data, sizeof(extension_data))); |
| 1236 |
|
| 1237 |
|
| 1238 |
struct s2n_stuffer extension = { 0 }; |
| 1239 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0)); |
| 1240 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, extension_data, sizeof(extension_data))); |
| 1241 |
|
| 1242 |
|
| 1243 |
EXPECT_SUCCESS(s2n_client_psk_recv(conn, &extension)); |
| 1244 |
EXPECT_EQUAL(conn->psk_params.chosen_psk_wire_index, 0); |
| 1245 |
EXPECT_EQUAL(conn->psk_params.chosen_psk, NULL); |
| 1246 |
|
| 1247 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 1248 |
EXPECT_SUCCESS(s2n_stuffer_free(&extension)); |
| 1249 |
}; |
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
{ |
| 1254 |
const uint8_t identity = 0x12; |
| 1255 |
const uint8_t identity_bytes[] = { identity }; |
| 1256 |
const uint8_t extension_data[] = { |
| 1257 |
0x00, 0x07, |
| 1258 |
0x00, 0x01, |
| 1259 |
identity, |
| 1260 |
0x00, 0x00, 0x00, 0x00, |
| 1261 |
0x00, 0x00, |
| 1262 |
}; |
| 1263 |
|
| 1264 |
struct s2n_connection *conn = NULL; |
| 1265 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 1266 |
EXPECT_OK(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_EXTERNAL)); |
| 1267 |
conn->client_hello.extensions.count = 1; |
| 1268 |
|
| 1269 |
|
| 1270 |
conn->psk_params.psk_ke_mode = S2N_PSK_DHE_KE; |
| 1271 |
S2N_CBIT_SET(conn->extension_requests_received, psk_ke_mode_id); |
| 1272 |
S2N_CBIT_SET(conn->extension_requests_received, key_share_id); |
| 1273 |
|
| 1274 |
|
| 1275 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->handshake.io, client_hello_data, sizeof(client_hello_data))); |
| 1276 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&conn->handshake.io, extension_data, sizeof(extension_data))); |
| 1277 |
|
| 1278 |
|
| 1279 |
struct s2n_stuffer extension = { 0 }; |
| 1280 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0)); |
| 1281 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, extension_data, sizeof(extension_data))); |
| 1282 |
|
| 1283 |
struct s2n_psk *psk = NULL; |
| 1284 |
EXPECT_OK(s2n_array_pushback(&conn->psk_params.psk_list, (void **) &psk)); |
| 1285 |
EXPECT_OK(s2n_psk_init(psk, S2N_PSK_TYPE_EXTERNAL)); |
| 1286 |
EXPECT_SUCCESS(s2n_psk_set_identity(psk, identity_bytes, sizeof(identity_bytes))); |
| 1287 |
|
| 1288 |
|
| 1289 |
conn->actual_protocol_version = S2N_TLS12; |
| 1290 |
EXPECT_SUCCESS(s2n_extension_recv(&s2n_client_psk_extension, conn, &extension)); |
| 1291 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 1292 |
EXPECT_EQUAL(s2n_stuffer_data_available(&extension), sizeof(extension_data)); |
| 1293 |
|
| 1294 |
|
| 1295 |
conn->actual_protocol_version = S2N_TLS13; |
| 1296 |
EXPECT_FAILURE_WITH_ERRNO(s2n_client_psk_recv(conn, &extension), S2N_ERR_BAD_MESSAGE); |
| 1297 |
|
| 1298 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 1299 |
EXPECT_SUCCESS(s2n_stuffer_free(&extension)); |
| 1300 |
}; |
| 1301 |
|
| 1302 |
|
| 1303 |
{ |
| 1304 |
const uint8_t extension_data[] = { 0 }; |
| 1305 |
|
| 1306 |
struct s2n_connection *conn = NULL; |
| 1307 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 1308 |
conn->client_hello.extensions.count = 1; |
| 1309 |
|
| 1310 |
|
| 1311 |
struct s2n_stuffer extension = { 0 }; |
| 1312 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0)); |
| 1313 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, extension_data, sizeof(extension_data))); |
| 1314 |
|
| 1315 |
EXPECT_EQUAL(conn->psk_params.psk_ke_mode, S2N_PSK_KE_UNKNOWN); |
| 1316 |
|
| 1317 |
EXPECT_FALSE(S2N_CBIT_TEST(conn->extension_requests_received, psk_ke_mode_id)); |
| 1318 |
|
| 1319 |
EXPECT_FAILURE_WITH_ERRNO(s2n_client_psk_recv(conn, &extension), S2N_ERR_MISSING_EXTENSION); |
| 1320 |
|
| 1321 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 1322 |
EXPECT_SUCCESS(s2n_stuffer_free(&extension)); |
| 1323 |
}; |
| 1324 |
|
| 1325 |
|
| 1326 |
{ |
| 1327 |
const uint8_t extension_data[] = { 0 }; |
| 1328 |
struct s2n_connection *conn = NULL; |
| 1329 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 1330 |
conn->client_hello.extensions.count = 1; |
| 1331 |
|
| 1332 |
|
| 1333 |
struct s2n_stuffer extension = { 0 }; |
| 1334 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0)); |
| 1335 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, extension_data, sizeof(extension_data))); |
| 1336 |
|
| 1337 |
conn->psk_params.psk_ke_mode = S2N_PSK_KE_UNKNOWN; |
| 1338 |
|
| 1339 |
S2N_CBIT_SET(conn->extension_requests_received, psk_ke_mode_id); |
| 1340 |
|
| 1341 |
EXPECT_SUCCESS(s2n_client_psk_recv(conn, &extension)); |
| 1342 |
EXPECT_NULL(conn->psk_params.chosen_psk); |
| 1343 |
|
| 1344 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 1345 |
EXPECT_SUCCESS(s2n_stuffer_free(&extension)); |
| 1346 |
}; |
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
{ |
| 1351 |
const uint8_t extension_data[] = { 0 }; |
| 1352 |
struct s2n_connection *conn = NULL; |
| 1353 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 1354 |
conn->client_hello.extensions.count = 1; |
| 1355 |
|
| 1356 |
|
| 1357 |
struct s2n_stuffer extension = { 0 }; |
| 1358 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0)); |
| 1359 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, extension_data, sizeof(extension_data))); |
| 1360 |
|
| 1361 |
conn->psk_params.psk_ke_mode = S2N_PSK_DHE_KE; |
| 1362 |
|
| 1363 |
S2N_CBIT_SET(conn->extension_requests_received, psk_ke_mode_id); |
| 1364 |
EXPECT_FALSE(S2N_CBIT_TEST(conn->extension_requests_received, key_share_id)); |
| 1365 |
|
| 1366 |
EXPECT_FAILURE_WITH_ERRNO(s2n_client_psk_recv(conn, &extension), S2N_ERR_MISSING_EXTENSION); |
| 1367 |
|
| 1368 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 1369 |
EXPECT_SUCCESS(s2n_stuffer_free(&extension)); |
| 1370 |
}; |
| 1371 |
|
| 1372 |
|
| 1373 |
{ |
| 1374 |
s2n_extension_type_id psk_ext_id = 0; |
| 1375 |
EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_PRE_SHARED_KEY, &psk_ext_id)); |
| 1376 |
|
| 1377 |
struct s2n_stuffer extension = { 0 }; |
| 1378 |
struct s2n_connection *conn = NULL; |
| 1379 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 1380 |
|
| 1381 |
conn->client_hello.extensions.count = 2; |
| 1382 |
conn->client_hello.extensions.parsed_extensions[psk_ext_id].wire_index = 0; |
| 1383 |
EXPECT_FAILURE_WITH_ERRNO(s2n_client_psk_recv(conn, &extension), S2N_ERR_UNSUPPORTED_EXTENSION); |
| 1384 |
|
| 1385 |
conn->client_hello.extensions.count = 5; |
| 1386 |
conn->client_hello.extensions.parsed_extensions[psk_ext_id].wire_index = 1; |
| 1387 |
EXPECT_FAILURE_WITH_ERRNO(s2n_client_psk_recv(conn, &extension), S2N_ERR_UNSUPPORTED_EXTENSION); |
| 1388 |
|
| 1389 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 1390 |
}; |
| 1391 |
|
| 1392 |
|
| 1393 |
{ |
| 1394 |
const uint8_t client_hello_prefix_data[] = { |
| 1395 |
0x01, |
| 1396 |
0x00, 0x00, 0x00, |
| 1397 |
0x12, 0x34, 0x56, |
| 1398 |
}; |
| 1399 |
|
| 1400 |
struct s2n_connection *client_conn = NULL; |
| 1401 |
EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT)); |
| 1402 |
struct s2n_stuffer *client_out = &client_conn->handshake.io; |
| 1403 |
|
| 1404 |
struct s2n_connection *server_conn = NULL; |
| 1405 |
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER)); |
| 1406 |
EXPECT_OK(s2n_connection_set_psk_type(server_conn, S2N_PSK_TYPE_EXTERNAL)); |
| 1407 |
struct s2n_stuffer *server_in = &server_conn->handshake.io; |
| 1408 |
server_conn->client_hello.extensions.count = 1; |
| 1409 |
|
| 1410 |
|
| 1411 |
server_conn->psk_params.psk_ke_mode = S2N_PSK_DHE_KE; |
| 1412 |
S2N_CBIT_SET(server_conn->extension_requests_received, psk_ke_mode_id); |
| 1413 |
S2N_CBIT_SET(server_conn->extension_requests_received, key_share_id); |
| 1414 |
|
| 1415 |
struct s2n_psk *shared_psk = NULL; |
| 1416 |
EXPECT_OK(s2n_array_pushback(&client_conn->psk_params.psk_list, (void **) &shared_psk)); |
| 1417 |
EXPECT_OK(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 1418 |
EXPECT_SUCCESS(s2n_psk_set_identity(shared_psk, test_identity, sizeof(test_identity))); |
| 1419 |
EXPECT_SUCCESS(s2n_psk_set_secret(shared_psk, test_secret, sizeof(test_secret))); |
| 1420 |
EXPECT_OK(s2n_array_pushback(&server_conn->psk_params.psk_list, (void **) &shared_psk)); |
| 1421 |
EXPECT_OK(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 1422 |
EXPECT_SUCCESS(s2n_psk_set_identity(shared_psk, test_identity, sizeof(test_identity))); |
| 1423 |
EXPECT_SUCCESS(s2n_psk_set_secret(shared_psk, test_secret, sizeof(test_secret))); |
| 1424 |
|
| 1425 |
struct s2n_psk *other_server_psk = NULL; |
| 1426 |
EXPECT_OK(s2n_array_pushback(&server_conn->psk_params.psk_list, (void **) &other_server_psk)); |
| 1427 |
EXPECT_OK(s2n_psk_init(other_server_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 1428 |
EXPECT_SUCCESS(s2n_psk_set_identity(other_server_psk, test_identity_2, sizeof(test_identity_2))); |
| 1429 |
|
| 1430 |
|
| 1431 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(client_out, |
| 1432 |
client_hello_prefix_data, sizeof(client_hello_prefix_data))); |
| 1433 |
|
| 1434 |
EXPECT_SUCCESS(s2n_client_psk_extension.send(client_conn, client_out)); |
| 1435 |
EXPECT_OK(s2n_finish_psk_extension(client_conn)); |
| 1436 |
|
| 1437 |
|
| 1438 |
EXPECT_SUCCESS(s2n_stuffer_copy(client_out, server_in, |
| 1439 |
s2n_stuffer_data_available(client_out))); |
| 1440 |
EXPECT_SUCCESS(s2n_stuffer_skip_read(server_in, sizeof(client_hello_prefix_data))); |
| 1441 |
|
| 1442 |
EXPECT_SUCCESS(s2n_client_psk_recv(server_conn, server_in)); |
| 1443 |
EXPECT_EQUAL(server_conn->psk_params.chosen_psk_wire_index, 0); |
| 1444 |
EXPECT_EQUAL(server_conn->psk_params.chosen_psk, shared_psk); |
| 1445 |
|
| 1446 |
EXPECT_SUCCESS(s2n_connection_free(server_conn)); |
| 1447 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 1448 |
}; |
| 1449 |
}; |
| 1450 |
|
| 1451 |
|
| 1452 |
if (s2n_is_tls13_fully_supported()) { |
| 1453 |
|
| 1454 |
struct s2n_connection *client_conn = NULL, *server_conn = NULL; |
| 1455 |
EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT)); |
| 1456 |
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER)); |
| 1457 |
EXPECT_OK(s2n_connection_set_psk_type(server_conn, S2N_PSK_TYPE_EXTERNAL)); |
| 1458 |
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13")); |
| 1459 |
EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13")); |
| 1460 |
|
| 1461 |
|
| 1462 |
struct s2n_test_io_pair io_pair = { 0 }; |
| 1463 |
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 1464 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair)); |
| 1465 |
EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair)); |
| 1466 |
|
| 1467 |
|
| 1468 |
uint8_t other_client_data[] = "other client data"; |
| 1469 |
struct s2n_psk *other_client_psk = NULL; |
| 1470 |
EXPECT_OK(s2n_array_pushback(&client_conn->psk_params.psk_list, (void **) &other_client_psk)); |
| 1471 |
EXPECT_OK(s2n_psk_init(other_client_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 1472 |
EXPECT_SUCCESS(s2n_psk_set_identity(other_client_psk, other_client_data, sizeof(other_client_data))); |
| 1473 |
EXPECT_SUCCESS(s2n_psk_set_secret(other_client_psk, other_client_data, sizeof(other_client_data))); |
| 1474 |
|
| 1475 |
|
| 1476 |
uint8_t other_server_data[] = "other server data"; |
| 1477 |
struct s2n_psk *other_server_psk = NULL; |
| 1478 |
EXPECT_OK(s2n_array_pushback(&server_conn->psk_params.psk_list, (void **) &other_server_psk)); |
| 1479 |
EXPECT_OK(s2n_psk_init(other_server_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 1480 |
EXPECT_SUCCESS(s2n_psk_set_identity(other_server_psk, other_server_data, sizeof(other_server_data))); |
| 1481 |
EXPECT_SUCCESS(s2n_psk_set_secret(other_server_psk, other_server_data, sizeof(other_server_data))); |
| 1482 |
|
| 1483 |
|
| 1484 |
struct s2n_psk *shared_psk = NULL; |
| 1485 |
EXPECT_OK(s2n_array_pushback(&client_conn->psk_params.psk_list, (void **) &shared_psk)); |
| 1486 |
EXPECT_OK(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 1487 |
EXPECT_SUCCESS(s2n_psk_set_identity(shared_psk, test_identity, sizeof(test_identity))); |
| 1488 |
EXPECT_SUCCESS(s2n_psk_set_secret(shared_psk, test_secret, sizeof(test_secret))); |
| 1489 |
|
| 1490 |
|
| 1491 |
EXPECT_OK(s2n_array_pushback(&server_conn->psk_params.psk_list, (void **) &shared_psk)); |
| 1492 |
EXPECT_OK(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 1493 |
EXPECT_SUCCESS(s2n_psk_set_identity(shared_psk, test_identity, sizeof(test_identity))); |
| 1494 |
EXPECT_SUCCESS(s2n_psk_set_secret(shared_psk, test_secret, sizeof(test_secret))); |
| 1495 |
|
| 1496 |
EXPECT_SUCCESS(s2n_client_hello_send(client_conn)); |
| 1497 |
EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io, |
| 1498 |
s2n_stuffer_data_available(&client_conn->handshake.io))); |
| 1499 |
EXPECT_SUCCESS(s2n_client_hello_recv(server_conn)); |
| 1500 |
|
| 1501 |
|
| 1502 |
EXPECT_EQUAL(server_conn->psk_params.chosen_psk_wire_index, 1); |
| 1503 |
EXPECT_EQUAL(server_conn->psk_params.chosen_psk, shared_psk); |
| 1504 |
EXPECT_EQUAL(shared_psk->secret.size, sizeof(test_secret)); |
| 1505 |
EXPECT_BYTEARRAY_EQUAL(shared_psk->secret.data, test_secret, sizeof(test_secret)); |
| 1506 |
|
| 1507 |
EXPECT_SUCCESS(s2n_connection_free(server_conn)); |
| 1508 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 1509 |
EXPECT_SUCCESS(s2n_io_pair_close(&io_pair)); |
| 1510 |
} |
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
if (s2n_is_tls13_fully_supported()) { |
| 1523 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, |
| 1524 |
s2n_cert_chain_and_key_ptr_free); |
| 1525 |
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, |
| 1526 |
S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, |
| 1527 |
S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY)); |
| 1528 |
|
| 1529 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), |
| 1530 |
s2n_config_ptr_free); |
| 1531 |
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key)); |
| 1532 |
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13")); |
| 1533 |
EXPECT_SUCCESS(s2n_config_disable_x509_verification(config)); |
| 1534 |
EXPECT_OK(s2n_resumption_test_ticket_key_setup(config)); |
| 1535 |
|
| 1536 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), |
| 1537 |
s2n_connection_ptr_free); |
| 1538 |
EXPECT_NOT_NULL(server_conn); |
| 1539 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 1540 |
|
| 1541 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), |
| 1542 |
s2n_connection_ptr_free); |
| 1543 |
EXPECT_NOT_NULL(client_conn); |
| 1544 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 1545 |
|
| 1546 |
DEFER_CLEANUP(struct s2n_stuffer io_stuffer = { 0 }, s2n_stuffer_free); |
| 1547 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&io_stuffer, 0)); |
| 1548 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, client_conn)); |
| 1549 |
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, server_conn)); |
| 1550 |
|
| 1551 |
|
| 1552 |
client_conn->security_policy_override = &security_policy_test_tls13_retry; |
| 1553 |
|
| 1554 |
client_conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256; |
| 1555 |
|
| 1556 |
|
| 1557 |
DEFER_CLEANUP(struct s2n_stuffer psk_identity = { 0 }, s2n_stuffer_free); |
| 1558 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&psk_identity, 0)); |
| 1559 |
EXPECT_OK(s2n_setup_encrypted_ticket(client_conn, &psk_identity)); |
| 1560 |
struct s2n_offered_psk_list identity_list = { .conn = client_conn }; |
| 1561 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&identity_list.wire_data, 0)); |
| 1562 |
EXPECT_OK(s2n_write_test_identity(&identity_list.wire_data, &psk_identity.blob)); |
| 1563 |
EXPECT_OK(s2n_select_resumption_psk(server_conn, &identity_list)); |
| 1564 |
|
| 1565 |
|
| 1566 |
struct s2n_psk *psk = client_conn->psk_params.chosen_psk; |
| 1567 |
psk->ticket_issue_time -= MILLIS_TO_NANOS(10); |
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
const unsigned long identity_data_size = psk->identity.size - strlen((char *) psk->identity.data); |
| 1572 |
|
| 1573 |
s2n_blocked_status blocked = 0; |
| 1574 |
|
| 1575 |
|
| 1576 |
EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO)); |
| 1577 |
|
| 1578 |
|
| 1579 |
EXPECT_SUCCESS(s2n_stuffer_skip_read_until(&io_stuffer, (char *) psk->identity.data)); |
| 1580 |
EXPECT_SUCCESS(s2n_stuffer_skip_read(&io_stuffer, identity_data_size)); |
| 1581 |
uint32_t obfuscated_ticket_age_1 = 0; |
| 1582 |
EXPECT_SUCCESS(s2n_stuffer_read_uint32(&io_stuffer, &obfuscated_ticket_age_1)); |
| 1583 |
|
| 1584 |
|
| 1585 |
EXPECT_SUCCESS(s2n_stuffer_skip_read(&io_stuffer, sizeof(uint16_t))); |
| 1586 |
|
| 1587 |
|
| 1588 |
uint8_t binder_1_size = 0; |
| 1589 |
EXPECT_SUCCESS(s2n_stuffer_read_uint8(&io_stuffer, &binder_1_size)); |
| 1590 |
EXPECT_TRUE(binder_1_size == BINDER_SIZE); |
| 1591 |
|
| 1592 |
|
| 1593 |
uint8_t binder_1[BINDER_SIZE] = { 0 }; |
| 1594 |
EXPECT_SUCCESS(s2n_stuffer_read_bytes(&io_stuffer, binder_1, BINDER_SIZE)); |
| 1595 |
EXPECT_SUCCESS(s2n_stuffer_reread(&io_stuffer)); |
| 1596 |
|
| 1597 |
|
| 1598 |
EXPECT_TRUE(obfuscated_ticket_age_1 != 0); |
| 1599 |
uint8_t zero_array[BINDER_SIZE] = { 0 }; |
| 1600 |
EXPECT_FALSE(s2n_constant_time_equals(binder_1, zero_array, BINDER_SIZE)); |
| 1601 |
|
| 1602 |
|
| 1603 |
EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, HELLO_RETRY_MSG)); |
| 1604 |
EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, CLIENT_HELLO)); |
| 1605 |
EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, CLIENT_HELLO)); |
| 1606 |
|
| 1607 |
|
| 1608 |
psk->ticket_issue_time -= MILLIS_TO_NANOS(1); |
| 1609 |
|
| 1610 |
|
| 1611 |
EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO)); |
| 1612 |
|
| 1613 |
|
| 1614 |
EXPECT_SUCCESS(s2n_stuffer_skip_read_until(&io_stuffer, (char *) psk->identity.data)); |
| 1615 |
EXPECT_SUCCESS(s2n_stuffer_skip_read(&io_stuffer, identity_data_size)); |
| 1616 |
uint32_t obfuscated_ticket_age_2 = 0; |
| 1617 |
EXPECT_SUCCESS(s2n_stuffer_read_uint32(&io_stuffer, &obfuscated_ticket_age_2)); |
| 1618 |
|
| 1619 |
|
| 1620 |
EXPECT_SUCCESS(s2n_stuffer_skip_read(&io_stuffer, sizeof(uint16_t))); |
| 1621 |
|
| 1622 |
|
| 1623 |
uint8_t binder_2_size = 0; |
| 1624 |
EXPECT_SUCCESS(s2n_stuffer_read_uint8(&io_stuffer, &binder_2_size)); |
| 1625 |
EXPECT_TRUE(binder_2_size == BINDER_SIZE); |
| 1626 |
|
| 1627 |
|
| 1628 |
uint8_t binder_2[BINDER_SIZE] = { 0 }; |
| 1629 |
EXPECT_SUCCESS(s2n_stuffer_read_bytes(&io_stuffer, binder_2, BINDER_SIZE)); |
| 1630 |
|
| 1631 |
|
| 1632 |
EXPECT_TRUE(obfuscated_ticket_age_1 != obfuscated_ticket_age_2); |
| 1633 |
EXPECT_FALSE(s2n_constant_time_equals(binder_1, binder_2, BINDER_SIZE)); |
| 1634 |
|
| 1635 |
EXPECT_SUCCESS(s2n_stuffer_free(&identity_list.wire_data)); |
| 1636 |
} |
| 1637 |
|
| 1638 |
|
| 1639 |
{ |
| 1640 |
s2n_extension_type_id key_share_id = 0; |
| 1641 |
s2n_extension_type_id psk_ke_mode_id = 0; |
| 1642 |
EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_KEY_SHARE, &key_share_id)); |
| 1643 |
EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_PSK_KEY_EXCHANGE_MODES, &psk_ke_mode_id)); |
| 1644 |
|
| 1645 |
uint8_t large_test_identity[] = "test identity"; |
| 1646 |
uint8_t large_test_secret[] = "test secret"; |
| 1647 |
|
| 1648 |
struct s2n_connection *client_conn = NULL; |
| 1649 |
EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT)); |
| 1650 |
struct s2n_stuffer *client_out = &client_conn->handshake.io; |
| 1651 |
|
| 1652 |
struct s2n_connection *server_conn = NULL; |
| 1653 |
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER)); |
| 1654 |
EXPECT_OK(s2n_connection_set_psk_type(server_conn, S2N_PSK_TYPE_EXTERNAL)); |
| 1655 |
struct s2n_stuffer *server_in = &server_conn->handshake.io; |
| 1656 |
|
| 1657 |
|
| 1658 |
server_conn->client_hello.extensions.count = 1; |
| 1659 |
|
| 1660 |
server_conn->psk_params.psk_ke_mode = S2N_PSK_DHE_KE; |
| 1661 |
S2N_CBIT_SET(server_conn->extension_requests_received, psk_ke_mode_id); |
| 1662 |
S2N_CBIT_SET(server_conn->extension_requests_received, key_share_id); |
| 1663 |
|
| 1664 |
struct s2n_psk *shared_psk = NULL; |
| 1665 |
EXPECT_OK(s2n_array_pushback(&client_conn->psk_params.psk_list, (void **) &shared_psk)); |
| 1666 |
EXPECT_OK(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 1667 |
EXPECT_SUCCESS(s2n_psk_set_identity(shared_psk, large_test_identity, sizeof(large_test_identity))); |
| 1668 |
EXPECT_SUCCESS(s2n_psk_set_secret(shared_psk, large_test_secret, sizeof(large_test_secret))); |
| 1669 |
EXPECT_OK(s2n_array_pushback(&server_conn->psk_params.psk_list, (void **) &shared_psk)); |
| 1670 |
EXPECT_OK(s2n_psk_init(shared_psk, S2N_PSK_TYPE_EXTERNAL)); |
| 1671 |
EXPECT_SUCCESS(s2n_psk_set_identity(shared_psk, large_test_identity, sizeof(large_test_identity))); |
| 1672 |
EXPECT_SUCCESS(s2n_psk_set_secret(shared_psk, large_test_secret, sizeof(large_test_secret))); |
| 1673 |
|
| 1674 |
|
| 1675 |
uint32_t large_prefix_size = UINT16_MAX + 100; |
| 1676 |
EXPECT_SUCCESS(s2n_stuffer_skip_write(client_out, large_prefix_size)); |
| 1677 |
|
| 1678 |
EXPECT_SUCCESS(s2n_client_psk_extension.send(client_conn, client_out)); |
| 1679 |
EXPECT_OK(s2n_finish_psk_extension(client_conn)); |
| 1680 |
|
| 1681 |
|
| 1682 |
EXPECT_SUCCESS(s2n_stuffer_copy(client_out, server_in, |
| 1683 |
s2n_stuffer_data_available(client_out))); |
| 1684 |
EXPECT_SUCCESS(s2n_stuffer_skip_read(server_in, large_prefix_size)); |
| 1685 |
|
| 1686 |
EXPECT_SUCCESS(s2n_client_psk_recv(server_conn, server_in)); |
| 1687 |
EXPECT_EQUAL(server_conn->psk_params.chosen_psk, shared_psk); |
| 1688 |
|
| 1689 |
EXPECT_SUCCESS(s2n_connection_free(server_conn)); |
| 1690 |
EXPECT_SUCCESS(s2n_connection_free(client_conn)); |
| 1691 |
}; |
| 1692 |
|
| 1693 |
END_TEST(); |
| 1694 |
} |