| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/extensions/s2n_npn.h" |
| 17 |
|
| 18 |
#include "tls/extensions/s2n_client_alpn.h" |
| 19 |
#include "tls/extensions/s2n_server_alpn.h" |
| 20 |
#include "tls/s2n_protocol_preferences.h" |
| 21 |
#include "tls/s2n_tls.h" |
| 22 |
#include "tls/s2n_tls_parameters.h" |
| 23 |
#include "utils/s2n_safety.h" |
| 24 |
|
| 25 |
bool s2n_npn_should_send(struct s2n_connection *conn) |
| 26 |
{ |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
return s2n_client_alpn_should_send(conn) && conn->config->npn_supported && !s2n_handshake_is_renegotiation(conn); |
| 34 |
} |
| 35 |
|
| 36 |
int s2n_client_npn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 37 |
{ |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
if (s2n_npn_should_send(conn) && !s2n_server_alpn_should_send(conn)) { |
| 42 |
conn->npn_negotiated = true; |
| 43 |
} |
| 44 |
|
| 45 |
return S2N_SUCCESS; |
| 46 |
} |
| 47 |
|
| 48 |
const s2n_extension_type s2n_client_npn_extension = { |
| 49 |
.iana_value = TLS_EXTENSION_NPN, |
| 50 |
.is_response = false, |
| 51 |
.send = s2n_extension_send_noop, |
| 52 |
.recv = s2n_client_npn_recv, |
| 53 |
.should_send = s2n_npn_should_send, |
| 54 |
.if_missing = s2n_extension_noop_if_missing, |
| 55 |
}; |
| 56 |
|
| 57 |
bool s2n_server_npn_should_send(struct s2n_connection *conn) |
| 58 |
{ |
| 59 |
return conn->npn_negotiated; |
| 60 |
} |
| 61 |
|
| 62 |
int s2n_server_npn_send(struct s2n_connection *conn, struct s2n_stuffer *out) |
| 63 |
{ |
| 64 |
struct s2n_blob *app_protocols = NULL; |
| 65 |
POSIX_GUARD(s2n_connection_get_protocol_preferences(conn, &app_protocols)); |
| 66 |
POSIX_ENSURE_REF(app_protocols); |
| 67 |
|
| 68 |
POSIX_GUARD(s2n_stuffer_write(out, app_protocols)); |
| 69 |
|
| 70 |
return S2N_SUCCESS; |
| 71 |
} |
| 72 |
|
| 73 |
int s2n_server_npn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension) |
| 74 |
{ |
| 75 |
struct s2n_blob *supported_protocols = NULL; |
| 76 |
POSIX_GUARD(s2n_connection_get_protocol_preferences(conn, &supported_protocols)); |
| 77 |
POSIX_ENSURE_REF(supported_protocols); |
| 78 |
|
| 79 |
if (supported_protocols->size == 0) { |
| 80 |
|
| 81 |
return S2N_SUCCESS; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
if (s2n_stuffer_data_available(extension)) { |
| 91 |
POSIX_GUARD_RESULT(s2n_select_server_preference_protocol(conn, extension, supported_protocols)); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
if (s2n_get_application_protocol(conn) == NULL) { |
| 101 |
struct s2n_stuffer stuffer = { 0 }; |
| 102 |
POSIX_GUARD(s2n_stuffer_init(&stuffer, supported_protocols)); |
| 103 |
POSIX_GUARD(s2n_stuffer_skip_write(&stuffer, supported_protocols->size)); |
| 104 |
struct s2n_blob protocol = { 0 }; |
| 105 |
POSIX_GUARD_RESULT(s2n_protocol_preferences_read(&stuffer, &protocol)); |
| 106 |
|
| 107 |
POSIX_ENSURE_LT(protocol.size, sizeof(conn->application_protocol)); |
| 108 |
POSIX_CHECKED_MEMCPY(conn->application_protocol, protocol.data, protocol.size); |
| 109 |
conn->application_protocol[protocol.size] = '\0'; |
| 110 |
} |
| 111 |
|
| 112 |
conn->npn_negotiated = true; |
| 113 |
|
| 114 |
return S2N_SUCCESS; |
| 115 |
} |
| 116 |
|
| 117 |
const s2n_extension_type s2n_server_npn_extension = { |
| 118 |
.iana_value = TLS_EXTENSION_NPN, |
| 119 |
.is_response = true, |
| 120 |
.send = s2n_server_npn_send, |
| 121 |
.recv = s2n_server_npn_recv, |
| 122 |
.should_send = s2n_server_npn_should_send, |
| 123 |
.if_missing = s2n_extension_noop_if_missing, |
| 124 |
}; |