LCOV - code coverage report
Current view: top level - tls - s2n_server_cert.c (source / functions) Hit Total Coverage
Test: unit_test_coverage.info Lines: 35 35 100.0 %
Date: 2025-08-15 07:28:39 Functions: 2 2 100.0 %
Branches: 21 38 55.3 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License").
       5                 :            :  * You may not use this file except in compliance with the License.
       6                 :            :  * A copy of the License is located at
       7                 :            :  *
       8                 :            :  *  http://aws.amazon.com/apache2.0
       9                 :            :  *
      10                 :            :  * or in the "license" file accompanying this file. This file is distributed
      11                 :            :  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
      12                 :            :  * express or implied. See the License for the specific language governing
      13                 :            :  * permissions and limitations under the License.
      14                 :            :  */
      15                 :            : 
      16                 :            : #include "api/s2n.h"
      17                 :            : #include "error/s2n_errno.h"
      18                 :            : #include "tls/s2n_auth_selection.h"
      19                 :            : #include "tls/s2n_cipher_suites.h"
      20                 :            : #include "tls/s2n_tls.h"
      21                 :            : #include "utils/s2n_safety.h"
      22                 :            : 
      23                 :            : int s2n_server_cert_recv(struct s2n_connection *conn)
      24                 :       4353 : {
      25                 :            :     /* s2n_server_cert_recv() may be re-entered due to handling an async callback.
      26                 :            :      * We operate on a copy of `handshake.io` to ensure the stuffer is initilized properly on the re-entry case.
      27                 :            :      */
      28                 :       4353 :     struct s2n_stuffer in = conn->handshake.io;
      29                 :            : 
      30         [ +  + ]:       4353 :     if (conn->actual_protocol_version == S2N_TLS13) {
      31                 :       2080 :         uint8_t certificate_request_context_len = 0;
      32         [ -  + ]:       2080 :         POSIX_GUARD(s2n_stuffer_read_uint8(&in, &certificate_request_context_len));
      33 [ -  + ][ #  # ]:       2080 :         S2N_ERROR_IF(certificate_request_context_len != 0, S2N_ERR_BAD_MESSAGE);
      34                 :       2080 :     }
      35                 :            : 
      36                 :       4353 :     uint32_t size_of_all_certificates = 0;
      37         [ -  + ]:       4353 :     POSIX_GUARD(s2n_stuffer_read_uint24(&in, &size_of_all_certificates));
      38                 :            : 
      39 [ +  + ][ +  + ]:       4353 :     S2N_ERROR_IF(size_of_all_certificates > s2n_stuffer_data_available(&in) || size_of_all_certificates < 3,
                 [ +  - ]
      40                 :       4353 :             S2N_ERR_BAD_MESSAGE);
      41                 :            : 
      42                 :       4348 :     s2n_cert_public_key public_key;
      43         [ -  + ]:       4348 :     POSIX_GUARD(s2n_pkey_zero_init(&public_key));
      44                 :            : 
      45                 :       4348 :     s2n_pkey_type actual_cert_pkey_type;
      46                 :       4348 :     struct s2n_blob cert_chain = { 0 };
      47                 :       4348 :     cert_chain.size = size_of_all_certificates;
      48                 :       4348 :     cert_chain.data = s2n_stuffer_raw_read(&in, size_of_all_certificates);
      49 [ -  + ][ #  # ]:       4348 :     POSIX_ENSURE_REF(cert_chain.data);
      50                 :            : 
      51         [ +  + ]:       4348 :     POSIX_GUARD_RESULT(s2n_x509_validator_validate_cert_chain(&conn->x509_validator, conn, cert_chain.data,
      52                 :       4316 :             cert_chain.size, &actual_cert_pkey_type, &public_key));
      53                 :            : 
      54         [ -  + ]:       4316 :     POSIX_GUARD(s2n_is_cert_type_valid_for_auth(conn, actual_cert_pkey_type));
      55         [ -  + ]:       4316 :     POSIX_GUARD_RESULT(s2n_pkey_setup_for_type(&public_key, actual_cert_pkey_type));
      56                 :       4316 :     conn->handshake_params.server_public_key = public_key;
      57                 :            : 
      58                 :            :     /* Update handshake.io to reflect the true stuffer state after all async callbacks are handled. */
      59                 :       4316 :     conn->handshake.io = in;
      60                 :            : 
      61                 :       4316 :     return 0;
      62                 :       4316 : }
      63                 :            : 
      64                 :            : int s2n_server_cert_send(struct s2n_connection *conn)
      65                 :       5162 : {
      66 [ -  + ][ #  # ]:       5162 :     S2N_ERROR_IF(conn->handshake_params.our_chain_and_key == NULL, S2N_ERR_CERT_TYPE_UNSUPPORTED);
      67         [ +  + ]:       5162 :     if (conn->actual_protocol_version == S2N_TLS13) {
      68                 :            :         /* server's certificate request context should always be of zero length */
      69                 :            :         /* https://tools.ietf.org/html/rfc8446#section-4.4.2 */
      70                 :       2856 :         uint8_t certificate_request_context_len = 0;
      71         [ -  + ]:       2856 :         POSIX_GUARD(s2n_stuffer_write_uint8(&conn->handshake.io, certificate_request_context_len));
      72                 :       2856 :     }
      73                 :            : 
      74         [ -  + ]:       5162 :     POSIX_GUARD(s2n_send_cert_chain(conn, &conn->handshake.io, conn->handshake_params.our_chain_and_key));
      75                 :            : 
      76                 :       5162 :     return 0;
      77                 :       5162 : }

Generated by: LCOV version 1.14