| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "error/s2n_errno.h" |
| 17 |
#include "tls/s2n_connection.h" |
| 18 |
#include "utils/s2n_safety.h" |
| 19 |
|
| 20 |
S2N_RESULT s2n_protocol_preferences_read(struct s2n_stuffer *protocol_preferences, struct s2n_blob *protocol) |
| 21 |
{ |
| 22 |
RESULT_ENSURE_REF(protocol_preferences); |
| 23 |
RESULT_ENSURE_REF(protocol); |
| 24 |
|
| 25 |
uint8_t length = 0; |
| 26 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(protocol_preferences, &length)); |
| 27 |
RESULT_ENSURE_GT(length, 0); |
| 28 |
|
| 29 |
uint8_t *data = s2n_stuffer_raw_read(protocol_preferences, length); |
| 30 |
RESULT_ENSURE_REF(data); |
| 31 |
|
| 32 |
RESULT_GUARD_POSIX(s2n_blob_init(protocol, data, length)); |
| 33 |
return S2N_RESULT_OK; |
| 34 |
} |
| 35 |
|
| 36 |
S2N_RESULT s2n_protocol_preferences_contain(struct s2n_blob *protocol_preferences, struct s2n_blob *protocol, bool *contains) |
| 37 |
{ |
| 38 |
RESULT_ENSURE_REF(contains); |
| 39 |
*contains = false; |
| 40 |
RESULT_ENSURE_REF(protocol_preferences); |
| 41 |
RESULT_ENSURE_REF(protocol); |
| 42 |
|
| 43 |
struct s2n_stuffer app_protocols_stuffer = { 0 }; |
| 44 |
RESULT_GUARD_POSIX(s2n_stuffer_init(&app_protocols_stuffer, protocol_preferences)); |
| 45 |
RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&app_protocols_stuffer, protocol_preferences->size)); |
| 46 |
|
| 47 |
while (s2n_stuffer_data_available(&app_protocols_stuffer) > 0) { |
| 48 |
struct s2n_blob match_against = { 0 }; |
| 49 |
RESULT_GUARD(s2n_protocol_preferences_read(&app_protocols_stuffer, &match_against)); |
| 50 |
|
| 51 |
if (match_against.size == protocol->size && memcmp(match_against.data, protocol->data, protocol->size) == 0) { |
| 52 |
*contains = true; |
| 53 |
return S2N_RESULT_OK; |
| 54 |
} |
| 55 |
} |
| 56 |
return S2N_RESULT_OK; |
| 57 |
} |
| 58 |
|
| 59 |
S2N_RESULT s2n_protocol_preferences_append(struct s2n_blob *application_protocols, const uint8_t *protocol, uint8_t protocol_len) |
| 60 |
{ |
| 61 |
RESULT_ENSURE_MUT(application_protocols); |
| 62 |
RESULT_ENSURE_REF(protocol); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
RESULT_ENSURE(protocol_len != 0, S2N_ERR_INVALID_APPLICATION_PROTOCOL); |
| 70 |
|
| 71 |
uint32_t prev_len = application_protocols->size; |
| 72 |
uint32_t new_len = prev_len + 1 + protocol_len; |
| 73 |
RESULT_ENSURE(new_len <= UINT16_MAX, S2N_ERR_INVALID_APPLICATION_PROTOCOL); |
| 74 |
|
| 75 |
RESULT_GUARD_POSIX(s2n_realloc(application_protocols, new_len)); |
| 76 |
|
| 77 |
struct s2n_stuffer protocol_stuffer = { 0 }; |
| 78 |
RESULT_GUARD_POSIX(s2n_stuffer_init(&protocol_stuffer, application_protocols)); |
| 79 |
RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&protocol_stuffer, prev_len)); |
| 80 |
RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&protocol_stuffer, protocol_len)); |
| 81 |
RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&protocol_stuffer, protocol, protocol_len)); |
| 82 |
|
| 83 |
return S2N_RESULT_OK; |
| 84 |
} |
| 85 |
|
| 86 |
S2N_RESULT s2n_protocol_preferences_set(struct s2n_blob *application_protocols, const char *const *protocols, int protocol_count) |
| 87 |
{ |
| 88 |
RESULT_ENSURE_MUT(application_protocols); |
| 89 |
|
| 90 |
|
| 91 |
if (protocols == NULL || protocol_count == 0) { |
| 92 |
RESULT_GUARD_POSIX(s2n_free(application_protocols)); |
| 93 |
return S2N_RESULT_OK; |
| 94 |
} |
| 95 |
|
| 96 |
DEFER_CLEANUP(struct s2n_blob new_protocols = { 0 }, s2n_free); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
RESULT_GUARD_POSIX(s2n_realloc(&new_protocols, protocol_count * 8)); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
RESULT_GUARD_POSIX(s2n_realloc(&new_protocols, 0)); |
| 110 |
RESULT_ENSURE_GTE(protocol_count, 0); |
| 111 |
|
| 112 |
for (size_t i = 0; i < (size_t) protocol_count; i++) { |
| 113 |
const uint8_t *protocol = (const uint8_t *) protocols[i]; |
| 114 |
size_t length = strlen(protocols[i]); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
RESULT_ENSURE(length < 256, S2N_ERR_INVALID_APPLICATION_PROTOCOL); |
| 122 |
|
| 123 |
RESULT_GUARD(s2n_protocol_preferences_append(&new_protocols, protocol, (uint8_t) length)); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
RESULT_GUARD_POSIX(s2n_free(application_protocols)); |
| 128 |
|
| 129 |
|
| 130 |
*application_protocols = new_protocols; |
| 131 |
|
| 132 |
ZERO_TO_DISABLE_DEFER_CLEANUP(new_protocols); |
| 133 |
|
| 134 |
return S2N_RESULT_OK; |
| 135 |
} |
| 136 |
|
| 137 |
S2N_RESULT s2n_select_server_preference_protocol(struct s2n_connection *conn, struct s2n_stuffer *server_list, |
| 138 |
struct s2n_blob *client_list) |
| 139 |
{ |
| 140 |
RESULT_ENSURE_REF(conn); |
| 141 |
RESULT_ENSURE_REF(server_list); |
| 142 |
RESULT_ENSURE_REF(client_list); |
| 143 |
|
| 144 |
while (s2n_stuffer_data_available(server_list) > 0) { |
| 145 |
struct s2n_blob protocol = { 0 }; |
| 146 |
RESULT_ENSURE_OK(s2n_protocol_preferences_read(server_list, &protocol), S2N_ERR_BAD_MESSAGE); |
| 147 |
|
| 148 |
bool match_found = false; |
| 149 |
RESULT_ENSURE_OK(s2n_protocol_preferences_contain(client_list, &protocol, &match_found), S2N_ERR_BAD_MESSAGE); |
| 150 |
|
| 151 |
if (match_found) { |
| 152 |
RESULT_ENSURE_LT(protocol.size, sizeof(conn->application_protocol)); |
| 153 |
RESULT_CHECKED_MEMCPY(conn->application_protocol, protocol.data, protocol.size); |
| 154 |
conn->application_protocol[protocol.size] = '\0'; |
| 155 |
return S2N_RESULT_OK; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return S2N_RESULT_OK; |
| 160 |
} |
| 161 |
|
| 162 |
int s2n_config_set_protocol_preferences(struct s2n_config *config, const char *const *protocols, int protocol_count) |
| 163 |
{ |
| 164 |
POSIX_ENSURE_REF(config); |
| 165 |
POSIX_GUARD_RESULT(s2n_protocol_preferences_set(&config->application_protocols, protocols, protocol_count)); |
| 166 |
return S2N_SUCCESS; |
| 167 |
} |
| 168 |
|
| 169 |
int s2n_config_append_protocol_preference(struct s2n_config *config, const uint8_t *protocol, uint8_t protocol_len) |
| 170 |
{ |
| 171 |
POSIX_ENSURE_REF(config); |
| 172 |
POSIX_GUARD_RESULT(s2n_protocol_preferences_append(&config->application_protocols, protocol, protocol_len)); |
| 173 |
return S2N_SUCCESS; |
| 174 |
} |
| 175 |
|
| 176 |
int s2n_connection_set_protocol_preferences(struct s2n_connection *conn, const char *const *protocols, int protocol_count) |
| 177 |
{ |
| 178 |
POSIX_ENSURE_REF(conn); |
| 179 |
POSIX_GUARD_RESULT(s2n_protocol_preferences_set(&conn->application_protocols_overridden, protocols, protocol_count)); |
| 180 |
return S2N_SUCCESS; |
| 181 |
} |
| 182 |
|
| 183 |
int s2n_connection_append_protocol_preference(struct s2n_connection *conn, const uint8_t *protocol, uint8_t protocol_len) |
| 184 |
{ |
| 185 |
POSIX_ENSURE_REF(conn); |
| 186 |
POSIX_GUARD_RESULT(s2n_protocol_preferences_append(&conn->application_protocols_overridden, protocol, protocol_len)); |
| 187 |
return S2N_SUCCESS; |
| 188 |
} |