| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/extensions/s2n_client_alpn.h" |
| 17 |
|
| 18 |
#include <stdint.h> |
| 19 |
|
| 20 |
#include "tls/extensions/s2n_extension_type.h" |
| 21 |
#include "tls/s2n_protocol_preferences.h" |
| 22 |
#include "tls/s2n_tls.h" |
| 23 |
#include "tls/s2n_tls_parameters.h" |
| 24 |
#include "utils/s2n_safety.h" |
| 25 |
|
| 26 |
bool s2n_client_alpn_should_send(struct s2n_connection *conn); |
| 27 |
static int s2n_client_alpn_send(struct s2n_connection *conn, struct s2n_stuffer *out); |
| 28 |
static int s2n_client_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension); |
| 29 |
|
| 30 |
const s2n_extension_type s2n_client_alpn_extension = { |
| 31 |
.iana_value = TLS_EXTENSION_ALPN, |
| 32 |
.is_response = false, |
| 33 |
.send = s2n_client_alpn_send, |
| 34 |
.recv = s2n_client_alpn_recv, |
| 35 |
.should_send = s2n_client_alpn_should_send, |
| 36 |
.if_missing = s2n_extension_noop_if_missing, |
| 37 |
}; |
| 38 |
|
| 39 |
bool s2n_client_alpn_should_send(struct s2n_connection *conn) |
| 40 |
{ |
| 41 |
struct s2n_blob *client_app_protocols = NULL; |
| 42 |
|
| 43 |
return s2n_connection_get_protocol_preferences(conn, &client_app_protocols) == S2N_SUCCESS |
| 44 |
&& client_app_protocols->size != 0 && client_app_protocols->data != NULL; |
| 45 |
} |
| 46 |
|
| 47 |
static int s2n_client_alpn_send(struct s2n_connection *conn, struct s2n_stuffer *out) |
| 48 |
{ |
| 49 |
struct s2n_blob *client_app_protocols = NULL; |
| 50 |
POSIX_GUARD(s2n_connection_get_protocol_preferences(conn, &client_app_protocols)); |
| 51 |
POSIX_ENSURE_REF(client_app_protocols); |
| 52 |
|
| 53 |
POSIX_GUARD(s2n_stuffer_write_uint16(out, client_app_protocols->size)); |
| 54 |
POSIX_GUARD(s2n_stuffer_write(out, client_app_protocols)); |
| 55 |
|
| 56 |
return S2N_SUCCESS; |
| 57 |
} |
| 58 |
|
| 59 |
static int s2n_client_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 60 |
{ |
| 61 |
struct s2n_blob *supported_protocols = NULL; |
| 62 |
POSIX_GUARD(s2n_connection_get_protocol_preferences(conn, &supported_protocols)); |
| 63 |
POSIX_ENSURE_REF(supported_protocols); |
| 64 |
|
| 65 |
if (supported_protocols->size == 0) { |
| 66 |
|
| 67 |
return S2N_SUCCESS; |
| 68 |
} |
| 69 |
|
| 70 |
uint16_t wire_size = 0; |
| 71 |
POSIX_GUARD(s2n_stuffer_read_uint16(extension, &wire_size)); |
| 72 |
if (wire_size > s2n_stuffer_data_available(extension) || wire_size < 3) { |
| 73 |
|
| 74 |
return S2N_SUCCESS; |
| 75 |
} |
| 76 |
|
| 77 |
struct s2n_blob client_protocols = { 0 }; |
| 78 |
POSIX_GUARD(s2n_blob_init(&client_protocols, s2n_stuffer_raw_read(extension, wire_size), wire_size)); |
| 79 |
|
| 80 |
struct s2n_stuffer server_protocols = { 0 }; |
| 81 |
POSIX_GUARD(s2n_stuffer_init(&server_protocols, supported_protocols)); |
| 82 |
POSIX_GUARD(s2n_stuffer_skip_write(&server_protocols, supported_protocols->size)); |
| 83 |
|
| 84 |
POSIX_GUARD_RESULT(s2n_select_server_preference_protocol(conn, &server_protocols, &client_protocols)); |
| 85 |
|
| 86 |
return S2N_SUCCESS; |
| 87 |
} |