LCOV - code coverage report
Current view: top level - tls - s2n_config.c (source / functions) Hit Total Coverage
Test: unit_test_coverage.info Lines: 805 880 91.5 %
Date: 2026-11-22 08:25:05 Functions: 87 91 95.6 %
Branches: 445 966 46.1 %

           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                 :            : #ifndef _S2N_PRELUDE_INCLUDED
      17                 :            :     /* make sure s2n_prelude.h is includes as part of the compiler flags, if not then fail the build */
      18                 :            :     #error "Expected s2n_prelude.h to be included as part of the compiler flags"
      19                 :            : #endif
      20                 :            : 
      21                 :            : #include <strings.h>
      22                 :            : #include <time.h>
      23                 :            : 
      24                 :            : #include "api/unstable/allow_ip_in_cn.h"
      25                 :            : #include "api/unstable/custom_x509_extensions.h"
      26                 :            : #include "api/unstable/npn.h"
      27                 :            : #include "crypto/s2n_certificate.h"
      28                 :            : #include "crypto/s2n_fips.h"
      29                 :            : #include "crypto/s2n_hkdf.h"
      30                 :            : #include "crypto/s2n_libcrypto.h"
      31                 :            : #include "crypto/s2n_pq.h"
      32                 :            : #include "error/s2n_errno.h"
      33                 :            : #include "tls/s2n_cipher_preferences.h"
      34                 :            : #include "tls/s2n_internal.h"
      35                 :            : #include "tls/s2n_ktls.h"
      36                 :            : #include "tls/s2n_record.h"
      37                 :            : #include "tls/s2n_security_policies.h"
      38                 :            : #include "tls/s2n_tls13.h"
      39                 :            : #include "utils/s2n_blob.h"
      40                 :            : #include "utils/s2n_map.h"
      41                 :            : #include "utils/s2n_safety.h"
      42                 :            : 
      43                 :            : #if defined(CLOCK_MONOTONIC_RAW)
      44                 :            :     #define S2N_CLOCK_HW CLOCK_MONOTONIC_RAW
      45                 :            : #else
      46                 :            :     #define S2N_CLOCK_HW CLOCK_MONOTONIC
      47                 :            : #endif
      48                 :            : 
      49                 :            : #define S2N_CLOCK_SYS CLOCK_REALTIME
      50                 :            : 
      51                 :            : int s2n_default_monotonic_clock(void *unused_data, uint64_t *nanoseconds)
      52                 :     535076 : {
      53                 :     535076 :     struct timespec current_time = { 0 };
      54                 :            : 
      55         [ -  + ]:     535076 :     POSIX_GUARD(clock_gettime(S2N_CLOCK_HW, &current_time));
      56                 :            : 
      57                 :     535076 :     *nanoseconds = (uint64_t) current_time.tv_sec * 1000000000ull;
      58                 :     535076 :     *nanoseconds += current_time.tv_nsec;
      59                 :            : 
      60                 :     535076 :     return 0;
      61                 :     535076 : }
      62                 :            : 
      63                 :            : static int wall_clock(void *data, uint64_t *nanoseconds)
      64                 :       4294 : {
      65                 :       4294 :     struct timespec current_time = { 0 };
      66                 :            : 
      67         [ -  + ]:       4294 :     POSIX_GUARD(clock_gettime(S2N_CLOCK_SYS, &current_time));
      68                 :            : 
      69                 :       4294 :     *nanoseconds = (uint64_t) current_time.tv_sec * 1000000000ull;
      70                 :       4294 :     *nanoseconds += current_time.tv_nsec;
      71                 :            : 
      72                 :       4294 :     return 0;
      73                 :       4294 : }
      74                 :            : 
      75                 :            : static struct s2n_config s2n_default_config = { 0 };
      76                 :            : static struct s2n_config s2n_default_fips_config = { 0 };
      77                 :            : static struct s2n_config s2n_default_tls13_config = { 0 };
      78                 :            : 
      79                 :            : static int s2n_config_setup_default(struct s2n_config *config)
      80                 :       3172 : {
      81         [ -  + ]:       3172 :     POSIX_GUARD(s2n_config_set_cipher_preferences(config, "default"));
      82                 :       3172 :     return S2N_SUCCESS;
      83                 :       3172 : }
      84                 :            : 
      85                 :            : static int s2n_config_setup_tls13(struct s2n_config *config)
      86                 :        576 : {
      87         [ -  + ]:        576 :     POSIX_GUARD(s2n_config_set_cipher_preferences(config, "default_tls13"));
      88                 :        576 :     return S2N_SUCCESS;
      89                 :        576 : }
      90                 :            : 
      91                 :            : static int s2n_config_setup_fips(struct s2n_config *config)
      92                 :          0 : {
      93         [ #  # ]:          0 :     POSIX_GUARD(s2n_config_set_cipher_preferences(config, "default_fips"));
      94                 :          0 :     return S2N_SUCCESS;
      95                 :          0 : }
      96                 :            : 
      97                 :            : static int s2n_config_init(struct s2n_config *config)
      98                 :       2823 : {
      99                 :       2823 :     config->wall_clock = wall_clock;
     100                 :       2823 :     config->monotonic_clock = s2n_default_monotonic_clock;
     101                 :       2823 :     config->ct_type = S2N_CT_SUPPORT_NONE;
     102                 :       2823 :     config->mfl_code = S2N_TLS_MAX_FRAG_LEN_EXT_NONE;
     103                 :       2823 :     config->alert_behavior = S2N_ALERT_FAIL_ON_WARNINGS;
     104                 :       2823 :     config->session_state_lifetime_in_nanos = S2N_STATE_LIFETIME_IN_NANOS;
     105                 :       2823 :     config->encrypt_decrypt_key_lifetime_in_nanos = S2N_TICKET_ENCRYPT_DECRYPT_KEY_LIFETIME_IN_NANOS;
     106                 :       2823 :     config->decrypt_key_lifetime_in_nanos = S2N_TICKET_DECRYPT_KEY_LIFETIME_IN_NANOS;
     107                 :       2823 :     config->async_pkey_validation_mode = S2N_ASYNC_PKEY_VALIDATION_FAST;
     108                 :       2823 :     config->check_ocsp = 1;
     109                 :            : 
     110                 :       2823 :     config->client_hello_cb_mode = S2N_CLIENT_HELLO_CB_BLOCKING;
     111                 :            : 
     112         [ -  + ]:       2823 :     POSIX_GUARD(s2n_config_setup_default(config));
     113         [ +  + ]:       2823 :     if (s2n_use_default_tls13_config()) {
     114         [ -  + ]:        227 :         POSIX_GUARD(s2n_config_setup_tls13(config));
     115         [ -  + ]:       2596 :     } else if (s2n_is_in_fips_mode()) {
     116         [ #  # ]:          0 :         POSIX_GUARD(s2n_config_setup_fips(config));
     117                 :          0 :     }
     118                 :            : 
     119         [ -  + ]:       2823 :     POSIX_GUARD_PTR(config->domain_name_to_cert_map = s2n_map_new_with_initial_capacity(1));
     120         [ -  + ]:       2823 :     POSIX_GUARD_RESULT(s2n_map_complete(config->domain_name_to_cert_map));
     121                 :            : 
     122                 :       2823 :     s2n_x509_trust_store_init_empty(&config->trust_store);
     123                 :            : 
     124                 :       2823 :     return 0;
     125                 :       2823 : }
     126                 :            : 
     127                 :            : static int s2n_config_cleanup(struct s2n_config *config)
     128                 :       3253 : {
     129                 :       3253 :     s2n_x509_trust_store_wipe(&config->trust_store);
     130                 :       3253 :     config->check_ocsp = 0;
     131                 :            : 
     132         [ -  + ]:       3253 :     if (config->custom_x509_extension_oids) {
     133                 :          0 :         sk_ASN1_OBJECT_pop_free(config->custom_x509_extension_oids, ASN1_OBJECT_free);
     134                 :          0 :         config->custom_x509_extension_oids = NULL;
     135                 :          0 :     }
     136                 :            : 
     137         [ -  + ]:       3253 :     POSIX_GUARD(s2n_config_free_session_ticket_keys(config));
     138         [ -  + ]:       3253 :     POSIX_GUARD(s2n_config_free_cert_chain_and_key(config));
     139         [ -  + ]:       3253 :     POSIX_GUARD(s2n_config_free_dhparams(config));
     140         [ +  + ]:       3253 :     POSIX_GUARD(s2n_free(&config->application_protocols));
     141         [ -  + ]:       3172 :     POSIX_GUARD(s2n_free(&config->cert_authorities));
     142         [ -  + ]:       3172 :     POSIX_GUARD_RESULT(s2n_map_free(config->domain_name_to_cert_map));
     143                 :            : 
     144 [ #  # ][ -  + ]:       3172 :     POSIX_CHECKED_MEMSET(config, 0, sizeof(struct s2n_config));
                 [ +  - ]
     145                 :            : 
     146                 :       3172 :     return 0;
     147                 :       3172 : }
     148                 :            : 
     149                 :            : static int s2n_config_update_domain_name_to_cert_map(struct s2n_config *config,
     150                 :            :         struct s2n_blob *name,
     151                 :            :         struct s2n_cert_chain_and_key *cert_key_pair)
     152                 :       1242 : {
     153 [ #  # ][ -  + ]:       1242 :     POSIX_ENSURE_REF(config);
     154 [ #  # ][ -  + ]:       1242 :     POSIX_ENSURE_REF(name);
     155                 :            : 
     156                 :       1242 :     struct s2n_map *domain_name_to_cert_map = config->domain_name_to_cert_map;
     157                 :            :     /* s2n_map does not allow zero-size key */
     158         [ -  + ]:       1242 :     if (name->size == 0) {
     159                 :          0 :         return 0;
     160                 :          0 :     }
     161                 :       1242 :     s2n_pkey_type cert_type = s2n_cert_chain_and_key_get_pkey_type(cert_key_pair);
     162 [ -  + ][ #  # ]:       1242 :     POSIX_ENSURE(cert_type >= 0, S2N_ERR_CERT_TYPE_UNSUPPORTED);
     163 [ #  # ][ -  + ]:       1242 :     POSIX_ENSURE(cert_type < S2N_CERT_TYPE_COUNT, S2N_ERR_CERT_TYPE_UNSUPPORTED);
     164                 :       1242 :     struct s2n_blob s2n_map_value = { 0 };
     165                 :       1242 :     bool key_found = false;
     166         [ -  + ]:       1242 :     POSIX_GUARD_RESULT(s2n_map_lookup(domain_name_to_cert_map, name, &s2n_map_value, &key_found));
     167         [ +  + ]:       1242 :     if (!key_found) {
     168                 :       1141 :         struct certs_by_type value = { { 0 } };
     169                 :       1141 :         value.certs[cert_type] = cert_key_pair;
     170                 :       1141 :         s2n_map_value.data = (uint8_t *) &value;
     171                 :       1141 :         s2n_map_value.size = sizeof(struct certs_by_type);
     172                 :            : 
     173         [ -  + ]:       1141 :         POSIX_GUARD_RESULT(s2n_map_unlock(domain_name_to_cert_map));
     174                 :            : 
     175         [ +  + ]:       1141 :         int add_result = s2n_result_is_ok(s2n_map_add(domain_name_to_cert_map, name, &s2n_map_value)) ? S2N_SUCCESS : S2N_FAILURE;
     176                 :            : 
     177                 :            :         /* The map must always be re-completed (made immutable) after an unlock,
     178                 :            :          * even if the add failed. Otherwise s2n_map_lookup, called on every
     179                 :            :          * ClientHello for SNI matching, will fail its RESULT_ENSURE(map->immutable)
     180                 :            :          * check, silently disabling SNI-based certificate selection for the
     181                 :            :          * lifetime of this config.
     182                 :            :          */
     183         [ -  + ]:       1141 :         POSIX_GUARD_RESULT(s2n_map_complete(domain_name_to_cert_map));
     184         [ +  + ]:       1141 :         POSIX_GUARD(add_result);
     185                 :       1141 :     } else {
     186                 :        101 :         struct certs_by_type *value = (void *) s2n_map_value.data;
     187         [ -  + ]:        101 :         if (value->certs[cert_type] == NULL) {
     188                 :          0 :             value->certs[cert_type] = cert_key_pair;
     189         [ +  + ]:        101 :         } else if (config->cert_tiebreak_cb) {
     190                 :            :             /* There's an existing certificate for this (domain_name, auth_method).
     191                 :            :              * Run the application's tiebreaking callback to decide which cert should be used.
     192                 :            :              * An application may have some context specific logic to resolve ties that are based
     193                 :            :              * on factors like trust, expiry, etc.
     194                 :            :              */
     195                 :         99 :             struct s2n_cert_chain_and_key *winner = config->cert_tiebreak_cb(
     196                 :         99 :                     value->certs[cert_type],
     197                 :         99 :                     cert_key_pair,
     198                 :         99 :                     name->data,
     199                 :         99 :                     name->size);
     200         [ +  - ]:         99 :             if (winner) {
     201                 :         99 :                 value->certs[cert_type] = winner;
     202                 :         99 :             }
     203                 :         99 :         }
     204                 :        101 :     }
     205                 :            : 
     206                 :       1241 :     return 0;
     207                 :       1242 : }
     208                 :            : 
     209                 :            : int s2n_config_build_domain_name_to_cert_map(struct s2n_config *config, struct s2n_cert_chain_and_key *cert_key_pair)
     210                 :       1241 : {
     211                 :       1241 :     uint32_t cn_len = 0;
     212         [ -  + ]:       1241 :     POSIX_GUARD_RESULT(s2n_array_num_elements(cert_key_pair->cn_names, &cn_len));
     213                 :       1241 :     uint32_t san_len = 0;
     214         [ -  + ]:       1241 :     POSIX_GUARD_RESULT(s2n_array_num_elements(cert_key_pair->san_names, &san_len));
     215                 :            : 
     216         [ +  + ]:       1241 :     if (san_len == 0) {
     217         [ +  + ]:        146 :         for (uint32_t i = 0; i < cn_len; i++) {
     218                 :         73 :             struct s2n_blob *cn_name = NULL;
     219         [ -  + ]:         73 :             POSIX_GUARD_RESULT(s2n_array_get(cert_key_pair->cn_names, i, (void **) &cn_name));
     220         [ -  + ]:         73 :             POSIX_GUARD(s2n_config_update_domain_name_to_cert_map(config, cn_name, cert_key_pair));
     221                 :         73 :         }
     222                 :       1168 :     } else {
     223         [ +  + ]:       2336 :         for (uint32_t i = 0; i < san_len; i++) {
     224                 :       1169 :             struct s2n_blob *san_name = NULL;
     225         [ -  + ]:       1169 :             POSIX_GUARD_RESULT(s2n_array_get(cert_key_pair->san_names, i, (void **) &san_name));
     226         [ +  + ]:       1169 :             POSIX_GUARD(s2n_config_update_domain_name_to_cert_map(config, san_name, cert_key_pair));
     227                 :       1169 :         }
     228                 :       1168 :     }
     229                 :            : 
     230                 :       1240 :     return 0;
     231                 :       1241 : }
     232                 :            : 
     233                 :            : struct s2n_config *s2n_fetch_default_config(void)
     234                 :     127130 : {
     235         [ +  + ]:     127130 :     if (s2n_use_default_tls13_config()) {
     236                 :        645 :         return &s2n_default_tls13_config;
     237                 :        645 :     }
     238         [ -  + ]:     126485 :     if (s2n_is_in_fips_mode()) {
     239                 :          0 :         return &s2n_default_fips_config;
     240                 :          0 :     }
     241                 :     126485 :     return &s2n_default_config;
     242                 :     126485 : }
     243                 :            : 
     244                 :            : int s2n_config_set_unsafe_for_testing(struct s2n_config *config)
     245                 :        480 : {
     246 [ #  # ][ -  + ]:        480 :     POSIX_ENSURE(s2n_in_test(), S2N_ERR_NOT_IN_TEST);
     247                 :        480 :     config->check_ocsp = 0;
     248                 :        480 :     config->disable_x509_validation = 1;
     249                 :            : 
     250                 :        480 :     return S2N_SUCCESS;
     251                 :        480 : }
     252                 :            : 
     253                 :            : int s2n_config_defaults_init(void)
     254                 :        349 : {
     255                 :            :     /* Set up fips defaults */
     256         [ -  + ]:        349 :     if (s2n_is_in_fips_mode()) {
     257         [ #  # ]:          0 :         POSIX_GUARD(s2n_config_init(&s2n_default_fips_config));
     258         [ #  # ]:          0 :         POSIX_GUARD(s2n_config_setup_fips(&s2n_default_fips_config));
     259         [ #  # ]:          0 :         POSIX_GUARD(s2n_config_load_system_certs(&s2n_default_fips_config));
     260                 :        349 :     } else {
     261                 :            :         /* Set up default */
     262         [ -  + ]:        349 :         POSIX_GUARD(s2n_config_init(&s2n_default_config));
     263         [ -  + ]:        349 :         POSIX_GUARD(s2n_config_setup_default(&s2n_default_config));
     264         [ -  + ]:        349 :         POSIX_GUARD(s2n_config_load_system_certs(&s2n_default_config));
     265                 :        349 :     }
     266                 :            : 
     267                 :            :     /* TLS 1.3 default config is only used in tests so avoid initialization costs in applications */
     268         [ -  + ]:        349 :     POSIX_GUARD(s2n_config_init(&s2n_default_tls13_config));
     269         [ -  + ]:        349 :     POSIX_GUARD(s2n_config_setup_tls13(&s2n_default_tls13_config));
     270                 :            : 
     271                 :        349 :     return S2N_SUCCESS;
     272                 :        349 : }
     273                 :            : 
     274                 :            : void s2n_wipe_static_configs(void)
     275                 :        376 : {
     276                 :        376 :     s2n_config_cleanup(&s2n_default_fips_config);
     277                 :        376 :     s2n_config_cleanup(&s2n_default_config);
     278                 :        376 :     s2n_config_cleanup(&s2n_default_tls13_config);
     279                 :        376 : }
     280                 :            : 
     281                 :            : int s2n_config_load_system_certs(struct s2n_config *config)
     282                 :       2482 : {
     283 [ +  - ][ +  + ]:       2482 :     POSIX_ENSURE_REF(config);
     284                 :            : 
     285                 :       2481 :     struct s2n_x509_trust_store *store = &config->trust_store;
     286 [ +  - ][ +  + ]:       2481 :     POSIX_ENSURE(!store->loaded_system_certs, S2N_ERR_X509_TRUST_STORE);
     287                 :            : 
     288         [ +  + ]:       2401 :     if (!store->trust_store) {
     289                 :       2400 :         store->trust_store = X509_STORE_new();
     290 [ -  + ][ #  # ]:       2400 :         POSIX_ENSURE_REF(store->trust_store);
     291                 :       2400 :     }
     292                 :            : 
     293                 :       2401 :     int err_code = X509_STORE_set_default_paths(store->trust_store);
     294         [ -  + ]:       2401 :     if (!err_code) {
     295                 :          0 :         s2n_x509_trust_store_wipe(store);
     296         [ #  # ]:          0 :         POSIX_BAIL(S2N_ERR_X509_TRUST_STORE);
     297                 :          0 :     }
     298                 :       2401 :     store->loaded_system_certs = true;
     299                 :            : 
     300                 :       2401 :     return S2N_SUCCESS;
     301                 :       2401 : }
     302                 :            : 
     303                 :            : struct s2n_config *s2n_config_new_minimal(void)
     304                 :       2125 : {
     305                 :       2125 :     struct s2n_blob allocator = { 0 };
     306                 :       2125 :     struct s2n_config *new_config = NULL;
     307                 :            : 
     308         [ -  + ]:       2125 :     PTR_GUARD_POSIX(s2n_alloc(&allocator, sizeof(struct s2n_config)));
     309         [ -  + ]:       2125 :     PTR_GUARD_POSIX(s2n_blob_zero(&allocator));
     310                 :            : 
     311                 :       2125 :     new_config = (struct s2n_config *) (void *) allocator.data;
     312         [ -  + ]:       2125 :     if (s2n_config_init(new_config) != S2N_SUCCESS) {
     313                 :          0 :         s2n_free(&allocator);
     314                 :          0 :         return NULL;
     315                 :          0 :     }
     316                 :            : 
     317                 :       2125 :     return new_config;
     318                 :       2125 : }
     319                 :            : 
     320                 :            : struct s2n_config *s2n_config_new(void)
     321                 :       2030 : {
     322                 :       2030 :     struct s2n_config *new_config = s2n_config_new_minimal();
     323 [ -  + ][ #  # ]:       2030 :     PTR_ENSURE_REF(new_config);
     324                 :            : 
     325                 :            :     /* For backwards compatibility, s2n_config_new loads system certs by default. */
     326         [ -  + ]:       2030 :     PTR_GUARD_POSIX(s2n_config_load_system_certs(new_config));
     327                 :            : 
     328                 :       2030 :     return new_config;
     329                 :       2030 : }
     330                 :            : 
     331                 :            : int s2n_config_init_session_ticket_keys(struct s2n_config *config)
     332                 :        142 : {
     333         [ +  + ]:        142 :     if (config->ticket_keys == NULL) {
     334 [ #  # ][ -  + ]:        141 :         POSIX_ENSURE_REF(config->ticket_keys = s2n_array_new_with_capacity(sizeof(struct s2n_ticket_key), S2N_MAX_TICKET_KEYS));
     335                 :        141 :     }
     336                 :            : 
     337                 :        142 :     return 0;
     338                 :        142 : }
     339                 :            : 
     340                 :            : int s2n_config_free_session_ticket_keys(struct s2n_config *config)
     341                 :       3257 : {
     342         [ +  + ]:       3257 :     if (config->ticket_keys != NULL) {
     343         [ -  + ]:        141 :         POSIX_GUARD_RESULT(s2n_array_free_p(&config->ticket_keys));
     344                 :        141 :     }
     345                 :            : 
     346                 :       3257 :     return 0;
     347                 :       3257 : }
     348                 :            : 
     349                 :            : int s2n_config_free_cert_chain_and_key(struct s2n_config *config)
     350                 :       3261 : {
     351                 :            :     /* We track certificate ownership on the config itself because a config
     352                 :            :      * CANNOT use a combination of owned and unowned chains.
     353                 :            :      * If it does, and the unowned chains are freed before the config is,
     354                 :            :      * then iterating over the chains to determine which are owned and need to be freed
     355                 :            :      * will mean also reading the invalid, freed memory of any unowned certificates.
     356                 :            :      * As of now, some tests free chains before the config, so that pattern may also
     357                 :            :      * appear in application code.
     358                 :            :      */
     359 [ -  + ][ #  # ]:       3261 :     POSIX_ENSURE_REF(config);
     360         [ +  + ]:       3261 :     if (config->cert_ownership != S2N_LIB_OWNED) {
     361                 :       3248 :         return S2N_SUCCESS;
     362                 :       3248 :     }
     363                 :            : 
     364                 :            :     /* Free the cert_chain_and_key since the application has no reference
     365                 :            :      * to it. This is necessary until s2n_config_add_cert_chain_and_key is deprecated. */
     366         [ +  + ]:         65 :     for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) {
     367                 :         52 :         s2n_cert_chain_and_key_free(config->default_certs_by_type.certs[i]);
     368                 :         52 :         config->default_certs_by_type.certs[i] = NULL;
     369                 :         52 :     }
     370                 :            : 
     371                 :         13 :     config->cert_ownership = S2N_NOT_OWNED;
     372                 :         13 :     return S2N_SUCCESS;
     373                 :       3261 : }
     374                 :            : 
     375                 :            : int s2n_config_free_dhparams(struct s2n_config *config)
     376                 :       3333 : {
     377 [ #  # ][ -  + ]:       3333 :     POSIX_ENSURE_REF(config);
     378                 :            : 
     379                 :            :     /* s2n_dh_params_free is safe to call when dh is NULL, so this handles
     380                 :            :      * both the configured and unconfigured cases. */
     381         [ -  + ]:       3333 :     POSIX_GUARD(s2n_dh_params_free(&config->dhparams));
     382                 :       3333 :     return S2N_SUCCESS;
     383                 :       3333 : }
     384                 :            : 
     385                 :            : S2N_CLEANUP_RESULT s2n_config_ptr_free(struct s2n_config **config)
     386                 :       1555 : {
     387 [ #  # ][ -  + ]:       1555 :     RESULT_ENSURE_REF(config);
     388         [ -  + ]:       1555 :     RESULT_GUARD_POSIX(s2n_config_free(*config));
     389                 :       1555 :     *config = NULL;
     390                 :       1555 :     return S2N_RESULT_OK;
     391                 :       1555 : }
     392                 :            : 
     393                 :            : int s2n_config_free(struct s2n_config *config)
     394                 :       2125 : {
     395                 :       2125 :     s2n_config_cleanup(config);
     396                 :            : 
     397         [ -  + ]:       2125 :     POSIX_GUARD(s2n_free_object((uint8_t **) &config, sizeof(struct s2n_config)));
     398                 :       2125 :     return 0;
     399                 :       2125 : }
     400                 :            : 
     401                 :            : int s2n_config_get_client_auth_type(struct s2n_config *config, s2n_cert_auth_type *client_auth_type)
     402                 :          0 : {
     403 [ #  # ][ #  # ]:          0 :     POSIX_ENSURE_REF(config);
     404 [ #  # ][ #  # ]:          0 :     POSIX_ENSURE_REF(client_auth_type);
     405                 :          0 :     *client_auth_type = config->client_cert_auth_type;
     406                 :          0 :     return 0;
     407                 :          0 : }
     408                 :            : 
     409                 :            : int s2n_config_set_client_auth_type(struct s2n_config *config, s2n_cert_auth_type client_auth_type)
     410                 :        173 : {
     411 [ #  # ][ -  + ]:        173 :     POSIX_ENSURE_REF(config);
     412                 :        173 :     config->client_cert_auth_type_overridden = 1;
     413                 :        173 :     config->client_cert_auth_type = client_auth_type;
     414                 :        173 :     return 0;
     415                 :        173 : }
     416                 :            : 
     417                 :            : int s2n_config_set_ct_support_level(struct s2n_config *config, s2n_ct_support_level type)
     418                 :          6 : {
     419 [ #  # ][ -  + ]:          6 :     POSIX_ENSURE_REF(config);
     420                 :          6 :     config->ct_type = type;
     421                 :            : 
     422                 :          6 :     return 0;
     423                 :          6 : }
     424                 :            : 
     425                 :            : int s2n_config_set_alert_behavior(struct s2n_config *config, s2n_alert_behavior alert_behavior)
     426                 :          9 : {
     427 [ -  + ][ #  # ]:          9 :     POSIX_ENSURE_REF(config);
     428                 :            : 
     429                 :          9 :     switch (alert_behavior) {
     430         [ +  + ]:          1 :         case S2N_ALERT_FAIL_ON_WARNINGS:
     431         [ +  + ]:          9 :         case S2N_ALERT_IGNORE_WARNINGS:
     432                 :          9 :             config->alert_behavior = alert_behavior;
     433                 :          9 :             break;
     434         [ -  + ]:          0 :         default:
     435         [ #  # ]:          0 :             POSIX_BAIL(S2N_ERR_INVALID_ARGUMENT);
     436                 :          9 :     }
     437                 :            : 
     438                 :          9 :     return 0;
     439                 :          9 : }
     440                 :            : 
     441                 :            : int s2n_config_set_verify_host_callback(struct s2n_config *config, s2n_verify_host_fn verify_host_fn, void *data)
     442                 :         32 : {
     443 [ #  # ][ -  + ]:         32 :     POSIX_ENSURE_REF(config);
     444                 :         32 :     config->verify_host_fn = verify_host_fn;
     445                 :         32 :     config->data_for_verify_host = data;
     446                 :         32 :     return 0;
     447                 :         32 : }
     448                 :            : 
     449                 :            : int s2n_config_set_check_stapled_ocsp_response(struct s2n_config *config, uint8_t check_ocsp)
     450                 :         20 : {
     451 [ -  + ][ #  # ]:         20 :     POSIX_ENSURE_REF(config);
     452 [ -  + ][ #  # ]:         20 :     S2N_ERROR_IF(check_ocsp && !s2n_x509_ocsp_stapling_supported(), S2N_ERR_OCSP_NOT_SUPPORTED);
                 [ #  # ]
     453                 :         20 :     config->check_ocsp = check_ocsp;
     454                 :         20 :     return 0;
     455                 :         20 : }
     456                 :            : 
     457                 :            : int s2n_config_disable_x509_time_verification(struct s2n_config *config)
     458                 :         11 : {
     459 [ +  + ][ +  - ]:         11 :     POSIX_ENSURE_REF(config);
     460                 :         10 :     config->disable_x509_time_validation = true;
     461                 :         10 :     return S2N_SUCCESS;
     462                 :         11 : }
     463                 :            : 
     464                 :            : int s2n_config_disable_x509_intent_verification(struct s2n_config *config)
     465                 :         49 : {
     466 [ +  + ][ +  - ]:         49 :     POSIX_ENSURE(config, S2N_ERR_INVALID_ARGUMENT);
     467                 :         48 :     config->disable_x509_intent_verification = true;
     468                 :         48 :     return S2N_SUCCESS;
     469                 :         49 : }
     470                 :            : 
     471                 :            : int s2n_config_allow_ip_in_cn(struct s2n_config *config)
     472                 :          3 : {
     473 [ +  + ][ +  - ]:          3 :     POSIX_ENSURE(config, S2N_ERR_INVALID_ARGUMENT);
     474                 :          2 :     config->allow_ip_in_cn = true;
     475                 :          2 :     return S2N_SUCCESS;
     476                 :          3 : }
     477                 :            : 
     478                 :            : int s2n_config_disable_x509_verification(struct s2n_config *config)
     479                 :        168 : {
     480 [ -  + ][ #  # ]:        168 :     POSIX_ENSURE_REF(config);
     481                 :        168 :     s2n_x509_trust_store_wipe(&config->trust_store);
     482                 :        168 :     config->disable_x509_validation = 1;
     483                 :        168 :     return 0;
     484                 :        168 : }
     485                 :            : 
     486                 :            : int s2n_config_set_max_cert_chain_depth(struct s2n_config *config, uint16_t max_depth)
     487                 :          0 : {
     488 [ #  # ][ #  # ]:          0 :     POSIX_ENSURE_REF(config);
     489 [ #  # ][ #  # ]:          0 :     S2N_ERROR_IF(max_depth == 0, S2N_ERR_INVALID_ARGUMENT);
     490                 :            : 
     491                 :          0 :     config->max_verify_cert_chain_depth = max_depth;
     492                 :          0 :     config->max_verify_cert_chain_depth_set = 1;
     493                 :          0 :     return 0;
     494                 :          0 : }
     495                 :            : 
     496                 :            : int s2n_config_set_status_request_type(struct s2n_config *config, s2n_status_request_type type)
     497                 :         30 : {
     498 [ +  + ][ -  + ]:         30 :     S2N_ERROR_IF(type == S2N_STATUS_REQUEST_OCSP && !s2n_x509_ocsp_stapling_supported(), S2N_ERR_OCSP_NOT_SUPPORTED);
                 [ #  # ]
     499                 :            : 
     500 [ -  + ][ #  # ]:         30 :     POSIX_ENSURE_REF(config);
     501                 :         30 :     config->ocsp_status_requested_by_user = (type == S2N_STATUS_REQUEST_OCSP);
     502                 :            : 
     503                 :            :     /* Reset the ocsp_status_requested_by_s2n flag if OCSP status requests were disabled. */
     504         [ +  + ]:         30 :     if (type == S2N_STATUS_REQUEST_NONE) {
     505                 :          8 :         config->ocsp_status_requested_by_s2n = false;
     506                 :          8 :     }
     507                 :            : 
     508                 :         30 :     return 0;
     509                 :         30 : }
     510                 :            : 
     511                 :            : int s2n_config_wipe_trust_store(struct s2n_config *config)
     512                 :         28 : {
     513 [ #  # ][ -  + ]:         28 :     POSIX_ENSURE_REF(config);
     514                 :            : 
     515                 :         28 :     s2n_x509_trust_store_wipe(&config->trust_store);
     516                 :            : 
     517                 :         28 :     return S2N_SUCCESS;
     518                 :         28 : }
     519                 :            : 
     520                 :            : int s2n_config_add_pem_to_trust_store(struct s2n_config *config, const char *pem)
     521                 :         24 : {
     522 [ -  + ][ #  # ]:         24 :     POSIX_ENSURE_REF(config);
     523 [ -  + ][ #  # ]:         24 :     POSIX_ENSURE_REF(pem);
     524                 :            : 
     525         [ +  + ]:         24 :     POSIX_GUARD(s2n_x509_trust_store_add_pem(&config->trust_store, pem));
     526                 :            : 
     527                 :         23 :     return 0;
     528                 :         24 : }
     529                 :            : 
     530                 :            : int s2n_config_set_verification_ca_location(struct s2n_config *config, const char *ca_pem_filename, const char *ca_dir)
     531                 :        290 : {
     532 [ #  # ][ -  + ]:        290 :     POSIX_ENSURE_REF(config);
     533                 :        290 :     int err_code = s2n_x509_trust_store_from_ca_file(&config->trust_store, ca_pem_filename, ca_dir);
     534                 :            : 
     535         [ +  - ]:        290 :     if (!err_code) {
     536         [ +  - ]:        290 :         config->ocsp_status_requested_by_s2n = s2n_x509_ocsp_stapling_supported() ? S2N_STATUS_REQUEST_OCSP : S2N_STATUS_REQUEST_NONE;
     537                 :        290 :     }
     538                 :            : 
     539                 :        290 :     return err_code;
     540                 :        290 : }
     541                 :            : 
     542                 :            : static int s2n_config_add_cert_chain_and_key_impl(struct s2n_config *config, struct s2n_cert_chain_and_key *cert_key_pair)
     543                 :       1240 : {
     544 [ #  # ][ -  + ]:       1240 :     POSIX_ENSURE_REF(config->domain_name_to_cert_map);
     545 [ -  + ][ #  # ]:       1240 :     POSIX_ENSURE_REF(cert_key_pair);
     546                 :            : 
     547         [ +  + ]:       1240 :     POSIX_GUARD_RESULT(s2n_security_policy_validate_certificate_chain(config->security_policy, cert_key_pair));
     548                 :            : 
     549                 :       1239 :     s2n_pkey_type cert_type = s2n_cert_chain_and_key_get_pkey_type(cert_key_pair);
     550 [ -  + ][ #  # ]:       1239 :     POSIX_ENSURE(cert_type >= 0, S2N_ERR_CERT_TYPE_UNSUPPORTED);
     551 [ -  + ][ #  # ]:       1239 :     POSIX_ENSURE(cert_type < S2N_CERT_TYPE_COUNT, S2N_ERR_CERT_TYPE_UNSUPPORTED);
     552                 :       1239 :     config->is_rsa_cert_configured |= (cert_type == S2N_PKEY_TYPE_RSA);
     553                 :            : 
     554                 :            :     /* Perform all fallible checks BEFORE inserting into the domain name map.
     555                 :            :      * If we insert into the map first and then fail, the caller's DEFER_CLEANUP
     556                 :            :      * will free cert_key_pair while the map still holds pointers to it,
     557                 :            :      * resulting in dangling pointers and a use-after-free during SNI lookup.
     558                 :            :      */
     559         [ +  - ]:       1239 :     if (!config->default_certs_are_explicit) {
     560         [ +  + ]:       1239 :         if (config->default_certs_by_type.certs[cert_type] != NULL) {
     561                 :            :             /* Because library-owned certificates are tracked and cleaned up via the
     562                 :            :              * default_certs_by_type mapping, library-owned chains MUST be set as the default
     563                 :            :              * to avoid a memory leak. If they're not the default, they're not freed.
     564                 :            :              */
     565 [ -  + ][ #  # ]:        106 :             POSIX_ENSURE(config->cert_ownership != S2N_LIB_OWNED,
     566                 :        106 :                     S2N_ERR_MULTIPLE_DEFAULT_CERTIFICATES_PER_AUTH_TYPE);
     567                 :        106 :         }
     568                 :       1239 :     }
     569                 :            : 
     570         [ -  + ]:       1239 :     POSIX_GUARD(s2n_config_build_domain_name_to_cert_map(config, cert_key_pair));
     571                 :            : 
     572         [ +  - ]:       1239 :     if (!config->default_certs_are_explicit) {
     573         [ +  + ]:       1239 :         if (config->default_certs_by_type.certs[cert_type] == NULL) {
     574                 :       1133 :             config->default_certs_by_type.certs[cert_type] = cert_key_pair;
     575                 :       1133 :         }
     576                 :       1239 :     }
     577                 :            : 
     578         [ +  + ]:       1239 :     if (s2n_pkey_check_key_exists(cert_key_pair->private_key) != S2N_SUCCESS) {
     579                 :          9 :         config->no_signing_key = true;
     580                 :          9 :     }
     581                 :            : 
     582                 :       1239 :     return S2N_SUCCESS;
     583                 :       1239 : }
     584                 :            : 
     585                 :            : S2N_RESULT s2n_config_validate_loaded_certificates(const struct s2n_config *config,
     586                 :            :         const struct s2n_security_policy *security_policy)
     587                 :       6355 : {
     588 [ -  + ][ #  # ]:       6355 :     RESULT_ENSURE_REF(config);
     589 [ -  + ][ #  # ]:       6355 :     RESULT_ENSURE_REF(security_policy);
     590                 :            : 
     591         [ +  + ]:       6355 :     if (security_policy->certificate_key_preferences == NULL
     592         [ +  + ]:       6355 :             && security_policy->certificate_signature_preferences == NULL) {
     593                 :       4234 :         return S2N_RESULT_OK;
     594                 :       4234 :     }
     595                 :            : 
     596                 :            :     /* Duplicates a check in s2n_security_policy_validate_certificate_chain.
     597                 :            :      * If a large number of certificates are configured, even iterating
     598                 :            :      * over the chains to call s2n_security_policy_validate_certificate_chain
     599                 :            :      * could be prohibitively expensive.
     600                 :            :      */
     601         [ +  + ]:       2121 :     if (!security_policy->certificate_preferences_apply_locally) {
     602                 :       2097 :         return S2N_RESULT_OK;
     603                 :       2097 :     }
     604                 :            : 
     605                 :            :     /* validate the default certs */
     606         [ +  + ]:        106 :     for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) {
     607                 :         86 :         struct s2n_cert_chain_and_key *cert = config->default_certs_by_type.certs[i];
     608         [ +  + ]:         86 :         if (cert == NULL) {
     609                 :         72 :             continue;
     610                 :         72 :         }
     611         [ +  + ]:         14 :         RESULT_GUARD(s2n_security_policy_validate_certificate_chain(security_policy, cert));
     612                 :         14 :     }
     613                 :            : 
     614                 :            :     /* validate the certs in the domain map */
     615         [ -  + ]:         20 :     if (config->domain_name_to_cert_map == NULL) {
     616                 :          0 :         return S2N_RESULT_OK;
     617                 :          0 :     }
     618                 :            : 
     619                 :         20 :     struct s2n_map_iterator iter = { 0 };
     620         [ +  + ]:         20 :     RESULT_GUARD(s2n_map_iterator_init(&iter, config->domain_name_to_cert_map));
     621                 :            : 
     622         [ +  + ]:         28 :     while (s2n_map_iterator_has_next(&iter)) {
     623                 :         10 :         struct s2n_blob value = { 0 };
     624         [ -  + ]:         10 :         RESULT_GUARD(s2n_map_iterator_next(&iter, &value));
     625                 :            : 
     626                 :         10 :         struct certs_by_type *domain_certs = (void *) value.data;
     627         [ +  + ]:         47 :         for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) {
     628                 :         38 :             struct s2n_cert_chain_and_key *cert = domain_certs->certs[i];
     629         [ +  + ]:         38 :             if (cert == NULL) {
     630                 :         28 :                 continue;
     631                 :         28 :             }
     632         [ +  + ]:         10 :             RESULT_GUARD(s2n_security_policy_validate_certificate_chain(security_policy, cert));
     633                 :         10 :         }
     634                 :         10 :     }
     635                 :         18 :     return S2N_RESULT_OK;
     636                 :         19 : }
     637                 :            : 
     638                 :            : /* Deprecated. Superseded by s2n_config_add_cert_chain_and_key_to_store */
     639                 :            : int s2n_config_add_cert_chain_and_key(struct s2n_config *config, const char *cert_chain_pem, const char *private_key_pem)
     640                 :         14 : {
     641 [ -  + ][ #  # ]:         14 :     POSIX_ENSURE_REF(config);
     642                 :            :     /* Only allow this deprecated API to be called once. Library-owned certs are
     643                 :            :      * tracked via default_certs_by_type, so adding a second cert that can't be
     644                 :            :      * stored there would leak or, worse, leave dangling pointers in the domain
     645                 :            :      * name map when DEFER_CLEANUP fires on failure.
     646                 :            :      */
     647 [ +  + ][ +  - ]:         14 :     POSIX_ENSURE(config->cert_ownership == S2N_NOT_OWNED, S2N_ERR_CERT_OWNERSHIP);
     648                 :            : 
     649                 :         12 :     DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(),
     650                 :         12 :             s2n_cert_chain_and_key_ptr_free);
     651 [ -  + ][ #  # ]:         12 :     POSIX_ENSURE_REF(chain_and_key);
     652         [ -  + ]:         12 :     POSIX_GUARD(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem));
     653         [ -  + ]:         12 :     POSIX_GUARD(s2n_config_add_cert_chain_and_key_impl(config, chain_and_key));
     654                 :         12 :     config->cert_ownership = S2N_LIB_OWNED;
     655                 :            : 
     656                 :         12 :     ZERO_TO_DISABLE_DEFER_CLEANUP(chain_and_key);
     657                 :         12 :     return S2N_SUCCESS;
     658                 :         12 : }
     659                 :            : 
     660                 :            : /* Only used in the Rust bindings. Superseded by s2n_config_add_cert_chain_and_key_to_store */
     661                 :            : int s2n_config_add_cert_chain(struct s2n_config *config,
     662                 :            :         uint8_t *cert_chain_pem, uint32_t cert_chain_pem_size)
     663                 :          1 : {
     664 [ -  + ][ #  # ]:          1 :     POSIX_ENSURE_REF(config);
     665                 :            :     /* Only allow this deprecated API to be called once. */
     666 [ -  + ][ #  # ]:          1 :     POSIX_ENSURE(config->cert_ownership == S2N_NOT_OWNED, S2N_ERR_CERT_OWNERSHIP);
     667                 :            : 
     668                 :          1 :     DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(),
     669                 :          1 :             s2n_cert_chain_and_key_ptr_free);
     670 [ -  + ][ #  # ]:          1 :     POSIX_ENSURE_REF(chain_and_key);
     671         [ -  + ]:          1 :     POSIX_GUARD(s2n_cert_chain_and_key_load_public_pem_bytes(chain_and_key,
     672                 :          1 :             cert_chain_pem, cert_chain_pem_size));
     673         [ -  + ]:          1 :     POSIX_GUARD(s2n_config_add_cert_chain_and_key_impl(config, chain_and_key));
     674                 :          1 :     config->cert_ownership = S2N_LIB_OWNED;
     675                 :            : 
     676                 :          1 :     ZERO_TO_DISABLE_DEFER_CLEANUP(chain_and_key);
     677                 :          1 :     return S2N_SUCCESS;
     678                 :          1 : }
     679                 :            : 
     680                 :            : int s2n_config_add_cert_chain_and_key_to_store(struct s2n_config *config, struct s2n_cert_chain_and_key *cert_key_pair)
     681                 :       1228 : {
     682 [ -  + ][ #  # ]:       1228 :     POSIX_ENSURE_REF(config);
     683 [ +  + ][ +  - ]:       1228 :     POSIX_ENSURE(config->cert_ownership != S2N_LIB_OWNED, S2N_ERR_CERT_OWNERSHIP);
     684                 :            : 
     685 [ #  # ][ -  + ]:       1227 :     POSIX_ENSURE_REF(cert_key_pair);
     686         [ +  + ]:       1227 :     POSIX_GUARD(s2n_config_add_cert_chain_and_key_impl(config, cert_key_pair));
     687                 :       1226 :     config->cert_ownership = S2N_APP_OWNED;
     688                 :            : 
     689                 :       1226 :     return S2N_SUCCESS;
     690                 :       1227 : }
     691                 :            : 
     692                 :            : int s2n_config_set_async_offload_callback(struct s2n_config *config, uint32_t allow_list, s2n_async_offload_cb fn, void *ctx)
     693                 :         14 : {
     694 [ +  - ][ +  + ]:         14 :     POSIX_ENSURE_REF(config);
     695 [ +  + ][ +  - ]:         13 :     POSIX_ENSURE_REF(fn);
     696 [ #  # ][ -  + ]:         12 :     POSIX_ENSURE(allow_list != 0, S2N_ERR_INVALID_ARGUMENT);
     697                 :            : 
     698                 :         12 :     config->async_offload_allow_list = allow_list;
     699                 :         12 :     config->async_offload_cb = fn;
     700                 :         12 :     config->async_offload_ctx = ctx;
     701                 :            : 
     702                 :         12 :     return S2N_SUCCESS;
     703                 :         12 : }
     704                 :            : 
     705                 :            : int s2n_config_set_async_pkey_callback(struct s2n_config *config, s2n_async_pkey_fn fn)
     706                 :         37 : {
     707 [ #  # ][ -  + ]:         37 :     POSIX_ENSURE_REF(config);
     708                 :            : 
     709                 :         37 :     config->async_pkey_cb = fn;
     710                 :            : 
     711                 :         37 :     return S2N_SUCCESS;
     712                 :         37 : }
     713                 :            : 
     714                 :            : static int s2n_config_clear_default_certificates(struct s2n_config *config)
     715                 :          5 : {
     716 [ #  # ][ -  + ]:          5 :     POSIX_ENSURE_REF(config);
     717                 :            : 
     718                 :            :     /* Clearing library-owned chains would lead to a memory leak.
     719                 :            :      * See s2n_config_free_cert_chain_and_key.
     720                 :            :      */
     721 [ -  + ][ #  # ]:          5 :     POSIX_ENSURE(config->cert_ownership != S2N_LIB_OWNED, S2N_ERR_CERT_OWNERSHIP);
     722                 :            : 
     723         [ +  + ]:         25 :     for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) {
     724                 :         20 :         config->default_certs_by_type.certs[i] = NULL;
     725                 :         20 :     }
     726                 :          5 :     config->cert_ownership = S2N_NOT_OWNED;
     727                 :          5 :     return 0;
     728                 :          5 : }
     729                 :            : 
     730                 :            : int s2n_config_set_cert_chain_and_key_defaults(struct s2n_config *config,
     731                 :            :         struct s2n_cert_chain_and_key **cert_key_pairs,
     732                 :            :         uint32_t num_cert_key_pairs)
     733                 :          9 : {
     734 [ -  + ][ #  # ]:          9 :     POSIX_ENSURE_REF(config);
     735 [ +  + ][ +  - ]:          9 :     POSIX_ENSURE_REF(cert_key_pairs);
     736 [ -  + ][ +  + ]:          8 :     S2N_ERROR_IF(num_cert_key_pairs < 1 || num_cert_key_pairs > S2N_CERT_TYPE_COUNT,
                 [ +  - ]
     737                 :          8 :             S2N_ERR_NUM_DEFAULT_CERTIFICATES);
     738                 :            : 
     739                 :            :     /* This method will set application-owned chains, so we must not already be using
     740                 :            :      * any library owned chains. See s2n_config_free_cert_chain_and_key.
     741                 :            :      */
     742 [ +  - ][ +  + ]:          7 :     POSIX_ENSURE(config->cert_ownership != S2N_LIB_OWNED, S2N_ERR_CERT_OWNERSHIP);
     743                 :            : 
     744                 :            :     /* Validate certs being set before clearing auto-chosen defaults or previously set defaults */
     745                 :          6 :     struct certs_by_type new_defaults = { { 0 } };
     746         [ +  + ]:         13 :     for (size_t i = 0; i < num_cert_key_pairs; i++) {
     747 [ -  + ][ #  # ]:          8 :         POSIX_ENSURE_REF(cert_key_pairs[i]);
     748                 :          8 :         s2n_pkey_type cert_type = s2n_cert_chain_and_key_get_pkey_type(cert_key_pairs[i]);
     749 [ -  + ][ #  # ]:          8 :         POSIX_ENSURE(cert_type >= 0, S2N_ERR_CERT_TYPE_UNSUPPORTED);
     750 [ #  # ][ -  + ]:          8 :         POSIX_ENSURE(cert_type < S2N_CERT_TYPE_COUNT, S2N_ERR_CERT_TYPE_UNSUPPORTED);
     751 [ +  + ][ +  - ]:          8 :         S2N_ERROR_IF(new_defaults.certs[cert_type] != NULL, S2N_ERR_MULTIPLE_DEFAULT_CERTIFICATES_PER_AUTH_TYPE);
     752                 :          7 :         new_defaults.certs[cert_type] = cert_key_pairs[i];
     753                 :          7 :     }
     754                 :            : 
     755         [ -  + ]:          5 :     POSIX_GUARD(s2n_config_clear_default_certificates(config));
     756         [ +  + ]:         11 :     for (size_t i = 0; i < num_cert_key_pairs; i++) {
     757                 :          6 :         s2n_pkey_type cert_type = s2n_cert_chain_and_key_get_pkey_type(cert_key_pairs[i]);
     758 [ -  + ][ #  # ]:          6 :         POSIX_ENSURE(cert_type >= 0, S2N_ERR_CERT_TYPE_UNSUPPORTED);
     759 [ -  + ][ #  # ]:          6 :         POSIX_ENSURE(cert_type < S2N_CERT_TYPE_COUNT, S2N_ERR_CERT_TYPE_UNSUPPORTED);
     760                 :          6 :         config->is_rsa_cert_configured |= (cert_type == S2N_PKEY_TYPE_RSA);
     761                 :          6 :         config->default_certs_by_type.certs[cert_type] = cert_key_pairs[i];
     762                 :          6 :     }
     763                 :            : 
     764                 :          5 :     config->default_certs_are_explicit = 1;
     765                 :          5 :     config->cert_ownership = S2N_APP_OWNED;
     766                 :          5 :     return 0;
     767                 :          5 : }
     768                 :            : 
     769                 :            : int s2n_config_add_dhparams(struct s2n_config *config, const char *dhparams_pem)
     770                 :         81 : {
     771 [ #  # ][ -  + ]:         81 :     POSIX_ENSURE_REF(config);
     772                 :         81 :     DEFER_CLEANUP(struct s2n_stuffer dhparams_in_stuffer = { 0 }, s2n_stuffer_free);
     773                 :         81 :     DEFER_CLEANUP(struct s2n_stuffer dhparams_out_stuffer = { 0 }, s2n_stuffer_free);
     774                 :         81 :     struct s2n_blob dhparams_blob = { 0 };
     775                 :            : 
     776         [ -  + ]:         81 :     POSIX_GUARD(s2n_stuffer_alloc_ro_from_string(&dhparams_in_stuffer, dhparams_pem));
     777         [ -  + ]:         81 :     POSIX_GUARD(s2n_stuffer_growable_alloc(&dhparams_out_stuffer, strlen(dhparams_pem)));
     778                 :            : 
     779                 :            :     /* Convert pem to asn1 and asn1 to the private key */
     780         [ -  + ]:         81 :     POSIX_GUARD(s2n_stuffer_dhparams_from_pem(&dhparams_in_stuffer, &dhparams_out_stuffer));
     781                 :            : 
     782                 :         81 :     dhparams_blob.size = s2n_stuffer_data_available(&dhparams_out_stuffer);
     783                 :         81 :     dhparams_blob.data = s2n_stuffer_raw_read(&dhparams_out_stuffer, dhparams_blob.size);
     784 [ #  # ][ -  + ]:         81 :     POSIX_ENSURE_REF(dhparams_blob.data);
     785                 :            : 
     786                 :            :     /* Parse into a temporary struct so that config->dhparams is only modified
     787                 :            :      * once parsing has fully succeeded. On failure, the DEFER_CLEANUP frees any
     788                 :            :      * DH that was allocated. */
     789                 :         81 :     DEFER_CLEANUP(struct s2n_dh_params dhparams = { 0 }, s2n_dh_params_free);
     790         [ +  + ]:         81 :     POSIX_GUARD(s2n_pkcs3_to_dh_params(&dhparams, &dhparams_blob));
     791                 :            : 
     792                 :            :     /* Release any previously configured dhparams before overwriting. */
     793         [ -  + ]:         80 :     POSIX_GUARD(s2n_config_free_dhparams(config));
     794                 :         80 :     config->dhparams = dhparams;
     795                 :         80 :     ZERO_TO_DISABLE_DEFER_CLEANUP(dhparams);
     796                 :            : 
     797                 :         80 :     return S2N_SUCCESS;
     798                 :         80 : }
     799                 :            : 
     800                 :            : int s2n_config_set_wall_clock(struct s2n_config *config, s2n_clock_time_nanoseconds clock_fn, void *ctx)
     801                 :         65 : {
     802 [ #  # ][ -  + ]:         65 :     POSIX_ENSURE_REF(config);
     803 [ -  + ][ #  # ]:         65 :     POSIX_ENSURE_REF(clock_fn);
     804                 :            : 
     805                 :         65 :     config->wall_clock = clock_fn;
     806                 :         65 :     config->sys_clock_ctx = ctx;
     807                 :            : 
     808                 :         65 :     return 0;
     809                 :         65 : }
     810                 :            : 
     811                 :            : int s2n_config_set_monotonic_clock(struct s2n_config *config, s2n_clock_time_nanoseconds clock_fn, void *ctx)
     812                 :         11 : {
     813 [ -  + ][ #  # ]:         11 :     POSIX_ENSURE_REF(config);
     814 [ #  # ][ -  + ]:         11 :     POSIX_ENSURE_REF(clock_fn);
     815                 :            : 
     816                 :         11 :     config->monotonic_clock = clock_fn;
     817                 :         11 :     config->monotonic_clock_ctx = ctx;
     818                 :            : 
     819                 :         11 :     return 0;
     820                 :         11 : }
     821                 :            : 
     822                 :            : int s2n_config_set_cache_store_callback(struct s2n_config *config, s2n_cache_store_callback cache_store_callback, void *data)
     823                 :          2 : {
     824 [ -  + ][ #  # ]:          2 :     POSIX_ENSURE_REF(config);
     825 [ #  # ][ -  + ]:          2 :     POSIX_ENSURE_REF(cache_store_callback);
     826                 :            : 
     827                 :          2 :     config->cache_store = cache_store_callback;
     828                 :          2 :     config->cache_store_data = data;
     829                 :            : 
     830                 :          2 :     return 0;
     831                 :          2 : }
     832                 :            : 
     833                 :            : int s2n_config_set_cache_retrieve_callback(struct s2n_config *config, s2n_cache_retrieve_callback cache_retrieve_callback, void *data)
     834                 :          2 : {
     835 [ #  # ][ -  + ]:          2 :     POSIX_ENSURE_REF(config);
     836 [ -  + ][ #  # ]:          2 :     POSIX_ENSURE_REF(cache_retrieve_callback);
     837                 :            : 
     838                 :          2 :     config->cache_retrieve = cache_retrieve_callback;
     839                 :          2 :     config->cache_retrieve_data = data;
     840                 :            : 
     841                 :          2 :     return 0;
     842                 :          2 : }
     843                 :            : 
     844                 :            : int s2n_config_set_cache_delete_callback(struct s2n_config *config, s2n_cache_delete_callback cache_delete_callback, void *data)
     845                 :          2 : {
     846 [ #  # ][ -  + ]:          2 :     POSIX_ENSURE_REF(config);
     847 [ -  + ][ #  # ]:          2 :     POSIX_ENSURE_REF(cache_delete_callback);
     848                 :            : 
     849                 :          2 :     config->cache_delete = cache_delete_callback;
     850                 :          2 :     config->cache_delete_data = data;
     851                 :            : 
     852                 :          2 :     return 0;
     853                 :          2 : }
     854                 :            : 
     855                 :            : int s2n_config_set_extension_data(struct s2n_config *config, s2n_tls_extension_type type, const uint8_t *data, uint32_t length)
     856                 :          8 : {
     857 [ -  + ][ #  # ]:          8 :     POSIX_ENSURE_REF(config);
     858                 :            : 
     859         [ -  + ]:          8 :     if (s2n_config_get_num_default_certs(config) == 0) {
     860         [ #  # ]:          0 :         POSIX_BAIL(S2N_ERR_UPDATING_EXTENSION);
     861                 :          0 :     }
     862                 :          8 :     struct s2n_cert_chain_and_key *config_chain_and_key = s2n_config_get_single_default_cert(config);
     863 [ -  + ][ #  # ]:          8 :     POSIX_ENSURE_REF(config_chain_and_key);
     864 [ +  + ][ +  - ]:          8 :     POSIX_ENSURE(config->cert_ownership == S2N_LIB_OWNED, S2N_ERR_CERT_OWNERSHIP);
     865                 :            : 
     866                 :          7 :     switch (type) {
     867         [ +  + ]:          2 :         case S2N_EXTENSION_CERTIFICATE_TRANSPARENCY:
     868         [ -  + ]:          2 :             POSIX_GUARD(s2n_cert_chain_and_key_set_sct_list(config_chain_and_key, data, length));
     869                 :          2 :             break;
     870         [ +  + ]:          5 :         case S2N_EXTENSION_OCSP_STAPLING:
     871         [ -  + ]:          5 :             POSIX_GUARD(s2n_cert_chain_and_key_set_ocsp_data(config_chain_and_key, data, length));
     872                 :          5 :             break;
     873         [ -  + ]:          5 :         default:
     874         [ #  # ]:          0 :             POSIX_BAIL(S2N_ERR_UNRECOGNIZED_EXTENSION);
     875                 :          7 :     }
     876                 :            : 
     877                 :          7 :     return 0;
     878                 :          7 : }
     879                 :            : 
     880                 :            : int s2n_config_add_custom_x509_extension(struct s2n_config *config, uint8_t *extension_oid, uint32_t extension_oid_len)
     881                 :          3 : {
     882 [ +  - ][ +  + ]:          3 :     POSIX_ENSURE(config, S2N_ERR_INVALID_ARGUMENT);
     883 [ +  + ][ +  - ]:          2 :     POSIX_ENSURE(extension_oid, S2N_ERR_INVALID_ARGUMENT);
     884                 :            : 
     885 [ +  - ][ +  - ]:          1 :     POSIX_ENSURE(s2n_libcrypto_supports_custom_oid(), S2N_ERR_API_UNSUPPORTED_BY_LIBCRYPTO);
     886                 :            : 
     887         [ #  # ]:          0 :     if (config->custom_x509_extension_oids == NULL) {
     888                 :          0 :         config->custom_x509_extension_oids = sk_ASN1_OBJECT_new_null();
     889                 :          0 :     }
     890 [ #  # ][ #  # ]:          0 :     POSIX_ENSURE_REF(config->custom_x509_extension_oids);
     891                 :            : 
     892                 :          0 :     DEFER_CLEANUP(struct s2n_blob oid_buffer = { 0 }, s2n_free);
     893         [ #  # ]:          0 :     POSIX_GUARD(s2n_alloc(&oid_buffer, extension_oid_len + 1));
     894                 :            : 
     895         [ #  # ]:          0 :     POSIX_GUARD(s2n_blob_zero(&oid_buffer));
     896 [ #  # ][ #  # ]:          0 :     POSIX_CHECKED_MEMCPY(oid_buffer.data, extension_oid, extension_oid_len);
                 [ #  # ]
     897                 :          0 :     oid_buffer.data[extension_oid_len] = '\0';
     898                 :            : 
     899                 :          0 :     const char *oid_string = (const char *) oid_buffer.data;
     900 [ #  # ][ #  # ]:          0 :     POSIX_ENSURE_REF(oid_string);
     901                 :            : 
     902                 :          0 :     ASN1_OBJECT *critical_oid = OBJ_txt2obj(oid_string, 1);
     903 [ #  # ][ #  # ]:          0 :     POSIX_ENSURE_REF(critical_oid);
     904 [ #  # ][ #  # ]:          0 :     POSIX_ENSURE(sk_ASN1_OBJECT_push(config->custom_x509_extension_oids, critical_oid) > 0, S2N_ERR_INTERNAL_LIBCRYPTO_ERROR);
     905                 :            : 
     906                 :          0 :     return S2N_SUCCESS;
     907                 :          0 : }
     908                 :            : 
     909                 :            : int s2n_config_set_cert_request_callback(struct s2n_config *config, s2n_cert_request_callback cert_request_cb, void *ctx)
     910                 :          3 : {
     911 [ +  - ][ +  + ]:          3 :     POSIX_ENSURE(config, S2N_ERR_INVALID_ARGUMENT);
     912                 :            : 
     913                 :          2 :     config->cert_request_cb = cert_request_cb;
     914                 :          2 :     config->cert_request_cb_ctx = ctx;
     915                 :            : 
     916                 :          2 :     return S2N_SUCCESS;
     917                 :          3 : }
     918                 :            : 
     919                 :            : int s2n_config_set_client_hello_cb(struct s2n_config *config, s2n_client_hello_fn client_hello_cb, void *ctx)
     920                 :        124 : {
     921 [ #  # ][ -  + ]:        124 :     POSIX_ENSURE_REF(config);
     922                 :            : 
     923                 :        124 :     config->client_hello_cb = client_hello_cb;
     924                 :        124 :     config->client_hello_cb_ctx = ctx;
     925                 :        124 :     return 0;
     926                 :        124 : }
     927                 :            : 
     928                 :            : int s2n_config_set_client_hello_cb_mode(struct s2n_config *config, s2n_client_hello_cb_mode cb_mode)
     929                 :         72 : {
     930 [ -  + ][ #  # ]:         72 :     POSIX_ENSURE_REF(config);
     931 [ #  # ][ +  + ]:         72 :     POSIX_ENSURE(cb_mode == S2N_CLIENT_HELLO_CB_BLOCKING || cb_mode == S2N_CLIENT_HELLO_CB_NONBLOCKING, S2N_ERR_INVALID_STATE);
                 [ +  - ]
     932                 :            : 
     933                 :         72 :     config->client_hello_cb_mode = cb_mode;
     934                 :         72 :     return S2N_SUCCESS;
     935                 :         72 : }
     936                 :            : 
     937                 :            : int s2n_config_send_max_fragment_length(struct s2n_config *config, s2n_max_frag_len mfl_code)
     938                 :        257 : {
     939 [ -  + ][ #  # ]:        257 :     POSIX_ENSURE_REF(config);
     940                 :            : 
     941 [ +  + ][ +  - ]:        257 :     S2N_ERROR_IF(mfl_code > S2N_TLS_MAX_FRAG_LEN_4096, S2N_ERR_INVALID_MAX_FRAG_LEN);
     942                 :            : 
     943                 :        256 :     config->mfl_code = mfl_code;
     944                 :            : 
     945                 :        256 :     return 0;
     946                 :        257 : }
     947                 :            : 
     948                 :            : int s2n_config_accept_max_fragment_length(struct s2n_config *config)
     949                 :         12 : {
     950 [ #  # ][ -  + ]:         12 :     POSIX_ENSURE_REF(config);
     951                 :            : 
     952                 :         12 :     config->accept_mfl = 1;
     953                 :            : 
     954                 :         12 :     return 0;
     955                 :         12 : }
     956                 :            : 
     957                 :            : int s2n_config_set_max_fragment_length(struct s2n_config *config, uint16_t max_fragment_length)
     958                 :         11 : {
     959 [ +  + ][ +  - ]:         11 :     POSIX_ENSURE_REF(config);
     960                 :            : 
     961 [ +  + ][ +  - ]:         10 :     POSIX_ENSURE(max_fragment_length > 0, S2N_ERR_FRAGMENT_LENGTH_TOO_SMALL);
     962 [ +  + ][ +  - ]:          9 :     POSIX_ENSURE(max_fragment_length <= S2N_TLS_MAXIMUM_FRAGMENT_LENGTH, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE);
     963                 :            : 
     964                 :          8 :     config->max_fragment_length = max_fragment_length;
     965                 :            : 
     966                 :          8 :     return S2N_SUCCESS;
     967                 :          9 : }
     968                 :            : 
     969                 :            : int s2n_config_set_session_state_lifetime(struct s2n_config *config,
     970                 :            :         uint64_t lifetime_in_secs)
     971                 :         16 : {
     972 [ -  + ][ #  # ]:         16 :     POSIX_ENSURE_REF(config);
     973                 :            : 
     974                 :         16 :     config->session_state_lifetime_in_nanos = (lifetime_in_secs * ONE_SEC_IN_NANOS);
     975                 :         16 :     return 0;
     976                 :         16 : }
     977                 :            : 
     978                 :            : int s2n_config_set_session_tickets_onoff(struct s2n_config *config, uint8_t enabled)
     979                 :        165 : {
     980 [ +  - ][ +  + ]:        165 :     POSIX_ENSURE_REF(config);
     981                 :            : 
     982         [ +  + ]:        163 :     if (config->use_tickets == enabled) {
     983                 :         19 :         return 0;
     984                 :         19 :     }
     985                 :            : 
     986                 :        144 :     config->use_tickets = enabled;
     987                 :            : 
     988         [ +  + ]:        144 :     if (config->initial_tickets_to_send == 0) {
     989                 :            :         /* Normally initial_tickets_to_send is set via s2n_config_set_initial_ticket_count.
     990                 :            :          * However, s2n_config_set_initial_ticket_count calls this method.
     991                 :            :          * So we set initial_tickets_to_send directly to avoid infinite recursion. */
     992                 :        136 :         config->initial_tickets_to_send = 1;
     993                 :        136 :     }
     994                 :            : 
     995                 :            :     /* session ticket || session id is enabled */
     996         [ +  + ]:        144 :     if (enabled) {
     997         [ -  + ]:        140 :         POSIX_GUARD(s2n_config_init_session_ticket_keys(config));
     998         [ +  - ]:        140 :     } else if (!config->use_session_cache) {
     999         [ -  + ]:          4 :         POSIX_GUARD(s2n_config_free_session_ticket_keys(config));
    1000                 :          4 :     }
    1001                 :            : 
    1002                 :        144 :     return 0;
    1003                 :        144 : }
    1004                 :            : 
    1005                 :            : int s2n_config_set_session_cache_onoff(struct s2n_config *config, uint8_t enabled)
    1006                 :          3 : {
    1007 [ #  # ][ -  + ]:          3 :     POSIX_ENSURE_REF(config);
    1008 [ +  - ][ +  + ]:          3 :     if (enabled && config->cache_store && config->cache_retrieve && config->cache_delete) {
         [ +  - ][ +  - ]
    1009         [ -  + ]:          2 :         POSIX_GUARD(s2n_config_init_session_ticket_keys(config));
    1010                 :          2 :         config->use_session_cache = 1;
    1011                 :          2 :     } else {
    1012         [ -  + ]:          1 :         if (!config->use_tickets) {
    1013         [ #  # ]:          0 :             POSIX_GUARD(s2n_config_free_session_ticket_keys(config));
    1014                 :          0 :         }
    1015                 :          1 :         config->use_session_cache = 0;
    1016                 :          1 :     }
    1017                 :          3 :     return 0;
    1018                 :          3 : }
    1019                 :            : 
    1020                 :            : int s2n_config_set_ticket_encrypt_decrypt_key_lifetime(struct s2n_config *config,
    1021                 :            :         uint64_t lifetime_in_secs)
    1022                 :          1 : {
    1023 [ -  + ][ #  # ]:          1 :     POSIX_ENSURE_REF(config);
    1024                 :            : 
    1025                 :          1 :     config->encrypt_decrypt_key_lifetime_in_nanos = (lifetime_in_secs * ONE_SEC_IN_NANOS);
    1026                 :          1 :     return 0;
    1027                 :          1 : }
    1028                 :            : 
    1029                 :            : int s2n_config_set_ticket_decrypt_key_lifetime(struct s2n_config *config,
    1030                 :            :         uint64_t lifetime_in_secs)
    1031                 :          1 : {
    1032 [ -  + ][ #  # ]:          1 :     POSIX_ENSURE_REF(config);
    1033                 :            : 
    1034                 :          1 :     config->decrypt_key_lifetime_in_nanos = (lifetime_in_secs * ONE_SEC_IN_NANOS);
    1035                 :          1 :     return 0;
    1036                 :          1 : }
    1037                 :            : 
    1038                 :            : int s2n_config_add_ticket_crypto_key(struct s2n_config *config,
    1039                 :            :         const uint8_t *name, uint32_t name_len,
    1040                 :            :         uint8_t *key, uint32_t key_len,
    1041                 :            :         uint64_t intro_time_in_seconds_from_epoch)
    1042                 :        145 : {
    1043 [ -  + ][ #  # ]:        145 :     POSIX_ENSURE_REF(config);
    1044 [ -  + ][ #  # ]:        145 :     POSIX_ENSURE_REF(name);
    1045 [ -  + ][ #  # ]:        145 :     POSIX_ENSURE_REF(key);
    1046                 :            : 
    1047                 :            :     /* both session ticket and session cache encryption/decryption can use the same key mechanism */
    1048 [ +  + ][ -  + ]:        145 :     if (!config->use_tickets && !config->use_session_cache) {
    1049                 :          0 :         return 0;
    1050                 :          0 :     }
    1051                 :            : 
    1052         [ -  + ]:        145 :     POSIX_GUARD(s2n_config_wipe_expired_ticket_crypto_keys(config, -1));
    1053                 :            : 
    1054 [ +  + ][ +  - ]:        145 :     POSIX_ENSURE(key_len != 0, S2N_ERR_INVALID_TICKET_KEY_LENGTH);
    1055                 :            : 
    1056                 :        144 :     uint32_t ticket_keys_len = 0;
    1057         [ -  + ]:        144 :     POSIX_GUARD_RESULT(s2n_array_num_elements(config->ticket_keys, &ticket_keys_len));
    1058 [ +  - ][ +  + ]:        144 :     POSIX_ENSURE(ticket_keys_len < S2N_MAX_TICKET_KEYS, S2N_ERR_TICKET_KEY_LIMIT);
    1059                 :            : 
    1060 [ +  - ][ +  + ]:        143 :     POSIX_ENSURE(name_len != 0, S2N_ERR_INVALID_TICKET_KEY_NAME_OR_NAME_LENGTH);
    1061 [ +  - ][ +  + ]:        142 :     POSIX_ENSURE(name_len <= S2N_TICKET_KEY_NAME_LEN, S2N_ERR_INVALID_TICKET_KEY_NAME_OR_NAME_LENGTH);
    1062                 :            : 
    1063                 :            :     /* Copy the name into a zero-padded array. */
    1064                 :            :     /* This ensures that all ticket names are equal in length, as the serialized name is fixed length */
    1065                 :        141 :     uint8_t name_data[S2N_TICKET_KEY_NAME_LEN] = { 0 };
    1066 [ -  + ][ #  # ]:        141 :     POSIX_CHECKED_MEMCPY(name_data, name, name_len);
                 [ +  - ]
    1067                 :            : 
    1068                 :        141 :     uint8_t output_pad[S2N_AES256_KEY_LEN + S2N_TICKET_AAD_IMPLICIT_LEN] = { 0 };
    1069                 :        141 :     struct s2n_blob out_key = { 0 };
    1070         [ -  + ]:        141 :     POSIX_GUARD(s2n_blob_init(&out_key, output_pad, s2n_array_len(output_pad)));
    1071                 :        141 :     struct s2n_blob in_key = { 0 };
    1072         [ -  + ]:        141 :     POSIX_GUARD(s2n_blob_init(&in_key, key, key_len));
    1073                 :        141 :     struct s2n_blob salt = { 0 };
    1074         [ -  + ]:        141 :     POSIX_GUARD(s2n_blob_init(&salt, NULL, 0));
    1075                 :        141 :     struct s2n_blob info = { 0 };
    1076         [ -  + ]:        141 :     POSIX_GUARD(s2n_blob_init(&info, NULL, 0));
    1077                 :            : 
    1078                 :        141 :     struct s2n_ticket_key *session_ticket_key = { 0 };
    1079                 :        141 :     DEFER_CLEANUP(struct s2n_blob allocator = { 0 }, s2n_free);
    1080         [ -  + ]:        141 :     POSIX_GUARD(s2n_alloc(&allocator, sizeof(struct s2n_ticket_key)));
    1081                 :        141 :     session_ticket_key = (struct s2n_ticket_key *) (void *) allocator.data;
    1082                 :            : 
    1083                 :        141 :     DEFER_CLEANUP(struct s2n_hmac_state hmac = { 0 }, s2n_hmac_free);
    1084                 :            : 
    1085         [ -  + ]:        141 :     POSIX_GUARD(s2n_hmac_new(&hmac));
    1086         [ -  + ]:        141 :     POSIX_GUARD(s2n_hkdf(&hmac, S2N_HMAC_SHA256, &salt, &in_key, &info, &out_key));
    1087                 :            : 
    1088 [ -  + ][ #  # ]:        141 :     POSIX_CHECKED_MEMCPY(session_ticket_key->key_name, name_data, s2n_array_len(name_data));
                 [ +  - ]
    1089 [ -  + ][ #  # ]:        141 :     POSIX_CHECKED_MEMCPY(session_ticket_key->aes_key, out_key.data, S2N_AES256_KEY_LEN);
                 [ +  - ]
    1090                 :        141 :     out_key.data = output_pad + S2N_AES256_KEY_LEN;
    1091 [ -  + ][ #  # ]:        141 :     POSIX_CHECKED_MEMCPY(session_ticket_key->implicit_aad, out_key.data, S2N_TICKET_AAD_IMPLICIT_LEN);
                 [ +  - ]
    1092                 :            : 
    1093         [ +  + ]:        141 :     if (intro_time_in_seconds_from_epoch == 0) {
    1094                 :         89 :         uint64_t now = 0;
    1095         [ -  + ]:         89 :         POSIX_GUARD_RESULT(s2n_config_wall_clock(config, &now));
    1096                 :         89 :         session_ticket_key->intro_timestamp = now;
    1097                 :         89 :     } else {
    1098                 :         52 :         session_ticket_key->intro_timestamp = (intro_time_in_seconds_from_epoch * ONE_SEC_IN_NANOS);
    1099                 :         52 :     }
    1100                 :            : 
    1101         [ +  + ]:        141 :     POSIX_GUARD(s2n_config_store_ticket_key(config, session_ticket_key));
    1102                 :            : 
    1103                 :        137 :     return 0;
    1104                 :        141 : }
    1105                 :            : 
    1106                 :            : int s2n_config_require_ticket_forward_secrecy(struct s2n_config *config, bool enabled)
    1107                 :          4 : {
    1108 [ #  # ][ -  + ]:          4 :     POSIX_ENSURE_REF(config);
    1109                 :            : 
    1110                 :          4 :     config->ticket_forward_secrecy = enabled;
    1111                 :            : 
    1112                 :          4 :     return S2N_SUCCESS;
    1113                 :          4 : }
    1114                 :            : 
    1115                 :            : int s2n_config_set_cert_tiebreak_callback(struct s2n_config *config, s2n_cert_tiebreak_callback cert_tiebreak_cb)
    1116                 :          1 : {
    1117 [ -  + ][ #  # ]:          1 :     POSIX_ENSURE_REF(config);
    1118                 :          1 :     config->cert_tiebreak_cb = cert_tiebreak_cb;
    1119                 :          1 :     return 0;
    1120                 :          1 : }
    1121                 :            : 
    1122                 :            : struct s2n_cert_chain_and_key *s2n_config_get_single_default_cert(struct s2n_config *config)
    1123                 :        184 : {
    1124 [ -  + ][ #  # ]:        184 :     PTR_ENSURE_REF(config);
    1125                 :        184 :     struct s2n_cert_chain_and_key *cert = NULL;
    1126                 :            : 
    1127         [ +  + ]:        920 :     for (int i = S2N_CERT_TYPE_COUNT - 1; i >= 0; i--) {
    1128         [ +  + ]:        736 :         if (config->default_certs_by_type.certs[i] != NULL) {
    1129                 :        181 :             cert = config->default_certs_by_type.certs[i];
    1130                 :        181 :         }
    1131                 :        736 :     }
    1132                 :        184 :     return cert;
    1133                 :        184 : }
    1134                 :            : 
    1135                 :            : int s2n_config_get_num_default_certs(const struct s2n_config *config)
    1136                 :    3562878 : {
    1137 [ #  # ][ -  + ]:    3562878 :     POSIX_ENSURE_REF(config);
    1138                 :    3562878 :     int num_certs = 0;
    1139         [ +  + ]:   17814390 :     for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) {
    1140         [ +  + ]:   14251512 :         if (config->default_certs_by_type.certs[i] != NULL) {
    1141                 :      12081 :             num_certs++;
    1142                 :      12081 :         }
    1143                 :   14251512 :     }
    1144                 :            : 
    1145                 :    3562878 :     return num_certs;
    1146                 :    3562878 : }
    1147                 :            : 
    1148                 :            : int s2n_config_enable_cert_req_dss_legacy_compat(struct s2n_config *config)
    1149                 :          1 : {
    1150 [ -  + ][ #  # ]:          1 :     POSIX_ENSURE_REF(config);
    1151                 :          1 :     config->cert_req_dss_legacy_compat_enabled = 1;
    1152                 :          1 :     return S2N_SUCCESS;
    1153                 :          1 : }
    1154                 :            : 
    1155                 :            : int s2n_config_set_psk_selection_callback(struct s2n_config *config, s2n_psk_selection_callback cb, void *context)
    1156                 :          9 : {
    1157 [ +  - ][ +  + ]:          9 :     POSIX_ENSURE_REF(config);
    1158                 :          8 :     config->psk_selection_cb = cb;
    1159                 :          8 :     config->psk_selection_ctx = context;
    1160                 :          8 :     return S2N_SUCCESS;
    1161                 :          9 : }
    1162                 :            : 
    1163                 :            : int s2n_config_set_key_log_cb(struct s2n_config *config, s2n_key_log_fn callback, void *ctx)
    1164                 :          4 : {
    1165 [ -  + ][ #  # ]:          4 :     POSIX_ENSURE_MUT(config);
    1166                 :            : 
    1167                 :          4 :     config->key_log_cb = callback;
    1168                 :          4 :     config->key_log_ctx = ctx;
    1169                 :            : 
    1170                 :          4 :     return S2N_SUCCESS;
    1171                 :          4 : }
    1172                 :            : 
    1173                 :            : int s2n_config_set_async_pkey_validation_mode(struct s2n_config *config, s2n_async_pkey_validation_mode mode)
    1174                 :          4 : {
    1175 [ #  # ][ -  + ]:          4 :     POSIX_ENSURE_REF(config);
    1176                 :            : 
    1177         [ -  + ]:          4 :     switch (mode) {
    1178         [ -  + ]:          0 :         case S2N_ASYNC_PKEY_VALIDATION_FAST:
    1179         [ +  - ]:          4 :         case S2N_ASYNC_PKEY_VALIDATION_STRICT:
    1180                 :          4 :             config->async_pkey_validation_mode = mode;
    1181                 :          4 :             return S2N_SUCCESS;
    1182                 :          4 :     }
    1183                 :            : 
    1184         [ #  # ]:          0 :     POSIX_BAIL(S2N_ERR_INVALID_ARGUMENT);
    1185                 :          0 : }
    1186                 :            : 
    1187                 :            : int s2n_config_set_ctx(struct s2n_config *config, void *ctx)
    1188                 :          1 : {
    1189 [ -  + ][ #  # ]:          1 :     POSIX_ENSURE_REF(config);
    1190                 :            : 
    1191                 :          1 :     config->context = ctx;
    1192                 :            : 
    1193                 :          1 :     return S2N_SUCCESS;
    1194                 :          1 : }
    1195                 :            : 
    1196                 :            : int s2n_config_get_ctx(struct s2n_config *config, void **ctx)
    1197                 :          2 : {
    1198 [ -  + ][ #  # ]:          2 :     POSIX_ENSURE_REF(config);
    1199 [ -  + ][ #  # ]:          2 :     POSIX_ENSURE_REF(ctx);
    1200                 :            : 
    1201                 :          2 :     *ctx = config->context;
    1202                 :            : 
    1203                 :          2 :     return S2N_SUCCESS;
    1204                 :          2 : }
    1205                 :            : 
    1206                 :            : int s2n_config_set_send_buffer_size(struct s2n_config *config, uint32_t size)
    1207                 :         11 : {
    1208 [ +  - ][ +  + ]:         11 :     POSIX_ENSURE_REF(config);
    1209 [ +  + ][ +  - ]:         10 :     POSIX_ENSURE(size >= S2N_MIN_SEND_BUFFER_SIZE, S2N_ERR_INVALID_ARGUMENT);
    1210                 :          8 :     config->send_buffer_size_override = size;
    1211                 :          8 :     return S2N_SUCCESS;
    1212                 :         10 : }
    1213                 :            : 
    1214                 :            : int s2n_config_set_verify_after_sign(struct s2n_config *config, s2n_verify_after_sign mode)
    1215                 :         25 : {
    1216 [ +  + ][ +  - ]:         25 :     POSIX_ENSURE_REF(config);
    1217                 :         24 :     switch (mode) {
    1218         [ +  + ]:          1 :         case S2N_VERIFY_AFTER_SIGN_DISABLED:
    1219                 :          1 :             config->verify_after_sign = false;
    1220                 :          1 :             break;
    1221         [ +  + ]:         21 :         case S2N_VERIFY_AFTER_SIGN_ENABLED:
    1222                 :         21 :             config->verify_after_sign = true;
    1223                 :         21 :             break;
    1224         [ +  + ]:          2 :         default:
    1225         [ +  - ]:          2 :             POSIX_BAIL(S2N_ERR_INVALID_ARGUMENT);
    1226                 :         24 :     }
    1227                 :         22 :     return S2N_SUCCESS;
    1228                 :         24 : }
    1229                 :            : 
    1230                 :            : /*
    1231                 :            :  *= https://www.rfc-editor.org/rfc/rfc5746#5
    1232                 :            :  *# TLS implementations SHOULD provide a mechanism to disable and enable
    1233                 :            :  *# renegotiation.
    1234                 :            :  */
    1235                 :            : int s2n_config_set_renegotiate_request_cb(struct s2n_config *config, s2n_renegotiate_request_cb cb, void *ctx)
    1236                 :         18 : {
    1237 [ +  + ][ +  - ]:         18 :     POSIX_ENSURE_REF(config);
    1238                 :            : 
    1239                 :            :     /* This feature cannot be used with serialization currently */
    1240 [ +  + ][ +  - ]:         17 :     POSIX_ENSURE(config->serialized_connection_version == S2N_SERIALIZED_CONN_NONE, S2N_ERR_INVALID_STATE);
    1241                 :            : 
    1242                 :         16 :     config->renegotiate_request_cb = cb;
    1243                 :         16 :     config->renegotiate_request_ctx = ctx;
    1244                 :         16 :     return S2N_SUCCESS;
    1245                 :         17 : }
    1246                 :            : 
    1247                 :            : int s2n_config_set_npn(struct s2n_config *config, bool enable)
    1248                 :          3 : {
    1249 [ +  - ][ +  + ]:          3 :     POSIX_ENSURE_REF(config);
    1250                 :            : 
    1251                 :          2 :     config->npn_supported = enable;
    1252                 :            : 
    1253                 :          2 :     return S2N_SUCCESS;
    1254                 :          3 : }
    1255                 :            : 
    1256                 :            : /*
    1257                 :            :  * Wrapper for wall_clock callback. This wrapper will ensure right return of s2n_errno everytime wall_clock
    1258                 :            :  * callback is called.
    1259                 :            :  */
    1260                 :            : S2N_RESULT s2n_config_wall_clock(struct s2n_config *config, uint64_t *output)
    1261                 :       4395 : {
    1262 [ -  + ][ #  # ]:       4395 :     RESULT_ENSURE_REF(config);
    1263 [ -  + ][ #  # ]:       4395 :     RESULT_ENSURE(config->wall_clock(config->sys_clock_ctx, output) >= S2N_SUCCESS, S2N_ERR_CANCELLED);
    1264                 :       4395 :     return S2N_RESULT_OK;
    1265                 :       4395 : }
    1266                 :            : 
    1267                 :            : /*
    1268                 :            :  * Get the indicated time from the monotonic clock.
    1269                 :            :  *
    1270                 :            :  * This callback ensures that the correct errno is set in the case of failure.
    1271                 :            :  */
    1272                 :            : S2N_RESULT s2n_config_monotonic_clock(struct s2n_config *config, uint64_t *output)
    1273                 :          0 : {
    1274 [ #  # ][ #  # ]:          0 :     RESULT_ENSURE_REF(config);
    1275 [ #  # ][ #  # ]:          0 :     RESULT_ENSURE(config->monotonic_clock(config->monotonic_clock_ctx, output) >= S2N_SUCCESS, S2N_ERR_CANCELLED);
    1276                 :          0 :     return S2N_RESULT_OK;
    1277                 :          0 : }
    1278                 :            : 
    1279                 :            : int s2n_config_set_crl_lookup_cb(struct s2n_config *config, s2n_crl_lookup_callback cb, void *ctx)
    1280                 :         22 : {
    1281 [ +  + ][ +  - ]:         22 :     POSIX_ENSURE_REF(config);
    1282                 :         21 :     config->crl_lookup_cb = cb;
    1283                 :         21 :     config->crl_lookup_ctx = ctx;
    1284                 :         21 :     return S2N_SUCCESS;
    1285                 :         22 : }
    1286                 :            : 
    1287                 :            : int s2n_config_set_recv_multi_record(struct s2n_config *config, bool enabled)
    1288                 :          6 : {
    1289 [ -  + ][ #  # ]:          6 :     POSIX_ENSURE_REF(config);
    1290                 :            : 
    1291                 :          6 :     config->recv_multi_record = enabled;
    1292                 :            : 
    1293                 :          6 :     return S2N_SUCCESS;
    1294                 :          6 : }
    1295                 :            : 
    1296                 :            : int s2n_config_set_cert_validation_cb(struct s2n_config *config, s2n_cert_validation_callback cb, void *ctx)
    1297                 :         34 : {
    1298 [ +  + ][ +  - ]:         34 :     POSIX_ENSURE_REF(config);
    1299                 :            : 
    1300                 :         33 :     config->cert_validation_cb = cb;
    1301                 :         33 :     config->cert_validation_ctx = ctx;
    1302                 :            : 
    1303                 :         33 :     return S2N_SUCCESS;
    1304                 :         34 : }
    1305                 :            : 
    1306                 :            : int s2n_config_get_supported_groups(struct s2n_config *config, uint16_t *groups, uint16_t groups_count_max,
    1307                 :            :         uint16_t *groups_count_out)
    1308                 :        226 : {
    1309 [ +  + ][ +  - ]:        226 :     POSIX_ENSURE_REF(groups_count_out);
    1310                 :        225 :     *groups_count_out = 0;
    1311 [ +  + ][ +  - ]:        225 :     POSIX_ENSURE_REF(config);
    1312 [ +  + ][ +  - ]:        224 :     POSIX_ENSURE_REF(groups);
    1313                 :            : 
    1314                 :        223 :     const struct s2n_security_policy *security_policy = config->security_policy;
    1315 [ #  # ][ -  + ]:        223 :     POSIX_ENSURE_REF(security_policy);
    1316                 :        223 :     const struct s2n_kem_preferences *kem_preferences = security_policy->kem_preferences;
    1317 [ #  # ][ -  + ]:        223 :     POSIX_ENSURE_REF(kem_preferences);
    1318                 :        223 :     const struct s2n_ecc_preferences *ecc_preferences = security_policy->ecc_preferences;
    1319 [ #  # ][ -  + ]:        223 :     POSIX_ENSURE_REF(ecc_preferences);
    1320                 :            : 
    1321                 :        223 :     uint16_t groups_count = 0;
    1322         [ +  + ]:        408 :     for (uint8_t i = 0; i < kem_preferences->tls13_kem_group_count; i++) {
    1323                 :        185 :         const struct s2n_kem_group *kem_group = kem_preferences->tls13_kem_groups[i];
    1324 [ #  # ][ -  + ]:        185 :         POSIX_ENSURE_REF(kem_group);
    1325         [ +  - ]:        185 :         if (!s2n_kem_group_is_available(kem_group)) {
    1326                 :        185 :             continue;
    1327                 :        185 :         }
    1328                 :            : 
    1329 [ #  # ][ #  # ]:          0 :         POSIX_ENSURE(groups_count < groups_count_max, S2N_ERR_INSUFFICIENT_MEM_SIZE);
    1330                 :          0 :         groups[groups_count] = kem_group->iana_id;
    1331                 :          0 :         groups_count += 1;
    1332                 :          0 :     }
    1333                 :            : 
    1334         [ +  + ]:        796 :     for (uint8_t i = 0; i < ecc_preferences->count; i++) {
    1335                 :        578 :         const struct s2n_ecc_named_curve *ecc_curve = ecc_preferences->ecc_curves[i];
    1336 [ -  + ][ #  # ]:        578 :         POSIX_ENSURE_REF(ecc_curve);
    1337                 :            : 
    1338 [ +  + ][ +  - ]:        578 :         POSIX_ENSURE(groups_count < groups_count_max, S2N_ERR_INSUFFICIENT_MEM_SIZE);
    1339                 :        573 :         groups[groups_count] = ecc_curve->iana_id;
    1340                 :        573 :         groups_count += 1;
    1341                 :        573 :     }
    1342                 :            : 
    1343                 :        218 :     *groups_count_out = groups_count;
    1344                 :            : 
    1345                 :        218 :     return S2N_SUCCESS;
    1346                 :        223 : }
    1347                 :            : 
    1348                 :            : int s2n_config_set_serialization_version(struct s2n_config *config, s2n_serialization_version version)
    1349                 :         10 : {
    1350 [ -  + ][ #  # ]:         10 :     POSIX_ENSURE_REF(config);
    1351                 :            : 
    1352                 :            :     /* This feature cannot be used with renegotiation currently */
    1353 [ +  + ][ +  - ]:         10 :     POSIX_ENSURE(config->renegotiate_request_cb == NULL, S2N_ERR_INVALID_STATE);
    1354                 :            : 
    1355                 :            :     /* Currently there is only one format version supported */
    1356 [ -  + ][ #  # ]:          9 :     POSIX_ENSURE_EQ(version, S2N_SERIALIZED_CONN_V1);
    1357                 :          9 :     config->serialized_connection_version = version;
    1358                 :            : 
    1359                 :          9 :     return S2N_SUCCESS;
    1360                 :          9 : }
    1361                 :            : 
    1362                 :            : int s2n_config_set_max_blinding_delay(struct s2n_config *config, uint32_t seconds)
    1363                 :        413 : {
    1364 [ -  + ][ #  # ]:        413 :     POSIX_ENSURE_REF(config);
    1365                 :            : 
    1366                 :        413 :     config->custom_blinding_set = 1;
    1367                 :        413 :     config->max_blinding = seconds;
    1368                 :            : 
    1369                 :        413 :     return S2N_SUCCESS;
    1370                 :        413 : }
    1371                 :            : 
    1372                 :            : int s2n_config_set_subscriber(struct s2n_config *config, void *subscriber)
    1373                 :          7 : {
    1374 [ +  + ][ +  - ]:          7 :     POSIX_ENSURE_REF(config);
    1375                 :          5 :     config->subscriber = subscriber;
    1376                 :          5 :     return S2N_SUCCESS;
    1377                 :          7 : }
    1378                 :            : 
    1379                 :            : int s2n_config_set_handshake_event(struct s2n_config *config, s2n_event_on_handshake_cb callback)
    1380                 :          1 : {
    1381 [ -  + ][ #  # ]:          1 :     POSIX_ENSURE_REF(config);
    1382                 :          1 :     config->on_handshake_event = callback;
    1383                 :          1 :     return S2N_SUCCESS;
    1384                 :          1 : }
    1385                 :            : 
    1386                 :            : int s2n_config_set_timing_checkpoint_cb(struct s2n_config *config, s2n_event_on_timing_checkpoint_cb callback)
    1387                 :          7 : {
    1388 [ +  + ][ +  - ]:          7 :     POSIX_ENSURE_REF(config);
    1389 [ +  - ][ +  + ]:          6 :     POSIX_ENSURE_REF(callback);
    1390                 :          5 :     config->on_timing_checkpoint_cb = callback;
    1391                 :          5 :     return S2N_SUCCESS;
    1392                 :          6 : }

Generated by: LCOV version 1.14