| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/extensions/s2n_client_psk.h" |
| 17 |
|
| 18 |
#include <stdint.h> |
| 19 |
|
| 20 |
#include "crypto/s2n_hash.h" |
| 21 |
#include "tls/s2n_psk.h" |
| 22 |
#include "tls/s2n_tls.h" |
| 23 |
#include "tls/s2n_tls_parameters.h" |
| 24 |
#include "utils/s2n_bitmap.h" |
| 25 |
#include "utils/s2n_safety.h" |
| 26 |
|
| 27 |
#define SIZE_OF_BINDER_SIZE sizeof(uint8_t) |
| 28 |
#define SIZE_OF_BINDER_LIST_SIZE sizeof(uint16_t) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#define MAX_REJECTED_TICKETS 3 |
| 34 |
|
| 35 |
static int s2n_client_psk_send(struct s2n_connection *conn, struct s2n_stuffer *out); |
| 36 |
static int s2n_client_psk_recv(struct s2n_connection *conn, struct s2n_stuffer *extension); |
| 37 |
static int s2n_client_psk_is_missing(struct s2n_connection *conn); |
| 38 |
|
| 39 |
const s2n_extension_type s2n_client_psk_extension = { |
| 40 |
.iana_value = TLS_EXTENSION_PRE_SHARED_KEY, |
| 41 |
.minimum_version = S2N_TLS13, |
| 42 |
.is_response = false, |
| 43 |
.send = s2n_client_psk_send, |
| 44 |
.recv = s2n_client_psk_recv, |
| 45 |
.should_send = s2n_client_psk_should_send, |
| 46 |
.if_missing = s2n_client_psk_is_missing, |
| 47 |
}; |
| 48 |
|
| 49 |
int s2n_client_psk_is_missing(struct s2n_connection *conn) |
| 50 |
{ |
| 51 |
POSIX_ENSURE_REF(conn); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
POSIX_ENSURE(conn->early_data_state != S2N_EARLY_DATA_REQUESTED, S2N_ERR_UNSUPPORTED_EXTENSION); |
| 63 |
return S2N_SUCCESS; |
| 64 |
} |
| 65 |
|
| 66 |
bool s2n_client_psk_should_send(struct s2n_connection *conn) |
| 67 |
{ |
| 68 |
if (!conn || !conn->secure) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
if (!s2n_is_hello_retry_handshake(conn)) { |
| 76 |
return conn->psk_params.psk_list.len > 0; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
for (size_t i = 0; i < conn->psk_params.psk_list.len; i++) { |
| 83 |
struct s2n_psk *psk = NULL; |
| 84 |
if (s2n_result_is_ok(s2n_array_get(&conn->psk_params.psk_list, i, (void **) &psk)) |
| 85 |
&& psk != NULL |
| 86 |
&& conn->secure->cipher_suite->prf_alg == psk->hmac_alg) { |
| 87 |
return true; |
| 88 |
} |
| 89 |
} |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
static S2N_RESULT s2n_generate_obfuscated_ticket_age(struct s2n_psk *psk, uint64_t current_time, uint32_t *output) |
| 102 |
{ |
| 103 |
RESULT_ENSURE_REF(psk); |
| 104 |
RESULT_ENSURE_MUT(output); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
if (psk->type == S2N_PSK_TYPE_EXTERNAL) { |
| 113 |
*output = 0; |
| 114 |
return S2N_RESULT_OK; |
| 115 |
} |
| 116 |
|
| 117 |
RESULT_ENSURE(current_time >= psk->ticket_issue_time, S2N_ERR_SAFETY); |
| 118 |
|
| 119 |
|
| 120 |
uint64_t ticket_age_in_nanos = current_time - psk->ticket_issue_time; |
| 121 |
|
| 122 |
|
| 123 |
uint64_t ticket_age_in_millis = ticket_age_in_nanos / ONE_MILLISEC_IN_NANOS; |
| 124 |
RESULT_ENSURE(ticket_age_in_millis <= UINT32_MAX, S2N_ERR_SAFETY); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
*output = ticket_age_in_millis + psk->ticket_age_add; |
| 129 |
|
| 130 |
return S2N_RESULT_OK; |
| 131 |
} |
| 132 |
|
| 133 |
static int s2n_client_psk_send(struct s2n_connection *conn, struct s2n_stuffer *out) |
| 134 |
{ |
| 135 |
POSIX_ENSURE_REF(conn); |
| 136 |
POSIX_ENSURE_REF(conn->secure); |
| 137 |
|
| 138 |
struct s2n_psk_parameters *psk_params = &conn->psk_params; |
| 139 |
struct s2n_array *psk_list = &psk_params->psk_list; |
| 140 |
|
| 141 |
struct s2n_stuffer_reservation identity_list_size; |
| 142 |
POSIX_GUARD(s2n_stuffer_reserve_uint16(out, &identity_list_size)); |
| 143 |
|
| 144 |
uint16_t binder_list_size = SIZE_OF_BINDER_LIST_SIZE; |
| 145 |
|
| 146 |
for (size_t i = 0; i < psk_list->len; i++) { |
| 147 |
struct s2n_psk *psk = NULL; |
| 148 |
POSIX_GUARD_RESULT(s2n_array_get(psk_list, i, (void **) &psk)); |
| 149 |
POSIX_ENSURE_REF(psk); |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
if (s2n_is_hello_retry_handshake(conn) && conn->secure->cipher_suite->prf_alg != psk->hmac_alg) { |
| 158 |
continue; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
POSIX_GUARD(s2n_stuffer_write_uint16(out, psk->identity.size)); |
| 163 |
POSIX_GUARD(s2n_stuffer_write(out, &psk->identity)); |
| 164 |
|
| 165 |
|
| 166 |
uint32_t obfuscated_ticket_age = 0; |
| 167 |
uint64_t current_time = 0; |
| 168 |
POSIX_GUARD_RESULT(s2n_config_wall_clock(conn->config, ¤t_time)); |
| 169 |
POSIX_GUARD_RESULT(s2n_generate_obfuscated_ticket_age(psk, current_time, &obfuscated_ticket_age)); |
| 170 |
POSIX_GUARD(s2n_stuffer_write_uint32(out, obfuscated_ticket_age)); |
| 171 |
|
| 172 |
|
| 173 |
uint8_t hash_size = 0; |
| 174 |
POSIX_GUARD(s2n_hmac_digest_size(psk->hmac_alg, &hash_size)); |
| 175 |
binder_list_size += hash_size + SIZE_OF_BINDER_SIZE; |
| 176 |
} |
| 177 |
|
| 178 |
POSIX_GUARD(s2n_stuffer_write_vector_size(&identity_list_size)); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
psk_params->binder_list_size = binder_list_size; |
| 186 |
POSIX_GUARD(s2n_stuffer_skip_write(out, binder_list_size)); |
| 187 |
|
| 188 |
return S2N_SUCCESS; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
static S2N_RESULT s2n_select_external_psk(struct s2n_connection *conn, struct s2n_offered_psk_list *client_identity_list) |
| 203 |
{ |
| 204 |
RESULT_ENSURE_REF(conn); |
| 205 |
RESULT_ENSURE_REF(client_identity_list); |
| 206 |
|
| 207 |
struct s2n_array *server_psks = &conn->psk_params.psk_list; |
| 208 |
conn->psk_params.chosen_psk = NULL; |
| 209 |
|
| 210 |
for (size_t i = 0; i < server_psks->len; i++) { |
| 211 |
struct s2n_psk *server_psk = NULL; |
| 212 |
RESULT_GUARD(s2n_array_get(server_psks, i, (void **) &server_psk)); |
| 213 |
RESULT_ENSURE_REF(server_psk); |
| 214 |
|
| 215 |
struct s2n_offered_psk client_psk = { 0 }; |
| 216 |
uint16_t wire_index = 0; |
| 217 |
|
| 218 |
RESULT_GUARD_POSIX(s2n_offered_psk_list_reread(client_identity_list)); |
| 219 |
while (s2n_offered_psk_list_has_next(client_identity_list)) { |
| 220 |
RESULT_GUARD_POSIX(s2n_offered_psk_list_next(client_identity_list, &client_psk)); |
| 221 |
uint16_t compare_size = S2N_MIN(client_psk.identity.size, server_psk->identity.size); |
| 222 |
if (s2n_constant_time_equals(client_psk.identity.data, server_psk->identity.data, compare_size) |
| 223 |
& (client_psk.identity.size == server_psk->identity.size) |
| 224 |
& (conn->psk_params.chosen_psk == NULL)) { |
| 225 |
conn->psk_params.chosen_psk = server_psk; |
| 226 |
conn->psk_params.chosen_psk_wire_index = wire_index; |
| 227 |
} |
| 228 |
wire_index++; |
| 229 |
}; |
| 230 |
} |
| 231 |
RESULT_ENSURE_REF(conn->psk_params.chosen_psk); |
| 232 |
return S2N_RESULT_OK; |
| 233 |
} |
| 234 |
|
| 235 |
static S2N_RESULT s2n_select_resumption_psk(struct s2n_connection *conn, struct s2n_offered_psk_list *client_identity_list) |
| 236 |
{ |
| 237 |
RESULT_ENSURE_REF(conn); |
| 238 |
RESULT_ENSURE_REF(client_identity_list); |
| 239 |
|
| 240 |
struct s2n_offered_psk client_psk = { 0 }; |
| 241 |
conn->psk_params.chosen_psk = NULL; |
| 242 |
|
| 243 |
uint8_t rejected_count = 0; |
| 244 |
while (s2n_offered_psk_list_has_next(client_identity_list) && (rejected_count < MAX_REJECTED_TICKETS)) { |
| 245 |
RESULT_GUARD_POSIX(s2n_offered_psk_list_next(client_identity_list, &client_psk)); |
| 246 |
|
| 247 |
if (s2n_offered_psk_list_choose_psk(client_identity_list, &client_psk) == S2N_SUCCESS) { |
| 248 |
return S2N_RESULT_OK; |
| 249 |
} |
| 250 |
rejected_count++; |
| 251 |
} |
| 252 |
|
| 253 |
RESULT_BAIL(S2N_ERR_INVALID_SESSION_TICKET); |
| 254 |
} |
| 255 |
|
| 256 |
static S2N_RESULT s2n_client_psk_recv_identity_list(struct s2n_connection *conn, struct s2n_stuffer *wire_identities_in) |
| 257 |
{ |
| 258 |
RESULT_ENSURE_REF(conn); |
| 259 |
RESULT_ENSURE_REF(conn->config); |
| 260 |
RESULT_ENSURE_REF(wire_identities_in); |
| 261 |
|
| 262 |
struct s2n_offered_psk_list identity_list = { |
| 263 |
.conn = conn, |
| 264 |
.wire_data = *wire_identities_in, |
| 265 |
}; |
| 266 |
|
| 267 |
if (conn->config->psk_selection_cb) { |
| 268 |
RESULT_GUARD_POSIX(conn->config->psk_selection_cb(conn, conn->config->psk_selection_ctx, &identity_list)); |
| 269 |
} else if (conn->psk_params.type == S2N_PSK_TYPE_EXTERNAL) { |
| 270 |
RESULT_GUARD(s2n_select_external_psk(conn, &identity_list)); |
| 271 |
} else if (conn->psk_params.type == S2N_PSK_TYPE_RESUMPTION) { |
| 272 |
RESULT_GUARD(s2n_select_resumption_psk(conn, &identity_list)); |
| 273 |
} |
| 274 |
|
| 275 |
RESULT_ENSURE_REF(conn->psk_params.chosen_psk); |
| 276 |
return S2N_RESULT_OK; |
| 277 |
} |
| 278 |
|
| 279 |
static S2N_RESULT s2n_client_psk_recv_binder_list(struct s2n_connection *conn, struct s2n_blob *partial_client_hello, |
| 280 |
struct s2n_stuffer *wire_binders_in) |
| 281 |
{ |
| 282 |
RESULT_ENSURE_REF(conn); |
| 283 |
RESULT_ENSURE_REF(wire_binders_in); |
| 284 |
|
| 285 |
uint16_t wire_index = 0; |
| 286 |
while (s2n_stuffer_data_available(wire_binders_in) > 0) { |
| 287 |
uint8_t wire_binder_size = 0; |
| 288 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(wire_binders_in, &wire_binder_size)); |
| 289 |
|
| 290 |
uint8_t *wire_binder_data = NULL; |
| 291 |
RESULT_ENSURE_REF(wire_binder_data = s2n_stuffer_raw_read(wire_binders_in, wire_binder_size)); |
| 292 |
|
| 293 |
struct s2n_blob wire_binder = { 0 }; |
| 294 |
RESULT_GUARD_POSIX(s2n_blob_init(&wire_binder, wire_binder_data, wire_binder_size)); |
| 295 |
|
| 296 |
if (wire_index == conn->psk_params.chosen_psk_wire_index) { |
| 297 |
RESULT_GUARD_POSIX(s2n_psk_verify_binder(conn, conn->psk_params.chosen_psk, |
| 298 |
partial_client_hello, &wire_binder)); |
| 299 |
return S2N_RESULT_OK; |
| 300 |
} |
| 301 |
wire_index++; |
| 302 |
} |
| 303 |
RESULT_BAIL(S2N_ERR_BAD_MESSAGE); |
| 304 |
} |
| 305 |
|
| 306 |
static S2N_RESULT s2n_client_psk_recv_identities(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 307 |
{ |
| 308 |
RESULT_ENSURE_REF(conn); |
| 309 |
|
| 310 |
uint16_t identity_list_size = 0; |
| 311 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(extension, &identity_list_size)); |
| 312 |
|
| 313 |
uint8_t *identity_list_data = NULL; |
| 314 |
RESULT_ENSURE_REF(identity_list_data = s2n_stuffer_raw_read(extension, identity_list_size)); |
| 315 |
|
| 316 |
struct s2n_blob identity_list_blob = { 0 }; |
| 317 |
RESULT_GUARD_POSIX(s2n_blob_init(&identity_list_blob, identity_list_data, identity_list_size)); |
| 318 |
|
| 319 |
struct s2n_stuffer identity_list = { 0 }; |
| 320 |
RESULT_GUARD_POSIX(s2n_stuffer_init(&identity_list, &identity_list_blob)); |
| 321 |
RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&identity_list, identity_list_blob.size)); |
| 322 |
|
| 323 |
return s2n_client_psk_recv_identity_list(conn, &identity_list); |
| 324 |
} |
| 325 |
|
| 326 |
static S2N_RESULT s2n_client_psk_recv_binders(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 327 |
{ |
| 328 |
RESULT_ENSURE_REF(conn); |
| 329 |
|
| 330 |
uint16_t binder_list_size = 0; |
| 331 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(extension, &binder_list_size)); |
| 332 |
|
| 333 |
uint8_t *binder_list_data = NULL; |
| 334 |
RESULT_ENSURE_REF(binder_list_data = s2n_stuffer_raw_read(extension, binder_list_size)); |
| 335 |
|
| 336 |
struct s2n_blob binder_list_blob = { 0 }; |
| 337 |
RESULT_GUARD_POSIX(s2n_blob_init(&binder_list_blob, binder_list_data, binder_list_size)); |
| 338 |
|
| 339 |
struct s2n_stuffer binder_list = { 0 }; |
| 340 |
RESULT_GUARD_POSIX(s2n_stuffer_init(&binder_list, &binder_list_blob)); |
| 341 |
RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&binder_list, binder_list_blob.size)); |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
struct s2n_blob partial_client_hello = { 0 }; |
| 346 |
const struct s2n_stuffer *client_hello = &conn->handshake.io; |
| 347 |
uint32_t binders_size = binder_list_blob.size + SIZE_OF_BINDER_LIST_SIZE; |
| 348 |
RESULT_ENSURE_GTE(client_hello->write_cursor, binders_size); |
| 349 |
uint32_t partial_client_hello_size = client_hello->write_cursor - binders_size; |
| 350 |
RESULT_GUARD_POSIX(s2n_blob_slice(&client_hello->blob, &partial_client_hello, 0, partial_client_hello_size)); |
| 351 |
|
| 352 |
return s2n_client_psk_recv_binder_list(conn, &partial_client_hello, &binder_list); |
| 353 |
} |
| 354 |
|
| 355 |
int s2n_client_psk_recv(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 356 |
{ |
| 357 |
POSIX_ENSURE_REF(conn); |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
s2n_extension_type_id psk_ext_id = 0; |
| 367 |
POSIX_GUARD(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_PRE_SHARED_KEY, &psk_ext_id)); |
| 368 |
POSIX_ENSURE_NE(conn->client_hello.extensions.count, 0); |
| 369 |
uint16_t last_wire_index = conn->client_hello.extensions.count - 1; |
| 370 |
uint16_t extension_wire_index = conn->client_hello.extensions.parsed_extensions[psk_ext_id].wire_index; |
| 371 |
POSIX_ENSURE(extension_wire_index == last_wire_index, S2N_ERR_UNSUPPORTED_EXTENSION); |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
s2n_extension_type_id psk_ke_mode_ext_id = 0; |
| 382 |
POSIX_GUARD(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_PSK_KEY_EXCHANGE_MODES, &psk_ke_mode_ext_id)); |
| 383 |
POSIX_ENSURE(S2N_CBIT_TEST(conn->extension_requests_received, psk_ke_mode_ext_id), S2N_ERR_MISSING_EXTENSION); |
| 384 |
|
| 385 |
if (conn->psk_params.psk_ke_mode == S2N_PSK_DHE_KE) { |
| 386 |
s2n_extension_type_id key_share_ext_id = 0; |
| 387 |
POSIX_GUARD(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_KEY_SHARE, &key_share_ext_id)); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
POSIX_ENSURE(S2N_CBIT_TEST(conn->extension_requests_received, key_share_ext_id), S2N_ERR_MISSING_EXTENSION); |
| 392 |
} else { |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
return S2N_SUCCESS; |
| 397 |
} |
| 398 |
|
| 399 |
if (s2n_result_is_error(s2n_client_psk_recv_identities(conn, extension))) { |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
conn->psk_params.chosen_psk = NULL; |
| 406 |
} |
| 407 |
|
| 408 |
if (conn->psk_params.chosen_psk) { |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
POSIX_GUARD_RESULT(s2n_client_psk_recv_binders(conn, extension)); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
return S2N_SUCCESS; |
| 421 |
} |