| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/extensions/s2n_client_supported_versions.h" |
| 17 |
|
| 18 |
#include <stdint.h> |
| 19 |
|
| 20 |
#include "tls/extensions/s2n_supported_versions.h" |
| 21 |
#include "tls/s2n_alerts.h" |
| 22 |
#include "tls/s2n_cipher_preferences.h" |
| 23 |
#include "tls/s2n_tls.h" |
| 24 |
#include "tls/s2n_tls_parameters.h" |
| 25 |
#include "utils/s2n_safety.h" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
static int s2n_client_supported_versions_send(struct s2n_connection *conn, struct s2n_stuffer *out); |
| 47 |
static int s2n_client_supported_versions_recv(struct s2n_connection *conn, struct s2n_stuffer *in); |
| 48 |
|
| 49 |
const s2n_extension_type s2n_client_supported_versions_extension = { |
| 50 |
.iana_value = TLS_EXTENSION_SUPPORTED_VERSIONS, |
| 51 |
.is_response = false, |
| 52 |
.send = s2n_client_supported_versions_send, |
| 53 |
.recv = s2n_client_supported_versions_recv, |
| 54 |
.should_send = s2n_extension_send_if_tls13_connection, |
| 55 |
.if_missing = s2n_extension_noop_if_missing, |
| 56 |
}; |
| 57 |
|
| 58 |
static int s2n_client_supported_versions_send(struct s2n_connection *conn, struct s2n_stuffer *out) |
| 59 |
{ |
| 60 |
uint8_t highest_supported_version = conn->client_protocol_version; |
| 61 |
uint8_t minimum_supported_version = s2n_unknown_protocol_version; |
| 62 |
POSIX_GUARD_RESULT(s2n_connection_get_minimum_supported_version(conn, &minimum_supported_version)); |
| 63 |
POSIX_ENSURE(highest_supported_version >= minimum_supported_version, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 64 |
|
| 65 |
uint8_t version_list_length = highest_supported_version - minimum_supported_version + 1; |
| 66 |
POSIX_GUARD(s2n_stuffer_write_uint8(out, version_list_length * S2N_TLS_PROTOCOL_VERSION_LEN)); |
| 67 |
|
| 68 |
for (uint8_t i = highest_supported_version; i >= minimum_supported_version; i--) { |
| 69 |
POSIX_GUARD(s2n_stuffer_write_uint8(out, i / 10)); |
| 70 |
POSIX_GUARD(s2n_stuffer_write_uint8(out, i % 10)); |
| 71 |
} |
| 72 |
|
| 73 |
return S2N_SUCCESS; |
| 74 |
} |
| 75 |
|
| 76 |
int s2n_extensions_client_supported_versions_process(struct s2n_connection *conn, struct s2n_stuffer *extension, |
| 77 |
uint8_t *client_protocol_version_out, uint8_t *actual_protocol_version_out) |
| 78 |
{ |
| 79 |
uint8_t highest_supported_version = conn->server_protocol_version; |
| 80 |
uint8_t minimum_supported_version = s2n_unknown_protocol_version; |
| 81 |
POSIX_GUARD_RESULT(s2n_connection_get_minimum_supported_version(conn, &minimum_supported_version)); |
| 82 |
|
| 83 |
uint8_t size_of_version_list = 0; |
| 84 |
POSIX_GUARD(s2n_stuffer_read_uint8(extension, &size_of_version_list)); |
| 85 |
S2N_ERROR_IF(size_of_version_list != s2n_stuffer_data_available(extension), S2N_ERR_BAD_MESSAGE); |
| 86 |
S2N_ERROR_IF(size_of_version_list % S2N_TLS_PROTOCOL_VERSION_LEN != 0, S2N_ERR_BAD_MESSAGE); |
| 87 |
|
| 88 |
uint8_t client_protocol_version = s2n_unknown_protocol_version; |
| 89 |
uint8_t actual_protocol_version = s2n_unknown_protocol_version; |
| 90 |
|
| 91 |
for (int i = 0; i < size_of_version_list; i += S2N_TLS_PROTOCOL_VERSION_LEN) { |
| 92 |
uint8_t client_version_parts[S2N_TLS_PROTOCOL_VERSION_LEN]; |
| 93 |
POSIX_GUARD(s2n_stuffer_read_bytes(extension, client_version_parts, S2N_TLS_PROTOCOL_VERSION_LEN)); |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
if (client_version_parts[0] != 3 || client_version_parts[1] > 4) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
uint16_t client_version = (client_version_parts[0] * 10) + client_version_parts[1]; |
| 103 |
|
| 104 |
client_protocol_version = S2N_MAX(client_version, client_protocol_version); |
| 105 |
|
| 106 |
if (client_version > highest_supported_version) { |
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
if (client_version < minimum_supported_version) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
actual_protocol_version = S2N_MAX(client_version, actual_protocol_version); |
| 117 |
} |
| 118 |
|
| 119 |
*client_protocol_version_out = client_protocol_version; |
| 120 |
*actual_protocol_version_out = actual_protocol_version; |
| 121 |
|
| 122 |
return S2N_SUCCESS; |
| 123 |
} |
| 124 |
|
| 125 |
static S2N_RESULT s2n_client_supported_versions_recv_impl(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 126 |
{ |
| 127 |
RESULT_ENSURE_REF(conn); |
| 128 |
RESULT_ENSURE_REF(extension); |
| 129 |
|
| 130 |
RESULT_GUARD_POSIX(s2n_extensions_client_supported_versions_process(conn, extension, &conn->client_protocol_version, |
| 131 |
&conn->actual_protocol_version)); |
| 132 |
|
| 133 |
RESULT_ENSURE(conn->client_protocol_version != s2n_unknown_protocol_version, S2N_ERR_UNKNOWN_PROTOCOL_VERSION); |
| 134 |
RESULT_ENSURE(conn->actual_protocol_version != s2n_unknown_protocol_version, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 135 |
|
| 136 |
return S2N_RESULT_OK; |
| 137 |
} |
| 138 |
|
| 139 |
static int s2n_client_supported_versions_recv(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 140 |
{ |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
if (s2n_connection_get_protocol_version(conn) < S2N_TLS13) { |
| 155 |
return S2N_SUCCESS; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
bool is_hrr_handshake = s2n_is_hello_retry_handshake(conn); |
| 164 |
|
| 165 |
s2n_result result = s2n_client_supported_versions_recv_impl(conn, extension); |
| 166 |
if (s2n_result_is_error(result)) { |
| 167 |
conn->client_protocol_version = s2n_unknown_protocol_version; |
| 168 |
conn->actual_protocol_version = s2n_unknown_protocol_version; |
| 169 |
|
| 170 |
s2n_queue_reader_unsupported_protocol_version_alert(conn); |
| 171 |
POSIX_ENSURE(s2n_errno != S2N_ERR_SAFETY, S2N_ERR_BAD_MESSAGE); |
| 172 |
} |
| 173 |
POSIX_GUARD_RESULT(result); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
if (is_hrr_handshake && conn->handshake.message_number > 0) { |
| 179 |
POSIX_ENSURE(conn->client_protocol_version == S2N_TLS13, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 180 |
POSIX_ENSURE(conn->actual_protocol_version == S2N_TLS13, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 181 |
} |
| 182 |
|
| 183 |
return S2N_SUCCESS; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
int s2n_extensions_client_supported_versions_size(struct s2n_connection *conn) |
| 189 |
{ |
| 190 |
uint8_t minimum_supported_version = s2n_unknown_protocol_version; |
| 191 |
POSIX_GUARD_RESULT(s2n_connection_get_minimum_supported_version(conn, &minimum_supported_version)); |
| 192 |
uint8_t highest_supported_version = conn->client_protocol_version; |
| 193 |
|
| 194 |
uint8_t version_list_length = highest_supported_version - minimum_supported_version + 1; |
| 195 |
|
| 196 |
return version_list_length * S2N_TLS_PROTOCOL_VERSION_LEN + 5; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
int s2n_extensions_client_supported_versions_recv(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 201 |
{ |
| 202 |
return s2n_extension_recv(&s2n_client_supported_versions_extension, conn, extension); |
| 203 |
} |