| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <stdint.h> |
| 17 |
|
| 18 |
#include "error/s2n_errno.h" |
| 19 |
#include "stuffer/s2n_stuffer.h" |
| 20 |
#include "tls/s2n_cipher_suites.h" |
| 21 |
#include "tls/s2n_connection.h" |
| 22 |
#include "tls/s2n_record.h" |
| 23 |
#include "tls/s2n_tls.h" |
| 24 |
#include "utils/s2n_map.h" |
| 25 |
#include "utils/s2n_safety.h" |
| 26 |
|
| 27 |
int s2n_handshake_write_header(struct s2n_stuffer *out, uint8_t message_type) |
| 28 |
{ |
| 29 |
S2N_ERROR_IF(s2n_stuffer_data_available(out), S2N_ERR_HANDSHAKE_STATE); |
| 30 |
|
| 31 |
|
| 32 |
POSIX_GUARD(s2n_stuffer_write_uint8(out, message_type)); |
| 33 |
|
| 34 |
|
| 35 |
uint16_t length = 0; |
| 36 |
POSIX_GUARD(s2n_stuffer_write_uint24(out, length)); |
| 37 |
|
| 38 |
return S2N_SUCCESS; |
| 39 |
} |
| 40 |
|
| 41 |
int s2n_handshake_finish_header(struct s2n_stuffer *out) |
| 42 |
{ |
| 43 |
uint32_t length = s2n_stuffer_data_available(out); |
| 44 |
S2N_ERROR_IF(length < TLS_HANDSHAKE_HEADER_LENGTH, S2N_ERR_SIZE_MISMATCH); |
| 45 |
|
| 46 |
uint32_t payload = length - TLS_HANDSHAKE_HEADER_LENGTH; |
| 47 |
|
| 48 |
|
| 49 |
POSIX_GUARD(s2n_stuffer_rewrite(out)); |
| 50 |
POSIX_GUARD(s2n_stuffer_skip_write(out, 1)); |
| 51 |
POSIX_GUARD(s2n_stuffer_write_uint24(out, payload)); |
| 52 |
POSIX_GUARD(s2n_stuffer_skip_write(out, payload)); |
| 53 |
|
| 54 |
return S2N_SUCCESS; |
| 55 |
} |
| 56 |
|
| 57 |
S2N_RESULT s2n_handshake_parse_header(struct s2n_stuffer *io, uint8_t *message_type, uint32_t *length) |
| 58 |
{ |
| 59 |
RESULT_ENSURE(s2n_stuffer_data_available(io) >= TLS_HANDSHAKE_HEADER_LENGTH, S2N_ERR_SIZE_MISMATCH); |
| 60 |
|
| 61 |
|
| 62 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(io, message_type)); |
| 63 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint24(io, length)); |
| 64 |
|
| 65 |
return S2N_RESULT_OK; |
| 66 |
} |
| 67 |
|
| 68 |
static int s2n_handshake_get_hash_state_ptr(struct s2n_connection *conn, s2n_hash_algorithm hash_alg, struct s2n_hash_state **hash_state) |
| 69 |
{ |
| 70 |
POSIX_ENSURE_REF(conn); |
| 71 |
POSIX_ENSURE_REF(conn->handshake.hashes); |
| 72 |
|
| 73 |
switch (hash_alg) { |
| 74 |
case S2N_HASH_MD5: |
| 75 |
*hash_state = &conn->handshake.hashes->md5; |
| 76 |
break; |
| 77 |
case S2N_HASH_SHA1: |
| 78 |
*hash_state = &conn->handshake.hashes->sha1; |
| 79 |
break; |
| 80 |
case S2N_HASH_SHA224: |
| 81 |
*hash_state = &conn->handshake.hashes->sha224; |
| 82 |
break; |
| 83 |
case S2N_HASH_SHA256: |
| 84 |
*hash_state = &conn->handshake.hashes->sha256; |
| 85 |
break; |
| 86 |
case S2N_HASH_SHA384: |
| 87 |
*hash_state = &conn->handshake.hashes->sha384; |
| 88 |
break; |
| 89 |
case S2N_HASH_SHA512: |
| 90 |
*hash_state = &conn->handshake.hashes->sha512; |
| 91 |
break; |
| 92 |
case S2N_HASH_MD5_SHA1: |
| 93 |
*hash_state = &conn->handshake.hashes->md5_sha1; |
| 94 |
break; |
| 95 |
default: |
| 96 |
POSIX_BAIL(S2N_ERR_HASH_INVALID_ALGORITHM); |
| 97 |
break; |
| 98 |
} |
| 99 |
|
| 100 |
return S2N_SUCCESS; |
| 101 |
} |
| 102 |
|
| 103 |
S2N_RESULT s2n_handshake_reset_hash_state(struct s2n_connection *conn, s2n_hash_algorithm hash_alg) |
| 104 |
{ |
| 105 |
struct s2n_hash_state *hash_state = NULL; |
| 106 |
RESULT_GUARD_POSIX(s2n_handshake_get_hash_state_ptr(conn, hash_alg, &hash_state)); |
| 107 |
RESULT_GUARD_POSIX(s2n_hash_reset(hash_state)); |
| 108 |
return S2N_RESULT_OK; |
| 109 |
} |
| 110 |
|
| 111 |
S2N_RESULT s2n_handshake_copy_hash_state(struct s2n_connection *conn, s2n_hash_algorithm hash_alg, struct s2n_hash_state *copy) |
| 112 |
{ |
| 113 |
struct s2n_hash_state *hash_state = NULL; |
| 114 |
RESULT_GUARD_POSIX(s2n_handshake_get_hash_state_ptr(conn, hash_alg, &hash_state)); |
| 115 |
RESULT_GUARD_POSIX(s2n_hash_copy(copy, hash_state)); |
| 116 |
return S2N_RESULT_OK; |
| 117 |
} |
| 118 |
|
| 119 |
int s2n_handshake_require_all_hashes(struct s2n_handshake *handshake) |
| 120 |
{ |
| 121 |
memset(handshake->required_hash_algs, 1, sizeof(handshake->required_hash_algs)); |
| 122 |
return S2N_SUCCESS; |
| 123 |
} |
| 124 |
|
| 125 |
static int s2n_handshake_require_hash(struct s2n_handshake *handshake, s2n_hash_algorithm hash_alg) |
| 126 |
{ |
| 127 |
handshake->required_hash_algs[hash_alg] = 1; |
| 128 |
return S2N_SUCCESS; |
| 129 |
} |
| 130 |
|
| 131 |
uint8_t s2n_handshake_is_hash_required(struct s2n_handshake *handshake, s2n_hash_algorithm hash_alg) |
| 132 |
{ |
| 133 |
return handshake->required_hash_algs[hash_alg]; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
int s2n_conn_update_required_handshake_hashes(struct s2n_connection *conn) |
| 142 |
{ |
| 143 |
POSIX_ENSURE_REF(conn); |
| 144 |
POSIX_ENSURE_REF(conn->secure); |
| 145 |
|
| 146 |
|
| 147 |
memset(conn->handshake.required_hash_algs, 0, sizeof(conn->handshake.required_hash_algs)); |
| 148 |
|
| 149 |
if (conn->actual_protocol_version < S2N_TLS13) { |
| 150 |
message_type_t handshake_message = s2n_conn_get_current_message_type(conn); |
| 151 |
const uint8_t client_cert_verify_done = (handshake_message >= CLIENT_CERT_VERIFY) ? 1 : 0; |
| 152 |
s2n_cert_auth_type client_cert_auth_type; |
| 153 |
POSIX_GUARD(s2n_connection_get_client_auth_type(conn, &client_cert_auth_type)); |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
if ((client_cert_auth_type != S2N_CERT_AUTH_NONE) && !client_cert_verify_done) { |
| 159 |
POSIX_GUARD(s2n_handshake_require_all_hashes(&conn->handshake)); |
| 160 |
return S2N_SUCCESS; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
switch (conn->actual_protocol_version) { |
| 166 |
case S2N_SSLv3: |
| 167 |
case S2N_TLS10: |
| 168 |
case S2N_TLS11: |
| 169 |
POSIX_GUARD(s2n_handshake_require_hash(&conn->handshake, S2N_HASH_MD5)); |
| 170 |
POSIX_GUARD(s2n_handshake_require_hash(&conn->handshake, S2N_HASH_SHA1)); |
| 171 |
break; |
| 172 |
case S2N_TLS12: |
| 173 |
|
| 174 |
case S2N_TLS13: { |
| 175 |
|
| 176 |
s2n_hmac_algorithm prf_alg = conn->secure->cipher_suite->prf_alg; |
| 177 |
s2n_hash_algorithm hash_alg; |
| 178 |
POSIX_GUARD(s2n_hmac_hash_alg(prf_alg, &hash_alg)); |
| 179 |
POSIX_GUARD(s2n_handshake_require_hash(&conn->handshake, hash_alg)); |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return S2N_SUCCESS; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
int s2n_create_wildcard_hostname(struct s2n_stuffer *hostname_stuffer, struct s2n_stuffer *output) |
| 208 |
{ |
| 209 |
|
| 210 |
POSIX_GUARD(s2n_stuffer_skip_to_char(hostname_stuffer, '.')); |
| 211 |
|
| 212 |
|
| 213 |
if (s2n_stuffer_data_available(hostname_stuffer) == 0) { |
| 214 |
return S2N_SUCCESS; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
POSIX_GUARD(s2n_stuffer_write_uint8(output, '*')); |
| 219 |
|
| 220 |
|
| 221 |
POSIX_GUARD(s2n_stuffer_copy(hostname_stuffer, output, s2n_stuffer_data_available(hostname_stuffer))); |
| 222 |
|
| 223 |
return S2N_SUCCESS; |
| 224 |
} |
| 225 |
|
| 226 |
static int s2n_find_cert_matches(struct s2n_map *domain_name_to_cert_map, |
| 227 |
struct s2n_blob *dns_name, |
| 228 |
struct s2n_cert_chain_and_key *matches[S2N_CERT_TYPE_COUNT], |
| 229 |
uint8_t *match_exists) |
| 230 |
{ |
| 231 |
struct s2n_blob map_value = { 0 }; |
| 232 |
bool key_found = false; |
| 233 |
POSIX_GUARD_RESULT(s2n_map_lookup(domain_name_to_cert_map, dns_name, &map_value, &key_found)); |
| 234 |
if (key_found) { |
| 235 |
struct certs_by_type *value = (void *) map_value.data; |
| 236 |
for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) { |
| 237 |
matches[i] = value->certs[i]; |
| 238 |
} |
| 239 |
*match_exists = 1; |
| 240 |
} |
| 241 |
|
| 242 |
return S2N_SUCCESS; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
int s2n_conn_find_name_matching_certs(struct s2n_connection *conn) |
| 253 |
{ |
| 254 |
if (!s2n_server_received_server_name(conn)) { |
| 255 |
return S2N_SUCCESS; |
| 256 |
} |
| 257 |
const char *name = conn->server_name; |
| 258 |
struct s2n_blob hostname_blob = { 0 }; |
| 259 |
POSIX_GUARD(s2n_blob_init(&hostname_blob, (uint8_t *) (uintptr_t) name, strlen(name))); |
| 260 |
POSIX_ENSURE_LTE(hostname_blob.size, S2N_MAX_SERVER_NAME); |
| 261 |
char normalized_hostname[S2N_MAX_SERVER_NAME + 1] = { 0 }; |
| 262 |
POSIX_CHECKED_MEMCPY(normalized_hostname, hostname_blob.data, hostname_blob.size); |
| 263 |
struct s2n_blob normalized_name = { 0 }; |
| 264 |
POSIX_GUARD(s2n_blob_init(&normalized_name, (uint8_t *) normalized_hostname, hostname_blob.size)); |
| 265 |
|
| 266 |
POSIX_GUARD(s2n_blob_char_to_lower(&normalized_name)); |
| 267 |
struct s2n_stuffer normalized_hostname_stuffer = { 0 }; |
| 268 |
POSIX_GUARD(s2n_stuffer_init(&normalized_hostname_stuffer, &normalized_name)); |
| 269 |
POSIX_GUARD(s2n_stuffer_skip_write(&normalized_hostname_stuffer, normalized_name.size)); |
| 270 |
|
| 271 |
|
| 272 |
POSIX_GUARD(s2n_find_cert_matches(conn->config->domain_name_to_cert_map, |
| 273 |
&normalized_name, |
| 274 |
conn->handshake_params.exact_sni_matches, |
| 275 |
&(conn->handshake_params.exact_sni_match_exists))); |
| 276 |
|
| 277 |
if (!conn->handshake_params.exact_sni_match_exists) { |
| 278 |
|
| 279 |
char wildcard_hostname[S2N_MAX_SERVER_NAME + 1] = { 0 }; |
| 280 |
struct s2n_blob wildcard_blob = { 0 }; |
| 281 |
POSIX_GUARD(s2n_blob_init(&wildcard_blob, (uint8_t *) wildcard_hostname, sizeof(wildcard_hostname))); |
| 282 |
struct s2n_stuffer wildcard_stuffer = { 0 }; |
| 283 |
POSIX_GUARD(s2n_stuffer_init(&wildcard_stuffer, &wildcard_blob)); |
| 284 |
POSIX_GUARD(s2n_create_wildcard_hostname(&normalized_hostname_stuffer, &wildcard_stuffer)); |
| 285 |
const uint32_t wildcard_len = s2n_stuffer_data_available(&wildcard_stuffer); |
| 286 |
|
| 287 |
|
| 288 |
if (wildcard_len == 0) { |
| 289 |
return S2N_SUCCESS; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
wildcard_blob.size = wildcard_len; |
| 294 |
POSIX_GUARD(s2n_find_cert_matches(conn->config->domain_name_to_cert_map, |
| 295 |
&wildcard_blob, |
| 296 |
conn->handshake_params.wc_sni_matches, |
| 297 |
&(conn->handshake_params.wc_sni_match_exists))); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
conn->server_name_used = conn->server_name_used |
| 304 |
|| conn->handshake_params.exact_sni_match_exists |
| 305 |
|| conn->handshake_params.wc_sni_match_exists; |
| 306 |
|
| 307 |
return S2N_SUCCESS; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
struct s2n_cert_chain_and_key *s2n_get_compatible_cert_chain_and_key(struct s2n_connection *conn, const s2n_pkey_type cert_type) |
| 316 |
{ |
| 317 |
if (conn->handshake_params.exact_sni_match_exists) { |
| 318 |
|
| 319 |
return conn->handshake_params.exact_sni_matches[cert_type]; |
| 320 |
} |
| 321 |
if (conn->handshake_params.wc_sni_match_exists) { |
| 322 |
return conn->handshake_params.wc_sni_matches[cert_type]; |
| 323 |
} else { |
| 324 |
|
| 325 |
return conn->config->default_certs_by_type.certs[cert_type]; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
S2N_RESULT s2n_negotiate_until_message(struct s2n_connection *conn, s2n_blocked_status *blocked, message_type_t end_message) |
| 338 |
{ |
| 339 |
RESULT_ENSURE_REF(conn); |
| 340 |
conn->handshake.end_of_messages = end_message; |
| 341 |
int r = s2n_negotiate(conn, blocked); |
| 342 |
conn->handshake.end_of_messages = APPLICATION_DATA; |
| 343 |
RESULT_GUARD_POSIX(r); |
| 344 |
return S2N_RESULT_OK; |
| 345 |
} |
| 346 |
|
| 347 |
S2N_RESULT s2n_handshake_validate(const struct s2n_handshake *s2n_handshake) |
| 348 |
{ |
| 349 |
RESULT_ENSURE_REF(s2n_handshake); |
| 350 |
RESULT_DEBUG_ENSURE(s2n_handshake->handshake_type < 256, S2N_ERR_SAFETY); |
| 351 |
RESULT_DEBUG_ENSURE(s2n_handshake->message_number >= 0 && s2n_handshake->message_number < 32, S2N_ERR_SAFETY); |
| 352 |
return S2N_RESULT_OK; |
| 353 |
} |
| 354 |
|
| 355 |
S2N_RESULT s2n_handshake_set_finished_len(struct s2n_connection *conn, uint8_t len) |
| 356 |
{ |
| 357 |
RESULT_ENSURE_REF(conn); |
| 358 |
RESULT_ENSURE_GT(len, 0); |
| 359 |
RESULT_ENSURE_LTE(len, sizeof(conn->handshake.server_finished)); |
| 360 |
RESULT_ENSURE_LTE(len, sizeof(conn->handshake.client_finished)); |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
uint8_t *finished_length = &conn->handshake.finished_len; |
| 371 |
if (*finished_length == 0) { |
| 372 |
*finished_length = len; |
| 373 |
} |
| 374 |
RESULT_ENSURE_EQ(*finished_length, len); |
| 375 |
|
| 376 |
return S2N_RESULT_OK; |
| 377 |
} |
| 378 |
|
| 379 |
bool s2n_handshake_is_renegotiation(struct s2n_connection *conn) |
| 380 |
{ |
| 381 |
return conn && conn->handshake.renegotiation; |
| 382 |
} |