LCOV - code coverage report
Current view: top level - tls - s2n_x509_validator.c (source / functions) Hit Total Coverage
Test: unit_test_coverage.info Lines: 585 629 93.0 %
Date: 2025-12-31 08:28:16 Functions: 36 38 94.7 %
Branches: 388 782 49.6 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License").
       5                 :            :  * You may not use this file except in compliance with the License.
       6                 :            :  * A copy of the License is located at
       7                 :            :  *
       8                 :            :  *  http://aws.amazon.com/apache2.0
       9                 :            :  *
      10                 :            :  * or in the "license" file accompanying this file. This file is distributed
      11                 :            :  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
      12                 :            :  * express or implied. See the License for the specific language governing
      13                 :            :  * permissions and limitations under the License.
      14                 :            :  */
      15                 :            : 
      16                 :            : #include <arpa/inet.h>
      17                 :            : #include <openssl/asn1.h>
      18                 :            : #include <openssl/err.h>
      19                 :            : #include <openssl/x509.h>
      20                 :            : #include <sys/socket.h>
      21                 :            : 
      22                 :            : #include "crypto/s2n_libcrypto.h"
      23                 :            : #include "crypto/s2n_openssl_x509.h"
      24                 :            : #include "crypto/s2n_pkey.h"
      25                 :            : #include "tls/extensions/s2n_extension_list.h"
      26                 :            : #include "tls/s2n_config.h"
      27                 :            : #include "tls/s2n_connection.h"
      28                 :            : #include "tls/s2n_crl.h"
      29                 :            : #include "tls/s2n_security_policies.h"
      30                 :            : #include "utils/s2n_result.h"
      31                 :            : #include "utils/s2n_rfc5952.h"
      32                 :            : #include "utils/s2n_safety.h"
      33                 :            : 
      34                 :            : #if S2N_OCSP_STAPLING_SUPPORTED
      35                 :            :     #include <openssl/ocsp.h>
      36                 :            : DEFINE_POINTER_CLEANUP_FUNC(OCSP_RESPONSE *, OCSP_RESPONSE_free);
      37                 :            : DEFINE_POINTER_CLEANUP_FUNC(OCSP_BASICRESP *, OCSP_BASICRESP_free);
      38                 :            : 
      39                 :            : #endif
      40                 :            : 
      41                 :            : #ifndef X509_V_FLAG_PARTIAL_CHAIN
      42                 :            :     #define X509_V_FLAG_PARTIAL_CHAIN 0x80000
      43                 :            : #endif
      44                 :            : 
      45                 :    3561844 : #define DEFAULT_MAX_CHAIN_DEPTH 7
      46                 :            : /* Time used by default for nextUpdate if none provided in OCSP: 1 hour since thisUpdate. */
      47                 :            : #define DEFAULT_OCSP_NEXT_UPDATE_PERIOD 3600
      48                 :            : 
      49                 :            : /* s2n's internal clock measures epoch-nanoseconds stored with a uint64_t. The
      50                 :            :  * maximum representable timestamp is Sunday, July 21, 2554. time_t measures
      51                 :            :  * epoch-seconds in a int64_t or int32_t (platform dependent). If time_t is an
      52                 :            :  * int32_t, the maximum representable timestamp is January 19, 2038.
      53                 :            :  *
      54                 :            :  * This means that converting from the internal clock to a time_t is not safe,
      55                 :            :  * because the internal clock might hold a value that is too large to represent
      56                 :            :  * in a time_t. This constant represents the largest internal clock value that
      57                 :            :  * can be safely represented as a time_t.
      58                 :            :  */
      59                 :            : #define MAX_32_TIMESTAMP_NANOS 2147483647 * ONE_SEC_IN_NANOS
      60                 :            : 
      61                 :          0 : #define OSSL_VERIFY_CALLBACK_IGNORE_ERROR 1
      62                 :            : 
      63                 :        380 : DEFINE_POINTER_CLEANUP_FUNC(STACK_OF(X509_CRL) *, sk_X509_CRL_free);
      64                 :            : DEFINE_POINTER_CLEANUP_FUNC(STACK_OF(GENERAL_NAME) *, GENERAL_NAMES_free);
      65                 :            : 
      66                 :            : uint8_t s2n_x509_ocsp_stapling_supported(void)
      67                 :        312 : {
      68                 :        312 :     return S2N_OCSP_STAPLING_SUPPORTED;
      69                 :        312 : }
      70                 :            : 
      71                 :            : void s2n_x509_trust_store_init_empty(struct s2n_x509_trust_store *store)
      72                 :       3515 : {
      73                 :       3515 :     store->trust_store = NULL;
      74                 :       3515 : }
      75                 :            : 
      76                 :            : uint8_t s2n_x509_trust_store_has_certs(struct s2n_x509_trust_store *store)
      77                 :        415 : {
      78         [ +  + ]:        415 :     return store->trust_store ? (uint8_t) 1 : (uint8_t) 0;
      79                 :        415 : }
      80                 :            : 
      81                 :            : int s2n_x509_trust_store_add_pem(struct s2n_x509_trust_store *store, const char *pem)
      82                 :         56 : {
      83 [ -  + ][ #  # ]:         56 :     POSIX_ENSURE_REF(store);
      84 [ -  + ][ #  # ]:         56 :     POSIX_ENSURE_REF(pem);
      85                 :            : 
      86         [ +  + ]:         56 :     if (!store->trust_store) {
      87                 :         35 :         store->trust_store = X509_STORE_new();
      88                 :         35 :     }
      89                 :            : 
      90                 :         56 :     DEFER_CLEANUP(struct s2n_stuffer pem_in_stuffer = { 0 }, s2n_stuffer_free);
      91                 :         56 :     DEFER_CLEANUP(struct s2n_stuffer der_out_stuffer = { 0 }, s2n_stuffer_free);
      92                 :            : 
      93         [ -  + ]:         56 :     POSIX_GUARD(s2n_stuffer_alloc_ro_from_string(&pem_in_stuffer, pem));
      94         [ -  + ]:         56 :     POSIX_GUARD(s2n_stuffer_growable_alloc(&der_out_stuffer, 2048));
      95                 :            : 
      96                 :        274 :     do {
      97                 :        274 :         DEFER_CLEANUP(struct s2n_blob next_cert = { 0 }, s2n_free);
      98                 :            : 
      99         [ +  + ]:        274 :         POSIX_GUARD(s2n_stuffer_certificate_from_pem(&pem_in_stuffer, &der_out_stuffer));
     100         [ -  + ]:        273 :         POSIX_GUARD(s2n_alloc(&next_cert, s2n_stuffer_data_available(&der_out_stuffer)));
     101         [ -  + ]:        273 :         POSIX_GUARD(s2n_stuffer_read(&der_out_stuffer, &next_cert));
     102                 :            : 
     103                 :        273 :         const uint8_t *data = next_cert.data;
     104                 :        273 :         DEFER_CLEANUP(X509 *ca_cert = d2i_X509(NULL, &data, next_cert.size), X509_free_pointer);
     105 [ -  + ][ #  # ]:        273 :         S2N_ERROR_IF(ca_cert == NULL, S2N_ERR_DECODE_CERTIFICATE);
     106                 :            : 
     107         [ -  + ]:        273 :         if (!X509_STORE_add_cert(store->trust_store, ca_cert)) {
     108                 :          0 :             unsigned long error = ERR_get_error();
     109 [ #  # ][ #  # ]:          0 :             POSIX_ENSURE(ERR_GET_REASON(error) == X509_R_CERT_ALREADY_IN_HASH_TABLE, S2N_ERR_DECODE_CERTIFICATE);
     110                 :          0 :         }
     111         [ +  + ]:        273 :     } while (s2n_stuffer_data_available(&pem_in_stuffer));
     112                 :            : 
     113                 :         55 :     return 0;
     114                 :         56 : }
     115                 :            : 
     116                 :            : int s2n_x509_trust_store_from_ca_file(struct s2n_x509_trust_store *store, const char *ca_pem_filename, const char *ca_dir)
     117                 :        316 : {
     118         [ +  + ]:        316 :     if (!store->trust_store) {
     119                 :        105 :         store->trust_store = X509_STORE_new();
     120 [ -  + ][ #  # ]:        105 :         POSIX_ENSURE_REF(store->trust_store);
     121                 :        105 :     }
     122                 :            : 
     123                 :        316 :     int err_code = X509_STORE_load_locations(store->trust_store, ca_pem_filename, ca_dir);
     124         [ +  + ]:        316 :     if (!err_code) {
     125                 :          2 :         s2n_x509_trust_store_wipe(store);
     126         [ +  - ]:          2 :         POSIX_BAIL(S2N_ERR_X509_TRUST_STORE);
     127                 :          2 :     }
     128                 :            : 
     129                 :        314 :     return 0;
     130                 :        316 : }
     131                 :            : 
     132                 :            : void s2n_x509_trust_store_wipe(struct s2n_x509_trust_store *store)
     133                 :       4364 : {
     134         [ +  + ]:       4364 :     if (store->trust_store) {
     135                 :       2959 :         X509_STORE_free(store->trust_store);
     136                 :       2959 :         store->trust_store = NULL;
     137                 :       2959 :         store->loaded_system_certs = false;
     138                 :       2959 :     }
     139                 :       4364 : }
     140                 :            : 
     141                 :            : int s2n_x509_validator_init_no_x509_validation(struct s2n_x509_validator *validator)
     142                 :      10294 : {
     143 [ -  + ][ #  # ]:      10294 :     POSIX_ENSURE_REF(validator);
     144                 :      10294 :     validator->trust_store = NULL;
     145                 :      10294 :     validator->store_ctx = NULL;
     146                 :      10294 :     validator->skip_cert_validation = 1;
     147                 :      10294 :     validator->check_stapled_ocsp = 0;
     148                 :      10294 :     validator->max_chain_depth = DEFAULT_MAX_CHAIN_DEPTH;
     149                 :      10294 :     validator->state = INIT;
     150                 :      10294 :     validator->cert_chain_from_wire = sk_X509_new_null();
     151                 :      10294 :     validator->crl_lookup_list = NULL;
     152                 :      10294 :     validator->cert_validation_info = (struct s2n_cert_validation_info){ 0 };
     153                 :      10294 :     validator->cert_validation_cb_invoked = false;
     154                 :            : 
     155                 :      10294 :     return 0;
     156                 :      10294 : }
     157                 :            : 
     158                 :            : int s2n_x509_validator_init(struct s2n_x509_validator *validator, struct s2n_x509_trust_store *trust_store, uint8_t check_ocsp)
     159                 :    3551550 : {
     160 [ -  + ][ #  # ]:    3551550 :     POSIX_ENSURE_REF(trust_store);
     161                 :    3551550 :     validator->trust_store = trust_store;
     162                 :    3551550 :     validator->skip_cert_validation = 0;
     163                 :    3551550 :     validator->check_stapled_ocsp = check_ocsp;
     164                 :    3551550 :     validator->max_chain_depth = DEFAULT_MAX_CHAIN_DEPTH;
     165                 :    3551550 :     validator->store_ctx = NULL;
     166         [ +  + ]:    3551550 :     if (validator->trust_store->trust_store) {
     167                 :    3550237 :         validator->store_ctx = X509_STORE_CTX_new();
     168 [ -  + ][ #  # ]:    3550237 :         POSIX_ENSURE_REF(validator->store_ctx);
     169                 :    3550237 :     }
     170                 :    3551550 :     validator->cert_chain_from_wire = sk_X509_new_null();
     171                 :    3551550 :     validator->state = INIT;
     172                 :    3551550 :     validator->crl_lookup_list = NULL;
     173                 :    3551550 :     validator->cert_validation_info = (struct s2n_cert_validation_info){ 0 };
     174                 :    3551550 :     validator->cert_validation_cb_invoked = false;
     175                 :            : 
     176                 :    3551550 :     return 0;
     177                 :    3551550 : }
     178                 :            : 
     179                 :            : static inline void wipe_cert_chain(STACK_OF(X509) *cert_chain)
     180                 :    7237641 : {
     181         [ +  + ]:    7237641 :     if (cert_chain) {
     182                 :    3561844 :         sk_X509_pop_free(cert_chain, X509_free);
     183                 :    3561844 :     }
     184                 :    7237641 : }
     185                 :            : 
     186                 :            : int s2n_x509_validator_wipe(struct s2n_x509_validator *validator)
     187                 :    7237641 : {
     188         [ +  + ]:    7237641 :     if (validator->store_ctx) {
     189                 :    3550237 :         X509_STORE_CTX_free(validator->store_ctx);
     190                 :    3550237 :         validator->store_ctx = NULL;
     191                 :    3550237 :     }
     192                 :    7237641 :     wipe_cert_chain(validator->cert_chain_from_wire);
     193                 :    7237641 :     validator->cert_chain_from_wire = NULL;
     194                 :    7237641 :     validator->trust_store = NULL;
     195                 :    7237641 :     validator->skip_cert_validation = 0;
     196                 :    7237641 :     validator->state = UNINIT;
     197                 :    7237641 :     validator->max_chain_depth = 0;
     198         [ +  + ]:    7237641 :     if (validator->crl_lookup_list) {
     199         [ -  + ]:         17 :         POSIX_GUARD_RESULT(s2n_array_free(validator->crl_lookup_list));
     200                 :         17 :         validator->crl_lookup_list = NULL;
     201                 :         17 :     }
     202                 :            : 
     203                 :    7237641 :     return S2N_SUCCESS;
     204                 :    7237641 : }
     205                 :            : 
     206                 :            : int s2n_x509_validator_set_max_chain_depth(struct s2n_x509_validator *validator, uint16_t max_depth)
     207                 :          3 : {
     208 [ #  # ][ -  + ]:          3 :     POSIX_ENSURE_REF(validator);
     209 [ +  + ][ +  - ]:          3 :     S2N_ERROR_IF(max_depth == 0, S2N_ERR_INVALID_ARGUMENT);
     210                 :            : 
     211                 :          2 :     validator->max_chain_depth = max_depth;
     212                 :          2 :     return 0;
     213                 :          3 : }
     214                 :            : 
     215                 :            : static S2N_RESULT s2n_verify_host_information_san_entry(struct s2n_connection *conn, GENERAL_NAME *current_name, bool *san_found)
     216                 :        341 : {
     217 [ -  + ][ #  # ]:        341 :     RESULT_ENSURE_REF(conn);
     218 [ #  # ][ -  + ]:        341 :     RESULT_ENSURE_REF(current_name);
     219 [ -  + ][ #  # ]:        341 :     RESULT_ENSURE_REF(san_found);
     220                 :            : 
     221 [ +  + ][ +  + ]:        341 :     if (current_name->type == GEN_DNS || current_name->type == GEN_URI) {
     222                 :        339 :         *san_found = true;
     223                 :            : 
     224                 :        339 :         const char *name = (const char *) ASN1_STRING_data(current_name->d.ia5);
     225 [ -  + ][ #  # ]:        339 :         RESULT_ENSURE_REF(name);
     226                 :        339 :         int name_len = ASN1_STRING_length(current_name->d.ia5);
     227 [ -  + ][ #  # ]:        339 :         RESULT_ENSURE_GT(name_len, 0);
     228                 :            : 
     229 [ +  - ][ +  + ]:        339 :         RESULT_ENSURE(conn->verify_host_fn(name, name_len, conn->data_for_verify_host), S2N_ERR_CERT_UNTRUSTED);
     230                 :            : 
     231                 :        325 :         return S2N_RESULT_OK;
     232                 :        339 :     }
     233                 :            : 
     234         [ +  + ]:          2 :     if (current_name->type == GEN_IPADD) {
     235                 :          1 :         *san_found = true;
     236                 :            : 
     237                 :            :         /* try to validate an IP address if it's in the subject alt name. */
     238                 :          1 :         const unsigned char *ip_addr = current_name->d.iPAddress->data;
     239 [ -  + ][ #  # ]:          1 :         RESULT_ENSURE_REF(ip_addr);
     240                 :          1 :         int ip_addr_len = current_name->d.iPAddress->length;
     241 [ -  + ][ #  # ]:          1 :         RESULT_ENSURE_GT(ip_addr_len, 0);
     242                 :            : 
     243 [ -  + ][ #  # ]:          2 :         RESULT_STACK_BLOB(address, INET6_ADDRSTRLEN + 1, INET6_ADDRSTRLEN + 1);
                 [ -  + ]
     244                 :            : 
     245         [ -  + ]:          1 :         if (ip_addr_len == 4) {
     246         [ #  # ]:          0 :             RESULT_GUARD(s2n_inet_ntop(AF_INET, ip_addr, &address));
     247         [ +  - ]:          1 :         } else if (ip_addr_len == 16) {
     248         [ -  + ]:          1 :             RESULT_GUARD(s2n_inet_ntop(AF_INET6, ip_addr, &address));
     249                 :          1 :         } else {
     250                 :            :             /* we aren't able to parse this value so skip it */
     251         [ #  # ]:          0 :             RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
     252                 :          0 :         }
     253                 :            : 
     254                 :            :         /* strlen should be safe here since we made sure we were null terminated AND that inet_ntop succeeded */
     255                 :          1 :         const char *name = (const char *) address.data;
     256                 :          1 :         size_t name_len = strlen(name);
     257                 :            : 
     258 [ #  # ][ -  + ]:          1 :         RESULT_ENSURE(conn->verify_host_fn(name, name_len, conn->data_for_verify_host), S2N_ERR_CERT_UNTRUSTED);
     259                 :            : 
     260                 :          1 :         return S2N_RESULT_OK;
     261                 :          1 :     }
     262                 :            : 
     263                 :            :     /* we don't understand this entry type so skip it */
     264         [ +  - ]:          1 :     RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
     265                 :          1 : }
     266                 :            : 
     267                 :            : static S2N_RESULT s2n_verify_host_information_san(struct s2n_connection *conn, X509 *public_cert, bool *san_found)
     268                 :        392 : {
     269 [ #  # ][ -  + ]:        392 :     RESULT_ENSURE_REF(conn);
     270 [ -  + ][ #  # ]:        392 :     RESULT_ENSURE_REF(public_cert);
     271 [ -  + ][ #  # ]:        392 :     RESULT_ENSURE_REF(san_found);
     272                 :            : 
     273                 :        392 :     *san_found = false;
     274                 :            : 
     275                 :        392 :     DEFER_CLEANUP(STACK_OF(GENERAL_NAME) *names_list = NULL, GENERAL_NAMES_free_pointer);
     276                 :        392 :     names_list = X509_get_ext_d2i(public_cert, NID_subject_alt_name, NULL, NULL);
     277 [ +  + ][ +  - ]:        392 :     RESULT_ENSURE(names_list, S2N_ERR_CERT_UNTRUSTED);
     278                 :            : 
     279                 :        338 :     int n = sk_GENERAL_NAME_num(names_list);
     280 [ -  + ][ #  # ]:        338 :     RESULT_ENSURE(n > 0, S2N_ERR_CERT_UNTRUSTED);
     281                 :            : 
     282                 :        338 :     s2n_result result = S2N_RESULT_OK;
     283         [ +  + ]:        353 :     for (int i = 0; i < n; i++) {
     284                 :        341 :         GENERAL_NAME *current_name = sk_GENERAL_NAME_value(names_list, i);
     285                 :            : 
     286                 :            :         /* return success on the first entry that passes verification */
     287                 :        341 :         result = s2n_verify_host_information_san_entry(conn, current_name, san_found);
     288         [ +  + ]:        341 :         if (s2n_result_is_ok(result)) {
     289                 :        326 :             return S2N_RESULT_OK;
     290                 :        326 :         }
     291                 :        341 :     }
     292                 :            : 
     293                 :            :     /* if an error was set by one of the entries, then just propagate the error from the last SAN entry call */
     294         [ +  - ]:         12 :     RESULT_GUARD(result);
     295                 :            : 
     296         [ #  # ]:          0 :     RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
     297                 :          0 : }
     298                 :            : 
     299                 :            : static S2N_RESULT s2n_verify_host_information_common_name(struct s2n_connection *conn, X509 *public_cert, bool *cn_found)
     300                 :         55 : {
     301 [ -  + ][ #  # ]:         55 :     RESULT_ENSURE_REF(conn);
     302 [ -  + ][ #  # ]:         55 :     RESULT_ENSURE_REF(public_cert);
     303 [ #  # ][ -  + ]:         55 :     RESULT_ENSURE_REF(cn_found);
     304                 :            : 
     305                 :         55 :     X509_NAME *subject_name = X509_get_subject_name(public_cert);
     306 [ -  + ][ #  # ]:         55 :     RESULT_ENSURE(subject_name, S2N_ERR_CERT_UNTRUSTED);
     307                 :            : 
     308                 :         55 :     int curr_idx = -1;
     309                 :        109 :     while (true) {
     310                 :        109 :         int next_idx = X509_NAME_get_index_by_NID(subject_name, NID_commonName, curr_idx);
     311         [ +  + ]:        109 :         if (next_idx >= 0) {
     312                 :         54 :             curr_idx = next_idx;
     313                 :         55 :         } else {
     314                 :         55 :             break;
     315                 :         55 :         }
     316                 :        109 :     }
     317                 :            : 
     318 [ +  - ][ +  + ]:         55 :     RESULT_ENSURE(curr_idx >= 0, S2N_ERR_CERT_UNTRUSTED);
     319                 :            : 
     320                 :         54 :     ASN1_STRING *common_name = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject_name, curr_idx));
     321 [ -  + ][ #  # ]:         54 :     RESULT_ENSURE(common_name, S2N_ERR_CERT_UNTRUSTED);
     322                 :            : 
     323                 :            :     /* X520CommonName allows the following ANSI string types per RFC 5280 Appendix A.1 */
     324 [ #  # ][ -  + ]:         54 :     RESULT_ENSURE(ASN1_STRING_type(common_name) == V_ASN1_TELETEXSTRING
         [ +  - ][ -  + ]
         [ -  + ][ #  # ]
     325                 :         54 :                     || ASN1_STRING_type(common_name) == V_ASN1_PRINTABLESTRING
     326                 :         54 :                     || ASN1_STRING_type(common_name) == V_ASN1_UNIVERSALSTRING
     327                 :         54 :                     || ASN1_STRING_type(common_name) == V_ASN1_UTF8STRING
     328                 :         54 :                     || ASN1_STRING_type(common_name) == V_ASN1_BMPSTRING,
     329                 :         54 :             S2N_ERR_CERT_UNTRUSTED);
     330                 :            : 
     331                 :            :     /* at this point we have a valid CN value */
     332                 :         54 :     *cn_found = true;
     333                 :            : 
     334                 :         54 :     char peer_cn[255] = { 0 };
     335                 :         54 :     int cn_len = ASN1_STRING_length(common_name);
     336 [ -  + ][ #  # ]:         54 :     RESULT_ENSURE_GT(cn_len, 0);
     337                 :         54 :     uint32_t len = (uint32_t) cn_len;
     338 [ #  # ][ -  + ]:         54 :     RESULT_ENSURE_LTE(len, s2n_array_len(peer_cn) - 1);
     339 [ -  + ][ #  # ]:         54 :     RESULT_CHECKED_MEMCPY(peer_cn, ASN1_STRING_data(common_name), len);
                 [ +  - ]
     340 [ #  # ][ -  + ]:         54 :     RESULT_ENSURE(conn->verify_host_fn(peer_cn, len, conn->data_for_verify_host), S2N_ERR_CERT_UNTRUSTED);
     341                 :            : 
     342                 :         54 :     return S2N_RESULT_OK;
     343                 :         54 : }
     344                 :            : 
     345                 :            : /*
     346                 :            :  * For each name in the cert. Iterate them. Call the callback. If one returns true, then consider it validated,
     347                 :            :  * if none of them return true, the cert is considered invalid.
     348                 :            :  */
     349                 :            : static S2N_RESULT s2n_verify_host_information(struct s2n_connection *conn, X509 *public_cert)
     350                 :        392 : {
     351                 :        392 :     bool entry_found = false;
     352                 :            : 
     353                 :            :     /* Check SubjectAltNames before CommonName as per RFC 6125 6.4.4 */
     354                 :        392 :     s2n_result result = s2n_verify_host_information_san(conn, public_cert, &entry_found);
     355                 :            : 
     356                 :            :     /*
     357                 :            :      *= https://www.rfc-editor.org/rfc/rfc6125#section-6.4.4
     358                 :            :      *# As noted, a client MUST NOT seek a match for a reference identifier
     359                 :            :      *# of CN-ID if the presented identifiers include a DNS-ID, SRV-ID,
     360                 :            :      *# URI-ID, or any application-specific identifier types supported by the
     361                 :            :      *# client.
     362                 :            :      */
     363         [ +  + ]:        392 :     if (entry_found) {
     364                 :        337 :         return result;
     365                 :        337 :     }
     366                 :            : 
     367                 :            :     /*
     368                 :            :      *= https://www.rfc-editor.org/rfc/rfc6125#section-6.4.4
     369                 :            :      *# Therefore, if and only if the presented identifiers do not include a
     370                 :            :      *# DNS-ID, SRV-ID, URI-ID, or any application-specific identifier types
     371                 :            :      *# supported by the client, then the client MAY as a last resort check
     372                 :            :      *# for a string whose form matches that of a fully qualified DNS domain
     373                 :            :      *# name in a Common Name field of the subject field (i.e., a CN-ID).
     374                 :            :      */
     375                 :         55 :     result = s2n_verify_host_information_common_name(conn, public_cert, &entry_found);
     376         [ +  + ]:         55 :     if (entry_found) {
     377                 :         54 :         return result;
     378                 :         54 :     }
     379                 :            : 
     380                 :            :     /* make a null-terminated string in case the callback tries to use strlen */
     381                 :          1 :     const char *name = "";
     382                 :          1 :     size_t name_len = 0;
     383                 :            : 
     384                 :            :     /* at this point, we don't have anything to identify the certificate with so pass an empty string to the callback */
     385 [ #  # ][ -  + ]:          1 :     RESULT_ENSURE(conn->verify_host_fn(name, name_len, conn->data_for_verify_host), S2N_ERR_CERT_UNTRUSTED);
     386                 :            : 
     387                 :          1 :     return S2N_RESULT_OK;
     388                 :          1 : }
     389                 :            : 
     390                 :            : S2N_RESULT s2n_x509_validator_read_asn1_cert(struct s2n_stuffer *cert_chain_in_stuffer,
     391                 :            :         struct s2n_blob *asn1_cert)
     392                 :      15726 : {
     393                 :      15726 :     uint32_t certificate_size = 0;
     394                 :            : 
     395         [ -  + ]:      15726 :     RESULT_GUARD_POSIX(s2n_stuffer_read_uint24(cert_chain_in_stuffer, &certificate_size));
     396 [ +  + ][ +  - ]:      15726 :     RESULT_ENSURE(certificate_size > 0, S2N_ERR_CERT_INVALID);
     397 [ +  - ][ +  + ]:      15722 :     RESULT_ENSURE(certificate_size <= s2n_stuffer_data_available(cert_chain_in_stuffer), S2N_ERR_CERT_INVALID);
     398                 :            : 
     399                 :      15718 :     asn1_cert->size = certificate_size;
     400                 :      15718 :     asn1_cert->data = s2n_stuffer_raw_read(cert_chain_in_stuffer, certificate_size);
     401 [ -  + ][ #  # ]:      15718 :     RESULT_ENSURE_REF(asn1_cert->data);
     402                 :            : 
     403                 :      15718 :     return S2N_RESULT_OK;
     404                 :      15718 : }
     405                 :            : 
     406                 :            : /**
     407                 :            : * Validates that each certificate in a peer's cert chain contains only signature algorithms in a security policy's
     408                 :            : * certificate_signatures_preference list.
     409                 :            : */
     410                 :            : S2N_RESULT s2n_x509_validator_check_cert_preferences(struct s2n_connection *conn, X509 *cert)
     411                 :        885 : {
     412 [ #  # ][ -  + ]:        885 :     RESULT_ENSURE_REF(conn);
     413 [ -  + ][ #  # ]:        885 :     RESULT_ENSURE_REF(cert);
     414                 :            : 
     415                 :        885 :     const struct s2n_security_policy *security_policy = NULL;
     416         [ -  + ]:        885 :     RESULT_GUARD_POSIX(s2n_connection_get_security_policy(conn, &security_policy));
     417                 :            : 
     418                 :            :     /**
     419                 :            :      * We only restrict the signature algorithm on the certificates in the
     420                 :            :      * peer's certificate chain if the certificate_signature_preferences field
     421                 :            :      * is set in the security policy. This is contrary to the RFC, which
     422                 :            :      * specifies that the signatures in the "signature_algorithms" extension
     423                 :            :      * apply to signatures in the certificate chain in certain scenarios, so RFC
     424                 :            :      * compliance would imply validating that the certificate chain signature
     425                 :            :      * algorithm matches one of the algorithms specified in the
     426                 :            :      * "signature_algorithms" extension.
     427                 :            :      *
     428                 :            :      *= https://www.rfc-editor.org/rfc/rfc5246#section-7.4.2
     429                 :            :      *= type=exception
     430                 :            :      *= reason=not implemented due to lack of utility
     431                 :            :      *# If the client provided a "signature_algorithms" extension, then all
     432                 :            :      *# certificates provided by the server MUST be signed by a
     433                 :            :      *# hash/signature algorithm pair that appears in that extension.
     434                 :            :      *
     435                 :            :      *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.3
     436                 :            :      *= type=exception
     437                 :            :      *= reason=not implemented due to lack of utility
     438                 :            :      *# If no "signature_algorithms_cert" extension is present, then the
     439                 :            :      *# "signature_algorithms" extension also applies to signatures appearing in
     440                 :            :      *# certificates.
     441                 :            :      */
     442                 :        885 :     struct s2n_cert_info info = { 0 };
     443         [ -  + ]:        885 :     RESULT_GUARD(s2n_openssl_x509_get_cert_info(cert, &info));
     444                 :            : 
     445         [ +  + ]:        885 :     bool certificate_preferences_defined = security_policy->certificate_signature_preferences != NULL
     446         [ -  + ]:        885 :             || security_policy->certificate_key_preferences != NULL;
     447 [ +  + ][ +  + ]:        885 :     if (certificate_preferences_defined && !info.self_signed && conn->actual_protocol_version == S2N_TLS13) {
                 [ +  + ]
     448                 :            :         /* Ensure that the certificate signature does not use SHA-1. While this check
     449                 :            :          * would ideally apply to all connections, we only enforce it when certificate
     450                 :            :          * preferences exist to stay backwards compatible.
     451                 :            :          */
     452 [ +  - ][ +  + ]:         96 :         RESULT_ENSURE(info.signature_digest_nid != NID_sha1, S2N_ERR_CERT_UNTRUSTED);
     453                 :         96 :     }
     454                 :            : 
     455         [ +  + ]:        882 :     if (!info.self_signed) {
     456         [ +  + ]:        636 :         RESULT_GUARD(s2n_security_policy_validate_cert_signature(security_policy, &info, S2N_ERR_CERT_UNTRUSTED));
     457                 :        636 :     }
     458         [ +  + ]:        881 :     RESULT_GUARD(s2n_security_policy_validate_cert_key(security_policy, &info, S2N_ERR_CERT_UNTRUSTED));
     459                 :            : 
     460                 :        880 :     return S2N_RESULT_OK;
     461                 :        881 : }
     462                 :            : 
     463                 :            : S2N_RESULT s2n_x509_validator_get_validated_cert_chain(const struct s2n_x509_validator *validator,
     464                 :            :         struct s2n_validated_cert_chain *validated_cert_chain)
     465                 :        711 : {
     466 [ #  # ][ -  + ]:        711 :     RESULT_ENSURE_REF(validator);
     467 [ #  # ][ -  + ]:        711 :     RESULT_ENSURE_REF(validated_cert_chain);
     468                 :            : 
     469 [ +  - ][ +  + ]:        711 :     RESULT_ENSURE(s2n_x509_validator_is_cert_chain_validated(validator), S2N_ERR_INVALID_CERT_STATE);
     470 [ #  # ][ -  + ]:        710 :     RESULT_ENSURE_REF(validator->store_ctx);
     471                 :            : 
     472                 :        710 : #if S2N_LIBCRYPTO_SUPPORTS_GET0_CHAIN
     473                 :            :     /* X509_STORE_CTX_get0_chain is used when available, since it returns a pointer to the
     474                 :            :      * validated cert chain in the X509_STORE_CTX, avoiding an allocation/copy.
     475                 :            :      */
     476                 :        710 :     validated_cert_chain->stack = X509_STORE_CTX_get0_chain(validator->store_ctx);
     477                 :            : #else
     478                 :            :     /* Otherwise, X509_STORE_CTX_get1_chain is used instead, which allocates a new cert chain. */
     479                 :            :     validated_cert_chain->stack = X509_STORE_CTX_get1_chain(validator->store_ctx);
     480                 :            : #endif
     481                 :            : 
     482 [ #  # ][ -  + ]:        710 :     RESULT_ENSURE_REF(validated_cert_chain->stack);
     483                 :            : 
     484                 :        710 :     return S2N_RESULT_OK;
     485                 :        710 : }
     486                 :            : 
     487                 :            : S2N_CLEANUP_RESULT s2n_x509_validator_validated_cert_chain_free(struct s2n_validated_cert_chain *validated_cert_chain)
     488                 :        710 : {
     489 [ #  # ][ -  + ]:        710 :     RESULT_ENSURE_REF(validated_cert_chain);
     490                 :            : 
     491                 :            : #if !S2N_LIBCRYPTO_SUPPORTS_GET0_CHAIN
     492                 :            :     /* When X509_STORE_CTX_get0_chain isn't supported, X509_STORE_CTX_get1_chain is used instead,
     493                 :            :      * which allocates a new cert chain that is owned by s2n-tls and MUST be freed.
     494                 :            :      *
     495                 :            :      * X509_STORE_CTX_get0_chain returns a pointer to the cert chain within the X509_STORE_CTX,
     496                 :            :      * which is NOT owned by s2n-tls and MUST NOT be manually freed.
     497                 :            :      */
     498                 :            :     RESULT_GUARD(s2n_openssl_x509_stack_pop_free(&validated_cert_chain->stack));
     499                 :            : #endif
     500                 :            : 
     501                 :            :     /* Even though the cert chain reference is still valid in the case that get0_chain is used, set
     502                 :            :      * it to null for consistency with the get1_chain case.
     503                 :            :      */
     504                 :        710 :     validated_cert_chain->stack = NULL;
     505                 :            : 
     506                 :        710 :     return S2N_RESULT_OK;
     507                 :        710 : }
     508                 :            : 
     509                 :            : /* Validates that the root certificate uses a key allowed by the security policy
     510                 :            :  * certificate preferences.
     511                 :            :  */
     512                 :            : static S2N_RESULT s2n_x509_validator_check_root_cert(struct s2n_x509_validator *validator, struct s2n_connection *conn)
     513                 :        335 : {
     514 [ -  + ][ #  # ]:        335 :     RESULT_ENSURE_REF(validator);
     515 [ -  + ][ #  # ]:        335 :     RESULT_ENSURE_REF(conn);
     516                 :            : 
     517                 :        335 :     const struct s2n_security_policy *security_policy = NULL;
     518         [ -  + ]:        335 :     RESULT_GUARD_POSIX(s2n_connection_get_security_policy(conn, &security_policy));
     519 [ -  + ][ #  # ]:        335 :     RESULT_ENSURE_REF(security_policy);
     520                 :            : 
     521                 :        335 :     DEFER_CLEANUP(struct s2n_validated_cert_chain validated_cert_chain = { 0 }, s2n_x509_validator_validated_cert_chain_free);
     522         [ -  + ]:        335 :     RESULT_GUARD(s2n_x509_validator_get_validated_cert_chain(validator, &validated_cert_chain));
     523                 :        335 :     STACK_OF(X509) *cert_chain = validated_cert_chain.stack;
     524 [ -  + ][ #  # ]:        335 :     RESULT_ENSURE_REF(cert_chain);
     525                 :            : 
     526                 :        335 :     const int certs_in_chain = sk_X509_num(cert_chain);
     527 [ -  + ][ #  # ]:        335 :     RESULT_ENSURE(certs_in_chain > 0, S2N_ERR_CERT_UNTRUSTED);
     528                 :        335 :     X509 *root = sk_X509_value(cert_chain, certs_in_chain - 1);
     529 [ -  + ][ #  # ]:        335 :     RESULT_ENSURE_REF(root);
     530                 :            : 
     531                 :        335 :     struct s2n_cert_info info = { 0 };
     532         [ -  + ]:        335 :     RESULT_GUARD(s2n_openssl_x509_get_cert_info(root, &info));
     533                 :            : 
     534         [ +  + ]:        335 :     RESULT_GUARD(s2n_security_policy_validate_cert_key(security_policy, &info,
     535                 :        334 :             S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT));
     536                 :            : 
     537                 :        334 :     return S2N_RESULT_OK;
     538                 :        335 : }
     539                 :            : 
     540                 :            : static S2N_RESULT s2n_x509_validator_read_cert_chain(struct s2n_x509_validator *validator, struct s2n_connection *conn,
     541                 :            :         uint8_t *cert_chain_in, uint32_t cert_chain_len)
     542                 :       4996 : {
     543 [ +  - ][ +  + ]:       4996 :     RESULT_ENSURE(validator->skip_cert_validation || s2n_x509_trust_store_has_certs(validator->trust_store), S2N_ERR_CERT_UNTRUSTED);
                 [ +  + ]
     544 [ -  + ][ #  # ]:       4993 :     RESULT_ENSURE(validator->state == INIT, S2N_ERR_INVALID_CERT_STATE);
     545                 :            : 
     546                 :       4993 :     struct s2n_blob cert_chain_blob = { 0 };
     547         [ -  + ]:       4993 :     RESULT_GUARD_POSIX(s2n_blob_init(&cert_chain_blob, cert_chain_in, cert_chain_len));
     548                 :       4993 :     DEFER_CLEANUP(struct s2n_stuffer cert_chain_in_stuffer = { 0 }, s2n_stuffer_free);
     549                 :            : 
     550         [ -  + ]:       4993 :     RESULT_GUARD_POSIX(s2n_stuffer_init(&cert_chain_in_stuffer, &cert_chain_blob));
     551         [ -  + ]:       4993 :     RESULT_GUARD_POSIX(s2n_stuffer_write(&cert_chain_in_stuffer, &cert_chain_blob));
     552                 :            : 
     553         [ +  + ]:      18182 :     while (s2n_stuffer_data_available(&cert_chain_in_stuffer)
     554         [ +  + ]:      18182 :             && sk_X509_num(validator->cert_chain_from_wire) < validator->max_chain_depth) {
     555                 :      13201 :         struct s2n_blob asn1_cert = { 0 };
     556         [ +  + ]:      13201 :         RESULT_GUARD(s2n_x509_validator_read_asn1_cert(&cert_chain_in_stuffer, &asn1_cert));
     557                 :            : 
     558                 :            :         /* We only do the trailing byte validation when parsing the leaf cert to
     559                 :            :          * match historical s2n-tls behavior.
     560                 :            :          */
     561                 :      13193 :         DEFER_CLEANUP(X509 *cert = NULL, X509_free_pointer);
     562         [ +  + ]:      13193 :         if (sk_X509_num(validator->cert_chain_from_wire) == 0) {
     563         [ +  + ]:       4985 :             RESULT_GUARD(s2n_openssl_x509_parse(&asn1_cert, &cert));
     564                 :       8208 :         } else {
     565         [ -  + ]:       8208 :             RESULT_GUARD(s2n_openssl_x509_parse_without_length_validation(&asn1_cert, &cert));
     566                 :       8208 :         }
     567                 :            : 
     568         [ +  + ]:      13191 :         if (!validator->skip_cert_validation) {
     569         [ +  + ]:        879 :             RESULT_GUARD(s2n_x509_validator_check_cert_preferences(conn, cert));
     570                 :        879 :         }
     571                 :            : 
     572                 :            :         /* add the cert to the chain */
     573 [ -  + ][ #  # ]:      13189 :         RESULT_ENSURE(sk_X509_push(validator->cert_chain_from_wire, cert) > 0,
     574                 :      13189 :                 S2N_ERR_INTERNAL_LIBCRYPTO_ERROR);
     575                 :            : 
     576                 :            :         /* After the cert is added to cert_chain_from_wire, it will be freed
     577                 :            :          * with the call to s2n_x509_validator_wipe. We disable the cleanup
     578                 :            :          * function since cleanup is no longer "owned" by cert.
     579                 :            :          */
     580                 :      13189 :         ZERO_TO_DISABLE_DEFER_CLEANUP(cert);
     581                 :            : 
     582                 :            :         /* certificate extensions is a field in TLS 1.3 - https://tools.ietf.org/html/rfc8446#section-4.4.2 */
     583         [ +  + ]:      13189 :         if (conn->actual_protocol_version >= S2N_TLS13) {
     584                 :       6520 :             s2n_parsed_extensions_list parsed_extensions_list = { 0 };
     585         [ -  + ]:       6520 :             RESULT_GUARD_POSIX(s2n_extension_list_parse(&cert_chain_in_stuffer, &parsed_extensions_list));
     586                 :       6520 :         }
     587                 :      13189 :     }
     588                 :            : 
     589                 :            :     /* if this occurred we exceeded validator->max_chain_depth */
     590 [ +  - ][ +  + ]:       4981 :     RESULT_ENSURE(validator->skip_cert_validation || s2n_stuffer_data_available(&cert_chain_in_stuffer) == 0,
                 [ +  + ]
     591                 :       4980 :             S2N_ERR_CERT_MAX_CHAIN_DEPTH_EXCEEDED);
     592 [ +  + ][ +  - ]:       4980 :     RESULT_ENSURE(sk_X509_num(validator->cert_chain_from_wire) > 0, S2N_ERR_NO_CERT_FOUND);
     593                 :            : 
     594                 :       4979 :     return S2N_RESULT_OK;
     595                 :       4980 : }
     596                 :            : 
     597                 :            : static S2N_RESULT s2n_x509_validator_process_cert_chain(struct s2n_x509_validator *validator, struct s2n_connection *conn,
     598                 :            :         uint8_t *cert_chain_in, uint32_t cert_chain_len)
     599                 :       4996 : {
     600 [ -  + ][ #  # ]:       4996 :     RESULT_ENSURE(validator->state == INIT, S2N_ERR_INVALID_CERT_STATE);
     601                 :            : 
     602         [ +  + ]:       4996 :     RESULT_GUARD(s2n_x509_validator_read_cert_chain(validator, conn, cert_chain_in, cert_chain_len));
     603                 :            : 
     604         [ +  + ]:       4979 :     if (validator->skip_cert_validation) {
     605                 :       4587 :         return S2N_RESULT_OK;
     606                 :       4587 :     }
     607                 :            : 
     608                 :        392 :     X509 *leaf = sk_X509_value(validator->cert_chain_from_wire, 0);
     609 [ #  # ][ -  + ]:        392 :     RESULT_ENSURE_REF(leaf);
     610                 :            : 
     611         [ +  - ]:        392 :     if (conn->verify_host_fn) {
     612         [ +  + ]:        392 :         RESULT_GUARD(s2n_verify_host_information(conn, leaf));
     613                 :        392 :     }
     614                 :            : 
     615 [ #  # ][ -  + ]:        381 :     RESULT_GUARD_OSSL(X509_STORE_CTX_init(validator->store_ctx, validator->trust_store->trust_store, leaf,
     616                 :        381 :                               validator->cert_chain_from_wire),
     617                 :        381 :             S2N_ERR_INTERNAL_LIBCRYPTO_ERROR);
     618                 :            : 
     619         [ +  + ]:        381 :     if (conn->config->crl_lookup_cb) {
     620         [ +  + ]:         17 :         RESULT_GUARD(s2n_crl_invoke_lookup_callbacks(conn, validator));
     621         [ +  + ]:         16 :         RESULT_GUARD(s2n_crl_handle_lookup_callback_result(validator));
     622                 :         16 :     }
     623                 :            : 
     624                 :        379 :     validator->state = READY_TO_VERIFY;
     625                 :            : 
     626                 :        379 :     return S2N_RESULT_OK;
     627                 :        381 : }
     628                 :            : 
     629                 :            : static S2N_RESULT s2n_x509_validator_set_no_check_time_flag(struct s2n_x509_validator *validator)
     630                 :          8 : {
     631 [ -  + ][ #  # ]:          8 :     RESULT_ENSURE_REF(validator);
     632 [ -  + ][ #  # ]:          8 :     RESULT_ENSURE_REF(validator->store_ctx);
     633                 :            : 
     634                 :          8 :     X509_VERIFY_PARAM *param = X509_STORE_CTX_get0_param(validator->store_ctx);
     635 [ #  # ][ -  + ]:          8 :     RESULT_ENSURE_REF(param);
     636                 :            : 
     637                 :          8 : #ifdef S2N_LIBCRYPTO_SUPPORTS_FLAG_NO_CHECK_TIME
     638 [ -  + ][ #  # ]:          8 :     RESULT_GUARD_OSSL(X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_NO_CHECK_TIME),
     639                 :          8 :             S2N_ERR_INTERNAL_LIBCRYPTO_ERROR);
     640                 :            : #else
     641                 :            :     RESULT_BAIL(S2N_ERR_UNIMPLEMENTED);
     642                 :            : #endif
     643                 :            : 
     644                 :          8 :     return S2N_RESULT_OK;
     645                 :          8 : }
     646                 :            : 
     647                 :            : int s2n_disable_time_validation_ossl_verify_callback(int default_ossl_ret, X509_STORE_CTX *ctx)
     648                 :          0 : {
     649                 :          0 :     int err = X509_STORE_CTX_get_error(ctx);
     650                 :          0 :     switch (err) {
     651         [ #  # ]:          0 :         case X509_V_ERR_CERT_NOT_YET_VALID:
     652         [ #  # ]:          0 :         case X509_V_ERR_CERT_HAS_EXPIRED:
     653                 :          0 :             return OSSL_VERIFY_CALLBACK_IGNORE_ERROR;
     654         [ #  # ]:          0 :         default:
     655                 :          0 :             break;
     656                 :          0 :     }
     657                 :            : 
     658                 :            :     /* If CRL validation is enabled, setting the time validation verify callback will override the
     659                 :            :      * CRL verify callback. The CRL verify callback is manually triggered to work around this
     660                 :            :      * issue.
     661                 :            :      *
     662                 :            :      * The CRL verify callback ignores validation errors exclusively for CRL timestamp fields. So,
     663                 :            :      * if CRL validation isn't enabled, the CRL verify callback is a no-op.
     664                 :            :      */
     665                 :          0 :     return s2n_crl_ossl_verify_callback(default_ossl_ret, ctx);
     666                 :          0 : }
     667                 :            : 
     668                 :            : static S2N_RESULT s2n_x509_validator_disable_time_validation(struct s2n_connection *conn,
     669                 :            :         struct s2n_x509_validator *validator)
     670                 :          8 : {
     671 [ #  # ][ -  + ]:          8 :     RESULT_ENSURE_REF(conn);
     672 [ #  # ][ -  + ]:          8 :     RESULT_ENSURE_REF(conn->config);
     673 [ -  + ][ #  # ]:          8 :     RESULT_ENSURE_REF(validator);
     674 [ -  + ][ #  # ]:          8 :     RESULT_ENSURE_REF(validator->store_ctx);
     675                 :            : 
     676                 :            :     /* Setting an X509_STORE verify callback is not recommended with AWS-LC:
     677                 :            :      * https://github.com/aws/aws-lc/blob/aa90e509f2e940916fbe9fdd469a4c90c51824f6/include/openssl/x509.h#L2980-L2990
     678                 :            :      *
     679                 :            :      * If the libcrypto supports the ability to disable time validation with an X509_VERIFY_PARAM
     680                 :            :      * NO_CHECK_TIME flag, this method is preferred.
     681                 :            :      *
     682                 :            :      * However, older versions of AWS-LC and OpenSSL 1.0.2 do not support this flag. In this case,
     683                 :            :      * an X509_STORE verify callback is used. This is acceptable in older versions of AWS-LC
     684                 :            :      * because the versions are fixed, and updates to AWS-LC will not break the callback
     685                 :            :      * implementation.
     686                 :            :      */
     687         [ +  - ]:          8 :     if (s2n_libcrypto_supports_flag_no_check_time()) {
     688         [ -  + ]:          8 :         RESULT_GUARD(s2n_x509_validator_set_no_check_time_flag(validator));
     689                 :          8 :     } else {
     690                 :          0 :         X509_STORE_CTX_set_verify_cb(validator->store_ctx,
     691                 :          0 :                 s2n_disable_time_validation_ossl_verify_callback);
     692                 :          0 :     }
     693                 :            : 
     694                 :          8 :     return S2N_RESULT_OK;
     695                 :          8 : }
     696                 :            : 
     697                 :            : int s2n_no_op_verify_custom_crit_oids_cb(X509_STORE_CTX *ctx, X509 *x509, STACK_OF(ASN1_OBJECT) *oids)
     698                 :          0 : {
     699                 :          0 :     return 1;
     700                 :          0 : }
     701                 :            : 
     702                 :            : static S2N_RESULT s2n_x509_validator_add_custom_extensions(struct s2n_x509_validator *validator, struct s2n_connection *conn)
     703                 :        380 : {
     704 [ #  # ][ -  + ]:        380 :     RESULT_ENSURE_REF(validator);
     705 [ #  # ][ -  + ]:        380 :     RESULT_ENSURE_REF(validator->store_ctx);
     706 [ -  + ][ #  # ]:        380 :     RESULT_ENSURE_REF(conn);
     707 [ -  + ][ #  # ]:        380 :     RESULT_ENSURE_REF(conn->config);
     708                 :            : 
     709         [ -  + ]:        380 :     if (conn->config->custom_x509_extension_oids) {
     710                 :            : #if S2N_LIBCRYPTO_SUPPORTS_CUSTOM_OID
     711                 :            :         size_t custom_oid_count = sk_ASN1_OBJECT_num(conn->config->custom_x509_extension_oids);
     712                 :            :         for (size_t i = 0; i < custom_oid_count; i++) {
     713                 :            :             ASN1_OBJECT *critical_oid = sk_ASN1_OBJECT_value(conn->config->custom_x509_extension_oids, i);
     714                 :            :             RESULT_ENSURE_REF(critical_oid);
     715                 :            :             RESULT_GUARD_OSSL(X509_STORE_CTX_add_custom_crit_oid(validator->store_ctx, critical_oid),
     716                 :            :                     S2N_ERR_INTERNAL_LIBCRYPTO_ERROR);
     717                 :            :         }
     718                 :            :         /* To enable AWS-LC accepting custom extensions, an X509_STORE_CTX_verify_crit_oids_cb must be set.
     719                 :            :          * See https://github.com/aws/aws-lc/blob/f0b4afedd7d45fc2517643d890b654856c57f994/include/openssl/x509.h#L2913-L2918.
     720                 :            :          * 
     721                 :            :          * The `X509_STORE_CTX_verify_crit_oids_cb` callback can be used to implement the validation for the
     722                 :            :          * custom certificate extensions. However, s2n-tls consumers are expected to implement this validation
     723                 :            :          * in the `s2n_cert_validation_callback` instead. So, a no-op callback is provided to AWS-LC.
     724                 :            :          */
     725                 :            :         X509_STORE_CTX_set_verify_crit_oids(validator->store_ctx, s2n_no_op_verify_custom_crit_oids_cb);
     726                 :            : #else
     727         [ #  # ]:          0 :         RESULT_BAIL(S2N_ERR_UNIMPLEMENTED);
     728                 :          0 : #endif
     729                 :          0 :     }
     730                 :        380 :     return S2N_RESULT_OK;
     731                 :        380 : }
     732                 :            : 
     733                 :            : static S2N_RESULT s2n_x509_validator_verify_intent_for_cert(struct s2n_connection *conn, X509 *cert, bool is_leaf)
     734                 :        307 : {
     735 [ #  # ][ -  + ]:        307 :     RESULT_ENSURE_REF(cert);
     736                 :            : 
     737                 :            :     /* The X509_PURPOSE values indicate the purpose that certificates must specify. For servers,
     738                 :            :      * received client certificates MUST have a TLS client purpose. For clients, received server
     739                 :            :      * certificates MUST have a TLS server purpose.
     740                 :            :      */
     741                 :        307 :     int purpose = X509_PURPOSE_SSL_CLIENT;
     742         [ +  + ]:        307 :     if (conn->mode == S2N_CLIENT) {
     743                 :        222 :         purpose = X509_PURPOSE_SSL_SERVER;
     744                 :        222 :     }
     745                 :            : 
     746 [ +  - ][ +  + ]:        307 :     RESULT_GUARD_OSSL(X509_check_purpose(cert, purpose, !is_leaf), S2N_ERR_CERT_INTENT_INVALID);
     747                 :            : 
     748                 :        288 :     return S2N_RESULT_OK;
     749                 :        307 : }
     750                 :            : 
     751                 :            : S2N_RESULT s2n_x509_validator_verify_intent(struct s2n_x509_validator *validator, struct s2n_connection *conn)
     752                 :        354 : {
     753 [ -  + ][ #  # ]:        354 :     RESULT_ENSURE_REF(conn);
     754 [ -  + ][ #  # ]:        354 :     RESULT_ENSURE_REF(conn->config);
     755                 :            : 
     756         [ +  + ]:        354 :     if (conn->config->disable_x509_intent_verification) {
     757                 :         46 :         return S2N_RESULT_OK;
     758                 :         46 :     }
     759                 :            : 
     760                 :        308 :     DEFER_CLEANUP(struct s2n_validated_cert_chain validated_cert_chain = { 0 }, s2n_x509_validator_validated_cert_chain_free);
     761         [ -  + ]:        308 :     RESULT_GUARD(s2n_x509_validator_get_validated_cert_chain(validator, &validated_cert_chain));
     762                 :            : 
     763                 :        308 :     int cert_count = sk_X509_num(validated_cert_chain.stack);
     764 [ #  # ][ -  + ]:        308 :     RESULT_ENSURE_GT(cert_count, 0);
     765                 :            : 
     766                 :            :     /* The validated cert chain returned from the libcrypto includes the trust anchor. The trust
     767                 :            :      * anchor is omitted from intent verification since its TLS intent is implicitly indicated by
     768                 :            :      * its presence in the s2n-tls trust store.
     769                 :            :      */
     770                 :        308 :     cert_count -= 1;
     771                 :            : 
     772         [ +  + ]:        596 :     for (int i = 0; i < cert_count; i++) {
     773                 :        307 :         X509 *cert = sk_X509_value(validated_cert_chain.stack, i);
     774 [ -  + ][ #  # ]:        307 :         RESULT_ENSURE_REF(cert);
     775                 :            : 
     776                 :        307 :         bool is_leaf = (i == 0);
     777         [ +  + ]:        307 :         RESULT_GUARD(s2n_x509_validator_verify_intent_for_cert(conn, cert, is_leaf));
     778                 :        307 :     }
     779                 :            : 
     780                 :        289 :     return S2N_RESULT_OK;
     781                 :        308 : }
     782                 :            : 
     783                 :            : static S2N_RESULT s2n_x509_validator_verify_cert_chain(struct s2n_x509_validator *validator, struct s2n_connection *conn)
     784                 :        380 : {
     785 [ -  + ][ #  # ]:        380 :     RESULT_ENSURE(validator->state == READY_TO_VERIFY, S2N_ERR_INVALID_CERT_STATE);
     786                 :            : 
     787                 :        380 :     X509_VERIFY_PARAM *param = X509_STORE_CTX_get0_param(validator->store_ctx);
     788                 :        380 :     X509_VERIFY_PARAM_set_depth(param, validator->max_chain_depth);
     789                 :            : 
     790                 :        380 :     DEFER_CLEANUP(STACK_OF(X509_CRL) *crl_stack = NULL, sk_X509_CRL_free_pointer);
     791                 :            : 
     792         [ +  + ]:        380 :     if (conn->config->crl_lookup_cb) {
     793                 :         16 :         X509_STORE_CTX_set_verify_cb(validator->store_ctx, s2n_crl_ossl_verify_callback);
     794                 :            : 
     795                 :         16 :         crl_stack = sk_X509_CRL_new_null();
     796         [ -  + ]:         16 :         RESULT_GUARD(s2n_crl_get_crls_from_lookup_list(validator, crl_stack));
     797                 :            : 
     798                 :            :         /* Set the CRL list that the libcrypto will use to validate certificates with */
     799                 :         16 :         X509_STORE_CTX_set0_crls(validator->store_ctx, crl_stack);
     800                 :            : 
     801                 :            :         /* Enable CRL validation for certificates in X509_verify_cert */
     802 [ -  + ][ #  # ]:         16 :         RESULT_GUARD_OSSL(X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK),
     803                 :         16 :                 S2N_ERR_INTERNAL_LIBCRYPTO_ERROR);
     804                 :            : 
     805                 :            :         /* Enable CRL validation for all certificates, not just the leaf */
     806 [ -  + ][ #  # ]:         16 :         RESULT_GUARD_OSSL(X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK_ALL),
     807                 :         16 :                 S2N_ERR_INTERNAL_LIBCRYPTO_ERROR);
     808                 :         16 :     }
     809                 :            : 
     810                 :            :     /* Disabling time validation may set a NO_CHECK_TIME flag on the X509_STORE_CTX. Calling
     811                 :            :      * X509_STORE_CTX_set_time will override this flag. To prevent this, X509_STORE_CTX_set_time is
     812                 :            :      * only called if time validation is enabled.
     813                 :            :      */
     814         [ +  + ]:        380 :     if (conn->config->disable_x509_time_validation) {
     815         [ -  + ]:          8 :         RESULT_GUARD(s2n_x509_validator_disable_time_validation(conn, validator));
     816                 :        372 :     } else {
     817                 :        372 :         uint64_t current_sys_time = 0;
     818         [ -  + ]:        372 :         RESULT_GUARD(s2n_config_wall_clock(conn->config, &current_sys_time));
     819                 :        372 :         if (sizeof(time_t) == 4) {
     820                 :            :             /* cast value to uint64_t to prevent overflow errors */
     821 [ #  # ][ #  # ]:          0 :             RESULT_ENSURE_LTE(current_sys_time, (uint64_t) MAX_32_TIMESTAMP_NANOS);
     822                 :          0 :         }
     823                 :            : 
     824                 :            :         /* this wants seconds not nanoseconds */
     825                 :        372 :         time_t current_time = (time_t) (current_sys_time / ONE_SEC_IN_NANOS);
     826                 :        372 :         X509_STORE_CTX_set_time(validator->store_ctx, 0, current_time);
     827                 :        372 :     }
     828                 :            : 
     829                 :            :     /* It's assumed that if a valid certificate chain is received with an issuer that's present in
     830                 :            :      * the trust store, the certificate chain should be trusted. This should be the case even if
     831                 :            :      * the issuer in the trust store isn't a root certificate. Setting the PARTIAL_CHAIN flag
     832                 :            :      * allows the libcrypto to trust certificates in the trust store that aren't root certificates.
     833                 :            :      */
     834                 :        380 :     X509_STORE_CTX_set_flags(validator->store_ctx, X509_V_FLAG_PARTIAL_CHAIN);
     835                 :            : 
     836         [ -  + ]:        380 :     RESULT_GUARD(s2n_x509_validator_add_custom_extensions(validator, conn));
     837                 :            : 
     838                 :        380 :     int verify_ret = X509_verify_cert(validator->store_ctx);
     839         [ +  + ]:        380 :     if (verify_ret <= 0) {
     840                 :         26 :         int ossl_error = X509_STORE_CTX_get_error(validator->store_ctx);
     841                 :         26 :         switch (ossl_error) {
     842         [ +  + ]:          3 :             case X509_V_ERR_CERT_NOT_YET_VALID:
     843         [ +  - ]:          3 :                 RESULT_BAIL(S2N_ERR_CERT_NOT_YET_VALID);
     844         [ +  + ]:          5 :             case X509_V_ERR_CERT_HAS_EXPIRED:
     845         [ +  - ]:          5 :                 RESULT_BAIL(S2N_ERR_CERT_EXPIRED);
     846         [ +  + ]:          7 :             case X509_V_ERR_CERT_REVOKED:
     847         [ +  - ]:          7 :                 RESULT_BAIL(S2N_ERR_CERT_REVOKED);
     848         [ +  + ]:          1 :             case X509_V_ERR_UNABLE_TO_GET_CRL:
     849         [ -  + ]:          1 :             case X509_V_ERR_DIFFERENT_CRL_SCOPE:
     850         [ +  - ]:          1 :                 RESULT_BAIL(S2N_ERR_CRL_LOOKUP_FAILED);
     851         [ -  + ]:          0 :             case X509_V_ERR_CRL_SIGNATURE_FAILURE:
     852         [ #  # ]:          0 :                 RESULT_BAIL(S2N_ERR_CRL_SIGNATURE);
     853         [ -  + ]:          0 :             case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
     854         [ #  # ]:          0 :                 RESULT_BAIL(S2N_ERR_CRL_ISSUER);
     855         [ -  + ]:          0 :             case X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION:
     856         [ #  # ]:          0 :                 RESULT_BAIL(S2N_ERR_CRL_UNHANDLED_CRITICAL_EXTENSION);
     857         [ -  + ]:          0 :             case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
     858         [ #  # ]:          0 :                 RESULT_BAIL(S2N_ERR_CERT_UNHANDLED_CRITICAL_EXTENSION);
     859         [ +  + ]:         10 :             default:
     860         [ +  - ]:         10 :                 RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
     861                 :         26 :         }
     862                 :         26 :     }
     863                 :            : 
     864                 :        354 :     validator->state = VALIDATED;
     865                 :            : 
     866                 :        354 :     return S2N_RESULT_OK;
     867                 :        380 : }
     868                 :            : 
     869                 :            : static S2N_RESULT s2n_x509_validator_parse_leaf_certificate_extensions(struct s2n_connection *conn,
     870                 :            :         uint8_t *cert_chain_in, uint32_t cert_chain_len,
     871                 :            :         s2n_parsed_extensions_list *first_certificate_extensions)
     872                 :       2523 : {
     873                 :            :     /* certificate extensions is a field in TLS 1.3 - https://tools.ietf.org/html/rfc8446#section-4.4.2 */
     874 [ -  + ][ #  # ]:       2523 :     RESULT_ENSURE_GTE(conn->actual_protocol_version, S2N_TLS13);
     875                 :            : 
     876                 :       2523 :     struct s2n_blob cert_chain_blob = { 0 };
     877         [ -  + ]:       2523 :     RESULT_GUARD_POSIX(s2n_blob_init(&cert_chain_blob, cert_chain_in, cert_chain_len));
     878                 :       2523 :     DEFER_CLEANUP(struct s2n_stuffer cert_chain_in_stuffer = { 0 }, s2n_stuffer_free);
     879                 :            : 
     880         [ -  + ]:       2523 :     RESULT_GUARD_POSIX(s2n_stuffer_init(&cert_chain_in_stuffer, &cert_chain_blob));
     881         [ -  + ]:       2523 :     RESULT_GUARD_POSIX(s2n_stuffer_write(&cert_chain_in_stuffer, &cert_chain_blob));
     882                 :            : 
     883                 :       2523 :     struct s2n_blob asn1_cert = { 0 };
     884         [ -  + ]:       2523 :     RESULT_GUARD(s2n_x509_validator_read_asn1_cert(&cert_chain_in_stuffer, &asn1_cert));
     885                 :            : 
     886                 :       2523 :     s2n_parsed_extensions_list parsed_extensions_list = { 0 };
     887         [ -  + ]:       2523 :     RESULT_GUARD_POSIX(s2n_extension_list_parse(&cert_chain_in_stuffer, &parsed_extensions_list));
     888                 :       2523 :     *first_certificate_extensions = parsed_extensions_list;
     889                 :            : 
     890                 :       2523 :     return S2N_RESULT_OK;
     891                 :       2523 : }
     892                 :            : 
     893                 :            : S2N_RESULT s2n_x509_validator_validate_cert_chain_pre_cb(struct s2n_x509_validator *validator, struct s2n_connection *conn,
     894                 :            :         uint8_t *cert_chain_in, uint32_t cert_chain_len)
     895                 :       5016 : {
     896 [ -  + ][ #  # ]:       5016 :     RESULT_ENSURE_REF(conn);
     897 [ -  + ][ #  # ]:       5016 :     RESULT_ENSURE_REF(conn->config);
     898                 :            : 
     899                 :       5016 :     switch (validator->state) {
     900         [ +  + ]:       4996 :         case INIT:
     901                 :       4996 :             break;
     902         [ +  + ]:         20 :         case AWAITING_CRL_CALLBACK:
     903         [ +  + ]:         20 :             RESULT_GUARD(s2n_crl_handle_lookup_callback_result(validator));
     904                 :          1 :             break;
     905         [ -  + ]:          1 :         default:
     906         [ #  # ]:          0 :             RESULT_BAIL(S2N_ERR_INVALID_CERT_STATE);
     907                 :       5016 :     }
     908                 :            : 
     909         [ +  + ]:       4997 :     if (validator->state == INIT) {
     910         [ +  + ]:       4996 :         RESULT_GUARD(s2n_x509_validator_process_cert_chain(validator, conn, cert_chain_in, cert_chain_len));
     911                 :       4996 :     }
     912                 :            : 
     913         [ +  + ]:       4967 :     if (validator->state == READY_TO_VERIFY) {
     914         [ +  + ]:        380 :         RESULT_GUARD(s2n_x509_validator_verify_cert_chain(validator, conn));
     915         [ +  + ]:        354 :         RESULT_GUARD(s2n_x509_validator_verify_intent(validator, conn));
     916         [ +  + ]:        335 :         RESULT_GUARD(s2n_x509_validator_check_root_cert(validator, conn));
     917                 :        335 :     }
     918                 :            : 
     919         [ +  + ]:       4921 :     if (conn->actual_protocol_version >= S2N_TLS13) {
     920                 :            :         /* Only process certificate extensions received in the first certificate. Extensions received in all other
     921                 :            :          * certificates are ignored.
     922                 :            :          *
     923                 :            :          *= https://www.rfc-editor.org/rfc/rfc8446#section-4.4.2
     924                 :            :          *# If an extension applies to the entire chain, it SHOULD be included in
     925                 :            :          *# the first CertificateEntry.
     926                 :            :          */
     927                 :       2523 :         s2n_parsed_extensions_list first_certificate_extensions = { 0 };
     928         [ -  + ]:       2523 :         RESULT_GUARD(s2n_x509_validator_parse_leaf_certificate_extensions(conn, cert_chain_in, cert_chain_len, &first_certificate_extensions));
     929         [ -  + ]:       2523 :         RESULT_GUARD_POSIX(s2n_extension_list_process(S2N_EXTENSION_LIST_CERTIFICATE, conn, &first_certificate_extensions));
     930                 :       2523 :     }
     931                 :            : 
     932                 :       4921 :     return S2N_RESULT_OK;
     933                 :       4921 : }
     934                 :            : 
     935                 :            : static S2N_RESULT s2n_x509_validator_handle_cert_validation_callback_result(struct s2n_x509_validator *validator)
     936                 :         46 : {
     937 [ -  + ][ #  # ]:         46 :     RESULT_ENSURE_REF(validator);
     938                 :            : 
     939         [ +  + ]:         46 :     if (!validator->cert_validation_info.finished) {
     940         [ +  - ]:         27 :         RESULT_BAIL(S2N_ERR_ASYNC_BLOCKED);
     941                 :         27 :     }
     942                 :            : 
     943 [ +  + ][ +  - ]:         19 :     RESULT_ENSURE(validator->cert_validation_info.accepted, S2N_ERR_CERT_REJECTED);
     944                 :         10 :     return S2N_RESULT_OK;
     945                 :         19 : }
     946                 :            : 
     947                 :            : S2N_RESULT s2n_x509_validator_validate_cert_chain(struct s2n_x509_validator *validator, struct s2n_connection *conn,
     948                 :            :         uint8_t *cert_chain_in, uint32_t cert_chain_len, s2n_pkey_type *pkey_type, struct s2n_pkey *public_key_out)
     949                 :       5043 : {
     950 [ #  # ][ -  + ]:       5043 :     RESULT_ENSURE_REF(validator);
     951                 :            : 
     952         [ +  + ]:       5043 :     if (validator->cert_validation_cb_invoked) {
     953         [ +  + ]:         27 :         RESULT_GUARD(s2n_x509_validator_handle_cert_validation_callback_result(validator));
     954                 :       5016 :     } else {
     955         [ +  + ]:       5016 :         RESULT_GUARD(s2n_x509_validator_validate_cert_chain_pre_cb(validator, conn, cert_chain_in, cert_chain_len));
     956                 :            : 
     957         [ +  + ]:       4921 :         if (conn->config->cert_validation_cb) {
     958 [ +  + ][ +  - ]:         29 :             RESULT_ENSURE(conn->config->cert_validation_cb(conn, &(validator->cert_validation_info), conn->config->cert_validation_ctx) == S2N_SUCCESS,
     959                 :         19 :                     S2N_ERR_CANCELLED);
     960                 :         19 :             validator->cert_validation_cb_invoked = true;
     961         [ +  + ]:         19 :             RESULT_GUARD(s2n_x509_validator_handle_cert_validation_callback_result(validator));
     962                 :         19 :         }
     963                 :       4921 :     }
     964                 :            : 
     965                 :            :     /* retrieve information from leaf cert */
     966 [ #  # ][ -  + ]:       4902 :     RESULT_ENSURE_GT(sk_X509_num(validator->cert_chain_from_wire), 0);
     967                 :       4902 :     X509 *leaf_cert = sk_X509_value(validator->cert_chain_from_wire, 0);
     968 [ #  # ][ -  + ]:       4902 :     RESULT_ENSURE_REF(leaf_cert);
     969                 :       4902 :     DEFER_CLEANUP(struct s2n_pkey public_key = { 0 }, s2n_pkey_free);
     970         [ -  + ]:       4902 :     RESULT_GUARD(s2n_pkey_from_x509(leaf_cert, &public_key, pkey_type));
     971                 :            : 
     972                 :       4902 :     *public_key_out = public_key;
     973                 :            : 
     974                 :            :     /* Reset the old struct, so we don't clean up public_key_out */
     975                 :       4902 :     ZERO_TO_DISABLE_DEFER_CLEANUP(public_key);
     976                 :            : 
     977                 :       4902 :     return S2N_RESULT_OK;
     978                 :       4902 : }
     979                 :            : 
     980                 :            : S2N_RESULT s2n_x509_validator_validate_cert_stapled_ocsp_response(struct s2n_x509_validator *validator,
     981                 :            :         struct s2n_connection *conn, const uint8_t *ocsp_response_raw, uint32_t ocsp_response_length)
     982                 :         36 : {
     983 [ +  + ][ +  + ]:         36 :     if (validator->skip_cert_validation || !validator->check_stapled_ocsp) {
     984                 :          8 :         validator->state = OCSP_VALIDATED;
     985                 :          8 :         return S2N_RESULT_OK;
     986                 :          8 :     }
     987                 :            : 
     988 [ -  + ][ #  # ]:         28 :     RESULT_ENSURE(validator->state == VALIDATED, S2N_ERR_INVALID_CERT_STATE);
     989                 :            : 
     990                 :            : #if !S2N_OCSP_STAPLING_SUPPORTED
     991                 :            :     /* Default to safety */
     992                 :            :     RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
     993                 :            : #else
     994                 :            : 
     995 [ -  + ][ #  # ]:         28 :     RESULT_ENSURE_REF(ocsp_response_raw);
     996                 :            : 
     997                 :         28 :     DEFER_CLEANUP(OCSP_RESPONSE *ocsp_response = d2i_OCSP_RESPONSE(NULL, &ocsp_response_raw, ocsp_response_length),
     998                 :         28 :             OCSP_RESPONSE_free_pointer);
     999 [ +  - ][ +  + ]:         28 :     RESULT_ENSURE(ocsp_response != NULL, S2N_ERR_INVALID_OCSP_RESPONSE);
    1000                 :            : 
    1001                 :         27 :     int ocsp_status = OCSP_response_status(ocsp_response);
    1002 [ #  # ][ -  + ]:         27 :     RESULT_ENSURE(ocsp_status == OCSP_RESPONSE_STATUS_SUCCESSFUL, S2N_ERR_CERT_UNTRUSTED);
    1003                 :            : 
    1004                 :         27 :     DEFER_CLEANUP(OCSP_BASICRESP *basic_response = OCSP_response_get1_basic(ocsp_response), OCSP_BASICRESP_free_pointer);
    1005 [ -  + ][ #  # ]:         27 :     RESULT_ENSURE(basic_response != NULL, S2N_ERR_INVALID_OCSP_RESPONSE);
    1006                 :            : 
    1007                 :         27 :     DEFER_CLEANUP(struct s2n_validated_cert_chain validated_cert_chain = { 0 }, s2n_x509_validator_validated_cert_chain_free);
    1008         [ -  + ]:         27 :     RESULT_GUARD(s2n_x509_validator_get_validated_cert_chain(validator, &validated_cert_chain));
    1009                 :         27 :     STACK_OF(X509) *cert_chain = validated_cert_chain.stack;
    1010 [ -  + ][ #  # ]:         27 :     RESULT_ENSURE_REF(cert_chain);
    1011                 :            : 
    1012                 :         27 :     const int certs_in_chain = sk_X509_num(cert_chain);
    1013 [ -  + ][ #  # ]:         27 :     RESULT_ENSURE(certs_in_chain > 0, S2N_ERR_NO_CERT_FOUND);
    1014                 :            : 
    1015                 :            :     /* leaf is the top: not the bottom. */
    1016                 :         27 :     X509 *subject = sk_X509_value(cert_chain, 0);
    1017                 :         27 :     X509 *issuer = NULL;
    1018                 :            :     /* find the issuer in the chain. If it's not there. Fail everything. */
    1019         [ +  + ]:         54 :     for (int i = 0; i < certs_in_chain; ++i) {
    1020                 :         53 :         X509 *issuer_candidate = sk_X509_value(cert_chain, i);
    1021                 :         53 :         const int issuer_value = X509_check_issued(issuer_candidate, subject);
    1022                 :            : 
    1023         [ +  + ]:         53 :         if (issuer_value == X509_V_OK) {
    1024                 :         26 :             issuer = issuer_candidate;
    1025                 :         26 :             break;
    1026                 :         26 :         }
    1027                 :         53 :     }
    1028 [ +  + ][ +  - ]:         27 :     RESULT_ENSURE(issuer != NULL, S2N_ERR_CERT_UNTRUSTED);
    1029                 :            : 
    1030                 :            :     /* Important: this checks that the stapled ocsp response CAN be verified, not that it has been verified. */
    1031                 :         26 :     const int ocsp_verify_res = OCSP_basic_verify(basic_response, cert_chain, validator->trust_store->trust_store, 0);
    1032 [ +  - ][ +  + ]:         26 :     RESULT_GUARD_OSSL(ocsp_verify_res, S2N_ERR_CERT_UNTRUSTED);
    1033                 :            : 
    1034                 :            :     /* do the crypto checks on the response.*/
    1035                 :         24 :     int status = 0;
    1036                 :         24 :     int reason = 0;
    1037                 :            : 
    1038                 :            :     /* SHA-1 is the only supported hash algorithm for the CertID due to its established use in 
    1039                 :            :      * OCSP responders. 
    1040                 :            :      */
    1041                 :         24 :     OCSP_CERTID *cert_id = OCSP_cert_to_id(EVP_sha1(), subject, issuer);
    1042 [ #  # ][ -  + ]:         24 :     RESULT_ENSURE_REF(cert_id);
    1043                 :            : 
    1044                 :            :     /**
    1045                 :            :      *= https://www.rfc-editor.org/rfc/rfc6960#section-2.4
    1046                 :            :      *#
    1047                 :            :      *# thisUpdate      The most recent time at which the status being
    1048                 :            :      *#                 indicated is known by the responder to have been
    1049                 :            :      *#                 correct.
    1050                 :            :      *#
    1051                 :            :      *# nextUpdate      The time at or before which newer information will be
    1052                 :            :      *#                 available about the status of the certificate.
    1053                 :            :      **/
    1054                 :         24 :     ASN1_GENERALIZEDTIME *revtime = NULL, *thisupd = NULL, *nextupd = NULL;
    1055                 :            :     /* Actual verification of the response */
    1056                 :         24 :     const int ocsp_resp_find_status_res = OCSP_resp_find_status(basic_response, cert_id, &status, &reason, &revtime, &thisupd, &nextupd);
    1057                 :         24 :     OCSP_CERTID_free(cert_id);
    1058 [ +  + ][ +  - ]:         24 :     RESULT_GUARD_OSSL(ocsp_resp_find_status_res, S2N_ERR_CERT_UNTRUSTED);
    1059                 :            : 
    1060                 :         23 :     uint64_t current_sys_time_nanoseconds = 0;
    1061         [ -  + ]:         23 :     RESULT_GUARD(s2n_config_wall_clock(conn->config, &current_sys_time_nanoseconds));
    1062                 :         23 :     if (sizeof(time_t) == 4) {
    1063                 :            :         /* cast value to uint64_t to prevent overflow errors */
    1064 [ #  # ][ #  # ]:          0 :         RESULT_ENSURE_LTE(current_sys_time_nanoseconds, (uint64_t) MAX_32_TIMESTAMP_NANOS);
    1065                 :          0 :     }
    1066                 :            :     /* convert the current_sys_time (which is in nanoseconds) to seconds */
    1067                 :         23 :     time_t current_sys_time_seconds = (time_t) (current_sys_time_nanoseconds / ONE_SEC_IN_NANOS);
    1068                 :            : 
    1069                 :         23 :     DEFER_CLEANUP(ASN1_GENERALIZEDTIME *current_sys_time = ASN1_GENERALIZEDTIME_set(NULL, current_sys_time_seconds), s2n_openssl_asn1_time_free_pointer);
    1070 [ -  + ][ #  # ]:         23 :     RESULT_ENSURE_REF(current_sys_time);
    1071                 :            : 
    1072                 :            :     /**
    1073                 :            :      * It is fine to use ASN1_TIME functions with ASN1_GENERALIZEDTIME structures
    1074                 :            :      * From openssl documentation:
    1075                 :            :      * It is recommended that functions starting with ASN1_TIME be used instead
    1076                 :            :      * of those starting with ASN1_UTCTIME or ASN1_GENERALIZEDTIME. The
    1077                 :            :      * functions starting with ASN1_UTCTIME and ASN1_GENERALIZEDTIME act only on
    1078                 :            :      * that specific time format. The functions starting with ASN1_TIME will
    1079                 :            :      * operate on either format.
    1080                 :            :      * https://www.openssl.org/docs/man1.1.1/man3/ASN1_TIME_to_generalizedtime.html
    1081                 :            :      *
    1082                 :            :      * ASN1_TIME_compare has a much nicer API, but is not available in Openssl
    1083                 :            :      * 1.0.1, so we use ASN1_TIME_diff.
    1084                 :            :      */
    1085                 :         23 :     int pday = 0;
    1086                 :         23 :     int psec = 0;
    1087 [ #  # ][ -  + ]:         23 :     RESULT_GUARD_OSSL(ASN1_TIME_diff(&pday, &psec, thisupd, current_sys_time), S2N_ERR_CERT_UNTRUSTED);
    1088                 :            :     /* ensure that current_time is after or the same as "this update" */
    1089 [ +  - ][ +  + ]:         23 :     RESULT_ENSURE(pday >= 0 && psec >= 0, S2N_ERR_CERT_INVALID);
                 [ +  + ]
    1090                 :            : 
    1091                 :            :     /* ensure that current_time is before or the same as "next update" */
    1092         [ +  + ]:         20 :     if (nextupd) {
    1093 [ -  + ][ #  # ]:         19 :         RESULT_GUARD_OSSL(ASN1_TIME_diff(&pday, &psec, current_sys_time, nextupd), S2N_ERR_CERT_UNTRUSTED);
    1094 [ +  - ][ +  + ]:         19 :         RESULT_ENSURE(pday >= 0 && psec >= 0, S2N_ERR_CERT_EXPIRED);
                 [ +  + ]
    1095                 :         19 :     } else {
    1096                 :            :         /**
    1097                 :            :          * if nextupd isn't present, assume that nextupd is
    1098                 :            :          * DEFAULT_OCSP_NEXT_UPDATE_PERIOD after thisupd. This means that if the
    1099                 :            :          * current time is more than DEFAULT_OCSP_NEXT_UPDATE_PERIOD
    1100                 :            :          * seconds ahead of thisupd, we consider it invalid. We already compared
    1101                 :            :          * current_sys_time to thisupd, so reuse those values
    1102                 :            :          */
    1103                 :          1 :         uint64_t seconds_after_thisupd = pday * (3600 * 24) + psec;
    1104 [ #  # ][ -  + ]:          1 :         RESULT_ENSURE(seconds_after_thisupd < DEFAULT_OCSP_NEXT_UPDATE_PERIOD, S2N_ERR_CERT_EXPIRED);
    1105                 :          1 :     }
    1106                 :            : 
    1107                 :         16 :     switch (status) {
    1108         [ +  + ]:         15 :         case V_OCSP_CERTSTATUS_GOOD:
    1109                 :         15 :             validator->state = OCSP_VALIDATED;
    1110                 :         15 :             return S2N_RESULT_OK;
    1111         [ +  + ]:          1 :         case V_OCSP_CERTSTATUS_REVOKED:
    1112         [ +  - ]:          1 :             RESULT_BAIL(S2N_ERR_CERT_REVOKED);
    1113         [ -  + ]:          0 :         default:
    1114         [ #  # ]:          0 :             RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
    1115                 :         16 :     }
    1116                 :         16 : #endif /* S2N_OCSP_STAPLING_SUPPORTED */
    1117                 :         16 : }
    1118                 :            : 
    1119                 :            : bool s2n_x509_validator_is_cert_chain_validated(const struct s2n_x509_validator *validator)
    1120                 :        739 : {
    1121 [ +  - ][ +  + ]:        739 :     return validator && (validator->state == VALIDATED || validator->state == OCSP_VALIDATED);
                 [ +  + ]
    1122                 :        739 : }
    1123                 :            : 
    1124                 :            : int s2n_cert_validation_accept(struct s2n_cert_validation_info *info)
    1125                 :         38 : {
    1126 [ +  - ][ +  + ]:         38 :     POSIX_ENSURE_REF(info);
    1127 [ +  - ][ +  + ]:         37 :     POSIX_ENSURE(!info->finished, S2N_ERR_INVALID_STATE);
    1128                 :            : 
    1129                 :         17 :     info->finished = true;
    1130                 :         17 :     info->accepted = true;
    1131                 :            : 
    1132                 :         17 :     return S2N_SUCCESS;
    1133                 :         37 : }
    1134                 :            : 
    1135                 :            : int s2n_cert_validation_reject(struct s2n_cert_validation_info *info)
    1136                 :         37 : {
    1137 [ +  + ][ +  - ]:         37 :     POSIX_ENSURE_REF(info);
    1138 [ +  + ][ +  - ]:         36 :     POSIX_ENSURE(!info->finished, S2N_ERR_INVALID_STATE);
    1139                 :            : 
    1140                 :         16 :     info->finished = true;
    1141                 :         16 :     info->accepted = false;
    1142                 :            : 
    1143                 :         16 :     return S2N_SUCCESS;
    1144                 :         36 : }

Generated by: LCOV version 1.14