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_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 : 107582 : {
38 : : /* Don't reduce encrypted length for explicit IV, composite decrypt expects it */
39 : 107582 : struct s2n_blob iv = { .data = implicit_iv, .size = cipher_suite->record_alg->cipher->io.comp.record_iv_size };
40 : 107582 : uint8_t ivpad[S2N_TLS_MAX_IV_LEN];
41 : :
42 : : /* Add the header to the HMAC */
43 : 107582 : uint8_t *header = s2n_stuffer_raw_read(&conn->header_in, S2N_TLS_RECORD_HEADER_LENGTH);
44 [ - + ][ # # ]: 107582 : POSIX_ENSURE_REF(header);
45 : :
46 : 107582 : struct s2n_blob en = { .size = encrypted_length, .data = s2n_stuffer_raw_read(&conn->in, encrypted_length) };
47 [ - + ][ # # ]: 107582 : POSIX_ENSURE_REF(en.data);
48 : :
49 : 107582 : uint16_t payload_length = encrypted_length;
50 : 107582 : uint8_t mac_digest_size = 0;
51 [ - + ]: 107582 : POSIX_GUARD(s2n_hmac_digest_size(mac->alg, &mac_digest_size));
52 : :
53 [ # # ][ - + ]: 107582 : POSIX_ENSURE_GTE(payload_length, mac_digest_size);
54 : 107582 : 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 : 107582 : int mac_size = 0;
62 [ - + ]: 107582 : 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 [ # # ][ - + ]: 107582 : POSIX_ENSURE_GTE(payload_length, mac_size);
65 : 107582 : payload_length -= mac_size;
66 : : /* Adjust payload_length for explicit IV */
67 [ + + ]: 107582 : if (conn->actual_protocol_version > S2N_TLS10) {
68 : 74793 : uint32_t out = 0;
69 [ - + ]: 74793 : POSIX_GUARD(s2n_sub_overflow(payload_length, cipher_suite->record_alg->cipher->io.comp.record_iv_size, &out));
70 : 74793 : payload_length = out;
71 : 74793 : }
72 : :
73 : : /* Decrypt stuff! */
74 [ - + ][ # # ]: 107582 : POSIX_ENSURE_NE(en.size, 0);
75 [ # # ][ - + ]: 107582 : POSIX_ENSURE_EQ(en.size % iv.size, 0);
76 : :
77 : : /* Copy the last encrypted block to be the next IV */
78 [ # # ][ - + ]: 107582 : 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 [ - + ]: 107582 : POSIX_GUARD((cipher_suite->record_alg->cipher->io.comp.decrypt(session_key, &iv, &en, &en)));
82 : :
83 [ - + ][ # # ]: 107582 : POSIX_CHECKED_MEMCPY(implicit_iv, ivpad, iv.size);
[ + - ]
84 : :
85 : : /* Subtract the padding length */
86 [ - + ][ # # ]: 107582 : POSIX_ENSURE_GT(en.size, 0);
87 : 107582 : uint32_t out = 0;
88 [ - + ]: 107582 : POSIX_GUARD(s2n_sub_overflow(payload_length, en.data[en.size - 1] + 1, &out));
89 : 107582 : payload_length = out;
90 : :
91 : 107582 : struct s2n_blob seq = { .data = sequence_number, .size = S2N_TLS_SEQUENCE_NUM_LEN };
92 [ - + ]: 107582 : 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 [ - + ]: 107582 : POSIX_GUARD(s2n_stuffer_reread(&conn->in));
98 [ - + ]: 107582 : POSIX_GUARD(s2n_stuffer_reread(&conn->header_in));
99 : :
100 : : /* Skip the IV, if any */
101 [ + + ]: 107582 : if (conn->actual_protocol_version > S2N_TLS10) {
102 [ - + ]: 74793 : POSIX_GUARD(s2n_stuffer_skip_read(&conn->in, cipher_suite->record_alg->cipher->io.comp.record_iv_size));
103 : 74793 : }
104 : :
105 : : /* Truncate and wipe the MAC and any padding */
106 [ - + ]: 107582 : POSIX_GUARD(s2n_stuffer_wipe_n(&conn->in, s2n_stuffer_data_available(&conn->in) - payload_length));
107 : 107582 : conn->in_status = PLAINTEXT;
108 : :
109 : 107582 : return 0;
110 : 107582 : }
|