| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#pragma once |
| 17 |
|
| 18 |
#include <errno.h> |
| 19 |
#include <signal.h> |
| 20 |
#include <stdint.h> |
| 21 |
|
| 22 |
#include "api/s2n.h" |
| 23 |
#include "crypto/s2n_hash.h" |
| 24 |
#include "crypto/s2n_hmac.h" |
| 25 |
#include "stuffer/s2n_stuffer.h" |
| 26 |
#include "tls/s2n_async_offload.h" |
| 27 |
#include "tls/s2n_client_hello.h" |
| 28 |
#include "tls/s2n_config.h" |
| 29 |
#include "tls/s2n_crypto.h" |
| 30 |
#include "tls/s2n_early_data.h" |
| 31 |
#include "tls/s2n_ecc_preferences.h" |
| 32 |
#include "tls/s2n_handshake.h" |
| 33 |
#include "tls/s2n_kem_preferences.h" |
| 34 |
#include "tls/s2n_key_update.h" |
| 35 |
#include "tls/s2n_post_handshake.h" |
| 36 |
#include "tls/s2n_prf.h" |
| 37 |
#include "tls/s2n_quic_support.h" |
| 38 |
#include "tls/s2n_record.h" |
| 39 |
#include "tls/s2n_resume.h" |
| 40 |
#include "tls/s2n_security_policies.h" |
| 41 |
#include "tls/s2n_tls_parameters.h" |
| 42 |
#include "tls/s2n_x509_validator.h" |
| 43 |
#include "unstable/events.h" |
| 44 |
#include "utils/s2n_atomic.h" |
| 45 |
#include "utils/s2n_mem.h" |
| 46 |
#include "utils/s2n_timer.h" |
| 47 |
|
| 48 |
#define S2N_TLS_PROTOCOL_VERSION_LEN 2 |
| 49 |
|
| 50 |
#define S2N_PEER_MODE(our_mode) ((our_mode + 1) % 2) |
| 51 |
|
| 52 |
#define is_handshake_complete(conn) (APPLICATION_DATA == s2n_conn_get_current_message_type(conn)) |
| 53 |
|
| 54 |
#define S2N_DEFAULT_BLINDING_MAX 30 |
| 55 |
#define S2N_DEFAULT_BLINDING_MIN 10 |
| 56 |
|
| 57 |
typedef enum { |
| 58 |
S2N_NO_TICKET = 0, |
| 59 |
S2N_DECRYPT_TICKET, |
| 60 |
S2N_NEW_TICKET |
| 61 |
} s2n_session_ticket_status; |
| 62 |
|
| 63 |
struct s2n_connection { |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
unsigned corked_io : 1; |
| 68 |
|
| 69 |
|
| 70 |
unsigned client_session_resumed : 1; |
| 71 |
|
| 72 |
|
| 73 |
unsigned quic_enabled : 1; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
unsigned secure_renegotiation : 1; |
| 81 |
|
| 82 |
unsigned ec_point_formats : 1; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
unsigned server_name_used : 1; |
| 89 |
|
| 90 |
|
| 91 |
unsigned write_fd_broken : 1; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
unsigned managed_send_io : 1; |
| 96 |
unsigned managed_recv_io : 1; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
unsigned early_data_expected : 1; |
| 103 |
|
| 104 |
|
| 105 |
unsigned server_max_early_data_size_overridden : 1; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
unsigned psk_mode_overridden : 1; |
| 111 |
|
| 112 |
|
| 113 |
unsigned ems_negotiated : 1; |
| 114 |
|
| 115 |
|
| 116 |
unsigned set_session : 1; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
unsigned multirecord_send : 1; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
unsigned dynamic_buffers : 1; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
unsigned npn_negotiated : 1; |
| 129 |
|
| 130 |
|
| 131 |
unsigned ktls_send_enabled : 1; |
| 132 |
unsigned ktls_recv_enabled : 1; |
| 133 |
|
| 134 |
|
| 135 |
unsigned request_ocsp_status : 1; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
unsigned deserialized_conn : 1; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
unsigned recv_buffering : 1; |
| 148 |
|
| 149 |
|
| 150 |
struct s2n_config *config; |
| 151 |
|
| 152 |
|
| 153 |
const struct s2n_security_policy *security_policy_override; |
| 154 |
|
| 155 |
|
| 156 |
void *context; |
| 157 |
|
| 158 |
|
| 159 |
s2n_secret_cb secret_cb; |
| 160 |
void *secret_cb_context; |
| 161 |
|
| 162 |
|
| 163 |
s2n_send_fn *send; |
| 164 |
s2n_recv_fn *recv; |
| 165 |
|
| 166 |
|
| 167 |
void *send_io_context; |
| 168 |
void *recv_io_context; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
s2n_extension_bitfield extension_requests_sent; |
| 177 |
s2n_extension_bitfield extension_requests_received; |
| 178 |
s2n_extension_bitfield extension_responses_received; |
| 179 |
|
| 180 |
|
| 181 |
s2n_mode mode; |
| 182 |
|
| 183 |
|
| 184 |
s2n_blinding blinding; |
| 185 |
|
| 186 |
|
| 187 |
struct s2n_timer write_timer; |
| 188 |
|
| 189 |
|
| 190 |
uint64_t last_write_elapsed; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
uint64_t delay; |
| 197 |
|
| 198 |
|
| 199 |
uint8_t session_id[S2N_TLS_SESSION_ID_MAX_LEN]; |
| 200 |
uint8_t session_id_len; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
uint8_t client_protocol_version; |
| 206 |
uint8_t server_protocol_version; |
| 207 |
uint8_t actual_protocol_version; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
uint8_t resume_protocol_version; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
uint8_t actual_protocol_version_established; |
| 217 |
|
| 218 |
|
| 219 |
struct s2n_crypto_parameters *initial; |
| 220 |
struct s2n_crypto_parameters *secure; |
| 221 |
struct s2n_secrets secrets; |
| 222 |
|
| 223 |
|
| 224 |
struct s2n_crypto_parameters *client; |
| 225 |
struct s2n_crypto_parameters *server; |
| 226 |
|
| 227 |
|
| 228 |
struct s2n_kex_parameters kex_params; |
| 229 |
|
| 230 |
|
| 231 |
struct s2n_handshake_parameters handshake_params; |
| 232 |
|
| 233 |
|
| 234 |
struct s2n_psk_parameters psk_params; |
| 235 |
|
| 236 |
|
| 237 |
struct s2n_prf_working_space *prf_space; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
uint8_t client_cert_auth_type_overridden; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
s2n_cert_auth_type client_cert_auth_type; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
uint8_t header_in_data[S2N_TLS_RECORD_HEADER_LENGTH]; |
| 254 |
struct s2n_stuffer header_in; |
| 255 |
struct s2n_stuffer buffer_in; |
| 256 |
struct s2n_stuffer in; |
| 257 |
struct s2n_stuffer out; |
| 258 |
enum { |
| 259 |
ENCRYPTED, |
| 260 |
PLAINTEXT |
| 261 |
} in_status; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
ssize_t current_user_data_consumed; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
uint8_t alert_in_data[S2N_ALERT_LENGTH]; |
| 273 |
struct s2n_stuffer alert_in; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
uint8_t writer_alert_out; |
| 279 |
uint8_t reader_alert_out; |
| 280 |
uint8_t reader_warning_out; |
| 281 |
bool alert_sent; |
| 282 |
|
| 283 |
|
| 284 |
s2n_atomic_flag error_alert_received; |
| 285 |
s2n_atomic_flag close_notify_received; |
| 286 |
|
| 287 |
|
| 288 |
struct s2n_handshake handshake; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
uint16_t max_outgoing_fragment_length; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
uint32_t dynamic_record_resize_threshold; |
| 306 |
|
| 307 |
|
| 308 |
uint16_t dynamic_record_timeout_threshold; |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
uint64_t active_application_bytes_consumed; |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
uint8_t negotiated_mfl_code; |
| 317 |
|
| 318 |
|
| 319 |
uint64_t wire_bytes_in; |
| 320 |
uint64_t wire_bytes_out; |
| 321 |
uint64_t early_data_bytes; |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
s2n_atomic_flag read_closed; |
| 327 |
s2n_atomic_flag write_closed; |
| 328 |
|
| 329 |
|
| 330 |
char server_name[S2N_MAX_SERVER_NAME + 1]; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
char application_protocol[256]; |
| 338 |
|
| 339 |
|
| 340 |
s2n_status_request_type status_type; |
| 341 |
struct s2n_blob status_response; |
| 342 |
|
| 343 |
|
| 344 |
s2n_ct_support_level ct_level_requested; |
| 345 |
struct s2n_blob ct_response; |
| 346 |
|
| 347 |
|
| 348 |
struct s2n_blob our_quic_transport_parameters; |
| 349 |
struct s2n_blob peer_quic_transport_parameters; |
| 350 |
|
| 351 |
struct s2n_client_hello client_hello; |
| 352 |
|
| 353 |
struct s2n_x509_validator x509_validator; |
| 354 |
|
| 355 |
struct s2n_async_offload_op async_offload_op; |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
s2n_verify_host_fn verify_host_fn; |
| 362 |
void *data_for_verify_host; |
| 363 |
uint8_t verify_host_fn_overridden; |
| 364 |
|
| 365 |
|
| 366 |
s2n_session_ticket_status session_ticket_status; |
| 367 |
struct s2n_blob client_ticket; |
| 368 |
uint32_t ticket_lifetime_hint; |
| 369 |
struct s2n_ticket_fields tls13_ticket_fields; |
| 370 |
|
| 371 |
|
| 372 |
uint8_t ticket_ext_data[S2N_TLS12_TICKET_SIZE_IN_BYTES]; |
| 373 |
struct s2n_stuffer client_ticket_to_decrypt; |
| 374 |
|
| 375 |
|
| 376 |
struct s2n_blob application_protocols_overridden; |
| 377 |
|
| 378 |
|
| 379 |
struct s2n_blob cookie; |
| 380 |
|
| 381 |
struct s2n_blob cert_authorities; |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
bool send_in_use; |
| 387 |
bool recv_in_use; |
| 388 |
bool negotiate_in_use; |
| 389 |
|
| 390 |
uint16_t tickets_to_send; |
| 391 |
uint16_t tickets_sent; |
| 392 |
|
| 393 |
s2n_early_data_state early_data_state; |
| 394 |
uint32_t server_max_early_data_size; |
| 395 |
struct s2n_blob server_early_data_context; |
| 396 |
uint32_t server_keying_material_lifetime; |
| 397 |
|
| 398 |
struct s2n_post_handshake post_handshake; |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
s2n_atomic_flag key_update_pending; |
| 403 |
|
| 404 |
|
| 405 |
uint8_t send_key_updated; |
| 406 |
uint8_t recv_key_updated; |
| 407 |
|
| 408 |
struct s2n_event_handshake handshake_event; |
| 409 |
}; |
| 410 |
|
| 411 |
S2N_CLEANUP_RESULT s2n_connection_ptr_free(struct s2n_connection **s2n_connection); |
| 412 |
|
| 413 |
int s2n_connection_is_managed_corked(const struct s2n_connection *s2n_connection); |
| 414 |
int s2n_connection_is_client_auth_enabled(struct s2n_connection *s2n_connection); |
| 415 |
|
| 416 |
typedef enum { |
| 417 |
S2N_IO_WRITABLE, |
| 418 |
S2N_IO_READABLE, |
| 419 |
S2N_IO_FULL_DUPLEX, |
| 420 |
S2N_IO_CLOSED, |
| 421 |
} s2n_io_status; |
| 422 |
bool s2n_connection_check_io_status(struct s2n_connection *conn, s2n_io_status status); |
| 423 |
S2N_RESULT s2n_connection_set_closed(struct s2n_connection *conn); |
| 424 |
|
| 425 |
|
| 426 |
int s2n_connection_send_stuffer(struct s2n_stuffer *stuffer, struct s2n_connection *conn, uint32_t len); |
| 427 |
int s2n_connection_recv_stuffer(struct s2n_stuffer *stuffer, struct s2n_connection *conn, uint32_t len); |
| 428 |
|
| 429 |
S2N_RESULT s2n_connection_wipe_all_keyshares(struct s2n_connection *conn); |
| 430 |
|
| 431 |
|
| 432 |
S2N_RESULT s2n_connection_dynamic_free_in_buffer(struct s2n_connection *conn); |
| 433 |
S2N_RESULT s2n_connection_dynamic_free_out_buffer(struct s2n_connection *conn); |
| 434 |
|
| 435 |
int s2n_connection_get_cipher_preferences(struct s2n_connection *conn, const struct s2n_cipher_preferences **cipher_preferences); |
| 436 |
int s2n_connection_get_security_policy(struct s2n_connection *conn, const struct s2n_security_policy **security_policy); |
| 437 |
int s2n_connection_get_kem_preferences(struct s2n_connection *conn, const struct s2n_kem_preferences **kem_preferences); |
| 438 |
int s2n_connection_get_signature_preferences(struct s2n_connection *conn, const struct s2n_signature_preferences **signature_preferences); |
| 439 |
int s2n_connection_get_ecc_preferences(struct s2n_connection *conn, const struct s2n_ecc_preferences **ecc_preferences); |
| 440 |
int s2n_connection_get_protocol_preferences(struct s2n_connection *conn, struct s2n_blob **protocol_preferences); |
| 441 |
int s2n_connection_set_client_auth_type(struct s2n_connection *conn, s2n_cert_auth_type cert_auth_type); |
| 442 |
int s2n_connection_get_client_auth_type(struct s2n_connection *conn, s2n_cert_auth_type *client_cert_auth_type); |
| 443 |
int s2n_connection_get_client_cert_chain(struct s2n_connection *conn, uint8_t **der_cert_chain_out, uint32_t *cert_chain_len); |
| 444 |
int s2n_connection_get_peer_cert_chain(const struct s2n_connection *conn, struct s2n_cert_chain_and_key *cert_chain_and_key); |
| 445 |
uint8_t s2n_connection_get_protocol_version(const struct s2n_connection *conn); |
| 446 |
S2N_RESULT s2n_connection_set_max_fragment_length(struct s2n_connection *conn, uint16_t length); |
| 447 |
S2N_RESULT s2n_connection_get_secure_cipher(struct s2n_connection *conn, const struct s2n_cipher **cipher); |
| 448 |
S2N_RESULT s2n_connection_get_sequence_number(struct s2n_connection *conn, |
| 449 |
s2n_mode mode, struct s2n_blob *seq_num); |