| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <time.h> |
| 17 |
|
| 18 |
#include "api/s2n.h" |
| 19 |
#include "crypto/s2n_fips.h" |
| 20 |
#include "error/s2n_errno.h" |
| 21 |
#include "stuffer/s2n_stuffer.h" |
| 22 |
#include "tls/s2n_alerts.h" |
| 23 |
#include "tls/s2n_cipher_preferences.h" |
| 24 |
#include "tls/s2n_cipher_suites.h" |
| 25 |
#include "tls/s2n_connection.h" |
| 26 |
#include "tls/s2n_security_policies.h" |
| 27 |
#include "tls/s2n_server_extensions.h" |
| 28 |
#include "tls/s2n_tls.h" |
| 29 |
#include "tls/s2n_tls13.h" |
| 30 |
#include "tls/s2n_tls13_handshake.h" |
| 31 |
#include "tls/s2n_tls13_key_schedule.h" |
| 32 |
#include "utils/s2n_bitmap.h" |
| 33 |
#include "utils/s2n_random.h" |
| 34 |
#include "utils/s2n_safety.h" |
| 35 |
|
| 36 |
|
| 37 |
#define S2N_TLS_COMPRESSION_METHOD_NULL 0 |
| 38 |
|
| 39 |
|
| 40 |
#define S2N_DOWNGRADE_PROTECTION_SIZE 8 |
| 41 |
const uint8_t tls12_downgrade_protection_bytes[] = { |
| 42 |
0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44, 0x01 |
| 43 |
}; |
| 44 |
|
| 45 |
const uint8_t tls11_downgrade_protection_bytes[] = { |
| 46 |
0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44, 0x00 |
| 47 |
}; |
| 48 |
|
| 49 |
static int s2n_random_value_is_hello_retry(struct s2n_connection *conn) |
| 50 |
{ |
| 51 |
POSIX_ENSURE_REF(conn); |
| 52 |
|
| 53 |
POSIX_ENSURE(s2n_constant_time_equals(hello_retry_req_random, conn->handshake_params.server_random, S2N_TLS_RANDOM_DATA_LEN), |
| 54 |
S2N_ERR_INVALID_HELLO_RETRY); |
| 55 |
|
| 56 |
return S2N_SUCCESS; |
| 57 |
} |
| 58 |
|
| 59 |
static int s2n_client_detect_downgrade_mechanism(struct s2n_connection *conn) |
| 60 |
{ |
| 61 |
POSIX_ENSURE_REF(conn); |
| 62 |
uint8_t *downgrade_bytes = &conn->handshake_params.server_random[S2N_TLS_RANDOM_DATA_LEN - S2N_DOWNGRADE_PROTECTION_SIZE]; |
| 63 |
|
| 64 |
|
| 65 |
if (conn->client_protocol_version == S2N_TLS13 && conn->server_protocol_version == S2N_TLS12) { |
| 66 |
if (s2n_constant_time_equals(downgrade_bytes, tls12_downgrade_protection_bytes, S2N_DOWNGRADE_PROTECTION_SIZE)) { |
| 67 |
POSIX_BAIL(S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED); |
| 68 |
} |
| 69 |
} else if (conn->client_protocol_version == S2N_TLS13 && conn->server_protocol_version <= S2N_TLS11) { |
| 70 |
if (s2n_constant_time_equals(downgrade_bytes, tls11_downgrade_protection_bytes, S2N_DOWNGRADE_PROTECTION_SIZE)) { |
| 71 |
POSIX_BAIL(S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
static int s2n_server_add_downgrade_mechanism(struct s2n_connection *conn) |
| 79 |
{ |
| 80 |
POSIX_ENSURE_REF(conn); |
| 81 |
uint8_t *downgrade_bytes = &conn->handshake_params.server_random[S2N_TLS_RANDOM_DATA_LEN - S2N_DOWNGRADE_PROTECTION_SIZE]; |
| 82 |
|
| 83 |
|
| 84 |
if (conn->server_protocol_version >= S2N_TLS13 && conn->actual_protocol_version == S2N_TLS12) { |
| 85 |
|
| 86 |
POSIX_CHECKED_MEMCPY(downgrade_bytes, tls12_downgrade_protection_bytes, S2N_DOWNGRADE_PROTECTION_SIZE); |
| 87 |
} else if (conn->server_protocol_version >= S2N_TLS13 && conn->actual_protocol_version <= S2N_TLS11) { |
| 88 |
|
| 89 |
POSIX_CHECKED_MEMCPY(downgrade_bytes, tls11_downgrade_protection_bytes, S2N_DOWNGRADE_PROTECTION_SIZE); |
| 90 |
} |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
static int s2n_server_hello_parse(struct s2n_connection *conn) |
| 96 |
{ |
| 97 |
POSIX_ENSURE_REF(conn); |
| 98 |
POSIX_ENSURE_REF(conn->secure); |
| 99 |
|
| 100 |
struct s2n_stuffer *in = &conn->handshake.io; |
| 101 |
uint8_t compression_method = 0; |
| 102 |
uint8_t session_id_len = 0; |
| 103 |
uint8_t protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN]; |
| 104 |
uint8_t session_id[S2N_TLS_SESSION_ID_MAX_LEN]; |
| 105 |
|
| 106 |
POSIX_GUARD(s2n_stuffer_read_bytes(in, protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN)); |
| 107 |
POSIX_GUARD(s2n_stuffer_read_bytes(in, conn->handshake_params.server_random, S2N_TLS_RANDOM_DATA_LEN)); |
| 108 |
|
| 109 |
uint8_t legacy_version = (uint8_t) (protocol_version[0] * 10) + protocol_version[1]; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if (s2n_random_value_is_hello_retry(conn) == S2N_SUCCESS) { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
POSIX_ENSURE(!s2n_is_hello_retry_handshake(conn), S2N_ERR_INVALID_HELLO_RETRY); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
POSIX_ENSURE(legacy_version == S2N_TLS12, S2N_ERR_INVALID_HELLO_RETRY); |
| 133 |
|
| 134 |
POSIX_GUARD(s2n_set_hello_retry_required(conn)); |
| 135 |
} |
| 136 |
|
| 137 |
POSIX_GUARD(s2n_stuffer_read_uint8(in, &session_id_len)); |
| 138 |
S2N_ERROR_IF(session_id_len > S2N_TLS_SESSION_ID_MAX_LEN, S2N_ERR_BAD_MESSAGE); |
| 139 |
POSIX_GUARD(s2n_stuffer_read_bytes(in, session_id, session_id_len)); |
| 140 |
|
| 141 |
uint8_t *cipher_suite_wire = s2n_stuffer_raw_read(in, S2N_TLS_CIPHER_SUITE_LEN); |
| 142 |
POSIX_ENSURE_REF(cipher_suite_wire); |
| 143 |
|
| 144 |
POSIX_GUARD(s2n_stuffer_read_uint8(in, &compression_method)); |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
S2N_ERROR_IF(compression_method != S2N_TLS_COMPRESSION_METHOD_NULL, S2N_ERR_BAD_MESSAGE); |
| 157 |
|
| 158 |
bool session_ids_match = session_id_len != 0 && session_id_len == conn->session_id_len |
| 159 |
&& s2n_constant_time_equals(session_id, conn->session_id, session_id_len); |
| 160 |
if (!session_ids_match) { |
| 161 |
conn->ems_negotiated = false; |
| 162 |
} |
| 163 |
|
| 164 |
POSIX_GUARD(s2n_server_extensions_recv(conn, in)); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
if (s2n_is_hello_retry_message(conn)) { |
| 171 |
s2n_extension_type_id supported_versions_id = s2n_unsupported_extension; |
| 172 |
POSIX_GUARD(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_SUPPORTED_VERSIONS, &supported_versions_id)); |
| 173 |
POSIX_ENSURE(S2N_CBIT_TEST(conn->extension_responses_received, supported_versions_id), |
| 174 |
S2N_ERR_MISSING_EXTENSION); |
| 175 |
} |
| 176 |
|
| 177 |
if (conn->server_protocol_version >= S2N_TLS13) { |
| 178 |
POSIX_ENSURE(!conn->handshake.renegotiation, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
POSIX_ENSURE(session_ids_match || (session_id_len == 0 && conn->session_id_len == 0), S2N_ERR_BAD_MESSAGE); |
| 192 |
|
| 193 |
conn->actual_protocol_version = conn->server_protocol_version; |
| 194 |
POSIX_GUARD(s2n_set_cipher_as_client(conn, cipher_suite_wire)); |
| 195 |
|
| 196 |
|
| 197 |
POSIX_GUARD(s2n_free(&conn->client_ticket)); |
| 198 |
} else { |
| 199 |
conn->server_protocol_version = legacy_version; |
| 200 |
|
| 201 |
POSIX_ENSURE(!s2n_client_detect_downgrade_mechanism(conn), S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED); |
| 202 |
POSIX_ENSURE(!s2n_connection_is_quic_enabled(conn), S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 203 |
|
| 204 |
|
| 205 |
POSIX_ENSURE(!s2n_is_hello_retry_handshake(conn), S2N_ERR_BAD_MESSAGE); |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
POSIX_ENSURE(conn->early_data_state != S2N_EARLY_DATA_REQUESTED, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 213 |
|
| 214 |
const struct s2n_security_policy *security_policy = NULL; |
| 215 |
POSIX_GUARD(s2n_connection_get_security_policy(conn, &security_policy)); |
| 216 |
|
| 217 |
if (conn->server_protocol_version < security_policy->minimum_protocol_version |
| 218 |
|| conn->server_protocol_version > conn->client_protocol_version) { |
| 219 |
POSIX_GUARD(s2n_queue_reader_unsupported_protocol_version_alert(conn)); |
| 220 |
POSIX_BAIL(S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 221 |
} |
| 222 |
|
| 223 |
conn->actual_protocol_version = S2N_MIN(conn->server_protocol_version, conn->client_protocol_version); |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
if (session_ids_match) { |
| 234 |
|
| 235 |
POSIX_ENSURE(conn->resume_protocol_version == conn->actual_protocol_version, S2N_ERR_BAD_MESSAGE); |
| 236 |
POSIX_ENSURE(s2n_constant_time_equals(conn->secure->cipher_suite->iana_value, cipher_suite_wire, S2N_TLS_CIPHER_SUITE_LEN), |
| 237 |
S2N_ERR_BAD_MESSAGE); |
| 238 |
|
| 239 |
|
| 240 |
conn->client_session_resumed = 1; |
| 241 |
} else { |
| 242 |
conn->session_id_len = session_id_len; |
| 243 |
POSIX_CHECKED_MEMCPY(conn->session_id, session_id, session_id_len); |
| 244 |
POSIX_GUARD(s2n_set_cipher_as_client(conn, cipher_suite_wire)); |
| 245 |
|
| 246 |
POSIX_CHECKED_MEMSET((uint8_t *) conn->secrets.version.tls12.master_secret, 0, S2N_TLS_SECRET_LEN); |
| 247 |
|
| 248 |
|
| 249 |
POSIX_GUARD(s2n_free(&conn->client_ticket)); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
POSIX_GUARD_RESULT(s2n_early_data_accept_or_reject(conn)); |
| 259 |
if (conn->early_data_state == S2N_EARLY_DATA_REJECTED) { |
| 260 |
POSIX_GUARD_RESULT(s2n_tls13_key_schedule_reset(conn)); |
| 261 |
} |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
int s2n_server_hello_recv(struct s2n_connection *conn) |
| 267 |
{ |
| 268 |
POSIX_ENSURE_REF(conn); |
| 269 |
|
| 270 |
|
| 271 |
POSIX_GUARD(s2n_server_hello_parse(conn)); |
| 272 |
|
| 273 |
conn->actual_protocol_version_established = 1; |
| 274 |
|
| 275 |
POSIX_GUARD(s2n_conn_set_handshake_type(conn)); |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
if (s2n_is_hello_retry_message(conn)) { |
| 280 |
POSIX_GUARD(s2n_server_hello_retry_recv(conn)); |
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
if (conn->actual_protocol_version < S2N_TLS13 && s2n_connection_is_session_resumed(conn)) { |
| 285 |
POSIX_GUARD(s2n_prf_key_expansion(conn)); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
POSIX_GUARD(s2n_conn_update_required_handshake_hashes(conn)); |
| 290 |
|
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
int s2n_server_hello_write_message(struct s2n_connection *conn) |
| 295 |
{ |
| 296 |
POSIX_ENSURE_REF(conn); |
| 297 |
POSIX_ENSURE_REF(conn->secure); |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
const uint16_t legacy_protocol_version = S2N_MIN(conn->actual_protocol_version, S2N_TLS12); |
| 303 |
uint8_t protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN]; |
| 304 |
protocol_version[0] = (uint8_t) (legacy_protocol_version / 10); |
| 305 |
protocol_version[1] = (uint8_t) (legacy_protocol_version % 10); |
| 306 |
|
| 307 |
POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN)); |
| 308 |
POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, conn->handshake_params.server_random, S2N_TLS_RANDOM_DATA_LEN)); |
| 309 |
POSIX_GUARD(s2n_stuffer_write_uint8(&conn->handshake.io, conn->session_id_len)); |
| 310 |
POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, conn->session_id, conn->session_id_len)); |
| 311 |
POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, conn->secure->cipher_suite->iana_value, S2N_TLS_CIPHER_SUITE_LEN)); |
| 312 |
POSIX_GUARD(s2n_stuffer_write_uint8(&conn->handshake.io, S2N_TLS_COMPRESSION_METHOD_NULL)); |
| 313 |
|
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
int s2n_server_hello_send(struct s2n_connection *conn) |
| 318 |
{ |
| 319 |
POSIX_ENSURE_REF(conn); |
| 320 |
|
| 321 |
struct s2n_stuffer server_random = { 0 }; |
| 322 |
struct s2n_blob b = { 0 }; |
| 323 |
POSIX_GUARD(s2n_blob_init(&b, conn->handshake_params.server_random, S2N_TLS_RANDOM_DATA_LEN)); |
| 324 |
|
| 325 |
|
| 326 |
POSIX_GUARD(s2n_stuffer_init(&server_random, &b)); |
| 327 |
|
| 328 |
struct s2n_blob rand_data = { 0 }; |
| 329 |
POSIX_GUARD(s2n_blob_init(&rand_data, s2n_stuffer_raw_write(&server_random, S2N_TLS_RANDOM_DATA_LEN), S2N_TLS_RANDOM_DATA_LEN)); |
| 330 |
POSIX_ENSURE_REF(rand_data.data); |
| 331 |
POSIX_GUARD_RESULT(s2n_get_public_random_data(&rand_data)); |
| 332 |
|
| 333 |
|
| 334 |
POSIX_GUARD(s2n_server_add_downgrade_mechanism(conn)); |
| 335 |
|
| 336 |
POSIX_GUARD(s2n_server_hello_write_message(conn)); |
| 337 |
|
| 338 |
POSIX_GUARD(s2n_server_extensions_send(conn, &conn->handshake.io)); |
| 339 |
|
| 340 |
conn->actual_protocol_version_established = 1; |
| 341 |
|
| 342 |
return 0; |
| 343 |
} |