LCOV - code coverage report
Current view: top level - tls/extensions - s2n_cert_status.c (source / functions) Hit Total Coverage
Test: unit_test_coverage.info Lines: 32 32 100.0 %
Date: 2025-08-14 07:26:07 Functions: 3 3 100.0 %
Branches: 21 40 52.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License").
       5                 :            :  * You may not use this file except in compliance with the License.
       6                 :            :  * A copy of the License is located at
       7                 :            :  *
       8                 :            :  *  http://aws.amazon.com/apache2.0
       9                 :            :  *
      10                 :            :  * or in the "license" file accompanying this file. This file is distributed
      11                 :            :  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
      12                 :            :  * express or implied. See the License for the specific language governing
      13                 :            :  * permissions and limitations under the License.
      14                 :            :  */
      15                 :            : 
      16                 :            : #include "tls/extensions/s2n_cert_status.h"
      17                 :            : 
      18                 :            : #include "tls/s2n_config.h"
      19                 :            : #include "tls/s2n_connection.h"
      20                 :            : #include "tls/s2n_tls.h"
      21                 :            : #include "tls/s2n_x509_validator.h"
      22                 :            : #include "utils/s2n_safety.h"
      23                 :            : 
      24                 :            : #define U24_SIZE 3
      25                 :            : 
      26                 :            : static bool s2n_cert_status_should_send(struct s2n_connection *conn);
      27                 :            : 
      28                 :            : /*
      29                 :            :  * The cert_status extension is sent in response to OCSP status requests in TLS 1.3. The
      30                 :            :  * OCSP response is contained in the extension data. In TLS 1.2, the cert_status_response
      31                 :            :  * extension is sent instead, indicating that the OCSP response will be sent in a
      32                 :            :  * Certificate Status handshake message.
      33                 :            :  */
      34                 :            : const s2n_extension_type s2n_cert_status_extension = {
      35                 :            :     .iana_value = TLS_EXTENSION_STATUS_REQUEST,
      36                 :            :     .is_response = true,
      37                 :            :     .send = s2n_cert_status_send,
      38                 :            :     .recv = s2n_cert_status_recv,
      39                 :            :     .should_send = s2n_cert_status_should_send,
      40                 :            :     .if_missing = s2n_extension_noop_if_missing,
      41                 :            : };
      42                 :            : 
      43                 :            : static bool s2n_cert_status_should_send(struct s2n_connection *conn)
      44                 :         52 : {
      45         [ +  + ]:         52 :     return conn->handshake_params.our_chain_and_key
      46         [ +  + ]:         52 :             && conn->handshake_params.our_chain_and_key->ocsp_status.size > 0;
      47                 :         52 : }
      48                 :            : 
      49                 :            : int s2n_cert_status_send(struct s2n_connection *conn, struct s2n_stuffer *out)
      50                 :         21 : {
      51 [ -  + ][ #  # ]:         21 :     POSIX_ENSURE_REF(conn);
      52                 :         21 :     struct s2n_blob *ocsp_status = &conn->handshake_params.our_chain_and_key->ocsp_status;
      53 [ -  + ][ #  # ]:         21 :     POSIX_ENSURE_REF(ocsp_status);
      54                 :            : 
      55         [ -  + ]:         21 :     POSIX_GUARD(s2n_stuffer_write_uint8(out, (uint8_t) S2N_STATUS_REQUEST_OCSP));
      56         [ -  + ]:         21 :     POSIX_GUARD(s2n_stuffer_write_uint24(out, ocsp_status->size));
      57         [ -  + ]:         21 :     POSIX_GUARD(s2n_stuffer_write(out, ocsp_status));
      58                 :            : 
      59                 :         21 :     return S2N_SUCCESS;
      60                 :         21 : }
      61                 :            : 
      62                 :            : int s2n_cert_status_recv(struct s2n_connection *conn, struct s2n_stuffer *in)
      63                 :         18 : {
      64 [ #  # ][ -  + ]:         18 :     POSIX_ENSURE_REF(conn);
      65                 :            :     /**
      66                 :            :      *= https://www.rfc-editor.org/rfc/rfc6066#section-8
      67                 :            :      *#   struct {
      68                 :            :      *#       CertificateStatusType status_type;
      69                 :            :      *#       select (status_type) {
      70                 :            :      *#          case ocsp: OCSPResponse;
      71                 :            :      *#       } response;
      72                 :            :      *#   } CertificateStatus;
      73                 :            :      *#
      74                 :            :      *#   opaque OCSPResponse<1..2^24-1>;
      75                 :            :      *#
      76                 :            :      *# An "ocsp_response" contains a complete, DER-encoded OCSP response
      77                 :            :      *# (using the ASN.1 type OCSPResponse defined in [RFC2560]).  Only one
      78                 :            :      *# OCSP response may be sent.
      79                 :            :      **/
      80                 :         18 :     uint8_t type = 0;
      81         [ -  + ]:         18 :     POSIX_GUARD(s2n_stuffer_read_uint8(in, &type));
      82         [ +  + ]:         18 :     if (type != S2N_STATUS_REQUEST_OCSP) {
      83                 :            :         /* We only support OCSP */
      84                 :          1 :         return S2N_SUCCESS;
      85                 :          1 :     }
      86                 :            : 
      87                 :            :     /* The status_type variable is only used when a client requests OCSP stapling from a
      88                 :            :      * server. A server can request OCSP stapling from a client, but it is not tracked
      89                 :            :      * with this variable.
      90                 :            :      */
      91         [ +  + ]:         17 :     if (conn->mode == S2N_CLIENT) {
      92                 :         11 :         conn->status_type = S2N_STATUS_REQUEST_OCSP;
      93                 :         11 :     }
      94                 :            : 
      95                 :         17 :     uint32_t status_size = 0;
      96         [ -  + ]:         17 :     POSIX_GUARD(s2n_stuffer_read_uint24(in, &status_size));
      97 [ -  + ][ #  # ]:         17 :     POSIX_ENSURE_LTE(status_size, s2n_stuffer_data_available(in));
      98                 :            : 
      99         [ -  + ]:         17 :     POSIX_GUARD(s2n_realloc(&conn->status_response, status_size));
     100         [ -  + ]:         17 :     POSIX_GUARD(s2n_stuffer_read_bytes(in, conn->status_response.data, status_size));
     101                 :            : 
     102         [ +  + ]:         17 :     POSIX_GUARD_RESULT(s2n_x509_validator_validate_cert_stapled_ocsp_response(&conn->x509_validator, conn,
     103                 :         16 :             conn->status_response.data, conn->status_response.size));
     104                 :            : 
     105                 :         16 :     return S2N_SUCCESS;
     106                 :         17 : }

Generated by: LCOV version 1.14