Skip to content

Commit ae78fe9

Browse files
authored
refactor: consolidate record wiping (#4412)
1 parent ec6ca6e commit ae78fe9

File tree

5 files changed

+20
-30
lines changed

5 files changed

+20
-30
lines changed

tls/s2n_handshake_io.c

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,13 +1336,10 @@ static int s2n_handshake_handle_sslv2(struct s2n_connection *conn)
13361336
POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
13371337

13381338
/* We're done with the record, wipe it */
1339-
POSIX_GUARD(s2n_stuffer_wipe(&conn->header_in));
1340-
POSIX_GUARD(s2n_stuffer_wipe(&conn->in));
1339+
POSIX_GUARD_RESULT(s2n_record_wipe(conn));
13411340

13421341
WITH_ERROR_BLINDING(conn, POSIX_GUARD(r));
13431342

1344-
conn->in_status = ENCRYPTED;
1345-
13461343
/* Advance the state machine */
13471344
POSIX_GUARD(s2n_advance_message(conn));
13481345

@@ -1360,15 +1357,6 @@ static int s2n_try_delete_session_cache(struct s2n_connection *conn)
13601357
return S2N_SUCCESS;
13611358
}
13621359

1363-
static S2N_RESULT s2n_wipe_record(struct s2n_connection *conn)
1364-
{
1365-
RESULT_ENSURE_REF(conn);
1366-
RESULT_GUARD_POSIX(s2n_stuffer_wipe(&conn->header_in));
1367-
RESULT_GUARD_POSIX(s2n_stuffer_wipe(&conn->in));
1368-
conn->in_status = ENCRYPTED;
1369-
return S2N_RESULT_OK;
1370-
}
1371-
13721360
static S2N_RESULT s2n_finish_read(struct s2n_connection *conn)
13731361
{
13741362
RESULT_ENSURE_REF(conn);
@@ -1439,7 +1427,7 @@ static int s2n_handshake_read_io(struct s2n_connection *conn)
14391427
if ((r < S2N_SUCCESS) && (s2n_errno == S2N_ERR_EARLY_DATA_TRIAL_DECRYPT)) {
14401428
POSIX_GUARD(s2n_stuffer_reread(&conn->in));
14411429
POSIX_GUARD_RESULT(s2n_early_data_record_bytes(conn, s2n_stuffer_data_available(&conn->in)));
1442-
POSIX_GUARD_RESULT(s2n_wipe_record(conn));
1430+
POSIX_GUARD_RESULT(s2n_record_wipe(conn));
14431431
return S2N_SUCCESS;
14441432
}
14451433
POSIX_GUARD(r);
@@ -1473,7 +1461,7 @@ static int s2n_handshake_read_io(struct s2n_connection *conn)
14731461
POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
14741462

14751463
/* We're done with the record, wipe it */
1476-
POSIX_GUARD_RESULT(s2n_wipe_record(conn));
1464+
POSIX_GUARD_RESULT(s2n_record_wipe(conn));
14771465

14781466
/* Advance the state machine if this was an expected message */
14791467
if (EXPECTED_RECORD_TYPE(conn) == TLS_CHANGE_CIPHER_SPEC && !CONNECTION_IS_WRITER(conn)) {
@@ -1489,7 +1477,7 @@ static int s2n_handshake_read_io(struct s2n_connection *conn)
14891477
/* Ignore record types that we don't support */
14901478

14911479
/* We're done with the record, wipe it */
1492-
POSIX_GUARD_RESULT(s2n_wipe_record(conn));
1480+
POSIX_GUARD_RESULT(s2n_record_wipe(conn));
14931481
return S2N_SUCCESS;
14941482
}
14951483

@@ -1507,7 +1495,7 @@ static int s2n_handshake_read_io(struct s2n_connection *conn)
15071495
/* Break out of this inner loop, but since we're not changing the state, the
15081496
* outer loop in s2n_handshake_io() will read another record.
15091497
*/
1510-
POSIX_GUARD_RESULT(s2n_wipe_record(conn));
1498+
POSIX_GUARD_RESULT(s2n_record_wipe(conn));
15111499
return S2N_SUCCESS;
15121500
}
15131501

@@ -1556,7 +1544,7 @@ static int s2n_handshake_read_io(struct s2n_connection *conn)
15561544
}
15571545

