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