| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/s2n_client_hello.h" |
| 17 |
|
| 18 |
#include <stdint.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <time.h> |
| 21 |
|
| 22 |
#include "api/unstable/fingerprint.h" |
| 23 |
#include "crypto/s2n_fips.h" |
| 24 |
#include "crypto/s2n_hash.h" |
| 25 |
#include "crypto/s2n_pq.h" |
| 26 |
#include "error/s2n_errno.h" |
| 27 |
#include "stuffer/s2n_stuffer.h" |
| 28 |
#include "tls/extensions/s2n_client_server_name.h" |
| 29 |
#include "tls/extensions/s2n_client_supported_groups.h" |
| 30 |
#include "tls/extensions/s2n_extension_list.h" |
| 31 |
#include "tls/extensions/s2n_server_key_share.h" |
| 32 |
#include "tls/s2n_alerts.h" |
| 33 |
#include "tls/s2n_auth_selection.h" |
| 34 |
#include "tls/s2n_cipher_preferences.h" |
| 35 |
#include "tls/s2n_cipher_suites.h" |
| 36 |
#include "tls/s2n_connection.h" |
| 37 |
#include "tls/s2n_handshake_type.h" |
| 38 |
#include "tls/s2n_security_policies.h" |
| 39 |
#include "tls/s2n_signature_algorithms.h" |
| 40 |
#include "tls/s2n_tls.h" |
| 41 |
#include "utils/s2n_bitmap.h" |
| 42 |
#include "utils/s2n_random.h" |
| 43 |
#include "utils/s2n_safety.h" |
| 44 |
|
| 45 |
struct s2n_client_hello *s2n_connection_get_client_hello(struct s2n_connection *conn) |
| 46 |
{ |
| 47 |
PTR_ENSURE_REF(conn); |
| 48 |
if (conn->client_hello.parsed != 1) { |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
|
| 52 |
return &conn->client_hello; |
| 53 |
} |
| 54 |
|
| 55 |
static uint32_t min_size(struct s2n_blob *blob, uint32_t max_length) |
| 56 |
{ |
| 57 |
return blob->size < max_length ? blob->size : max_length; |
| 58 |
} |
| 59 |
|
| 60 |
static S2N_RESULT s2n_generate_client_session_id(struct s2n_connection *conn) |
| 61 |
{ |
| 62 |
RESULT_ENSURE_REF(conn); |
| 63 |
RESULT_ENSURE_REF(conn->config); |
| 64 |
|
| 65 |
|
| 66 |
if (conn->session_id_len) { |
| 67 |
return S2N_RESULT_OK; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
bool generate = conn->config->use_tickets; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
if (conn->client_protocol_version >= S2N_TLS13) { |
| 78 |
generate = s2n_is_middlebox_compat_enabled(conn) || conn->resume_protocol_version; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
if (!generate) { |
| 83 |
return S2N_RESULT_OK; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
RESULT_ENSURE(!conn->quic_enabled, S2N_ERR_UNSUPPORTED_WITH_QUIC); |
| 93 |
|
| 94 |
struct s2n_blob session_id = { 0 }; |
| 95 |
RESULT_GUARD_POSIX(s2n_blob_init(&session_id, conn->session_id, S2N_TLS_SESSION_ID_MAX_LEN)); |
| 96 |
RESULT_GUARD(s2n_get_public_random_data(&session_id)); |
| 97 |
conn->session_id_len = S2N_TLS_SESSION_ID_MAX_LEN; |
| 98 |
return S2N_RESULT_OK; |
| 99 |
} |
| 100 |
|
| 101 |
ssize_t s2n_client_hello_get_raw_message_length(struct s2n_client_hello *ch) |
| 102 |
{ |
| 103 |
POSIX_ENSURE_REF(ch); |
| 104 |
|
| 105 |
return ch->raw_message.size; |
| 106 |
} |
| 107 |
|
| 108 |
ssize_t s2n_client_hello_get_raw_message(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length) |
| 109 |
{ |
| 110 |
POSIX_ENSURE_REF(ch); |
| 111 |
POSIX_ENSURE_REF(out); |
| 112 |
|
| 113 |
uint32_t len = min_size(&ch->raw_message, max_length); |
| 114 |
POSIX_CHECKED_MEMCPY(out, ch->raw_message.data, len); |
| 115 |
return len; |
| 116 |
} |
| 117 |
|
| 118 |
ssize_t s2n_client_hello_get_cipher_suites_length(struct s2n_client_hello *ch) |
| 119 |
{ |
| 120 |
POSIX_ENSURE_REF(ch); |
| 121 |
|
| 122 |
return ch->cipher_suites.size; |
| 123 |
} |
| 124 |
|
| 125 |
int s2n_client_hello_cb_done(struct s2n_connection *conn) |
| 126 |
{ |
| 127 |
POSIX_ENSURE_REF(conn); |
| 128 |
POSIX_ENSURE_REF(conn->config); |
| 129 |
POSIX_ENSURE(conn->config->client_hello_cb_mode == S2N_CLIENT_HELLO_CB_NONBLOCKING, S2N_ERR_INVALID_STATE); |
| 130 |
POSIX_ENSURE(conn->client_hello.callback_invoked == 1, S2N_ERR_ASYNC_NOT_PERFORMED); |
| 131 |
POSIX_ENSURE(conn->client_hello.parsed == 1, S2N_ERR_INVALID_STATE); |
| 132 |
|
| 133 |
conn->client_hello.callback_async_blocked = 0; |
| 134 |
conn->client_hello.callback_async_done = 1; |
| 135 |
|
| 136 |
return S2N_SUCCESS; |
| 137 |
} |
| 138 |
|
| 139 |
ssize_t s2n_client_hello_get_cipher_suites(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length) |
| 140 |
{ |
| 141 |
POSIX_ENSURE_REF(ch); |
| 142 |
POSIX_ENSURE_REF(out); |
| 143 |
POSIX_ENSURE_REF(ch->cipher_suites.data); |
| 144 |
|
| 145 |
uint32_t len = min_size(&ch->cipher_suites, max_length); |
| 146 |
|
| 147 |
POSIX_CHECKED_MEMCPY(out, ch->cipher_suites.data, len); |
| 148 |
|
| 149 |
return len; |
| 150 |
} |
| 151 |
|
| 152 |
ssize_t s2n_client_hello_get_extensions_length(struct s2n_client_hello *ch) |
| 153 |
{ |
| 154 |
POSIX_ENSURE_REF(ch); |
| 155 |
|
| 156 |
return ch->extensions.raw.size; |
| 157 |
} |
| 158 |
|
| 159 |
ssize_t s2n_client_hello_get_extensions(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length) |
| 160 |
{ |
| 161 |
POSIX_ENSURE_REF(ch); |
| 162 |
POSIX_ENSURE_REF(out); |
| 163 |
POSIX_ENSURE_REF(ch->extensions.raw.data); |
| 164 |
|
| 165 |
uint32_t len = min_size(&ch->extensions.raw, max_length); |
| 166 |
|
| 167 |
POSIX_CHECKED_MEMCPY(out, ch->extensions.raw.data, len); |
| 168 |
|
| 169 |
return len; |
| 170 |
} |
| 171 |
|
| 172 |
int s2n_client_hello_get_random(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length) |
| 173 |
{ |
| 174 |
POSIX_ENSURE_REF(ch); |
| 175 |
POSIX_ENSURE_REF(out); |
| 176 |
|
| 177 |
POSIX_ENSURE(max_length >= S2N_TLS_RANDOM_DATA_LEN, S2N_ERR_INSUFFICIENT_MEM_SIZE); |
| 178 |
POSIX_CHECKED_MEMCPY(out, ch->random, S2N_TLS_RANDOM_DATA_LEN); |
| 179 |
return S2N_SUCCESS; |
| 180 |
} |
| 181 |
|
| 182 |
int s2n_client_hello_free_raw_message(struct s2n_client_hello *client_hello) |
| 183 |
{ |
| 184 |
POSIX_ENSURE_REF(client_hello); |
| 185 |
|
| 186 |
POSIX_GUARD(s2n_free(&client_hello->raw_message)); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
client_hello->cipher_suites.data = NULL; |
| 191 |
client_hello->extensions.raw.data = NULL; |
| 192 |
|
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
int s2n_client_hello_free(struct s2n_client_hello **ch) |
| 197 |
{ |
| 198 |
POSIX_ENSURE_REF(ch); |
| 199 |
if (*ch == NULL) { |
| 200 |
return S2N_SUCCESS; |
| 201 |
} |
| 202 |
|
| 203 |
POSIX_ENSURE((*ch)->alloced, S2N_ERR_INVALID_ARGUMENT); |
| 204 |
POSIX_GUARD(s2n_client_hello_free_raw_message(*ch)); |
| 205 |
POSIX_GUARD(s2n_free_object((uint8_t **) ch, sizeof(struct s2n_client_hello))); |
| 206 |
*ch = NULL; |
| 207 |
return S2N_SUCCESS; |
| 208 |
} |
| 209 |
|
| 210 |
int s2n_collect_client_hello(struct s2n_client_hello *ch, struct s2n_stuffer *source) |
| 211 |
{ |
| 212 |
POSIX_ENSURE_REF(ch); |
| 213 |
POSIX_ENSURE_REF(source); |
| 214 |
|
| 215 |
uint32_t size = s2n_stuffer_data_available(source); |
| 216 |
S2N_ERROR_IF(size == 0, S2N_ERR_BAD_MESSAGE); |
| 217 |
|
| 218 |
POSIX_GUARD(s2n_realloc(&ch->raw_message, size)); |
| 219 |
POSIX_GUARD(s2n_stuffer_read(source, &ch->raw_message)); |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
static S2N_RESULT s2n_client_hello_verify_for_retry(struct s2n_connection *conn, |
| 225 |
struct s2n_client_hello *old_ch, struct s2n_client_hello *new_ch, |
| 226 |
uint8_t *previous_client_random) |
| 227 |
{ |
| 228 |
RESULT_ENSURE_REF(conn); |
| 229 |
RESULT_ENSURE_REF(old_ch); |
| 230 |
RESULT_ENSURE_REF(new_ch); |
| 231 |
RESULT_ENSURE_REF(previous_client_random); |
| 232 |
|
| 233 |
if (!s2n_is_hello_retry_handshake(conn)) { |
| 234 |
return S2N_RESULT_OK; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
RESULT_ENSURE(old_ch->legacy_version == new_ch->legacy_version, S2N_ERR_BAD_MESSAGE); |
| 247 |
RESULT_ENSURE(old_ch->compression_methods.size == new_ch->compression_methods.size, S2N_ERR_BAD_MESSAGE); |
| 248 |
RESULT_ENSURE(s2n_constant_time_equals(old_ch->compression_methods.data, new_ch->compression_methods.data, |
| 249 |
new_ch->compression_methods.size), |
| 250 |
S2N_ERR_BAD_MESSAGE); |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
if (s2n_in_test()) { |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
RESULT_ENSURE(s2n_constant_time_equals( |
| 263 |
previous_client_random, |
| 264 |
conn->client_hello.random, |
| 265 |
S2N_TLS_RANDOM_DATA_LEN), |
| 266 |
S2N_ERR_BAD_MESSAGE); |
| 267 |
|
| 268 |
|
| 269 |
RESULT_ENSURE(old_ch->session_id.size == new_ch->session_id.size, S2N_ERR_BAD_MESSAGE); |
| 270 |
RESULT_ENSURE(s2n_constant_time_equals(old_ch->session_id.data, new_ch->session_id.data, |
| 271 |
new_ch->session_id.size), |
| 272 |
S2N_ERR_BAD_MESSAGE); |
| 273 |
|
| 274 |
|
| 275 |
RESULT_ENSURE(old_ch->cipher_suites.size == new_ch->cipher_suites.size, S2N_ERR_BAD_MESSAGE); |
| 276 |
RESULT_ENSURE(s2n_constant_time_equals(old_ch->cipher_suites.data, new_ch->cipher_suites.data, |
| 277 |
new_ch->cipher_suites.size), |
| 278 |
S2N_ERR_BAD_MESSAGE); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
for (size_t i = 0; i < s2n_array_len(s2n_supported_extensions); i++) { |
| 286 |
s2n_parsed_extension *old_extension = &old_ch->extensions.parsed_extensions[i]; |
| 287 |
uint32_t old_size = old_extension->extension.size; |
| 288 |
s2n_parsed_extension *new_extension = &new_ch->extensions.parsed_extensions[i]; |
| 289 |
uint32_t new_size = new_extension->extension.size; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
uint16_t extension_type = 0; |
| 295 |
if (old_size != 0) { |
| 296 |
extension_type = old_extension->extension_type; |
| 297 |
} else if (new_size != 0) { |
| 298 |
extension_type = new_extension->extension_type; |
| 299 |
} else { |
| 300 |
continue; |
| 301 |
} |
| 302 |
|
| 303 |
switch (extension_type) { |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
case TLS_EXTENSION_KEY_SHARE: |
| 311 |
|
| 312 |
break; |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
case TLS_EXTENSION_EARLY_DATA: |
| 319 |
RESULT_ENSURE(new_size == 0, S2N_ERR_BAD_MESSAGE); |
| 320 |
break; |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
case TLS_EXTENSION_COOKIE: |
| 327 |
|
| 328 |
break; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
case TLS_EXTENSION_PRE_SHARED_KEY: |
| 337 |
|
| 338 |
break; |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
case TLS_EXTENSION_SUPPORTED_VERSIONS: |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
break; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
default: |
| 355 |
RESULT_ENSURE(old_size == new_size, S2N_ERR_BAD_MESSAGE); |
| 356 |
RESULT_ENSURE(s2n_constant_time_equals( |
| 357 |
new_extension->extension.data, |
| 358 |
old_extension->extension.data, |
| 359 |
old_size), |
| 360 |
S2N_ERR_BAD_MESSAGE); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
return S2N_RESULT_OK; |
| 365 |
} |
| 366 |
|
| 367 |
S2N_RESULT s2n_client_hello_parse_raw(struct s2n_client_hello *client_hello) |
| 368 |
{ |
| 369 |
RESULT_ENSURE_REF(client_hello); |
| 370 |
|
| 371 |
struct s2n_stuffer in_stuffer = { 0 }; |
| 372 |
RESULT_GUARD_POSIX(s2n_stuffer_init_written(&in_stuffer, &client_hello->raw_message)); |
| 373 |
struct s2n_stuffer *in = &in_stuffer; |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
uint8_t client_protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN] = { 0 }; |
| 396 |
RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(in, client_protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN)); |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
client_hello->legacy_version = (client_protocol_version[0] * 10) + client_protocol_version[1]; |
| 403 |
|
| 404 |
|
| 405 |
RESULT_GUARD_POSIX(s2n_stuffer_erase_and_read_bytes(in, client_hello->random, S2N_TLS_RANDOM_DATA_LEN)); |
| 406 |
|
| 407 |
|
| 408 |
uint8_t session_id_len = 0; |
| 409 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(in, &session_id_len)); |
| 410 |
RESULT_ENSURE(session_id_len <= S2N_TLS_SESSION_ID_MAX_LEN, S2N_ERR_BAD_MESSAGE); |
| 411 |
uint8_t *session_id = s2n_stuffer_raw_read(in, session_id_len); |
| 412 |
RESULT_ENSURE(session_id != NULL, S2N_ERR_BAD_MESSAGE); |
| 413 |
RESULT_GUARD_POSIX(s2n_blob_init(&client_hello->session_id, session_id, session_id_len)); |
| 414 |
|
| 415 |
|
| 416 |
uint16_t cipher_suites_length = 0; |
| 417 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(in, &cipher_suites_length)); |
| 418 |
RESULT_ENSURE(cipher_suites_length > 0, S2N_ERR_BAD_MESSAGE); |
| 419 |
RESULT_ENSURE(cipher_suites_length % S2N_TLS_CIPHER_SUITE_LEN == 0, S2N_ERR_BAD_MESSAGE); |
| 420 |
uint8_t *cipher_suites = s2n_stuffer_raw_read(in, cipher_suites_length); |
| 421 |
RESULT_ENSURE(cipher_suites != NULL, S2N_ERR_BAD_MESSAGE); |
| 422 |
RESULT_GUARD_POSIX(s2n_blob_init(&client_hello->cipher_suites, cipher_suites, cipher_suites_length)); |
| 423 |
|
| 424 |
|
| 425 |
uint8_t compression_methods_len = 0; |
| 426 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(in, &compression_methods_len)); |
| 427 |
uint8_t *compression_methods = s2n_stuffer_raw_read(in, compression_methods_len); |
| 428 |
RESULT_ENSURE(compression_methods != NULL, S2N_ERR_BAD_MESSAGE); |
| 429 |
RESULT_GUARD_POSIX(s2n_blob_init(&client_hello->compression_methods, compression_methods, compression_methods_len)); |
| 430 |
|
| 431 |
|
| 432 |
RESULT_GUARD_POSIX(s2n_extension_list_parse(in, &client_hello->extensions)); |
| 433 |
|
| 434 |
return S2N_RESULT_OK; |
| 435 |
} |
| 436 |
|
| 437 |
int s2n_parse_client_hello(struct s2n_connection *conn) |
| 438 |
{ |
| 439 |
POSIX_ENSURE_REF(conn); |
| 440 |
|
| 441 |
|
| 442 |
if (s2n_is_hello_retry_handshake(conn)) { |
| 443 |
POSIX_ENSURE(!conn->client_hello.sslv2, S2N_ERR_BAD_MESSAGE); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
DEFER_CLEANUP(struct s2n_client_hello previous_hello_retry = conn->client_hello, |
| 450 |
s2n_client_hello_free_raw_message); |
| 451 |
|
| 452 |
|
| 453 |
uint8_t previous_client_random[S2N_TLS_RANDOM_DATA_LEN] = { 0 }; |
| 454 |
POSIX_CHECKED_MEMCPY(previous_client_random, conn->client_hello.random, |
| 455 |
S2N_TLS_RANDOM_DATA_LEN); |
| 456 |
|
| 457 |
if (s2n_is_hello_retry_handshake(conn)) { |
| 458 |
POSIX_CHECKED_MEMSET(&conn->client_hello, 0, sizeof(struct s2n_client_hello)); |
| 459 |
} |
| 460 |
|
| 461 |
POSIX_GUARD(s2n_collect_client_hello(&conn->client_hello, &conn->handshake.io)); |
| 462 |
|
| 463 |
if (conn->client_hello.sslv2) { |
| 464 |
POSIX_GUARD(s2n_sslv2_client_hello_parse(conn)); |
| 465 |
return S2N_SUCCESS; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
POSIX_GUARD_RESULT(s2n_client_hello_parse_raw(&conn->client_hello)); |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
conn->client_protocol_version = S2N_MIN(conn->client_hello.legacy_version, S2N_TLS12); |
| 476 |
|
| 477 |
|
| 478 |
conn->session_id_len = conn->client_hello.session_id.size; |
| 479 |
POSIX_CHECKED_MEMCPY(conn->session_id, conn->client_hello.session_id.data, conn->session_id_len); |
| 480 |
|
| 481 |
POSIX_GUARD_RESULT(s2n_client_hello_verify_for_retry(conn, |
| 482 |
&previous_hello_retry, &conn->client_hello, previous_client_random)); |
| 483 |
return S2N_SUCCESS; |
| 484 |
} |
| 485 |
|
| 486 |
static S2N_RESULT s2n_client_hello_parse_message_impl(struct s2n_client_hello **result, |
| 487 |
const uint8_t *raw_message, uint32_t raw_message_size) |
| 488 |
{ |
| 489 |
RESULT_ENSURE_REF(result); |
| 490 |
|
| 491 |
DEFER_CLEANUP(struct s2n_blob mem = { 0 }, s2n_free); |
| 492 |
RESULT_GUARD_POSIX(s2n_alloc(&mem, sizeof(struct s2n_client_hello))); |
| 493 |
RESULT_GUARD_POSIX(s2n_blob_zero(&mem)); |
| 494 |
|
| 495 |
DEFER_CLEANUP(struct s2n_client_hello *client_hello = NULL, s2n_client_hello_free); |
| 496 |
client_hello = (struct s2n_client_hello *) (void *) mem.data; |
| 497 |
client_hello->alloced = true; |
| 498 |
ZERO_TO_DISABLE_DEFER_CLEANUP(mem); |
| 499 |
|
| 500 |
DEFER_CLEANUP(struct s2n_stuffer in = { 0 }, s2n_stuffer_free); |
| 501 |
RESULT_GUARD_POSIX(s2n_stuffer_alloc(&in, raw_message_size)); |
| 502 |
RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&in, raw_message, raw_message_size)); |
| 503 |
|
| 504 |
uint8_t message_type = 0; |
| 505 |
uint32_t message_len = 0; |
| 506 |
RESULT_GUARD(s2n_handshake_parse_header(&in, &message_type, &message_len)); |
| 507 |
RESULT_ENSURE(message_type == TLS_CLIENT_HELLO, S2N_ERR_BAD_MESSAGE); |
| 508 |
RESULT_ENSURE(message_len == s2n_stuffer_data_available(&in), S2N_ERR_BAD_MESSAGE); |
| 509 |
|
| 510 |
RESULT_GUARD_POSIX(s2n_collect_client_hello(client_hello, &in)); |
| 511 |
RESULT_ENSURE(s2n_stuffer_data_available(&in) == 0, S2N_ERR_BAD_MESSAGE); |
| 512 |
|
| 513 |
RESULT_GUARD(s2n_client_hello_parse_raw(client_hello)); |
| 514 |
|
| 515 |
*result = client_hello; |
| 516 |
ZERO_TO_DISABLE_DEFER_CLEANUP(client_hello); |
| 517 |
return S2N_RESULT_OK; |
| 518 |
} |
| 519 |
|
| 520 |
struct s2n_client_hello *s2n_client_hello_parse_message(const uint8_t *raw_message, uint32_t raw_message_size) |
| 521 |
{ |
| 522 |
struct s2n_client_hello *result = NULL; |
| 523 |
PTR_GUARD_RESULT(s2n_client_hello_parse_message_impl(&result, raw_message, raw_message_size)); |
| 524 |
return result; |
| 525 |
} |
| 526 |
|
| 527 |
int s2n_process_client_hello(struct s2n_connection *conn) |
| 528 |
{ |
| 529 |
POSIX_ENSURE_REF(conn); |
| 530 |
POSIX_ENSURE_REF(conn->secure); |
| 531 |
POSIX_ENSURE_REF(conn->secure->cipher_suite); |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
struct s2n_client_hello *client_hello = &conn->client_hello; |
| 536 |
|
| 537 |
const struct s2n_security_policy *security_policy = NULL; |
| 538 |
POSIX_GUARD(s2n_connection_get_security_policy(conn, &security_policy)); |
| 539 |
|
| 540 |
if (!s2n_connection_supports_tls13(conn) || !s2n_security_policy_supports_tls13(security_policy)) { |
| 541 |
conn->server_protocol_version = S2N_MIN(conn->server_protocol_version, S2N_TLS12); |
| 542 |
conn->actual_protocol_version = S2N_MIN(conn->server_protocol_version, S2N_TLS12); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
const struct s2n_ecc_preferences *ecc_pref = NULL; |
| 561 |
POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref)); |
| 562 |
POSIX_ENSURE_REF(ecc_pref); |
| 563 |
|
| 564 |
if (ecc_pref->count == 0) { |
| 565 |
conn->kex_params.server_ecc_evp_params.negotiated_curve = NULL; |
| 566 |
} else if (s2n_ecc_preferences_includes_curve(ecc_pref, TLS_EC_CURVE_SECP_256_R1)) { |
| 567 |
conn->kex_params.server_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp256r1; |
| 568 |
} else { |
| 569 |
|
| 570 |
conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0]; |
| 571 |
} |
| 572 |
|
| 573 |
POSIX_GUARD(s2n_extension_list_process(S2N_EXTENSION_LIST_CLIENT_HELLO, conn, &conn->client_hello.extensions)); |
| 574 |
|
| 575 |
|
| 576 |
if (conn->actual_protocol_version >= S2N_TLS13) { |
| 577 |
POSIX_GUARD(s2n_extensions_server_key_share_select(conn)); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
if (conn->actual_protocol_version < S2N_TLS13) { |
| 582 |
conn->actual_protocol_version = S2N_MIN(conn->server_protocol_version, conn->client_protocol_version); |
| 583 |
} |
| 584 |
|
| 585 |
if (conn->client_protocol_version < security_policy->minimum_protocol_version) { |
| 586 |
POSIX_GUARD(s2n_queue_reader_unsupported_protocol_version_alert(conn)); |
| 587 |
POSIX_BAIL(S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 588 |
} |
| 589 |
|
| 590 |
if (s2n_connection_is_quic_enabled(conn)) { |
| 591 |
POSIX_ENSURE(conn->actual_protocol_version >= S2N_TLS13, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
POSIX_GUARD(s2n_conn_find_name_matching_certs(conn)); |
| 596 |
|
| 597 |
|
| 598 |
uint8_t previous_cipher_suite_iana[S2N_TLS_CIPHER_SUITE_LEN] = { 0 }; |
| 599 |
POSIX_CHECKED_MEMCPY(previous_cipher_suite_iana, conn->secure->cipher_suite->iana_value, S2N_TLS_CIPHER_SUITE_LEN); |
| 600 |
|
| 601 |
|
| 602 |
if (conn->client_hello.sslv2) { |
| 603 |
POSIX_GUARD(s2n_set_cipher_as_sslv2_server(conn, client_hello->cipher_suites.data, |
| 604 |
client_hello->cipher_suites.size / S2N_SSLv2_CIPHER_SUITE_LEN)); |
| 605 |
} else { |
| 606 |
POSIX_GUARD(s2n_set_cipher_as_tls_server(conn, client_hello->cipher_suites.data, |
| 607 |
client_hello->cipher_suites.size / 2)); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
if (s2n_is_hello_retry_handshake(conn) && conn->handshake.message_number > 0) { |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
POSIX_ENSURE(s2n_constant_time_equals(previous_cipher_suite_iana, conn->secure->cipher_suite->iana_value, |
| 620 |
S2N_TLS_CIPHER_SUITE_LEN), |
| 621 |
S2N_ERR_BAD_MESSAGE); |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
if (conn->psk_params.chosen_psk != NULL) { |
| 627 |
return S2N_SUCCESS; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
POSIX_GUARD_RESULT(s2n_signature_algorithm_select(conn)); |
| 632 |
|
| 633 |
|
| 634 |
POSIX_GUARD(s2n_select_certs_for_server_auth(conn, &conn->handshake_params.our_chain_and_key)); |
| 635 |
|
| 636 |
return S2N_SUCCESS; |
| 637 |
} |
| 638 |
|
| 639 |
static S2N_RESULT s2n_client_hello_process_cb_response(struct s2n_connection *conn, int rc) |
| 640 |
{ |
| 641 |
if (rc < 0) { |
| 642 |
goto fail; |
| 643 |
} |
| 644 |
switch (conn->config->client_hello_cb_mode) { |
| 645 |
case S2N_CLIENT_HELLO_CB_BLOCKING: { |
| 646 |
if (rc) { |
| 647 |
conn->server_name_used = 1; |
| 648 |
} |
| 649 |
return S2N_RESULT_OK; |
| 650 |
} |
| 651 |
case S2N_CLIENT_HELLO_CB_NONBLOCKING: { |
| 652 |
if (conn->client_hello.callback_async_done) { |
| 653 |
return S2N_RESULT_OK; |
| 654 |
} |
| 655 |
conn->client_hello.callback_async_blocked = 1; |
| 656 |
RESULT_BAIL(S2N_ERR_ASYNC_BLOCKED); |
| 657 |
} |
| 658 |
} |
| 659 |
fail: |
| 660 |
|
| 661 |
RESULT_GUARD_POSIX(s2n_queue_reader_handshake_failure_alert(conn)); |
| 662 |
RESULT_BAIL(S2N_ERR_CANCELLED); |
| 663 |
} |
| 664 |
|
| 665 |
int s2n_client_hello_recv(struct s2n_connection *conn) |
| 666 |
{ |
| 667 |
POSIX_ENSURE(!conn->client_hello.callback_async_blocked, S2N_ERR_ASYNC_BLOCKED); |
| 668 |
|
| 669 |
|
| 670 |
if (!conn->client_hello.parsed) { |
| 671 |
POSIX_GUARD(s2n_parse_client_hello(conn)); |
| 672 |
|
| 673 |
conn->client_hello.parsed = true; |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
if (!conn->client_hello.callback_invoked && !IS_HELLO_RETRY_HANDSHAKE(conn)) { |
| 682 |
|
| 683 |
conn->client_hello.callback_invoked = true; |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
POSIX_ENSURE(conn->config, S2N_ERR_CONFIG_NULL_BEFORE_CH_CALLBACK); |
| 690 |
|
| 691 |
|
| 692 |
if (conn->config->client_hello_cb) { |
| 693 |
int rc = conn->config->client_hello_cb(conn, conn->config->client_hello_cb_ctx); |
| 694 |
POSIX_GUARD_RESULT(s2n_client_hello_process_cb_response(conn, rc)); |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
POSIX_GUARD(s2n_process_client_hello(conn)); |
| 699 |
|
| 700 |
return 0; |
| 701 |
} |
| 702 |
|
| 703 |
S2N_RESULT s2n_cipher_suite_validate_available(struct s2n_connection *conn, struct s2n_cipher_suite *cipher) |
| 704 |
{ |
| 705 |
RESULT_ENSURE_REF(conn); |
| 706 |
RESULT_ENSURE_REF(cipher); |
| 707 |
RESULT_ENSURE_EQ(cipher->available, true); |
| 708 |
RESULT_ENSURE_LTE(cipher->minimum_required_tls_version, conn->client_protocol_version); |
| 709 |
if (s2n_connection_is_quic_enabled(conn)) { |
| 710 |
RESULT_ENSURE_GTE(cipher->minimum_required_tls_version, S2N_TLS13); |
| 711 |
} |
| 712 |
return S2N_RESULT_OK; |
| 713 |
} |
| 714 |
|
| 715 |
int s2n_client_hello_send(struct s2n_connection *conn) |
| 716 |
{ |
| 717 |
POSIX_ENSURE_REF(conn); |
| 718 |
|
| 719 |
const struct s2n_security_policy *security_policy = NULL; |
| 720 |
POSIX_GUARD(s2n_connection_get_security_policy(conn, &security_policy)); |
| 721 |
|
| 722 |
const struct s2n_cipher_preferences *cipher_preferences = security_policy->cipher_preferences; |
| 723 |
POSIX_ENSURE_REF(cipher_preferences); |
| 724 |
|
| 725 |
if (!s2n_connection_supports_tls13(conn) || !s2n_security_policy_supports_tls13(security_policy)) { |
| 726 |
conn->client_protocol_version = S2N_MIN(conn->client_protocol_version, S2N_TLS12); |
| 727 |
conn->actual_protocol_version = S2N_MIN(conn->actual_protocol_version, S2N_TLS12); |
| 728 |
} |
| 729 |
|
| 730 |
struct s2n_stuffer *out = &conn->handshake.io; |
| 731 |
uint8_t client_protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN] = { 0 }; |
| 732 |
|
| 733 |
uint8_t reported_protocol_version = S2N_MIN(conn->client_protocol_version, S2N_TLS12); |
| 734 |
conn->client_hello.legacy_version = reported_protocol_version; |
| 735 |
client_protocol_version[0] = reported_protocol_version / 10; |
| 736 |
client_protocol_version[1] = reported_protocol_version % 10; |
| 737 |
POSIX_GUARD(s2n_stuffer_write_bytes(out, client_protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN)); |
| 738 |
|
| 739 |
struct s2n_blob client_random = { 0 }; |
| 740 |
POSIX_GUARD(s2n_blob_init(&client_random, conn->client_hello.random, S2N_TLS_RANDOM_DATA_LEN)); |
| 741 |
if (!s2n_is_hello_retry_handshake(conn)) { |
| 742 |
|
| 743 |
|
| 744 |
POSIX_GUARD_RESULT(s2n_get_public_random_data(&client_random)); |
| 745 |
} |
| 746 |
POSIX_GUARD(s2n_stuffer_write(out, &client_random)); |
| 747 |
|
| 748 |
POSIX_GUARD_RESULT(s2n_generate_client_session_id(conn)); |
| 749 |
POSIX_GUARD(s2n_stuffer_write_uint8(out, conn->session_id_len)); |
| 750 |
if (conn->session_id_len > 0) { |
| 751 |
POSIX_GUARD(s2n_stuffer_write_bytes(out, conn->session_id, conn->session_id_len)); |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
struct s2n_stuffer_reservation available_cipher_suites_size; |
| 756 |
POSIX_GUARD(s2n_stuffer_reserve_uint16(out, &available_cipher_suites_size)); |
| 757 |
|
| 758 |
|
| 759 |
struct s2n_cipher_suite *cipher = NULL; |
| 760 |
bool tls12_is_possible = false; |
| 761 |
for (size_t i = 0; i < security_policy->cipher_preferences->count; i++) { |
| 762 |
cipher = cipher_preferences->suites[i]; |
| 763 |
if (s2n_result_is_error(s2n_cipher_suite_validate_available(conn, cipher))) { |
| 764 |
continue; |
| 765 |
} |
| 766 |
if (cipher->minimum_required_tls_version < S2N_TLS13) { |
| 767 |
tls12_is_possible = true; |
| 768 |
} |
| 769 |
POSIX_GUARD(s2n_stuffer_write_bytes(out, cipher->iana_value, S2N_TLS_CIPHER_SUITE_LEN)); |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
if (tls12_is_possible && !s2n_handshake_is_renegotiation(conn)) { |
| 787 |
uint8_t renegotiation_info_scsv[S2N_TLS_CIPHER_SUITE_LEN] = { TLS_EMPTY_RENEGOTIATION_INFO_SCSV }; |
| 788 |
POSIX_GUARD(s2n_stuffer_write_bytes(out, renegotiation_info_scsv, S2N_TLS_CIPHER_SUITE_LEN)); |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
uint32_t ciphers_size = 0; |
| 793 |
POSIX_GUARD(s2n_stuffer_get_vector_size(&available_cipher_suites_size, &ciphers_size)); |
| 794 |
POSIX_ENSURE(ciphers_size > 0, S2N_ERR_INVALID_CIPHER_PREFERENCES); |
| 795 |
POSIX_GUARD(s2n_stuffer_write_reservation(&available_cipher_suites_size, ciphers_size)); |
| 796 |
|
| 797 |
|
| 798 |
POSIX_GUARD(s2n_stuffer_write_uint8(out, 1)); |
| 799 |
POSIX_GUARD(s2n_stuffer_write_uint8(out, 0)); |
| 800 |
|
| 801 |
|
| 802 |
POSIX_GUARD(s2n_extension_list_send(S2N_EXTENSION_LIST_CLIENT_HELLO, conn, out)); |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
POSIX_GUARD_RESULT(s2n_finish_psk_extension(conn)); |
| 810 |
|
| 811 |
|
| 812 |
if (conn->early_data_state == S2N_UNKNOWN_EARLY_DATA_STATE) { |
| 813 |
POSIX_GUARD_RESULT(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_NOT_REQUESTED)); |
| 814 |
} |
| 815 |
|
| 816 |
return S2N_SUCCESS; |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
int s2n_sslv2_client_hello_parse(struct s2n_connection *conn) |
| 833 |
{ |
| 834 |
struct s2n_client_hello *client_hello = &conn->client_hello; |
| 835 |
conn->client_protocol_version = S2N_MIN(client_hello->legacy_version, S2N_TLS12); |
| 836 |
|
| 837 |
struct s2n_stuffer in_stuffer = { 0 }; |
| 838 |
POSIX_GUARD(s2n_stuffer_init(&in_stuffer, &client_hello->raw_message)); |
| 839 |
POSIX_GUARD(s2n_stuffer_skip_write(&in_stuffer, client_hello->raw_message.size)); |
| 840 |
struct s2n_stuffer *in = &in_stuffer; |
| 841 |
|
| 842 |
|
| 843 |
uint16_t cipher_suites_length = 0; |
| 844 |
POSIX_GUARD(s2n_stuffer_read_uint16(in, &cipher_suites_length)); |
| 845 |
POSIX_ENSURE(cipher_suites_length > 0, S2N_ERR_BAD_MESSAGE); |
| 846 |
POSIX_ENSURE(cipher_suites_length % S2N_SSLv2_CIPHER_SUITE_LEN == 0, S2N_ERR_BAD_MESSAGE); |
| 847 |
|
| 848 |
uint16_t session_id_length = 0; |
| 849 |
POSIX_GUARD(s2n_stuffer_read_uint16(in, &session_id_length)); |
| 850 |
|
| 851 |
uint16_t challenge_length = 0; |
| 852 |
POSIX_GUARD(s2n_stuffer_read_uint16(in, &challenge_length)); |
| 853 |
|
| 854 |
S2N_ERROR_IF(challenge_length > S2N_TLS_RANDOM_DATA_LEN, S2N_ERR_BAD_MESSAGE); |
| 855 |
|
| 856 |
client_hello->cipher_suites.size = cipher_suites_length; |
| 857 |
client_hello->cipher_suites.data = s2n_stuffer_raw_read(in, cipher_suites_length); |
| 858 |
POSIX_ENSURE_REF(client_hello->cipher_suites.data); |
| 859 |
|
| 860 |
S2N_ERROR_IF(session_id_length > s2n_stuffer_data_available(in), S2N_ERR_BAD_MESSAGE); |
| 861 |
POSIX_GUARD(s2n_blob_init(&client_hello->session_id, s2n_stuffer_raw_read(in, session_id_length), session_id_length)); |
| 862 |
if (session_id_length > 0 && session_id_length <= S2N_TLS_SESSION_ID_MAX_LEN) { |
| 863 |
POSIX_CHECKED_MEMCPY(conn->session_id, client_hello->session_id.data, session_id_length); |
| 864 |
conn->session_id_len = (uint8_t) session_id_length; |
| 865 |
} |
| 866 |
|
| 867 |
struct s2n_blob b = { 0 }; |
| 868 |
POSIX_GUARD(s2n_blob_init(&b, conn->client_hello.random, S2N_TLS_RANDOM_DATA_LEN)); |
| 869 |
|
| 870 |
b.data += S2N_TLS_RANDOM_DATA_LEN - challenge_length; |
| 871 |
b.size -= S2N_TLS_RANDOM_DATA_LEN - challenge_length; |
| 872 |
|
| 873 |
POSIX_GUARD(s2n_stuffer_read(in, &b)); |
| 874 |
|
| 875 |
return 0; |
| 876 |
} |
| 877 |
|
| 878 |
int s2n_client_hello_get_parsed_extension(s2n_tls_extension_type extension_type, |
| 879 |
s2n_parsed_extensions_list *parsed_extension_list, s2n_parsed_extension **parsed_extension) |
| 880 |
{ |
| 881 |
POSIX_ENSURE_REF(parsed_extension_list); |
| 882 |
POSIX_ENSURE_REF(parsed_extension); |
| 883 |
|
| 884 |
s2n_extension_type_id extension_type_id = 0; |
| 885 |
POSIX_GUARD(s2n_extension_supported_iana_value_to_id(extension_type, &extension_type_id)); |
| 886 |
|
| 887 |
s2n_parsed_extension *found_parsed_extension = &parsed_extension_list->parsed_extensions[extension_type_id]; |
| 888 |
POSIX_ENSURE(found_parsed_extension->extension.data, S2N_ERR_EXTENSION_NOT_RECEIVED); |
| 889 |
POSIX_ENSURE(found_parsed_extension->extension_type == extension_type, S2N_ERR_INVALID_PARSED_EXTENSIONS); |
| 890 |
|
| 891 |
*parsed_extension = found_parsed_extension; |
| 892 |
return S2N_SUCCESS; |
| 893 |
} |
| 894 |
|
| 895 |
ssize_t s2n_client_hello_get_extension_length(struct s2n_client_hello *ch, s2n_tls_extension_type extension_type) |
| 896 |
{ |
| 897 |
POSIX_ENSURE_REF(ch); |
| 898 |
|
| 899 |
s2n_parsed_extension *parsed_extension = NULL; |
| 900 |
if (s2n_client_hello_get_parsed_extension(extension_type, &ch->extensions, &parsed_extension) != S2N_SUCCESS) { |
| 901 |
return 0; |
| 902 |
} |
| 903 |
|
| 904 |
return parsed_extension->extension.size; |
| 905 |
} |
| 906 |
|
| 907 |
ssize_t s2n_client_hello_get_extension_by_id(struct s2n_client_hello *ch, s2n_tls_extension_type extension_type, uint8_t *out, uint32_t max_length) |
| 908 |
{ |
| 909 |
POSIX_ENSURE_REF(ch); |
| 910 |
POSIX_ENSURE_REF(out); |
| 911 |
|
| 912 |
s2n_parsed_extension *parsed_extension = NULL; |
| 913 |
if (s2n_client_hello_get_parsed_extension(extension_type, &ch->extensions, &parsed_extension) != S2N_SUCCESS) { |
| 914 |
return 0; |
| 915 |
} |
| 916 |
|
| 917 |
uint32_t len = min_size(&parsed_extension->extension, max_length); |
| 918 |
POSIX_CHECKED_MEMCPY(out, parsed_extension->extension.data, len); |
| 919 |
return len; |
| 920 |
} |
| 921 |
|
| 922 |
int s2n_client_hello_get_session_id_length(struct s2n_client_hello *ch, uint32_t *out_length) |
| 923 |
{ |
| 924 |
POSIX_ENSURE_REF(ch); |
| 925 |
POSIX_ENSURE_REF(out_length); |
| 926 |
*out_length = ch->session_id.size; |
| 927 |
return S2N_SUCCESS; |
| 928 |
} |
| 929 |
|
| 930 |
int s2n_client_hello_get_session_id(struct s2n_client_hello *ch, uint8_t *out, uint32_t *out_length, uint32_t max_length) |
| 931 |
{ |
| 932 |
POSIX_ENSURE_REF(ch); |
| 933 |
POSIX_ENSURE_REF(out); |
| 934 |
POSIX_ENSURE_REF(out_length); |
| 935 |
|
| 936 |
uint32_t len = min_size(&ch->session_id, max_length); |
| 937 |
POSIX_CHECKED_MEMCPY(out, ch->session_id.data, len); |
| 938 |
*out_length = len; |
| 939 |
|
| 940 |
return S2N_SUCCESS; |
| 941 |
} |
| 942 |
|
| 943 |
int s2n_client_hello_get_compression_methods_length(struct s2n_client_hello *ch, uint32_t *out_length) |
| 944 |
{ |
| 945 |
POSIX_ENSURE_REF(ch); |
| 946 |
POSIX_ENSURE_REF(out_length); |
| 947 |
*out_length = ch->compression_methods.size; |
| 948 |
return S2N_SUCCESS; |
| 949 |
} |
| 950 |
|
| 951 |
int s2n_client_hello_get_compression_methods(struct s2n_client_hello *ch, uint8_t *list, uint32_t list_length, uint32_t *out_length) |
| 952 |
{ |
| 953 |
POSIX_ENSURE_REF(ch); |
| 954 |
POSIX_ENSURE_REF(list); |
| 955 |
POSIX_ENSURE_REF(out_length); |
| 956 |
|
| 957 |
POSIX_ENSURE(list_length >= ch->compression_methods.size, S2N_ERR_INSUFFICIENT_MEM_SIZE); |
| 958 |
POSIX_CHECKED_MEMCPY(list, ch->compression_methods.data, ch->compression_methods.size); |
| 959 |
*out_length = ch->compression_methods.size; |
| 960 |
return S2N_SUCCESS; |
| 961 |
} |
| 962 |
|
| 963 |
int s2n_client_hello_get_legacy_protocol_version(struct s2n_client_hello *ch, uint8_t *out) |
| 964 |
{ |
| 965 |
POSIX_ENSURE_REF(ch); |
| 966 |
POSIX_ENSURE_REF(out); |
| 967 |
*out = ch->legacy_version; |
| 968 |
return S2N_SUCCESS; |
| 969 |
} |
| 970 |
|
| 971 |
int s2n_client_hello_get_legacy_record_version(struct s2n_client_hello *ch, uint8_t *out) |
| 972 |
{ |
| 973 |
POSIX_ENSURE_REF(ch); |
| 974 |
POSIX_ENSURE_REF(out); |
| 975 |
POSIX_ENSURE(ch->record_version_recorded, S2N_ERR_INVALID_ARGUMENT); |
| 976 |
*out = ch->legacy_record_version; |
| 977 |
return S2N_SUCCESS; |
| 978 |
} |
| 979 |
|
| 980 |
S2N_RESULT s2n_client_hello_get_raw_extension(uint16_t extension_iana, |
| 981 |
struct s2n_blob *raw_extensions, struct s2n_blob *extension) |
| 982 |
{ |
| 983 |
RESULT_ENSURE_REF(raw_extensions); |
| 984 |
RESULT_ENSURE_REF(extension); |
| 985 |
|
| 986 |
*extension = (struct s2n_blob){ 0 }; |
| 987 |
|
| 988 |
struct s2n_stuffer raw_extensions_stuffer = { 0 }; |
| 989 |
RESULT_GUARD_POSIX(s2n_stuffer_init(&raw_extensions_stuffer, raw_extensions)); |
| 990 |
RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&raw_extensions_stuffer, raw_extensions->size)); |
| 991 |
|
| 992 |
while (s2n_stuffer_data_available(&raw_extensions_stuffer) > 0) { |
| 993 |
uint16_t extension_type = 0; |
| 994 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&raw_extensions_stuffer, &extension_type)); |
| 995 |
|
| 996 |
uint16_t extension_size = 0; |
| 997 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&raw_extensions_stuffer, &extension_size)); |
| 998 |
|
| 999 |
uint8_t *extension_data = s2n_stuffer_raw_read(&raw_extensions_stuffer, extension_size); |
| 1000 |
RESULT_ENSURE_REF(extension_data); |
| 1001 |
|
| 1002 |
if (extension_iana == extension_type) { |
| 1003 |
RESULT_GUARD_POSIX(s2n_blob_init(extension, extension_data, extension_size)); |
| 1004 |
return S2N_RESULT_OK; |
| 1005 |
} |
| 1006 |
} |
| 1007 |
return S2N_RESULT_OK; |
| 1008 |
} |
| 1009 |
|
| 1010 |
int s2n_client_hello_has_extension(struct s2n_client_hello *ch, uint16_t extension_iana, bool *exists) |
| 1011 |
{ |
| 1012 |
POSIX_ENSURE_REF(ch); |
| 1013 |
POSIX_ENSURE_REF(exists); |
| 1014 |
|
| 1015 |
*exists = false; |
| 1016 |
|
| 1017 |
s2n_extension_type_id extension_type_id = s2n_unsupported_extension; |
| 1018 |
if (s2n_extension_supported_iana_value_to_id(extension_iana, &extension_type_id) == S2N_SUCCESS) { |
| 1019 |
s2n_parsed_extension *parsed_extension = NULL; |
| 1020 |
if (s2n_client_hello_get_parsed_extension(extension_iana, &ch->extensions, &parsed_extension) == S2N_SUCCESS) { |
| 1021 |
*exists = true; |
| 1022 |
} |
| 1023 |
return S2N_SUCCESS; |
| 1024 |
} |
| 1025 |
|
| 1026 |
struct s2n_blob extension = { 0 }; |
| 1027 |
POSIX_GUARD_RESULT(s2n_client_hello_get_raw_extension(extension_iana, &ch->extensions.raw, &extension)); |
| 1028 |
if (extension.data != NULL) { |
| 1029 |
*exists = true; |
| 1030 |
} |
| 1031 |
return S2N_SUCCESS; |
| 1032 |
} |
| 1033 |
|
| 1034 |
int s2n_client_hello_get_supported_groups(struct s2n_client_hello *ch, uint16_t *groups, |
| 1035 |
uint16_t groups_count_max, uint16_t *groups_count_out) |
| 1036 |
{ |
| 1037 |
POSIX_ENSURE_REF(groups_count_out); |
| 1038 |
*groups_count_out = 0; |
| 1039 |
POSIX_ENSURE_REF(ch); |
| 1040 |
POSIX_ENSURE_REF(groups); |
| 1041 |
|
| 1042 |
s2n_parsed_extension *supported_groups_extension = NULL; |
| 1043 |
POSIX_GUARD(s2n_client_hello_get_parsed_extension(S2N_EXTENSION_SUPPORTED_GROUPS, &ch->extensions, &supported_groups_extension)); |
| 1044 |
POSIX_ENSURE_REF(supported_groups_extension); |
| 1045 |
|
| 1046 |
struct s2n_stuffer extension_stuffer = { 0 }; |
| 1047 |
POSIX_GUARD(s2n_stuffer_init_written(&extension_stuffer, &supported_groups_extension->extension)); |
| 1048 |
|
| 1049 |
uint16_t supported_groups_count = 0; |
| 1050 |
POSIX_GUARD_RESULT(s2n_supported_groups_parse_count(&extension_stuffer, &supported_groups_count)); |
| 1051 |
POSIX_ENSURE(supported_groups_count <= groups_count_max, S2N_ERR_INSUFFICIENT_MEM_SIZE); |
| 1052 |
|
| 1053 |
for (size_t i = 0; i < supported_groups_count; i++) { |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
POSIX_GUARD(s2n_stuffer_read_uint16(&extension_stuffer, &groups[i])); |
| 1058 |
} |
| 1059 |
|
| 1060 |
*groups_count_out = supported_groups_count; |
| 1061 |
|
| 1062 |
return S2N_SUCCESS; |
| 1063 |
} |
| 1064 |
|
| 1065 |
int s2n_client_hello_get_server_name_length(struct s2n_client_hello *ch, uint16_t *length) |
| 1066 |
{ |
| 1067 |
POSIX_ENSURE_REF(ch); |
| 1068 |
POSIX_ENSURE_REF(length); |
| 1069 |
*length = 0; |
| 1070 |
|
| 1071 |
s2n_parsed_extension *server_name_extension = NULL; |
| 1072 |
POSIX_GUARD(s2n_client_hello_get_parsed_extension(S2N_EXTENSION_SERVER_NAME, &ch->extensions, &server_name_extension)); |
| 1073 |
POSIX_ENSURE_REF(server_name_extension); |
| 1074 |
|
| 1075 |
struct s2n_stuffer extension_stuffer = { 0 }; |
| 1076 |
POSIX_GUARD(s2n_stuffer_init_written(&extension_stuffer, &server_name_extension->extension)); |
| 1077 |
|
| 1078 |
struct s2n_blob blob = { 0 }; |
| 1079 |
POSIX_GUARD_RESULT(s2n_client_server_name_parse(&extension_stuffer, &blob)); |
| 1080 |
*length = blob.size; |
| 1081 |
|
| 1082 |
return S2N_SUCCESS; |
| 1083 |
} |
| 1084 |
|
| 1085 |
int s2n_client_hello_get_server_name(struct s2n_client_hello *ch, uint8_t *server_name, uint16_t length, uint16_t *out_length) |
| 1086 |
{ |
| 1087 |
POSIX_ENSURE_REF(out_length); |
| 1088 |
POSIX_ENSURE_REF(ch); |
| 1089 |
POSIX_ENSURE_REF(server_name); |
| 1090 |
*out_length = 0; |
| 1091 |
|
| 1092 |
s2n_parsed_extension *server_name_extension = NULL; |
| 1093 |
POSIX_GUARD(s2n_client_hello_get_parsed_extension(S2N_EXTENSION_SERVER_NAME, &ch->extensions, &server_name_extension)); |
| 1094 |
POSIX_ENSURE_REF(server_name_extension); |
| 1095 |
|
| 1096 |
struct s2n_stuffer extension_stuffer = { 0 }; |
| 1097 |
POSIX_GUARD(s2n_stuffer_init_written(&extension_stuffer, &server_name_extension->extension)); |
| 1098 |
|
| 1099 |
struct s2n_blob blob = { 0 }; |
| 1100 |
POSIX_GUARD_RESULT(s2n_client_server_name_parse(&extension_stuffer, &blob)); |
| 1101 |
POSIX_ENSURE_LTE(blob.size, length); |
| 1102 |
POSIX_CHECKED_MEMCPY(server_name, blob.data, blob.size); |
| 1103 |
|
| 1104 |
*out_length = blob.size; |
| 1105 |
|
| 1106 |
return S2N_SUCCESS; |
| 1107 |
} |