| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/s2n_connection.h" |
| 17 |
#include "tls/s2n_early_data.h" |
| 18 |
#include "utils/s2n_mem.h" |
| 19 |
#include "utils/s2n_safety.h" |
| 20 |
|
| 21 |
int s2n_end_of_early_data_send(struct s2n_connection *conn) |
| 22 |
{ |
| 23 |
if (conn->early_data_expected) { |
| 24 |
POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io)); |
| 25 |
POSIX_BAIL(S2N_ERR_EARLY_DATA_BLOCKED); |
| 26 |
} |
| 27 |
|
| 28 |
POSIX_GUARD_RESULT(s2n_connection_set_early_data_state(conn, S2N_END_OF_EARLY_DATA)); |
| 29 |
return S2N_SUCCESS; |
| 30 |
} |
| 31 |
|
| 32 |
int s2n_end_of_early_data_recv(struct s2n_connection *conn) |
| 33 |
{ |
| 34 |
POSIX_ENSURE(!s2n_connection_is_quic_enabled(conn), S2N_ERR_BAD_MESSAGE); |
| 35 |
POSIX_GUARD_RESULT(s2n_connection_set_early_data_state(conn, S2N_END_OF_EARLY_DATA)); |
| 36 |
return S2N_SUCCESS; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
bool s2n_early_data_is_trial_decryption_allowed(struct s2n_connection *conn, uint8_t record_type) |
| 48 |
{ |
| 49 |
return conn && (conn->early_data_state == S2N_EARLY_DATA_REJECTED) |
| 50 |
&& record_type == TLS_APPLICATION_DATA |
| 51 |
|
| 52 |
&& (conn->mode == S2N_SERVER) |
| 53 |
|
| 54 |
&& (s2n_conn_get_current_message_type(conn) != APPLICATION_DATA); |
| 55 |
} |
| 56 |
|
| 57 |
static bool s2n_is_early_data_io(struct s2n_connection *conn) |
| 58 |
{ |
| 59 |
if (s2n_conn_get_current_message_type(conn) == APPLICATION_DATA) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
if (conn->early_data_expected |
| 73 |
|| (conn->mode == S2N_CLIENT && conn->early_data_state == S2N_EARLY_DATA_REQUESTED) |
| 74 |
|| conn->early_data_state == S2N_EARLY_DATA_ACCEPTED |
| 75 |
|| conn->early_data_state == S2N_END_OF_EARLY_DATA) { |
| 76 |
return true; |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
S2N_RESULT s2n_early_data_record_bytes(struct s2n_connection *conn, ssize_t data_len) |
| 82 |
{ |
| 83 |
RESULT_ENSURE_REF(conn); |
| 84 |
if (data_len < 0 || !s2n_is_early_data_io(conn)) { |
| 85 |
return S2N_RESULT_OK; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
if ((size_t) data_len > (UINT64_MAX - conn->early_data_bytes)) { |
| 90 |
conn->early_data_bytes = UINT64_MAX; |
| 91 |
RESULT_BAIL(S2N_ERR_INTEGER_OVERFLOW); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
conn->early_data_bytes += data_len; |
| 98 |
|
| 99 |
uint32_t max_early_data_size = 0; |
| 100 |
RESULT_GUARD_POSIX(s2n_connection_get_max_early_data_size(conn, &max_early_data_size)); |
| 101 |
RESULT_ENSURE(conn->early_data_bytes <= max_early_data_size, S2N_ERR_MAX_EARLY_DATA_SIZE); |
| 102 |
|
| 103 |
return S2N_RESULT_OK; |
| 104 |
} |
| 105 |
|
| 106 |
S2N_RESULT s2n_early_data_validate_send(struct s2n_connection *conn, uint32_t bytes_to_send) |
| 107 |
{ |
| 108 |
RESULT_ENSURE_REF(conn); |
| 109 |
if (!s2n_is_early_data_io(conn)) { |
| 110 |
return S2N_RESULT_OK; |
| 111 |
} |
| 112 |
|
| 113 |
RESULT_ENSURE(conn->early_data_expected, S2N_ERR_EARLY_DATA_NOT_ALLOWED); |
| 114 |
RESULT_ENSURE(conn->mode == S2N_CLIENT, S2N_ERR_EARLY_DATA_NOT_ALLOWED); |
| 115 |
RESULT_ENSURE(conn->early_data_state == S2N_EARLY_DATA_REQUESTED |
| 116 |
|| conn->early_data_state == S2N_EARLY_DATA_ACCEPTED, |
| 117 |
S2N_ERR_EARLY_DATA_NOT_ALLOWED); |
| 118 |
|
| 119 |
uint32_t allowed_early_data_size = 0; |
| 120 |
RESULT_GUARD_POSIX(s2n_connection_get_remaining_early_data_size(conn, &allowed_early_data_size)); |
| 121 |
RESULT_ENSURE(bytes_to_send <= allowed_early_data_size, S2N_ERR_MAX_EARLY_DATA_SIZE); |
| 122 |
|
| 123 |
return S2N_RESULT_OK; |
| 124 |
} |
| 125 |
|
| 126 |
S2N_RESULT s2n_early_data_validate_recv(struct s2n_connection *conn) |
| 127 |
{ |
| 128 |
RESULT_ENSURE_REF(conn); |
| 129 |
if (!s2n_is_early_data_io(conn)) { |
| 130 |
return S2N_RESULT_OK; |
| 131 |
} |
| 132 |
|
| 133 |
RESULT_ENSURE(conn->early_data_expected, S2N_ERR_EARLY_DATA_NOT_ALLOWED); |
| 134 |
RESULT_ENSURE(conn->mode == S2N_SERVER, S2N_ERR_EARLY_DATA_NOT_ALLOWED); |
| 135 |
RESULT_ENSURE(conn->early_data_state == S2N_EARLY_DATA_ACCEPTED, S2N_ERR_EARLY_DATA_NOT_ALLOWED); |
| 136 |
RESULT_ENSURE(s2n_conn_get_current_message_type(conn) == END_OF_EARLY_DATA, S2N_ERR_EARLY_DATA_NOT_ALLOWED); |
| 137 |
return S2N_RESULT_OK; |
| 138 |
} |
| 139 |
|
| 140 |
static bool s2n_early_data_can_continue(struct s2n_connection *conn) |
| 141 |
{ |
| 142 |
uint32_t remaining_early_data_size = 0; |
| 143 |
return s2n_connection_get_remaining_early_data_size(conn, &remaining_early_data_size) >= S2N_SUCCESS |
| 144 |
&& remaining_early_data_size > 0; |
| 145 |
} |
| 146 |
|
| 147 |
S2N_RESULT s2n_send_early_data_impl(struct s2n_connection *conn, const uint8_t *data, ssize_t data_len_signed, |
| 148 |
ssize_t *data_sent, s2n_blocked_status *blocked) |
| 149 |
{ |
| 150 |
RESULT_ENSURE_GTE(data_len_signed, 0); |
| 151 |
size_t data_len = data_len_signed; |
| 152 |
RESULT_ENSURE_REF(conn); |
| 153 |
RESULT_ENSURE_REF(blocked); |
| 154 |
*blocked = S2N_NOT_BLOCKED; |
| 155 |
RESULT_ENSURE_REF(data_sent); |
| 156 |
*data_sent = 0; |
| 157 |
|
| 158 |
RESULT_ENSURE(conn->mode == S2N_CLIENT, S2N_ERR_SERVER_MODE); |
| 159 |
RESULT_ENSURE(s2n_connection_supports_tls13(conn), S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 160 |
|
| 161 |
if (!s2n_early_data_can_continue(conn)) { |
| 162 |
return S2N_RESULT_OK; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
int negotiate_result = s2n_negotiate(conn, blocked); |
| 168 |
if (negotiate_result < S2N_SUCCESS) { |
| 169 |
if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) { |
| 170 |
RESULT_GUARD_POSIX(negotiate_result); |
| 171 |
} else if (*blocked != S2N_BLOCKED_ON_EARLY_DATA && *blocked != S2N_BLOCKED_ON_READ) { |
| 172 |
RESULT_GUARD_POSIX(negotiate_result); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
int negotiate_error = s2n_errno; |
| 177 |
s2n_blocked_status negotiate_blocked = *blocked; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
uint32_t early_data_to_send = 0; |
| 182 |
RESULT_GUARD_POSIX(s2n_connection_get_remaining_early_data_size(conn, &early_data_to_send)); |
| 183 |
early_data_to_send = S2N_MIN(data_len, early_data_to_send); |
| 184 |
if (early_data_to_send) { |
| 185 |
ssize_t send_result = s2n_send(conn, data, early_data_to_send, blocked); |
| 186 |
RESULT_GUARD_POSIX(send_result); |
| 187 |
*data_sent = send_result; |
| 188 |
} |
| 189 |
*blocked = S2N_NOT_BLOCKED; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if (negotiate_result < S2N_SUCCESS) { |
| 194 |
RESULT_ENSURE_EQ(s2n_error_get_type(negotiate_error), S2N_ERR_T_BLOCKED); |
| 195 |
if (negotiate_blocked == S2N_BLOCKED_ON_EARLY_DATA) { |
| 196 |
return S2N_RESULT_OK; |
| 197 |
} else if (s2n_early_data_can_continue(conn)) { |
| 198 |
*blocked = negotiate_blocked; |
| 199 |
RESULT_BAIL(negotiate_error); |
| 200 |
} else { |
| 201 |
return S2N_RESULT_OK; |
| 202 |
} |
| 203 |
} |
| 204 |
return S2N_RESULT_OK; |
| 205 |
} |
| 206 |
|
| 207 |
int s2n_send_early_data(struct s2n_connection *conn, const uint8_t *data, ssize_t data_len, |
| 208 |
ssize_t *data_sent, s2n_blocked_status *blocked) |
| 209 |
{ |
| 210 |
POSIX_ENSURE_REF(conn); |
| 211 |
|
| 212 |
|
| 213 |
POSIX_GUARD(s2n_connection_set_early_data_expected(conn)); |
| 214 |
|
| 215 |
s2n_result result = s2n_send_early_data_impl(conn, data, data_len, data_sent, blocked); |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
POSIX_GUARD(s2n_connection_set_end_of_early_data(conn)); |
| 220 |
|
| 221 |
POSIX_GUARD_RESULT(result); |
| 222 |
return S2N_SUCCESS; |
| 223 |
} |
| 224 |
|
| 225 |
S2N_RESULT s2n_recv_early_data_impl(struct s2n_connection *conn, uint8_t *data, ssize_t max_data_len, |
| 226 |
ssize_t *data_received, s2n_blocked_status *blocked) |
| 227 |
{ |
| 228 |
RESULT_ENSURE_REF(conn); |
| 229 |
RESULT_ENSURE_REF(blocked); |
| 230 |
*blocked = S2N_NOT_BLOCKED; |
| 231 |
RESULT_ENSURE_REF(data_received); |
| 232 |
*data_received = 0; |
| 233 |
|
| 234 |
RESULT_ENSURE(conn->mode == S2N_SERVER, S2N_ERR_CLIENT_MODE); |
| 235 |
|
| 236 |
if (!s2n_early_data_can_continue(conn)) { |
| 237 |
return S2N_RESULT_OK; |
| 238 |
} |
| 239 |
|
| 240 |
int negotiate_result = S2N_SUCCESS; |
| 241 |
while ((negotiate_result = s2n_negotiate(conn, blocked)) != S2N_SUCCESS) { |
| 242 |
if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) { |
| 243 |
RESULT_GUARD_POSIX(negotiate_result); |
| 244 |
} else if (max_data_len <= *data_received) { |
| 245 |
RESULT_GUARD_POSIX(negotiate_result); |
| 246 |
} else if (*blocked != S2N_BLOCKED_ON_EARLY_DATA) { |
| 247 |
if (s2n_early_data_can_continue(conn)) { |
| 248 |
RESULT_GUARD_POSIX(negotiate_result); |
| 249 |
} else { |
| 250 |
*blocked = S2N_NOT_BLOCKED; |
| 251 |
return S2N_RESULT_OK; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
ssize_t recv_result = s2n_recv(conn, data + *data_received, |
| 256 |
max_data_len - *data_received, blocked); |
| 257 |
RESULT_GUARD_POSIX(recv_result); |
| 258 |
*data_received += recv_result; |
| 259 |
} |
| 260 |
return S2N_RESULT_OK; |
| 261 |
} |
| 262 |
|
| 263 |
int s2n_recv_early_data(struct s2n_connection *conn, uint8_t *data, ssize_t max_data_len, |
| 264 |
ssize_t *data_received, s2n_blocked_status *blocked) |
| 265 |
{ |
| 266 |
|
| 267 |
POSIX_GUARD(s2n_connection_set_early_data_expected(conn)); |
| 268 |
|
| 269 |
s2n_result result = s2n_recv_early_data_impl(conn, data, max_data_len, data_received, blocked); |
| 270 |
|
| 271 |
|
| 272 |
POSIX_GUARD(s2n_connection_set_end_of_early_data(conn)); |
| 273 |
|
| 274 |
POSIX_GUARD_RESULT(result); |
| 275 |
return S2N_SUCCESS; |
| 276 |
} |