Debian Codegraph @

crt/s2n/tls/s2n_record_read_composite.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_read.h"
25 #include "utils/s2n_blob.h"
26 #include "utils/s2n_safety.h"
27
28 int s2n_record_parse_composite(
29 const struct s2n_cipher_suite *cipher_suite,
30 struct s2n_connection *conn,
31 uint8_t content_type,
32 uint16_t encrypted_length,
33 uint8_t *implicit_iv,
34 struct s2n_hmac_state *mac,
35 uint8_t *sequence_number,
36 struct s2n_session_key *session_key)
37 {
38 /* Don't reduce encrypted length for explicit IV, composite decrypt expects it */
39 struct s2n_blob iv = { .data = implicit_iv, .size = cipher_suite->record_alg->cipher->io.comp.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 struct s2n_blob en = { .size = encrypted_length, .data = s2n_stuffer_raw_read(&conn->in, encrypted_length) };
47 POSIX_ENSURE_REF(en.data);
48
49 uint16_t payload_length = encrypted_length;
50 uint8_t mac_digest_size = 0;
51 POSIX_GUARD(s2n_hmac_digest_size(mac->alg, &mac_digest_size));
52
53 POSIX_ENSURE_GTE(payload_length, mac_digest_size);
54 payload_length -= mac_digest_size;
55
56 /* Compute non-payload parts of the MAC(seq num, type, proto vers, fragment length) for composite ciphers.
57 * Composite "decrypt" will MAC the actual payload data.
58 */
59 /* In the decrypt case, this outputs the MAC digest length:
60 * https://github.com/openssl/openssl/blob/master/crypto/evp/e_aes_cbc_hmac_sha1.c#L842 */
61 int mac_size = 0;
62 POSIX_GUARD(cipher_suite->record_alg->cipher->io.comp.initial_hmac(session_key, sequence_number, content_type, conn->actual_protocol_version, payload_length, &mac_size));
63
64 POSIX_ENSURE_GTE(payload_length, mac_size);
65 payload_length -= mac_size;
66 /* Adjust payload_length for explicit IV */
67 if (conn->actual_protocol_version > S2N_TLS10) {
68 uint32_t out = 0;
69 POSIX_GUARD(s2n_sub_overflow(payload_length, cipher_suite->record_alg->cipher->io.comp.record_iv_size, &out));
70 payload_length = out;
71 }
72
73 /* Decrypt stuff! */
74 POSIX_ENSURE_NE(en.size, 0);
75 POSIX_ENSURE_EQ(en.size % iv.size, 0);
76
77 /* Copy the last encrypted block to be the next IV */
78 POSIX_CHECKED_MEMCPY(ivpad, en.data + en.size - iv.size, iv.size);
79
80 /* This will: Skip the explicit IV(if applicable), decrypt the payload, verify the MAC and padding. */
81 POSIX_GUARD((cipher_suite->record_alg->cipher->io.comp.decrypt(session_key, &iv, &en, &en)));
82
83 POSIX_CHECKED_MEMCPY(implicit_iv, ivpad, iv.size);
84
85 /* Subtract the padding length */
86 POSIX_ENSURE_GT(en.size, 0);
87 uint32_t out = 0;
88 POSIX_GUARD(s2n_sub_overflow(payload_length, en.data[en.size - 1] + 1, &out));
89 payload_length = out;
90
91 struct s2n_blob seq = { .data = sequence_number, .size = S2N_TLS_SEQUENCE_NUM_LEN };
92 POSIX_GUARD(s2n_increment_sequence_number(&seq));
93
94 /* O.k., we've successfully read and decrypted the record, now we need to align the stuffer
95 * for reading the plaintext data.
96 */
97 POSIX_GUARD(s2n_stuffer_reread(&conn->in));
98 POSIX_GUARD(s2n_stuffer_reread(&conn->header_in));
99
100 /* Skip the IV, if any */
101 if (conn->actual_protocol_version > S2N_TLS10) {
102 POSIX_GUARD(s2n_stuffer_skip_read(&conn->in, cipher_suite->record_alg->cipher->io.comp.record_iv_size));
103 }
104
105 /* Truncate and wipe the MAC and any padding */
106 POSIX_GUARD(s2n_stuffer_wipe_n(&conn->in, s2n_stuffer_data_available(&conn->in) - payload_length));
107 conn->in_status = PLAINTEXT;
108
109 return 0;
110 }