15581546
/* We're done with the record, wipe it */
1559-
POSIX_GUARD_RESULT(s2n_wipe_record(conn));
1547+
POSIX_GUARD_RESULT(s2n_record_wipe(conn));
15601548
return S2N_SUCCESS;
15611549
}
15621550

@@ -1579,9 +1567,7 @@ static int s2n_handle_retry_state(struct s2n_connection *conn)
15791567

15801568
if (!CONNECTION_IS_WRITER(conn)) {
15811569
/* We're done parsing the record, reset everything */
1582-
POSIX_GUARD(s2n_stuffer_wipe(&conn->header_in));
1583-
POSIX_GUARD(s2n_stuffer_wipe(&conn->in));
1584-
conn->in_status = ENCRYPTED;
1570+
POSIX_GUARD_RESULT(s2n_record_wipe(conn));
15851571
}
15861572

15871573
if (CONNECTION_IS_WRITER(conn)) {

tls/s2n_record.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,4 @@ int s2n_sslv2_record_header_parse(struct s2n_connection *conn, uint8_t *record_t
7878
int s2n_verify_cbc(struct s2n_connection *conn, struct s2n_hmac_state *hmac, struct s2n_blob *decrypted);
7979
S2N_RESULT s2n_aead_aad_init(const struct s2n_connection *conn, uint8_t *sequence_number, uint8_t content_type, uint16_t record_length, struct s2n_blob *ad);
8080
S2N_RESULT s2n_tls13_aead_aad_init(uint16_t record_length, uint8_t tag_length, struct s2n_blob *ad);
81+
S2N_RESULT s2n_record_wipe(struct s2n_connection *conn);

tls/s2n_record_read.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,12 @@ int s2n_tls13_parse_record_type(struct s2n_stuffer *stuffer, uint8_t *record_typ
252252

253253
return 0;
254254
}
255+
256+
S2N_RESULT s2n_record_wipe(struct s2n_connection *conn)
257+
{
258+
RESULT_ENSURE_REF(conn);
259+
RESULT_GUARD_POSIX(s2n_stuffer_wipe(&conn->header_in));
260+
RESULT_GUARD_POSIX(s2n_stuffer_wipe(&conn->in));
261+
conn->in_status = ENCRYPTED;
262+
return S2N_RESULT_OK;
263+
}

tls/s2n_recv.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,7 @@ ssize_t s2n_recv_impl(struct s2n_connection *conn, void *buf, ssize_t size_signe
203203
break;
204204
}
205205
}
206-
POSIX_GUARD(s2n_stuffer_wipe(&conn->header_in));
207-
POSIX_GUARD(s2n_stuffer_wipe(&conn->in));
208-
conn->in_status = ENCRYPTED;
206+
POSIX_GUARD_RESULT(s2n_record_wipe(conn));
209207
continue;
210208
}
211209

@@ -219,9 +217,7 @@ ssize_t s2n_recv_impl(struct s2n_connection *conn, void *buf, ssize_t size_signe
219217

220218
/* Are we ready for more encrypted data? */
221219
if (s2n_stuffer_data_available(&conn->in) == 0) {
222-
POSIX_GUARD(s2n_stuffer_wipe(&conn->header_in));
223-
POSIX_GUARD(s2n_stuffer_wipe(&conn->in));
224-
conn->in_status = ENCRYPTED;
220+
POSIX_GUARD_RESULT(s2n_record_wipe(conn));
225221
}
226222

227223
/* If we've read some data, return it in legacy mode */

tls/s2n_shutdown.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ int s2n_shutdown(struct s2n_connection *conn, s2n_blocked_status *blocked)
124124
/* Reset IO. Make sure we do this before attempting to read a record in
125125
* case a previous failed read left IO in a bad state.
126126
*/
127-
POSIX_GUARD(s2n_stuffer_wipe(&conn->header_in));
128-
POSIX_GUARD(s2n_stuffer_wipe(&conn->in));
129-
conn->in_status = ENCRYPTED;
127+
POSIX_GUARD_RESULT(s2n_record_wipe(conn));
130128

131129
POSIX_GUARD(s2n_read_full_record(conn, &record_type, &isSSLv2));
132130
POSIX_ENSURE(!isSSLv2, S2N_ERR_BAD_MESSAGE);

0 commit comments

Comments
 (0)