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