| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/s2n_record_read.h" |
| 17 |
|
| 18 |
#include "crypto/s2n_cipher.h" |
| 19 |
#include "crypto/s2n_hmac.h" |
| 20 |
#include "crypto/s2n_sequence.h" |
| 21 |
#include "error/s2n_errno.h" |
| 22 |
#include "stuffer/s2n_stuffer.h" |
| 23 |
#include "tls/s2n_cipher_suites.h" |
| 24 |
#include "tls/s2n_connection.h" |
| 25 |
#include "tls/s2n_crypto.h" |
| 26 |
#include "utils/s2n_blob.h" |
| 27 |
#include "utils/s2n_safety.h" |
| 28 |
|
| 29 |
int s2n_sslv2_record_header_parse( |
| 30 |
struct s2n_connection *conn, |
| 31 |
uint8_t *record_type, |
| 32 |
uint8_t *client_protocol_version, |
| 33 |
uint16_t *fragment_length) |
| 34 |
{ |
| 35 |
struct s2n_stuffer *header_in = &conn->header_in; |
| 36 |
|
| 37 |
POSIX_ENSURE(s2n_stuffer_data_available(header_in) >= S2N_TLS_RECORD_HEADER_LENGTH, |
| 38 |
S2N_ERR_BAD_MESSAGE); |
| 39 |
|
| 40 |
POSIX_GUARD(s2n_stuffer_read_uint16(header_in, fragment_length)); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
POSIX_ENSURE(*fragment_length & S2N_TLS_SSLV2_HEADER_FLAG_UINT16, S2N_ERR_BAD_MESSAGE); |
| 60 |
*fragment_length ^= S2N_TLS_SSLV2_HEADER_FLAG_UINT16; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
POSIX_ENSURE(*fragment_length >= s2n_stuffer_data_available(header_in), S2N_ERR_BAD_MESSAGE); |
| 69 |
*fragment_length -= s2n_stuffer_data_available(header_in); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
POSIX_GUARD(s2n_stuffer_read_uint8(header_in, record_type)); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
uint8_t protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN] = { 0 }; |
| 89 |
POSIX_GUARD(s2n_stuffer_read_bytes(header_in, protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN)); |
| 90 |
*client_protocol_version = (protocol_version[0] * 10) + protocol_version[1]; |
| 91 |
|
| 92 |
POSIX_GUARD(s2n_stuffer_reread(header_in)); |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
int s2n_record_header_parse( |
| 97 |
struct s2n_connection *conn, |
| 98 |
uint8_t *content_type, |
| 99 |
uint16_t *fragment_length) |
| 100 |
{ |
| 101 |
struct s2n_stuffer *in = &conn->header_in; |
| 102 |
|
| 103 |
S2N_ERROR_IF(s2n_stuffer_data_available(in) < S2N_TLS_RECORD_HEADER_LENGTH, S2N_ERR_BAD_MESSAGE); |
| 104 |
|
| 105 |
POSIX_GUARD(s2n_stuffer_read_uint8(in, content_type)); |
| 106 |
|
| 107 |
uint8_t protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN]; |
| 108 |
POSIX_GUARD(s2n_stuffer_read_bytes(in, protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN)); |
| 109 |
|
| 110 |
const uint8_t version = (protocol_version[0] * 10) + protocol_version[1]; |
| 111 |
|
| 112 |
if (!conn->client_hello.record_version_recorded) { |
| 113 |
conn->client_hello.legacy_record_version = version; |
| 114 |
conn->client_hello.record_version_recorded = 1; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
S2N_ERROR_IF(conn->actual_protocol_version_established && S2N_MIN(conn->actual_protocol_version, S2N_TLS12) |
| 126 |
!= version, |
| 127 |
S2N_ERR_BAD_MESSAGE); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
POSIX_GUARD(s2n_stuffer_read_uint16(in, fragment_length)); |
| 141 |
POSIX_GUARD(s2n_stuffer_reread(in)); |
| 142 |
|
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
static bool s2n_is_tls13_plaintext_content(struct s2n_connection *conn, uint8_t content_type) |
| 160 |
{ |
| 161 |
return conn->actual_protocol_version == S2N_TLS13 && (content_type == TLS_ALERT || content_type == TLS_CHANGE_CIPHER_SPEC); |
| 162 |
} |
| 163 |
|
| 164 |
int s2n_record_parse(struct s2n_connection *conn) |
| 165 |
{ |
| 166 |
uint8_t content_type = 0; |
| 167 |
uint16_t encrypted_length = 0; |
| 168 |
POSIX_GUARD(s2n_record_header_parse(conn, &content_type, &encrypted_length)); |
| 169 |
|
| 170 |
struct s2n_crypto_parameters *current_client_crypto = conn->client; |
| 171 |
struct s2n_crypto_parameters *current_server_crypto = conn->server; |
| 172 |
if (s2n_is_tls13_plaintext_content(conn, content_type)) { |
| 173 |
POSIX_ENSURE_REF(conn->initial); |
| 174 |
conn->client = conn->initial; |
| 175 |
conn->server = conn->initial; |
| 176 |
} |
| 177 |
|
| 178 |
const struct s2n_cipher_suite *cipher_suite = conn->client->cipher_suite; |
| 179 |
uint8_t *implicit_iv = conn->client->client_implicit_iv; |
| 180 |
struct s2n_hmac_state *mac = &conn->client->client_record_mac; |
| 181 |
uint8_t *sequence_number = conn->client->client_sequence_number; |
| 182 |
struct s2n_session_key *session_key = &conn->client->client_key; |
| 183 |
|
| 184 |
if (conn->mode == S2N_CLIENT) { |
| 185 |
cipher_suite = conn->server->cipher_suite; |
| 186 |
implicit_iv = conn->server->server_implicit_iv; |
| 187 |
mac = &conn->server->server_record_mac; |
| 188 |
sequence_number = conn->server->server_sequence_number; |
| 189 |
session_key = &conn->server->server_key; |
| 190 |
} |
| 191 |
|
| 192 |
if (s2n_is_tls13_plaintext_content(conn, content_type)) { |
| 193 |
conn->client = current_client_crypto; |
| 194 |
conn->server = current_server_crypto; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
if (cipher_suite->record_alg->cipher == &s2n_null_cipher) { |
| 200 |
POSIX_ENSURE(content_type != TLS_APPLICATION_DATA, S2N_ERR_DECRYPT); |
| 201 |
} |
| 202 |
|
| 203 |
switch (cipher_suite->record_alg->cipher->type) { |
| 204 |
case S2N_AEAD: |
| 205 |
POSIX_GUARD(s2n_record_parse_aead(cipher_suite, conn, content_type, encrypted_length, implicit_iv, mac, sequence_number, session_key)); |
| 206 |
break; |
| 207 |
case S2N_CBC: |
| 208 |
POSIX_GUARD(s2n_record_parse_cbc(cipher_suite, conn, content_type, encrypted_length, implicit_iv, mac, sequence_number, session_key)); |
| 209 |
break; |
| 210 |
case S2N_COMPOSITE: |
| 211 |
POSIX_GUARD(s2n_record_parse_composite(cipher_suite, conn, content_type, encrypted_length, implicit_iv, mac, sequence_number, session_key)); |
| 212 |
break; |
| 213 |
case S2N_STREAM: |
| 214 |
POSIX_GUARD(s2n_record_parse_stream(cipher_suite, conn, content_type, encrypted_length, implicit_iv, mac, sequence_number, session_key)); |
| 215 |
break; |
| 216 |
default: |
| 217 |
POSIX_BAIL(S2N_ERR_CIPHER_TYPE); |
| 218 |
break; |
| 219 |
} |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
int s2n_tls13_parse_record_type(struct s2n_stuffer *stuffer, uint8_t *record_type) |
| 225 |
{ |
| 226 |
uint32_t bytes_left = s2n_stuffer_data_available(stuffer); |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
S2N_ERROR_IF(bytes_left > S2N_MAXIMUM_INNER_PLAINTEXT_LENGTH + 16, S2N_ERR_MAX_INNER_PLAINTEXT_SIZE); |
| 239 |
|
| 240 |
|
| 241 |
POSIX_GUARD(s2n_stuffer_skip_read(stuffer, bytes_left)); |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
*record_type = 0; |
| 248 |
while (*record_type == 0) { |
| 249 |
|
| 250 |
POSIX_GUARD(s2n_stuffer_rewind_read(stuffer, 1)); |
| 251 |
|
| 252 |
|
| 253 |
POSIX_GUARD(s2n_stuffer_read_uint8(stuffer, record_type)); |
| 254 |
|
| 255 |
|
| 256 |
POSIX_GUARD(s2n_stuffer_wipe_n(stuffer, 1)); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
POSIX_GUARD(s2n_stuffer_reread(stuffer)); |
| 262 |
|
| 263 |
|
| 264 |
S2N_ERROR_IF(s2n_stuffer_data_available(stuffer) > S2N_MAXIMUM_INNER_PLAINTEXT_LENGTH - 1, S2N_ERR_MAX_INNER_PLAINTEXT_SIZE); |
| 265 |
|
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
S2N_RESULT s2n_record_wipe(struct s2n_connection *conn) |
| 270 |
{ |
| 271 |
RESULT_ENSURE_REF(conn); |
| 272 |
RESULT_GUARD_POSIX(s2n_stuffer_wipe(&conn->header_in)); |
| 273 |
RESULT_GUARD_POSIX(s2n_stuffer_wipe(&conn->in)); |
| 274 |
conn->in_status = ENCRYPTED; |
| 275 |
|
| 276 |
|
| 277 |
RESULT_GUARD_POSIX(s2n_stuffer_free(&conn->in)); |
| 278 |
conn->buffer_in.tainted = false; |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
if (s2n_stuffer_is_consumed(&conn->buffer_in)) { |
| 284 |
RESULT_GUARD_POSIX(s2n_stuffer_rewrite(&conn->buffer_in)); |
| 285 |
} |
| 286 |
return S2N_RESULT_OK; |
| 287 |
} |