LCOV - code coverage report
Current view: top level - tls - s2n_record_read_cbc.c (source / functions) Hit Total Coverage
Test: unit_test_coverage.info Lines: 53 55 96.4 %
Date: 2026-08-22 07:27:53 Functions: 1 1 100.0 %
Branches: 38 86 44.2 %

           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 "crypto/s2n_cipher.h"
      17                 :            : #include "crypto/s2n_hmac.h"
      18                 :            : #include "crypto/s2n_sequence.h"
      19                 :            : #include "error/s2n_errno.h"
      20                 :            : #include "stuffer/s2n_stuffer.h"
      21                 :            : #include "tls/s2n_cipher_suites.h"
      22                 :            : #include "tls/s2n_connection.h"
      23                 :            : #include "tls/s2n_crypto.h"
      24                 :            : #include "tls/s2n_record.h"
      25                 :            : #include "tls/s2n_record_read.h"
      26                 :            : #include "utils/s2n_blob.h"
      27                 :            : #include "utils/s2n_safety.h"
      28                 :            : 
      29                 :            : int s2n_record_parse_cbc(
      30                 :            :         const struct s2n_cipher_suite *cipher_suite,
      31                 :            :         struct s2n_connection *conn,
      32                 :            :         struct s2n_record_header *header,
      33                 :            :         uint8_t *implicit_iv,
      34                 :            :         struct s2n_hmac_state *mac,
      35                 :            :         uint8_t *sequence_number,
      36                 :            :         struct s2n_session_key *session_key)
      37                 :      41812 : {
      38                 :      41812 :     struct s2n_blob iv = { .data = implicit_iv, .size = cipher_suite->record_alg->cipher->io.cbc.record_iv_size };
      39                 :      41812 :     uint8_t ivpad[S2N_TLS_MAX_IV_LEN] = { 0 };
      40                 :      41812 :     uint16_t encrypted_length = header->length;
      41                 :            : 
      42 [ -  + ][ #  # ]:      41812 :     POSIX_ENSURE_LTE(cipher_suite->record_alg->cipher->io.cbc.record_iv_size, S2N_TLS_MAX_IV_LEN);
      43                 :            : 
      44                 :            :     /* For TLS >= 1.1 the IV is in the packet */
      45         [ +  + ]:      41812 :     if (conn->actual_protocol_version > S2N_TLS10) {
      46         [ -  + ]:      32916 :         POSIX_GUARD(s2n_stuffer_read(&conn->in, &iv));
      47 [ #  # ][ -  + ]:      32916 :         POSIX_ENSURE_GTE(encrypted_length, iv.size);
      48                 :      32916 :         encrypted_length -= iv.size;
      49                 :      32916 :     }
      50                 :            : 
      51                 :      41812 :     struct s2n_blob en = { .size = encrypted_length, .data = s2n_stuffer_raw_read(&conn->in, encrypted_length) };
      52 [ -  + ][ #  # ]:      41812 :     POSIX_ENSURE_REF(en.data);
      53                 :            : 
      54                 :      41812 :     uint16_t payload_length = encrypted_length;
      55                 :      41812 :     uint8_t mac_digest_size = 0;
      56         [ -  + ]:      41812 :     POSIX_GUARD(s2n_hmac_digest_size(mac->alg, &mac_digest_size));
      57                 :            : 
      58 [ -  + ][ #  # ]:      41812 :     POSIX_ENSURE_GTE(payload_length, mac_digest_size);
      59                 :      41812 :     payload_length -= mac_digest_size;
      60                 :            : 
      61                 :            :     /* Decrypt stuff! */
      62                 :            :     /* Check that we have some data to decrypt */
      63 [ -  + ][ #  # ]:      41812 :     POSIX_ENSURE_NE(en.size, 0);
      64                 :            : 
      65                 :            :     /* ... and that we have a multiple of the block size */
      66 [ -  + ][ #  # ]:      41812 :     POSIX_ENSURE_EQ(en.size % iv.size, 0);
      67                 :            : 
      68                 :            :     /* Copy the last encrypted block to be the next IV */
      69         [ +  + ]:      41812 :     if (conn->actual_protocol_version < S2N_TLS11) {
      70 [ -  + ][ #  # ]:       8896 :         POSIX_CHECKED_MEMCPY(ivpad, en.data + en.size - iv.size, iv.size);
                 [ +  - ]
      71                 :       8896 :     }
      72                 :            : 
      73         [ -  + ]:      41812 :     POSIX_GUARD(cipher_suite->record_alg->cipher->io.cbc.decrypt(session_key, &iv, &en, &en));
      74                 :            : 
      75         [ +  + ]:      41812 :     if (conn->actual_protocol_version < S2N_TLS11) {
      76 [ #  # ][ -  + ]:       8896 :         POSIX_CHECKED_MEMCPY(implicit_iv, ivpad, iv.size);
                 [ +  - ]
      77                 :       8896 :     }
      78                 :            : 
      79                 :            :     /* Subtract the padding length */
      80 [ -  + ][ #  # ]:      41812 :     POSIX_ENSURE_GT(en.size, 0);
      81                 :      41812 :     uint32_t out = 0;
      82         [ -  + ]:      41812 :     POSIX_GUARD(s2n_sub_overflow(payload_length, en.data[en.size - 1] + 1, &out));
      83                 :      41812 :     payload_length = out;
      84         [ -  + ]:      41812 :     POSIX_GUARD(s2n_hmac_reset(mac));
      85         [ -  + ]:      41812 :     POSIX_GUARD(s2n_hmac_update(mac, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
      86                 :            : 
      87         [ +  + ]:      41812 :     if (conn->actual_protocol_version == S2N_SSLv3) {
      88         [ -  + ]:        666 :         POSIX_GUARD(s2n_hmac_update(mac, &header->content_type, 1));
      89         [ -  + ]:        666 :         POSIX_GUARD(s2n_hmac_update_u16(mac, payload_length));
      90                 :      41146 :     } else {
      91         [ -  + ]:      41146 :         POSIX_GUARD(s2n_hmac_update(mac, &header->content_type, sizeof(header->content_type)));
      92         [ -  + ]:      41146 :         POSIX_GUARD(s2n_hmac_update_u16(mac, header->version));
      93         [ -  + ]:      41146 :         POSIX_GUARD(s2n_hmac_update_u16(mac, payload_length));
      94                 :      41146 :     }
      95                 :            : 
      96                 :      41812 :     struct s2n_blob seq = { .data = sequence_number, .size = S2N_TLS_SEQUENCE_NUM_LEN };
      97         [ -  + ]:      41812 :     POSIX_GUARD(s2n_increment_sequence_number(&seq));
      98                 :            : 
      99                 :            :     /* Padding. This finalizes the provided HMAC. */
     100         [ -  + ]:      41812 :     if (s2n_verify_cbc(conn, mac, &en) < 0) {
     101         [ #  # ]:          0 :         POSIX_BAIL(S2N_ERR_BAD_MESSAGE);
     102                 :          0 :     }
     103                 :            : 
     104                 :            :     /* O.k., we've successfully read and decrypted the record, now we need to align the stuffer
     105                 :            :      * for reading the plaintext data.
     106                 :            :      */
     107         [ -  + ]:      41812 :     POSIX_GUARD(s2n_stuffer_reread(&conn->in));
     108                 :            : 
     109                 :            :     /* Skip the IV, if any */
     110         [ +  + ]:      41812 :     if (conn->actual_protocol_version > S2N_TLS10) {
     111         [ -  + ]:      32916 :         POSIX_GUARD(s2n_stuffer_skip_read(&conn->in, cipher_suite->record_alg->cipher->io.cbc.record_iv_size));
     112                 :      32916 :     }
     113                 :            : 
     114                 :            :     /* Truncate and wipe the MAC and any padding */
     115                 :      41812 :     uint32_t mac_len = 0;
     116         [ -  + ]:      41812 :     POSIX_GUARD(s2n_sub_overflow(s2n_stuffer_data_available(&conn->in), payload_length, &mac_len));
     117         [ -  + ]:      41812 :     POSIX_GUARD(s2n_stuffer_wipe_n(&conn->in, mac_len));
     118                 :      41812 :     conn->in_status = PLAINTEXT;
     119                 :            : 
     120                 :      41812 :     return 0;
     121                 :      41812 : }

Generated by: LCOV version 1.14