| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/extensions/s2n_server_alpn.h" |
| 17 |
|
| 18 |
#include "stuffer/s2n_stuffer.h" |
| 19 |
#include "tls/s2n_connection.h" |
| 20 |
#include "tls/s2n_tls.h" |
| 21 |
#include "utils/s2n_safety.h" |
| 22 |
|
| 23 |
bool s2n_server_alpn_should_send(struct s2n_connection *conn); |
| 24 |
static int s2n_alpn_send(struct s2n_connection *conn, struct s2n_stuffer *out); |
| 25 |
static int s2n_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension); |
| 26 |
|
| 27 |
const s2n_extension_type s2n_server_alpn_extension = { |
| 28 |
.iana_value = TLS_EXTENSION_ALPN, |
| 29 |
.is_response = true, |
| 30 |
.send = s2n_alpn_send, |
| 31 |
.recv = s2n_alpn_recv, |
| 32 |
.should_send = s2n_server_alpn_should_send, |
| 33 |
.if_missing = s2n_extension_noop_if_missing, |
| 34 |
}; |
| 35 |
|
| 36 |
bool s2n_server_alpn_should_send(struct s2n_connection *conn) |
| 37 |
{ |
| 38 |
return conn && strlen(conn->application_protocol) > 0; |
| 39 |
} |
| 40 |
|
| 41 |
static int s2n_alpn_send(struct s2n_connection *conn, struct s2n_stuffer *out) |
| 42 |
{ |
| 43 |
POSIX_ENSURE_REF(conn); |
| 44 |
const uint8_t application_protocol_len = strlen(conn->application_protocol); |
| 45 |
|
| 46 |
|
| 47 |
POSIX_GUARD(s2n_stuffer_write_uint16(out, application_protocol_len + sizeof(uint8_t))); |
| 48 |
|
| 49 |
|
| 50 |
POSIX_GUARD(s2n_stuffer_write_uint8(out, application_protocol_len)); |
| 51 |
POSIX_GUARD(s2n_stuffer_write_bytes(out, (uint8_t *) conn->application_protocol, application_protocol_len)); |
| 52 |
|
| 53 |
return S2N_SUCCESS; |
| 54 |
} |
| 55 |
|
| 56 |
static int s2n_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 57 |
{ |
| 58 |
POSIX_ENSURE_REF(conn); |
| 59 |
|
| 60 |
uint16_t size_of_all = 0; |
| 61 |
POSIX_GUARD(s2n_stuffer_read_uint16(extension, &size_of_all)); |
| 62 |
if (size_of_all > s2n_stuffer_data_available(extension) || size_of_all < 3) { |
| 63 |
|
| 64 |
return S2N_SUCCESS; |
| 65 |
} |
| 66 |
|
| 67 |
uint8_t protocol_len = 0; |
| 68 |
POSIX_GUARD(s2n_stuffer_read_uint8(extension, &protocol_len)); |
| 69 |
|
| 70 |
uint8_t *protocol = s2n_stuffer_raw_read(extension, protocol_len); |
| 71 |
POSIX_ENSURE_REF(protocol); |
| 72 |
|
| 73 |
|
| 74 |
POSIX_CHECKED_MEMCPY(conn->application_protocol, protocol, protocol_len); |
| 75 |
conn->application_protocol[protocol_len] = '\0'; |
| 76 |
|
| 77 |
return S2N_SUCCESS; |
| 78 |
} |