Debian Codegraph @

crt/s2n/tls/s2n_record_read_stream.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_stream(
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 /* Add the header to the HMAC */
39 uint8_t *header = s2n_stuffer_raw_read(&conn->header_in, S2N_TLS_RECORD_HEADER_LENGTH);
40 POSIX_ENSURE_REF(header);
41
42 struct s2n_blob en = { .size = encrypted_length, .data = s2n_stuffer_raw_read(&conn->in, encrypted_length) };
43 POSIX_ENSURE_REF(en.data);
44
45 uint16_t payload_length = encrypted_length;
46 uint8_t mac_digest_size = 0;
47 POSIX_GUARD(s2n_hmac_digest_size(mac->alg, &mac_digest_size));
48
49 POSIX_ENSURE_GTE(payload_length, mac_digest_size);
50 payload_length -= mac_digest_size;
51
52 /* Decrypt stuff! */
53 POSIX_GUARD(cipher_suite->record_alg->cipher->io.stream.decrypt(session_key, &en, &en));
54
55 /* Update the MAC */
56 header[3] = (payload_length >> 8);
57 header[4] = payload_length & 0xff;
58 POSIX_GUARD(s2n_hmac_reset(mac));
59 POSIX_GUARD(s2n_hmac_update(mac, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
60
61 if (conn->actual_protocol_version == S2N_SSLv3) {
62 POSIX_GUARD(s2n_hmac_update(mac, header, 1));
63 POSIX_GUARD(s2n_hmac_update(mac, header + 3, 2));
64 } else {
65 POSIX_GUARD(s2n_hmac_update(mac, header, S2N_TLS_RECORD_HEADER_LENGTH));
66 }
67
68 struct s2n_blob seq = { .data = sequence_number, .size = S2N_TLS_SEQUENCE_NUM_LEN };
69 POSIX_GUARD(s2n_increment_sequence_number(&seq));
70
71 /* MAC check for streaming ciphers - no padding */
72 POSIX_GUARD(s2n_hmac_update(mac, en.data, payload_length));
73
74 uint8_t check_digest[S2N_MAX_DIGEST_LEN];
75 POSIX_ENSURE_LTE(mac_digest_size, sizeof(check_digest));
76 POSIX_GUARD(s2n_hmac_digest(mac, check_digest, mac_digest_size));
77
78 if (s2n_hmac_digest_verify(en.data + payload_length, check_digest, mac_digest_size) < 0) {
79 POSIX_BAIL(S2N_ERR_BAD_MESSAGE);
80 }
81
82 /* O.k., we've successfully read and decrypted the record, now we need to align the stuffer
83 * for reading the plaintext data.
84 */
85 POSIX_GUARD(s2n_stuffer_reread(&conn->in));
86 POSIX_GUARD(s2n_stuffer_reread(&conn->header_in));
87
88 /* Truncate and wipe the MAC and any padding */
89 POSIX_GUARD(s2n_stuffer_wipe_n(&conn->in, s2n_stuffer_data_available(&conn->in) - payload_length));
90 conn->in_status = PLAINTEXT;
91
92 return 0;
93 }