LCOV - code coverage report
Current view: top level - tls/extensions - s2n_server_cookie.c (source / functions) Hit Total Coverage
Test: unit_test_coverage.info Lines: 13 13 100.0 %
Date: 2025-08-14 07:26:07 Functions: 2 2 100.0 %
Branches: 12 24 50.0 %

           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_cookie.h"
      17                 :            : #include "tls/s2n_tls.h"
      18                 :            : #include "utils/s2n_random.h"
      19                 :            : 
      20                 :            : /*
      21                 :            :  *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.2
      22                 :            :  *# Cookies serve two primary purposes:
      23                 :            :  *#
      24                 :            :  *#    -  Allowing the server to force the client to demonstrate
      25                 :            :  *#       reachability at their apparent network address (thus providing a
      26                 :            :  *#       measure of DoS protection).  This is primarily useful for
      27                 :            :  *#       non-connection-oriented transports (see [RFC6347] for an example
      28                 :            :  *#       of this).
      29                 :            :  *#
      30                 :            :  *#    -  Allowing the server to offload state to the client, thus allowing
      31                 :            :  *#       it to send a HelloRetryRequest without storing any state.  The
      32                 :            :  *#       server can do this by storing the hash of the ClientHello in the
      33                 :            :  *#       HelloRetryRequest cookie (protected with some suitable integrity
      34                 :            :  *#       protection algorithm).
      35                 :            :  *#
      36                 :            :  *# When sending a HelloRetryRequest, the server MAY provide a "cookie"
      37                 :            :  *# extension to the client (this is an exception to the usual rule that
      38                 :            :  *# the only extensions that may be sent are those that appear in the
      39                 :            :  *# ClientHello).
      40                 :            :  *
      41                 :            :  * So our server does not send cookies in production,
      42                 :            :  * because it doesn't support DTLS and isn't stateless.
      43                 :            :  *
      44                 :            :  * However, we will sometimes send cookies for testing.
      45                 :            :  */
      46                 :            : static bool s2n_server_cookie_should_send(struct s2n_connection *conn)
      47                 :        654 : {
      48 [ +  - ][ +  + ]:        654 :     return conn && conn->cookie.size > 0 && s2n_in_unit_test();
                 [ +  - ]
      49                 :        654 : }
      50                 :            : 
      51                 :            : /*
      52                 :            :  *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.2
      53                 :            :  *# When sending the new ClientHello, the client MUST copy
      54                 :            :  *# the contents of the extension received in the HelloRetryRequest into
      55                 :            :  *# a "cookie" extension in the new ClientHello.
      56                 :            :  *
      57                 :            :  * Store the server's cookie for later use in the new ClientHello.
      58                 :            :  */
      59                 :            : static int s2n_server_cookie_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
      60                 :         48 : {
      61 [ -  + ][ #  # ]:         48 :     POSIX_ENSURE_REF(conn);
      62                 :            : 
      63                 :            :     /* This extension should only appear on the hello retry request extension list,
      64                 :            :      * but verify the retry anyway.
      65                 :            :      */
      66 [ +  + ][ +  - ]:         48 :     POSIX_ENSURE(s2n_is_hello_retry_handshake(conn), S2N_ERR_UNSUPPORTED_EXTENSION);
      67                 :            : 
      68                 :         43 :     uint16_t size = 0;
      69         [ -  + ]:         43 :     POSIX_GUARD(s2n_stuffer_read_uint16(extension, &size));
      70 [ -  + ][ #  # ]:         43 :     POSIX_ENSURE(s2n_stuffer_data_available(extension) >= size, S2N_ERR_BAD_MESSAGE);
      71                 :            : 
      72         [ -  + ]:         43 :     POSIX_GUARD(s2n_realloc(&conn->cookie, size));
      73         [ -  + ]:         43 :     POSIX_GUARD(s2n_stuffer_read(extension, &conn->cookie));
      74                 :         43 :     return S2N_SUCCESS;
      75                 :         43 : }
      76                 :            : 
      77                 :            : const s2n_extension_type s2n_server_cookie_extension = {
      78                 :            :     .iana_value = TLS_EXTENSION_COOKIE,
      79                 :            :     .minimum_version = S2N_TLS13,
      80                 :            :     .is_response = false,
      81                 :            :     .send = s2n_cookie_send,
      82                 :            :     .recv = s2n_server_cookie_recv,
      83                 :            :     .should_send = s2n_server_cookie_should_send,
      84                 :            :     .if_missing = s2n_extension_noop_if_missing,
      85                 :            : };

Generated by: LCOV version 1.14