| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/s2n_renegotiate.h" |
| 17 |
|
| 18 |
#include "error/s2n_errno.h" |
| 19 |
#include "stuffer/s2n_stuffer.h" |
| 20 |
#include "tls/s2n_connection.h" |
| 21 |
#include "utils/s2n_safety.h" |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
const s2n_blocked_status S2N_BLOCKED_ON_APPLICATION_DATA = S2N_BLOCKED_ON_EARLY_DATA; |
| 32 |
|
| 33 |
S2N_RESULT s2n_renegotiate_validate(struct s2n_connection *conn) |
| 34 |
{ |
| 35 |
RESULT_ENSURE_REF(conn); |
| 36 |
RESULT_ENSURE(conn->mode == S2N_CLIENT, S2N_ERR_NO_RENEGOTIATION); |
| 37 |
RESULT_ENSURE(conn->secure_renegotiation, S2N_ERR_NO_RENEGOTIATION); |
| 38 |
RESULT_ENSURE(conn->handshake.renegotiation, S2N_ERR_INVALID_STATE); |
| 39 |
RESULT_ENSURE(!conn->ktls_send_enabled, S2N_ERR_KTLS_RENEG); |
| 40 |
RESULT_ENSURE(!conn->ktls_recv_enabled, S2N_ERR_KTLS_RENEG); |
| 41 |
return S2N_RESULT_OK; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
int s2n_renegotiate_wipe(struct s2n_connection *conn) |
| 58 |
{ |
| 59 |
POSIX_ENSURE_REF(conn); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
POSIX_ENSURE(conn->mode == S2N_CLIENT || s2n_in_unit_test(), S2N_ERR_NO_RENEGOTIATION); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
POSIX_ENSURE(s2n_stuffer_data_available(&conn->header_in) == 0, S2N_ERR_INVALID_STATE); |
| 72 |
POSIX_ENSURE(s2n_stuffer_data_available(&conn->in) == 0, S2N_ERR_INVALID_STATE); |
| 73 |
POSIX_ENSURE(s2n_stuffer_data_available(&conn->out) == 0, S2N_ERR_INVALID_STATE); |
| 74 |
|
| 75 |
|
| 76 |
DEFER_CLEANUP(struct s2n_stuffer buffer_in = conn->buffer_in, s2n_stuffer_free); |
| 77 |
conn->buffer_in = (struct s2n_stuffer){ 0 }; |
| 78 |
POSIX_GUARD(s2n_stuffer_growable_alloc(&conn->buffer_in, 0)); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
DEFER_CLEANUP(struct s2n_crypto_parameters *secure_crypto_params = conn->secure, |
| 84 |
s2n_crypto_parameters_free); |
| 85 |
conn->secure = NULL; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
uint16_t max_frag_len = conn->max_outgoing_fragment_length; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
uint8_t actual_protocol_version = conn->actual_protocol_version; |
| 95 |
uint8_t server_protocol_version = conn->server_protocol_version; |
| 96 |
uint8_t client_protocol_version = conn->client_protocol_version; |
| 97 |
POSIX_ENSURE(actual_protocol_version < S2N_TLS13, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
uint64_t wire_bytes_in = conn->wire_bytes_in; |
| 102 |
uint64_t wire_bytes_out = conn->wire_bytes_out; |
| 103 |
|
| 104 |
|
| 105 |
bool send_managed = conn->managed_send_io; |
| 106 |
s2n_send_fn *send_fn = conn->send; |
| 107 |
void *send_ctx = conn->send_io_context; |
| 108 |
bool recv_managed = conn->managed_recv_io; |
| 109 |
s2n_recv_fn *recv_fn = conn->recv; |
| 110 |
void *recv_ctx = conn->recv_io_context; |
| 111 |
|
| 112 |
conn->managed_send_io = false; |
| 113 |
conn->managed_recv_io = false; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
bool secure_renegotiation = conn->secure_renegotiation; |
| 120 |
POSIX_ENSURE(secure_renegotiation, S2N_ERR_NO_RENEGOTIATION); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
uint8_t finished_len = conn->handshake.finished_len; |
| 126 |
uint8_t client_finished[sizeof(conn->handshake.client_finished)] = { 0 }; |
| 127 |
POSIX_CHECKED_MEMCPY(client_finished, conn->handshake.client_finished, finished_len); |
| 128 |
uint8_t server_finished[sizeof(conn->handshake.server_finished)] = { 0 }; |
| 129 |
POSIX_CHECKED_MEMCPY(server_finished, conn->handshake.server_finished, finished_len); |
| 130 |
|
| 131 |
POSIX_GUARD(s2n_connection_wipe(conn)); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
POSIX_GUARD_RESULT(s2n_crypto_parameters_free(&conn->initial)); |
| 138 |
conn->initial = secure_crypto_params; |
| 139 |
ZERO_TO_DISABLE_DEFER_CLEANUP(secure_crypto_params); |
| 140 |
conn->client = conn->initial; |
| 141 |
conn->server = conn->initial; |
| 142 |
|
| 143 |
|
| 144 |
POSIX_GUARD_RESULT(s2n_connection_set_max_fragment_length(conn, max_frag_len)); |
| 145 |
POSIX_CHECKED_MEMCPY(conn->handshake.client_finished, client_finished, finished_len); |
| 146 |
POSIX_CHECKED_MEMCPY(conn->handshake.server_finished, server_finished, finished_len); |
| 147 |
conn->handshake.finished_len = finished_len; |
| 148 |
conn->actual_protocol_version = actual_protocol_version; |
| 149 |
conn->server_protocol_version = server_protocol_version; |
| 150 |
conn->client_protocol_version = client_protocol_version; |
| 151 |
conn->wire_bytes_in = wire_bytes_in; |
| 152 |
conn->wire_bytes_out = wire_bytes_out; |
| 153 |
conn->managed_send_io = send_managed; |
| 154 |
conn->send = send_fn; |
| 155 |
conn->send_io_context = send_ctx; |
| 156 |
conn->managed_recv_io = recv_managed; |
| 157 |
conn->recv = recv_fn; |
| 158 |
conn->recv_io_context = recv_ctx; |
| 159 |
conn->secure_renegotiation = secure_renegotiation; |
| 160 |
conn->buffer_in = buffer_in; |
| 161 |
ZERO_TO_DISABLE_DEFER_CLEANUP(buffer_in); |
| 162 |
|
| 163 |
conn->handshake.renegotiation = true; |
| 164 |
return S2N_SUCCESS; |
| 165 |
} |
| 166 |
|
| 167 |
static S2N_RESULT s2n_renegotiate_read_app_data(struct s2n_connection *conn, uint8_t *app_data_buf, ssize_t app_data_buf_size, |
| 168 |
ssize_t *app_data_size, s2n_blocked_status *blocked) |
| 169 |
{ |
| 170 |
RESULT_ENSURE_REF(blocked); |
| 171 |
|
| 172 |
ssize_t r = s2n_recv(conn, app_data_buf, app_data_buf_size, blocked); |
| 173 |
RESULT_GUARD_POSIX(r); |
| 174 |
*app_data_size = r; |
| 175 |
|
| 176 |
*blocked = S2N_BLOCKED_ON_APPLICATION_DATA; |
| 177 |
RESULT_BAIL(S2N_ERR_APP_DATA_BLOCKED); |
| 178 |
} |
| 179 |
|
| 180 |
int s2n_renegotiate(struct s2n_connection *conn, uint8_t *app_data_buf, ssize_t app_data_buf_size, |
| 181 |
ssize_t *app_data_size, s2n_blocked_status *blocked) |
| 182 |
{ |
| 183 |
POSIX_ENSURE_REF(blocked); |
| 184 |
*blocked = S2N_BLOCKED_ON_READ; |
| 185 |
|
| 186 |
POSIX_GUARD_RESULT(s2n_renegotiate_validate(conn)); |
| 187 |
|
| 188 |
POSIX_ENSURE_REF(app_data_size); |
| 189 |
*app_data_size = 0; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
if (s2n_peek(conn) > 0) { |
| 195 |
POSIX_GUARD_RESULT(s2n_renegotiate_read_app_data(conn, |
| 196 |
app_data_buf, app_data_buf_size, app_data_size, blocked)); |
| 197 |
} |
| 198 |
|
| 199 |
int result = s2n_negotiate(conn, blocked); |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
if (result != S2N_SUCCESS && s2n_errno == S2N_ERR_APP_DATA_BLOCKED) { |
| 205 |
POSIX_GUARD_RESULT(s2n_renegotiate_read_app_data(conn, |
| 206 |
app_data_buf, app_data_buf_size, app_data_size, blocked)); |
| 207 |
} |
| 208 |
|
| 209 |
return result; |
| 210 |
} |