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