| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "api/s2n.h" |
| 17 |
#include "tls/extensions/s2n_client_psk.h" |
| 18 |
#include "tls/extensions/s2n_early_data_indication.h" |
| 19 |
#include "tls/s2n_cipher_suites.h" |
| 20 |
#include "tls/s2n_early_data.h" |
| 21 |
#include "tls/s2n_protocol_preferences.h" |
| 22 |
#include "tls/s2n_tls13.h" |
| 23 |
#include "utils/s2n_safety.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
static S2N_RESULT s2n_setup_middlebox_compat_for_early_data(struct s2n_connection *conn) |
| 41 |
{ |
| 42 |
RESULT_ENSURE_REF(conn); |
| 43 |
|
| 44 |
if (s2n_is_middlebox_compat_enabled(conn)) { |
| 45 |
RESULT_GUARD(s2n_handshake_type_set_tls13_flag(conn, MIDDLEBOX_COMPAT)); |
| 46 |
RESULT_GUARD(s2n_handshake_type_set_tls13_flag(conn, EARLY_CLIENT_CCS)); |
| 47 |
} |
| 48 |
return S2N_RESULT_OK; |
| 49 |
} |
| 50 |
|
| 51 |
static S2N_RESULT s2n_early_data_config_is_possible(struct s2n_connection *conn) |
| 52 |
{ |
| 53 |
RESULT_ENSURE_REF(conn); |
| 54 |
|
| 55 |
struct s2n_psk *first_psk = NULL; |
| 56 |
RESULT_GUARD(s2n_array_get(&conn->psk_params.psk_list, 0, (void **) &first_psk)); |
| 57 |
RESULT_ENSURE_REF(first_psk); |
| 58 |
|
| 59 |
struct s2n_early_data_config *early_data_config = &first_psk->early_data_config; |
| 60 |
|
| 61 |
|
| 62 |
RESULT_ENSURE_GT(early_data_config->max_early_data_size, 0); |
| 63 |
|
| 64 |
|
| 65 |
RESULT_ENSURE_GTE(s2n_connection_get_protocol_version(conn), early_data_config->protocol_version); |
| 66 |
RESULT_ENSURE_GTE(s2n_connection_get_protocol_version(conn), S2N_TLS13); |
| 67 |
|
| 68 |
const struct s2n_cipher_preferences *cipher_preferences = NULL; |
| 69 |
RESULT_GUARD_POSIX(s2n_connection_get_cipher_preferences(conn, &cipher_preferences)); |
| 70 |
RESULT_ENSURE_REF(cipher_preferences); |
| 71 |
|
| 72 |
|
| 73 |
bool match = false; |
| 74 |
for (size_t i = 0; i < cipher_preferences->count; i++) { |
| 75 |
if (cipher_preferences->suites[i] == early_data_config->cipher_suite) { |
| 76 |
match = true; |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
RESULT_ENSURE_EQ(match, true); |
| 81 |
|
| 82 |
|
| 83 |
if (early_data_config->application_protocol.size > 0) { |
| 84 |
struct s2n_blob *application_protocols = NULL; |
| 85 |
RESULT_GUARD_POSIX(s2n_connection_get_protocol_preferences(conn, &application_protocols)); |
| 86 |
RESULT_ENSURE_REF(application_protocols); |
| 87 |
|
| 88 |
match = false; |
| 89 |
RESULT_GUARD(s2n_protocol_preferences_contain(application_protocols, &early_data_config->application_protocol, &match)); |
| 90 |
RESULT_ENSURE_EQ(match, true); |
| 91 |
} |
| 92 |
|
| 93 |
return S2N_RESULT_OK; |
| 94 |
} |
| 95 |
|
| 96 |
static bool s2n_client_early_data_indication_should_send(struct s2n_connection *conn) |
| 97 |
{ |
| 98 |
return s2n_result_is_ok(s2n_early_data_config_is_possible(conn)) |
| 99 |
&& conn && conn->early_data_expected |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
&& !s2n_is_hello_retry_handshake(conn) |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
&& s2n_client_psk_extension.should_send(conn); |
| 114 |
} |
| 115 |
|
| 116 |
static int s2n_client_early_data_indication_is_missing(struct s2n_connection *conn) |
| 117 |
{ |
| 118 |
if (conn->early_data_state != S2N_EARLY_DATA_REJECTED) { |
| 119 |
POSIX_GUARD_RESULT(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_NOT_REQUESTED)); |
| 120 |
} |
| 121 |
return S2N_SUCCESS; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
static int s2n_client_early_data_indication_send(struct s2n_connection *conn, struct s2n_stuffer *out) |
| 145 |
{ |
| 146 |
POSIX_ENSURE_REF(conn); |
| 147 |
POSIX_ENSURE_REF(conn->secure); |
| 148 |
|
| 149 |
POSIX_GUARD_RESULT(s2n_setup_middlebox_compat_for_early_data(conn)); |
| 150 |
POSIX_GUARD_RESULT(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_REQUESTED)); |
| 151 |
|
| 152 |
|
| 153 |
struct s2n_psk *first_psk = NULL; |
| 154 |
POSIX_GUARD_RESULT(s2n_array_get(&conn->psk_params.psk_list, 0, (void **) &first_psk)); |
| 155 |
POSIX_ENSURE_REF(first_psk); |
| 156 |
conn->secure->cipher_suite = first_psk->early_data_config.cipher_suite; |
| 157 |
|
| 158 |
return S2N_SUCCESS; |
| 159 |
} |
| 160 |
|
| 161 |
static int s2n_client_early_data_indiction_recv(struct s2n_connection *conn, struct s2n_stuffer *in) |
| 162 |
{ |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
POSIX_ENSURE(conn->handshake.message_number == 0, S2N_ERR_UNSUPPORTED_EXTENSION); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
POSIX_GUARD_RESULT(s2n_setup_middlebox_compat_for_early_data(conn)); |
| 175 |
|
| 176 |
POSIX_GUARD_RESULT(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_REQUESTED)); |
| 177 |
return S2N_SUCCESS; |
| 178 |
} |
| 179 |
|
| 180 |
const s2n_extension_type s2n_client_early_data_indication_extension = { |
| 181 |
.iana_value = TLS_EXTENSION_EARLY_DATA, |
| 182 |
.is_response = false, |
| 183 |
.send = s2n_client_early_data_indication_send, |
| 184 |
.recv = s2n_client_early_data_indiction_recv, |
| 185 |
.should_send = s2n_client_early_data_indication_should_send, |
| 186 |
.if_missing = s2n_client_early_data_indication_is_missing, |
| 187 |
}; |