LCOV - code coverage report
Current view: top level - tls - s2n_connection.c (source / functions) Hit Total Coverage
Test: unit_test_coverage.info Lines: 1149 1190 96.6 %
Date: 2025-09-30 07:28:05 Functions: 93 96 96.9 %
Branches: 794 1518 52.3 %

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

Generated by: LCOV version 1.14