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