Debian Codegraph @

crt/s2n/tls/s2n_record_read_cbc.c

raw symbols SCIP
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 uint8_t content_type,
33 uint16_t encrypted_length,
34 uint8_t *implicit_iv,
35 struct s2n_hmac_state *mac,
36 uint8_t *sequence_number,
37 struct s2n_session_key *session_key)
38 {
39 struct s2n_blob iv = { .data = implicit_iv, .size = cipher_suite->record_alg->cipher->io.cbc.record_iv_size };
40 uint8_t ivpad[S2N_TLS_MAX_IV_LEN];
41
42 /* Add the header to the HMAC */
43 uint8_t *header = s2n_stuffer_raw_read(&conn->header_in, S2N_TLS_RECORD_HEADER_LENGTH);
44 POSIX_ENSURE_REF(header);
45
46 POSIX_ENSURE_LTE(cipher_suite->record_alg->cipher->io.cbc.record_iv_size, S2N_TLS_MAX_IV_LEN);
47
48 /* For TLS >= 1.1 the IV is in the packet */
49 if (conn->actual_protocol_version > S2N_TLS10) {
50 POSIX_GUARD(s2n_stuffer_read(&conn->in, &iv));
51 POSIX_ENSURE_GTE(encrypted_length, iv.size);
52 encrypted_length -= iv.size;
53 }
54
55 struct s2n_blob en = { .size = encrypted_length, .data = s2n_stuffer_raw_read(&conn->in, encrypted_length) };
56 POSIX_ENSURE_REF(en.data);
57
58 uint16_t payload_length = encrypted_length;
59 uint8_t mac_digest_size = 0;
60 POSIX_GUARD(s2n_hmac_digest_size(mac->alg, &mac_digest_size));
61
62 POSIX_ENSURE_GTE(payload_length, mac_digest_size);
63 payload_length -= mac_digest_size;
64
65 /* Decrypt stuff! */
66 /* Check that we have some data to decrypt */
67 POSIX_ENSURE_NE(en.size, 0);
68
69 /* ... and that we have a multiple of the block size */
70 POSIX_ENSURE_EQ(en.size % iv.size, 0);
71
72 /* Copy the last encrypted block to be the next IV */
73 if (conn->actual_protocol_version < S2N_TLS11) {
74 POSIX_CHECKED_MEMCPY(ivpad, en.data + en.size - iv.size, iv.size);
75 }
76
77 POSIX_GUARD(cipher_suite->record_alg->cipher->io.cbc.decrypt(session_key, &iv, &en, &en));
78
79 if (conn->actual_protocol_version < S2N_TLS11) {
80 POSIX_CHECKED_MEMCPY(implicit_iv, ivpad, iv.size);
81 }
82
83 /* Subtract the padding length */
84 POSIX_ENSURE_GT(en.size, 0);
85 uint32_t out = 0;
86 POSIX_GUARD(s2n_sub_overflow(payload_length, en.data[en.size - 1] + 1, &out));
87 payload_length = out;
88 /* Update the MAC */
89 header[3] = (payload_length >> 8);
90 header[4] = payload_length & 0xff;
91 POSIX_GUARD(s2n_hmac_reset(mac));
92 POSIX_GUARD(s2n_hmac_update(mac, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
93
94 if (conn->actual_protocol_version == S2N_SSLv3) {
95 POSIX_GUARD(s2n_hmac_update(mac, header, 1));
96 POSIX_GUARD(s2n_hmac_update(mac, header + 3, 2));
97 } else {
98 POSIX_GUARD(s2n_hmac_update(mac, header, S2N_TLS_RECORD_HEADER_LENGTH));
99 }
100
101 struct s2n_blob seq = { .data = sequence_number, .size = S2N_TLS_SEQUENCE_NUM_LEN };
102 POSIX_GUARD(s2n_increment_sequence_number(&seq));
103
104 /* Padding. This finalizes the provided HMAC. */
105 if (s2n_verify_cbc(conn, mac, &en) < 0) {
106 POSIX_BAIL(S2N_ERR_BAD_MESSAGE);
107 }
108
109 /* O.k., we've successfully read and decrypted the record, now we need to align the stuffer
110 * for reading the plaintext data.
111 */
112 POSIX_GUARD(s2n_stuffer_reread(&conn->in));
113 POSIX_GUARD(s2n_stuffer_reread(&conn->header_in));
114
115 /* Skip the IV, if any */
116 if (conn->actual_protocol_version > S2N_TLS10) {
117 POSIX_GUARD(s2n_stuffer_skip_read(&conn->in, cipher_suite->record_alg->cipher->io.cbc.record_iv_size));
118 }
119
120 /* Truncate and wipe the MAC and any padding */
121 POSIX_GUARD(s2n_stuffer_wipe_n(&conn->in, s2n_stuffer_data_available(&conn->in) - payload_length));
122 conn->in_status = PLAINTEXT;
123
124 return 0;
125 }