Debian Codegraph @

crt/s2n/tls/s2n_connection.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "tls/s2n_connection.h"
17
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <strings.h>
23 #include <time.h>
24 #include <unistd.h>
25
26 #include "api/s2n.h"
27 /* Required for s2n_connection_get_key_update_counts */
28 #include "api/unstable/ktls.h"
29 #include "crypto/s2n_certificate.h"
30 #include "crypto/s2n_cipher.h"
31 #include "crypto/s2n_crypto.h"
32 #include "crypto/s2n_fips.h"
33 #include "crypto/s2n_openssl_x509.h"
34 #include "error/s2n_errno.h"
35 #include "tls/extensions/s2n_client_server_name.h"
36 #include "tls/extensions/s2n_client_supported_versions.h"
37 #include "tls/s2n_alerts.h"
38 #include "tls/s2n_cipher_suites.h"
39 #include "tls/s2n_handshake.h"
40 #include "tls/s2n_internal.h"
41 #include "tls/s2n_kem.h"
42 #include "tls/s2n_prf.h"
43 #include "tls/s2n_record.h"
44 #include "tls/s2n_resume.h"
45 #include "tls/s2n_security_policies.h"
46 #include "tls/s2n_tls.h"
47 #include "tls/s2n_tls13_handshake.h"
48 #include "tls/s2n_tls_parameters.h"
49 #include "utils/s2n_atomic.h"
50 #include "utils/s2n_blob.h"
51 #include "utils/s2n_compiler.h"
52 #include "utils/s2n_io.h"
53 #include "utils/s2n_mem.h"
54 #include "utils/s2n_random.h"
55 #include "utils/s2n_safety.h"
56 #ifndef _WIN32
57 #include "utils/s2n_socket.h"
58 #endif
59 #include "utils/s2n_timer.h"
60
61 #define S2N_SET_KEY_SHARE_LIST_EMPTY(keyshares) (keyshares |= 1)
62 #define S2N_SET_KEY_SHARE_REQUEST(keyshares, i) (keyshares |= (1 << (i + 1)))
63
64 static S2N_RESULT s2n_connection_and_config_get_client_auth_type(const struct s2n_connection *conn,
65 const struct s2n_config *config, s2n_cert_auth_type *client_cert_auth_type);
66
67 /* Allocates and initializes memory for a new connection.
68 *
69 * Since customers can reuse a connection, ensure that values on the connection are
70 * initialized in `s2n_connection_wipe` where possible. */
71 struct s2n_connection *s2n_connection_new(s2n_mode mode)
72 {
73 struct s2n_blob blob = { 0 };
74 PTR_GUARD_POSIX(s2n_alloc(&blob, sizeof(struct s2n_connection)));
75 PTR_GUARD_POSIX(s2n_blob_zero(&blob));
76
77 /* Cast 'through' void to acknowledge that we are changing alignment,
78 * which is ok, as blob.data is always aligned.
79 */
80 struct s2n_connection *conn = (struct s2n_connection *) (void *) blob.data;
81
82 PTR_GUARD_POSIX(s2n_connection_set_config(conn, s2n_fetch_default_config()));
83
84 /* `mode` is initialized here since it's passed in as a parameter. */
85 conn->mode = mode;
86
87 /* Allocate the fixed-size stuffers */
88 blob = (struct s2n_blob){ 0 };
89 PTR_GUARD_POSIX(s2n_blob_init(&blob, conn->alert_in_data, S2N_ALERT_LENGTH));
90 PTR_GUARD_POSIX(s2n_stuffer_init(&conn->alert_in, &blob));
91
92 blob = (struct s2n_blob){ 0 };
93 PTR_GUARD_POSIX(s2n_blob_init(&blob, conn->ticket_ext_data, S2N_TLS12_TICKET_SIZE_IN_BYTES));
94 PTR_GUARD_POSIX(s2n_stuffer_init(&conn->client_ticket_to_decrypt, &blob));
95
96 /* Allocate long term hash and HMAC memory */
97 PTR_GUARD_RESULT(s2n_prf_new(conn));
98 PTR_GUARD_RESULT(s2n_handshake_hashes_new(&conn->handshake.hashes));
99
100 /* Initialize the growable stuffers. Zero length at first, but the resize
101 * in _wipe will fix that
102 */
103 blob = (struct s2n_blob){ 0 };
104 PTR_GUARD_POSIX(s2n_blob_init(&blob, conn->header_in_data, S2N_TLS_RECORD_HEADER_LENGTH));
105 PTR_GUARD_POSIX(s2n_stuffer_init(&conn->header_in, &blob));
106 PTR_GUARD_POSIX(s2n_stuffer_growable_alloc(&conn->out, 0));
107 PTR_GUARD_POSIX(s2n_stuffer_growable_alloc(&conn->buffer_in, 0));
108 PTR_GUARD_POSIX(s2n_stuffer_growable_alloc(&conn->handshake.io, 0));
109 PTR_GUARD_RESULT(s2n_timer_start(conn->config, &conn->write_timer));
110
111 /* NOTE: s2n_connection_wipe MUST be called last in this function.
112 *
113 * s2n_connection_wipe is used for initializing values but also used by customers to
114 * reset/reuse the connection. Calling it last ensures that s2n_connection_wipe is
115 * implemented correctly and safe.
116 */
117 PTR_GUARD_POSIX(s2n_connection_wipe(conn));
118 return conn;
119 }
120
121 static int s2n_connection_zero(struct s2n_connection *conn, int mode, struct s2n_config *config)
122 {
123 POSIX_ENSURE_REF(conn);
124 POSIX_ENSURE_REF(config);
125
126 /* Zero the whole connection structure */
127 POSIX_CHECKED_MEMSET(conn, 0, sizeof(struct s2n_connection));
128
129 conn->mode = mode;
130 conn->max_outgoing_fragment_length = S2N_DEFAULT_FRAGMENT_LENGTH;
131 conn->handshake.end_of_messages = APPLICATION_DATA;
132 s2n_connection_set_config(conn, config);
133
134 return 0;
135 }
136
137 S2N_RESULT s2n_connection_wipe_all_keyshares(struct s2n_connection *conn)
138 {
139 RESULT_ENSURE_REF(conn);
140
141 RESULT_GUARD_POSIX(s2n_ecc_evp_params_free(&conn->kex_params.server_ecc_evp_params));
142 RESULT_GUARD_POSIX(s2n_ecc_evp_params_free(&conn->kex_params.client_ecc_evp_params));
143
144 RESULT_GUARD_POSIX(s2n_kem_group_free(&conn->kex_params.server_kem_group_params));
145 RESULT_GUARD_POSIX(s2n_kem_group_free(&conn->kex_params.client_kem_group_params));
146
147 return S2N_RESULT_OK;
148 }
149
150 static int s2n_connection_wipe_keys(struct s2n_connection *conn)
151 {
152 POSIX_ENSURE_REF(conn);
153
154 /* Free any server key received (we may not have completed a
155 * handshake, so this may not have been free'd yet) */
156 POSIX_GUARD(s2n_pkey_free(&conn->handshake_params.server_public_key));
157 POSIX_GUARD(s2n_pkey_zero_init(&conn->handshake_params.server_public_key));
158 POSIX_GUARD(s2n_pkey_free(&conn->handshake_params.client_public_key));
159 POSIX_GUARD(s2n_pkey_zero_init(&conn->handshake_params.client_public_key));
160 s2n_x509_validator_wipe(&conn->x509_validator);
161 POSIX_GUARD(s2n_dh_params_free(&conn->kex_params.server_dh_params));
162 POSIX_GUARD_RESULT(s2n_connection_wipe_all_keyshares(conn));
163 POSIX_GUARD(s2n_kem_free(&conn->kex_params.kem_params));
164 POSIX_GUARD(s2n_free(&conn->handshake_params.client_cert_chain));
165 POSIX_GUARD(s2n_free(&conn->ct_response));
166
167 return 0;
168 }
169
170 static int s2n_connection_free_managed_recv_io(struct s2n_connection *conn)
171 {
172 POSIX_ENSURE_REF(conn);
173
174 #ifndef _WIN32
175 if (conn->managed_recv_io) {
176 POSIX_GUARD(s2n_free_object((uint8_t **) &conn->recv_io_context, sizeof(struct s2n_socket_read_io_context)));
177 conn->managed_recv_io = false;
178 conn->recv = NULL;
179 }
180 #endif
181 return S2N_SUCCESS;
182 }
183
184 static int s2n_connection_free_managed_send_io(struct s2n_connection *conn)
185 {
186 POSIX_ENSURE_REF(conn);
187
188 #ifndef _WIN32
189 if (conn->managed_send_io) {
190 POSIX_GUARD(s2n_free_object((uint8_t **) &conn->send_io_context, sizeof(struct s2n_socket_write_io_context)));
191 conn->managed_send_io = false;
192 conn->send = NULL;
193 }
194 #endif
195 return S2N_SUCCESS;
196 }
197
198 static int s2n_connection_free_managed_io(struct s2n_connection *conn)
199 {
200 POSIX_GUARD(s2n_connection_free_managed_recv_io(conn));
201 POSIX_GUARD(s2n_connection_free_managed_send_io(conn));
202 return S2N_SUCCESS;
203 }
204
205 static int s2n_connection_wipe_io(struct s2n_connection *conn)
206 {
207 #ifndef _WIN32
208 if (s2n_connection_is_managed_corked(conn) && conn->recv) {
209 POSIX_GUARD(s2n_socket_read_restore(conn));
210 }
211 if (s2n_connection_is_managed_corked(conn) && conn->send) {
212 POSIX_GUARD(s2n_socket_write_restore(conn));
213 }
214 #endif
215
216 /* Remove all I/O-related members */
217 POSIX_GUARD(s2n_connection_free_managed_io(conn));
218
219 return 0;
220 }
221
222 static uint8_t s2n_default_verify_host(const char *host_name, size_t len, void *data)
223 {
224 /* if present, match server_name of the connection using rules
225 * outlined in RFC6125 6.4. */
226
227 struct s2n_connection *conn = data;
228
229 if (conn->server_name[0] == '\0') {
230 return 0;
231 }
232
233 /* complete match */
234 if (strlen(conn->server_name) == len && strncasecmp(conn->server_name, host_name, len) == 0) {
235 return 1;
236 }
237
238 /* match 1 level of wildcard */
239 if (len > 2 && host_name[0] == '*' && host_name[1] == '.') {
240 const char *suffix = strchr(conn->server_name, '.');
241
242 if (suffix == NULL) {
243 return 0;
244 }
245
246 if (strlen(suffix) == len - 1 && strncasecmp(suffix, host_name + 1, len - 1) == 0) {
247 return 1;
248 }
249 }
250
251 return 0;
252 }
253
254 S2N_CLEANUP_RESULT s2n_connection_ptr_free(struct s2n_connection **conn)
255 {
256 RESULT_ENSURE_REF(conn);
257 RESULT_GUARD_POSIX(s2n_connection_free(*conn));
258 *conn = NULL;
259 return S2N_RESULT_OK;
260 }
261
262 int s2n_connection_free(struct s2n_connection *conn)
263 {
264 POSIX_GUARD(s2n_connection_wipe_keys(conn));
265 POSIX_GUARD_RESULT(s2n_psk_parameters_wipe(&conn->psk_params));
266
267 POSIX_GUARD_RESULT(s2n_prf_free(conn));
268 POSIX_GUARD_RESULT(s2n_handshake_hashes_free(&conn->handshake.hashes));
269
270 POSIX_GUARD(s2n_connection_free_managed_io(conn));
271
272 POSIX_GUARD(s2n_free(&conn->client_ticket));
273 POSIX_GUARD(s2n_free(&conn->status_response));
274 POSIX_GUARD(s2n_free(&conn->our_quic_transport_parameters));
275 POSIX_GUARD(s2n_free(&conn->peer_quic_transport_parameters));
276 POSIX_GUARD(s2n_free(&conn->server_early_data_context));
277 POSIX_GUARD(s2n_free(&conn->tls13_ticket_fields.session_secret));
278 POSIX_GUARD(s2n_stuffer_free(&conn->buffer_in));
279 POSIX_GUARD(s2n_stuffer_free(&conn->in));
280 POSIX_GUARD(s2n_stuffer_free(&conn->out));
281 POSIX_GUARD(s2n_stuffer_free(&conn->handshake.io));
282 POSIX_GUARD(s2n_stuffer_free(&conn->post_handshake.in));
283 s2n_x509_validator_wipe(&conn->x509_validator);
284 POSIX_GUARD_RESULT(s2n_async_offload_op_wipe(&conn->async_offload_op));
285 POSIX_GUARD(s2n_client_hello_free_raw_message(&conn->client_hello));
286 POSIX_GUARD(s2n_free(&conn->application_protocols_overridden));
287 POSIX_GUARD(s2n_free(&conn->cookie));
288 POSIX_GUARD(s2n_free(&conn->cert_authorities));
289 POSIX_GUARD_RESULT(s2n_crypto_parameters_free(&conn->initial));
290 POSIX_GUARD_RESULT(s2n_crypto_parameters_free(&conn->secure));
291 POSIX_GUARD(s2n_free_object((uint8_t **) &conn, sizeof(struct s2n_connection)));
292
293 return 0;
294 }
295
296 int s2n_connection_set_config(struct s2n_connection *conn, struct s2n_config *config)
297 {
298 POSIX_ENSURE_REF(conn);
299 POSIX_ENSURE_REF(config);
300
301 if (conn->config == config) {
302 return 0;
303 }
304
305 /* s2n_config invariant: any s2n_config is always in a state that respects the
306 * config->security_policy certificate preferences. Therefore we only need to
307 * validate certificates here if the connection is using a security policy override.
308 */
309 const struct s2n_security_policy *security_policy_override = conn->security_policy_override;
310 if (security_policy_override) {
311 POSIX_GUARD_RESULT(s2n_config_validate_loaded_certificates(config, security_policy_override));
312 }
313
314 /* We only support one client certificate */
315 if (s2n_config_get_num_default_certs(config) > 1 && conn->mode == S2N_CLIENT) {
316 POSIX_BAIL(S2N_ERR_TOO_MANY_CERTIFICATES);
317 }
318
319 s2n_x509_validator_wipe(&conn->x509_validator);
320
321 if (config->disable_x509_validation) {
322 POSIX_GUARD(s2n_x509_validator_init_no_x509_validation(&conn->x509_validator));
323 } else {
324 POSIX_GUARD(s2n_x509_validator_init(&conn->x509_validator, &config->trust_store, config->check_ocsp));
325 if (!conn->verify_host_fn_overridden) {
326 if (config->verify_host_fn != NULL) {
327 conn->verify_host_fn = config->verify_host_fn;
328 conn->data_for_verify_host = config->data_for_verify_host;
329 } else {
330 conn->verify_host_fn = s2n_default_verify_host;
331 conn->data_for_verify_host = conn;
332 }
333 }
334
335 if (config->max_verify_cert_chain_depth_set) {
336 POSIX_GUARD(s2n_x509_validator_set_max_chain_depth(&conn->x509_validator, config->max_verify_cert_chain_depth));
337 }
338 }
339 conn->tickets_to_send = config->initial_tickets_to_send;
340
341 if (conn->psk_params.psk_list.len == 0 && !conn->psk_mode_overridden) {
342 POSIX_GUARD(s2n_connection_set_psk_mode(conn, config->psk_mode));
343 conn->psk_mode_overridden = false;
344 }
345
346 /* If at least one certificate does not have a private key configured,
347 * the config must provide an async pkey callback.
348 * The handshake could still fail if the callback doesn't offload the
349 * signature, but this at least catches configuration mistakes.
350 */
351 if (config->no_signing_key) {
352 POSIX_ENSURE(config->async_pkey_cb, S2N_ERR_NO_PRIVATE_KEY);
353 }
354
355 if (config->quic_enabled) {
356 /* If QUIC is ever enabled for a connection via the config,
357 * we should enforce that it can never be disabled by
358 * changing the config.
359 *
360 * Enabling QUIC indicates that the connection is being used by
361 * a QUIC implementation, which never changes. Disabling QUIC
362 * partially through a connection could also potentially be
363 * dangerous, as QUIC handles encryption.
364 */
365 POSIX_GUARD(s2n_connection_enable_quic(conn));
366 }
367
368 if (config->send_buffer_size_override) {
369 conn->multirecord_send = true;
370 }
371
372 /* Historically, calling s2n_config_set_verification_ca_location enabled OCSP stapling
373 * regardless of the value set by an application calling s2n_config_set_status_request_type.
374 * We maintain this behavior for backwards compatibility.
375 *
376 * However, the s2n_config_set_verification_ca_location behavior predates client authentication
377 * support for OCSP stapling, so could only affect whether clients requested OCSP stapling. We
378 * therefore only have to maintain the legacy behavior for clients, not servers.
379 *
380 * Note: The Rust bindings do not maintain the legacy behavior.
381 */
382 conn->request_ocsp_status = config->ocsp_status_requested_by_user;
383 if (config->ocsp_status_requested_by_s2n && conn->mode == S2N_CLIENT) {
384 conn->request_ocsp_status = true;
385 }
386
387 conn->config = config;
388 return S2N_SUCCESS;
389 }
390
391 int s2n_connection_server_name_extension_used(struct s2n_connection *conn)
392 {
393 POSIX_ENSURE_REF(conn);
394 POSIX_ENSURE(conn->mode == S2N_SERVER, S2N_ERR_INVALID_STATE);
395 POSIX_ENSURE(!(conn->handshake.client_hello_received), S2N_ERR_INVALID_STATE);
396
397 conn->server_name_used = 1;
398 return S2N_SUCCESS;
399 }
400
401 int s2n_connection_set_ctx(struct s2n_connection *conn, void *ctx)
402 {
403 POSIX_ENSURE_REF(conn);
404
405 conn->context = ctx;
406 return S2N_SUCCESS;
407 }
408
409 void *s2n_connection_get_ctx(struct s2n_connection *conn)
410 {
411 PTR_ENSURE_REF(conn);
412 return conn->context;
413 }
414
415 int s2n_connection_release_buffers(struct s2n_connection *conn)
416 {
417 POSIX_ENSURE_REF(conn);
418 POSIX_PRECONDITION(s2n_stuffer_validate(&conn->out));
419 POSIX_PRECONDITION(s2n_stuffer_validate(&conn->in));
420
421 POSIX_ENSURE(s2n_stuffer_is_consumed(&conn->out), S2N_ERR_STUFFER_HAS_UNPROCESSED_DATA);
422 POSIX_GUARD(s2n_stuffer_resize(&conn->out, 0));
423
424 POSIX_ENSURE(s2n_stuffer_is_consumed(&conn->in), S2N_ERR_STUFFER_HAS_UNPROCESSED_DATA);
425 if (s2n_stuffer_is_consumed(&conn->buffer_in)) {
426 POSIX_GUARD(s2n_stuffer_resize(&conn->buffer_in, 0));
427 }
428
429 POSIX_ENSURE(s2n_stuffer_is_consumed(&conn->post_handshake.in), S2N_ERR_STUFFER_HAS_UNPROCESSED_DATA);
430 POSIX_GUARD(s2n_stuffer_free(&conn->post_handshake.in));
431
432 POSIX_POSTCONDITION(s2n_stuffer_validate(&conn->out));
433 POSIX_POSTCONDITION(s2n_stuffer_validate(&conn->in));
434 return S2N_SUCCESS;
435 }
436
437 int s2n_connection_free_handshake(struct s2n_connection *conn)
438 {
439 POSIX_ENSURE_REF(conn);
440
441 /* We are done with the handshake */
442 POSIX_GUARD_RESULT(s2n_handshake_hashes_free(&conn->handshake.hashes));
443 POSIX_GUARD_RESULT(s2n_prf_free(conn));
444
445 /* All IO should use conn->secure after the handshake.
446 * However, if this method is called before the handshake completes,
447 * the connection may still be using conn->initial.
448 */
449 if (conn->client != conn->initial && conn->server != conn->initial) {
450 POSIX_GUARD_RESULT(s2n_crypto_parameters_free(&conn->initial));
451 }
452
453 /* Wipe the buffers we are going to free */
454 POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
455 POSIX_GUARD(s2n_blob_zero(&conn->client_hello.raw_message));
456
457 /* Truncate buffers to save memory, we are done with the handshake */
458 POSIX_GUARD(s2n_stuffer_resize(&conn->handshake.io, 0));
459 POSIX_GUARD(s2n_free(&conn->client_hello.raw_message));
460
461 /* We can free extension data we no longer need */
462 POSIX_GUARD(s2n_free(&conn->client_ticket));
463 POSIX_GUARD(s2n_free(&conn->status_response));
464 POSIX_GUARD(s2n_free(&conn->our_quic_transport_parameters));
465 POSIX_GUARD(s2n_free(&conn->application_protocols_overridden));
466 POSIX_GUARD(s2n_free(&conn->cookie));
467 POSIX_GUARD(s2n_free(&conn->cert_authorities));
468
469 return 0;
470 }
471
472 /* An idempotent operation which initializes values on the connection.
473 *
474 * Called in order to reuse a connection structure for a new connection. Should wipe
475 * any persistent memory, free any temporary memory, and set all fields back to their
476 * defaults.
477 */
478 int s2n_connection_wipe(struct s2n_connection *conn)
479 {
480 POSIX_ENSURE_REF(conn);
481
482 /* First make a copy of everything we'd like to save, which isn't very much. */
483 int mode = conn->mode;
484 struct s2n_config *config = conn->config;
485 struct s2n_stuffer alert_in = { 0 };
486 struct s2n_stuffer client_ticket_to_decrypt = { 0 };
487 struct s2n_stuffer handshake_io = { 0 };
488 struct s2n_stuffer header_in = { 0 };
489 struct s2n_stuffer buffer_in = { 0 };
490 struct s2n_stuffer out = { 0 };
491
492 /* Some required structures might have been freed to conserve memory between handshakes.
493 * Restore them.
494 */
495 if (!conn->handshake.hashes) {
496 POSIX_GUARD_RESULT(s2n_handshake_hashes_new(&conn->handshake.hashes));
497 }
498 POSIX_GUARD_RESULT(s2n_handshake_hashes_wipe(conn->handshake.hashes));
499 struct s2n_handshake_hashes *handshake_hashes = conn->handshake.hashes;
500 if (!conn->prf_space) {
501 POSIX_GUARD_RESULT(s2n_prf_new(conn));
502 }
503 POSIX_GUARD_RESULT(s2n_prf_wipe(conn));
504 struct s2n_prf_working_space *prf_workspace = conn->prf_space;
505 if (!conn->initial) {
506 POSIX_GUARD_RESULT(s2n_crypto_parameters_new(&conn->initial));
507 } else {
508 POSIX_GUARD_RESULT(s2n_crypto_parameters_wipe(conn->initial));
509 }
510 struct s2n_crypto_parameters *initial = conn->initial;
511 if (!conn->secure) {
512 POSIX_GUARD_RESULT(s2n_crypto_parameters_new(&conn->secure));
513 } else {
514 POSIX_GUARD_RESULT(s2n_crypto_parameters_wipe(conn->secure));
515 }
516 struct s2n_crypto_parameters *secure = conn->secure;
517
518 /* Wipe all of the sensitive stuff */
519 POSIX_GUARD(s2n_connection_wipe_keys(conn));
520 POSIX_GUARD(s2n_stuffer_wipe(&conn->alert_in));
521 POSIX_GUARD(s2n_stuffer_wipe(&conn->client_ticket_to_decrypt));
522 POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
523 POSIX_GUARD(s2n_stuffer_wipe(&conn->post_handshake.in));
524 POSIX_GUARD(s2n_blob_zero(&conn->client_hello.raw_message));
525 POSIX_GUARD(s2n_stuffer_wipe(&conn->header_in));
526 POSIX_GUARD(s2n_stuffer_wipe(&conn->buffer_in));
527 POSIX_GUARD(s2n_stuffer_wipe(&conn->out));
528
529 /* Free stuffers we plan to just recreate */
530 POSIX_GUARD(s2n_stuffer_free(&conn->post_handshake.in));
531 POSIX_GUARD(s2n_stuffer_free(&conn->in));
532
533 POSIX_GUARD_RESULT(s2n_psk_parameters_wipe(&conn->psk_params));
534 POSIX_GUARD_RESULT(s2n_async_offload_op_wipe(&conn->async_offload_op));
535
536 /* Wipe the I/O-related info and restore the original socket if necessary */
537 POSIX_GUARD(s2n_connection_wipe_io(conn));
538
539 POSIX_GUARD(s2n_free(&conn->client_ticket));
540 POSIX_GUARD(s2n_free(&conn->status_response));
541 POSIX_GUARD(s2n_free(&conn->application_protocols_overridden));
542 POSIX_GUARD(s2n_free(&conn->our_quic_transport_parameters));
543 POSIX_GUARD(s2n_free(&conn->peer_quic_transport_parameters));
544 POSIX_GUARD(s2n_free(&conn->server_early_data_context));
545 POSIX_GUARD(s2n_free(&conn->tls13_ticket_fields.session_secret));
546 POSIX_GUARD(s2n_free(&conn->cookie));
547 POSIX_GUARD(s2n_free(&conn->cert_authorities));
548
549 /* Allocate memory for handling handshakes */
550 POSIX_GUARD(s2n_stuffer_resize(&conn->handshake.io, S2N_LARGE_RECORD_LENGTH));
551
552 /* Truncate the message buffers to save memory, we will dynamically resize it as needed */
553 POSIX_GUARD(s2n_free(&conn->client_hello.raw_message));
554 POSIX_GUARD(s2n_stuffer_resize(&conn->buffer_in, 0));
555 POSIX_GUARD(s2n_stuffer_resize(&conn->out, 0));
556
557 /* Remove context associated with connection */
558 conn->context = NULL;
559 conn->verify_host_fn_overridden = 0;
560 conn->verify_host_fn = NULL;
561 conn->data_for_verify_host = NULL;
562
563 /* Clone the stuffers */
564 /* ignore address warnings because dest is allocated on the stack */
565 #ifdef S2N_DIAGNOSTICS_PUSH_SUPPORTED
566 #pragma GCC diagnostic push
567 #pragma GCC diagnostic ignored "-Waddress"
568 #endif
569 POSIX_CHECKED_MEMCPY(&alert_in, &conn->alert_in, sizeof(struct s2n_stuffer));
570 POSIX_CHECKED_MEMCPY(&client_ticket_to_decrypt, &conn->client_ticket_to_decrypt, sizeof(struct s2n_stuffer));
571 POSIX_CHECKED_MEMCPY(&handshake_io, &conn->handshake.io, sizeof(struct s2n_stuffer));
572 POSIX_CHECKED_MEMCPY(&header_in, &conn->header_in, sizeof(struct s2n_stuffer));
573 POSIX_CHECKED_MEMCPY(&buffer_in, &conn->buffer_in, sizeof(struct s2n_stuffer));
574 POSIX_CHECKED_MEMCPY(&out, &conn->out, sizeof(struct s2n_stuffer));
575 #ifdef S2N_DIAGNOSTICS_POP_SUPPORTED
576 #pragma GCC diagnostic pop
577 #endif
578
579 POSIX_GUARD(s2n_connection_zero(conn, mode, config));
580
581 POSIX_CHECKED_MEMCPY(&conn->alert_in, &alert_in, sizeof(struct s2n_stuffer));
582 POSIX_CHECKED_MEMCPY(&conn->client_ticket_to_decrypt, &client_ticket_to_decrypt, sizeof(struct s2n_stuffer));
583 POSIX_CHECKED_MEMCPY(&conn->handshake.io, &handshake_io, sizeof(struct s2n_stuffer));
584 POSIX_CHECKED_MEMCPY(&conn->header_in, &header_in, sizeof(struct s2n_stuffer));
585 POSIX_CHECKED_MEMCPY(&conn->buffer_in, &buffer_in, sizeof(struct s2n_stuffer));
586 POSIX_CHECKED_MEMCPY(&conn->out, &out, sizeof(struct s2n_stuffer));
587
588 /* conn->in will eventually point to part of conn->buffer_in, but we initialize
589 * it as growable and allocated to support legacy tests.
590 */
591 POSIX_GUARD(s2n_stuffer_growable_alloc(&conn->in, 0));
592
593 conn->handshake.hashes = handshake_hashes;
594 conn->prf_space = prf_workspace;
595 conn->initial = initial;
596 conn->secure = secure;
597 conn->client = conn->initial;
598 conn->server = conn->initial;
599 conn->handshake_params.client_cert_sig_scheme = &s2n_null_sig_scheme;
600 conn->handshake_params.server_cert_sig_scheme = &s2n_null_sig_scheme;
601
602 POSIX_GUARD_RESULT(s2n_psk_parameters_init(&conn->psk_params));
603 conn->server_keying_material_lifetime = ONE_WEEK_IN_SEC;
604
605 /* Require all handshakes hashes. This set can be reduced as the handshake progresses. */
606 POSIX_GUARD(s2n_handshake_require_all_hashes(&conn->handshake));
607
608 if (conn->mode == S2N_SERVER) {
609 /* Start with the highest protocol version so that the highest common protocol version can be selected */
610 /* during handshake. */
611 conn->server_protocol_version = s2n_highest_protocol_version;
612 conn->client_protocol_version = s2n_unknown_protocol_version;
613 conn->actual_protocol_version = s2n_unknown_protocol_version;
614 } else {
615 /* For clients, also set actual_protocol_version. Record generation uses that value for the initial */
616 /* ClientHello record version. Not all servers ignore the record version in ClientHello. */
617 conn->server_protocol_version = s2n_unknown_protocol_version;
618 conn->client_protocol_version = s2n_highest_protocol_version;
619 conn->actual_protocol_version = s2n_highest_protocol_version;
620 }
621
622 /* Initialize remaining values */
623 conn->blinding = S2N_BUILT_IN_BLINDING;
624 conn->session_ticket_status = S2N_NO_TICKET;
625
626 return 0;
627 }
628
629 int s2n_connection_set_recv_ctx(struct s2n_connection *conn, void *ctx)
630 {
631 POSIX_ENSURE_REF(conn);
632 POSIX_GUARD(s2n_connection_free_managed_recv_io(conn));
633 conn->recv_io_context = ctx;
634 return S2N_SUCCESS;
635 }
636
637 int s2n_connection_set_send_ctx(struct s2n_connection *conn, void *ctx)
638 {
639 POSIX_ENSURE_REF(conn);
640 POSIX_GUARD(s2n_connection_free_managed_send_io(conn));
641 conn->send_io_context = ctx;
642 return S2N_SUCCESS;
643 }
644
645 int s2n_connection_set_recv_cb(struct s2n_connection *conn, s2n_recv_fn recv)
646 {
647 POSIX_ENSURE_REF(conn);
648 POSIX_GUARD(s2n_connection_free_managed_recv_io(conn));
649 conn->recv = recv;
650 return S2N_SUCCESS;
651 }
652
653 int s2n_connection_set_send_cb(struct s2n_connection *conn, s2n_send_fn send)
654 {
655 POSIX_ENSURE_REF(conn);
656 POSIX_GUARD(s2n_connection_free_managed_send_io(conn));
657 conn->send = send;
658 return S2N_SUCCESS;
659 }
660
661 int s2n_connection_get_client_cert_chain(struct s2n_connection *conn, uint8_t **cert_chain_out, uint32_t *cert_chain_len)
662 {
663 POSIX_ENSURE_REF(conn);
664 POSIX_ENSURE_REF(cert_chain_out);
665 POSIX_ENSURE_REF(cert_chain_len);
666 POSIX_ENSURE_REF(conn->handshake_params.client_cert_chain.data);
667
668 *cert_chain_out = conn->handshake_params.client_cert_chain.data;
669 *cert_chain_len = conn->handshake_params.client_cert_chain.size;
670
671 return S2N_SUCCESS;
672 }
673
674 int s2n_connection_get_cipher_preferences(struct s2n_connection *conn, const struct s2n_cipher_preferences **cipher_preferences)
675 {
676 POSIX_ENSURE_REF(conn);
677 POSIX_ENSURE_REF(conn->config);
678 POSIX_ENSURE_REF(cipher_preferences);
679
680 if (conn->security_policy_override != NULL) {
681 *cipher_preferences = conn->security_policy_override->cipher_preferences;
682 } else if (conn->config->security_policy != NULL) {
683 *cipher_preferences = conn->config->security_policy->cipher_preferences;
684 } else {
685 POSIX_BAIL(S2N_ERR_INVALID_CIPHER_PREFERENCES);
686 }
687
688 POSIX_ENSURE_REF(*cipher_preferences);
689 return 0;
690 }
691
692 int s2n_connection_get_certificate_match(struct s2n_connection *conn, s2n_cert_sni_match *match_status)
693 {
694 POSIX_ENSURE(conn, S2N_ERR_INVALID_ARGUMENT);
695 POSIX_ENSURE(match_status, S2N_ERR_INVALID_ARGUMENT);
696 POSIX_ENSURE(conn->mode == S2N_SERVER, S2N_ERR_CLIENT_MODE);
697
698 /* Server must have gotten past certificate selection */
699 POSIX_ENSURE(conn->handshake_params.our_chain_and_key, S2N_ERR_NO_CERT_FOUND);
700
701 if (!s2n_server_received_server_name(conn)) {
702 *match_status = S2N_SNI_NONE;
703 } else if (conn->handshake_params.exact_sni_match_exists) {
704 *match_status = S2N_SNI_EXACT_MATCH;
705 } else if (conn->handshake_params.wc_sni_match_exists) {
706 *match_status = S2N_SNI_WILDCARD_MATCH;
707 } else {
708 *match_status = S2N_SNI_NO_MATCH;
709 }
710
711 return S2N_SUCCESS;
712 }
713
714 int s2n_connection_get_security_policy(struct s2n_connection *conn, const struct s2n_security_policy **security_policy)
715 {
716 POSIX_ENSURE_REF(conn);
717 POSIX_ENSURE_REF(conn->config);
718 POSIX_ENSURE_REF(security_policy);
719
720 if (conn->security_policy_override != NULL) {
721 *security_policy = conn->security_policy_override;
722 } else if (conn->config->security_policy != NULL) {
723 *security_policy = conn->config->security_policy;
724 } else {
725 POSIX_BAIL(S2N_ERR_INVALID_SECURITY_POLICY);
726 }
727
728 POSIX_ENSURE_REF(*security_policy);
729 return 0;
730 }
731
732 int s2n_connection_get_kem_preferences(struct s2n_connection *conn, const struct s2n_kem_preferences **kem_preferences)
733 {
734 POSIX_ENSURE_REF(conn);
735 POSIX_ENSURE_REF(conn->config);
736 POSIX_ENSURE_REF(kem_preferences);
737
738 if (conn->security_policy_override != NULL) {
739 *kem_preferences = conn->security_policy_override->kem_preferences;
740 } else if (conn->config->security_policy != NULL) {
741 *kem_preferences = conn->config->security_policy->kem_preferences;
742 } else {
743 POSIX_BAIL(S2N_ERR_INVALID_KEM_PREFERENCES);
744 }
745
746 POSIX_ENSURE_REF(*kem_preferences);
747 return 0;
748 }
749
750 int s2n_connection_get_signature_preferences(struct s2n_connection *conn, const struct s2n_signature_preferences **signature_preferences)
751 {
752 POSIX_ENSURE_REF(conn);
753 POSIX_ENSURE_REF(conn->config);
754 POSIX_ENSURE_REF(signature_preferences);
755
756 if (conn->security_policy_override != NULL) {
757 *signature_preferences = conn->security_policy_override->signature_preferences;
758 } else if (conn->config->security_policy != NULL) {
759 *signature_preferences = conn->config->security_policy->signature_preferences;
760 } else {
761 POSIX_BAIL(S2N_ERR_INVALID_SIGNATURE_ALGORITHMS_PREFERENCES);
762 }
763
764 POSIX_ENSURE_REF(*signature_preferences);
765 return 0;
766 }
767
768 int s2n_connection_get_ecc_preferences(struct s2n_connection *conn, const struct s2n_ecc_preferences **ecc_preferences)
769 {
770 POSIX_ENSURE_REF(conn);
771 POSIX_ENSURE_REF(conn->config);
772 POSIX_ENSURE_REF(ecc_preferences);
773
774 if (conn->security_policy_override != NULL) {
775 *ecc_preferences = conn->security_policy_override->ecc_preferences;
776 } else if (conn->config->security_policy != NULL) {
777 *ecc_preferences = conn->config->security_policy->ecc_preferences;
778 } else {
779 POSIX_BAIL(S2N_ERR_INVALID_ECC_PREFERENCES);
780 }
781
782 POSIX_ENSURE_REF(*ecc_preferences);
783 return 0;
784 }
785
786 int s2n_connection_get_protocol_preferences(struct s2n_connection *conn, struct s2n_blob **protocol_preferences)
787 {
788 POSIX_ENSURE_REF(conn);
789 POSIX_ENSURE_REF(protocol_preferences);
790
791 *protocol_preferences = NULL;
792 if (conn->application_protocols_overridden.size > 0) {
793 *protocol_preferences = &conn->application_protocols_overridden;
794 } else {
795 POSIX_ENSURE_REF(conn->config);
796 *protocol_preferences = &conn->config->application_protocols;
797 }
798
799 POSIX_ENSURE_REF(*protocol_preferences);
800 return 0;
801 }
802
803 static S2N_RESULT s2n_connection_and_config_get_client_auth_type(const struct s2n_connection *conn,
804 const struct s2n_config *config, s2n_cert_auth_type *client_cert_auth_type)
805 {
806 RESULT_ENSURE_REF(conn);
807 RESULT_ENSURE_REF(config);
808 RESULT_ENSURE_REF(client_cert_auth_type);
809
810 if (conn->client_cert_auth_type_overridden) {
811 *client_cert_auth_type = conn->client_cert_auth_type;
812 } else if (config->client_cert_auth_type_overridden) {
813 *client_cert_auth_type = config->client_cert_auth_type;
814 } else if (conn->mode == S2N_CLIENT) {
815 /* Clients should default to "Optional" so that they handle any
816 * CertificateRequests sent by the server.
817 */
818 *client_cert_auth_type = S2N_CERT_AUTH_OPTIONAL;
819 } else {
820 /* Servers should default to "None" so that they send no CertificateRequests. */
821 *client_cert_auth_type = S2N_CERT_AUTH_NONE;
822 }
823
824 return S2N_RESULT_OK;
825 }
826
827 int s2n_connection_get_client_auth_type(struct s2n_connection *conn,
828 s2n_cert_auth_type *client_cert_auth_type)
829 {
830 POSIX_ENSURE_REF(conn);
831 POSIX_GUARD_RESULT(s2n_connection_and_config_get_client_auth_type(
832 conn, conn->config, client_cert_auth_type));
833 return S2N_SUCCESS;
834 }
835
836 int s2n_connection_set_client_auth_type(struct s2n_connection *conn, s2n_cert_auth_type client_cert_auth_type)
837 {
838 POSIX_ENSURE_REF(conn);
839
840 conn->client_cert_auth_type_overridden = 1;
841 conn->client_cert_auth_type = client_cert_auth_type;
842 return 0;
843 }
844
845 #ifndef _WIN32
846 int s2n_connection_set_read_fd(struct s2n_connection *conn, int rfd)
847 {
848 struct s2n_blob ctx_mem = { 0 };
849 struct s2n_socket_read_io_context *peer_socket_ctx = NULL;
850
851 POSIX_ENSURE_REF(conn);
852 POSIX_GUARD(s2n_alloc(&ctx_mem, sizeof(struct s2n_socket_read_io_context)));
853 POSIX_GUARD(s2n_blob_zero(&ctx_mem));
854
855 peer_socket_ctx = (struct s2n_socket_read_io_context *) (void *) ctx_mem.data;
856 peer_socket_ctx->fd = rfd;
857
858 POSIX_GUARD(s2n_connection_set_recv_cb(conn, s2n_socket_read));
859 POSIX_GUARD(s2n_connection_set_recv_ctx(conn, peer_socket_ctx));
860 conn->managed_recv_io = true;
861
862 /* This is only needed if the user is using corked io.
863 * Take the snapshot in case optimized io is enabled after setting the fd.
864 */
865 POSIX_GUARD(s2n_socket_read_snapshot(conn));
866
867 return 0;
868 }
869
870 int s2n_connection_get_read_fd(struct s2n_connection *conn, int *readfd)
871 {
872 POSIX_ENSURE_REF(conn);
873 POSIX_ENSURE_REF(readfd);
874 POSIX_ENSURE((conn->managed_recv_io && conn->recv_io_context), S2N_ERR_INVALID_STATE);
875
876 const struct s2n_socket_read_io_context *peer_socket_ctx = conn->recv_io_context;
877 *readfd = peer_socket_ctx->fd;
878 return S2N_SUCCESS;
879 }
880
881 int s2n_connection_set_write_fd(struct s2n_connection *conn, int wfd)
882 {
883 struct s2n_blob ctx_mem = { 0 };
884 struct s2n_socket_write_io_context *peer_socket_ctx = NULL;
885
886 POSIX_ENSURE_REF(conn);
887 POSIX_GUARD(s2n_alloc(&ctx_mem, sizeof(struct s2n_socket_write_io_context)));
888
889 peer_socket_ctx = (struct s2n_socket_write_io_context *) (void *) ctx_mem.data;
890 peer_socket_ctx->fd = wfd;
891
892 POSIX_GUARD(s2n_connection_set_send_cb(conn, s2n_socket_write));
893 POSIX_GUARD(s2n_connection_set_send_ctx(conn, peer_socket_ctx));
894 conn->managed_send_io = true;
895
896 /* This is only needed if the user is using corked io.
897 * Take the snapshot in case optimized io is enabled after setting the fd.
898 */
899 POSIX_GUARD(s2n_socket_write_snapshot(conn));
900
901 conn->write_fd_broken = 0;
902
903 return 0;
904 }
905
906 int s2n_connection_get_write_fd(struct s2n_connection *conn, int *writefd)
907 {
908 POSIX_ENSURE_REF(conn);
909 POSIX_ENSURE_REF(writefd);
910 POSIX_ENSURE((conn->managed_send_io && conn->send_io_context), S2N_ERR_INVALID_STATE);
911
912 const struct s2n_socket_write_io_context *peer_socket_ctx = conn->send_io_context;
913 *writefd = peer_socket_ctx->fd;
914 return S2N_SUCCESS;
915 }
916 int s2n_connection_set_fd(struct s2n_connection *conn, int fd)
917 {
918 POSIX_GUARD(s2n_connection_set_read_fd(conn, fd));
919 POSIX_GUARD(s2n_connection_set_write_fd(conn, fd));
920 return 0;
921 }
922
923 int s2n_connection_use_corked_io(struct s2n_connection *conn)
924 {
925 POSIX_ENSURE_REF(conn);
926
927 /* Caller shouldn't be trying to set s2n IO corked on non-s2n-managed IO */
928 POSIX_ENSURE(conn->managed_send_io, S2N_ERR_CORK_SET_ON_UNMANAGED);
929 conn->corked_io = 1;
930
931 return 0;
932 }
933 #endif
934
935 uint64_t s2n_connection_get_wire_bytes_in(struct s2n_connection *conn)
936 {
937 if (conn == NULL) {
938 return 0;
939 }
940 if (conn->ktls_recv_enabled) {
941 return 0;
942 }
943 return conn->wire_bytes_in;
944 }
945
946 uint64_t s2n_connection_get_wire_bytes_out(struct s2n_connection *conn)
947 {
948 if (conn == NULL) {
949 return 0;
950 }
951 if (conn->ktls_send_enabled) {
952 return 0;
953 }
954 return conn->wire_bytes_out;
955 }
956
957 const char *s2n_connection_get_cipher(struct s2n_connection *conn)
958 {
959 PTR_ENSURE_REF(conn);
960 PTR_ENSURE_REF(conn->secure);
961 PTR_ENSURE_REF(conn->secure->cipher_suite);
962
963 return conn->secure->cipher_suite->name;
964 }
965
966 int s2n_connection_get_cipher_iana_value(struct s2n_connection *conn, uint8_t *first, uint8_t *second)
967 {
968 POSIX_ENSURE_REF(conn);
969 POSIX_ENSURE_REF(conn->secure);
970 POSIX_ENSURE_REF(conn->secure->cipher_suite);
971 POSIX_ENSURE_MUT(first);
972 POSIX_ENSURE_MUT(second);
973
974 /* ensure we've negotiated a cipher suite */
975 POSIX_ENSURE(!s2n_constant_time_equals(conn->secure->cipher_suite->iana_value,
976 s2n_null_cipher_suite.iana_value, sizeof(s2n_null_cipher_suite.iana_value)),
977 S2N_ERR_INVALID_STATE);
978
979 const uint8_t *iana_value = conn->secure->cipher_suite->iana_value;
980 *first = iana_value[0];
981 *second = iana_value[1];
982
983 return S2N_SUCCESS;
984 }
985
986 const char *s2n_connection_get_curve(struct s2n_connection *conn)
987 {
988 PTR_ENSURE_REF(conn);
989 PTR_ENSURE_REF(conn->secure);
990 PTR_ENSURE_REF(conn->secure->cipher_suite);
991
992 if (conn->kex_params.server_ecc_evp_params.negotiated_curve) {
993 /* TLS1.3 currently only uses ECC groups. */
994 bool tls13 = conn->actual_protocol_version >= S2N_TLS13;
995 /* we check for a full handshake, because TLS 1.2 resumption does not perform
996 * an additional diffie-hellman exchange */
997 bool ecdhe_cipher_negotiated = s2n_kex_includes(conn->secure->cipher_suite->key_exchange_alg, &s2n_ecdhe)
998 && IS_FULL_HANDSHAKE(conn);
999 if (tls13 || ecdhe_cipher_negotiated) {
1000 return conn->kex_params.server_ecc_evp_params.negotiated_curve->name;
1001 }
1002 }
1003
1004 return "NONE";
1005 }
1006
1007 const char *s2n_connection_get_kem_name(struct s2n_connection *conn)
1008 {
1009 PTR_ENSURE_REF(conn);
1010
1011 if (!conn->kex_params.kem_params.kem) {
1012 return "NONE";
1013 }
1014
1015 return conn->kex_params.kem_params.kem->name;
1016 }
1017
1018 const char *s2n_connection_get_kem_group_name(struct s2n_connection *conn)
1019 {
1020 PTR_ENSURE_REF(conn);
1021
1022 if (conn->actual_protocol_version < S2N_TLS13 || !conn->kex_params.server_kem_group_params.kem_group) {
1023 return "NONE";
1024 }
1025
1026 return conn->kex_params.server_kem_group_params.kem_group->name;
1027 }
1028
1029 int s2n_connection_get_key_exchange_group(struct s2n_connection *conn, const char **group_name)
1030 {
1031 POSIX_ENSURE_REF(conn);
1032 POSIX_ENSURE_REF(group_name);
1033
1034 /* s2n_connection_get_curve returns only the ECDH curve portion of a named group, even if
1035 the negotiated group was a hybrid PQ key exchange also containing a KEM. Therefore,
1036 we use the result of s2n_connection_get_kem_group_name if the connection supports PQ. */
1037 if (s2n_tls13_pq_hybrid_supported(conn)) {
1038 *group_name = s2n_connection_get_kem_group_name(conn);
1039 } else {
1040 *group_name = s2n_connection_get_curve(conn);
1041 }
1042
1043 POSIX_ENSURE(*group_name != NULL && strcmp(*group_name, "NONE"), S2N_ERR_INVALID_STATE);
1044
1045 return S2N_SUCCESS;
1046 }
1047
1048 static S2N_RESULT s2n_connection_get_client_supported_version(struct s2n_connection *conn,
1049 uint8_t *client_supported_version)
1050 {
1051 RESULT_ENSURE_REF(conn);
1052 RESULT_ENSURE_EQ(conn->mode, S2N_SERVER);
1053
1054 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(conn);
1055 RESULT_ENSURE_REF(client_hello);
1056
1057 s2n_parsed_extension *supported_versions_extension = NULL;
1058 RESULT_GUARD_POSIX(s2n_client_hello_get_parsed_extension(S2N_EXTENSION_SUPPORTED_VERSIONS, &client_hello->extensions,
1059 &supported_versions_extension));
1060 RESULT_ENSURE_REF(supported_versions_extension);
1061
1062 struct s2n_stuffer supported_versions_stuffer = { 0 };
1063 RESULT_GUARD_POSIX(s2n_stuffer_init_written(&supported_versions_stuffer, &supported_versions_extension->extension));
1064
1065 uint8_t client_protocol_version = s2n_unknown_protocol_version;
1066 uint8_t actual_protocol_version = s2n_unknown_protocol_version;
1067 RESULT_GUARD_POSIX(s2n_extensions_client_supported_versions_process(conn, &supported_versions_stuffer,
1068 &client_protocol_version, &actual_protocol_version));
1069
1070 RESULT_ENSURE_NE(client_protocol_version, s2n_unknown_protocol_version);
1071
1072 *client_supported_version = client_protocol_version;
1073
1074 return S2N_RESULT_OK;
1075 }
1076
1077 int s2n_connection_get_client_protocol_version(struct s2n_connection *conn)
1078 {
1079 POSIX_ENSURE_REF(conn);
1080
1081 /* For backwards compatibility, the client_protocol_version field isn't updated via the
1082 * supported versions extension on TLS 1.2 servers. See
1083 * https://github.com/aws/s2n-tls/issues/4240.
1084 *
1085 * The extension is processed here to ensure that TLS 1.2 servers report the same client
1086 * protocol version to applications as TLS 1.3 servers.
1087 */
1088 if (conn->mode == S2N_SERVER && conn->server_protocol_version <= S2N_TLS12) {
1089 uint8_t client_supported_version = s2n_unknown_protocol_version;
1090 s2n_result result = s2n_connection_get_client_supported_version(conn, &client_supported_version);
1091
1092 /* If the extension wasn't received, or if a client protocol version couldn't be determined
1093 * after processing the extension, the extension is ignored.
1094 */
1095 if (s2n_result_is_ok(result)) {
1096 return client_supported_version;
1097 }
1098 }
1099
1100 return conn->client_protocol_version;
1101 }
1102
1103 int s2n_connection_get_server_protocol_version(struct s2n_connection *conn)
1104 {
1105 POSIX_ENSURE_REF(conn);
1106
1107 return conn->server_protocol_version;
1108 }
1109
1110 int s2n_connection_get_actual_protocol_version(struct s2n_connection *conn)
1111 {
1112 POSIX_ENSURE_REF(conn);
1113
1114 return conn->actual_protocol_version;
1115 }
1116
1117 int s2n_connection_get_client_hello_version(struct s2n_connection *conn)
1118 {
1119 POSIX_ENSURE_REF(conn);
1120
1121 if (conn->client_hello.sslv2) {
1122 return S2N_SSLv2;
1123 } else {
1124 return S2N_MIN(conn->client_hello.legacy_version, S2N_TLS12);
1125 }
1126 }
1127
1128 int s2n_connection_client_cert_used(struct s2n_connection *conn)
1129 {
1130 POSIX_ENSURE_REF(conn);
1131
1132 if (IS_CLIENT_AUTH_HANDSHAKE(conn) && is_handshake_complete(conn)) {
1133 if (IS_CLIENT_AUTH_NO_CERT(conn)) {
1134 return 0;
1135 }
1136 return 1;
1137 }
1138 return 0;
1139 }
1140
1141 int s2n_connection_get_alert(struct s2n_connection *conn)
1142 {
1143 POSIX_ENSURE_REF(conn);
1144
1145 S2N_ERROR_IF(s2n_stuffer_data_available(&conn->alert_in) != 2, S2N_ERR_NO_ALERT);
1146
1147 /* Shallow copy the stuffer. We assume that multiple threads might call this
1148 * function concurrently, so we must not mutate anything outside of the function scope */
1149 struct s2n_stuffer alert_stuffer = conn->alert_in;
1150 uint8_t alert_code = 0;
1151 POSIX_GUARD(s2n_stuffer_read_uint8(&alert_stuffer, &alert_code));
1152 POSIX_GUARD(s2n_stuffer_read_uint8(&alert_stuffer, &alert_code));
1153
1154 return alert_code;
1155 }
1156
1157 int s2n_set_server_name(struct s2n_connection *conn, const char *server_name)
1158 {
1159 POSIX_ENSURE_REF(conn);
1160 POSIX_ENSURE_REF(server_name);
1161
1162 S2N_ERROR_IF(conn->mode != S2N_CLIENT, S2N_ERR_CLIENT_MODE);
1163
1164 int len = strlen(server_name);
1165 S2N_ERROR_IF(len > S2N_MAX_SERVER_NAME, S2N_ERR_SERVER_NAME_TOO_LONG);
1166
1167 POSIX_CHECKED_MEMCPY(conn->server_name, server_name, len);
1168
1169 return 0;
1170 }
1171
1172 const char *s2n_get_server_name(struct s2n_connection *conn)
1173 {
1174 PTR_ENSURE_REF(conn);
1175
1176 if (conn->server_name[0]) {
1177 return conn->server_name;
1178 }
1179
1180 PTR_GUARD_POSIX(s2n_extension_process(&s2n_client_server_name_extension, conn, &conn->client_hello.extensions));
1181
1182 if (!conn->server_name[0]) {
1183 return NULL;
1184 }
1185
1186 return conn->server_name;
1187 }
1188
1189 const char *s2n_get_application_protocol(struct s2n_connection *conn)
1190 {
1191 PTR_ENSURE_REF(conn);
1192
1193 if (strlen(conn->application_protocol) == 0) {
1194 return NULL;
1195 }
1196
1197 return conn->application_protocol;
1198 }
1199
1200 int s2n_connection_get_session_id_length(struct s2n_connection *conn)
1201 {
1202 POSIX_ENSURE_REF(conn);
1203 /* Stateful session resumption in TLS1.3 using session id is not yet supported. */
1204 if (conn->actual_protocol_version >= S2N_TLS13) {
1205 return 0;
1206 }
1207 return conn->session_id_len;
1208 }
1209
1210 int s2n_connection_get_session_id(struct s2n_connection *conn, uint8_t *session_id, size_t max_length)
1211 {
1212 POSIX_ENSURE_REF(conn);
1213 POSIX_ENSURE_REF(session_id);
1214
1215 const int session_id_len = s2n_connection_get_session_id_length(conn);
1216 POSIX_GUARD(session_id_len);
1217
1218 POSIX_ENSURE((size_t) session_id_len <= max_length, S2N_ERR_SESSION_ID_TOO_LONG);
1219
1220 POSIX_CHECKED_MEMCPY(session_id, conn->session_id, session_id_len);
1221
1222 return session_id_len;
1223 }
1224
1225 int s2n_connection_set_blinding(struct s2n_connection *conn, s2n_blinding blinding)
1226 {
1227 POSIX_ENSURE_REF(conn);
1228 conn->blinding = blinding;
1229
1230 return 0;
1231 }
1232
1233 #define ONE_S INT64_C(1000000000)
1234
1235 static S2N_RESULT s2n_connection_get_delay_impl(struct s2n_connection *conn, uint64_t *delay)
1236 {
1237 RESULT_ENSURE_REF(conn);
1238 RESULT_ENSURE_REF(delay);
1239
1240 if (!conn->delay) {
1241 *delay = 0;
1242 return S2N_RESULT_OK;
1243 }
1244
1245 uint64_t elapsed = 0;
1246 RESULT_GUARD(s2n_timer_elapsed(conn->config, &conn->write_timer, &elapsed));
1247
1248 if (elapsed > conn->delay) {
1249 *delay = 0;
1250 return S2N_RESULT_OK;
1251 }
1252
1253 *delay = conn->delay - elapsed;
1254
1255 return S2N_RESULT_OK;
1256 }
1257
1258 uint64_t s2n_connection_get_delay(struct s2n_connection *conn)
1259 {
1260 uint64_t delay = 0;
1261 if (s2n_result_is_ok(s2n_connection_get_delay_impl(conn, &delay))) {
1262 return delay;
1263 } else {
1264 return UINT64_MAX;
1265 }
1266 }
1267
1268 /* s2n-tls has a random delay that will trigger for sensitive errors. This is a mitigation
1269 * for possible timing sidechannels.
1270 *
1271 * The historical sidechannel that inspired s2n-tls blinding was the Lucky 13 attack, which takes
1272 * advantage of potential timing differences when removing padding from a record encrypted in CBC mode.
1273 * The attack is only theoretical in TLS; the attack criteria is unlikely to ever occur
1274 * (See: Fardan, N. J. A., & Paterson, K. G. (2013, May 1). Lucky Thirteen: Breaking the TLS and
1275 * DTLS Record Protocols.) However, we still include blinding to provide a defense in depth mitigation.
1276 */
1277 S2N_RESULT s2n_connection_calculate_blinding(struct s2n_connection *conn, int64_t *min, int64_t *max)
1278 {
1279 RESULT_ENSURE_REF(conn);
1280 RESULT_ENSURE_REF(min);
1281 RESULT_ENSURE_REF(max);
1282 RESULT_ENSURE_REF(conn->config);
1283
1284 /*
1285 * The default delay is a random value between 10-30s. The rationale behind the range is that the
1286 * floor is the fixed cost that an attacker must pay per attempt, in this case, 10s. The length of
1287 * the range then affects the number of attempts that an attacker must perform in order to recover a
1288 * byte of plaintext with a certain degree of confidence.
1289 *
1290 * A uniform distribution of the range [a, b] has a variance of ((b - a)^2)/12. Therefore, given a
1291 * hypothetical timing difference of 1us, the number of attempts necessary to distinguish the correct
1292 * byte from an incorrect byte in a Lucky13-style attack is (((30 - 10) * 10 ^6)^2)/12 ~= 3.3 trillion
1293 * (note that we first have to convert from seconds to microseconds to match the unit of the timing difference.)
1294 */
1295 *min = S2N_DEFAULT_BLINDING_MIN * ONE_S;
1296 *max = S2N_DEFAULT_BLINDING_MAX * ONE_S;
1297
1298 /* Setting the min to 1/3 of the max is an arbitrary ratio of fixed to variable delay.
1299 * It is based on the ratio of our original default values.
1300 */
1301 if (conn->config->custom_blinding_set) {
1302 *max = conn->config->max_blinding * ONE_S;
1303 *min = *max / 3;
1304 }
1305
1306 return S2N_RESULT_OK;
1307 }
1308
1309 static S2N_RESULT s2n_connection_kill(struct s2n_connection *conn)
1310 {
1311 RESULT_ENSURE_REF(conn);
1312 RESULT_GUARD(s2n_connection_set_closed(conn));
1313
1314 int64_t min = 0, max = 0;
1315 RESULT_GUARD(s2n_connection_calculate_blinding(conn, &min, &max));
1316 if (max == 0) {
1317 return S2N_RESULT_OK;
1318 }
1319
1320 /* Keep track of the delay so that it can be enforced */
1321 uint64_t rand_delay = 0;
1322 RESULT_GUARD(s2n_public_random(max - min, &rand_delay));
1323
1324 conn->delay = min + rand_delay;
1325
1326 /* Restart the write timer */
1327 RESULT_GUARD(s2n_timer_start(conn->config, &conn->write_timer));
1328
1329 if (conn->blinding == S2N_BUILT_IN_BLINDING) {
1330 struct timespec sleep_time = { .tv_sec = conn->delay / ONE_S, .tv_nsec = conn->delay % ONE_S };
1331
1332 int r = 0;
1333 do {
1334 r = nanosleep(&sleep_time, &sleep_time);
1335 } while (r != 0);
1336 }
1337
1338 return S2N_RESULT_OK;
1339 }
1340
1341 S2N_CLEANUP_RESULT s2n_connection_apply_error_blinding(struct s2n_connection **conn)
1342 {
1343 RESULT_ENSURE_REF(conn);
1344 if (*conn == NULL) {
1345 return S2N_RESULT_OK;
1346 }
1347
1348 int error_code = s2n_errno;
1349 int error_type = s2n_error_get_type(error_code);
1350
1351 switch (error_type) {
1352 case S2N_ERR_T_OK:
1353 /* Ignore no error */
1354 return S2N_RESULT_OK;
1355 case S2N_ERR_T_BLOCKED:
1356 /* All blocking errors are retriable and should trigger no further action. */
1357 return S2N_RESULT_OK;
1358 default:
1359 break;
1360 }
1361
1362 /* Ensure that conn->in doesn't contain any leftover invalid or unauthenticated data. */
1363 RESULT_GUARD_POSIX(s2n_stuffer_wipe(&(*conn)->in));
1364
1365 switch (error_code) {
1366 /* Don't invoke blinding on some of the common errors.
1367 *
1368 * Be careful adding new errors here. Disabling blinding for an
1369 * error that can be triggered by secret / encrypted values can
1370 * potentially lead to a side channel attack.
1371 *
1372 * We may want to someday add an explicit error type for these errors.
1373 */
1374 case S2N_ERR_CLOSED:
1375 case S2N_ERR_CANCELLED:
1376 case S2N_ERR_CIPHER_NOT_SUPPORTED:
1377 case S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED:
1378 case S2N_ERR_CONFIG_NULL_BEFORE_CH_CALLBACK:
1379 RESULT_GUARD(s2n_connection_set_closed(*conn));
1380 break;
1381 default:
1382 /* Apply blinding to all other errors */
1383 RESULT_GUARD(s2n_connection_kill(*conn));
1384 break;
1385 }
1386
1387 return S2N_RESULT_OK;
1388 }
1389
1390 S2N_RESULT s2n_connection_set_closed(struct s2n_connection *conn)
1391 {
1392 RESULT_ENSURE_REF(conn);
1393 s2n_atomic_flag_set(&conn->read_closed);
1394 s2n_atomic_flag_set(&conn->write_closed);
1395 return S2N_RESULT_OK;
1396 }
1397
1398 const uint8_t *s2n_connection_get_ocsp_response(struct s2n_connection *conn, uint32_t *length)
1399 {
1400 PTR_ENSURE_REF(conn);
1401 PTR_ENSURE_REF(length);
1402
1403 *length = conn->status_response.size;
1404 return conn->status_response.data;
1405 }
1406
1407 S2N_RESULT s2n_connection_set_max_fragment_length(struct s2n_connection *conn, uint16_t max_frag_length)
1408 {
1409 RESULT_ENSURE_REF(conn);
1410
1411 if (conn->negotiated_mfl_code) {
1412 /* Respect the upper limit agreed on with the peer */
1413 RESULT_ENSURE_LT(conn->negotiated_mfl_code, s2n_array_len(mfl_code_to_length));
1414 conn->max_outgoing_fragment_length = S2N_MIN(mfl_code_to_length[conn->negotiated_mfl_code], max_frag_length);
1415 } else {
1416 conn->max_outgoing_fragment_length = max_frag_length;
1417 }
1418
1419 /* If no buffer has been initialized yet, no need to resize.
1420 * The standard I/O logic will handle initializing the buffer.
1421 */
1422 if (s2n_stuffer_is_freed(&conn->out)) {
1423 return S2N_RESULT_OK;
1424 }
1425
1426 uint16_t max_wire_record_size = 0;
1427 RESULT_GUARD(s2n_record_max_write_size(conn, conn->max_outgoing_fragment_length, &max_wire_record_size));
1428 if ((conn->out.blob.size < max_wire_record_size)) {
1429 RESULT_GUARD_POSIX(s2n_realloc(&conn->out.blob, max_wire_record_size));
1430 }
1431
1432 return S2N_RESULT_OK;
1433 }
1434
1435 int s2n_connection_prefer_throughput(struct s2n_connection *conn)
1436 {
1437 POSIX_GUARD_RESULT(s2n_connection_set_max_fragment_length(conn, S2N_LARGE_FRAGMENT_LENGTH));
1438 return S2N_SUCCESS;
1439 }
1440
1441 int s2n_connection_prefer_low_latency(struct s2n_connection *conn)
1442 {
1443 POSIX_GUARD_RESULT(s2n_connection_set_max_fragment_length(conn, S2N_SMALL_FRAGMENT_LENGTH));
1444 return S2N_SUCCESS;
1445 }
1446
1447 int s2n_connection_set_dynamic_buffers(struct s2n_connection *conn, bool enabled)
1448 {
1449 POSIX_ENSURE_REF(conn);
1450 conn->dynamic_buffers = enabled;
1451 return S2N_SUCCESS;
1452 }
1453
1454 int s2n_connection_set_dynamic_record_threshold(struct s2n_connection *conn, uint32_t resize_threshold, uint16_t timeout_threshold)
1455 {
1456 POSIX_ENSURE_REF(conn);
1457 S2N_ERROR_IF(resize_threshold > S2N_TLS_MAX_RESIZE_THRESHOLD, S2N_ERR_INVALID_DYNAMIC_THRESHOLD);
1458
1459 conn->dynamic_record_resize_threshold = resize_threshold;
1460 conn->dynamic_record_timeout_threshold = timeout_threshold;
1461 return 0;
1462 }
1463
1464 int s2n_connection_set_verify_host_callback(struct s2n_connection *conn, s2n_verify_host_fn verify_host_fn, void *data)
1465 {
1466 POSIX_ENSURE_REF(conn);
1467
1468 conn->verify_host_fn = verify_host_fn;
1469 conn->data_for_verify_host = data;
1470 conn->verify_host_fn_overridden = 1;
1471
1472 return 0;
1473 }
1474
1475 int s2n_connection_recv_stuffer(struct s2n_stuffer *stuffer, struct s2n_connection *conn, uint32_t len)
1476 {
1477 POSIX_ENSURE_REF(conn->recv);
1478 /* Make sure we have enough space to write */
1479 POSIX_GUARD(s2n_stuffer_reserve_space(stuffer, len));
1480
1481 int r = 0;
1482 S2N_IO_RETRY_EINTR(r,
1483 conn->recv(conn->recv_io_context, stuffer->blob.data + stuffer->write_cursor, len));
1484 POSIX_ENSURE(r >= 0, S2N_ERR_RECV_STUFFER_FROM_CONN);
1485
1486 /* Record just how many bytes we have written */
1487 POSIX_GUARD(s2n_stuffer_skip_write(stuffer, r));
1488 return r;
1489 }
1490
1491 int s2n_connection_send_stuffer(struct s2n_stuffer *stuffer, struct s2n_connection *conn, uint32_t len)
1492 {
1493 POSIX_ENSURE_REF(conn);
1494 POSIX_ENSURE_REF(conn->send);
1495 if (conn->write_fd_broken) {
1496 POSIX_BAIL(S2N_ERR_SEND_STUFFER_TO_CONN);
1497 }
1498 /* Make sure we even have the data */
1499 S2N_ERROR_IF(s2n_stuffer_data_available(stuffer) < len, S2N_ERR_STUFFER_OUT_OF_DATA);
1500
1501 int w = 0;
1502 S2N_IO_RETRY_EINTR(w,
1503 conn->send(conn->send_io_context, stuffer->blob.data + stuffer->read_cursor, len));
1504 if (w < 0 && errno == EPIPE) {
1505 conn->write_fd_broken = 1;
1506 }
1507 POSIX_ENSURE(w >= 0, S2N_ERR_SEND_STUFFER_TO_CONN);
1508
1509 POSIX_GUARD(s2n_stuffer_skip_read(stuffer, w));
1510 return w;
1511 }
1512
1513 int s2n_connection_is_managed_corked(const struct s2n_connection *s2n_connection)
1514 {
1515 POSIX_ENSURE_REF(s2n_connection);
1516
1517 return (s2n_connection->managed_send_io && s2n_connection->corked_io);
1518 }
1519
1520 const uint8_t *s2n_connection_get_sct_list(struct s2n_connection *conn, uint32_t *length)
1521 {
1522 PTR_ENSURE_REF(conn);
1523 if (!length) {
1524 return NULL;
1525 }
1526
1527 *length = conn->ct_response.size;
1528 return conn->ct_response.data;
1529 }
1530
1531 int s2n_connection_is_client_auth_enabled(struct s2n_connection *s2n_connection)
1532 {
1533 s2n_cert_auth_type auth_type;
1534 POSIX_GUARD(s2n_connection_get_client_auth_type(s2n_connection, &auth_type));
1535
1536 return (auth_type != S2N_CERT_AUTH_NONE);
1537 }
1538
1539 struct s2n_cert_chain_and_key *s2n_connection_get_selected_cert(struct s2n_connection *conn)
1540 {
1541 PTR_ENSURE_REF(conn);
1542 return conn->handshake_params.our_chain_and_key;
1543 }
1544
1545 uint8_t s2n_connection_get_protocol_version(const struct s2n_connection *conn)
1546 {
1547 if (conn == NULL) {
1548 return S2N_UNKNOWN_PROTOCOL_VERSION;
1549 }
1550
1551 if (conn->actual_protocol_version != S2N_UNKNOWN_PROTOCOL_VERSION) {
1552 return conn->actual_protocol_version;
1553 }
1554
1555 if (conn->mode == S2N_CLIENT) {
1556 return conn->client_protocol_version;
1557 }
1558 return conn->server_protocol_version;
1559 }
1560
1561 DEFINE_POINTER_CLEANUP_FUNC(struct s2n_cert_chain *, s2n_cert_chain_free);
1562
1563 int s2n_connection_get_peer_cert_chain(const struct s2n_connection *conn, struct s2n_cert_chain_and_key *cert_chain_and_key)
1564 {
1565 POSIX_ENSURE_REF(conn);
1566 POSIX_ENSURE_REF(cert_chain_and_key);
1567 POSIX_ENSURE_REF(cert_chain_and_key->cert_chain);
1568
1569 /* Ensure that cert_chain_and_key is empty BEFORE we modify it in any way.
1570 * That includes before tying its cert_chain to DEFER_CLEANUP.
1571 */
1572 POSIX_ENSURE(cert_chain_and_key->cert_chain->head == NULL, S2N_ERR_INVALID_ARGUMENT);
1573
1574 DEFER_CLEANUP(struct s2n_cert_chain *cert_chain = cert_chain_and_key->cert_chain, s2n_cert_chain_free_pointer);
1575 struct s2n_cert **insert = &cert_chain->head;
1576
1577 const struct s2n_x509_validator *validator = &conn->x509_validator;
1578 POSIX_ENSURE_REF(validator);
1579 POSIX_ENSURE(s2n_x509_validator_is_cert_chain_validated(validator), S2N_ERR_CERT_NOT_VALIDATED);
1580
1581 DEFER_CLEANUP(struct s2n_validated_cert_chain validated_cert_chain = { 0 }, s2n_x509_validator_validated_cert_chain_free);
1582 POSIX_GUARD_RESULT(s2n_x509_validator_get_validated_cert_chain(validator, &validated_cert_chain));
1583 STACK_OF(X509) *cert_chain_validated = validated_cert_chain.stack;
1584 POSIX_ENSURE_REF(cert_chain_validated);
1585
1586 int cert_count = sk_X509_num(cert_chain_validated);
1587 POSIX_ENSURE_GTE(cert_count, 0);
1588
1589 for (size_t cert_idx = 0; cert_idx < (size_t) cert_count; cert_idx++) {
1590 X509 *cert = sk_X509_value(cert_chain_validated, cert_idx);
1591 POSIX_ENSURE_REF(cert);
1592 DEFER_CLEANUP(uint8_t *cert_data = NULL, s2n_crypto_free);
1593 int cert_size = i2d_X509(cert, &cert_data);
1594 POSIX_ENSURE_GT(cert_size, 0);
1595
1596 struct s2n_blob mem = { 0 };
1597 POSIX_GUARD(s2n_alloc(&mem, sizeof(struct s2n_cert)));
1598
1599 struct s2n_cert *new_node = (struct s2n_cert *) (void *) mem.data;
1600 POSIX_ENSURE_REF(new_node);
1601
1602 new_node->next = NULL;
1603 *insert = new_node;
1604 insert = &new_node->next;
1605
1606 POSIX_GUARD(s2n_alloc(&new_node->raw, cert_size));
1607 POSIX_CHECKED_MEMCPY(new_node->raw.data, cert_data, cert_size);
1608 }
1609
1610 ZERO_TO_DISABLE_DEFER_CLEANUP(cert_chain);
1611
1612 return S2N_SUCCESS;
1613 }
1614
1615 static S2N_RESULT s2n_signature_scheme_to_tls_iana(const struct s2n_signature_scheme *sig_scheme,
1616 s2n_tls_hash_algorithm *converted_scheme)
1617 {
1618 RESULT_ENSURE_REF(sig_scheme);
1619 RESULT_ENSURE_REF(converted_scheme);
1620 *converted_scheme = S2N_TLS_HASH_NONE;
1621
1622 switch (sig_scheme->hash_alg) {
1623 case S2N_HASH_MD5:
1624 *converted_scheme = S2N_TLS_HASH_MD5;
1625 break;
1626 case S2N_HASH_SHA1:
1627 *converted_scheme = S2N_TLS_HASH_SHA1;
1628 break;
1629 case S2N_HASH_SHA224:
1630 *converted_scheme = S2N_TLS_HASH_SHA224;
1631 break;
1632 case S2N_HASH_SHA256:
1633 *converted_scheme = S2N_TLS_HASH_SHA256;
1634 break;
1635 case S2N_HASH_SHA384:
1636 *converted_scheme = S2N_TLS_HASH_SHA384;
1637 break;
1638 case S2N_HASH_SHA512:
1639 *converted_scheme = S2N_TLS_HASH_SHA512;
1640 break;
1641 case S2N_HASH_MD5_SHA1:
1642 *converted_scheme = S2N_TLS_HASH_MD5_SHA1;
1643 break;
1644 case S2N_HASH_NONE:
1645 case S2N_HASH_SHAKE256_64:
1646 case S2N_HASH_ALGS_COUNT:
1647 *converted_scheme = S2N_TLS_HASH_NONE;
1648 break;
1649 }
1650
1651 return S2N_RESULT_OK;
1652 }
1653
1654 int s2n_connection_get_selected_digest_algorithm(struct s2n_connection *conn,
1655 s2n_tls_hash_algorithm *converted_scheme)
1656 {
1657 POSIX_ENSURE_REF(conn);
1658 POSIX_ENSURE_REF(converted_scheme);
1659
1660 POSIX_GUARD_RESULT(s2n_signature_scheme_to_tls_iana(
1661 conn->handshake_params.server_cert_sig_scheme, converted_scheme));
1662
1663 return S2N_SUCCESS;
1664 }
1665
1666 int s2n_connection_get_selected_client_cert_digest_algorithm(struct s2n_connection *conn,
1667 s2n_tls_hash_algorithm *converted_scheme)
1668 {
1669 POSIX_ENSURE_REF(conn);
1670 POSIX_ENSURE_REF(converted_scheme);
1671
1672 POSIX_GUARD_RESULT(s2n_signature_scheme_to_tls_iana(
1673 conn->handshake_params.client_cert_sig_scheme, converted_scheme));
1674 return S2N_SUCCESS;
1675 }
1676
1677 static S2N_RESULT s2n_signature_scheme_to_signature_algorithm(const struct s2n_signature_scheme *sig_scheme,
1678 s2n_tls_signature_algorithm *converted_scheme)
1679 {
1680 RESULT_ENSURE_REF(sig_scheme);
1681 RESULT_ENSURE_REF(converted_scheme);
1682 *converted_scheme = S2N_TLS_SIGNATURE_ANONYMOUS;
1683
1684 switch (sig_scheme->sig_alg) {
1685 case S2N_SIGNATURE_RSA:
1686 *converted_scheme = S2N_TLS_SIGNATURE_RSA;
1687 break;
1688 case S2N_SIGNATURE_ECDSA:
1689 *converted_scheme = S2N_TLS_SIGNATURE_ECDSA;
1690 break;
1691 case S2N_SIGNATURE_RSA_PSS_RSAE:
1692 *converted_scheme = S2N_TLS_SIGNATURE_RSA_PSS_RSAE;
1693 break;
1694 case S2N_SIGNATURE_RSA_PSS_PSS:
1695 *converted_scheme = S2N_TLS_SIGNATURE_RSA_PSS_PSS;
1696 break;
1697 case S2N_SIGNATURE_MLDSA:
1698 *converted_scheme = S2N_TLS_SIGNATURE_MLDSA;
1699 break;
1700 case S2N_SIGNATURE_ANONYMOUS:
1701 *converted_scheme = S2N_TLS_SIGNATURE_ANONYMOUS;
1702 break;
1703 }
1704
1705 return S2N_RESULT_OK;
1706 }
1707
1708 int s2n_connection_get_selected_signature_algorithm(struct s2n_connection *conn,
1709 s2n_tls_signature_algorithm *converted_scheme)
1710 {
1711 POSIX_ENSURE_REF(conn);
1712 POSIX_ENSURE_REF(converted_scheme);
1713
1714 POSIX_GUARD_RESULT(s2n_signature_scheme_to_signature_algorithm(
1715 conn->handshake_params.server_cert_sig_scheme, converted_scheme));
1716
1717 return S2N_SUCCESS;
1718 }
1719
1720 int s2n_connection_get_selected_client_cert_signature_algorithm(struct s2n_connection *conn,
1721 s2n_tls_signature_algorithm *converted_scheme)
1722 {
1723 POSIX_ENSURE_REF(conn);
1724 POSIX_ENSURE_REF(converted_scheme);
1725
1726 POSIX_GUARD_RESULT(s2n_signature_scheme_to_signature_algorithm(
1727 conn->handshake_params.client_cert_sig_scheme, converted_scheme));
1728
1729 return S2N_SUCCESS;
1730 }
1731
1732 int s2n_connection_get_signature_scheme(struct s2n_connection *conn, const char **scheme_name)
1733 {
1734 POSIX_ENSURE_REF(conn);
1735 POSIX_ENSURE_REF(scheme_name);
1736 POSIX_ENSURE(IS_NEGOTIATED(conn), S2N_ERR_INVALID_STATE);
1737
1738 const struct s2n_signature_scheme *scheme = conn->handshake_params.server_cert_sig_scheme;
1739 /* The scheme should never be NULL. A "none" placeholder is used if no
1740 * scheme has been negotiated.
1741 */
1742 POSIX_ENSURE_REF(scheme);
1743
1744 *scheme_name = scheme->name;
1745 if (scheme->signature_curve) {
1746 /* Some TLS1.2 and TLS1.3 signature schemes share an IANA value,
1747 * but are NOT the same. The TLS1.3 version implies a specific curve.
1748 */
1749 if (conn->actual_protocol_version >= S2N_TLS13) {
1750 *scheme_name = scheme->tls13_name;
1751 } else {
1752 *scheme_name = scheme->legacy_name;
1753 }
1754 }
1755
1756 POSIX_ENSURE_REF(*scheme_name);
1757 return S2N_SUCCESS;
1758 }
1759
1760 /*
1761 * Gets the config set on the connection.
1762 */
1763 int s2n_connection_get_config(struct s2n_connection *conn, struct s2n_config **config)
1764 {
1765 POSIX_ENSURE_REF(conn);
1766 POSIX_ENSURE_REF(config);
1767
1768 if (s2n_fetch_default_config() == conn->config) {
1769 POSIX_BAIL(S2N_ERR_NULL);
1770 }
1771
1772 *config = conn->config;
1773
1774 return S2N_SUCCESS;
1775 }
1776
1777 S2N_RESULT s2n_connection_dynamic_free_out_buffer(struct s2n_connection *conn)
1778 {
1779 RESULT_ENSURE_REF(conn);
1780
1781 /* free the out buffer if we're in dynamic mode and it's completely flushed */
1782 if (conn->dynamic_buffers && s2n_stuffer_is_consumed(&conn->out)) {
1783 /* since outgoing buffers are already encrypted, the buffers don't need to be zeroed, which saves some overhead */
1784 RESULT_GUARD_POSIX(s2n_stuffer_free_without_wipe(&conn->out));
1785
1786 /* reset the stuffer to its initial state */
1787 RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&conn->out, 0));
1788 }
1789
1790 return S2N_RESULT_OK;
1791 }
1792
1793 S2N_RESULT s2n_connection_dynamic_free_in_buffer(struct s2n_connection *conn)
1794 {
1795 RESULT_ENSURE_REF(conn);
1796
1797 /* free `buffer_in` if we're in dynamic mode and it's completely flushed */
1798 if (conn->dynamic_buffers && s2n_stuffer_is_consumed(&conn->buffer_in)) {
1799 /* when copying the buffer into the application, we use `s2n_stuffer_erase_and_read`, which already zeroes the memory */
1800 RESULT_GUARD_POSIX(s2n_stuffer_free_without_wipe(&conn->buffer_in));
1801
1802 /* reset the stuffer to its initial state */
1803 RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&conn->buffer_in, 0));
1804 }
1805
1806 return S2N_RESULT_OK;
1807 }
1808
1809 bool s2n_connection_check_io_status(struct s2n_connection *conn, s2n_io_status status)
1810 {
1811 if (!conn) {
1812 return false;
1813 }
1814
1815 bool read_closed = s2n_atomic_flag_test(&conn->read_closed);
1816 bool write_closed = s2n_atomic_flag_test(&conn->write_closed);
1817 bool full_duplex = !read_closed && !write_closed;
1818
1819 /*
1820 *= https://www.rfc-editor.org/rfc/rfc8446#section-6.1
1821 *# Note that this is a change from versions of TLS prior to TLS 1.3 in
1822 *# which implementations were required to react to a "close_notify" by
1823 *# discarding pending writes and sending an immediate "close_notify"
1824 *# alert of their own.
1825 */
1826 if (s2n_connection_get_protocol_version(conn) < S2N_TLS13) {
1827 switch (status) {
1828 case S2N_IO_WRITABLE:
1829 case S2N_IO_READABLE:
1830 case S2N_IO_FULL_DUPLEX:
1831 return full_duplex;
1832 case S2N_IO_CLOSED:
1833 return !full_duplex;
1834 }
1835 }
1836
1837 switch (status) {
1838 case S2N_IO_WRITABLE:
1839 return !write_closed;
1840 case S2N_IO_READABLE:
1841 return !read_closed;
1842 case S2N_IO_FULL_DUPLEX:
1843 return full_duplex;
1844 case S2N_IO_CLOSED:
1845 return read_closed && write_closed;
1846 }
1847
1848 return false;
1849 }
1850
1851 S2N_RESULT s2n_connection_get_secure_cipher(struct s2n_connection *conn, const struct s2n_cipher **cipher)
1852 {
1853 RESULT_ENSURE_REF(conn);
1854 RESULT_ENSURE_REF(cipher);
1855 RESULT_ENSURE_REF(conn->secure);
1856 RESULT_ENSURE_REF(conn->secure->cipher_suite);
1857 RESULT_ENSURE_REF(conn->secure->cipher_suite->record_alg);
1858 *cipher = conn->secure->cipher_suite->record_alg->cipher;
1859 return S2N_RESULT_OK;
1860 }
1861
1862 S2N_RESULT s2n_connection_get_sequence_number(struct s2n_connection *conn,
1863 s2n_mode mode, struct s2n_blob *seq_num)
1864 {
1865 RESULT_ENSURE_REF(conn);
1866 RESULT_ENSURE_REF(seq_num);
1867 RESULT_ENSURE_REF(conn->secure);
1868
1869 switch (mode) {
1870 case S2N_CLIENT:
1871 RESULT_GUARD_POSIX(s2n_blob_init(seq_num, conn->secure->client_sequence_number,
1872 sizeof(conn->secure->client_sequence_number)));
1873 break;
1874 case S2N_SERVER:
1875 RESULT_GUARD_POSIX(s2n_blob_init(seq_num, conn->secure->server_sequence_number,
1876 sizeof(conn->secure->server_sequence_number)));
1877 break;
1878 default:
1879 RESULT_BAIL(S2N_ERR_SAFETY);
1880 }
1881
1882 return S2N_RESULT_OK;
1883 }
1884
1885 int s2n_connection_get_key_update_counts(struct s2n_connection *conn,
1886 uint8_t *send_key_updates, uint8_t *recv_key_updates)
1887 {
1888 POSIX_ENSURE_REF(conn);
1889 POSIX_ENSURE_REF(send_key_updates);
1890 POSIX_ENSURE_REF(recv_key_updates);
1891 *send_key_updates = conn->send_key_updated;
1892 *recv_key_updates = conn->recv_key_updated;
1893 return S2N_SUCCESS;
1894 }
1895
1896 int s2n_connection_set_recv_buffering(struct s2n_connection *conn, bool enabled)
1897 {
1898 POSIX_ENSURE_REF(conn);
1899 /* QUIC support is not currently compatible with recv_buffering */
1900 POSIX_ENSURE(!s2n_connection_is_quic_enabled(conn), S2N_ERR_INVALID_STATE);
1901 conn->recv_buffering = enabled;
1902 return S2N_SUCCESS;
1903 }
1904
1905 s2n_mode s2n_connection_get_mode(struct s2n_connection *conn)
1906 {
1907 if (conn == NULL) {
1908 return S2N_SERVER;
1909 }
1910 return conn->mode;
1911 }